1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Library for the import of Aiken format. |
7
|
|
|
* |
8
|
|
|
* @author claro team <[email protected]> |
9
|
|
|
* @author Guillaume Lederer <[email protected]> |
10
|
|
|
* @author César Perales <[email protected]> Parse function for Aiken format |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This function displays the form for import of the zip file with qti2. |
15
|
|
|
* |
16
|
|
|
* @param string Report message to show in case of error |
17
|
|
|
*/ |
18
|
|
|
function aiken_display_form() |
19
|
|
|
{ |
20
|
|
|
$name_tools = get_lang('ImportAikenQuiz'); |
21
|
|
|
$form = '<div class="actions">'; |
22
|
|
|
$form .= '<a href="exercise.php?show=test&'.api_get_cidreq().'">'. |
23
|
|
|
Display::return_icon( |
24
|
|
|
'back.png', |
25
|
|
|
get_lang('BackToExercisesList'), |
26
|
|
|
'', |
27
|
|
|
ICON_SIZE_MEDIUM |
28
|
|
|
).'</a>'; |
29
|
|
|
$form .= '</div>'; |
30
|
|
|
$form_validator = new FormValidator( |
31
|
|
|
'aiken_upload', |
32
|
|
|
'post', |
33
|
|
|
api_get_self()."?".api_get_cidreq(), |
34
|
|
|
null, |
35
|
|
|
['enctype' => 'multipart/form-data'] |
36
|
|
|
); |
37
|
|
|
$form_validator->addElement('header', $name_tools); |
38
|
|
|
$form_validator->addElement('text', 'total_weight', get_lang('TotalWeight')); |
39
|
|
|
$form_validator->addElement('file', 'userFile', get_lang('File')); |
40
|
|
|
$form_validator->addButtonUpload(get_lang('Upload'), 'submit'); |
41
|
|
|
$form .= $form_validator->returnForm(); |
42
|
|
|
$form .= '<blockquote>'.get_lang('ImportAikenQuizExplanation').'<br /><pre>'.get_lang('ImportAikenQuizExplanationExample').'</pre></blockquote>'; |
43
|
|
|
echo $form; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Generates Aiken format using AI APIs (supports multiple providers). |
48
|
|
|
* Requires plugin ai_helper to connect to the API. |
49
|
|
|
*/ |
50
|
|
|
function generateAikenForm() |
51
|
|
|
{ |
52
|
|
|
if (!('true' === api_get_plugin_setting('ai_helper', 'tool_enable') && 'true' === api_get_plugin_setting('ai_helper', 'tool_quiz_enable'))) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$plugin = AiHelperPlugin::create(); |
57
|
|
|
$availableApis = $plugin->getApiList(); |
58
|
|
|
|
59
|
|
|
$configuredApi = $plugin->get('api_name'); |
60
|
|
|
$hasSingleApi = count($availableApis) === 1 || isset($availableApis[$configuredApi]); |
61
|
|
|
|
62
|
|
|
$form = new FormValidator( |
63
|
|
|
'aiken_generate', |
64
|
|
|
'post', |
65
|
|
|
api_get_self()."?".api_get_cidreq(), |
66
|
|
|
null |
67
|
|
|
); |
68
|
|
|
$form->addElement('header', get_lang('AIQuestionsGenerator')); |
69
|
|
|
|
70
|
|
|
if ($hasSingleApi) { |
71
|
|
|
$apiName = $availableApis[$configuredApi] ?? $configuredApi; |
72
|
|
|
$form->addHtml('<div style="margin-bottom: 10px; font-size: 14px; color: #555;">' |
73
|
|
|
. sprintf(get_lang('UsingAIProviderX'), '<strong>'.htmlspecialchars($apiName).'</strong>').'</div>'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$form->addElement('text', 'quiz_name', get_lang('QuestionsTopic')); |
77
|
|
|
$form->addRule('quiz_name', get_lang('ThisFieldIsRequired'), 'required'); |
78
|
|
|
$form->addElement('number', 'nro_questions', get_lang('NumberOfQuestions')); |
79
|
|
|
$form->addRule('nro_questions', get_lang('ThisFieldIsRequired'), 'required'); |
80
|
|
|
|
81
|
|
|
$options = [ |
82
|
|
|
'multiple_choice' => get_lang('MultipleAnswer'), |
83
|
|
|
]; |
84
|
|
|
$form->addElement('select', 'question_type', get_lang('QuestionType'), $options); |
85
|
|
|
|
86
|
|
|
if (!$hasSingleApi) { |
87
|
|
|
$form->addElement( |
88
|
|
|
'select', |
89
|
|
|
'ai_provider', |
90
|
|
|
get_lang('AIProvider'), |
91
|
|
|
array_combine(array_keys($availableApis), array_keys($availableApis)) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$generateUrl = api_get_path(WEB_PLUGIN_PATH).'ai_helper/tool/answers.php'; |
96
|
|
|
$language = api_get_interface_language(); |
97
|
|
|
$form->addHtml('<script> |
98
|
|
|
$(function () { |
99
|
|
|
$("#aiken-area").hide(); |
100
|
|
|
$("#generate-aiken").on("click", function (e) { |
101
|
|
|
e.preventDefault(); |
102
|
|
|
e.stopPropagation(); |
103
|
|
|
|
104
|
|
|
var btnGenerate = $(this); |
105
|
|
|
var quizName = $("[name=\'quiz_name\']").val(); |
106
|
|
|
var nroQ = parseInt($("[name=\'nro_questions\']").val()); |
107
|
|
|
var qType = $("[name=\'question_type\']").val();' |
108
|
|
|
. (!$hasSingleApi ? 'var provider = $("[name=\'ai_provider\']").val();' : 'var provider = "' . $configuredApi . '";') . |
109
|
|
|
'var valid = (quizName != \'\' && nroQ > 0); |
110
|
|
|
if (valid) { |
111
|
|
|
btnGenerate.attr("disabled", true); |
112
|
|
|
btnGenerate.text("'.get_lang('PleaseWaitThisCouldTakeAWhile').'"); |
113
|
|
|
$("#textarea-aiken").text(""); |
114
|
|
|
$("#aiken-area").hide(); |
115
|
|
|
$.getJSON("'.$generateUrl.'", { |
116
|
|
|
"quiz_name": quizName, |
117
|
|
|
"nro_questions": nroQ, |
118
|
|
|
"question_type": qType, |
119
|
|
|
"language": "'.$language.'", |
120
|
|
|
"ai_provider": provider |
121
|
|
|
}).done(function (data) { |
122
|
|
|
btnGenerate.attr("disabled", false); |
123
|
|
|
btnGenerate.text("'.get_lang('Generate').'"); |
124
|
|
|
if (data.success && data.success == true) { |
125
|
|
|
$("#aiken-area").show(); |
126
|
|
|
$("#textarea-aiken").text(data.text); |
127
|
|
|
$("input[name=\'ai_total_weight\']").val(nroQ * qWeight); |
128
|
|
|
$("#textarea-aiken").focus(); |
129
|
|
|
} else { |
130
|
|
|
var errorMessage = "'.get_lang('NoSearchResults').'. '.get_lang('PleaseTryAgain').'"; |
131
|
|
|
if (data.text) { |
132
|
|
|
errorMessage = data.text; |
133
|
|
|
} |
134
|
|
|
alert(errorMessage); |
135
|
|
|
} |
136
|
|
|
}); |
137
|
|
|
} |
138
|
|
|
}); |
139
|
|
|
}); |
140
|
|
|
</script>'); |
141
|
|
|
|
142
|
|
|
$form->addButton( |
143
|
|
|
'generate_aiken_button', |
144
|
|
|
get_lang('Generate'), |
145
|
|
|
'', |
146
|
|
|
'default', |
147
|
|
|
'default', |
148
|
|
|
null, |
149
|
|
|
['id' => 'generate-aiken'] |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
$form->addHtml('<div id="aiken-area">'); |
153
|
|
|
$form->addElement( |
154
|
|
|
'textarea', |
155
|
|
|
'aiken_format', |
156
|
|
|
get_lang('Answers'), |
157
|
|
|
[ |
158
|
|
|
'id' => 'textarea-aiken', |
159
|
|
|
'style' => 'width: 100%; height: 250px;', |
160
|
|
|
] |
161
|
|
|
); |
162
|
|
|
$form->addElement('number', 'ai_total_weight', get_lang('TotalWeight')); |
163
|
|
|
$form->addButtonImport(get_lang('Import'), 'submit_aiken_generated'); |
164
|
|
|
$form->addHtml('</div>'); |
165
|
|
|
|
166
|
|
|
echo $form->returnForm(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Gets the uploaded file (from $_FILES) and unzip it to the given directory. |
171
|
|
|
* |
172
|
|
|
* @param string The directory where to do the work |
173
|
|
|
* @param string The path of the temporary directory where the exercise was uploaded and unzipped |
174
|
|
|
* @param string $baseWorkDir |
175
|
|
|
* @param string $uploadPath |
176
|
|
|
* |
177
|
|
|
* @return bool True on success, false on failure |
178
|
|
|
*/ |
179
|
|
|
function get_and_unzip_uploaded_exercise($baseWorkDir, $uploadPath) |
180
|
|
|
{ |
181
|
|
|
$_course = api_get_course_info(); |
182
|
|
|
$_user = api_get_user_info(); |
183
|
|
|
|
184
|
|
|
// Check if the file is valid (not to big and exists) |
185
|
|
|
if (!isset($_FILES['userFile']) || !is_uploaded_file($_FILES['userFile']['tmp_name'])) { |
186
|
|
|
// upload failed |
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
if (preg_match('/.zip$/i', $_FILES['userFile']['name']) && |
191
|
|
|
handle_uploaded_document( |
192
|
|
|
$_course, |
193
|
|
|
$_FILES['userFile'], |
194
|
|
|
$baseWorkDir, |
195
|
|
|
$uploadPath, |
196
|
|
|
$_user['user_id'], |
197
|
|
|
0, |
198
|
|
|
null, |
199
|
|
|
1, |
200
|
|
|
'overwrite', |
201
|
|
|
false, |
202
|
|
|
true |
203
|
|
|
) |
204
|
|
|
) { |
205
|
|
|
if (!function_exists('gzopen')) { |
206
|
|
|
return false; |
207
|
|
|
} |
208
|
|
|
// upload successful |
209
|
|
|
return true; |
210
|
|
|
} elseif (preg_match('/.txt/i', $_FILES['userFile']['name']) && |
211
|
|
|
handle_uploaded_document( |
212
|
|
|
$_course, |
213
|
|
|
$_FILES['userFile'], |
214
|
|
|
$baseWorkDir, |
215
|
|
|
$uploadPath, |
216
|
|
|
$_user['user_id'], |
217
|
|
|
0, |
218
|
|
|
null, |
219
|
|
|
0, |
220
|
|
|
'overwrite', |
221
|
|
|
false |
222
|
|
|
) |
223
|
|
|
) { |
224
|
|
|
return true; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return false; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Main function to import the Aiken exercise. |
232
|
|
|
* |
233
|
|
|
* @param string $file |
234
|
|
|
* @param array $request |
235
|
|
|
* |
236
|
|
|
* @return mixed True on success, error message on failure |
237
|
|
|
*/ |
238
|
|
|
function aikenImportExercise($file = null, $request = []) |
239
|
|
|
{ |
240
|
|
|
$exerciseInfo = []; |
241
|
|
|
$fileIsSet = false; |
242
|
|
|
|
243
|
|
|
if (isset($file)) { |
244
|
|
|
$fileIsSet = true; |
245
|
|
|
// The import is from aiken file format. |
246
|
|
|
$archivePath = api_get_path(SYS_ARCHIVE_PATH).'aiken/'; |
247
|
|
|
$baseWorkDir = $archivePath; |
248
|
|
|
|
249
|
|
|
$uploadPath = 'aiken_'.api_get_unique_id(); |
250
|
|
|
if (!is_dir($baseWorkDir.$uploadPath)) { |
251
|
|
|
mkdir($baseWorkDir.$uploadPath, api_get_permissions_for_new_directories(), true); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
// set some default values for the new exercise |
255
|
|
|
$exerciseInfo['name'] = preg_replace('/.(zip|txt)$/i', '', $file); |
256
|
|
|
$exerciseInfo['total_weight'] = !empty($_POST['total_weight']) ? (int) ($_POST['total_weight']) : 20; |
257
|
|
|
$exerciseInfo['question'] = []; |
258
|
|
|
|
259
|
|
|
// if file is not a .zip, then we cancel all |
260
|
|
|
if (!preg_match('/.(zip|txt)$/i', $file)) { |
261
|
|
|
return 'YouMustUploadAZipOrTxtFile'; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
// unzip the uploaded file in a tmp directory |
265
|
|
|
if (preg_match('/.(zip|txt)$/i', $file)) { |
266
|
|
|
if (!get_and_unzip_uploaded_exercise($baseWorkDir.$uploadPath, '/')) { |
267
|
|
|
return 'ThereWasAProblemWithYourFile'; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
// find the different manifests for each question and parse them |
272
|
|
|
$exerciseHandle = opendir($baseWorkDir.$uploadPath); |
273
|
|
|
$fileFound = false; |
274
|
|
|
$operation = false; |
275
|
|
|
$result = false; |
276
|
|
|
|
277
|
|
|
// Parse every subdirectory to search txt question files |
278
|
|
|
while (false !== ($file = readdir($exerciseHandle))) { |
279
|
|
|
if (is_dir($baseWorkDir.'/'.$uploadPath.$file) && $file != "." && $file != "..") { |
280
|
|
|
//find each manifest for each question repository found |
281
|
|
|
$questionHandle = opendir($baseWorkDir.'/'.$uploadPath.$file); |
282
|
|
|
while (false !== ($questionFile = readdir($questionHandle))) { |
283
|
|
|
if (preg_match('/.txt$/i', $questionFile)) { |
284
|
|
|
$result = aiken_parse_file( |
285
|
|
|
$exerciseInfo, |
286
|
|
|
$baseWorkDir, |
287
|
|
|
$file, |
288
|
|
|
$questionFile |
289
|
|
|
); |
290
|
|
|
$fileFound = true; |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
} elseif (preg_match('/.txt$/i', $file)) { |
294
|
|
|
$result = aiken_parse_file($exerciseInfo, $baseWorkDir.$uploadPath, '', $file); |
295
|
|
|
$fileFound = true; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
if (!$fileFound) { |
300
|
|
|
$result = 'NoTxtFileFoundInTheZip'; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
if (true !== $result) { |
304
|
|
|
return $result; |
305
|
|
|
} |
306
|
|
|
} elseif (!empty($request)) { |
307
|
|
|
// The import is from aiken generated in textarea. |
308
|
|
|
$exerciseInfo['name'] = $request['quiz_name']; |
309
|
|
|
$exerciseInfo['total_weight'] = !empty($_POST['ai_total_weight']) ? (int) ($_POST['ai_total_weight']) : (int) $request['nro_questions']; |
310
|
|
|
$exerciseInfo['question'] = []; |
311
|
|
|
$exerciseInfo['course_id'] = isset($request['course_id']) ? (int) $request['course_id'] : 0; |
312
|
|
|
setExerciseInfoFromAikenText($request['aiken_format'], $exerciseInfo); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
// 1. Create exercise. |
316
|
|
|
if (!empty($exerciseInfo)) { |
317
|
|
|
$exercise = new Exercise($exerciseInfo['course_id']); |
318
|
|
|
$exercise->exercise = $exerciseInfo['name']; |
319
|
|
|
$exercise->disable(); // Invisible by default |
320
|
|
|
$exercise->updateResultsDisabled(0); // Auto-evaluation mode: show score and expected answers |
321
|
|
|
$exercise->save(); |
322
|
|
|
$lastExerciseId = $exercise->selectId(); |
323
|
|
|
$tableQuestion = Database::get_course_table(TABLE_QUIZ_QUESTION); |
324
|
|
|
$tableAnswer = Database::get_course_table(TABLE_QUIZ_ANSWER); |
325
|
|
|
if (!empty($lastExerciseId)) { |
326
|
|
|
$courseId = !empty($exerciseInfo['course_id']) ? (int) $exerciseInfo['course_id'] : api_get_course_int_id(); |
327
|
|
|
foreach ($exerciseInfo['question'] as $key => $questionArray) { |
328
|
|
|
if (!isset($questionArray['title'])) { |
329
|
|
|
continue; |
330
|
|
|
} |
331
|
|
|
// 2. Create question. |
332
|
|
|
$question = new Aiken2Question(); |
333
|
|
|
$question->type = $questionArray['type']; |
334
|
|
|
$question->setAnswer(); |
335
|
|
|
$question->updateTitle($questionArray['title']); |
336
|
|
|
|
337
|
|
|
if (isset($questionArray['description'])) { |
338
|
|
|
$question->updateDescription($questionArray['description']); |
339
|
|
|
} |
340
|
|
|
$type = $question->selectType(); |
341
|
|
|
$question->course = api_get_course_info_by_id($courseId); |
342
|
|
|
$question->type = constant($type); |
343
|
|
|
$question->save($exercise); |
344
|
|
|
$last_question_id = $question->selectId(); |
345
|
|
|
|
346
|
|
|
// 3. Create answer |
347
|
|
|
$answer = new Answer($last_question_id, $courseId, $exercise, false); |
348
|
|
|
$answer->new_nbrAnswers = isset($questionArray['answer']) ? count($questionArray['answer']) : 0; |
349
|
|
|
$max_score = 0; |
350
|
|
|
|
351
|
|
|
$scoreFromFile = 0; |
352
|
|
|
if (isset($questionArray['score']) && !empty($questionArray['score'])) { |
353
|
|
|
$scoreFromFile = $questionArray['score']; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
foreach ($questionArray['answer'] as $key => $answers) { |
357
|
|
|
$key++; |
358
|
|
|
$answer->new_answer[$key] = $answers['value']; |
359
|
|
|
$answer->new_position[$key] = $key; |
360
|
|
|
$answer->new_comment[$key] = ''; |
361
|
|
|
// Correct answers ... |
362
|
|
|
if (isset($questionArray['correct_answers']) && |
363
|
|
|
in_array($key, $questionArray['correct_answers']) |
364
|
|
|
) { |
365
|
|
|
$answer->new_correct[$key] = 1; |
366
|
|
|
if (isset($questionArray['feedback'])) { |
367
|
|
|
$answer->new_comment[$key] = $questionArray['feedback']; |
368
|
|
|
} |
369
|
|
|
} else { |
370
|
|
|
$answer->new_correct[$key] = 0; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
if (isset($questionArray['weighting'][$key - 1])) { |
374
|
|
|
$answer->new_weighting[$key] = $questionArray['weighting'][$key - 1]; |
375
|
|
|
$max_score += $questionArray['weighting'][$key - 1]; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
if (!empty($scoreFromFile) && $answer->new_correct[$key]) { |
379
|
|
|
$answer->new_weighting[$key] = $scoreFromFile; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
$params = [ |
383
|
|
|
'c_id' => $courseId, |
384
|
|
|
'question_id' => $last_question_id, |
385
|
|
|
'answer' => $answer->new_answer[$key], |
386
|
|
|
'correct' => $answer->new_correct[$key], |
387
|
|
|
'comment' => $answer->new_comment[$key], |
388
|
|
|
'ponderation' => isset($answer->new_weighting[$key]) ? $answer->new_weighting[$key] : '', |
389
|
|
|
'position' => $answer->new_position[$key], |
390
|
|
|
'hotspot_coordinates' => '', |
391
|
|
|
'hotspot_type' => '', |
392
|
|
|
]; |
393
|
|
|
|
394
|
|
|
$answerId = Database::insert($tableAnswer, $params); |
395
|
|
|
if ($answerId) { |
396
|
|
|
$params = [ |
397
|
|
|
'id_auto' => $answerId, |
398
|
|
|
'iid' => $answerId, |
399
|
|
|
]; |
400
|
|
|
Database::update($tableAnswer, $params, ['iid = ?' => [$answerId]]); |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
if (!empty($scoreFromFile)) { |
405
|
|
|
$max_score = $scoreFromFile; |
406
|
|
|
} |
407
|
|
|
$params = ['ponderation' => $max_score]; |
408
|
|
|
Database::update( |
409
|
|
|
$tableQuestion, |
410
|
|
|
$params, |
411
|
|
|
['iid = ?' => [$last_question_id]] |
412
|
|
|
); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
// Delete the temp dir where the exercise was unzipped |
416
|
|
|
if ($fileIsSet) { |
417
|
|
|
my_delete($baseWorkDir.$uploadPath); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
// Invisible by default |
421
|
|
|
api_item_property_update( |
422
|
|
|
api_get_course_info(), |
423
|
|
|
TOOL_QUIZ, |
424
|
|
|
$lastExerciseId, |
425
|
|
|
'invisible', |
426
|
|
|
api_get_user_id() |
427
|
|
|
); |
428
|
|
|
|
429
|
|
|
return $lastExerciseId; |
430
|
|
|
} |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
return false; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Set the exercise information from an aiken text formatted. |
438
|
|
|
*/ |
439
|
|
|
function setExerciseInfoFromAikenText($aikenText, &$exerciseInfo) |
440
|
|
|
{ |
441
|
|
|
$detect = mb_detect_encoding($aikenText, 'ASCII', true); |
442
|
|
|
if ('ASCII' === $detect) { |
443
|
|
|
$data = explode("\n", $aikenText); |
444
|
|
|
} else { |
445
|
|
|
if (false !== stripos($aikenText, "\x0D") || false !== stripos($aikenText, "\r\n")) { |
446
|
|
|
$text = str_ireplace(["\x0D", "\r\n"], "\n", $aikenText); // Removes ^M char from win files. |
447
|
|
|
$data = explode("\n\n", $text); |
448
|
|
|
} else { |
449
|
|
|
$data = explode("\n", $aikenText); |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
$questionIndex = 0; |
454
|
|
|
$answersArray = []; |
455
|
|
|
foreach ($data as $line => $info) { |
456
|
|
|
$info = trim($info); |
457
|
|
|
if (empty($info)) { |
458
|
|
|
continue; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
//make sure it is transformed from iso-8859-1 to utf-8 if in that form |
462
|
|
|
if (!mb_check_encoding($info, 'utf-8') && mb_check_encoding($info, 'iso-8859-1')) { |
463
|
|
|
$info = utf8_encode($info); |
464
|
|
|
} |
465
|
|
|
$exerciseInfo['question'][$questionIndex]['type'] = 'MCUA'; |
466
|
|
|
|
467
|
|
|
if (preg_match('/^([A-Za-z])(\)|\.)\s(.*)/', $info, $matches)) { |
468
|
|
|
//adding one of the possible answers |
469
|
|
|
$exerciseInfo['question'][$questionIndex]['answer'][]['value'] = $matches[3]; |
470
|
|
|
$answersArray[] = $matches[1]; |
471
|
|
|
} elseif (preg_match('/^ANSWER:\s?([A-Z])\s?/', $info, $matches)) { |
472
|
|
|
//the correct answers |
473
|
|
|
$correctAnswerIndex = array_search($matches[1], $answersArray); |
474
|
|
|
$exerciseInfo['question'][$questionIndex]['correct_answers'][] = $correctAnswerIndex + 1; |
475
|
|
|
//weight for correct answer |
476
|
|
|
$exerciseInfo['question'][$questionIndex]['weighting'][$correctAnswerIndex] = 1; |
477
|
|
|
$next = $line + 1; |
478
|
|
|
|
479
|
|
|
if (isset($data[$next]) && false !== strpos($data[$next], 'ANSWER_EXPLANATION:')) { |
480
|
|
|
continue; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
if (isset($data[$next]) && false !== strpos($data[$next], 'DESCRIPTION:')) { |
484
|
|
|
continue; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
// Check if next has score, otherwise loop too next question. |
488
|
|
|
if (isset($data[$next]) && false === strpos($data[$next], 'SCORE:')) { |
489
|
|
|
$answersArray = []; |
490
|
|
|
$questionIndex++; |
491
|
|
|
continue; |
492
|
|
|
} |
493
|
|
|
} elseif (preg_match('/^SCORE:\s?(.*)/', $info, $matches)) { |
494
|
|
|
$exerciseInfo['question'][$questionIndex]['score'] = (float) $matches[1]; |
495
|
|
|
$answersArray = []; |
496
|
|
|
$questionIndex++; |
497
|
|
|
continue; |
498
|
|
|
} elseif (preg_match('/^DESCRIPTION:\s?(.*)/', $info, $matches)) { |
499
|
|
|
$exerciseInfo['question'][$questionIndex]['description'] = $matches[1]; |
500
|
|
|
$next = $line + 1; |
501
|
|
|
|
502
|
|
|
if (isset($data[$next]) && false !== strpos($data[$next], 'ANSWER_EXPLANATION:')) { |
503
|
|
|
continue; |
504
|
|
|
} |
505
|
|
|
// Check if next has score, otherwise loop too next question. |
506
|
|
|
if (isset($data[$next]) && false === strpos($data[$next], 'SCORE:')) { |
507
|
|
|
$answersArray = []; |
508
|
|
|
$questionIndex++; |
509
|
|
|
continue; |
510
|
|
|
} |
511
|
|
|
} elseif (preg_match('/^ANSWER_EXPLANATION:\s?(.*)/', $info, $matches)) { |
512
|
|
|
// Comment of correct answer |
513
|
|
|
$correctAnswerIndex = array_search($matches[1], $answersArray); |
514
|
|
|
$exerciseInfo['question'][$questionIndex]['feedback'] = $matches[1]; |
515
|
|
|
$next = $line + 1; |
516
|
|
|
// Check if next has score, otherwise loop too next question. |
517
|
|
|
if (isset($data[$next]) && false === strpos($data[$next], 'SCORE:')) { |
518
|
|
|
$answersArray = []; |
519
|
|
|
$questionIndex++; |
520
|
|
|
continue; |
521
|
|
|
} |
522
|
|
|
} elseif (preg_match('/^TEXTO_CORRECTA:\s?(.*)/', $info, $matches)) { |
523
|
|
|
//Comment of correct answer (Spanish e-ducativa format) |
524
|
|
|
$correctAnswerIndex = array_search($matches[1], $answersArray); |
525
|
|
|
$exerciseInfo['question'][$questionIndex]['feedback'] = $matches[1]; |
526
|
|
|
} elseif (preg_match('/^T:\s?(.*)/', $info, $matches)) { |
527
|
|
|
//Question Title |
528
|
|
|
$correctAnswerIndex = array_search($matches[1], $answersArray); |
529
|
|
|
$exerciseInfo['question'][$questionIndex]['title'] = $matches[1]; |
530
|
|
|
} elseif (preg_match('/^TAGS:\s?([A-Z])\s?/', $info, $matches)) { |
531
|
|
|
//TAGS for chamilo >= 1.10 |
532
|
|
|
$exerciseInfo['question'][$questionIndex]['answer_tags'] = explode(',', $matches[1]); |
533
|
|
|
} elseif (preg_match('/^ETIQUETAS:\s?([A-Z])\s?/', $info, $matches)) { |
534
|
|
|
//TAGS for chamilo >= 1.10 (Spanish e-ducativa format) |
535
|
|
|
$exerciseInfo['question'][$questionIndex]['answer_tags'] = explode(',', $matches[1]); |
536
|
|
|
} else { |
537
|
|
|
if (empty($exerciseInfo['question'][$questionIndex]['title'])) { |
538
|
|
|
$exerciseInfo['question'][$questionIndex]['title'] = $info; |
539
|
|
|
} |
540
|
|
|
} |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
$totalQuestions = count($exerciseInfo['question']); |
544
|
|
|
$totalWeight = (int) $exerciseInfo['total_weight']; |
545
|
|
|
foreach ($exerciseInfo['question'] as $key => $question) { |
546
|
|
|
if (!isset($exerciseInfo['question'][$key]['weighting'])) { |
547
|
|
|
continue; |
548
|
|
|
} |
549
|
|
|
$exerciseInfo['question'][$key]['weighting'][current(array_keys($exerciseInfo['question'][$key]['weighting']))] = $totalWeight / $totalQuestions; |
550
|
|
|
} |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* Parses an Aiken file and builds an array of exercise + questions to be |
555
|
|
|
* imported by the import_exercise() function. |
556
|
|
|
* |
557
|
|
|
* @param array The reference to the array in which to store the questions |
558
|
|
|
* @param string Path to the directory with the file to be parsed (without final /) |
559
|
|
|
* @param string Name of the last directory part for the file (without /) |
560
|
|
|
* @param string Name of the file to be parsed (including extension) |
561
|
|
|
* @param string $exercisePath |
562
|
|
|
* @param string $file |
563
|
|
|
* @param string $questionFile |
564
|
|
|
* |
565
|
|
|
* @return string|bool True on success, error message on error |
566
|
|
|
* @assert ('','','') === false |
567
|
|
|
*/ |
568
|
|
|
function aiken_parse_file(&$exercise_info, $exercisePath, $file, $questionFile) |
569
|
|
|
{ |
570
|
|
|
$questionTempDir = $exercisePath.'/'.$file.'/'; |
571
|
|
|
$questionFilePath = $questionTempDir.$questionFile; |
572
|
|
|
|
573
|
|
|
if (!is_file($questionFilePath)) { |
574
|
|
|
return 'FileNotFound'; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
$text = file_get_contents($questionFilePath); |
578
|
|
|
setExerciseInfoFromAikenText($text, $exercise_info); |
579
|
|
|
|
580
|
|
|
return true; |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
/** |
584
|
|
|
* Imports the zip file. |
585
|
|
|
* |
586
|
|
|
* @param array $array_file ($_FILES) |
587
|
|
|
* |
588
|
|
|
* @return bool |
589
|
|
|
*/ |
590
|
|
|
function aiken_import_file($array_file) |
591
|
|
|
{ |
592
|
|
|
$unzip = 0; |
593
|
|
|
$process = process_uploaded_file($array_file, false); |
594
|
|
|
if (preg_match('/\.(zip|txt)$/i', $array_file['name'])) { |
595
|
|
|
// if it's a zip, allow zip upload |
596
|
|
|
$unzip = 1; |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
if ($process && $unzip == 1) { |
600
|
|
|
$imported = aikenImportExercise($array_file['name']); |
601
|
|
|
if (is_numeric($imported) && !empty($imported)) { |
602
|
|
|
Display::addFlash(Display::return_message(get_lang('Uploaded'))); |
603
|
|
|
|
604
|
|
|
return $imported; |
605
|
|
|
} else { |
606
|
|
|
Display::addFlash(Display::return_message(get_lang($imported), 'error')); |
607
|
|
|
|
608
|
|
|
return false; |
609
|
|
|
} |
610
|
|
|
} |
611
|
|
|
} |
612
|
|
|
|