aiken_import.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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="exercise.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->addButtonUpload(get_lang('Send'), 'submit');
  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. $_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']) && handle_uploaded_document($_course, $_FILES['userFile'], $baseWorkDir, $uploadPath, $_user['user_id'], 0, null, 1, 'overwrite', false)) {
  64. if (!function_exists('gzopen')) {
  65. return false;
  66. }
  67. // upload successful
  68. return true;
  69. } elseif (preg_match('/.txt/i', $_FILES['userFile']['name']) && handle_uploaded_document($_course, $_FILES['userFile'], $baseWorkDir, $uploadPath, $_user['user_id'], 0, null, 0, 'overwrite', false)) {
  70. return true;
  71. } else {
  72. return false;
  73. }
  74. }
  75. /**
  76. * Main function to import the Aiken exercise
  77. * @return mixed True on success, error message on failure
  78. */
  79. function aiken_import_exercise($file)
  80. {
  81. global $exercise_info;
  82. global $element_pile;
  83. global $non_HTML_tag_to_avoid;
  84. global $record_item_body;
  85. // used to specify the question directory where files could be found in relation in any question
  86. global $questionTempDir;
  87. $archive_path = api_get_path(SYS_ARCHIVE_PATH) . 'aiken';
  88. $baseWorkDir = $archive_path;
  89. if (!is_dir($baseWorkDir)) {
  90. mkdir($baseWorkDir, api_get_permissions_for_new_directories(), true);
  91. }
  92. $uploadPath = '/';
  93. // set some default values for the new exercise
  94. $exercise_info = array();
  95. $exercise_info['name'] = preg_replace('/.(zip|txt)$/i', '', $file);
  96. $exercise_info['question'] = array();
  97. $element_pile = array();
  98. // create parser and array to retrieve info from manifest
  99. $element_pile = array(); //pile to known the depth in which we are
  100. // if file is not a .zip, then we cancel all
  101. if (!preg_match('/.(zip|txt)$/i', $file)) {
  102. //Display :: display_error_message(get_lang('YouMustUploadAZipOrTxtFile'));
  103. return 'YouMustUploadAZipOrTxtFile';
  104. }
  105. // unzip the uploaded file in a tmp directory
  106. if (preg_match('/.(zip|txt)$/i', $file)) {
  107. if (!get_and_unzip_uploaded_exercise($baseWorkDir, $uploadPath)) {
  108. return 'ThereWasAProblemWithYourFile';
  109. }
  110. }
  111. // find the different manifests for each question and parse them
  112. $exerciseHandle = opendir($baseWorkDir);
  113. $file_found = false;
  114. $operation = false;
  115. $result = false;
  116. // Parse every subdirectory to search txt question files
  117. while (false !== ($file = readdir($exerciseHandle))) {
  118. if (is_dir($baseWorkDir . '/' . $file) && $file != "." && $file != "..") {
  119. //find each manifest for each question repository found
  120. $questionHandle = opendir($baseWorkDir . '/' . $file);
  121. while (false !== ($questionFile = readdir($questionHandle))) {
  122. if (preg_match('/.txt$/i', $questionFile)) {
  123. $result = aiken_parse_file(
  124. $exercise_info,
  125. $baseWorkDir,
  126. $file,
  127. $questionFile
  128. );
  129. $file_found = true;
  130. }
  131. }
  132. } elseif (preg_match('/.txt$/i', $file)) {
  133. $result = aiken_parse_file($exercise_info, $baseWorkDir, '', $file);
  134. $file_found = true;
  135. } // else ignore file
  136. }
  137. if (!$file_found) {
  138. $result = 'NoTxtFileFoundInTheZip';
  139. }
  140. if ($result !== true ) {
  141. return $result;
  142. }
  143. // Add exercise in tool
  144. // 1.create exercise
  145. $exercise = new Exercise();
  146. $exercise->exercise = $exercise_info['name'];
  147. $exercise->save();
  148. $last_exercise_id = $exercise->selectId();
  149. if (!empty($last_exercise_id)) {
  150. // For each question found...
  151. foreach ($exercise_info['question'] as $key => $question_array) {
  152. //2.create question
  153. $question = new Aiken2Question();
  154. $question->type = $question_array['type'];
  155. $question->setAnswer();
  156. $question->updateTitle($question_array['title']);
  157. if (isset($question_array['description'])) {
  158. $question->updateDescription($question_array['description']);
  159. }
  160. $type = $question->selectType();
  161. $question->type = constant($type);
  162. $question->save($last_exercise_id);
  163. $last_question_id = $question->selectId();
  164. //3. Create answer
  165. $answer = new Answer($last_question_id);
  166. $answer->new_nbrAnswers = count($question_array['answer']);
  167. $max_score = 0;
  168. foreach ($question_array['answer'] as $key => $answers) {
  169. $key++;
  170. $answer->new_answer[$key] = $answers['value'];
  171. $answer->new_position[$key] = $key;
  172. // Correct answers ...
  173. if (in_array($key, $question_array['correct_answers'])) {
  174. $answer->new_correct[$key] = 1;
  175. if (isset($question_array['feedback'])) {
  176. $answer->new_comment[$key] = $question_array['feedback'];
  177. }
  178. } else {
  179. $answer->new_correct[$key] = 0;
  180. }
  181. if (isset($question_array['weighting'][$key - 1])) {
  182. $answer->new_weighting[$key] = $question_array['weighting'][$key - 1];
  183. $max_score += $question_array['weighting'][$key - 1];
  184. }
  185. }
  186. $answer->save();
  187. // Now that we know the question score, set it!
  188. $question->updateWeighting($max_score);
  189. $question->save();
  190. }
  191. // Delete the temp dir where the exercise was unzipped
  192. my_delete($baseWorkDir . $uploadPath);
  193. $operation = $last_exercise_id;
  194. }
  195. return $operation;
  196. }
  197. /**
  198. * Parses an Aiken file and builds an array of exercise + questions to be
  199. * imported by the import_exercise() function
  200. * @param array The reference to the array in which to store the questions
  201. * @param string Path to the directory with the file to be parsed (without final /)
  202. * @param string Name of the last directory part for the file (without /)
  203. * @param string Name of the file to be parsed (including extension)
  204. * @return mixed True on success, error message on error
  205. * @assert ('','','') === false
  206. */
  207. function aiken_parse_file(&$exercise_info, $exercisePath, $file, $questionFile) {
  208. global $questionTempDir;
  209. $questionTempDir = $exercisePath . '/' . $file . '/';
  210. $questionFilePath = $questionTempDir . $questionFile;
  211. if (!is_file($questionFilePath)) {
  212. return 'FileNotFound';
  213. }
  214. $data = file($questionFilePath);
  215. $question_index = 0;
  216. $correct_answer = '';
  217. $answers_array = array();
  218. $new_question = true;
  219. foreach ($data as $line => $info) {
  220. if ($question_index > 0 && $new_question == true && preg_match('/^(\r)?\n/',$info)) {
  221. // double empty line
  222. continue;
  223. }
  224. $new_question = false;
  225. //make sure it is transformed from iso-8859-1 to utf-8 if in that form
  226. if (!mb_check_encoding($info,'utf-8') && mb_check_encoding($info,'iso-8859-1')) {
  227. $info = utf8_encode($info);
  228. }
  229. $exercise_info['question'][$question_index]['type'] = 'MCUA';
  230. if (preg_match('/^([A-Za-z])(\)|\.)\s(.*)/', $info, $matches)) {
  231. //adding one of the possible answers
  232. $exercise_info['question'][$question_index]['answer'][]['value'] = $matches[3];
  233. $answers_array[] = $matches[1];
  234. } elseif (preg_match('/^ANSWER:\s?([A-Z])\s?/', $info, $matches)) {
  235. //the correct answers
  236. $correct_answer_index = array_search($matches[1], $answers_array);
  237. $exercise_info['question'][$question_index]['correct_answers'][] = $correct_answer_index + 1;
  238. //weight for correct answer
  239. $exercise_info['question'][$question_index]['weighting'][$correct_answer_index] = 1;
  240. } elseif (preg_match('/^ANSWER_EXPLANATION:\s?(.*)/', $info, $matches)) {
  241. //Comment of correct answer
  242. $correct_answer_index = array_search($matches[1], $answers_array);
  243. $exercise_info['question'][$question_index]['feedback'] = $matches[1];
  244. } elseif (preg_match('/^TEXTO_CORRECTA:\s?(.*)/', $info, $matches)) {
  245. //Comment of correct answer (Spanish e-ducativa format)
  246. $correct_answer_index = array_search($matches[1], $answers_array);
  247. $exercise_info['question'][$question_index]['feedback'] = $matches[1];
  248. } elseif (preg_match('/^T:\s?(.*)/', $info, $matches)) {
  249. //Question Title
  250. $correct_answer_index = array_search($matches[1], $answers_array);
  251. $exercise_info['question'][$question_index]['title'] = $matches[1];
  252. } elseif (preg_match('/^TAGS:\s?([A-Z])\s?/', $info, $matches)) {
  253. //TAGS for chamilo >= 1.10
  254. $exercise_info['question'][$question_index]['answer_tags'] = explode(',', $matches[1]);
  255. } elseif (preg_match('/^ETIQUETAS:\s?([A-Z])\s?/', $info, $matches)) {
  256. //TAGS for chamilo >= 1.10 (Spanish e-ducativa format)
  257. $exercise_info['question'][$question_index]['answer_tags'] = explode(',', $matches[1]);
  258. } elseif (preg_match('/^(\r)?\n/',$info)) {
  259. //moving to next question (tolerate \r\n or just \n)
  260. if (empty($exercise_info['question'][$question_index]['correct_answers'])) {
  261. error_log('Aiken: Error in question index '.$question_index.': no correct answer defined');
  262. return 'ExerciseAikenErrorNoCorrectAnswerDefined';
  263. }
  264. if (empty($exercise_info['question'][$question_index]['answer'])) {
  265. error_log('Aiken: Error in question index '.$question_index.': no answer option given');
  266. return 'ExerciseAikenErrorNoAnswerOptionGiven';
  267. }
  268. $question_index++;
  269. //emptying answers array when moving to next question
  270. $answers_array = array();
  271. $new_question = true;
  272. } else {
  273. if (empty($exercise_info['question'][$question_index]['title'])) {
  274. if (strlen($info) < 100) {
  275. $exercise_info['question'][$question_index]['title'] = $info;
  276. } else {
  277. //Question itself (use a 100-chars long title and a larger description)
  278. $exercise_info['question'][$question_index]['title'] = trim(substr($info, 0, 100)) . '...';
  279. $exercise_info['question'][$question_index]['description'] = $info;
  280. }
  281. } else {
  282. $exercise_info['question'][$question_index]['description'] = $info;
  283. }
  284. }
  285. }
  286. $total_questions = count($exercise_info['question']);
  287. $total_weight = (!empty($_POST['total_weight'])) ? intval($_POST['total_weight']) : 20;
  288. foreach ($exercise_info['question'] as $key => $question) {
  289. $exercise_info['question'][$key]['weighting'][current(array_keys($exercise_info['question'][$key]['weighting']))] = $total_weight / $total_questions;
  290. }
  291. return true;
  292. }
  293. /**
  294. * Imports the zip file
  295. * @param array $array_file ($_FILES)
  296. */
  297. function aiken_import_file($array_file) {
  298. $unzip = 0;
  299. $process = process_uploaded_file($array_file, false);
  300. if (preg_match('/\.(zip|txt)$/i', $array_file['name'])) {
  301. // if it's a zip, allow zip upload
  302. $unzip = 1;
  303. }
  304. if ($process && $unzip == 1) {
  305. $imported = aiken_import_exercise($array_file['name']);
  306. if (is_numeric($imported) && !empty($imported)) {
  307. return $imported;
  308. } else {
  309. $msg = Display::return_message(get_lang($imported), 'error');
  310. return $msg;
  311. }
  312. }
  313. }