PageRenderTime 28ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/kudutest1/moodlegit
PHP | 974 lines | 651 code | 302 blank | 21 comment | 114 complexity | 765f951b98d2bdfd3c0e00f0f46e5c16 MD5 | raw file
  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. * @copyright 2011 Darko Miletic (dmiletic@moodlerooms.com)
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
  24. class cc_quiz extends entities {
  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. self::safexml($instance['title']),
  103. self::safexml($instance['title']),
  104. self::safexml($quiz_stamp),
  105. self::safexml($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); //this one has tags
  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, cc2moodle::getquizns());
  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. self::safexml($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. '[#question_defaultgrade#]',
  196. '[#date_now#]',
  197. '[#question_type_nodes#]',
  198. '[#question_stamp#]',
  199. '[#question_version#]',
  200. '[#logged_user#]');
  201. $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION);
  202. $questions = $instance['questions'];
  203. if (!empty($questions)) {
  204. foreach ($questions as $question) {
  205. $quiz_stamp = 'localhost+' . time() . '+' . $this->generate_random_string(6);
  206. $quiz_version = 'localhost+' . time() . '+' . $this->generate_random_string(6);
  207. $question_moodle_type = $question['moodle_type'];
  208. $question_cc_type = $question['cc_type'];
  209. $question_type_node = '';
  210. $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;
  211. $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;
  212. $question_type_node = ($question_moodle_type == MOODLE_QUIZ_ESSAY) ? $this->create_node_course_question_categories_question_category_question_eesay($question) : $question_type_node;
  213. $question_type_node = ($question_moodle_type == MOODLE_QUIZ_SHORTANSWER) ? $this->create_node_course_question_categories_question_category_question_shortanswer($question) : $question_type_node;
  214. $replace_values = array($question['id'],
  215. self::safexml($this->truncate_text($question['title'], 255, true)),
  216. self::safexml($question['title']),
  217. $question_moodle_type,
  218. self::safexml($question['feedback']),
  219. $question['defaultgrade'], //default grade
  220. time(),
  221. $question_type_node,
  222. $quiz_stamp,
  223. $quiz_version,
  224. $USER->id);
  225. $node_course_question_categories_question .= str_replace($find_tags, $replace_values, $sheet_question_categories_question);
  226. }
  227. }
  228. $node_course_question_categories_question = empty($node_course_question_categories_question) ? '' : $node_course_question_categories_question;
  229. return $node_course_question_categories_question;
  230. }
  231. private function get_questions ($assessment, &$last_question_id, &$last_answer_id, $root_path, $is_question_bank) {
  232. $questions = array();
  233. $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
  234. if (!$is_question_bank) {
  235. $questions_items = $xpath->query('/xmlns:questestinterop/xmlns:assessment/xmlns:section/xmlns:item');
  236. } else {
  237. $questions_items = $xpath->query('/xmlns:questestinterop/xmlns:objectbank/xmlns:item');
  238. }
  239. foreach ($questions_items as $question_item) {
  240. $count_questions = $xpath->evaluate('count(xmlns:presentation/xmlns:flow/xmlns:material/xmlns:mattext)', $question_item);
  241. if ($count_questions == 0) {
  242. $question_title = $xpath->query('xmlns:presentation/xmlns:material/xmlns:mattext', $question_item);
  243. } else {
  244. $question_title = $xpath->query('xmlns:presentation/xmlns:flow/xmlns:material/xmlns:mattext', $question_item);
  245. }
  246. $question_title = !empty($question_title->item(0)->nodeValue) ? $question_title->item(0)->nodeValue : '';
  247. $question_identifier = $xpath->query('@ident', $question_item);
  248. $question_identifier = !empty($question_identifier->item(0)->nodeValue) ? $question_identifier->item(0)->nodeValue : '';
  249. if (!empty($question_identifier)) {
  250. $question_type = $this->get_question_type($question_identifier, $assessment);
  251. if (!empty($question_type['moodle'])) {
  252. $last_question_id++;
  253. $questions[$question_identifier]['id'] = $last_question_id;
  254. $question_title = $this->update_sources($question_title, $root_path);
  255. $question_title = !empty($question_title) ? str_replace("%24", "\$", $this->include_titles($question_title)) : '';
  256. $questions[$question_identifier]['title'] = $question_title;
  257. $questions[$question_identifier]['identifier'] = $question_identifier;
  258. $questions[$question_identifier]['moodle_type'] = $question_type['moodle'];
  259. $questions[$question_identifier]['cc_type'] = $question_type['cc'];
  260. $questions[$question_identifier]['feedback'] = $this->get_general_feedback($assessment, $question_identifier);
  261. $questions[$question_identifier]['defaultgrade'] = $this->get_defaultgrade($assessment, $question_identifier);
  262. $questions[$question_identifier]['answers'] = $this->get_answers($question_identifier, $assessment, $last_answer_id);
  263. }
  264. }
  265. }
  266. $questions = !empty($questions) ? $questions : '';
  267. return $questions;
  268. }
  269. private function str_replace_once ($search, $replace, $subject) {
  270. $first_char = strpos($subject, $search);
  271. if ($first_char !== false) {
  272. $before_str = substr($subject, 0, $first_char);
  273. $after_str = substr($subject, $first_char + strlen($search));
  274. return $before_str . $replace . $after_str;
  275. } else {
  276. return $subject;
  277. }
  278. }
  279. private function get_defaultgrade($assessment, $question_identifier) {
  280. $result = 1;
  281. $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
  282. $query = '//xmlns:item[@ident="' . $question_identifier . '"]';
  283. $query .= '//xmlns:qtimetadatafield[xmlns:fieldlabel="cc_weighting"]/xmlns:fieldentry';
  284. $defgrade = $xpath->query($query);
  285. if (!empty($defgrade) && ($defgrade->length > 0)) {
  286. $resp = (int)$defgrade->item(0)->nodeValue;
  287. if ($resp >= 0 && $resp <= 99) {
  288. $result = $resp;
  289. }
  290. }
  291. return $result;
  292. }
  293. private function get_general_feedback ($assessment, $question_identifier) {
  294. $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
  295. $respconditions = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
  296. if (!empty($respconditions)) {
  297. foreach ($respconditions as $respcondition) {
  298. $continue = $respcondition->getAttributeNode('continue');
  299. $continue = !empty($continue->nodeValue) ? strtolower($continue->nodeValue) : '';
  300. if ($continue == 'yes') {
  301. $display_feedback = $xpath->query('xmlns:displayfeedback', $respcondition);
  302. if (!empty($display_feedback)) {
  303. foreach ($display_feedback as $feedback) {
  304. $feedback_identifier = $feedback->getAttributeNode('linkrefid');
  305. $feedback_identifier = !empty($feedback_identifier->nodeValue) ? $feedback_identifier->nodeValue : '';
  306. if (!empty($feedback_identifier)) {
  307. $feedbacks_identifiers[] = $feedback_identifier;
  308. }
  309. }
  310. }
  311. }
  312. }
  313. }
  314. $feedback = '';
  315. $feedbacks_identifiers = empty($feedbacks_identifiers) ? '' : $feedbacks_identifiers;
  316. if (!empty($feedbacks_identifiers)) {
  317. foreach ($feedbacks_identifiers as $feedback_identifier) {
  318. $feedbacks = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:itemfeedback[@ident="' . $feedback_identifier . '"]/xmlns:flow_mat/xmlns:material/xmlns:mattext');
  319. $feedback .= !empty($feedbacks->item(0)->nodeValue) ? $feedbacks->item(0)->nodeValue . ' ' : '';
  320. }
  321. }
  322. return $feedback;
  323. }
  324. private function get_feedback ($assessment, $identifier, $item_identifier, $question_type) {
  325. $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
  326. $resource_processing = $xpath->query('//xmlns:item[@ident="' . $item_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
  327. if (!empty($resource_processing)) {
  328. foreach ($resource_processing as $response) {
  329. $varequal = $xpath->query('xmlns:conditionvar/xmlns:varequal', $response);
  330. $varequal = !empty($varequal->item(0)->nodeValue) ? $varequal->item(0)->nodeValue : '';
  331. if (strtolower($varequal) == strtolower($identifier) || ($question_type == CC_QUIZ_ESSAY)) {
  332. $display_feedback = $xpath->query('xmlns:displayfeedback', $response);
  333. if (!empty($display_feedback)) {
  334. foreach ($display_feedback as $feedback) {
  335. $feedback_identifier = $feedback->getAttributeNode('linkrefid');
  336. $feedback_identifier = !empty($feedback_identifier->nodeValue) ? $feedback_identifier->nodeValue : '';
  337. if (!empty($feedback_identifier)) {
  338. $feedbacks_identifiers[] = $feedback_identifier;
  339. }
  340. }
  341. }
  342. }
  343. }
  344. }
  345. $feedback = '';
  346. $feedbacks_identifiers = empty($feedbacks_identifiers) ? '' : $feedbacks_identifiers;
  347. if (!empty($feedbacks_identifiers)) {
  348. foreach ($feedbacks_identifiers as $feedback_identifier) {
  349. $feedbacks = $xpath->query('//xmlns:item[@ident="' . $item_identifier . '"]/xmlns:itemfeedback[@ident="' . $feedback_identifier . '"]/xmlns:flow_mat/xmlns:material/xmlns:mattext');
  350. $feedback .= !empty($feedbacks->item(0)->nodeValue) ? $feedbacks->item(0)->nodeValue . ' ' : '';
  351. }
  352. }
  353. return $feedback;
  354. }
  355. private function get_answers_fib ($question_identifier, $identifier, $assessment, &$last_answer_id) {
  356. $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
  357. $answers_fib = array();
  358. $response_items = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
  359. foreach ($response_items as $response_item) {
  360. $setvar = $xpath->query('xmlns:setvar', $response_item);
  361. $setvar = is_object($setvar->item(0)) ? $setvar->item(0)->nodeValue : '';
  362. if ($setvar != '') {
  363. $last_answer_id++;
  364. $answer_title = $xpath->query('xmlns:conditionvar/xmlns:varequal[@respident="' . $identifier . '"]', $response_item);
  365. $answer_title = !empty($answer_title->item(0)->nodeValue) ? $answer_title->item(0)->nodeValue : '';
  366. $case = $xpath->query('xmlns:conditionvar/xmlns:varequal/@case', $response_item);
  367. $case = is_object($case->item(0)) ? $case->item(0)->nodeValue : 'no'
  368. ;
  369. $case = strtolower($case) == 'yes' ? 1 :
  370. 0;
  371. $display_feedback = $xpath->query('xmlns:displayfeedback', $response_item);
  372. unset($feedbacks_identifiers);
  373. if (!empty($display_feedback)) {
  374. foreach ($display_feedback as $feedback) {
  375. $feedback_identifier = $feedback->getAttributeNode('linkrefid');
  376. $feedback_identifier = !empty($feedback_identifier->nodeValue) ? $feedback_identifier->nodeValue : '';
  377. if (!empty($feedback_identifier)) {
  378. $feedbacks_identifiers[] = $feedback_identifier;
  379. }
  380. }
  381. }
  382. $feedback = '';
  383. $feedbacks_identifiers = empty($feedbacks_identifiers) ? '' : $feedbacks_identifiers;
  384. if (!empty($feedbacks_identifiers)) {
  385. foreach ($feedbacks_identifiers as $feedback_identifier) {
  386. $feedbacks = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:itemfeedback[@ident="' . $feedback_identifier . '"]/xmlns:flow_mat/xmlns:material/xmlns:mattext');
  387. $feedback .= !empty($feedbacks->item(0)->nodeValue) ? $feedbacks->item(0)->nodeValue . ' ' : '';
  388. }
  389. }
  390. $answers_fib[] = array('id' => $last_answer_id,
  391. 'title' => $answer_title,
  392. 'score' => $setvar,
  393. 'feedback' => $feedback,
  394. 'case' => $case);
  395. }
  396. }
  397. $answers_fib = empty($answers_fib) ? '' : $answers_fib;
  398. return $answers_fib;
  399. }
  400. private function get_answers_pattern_match ($question_identifier, $identifier, $assessment, &$last_answer_id) {
  401. $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
  402. $answers_fib = array();
  403. $response_items = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
  404. foreach ($response_items as $response_item) {
  405. $setvar = $xpath->query('xmlns:setvar', $response_item);
  406. $setvar = is_object($setvar->item(0)) ? $setvar->item(0)->nodeValue : '';
  407. if ($setvar != '') {
  408. $last_answer_id++;
  409. $answer_title = $xpath->query('xmlns:conditionvar/xmlns:varequal[@respident="' . $identifier . '"]', $response_item);
  410. $answer_title = !empty($answer_title->item(0)->nodeValue) ? $answer_title->item(0)->nodeValue : '';
  411. if (empty($answer_title)) {
  412. $answer_title = $xpath->query('xmlns:conditionvar/xmlns:varsubstring[@respident="' . $identifier . '"]', $response_item);
  413. $answer_title = !empty($answer_title->item(0)->nodeValue) ? '*' . $answer_title->item(0)->nodeValue . '*' : '';
  414. }
  415. if (empty($answer_title)) {
  416. $answer_title = '*';
  417. }
  418. $case = $xpath->query('xmlns:conditionvar/xmlns:varequal/@case', $response_item);
  419. $case = is_object($case->item(0)) ? $case->item(0)->nodeValue : 'no'
  420. ;
  421. $case = strtolower($case) == 'yes' ? 1 :
  422. 0;
  423. $display_feedback = $xpath->query('xmlns:displayfeedback', $response_item);
  424. unset($feedbacks_identifiers);
  425. if (!empty($display_feedback)) {
  426. foreach ($display_feedback as $feedback) {
  427. $feedback_identifier = $feedback->getAttributeNode('linkrefid');
  428. $feedback_identifier = !empty($feedback_identifier->nodeValue) ? $feedback_identifier->nodeValue : '';
  429. if (!empty($feedback_identifier)) {
  430. $feedbacks_identifiers[] = $feedback_identifier;
  431. }
  432. }
  433. }
  434. $feedback = '';
  435. $feedbacks_identifiers = empty($feedbacks_identifiers) ? '' : $feedbacks_identifiers;
  436. if (!empty($feedbacks_identifiers)) {
  437. foreach ($feedbacks_identifiers as $feedback_identifier) {
  438. $feedbacks = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:itemfeedback[@ident="' . $feedback_identifier . '"]/xmlns:flow_mat/xmlns:material/xmlns:mattext');
  439. $feedback .= !empty($feedbacks->item(0)->nodeValue) ? $feedbacks->item(0)->nodeValue . ' ' : '';
  440. }
  441. }
  442. $answers_fib[] = array('id' => $last_answer_id,
  443. 'title' => $answer_title,
  444. 'score' => $setvar,
  445. 'feedback' => $feedback,
  446. 'case' => $case);
  447. }
  448. }
  449. $answers_fib = empty($answers_fib) ? '' : $answers_fib;
  450. return $answers_fib;
  451. }
  452. private function get_answers ($identifier, $assessment, &$last_answer_id) {
  453. $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
  454. $answers = array();
  455. $question_cc_type = $this->get_question_type($identifier, $assessment);
  456. $question_cc_type = $question_cc_type['cc'];
  457. if ($question_cc_type == CC_QUIZ_MULTIPLE_CHOICE || $question_cc_type == CC_QUIZ_MULTIPLE_RESPONSE || $question_cc_type == CC_QUIZ_TRUE_FALSE) {
  458. $query_answers = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:response_lid/xmlns:render_choice/xmlns:response_label';
  459. $query_answers_with_flow = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:flow/xmlns:response_lid/xmlns:render_choice/xmlns:response_label';
  460. $query_indentifer = '@ident';
  461. $query_title = 'xmlns:material/xmlns:mattext';
  462. }
  463. if ($question_cc_type == CC_QUIZ_ESSAY) {
  464. $query_answers = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:response_str';
  465. $query_answers_with_flow = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:flow/xmlns:response_str';
  466. $query_indentifer = '@ident';
  467. $query_title = 'xmlns:render_fib';
  468. }
  469. if ($question_cc_type == CC_QUIZ_FIB || $question_cc_type == CC_QUIZ_PATTERN_MACHT) {
  470. $xpath_query = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:response_str/@ident';
  471. $xpath_query_with_flow = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:flow/xmlns:response_str/@ident';
  472. $count_response = $xpath->evaluate('count(' . $xpath_query_with_flow . ')');
  473. if ($count_response == 0) {
  474. $answer_identifier = $xpath->query($xpath_query);
  475. } else {
  476. $answer_identifier = $xpath->query($xpath_query_with_flow);
  477. }
  478. $answer_identifier = !empty($answer_identifier->item(0)->nodeValue) ? $answer_identifier->item(0)->nodeValue : '';
  479. if ($question_cc_type == CC_QUIZ_FIB) {
  480. $answers = $this->get_answers_fib ($identifier, $answer_identifier, $assessment, $last_answer_id);
  481. } else {
  482. $answers = $this->get_answers_pattern_match ($identifier, $answer_identifier, $assessment, $last_answer_id);
  483. }
  484. } else {
  485. $count_response = $xpath->evaluate('count(' . $query_answers_with_flow . ')');
  486. if ($count_response == 0) {
  487. $response_items = $xpath->query($query_answers);
  488. } else {
  489. $response_items = $xpath->query($query_answers_with_flow);
  490. }
  491. if (!empty($response_items)) {
  492. foreach ($response_items as $response_item) {
  493. $last_answer_id++;
  494. $answer_identifier = $xpath->query($query_indentifer, $response_item);
  495. $answer_identifier = !empty($answer_identifier->item(0)->nodeValue) ? $answer_identifier->item(0)->nodeValue : '';
  496. $answer_title = $xpath->query($query_title, $response_item);
  497. $answer_title = !empty($answer_title->item(0)->nodeValue) ? $answer_title->item(0)->nodeValue : '';
  498. $answer_feedback = $this->get_feedback($assessment, $answer_identifier, $identifier, $question_cc_type);
  499. $answer_score = $this->get_score($assessment, $answer_identifier, $identifier);
  500. $answers[] = array('id' => $last_answer_id,
  501. 'title' => $answer_title,
  502. 'score' => $answer_score,
  503. 'identifier' => $answer_identifier,
  504. 'feedback' => $answer_feedback);
  505. }
  506. }
  507. }
  508. $answers = empty($answers) ? '' : $answers;
  509. return $answers;
  510. }
  511. private function get_score ($assessment, $identifier, $question_identifier) {
  512. $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
  513. $resource_processing = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
  514. if (!empty($resource_processing)) {
  515. foreach ($resource_processing as $response) {
  516. $question_cc_type = $this->get_question_type($question_identifier, $assessment);
  517. $question_cc_type = $question_cc_type['cc'];
  518. $varequal = $xpath->query('xmlns:conditionvar/xmlns:varequal', $response);
  519. $varequal = !empty($varequal->item(0)->nodeValue) ? $varequal->item(0)->nodeValue : '';
  520. if (strtolower($varequal) == strtolower($identifier)) {
  521. $score = $xpath->query('xmlns:setvar', $response);
  522. $score = !empty($score->item(0)->nodeValue) ? $score->item(0)->nodeValue : '';
  523. }
  524. }
  525. }
  526. $score = empty($score) ? 0 : $score;
  527. return $score;
  528. }
  529. private function create_node_course_question_categories_question_category_question_multiple_choice ($question) {
  530. $node_course_question_categories_question_answer = '';
  531. $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_MULTIPLE_CHOICE);
  532. if (!empty($question['answers'])) {
  533. foreach ($question['answers'] as $answer) {
  534. $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer);
  535. }
  536. }
  537. $answer_string = $this->get_answers_string($question['answers']);
  538. $is_single = ($question['cc_type'] == CC_QUIZ_MULTIPLE_CHOICE) ? 1 : 0;
  539. $find_tags = array('[#node_course_question_categories_question_category_question_answer#]',
  540. '[#answer_string#]',
  541. '[#is_single#]');
  542. $replace_values = array($node_course_question_categories_question_answer,
  543. self::safexml($answer_string),
  544. $is_single);
  545. $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);
  546. return $node_question_categories_question;
  547. }
  548. private function create_node_course_question_categories_question_category_question_eesay ($question) {
  549. $node_course_question_categories_question_answer = '';
  550. $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_EESAY);
  551. if (!empty($question['answers'])) {
  552. foreach ($question['answers'] as $answer) {
  553. $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer);
  554. }
  555. }
  556. $find_tags = array('[#node_course_question_categories_question_category_question_answer#]');
  557. $replace_values = array($node_course_question_categories_question_answer);
  558. $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);
  559. return $node_question_categories_question;
  560. }
  561. private function create_node_course_question_categories_question_category_question_shortanswer ($question) { //, &$fib_questions) {
  562. $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_SHORTANSWER);
  563. $node_course_question_categories_question_answer = '';
  564. if (!empty($question['answers'])) {
  565. foreach ($question['answers'] as $answer) {
  566. $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer);
  567. }
  568. }
  569. $answers_string = $this->get_answers_string($question['answers']);
  570. $use_case = 0;
  571. foreach ($question['answers'] as $answer) {
  572. if ($answer['case'] == 1) {
  573. $use_case = 1;
  574. }
  575. }
  576. $find_tags = array('[#answers_string#]',
  577. '[#use_case#]',
  578. '[#node_course_question_categories_question_category_question_answer#]');
  579. $replace_values = array(self::safexml($answers_string),
  580. self::safexml($use_case),
  581. $node_course_question_categories_question_answer);
  582. $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);
  583. return $node_question_categories_question;
  584. }
  585. private function create_node_course_question_categories_question_category_question_true_false ($question) {
  586. $node_course_question_categories_question_answer = '';
  587. $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_TRUE_FALSE);
  588. $max_score = 0;
  589. $true_answer_id = 0;
  590. $false_answer_id = 0;
  591. if (!empty($question['answers'])) {
  592. foreach ($question['answers'] as $answer) {
  593. if ($answer['score'] > $max_score) {
  594. $max_score = $answer['score'];
  595. $true_answer_id = $answer['id'];
  596. }
  597. $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer);
  598. }
  599. foreach ($question['answers'] as $answer) {
  600. if ($answer['id'] != $true_answer_id) {
  601. $max_score = $answer['score'];
  602. $false_answer_id = $answer['id'];
  603. }
  604. }
  605. }
  606. $find_tags = array('[#node_course_question_categories_question_category_question_answer#]',
  607. '[#true_answer_id#]',
  608. '[#false_answer_id#]');
  609. $replace_values = array($node_course_question_categories_question_answer,
  610. $true_answer_id,
  611. $false_answer_id);
  612. $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);
  613. return $node_question_categories_question;
  614. }
  615. private function get_answers_string ($answers) {
  616. $answer_string = '';
  617. if (!empty($answers)) {
  618. foreach ($answers as $answer) {
  619. $answer_string .= $answer['id'] . ',';
  620. }
  621. }
  622. $answer_string = !empty($answer_string) ? substr($answer_string, 0, strlen($answer_string) - 1) : '';
  623. return $answer_string;
  624. }
  625. private function create_node_course_question_categories_question_category_question_answer ($answer) {
  626. $sheet_question_categories_question_answer = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_ANSWER);
  627. $find_tags = array('[#answer_id#]',
  628. '[#answer_text#]',
  629. '[#answer_score#]',
  630. '[#answer_feedback#]');
  631. $replace_values = array($answer['id'],
  632. self::safexml($answer['title']),
  633. $answer['score'],
  634. self::safexml($answer['feedback']));
  635. $node_question_categories_question_answer = str_replace($find_tags, $replace_values, $sheet_question_categories_question_answer);
  636. return $node_question_categories_question_answer;
  637. }
  638. private function get_question_type ($identifier, $assessment) {
  639. $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
  640. $metadata = $xpath->query('//xmlns:item[@ident="' . $identifier . '"]/xmlns:itemmetadata/xmlns:qtimetadata/xmlns:qtimetadatafield');
  641. foreach ($metadata as $field) {
  642. $field_label = $xpath->query('xmlns:fieldlabel', $field);
  643. $field_label = !empty($field_label->item(0)->nodeValue) ? $field_label->item(0)->nodeValue : '';
  644. if ($field_label == 'cc_profile') {
  645. $field_entry = $xpath->query('xmlns:fieldentry', $field);
  646. $type = !empty($field_entry->item(0)->nodeValue) ? $field_entry->item(0)->nodeValue : '';
  647. }
  648. }
  649. $return_type = array();
  650. $return_type['moodle'] = '';
  651. $return_type['cc'] = $type;
  652. if ($type == CC_QUIZ_MULTIPLE_CHOICE) {
  653. $return_type['moodle'] = MOODLE_QUIZ_MULTIPLE_CHOICE;
  654. }
  655. if ($type == CC_QUIZ_MULTIPLE_RESPONSE) {
  656. $return_type['moodle'] = MOODLE_QUIZ_MULTIPLE_CHOICE;
  657. }
  658. if ($type == CC_QUIZ_TRUE_FALSE) {
  659. $return_type['moodle'] = MOODLE_QUIZ_TRUE_FALSE;
  660. }
  661. if ($type == CC_QUIZ_ESSAY) {
  662. $return_type['moodle'] = MOODLE_QUIZ_ESSAY;
  663. }
  664. if ($type == CC_QUIZ_FIB) {
  665. $return_type['moodle'] = MOODLE_QUIZ_SHORTANSWER;
  666. }
  667. if ($type == CC_QUIZ_PATTERN_MACHT) {
  668. $return_type['moodle'] = MOODLE_QUIZ_SHORTANSWER;
  669. }
  670. return $return_type;
  671. }
  672. }