-
Notifications
You must be signed in to change notification settings - Fork 0
/
eparse.module
261 lines (214 loc) · 7.95 KB
/
eparse.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
function eparse_menu() {
// This is the minimum information you can provide for a menu item. This menu
// item will be created in the default menu, usually Navigation.
$items['eparse/index'] = array(
'title' => 'Page Example',
'page callback' => 'eparse_form',
'access callback' => TRUE,
'expanded' => TRUE
);
$items['eparse/%/view'] = array(
'title' => 'Page Example',
'page callback' => 'eparse_view',
'page arguments' => array(1),
'access callback' => TRUE,
'expanded' => TRUE
);
return $items;
}
function eparse_form() {
return drupal_get_form('eparse_my_form');
}
function eparse_my_form($form_state) {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name')
);
$form['exam'] = array(
'#type' => 'textarea',
'#title' => t('Exam')
);
$form['answers'] = array(
'#type' => 'textarea',
'#title' => t('Answers')
);
// Adds a simple submit button that refreshes the form and clears its contents -- this is the default behavior for forms.
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit'
);
return $form;
}
function eparse_my_form_submit($form, &$form_state){
$nid = db_insert('eparser') // Table name no longer needs {}
->fields(array(
'name' => $form_state['values']['name'],
'answers' => $form_state['values']['answers'],
'html' => $form_state['values']['exam']
))
->execute();
$form_state['redirect'] = array(
'eparse/'. $nid . '/view'
);
}
function eparse_view($id){
$questions = array();
$content ="";
$result = db_select('eparser', 'n')
->fields('n')
->condition('id', $id,'=')
->execute()
->fetchAssoc();
//Create node
$quiz = new stdClass();
$quiz->title = $result['name'];
$quiz->type = "quiz";
$quiz->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
$quiz->uid = 1;
$quiz->status = 1; //(1 or 0): published or not
$quiz->promote = 0; //(1 or 0): promoted to front page
$quiz->comment = 0; //2 = comments on, 1 = comments of
//$quiz = node_submit($quiz); // Prepare quiz for saving
//node_save($quiz);
//Find questions
$subject = ' qstart intro ' . $result['html'];
$pattern = '/0000([a-z0-9]{14})</';
// 1
$subject= preg_replace($pattern, "> altend qend $1 qstart intro <" ,$subject);
// 2
//$pattern = "/>(a\s{0,1}<)/ims"; old
$pattern = "/>(a\s{0,1}<\/b><br>\r\n\r\n.+\r\n\r\n)/imsU";
$subject= preg_replace($pattern, "> iend altstart <p>$1" ,$subject);
//clean q numbers
$subject = preg_replace('/-[0-9]{1,3}-/', '', $subject);
//clean Test preview as PDF!
$subject = str_replace('Test preview as PDF!', '', $subject);
//Find qstart and qend
$pattern = '/qstart(.+?)qend/ims';
preg_match_all($pattern, $subject, $matches);
//true alts without whitespace
$true_alts = array();
//counter
$c = 1;
//Questions:
foreach ($matches[1] as $key => $question) {
//find intro
preg_match('/intro(.+?)iend/ims', $question, $intro);
if (array_key_exists(0, $intro)){
$intro[1] = preg_replace('/>[0-9]{1,3}</', '', $intro[1]);
$intro[1] = trim(strip_tags($intro[1], '<img>'));
$intro[1] = preg_replace( '/\s{2}/', '', $intro[1]);
//find alternatives
preg_match('/altstart(.+?)altend/ims', $question, $alternatives);
//try to remove letters
$alternatives[1] = trim(strip_tags(preg_replace('/<.+?>[ABCDEFGH]{1}.?<.+?>/', '', $alternatives[1])));
$alternatives[1] = preg_replace('/^(.+?)$/msx', '---$1---', $alternatives[1]);
preg_match_all('/---(.+)---/U', $alternatives[1], $alternatives);
$i = 0;
if (array_key_exists(0, $alternatives)){
foreach ($alternatives[1] as $key2 => $value) {
if (preg_match('/[a-z0-9]+/i', $value, $out))
{
//amend img tags
$path = str_replace(' ', '-', $result['name']);
$intro[1] = str_replace('IMG src="', 'img src="/exam_images/' . $path . '/', $intro[1]);
$true_alts[$c]['title'] = $intro[1];
$true_alts[$c]['alternatives'][$i]['text'] = $value;
$true_alts[$c]['alternatives'][$i]['value'] = 0;
$i++;
}
}
}
$c++;
}
}
//Work out answers
$answers = $result['answers'];
$answers = str_replace(
array('A','B','C','D','E','F','G'),
array('0','1','2','3','4','5','6'),
$answers
);
//preg_match_all('/>([0-9]+)<.+\r\n\t.+>([0-9]{1})</msxU', $answers, $pairs); old
preg_match_all('/^([0-9]{1,3})<br>\r\n\r\n([0-9]{1})</msxU', $answers, $pairs);
$q_numbers = $pairs[1];
$q_answers = $pairs[2];
foreach ($q_numbers as $key => $value) {
# Find the correct answer
$answer = $q_answers[$key];
#Add the point
$true_alts["$value"]['alternatives']["$answer"]['value'] = 1;
}
// Now you Create nodes and Questions
// return dpm($true_alts);
//Temporary pretty
foreach ($true_alts as $key => $value) {
//short and full titles
$full_title = $value['title'];
$short_title = htmlspecialchars(strip_tags($value['title']), ENT_NOQUOTES, 'UTF-8');
$short_title = htmlspecialchars(substr($short_title, 0, 50), ENT_NOQUOTES, 'UTF-8');
//Create question node
$multichoice = new stdClass();
$multichoice->title = $short_title;
$multichoice->type = "multichoice";
$multichoice->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
$multichoice->add_directly['new'] = '';
$multichoice->uid = 1;
$multichoice->choice_multi = 0;
$multichoice->choice_random = 0;
$multichoice->choice_boolean = 0;
$multichoice->status = 1; //(1 or 0): published or not
$multichoice->promote = 0; //(1 or 0): promoted to front page
$multichoice->comment = 0; //2 = comments on, 1 = comments of
$multichoice->body[$multichoice->language][0]['value'] = $full_title;
$multichoice->body[$multichoice->language][0]['summary'] = $short_title;
$multichoice->body[$multichoice->language][0]['format'] = 'full_html';
//$multichoice = node_submit($multichoice); // Prepare multichoice for saving
//node_save($multichoice);
/*
//add question to quiz
//Add alternatives to db
$add_question_to_quiz = db_insert('quiz_node_relationship') // Table name no longer needs {}
->fields(array(
'parent_nid' => $quiz->nid,
'parent_vid' => $quiz->vid,
'child_nid' => $multichoice->nid,
'child_vid' => $multichoice->vid,
'question_status' => 1,
'weight' => $key,
'max_score' => 1,
))
->execute();
*/
//Display question
$content .= "<h3>$key: ". $value['title'] ."</h3>";
//ordered list
$content .= "<ol>";
//Display alternatives
foreach ($value['alternatives'] as $key => $value) {
/*
//Add alternatives to db
$add_db_alt = db_insert('quiz_multichoice_answers') // Table name no longer needs {}
->fields(array(
'answer' => htmlspecialchars($value['text'], ENT_NOQUOTES, 'UTF-8'),
'answer_format' => 'plain_text',
'feedback_if_chosen' => '',
'feedback_if_chosen_format' => 'full_html',
'feedback_if_not_chosen' => '',
'feedback_if_not_chosen_format' => 'full_html',
'score_if_chosen' => $value['value'],
'score_if_not_chosen' => '0',
'question_nid' => $multichoice->nid,
'question_vid' => $multichoice->vid
))
->execute();
*/
$content .= ($value['value']) ? "<li><b>" . $value['text'] . "</b></li>" : "<li>" . $value['text'] . "</li>";
}
//ordered list
$content .= "</ol>";
// $content .= "<p><a href=/node/$multichoice->nid/edit>EDIT</a></p>";
}
return $content;
}