function process() { global $DB, $CFG, $USER, $QUIZ_CACHE; $cm = get_coursemodule_from_id('quiz', $this->id); $context = context_module::instance($cm->id); $quiz = $DB->get_record('quiz', array('id' => $cm->instance), '*', MUST_EXIST); $quizobj = quiz::create($cm->instance, $USER->id); $mQH = new QuizHelper(); $mQH->init($this->server_connection); try { $quizobj->preload_questions(); $quizobj->load_questions(); $qs = $quizobj->get_questions(); $md5postfix = ""; foreach ($this->configArray as $key => $value) { $md5postfix .= $key[0] . (string) $value; } // generate the md5 of the quiz $this->md5 = md5(serialize($qs)) . $this->id . "c" . $md5postfix; // find if this quiz already exists $resp = $mQH->exec('quizprops/digest/' . $this->md5, array(), 'get'); if (!isset($resp->quizzes)) { echo get_string('error_connection', 'block_oppia_mobile_export'); die; } $filename = extractImageFile($quiz->intro, 'mod_quiz', 'intro', '0', $context->id, $this->courseroot, $cm->id); if ($filename) { $this->quiz_image = "/images/" . resizeImage($this->courseroot . "/" . $filename, $this->courseroot . "/images/" . $cm->id, $CFG->block_oppia_mobile_export_thumb_width, $CFG->block_oppia_mobile_export_thumb_height); //delete original image unlink($this->courseroot . "/" . $filename) or die(get_string('error_file_delete', 'block_oppia_mobile_export')); } // Don't export the full quiz if it already exists on the server // instead just add the course version if (count($resp->quizzes) > 0) { $quiz_id = $resp->quizzes[0]->quiz_id; $quiz = $mQH->exec('quiz/' . $quiz_id, array(), 'get'); $this->content = json_encode($quiz); $post = array('quiz_id' => $quiz_id, 'name' => "courseversion", 'value' => $this->courseversion); $resp = $mQH->exec('quizprops', $post); $this->exportQuestionImages(); $this->exportQuestionMedia(); return; } $props = array(); array_push($props, array('name' => "digest", 'value' => $this->md5)); array_push($props, array('name' => "courseversion", 'value' => $this->courseversion)); foreach ($this->configArray as $k => $v) { if ($k != 'randomselect' || $v != 0) { array_push($props, array('name' => $k, 'value' => $v)); } } $nameJSON = extractLangs($cm->name, true); $descJSON = extractLangs($this->summary, true); //create the quiz $post = array('title' => $nameJSON, 'description' => $descJSON, 'questions' => array(), 'props' => $props); $resp = $mQH->exec('quiz', $post); $quiz_uri = $resp->resource_uri; $quiz_id = $resp->id; $i = 1; foreach ($qs as $q) { // skip any essay questions if ($q->qtype == 'essay') { $q->qtype = 'essay'; // echo get_string('export_quiz_skip_essay','block_oppia_mobile_export')."<br/>"; // continue; } // skip any random questions if ($q->qtype == 'random') { echo get_string('export_quiz_skip_random', 'block_oppia_mobile_export') . "<br/>"; continue; } //check to see if a multichoice is actually a multiselect if ($q->qtype == 'multichoice') { $counter = 0; foreach ($q->options->answers as $r) { if ($r->fraction > 0) { $counter++; } } if ($counter > 1) { $q->qtype = 'multiselect'; } } if ($q->qtype == 'truefalse') { $q->qtype = 'multichoice'; } // add max score property $props = array(); $props[0] = array('name' => "maxscore", 'value' => $q->maxmark); //add feedback for matching questions if ($q->qtype == 'match') { $q->qtype = 'matching'; $prop_i = 1; if ($q->options->correctfeedback != "") { $feedbackJSON = extractLangs($q->options->correctfeedback, true); $props[$prop_i] = array('name' => "correctfeedback", 'value' => $feedbackJSON); $prop_i++; } if ($q->options->partiallycorrectfeedback != "") { $feedbackJSON = extractLangs($q->options->partiallycorrectfeedback, true); $props[$prop_i] = array('name' => "partiallycorrectfeedback", 'value' => $feedbackJSON); $prop_i++; } if ($q->options->incorrectfeedback != "") { $feedbackJSON = extractLangs($q->options->incorrectfeedback, true); $props[$prop_i] = array('name' => "incorrectfeedback", 'value' => $feedbackJSON); $prop_i++; } } // find if the question text has any images in it $question_image = extractImageFile($q->questiontext, 'question', 'questiontext', $q->id, $q->contextid, $this->courseroot, $cm->id); if ($question_image) { array_push($props, array('name' => "image", 'value' => $question_image)); } // find if any videos embedded in question text $q->questiontext = $this->extractMedia($q->id, $q->questiontext); if (array_key_exists($q->id, $this->quiz_media)) { foreach ($this->quiz_media[$q->id] as $media) { array_push($props, array('name' => "media", 'value' => $media->filename)); } } $questionJSON = extractLangs($q->questiontext, true); // create question $post = array('title' => $questionJSON, 'type' => $q->qtype, 'responses' => array(), 'props' => $props); $resp = $mQH->exec('question', $post); $question_uri = $resp->resource_uri; $j = 1; // if matching question then concat the options with | if (isset($q->options->subquestions)) { // Find out how many subquestions $subqs = 0; foreach ($q->options->subquestions as $sq) { if (trim($sq->questiontext) != "") { $subqs++; } } foreach ($q->options->subquestions as $sq) { $titleJSON = extractLangs($sq->questiontext . $this->MATCHING_SEPERATOR . $sq->answertext, true); // add response $props = array(); $post = array('question' => $question_uri, 'order' => $j, 'title' => $titleJSON, 'score' => $q->maxmark / $subqs, 'props' => $props); $resp = $mQH->exec('response', $post); $response_uri = $resp->resource_uri; $j++; } } // for multichoice/multiselect/shortanswer/numerical questions if (isset($q->options->answers)) { foreach ($q->options->answers as $r) { $props = array(); if (strip_tags($r->feedback) != "") { $feedbackJSON = extractLangs($r->feedback, true); $props[0] = array('name' => 'feedback', 'value' => $feedbackJSON); } // if numerical also add a tolerance if ($q->qtype == 'numerical') { $props[1] = array('name' => 'tolerance', 'value' => $r->tolerance); } $responseJSON = extractLangs($r->answer, true); // add response $post = array('question' => $question_uri, 'order' => $j, 'title' => $responseJSON, 'score' => $r->fraction * $q->maxmark, 'props' => $props); $resp = $mQH->exec('response', $post); $j++; } } // add question to quiz $post = array('quiz' => $quiz_uri, 'question' => $question_uri, 'order' => $i); $resp = $mQH->exec('quizquestion', $post); $i++; } // get the final quiz object $quiz = $mQH->exec('quiz/' . $quiz_id, array(), 'get'); $this->content = json_encode($quiz); } catch (moodle_exception $me) { echo get_string('export_quiz_skip', 'block_oppia_mobile_export') . "<br/>"; $this->is_valid = false; return; } }
function process() { global $DB, $CFG, $USER; $cm = get_coursemodule_from_id('feedback', $this->id); $context = context_module::instance($cm->id); $feedback = $DB->get_record('feedback', array('id' => $cm->instance), '*', MUST_EXIST); $select = 'feedback = ?'; $params = array($feedback->id); $feedbackitems = $DB->get_records_select('feedback_item', $select, $params, 'position'); $mQH = new QuizHelper(); $mQH->init($this->server_connection); $this->md5 = md5(serialize($feedbackitems)) . $this->id; // find if this quiz already exists $resp = $mQH->exec('quizprops/digest/' . $this->md5, array(), 'get'); if (!isset($resp->quizzes)) { echo get_string('error_connection', 'block_oppia_mobile_export'); die; } $filename = extractImageFile($feedback->intro, 'mod_feedback', 'intro', '0', $context->id, $this->courseroot, $cm->id); if ($filename) { $this->feedback_image = "/images/" . resizeImage($this->courseroot . "/" . $filename, $this->courseroot . "/images/" . $cm->id, $CFG->block_oppia_mobile_export_thumb_width, $CFG->block_oppia_mobile_export_thumb_height); //delete original image unlink($this->courseroot . "/" . $filename) or die(get_string('error_file_delete', 'block_oppia_mobile_export')); } if (count($resp->quizzes) > 0) { $quiz_id = $resp->quizzes[0]->quiz_id; $quiz = $mQH->exec('quiz/' . $quiz_id, array(), 'get'); $this->content = json_encode($quiz); return; } $props = array(); $props[0] = array('name' => "digest", 'value' => $this->md5); $props[1] = array('name' => "courseversion", 'value' => $this->courseversion); $nameJSON = extractLangs($cm->name, true); $descJSON = extractLangs($this->summary, true); //create the quiz $post = array('title' => $nameJSON, 'description' => $descJSON, 'questions' => array(), 'props' => $props); $resp = $mQH->exec('quiz', $post); $quiz_uri = $resp->resource_uri; $quiz_id = $resp->id; $i = 1; foreach ($feedbackitems as $fi) { if (!in_array($fi->typ, $this->supported_types)) { continue; } if ($fi->required) { $value = "true"; } else { $value = "false"; } $props = array(); $props[0] = array('name' => "required", 'value' => $value); $qtitle = extractLangs($fi->name, true); // create the question if (strpos($fi->presentation, 'r') === 0 && ($fi->typ == "multichoice" || $fi->typ == "multichoicerated")) { $post = array('title' => $qtitle, 'type' => "multichoice", 'responses' => array(), 'props' => $props); $resp = $mQH->exec('question', $post); $question_uri = $resp->resource_uri; } if (strpos($fi->presentation, 'c') === 0 && $fi->typ == "multichoice") { $post = array('title' => $qtitle, 'type' => "multiselect", 'responses' => array(), 'props' => $props); $resp = $mQH->exec('question', $post); $question_uri = $resp->resource_uri; } if ($fi->typ == "textarea") { $post = array('title' => $qtitle, 'type' => "essay", 'responses' => array(), 'props' => $props); $resp = $mQH->exec('question', $post); $question_uri = $resp->resource_uri; } if ($fi->typ == "numeric") { $post = array('title' => $qtitle, 'type' => "numerical", 'responses' => array(), 'props' => $props); $resp = $mQH->exec('question', $post); $question_uri = $resp->resource_uri; } if ($fi->typ == "textfield") { $post = array('title' => $qtitle, 'type' => "shortanswer", 'responses' => array(), 'props' => $props); $resp = $mQH->exec('question', $post); $question_uri = $resp->resource_uri; } // add the response options if ($fi->typ == "multichoice") { $j = 1; $presentation = preg_replace("(r[>]+)", '', $fi->presentation); $presentation = preg_replace("(c[>]+)", '', $presentation); $response_options = explode("|", $presentation); foreach ($response_options as $ro) { $responseopt = extractLangs($ro, true); $post = array('question' => $question_uri, 'order' => $j, 'title' => $responseopt, 'score' => 0, 'props' => array()); $resp = $mQH->exec('response', $post); $j++; } } if ($fi->typ == "multichoicerated") { $j = 1; $presentation = preg_replace("(r[>>]+)", '', $fi->presentation); $response_options = explode("|", $presentation); foreach ($response_options as $ro) { $new_ro = preg_replace("([0-9]+[#]+)", '', $ro); $responseopt = extractLangs($new_ro, true); $post = array('question' => $question_uri, 'order' => $j, 'title' => $responseopt, 'score' => 0, 'props' => array()); $resp = $mQH->exec('response', $post); $j++; } } // add question to quiz $post = array('quiz' => $quiz_uri, 'question' => $question_uri, 'order' => $i); $resp = $mQH->exec('quizquestion', $post); $i++; } // get the final quiz object $feedback = $mQH->exec('quiz/' . $quiz_id, array(), 'get'); $this->content = json_encode($feedback); }