MoodleImport.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class MoodleImport
  5. *
  6. * @author José Loguercio <jose.loguercio@beeznest.com>,
  7. * @author Julio Montoya <gugli100@gmail.com>
  8. *
  9. * @package chamilo.library
  10. */
  11. class MoodleImport
  12. {
  13. /**
  14. * Import moodle file
  15. *
  16. * @param resource $uploadedFile *.* mbz file moodle course backup
  17. * @return bool
  18. */
  19. public function import($uploadedFile)
  20. {
  21. $file = $uploadedFile['tmp_name'];
  22. if (is_file($file) && is_readable($file)) {
  23. $package = new PclZip($file);
  24. $packageContent = $package->listContent();
  25. $mainFileKey = 0;
  26. foreach ($packageContent as $index => $value) {
  27. if ($value['filename'] == 'moodle_backup.xml') {
  28. $mainFileKey = $index;
  29. break;
  30. }
  31. }
  32. if (!$mainFileKey) {
  33. Display::addFlash(
  34. Display::return_message(
  35. get_lang('FailedToImportThisIsNotAMoodleFile'),
  36. 'error'
  37. )
  38. );
  39. }
  40. $folder = api_get_unique_id();
  41. $destinationDir = api_get_path(SYS_ARCHIVE_PATH).$folder;
  42. $coursePath = api_get_course_path();
  43. $sessionId = api_get_session_id();
  44. $groupId = api_get_group_id();
  45. $documentPath = api_get_path(SYS_COURSE_PATH).$coursePath.'/document';
  46. $courseInfo = api_get_course_info();
  47. mkdir($destinationDir, api_get_permissions_for_new_directories(), true);
  48. create_unexisting_directory(
  49. $courseInfo,
  50. api_get_user_id(),
  51. $sessionId,
  52. $groupId,
  53. null,
  54. $documentPath,
  55. '/moodle',
  56. 'Moodle Docs',
  57. 0
  58. );
  59. $package->extract(
  60. PCLZIP_OPT_PATH,
  61. $destinationDir
  62. );
  63. // This process will upload all question resource files
  64. $filesXml = @file_get_contents($destinationDir.'/files.xml');
  65. $mainFileModuleValues = $this->getAllQuestionFiles($filesXml);
  66. $currentResourceFilePath = $destinationDir.'/files/';
  67. $importedFiles = [];
  68. foreach ($mainFileModuleValues as $fileInfo) {
  69. $dirs = new RecursiveDirectoryIterator($currentResourceFilePath);
  70. foreach (new RecursiveIteratorIterator($dirs) as $file) {
  71. if (is_file($file) && strpos($file, $fileInfo['contenthash']) !== false) {
  72. $files = [];
  73. $files['file']['name'] = $fileInfo['filename'];
  74. $files['file']['tmp_name'] = $file->getPathname();
  75. $files['file']['type'] = $fileInfo['mimetype'];
  76. $files['file']['error'] = 0;
  77. $files['file']['size'] = $fileInfo['filesize'];
  78. $files['file']['from_file'] = true;
  79. $files['file']['move_file'] = true;
  80. $_POST['language'] = $courseInfo['language'];
  81. $_POST['moodle_import'] = true;
  82. $data = DocumentManager::upload_document(
  83. $files,
  84. '/moodle',
  85. isset($fileInfo['title']) ? $fileInfo['title'] : pathinfo($fileInfo['filename'], PATHINFO_FILENAME),
  86. '',
  87. null,
  88. null,
  89. true,
  90. true,
  91. 'file',
  92. // This is to validate spaces as hyphens
  93. false
  94. );
  95. if ($data) {
  96. $importedFiles[$fileInfo['filename']] = basename($data['path']);
  97. }
  98. }
  99. }
  100. }
  101. $xml = @file_get_contents($destinationDir.'/moodle_backup.xml');
  102. $doc = new DOMDocument();
  103. $res = @$doc->loadXML($xml);
  104. if ($res) {
  105. $activities = $doc->getElementsByTagName('activity');
  106. foreach ($activities as $activity) {
  107. if ($activity->childNodes->length) {
  108. $currentItem = [];
  109. foreach ($activity->childNodes as $item) {
  110. $currentItem[$item->nodeName] = $item->nodeValue;
  111. }
  112. $moduleName = isset($currentItem['modulename']) ? $currentItem['modulename'] : false;
  113. switch ($moduleName) {
  114. case 'forum':
  115. require_once '../forum/forumfunction.inc.php';
  116. $catForumValues = [];
  117. // Read the current forum module xml.
  118. $moduleDir = $currentItem['directory'];
  119. $moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml');
  120. $moduleValues = $this->readForumModule($moduleXml);
  121. // Create a Forum category based on Moodle forum type.
  122. $catForumValues['forum_category_title'] = $moduleValues['type'];
  123. $catForumValues['forum_category_comment'] = '';
  124. $catId = store_forumcategory($catForumValues, $courseInfo, false);
  125. $forumValues = [];
  126. $forumValues['forum_title'] = $moduleValues['name'];
  127. $forumValues['forum_image'] = '';
  128. $forumValues['forum_comment'] = $moduleValues['intro'];
  129. $forumValues['forum_category'] = $catId;
  130. $forumValues['moderated'] = 0;
  131. store_forum($forumValues, $courseInfo);
  132. break;
  133. case 'quiz':
  134. // Read the current quiz module xml.
  135. // The quiz case is the very complicate process of all the import.
  136. // Please if you want to review the script, try to see the readingXML functions.
  137. // The readingXML functions in this clases do all the mayor work here.
  138. $moduleDir = $currentItem['directory'];
  139. $moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml');
  140. $questionsXml = @file_get_contents($destinationDir.'/questions.xml');
  141. $moduleValues = $this->readQuizModule($moduleXml);
  142. // At this point we got all the prepared resources from Moodle file
  143. // $moduleValues variable contains all the necesary info to the quiz import
  144. // var_dump($moduleValues); // <-- uncomment this to see the final array
  145. $exercise = new Exercise($courseInfo['real_id']);
  146. $title = Exercise::format_title_variable($moduleValues['name']);
  147. $exercise->updateTitle($title);
  148. $exercise->updateDescription($moduleValues['intro']);
  149. $exercise->updateAttempts($moduleValues['attempts_number']);
  150. $exercise->updateFeedbackType(0);
  151. // Match shuffle question with chamilo
  152. switch ($moduleValues['shufflequestions']) {
  153. case '0':
  154. $exercise->setRandom(0);
  155. break;
  156. case '1':
  157. $exercise->setRandom(-1);
  158. break;
  159. default:
  160. $exercise->setRandom(0);
  161. }
  162. $exercise->updateRandomAnswers($moduleValues['shuffleanswers']);
  163. // @todo divide to minutes
  164. $exercise->updateExpiredTime($moduleValues['timelimit']);
  165. if ($moduleValues['questionsperpage'] == 1) {
  166. $exercise->updateType(2);
  167. } else {
  168. $exercise->updateType(1);
  169. }
  170. // Create the new Quiz
  171. $exercise->save();
  172. // Ok, we got the Quiz and create it, now its time to add the Questions
  173. foreach ($moduleValues['question_instances'] as $index => $question) {
  174. $questionsValues = $this->readMainQuestionsXml($questionsXml, $question['questionid']);
  175. $moduleValues['question_instances'][$index] = $questionsValues;
  176. // Set Question Type from Moodle XML element <qtype>
  177. $qType = $moduleValues['question_instances'][$index]['qtype'];
  178. // Add the matched chamilo question type to the array
  179. $moduleValues['question_instances'][$index]['chamilo_qtype'] = $this->matchMoodleChamiloQuestionTypes($qType);
  180. $questionInstance = Question::getInstance($moduleValues['question_instances'][$index]['chamilo_qtype']);
  181. if ($questionInstance) {
  182. $questionInstance->updateTitle($moduleValues['question_instances'][$index]['name']);
  183. $questionText = $moduleValues['question_instances'][$index]['questiontext'];
  184. // Replace the path from @@PLUGINFILE@@ to a correct chamilo path
  185. $questionText = str_replace(
  186. '@@PLUGINFILE@@',
  187. '/courses/'.$coursePath.'/document/moodle',
  188. $questionText
  189. );
  190. if ($importedFiles) {
  191. $this->fixPathInText($importedFiles, $questionText);
  192. }
  193. $questionInstance->updateDescription($questionText);
  194. $questionInstance->updateLevel(1);
  195. $questionInstance->updateCategory(0);
  196. //Save normal question if NOT media
  197. if ($questionInstance->type != MEDIA_QUESTION) {
  198. $questionInstance->save($exercise->id);
  199. // modify the exercise
  200. $exercise->addToList($questionInstance->id);
  201. $exercise->update_question_positions();
  202. }
  203. $questionList = $moduleValues['question_instances'][$index]['plugin_qtype_'.$qType.'_question'];
  204. $currentQuestion = $moduleValues['question_instances'][$index];
  205. $this->processAnswers(
  206. $questionList,
  207. $qType,
  208. $questionInstance,
  209. $currentQuestion,
  210. $importedFiles
  211. );
  212. }
  213. }
  214. break;
  215. case 'resource':
  216. // Read the current resource module xml.
  217. $moduleDir = $currentItem['directory'];
  218. $moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml');
  219. $filesXml = @file_get_contents($destinationDir.'/files.xml');
  220. $moduleValues = $this->readResourceModule($moduleXml);
  221. $mainFileModuleValues = $this->readMainFilesXml($filesXml, $moduleValues['contextid']);
  222. $fileInfo = array_merge($moduleValues, $mainFileModuleValues, $currentItem);
  223. $currentResourceFilePath = $destinationDir.'/files/';
  224. $dirs = new RecursiveDirectoryIterator($currentResourceFilePath);
  225. foreach (new RecursiveIteratorIterator($dirs) as $file) {
  226. if (is_file($file) && strpos($file, $fileInfo['contenthash']) !== false) {
  227. $files = [];
  228. $files['file']['name'] = $fileInfo['filename'];
  229. $files['file']['tmp_name'] = $file->getPathname();
  230. $files['file']['type'] = $fileInfo['mimetype'];
  231. $files['file']['error'] = 0;
  232. $files['file']['size'] = $fileInfo['filesize'];
  233. $files['file']['from_file'] = true;
  234. $files['file']['move_file'] = true;
  235. $_POST['language'] = $courseInfo['language'];
  236. $_POST['moodle_import'] = true;
  237. DocumentManager::upload_document(
  238. $files,
  239. '/moodle',
  240. $fileInfo['title'],
  241. '',
  242. null,
  243. null,
  244. true,
  245. true
  246. );
  247. }
  248. }
  249. break;
  250. case 'url':
  251. // Read the current url module xml.
  252. $moduleDir = $currentItem['directory'];
  253. $moduleXml = @file_get_contents($destinationDir.'/'.$moduleDir.'/'.$moduleName.'.xml');
  254. $moduleValues = $this->readUrlModule($moduleXml);
  255. $_POST['title'] = $moduleValues['name'];
  256. $_POST['url'] = $moduleValues['externalurl'];
  257. $_POST['description'] = $moduleValues['intro'];
  258. $_POST['category_id'] = 0;
  259. $_POST['target'] = '_blank';
  260. Link::addlinkcategory("link");
  261. break;
  262. }
  263. }
  264. }
  265. } else {
  266. removeDir($destinationDir);
  267. return false;
  268. }
  269. } else {
  270. return false;
  271. }
  272. removeDir($destinationDir);
  273. return $packageContent[$mainFileKey];
  274. }
  275. /**
  276. * Read and validate the forum module XML
  277. *
  278. * @param resource $moduleXml XML file
  279. * @return mixed | array if is a valid xml file, false otherwise
  280. */
  281. public function readForumModule($moduleXml)
  282. {
  283. $moduleDoc = new DOMDocument();
  284. $moduleRes = @$moduleDoc->loadXML($moduleXml);
  285. if ($moduleRes) {
  286. $activities = $moduleDoc->getElementsByTagName('forum');
  287. $currentItem = [];
  288. foreach ($activities as $activity) {
  289. if ($activity->childNodes->length) {
  290. foreach ($activity->childNodes as $item) {
  291. $currentItem[$item->nodeName] = $item->nodeValue;
  292. }
  293. }
  294. }
  295. return $currentItem;
  296. }
  297. return false;
  298. }
  299. /**
  300. * Read and validate the resource module XML
  301. *
  302. * @param resource $moduleXml XML file
  303. * @return mixed | array if is a valid xml file, false otherwise
  304. */
  305. public function readResourceModule($moduleXml)
  306. {
  307. $moduleDoc = new DOMDocument();
  308. $moduleRes = @$moduleDoc->loadXML($moduleXml);
  309. if ($moduleRes) {
  310. $activities = $moduleDoc->getElementsByTagName('resource');
  311. $mainActivity = $moduleDoc->getElementsByTagName('activity');
  312. $contextId = $mainActivity->item(0)->getAttribute('contextid');
  313. $currentItem = [];
  314. foreach ($activities as $activity) {
  315. if ($activity->childNodes->length) {
  316. foreach ($activity->childNodes as $item) {
  317. $currentItem[$item->nodeName] = $item->nodeValue;
  318. }
  319. }
  320. }
  321. $currentItem['contextid'] = $contextId;
  322. return $currentItem;
  323. }
  324. return false;
  325. }
  326. /**
  327. * Read and validate the url module XML
  328. *
  329. * @param resource $moduleXml XML file
  330. * @return mixed | array if is a valid xml file, false otherwise
  331. */
  332. public function readUrlModule($moduleXml)
  333. {
  334. $moduleDoc = new DOMDocument();
  335. $moduleRes = @$moduleDoc->loadXML($moduleXml);
  336. if ($moduleRes) {
  337. $activities = $moduleDoc->getElementsByTagName('url');
  338. $currentItem = [];
  339. foreach ($activities as $activity) {
  340. if ($activity->childNodes->length) {
  341. foreach ($activity->childNodes as $item) {
  342. $currentItem[$item->nodeName] = $item->nodeValue;
  343. }
  344. }
  345. }
  346. return $currentItem;
  347. }
  348. return false;
  349. }
  350. /**
  351. * Read and validate the quiz module XML
  352. *
  353. * @param resource $moduleXml XML file
  354. * @return mixed | array if is a valid xml file, false otherwise
  355. */
  356. public function readQuizModule($moduleXml)
  357. {
  358. $moduleDoc = new DOMDocument();
  359. $moduleRes = @$moduleDoc->loadXML($moduleXml);
  360. if ($moduleRes) {
  361. $activities = $moduleDoc->getElementsByTagName('quiz');
  362. $currentItem = [];
  363. foreach ($activities as $activity) {
  364. if ($activity->childNodes->length) {
  365. foreach ($activity->childNodes as $item) {
  366. $currentItem[$item->nodeName] = $item->nodeValue;
  367. }
  368. }
  369. }
  370. $questions = $moduleDoc->getElementsByTagName('question_instance');
  371. $questionList = [];
  372. $counter = 0;
  373. foreach ($questions as $question) {
  374. if ($question->childNodes->length) {
  375. foreach ($question->childNodes as $item) {
  376. $questionList[$counter][$item->nodeName] = $item->nodeValue;
  377. }
  378. $counter++;
  379. }
  380. }
  381. $currentItem['question_instances'] = $questionList;
  382. return $currentItem;
  383. }
  384. return false;
  385. }
  386. /**
  387. * Search the current file resource in main Files XML
  388. *
  389. * @param resource $filesXml XML file
  390. * @param int $contextId
  391. * @return mixed | array if is a valid xml file, false otherwise
  392. */
  393. public function readMainFilesXml($filesXml, $contextId)
  394. {
  395. $moduleDoc = new DOMDocument();
  396. $moduleRes = @$moduleDoc->loadXML($filesXml);
  397. if ($moduleRes) {
  398. $activities = $moduleDoc->getElementsByTagName('file');
  399. $currentItem = [];
  400. foreach ($activities as $activity) {
  401. if ($activity->childNodes->length) {
  402. $isThisItemThatIWant = false;
  403. foreach ($activity->childNodes as $item) {
  404. if (!$isThisItemThatIWant && $item->nodeName == 'contenthash') {
  405. $currentItem['contenthash'] = $item->nodeValue;
  406. }
  407. if ($item->nodeName == 'contextid' && intval($item->nodeValue) == intval($contextId) && !$isThisItemThatIWant) {
  408. $isThisItemThatIWant = true;
  409. continue;
  410. }
  411. if ($isThisItemThatIWant && $item->nodeName == 'filename') {
  412. $currentItem['filename'] = $item->nodeValue;
  413. }
  414. if ($isThisItemThatIWant && $item->nodeName == 'filesize') {
  415. $currentItem['filesize'] = $item->nodeValue;
  416. }
  417. if ($isThisItemThatIWant && $item->nodeName == 'mimetype' && $item->nodeValue == 'document/unknown') {
  418. break;
  419. }
  420. if ($isThisItemThatIWant && $item->nodeName == 'mimetype' && $item->nodeValue !== 'document/unknown') {
  421. $currentItem['mimetype'] = $item->nodeValue;
  422. break 2;
  423. }
  424. }
  425. }
  426. }
  427. return $currentItem;
  428. }
  429. return false;
  430. }
  431. /**
  432. * Search the current question resource in main Questions XML
  433. *
  434. * @param resource $questionsXml XML file
  435. * @param int $questionId
  436. * @return mixed | array if is a valid xml file, false otherwise
  437. */
  438. public function readMainQuestionsXml($questionsXml, $questionId)
  439. {
  440. $moduleDoc = new DOMDocument();
  441. $moduleRes = @$moduleDoc->loadXML($questionsXml);
  442. if ($moduleRes) {
  443. $questions = $moduleDoc->getElementsByTagName('question');
  444. $currentItem = [];
  445. foreach ($questions as $question) {
  446. if (intval($question->getAttribute('id')) == $questionId) {
  447. if ($question->childNodes->length) {
  448. $currentItem['questionid'] = $questionId;
  449. $questionType = '';
  450. foreach ($question->childNodes as $item) {
  451. $currentItem[$item->nodeName] = $item->nodeValue;
  452. if ($item->nodeName == 'qtype') {
  453. $questionType = $item->nodeValue;
  454. }
  455. if ($item->nodeName == 'plugin_qtype_'.$questionType.'_question') {
  456. $answer = $item->getElementsByTagName($this->getQuestionTypeAnswersTag($questionType));
  457. $currentItem['plugin_qtype_'.$questionType.'_question'] = [];
  458. for ($i = 0; $i <= $answer->length - 1; $i++) {
  459. $currentItem['plugin_qtype_'.$questionType.'_question'][$i]['answerid'] = $answer->item($i)->getAttribute('id');
  460. foreach ($answer->item($i)->childNodes as $properties) {
  461. $currentItem['plugin_qtype_'.$questionType.'_question'][$i][$properties->nodeName] = $properties->nodeValue;
  462. }
  463. }
  464. $typeValues = $item->getElementsByTagName($this->getQuestionTypeOptionsTag($questionType));
  465. for ($i = 0; $i <= $typeValues->length - 1; $i++) {
  466. foreach ($typeValues->item($i)->childNodes as $properties) {
  467. $currentItem[$questionType.'_values'][$properties->nodeName] = $properties->nodeValue;
  468. if ($properties->nodeName == 'sequence') {
  469. $sequence = $properties->nodeValue;
  470. $sequenceIds = explode(',', $sequence);
  471. foreach ($sequenceIds as $qId) {
  472. $questionMatch = $this->readMainQuestionsXml($questionsXml, $qId);
  473. $currentItem['plugin_qtype_'.$questionType.'_question'][] = $questionMatch;
  474. }
  475. }
  476. }
  477. }
  478. }
  479. }
  480. }
  481. }
  482. }
  483. $this->traverseArray($currentItem, ['#text', 'question_hints', 'tags']);
  484. return $currentItem;
  485. }
  486. return false;
  487. }
  488. /**
  489. * return the correct question type options tag
  490. *
  491. * @param string $questionType name
  492. * @return string question type tag
  493. */
  494. public function getQuestionTypeOptionsTag($questionType)
  495. {
  496. switch ($questionType) {
  497. case 'match':
  498. case 'ddmatch':
  499. return 'matchoptions';
  500. break;
  501. default:
  502. return $questionType;
  503. break;
  504. }
  505. }
  506. /**
  507. * return the correct question type answers tag
  508. *
  509. * @param string $questionType name
  510. * @return string question type tag
  511. */
  512. public function getQuestionTypeAnswersTag($questionType)
  513. {
  514. switch ($questionType) {
  515. case 'match':
  516. case 'ddmatch':
  517. return 'match';
  518. break;
  519. default:
  520. return 'answer';
  521. break;
  522. }
  523. }
  524. /**
  525. *
  526. * @param string $moodleQuestionType
  527. * @return integer Chamilo question type
  528. */
  529. public function matchMoodleChamiloQuestionTypes($moodleQuestionType)
  530. {
  531. switch ($moodleQuestionType) {
  532. case 'multichoice':
  533. return UNIQUE_ANSWER;
  534. break;
  535. case 'multianswer':
  536. case 'shortanswer':
  537. case 'match':
  538. return FILL_IN_BLANKS;
  539. break;
  540. case 'essay':
  541. return FREE_ANSWER;
  542. break;
  543. case 'truefalse':
  544. return UNIQUE_ANSWER_NO_OPTION;
  545. break;
  546. }
  547. }
  548. /**
  549. * Fix moodle files that contains spaces
  550. * @param array $importedFiles
  551. * @param string $text
  552. * @return mixed
  553. */
  554. public function fixPathInText($importedFiles, &$text)
  555. {
  556. if ($importedFiles) {
  557. foreach ($importedFiles as $old => $new) {
  558. // Ofaj fix moodle file names
  559. // In some questions moodle text contains file with name like:
  560. // Bild%20Check-In-Formular%20Ausfu%CC%88llen.jpg"
  561. // rawurlencode function transforms '' (whitespace) to %20 and so on
  562. $text = str_replace(rawurlencode($old), $new, $text);
  563. }
  564. }
  565. return $text;
  566. }
  567. /**
  568. * Process Moodle Answers to Chamilo
  569. *
  570. * @param array $questionList
  571. * @param string $questionType
  572. * @param object $questionInstance Question/Answer instance
  573. * @param array $currentQuestion
  574. * @param array $importedFiles
  575. * @return integer db response
  576. */
  577. public function processAnswers($questionList, $questionType, $questionInstance, $currentQuestion, $importedFiles)
  578. {
  579. switch ($questionType) {
  580. case 'multichoice':
  581. $objAnswer = new Answer($questionInstance->id);
  582. $questionWeighting = 0;
  583. foreach ($questionList as $slot => $answer) {
  584. $this->processUniqueAnswer(
  585. $objAnswer,
  586. $answer,
  587. $slot + 1,
  588. $questionWeighting,
  589. $importedFiles
  590. );
  591. }
  592. // saves the answers into the data base
  593. $objAnswer->save();
  594. // sets the total weighting of the question
  595. $questionInstance->updateWeighting($questionWeighting);
  596. $questionInstance->save();
  597. return true;
  598. break;
  599. case 'multianswer':
  600. $objAnswer = new Answer($questionInstance->id);
  601. $coursePath = api_get_course_path();
  602. $placeholder = str_replace(
  603. '@@PLUGINFILE@@',
  604. '/courses/'.$coursePath.'/document/moodle',
  605. $currentQuestion['questiontext']
  606. );
  607. $optionsValues = [];
  608. foreach ($questionList as $slot => $subQuestion) {
  609. $qtype = $subQuestion['qtype'];
  610. $optionsValues[] = $this->processFillBlanks(
  611. $objAnswer,
  612. $qtype,
  613. $subQuestion['plugin_qtype_'.$qtype.'_question'],
  614. $placeholder,
  615. $slot + 1,
  616. $importedFiles
  617. );
  618. }
  619. $answerOptionsWeight = '::';
  620. $answerOptionsSize = '';
  621. $questionWeighting = 0;
  622. foreach ($optionsValues as $index => $value) {
  623. $questionWeighting += $value['weight'];
  624. $answerOptionsWeight .= $value['weight'].',';
  625. $answerOptionsSize .= $value['size'].',';
  626. }
  627. $answerOptionsWeight = substr($answerOptionsWeight, 0, -1);
  628. $answerOptionsSize = substr($answerOptionsSize, 0, -1);
  629. $answerOptions = $answerOptionsWeight.':'.$answerOptionsSize.':0@';
  630. $placeholder = $placeholder.PHP_EOL.$answerOptions;
  631. // This is a minor trick to clean the question description that in a multianswer is the main placeholder
  632. $questionInstance->updateDescription('');
  633. // sets the total weighting of the question
  634. $questionInstance->updateWeighting($questionWeighting);
  635. $questionInstance->save();
  636. $this->fixPathInText($importedFiles, $placeholder);
  637. // saves the answers into the data base
  638. $objAnswer->createAnswer($placeholder, 0, '', 0, 1);
  639. $objAnswer->save();
  640. return true;
  641. case 'match':
  642. $objAnswer = new Answer($questionInstance->id);
  643. $placeholder = '';
  644. $optionsValues = $this->processFillBlanks(
  645. $objAnswer,
  646. 'match',
  647. $questionList,
  648. $placeholder,
  649. 0,
  650. $importedFiles
  651. );
  652. $answerOptionsWeight = '::';
  653. $answerOptionsSize = '';
  654. $questionWeighting = 0;
  655. foreach ($optionsValues as $index => $value) {
  656. $questionWeighting += $value['weight'];
  657. $answerOptionsWeight .= $value['weight'].',';
  658. $answerOptionsSize .= $value['size'].',';
  659. }
  660. $answerOptionsWeight = substr($answerOptionsWeight, 0, -1);
  661. $answerOptionsSize = substr($answerOptionsSize, 0, -1);
  662. $answerOptions = $answerOptionsWeight.':'.$answerOptionsSize.':0@';
  663. $placeholder = $placeholder.PHP_EOL.$answerOptions;
  664. // sets the total weighting of the question
  665. $questionInstance->updateWeighting($questionWeighting);
  666. $questionInstance->save();
  667. // saves the answers into the database
  668. $this->fixPathInText($importedFiles, $placeholder);
  669. $objAnswer->createAnswer($placeholder, 0, '', 0, 1);
  670. $objAnswer->save();
  671. return true;
  672. break;
  673. case 'shortanswer':
  674. case 'ddmatch':
  675. $questionWeighting = $currentQuestion['defaultmark'];
  676. $questionInstance->updateWeighting($questionWeighting);
  677. $questionInstance->updateDescription(get_lang('ThisQuestionIsNotSupportedYet'));
  678. $questionInstance->save();
  679. return false;
  680. break;
  681. case 'essay':
  682. $questionWeighting = $currentQuestion['defaultmark'];
  683. $questionInstance->updateWeighting($questionWeighting);
  684. $questionInstance->save();
  685. return true;
  686. break;
  687. case 'truefalse':
  688. $objAnswer = new Answer($questionInstance->id);
  689. $questionWeighting = 0;
  690. foreach ($questionList as $slot => $answer) {
  691. $this->processTrueFalse(
  692. $objAnswer,
  693. $answer,
  694. $slot + 1,
  695. $questionWeighting,
  696. $importedFiles
  697. );
  698. }
  699. // saves the answers into the data base
  700. $objAnswer->save();
  701. // sets the total weighting of the question
  702. $questionInstance->updateWeighting($questionWeighting);
  703. $questionInstance->save();
  704. return false;
  705. break;
  706. default:
  707. return false;
  708. break;
  709. }
  710. }
  711. /**
  712. * Process Chamilo Unique Answer
  713. *
  714. * @param object $objAnswer
  715. * @param array $answerValues
  716. * @param integer $position
  717. * @param integer $questionWeighting
  718. * @param array $importedFiles
  719. * @return integer db response
  720. */
  721. public function processUniqueAnswer($objAnswer, $answerValues, $position, &$questionWeighting, $importedFiles)
  722. {
  723. $correct = intval($answerValues['fraction']) ? intval($answerValues['fraction']) : 0;
  724. $answer = $answerValues['answertext'];
  725. $comment = $answerValues['feedback'];
  726. $weighting = $answerValues['fraction'];
  727. $weighting = abs($weighting);
  728. if ($weighting > 0) {
  729. $questionWeighting += $weighting;
  730. }
  731. $goodAnswer = $correct ? true : false;
  732. $this->fixPathInText($importedFiles, $answer);
  733. $objAnswer->createAnswer(
  734. $answer,
  735. $goodAnswer,
  736. $comment,
  737. $weighting,
  738. $position,
  739. null,
  740. null,
  741. ''
  742. );
  743. }
  744. /**
  745. * Process Chamilo True False
  746. *
  747. * @param object $objAnswer
  748. * @param array $answerValues
  749. * @param integer $position
  750. * @param integer $questionWeighting
  751. * @param array $importedFiles
  752. *
  753. * @return integer db response
  754. */
  755. public function processTrueFalse($objAnswer, $answerValues, $position, &$questionWeighting, $importedFiles)
  756. {
  757. $correct = intval($answerValues['fraction']) ? intval($answerValues['fraction']) : 0;
  758. $answer = $answerValues['answertext'];
  759. $comment = $answerValues['feedback'];
  760. $weighting = $answerValues['fraction'];
  761. $weighting = abs($weighting);
  762. if ($weighting > 0) {
  763. $questionWeighting += $weighting;
  764. }
  765. $goodAnswer = $correct ? true : false;
  766. $this->fixPathInText($importedFiles, $answer);
  767. $objAnswer->createAnswer(
  768. $answer,
  769. $goodAnswer,
  770. $comment,
  771. $weighting,
  772. $position,
  773. null,
  774. null,
  775. ''
  776. );
  777. }
  778. /**
  779. * Process Chamilo FillBlanks
  780. *
  781. * @param object $objAnswer
  782. * @param array $questionType
  783. * @param array $answerValues
  784. * @param string $placeholder
  785. * @param integer $position
  786. * @param array $importedFiles
  787. * @return integer db response
  788. *
  789. */
  790. public function processFillBlanks($objAnswer, $questionType, $answerValues, &$placeholder, $position, $importedFiles)
  791. {
  792. $coursePath = api_get_course_path();
  793. switch ($questionType) {
  794. case 'multichoice':
  795. $optionsValues = [];
  796. $correctAnswer = '';
  797. $othersAnswer = '';
  798. foreach ($answerValues as $answer) {
  799. $correct = intval($answer['fraction']);
  800. if ($correct) {
  801. $correctAnswer .= $answer['answertext'].'|';
  802. $optionsValues['weight'] = $answer['fraction'];
  803. $optionsValues['size'] = '200';
  804. } else {
  805. $othersAnswer .= $answer['answertext'].'|';
  806. }
  807. }
  808. $currentAnswers = $correctAnswer.$othersAnswer;
  809. $currentAnswers = '['.substr($currentAnswers, 0, -1).']';
  810. $placeholder = str_replace("{#$position}", $currentAnswers, $placeholder);
  811. return $optionsValues;
  812. break;
  813. case 'shortanswer':
  814. $optionsValues = [];
  815. $correctAnswer = '';
  816. foreach ($answerValues as $answer) {
  817. $correct = intval($answer['fraction']);
  818. if ($correct) {
  819. $correctAnswer .= $answer['answertext'];
  820. $optionsValues['weight'] = $answer['fraction'];
  821. $optionsValues['size'] = '200';
  822. }
  823. }
  824. $currentAnswers = '['.$correctAnswer.']';
  825. $placeholder = str_replace("{#$position}", $currentAnswers, $placeholder);
  826. return $optionsValues;
  827. break;
  828. case 'match':
  829. $answers = [];
  830. // Here first we need to extract all the possible answers
  831. foreach ($answerValues as $slot => $answer) {
  832. $answers[$slot] = $answer['answertext'];
  833. }
  834. // Now we set the order of the values matching the correct answer and set it to the first element
  835. $optionsValues = [];
  836. foreach ($answerValues as $slot => $answer) {
  837. $correctAnswer = '';
  838. $othersAnswers = '';
  839. $correctAnswer .= $answer['answertext'].'|';
  840. foreach ($answers as $other) {
  841. if ($other !== $answer['answertext']) {
  842. $othersAnswers .= $other.'|';
  843. }
  844. }
  845. $optionsValues[$slot]['weight'] = 1;
  846. $optionsValues[$slot]['size'] = '200';
  847. $currentAnswers = htmlentities($correctAnswer.$othersAnswers);
  848. $currentAnswers = '['.substr($currentAnswers, 0, -1).'] ';
  849. $answer['questiontext'] = str_replace(
  850. '@@PLUGINFILE@@',
  851. '/courses/'.$coursePath.'/document/moodle',
  852. $answer['questiontext']
  853. );
  854. $placeholder .= '<p> '.strip_tags($answer['questiontext']).' '.$currentAnswers.' </p>';
  855. }
  856. return $optionsValues;
  857. break;
  858. default:
  859. return false;
  860. break;
  861. }
  862. }
  863. /**
  864. * get All files associated with a question
  865. *
  866. * @param $filesXml
  867. * @return array
  868. */
  869. public function getAllQuestionFiles($filesXml)
  870. {
  871. $moduleDoc = new DOMDocument();
  872. $moduleRes = @$moduleDoc->loadXML($filesXml);
  873. $allFiles = [];
  874. if ($moduleRes) {
  875. $activities = $moduleDoc->getElementsByTagName('file');
  876. foreach ($activities as $activity) {
  877. $currentItem = [];
  878. $thisIsAnInvalidItem = false;
  879. if ($activity->childNodes->length) {
  880. foreach ($activity->childNodes as $item) {
  881. if ($item->nodeName == 'component' && $item->nodeValue == 'mod_resource') {
  882. $thisIsAnInvalidItem = true;
  883. }
  884. if ($item->nodeName == 'contenthash') {
  885. $currentItem['contenthash'] = $item->nodeValue;
  886. }
  887. if ($item->nodeName == 'filename') {
  888. $currentItem['filename'] = $item->nodeValue;
  889. }
  890. if ($item->nodeName == 'filesize') {
  891. $currentItem['filesize'] = $item->nodeValue;
  892. }
  893. if ($item->nodeName == 'mimetype' && $item->nodeValue == 'document/unknown') {
  894. $thisIsAnInvalidItem = true;
  895. }
  896. if ($item->nodeName == 'mimetype' && $item->nodeValue !== 'document/unknown') {
  897. $currentItem['mimetype'] = $item->nodeValue;
  898. }
  899. }
  900. }
  901. if (!$thisIsAnInvalidItem) {
  902. $allFiles[] = $currentItem;
  903. }
  904. }
  905. }
  906. return $allFiles;
  907. }
  908. /**
  909. * Litle utility to delete the unuseful tags
  910. *
  911. * @param $array
  912. * @param $keys
  913. */
  914. public function traverseArray(&$array, $keys)
  915. {
  916. foreach ($array as $key => &$value) {
  917. if (is_array($value)) {
  918. $this->traverseArray($value, $keys);
  919. } else {
  920. if (in_array($key, $keys)) {
  921. unset($array[$key]);
  922. }
  923. }
  924. }
  925. }
  926. }