aiken_import.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Library for the import of Aiken format
  5. * @author claro team <cvs@claroline.net>
  6. * @author Guillaume Lederer <guillaume@claroline.net>
  7. * @author César Perales <cesar.perales@gmail.com> Parse function for Aiken format
  8. * @package chamilo.exercise
  9. */
  10. /**
  11. * Security check
  12. */
  13. if (count(get_included_files()) == 1)
  14. die('---');
  15. /**
  16. * Creates a temporary directory
  17. * @param $dir
  18. * @param string $prefix
  19. * @param int $mode
  20. * @return string
  21. */
  22. function tempdir($dir, $prefix = 'tmp', $mode = 0777) {
  23. if (substr($dir, -1) != '/')
  24. $dir .= '/';
  25. do {
  26. $path = $dir . $prefix . mt_rand(0, 9999999);
  27. } while (!mkdir($path, $mode));
  28. return $path;
  29. }
  30. /**
  31. * This function displays the form for import of the zip file with qti2
  32. * @param string Report message to show in case of error
  33. */
  34. function aiken_display_form($msg = '') {
  35. $name_tools = get_lang('ImportAikenQuiz');
  36. $form = '<div class="actions">';
  37. $form .= '<a href="exercice.php?show=test">' . Display :: return_icon('back.png', get_lang('BackToExercisesList'),'',ICON_SIZE_MEDIUM).'</a>';
  38. $form .= '</div>';
  39. $form .= $msg;
  40. $form_validator = new FormValidator('aiken_upload', 'post',api_get_self()."?".api_get_cidreq(), null, array('enctype' => 'multipart/form-data') );
  41. $form_validator->addElement('header', $name_tools);
  42. $form_validator->addElement('text', 'total_weight', get_lang('TotalWeight'));
  43. $form_validator->addElement('file', 'userFile', get_lang('DownloadFile'));
  44. $form_validator->addElement('style_submit_button', 'submit', get_lang('Send'), 'class="upload"');
  45. $form .= $form_validator->return_form();
  46. $form .= '<blockquote>'.get_lang('ImportAikenQuizExplanation').'<br /><pre>'.get_lang('ImportAikenQuizExplanationExample').'</pre></blockquote>';
  47. echo $form;
  48. }
  49. /**
  50. * Gets the uploaded file (from $_FILES) and unzip it to the given directory
  51. * @param string The directory where to do the work
  52. * @param string The path of the temporary directory where the exercise was uploaded and unzipped
  53. * @return bool True on success, false on failure
  54. */
  55. function get_and_unzip_uploaded_exercise($baseWorkDir, $uploadPath) {
  56. global $_course, $_user;
  57. //Check if the file is valid (not to big and exists)
  58. if (!isset ($_FILES['userFile']) || !is_uploaded_file($_FILES['userFile']['tmp_name'])) {
  59. // upload failed
  60. return false;
  61. }
  62. if (preg_match('/.zip$/i', $_FILES['userFile']['name']) && handle_uploaded_document($_course, $_FILES['userFile'], $baseWorkDir, $uploadPath, $_user['user_id'], 0, null, 1, 'overwrite', false)) {
  63. if (!function_exists('gzopen')) {
  64. return false;
  65. }
  66. // upload successful
  67. return true;
  68. } elseif (preg_match('/.txt/i', $_FILES['userFile']['name']) && handle_uploaded_document($_course, $_FILES['userFile'], $baseWorkDir, $uploadPath, $_user['user_id'], 0, null, 0, 'overwrite', false)) {
  69. return true;
  70. } else {
  71. return false;
  72. }
  73. }
  74. /**
  75. * Main function to import the Aiken exercise
  76. * @return mixed True on success, error message on failure
  77. */
  78. function aiken_import_exercise($file)
  79. {
  80. global $exercise_info;
  81. global $element_pile;
  82. global $non_HTML_tag_to_avoid;
  83. global $record_item_body;
  84. // used to specify the question directory where files could be found in relation in any question
  85. global $questionTempDir;
  86. $archive_path = api_get_path(SYS_ARCHIVE_PATH) . 'aiken';
  87. $baseWorkDir = $archive_path;
  88. if (!is_dir($baseWorkDir)) {
  89. mkdir($baseWorkDir, api_get_permissions_for_new_directories(), true);
  90. }
  91. $uploadPath = '/';
  92. // set some default values for the new exercise
  93. $exercise_info = array();
  94. $exercise_info['name'] = preg_replace('/.(zip|txt)$/i', '', $file);
  95. $exercise_info['question'] = array();
  96. $element_pile = array();
  97. // create parser and array to retrieve info from manifest
  98. $element_pile = array(); //pile to known the depth in which we are
  99. // if file is not a .zip, then we cancel all
  100. if (!preg_match('/.(zip|txt)$/i', $file)) {
  101. //Display :: display_error_message(get_lang('YouMustUploadAZipOrTxtFile'));
  102. return 'YouMustUploadAZipOrTxtFile';
  103. }
  104. // unzip the uploaded file in a tmp directory
  105. if (preg_match('/.(zip|txt)$/i', $file)) {
  106. if (!get_and_unzip_uploaded_exercise($baseWorkDir, $uploadPath)) {
  107. return 'ThereWasAProblemWithYourFile';
  108. }
  109. }
  110. // find the different manifests for each question and parse them
  111. $exerciseHandle = opendir($baseWorkDir);
  112. $file_found = false;
  113. $operation = false;
  114. $result = false;
  115. // Parse every subdirectory to search txt question files
  116. while (false !== ($file = readdir($exerciseHandle))) {
  117. if (is_dir($baseWorkDir . '/' . $file) && $file != "." && $file != "..") {
  118. //find each manifest for each question repository found
  119. $questionHandle = opendir($baseWorkDir . '/' . $file);
  120. while (false !== ($questionFile = readdir($questionHandle))) {
  121. if (preg_match('/.txt$/i', $questionFile)) {
  122. $result = aiken_parse_file(
  123. $exercise_info,
  124. $baseWorkDir,
  125. $file,
  126. $questionFile
  127. );
  128. $file_found = true;
  129. }
  130. }
  131. } elseif (preg_match('/.txt$/i', $file)) {
  132. $result = aiken_parse_file($exercise_info, $baseWorkDir, '', $file);
  133. $file_found = true;
  134. } // else ignore file
  135. }
  136. if (!$file_found) {
  137. $result = 'NoTxtFileFoundInTheZip';
  138. }
  139. if ($result !== true ) {
  140. return $result;
  141. }
  142. // Add exercise in tool
  143. // 1.create exercise
  144. $exercise = new Exercise();
  145. $exercise->exercise = $exercise_info['name'];
  146. $exercise->save();
  147. $last_exercise_id = $exercise->selectId();
  148. if (!empty($last_exercise_id)) {
  149. // For each question found...
  150. foreach ($exercise_info['question'] as $key => $question_array) {
  151. //2.create question
  152. $question = new Aiken2Question();
  153. $question->type = $question_array['type'];
  154. $question->setAnswer();
  155. $question->updateTitle($question_array['title']);
  156. $question->updateDescription($question_array['description']);
  157. $type = $question->selectType();
  158. $question->type = constant($type);
  159. $question->save($last_exercise_id);
  160. $last_question_id = $question->selectId();
  161. //3.create answer
  162. $answer = new Answer($last_question_id);
  163. $answer->new_nbrAnswers = count($question_array['answer']);
  164. $max_score = 0;
  165. foreach ($question_array['answer'] as $key => $answers) {
  166. $key++;
  167. $answer->new_answer[$key] = $answers['value'];
  168. $answer->new_position[$key] = $key;
  169. // Correct answers ...
  170. if (in_array($key, $question_array['correct_answers'])) {
  171. $answer->new_correct[$key] = 1;
  172. $answer->new_comment[$key] = $question_array['feedback'];
  173. } else {
  174. $answer->new_correct[$key] = 0;
  175. }
  176. $answer->new_weighting[$key] = $question_array['weighting'][$key - 1];
  177. $max_score += $question_array['weighting'][$key - 1];
  178. }
  179. $answer->save();
  180. // Now that we know the question score, set it!
  181. $question->updateWeighting($max_score);
  182. $question->save();
  183. }
  184. // delete the temp dir where the exercise was unzipped
  185. my_delete($baseWorkDir . $uploadPath);
  186. $operation = $last_exercise_id;
  187. }
  188. return $operation;
  189. }
  190. /**
  191. * Parses an Aiken file and builds an array of exercise + questions to be
  192. * imported by the import_exercise() function
  193. * @param array The reference to the array in which to store the questions
  194. * @param string Path to the directory with the file to be parsed (without final /)
  195. * @param string Name of the last directory part for the file (without /)
  196. * @param string Name of the file to be parsed (including extension)
  197. * @return mixed True on success, error message on error
  198. * @assert ('','','') === false
  199. */
  200. function aiken_parse_file(&$exercise_info, $exercisePath, $file, $questionFile) {
  201. global $questionTempDir;
  202. $questionTempDir = $exercisePath . '/' . $file . '/';
  203. $questionFilePath = $questionTempDir . $questionFile;
  204. if (!is_file($questionFilePath)) {
  205. return 'FileNotFound';
  206. }
  207. $data = file($questionFilePath);
  208. $question_index = 0;
  209. $correct_answer = '';
  210. $answers_array = array();
  211. $new_question = true;
  212. foreach ($data as $line => $info) {
  213. if ($question_index > 0 && $new_question == true && preg_match('/^(\r)?\n/',$info)) {
  214. // double empty line
  215. continue;
  216. }
  217. $new_question = false;
  218. //make sure it is transformed from iso-8859-1 to utf-8 if in that form
  219. if (!mb_check_encoding($info,'utf-8') && mb_check_encoding($info,'iso-8859-1')) {
  220. $info = utf8_encode($info);
  221. }
  222. $exercise_info['question'][$question_index]['type'] = 'MCUA';
  223. if (preg_match('/^([A-Za-z])(\)|\.)\s(.*)/', $info, $matches)) {
  224. //adding one of the possible answers
  225. $exercise_info['question'][$question_index]['answer'][]['value'] = $matches[3];
  226. $answers_array[] = $matches[1];
  227. } elseif (preg_match('/^ANSWER:\s?([A-Z])\s?/', $info, $matches)) {
  228. //the correct answers
  229. $correct_answer_index = array_search($matches[1], $answers_array);
  230. $exercise_info['question'][$question_index]['correct_answers'][] = $correct_answer_index + 1;
  231. //weight for correct answer
  232. $exercise_info['question'][$question_index]['weighting'][$correct_answer_index] = 1;
  233. } elseif (preg_match('/^ANSWER_EXPLANATION:\s?(.*)/', $info, $matches)) {
  234. //Comment of correct answer
  235. $correct_answer_index = array_search($matches[1], $answers_array);
  236. $exercise_info['question'][$question_index]['feedback'] = $matches[1];
  237. } elseif (preg_match('/^TEXTO_CORRECTA:\s?(.*)/', $info, $matches)) {
  238. //Comment of correct answer (Spanish e-ducativa format)
  239. $correct_answer_index = array_search($matches[1], $answers_array);
  240. $exercise_info['question'][$question_index]['feedback'] = $matches[1];
  241. } elseif (preg_match('/^T:\s?(.*)/', $info, $matches)) {
  242. //Question Title
  243. $correct_answer_index = array_search($matches[1], $answers_array);
  244. $exercise_info['question'][$question_index]['title'] = $matches[1];
  245. } elseif (preg_match('/^TAGS:\s?([A-Z])\s?/', $info, $matches)) {
  246. //TAGS for chamilo >= 1.10
  247. $exercise_info['question'][$question_index]['answer_tags'] = explode(',', $matches[1]);
  248. } elseif (preg_match('/^ETIQUETAS:\s?([A-Z])\s?/', $info, $matches)) {
  249. //TAGS for chamilo >= 1.10 (Spanish e-ducativa format)
  250. $exercise_info['question'][$question_index]['answer_tags'] = explode(',', $matches[1]);
  251. } elseif (preg_match('/^(\r)?\n/',$info)) {
  252. //moving to next question (tolerate \r\n or just \n)
  253. if (empty($exercise_info['question'][$question_index]['correct_answers'])) {
  254. error_log('Aiken: Error in question index '.$question_index.': no correct answer defined');
  255. return 'ExerciseAikenErrorNoCorrectAnswerDefined';
  256. }
  257. if (empty($exercise_info['question'][$question_index]['answer'])) {
  258. error_log('Aiken: Error in question index '.$question_index.': no answer option given');
  259. return 'ExerciseAikenErrorNoAnswerOptionGiven';
  260. }
  261. $question_index++;
  262. //emptying answers array when moving to next question
  263. $answers_array = array();
  264. $new_question = true;
  265. } else {
  266. if (empty($exercise_info['question'][$question_index]['title'])) {
  267. if (strlen($info) < 40) {
  268. $exercise_info['question'][$question_index]['title'] = $info;
  269. } else {
  270. //Question itself (use a 40-chars long title and a larger description)
  271. $exercise_info['question'][$question_index]['title'] = trim(substr($info,0,40)).'...';
  272. $exercise_info['question'][$question_index]['description'] = $info;
  273. }
  274. } else {
  275. $exercise_info['question'][$question_index]['description'] = $info;
  276. }
  277. }
  278. }
  279. $total_questions = count($exercise_info['question']);
  280. $total_weight = (!empty($_POST['total_weight'])) ? intval($_POST['total_weight']) : 20;
  281. foreach ($exercise_info['question'] as $key => $question) {
  282. $exercise_info['question'][$key]['weighting'][current(array_keys($exercise_info['question'][$key]['weighting']))] = $total_weight / $total_questions;
  283. }
  284. return true;
  285. }
  286. /**
  287. * Imports the zip file
  288. * @param array $array_file ($_FILES)
  289. */
  290. function aiken_import_file($array_file) {
  291. $unzip = 0;
  292. $process = process_uploaded_file($array_file, false);
  293. if (preg_match('/\.(zip|txt)$/i', $array_file['name'])) {
  294. // if it's a zip, allow zip upload
  295. $unzip = 1;
  296. }
  297. if ($process && $unzip == 1) {
  298. $imported = aiken_import_exercise($array_file['name']);
  299. if (is_numeric($imported) && !empty($imported)) {
  300. return $imported;
  301. } else {
  302. $msg = Display::return_message(get_lang($imported), 'error');
  303. return $msg;
  304. }
  305. }
  306. }