questiontype.php
来自「很棒的在线教学系统」· PHP 代码 · 共 404 行 · 第 1/2 页
PHP
404 行
<?php // $Id: questiontype.php,v 1.12.2.9 2009/02/17 06:14:48 tjhunt Exp $/** * Class for the random question type. * * The random question type does not have any options. When the question is * attempted, it picks a question at random from the category it is in (and * optionally its subcategories). For details see create_session_and_responses. * Then all other method calls as delegated to that other question. * * @package questionbank * @subpackage questiontypes */class random_qtype extends default_questiontype { var $selectmanual; var $excludedqtypes = null; var $manualqtypes = null; function random_qtype() { $this->selectmanual = get_config('qtype_random', 'selectmanual'); } // Caches questions available as randoms sorted by category // This is a 2-d array. The first key is question category, and the // second is whether to include subcategories. var $catrandoms = array(); function name() { return 'random'; } function menu_name() { // Don't include this question type in the 'add new question' menu. return false; } function is_manual_graded() { return $this->selectmanual; } function is_question_manual_graded($question, $otherquestionsinuse) { if (!$this->selectmanual) { return false; } // We take our best shot at working whether a particular question is manually // graded follows: We look to see if any of the questions that this random // question might select if of a manually graded type. If a category contains // a mixture of manual and non-manual questions, and if all the attempts so // far selected non-manual ones, this will give the wrong answer, but we // don't care. Even so, this is an expensive calculation! $this->init_qtype_lists(); if (!$this->manualqtypes) { return false; } if ($question->questiontext) { $categorylist = question_categorylist($question->category); } else { $categorylist = $question->category; } return record_exists_select('question', "category IN ($categorylist) AND parent = 0 AND hidden = 0 AND id NOT IN ($otherquestionsinuse) AND qtype IN ($this->manualqtypes)"); } function is_usable_by_random() { return false; } /** * This method needs to be called before the ->excludedqtypes and * ->manualqtypes fields can be used. */ function init_qtype_lists() { global $QTYPES; if (is_null($this->excludedqtypes)) { $excludedqtypes = array(); $manualqtypes = array(); foreach ($QTYPES as $qtype) { $quotedname = "'" . $qtype->name() . "'"; if (!$qtype->is_usable_by_random()) { $excludedqtypes[] = $quotedname; } else if ($this->selectmanual && $qtype->is_manual_graded()) { $manualqtypes[] = $quotedname; } } $this->excludedqtypes = implode(',', $excludedqtypes); $this->manualqtypes = implode(',', $manualqtypes); } } function get_question_options(&$question) { // Don't do anything here, because the random question has no options. // Everything is handled by the create- or restore_session_and_responses // functions. return true; } /** * Random questions always get a question name that is Random (cateogryname). * This function is a centralised place to calculate that, given the category. */ function question_name($category) { return get_string('random', 'quiz') .' ('. $category->name .')'; } function save_question($question, $form, $course) { $form->name = ''; // Name is not a required field for random questions, but parent::save_question // Assumes that it is. return parent::save_question($question, $form, $course); } function save_question_options($question) { // No options, as such, but we set the parent field to the question's // own id. Setting the parent field has the effect of hiding this // question in various places. $updateobject = new stdClass; $updateobject->id = $question->id; $updateobject->parent = $question->id; // We also force the question name to be 'Random (categoryname)'. if (!$category = get_record('question_categories', 'id', $question->category)) { error('Could retrieve question category'); } $updateobject->name = addslashes($this->question_name($category)); return update_record('question', $updateobject); } /** * Get all the usable questions from a particular question category. * * @param integer $categoryid the id of a question category. * @param boolean whether to include questions from subcategories. * @param string $questionsinuse comma-separated list of question ids to exclude from consideration. * @return array of question records. */ function get_usable_questions_from_category($categoryid, $subcategories, $questionsinuse) { $this->init_qtype_lists(); if ($subcategories) { $categorylist = question_categorylist($categoryid); } else { $categorylist = $categoryid; } if (!$catrandoms = get_records_select('question', "category IN ($categorylist) AND parent = 0 AND hidden = 0 AND id NOT IN ($questionsinuse) AND qtype NOT IN ($this->excludedqtypes)", '', 'id')) { $catrandoms = array(); } return $catrandoms; } function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) { global $QTYPES; // Choose a random question from the category: // We need to make sure that no question is used more than once in the // quiz. Therfore the following need to be excluded: // 1. All questions that are explicitly assigned to the quiz // 2. All random questions // 3. All questions that are already chosen by an other random question // 4. Deleted questions if (!isset($cmoptions->questionsinuse)) { $cmoptions->questionsinuse = $attempt->layout; } if (!isset($this->catrandoms[$question->category][$question->questiontext])) { $catrandoms = $this->get_usable_questions_from_category($question->category, $question->questiontext == "1", $cmoptions->questionsinuse); $this->catrandoms[$question->category][$question->questiontext] = swapshuffle_assoc($catrandoms); } while ($wrappedquestion = array_pop($this->catrandoms[$question->category][$question->questiontext])) { if (!ereg("(^|,)$wrappedquestion->id(,|$)", $cmoptions->questionsinuse)) { /// $randomquestion is not in use and will therefore be used /// as the randomquestion here... $wrappedquestion = get_record('question', 'id', $wrappedquestion->id); $QTYPES[$wrappedquestion->qtype] ->get_question_options($wrappedquestion); $QTYPES[$wrappedquestion->qtype] ->create_session_and_responses($wrappedquestion, $state, $cmoptions, $attempt); $wrappedquestion->name_prefix = $question->name_prefix; $wrappedquestion->maxgrade = $question->maxgrade; $cmoptions->questionsinuse .= ",$wrappedquestion->id"; $state->options->question = &$wrappedquestion; return true; } } $question->questiontext = '<span class="notifyproblem">'. get_string('toomanyrandom', 'quiz'). '</span>'; $question->qtype = 'description'; $state->responses = array('' => ''); return true; } function restore_session_and_responses(&$question, &$state) { /// The raw response records for random questions come in two flavours:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?