format.php
来自「很棒的在线教学系统」· PHP 代码 · 共 922 行 · 第 1/3 页
PHP
922 行
// remove the temporary directory remove_dir( $path ); return true; }/** * exports a quiz (as opposed to exporting a category of questions) * * The parent class method was overridden because the IMS export consists of multiple files * * @param object $quiz * @param array $questions - an array of question objects * @param object $result - if set, contains result of calling quiz_grade_responses() * @param string $redirect - a URL to redirect to in case of failure * @param string $submiturl - the URL for the qti player to send the results to (e.g. attempt.php) * @todo use $result in the ouput */ function export_quiz($course, $quiz, $questions, $result, $redirect, $submiturl = null) { $this->xml_entitize($course); $this->xml_entitize($quiz); $this->xml_entitize($questions); $this->xml_entitize($result); $this->xml_entitize($submiturl); if (! $this->exportpreprocess(0, $course)) { // Do anything before that we need to error("Error occurred during pre-processing!", $redirect); } if (! $this->exportprocess_quiz($quiz, $questions, $result, $submiturl, $course)) { // Process the export data error("Error occurred during processing!", $redirect); } if (! $this->exportpostprocess()) { // In case anything needs to be done after error("Error occurred during post-processing!", $redirect); } }/** * This function is called to export a quiz (as opposed to exporting a category of questions) * * @uses $USER * @param object $quiz * @param array $questions - an array of question objects * @param object $result - if set, contains result of calling quiz_grade_responses() * @todo use $result in the ouput */ function exportprocess_quiz($quiz, $questions, $result, $submiturl, $course) { global $USER; global $CFG; $gradingmethod = array (1 => 'GRADEHIGHEST', 2 => 'GRADEAVERAGE', 3 => 'ATTEMPTFIRST' , 4 => 'ATTEMPTLAST'); $questions = $this->quiz_export_prepare_questions($questions, $quiz->id, $course->id, $quiz->shuffleanswers); $smarty =& $this->init_smarty(); $smarty->assign('questions', $questions); // quiz level smarty variables $manifestid = str_replace(array(':', '/'), array('-','_'), "quiz{$quiz->id}-{$CFG->wwwroot}"); $smarty->assign('manifestidentifier', $manifestid); $smarty->assign('submiturl', $submiturl); $smarty->assign('userid', $USER->id); $smarty->assign('username', htmlspecialchars($USER->username, ENT_COMPAT, 'UTF-8')); $smarty->assign('quiz_level_export', 1); $smarty->assign('quiztitle', format_string($quiz->name,true)); //assigned specifically so as not to cause problems with category-level export $smarty->assign('quiztimeopen', date('Y-m-d\TH:i:s', $quiz->timeopen)); // ditto $smarty->assign('quiztimeclose', date('Y-m-d\TH:i:s', $quiz->timeclose)); // ditto $smarty->assign('grademethod', $gradingmethod[$quiz->grademethod]); $smarty->assign('quiz', $quiz); $smarty->assign('course', $course); $smarty->assign('lang', $this->lang); $expout = $smarty->fetch('imsmanifest.tpl'); echo $expout; return true; }/** * Prepares questions for quiz export * * The questions are changed as follows: * - the question answers atached to the questions * - image set to an http reference instead of a file path * - qti specific info added * - exporttext added, which contains an xml-formatted qti assesmentItem * * @param array $questions - an array of question objects * @param int $quizid * @return an array of question arrays */ function quiz_export_prepare_questions($questions, $quizid, $courseid, $shuffleanswers = null) { global $CFG; // add the answers to the questions and format the image property foreach ($questions as $key=>$question) { $questions[$key] = get_question_data($question); $questions[$key]->courseid = $courseid; $questions[$key]->quizid = $quizid; if ($question->image) { if (empty($question->mediamimetype)) { $questions[$key]->mediamimetype = mimeinfo('type',$question->image); } $localfile = (substr(strtolower($question->image), 0, 7) == 'http://') ? false : true; if ($localfile) { // create the http url that the player will need to access the file if ($CFG->slasharguments) { // Use this method if possible for better caching $questions[$key]->mediaurl = "$CFG->wwwroot/file.php/$question->image"; } else { $questions[$key]->mediaurl = "$CFG->wwwroot/file.php?file=$question->image"; } } else { $questions[$key]->mediaurl = $question->image; } } } $this->add_qti_info($questions); $questions = $this->questions_with_export_info($questions, $shuffleanswers); $questions = $this->objects_to_array($questions); return $questions; }/** * calls htmlspecialchars for each string field, to convert, for example, & to & * * collections are processed recursively * * @param array $collection - an array or object or string */function xml_entitize(&$collection) { if (is_array($collection)) { foreach ($collection as $key=>$var) { if (is_string($var)) { $collection[$key]= htmlspecialchars($var, ENT_COMPAT, 'UTF-8'); } else if (is_array($var) || is_object($var)) { $this->xml_entitize($collection[$key]); } } } else if (is_object($collection)) { $vars = get_object_vars($collection); foreach ($vars as $key=>$var) { if (is_string($var)) { $collection->$key = htmlspecialchars($var, ENT_COMPAT, 'UTF-8'); } else if (is_array($var) || is_object($var)) { $this->xml_entitize($collection->$key); } } } else if (is_string($collection)) { $collection = htmlspecialchars($collection, ENT_COMPAT, 'UTF-8'); }}/** * adds exporttext property to the questions * * Adds the qti export text to the questions * * @param array $questions - an array of question objects * @return an array of question objects */ function questions_with_export_info($questions, $shuffleanswers = null) { $exportquestions = array(); foreach($questions as $key=>$question) { $expout = $this->writequestion( $question , $shuffleanswers) . "\n"; $expout = $this->presave_process( $expout ); $key = $this->get_assesment_item_id($question); $exportquestions[$key] = $question; $exportquestions[$key]->exporttext = $expout; } return $exportquestions; }/** * Creates the export text for a question * * @todo handle in-line media (specified in the question/subquestion/answer text) for course-level exports * @param object $question * @param boolean $shuffleanswers whether or not to shuffle the answers * @param boolean $courselevel whether or not this is a course-level export * @param string $path provide the path to copy question media files to, if $courselevel == true * @return string containing export text */ function writequestion($question, $shuffleanswers = null, $courselevel = false, $path = '') { // turns question into string // question reflects database fields for general question and specific to type global $CFG; $expout = ''; //need to unencode the html entities in the questiontext field. // the whole question object was earlier run throught htmlspecialchars in xml_entitize(). $question->questiontext = html_entity_decode($question->questiontext, ENT_COMPAT); $hasimage = empty($question->image) ? 0 : 1; $hassize = empty($question->mediax) ? 0 : 1; $allowedtags = '<a><br><b><h1><h2><h3><h4><i><img><li><ol><strong><table><tr><td><th><u><ul><object>'; // all other tags will be stripped from question text $smarty =& $this->init_smarty(); $assesmentitemid = $this->get_assesment_item_id($question); $question_type = $this->get_qtype( $question->qtype ); $questionid = "question{$question->id}$question_type"; $smarty->assign('question_has_image', $hasimage); $smarty->assign('hassize', $hassize); $smarty->assign('questionid', $questionid); $smarty->assign('assessmentitemidentifier', $assesmentitemid); $smarty->assign('assessmentitemtitle', $question->name); $smarty->assign('courselevelexport', $courselevel); if ($question->qtype == MULTIANSWER) { $question->questiontext = strip_tags($question->questiontext, $allowedtags . '<intro>'); $smarty->assign('questionText', $this->get_cloze_intro($question->questiontext)); } else { $smarty->assign('questionText', strip_tags($question->questiontext, $allowedtags)); } $smarty->assign('question', $question); // the following two are left for compatibility; the templates should be changed, though, to make object tags for the questions //$smarty->assign('questionimage', $question->image); //$smarty->assign('questionimagealt', "image: $question->image"); // output depends on question type switch($question->qtype) { case TRUEFALSE: $qanswers = $question->options->answers; $answers[0] = (array)$qanswers['true']; $answers[0]['answer'] = get_string("true", "quiz"); $answers[1] = (array)$qanswers['false']; $answers[1]['answer'] = get_string("false", "quiz"); if (!empty($shuffleanswers)) { $answers = $this->shuffle_things($answers); } if (isset($question->response)) { $correctresponseid = $question->response[$questionid]; if ($answers[0]['id'] == $correctresponseid) { $correctresponse = $answers[0]; } else { $correctresponse = $answers[1]; } } else { $correctresponse = ''; } $smarty->assign('correctresponse', $correctresponse); $smarty->assign('answers', $answers); $expout = $smarty->fetch('choice.tpl'); break; case MULTICHOICE: $answers = $this->objects_to_array($question->options->answers); $correctresponses = $this->get_correct_answers($answers); $correctcount = count($correctresponses); $smarty->assign('responsedeclarationcardinality', $question->options->single ? 'single' : 'multiple'); $smarty->assign('operator', $question->options->single ? 'match' : 'member'); $smarty->assign('correctresponses', $correctresponses); $smarty->assign('answers', $answers); $smarty->assign('maxChoices', $question->options->single ? '1' : count($answers)); $smarty->assign('maxChoices', $question->options->single ? '1' : count($answers)); $smarty->assign('shuffle', empty($shuffleanswers) ? 'false' : 'true'); $smarty->assign('generalfeedback', $question->generalfeedback); $smarty->assign('correctfeedback', $question->options->correctfeedback); $smarty->assign('partiallycorrectfeedback', $question->options->partiallycorrectfeedback); $smarty->assign('incorrectfeedback', $question->options->incorrectfeedback); $expout = $smarty->fetch('choiceMultiple.tpl'); break; case SHORTANSWER: $answers = $this->objects_to_array($question->options->answers); if (!empty($shuffleanswers)) { $answers = $this->shuffle_things($answers); } $correctresponses = $this->get_correct_answers($answers); $correctcount = count($correctresponses); $smarty->assign('responsedeclarationcardinality', $correctcount > 1 ? 'multiple' : 'single'); $smarty->assign('correctresponses', $correctresponses); $smarty->assign('answers', $answers); $expout = $smarty->fetch('textEntry.tpl'); break; case NUMERICAL: $qanswer = array_pop( $question->options->answers ); $smarty->assign('lowerbound', $qanswer->answer - $qanswer->tolerance); $smarty->assign('upperbound', $qanswer->answer + $qanswer->tolerance); $smarty->assign('answer', $qanswer->answer); $expout = $smarty->fetch('numerical.tpl'); break; case MATCH: $this->xml_entitize($question->options->subquestions); $subquestions = $this->objects_to_array($question->options->subquestions); if (!empty($shuffleanswers)) { $subquestions = $this->shuffle_things($subquestions); } $setcount = count($subquestions); $smarty->assign('setcount', $setcount); $smarty->assign('matchsets', $subquestions); $expout = $smarty->fetch('match.tpl'); break; case DESCRIPTION: $expout = $smarty->fetch('extendedText.tpl'); break;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?