questiontype.php

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

PHP
723
字号
        // mark the state as graded        $state->event = ($state->event ==  QUESTION_EVENTCLOSE) ? QUESTION_EVENTCLOSEANDGRADE : QUESTION_EVENTGRADE;        return true;    }    function compare_responses($question, $state, $teststate) {        foreach ($state->responses as $i=>$sr) {            if (empty($teststate->responses[$i])) {                if (!empty($state->responses[$i])) {                    return false;                }            } else if ($state->responses[$i] != $teststate->responses[$i]) {                return false;            }        }        return true;    }    // ULPGC ecastro for stats report    function get_all_responses($question, $state) {        $answers = array();        if (is_array($question->options->subquestions)) {            foreach ($question->options->subquestions as $aid => $answer) {                if ($answer->questiontext !== '' && !is_null($answer->questiontext)) {                    $r = new stdClass;                    $r->answer = $answer->questiontext . ": " . $answer->answertext;                    $r->credit = 1;                    $answers[$aid] = $r;                }            }        }        $result = new stdClass;        $result->id = $question->id;        $result->responses = $answers;        return $result;    }    // ULPGC ecastro    function get_actual_response($question, $state) {       $subquestions = &$state->options->subquestions;       $responses    = &$state->responses;       $results=array();       foreach ($subquestions as $key => $sub) {           foreach ($responses as $ind => $code) {               if (isset($sub->options->answers[$code])) {                   $results[$ind] =  $subquestions[$ind]->questiontext . ": " . $sub->options->answers[$code]->answer;               }           }       }       return $results;   }    function response_summary($question, $state, $length=80) {        // This should almost certainly be overridden        return shorten_text(implode(', ', $this->get_actual_response($question, $state)), $length);    }/// BACKUP FUNCTIONS ////////////////////////////    /*     * Backup the data in the question     *     * This is used in question/backuplib.php     */    function backup($bf,$preferences,$question,$level=6) {        $status = true;        // Output the shuffleanswers setting.        $matchoptions = get_record('question_match', 'question', $question);        if ($matchoptions) {            $status = fwrite ($bf,start_tag("MATCHOPTIONS",6,true));            fwrite ($bf,full_tag("SHUFFLEANSWERS",7,false,$matchoptions->shuffleanswers));            $status = fwrite ($bf,end_tag("MATCHOPTIONS",6,true));        }        $matchs = get_records('question_match_sub', 'question', $question, 'id ASC');        //If there are matchs        if ($matchs) {            //Print match contents            $status = fwrite ($bf,start_tag("MATCHS",6,true));            //Iterate over each match            foreach ($matchs as $match) {                $status = fwrite ($bf,start_tag("MATCH",7,true));                //Print match contents                fwrite ($bf,full_tag("ID",8,false,$match->id));                fwrite ($bf,full_tag("CODE",8,false,$match->code));                fwrite ($bf,full_tag("QUESTIONTEXT",8,false,$match->questiontext));                fwrite ($bf,full_tag("ANSWERTEXT",8,false,$match->answertext));                $status = fwrite ($bf,end_tag("MATCH",7,true));            }            $status = fwrite ($bf,end_tag("MATCHS",6,true));        }        return $status;    }/// RESTORE FUNCTIONS /////////////////    /*     * Restores the data in the question     *     * This is used in question/restorelib.php     */    function restore($old_question_id,$new_question_id,$info,$restore) {        $status = true;        //Get the matchs array        $matchs = $info['#']['MATCHS']['0']['#']['MATCH'];        //We have to build the subquestions field (a list of match_sub id)        $subquestions_field = "";        $in_first = true;        //Iterate over matchs        for($i = 0; $i < sizeof($matchs); $i++) {            $mat_info = $matchs[$i];            //We'll need this later!!            $oldid = backup_todb($mat_info['#']['ID']['0']['#']);            //Now, build the question_match_SUB record structure            $match_sub = new stdClass;            $match_sub->question = $new_question_id;            $match_sub->code = isset($mat_info['#']['CODE']['0']['#'])?backup_todb($mat_info['#']['CODE']['0']['#']):'';            if (!$match_sub->code) {                $match_sub->code = $oldid;            }            $match_sub->questiontext = backup_todb($mat_info['#']['QUESTIONTEXT']['0']['#']);            $match_sub->answertext = backup_todb($mat_info['#']['ANSWERTEXT']['0']['#']);            //The structure is equal to the db, so insert the question_match_sub            $newid = insert_record ("question_match_sub",$match_sub);            //Do some output            if (($i+1) % 50 == 0) {                if (!defined('RESTORE_SILENTLY')) {                    echo ".";                    if (($i+1) % 1000 == 0) {                        echo "<br />";                    }                }                backup_flush(300);            }            if ($newid) {                //We have the newid, update backup_ids                backup_putid($restore->backup_unique_code,"question_match_sub",$oldid,                             $newid);                //We have a new match_sub, append it to subquestions_field                if ($in_first) {                    $subquestions_field .= $newid;                    $in_first = false;                } else {                    $subquestions_field .= ",".$newid;                }            } else {                $status = false;            }        }        //We have created every match_sub, now create the match        $match = new stdClass;        $match->question = $new_question_id;        $match->subquestions = $subquestions_field;        // Get the shuffleanswers option, if it is there.        if (!empty($info['#']['MATCHOPTIONS']['0']['#']['SHUFFLEANSWERS'])) {            $match->shuffleanswers = backup_todb($info['#']['MATCHOPTIONS']['0']['#']['SHUFFLEANSWERS']['0']['#']);        } else {            $match->shuffleanswers = 1;        }        //The structure is equal to the db, so insert the question_match_sub        $newid = insert_record ("question_match",$match);        if (!$newid) {            $status = false;        }        return $status;    }    function restore_map($old_question_id,$new_question_id,$info,$restore) {        $status = true;        //Get the matchs array        $matchs = $info['#']['MATCHS']['0']['#']['MATCH'];        //We have to build the subquestions field (a list of match_sub id)        $subquestions_field = "";        $in_first = true;        //Iterate over matchs        for($i = 0; $i < sizeof($matchs); $i++) {            $mat_info = $matchs[$i];            //We'll need this later!!            $oldid = backup_todb($mat_info['#']['ID']['0']['#']);            //Now, build the question_match_SUB record structure            $match_sub->question = $new_question_id;            $match_sub->questiontext = backup_todb($mat_info['#']['QUESTIONTEXT']['0']['#']);            $match_sub->answertext = backup_todb($mat_info['#']['ANSWERTEXT']['0']['#']);            //If we are in this method is because the question exists in DB, so its            //match_sub must exist too.            //Now, we are going to look for that match_sub in DB and to create the            //mappings in backup_ids to use them later where restoring states (user level).            //Get the match_sub from DB (by question, questiontext and answertext)            $db_match_sub = get_record ("question_match_sub","question",$new_question_id,                                                      "questiontext",$match_sub->questiontext,                                                      "answertext",$match_sub->answertext);            //Do some output            if (($i+1) % 50 == 0) {                if (!defined('RESTORE_SILENTLY')) {                    echo ".";                    if (($i+1) % 1000 == 0) {                        echo "<br />";                    }                }                backup_flush(300);            }            //We have the database match_sub, so update backup_ids            if ($db_match_sub) {                //We have the newid, update backup_ids                backup_putid($restore->backup_unique_code,"question_match_sub",$oldid,                             $db_match_sub->id);            } else {                $status = false;            }        }        return $status;    }    function restore_recode_answer($state, $restore) {        //The answer is a comma separated list of hypen separated math_subs (for question and answer)        $answer_field = "";        $in_first = true;        $tok = strtok($state->answer,",");        while ($tok) {            //Extract the match_sub for the question and the answer            $exploded = explode("-",$tok);            $match_question_id = $exploded[0];            $match_answer_id = $exploded[1];            //Get the match_sub from backup_ids (for the question)            if (!$match_que = backup_getid($restore->backup_unique_code,"question_match_sub",$match_question_id)) {                echo 'Could not recode question in question_match_sub '.$match_question_id.'<br />';            } else {                if ($in_first) {                    $in_first = false;                } else {                    $answer_field .= ',';                }                $answer_field .= $match_que->new_id.'-'.$match_answer_id;            }            //check for next            $tok = strtok(",");        }        return $answer_field;    }    /**     * Decode links in question type specific tables.     * @return bool success or failure.     */    function decode_content_links_caller($questionids, $restore, &$i) {        $status = true;        // Decode links in the question_match_sub table.        if ($subquestions = get_records_list('question_match_sub', 'question',                implode(',',  $questionids), '', 'id, questiontext')) {            foreach ($subquestions as $subquestion) {                $questiontext = restore_decode_content_links_worker($subquestion->questiontext, $restore);                if ($questiontext != $subquestion->questiontext) {                    $subquestion->questiontext = addslashes($questiontext);                    if (!update_record('question_match_sub', $subquestion)) {                        $status = false;                    }                }                // Do some output.                if (++$i % 5 == 0 && !defined('RESTORE_SILENTLY')) {                    echo ".";                    if ($i % 100 == 0) {                        echo "<br />";                    }                    backup_flush(300);                }            }        }        return $status;    }    function find_file_links($question, $courseid){        // find links in the question_match_sub table.        $urls = array();        if (isset($question->options->subquestions)){            foreach ($question->options->subquestions as $subquestion) {                $urls += question_find_file_links_from_html($subquestion->questiontext, $courseid);            }            //set all the values of the array to the question object            if ($urls){                $urls = array_combine(array_keys($urls), array_fill(0, count($urls), array($question->id)));            }        }        $urls = array_merge_recursive($urls, parent::find_file_links($question, $courseid));        return $urls;    }    function replace_file_links($question, $fromcourseid, $tocourseid, $url, $destination){        parent::replace_file_links($question, $fromcourseid, $tocourseid, $url, $destination);        // replace links in the question_match_sub table.        if (isset($question->options->subquestions)){            foreach ($question->options->subquestions as $subquestion) {                $subquestionchanged = false;                $subquestion->questiontext = question_replace_file_links_in_html($subquestion->questiontext, $fromcourseid, $tocourseid, $url, $destination, $subquestionchanged);                if ($subquestionchanged){//need to update rec in db                    if (!update_record('question_match_sub', addslashes_recursive($subquestion))) {                        error('Couldn\'t update \'question_match_sub\' record '.$subquestion->id);                    }                }            }        }    }    /**     * Runs all the code required to set up and save an essay question for testing purposes.     * Alternate DB table prefix may be used to facilitate data deletion.     */    function generate_test($name, $courseid = null) {        list($form, $question) = parent::generate_test($name, $courseid);        $form->shuffleanswers = 1;        $form->noanswers = 3;        $form->subquestions = array('cat', 'dog', 'cow');        $form->subanswers = array('feline', 'canine', 'bovine');        if ($courseid) {            $course = get_record('course', 'id', $courseid);        }        return $this->save_question($question, $form, $course);    }}//// END OF CLASS ////////////////////////////////////////////////////////////////////////////////// INITIATION - Without this line the question type is not in use... /////////////////////////////////////////////////////////////////////////////question_register_questiontype(new question_match_qtype());?>

⌨️ 快捷键说明

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