abstractqtype.php

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

PHP
753
字号
<?php  // $Id: abstractqtype.php,v 1.16.2.4 2009/02/10 05:40:13 tjhunt Exp $////////////////////////////////////////////////////////////////// ABSTRACT SUPERCLASS FOR QUSTION TYPES THAT USE DATASETS ///////////////////////////////////////////////////////////////////** * @package questionbank * @subpackage questiontypes */require_once($CFG->libdir.'/filelib.php');define("LITERAL", "1");define("FILE", "2");define("LINK", "3");class question_dataset_dependent_questiontype extends default_questiontype {    var $virtualqtype = false;    function name() {        return 'datasetdependent';    }    function save_question_options($question) {        // Default does nothing...        return true;    }    function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {        // Find out how many datasets are available        global $CFG;        if(!$maxnumber = (int)get_field_sql(                            "SELECT MIN(a.itemcount)                            FROM {$CFG->prefix}question_dataset_definitions a,                                 {$CFG->prefix}question_datasets b                            WHERE b.question = $question->id                            AND   a.id = b.datasetdefinition")) {            error("Couldn't get the specified dataset for a calculated " .                  "question! (question: {$question->id}");        }        // Choose a random dataset        $state->options->datasetitem = rand(1, $maxnumber);        $state->options->dataset =         $this->pick_question_dataset($question,$state->options->datasetitem);        $state->responses = array('' => '');        return true;    }    function restore_session_and_responses(&$question, &$state) {        if (!ereg('^dataset([0-9]+)[^-]*-(.*)$',                $state->responses[''], $regs)) {            notify ("Wrongly formatted raw response answer " .                   "{$state->responses['']}! Could not restore session for " .                   " question #{$question->id}.");            $state->options->datasetitem = 1;            $state->options->dataset = array();            $state->responses = array('' => '');            return false;        }        // Restore the chosen dataset        $state->options->datasetitem = $regs[1];        $state->options->dataset =         $this->pick_question_dataset($question,$state->options->datasetitem);        $state->responses = array('' => $regs[2]);        return true;    }    function save_session_and_responses(&$question, &$state) {        $responses = 'dataset'.$state->options->datasetitem.'-'.         $state->responses[''];        // Set the legacy answer field        if (!set_field('question_states', 'answer', $responses, 'id',         $state->id)) {            return false;        }        return true;    }    function substitute_variables($str, $dataset) {        foreach ($dataset as $name => $value) {            if($value < 0 ){                $str = str_replace('{'.$name.'}', '('.$value.')', $str);            } else {            $str = str_replace('{'.$name.'}', $value, $str);        }        }        return $str;    }    function finished_edit_wizard(&$form) {        return isset($form->backtoquiz);    }    // This gets called by editquestion.php after the standard question is saved    function print_next_wizard_page(&$question, &$form, $course) {        global $CFG, $USER, $SESSION, $COURSE;        // Catch invalid navigation & reloads        if (empty($question->id) && empty($SESSION->datasetdependent)) {            redirect('edit.php?courseid='.$COURSE->id, 'The page you are loading has expired.', 3);        }        // See where we're coming from        switch($form->wizardpage) {            case 'question':                require("$CFG->dirroot/question/type/datasetdependent/datasetdefinitions.php");                break;            case 'datasetdefinitions':            case 'datasetitems':                require("$CFG->dirroot/question/type/datasetdependent/datasetitems.php");                break;            default:                error('Incorrect or no wizard page specified!');                break;        }    }    // This gets called by question2.php after the standard question is saved    function &next_wizard_form($submiturl, $question, $wizardnow){        global $CFG, $SESSION, $COURSE;        // Catch invalid navigation & reloads        if (empty($question->id) && empty($SESSION->datasetdependent)) {            redirect('edit.php?courseid='.$COURSE->id, 'The page you are loading has expired. Cannot get next wizard form.', 3);        }        if (empty($question->id)){            $question =& $SESSION->datasetdependent->questionform;        }        // See where we're coming from        switch($wizardnow) {            case 'datasetdefinitions':                require("$CFG->dirroot/question/type/datasetdependent/datasetdefinitions_form.php");                $mform =& new question_dataset_dependent_definitions_form("$submiturl?wizardnow=datasetdefinitions", $question);                break;            case 'datasetitems':                require("$CFG->dirroot/question/type/datasetdependent/datasetitems_form.php");                $regenerate = optional_param('forceregeneration', 0, PARAM_BOOL);                $mform =& new question_dataset_dependent_items_form("$submiturl?wizardnow=datasetitems", $question, $regenerate);                break;            default:                error('Incorrect or no wizard page specified!');                break;        }        return $mform;    }    /**     * This method should be overriden if you want to include a special heading or some other     * html on a question editing page besides the question editing form.     *     * @param question_edit_form $mform a child of question_edit_form     * @param object $question     * @param string $wizardnow is '' for first page.     */    function display_question_editing_page(&$mform, $question, $wizardnow){        switch ($wizardnow){            case '':                //on first page default display is fine                parent::display_question_editing_page($mform, $question, $wizardnow);                return;                break;            case 'datasetdefinitions':                print_heading_with_help(get_string("choosedatasetproperties", "quiz"), "questiondatasets", "quiz");                break;            case 'datasetitems':                print_heading_with_help(get_string("editdatasets", "quiz"), 'questiondatasets', "quiz");                break;        }        $mform->display();    }     /**     * This method prepare the $datasets in a format similar to dadatesetdefinitions_form.php     * so that they can be saved     * using the function save_dataset_definitions($form)     *  when creating a new calculated question or     *  whenediting an already existing calculated question     * or by  function save_as_new_dataset_definitions($form, $initialid)     *  when saving as new an already existing calculated question     *     * @param object $form     * @param int $questionfromid default = '0'     */    function preparedatasets(&$form , $questionfromid='0'){        // the dataset names present in the edit_question_form and edit_calculated_form are retrieved        $possibledatasets = $this->find_dataset_names($form->questiontext);        $mandatorydatasets = array();            foreach ($form->answers as $answer) {                $mandatorydatasets += $this->find_dataset_names($answer);            }        // if there are identical datasetdefs already saved in the original question.        // either when editing a question or saving as new        // they are retrieved using $questionfromid        if ($questionfromid!='0'){            $form->id = $questionfromid ;        }        $datasets = array();        $key = 0 ;        // always prepare the mandatorydatasets present in the answers        // the $options are not used here        foreach ($mandatorydatasets as $datasetname) {            if (!isset($datasets[$datasetname])) {                list($options, $selected) =                        $this->dataset_options($form, $datasetname);                $datasets[$datasetname]='';                 $form->dataset[$key]=$selected ;                $key++;            }        }        // do not prepare possibledatasets when creating a question        // they will defined and stored with datasetdefinitions_form.php        // the $options are not used here        if ($questionfromid!='0'){        foreach ($possibledatasets as $datasetname) {            if (!isset($datasets[$datasetname])) {                list($options, $selected) =                        $this->dataset_options($form, $datasetname,false);                $datasets[$datasetname]='';                 $form->dataset[$key]=$selected ;                $key++;            }        }        }     return $datasets ;     }    /**    * this version save the available data at the different steps of the question editing process    * without using global $SESSION as storage between steps    * at the first step $wizardnow = 'question'    *  when creating a new question    *  when modifying a question    *  when copying as a new question    *  the general parameters and answers are saved using parent::save_question    *  then the datasets are prepared and saved    * at the second step $wizardnow = 'datasetdefinitions'    *  the datadefs final type are defined as private, category or not a datadef    * at the third step $wizardnow = 'datasetitems'    *  the datadefs parameters and the data items are created or defined    *    * @param object question

⌨️ 快捷键说明

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