MDL-30514 registration: table aliases don't use AS keyword
[moodle.git] / backup / cc / entity.quiz.class.php
blob4bb8c32b839b3d2317ac034fdd3ccbef083eabf5
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.
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
24 defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
26 class cc_quiz extends entities {
28 public function generate_node_question_categories () {
30 $instances = $this->generate_instances();
32 $node_course_question_categories = $this->create_node_course_question_categories($instances);
33 $node_course_question_categories = empty($node_course_question_categories) ? '' : $node_course_question_categories;
35 return $node_course_question_categories;
39 public function generate_node_course_modules_mod () {
41 cc2moodle::log_action('Creating Quiz mods');
43 $node_course_modules_mod = '';
44 $instances = $this->generate_instances();
46 if (!empty($instances)) {
47 foreach ($instances as $instance) {
48 if ($instance['is_question_bank'] == 0) {
49 $node_course_modules_mod .= $this->create_node_course_modules_mod($instance);
54 return $node_course_modules_mod;
58 private function create_node_course_modules_mod_quiz_feedback () {
60 $sheet_question_mod_feedback = cc2moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_QUIZ_FEEDBACK);
62 return $sheet_question_mod_feedback;
65 private function generate_instances () {
67 $last_instance_id = 0;
68 $last_question_id = 0;
69 $last_answer_id = 0;
71 $instances = '';
73 $types = array(MOODLE_TYPE_QUIZ, MOODLE_TYPE_QUESTION_BANK);
75 foreach ($types as $type) {
77 if (!empty(cc2moodle::$instances['instances'][$type])) {
79 foreach (cc2moodle::$instances['instances'][$type] as $instance) {
81 if ($type == MOODLE_TYPE_QUIZ) {
82 $is_question_bank = 0;
83 } else {
84 $is_question_bank = 1;
87 $assessment_file = $this->get_external_xml($instance['resource_indentifier']);
89 if (!empty($assessment_file)) {
91 $assessment = $this->load_xml_resource(cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $assessment_file);
93 if (!empty($assessment)) {
95 $replace_values = array('unlimited' => 0);
97 $questions = $this->get_questions($assessment, $last_question_id, $last_answer_id, dirname($assessment_file), $is_question_bank);
98 $question_count = count($questions);
100 if (!empty($question_count)) {
102 $last_instance_id++;
104 $instances[$instance['resource_indentifier']]['questions'] = $questions;
105 $instances[$instance['resource_indentifier']]['id'] = $last_instance_id;
106 $instances[$instance['resource_indentifier']]['title'] = $instance['title'];
107 $instances[$instance['resource_indentifier']]['is_question_bank'] = $is_question_bank;
108 $instances[$instance['resource_indentifier']]['options']['timelimit'] = $this->get_global_config($assessment, 'qmd_timelimit', 0);
109 $instances[$instance['resource_indentifier']]['options']['max_attempts'] = $this->get_global_config($assessment, 'cc_maxattempts', 0, $replace_values);
117 return $instances;
121 private function create_node_course_modules_mod ($instance) {
123 $sheet_question_mod = cc2moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_QUIZ);
125 $node_course_modules_quiz_question_instances = $this->create_node_course_modules_mod_quiz_question_instances($instance);
126 $node_course_modules_quiz_feedback = $this->create_node_course_modules_mod_quiz_feedback($instance);
128 $questions_strings = $this->get_questions_string($instance);
129 $quiz_stamp = 'localhost+' . time() . '+' . $this->generate_random_string(6);
131 $find_tags = array('[#mod_id#]',
132 '[#mod_name#]',
133 '[#mod_intro#]',
134 '[#mod_stamp#]',
135 '[#question_string#]',
136 '[#date_now#]',
137 '[#mod_max_attempts#]',
138 '[#mod_timelimit#]',
139 '[#node_question_instance#]',
140 '[#node_questions_feedback#]');
142 $replace_values = array($instance['id'],
143 self::safexml($instance['title']),
144 self::safexml($instance['title']),
145 self::safexml($quiz_stamp),
146 self::safexml($questions_strings),
147 time(),
148 $instance['options']['max_attempts'],
149 $instance['options']['timelimit'],
150 $node_course_modules_quiz_question_instances,
151 $node_course_modules_quiz_feedback); //this one has tags
153 $node_question_mod = str_replace($find_tags, $replace_values, $sheet_question_mod);
155 return $node_question_mod;
158 private function get_global_config ($assessment, $option, $default_value, $replace_values = '') {
160 $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
161 $metadata = $xpath->query('/xmlns:questestinterop/xmlns:assessment/xmlns:qtimetadata/xmlns:qtimetadatafield');
163 foreach ($metadata as $field) {
164 $field_label = $xpath->query('xmlns:fieldlabel', $field);
165 $field_label = !empty($field_label->item(0)->nodeValue) ? $field_label->item(0)->nodeValue : '';
167 if (strtolower($field_label) == strtolower($option)) {
168 $field_entry = $xpath->query('xmlns:fieldentry', $field);
169 $response = !empty($field_entry->item(0)->nodeValue) ? $field_entry->item(0)->nodeValue : '';
173 $response = !empty($response) ? trim($response) : '';
175 if (!empty($replace_values)) {
176 foreach ($replace_values as $key => $value) {
177 $response = ($key == $response) ? $value : $response;
181 $response = empty($response) ? $default_value : $response;
183 return $response;
186 private function create_node_course_modules_mod_quiz_question_instances ($instance) {
188 $node_course_module_mod_quiz_questions_instances = '';
189 $sheet_question_mod_instance = cc2moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_QUIZ_QUESTION_INSTANCE);
191 $find_tags = array('[#question_id#]' , '[#instance_id#]');
193 if (!empty($instance['questions'])) {
195 foreach ($instance['questions'] as $question) {
196 $replace_values = array($question['id'] , $question['id']);
197 $node_course_module_mod_quiz_questions_instances .= str_replace($find_tags, $replace_values, $sheet_question_mod_instance);
200 $node_course_module_mod_quiz_questions_instances = str_replace($find_tags, $replace_values, $node_course_module_mod_quiz_questions_instances);
203 return $node_course_module_mod_quiz_questions_instances;
206 private function get_questions_string ($instance) {
208 $questions_string = '';
210 if (!empty($instance['questions'])) {
211 foreach ($instance['questions'] as $question) {
212 $questions_string .= $question['id'] . ',';
216 $questions_string = !empty($questions_string) ? substr($questions_string, 0, strlen($questions_string) - 1) : '';
218 return $questions_string;
221 private function create_node_course_question_categories ($instances) {
223 $sheet_question_categories = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES);
225 if (!empty($instances)) {
227 $node_course_question_categories_question_category = '';
229 foreach ($instances as $instance) {
230 $node_course_question_categories_question_category .= $this->create_node_course_question_categories_question_category($instance);
233 $find_tags = array('[#node_course_question_categories_question_category#]');
234 $replace_values = array($node_course_question_categories_question_category);
236 $node_course_question_categories = str_replace($find_tags, $replace_values, $sheet_question_categories);
239 $node_course_question_categories = empty($node_course_question_categories) ? '' : $node_course_question_categories;
241 return $node_course_question_categories;
244 private function create_node_course_question_categories_question_category ($instance) {
246 $sheet_question_categories_quetion_category = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY);
248 $find_tags = array('[#quiz_id#]',
249 '[#quiz_name#]',
250 '[#quiz_stamp#]',
251 '[#node_course_question_categories_question_category_questions#]');
253 $node_course_question_categories_questions = $this->create_node_course_question_categories_question_category_question($instance);
254 $node_course_question_categories_questions = empty($node_course_question_categories_questions) ? '' : $node_course_question_categories_questions;
256 $quiz_stamp = 'localhost+' . time() . '+' . $this->generate_random_string(6);
258 $replace_values = array($instance['id'],
259 self::safexml($instance['title']),
260 $quiz_stamp,
261 $node_course_question_categories_questions);
263 $node_question_categories = str_replace($find_tags, $replace_values, $sheet_question_categories_quetion_category);
265 return $node_question_categories;
268 private function create_node_course_question_categories_question_category_question ($instance) {
270 global $USER;
272 $node_course_question_categories_question = '';
274 $find_tags = array('[#question_id#]',
275 '[#question_title#]',
276 '[#question_text#]',
277 '[#question_type#]',
278 '[#question_general_feedback#]',
279 '[#question_defaultgrade#]',
280 '[#date_now#]',
281 '[#question_type_nodes#]',
282 '[#question_stamp#]',
283 '[#question_version#]',
284 '[#logged_user#]');
286 $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION);
288 $questions = $instance['questions'];
290 if (!empty($questions)) {
292 foreach ($questions as $question) {
294 $quiz_stamp = 'localhost+' . time() . '+' . $this->generate_random_string(6);
295 $quiz_version = 'localhost+' . time() . '+' . $this->generate_random_string(6);
297 $question_moodle_type = $question['moodle_type'];
298 $question_cc_type = $question['cc_type'];
300 $question_type_node = '';
302 $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;
303 $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;
304 $question_type_node = ($question_moodle_type == MOODLE_QUIZ_ESSAY) ? $this->create_node_course_question_categories_question_category_question_eesay($question) : $question_type_node;
305 $question_type_node = ($question_moodle_type == MOODLE_QUIZ_SHORTANSWER) ? $this->create_node_course_question_categories_question_category_question_shortanswer($question) : $question_type_node;
307 $replace_values = array($question['id'],
308 self::safexml($this->truncate_text($question['title'], 255, true)),
309 self::safexml($question['title']),
310 $question_moodle_type,
311 self::safexml($question['feedback']),
312 $question['defaultgrade'], //default grade
313 time(),
314 $question_type_node,
315 $quiz_stamp,
316 $quiz_version,
317 $USER->id);
319 $node_course_question_categories_question .= str_replace($find_tags, $replace_values, $sheet_question_categories_question);
323 $node_course_question_categories_question = empty($node_course_question_categories_question) ? '' : $node_course_question_categories_question;
325 return $node_course_question_categories_question;
328 private function get_questions ($assessment, &$last_question_id, &$last_answer_id, $root_path, $is_question_bank) {
330 $questions = array();
332 $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
334 if (!$is_question_bank) {
335 $questions_items = $xpath->query('/xmlns:questestinterop/xmlns:assessment/xmlns:section/xmlns:item');
336 } else {
337 $questions_items = $xpath->query('/xmlns:questestinterop/xmlns:objectbank/xmlns:item');
340 foreach ($questions_items as $question_item) {
342 $count_questions = $xpath->evaluate('count(xmlns:presentation/xmlns:flow/xmlns:material/xmlns:mattext)', $question_item);
344 if ($count_questions == 0) {
345 $question_title = $xpath->query('xmlns:presentation/xmlns:material/xmlns:mattext', $question_item);
346 } else {
347 $question_title = $xpath->query('xmlns:presentation/xmlns:flow/xmlns:material/xmlns:mattext', $question_item);
350 $question_title = !empty($question_title->item(0)->nodeValue) ? $question_title->item(0)->nodeValue : '';
352 $question_identifier = $xpath->query('@ident', $question_item);
353 $question_identifier = !empty($question_identifier->item(0)->nodeValue) ? $question_identifier->item(0)->nodeValue : '';
355 if (!empty($question_identifier)) {
357 $question_type = $this->get_question_type($question_identifier, $assessment);
359 if (!empty($question_type['moodle'])) {
361 $last_question_id++;
363 $questions[$question_identifier]['id'] = $last_question_id;
365 $question_title = $this->update_sources($question_title, $root_path);
366 $question_title = !empty($question_title) ? str_replace("%24", "\$", $this->include_titles($question_title)) : '';
368 $questions[$question_identifier]['title'] = $question_title;
369 $questions[$question_identifier]['identifier'] = $question_identifier;
370 $questions[$question_identifier]['moodle_type'] = $question_type['moodle'];
371 $questions[$question_identifier]['cc_type'] = $question_type['cc'];
372 $questions[$question_identifier]['feedback'] = $this->get_general_feedback($assessment, $question_identifier);
373 $questions[$question_identifier]['defaultgrade'] = $this->get_defaultgrade($assessment, $question_identifier);
374 $questions[$question_identifier]['answers'] = $this->get_answers($question_identifier, $assessment, $last_answer_id);
380 $questions = !empty($questions) ? $questions : '';
382 return $questions;
385 private function str_replace_once ($search, $replace, $subject) {
387 $first_char = strpos($subject, $search);
389 if ($first_char !== false) {
391 $before_str = substr($subject, 0, $first_char);
392 $after_str = substr($subject, $first_char + strlen($search));
394 return $before_str . $replace . $after_str;
396 } else {
397 return $subject;
401 private function get_defaultgrade($assessment, $question_identifier) {
402 $result = 1;
403 $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
404 $query = '//xmlns:item[@ident="' . $question_identifier . '"]';
405 $query .= '//xmlns:qtimetadatafield[xmlns:fieldlabel="cc_weighting"]/xmlns:fieldentry';
406 $defgrade = $xpath->query($query);
407 if (!empty($defgrade) && ($defgrade->length > 0)) {
408 $resp = (int)$defgrade->item(0)->nodeValue;
409 if ($resp >= 0 && $resp <= 99) {
410 $result = $resp;
413 return $result;
416 private function get_general_feedback ($assessment, $question_identifier) {
418 $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
420 $respconditions = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
422 if (!empty($respconditions)) {
424 foreach ($respconditions as $respcondition) {
426 $continue = $respcondition->getAttributeNode('continue');
427 $continue = !empty($continue->nodeValue) ? strtolower($continue->nodeValue) : '';
429 if ($continue == 'yes') {
431 $display_feedback = $xpath->query('xmlns:displayfeedback', $respcondition);
433 if (!empty($display_feedback)) {
434 foreach ($display_feedback as $feedback) {
436 $feedback_identifier = $feedback->getAttributeNode('linkrefid');
437 $feedback_identifier = !empty($feedback_identifier->nodeValue) ? $feedback_identifier->nodeValue : '';
439 if (!empty($feedback_identifier)) {
440 $feedbacks_identifiers[] = $feedback_identifier;
448 $feedback = '';
449 $feedbacks_identifiers = empty($feedbacks_identifiers) ? '' : $feedbacks_identifiers;
451 if (!empty($feedbacks_identifiers)) {
452 foreach ($feedbacks_identifiers as $feedback_identifier) {
453 $feedbacks = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:itemfeedback[@ident="' . $feedback_identifier . '"]/xmlns:flow_mat/xmlns:material/xmlns:mattext');
454 $feedback .= !empty($feedbacks->item(0)->nodeValue) ? $feedbacks->item(0)->nodeValue . ' ' : '';
458 return $feedback;
461 private function get_feedback ($assessment, $identifier, $item_identifier, $question_type) {
463 $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
465 $resource_processing = $xpath->query('//xmlns:item[@ident="' . $item_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
467 if (!empty($resource_processing)) {
469 foreach ($resource_processing as $response) {
471 $varequal = $xpath->query('xmlns:conditionvar/xmlns:varequal', $response);
472 $varequal = !empty($varequal->item(0)->nodeValue) ? $varequal->item(0)->nodeValue : '';
474 if (strtolower($varequal) == strtolower($identifier) || ($question_type == CC_QUIZ_ESSAY)) {
476 $display_feedback = $xpath->query('xmlns:displayfeedback', $response);
478 if (!empty($display_feedback)) {
479 foreach ($display_feedback as $feedback) {
481 $feedback_identifier = $feedback->getAttributeNode('linkrefid');
482 $feedback_identifier = !empty($feedback_identifier->nodeValue) ? $feedback_identifier->nodeValue : '';
484 if (!empty($feedback_identifier)) {
485 $feedbacks_identifiers[] = $feedback_identifier;
493 $feedback = '';
494 $feedbacks_identifiers = empty($feedbacks_identifiers) ? '' : $feedbacks_identifiers;
496 if (!empty($feedbacks_identifiers)) {
497 foreach ($feedbacks_identifiers as $feedback_identifier) {
498 $feedbacks = $xpath->query('//xmlns:item[@ident="' . $item_identifier . '"]/xmlns:itemfeedback[@ident="' . $feedback_identifier . '"]/xmlns:flow_mat/xmlns:material/xmlns:mattext');
499 $feedback .= !empty($feedbacks->item(0)->nodeValue) ? $feedbacks->item(0)->nodeValue . ' ' : '';
503 return $feedback;
506 private function get_answers_fib ($question_identifier, $identifier, $assessment, &$last_answer_id) {
508 $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
510 $answers_fib = array();
512 $response_items = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
514 foreach ($response_items as $response_item) {
516 $setvar = $xpath->query('xmlns:setvar', $response_item);
517 $setvar = is_object($setvar->item(0)) ? $setvar->item(0)->nodeValue : '';
519 if ($setvar != '') {
521 $last_answer_id++;
523 $answer_title = $xpath->query('xmlns:conditionvar/xmlns:varequal[@respident="' . $identifier . '"]', $response_item);
524 $answer_title = !empty($answer_title->item(0)->nodeValue) ? $answer_title->item(0)->nodeValue : '';
526 $case = $xpath->query('xmlns:conditionvar/xmlns:varequal/@case', $response_item);
527 $case = is_object($case->item(0)) ? $case->item(0)->nodeValue : 'no'
529 $case = strtolower($case) == 'yes' ? 1 :
532 $display_feedback = $xpath->query('xmlns:displayfeedback', $response_item);
534 unset($feedbacks_identifiers);
536 if (!empty($display_feedback)) {
538 foreach ($display_feedback as $feedback) {
540 $feedback_identifier = $feedback->getAttributeNode('linkrefid');
541 $feedback_identifier = !empty($feedback_identifier->nodeValue) ? $feedback_identifier->nodeValue : '';
543 if (!empty($feedback_identifier)) {
544 $feedbacks_identifiers[] = $feedback_identifier;
549 $feedback = '';
550 $feedbacks_identifiers = empty($feedbacks_identifiers) ? '' : $feedbacks_identifiers;
552 if (!empty($feedbacks_identifiers)) {
553 foreach ($feedbacks_identifiers as $feedback_identifier) {
554 $feedbacks = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:itemfeedback[@ident="' . $feedback_identifier . '"]/xmlns:flow_mat/xmlns:material/xmlns:mattext');
555 $feedback .= !empty($feedbacks->item(0)->nodeValue) ? $feedbacks->item(0)->nodeValue . ' ' : '';
559 $answers_fib[] = array('id' => $last_answer_id,
560 'title' => $answer_title,
561 'score' => $setvar,
562 'feedback' => $feedback,
563 'case' => $case);
567 $answers_fib = empty($answers_fib) ? '' : $answers_fib;
569 return $answers_fib;
572 private function get_answers_pattern_match ($question_identifier, $identifier, $assessment, &$last_answer_id) {
574 $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
576 $answers_fib = array();
578 $response_items = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
580 foreach ($response_items as $response_item) {
582 $setvar = $xpath->query('xmlns:setvar', $response_item);
583 $setvar = is_object($setvar->item(0)) ? $setvar->item(0)->nodeValue : '';
585 if ($setvar != '') {
587 $last_answer_id++;
589 $answer_title = $xpath->query('xmlns:conditionvar/xmlns:varequal[@respident="' . $identifier . '"]', $response_item);
590 $answer_title = !empty($answer_title->item(0)->nodeValue) ? $answer_title->item(0)->nodeValue : '';
592 if (empty($answer_title)) {
593 $answer_title = $xpath->query('xmlns:conditionvar/xmlns:varsubstring[@respident="' . $identifier . '"]', $response_item);
594 $answer_title = !empty($answer_title->item(0)->nodeValue) ? '*' . $answer_title->item(0)->nodeValue . '*' : '';
597 if (empty($answer_title)) {
598 $answer_title = '*';
601 $case = $xpath->query('xmlns:conditionvar/xmlns:varequal/@case', $response_item);
602 $case = is_object($case->item(0)) ? $case->item(0)->nodeValue : 'no'
604 $case = strtolower($case) == 'yes' ? 1 :
607 $display_feedback = $xpath->query('xmlns:displayfeedback', $response_item);
609 unset($feedbacks_identifiers);
611 if (!empty($display_feedback)) {
613 foreach ($display_feedback as $feedback) {
615 $feedback_identifier = $feedback->getAttributeNode('linkrefid');
616 $feedback_identifier = !empty($feedback_identifier->nodeValue) ? $feedback_identifier->nodeValue : '';
618 if (!empty($feedback_identifier)) {
619 $feedbacks_identifiers[] = $feedback_identifier;
624 $feedback = '';
625 $feedbacks_identifiers = empty($feedbacks_identifiers) ? '' : $feedbacks_identifiers;
627 if (!empty($feedbacks_identifiers)) {
628 foreach ($feedbacks_identifiers as $feedback_identifier) {
629 $feedbacks = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:itemfeedback[@ident="' . $feedback_identifier . '"]/xmlns:flow_mat/xmlns:material/xmlns:mattext');
630 $feedback .= !empty($feedbacks->item(0)->nodeValue) ? $feedbacks->item(0)->nodeValue . ' ' : '';
634 $answers_fib[] = array('id' => $last_answer_id,
635 'title' => $answer_title,
636 'score' => $setvar,
637 'feedback' => $feedback,
638 'case' => $case);
642 $answers_fib = empty($answers_fib) ? '' : $answers_fib;
644 return $answers_fib;
648 private function get_answers ($identifier, $assessment, &$last_answer_id) {
650 $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
652 $answers = array();
654 $question_cc_type = $this->get_question_type($identifier, $assessment);
655 $question_cc_type = $question_cc_type['cc'];
657 if ($question_cc_type == CC_QUIZ_MULTIPLE_CHOICE || $question_cc_type == CC_QUIZ_MULTIPLE_RESPONSE || $question_cc_type == CC_QUIZ_TRUE_FALSE) {
659 $query_answers = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:response_lid/xmlns:render_choice/xmlns:response_label';
660 $query_answers_with_flow = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:flow/xmlns:response_lid/xmlns:render_choice/xmlns:response_label';
662 $query_indentifer = '@ident';
663 $query_title = 'xmlns:material/xmlns:mattext';
666 if ($question_cc_type == CC_QUIZ_ESSAY) {
668 $query_answers = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:response_str';
669 $query_answers_with_flow = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:flow/xmlns:response_str';
671 $query_indentifer = '@ident';
672 $query_title = 'xmlns:render_fib';
675 if ($question_cc_type == CC_QUIZ_FIB || $question_cc_type == CC_QUIZ_PATTERN_MACHT) {
677 $xpath_query = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:response_str/@ident';
678 $xpath_query_with_flow = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:flow/xmlns:response_str/@ident';
680 $count_response = $xpath->evaluate('count(' . $xpath_query_with_flow . ')');
682 if ($count_response == 0) {
683 $answer_identifier = $xpath->query($xpath_query);
684 } else {
685 $answer_identifier = $xpath->query($xpath_query_with_flow);
688 $answer_identifier = !empty($answer_identifier->item(0)->nodeValue) ? $answer_identifier->item(0)->nodeValue : '';
690 if ($question_cc_type == CC_QUIZ_FIB) {
691 $answers = $this->get_answers_fib ($identifier, $answer_identifier, $assessment, $last_answer_id);
692 } else {
693 $answers = $this->get_answers_pattern_match ($identifier, $answer_identifier, $assessment, $last_answer_id);
696 } else {
698 $count_response = $xpath->evaluate('count(' . $query_answers_with_flow . ')');
700 if ($count_response == 0) {
701 $response_items = $xpath->query($query_answers);
702 } else {
703 $response_items = $xpath->query($query_answers_with_flow);
706 if (!empty($response_items)) {
708 foreach ($response_items as $response_item) {
710 $last_answer_id++;
712 $answer_identifier = $xpath->query($query_indentifer, $response_item);
713 $answer_identifier = !empty($answer_identifier->item(0)->nodeValue) ? $answer_identifier->item(0)->nodeValue : '';
715 $answer_title = $xpath->query($query_title, $response_item);
716 $answer_title = !empty($answer_title->item(0)->nodeValue) ? $answer_title->item(0)->nodeValue : '';
718 $answer_feedback = $this->get_feedback($assessment, $answer_identifier, $identifier, $question_cc_type);
720 $answer_score = $this->get_score($assessment, $answer_identifier, $identifier);
722 $answers[] = array('id' => $last_answer_id,
723 'title' => $answer_title,
724 'score' => $answer_score,
725 'identifier' => $answer_identifier,
726 'feedback' => $answer_feedback);
731 $answers = empty($answers) ? '' : $answers;
733 return $answers;
737 private function get_score ($assessment, $identifier, $question_identifier) {
739 $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
741 $resource_processing = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
743 if (!empty($resource_processing)) {
745 foreach ($resource_processing as $response) {
747 $question_cc_type = $this->get_question_type($question_identifier, $assessment);
748 $question_cc_type = $question_cc_type['cc'];
750 $varequal = $xpath->query('xmlns:conditionvar/xmlns:varequal', $response);
751 $varequal = !empty($varequal->item(0)->nodeValue) ? $varequal->item(0)->nodeValue : '';
753 if (strtolower($varequal) == strtolower($identifier)) {
754 $score = $xpath->query('xmlns:setvar', $response);
755 $score = !empty($score->item(0)->nodeValue) ? $score->item(0)->nodeValue : '';
760 $score = empty($score) ? 0 : $score;
762 return $score;
765 private function create_node_course_question_categories_question_category_question_multiple_choice ($question) {
767 $node_course_question_categories_question_answer = '';
768 $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_MULTIPLE_CHOICE);
770 if (!empty($question['answers'])) {
771 foreach ($question['answers'] as $answer) {
772 $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer);
776 $answer_string = $this->get_answers_string($question['answers']);
778 $is_single = ($question['cc_type'] == CC_QUIZ_MULTIPLE_CHOICE) ? 1 : 0;
780 $find_tags = array('[#node_course_question_categories_question_category_question_answer#]',
781 '[#answer_string#]',
782 '[#is_single#]');
784 $replace_values = array($node_course_question_categories_question_answer,
785 self::safexml($answer_string),
786 $is_single);
788 $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);
790 return $node_question_categories_question;
793 private function create_node_course_question_categories_question_category_question_eesay ($question) {
795 $node_course_question_categories_question_answer = '';
797 $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_EESAY);
799 if (!empty($question['answers'])) {
800 foreach ($question['answers'] as $answer) {
801 $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer);
805 $find_tags = array('[#node_course_question_categories_question_category_question_answer#]');
806 $replace_values = array($node_course_question_categories_question_answer);
808 $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);
810 return $node_question_categories_question;
813 private function create_node_course_question_categories_question_category_question_shortanswer ($question) { //, &$fib_questions) {
815 $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_SHORTANSWER);
816 $node_course_question_categories_question_answer = '';
818 if (!empty($question['answers'])) {
819 foreach ($question['answers'] as $answer) {
820 $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer);
824 $answers_string = $this->get_answers_string($question['answers']);
826 $use_case = 0;
828 foreach ($question['answers'] as $answer) {
830 if ($answer['case'] == 1) {
831 $use_case = 1;
836 $find_tags = array('[#answers_string#]',
837 '[#use_case#]',
838 '[#node_course_question_categories_question_category_question_answer#]');
840 $replace_values = array(self::safexml($answers_string),
841 self::safexml($use_case),
842 $node_course_question_categories_question_answer);
846 $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);
848 return $node_question_categories_question;
852 private function create_node_course_question_categories_question_category_question_true_false ($question) {
854 $node_course_question_categories_question_answer = '';
856 $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_TRUE_FALSE);
858 $max_score = 0;
859 $true_answer_id = 0;
860 $false_answer_id = 0;
862 if (!empty($question['answers'])) {
864 foreach ($question['answers'] as $answer) {
865 if ($answer['score'] > $max_score) {
866 $max_score = $answer['score'];
867 $true_answer_id = $answer['id'];
870 $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer);
873 foreach ($question['answers'] as $answer) {
875 if ($answer['id'] != $true_answer_id) {
876 $max_score = $answer['score'];
877 $false_answer_id = $answer['id'];
882 $find_tags = array('[#node_course_question_categories_question_category_question_answer#]',
883 '[#true_answer_id#]',
884 '[#false_answer_id#]');
886 $replace_values = array($node_course_question_categories_question_answer,
887 $true_answer_id,
888 $false_answer_id);
890 $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);
892 return $node_question_categories_question;
895 private function get_answers_string ($answers) {
897 $answer_string = '';
899 if (!empty($answers)) {
900 foreach ($answers as $answer) {
901 $answer_string .= $answer['id'] . ',';
905 $answer_string = !empty($answer_string) ? substr($answer_string, 0, strlen($answer_string) - 1) : '';
907 return $answer_string;
911 private function create_node_course_question_categories_question_category_question_answer ($answer) {
913 $sheet_question_categories_question_answer = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_ANSWER);
915 $find_tags = array('[#answer_id#]',
916 '[#answer_text#]',
917 '[#answer_score#]',
918 '[#answer_feedback#]');
920 $replace_values = array($answer['id'],
921 self::safexml($answer['title']),
922 $answer['score'],
923 self::safexml($answer['feedback']));
925 $node_question_categories_question_answer = str_replace($find_tags, $replace_values, $sheet_question_categories_question_answer);
927 return $node_question_categories_question_answer;
930 private function get_question_type ($identifier, $assessment) {
932 $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
934 $metadata = $xpath->query('//xmlns:item[@ident="' . $identifier . '"]/xmlns:itemmetadata/xmlns:qtimetadata/xmlns:qtimetadatafield');
936 foreach ($metadata as $field) {
938 $field_label = $xpath->query('xmlns:fieldlabel', $field);
939 $field_label = !empty($field_label->item(0)->nodeValue) ? $field_label->item(0)->nodeValue : '';
941 if ($field_label == 'cc_profile') {
942 $field_entry = $xpath->query('xmlns:fieldentry', $field);
943 $type = !empty($field_entry->item(0)->nodeValue) ? $field_entry->item(0)->nodeValue : '';
947 $return_type = array();
949 $return_type['moodle'] = '';
950 $return_type['cc'] = $type;
952 if ($type == CC_QUIZ_MULTIPLE_CHOICE) {
953 $return_type['moodle'] = MOODLE_QUIZ_MULTIPLE_CHOICE;
955 if ($type == CC_QUIZ_MULTIPLE_RESPONSE) {
956 $return_type['moodle'] = MOODLE_QUIZ_MULTIPLE_CHOICE;
958 if ($type == CC_QUIZ_TRUE_FALSE) {
959 $return_type['moodle'] = MOODLE_QUIZ_TRUE_FALSE;
961 if ($type == CC_QUIZ_ESSAY) {
962 $return_type['moodle'] = MOODLE_QUIZ_ESSAY;
964 if ($type == CC_QUIZ_FIB) {
965 $return_type['moodle'] = MOODLE_QUIZ_SHORTANSWER;
967 if ($type == CC_QUIZ_PATTERN_MACHT) {
968 $return_type['moodle'] = MOODLE_QUIZ_SHORTANSWER;
971 return $return_type;