aiken_import.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Library for the import of Aiken format.
  5. *
  6. * @author claro team <cvs@claroline.net>
  7. * @author Guillaume Lederer <guillaume@claroline.net>
  8. * @author César Perales <cesar.perales@gmail.com> Parse function for Aiken format
  9. *
  10. * @package chamilo.exercise
  11. */
  12. /**
  13. * This function displays the form for import of the zip file with qti2.
  14. *
  15. * @param string Report message to show in case of error
  16. */
  17. function aiken_display_form()
  18. {
  19. $name_tools = get_lang('Import Aiken quiz');
  20. $form = '<div class="actions">';
  21. $form .= '<a href="exercise.php?show=test&'.api_get_cidreq().'">'.
  22. Display::return_icon(
  23. 'back.png',
  24. get_lang('Back to Tests tool'),
  25. '',
  26. ICON_SIZE_MEDIUM
  27. ).'</a>';
  28. $form .= '</div>';
  29. $form_validator = new FormValidator(
  30. 'aiken_upload',
  31. 'post',
  32. api_get_self()."?".api_get_cidreq(),
  33. null,
  34. ['enctype' => 'multipart/form-data']
  35. );
  36. $form_validator->addElement('header', $name_tools);
  37. $form_validator->addElement('text', 'total_weight', get_lang('Total weight'));
  38. $form_validator->addElement('file', 'userFile', get_lang('File'));
  39. $form_validator->addButtonUpload(get_lang('Upload'), 'submit');
  40. $form .= $form_validator->returnForm();
  41. $form .= '<blockquote>'.get_lang('Import Aiken quizExplanation').'<br /><pre>'.get_lang('Import Aiken quizExplanationExample').'</pre></blockquote>';
  42. echo $form;
  43. }
  44. /**
  45. * Gets the uploaded file (from $_FILES) and unzip it to the given directory.
  46. *
  47. * @param string The directory where to do the work
  48. * @param string The path of the temporary directory where the exercise was uploaded and unzipped
  49. * @param string $baseWorkDir
  50. * @param string $uploadPath
  51. *
  52. * @return bool True on success, false on failure
  53. */
  54. function get_and_unzip_uploaded_exercise($baseWorkDir, $uploadPath)
  55. {
  56. $_course = api_get_course_info();
  57. $_user = api_get_user_info();
  58. // Check if the file is valid (not to big and exists)
  59. if (!isset($_FILES['userFile']) || !is_uploaded_file($_FILES['userFile']['tmp_name'])) {
  60. // upload failed
  61. return false;
  62. }
  63. if (preg_match('/.zip$/i', $_FILES['userFile']['name']) &&
  64. handle_uploaded_document(
  65. $_course,
  66. $_FILES['userFile'],
  67. $baseWorkDir,
  68. $uploadPath,
  69. $_user['user_id'],
  70. 0,
  71. null,
  72. 1,
  73. 'overwrite',
  74. false
  75. )
  76. ) {
  77. if (!function_exists('gzopen')) {
  78. return false;
  79. }
  80. // upload successful
  81. return true;
  82. } elseif (preg_match('/.txt/i', $_FILES['userFile']['name']) &&
  83. handle_uploaded_document(
  84. $_course,
  85. $_FILES['userFile'],
  86. $baseWorkDir,
  87. $uploadPath,
  88. $_user['user_id'],
  89. 0,
  90. null,
  91. 0,
  92. 'overwrite',
  93. false
  94. )
  95. ) {
  96. return true;
  97. } else {
  98. return false;
  99. }
  100. }
  101. /**
  102. * Main function to import the Aiken exercise.
  103. *
  104. * @param string $file
  105. *
  106. * @return mixed True on success, error message on failure
  107. */
  108. function aiken_import_exercise($file)
  109. {
  110. $archive_path = api_get_path(SYS_ARCHIVE_PATH).'aiken/';
  111. $baseWorkDir = $archive_path;
  112. if (!is_dir($baseWorkDir)) {
  113. mkdir($baseWorkDir, api_get_permissions_for_new_directories(), true);
  114. }
  115. $uploadPath = 'aiken_'.api_get_unique_id().'/';
  116. // set some default values for the new exercise
  117. $exercise_info = [];
  118. $exercise_info['name'] = preg_replace('/.(zip|txt)$/i', '', $file);
  119. $exercise_info['question'] = [];
  120. // if file is not a .zip, then we cancel all
  121. if (!preg_match('/.(zip|txt)$/i', $file)) {
  122. return 'YouMustUploadAZipOrTxtFile';
  123. }
  124. // unzip the uploaded file in a tmp directory
  125. if (preg_match('/.(zip|txt)$/i', $file)) {
  126. if (!get_and_unzip_uploaded_exercise($baseWorkDir, $uploadPath)) {
  127. return 'ThereWasAProblemWithYourFile';
  128. }
  129. }
  130. // find the different manifests for each question and parse them
  131. $exerciseHandle = opendir($baseWorkDir.$uploadPath);
  132. $file_found = false;
  133. $operation = false;
  134. $result = false;
  135. // Parse every subdirectory to search txt question files
  136. while (false !== ($file = readdir($exerciseHandle))) {
  137. if (is_dir($baseWorkDir.'/'.$uploadPath.$file) && $file != "." && $file != "..") {
  138. //find each manifest for each question repository found
  139. $questionHandle = opendir($baseWorkDir.'/'.$uploadPath.$file);
  140. while (false !== ($questionFile = readdir($questionHandle))) {
  141. if (preg_match('/.txt$/i', $questionFile)) {
  142. $result = aiken_parse_file(
  143. $exercise_info,
  144. $baseWorkDir,
  145. $file,
  146. $questionFile
  147. );
  148. $file_found = true;
  149. }
  150. }
  151. } elseif (preg_match('/.txt$/i', $file)) {
  152. $result = aiken_parse_file($exercise_info, $baseWorkDir.$uploadPath, '', $file);
  153. $file_found = true;
  154. }
  155. }
  156. if (!$file_found) {
  157. $result = 'NoTxtFileFoundInTheZip';
  158. }
  159. if ($result !== true) {
  160. return $result;
  161. }
  162. // 1. Create exercise
  163. $exercise = new Exercise();
  164. $exercise->exercise = $exercise_info['name'];
  165. $exercise->save();
  166. $last_exercise_id = $exercise->selectId();
  167. $tableQuestion = Database::get_course_table(TABLE_QUIZ_QUESTION);
  168. $tableAnswer = Database::get_course_table(TABLE_QUIZ_ANSWER);
  169. if (!empty($last_exercise_id)) {
  170. // For each question found...
  171. $courseId = api_get_course_int_id();
  172. foreach ($exercise_info['question'] as $key => $question_array) {
  173. // 2.create question
  174. $question = new Aiken2Question();
  175. $question->type = $question_array['type'];
  176. $question->setAnswer();
  177. $question->updateTitle($question_array['title']);
  178. if (isset($question_array['description'])) {
  179. $question->updateDescription($question_array['description']);
  180. }
  181. $type = $question->selectType();
  182. $question->type = constant($type);
  183. $question->save($exercise);
  184. $last_question_id = $question->selectId();
  185. // 3. Create answer
  186. $answer = new Answer($last_question_id, $courseId, $exercise, false);
  187. $answer->new_nbrAnswers = count($question_array['answer']);
  188. $max_score = 0;
  189. $scoreFromFile = 0;
  190. if (isset($question_array['score']) && !empty($question_array['score'])) {
  191. $scoreFromFile = $question_array['score'];
  192. }
  193. foreach ($question_array['answer'] as $key => $answers) {
  194. $key++;
  195. $answer->new_answer[$key] = $answers['value'];
  196. $answer->new_position[$key] = $key;
  197. $answer->new_comment[$key] = '';
  198. // Correct answers ...
  199. if (in_array($key, $question_array['correct_answers'])) {
  200. $answer->new_correct[$key] = 1;
  201. if (isset($question_array['feedback'])) {
  202. $answer->new_comment[$key] = $question_array['feedback'];
  203. }
  204. } else {
  205. $answer->new_correct[$key] = 0;
  206. }
  207. if (isset($question_array['weighting'][$key - 1])) {
  208. $answer->new_weighting[$key] = $question_array['weighting'][$key - 1];
  209. $max_score += $question_array['weighting'][$key - 1];
  210. }
  211. if (!empty($scoreFromFile) && $answer->new_correct[$key]) {
  212. $answer->new_weighting[$key] = $scoreFromFile;
  213. }
  214. $params = [
  215. 'c_id' => $courseId,
  216. 'question_id' => $last_question_id,
  217. 'answer' => $answer->new_answer[$key],
  218. 'correct' => $answer->new_correct[$key],
  219. 'comment' => $answer->new_comment[$key],
  220. 'ponderation' => isset($answer->new_weighting[$key]) ? $answer->new_weighting[$key] : '',
  221. 'position' => $answer->new_position[$key],
  222. 'hotspot_coordinates' => '',
  223. 'hotspot_type' => '',
  224. ];
  225. $answerId = Database::insert($tableAnswer, $params);
  226. if ($answerId) {
  227. $params = [
  228. 'id_auto' => $answerId,
  229. 'id' => $answerId,
  230. ];
  231. Database::update($tableAnswer, $params, ['iid = ?' => [$answerId]]);
  232. }
  233. }
  234. if (!empty($scoreFromFile)) {
  235. $max_score = $scoreFromFile;
  236. }
  237. //$answer->save();
  238. $params = ['ponderation' => $max_score];
  239. Database::update(
  240. $tableQuestion,
  241. $params,
  242. ['iid = ?' => [$last_question_id]]
  243. );
  244. }
  245. // Delete the temp dir where the exercise was unzipped
  246. my_delete($baseWorkDir.$uploadPath);
  247. $operation = $last_exercise_id;
  248. }
  249. return $operation;
  250. }
  251. /**
  252. * Parses an Aiken file and builds an array of exercise + questions to be
  253. * imported by the import_exercise() function.
  254. *
  255. * @param array The reference to the array in which to store the questions
  256. * @param string Path to the directory with the file to be parsed (without final /)
  257. * @param string Name of the last directory part for the file (without /)
  258. * @param string Name of the file to be parsed (including extension)
  259. * @param string $exercisePath
  260. * @param string $file
  261. * @param string $questionFile
  262. *
  263. * @return string|bool True on success, error message on error
  264. * @assert ('','','') === false
  265. */
  266. function aiken_parse_file(&$exercise_info, $exercisePath, $file, $questionFile)
  267. {
  268. $questionTempDir = $exercisePath.'/'.$file.'/';
  269. $questionFilePath = $questionTempDir.$questionFile;
  270. if (!is_file($questionFilePath)) {
  271. return 'FileNotFound';
  272. }
  273. $data = file($questionFilePath);
  274. $question_index = 0;
  275. $answers_array = [];
  276. $new_question = true;
  277. foreach ($data as $line => $info) {
  278. if ($question_index > 0 && $new_question == true && preg_match('/^(\r)?\n/', $info)) {
  279. // double empty line
  280. continue;
  281. }
  282. $new_question = false;
  283. //make sure it is transformed from iso-8859-1 to utf-8 if in that form
  284. if (!mb_check_encoding($info, 'utf-8') && mb_check_encoding($info, 'iso-8859-1')) {
  285. $info = utf8_encode($info);
  286. }
  287. $exercise_info['question'][$question_index]['type'] = 'MCUA';
  288. if (preg_match('/^([A-Za-z])(\)|\.)\s(.*)/', $info, $matches)) {
  289. //adding one of the possible answers
  290. $exercise_info['question'][$question_index]['answer'][]['value'] = $matches[3];
  291. $answers_array[] = $matches[1];
  292. } elseif (preg_match('/^ANSWER:\s?([A-Z])\s?/', $info, $matches)) {
  293. //the correct answers
  294. $correct_answer_index = array_search($matches[1], $answers_array);
  295. $exercise_info['question'][$question_index]['correct_answers'][] = $correct_answer_index + 1;
  296. //weight for correct answer
  297. $exercise_info['question'][$question_index]['weighting'][$correct_answer_index] = 1;
  298. } elseif (preg_match('/^SCORE:\s?(.*)/', $info, $matches)) {
  299. $exercise_info['question'][$question_index]['score'] = (float) $matches[1];
  300. } elseif (preg_match('/^DESCRIPTION:\s?(.*)/', $info, $matches)) {
  301. $exercise_info['question'][$question_index]['description'] = $matches[1];
  302. } elseif (preg_match('/^ANSWER_EXPLANATION:\s?(.*)/', $info, $matches)) {
  303. //Comment of correct answer
  304. $correct_answer_index = array_search($matches[1], $answers_array);
  305. $exercise_info['question'][$question_index]['feedback'] = $matches[1];
  306. } elseif (preg_match('/^TEXTO_CORRECTA:\s?(.*)/', $info, $matches)) {
  307. //Comment of correct answer (Spanish e-ducativa format)
  308. $correct_answer_index = array_search($matches[1], $answers_array);
  309. $exercise_info['question'][$question_index]['feedback'] = $matches[1];
  310. } elseif (preg_match('/^T:\s?(.*)/', $info, $matches)) {
  311. //Question Title
  312. $correct_answer_index = array_search($matches[1], $answers_array);
  313. $exercise_info['question'][$question_index]['title'] = $matches[1];
  314. } elseif (preg_match('/^TAGS:\s?([A-Z])\s?/', $info, $matches)) {
  315. //TAGS for chamilo >= 1.10
  316. $exercise_info['question'][$question_index]['answer_tags'] = explode(',', $matches[1]);
  317. } elseif (preg_match('/^ETIQUETAS:\s?([A-Z])\s?/', $info, $matches)) {
  318. //TAGS for chamilo >= 1.10 (Spanish e-ducativa format)
  319. $exercise_info['question'][$question_index]['answer_tags'] = explode(',', $matches[1]);
  320. } elseif (preg_match('/^(\r)?\n/', $info)) {
  321. //moving to next question (tolerate \r\n or just \n)
  322. if (empty($exercise_info['question'][$question_index]['correct_answers'])) {
  323. error_log('Aiken: Error in question index '.$question_index.': no correct answer defined');
  324. return 'ExerciseAikenErrorNoCorrectAnswerDefined';
  325. }
  326. if (empty($exercise_info['question'][$question_index]['answer'])) {
  327. error_log('Aiken: Error in question index '.$question_index.': no answer option given');
  328. return 'ExerciseAikenErrorNoAnswerOptionGiven';
  329. }
  330. $question_index++;
  331. //emptying answers array when moving to next question
  332. $answers_array = [];
  333. $new_question = true;
  334. } else {
  335. if (empty($exercise_info['question'][$question_index]['title'])) {
  336. $exercise_info['question'][$question_index]['title'] = $info;
  337. }
  338. }
  339. }
  340. $total_questions = count($exercise_info['question']);
  341. $total_weight = (!empty($_POST['total_weight'])) ? intval($_POST['total_weight']) : 20;
  342. foreach ($exercise_info['question'] as $key => $question) {
  343. $exercise_info['question'][$key]['weighting'][current(array_keys($exercise_info['question'][$key]['weighting']))] = $total_weight / $total_questions;
  344. }
  345. return true;
  346. }
  347. /**
  348. * Imports the zip file.
  349. *
  350. * @param array $array_file ($_FILES)
  351. *
  352. * @return bool
  353. */
  354. function aiken_import_file($array_file)
  355. {
  356. $unzip = 0;
  357. $process = process_uploaded_file($array_file, false);
  358. if (preg_match('/\.(zip|txt)$/i', $array_file['name'])) {
  359. // if it's a zip, allow zip upload
  360. $unzip = 1;
  361. }
  362. if ($process && $unzip == 1) {
  363. $imported = aiken_import_exercise($array_file['name']);
  364. if (is_numeric($imported) && !empty($imported)) {
  365. Display::addFlash(Display::return_message(get_lang('Uploaded.')));
  366. return $imported;
  367. } else {
  368. Display::addFlash(Display::return_message(get_lang($imported), 'error'));
  369. return false;
  370. }
  371. }
  372. }