PageRenderTime 524ms CodeModel.GetById 80ms RepoModel.GetById 15ms app.codeStats 1ms

/backup/cc/entity.quiz.class.php

https://bitbucket.org/ceu/moodle_demo
PHP | 958 lines | 636 code | 302 blank | 20 comment | 110 complexity | 0af8c4522ebb622882af10b03d22bdee MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * @package moodlecore
  18. * @subpackage backup-imscc
  19. * @copyright 2009 Mauro Rondinelli (mauro.rondinelli [AT] uvcms.com)
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
  23. class quiz extends entities {
  24. private $namespaces = array('xmlns' => 'http://www.imsglobal.org/xsd/ims_qtiasiv1p2');
  25. public function generate_node_question_categories () {
  26. $instances = $this->generate_instances();
  27. $node_course_question_categories = $this->create_node_course_question_categories($instances);
  28. $node_course_question_categories = empty($node_course_question_categories) ? '' : $node_course_question_categories;
  29. return $node_course_question_categories;
  30. }
  31. public function generate_node_course_modules_mod () {
  32. cc2moodle::log_action('Creating Quiz mods');
  33. $node_course_modules_mod = '';
  34. $instances = $this->generate_instances();
  35. if (!empty($instances)) {
  36. foreach ($instances as $instance) {
  37. if ($instance['is_question_bank'] == 0) {
  38. $node_course_modules_mod .= $this->create_node_course_modules_mod($instance);
  39. }
  40. }
  41. }
  42. return $node_course_modules_mod;
  43. }
  44. private function create_node_course_modules_mod_quiz_feedback () {
  45. $sheet_question_mod_feedback = cc2moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_QUIZ_FEEDBACK);
  46. return $sheet_question_mod_feedback;
  47. }
  48. private function generate_instances () {
  49. $last_instance_id = 0;
  50. $last_question_id = 0;
  51. $last_answer_id = 0;
  52. $instances = '';
  53. $types = array(MOODLE_TYPE_QUIZ, MOODLE_TYPE_QUESTION_BANK);
  54. foreach ($types as $type) {
  55. if (!empty(cc2moodle::$instances['instances'][$type])) {
  56. foreach (cc2moodle::$instances['instances'][$type] as $instance) {
  57. if ($type == MOODLE_TYPE_QUIZ) {
  58. $is_question_bank = 0;
  59. } else {
  60. $is_question_bank = 1;
  61. }
  62. $assessment_file = $this->get_external_xml($instance['resource_indentifier']);
  63. if (!empty($assessment_file)) {
  64. $assessment = $this->load_xml_resource(cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $assessment_file);
  65. if (!empty($assessment)) {
  66. $replace_values = array('unlimited' => 0);
  67. $questions = $this->get_questions($assessment, $last_question_id, $last_answer_id, dirname($assessment_file), $is_question_bank);
  68. $question_count = count($questions);
  69. if (!empty($question_count)) {
  70. $last_instance_id++;
  71. $instances[$instance['resource_indentifier']]['questions'] = $questions;
  72. $instances[$instance['resource_indentifier']]['id'] = $last_instance_id;
  73. $instances[$instance['resource_indentifier']]['title'] = $instance['title'];
  74. $instances[$instance['resource_indentifier']]['is_question_bank'] = $is_question_bank;
  75. $instances[$instance['resource_indentifier']]['options']['timelimit'] = $this->get_global_config($assessment, 'qmd_timelimit', 0);
  76. $instances[$instance['resource_indentifier']]['options']['max_attempts'] = $this->get_global_config($assessment, 'cc_maxattempts', 0, $replace_values);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. return $instances;
  84. }
  85. private function create_node_course_modules_mod ($instance) {
  86. $sheet_question_mod = cc2moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_QUIZ);
  87. $node_course_modules_quiz_question_instances = $this->create_node_course_modules_mod_quiz_question_instances($instance);
  88. $node_course_modules_quiz_feedback = $this->create_node_course_modules_mod_quiz_feedback($instance);
  89. $questions_strings = $this->get_questions_string($instance);
  90. $quiz_stamp = 'localhost+' . time() . '+' . $this->generate_random_string(6);
  91. $find_tags = array('[#mod_id#]',
  92. '[#mod_name#]',
  93. '[#mod_intro#]',
  94. '[#mod_stamp#]',
  95. '[#question_string#]',
  96. '[#date_now#]',
  97. '[#mod_max_attempts#]',
  98. '[#mod_timelimit#]',
  99. '[#node_question_instance#]',
  100. '[#node_questions_feedback#]');
  101. $replace_values = array($instance['id'],
  102. $instance['title'],
  103. $instance['title'],
  104. $quiz_stamp,
  105. $questions_strings,
  106. time(),
  107. $instance['options']['max_attempts'],
  108. $instance['options']['timelimit'],
  109. $node_course_modules_quiz_question_instances,
  110. $node_course_modules_quiz_feedback);
  111. $node_question_mod = str_replace($find_tags, $replace_values, $sheet_question_mod);
  112. return $node_question_mod;
  113. }
  114. private function get_global_config ($assessment, $option, $default_value, $replace_values = '') {
  115. $xpath = cc2moodle::newx_path($assessment, $this->namespaces);
  116. $metadata = $xpath->query('/xmlns:questestinterop/xmlns:assessment/xmlns:qtimetadata/xmlns:qtimetadatafield');
  117. foreach ($metadata as $field) {
  118. $field_label = $xpath->query('xmlns:fieldlabel', $field);
  119. $field_label = !empty($field_label->item(0)->nodeValue) ? $field_label->item(0)->nodeValue : '';
  120. if (strtolower($field_label) == strtolower($option)) {
  121. $field_entry = $xpath->query('xmlns:fieldentry', $field);
  122. $response = !empty($field_entry->item(0)->nodeValue) ? $field_entry->item(0)->nodeValue : '';
  123. }
  124. }
  125. $response = !empty($response) ? trim($response) : '';
  126. if (!empty($replace_values)) {
  127. foreach ($replace_values as $key => $value) {
  128. $response = ($key == $response) ? $value : $response;
  129. }
  130. }
  131. $response = empty($response) ? $default_value : $response;
  132. return $response;
  133. }
  134. private function create_node_course_modules_mod_quiz_question_instances ($instance) {
  135. $node_course_module_mod_quiz_questions_instances = '';
  136. $sheet_question_mod_instance = cc2moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_QUIZ_QUESTION_INSTANCE);
  137. $find_tags = array('[#question_id#]' , '[#instance_id#]');
  138. if (!empty($instance['questions'])) {
  139. foreach ($instance['questions'] as $question) {
  140. $replace_values = array($question['id'] , $question['id']);
  141. $node_course_module_mod_quiz_questions_instances .= str_replace($find_tags, $replace_values, $sheet_question_mod_instance);
  142. }
  143. $node_course_module_mod_quiz_questions_instances = str_replace($find_tags, $replace_values, $node_course_module_mod_quiz_questions_instances);
  144. }
  145. return $node_course_module_mod_quiz_questions_instances;
  146. }
  147. private function get_questions_string ($instance) {
  148. $questions_string = '';
  149. if (!empty($instance['questions'])) {
  150. foreach ($instance['questions'] as $question) {
  151. $questions_string .= $question['id'] . ',';
  152. }
  153. }
  154. $questions_string = !empty($questions_string) ? substr($questions_string, 0, strlen($questions_string) - 1) : '';
  155. return $questions_string;
  156. }
  157. private function create_node_course_question_categories ($instances) {
  158. $sheet_question_categories = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES);
  159. if (!empty($instances)) {
  160. $node_course_question_categories_question_category = '';
  161. foreach ($instances as $instance) {
  162. $node_course_question_categories_question_category .= $this->create_node_course_question_categories_question_category($instance);
  163. }
  164. $find_tags = array('[#node_course_question_categories_question_category#]');
  165. $replace_values = array($node_course_question_categories_question_category);
  166. $node_course_question_categories = str_replace($find_tags, $replace_values, $sheet_question_categories);
  167. }
  168. $node_course_question_categories = empty($node_course_question_categories) ? '' : $node_course_question_categories;
  169. return $node_course_question_categories;
  170. }
  171. private function create_node_course_question_categories_question_category ($instance) {
  172. $sheet_question_categories_quetion_category = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY);
  173. $find_tags = array('[#quiz_id#]',
  174. '[#quiz_name#]',
  175. '[#quiz_stamp#]',
  176. '[#node_course_question_categories_question_category_questions#]');
  177. $node_course_question_categories_questions = $this->create_node_course_question_categories_question_category_question($instance);
  178. $node_course_question_categories_questions = empty($node_course_question_categories_questions) ? '' : $node_course_question_categories_questions;
  179. $quiz_stamp = 'localhost+' . time() . '+' . $this->generate_random_string(6);
  180. $replace_values = array($instance['id'],
  181. $instance['title'],
  182. $quiz_stamp,
  183. $node_course_question_categories_questions);
  184. $node_question_categories = str_replace($find_tags, $replace_values, $sheet_question_categories_quetion_category);
  185. return $node_question_categories;
  186. }
  187. private function create_node_course_question_categories_question_category_question ($instance) {
  188. global $USER;
  189. $node_course_question_categories_question = '';
  190. $find_tags = array('[#question_id#]',
  191. '[#question_title#]',
  192. '[#question_text#]',
  193. '[#question_type#]',
  194. '[#question_general_feedback#]',
  195. '[#date_now#]',
  196. '[#question_type_nodes#]',
  197. '[#question_stamp#]',
  198. '[#question_version#]',
  199. '[#logged_user#]');
  200. $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION);
  201. $questions = $instance['questions'];
  202. if (!empty($questions)) {
  203. foreach ($questions as $question) {
  204. $quiz_stamp = 'localhost+' . time() . '+' . $this->generate_random_string(6);
  205. $quiz_version = 'localhost+' . time() . '+' . $this->generate_random_string(6);
  206. $question_moodle_type = $question['moodle_type'];
  207. $question_cc_type = $question['cc_type'];
  208. $question_type_node = '';
  209. $question_type_node = ($question_moodle_type == MOODLE_QUIZ_MULTIPLE_CHOICE) ? $this->create_node_course_question_categories_question_category_question_multiple_choice($question) : $question_type_node;
  210. $question_type_node = ($question_moodle_type == MOODLE_QUIZ_TRUE_FALSE) ? $this->create_node_course_question_categories_question_category_question_true_false($question) : $question_type_node;
  211. $question_type_node = ($question_moodle_type == MOODLE_QUIZ_ESSAY) ? $this->create_node_course_question_categories_question_category_question_eesay($question) : $question_type_node;
  212. $question_type_node = ($question_moodle_type == MOODLE_QUIZ_SHORTANSWER) ? $this->create_node_course_question_categories_question_category_question_shortanswer($question) : $question_type_node;
  213. $replace_values = array($question['id'],
  214. $this->truncate_text($question['title'], 255, true),
  215. $question['title'],
  216. $question_moodle_type,
  217. $question['feedback'],
  218. time(),
  219. $question_type_node,
  220. $quiz_stamp,
  221. $quiz_version,
  222. $USER->id);
  223. $node_course_question_categories_question .= str_replace($find_tags, $replace_values, $sheet_question_categories_question);
  224. }
  225. }
  226. $node_course_question_categories_question = empty($node_course_question_categories_question) ? '' : $node_course_question_categories_question;
  227. return $node_course_question_categories_question;
  228. }
  229. private function get_questions ($assessment, &$last_question_id, &$last_answer_id, $root_path, $is_question_bank) {
  230. $questions = array();
  231. $xpath = cc2moodle::newx_path($assessment, $this->namespaces);
  232. if (!$is_question_bank) {
  233. $questions_items = $xpath->query('/xmlns:questestinterop/xmlns:assessment/xmlns:section/xmlns:item');
  234. } else {
  235. $questions_items = $xpath->query('/xmlns:questestinterop/xmlns:objectbank/xmlns:item');
  236. }
  237. foreach ($questions_items as $question_item) {
  238. $count_questions = $xpath->evaluate('count(xmlns:presentation/xmlns:flow/xmlns:material/xmlns:mattext)', $question_item);
  239. if ($count_questions == 0) {
  240. $question_title = $xpath->query('xmlns:presentation/xmlns:material/xmlns:mattext', $question_item);
  241. } else {
  242. $question_title = $xpath->query('xmlns:presentation/xmlns:flow/xmlns:material/xmlns:mattext', $question_item);
  243. }
  244. $question_title = !empty($question_title->item(0)->nodeValue) ? $question_title->item(0)->nodeValue : '';
  245. $question_identifier = $xpath->query('@ident', $question_item);
  246. $question_identifier = !empty($question_identifier->item(0)->nodeValue) ? $question_identifier->item(0)->nodeValue : '';
  247. if (!empty($question_identifier)) {
  248. $question_type = $this->get_question_type($question_identifier, $assessment);
  249. if (!empty($question_type['moodle'])) {
  250. $last_question_id++;
  251. $questions[$question_identifier]['id'] = $last_question_id;
  252. $question_title = $this->update_sources($question_title, $root_path);
  253. $question_title = !empty($question_title) ? str_replace("%24", "\$", $this->include_titles($question_title)) : '';
  254. $questions[$question_identifier]['title'] = $question_title;
  255. $questions[$question_identifier]['identifier'] = $question_identifier;
  256. $questions[$question_identifier]['moodle_type'] = $question_type['moodle'];
  257. $questions[$question_identifier]['cc_type'] = $question_type['cc'];
  258. $questions[$question_identifier]['feedback'] = $this->get_general_feedback($assessment, $question_identifier);
  259. $questions[$question_identifier]['answers'] = $this->get_answers($question_identifier, $assessment, $last_answer_id);
  260. }
  261. }
  262. }
  263. $questions = !empty($questions) ? $questions : '';
  264. return $questions;
  265. }
  266. private function str_replace_once ($search, $replace, $subject) {
  267. $first_char = strpos($subject, $search);
  268. if ($first_char !== false) {
  269. $before_str = substr($subject, 0, $first_char);
  270. $after_str = substr($subject, $first_char + strlen($search));
  271. return $before_str . $replace . $after_str;
  272. } else {
  273. return $subject;
  274. }
  275. }
  276. private function get_general_feedback ($assessment, $question_identifier) {
  277. $xpath = cc2moodle::newx_path($assessment, $this->namespaces);
  278. $respconditions = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
  279. if (!empty($respconditions)) {
  280. foreach ($respconditions as $respcondition) {
  281. $continue = $respcondition->getAttributeNode('continue');
  282. $continue = !empty($continue->nodeValue) ? strtolower($continue->nodeValue) : '';
  283. if ($continue == 'yes') {
  284. $display_feedback = $xpath->query('xmlns:displayfeedback', $respcondition);
  285. if (!empty($display_feedback)) {
  286. foreach ($display_feedback as $feedback) {
  287. $feedback_identifier = $feedback->getAttributeNode('linkrefid');
  288. $feedback_identifier = !empty($feedback_identifier->nodeValue) ? $feedback_identifier->nodeValue : '';
  289. if (!empty($feedback_identifier)) {
  290. $feedbacks_identifiers[] = $feedback_identifier;
  291. }
  292. }
  293. }
  294. }
  295. }
  296. }
  297. $feedback = '';
  298. $feedbacks_identifiers = empty($feedbacks_identifiers) ? '' : $feedbacks_identifiers;
  299. if (!empty($feedbacks_identifiers)) {
  300. foreach ($feedbacks_identifiers as $feedback_identifier) {
  301. $feedbacks = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:itemfeedback[@ident="' . $feedback_identifier . '"]/xmlns:flow_mat/xmlns:material/xmlns:mattext');
  302. $feedback .= !empty($feedbacks->item(0)->nodeValue) ? $feedbacks->item(0)->nodeValue . ' ' : '';
  303. }
  304. }
  305. return $feedback;
  306. }
  307. private function get_feedback ($assessment, $identifier, $item_identifier, $question_type) {
  308. $xpath = cc2moodle::newx_path($assessment, $this->namespaces);
  309. $resource_processing = $xpath->query('//xmlns:item[@ident="' . $item_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
  310. if (!empty($resource_processing)) {
  311. foreach ($resource_processing as $response) {
  312. $varequal = $xpath->query('xmlns:conditionvar/xmlns:varequal', $response);
  313. $varequal = !empty($varequal->item(0)->nodeValue) ? $varequal->item(0)->nodeValue : '';
  314. if (strtolower($varequal) == strtolower($identifier) || ($question_type == CC_QUIZ_ESSAY)) {
  315. $display_feedback = $xpath->query('xmlns:displayfeedback', $response);
  316. if (!empty($display_feedback)) {
  317. foreach ($display_feedback as $feedback) {
  318. $feedback_identifier = $feedback->getAttributeNode('linkrefid');
  319. $feedback_identifier = !empty($feedback_identifier->nodeValue) ? $feedback_identifier->nodeValue : '';
  320. if (!empty($feedback_identifier)) {
  321. $feedbacks_identifiers[] = $feedback_identifier;
  322. }
  323. }
  324. }
  325. }
  326. }
  327. }
  328. $feedback = '';
  329. $feedbacks_identifiers = empty($feedbacks_identifiers) ? '' : $feedbacks_identifiers;
  330. if (!empty($feedbacks_identifiers)) {
  331. foreach ($feedbacks_identifiers as $feedback_identifier) {
  332. $feedbacks = $xpath->query('//xmlns:item[@ident="' . $item_identifier . '"]/xmlns:itemfeedback[@ident="' . $feedback_identifier . '"]/xmlns:flow_mat/xmlns:material/xmlns:mattext');
  333. $feedback .= !empty($feedbacks->item(0)->nodeValue) ? $feedbacks->item(0)->nodeValue . ' ' : '';
  334. }
  335. }
  336. return $feedback;
  337. }
  338. private function get_answers_fib ($question_identifier, $identifier, $assessment, &$last_answer_id) {
  339. $xpath = cc2moodle::newx_path($assessment, $this->namespaces);
  340. $answers_fib = array();
  341. $response_items = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
  342. foreach ($response_items as $response_item) {
  343. $setvar = $xpath->query('xmlns:setvar', $response_item);
  344. $setvar = is_object($setvar->item(0)) ? $setvar->item(0)->nodeValue : '';
  345. if ($setvar != '') {
  346. $last_answer_id++;
  347. $answer_title = $xpath->query('xmlns:conditionvar/xmlns:varequal[@respident="' . $identifier . '"]', $response_item);
  348. $answer_title = !empty($answer_title->item(0)->nodeValue) ? $answer_title->item(0)->nodeValue : '';
  349. $case = $xpath->query('xmlns:conditionvar/xmlns:varequal/@case', $response_item);
  350. $case = is_object($case->item(0)) ? $case->item(0)->nodeValue : 'no'
  351. ;
  352. $case = strtolower($case) == 'yes' ? 1 :
  353. 0;
  354. $display_feedback = $xpath->query('xmlns:displayfeedback', $response_item);
  355. unset($feedbacks_identifiers);
  356. if (!empty($display_feedback)) {
  357. foreach ($display_feedback as $feedback) {
  358. $feedback_identifier = $feedback->getAttributeNode('linkrefid');
  359. $feedback_identifier = !empty($feedback_identifier->nodeValue) ? $feedback_identifier->nodeValue : '';
  360. if (!empty($feedback_identifier)) {
  361. $feedbacks_identifiers[] = $feedback_identifier;
  362. }
  363. }
  364. }
  365. $feedback = '';
  366. $feedbacks_identifiers = empty($feedbacks_identifiers) ? '' : $feedbacks_identifiers;
  367. if (!empty($feedbacks_identifiers)) {
  368. foreach ($feedbacks_identifiers as $feedback_identifier) {
  369. $feedbacks = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:itemfeedback[@ident="' . $feedback_identifier . '"]/xmlns:flow_mat/xmlns:material/xmlns:mattext');
  370. $feedback .= !empty($feedbacks->item(0)->nodeValue) ? $feedbacks->item(0)->nodeValue . ' ' : '';
  371. }
  372. }
  373. $answers_fib[] = array('id' => $last_answer_id,
  374. 'title' => $answer_title,
  375. 'score' => $setvar,
  376. 'feedback' => $feedback,
  377. 'case' => $case);
  378. }
  379. }
  380. $answers_fib = empty($answers_fib) ? '' : $answers_fib;
  381. return $answers_fib;
  382. }
  383. private function get_answers_pattern_match ($question_identifier, $identifier, $assessment, &$last_answer_id) {
  384. $xpath = cc2moodle::newx_path($assessment, $this->namespaces);
  385. $answers_fib = array();
  386. $response_items = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
  387. foreach ($response_items as $response_item) {
  388. $setvar = $xpath->query('xmlns:setvar', $response_item);
  389. $setvar = is_object($setvar->item(0)) ? $setvar->item(0)->nodeValue : '';
  390. if ($setvar != '') {
  391. $last_answer_id++;
  392. $answer_title = $xpath->query('xmlns:conditionvar/xmlns:varequal[@respident="' . $identifier . '"]', $response_item);
  393. $answer_title = !empty($answer_title->item(0)->nodeValue) ? $answer_title->item(0)->nodeValue : '';
  394. if (empty($answer_title)) {
  395. $answer_title = $xpath->query('xmlns:conditionvar/xmlns:varsubstring[@respident="' . $identifier . '"]', $response_item);
  396. $answer_title = !empty($answer_title->item(0)->nodeValue) ? '*' . $answer_title->item(0)->nodeValue . '*' : '';
  397. }
  398. if (empty($answer_title)) {
  399. $answer_title = '*';
  400. }
  401. $case = $xpath->query('xmlns:conditionvar/xmlns:varequal/@case', $response_item);
  402. $case = is_object($case->item(0)) ? $case->item(0)->nodeValue : 'no'
  403. ;
  404. $case = strtolower($case) == 'yes' ? 1 :
  405. 0;
  406. $display_feedback = $xpath->query('xmlns:displayfeedback', $response_item);
  407. unset($feedbacks_identifiers);
  408. if (!empty($display_feedback)) {
  409. foreach ($display_feedback as $feedback) {
  410. $feedback_identifier = $feedback->getAttributeNode('linkrefid');
  411. $feedback_identifier = !empty($feedback_identifier->nodeValue) ? $feedback_identifier->nodeValue : '';
  412. if (!empty($feedback_identifier)) {
  413. $feedbacks_identifiers[] = $feedback_identifier;
  414. }
  415. }
  416. }
  417. $feedback = '';
  418. $feedbacks_identifiers = empty($feedbacks_identifiers) ? '' : $feedbacks_identifiers;
  419. if (!empty($feedbacks_identifiers)) {
  420. foreach ($feedbacks_identifiers as $feedback_identifier) {
  421. $feedbacks = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:itemfeedback[@ident="' . $feedback_identifier . '"]/xmlns:flow_mat/xmlns:material/xmlns:mattext');
  422. $feedback .= !empty($feedbacks->item(0)->nodeValue) ? $feedbacks->item(0)->nodeValue . ' ' : '';
  423. }
  424. }
  425. $answers_fib[] = array('id' => $last_answer_id,
  426. 'title' => $answer_title,
  427. 'score' => $setvar,
  428. 'feedback' => $feedback,
  429. 'case' => $case);
  430. }
  431. }
  432. $answers_fib = empty($answers_fib) ? '' : $answers_fib;
  433. return $answers_fib;
  434. }
  435. private function get_answers ($identifier, $assessment, &$last_answer_id) {
  436. $xpath = cc2moodle::newx_path($assessment, $this->namespaces);
  437. $answers = array();
  438. $question_cc_type = $this->get_question_type($identifier, $assessment);
  439. $question_cc_type = $question_cc_type['cc'];
  440. if ($question_cc_type == CC_QUIZ_MULTIPLE_CHOICE || $question_cc_type == CC_QUIZ_MULTIPLE_RESPONSE || $question_cc_type == CC_QUIZ_TRUE_FALSE) {
  441. $query_answers = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:response_lid/xmlns:render_choice/xmlns:response_label';
  442. $query_answers_with_flow = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:flow/xmlns:response_lid/xmlns:render_choice/xmlns:response_label';
  443. $query_indentifer = '@ident';
  444. $query_title = 'xmlns:material/xmlns:mattext';
  445. }
  446. if ($question_cc_type == CC_QUIZ_ESSAY) {
  447. $query_answers = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:response_str';
  448. $query_answers_with_flow = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:flow/xmlns:response_str';
  449. $query_indentifer = '@ident';
  450. $query_title = 'xmlns:render_fib';
  451. }
  452. if ($question_cc_type == CC_QUIZ_FIB || $question_cc_type == CC_QUIZ_PATTERN_MACHT) {
  453. $xpath_query = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:response_str/@ident';
  454. $xpath_query_with_flow = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:flow/xmlns:response_str/@ident';
  455. $count_response = $xpath->evaluate('count(' . $xpath_query_with_flow . ')');
  456. if ($count_response == 0) {
  457. $answer_identifier = $xpath->query($xpath_query);
  458. } else {
  459. $answer_identifier = $xpath->query($xpath_query_with_flow);
  460. }
  461. $answer_identifier = !empty($answer_identifier->item(0)->nodeValue) ? $answer_identifier->item(0)->nodeValue : '';
  462. if ($question_cc_type == CC_QUIZ_FIB) {
  463. $answers = $this->get_answers_fib ($identifier, $answer_identifier, $assessment, $last_answer_id);
  464. } else {
  465. $answers = $this->get_answers_pattern_match ($identifier, $answer_identifier, $assessment, $last_answer_id);
  466. }
  467. } else {
  468. $count_response = $xpath->evaluate('count(' . $query_answers_with_flow . ')');
  469. if ($count_response == 0) {
  470. $response_items = $xpath->query($query_answers);
  471. } else {
  472. $response_items = $xpath->query($query_answers_with_flow);
  473. }
  474. if (!empty($response_items)) {
  475. foreach ($response_items as $response_item) {
  476. $last_answer_id++;
  477. $answer_identifier = $xpath->query($query_indentifer, $response_item);
  478. $answer_identifier = !empty($answer_identifier->item(0)->nodeValue) ? $answer_identifier->item(0)->nodeValue : '';
  479. $answer_title = $xpath->query($query_title, $response_item);
  480. $answer_title = !empty($answer_title->item(0)->nodeValue) ? $answer_title->item(0)->nodeValue : '';
  481. $answer_feedback = $this->get_feedback($assessment, $answer_identifier, $identifier, $question_cc_type);
  482. $answer_score = $this->get_score($assessment, $answer_identifier, $identifier);
  483. $answers[] = array('id' => $last_answer_id,
  484. 'title' => $answer_title,
  485. 'score' => $answer_score,
  486. 'identifier' => $answer_identifier,
  487. 'feedback' => $answer_feedback);
  488. }
  489. }
  490. }
  491. $answers = empty($answers) ? '' : $answers;
  492. return $answers;
  493. }
  494. private function get_score ($assessment, $identifier, $question_identifier) {
  495. $xpath = cc2moodle::newx_path($assessment, $this->namespaces);
  496. $resource_processing = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
  497. if (!empty($resource_processing)) {
  498. foreach ($resource_processing as $response) {
  499. $question_cc_type = $this->get_question_type($question_identifier, $assessment);
  500. $question_cc_type = $question_cc_type['cc'];
  501. $varequal = $xpath->query('xmlns:conditionvar/xmlns:varequal', $response);
  502. $varequal = !empty($varequal->item(0)->nodeValue) ? $varequal->item(0)->nodeValue : '';
  503. if (strtolower($varequal) == strtolower($identifier)) {
  504. $score = $xpath->query('xmlns:setvar', $response);
  505. $score = !empty($score->item(0)->nodeValue) ? $score->item(0)->nodeValue : '';
  506. }
  507. }
  508. }
  509. $score = empty($score) ? 0 : $score;
  510. return $score;
  511. }
  512. private function create_node_course_question_categories_question_category_question_multiple_choice ($question) {
  513. $node_course_question_categories_question_answer = '';
  514. $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_MULTIPLE_CHOICE);
  515. if (!empty($question['answers'])) {
  516. foreach ($question['answers'] as $answer) {
  517. $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer);
  518. }
  519. }
  520. $answer_string = $this->get_answers_string($question['answers']);
  521. $is_single = ($question['cc_type'] == CC_QUIZ_MULTIPLE_CHOICE) ? 1 : 0;
  522. $find_tags = array('[#node_course_question_categories_question_category_question_answer#]',
  523. '[#answer_string#]',
  524. '[#is_single#]');
  525. $replace_values = array($node_course_question_categories_question_answer,
  526. $answer_string,
  527. $is_single);
  528. $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);
  529. return $node_question_categories_question;
  530. }
  531. private function create_node_course_question_categories_question_category_question_eesay ($question) {
  532. $node_course_question_categories_question_answer = '';
  533. $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_EESAY);
  534. if (!empty($question['answers'])) {
  535. foreach ($question['answers'] as $answer) {
  536. $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer);
  537. }
  538. }
  539. $find_tags = array('[#node_course_question_categories_question_category_question_answer#]');
  540. $replace_values = array($node_course_question_categories_question_answer);
  541. $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);
  542. return $node_question_categories_question;
  543. }
  544. private function create_node_course_question_categories_question_category_question_shortanswer ($question) { //, &$fib_questions) {
  545. $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_SHORTANSWER);
  546. $node_course_question_categories_question_answer = '';
  547. if (!empty($question['answers'])) {
  548. foreach ($question['answers'] as $answer) {
  549. $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer);
  550. }
  551. }
  552. $answers_string = $this->get_answers_string($question['answers']);
  553. $use_case = 0;
  554. foreach ($question['answers'] as $answer) {
  555. if ($answer['case'] == 1) {
  556. $use_case = 1;
  557. }
  558. }
  559. $find_tags = array('[#answers_string#]',
  560. '[#use_case#]',
  561. '[#node_course_question_categories_question_category_question_answer#]');
  562. $replace_values = array($answers_string,
  563. $use_case,
  564. $node_course_question_categories_question_answer);
  565. $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);
  566. return $node_question_categories_question;
  567. }
  568. private function create_node_course_question_categories_question_category_question_true_false ($question) {
  569. $node_course_question_categories_question_answer = '';
  570. $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_TRUE_FALSE);
  571. $max_score = 0;
  572. $true_answer_id = 0;
  573. $false_answer_id = 0;
  574. if (!empty($question['answers'])) {
  575. foreach ($question['answers'] as $answer) {
  576. if ($answer['score'] > $max_score) {
  577. $max_score = $answer['score'];
  578. $true_answer_id = $answer['id'];
  579. }
  580. $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer);
  581. }
  582. foreach ($question['answers'] as $answer) {
  583. if ($answer['id'] != $true_answer_id) {
  584. $max_score = $answer['score'];
  585. $false_answer_id = $answer['id'];
  586. }
  587. }
  588. }
  589. $find_tags = array('[#node_course_question_categories_question_category_question_answer#]',
  590. '[#true_answer_id#]',
  591. '[#false_answer_id#]');
  592. $replace_values = array($node_course_question_categories_question_answer,
  593. $true_answer_id,
  594. $false_answer_id);
  595. $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);
  596. return $node_question_categories_question;
  597. }
  598. private function get_answers_string ($answers) {
  599. $answer_string = '';
  600. if (!empty($answers)) {
  601. foreach ($answers as $answer) {
  602. $answer_string .= $answer['id'] . ',';
  603. }
  604. }
  605. $answer_string = !empty($answer_string) ? substr($answer_string, 0, strlen($answer_string) - 1) : '';
  606. return $answer_string;
  607. }
  608. private function create_node_course_question_categories_question_category_question_answer ($answer) {
  609. $sheet_question_categories_question_answer = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_ANSWER);
  610. $find_tags = array('[#answer_id#]',
  611. '[#answer_text#]',
  612. '[#answer_score#]',
  613. '[#answer_feedback#]');
  614. $replace_values = array($answer['id'],
  615. $answer['title'],
  616. $answer['score'],
  617. $answer['feedback']);
  618. $node_question_categories_question_answer = str_replace($find_tags, $replace_values, $sheet_question_categories_question_answer);
  619. return $node_question_categories_question_answer;
  620. }
  621. private function get_question_type ($identifier, $assessment) {
  622. $xpath = cc2moodle::newx_path($assessment, $this->namespaces);
  623. $metadata = $xpath->query('//xmlns:item[@ident="' . $identifier . '"]/xmlns:itemmetadata/xmlns:qtimetadata/xmlns:qtimetadatafield');
  624. foreach ($metadata as $field) {
  625. $field_label = $xpath->query('xmlns:fieldlabel', $field);
  626. $field_label = !empty($field_label->item(0)->nodeValue) ? $field_label->item(0)->nodeValue : '';
  627. if ($field_label == 'cc_profile') {
  628. $field_entry = $xpath->query('xmlns:fieldentry', $field);
  629. $type = !empty($field_entry->item(0)->nodeValue) ? $field_entry->item(0)->nodeValue : '';
  630. }
  631. }
  632. $return_type = array();
  633. $return_type['moodle'] = '';
  634. $return_type['cc'] = $type;
  635. if ($type == CC_QUIZ_MULTIPLE_CHOICE) {
  636. $return_type['moodle'] = MOODLE_QUIZ_MULTIPLE_CHOICE;
  637. }
  638. if ($type == CC_QUIZ_MULTIPLE_RESPONSE) {
  639. $return_type['moodle'] = MOODLE_QUIZ_MULTIPLE_CHOICE;
  640. }
  641. if ($type == CC_QUIZ_TRUE_FALSE) {
  642. $return_type['moodle'] = MOODLE_QUIZ_TRUE_FALSE;
  643. }
  644. if ($type == CC_QUIZ_ESSAY) {
  645. $return_type['moodle'] = MOODLE_QUIZ_ESSAY;
  646. }
  647. if ($type == CC_QUIZ_FIB) {
  648. $return_type['moodle'] = MOODLE_QUIZ_SHORTANSWER;
  649. }
  650. if ($type == CC_QUIZ_PATTERN_MACHT) {
  651. $return_type['moodle'] = MOODLE_QUIZ_SHORTANSWER;
  652. }
  653. return $return_type;
  654. }
  655. }
  656. ?>