MoodleImport.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. }