questiontype.php

来自「很棒的在线教学系统」· PHP 代码 · 共 390 行 · 第 1/2 页

PHP
390
字号
<?php  // $Id: questiontype.php,v 1.20.2.12 2009/03/16 01:52:24 tjhunt Exp $////////////////////// SHORTANSWER ///////////////////////// QUESTION TYPE CLASS //////////////////////// This class contains some special features in order to make the/// question type embeddable within a multianswer (cloze) question////** * @package questionbank * @subpackage questiontypes */require_once("$CFG->dirroot/question/type/questiontype.php");class question_shortanswer_qtype extends default_questiontype {    function name() {        return 'shortanswer';    }    function extra_question_fields() {        return array('question_shortanswer','answers','usecase');    }    function questionid_column_name() {        return 'question';    }    function save_question_options($question) {        $result = new stdClass;        if (!$oldanswers = get_records('question_answers', 'question', $question->id, 'id ASC')) {            $oldanswers = array();        }        $answers = array();        $maxfraction = -1;        // Insert all the new answers        foreach ($question->answer as $key => $dataanswer) {            // Check for, and ingore, completely blank answer from the form.            if (trim($dataanswer) == '' && $question->fraction[$key] == 0 &&                    html_is_blank($question->feedback[$key])) {                continue;            }            if ($oldanswer = array_shift($oldanswers)) {  // Existing answer, so reuse it                $answer = $oldanswer;                $answer->answer   = trim($dataanswer);                $answer->fraction = $question->fraction[$key];                $answer->feedback = $question->feedback[$key];                if (!update_record("question_answers", $answer)) {                    $result->error = "Could not update quiz answer! (id=$answer->id)";                    return $result;                }            } else {    // This is a completely new answer                $answer = new stdClass;                $answer->answer   = trim($dataanswer);                $answer->question = $question->id;                $answer->fraction = $question->fraction[$key];                $answer->feedback = $question->feedback[$key];                if (!$answer->id = insert_record("question_answers", $answer)) {                    $result->error = "Could not insert quiz answer!";                    return $result;                }            }            $answers[] = $answer->id;            if ($question->fraction[$key] > $maxfraction) {                $maxfraction = $question->fraction[$key];            }        }        $question->answers = implode(',', $answers);        $parentresult = parent::save_question_options($question);        if($parentresult !== null) { // Parent function returns null if all is OK            return $parentresult;        }        // delete old answer records        if (!empty($oldanswers)) {            foreach($oldanswers as $oa) {                delete_records('question_answers', 'id', $oa->id);            }        }        /// Perform sanity checks on fractional grades        if ($maxfraction != 1) {            $maxfraction = $maxfraction * 100;            $result->noticeyesno = get_string("fractionsnomax", "quiz", $maxfraction);            return $result;        } else {            return true;        }    }    function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) {        global $CFG;    /// This implementation is also used by question type 'numerical'        $readonly = empty($options->readonly) ? '' : 'readonly="readonly"';        $formatoptions = new stdClass;        $formatoptions->noclean = true;        $formatoptions->para = false;        $nameprefix = $question->name_prefix;        /// Print question text and media        $questiontext = format_text($question->questiontext,                $question->questiontextformat,                $formatoptions, $cmoptions->course);        $image = get_question_image($question);        /// Print input controls        if (isset($state->responses['']) && $state->responses[''] != '') {            $value = ' value="'.s($state->responses[''], true).'" ';        } else {            $value = ' value="" ';        }        $inputname = ' name="'.$nameprefix.'" ';        $feedback = '';        $class = '';        $feedbackimg = '';        if ($options->feedback) {            $class = question_get_feedback_class(0);            $feedbackimg = question_get_feedback_image(0);            foreach($question->options->answers as $answer) {                if ($this->test_response($question, $state, $answer)) {                    // Answer was correct or partially correct.                    $class = question_get_feedback_class($answer->fraction);                    $feedbackimg = question_get_feedback_image($answer->fraction);                    if ($answer->feedback) {                        $feedback = format_text($answer->feedback, true, $formatoptions, $cmoptions->course);                    }                    break;                }            }        }        /// Removed correct answer, to be displayed later MDL-7496        include("$CFG->dirroot/question/type/shortanswer/display.html");    }    function check_response(&$question, &$state) {        foreach($question->options->answers as $aid => $answer) {            if ($this->test_response($question, $state, $answer)) {                return $aid;            }        }        return false;    }    function compare_responses($question, $state, $teststate) {        if (isset($state->responses['']) && isset($teststate->responses[''])) {            return $state->responses[''] === $teststate->responses[''];        }        return false;    }    function test_response(&$question, $state, $answer) {        // Trim the response before it is saved in the database. See MDL-10709        $state->responses[''] = trim($state->responses['']);        return $this->compare_string_with_wildcard(stripslashes_safe($state->responses['']),                $answer->answer, !$question->options->usecase);    }    function compare_string_with_wildcard($string, $pattern, $ignorecase) {        // Break the string on non-escaped asterisks.        $bits = preg_split('/(?<!\\\\)\*/', $pattern);        // Escape regexp special characters in the bits.        $excapedbits = array();        foreach ($bits as $bit) {            $excapedbits[] = preg_quote(str_replace('\*', '*', $bit));        }        // Put it back together to make the regexp.        $regexp = '|^' . implode('.*', $excapedbits) . '$|u';        // Make the match insensitive if requested to.        if ($ignorecase) {            $regexp .= 'i';        }        return preg_match($regexp, trim($string));    }    /*     * Override the parent class method, to remove escaping from asterisks.     */    function get_correct_responses(&$question, &$state) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?