-
Notifications
You must be signed in to change notification settings - Fork 479
/
aiken_import.inc.php
executable file
·390 lines (356 loc) · 14.5 KB
/
aiken_import.inc.php
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/* For licensing terms, see /license.txt */
/**
* Library for the import of Aiken format.
*
* @author claro team <cvs@claroline.net>
* @author Guillaume Lederer <guillaume@claroline.net>
* @author César Perales <cesar.perales@gmail.com> Parse function for Aiken format
*
* @package chamilo.exercise
*/
/**
* This function displays the form for import of the zip file with qti2.
*
* @param string Report message to show in case of error
*/
function aiken_display_form()
{
$name_tools = get_lang('ImportAikenQuiz');
$form = '<div class="actions">';
$form .= '<a href="exercise.php?show=test&'.api_get_cidreq().'">'.
Display::return_icon(
'back.png',
get_lang('BackToExercisesList'),
'',
ICON_SIZE_MEDIUM
).'</a>';
$form .= '</div>';
$form_validator = new FormValidator(
'aiken_upload',
'post',
api_get_self()."?".api_get_cidreq(),
null,
['enctype' => 'multipart/form-data']
);
$form_validator->addElement('header', $name_tools);
$form_validator->addElement('text', 'total_weight', get_lang('TotalWeight'));
$form_validator->addElement('file', 'userFile', get_lang('File'));
$form_validator->addButtonUpload(get_lang('Upload'), 'submit');
$form .= $form_validator->returnForm();
$form .= '<blockquote>'.get_lang('ImportAikenQuizExplanation').'<br /><pre>'.get_lang('ImportAikenQuizExplanationExample').'</pre></blockquote>';
echo $form;
}
/**
* Gets the uploaded file (from $_FILES) and unzip it to the given directory.
*
* @param string The directory where to do the work
* @param string The path of the temporary directory where the exercise was uploaded and unzipped
* @param string $baseWorkDir
* @param string $uploadPath
*
* @return bool True on success, false on failure
*/
function get_and_unzip_uploaded_exercise($baseWorkDir, $uploadPath)
{
$_course = api_get_course_info();
$_user = api_get_user_info();
// Check if the file is valid (not to big and exists)
if (!isset($_FILES['userFile']) || !is_uploaded_file($_FILES['userFile']['tmp_name'])) {
// upload failed
return false;
}
if (preg_match('/.zip$/i', $_FILES['userFile']['name']) &&
handle_uploaded_document(
$_course,
$_FILES['userFile'],
$baseWorkDir,
$uploadPath,
$_user['user_id'],
0,
null,
1,
'overwrite',
false
)
) {
if (!function_exists('gzopen')) {
return false;
}
// upload successful
return true;
} elseif (preg_match('/.txt/i', $_FILES['userFile']['name']) &&
handle_uploaded_document(
$_course,
$_FILES['userFile'],
$baseWorkDir,
$uploadPath,
$_user['user_id'],
0,
null,
0,
'overwrite',
false
)
) {
return true;
} else {
return false;
}
}
/**
* Main function to import the Aiken exercise.
*
* @param string $file
*
* @return mixed True on success, error message on failure
*/
function aiken_import_exercise($file)
{
$archive_path = api_get_path(SYS_ARCHIVE_PATH).'aiken/';
$baseWorkDir = $archive_path;
if (!is_dir($baseWorkDir)) {
mkdir($baseWorkDir, api_get_permissions_for_new_directories(), true);
}
$uploadPath = 'aiken_'.api_get_unique_id().'/';
// set some default values for the new exercise
$exercise_info = [];
$exercise_info['name'] = preg_replace('/.(zip|txt)$/i', '', $file);
$exercise_info['question'] = [];
// if file is not a .zip, then we cancel all
if (!preg_match('/.(zip|txt)$/i', $file)) {
return 'YouMustUploadAZipOrTxtFile';
}
// unzip the uploaded file in a tmp directory
if (preg_match('/.(zip|txt)$/i', $file)) {
if (!get_and_unzip_uploaded_exercise($baseWorkDir, $uploadPath)) {
return 'ThereWasAProblemWithYourFile';
}
}
// find the different manifests for each question and parse them
$exerciseHandle = opendir($baseWorkDir.$uploadPath);
$file_found = false;
$operation = false;
$result = false;
// Parse every subdirectory to search txt question files
while (false !== ($file = readdir($exerciseHandle))) {
if (is_dir($baseWorkDir.'/'.$uploadPath.$file) && $file != "." && $file != "..") {
//find each manifest for each question repository found
$questionHandle = opendir($baseWorkDir.'/'.$uploadPath.$file);
while (false !== ($questionFile = readdir($questionHandle))) {
if (preg_match('/.txt$/i', $questionFile)) {
$result = aiken_parse_file(
$exercise_info,
$baseWorkDir,
$file,
$questionFile
);
$file_found = true;
}
}
} elseif (preg_match('/.txt$/i', $file)) {
$result = aiken_parse_file($exercise_info, $baseWorkDir.$uploadPath, '', $file);
$file_found = true;
}
}
if (!$file_found) {
$result = 'NoTxtFileFoundInTheZip';
}
if ($result !== true) {
return $result;
}
// 1. Create exercise
$exercise = new Exercise();
$exercise->exercise = $exercise_info['name'];
$exercise->save();
$last_exercise_id = $exercise->selectId();
if (!empty($last_exercise_id)) {
// For each question found...
foreach ($exercise_info['question'] as $key => $question_array) {
//2.create question
$question = new Aiken2Question();
$question->type = $question_array['type'];
$question->setAnswer();
$question->updateTitle($question_array['title']);
if (isset($question_array['description'])) {
$question->updateDescription($question_array['description']);
}
$type = $question->selectType();
$question->type = constant($type);
$question->save($exercise);
$last_question_id = $question->selectId();
//3. Create answer
$answer = new Answer($last_question_id);
$answer->new_nbrAnswers = count($question_array['answer']);
$max_score = 0;
$scoreFromFile = 0;
if (isset($question_array['score']) && !empty($question_array['score'])) {
$scoreFromFile = $question_array['score'];
}
foreach ($question_array['answer'] as $key => $answers) {
$key++;
$answer->new_answer[$key] = $answers['value'];
$answer->new_position[$key] = $key;
// Correct answers ...
if (in_array($key, $question_array['correct_answers'])) {
$answer->new_correct[$key] = 1;
if (isset($question_array['feedback'])) {
$answer->new_comment[$key] = $question_array['feedback'];
}
} else {
$answer->new_correct[$key] = 0;
}
if (isset($question_array['weighting'][$key - 1])) {
$answer->new_weighting[$key] = $question_array['weighting'][$key - 1];
$max_score += $question_array['weighting'][$key - 1];
}
if (!empty($scoreFromFile) && $answer->new_correct[$key]) {
$answer->new_weighting[$key] = $scoreFromFile;
}
}
if (!empty($scoreFromFile)) {
$max_score = $scoreFromFile;
}
$answer->save();
// Now that we know the question score, set it!
$question->updateWeighting($max_score);
$question->save($exercise);
}
// Delete the temp dir where the exercise was unzipped
my_delete($baseWorkDir.$uploadPath);
$operation = $last_exercise_id;
}
return $operation;
}
/**
* Parses an Aiken file and builds an array of exercise + questions to be
* imported by the import_exercise() function.
*
* @param array The reference to the array in which to store the questions
* @param string Path to the directory with the file to be parsed (without final /)
* @param string Name of the last directory part for the file (without /)
* @param string Name of the file to be parsed (including extension)
* @param string $exercisePath
* @param string $file
* @param string $questionFile
*
* @return string|bool True on success, error message on error
* @assert ('','','') === false
*/
function aiken_parse_file(&$exercise_info, $exercisePath, $file, $questionFile)
{
$questionTempDir = $exercisePath.'/'.$file.'/';
$questionFilePath = $questionTempDir.$questionFile;
if (!is_file($questionFilePath)) {
return 'FileNotFound';
}
$data = file($questionFilePath);
$question_index = 0;
$answers_array = [];
$new_question = true;
foreach ($data as $line => $info) {
if ($question_index > 0 && $new_question == true && preg_match('/^(\r)?\n/', $info)) {
// double empty line
continue;
}
$new_question = false;
//make sure it is transformed from iso-8859-1 to utf-8 if in that form
if (!mb_check_encoding($info, 'utf-8') && mb_check_encoding($info, 'iso-8859-1')) {
$info = utf8_encode($info);
}
$exercise_info['question'][$question_index]['type'] = 'MCUA';
if (preg_match('/^([A-Za-z])(\)|\.)\s(.*)/', $info, $matches)) {
//adding one of the possible answers
$exercise_info['question'][$question_index]['answer'][]['value'] = $matches[3];
$answers_array[] = $matches[1];
} elseif (preg_match('/^ANSWER:\s?([A-Z])\s?/', $info, $matches)) {
//the correct answers
$correct_answer_index = array_search($matches[1], $answers_array);
$exercise_info['question'][$question_index]['correct_answers'][] = $correct_answer_index + 1;
//weight for correct answer
$exercise_info['question'][$question_index]['weighting'][$correct_answer_index] = 1;
} elseif (preg_match('/^SCORE:\s?(.*)/', $info, $matches)) {
//the correct answers
$correct_answer_index = array_search($matches[1], $answers_array);
$exercise_info['question'][$question_index]['score'] = (float) $matches[1];
//weight for correct answer
//$exercise_info['question'][$question_index]['weighting'][$correct_answer_index] = 1;
} elseif (preg_match('/^ANSWER_EXPLANATION:\s?(.*)/', $info, $matches)) {
//Comment of correct answer
$correct_answer_index = array_search($matches[1], $answers_array);
$exercise_info['question'][$question_index]['feedback'] = $matches[1];
} elseif (preg_match('/^TEXTO_CORRECTA:\s?(.*)/', $info, $matches)) {
//Comment of correct answer (Spanish e-ducativa format)
$correct_answer_index = array_search($matches[1], $answers_array);
$exercise_info['question'][$question_index]['feedback'] = $matches[1];
} elseif (preg_match('/^T:\s?(.*)/', $info, $matches)) {
//Question Title
$correct_answer_index = array_search($matches[1], $answers_array);
$exercise_info['question'][$question_index]['title'] = $matches[1];
} elseif (preg_match('/^TAGS:\s?([A-Z])\s?/', $info, $matches)) {
//TAGS for chamilo >= 1.10
$exercise_info['question'][$question_index]['answer_tags'] = explode(',', $matches[1]);
} elseif (preg_match('/^ETIQUETAS:\s?([A-Z])\s?/', $info, $matches)) {
//TAGS for chamilo >= 1.10 (Spanish e-ducativa format)
$exercise_info['question'][$question_index]['answer_tags'] = explode(',', $matches[1]);
} elseif (preg_match('/^(\r)?\n/', $info)) {
//moving to next question (tolerate \r\n or just \n)
if (empty($exercise_info['question'][$question_index]['correct_answers'])) {
error_log('Aiken: Error in question index '.$question_index.': no correct answer defined');
return 'ExerciseAikenErrorNoCorrectAnswerDefined';
}
if (empty($exercise_info['question'][$question_index]['answer'])) {
error_log('Aiken: Error in question index '.$question_index.': no answer option given');
return 'ExerciseAikenErrorNoAnswerOptionGiven';
}
$question_index++;
//emptying answers array when moving to next question
$answers_array = [];
$new_question = true;
} else {
if (empty($exercise_info['question'][$question_index]['title'])) {
if (strlen($info) < 100) {
$exercise_info['question'][$question_index]['title'] = $info;
} else {
//Question itself (use a 100-chars long title and a larger description)
$exercise_info['question'][$question_index]['title'] = trim(substr($info, 0, 100)).'...';
$exercise_info['question'][$question_index]['description'] = $info;
}
} else {
$exercise_info['question'][$question_index]['description'] = $info;
}
}
}
$total_questions = count($exercise_info['question']);
$total_weight = (!empty($_POST['total_weight'])) ? intval($_POST['total_weight']) : 20;
foreach ($exercise_info['question'] as $key => $question) {
$exercise_info['question'][$key]['weighting'][current(array_keys($exercise_info['question'][$key]['weighting']))] = $total_weight / $total_questions;
}
return true;
}
/**
* Imports the zip file.
*
* @param array $array_file ($_FILES)
*
* @return bool
*/
function aiken_import_file($array_file)
{
$unzip = 0;
$process = process_uploaded_file($array_file, false);
if (preg_match('/\.(zip|txt)$/i', $array_file['name'])) {
// if it's a zip, allow zip upload
$unzip = 1;
}
if ($process && $unzip == 1) {
$imported = aiken_import_exercise($array_file['name']);
if (is_numeric($imported) && !empty($imported)) {
Display::addFlash(Display::return_message(get_lang('Uploaded')));
return $imported;
} else {
Display::addFlash(Display::return_message(get_lang($imported), 'error'));
return false;
}
}
}