Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/cc/entity.quiz.class.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00024 defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
00025 
00026 class cc_quiz extends entities {
00027 
00028     public function generate_node_question_categories () {
00029 
00030         $instances = $this->generate_instances();
00031 
00032         $node_course_question_categories = $this->create_node_course_question_categories($instances);
00033         $node_course_question_categories = empty($node_course_question_categories) ? '' : $node_course_question_categories;
00034 
00035         return $node_course_question_categories;
00036 
00037     }
00038 
00039     public function generate_node_course_modules_mod () {
00040 
00041         cc2moodle::log_action('Creating Quiz mods');
00042 
00043         $node_course_modules_mod = '';
00044         $instances = $this->generate_instances();
00045 
00046         if (!empty($instances)) {
00047             foreach ($instances as $instance) {
00048                 if ($instance['is_question_bank'] == 0) {
00049                     $node_course_modules_mod .= $this->create_node_course_modules_mod($instance);
00050                 }
00051             }
00052         }
00053 
00054         return $node_course_modules_mod;
00055 
00056     }
00057 
00058     private function create_node_course_modules_mod_quiz_feedback () {
00059 
00060         $sheet_question_mod_feedback = cc2moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_QUIZ_FEEDBACK);
00061 
00062         return $sheet_question_mod_feedback;
00063     }
00064 
00065     private function generate_instances () {
00066 
00067         $last_instance_id = 0;
00068         $last_question_id = 0;
00069         $last_answer_id = 0;
00070 
00071         $instances = '';
00072 
00073         $types = array(MOODLE_TYPE_QUIZ, MOODLE_TYPE_QUESTION_BANK);
00074 
00075         foreach ($types as $type) {
00076 
00077             if (!empty(cc2moodle::$instances['instances'][$type])) {
00078 
00079                 foreach (cc2moodle::$instances['instances'][$type] as $instance) {
00080 
00081                     if ($type == MOODLE_TYPE_QUIZ) {
00082                         $is_question_bank = 0;
00083                     } else {
00084                         $is_question_bank = 1;
00085                     }
00086 
00087                     $assessment_file = $this->get_external_xml($instance['resource_indentifier']);
00088 
00089                     if (!empty($assessment_file)) {
00090 
00091                         $assessment = $this->load_xml_resource(cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $assessment_file);
00092 
00093                         if (!empty($assessment)) {
00094 
00095                             $replace_values = array('unlimited' => 0);
00096 
00097                             $questions = $this->get_questions($assessment, $last_question_id, $last_answer_id, dirname($assessment_file), $is_question_bank);
00098                             $question_count = count($questions);
00099 
00100                             if (!empty($question_count)) {
00101 
00102                                 $last_instance_id++;
00103 
00104                                 $instances[$instance['resource_indentifier']]['questions'] = $questions;
00105                                 $instances[$instance['resource_indentifier']]['id'] = $last_instance_id;
00106                                 $instances[$instance['resource_indentifier']]['title'] = $instance['title'];
00107                                 $instances[$instance['resource_indentifier']]['is_question_bank'] = $is_question_bank;
00108                                 $instances[$instance['resource_indentifier']]['options']['timelimit'] = $this->get_global_config($assessment, 'qmd_timelimit', 0);
00109                                 $instances[$instance['resource_indentifier']]['options']['max_attempts'] = $this->get_global_config($assessment, 'cc_maxattempts', 0, $replace_values);
00110                             }
00111                         }
00112                     }
00113                 }
00114             }
00115         }
00116 
00117         return $instances;
00118     }
00119 
00120 
00121     private function create_node_course_modules_mod ($instance) {
00122 
00123         $sheet_question_mod = cc2moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_QUIZ);
00124 
00125         $node_course_modules_quiz_question_instances = $this->create_node_course_modules_mod_quiz_question_instances($instance);
00126         $node_course_modules_quiz_feedback = $this->create_node_course_modules_mod_quiz_feedback($instance);
00127 
00128         $questions_strings = $this->get_questions_string($instance);
00129         $quiz_stamp = 'localhost+' . time() . '+' . $this->generate_random_string(6);
00130 
00131         $find_tags = array('[#mod_id#]',
00132                            '[#mod_name#]',
00133                            '[#mod_intro#]',
00134                            '[#mod_stamp#]',
00135                            '[#question_string#]',
00136                            '[#date_now#]',
00137                            '[#mod_max_attempts#]',
00138                            '[#mod_timelimit#]',
00139                            '[#node_question_instance#]',
00140                            '[#node_questions_feedback#]');
00141 
00142         $replace_values = array($instance['id'],
00143                                 self::safexml($instance['title']),
00144                                 self::safexml($instance['title']),
00145                                 self::safexml($quiz_stamp),
00146                                 self::safexml($questions_strings),
00147                                 time(),
00148                                 $instance['options']['max_attempts'],
00149                                 $instance['options']['timelimit'],
00150                                 $node_course_modules_quiz_question_instances,
00151                                 $node_course_modules_quiz_feedback); //this one has tags
00152 
00153         $node_question_mod = str_replace($find_tags, $replace_values, $sheet_question_mod);
00154 
00155         return $node_question_mod;
00156     }
00157 
00158     private function get_global_config ($assessment, $option, $default_value, $replace_values = '') {
00159 
00160         $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
00161         $metadata = $xpath->query('/xmlns:questestinterop/xmlns:assessment/xmlns:qtimetadata/xmlns:qtimetadatafield');
00162 
00163         foreach ($metadata as $field) {
00164             $field_label = $xpath->query('xmlns:fieldlabel', $field);
00165             $field_label = !empty($field_label->item(0)->nodeValue) ? $field_label->item(0)->nodeValue : '';
00166 
00167             if (strtolower($field_label) == strtolower($option)) {
00168                 $field_entry = $xpath->query('xmlns:fieldentry', $field);
00169                 $response = !empty($field_entry->item(0)->nodeValue) ? $field_entry->item(0)->nodeValue : '';
00170             }
00171         }
00172 
00173         $response = !empty($response) ? trim($response) : '';
00174 
00175         if (!empty($replace_values)) {
00176             foreach ($replace_values as $key => $value) {
00177                 $response = ($key == $response) ? $value : $response;
00178             }
00179         }
00180 
00181         $response = empty($response) ? $default_value : $response;
00182 
00183         return $response;
00184     }
00185 
00186     private function create_node_course_modules_mod_quiz_question_instances ($instance) {
00187 
00188         $node_course_module_mod_quiz_questions_instances = '';
00189         $sheet_question_mod_instance = cc2moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_QUIZ_QUESTION_INSTANCE);
00190 
00191         $find_tags = array('[#question_id#]' , '[#instance_id#]');
00192 
00193         if (!empty($instance['questions'])) {
00194 
00195             foreach ($instance['questions'] as $question) {
00196                 $replace_values = array($question['id'] , $question['id']);
00197                 $node_course_module_mod_quiz_questions_instances .= str_replace($find_tags, $replace_values, $sheet_question_mod_instance);
00198             }
00199 
00200             $node_course_module_mod_quiz_questions_instances = str_replace($find_tags, $replace_values, $node_course_module_mod_quiz_questions_instances);
00201         }
00202 
00203         return $node_course_module_mod_quiz_questions_instances;
00204     }
00205 
00206     private function get_questions_string ($instance) {
00207 
00208         $questions_string = '';
00209 
00210         if (!empty($instance['questions'])) {
00211             foreach ($instance['questions'] as $question) {
00212                 $questions_string .= $question['id'] . ',';
00213             }
00214         }
00215 
00216         $questions_string = !empty($questions_string) ? substr($questions_string, 0, strlen($questions_string) - 1) : '';
00217 
00218         return $questions_string;
00219     }
00220 
00221     private function create_node_course_question_categories ($instances) {
00222 
00223         $sheet_question_categories = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES);
00224 
00225         if (!empty($instances)) {
00226 
00227             $node_course_question_categories_question_category = '';
00228 
00229             foreach ($instances as $instance) {
00230                 $node_course_question_categories_question_category .= $this->create_node_course_question_categories_question_category($instance);
00231             }
00232 
00233             $find_tags = array('[#node_course_question_categories_question_category#]');
00234             $replace_values = array($node_course_question_categories_question_category);
00235 
00236             $node_course_question_categories = str_replace($find_tags, $replace_values, $sheet_question_categories);
00237         }
00238 
00239         $node_course_question_categories = empty($node_course_question_categories) ? '' : $node_course_question_categories;
00240 
00241         return $node_course_question_categories;
00242     }
00243 
00244     private function create_node_course_question_categories_question_category ($instance) {
00245 
00246         $sheet_question_categories_quetion_category = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY);
00247 
00248         $find_tags = array('[#quiz_id#]',
00249                            '[#quiz_name#]',
00250                            '[#quiz_stamp#]',
00251                            '[#node_course_question_categories_question_category_questions#]');
00252 
00253         $node_course_question_categories_questions = $this->create_node_course_question_categories_question_category_question($instance);
00254         $node_course_question_categories_questions = empty($node_course_question_categories_questions) ? '' : $node_course_question_categories_questions;
00255 
00256         $quiz_stamp = 'localhost+' . time() . '+' . $this->generate_random_string(6);
00257 
00258         $replace_values = array($instance['id'],
00259                                 self::safexml($instance['title']),
00260                                 $quiz_stamp,
00261                                 $node_course_question_categories_questions);
00262 
00263         $node_question_categories = str_replace($find_tags, $replace_values, $sheet_question_categories_quetion_category);
00264 
00265         return $node_question_categories;
00266     }
00267 
00268     private function create_node_course_question_categories_question_category_question ($instance) {
00269 
00270         global $USER;
00271 
00272         $node_course_question_categories_question = '';
00273 
00274         $find_tags = array('[#question_id#]',
00275                            '[#question_title#]',
00276                            '[#question_text#]',
00277                            '[#question_type#]',
00278                            '[#question_general_feedback#]',
00279                            '[#question_defaultgrade#]',
00280                            '[#date_now#]',
00281                            '[#question_type_nodes#]',
00282                            '[#question_stamp#]',
00283                            '[#question_version#]',
00284                            '[#logged_user#]');
00285 
00286         $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION);
00287 
00288         $questions = $instance['questions'];
00289 
00290         if (!empty($questions)) {
00291 
00292             foreach ($questions as $question) {
00293 
00294                 $quiz_stamp = 'localhost+' . time() . '+' . $this->generate_random_string(6);
00295                 $quiz_version = 'localhost+' . time() . '+' . $this->generate_random_string(6);
00296 
00297                 $question_moodle_type = $question['moodle_type'];
00298                 $question_cc_type = $question['cc_type'];
00299 
00300                 $question_type_node = '';
00301 
00302                 $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;
00303                 $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;
00304                 $question_type_node = ($question_moodle_type == MOODLE_QUIZ_ESSAY) ? $this->create_node_course_question_categories_question_category_question_eesay($question) : $question_type_node;
00305                 $question_type_node = ($question_moodle_type == MOODLE_QUIZ_SHORTANSWER) ? $this->create_node_course_question_categories_question_category_question_shortanswer($question) : $question_type_node;
00306 
00307                 $replace_values = array($question['id'],
00308                                         self::safexml($this->truncate_text($question['title'], 255, true)),
00309                                         self::safexml($question['title']),
00310                                         $question_moodle_type,
00311                                         self::safexml($question['feedback']),
00312                                         $question['defaultgrade'], //default grade
00313                                         time(),
00314                                         $question_type_node,
00315                                         $quiz_stamp,
00316                                         $quiz_version,
00317                                         $USER->id);
00318 
00319                 $node_course_question_categories_question .= str_replace($find_tags, $replace_values, $sheet_question_categories_question);
00320             }
00321         }
00322 
00323         $node_course_question_categories_question = empty($node_course_question_categories_question) ? '' : $node_course_question_categories_question;
00324 
00325         return $node_course_question_categories_question;
00326     }
00327 
00328     private function get_questions ($assessment, &$last_question_id, &$last_answer_id, $root_path, $is_question_bank) {
00329 
00330         $questions = array();
00331 
00332         $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
00333 
00334         if (!$is_question_bank) {
00335             $questions_items = $xpath->query('/xmlns:questestinterop/xmlns:assessment/xmlns:section/xmlns:item');
00336         } else {
00337             $questions_items = $xpath->query('/xmlns:questestinterop/xmlns:objectbank/xmlns:item');
00338         }
00339 
00340         foreach ($questions_items as $question_item) {
00341 
00342             $count_questions = $xpath->evaluate('count(xmlns:presentation/xmlns:flow/xmlns:material/xmlns:mattext)', $question_item);
00343 
00344             if ($count_questions == 0) {
00345                 $question_title = $xpath->query('xmlns:presentation/xmlns:material/xmlns:mattext', $question_item);
00346             } else {
00347                 $question_title = $xpath->query('xmlns:presentation/xmlns:flow/xmlns:material/xmlns:mattext', $question_item);
00348             }
00349 
00350             $question_title = !empty($question_title->item(0)->nodeValue) ? $question_title->item(0)->nodeValue : '';
00351 
00352             $question_identifier = $xpath->query('@ident', $question_item);
00353             $question_identifier = !empty($question_identifier->item(0)->nodeValue) ? $question_identifier->item(0)->nodeValue : '';
00354 
00355             if (!empty($question_identifier)) {
00356 
00357                 $question_type = $this->get_question_type($question_identifier, $assessment);
00358 
00359                 if (!empty($question_type['moodle'])) {
00360 
00361                     $last_question_id++;
00362 
00363                     $questions[$question_identifier]['id'] = $last_question_id;
00364 
00365                     $question_title = $this->update_sources($question_title, $root_path);
00366                     $question_title = !empty($question_title) ? str_replace("%24", "\$", $this->include_titles($question_title)) : '';
00367 
00368                     $questions[$question_identifier]['title'] = $question_title;
00369                     $questions[$question_identifier]['identifier'] = $question_identifier;
00370                     $questions[$question_identifier]['moodle_type'] = $question_type['moodle'];
00371                     $questions[$question_identifier]['cc_type'] = $question_type['cc'];
00372                     $questions[$question_identifier]['feedback'] = $this->get_general_feedback($assessment, $question_identifier);
00373                     $questions[$question_identifier]['defaultgrade'] = $this->get_defaultgrade($assessment, $question_identifier);
00374                     $questions[$question_identifier]['answers'] = $this->get_answers($question_identifier, $assessment, $last_answer_id);
00375 
00376                 }
00377             }
00378         }
00379 
00380         $questions = !empty($questions) ? $questions : '';
00381 
00382         return $questions;
00383     }
00384 
00385     private function str_replace_once ($search, $replace, $subject) {
00386 
00387         $first_char = strpos($subject, $search);
00388 
00389         if ($first_char !== false) {
00390 
00391             $before_str = substr($subject, 0, $first_char);
00392             $after_str = substr($subject, $first_char + strlen($search));
00393 
00394             return $before_str . $replace . $after_str;
00395 
00396         } else {
00397             return $subject;
00398         }
00399     }
00400 
00401     private function get_defaultgrade($assessment, $question_identifier) {
00402         $result = 1;
00403         $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
00404         $query = '//xmlns:item[@ident="' . $question_identifier . '"]';
00405         $query .= '//xmlns:qtimetadatafield[xmlns:fieldlabel="cc_weighting"]/xmlns:fieldentry';
00406         $defgrade = $xpath->query($query);
00407         if (!empty($defgrade) && ($defgrade->length > 0)) {
00408             $resp = (int)$defgrade->item(0)->nodeValue;
00409             if ($resp >= 0 && $resp <= 99) {
00410                 $result = $resp;
00411             }
00412         }
00413         return $result;
00414     }
00415 
00416     private function get_general_feedback ($assessment, $question_identifier) {
00417 
00418         $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
00419 
00420         $respconditions = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
00421 
00422         if (!empty($respconditions)) {
00423 
00424             foreach ($respconditions as $respcondition) {
00425 
00426                 $continue = $respcondition->getAttributeNode('continue');
00427                 $continue = !empty($continue->nodeValue) ? strtolower($continue->nodeValue) : '';
00428 
00429                 if ($continue == 'yes') {
00430 
00431                     $display_feedback = $xpath->query('xmlns:displayfeedback', $respcondition);
00432 
00433                     if (!empty($display_feedback)) {
00434                         foreach ($display_feedback as $feedback) {
00435 
00436                             $feedback_identifier = $feedback->getAttributeNode('linkrefid');
00437                             $feedback_identifier = !empty($feedback_identifier->nodeValue) ? $feedback_identifier->nodeValue : '';
00438 
00439                             if (!empty($feedback_identifier)) {
00440                                 $feedbacks_identifiers[] = $feedback_identifier;
00441                             }
00442                         }
00443                     }
00444                 }
00445             }
00446         }
00447 
00448         $feedback = '';
00449         $feedbacks_identifiers = empty($feedbacks_identifiers) ? '' : $feedbacks_identifiers;
00450 
00451         if (!empty($feedbacks_identifiers)) {
00452             foreach ($feedbacks_identifiers as $feedback_identifier) {
00453                 $feedbacks = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:itemfeedback[@ident="' . $feedback_identifier . '"]/xmlns:flow_mat/xmlns:material/xmlns:mattext');
00454                 $feedback .= !empty($feedbacks->item(0)->nodeValue) ? $feedbacks->item(0)->nodeValue . ' ' : '';
00455             }
00456         }
00457 
00458         return $feedback;
00459     }
00460 
00461     private function get_feedback ($assessment, $identifier, $item_identifier, $question_type) {
00462 
00463         $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
00464 
00465         $resource_processing = $xpath->query('//xmlns:item[@ident="' . $item_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
00466 
00467         if (!empty($resource_processing)) {
00468 
00469             foreach ($resource_processing as $response) {
00470 
00471                 $varequal = $xpath->query('xmlns:conditionvar/xmlns:varequal', $response);
00472                 $varequal = !empty($varequal->item(0)->nodeValue) ? $varequal->item(0)->nodeValue : '';
00473 
00474                 if (strtolower($varequal) == strtolower($identifier) || ($question_type == CC_QUIZ_ESSAY)) {
00475 
00476                     $display_feedback = $xpath->query('xmlns:displayfeedback', $response);
00477 
00478                     if (!empty($display_feedback)) {
00479                         foreach ($display_feedback as $feedback) {
00480 
00481                             $feedback_identifier = $feedback->getAttributeNode('linkrefid');
00482                             $feedback_identifier = !empty($feedback_identifier->nodeValue) ? $feedback_identifier->nodeValue : '';
00483 
00484                             if (!empty($feedback_identifier)) {
00485                                 $feedbacks_identifiers[] = $feedback_identifier;
00486                             }
00487                         }
00488                     }
00489                 }
00490             }
00491         }
00492 
00493         $feedback = '';
00494         $feedbacks_identifiers = empty($feedbacks_identifiers) ? '' : $feedbacks_identifiers;
00495 
00496         if (!empty($feedbacks_identifiers)) {
00497             foreach ($feedbacks_identifiers as $feedback_identifier) {
00498                 $feedbacks = $xpath->query('//xmlns:item[@ident="' . $item_identifier . '"]/xmlns:itemfeedback[@ident="' . $feedback_identifier . '"]/xmlns:flow_mat/xmlns:material/xmlns:mattext');
00499                 $feedback .= !empty($feedbacks->item(0)->nodeValue) ? $feedbacks->item(0)->nodeValue . ' ' : '';
00500             }
00501         }
00502 
00503         return $feedback;
00504     }
00505 
00506     private function get_answers_fib ($question_identifier, $identifier, $assessment, &$last_answer_id) {
00507 
00508         $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
00509 
00510         $answers_fib = array();
00511 
00512         $response_items = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
00513 
00514         foreach ($response_items as $response_item) {
00515 
00516             $setvar = $xpath->query('xmlns:setvar', $response_item);
00517             $setvar = is_object($setvar->item(0)) ? $setvar->item(0)->nodeValue : '';
00518 
00519             if ($setvar != '') {
00520 
00521                 $last_answer_id++;
00522 
00523                 $answer_title = $xpath->query('xmlns:conditionvar/xmlns:varequal[@respident="' . $identifier . '"]', $response_item);
00524                 $answer_title = !empty($answer_title->item(0)->nodeValue) ? $answer_title->item(0)->nodeValue : '';
00525 
00526             $case = $xpath->query('xmlns:conditionvar/xmlns:varequal/@case', $response_item);
00527             $case = is_object($case->item(0)) ? $case->item(0)->nodeValue : 'no'
00528                                     ;
00529             $case = strtolower($case) == 'yes' ? 1 :
00530                             0;
00531 
00532                 $display_feedback = $xpath->query('xmlns:displayfeedback', $response_item);
00533 
00534                 unset($feedbacks_identifiers);
00535 
00536                 if (!empty($display_feedback)) {
00537 
00538                     foreach ($display_feedback as $feedback) {
00539 
00540                         $feedback_identifier = $feedback->getAttributeNode('linkrefid');
00541                         $feedback_identifier = !empty($feedback_identifier->nodeValue) ? $feedback_identifier->nodeValue : '';
00542 
00543                         if (!empty($feedback_identifier)) {
00544                             $feedbacks_identifiers[] = $feedback_identifier;
00545                         }
00546                     }
00547                 }
00548 
00549                 $feedback = '';
00550                 $feedbacks_identifiers = empty($feedbacks_identifiers) ? '' : $feedbacks_identifiers;
00551 
00552                 if (!empty($feedbacks_identifiers)) {
00553                     foreach ($feedbacks_identifiers as $feedback_identifier) {
00554                         $feedbacks = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:itemfeedback[@ident="' . $feedback_identifier . '"]/xmlns:flow_mat/xmlns:material/xmlns:mattext');
00555                         $feedback .= !empty($feedbacks->item(0)->nodeValue) ? $feedbacks->item(0)->nodeValue . ' ' : '';
00556                     }
00557                 }
00558 
00559                 $answers_fib[] = array('id' => $last_answer_id,
00560                                        'title' => $answer_title,
00561                                        'score' => $setvar,
00562                                        'feedback' => $feedback,
00563                                        'case' => $case);
00564             }
00565         }
00566 
00567         $answers_fib = empty($answers_fib) ? '' : $answers_fib;
00568 
00569         return $answers_fib;
00570     }
00571 
00572     private function get_answers_pattern_match ($question_identifier, $identifier, $assessment, &$last_answer_id) {
00573 
00574         $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
00575 
00576         $answers_fib = array();
00577 
00578         $response_items = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
00579 
00580         foreach ($response_items as $response_item) {
00581 
00582             $setvar = $xpath->query('xmlns:setvar', $response_item);
00583             $setvar = is_object($setvar->item(0)) ? $setvar->item(0)->nodeValue : '';
00584 
00585             if ($setvar != '') {
00586 
00587                 $last_answer_id++;
00588 
00589                 $answer_title = $xpath->query('xmlns:conditionvar/xmlns:varequal[@respident="' . $identifier . '"]', $response_item);
00590                 $answer_title = !empty($answer_title->item(0)->nodeValue) ? $answer_title->item(0)->nodeValue : '';
00591 
00592                 if (empty($answer_title)) {
00593                     $answer_title = $xpath->query('xmlns:conditionvar/xmlns:varsubstring[@respident="' . $identifier . '"]', $response_item);
00594                     $answer_title = !empty($answer_title->item(0)->nodeValue) ? '*' . $answer_title->item(0)->nodeValue . '*' : '';
00595                 }
00596 
00597                 if (empty($answer_title)) {
00598                     $answer_title = '*';
00599                 }
00600 
00601             $case = $xpath->query('xmlns:conditionvar/xmlns:varequal/@case', $response_item);
00602             $case = is_object($case->item(0)) ? $case->item(0)->nodeValue : 'no'
00603                                     ;
00604             $case = strtolower($case) == 'yes' ? 1 :
00605                             0;
00606 
00607                 $display_feedback = $xpath->query('xmlns:displayfeedback', $response_item);
00608 
00609                 unset($feedbacks_identifiers);
00610 
00611                 if (!empty($display_feedback)) {
00612 
00613                     foreach ($display_feedback as $feedback) {
00614 
00615                         $feedback_identifier = $feedback->getAttributeNode('linkrefid');
00616                         $feedback_identifier = !empty($feedback_identifier->nodeValue) ? $feedback_identifier->nodeValue : '';
00617 
00618                         if (!empty($feedback_identifier)) {
00619                             $feedbacks_identifiers[] = $feedback_identifier;
00620                         }
00621                     }
00622                 }
00623 
00624                 $feedback = '';
00625                 $feedbacks_identifiers = empty($feedbacks_identifiers) ? '' : $feedbacks_identifiers;
00626 
00627                 if (!empty($feedbacks_identifiers)) {
00628                     foreach ($feedbacks_identifiers as $feedback_identifier) {
00629                         $feedbacks = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:itemfeedback[@ident="' . $feedback_identifier . '"]/xmlns:flow_mat/xmlns:material/xmlns:mattext');
00630                         $feedback .= !empty($feedbacks->item(0)->nodeValue) ? $feedbacks->item(0)->nodeValue . ' ' : '';
00631                     }
00632                 }
00633 
00634                 $answers_fib[] = array('id' => $last_answer_id,
00635                                        'title' => $answer_title,
00636                                        'score' => $setvar,
00637                                        'feedback' => $feedback,
00638                                        'case' => $case);
00639             }
00640         }
00641 
00642         $answers_fib = empty($answers_fib) ? '' : $answers_fib;
00643 
00644         return $answers_fib;
00645     }
00646 
00647 
00648     private function get_answers ($identifier, $assessment, &$last_answer_id) {
00649 
00650         $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
00651 
00652         $answers = array();
00653 
00654         $question_cc_type = $this->get_question_type($identifier, $assessment);
00655         $question_cc_type = $question_cc_type['cc'];
00656 
00657         if ($question_cc_type == CC_QUIZ_MULTIPLE_CHOICE || $question_cc_type == CC_QUIZ_MULTIPLE_RESPONSE || $question_cc_type == CC_QUIZ_TRUE_FALSE) {
00658 
00659             $query_answers = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:response_lid/xmlns:render_choice/xmlns:response_label';
00660             $query_answers_with_flow = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:flow/xmlns:response_lid/xmlns:render_choice/xmlns:response_label';
00661 
00662             $query_indentifer = '@ident';
00663             $query_title = 'xmlns:material/xmlns:mattext';
00664         }
00665 
00666         if ($question_cc_type == CC_QUIZ_ESSAY) {
00667 
00668             $query_answers = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:response_str';
00669             $query_answers_with_flow = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:flow/xmlns:response_str';
00670 
00671             $query_indentifer = '@ident';
00672             $query_title = 'xmlns:render_fib';
00673         }
00674 
00675         if ($question_cc_type == CC_QUIZ_FIB || $question_cc_type == CC_QUIZ_PATTERN_MACHT) {
00676 
00677             $xpath_query = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:response_str/@ident';
00678             $xpath_query_with_flow = '//xmlns:item[@ident="' . $identifier . '"]/xmlns:presentation/xmlns:flow/xmlns:response_str/@ident';
00679 
00680             $count_response = $xpath->evaluate('count(' . $xpath_query_with_flow . ')');
00681 
00682             if ($count_response == 0) {
00683                 $answer_identifier = $xpath->query($xpath_query);
00684             } else {
00685                 $answer_identifier = $xpath->query($xpath_query_with_flow);
00686             }
00687 
00688             $answer_identifier = !empty($answer_identifier->item(0)->nodeValue) ? $answer_identifier->item(0)->nodeValue : '';
00689 
00690             if ($question_cc_type == CC_QUIZ_FIB) {
00691                 $answers = $this->get_answers_fib ($identifier, $answer_identifier, $assessment, $last_answer_id);
00692             } else {
00693                 $answers = $this->get_answers_pattern_match ($identifier, $answer_identifier, $assessment, $last_answer_id);
00694             }
00695 
00696         } else {
00697 
00698             $count_response = $xpath->evaluate('count(' . $query_answers_with_flow . ')');
00699 
00700             if ($count_response == 0) {
00701                 $response_items = $xpath->query($query_answers);
00702             } else {
00703                 $response_items = $xpath->query($query_answers_with_flow);
00704             }
00705 
00706             if (!empty($response_items)) {
00707 
00708                 foreach ($response_items as $response_item) {
00709 
00710                     $last_answer_id++;
00711 
00712                     $answer_identifier = $xpath->query($query_indentifer, $response_item);
00713                     $answer_identifier = !empty($answer_identifier->item(0)->nodeValue) ? $answer_identifier->item(0)->nodeValue : '';
00714 
00715                     $answer_title = $xpath->query($query_title, $response_item);
00716                     $answer_title = !empty($answer_title->item(0)->nodeValue) ? $answer_title->item(0)->nodeValue : '';
00717 
00718                     $answer_feedback = $this->get_feedback($assessment, $answer_identifier, $identifier, $question_cc_type);
00719 
00720                     $answer_score = $this->get_score($assessment, $answer_identifier, $identifier);
00721 
00722                     $answers[] = array('id' => $last_answer_id,
00723                                        'title' => $answer_title,
00724                                        'score' => $answer_score,
00725                                        'identifier' => $answer_identifier,
00726                                        'feedback' => $answer_feedback);
00727                 }
00728             }
00729         }
00730 
00731         $answers = empty($answers) ? '' : $answers;
00732 
00733         return $answers;
00734 
00735     }
00736 
00737     private function get_score ($assessment, $identifier, $question_identifier) {
00738 
00739         $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
00740 
00741         $resource_processing = $xpath->query('//xmlns:item[@ident="' . $question_identifier . '"]/xmlns:resprocessing/xmlns:respcondition');
00742 
00743         if (!empty($resource_processing)) {
00744 
00745             foreach ($resource_processing as $response) {
00746 
00747                 $question_cc_type = $this->get_question_type($question_identifier, $assessment);
00748                 $question_cc_type = $question_cc_type['cc'];
00749 
00750                 $varequal = $xpath->query('xmlns:conditionvar/xmlns:varequal', $response);
00751                 $varequal = !empty($varequal->item(0)->nodeValue) ? $varequal->item(0)->nodeValue : '';
00752 
00753                 if (strtolower($varequal) == strtolower($identifier)) {
00754                     $score = $xpath->query('xmlns:setvar', $response);
00755                     $score = !empty($score->item(0)->nodeValue) ? $score->item(0)->nodeValue : '';
00756                 }
00757             }
00758         }
00759 
00760         $score = empty($score) ? 0 : $score;
00761 
00762         return $score;
00763     }
00764 
00765     private function create_node_course_question_categories_question_category_question_multiple_choice ($question) {
00766 
00767         $node_course_question_categories_question_answer = '';
00768         $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_MULTIPLE_CHOICE);
00769 
00770         if (!empty($question['answers'])) {
00771             foreach ($question['answers'] as $answer) {
00772                 $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer);
00773             }
00774         }
00775 
00776         $answer_string = $this->get_answers_string($question['answers']);
00777 
00778         $is_single = ($question['cc_type'] == CC_QUIZ_MULTIPLE_CHOICE) ? 1 : 0;
00779 
00780         $find_tags = array('[#node_course_question_categories_question_category_question_answer#]',
00781                            '[#answer_string#]',
00782                            '[#is_single#]');
00783 
00784         $replace_values = array($node_course_question_categories_question_answer,
00785                                 self::safexml($answer_string),
00786                                 $is_single);
00787 
00788         $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);
00789 
00790         return $node_question_categories_question;
00791     }
00792 
00793     private function create_node_course_question_categories_question_category_question_eesay ($question) {
00794 
00795         $node_course_question_categories_question_answer = '';
00796 
00797         $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_EESAY);
00798 
00799         if (!empty($question['answers'])) {
00800             foreach ($question['answers'] as $answer) {
00801                 $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer);
00802             }
00803         }
00804 
00805         $find_tags = array('[#node_course_question_categories_question_category_question_answer#]');
00806         $replace_values = array($node_course_question_categories_question_answer);
00807 
00808         $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);
00809 
00810         return $node_question_categories_question;
00811     }
00812 
00813     private function create_node_course_question_categories_question_category_question_shortanswer ($question) { //, &$fib_questions) {
00814 
00815         $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_SHORTANSWER);
00816         $node_course_question_categories_question_answer = '';
00817 
00818         if (!empty($question['answers'])) {
00819             foreach ($question['answers'] as $answer) {
00820                 $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer);
00821             }
00822         }
00823 
00824         $answers_string = $this->get_answers_string($question['answers']);
00825 
00826         $use_case = 0;
00827 
00828         foreach ($question['answers'] as $answer) {
00829 
00830             if ($answer['case'] == 1) {
00831                 $use_case = 1;
00832             }
00833 
00834         }
00835 
00836         $find_tags = array('[#answers_string#]',
00837                            '[#use_case#]',
00838                            '[#node_course_question_categories_question_category_question_answer#]');
00839 
00840         $replace_values = array(self::safexml($answers_string),
00841                                 self::safexml($use_case),
00842                                 $node_course_question_categories_question_answer);
00843 
00844 
00845 
00846         $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);
00847 
00848         return $node_question_categories_question;
00849 
00850     }
00851 
00852     private function create_node_course_question_categories_question_category_question_true_false ($question) {
00853 
00854         $node_course_question_categories_question_answer = '';
00855 
00856         $sheet_question_categories_question = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_TRUE_FALSE);
00857 
00858         $max_score = 0;
00859         $true_answer_id = 0;
00860         $false_answer_id = 0;
00861 
00862         if (!empty($question['answers'])) {
00863 
00864             foreach ($question['answers'] as $answer) {
00865                 if ($answer['score'] > $max_score) {
00866                     $max_score = $answer['score'];
00867                     $true_answer_id = $answer['id'];
00868                 }
00869 
00870                 $node_course_question_categories_question_answer .= $this->create_node_course_question_categories_question_category_question_answer($answer);
00871             }
00872 
00873             foreach ($question['answers'] as $answer) {
00874 
00875                 if ($answer['id'] != $true_answer_id) {
00876                     $max_score = $answer['score'];
00877                     $false_answer_id = $answer['id'];
00878                 }
00879             }
00880         }
00881 
00882         $find_tags = array('[#node_course_question_categories_question_category_question_answer#]',
00883                            '[#true_answer_id#]',
00884                            '[#false_answer_id#]');
00885 
00886         $replace_values = array($node_course_question_categories_question_answer,
00887                                 $true_answer_id,
00888                                 $false_answer_id);
00889 
00890         $node_question_categories_question = str_replace($find_tags, $replace_values, $sheet_question_categories_question);
00891 
00892         return $node_question_categories_question;
00893     }
00894 
00895     private function get_answers_string ($answers) {
00896 
00897         $answer_string = '';
00898 
00899         if (!empty($answers)) {
00900             foreach ($answers as $answer) {
00901                 $answer_string .= $answer['id'] . ',';
00902             }
00903         }
00904 
00905         $answer_string = !empty($answer_string) ? substr($answer_string, 0, strlen($answer_string) - 1) : '';
00906 
00907         return $answer_string;
00908 
00909     }
00910 
00911     private function create_node_course_question_categories_question_category_question_answer ($answer) {
00912 
00913         $sheet_question_categories_question_answer = cc2moodle::loadsheet(SHEET_COURSE_QUESTION_CATEGORIES_QUESTION_CATEGORY_QUESTION_ANSWER);
00914 
00915         $find_tags = array('[#answer_id#]',
00916                            '[#answer_text#]',
00917                            '[#answer_score#]',
00918                            '[#answer_feedback#]');
00919 
00920         $replace_values = array($answer['id'],
00921                                 self::safexml($answer['title']),
00922                                 $answer['score'],
00923                                 self::safexml($answer['feedback']));
00924 
00925         $node_question_categories_question_answer = str_replace($find_tags, $replace_values, $sheet_question_categories_question_answer);
00926 
00927         return $node_question_categories_question_answer;
00928     }
00929 
00930     private function get_question_type ($identifier, $assessment) {
00931 
00932         $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
00933 
00934         $metadata = $xpath->query('//xmlns:item[@ident="' . $identifier . '"]/xmlns:itemmetadata/xmlns:qtimetadata/xmlns:qtimetadatafield');
00935 
00936         foreach ($metadata as $field) {
00937 
00938             $field_label = $xpath->query('xmlns:fieldlabel', $field);
00939             $field_label = !empty($field_label->item(0)->nodeValue) ? $field_label->item(0)->nodeValue : '';
00940 
00941             if ($field_label == 'cc_profile') {
00942                 $field_entry = $xpath->query('xmlns:fieldentry', $field);
00943                 $type = !empty($field_entry->item(0)->nodeValue) ? $field_entry->item(0)->nodeValue : '';
00944             }
00945         }
00946 
00947         $return_type = array();
00948 
00949         $return_type['moodle'] = '';
00950         $return_type['cc'] = $type;
00951 
00952         if ($type == CC_QUIZ_MULTIPLE_CHOICE) {
00953             $return_type['moodle'] = MOODLE_QUIZ_MULTIPLE_CHOICE;
00954         }
00955         if ($type == CC_QUIZ_MULTIPLE_RESPONSE) {
00956             $return_type['moodle'] = MOODLE_QUIZ_MULTIPLE_CHOICE;
00957         }
00958         if ($type == CC_QUIZ_TRUE_FALSE) {
00959             $return_type['moodle'] = MOODLE_QUIZ_TRUE_FALSE;
00960         }
00961         if ($type == CC_QUIZ_ESSAY) {
00962             $return_type['moodle'] = MOODLE_QUIZ_ESSAY;
00963         }
00964         if ($type == CC_QUIZ_FIB) {
00965             $return_type['moodle'] = MOODLE_QUIZ_SHORTANSWER;
00966         }
00967         if ($type == CC_QUIZ_PATTERN_MACHT) {
00968             $return_type['moodle'] = MOODLE_QUIZ_SHORTANSWER;
00969         }
00970 
00971         return $return_type;
00972 
00973     }
00974 }
 All Data Structures Namespaces Files Functions Variables Enumerations