aiken_import.inc.php 14 KB

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