diff --git a/question/type/multianswer/backup/moodle2/backup_qtype_multianswer_plugin.class.php b/question/type/multianswer/backup/moodle2/backup_qtype_multianswer_plugin.class.php index a388c81e3b2..e236c572602 100644 --- a/question/type/multianswer/backup/moodle2/backup_qtype_multianswer_plugin.class.php +++ b/question/type/multianswer/backup/moodle2/backup_qtype_multianswer_plugin.class.php @@ -59,7 +59,8 @@ class backup_qtype_multianswer_plugin extends backup_qtype_plugin { $pluginwrapper->add_child($multianswer); // set source to populate the data - $multianswer->set_source_table('question_multianswer', array('question' => backup::VAR_PARENTID)); + $multianswer->set_source_table('question_multianswer', + array('question' => backup::VAR_PARENTID)); // don't need to annotate ids nor files diff --git a/question/type/multianswer/backup/moodle2/restore_qtype_multianswer_plugin.class.php b/question/type/multianswer/backup/moodle2/restore_qtype_multianswer_plugin.class.php index 7a8ed488fa7..7e51373b10e 100644 --- a/question/type/multianswer/backup/moodle2/restore_qtype_multianswer_plugin.class.php +++ b/question/type/multianswer/backup/moodle2/restore_qtype_multianswer_plugin.class.php @@ -38,7 +38,6 @@ class restore_qtype_multianswer_plugin extends restore_qtype_plugin { * Returns the paths to be handled by the plugin at question level */ protected function define_question_plugin_structure() { - $paths = array(); // This qtype uses question_answers, add them @@ -46,10 +45,9 @@ class restore_qtype_multianswer_plugin extends restore_qtype_plugin { // Add own qtype stuff $elename = 'multianswer'; - $elepath = $this->get_pathfor('/multianswer'); // we used get_recommended_name() so this works + $elepath = $this->get_pathfor('/multianswer'); $paths[] = new restore_path_element($elename, $elepath); - return $paths; // And we return the interesting paths } @@ -67,7 +65,8 @@ class restore_qtype_multianswer_plugin extends restore_qtype_plugin { $newquestionid = $this->get_new_parentid('question'); $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false; - // If the question has been created by restore, we need to create its question_multianswer too + // If the question has been created by restore, we need to create its + // question_multianswer too if ($questioncreated) { // Adjust some columns $data->question = $newquestionid; @@ -79,8 +78,6 @@ class restore_qtype_multianswer_plugin extends restore_qtype_plugin { $newitemid = $DB->insert_record('question_multianswer', $data); // Create mapping (need it for after_execute recode of sequence) $this->set_mapping('question_multianswer', $oldid, $newitemid); - } else { - // Nothing to remap if the question already existed } } @@ -97,18 +94,21 @@ class restore_qtype_multianswer_plugin extends restore_qtype_plugin { global $DB; // Now that all the questions have been restored, let's process // the created question_multianswer sequences (list of question ids) - $rs = $DB->get_recordset_sql("SELECT qma.id, qma.sequence - FROM {question_multianswer} qma - JOIN {backup_ids_temp} bi ON bi.newitemid = qma.question - WHERE bi.backupid = ? - AND bi.itemname = 'question_created'", array($this->get_restoreid())); + $rs = $DB->get_recordset_sql(" + SELECT qma.id, qma.sequence + FROM {question_multianswer} qma + JOIN {backup_ids_temp} bi ON bi.newitemid = qma.question + WHERE bi.backupid = ? + AND bi.itemname = 'question_created'", + array($this->get_restoreid())); foreach ($rs as $rec) { $sequencearr = explode(',', $rec->sequence); foreach ($sequencearr as $key => $question) { $sequencearr[$key] = $this->get_mappingid('question', $question); } $sequence = implode(',', $sequencearr); - $DB->set_field('question_multianswer', 'sequence', $sequence, array('id' => $rec->id)); + $DB->set_field('question_multianswer', 'sequence', $sequence, + array('id' => $rec->id)); } $rs->close(); } @@ -127,7 +127,8 @@ class restore_qtype_multianswer_plugin extends restore_qtype_plugin { $answer = $state->answer; $resultarr = array(); // Get sequence of questions - $sequence = $DB->get_field('question_multianswer', 'sequence', array('question' => $state->question)); + $sequence = $DB->get_field('question_multianswer', 'sequence', + array('question' => $state->question)); $sequencearr = explode(',', $sequence); // Let's process each pair foreach (explode(',', $answer) as $pair) { diff --git a/question/type/multianswer/edit_multianswer_form.php b/question/type/multianswer/edit_multianswer_form.php index c5658279f02..cd35c521579 100644 --- a/question/type/multianswer/edit_multianswer_form.php +++ b/question/type/multianswer/edit_multianswer_form.php @@ -35,221 +35,257 @@ defined('MOODLE_INTERNAL') || die(); */ class question_edit_multianswer_form extends question_edit_form { - // $questiondisplay will contain the qtype_multianswer_extract_question from the questiontext - public $questiondisplay ; - // $savedquestiondisplay will contain the qtype_multianswer_extract_question from the questiontext in database - public $savedquestion ; - public $savedquestiondisplay ; - public $used_in_quiz = false ; - public $qtype_change = false ; - public $negative_diff = 0 ; + // $questiondisplay will contain the qtype_multianswer_extract_question from + // the questiontext + public $questiondisplay; + // $savedquestiondisplay will contain the qtype_multianswer_extract_question + // from the questiontext in database + public $savedquestion; + public $savedquestiondisplay; + public $used_in_quiz = false; + public $qtype_change = false; + public $negative_diff = 0; public $nb_of_quiz = 0; public $nb_of_attempts = 0; - public $confirm = 0 ; - public $reload = false ; + public $confirm = 0; + public $reload = false; - function question_edit_multianswer_form(&$submiturl, &$question, &$category, &$contexts, $formeditable = true){ - global $QTYPES, $SESSION, $CFG, $DB; + public function __construct($submiturl, $question, $category, $contexts, $formeditable = true) { + global $SESSION, $CFG, $DB; $this->regenerate = true; - if ( "1" == optional_param('reload','', PARAM_INT )) { - $this->reload = true ; - }else { - $this->reload = false ; + if ("1" == optional_param('reload', '', PARAM_INT)) { + $this->reload = true; + } else { + $this->reload = false; } - // $this->question = $question; - $this->used_in_quiz =false; - // echo "
question
";print_r($question);echo ""; - if(isset($question->id) && $question->id != 0 ){ - $this->savedquestiondisplay =fullclone($question ) ; - if ($list = $DB->get_records('quiz_question_instances', array( 'question'=> $question->id))){ - foreach($list as $key => $li){ + + $this->used_in_quiz = false; + + if (isset($question->id) && $question->id != 0) { + $this->savedquestiondisplay = fullclone($question); + if ($list = $DB->get_records('quiz_question_instances', + array('question' => $question->id))) { + foreach ($list as $key => $li) { $this->nb_of_quiz ++; - if($att = $DB->get_records('quiz_attempts',array( 'quiz'=> $li->quiz, 'preview'=> '0'))){ - $this->nb_of_attempts+= count($att); + if ($att = $DB->get_records('quiz_attempts', + array('quiz' => $li->quiz, 'preview' => '0'))) { + $this->nb_of_attempts += count($att); $this->used_in_quiz = true; } } } } - parent::question_edit_form($submiturl, $question, $category, $contexts, $formeditable); + parent::__construct($submiturl, $question, $category, $contexts, $formeditable); } protected function definition_inner($mform) { $mform->addElement('hidden', 'reload', 1); - // $mform->addElement('hidden', 'generalfeedback',''); $mform->setType('reload', PARAM_INT); // Remove meaningless defaultgrade field. $mform->removeElement('defaultgrade'); - $this->confirm = optional_param('confirm','0', PARAM_RAW); + $this->confirm = optional_param('confirm', '0', PARAM_RAW); // display the questions from questiontext; - if ( "" != optional_param('questiontext','', PARAM_RAW)) { - // echo "
optional_param('questiontext'
";print_r(optional_param('questiontext','', PARAM_RAW));echo "";
+ if ("" != optional_param('questiontext', '', PARAM_RAW)) {
+ $this->questiondisplay = fullclone(qtype_multianswer_extract_question(
+ optional_param('questiontext', '', PARAM_RAW)));
- $this->questiondisplay = fullclone(qtype_multianswer_extract_question(optional_param('questiontext','', PARAM_RAW))) ;
-
- }else {
- if(!$this->reload && !empty($this->savedquestiondisplay->id)){
+ } else {
+ if (!$this->reload && !empty($this->savedquestiondisplay->id)) {
// use database data as this is first pass
// question->id == 0 so no stored datasets
$this->questiondisplay = fullclone($this->savedquestiondisplay);
- foreach($this->questiondisplay->options->questions as $subquestion){
- if (!empty($subquestion)){
+ foreach ($this->questiondisplay->options->questions as $subquestion) {
+ if (!empty($subquestion)) {
$subquestion->answer = array('');
- foreach($subquestion->options->answers as $ans){
- $subquestion->answer[]=$ans->answer ;
+ foreach ($subquestion->options->answers as $ans) {
+ $subquestion->answer[] = $ans->answer;
}
- // $subquestion->answer = fullclone($subquestion->options->answers);
}
}
- }else {
+ } else {
$this->questiondisplay = "";
}
}
- if ( isset($this->savedquestiondisplay->options->questions) && is_array($this->savedquestiondisplay->options->questions) ) {
- $countsavedsubquestions =0;
- foreach($this->savedquestiondisplay->options->questions as $subquestion){
- if (!empty($subquestion)){
+ if (isset($this->savedquestiondisplay->options->questions) &&
+ is_array($this->savedquestiondisplay->options->questions)) {
+ $countsavedsubquestions = 0;
+ foreach ($this->savedquestiondisplay->options->questions as $subquestion) {
+ if (!empty($subquestion)) {
$countsavedsubquestions++;
}
}
} else {
- $countsavedsubquestions =0;
+ $countsavedsubquestions = 0;
}
- if ($this->reload){
- if ( isset($this->questiondisplay->options->questions) && is_array($this->questiondisplay->options->questions) ) {
- $countsubquestions =0;
- foreach($this->questiondisplay->options->questions as $subquestion){
- if (!empty($subquestion)){
+ if ($this->reload) {
+ if (isset($this->questiondisplay->options->questions) &&
+ is_array($this->questiondisplay->options->questions)) {
+ $countsubquestions = 0;
+ foreach ($this->questiondisplay->options->questions as $subquestion) {
+ if (!empty($subquestion)) {
$countsubquestions++;
}
}
} else {
- $countsubquestions =0;
+ $countsubquestions = 0;
}
- }else{
- $countsubquestions =$countsavedsubquestions ;
+ } else {
+ $countsubquestions = $countsavedsubquestions;
}
- // echo "count subquestion $countsubquestions
";print_r($this->savedquestiondisplay);echo ""; - // // echo "
saved question $countsubquestions
";print_r($this->questiondisplay);echo ""; - - $mform->addElement('submit', 'analyzequestion', get_string('decodeverifyquestiontext','qtype_multianswer')); + $mform->addElement('submit', 'analyzequestion', + get_string('decodeverifyquestiontext', 'qtype_multianswer')); $mform->registerNoSubmitButton('analyzequestion'); - if ( $this->reload ){ + if ($this->reload) { $mform->addElement('html', '
questiondisplay ".$data['questiontext']['text']."
";print_r($questiondisplay);echo ""; if (isset($questiondisplay->options->questions)) { - $subquestions = fullclone($questiondisplay->options->questions) ; + $subquestions = fullclone($questiondisplay->options->questions); if (count($subquestions)) { - $sub =1; + $sub = 1; foreach ($subquestions as $subquestion) { - $prefix = 'sub_'.$sub.'_' ; + $prefix = 'sub_'.$sub.'_'; $answercount = 0; $maxgrade = false; $maxfraction = -1; - if(isset($this->savedquestiondisplay->options->questions[$sub]->qtype) && - $this->savedquestiondisplay->options->questions[$sub]->qtype != $questiondisplay->options->questions[$sub]->qtype ){ - $storemess = " STORED QTYPE ".question_bank::get_qtype_name($this->savedquestiondisplay->options->questions[$sub]->qtype); - } - foreach ( $subquestion->answer as $key=>$answer) { + if (isset($this->savedquestiondisplay->options->questions[$sub]->qtype) && + $this->savedquestiondisplay->options->questions[$sub]->qtype != + $questiondisplay->options->questions[$sub]->qtype) { + $storemess = " STORED QTYPE ".question_bank::get_qtype_name( + $this->savedquestiondisplay->options->questions[$sub]->qtype); + } + foreach ($subquestion->answer as $key => $answer) { $trimmedanswer = trim($answer); if ($trimmedanswer !== '') { $answercount++; - if ($subquestion->qtype =='numerical' && !(is_numeric($trimmedanswer) || $trimmedanswer == '*')) { - $errors[$prefix.'answer['.$key.']']= get_string('answermustbenumberorstar', 'qtype_numerical'); + if ($subquestion->qtype == 'numerical' && + !(is_numeric($trimmedanswer) || $trimmedanswer == '*')) { + $errors[$prefix.'answer['.$key.']'] = + get_string('answermustbenumberorstar', 'qtype_numerical'); } if ($subquestion->fraction[$key] == 1) { $maxgrade = true; } if ($subquestion->fraction[$key] > $maxfraction) { - $maxfraction = $subquestion->fraction[$key] ; + $maxfraction = $subquestion->fraction[$key]; } } } - if ($answercount==0) { - if ( $subquestion->qtype =='multichoice' ) { - $errors[$prefix.'answer[0]']= get_string('notenoughanswers', 'qtype_multichoice', 2); - }else { - $errors[$prefix.'answer[0]'] = get_string('notenoughanswers', 'question', 1); + if ($answercount == 0) { + if ($subquestion->qtype == 'multichoice') { + $errors[$prefix.'answer[0]'] = + get_string('notenoughanswers', 'qtype_multichoice', 2); + } else { + $errors[$prefix.'answer[0]'] = + get_string('notenoughanswers', 'question', 1); } } if ($maxgrade == false) { - $errors[$prefix.'fraction[0]']=get_string('fractionsnomax', 'question'); + $errors[$prefix.'fraction[0]'] = + get_string('fractionsnomax', 'question'); } $sub++; } } else { - $errors['questiontext']=get_string('questionsmissing', 'qtype_multianswer'); + $errors['questiontext'] = get_string('questionsmissing', 'qtype_multianswer'); } } - // $question = qtype_multianswer_extract_question($data['questiontext']); - // if (isset $question->options->questions - if (( $this->negative_diff > 0 || $this->used_in_quiz && ($this->negative_diff > 0 ||$this->negative_diff < 0 || $this->qtype_change ))&& $this->confirm == 0 ){ - $errors['confirm']=get_string('confirmsave', 'qtype_multianswer',$this->negative_diff); - } + if (($this->negative_diff > 0 || $this->used_in_quiz && + ($this->negative_diff > 0 || $this->negative_diff < 0 || + $this->qtype_change))&& $this->confirm == 0) { + $errors['confirm'] = + get_string('confirmsave', 'qtype_multianswer', $this->negative_diff); + } return $errors; } - function qtype() { + public function qtype() { return 'multianswer'; } } diff --git a/question/type/multianswer/lang/en/qtype_multianswer.php b/question/type/multianswer/lang/en/qtype_multianswer.php index 2edbe58020d..f25f5ef72b1 100644 --- a/question/type/multianswer/lang/en/qtype_multianswer.php +++ b/question/type/multianswer/lang/en/qtype_multianswer.php @@ -44,7 +44,7 @@ $string['noquestions'] = 'The Cloze(multianswer) question "{$a} $string['qtypenotrecognized'] = 'questiontype {$a} not recognized'; $string['questionnadded'] = 'Question added'; $string['questiondefinition'] = 'Question definition'; -$string['questiondeleted'] = 'Question deleted' ; +$string['questiondeleted'] = 'Question deleted'; $string['questioninquiz'] = '
multianswer positionkey $positionkey response $response state
";print_r($state);echo ""; + $inputname = $nameprefix.$positionkey; + if (isset($state->responses[$positionkey])) { + $response = $state->responses[$positionkey]; + } else { + $response = null; + } - // Determine feedback popup if any - $popup = ''; - $style = ''; - $feedbackimg = ''; - $feedback = '' ; - $correctanswer = ''; - $strfeedbackwrapped = $strfeedback; + // Determine feedback popup if any + $popup = ''; + $style = ''; + $feedbackimg = ''; + $feedback = ''; + $correctanswer = ''; + $strfeedbackwrapped = $strfeedback; $testedstate = clone($state); if ($correctanswers = $QTYPES[$wrapped->qtype]->get_correct_responses($wrapped, $state)) { if ($options->readonly && $options->correct_responses) { $delimiter = ''; if ($correctanswers) { foreach ($correctanswers as $ca) { - switch($wrapped->qtype){ + switch($wrapped->qtype) { case 'numerical': case 'shortanswer': $correctanswer .= $delimiter.$ca; - break ; + break; case 'multichoice': - if (isset($answers[$ca])){ + if (isset($answers[$ca])) { $correctanswer .= $delimiter.$answers[$ca]->answer; } - break ; + break; } $delimiter = ', '; } } } - if ($correctanswer != '' ) { + if ($correctanswer != '') { $feedback = '
| + control; ?> + | ++ + | ++ feedback; ?> + | +
| + control; ?> + | ++ + | ++ feedback; ?> + | +
| - control; ?> - | -- - | -- feedback; ?> - | -
| - control; ?> - | -- - | -- feedback; ?> - | - -
questiontext 2
";print_r($question->questiontext);echo"";
}
-// echo"questiontext
";print_r($question->questiontext);echo"";
$question->questiontext = $question->questiontext;
-// echo"question
";print_r($question);echo"";
return $question;
}
diff --git a/question/type/randomsamatch/backup/moodle2/backup_qtype_randomsamatch_plugin.class.php b/question/type/randomsamatch/backup/moodle2/backup_qtype_randomsamatch_plugin.class.php
index e5c20c93da3..ca1cce36a0b 100644
--- a/question/type/randomsamatch/backup/moodle2/backup_qtype_randomsamatch_plugin.class.php
+++ b/question/type/randomsamatch/backup/moodle2/backup_qtype_randomsamatch_plugin.class.php
@@ -55,9 +55,8 @@ class backup_qtype_randomsamatch_plugin extends backup_qtype_plugin {
$pluginwrapper->add_child($randomsamatch);
// set source to populate the data
- $randomsamatch->set_source_table('question_randomsamatch', array('question' => backup::VAR_PARENTID));
-
- // don't need to annotate ids nor files
+ $randomsamatch->set_source_table('question_randomsamatch',
+ array('question' => backup::VAR_PARENTID));
return $plugin;
}
diff --git a/question/type/randomsamatch/backup/moodle2/restore_qtype_randomsamatch_plugin.class.php b/question/type/randomsamatch/backup/moodle2/restore_qtype_randomsamatch_plugin.class.php
index 4b26cdf934a..cc416453e08 100644
--- a/question/type/randomsamatch/backup/moodle2/restore_qtype_randomsamatch_plugin.class.php
+++ b/question/type/randomsamatch/backup/moodle2/restore_qtype_randomsamatch_plugin.class.php
@@ -43,10 +43,9 @@ class restore_qtype_randomsamatch_plugin extends restore_qtype_plugin {
// Add own qtype stuff
$elename = 'randomsamatch';
- $elepath = $this->get_pathfor('/randomsamatch'); // we used get_recommended_name() so this works
+ $elepath = $this->get_pathfor('/randomsamatch');
$paths[] = new restore_path_element($elename, $elepath);
-
return $paths; // And we return the interesting paths
}
@@ -64,7 +63,8 @@ class restore_qtype_randomsamatch_plugin extends restore_qtype_plugin {
$newquestionid = $this->get_new_parentid('question');
$questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
- // If the question has been created by restore, we need to create its question_randomsamatch too
+ // If the question has been created by restore, we need to create its
+ // question_randomsamatch too
if ($questioncreated) {
// Adjust some columns
$data->question = $newquestionid;
@@ -72,8 +72,6 @@ class restore_qtype_randomsamatch_plugin extends restore_qtype_plugin {
$newitemid = $DB->insert_record('question_randomsamatch', $data);
// Create mapping
$this->set_mapping('question_randomsamatch', $oldid, $newitemid);
- } else {
- // Nothing to remap if the question already existed
}
}
diff --git a/question/type/randomsamatch/edit_randomsamatch_form.php b/question/type/randomsamatch/edit_randomsamatch_form.php
index ca4cd43e59c..6a41cacca93 100644
--- a/question/type/randomsamatch/edit_randomsamatch_form.php
+++ b/question/type/randomsamatch/edit_randomsamatch_form.php
@@ -33,19 +33,15 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2007 Jamie Pratt me@jamiep.org
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
-class question_edit_randomsamatch_form extends question_edit_form {
- /**
- * Add question-type specific form fields.
- *
- * @param MoodleQuickForm $mform the form being built.
- */
- protected function definition_inner(&$mform) {
+class qtype_randomsamatch_edit_form extends question_edit_form {
+ protected function definition_inner($mform) {
$questionstoselect = array();
- for ($i=2; $i<=QUESTION_NUMANS; $i++){
+ for ($i = 2; $i <= qtype_randomsamatch::MAX_SUBQUESTIONS; $i++) {
$questionstoselect[$i] = $i;
}
- $mform->addElement('select', 'choose', get_string("randomsamatchnumber", "quiz"), $questionstoselect);
+ $mform->addElement('select', 'choose',
+ get_string('randomsamatchnumber', 'quiz'), $questionstoselect);
$mform->setType('feedback', PARAM_RAW);
$mform->addElement('hidden', 'fraction', 0);
@@ -54,11 +50,11 @@ class question_edit_randomsamatch_form extends question_edit_form {
protected function data_preprocessing($question) {
if (empty($question->name)) {
- $question->name = get_string("randomsamatch", "quiz");
+ $question->name = get_string('randomsamatch', 'quiz');
}
if (empty($question->questiontext)) {
- $question->questiontext = get_string("randomsamatchintro", "quiz");
+ $question->questiontext = get_string('randomsamatchintro', 'quiz');
}
return $question;
}
@@ -67,22 +63,22 @@ class question_edit_randomsamatch_form extends question_edit_form {
return 'randomsamatch';
}
- function validation($data, $files) {
- global $QTYPES, $DB;
+ public function validation($data, $files) {
+ global $DB;
$errors = parent::validation($data, $files);
if (isset($data->categorymoveto)) {
list($category) = explode(',', $data['categorymoveto']);
} else {
list($category) = explode(',', $data['category']);
}
- $saquestions = $QTYPES['randomsamatch']->get_sa_candidates($category);
+ $saquestions = question_bank::get_qtype('randomsamatch')->get_sa_candidates($category);
$numberavailable = count($saquestions);
- if ($saquestions === false){
+ if ($saquestions === false) {
$a = new stdClass();
$a->catname = $DB->get_field('question_categories', 'name', array('id' => $category));
$errors['choose'] = get_string('nosaincategory', 'qtype_randomsamatch', $a);
- } elseif ($numberavailable < $data['choose']){
+ } else if ($numberavailable < $data['choose']) {
$a = new stdClass();
$a->catname = $DB->get_field('question_categories', 'name', array('id' => $category));
$a->nosaquestions = $numberavailable;
diff --git a/question/type/randomsamatch/questiontype.php b/question/type/randomsamatch/questiontype.php
index 4f27059bc0f..b88006bf149 100644
--- a/question/type/randomsamatch/questiontype.php
+++ b/question/type/randomsamatch/questiontype.php
@@ -36,27 +36,21 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class question_randomsamatch_qtype extends qtype_match {
-/// Extends 'match' as there are quite a few simularities...
+class qtype_randomsamatch extends question_type {
+ const MAX_SUBQUESTIONS = 10;
- function name() {
- return 'randomsamatch';
+ public function requires_qtypes() {
+ return array('shortanswer', 'match');
}
- function requires_qtypes() {
- return array('shortanswer');
- }
-
- function is_usable_by_random() {
+ public function is_usable_by_random() {
return false;
}
- function get_question_options(&$question) {
- global $DB, $OUTPUT;
- if (!$question->options = $DB->get_record('question_randomsamatch', array('question' => $question->id))) {
- echo $OUTPUT->notification('Error: Missing question options for random short answer question '.$question->id.'!');
- return false;
- }
+ public function get_question_options($question) {
+ global $DB;
+ $question->options = $DB->get_record('question_randomsamatch',
+ array('question' => $question->id), '*', MUST_EXIST);
// This could be included as a flag in the database. It's already
// supported by the code.
@@ -66,7 +60,7 @@ class question_randomsamatch_qtype extends qtype_match {
}
- function save_question_options($question) {
+ public function save_question_options($question) {
global $DB;
$options->question = $question->id;
$options->choose = $question->choose;
@@ -76,23 +70,24 @@ class question_randomsamatch_qtype extends qtype_match {
return $result;
}
- if ($existing = $DB->get_record("question_randomsamatch", array("question" => $options->question))) {
+ if ($existing = $DB->get_record('question_randomsamatch',
+ array('question' => $options->question))) {
$options->id = $existing->id;
- $DB->update_record("question_randomsamatch", $options);
+ $DB->update_record('question_randomsamatch', $options);
} else {
- $DB->insert_record("question_randomsamatch", $options);
+ $DB->insert_record('question_randomsamatch', $options);
}
return true;
}
- function delete_question($questionid, $contextid) {
+ public function delete_question($questionid, $contextid) {
global $DB;
$DB->delete_records('question_randomsamatch', array('question' => $questionid));
parent::delete_question($questionid, $contextid);
}
- function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {
+ public function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {
// Choose a random shortanswer 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:
@@ -190,7 +185,7 @@ class question_randomsamatch_qtype extends qtype_match {
foreach ($responses as $response) {
$wqid = $response[0];
$state->responses[$wqid] = $response[1];
- if (!isset($wrappedquestions[$wqid])){
+ if (!isset($wrappedquestions[$wqid])) {
if (!$wrappedquestions[$wqid] = $DB->get_record('question', array('id' => $wqid))) {
echo $OUTPUT->notification("Couldn't get question (id=$wqid)!");
return false;
@@ -231,23 +226,11 @@ class question_randomsamatch_qtype extends qtype_match {
return true;
}
- function extract_response($rawresponse, $nameprefix) {
- /// Simple implementation that does not check with the database
- /// and thus - does not bother to check whether there has been
- /// any changes to the question options.
- $response = array();
- $rawitems = explode(',', $rawresponse->answer);
- foreach ($rawitems as $rawitem) {
- $splits = explode('-', $rawitem, 2);
- $response[$nameprefix.$splits[0]] = $splits[1];
- }
- return $response;
- }
-
- function get_sa_candidates($categorylist, $questionsinuse = 0) {
+ public function get_sa_candidates($categorylist, $questionsinuse = 0) {
global $DB;
list ($usql, $params) = $DB->get_in_or_equal($categorylist);
- list ($ques_usql, $ques_params) = $DB->get_in_or_equal(explode(',', $questionsinuse), SQL_PARAMS_QM, null, false);
+ list ($ques_usql, $ques_params) = $DB->get_in_or_equal(explode(',', $questionsinuse),
+ SQL_PARAMS_QM, null, false);
$params = array_merge($params, $ques_params);
return $DB->get_records_select('question',
"qtype = 'shortanswer' " .
@@ -256,80 +239,6 @@ class question_randomsamatch_qtype extends qtype_match {
"AND hidden = '0'" .
"AND id $ques_usql", $params);
}
- function get_all_responses($question, $state) {
- $answers = array();
- if (is_array($question->options->subquestions)) {
- foreach ($question->options->subquestions as $aid => $answer) {
- if ($answer->questiontext) {
- foreach($answer->options->answers as $ans ){
- $answer->answertext = $ans->answer ;
- }
- $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;
- }
- /**
- * The difference between this method an get_all_responses is that this
- * method is not passed a state object. It is the possible answers to a
- * question no matter what the state.
- * This method is not called for random questions.
- * @return array of possible answers.
- */
- function get_possible_responses(&$question) {
- global $QTYPES;
- static $answers = array();
- if (!isset($answers[$question->id])){
- if ($question->options->subcats) {
- // recurse into subcategories
- $categorylist = question_categorylist($question->category);
- } else {
- $categorylist = array($question->category);
- }
-
- $question->options->subquestions = $this->get_sa_candidates($categorylist);
- foreach ($question->options->subquestions as $key => $wrappedquestion) {
- if (!$QTYPES[$wrappedquestion->qtype]
- ->get_question_options($wrappedquestion)) {
- return false;
- }
-
- // Now we overwrite the $question->options->answers field to only
- // *one* (the first) correct answer. This loop can be deleted to
- // take all answers into account (i.e. put them all into the
- // drop-down menu.
- $foundcorrect = false;
- foreach ($wrappedquestion->options->answers as $answer) {
- if ($foundcorrect || $answer->fraction != 1.0) {
- unset($wrappedquestion->options->answers[$answer->id]);
- } else if (!$foundcorrect) {
- $foundcorrect = true;
- }
- }
- }
- $answers[$question->id] = array();
- if (is_array($question->options->subquestions)) {
- foreach ($question->options->subquestions as $subqid => $answer) {
- if ($answer->questiontext) {
- $ans = array_shift($answer->options->answers);
- $answer->answertext = $ans->answer ;
- $r = new stdClass();
- $r->answer = $answer->questiontext . ": " . $answer->answertext;
- $r->credit = 1;
- $answers[$question->id][$subqid] = array($ans->id => $r);
- }
- }
- }
- }
- return $answers[$question->id];
- }
/**
* @param object $question
@@ -337,7 +246,7 @@ class question_randomsamatch_qtype extends qtype_match {
* guess by a student might give or an empty string which means will not
* calculate.
*/
- function get_random_guess_score($question) {
+ public function get_random_guess_score($question) {
return 1/$question->options->choose;
}
}