From 59a3fcd3ebcb1909c8d91393da43fe84b1eba3ae Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Fri, 20 May 2011 20:18:04 +0100 Subject: [PATCH] MDL-20636 Fix a lot more coding style issues. --- .../backup_qtype_multianswer_plugin.class.php | 3 +- ...restore_qtype_multianswer_plugin.class.php | 27 +- .../multianswer/edit_multianswer_form.php | 432 ++++++----- .../multianswer/lang/en/qtype_multianswer.php | 4 +- question/type/multianswer/questiontype.php | 711 ++++++++---------- ...ackup_qtype_randomsamatch_plugin.class.php | 5 +- ...store_qtype_randomsamatch_plugin.class.php | 8 +- .../randomsamatch/edit_randomsamatch_form.php | 28 +- question/type/randomsamatch/questiontype.php | 133 +--- 9 files changed, 601 insertions(+), 750 deletions(-) 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', '
'); $mform->addElement('html', '
'); - for ($sub =1;$sub <=$countsubquestions ;$sub++) { + for ($sub = 1; $sub <= $countsubquestions; $sub++) { $this->editas[$sub] = 'unknown type'; - if (isset( $this->questiondisplay->options->questions[$sub]->qtype) ) { - $this->editas[$sub] = $this->questiondisplay->options->questions[$sub]->qtype ; + if (isset($this->questiondisplay->options->questions[$sub]->qtype)) { + $this->editas[$sub] = $this->questiondisplay->options->questions[$sub]->qtype; } else if (optional_param('sub_'.$sub."_".'qtype', '', PARAM_RAW) != '') { $this->editas[$sub] = optional_param('sub_'.$sub."_".'qtype', '', PARAM_RAW); } $storemess = ''; - if(isset($this->savedquestiondisplay->options->questions[$sub]->qtype) && - $this->savedquestiondisplay->options->questions[$sub]->qtype != $this->questiondisplay->options->questions[$sub]->qtype ){ - $this->qtype_change = true ; - $storemess = " STORED QTYPE ".question_bank::get_qtype_name($this->savedquestiondisplay->options->questions[$sub]->qtype).""; - } + if (isset($this->savedquestiondisplay->options->questions[$sub]->qtype) && + $this->savedquestiondisplay->options->questions[$sub]->qtype != + $this->questiondisplay->options->questions[$sub]->qtype) { + $this->qtype_change = true; + $storemess = " STORED QTYPE " . + question_bank::get_qtype_name( + $this->savedquestiondisplay->options->questions[$sub]->qtype). + ""; + } $mform->addElement('header', 'subhdr'.$sub, get_string('questionno', 'question', - '{#'.$sub.'}').' '.question_bank::get_qtype_name($this->questiondisplay->options->questions[$sub]->qtype).$storemess); + '{#'.$sub.'}').' '.question_bank::get_qtype_name( + $this->questiondisplay->options->questions[$sub]->qtype).$storemess); - $mform->addElement('static', 'sub_'.$sub."_".'questiontext', get_string('questiondefinition','qtype_multianswer'),array('cols'=>60, 'rows'=>3)); + $mform->addElement('static', 'sub_'.$sub."_".'questiontext', + get_string('questiondefinition', 'qtype_multianswer'), + array('cols' => 60, 'rows' => 3)); - if (isset ( $this->questiondisplay->options->questions[$sub]->questiontext)) { - $mform->setDefault('sub_'.$sub."_".'questiontext', $this->questiondisplay->options->questions[$sub]->questiontext['text']); + if (isset ($this->questiondisplay->options->questions[$sub]->questiontext)) { + $mform->setDefault('sub_'.$sub."_".'questiontext', + $this->questiondisplay->options->questions[$sub]->questiontext['text']); } - $mform->addElement('static', 'sub_'.$sub."_".'defaultgrade', get_string('defaultgrade', 'question')); - $mform->setDefault('sub_'.$sub."_".'defaultgrade',$this->questiondisplay->options->questions[$sub]->defaultgrade); + $mform->addElement('static', 'sub_'.$sub."_".'defaultgrade', + get_string('defaultgrade', 'question')); + $mform->setDefault('sub_'.$sub."_".'defaultgrade', + $this->questiondisplay->options->questions[$sub]->defaultgrade); - if ($this->questiondisplay->options->questions[$sub]->qtype =='shortanswer' ) { - $mform->addElement('static', 'sub_'.$sub."_".'usecase', get_string('casesensitive', 'question')); + if ($this->questiondisplay->options->questions[$sub]->qtype == 'shortanswer') { + $mform->addElement('static', 'sub_'.$sub."_".'usecase', + get_string('casesensitive', 'question')); } - if ($this->questiondisplay->options->questions[$sub]->qtype =='multichoice' ) { - $mform->addElement('static', 'sub_'.$sub."_".'layout', get_string('layout', 'qtype_multianswer'),array('cols'=>60, 'rows'=>1)) ;//, $gradeoptions); + if ($this->questiondisplay->options->questions[$sub]->qtype == 'multichoice') { + $mform->addElement('static', 'sub_'.$sub."_".'layout', + get_string('layout', 'qtype_multianswer'), + array('cols' => 60, 'rows' => 1)); } - foreach ($this->questiondisplay->options->questions[$sub]->answer as $key =>$ans) { + foreach ($this->questiondisplay->options->questions[$sub]->answer as $key => $ans) { - $mform->addElement('static', 'sub_'.$sub."_".'answer['.$key.']', get_string('answer', 'question'), array('cols'=>60, 'rows'=>1)); + $mform->addElement('static', 'sub_'.$sub."_".'answer['.$key.']', + get_string('answer', 'question'), array('cols' => 60, 'rows' => 1)); - if ($this->questiondisplay->options->questions[$sub]->qtype =='numerical' && $key == 0 ) { - $mform->addElement('static', 'sub_'.$sub."_".'tolerance['.$key.']', get_string('acceptederror', 'quiz')) ;//, $gradeoptions); + if ($this->questiondisplay->options->questions[$sub]->qtype == 'numerical' && + $key == 0) { + $mform->addElement('static', 'sub_'.$sub."_".'tolerance['.$key.']', + get_string('acceptederror', 'quiz')); } - $mform->addElement('static', 'sub_'.$sub."_".'fraction['.$key.']', get_string('grade')) ;//, $gradeoptions); + $mform->addElement('static', 'sub_'.$sub."_".'fraction['.$key.']', + get_string('grade')); - $mform->addElement('static', 'sub_'.$sub."_".'feedback['.$key.']', get_string('feedback', 'question')); + $mform->addElement('static', 'sub_'.$sub."_".'feedback['.$key.']', + get_string('feedback', 'question')); } } $mform->addElement('html', '
'); - $this->negative_diff =$countsavedsubquestions - $countsubquestions ; - if ( ($this->negative_diff > 0 ) ||$this->qtype_change || ($this->used_in_quiz && $this->negative_diff != 0)){ - $mform->addElement('header', 'additemhdr', get_string('warningquestionmodified','qtype_multianswer')); + $this->negative_diff = $countsavedsubquestions - $countsubquestions; + if (($this->negative_diff > 0) ||$this->qtype_change || + ($this->used_in_quiz && $this->negative_diff != 0)) { + $mform->addElement('header', 'additemhdr', + get_string('warningquestionmodified', 'qtype_multianswer')); } - if($this->negative_diff > 0) { - $mform->addElement('static', 'alert1', "".get_string('questiondeleted','qtype_multianswer')."",get_string('questionsless','qtype_multianswer',$this->negative_diff)); + if ($this->negative_diff > 0) { + $mform->addElement('static', 'alert1', "". + get_string('questiondeleted', 'qtype_multianswer')."", + get_string('questionsless', 'qtype_multianswer', $this->negative_diff)); } - if($this->qtype_change ) { - $mform->addElement('static', 'alert1', "".get_string('questiontypechanged','qtype_multianswer')."",get_string('questiontypechangedcomment','qtype_multianswer')); + if ($this->qtype_change) { + $mform->addElement('static', 'alert1', "". + get_string('questiontypechanged', 'qtype_multianswer')."", + get_string('questiontypechangedcomment', 'qtype_multianswer')); } $mform->addElement('html', '
'); } - if( $this->used_in_quiz){ - if($this->negative_diff < 0) { + if ($this->used_in_quiz) { + if ($this->negative_diff < 0) { $diff = $countsubquestions - $countsavedsubquestions; - $mform->addElement('static', 'alert1', "".get_string('questionsadded','qtype_multianswer')."","".get_string('questionsmore','qtype_multianswer',$diff).""); + $mform->addElement('static', 'alert1', "". + get_string('questionsadded', 'qtype_multianswer')."", + "".get_string('questionsmore', 'qtype_multianswer', $diff). + ""); } - $a = new stdClass() ; + $a = new stdClass(); $a->nb_of_quiz = $this->nb_of_quiz; $a->nb_of_attempts = $this->nb_of_attempts; - $mform->addElement('header', 'additemhdr2', get_string('questionusedinquiz','qtype_multianswer',$a)); - $mform->addElement('static', 'alertas', get_string('youshouldnot','qtype_multianswer')); + $mform->addElement('header', 'additemhdr2', + get_string('questionusedinquiz', 'qtype_multianswer', $a)); + $mform->addElement('static', 'alertas', + get_string('youshouldnot', 'qtype_multianswer')); } - if ( ($this->negative_diff > 0 || $this->used_in_quiz && ($this->negative_diff > 0 ||$this->negative_diff < 0 || $this->qtype_change ) ) && $this->reload ){ - $mform->addElement('header', 'additemhdr', get_string('questionsaveasedited', 'qtype_multianswer')); - $mform->addElement('checkbox', 'confirm','' ,get_string('confirmquestionsaveasedited', 'qtype_multianswer')); + if (($this->negative_diff > 0 || $this->used_in_quiz && + ($this->negative_diff > 0 || $this->negative_diff < 0 || $this->qtype_change)) && + $this->reload) { + $mform->addElement('header', 'additemhdr', + get_string('questionsaveasedited', 'qtype_multianswer')); + $mform->addElement('checkbox', 'confirm', '', + get_string('confirmquestionsaveasedited', 'qtype_multianswer')); $mform->setDefault('confirm', 0); - }else { - $mform->addElement('hidden', 'confirm',0); + } else { + $mform->addElement('hidden', 'confirm', 0); } } - function set_data($question) { + public function set_data($question) { global $DB; - $default_values =array(); - if (isset($question->id) and $question->id and $question->qtype and $question->questiontext) { + $default_values = array(); + if (isset($question->id) and $question->id and $question->qtype && + $question->questiontext) { foreach ($question->options->questions as $key => $wrapped) { - if(!empty($wrapped)){ + if (!empty($wrapped)) { // The old way of restoring the definitions is kept to gradually // update all multianswer questions if (empty($wrapped->questiontext)) { $parsableanswerdef = '{' . $wrapped->defaultgrade . ':'; switch ($wrapped->qtype) { - case 'multichoice': - $parsableanswerdef .= 'MULTICHOICE:'; - break; - case 'shortanswer': - $parsableanswerdef .= 'SHORTANSWER:'; - break; - case 'numerical': - $parsableanswerdef .= 'NUMERICAL:'; - break; - default: - print_error('unknownquestiontype', 'question', '', $wrapped->qtype); + case 'multichoice': + $parsableanswerdef .= 'MULTICHOICE:'; + break; + case 'shortanswer': + $parsableanswerdef .= 'SHORTANSWER:'; + break; + case 'numerical': + $parsableanswerdef .= 'NUMERICAL:'; + break; + default: + print_error('unknownquestiontype', 'question', '', + $wrapped->qtype); } - $separator= ''; + $separator = ''; foreach ($wrapped->options->answers as $subanswer) { $parsableanswerdef .= $separator . '%' . round(100*$subanswer->fraction) . '%'; @@ -268,168 +304,192 @@ class question_edit_multianswer_form extends question_edit_form { } $parsableanswerdef .= '}'; // Fix the questiontext fields of old questions - $DB->set_field('question', 'questiontext', $parsableanswerdef, array('id' => $wrapped->id)); + $DB->set_field('question', 'questiontext', $parsableanswerdef, + array('id' => $wrapped->id)); } else { $parsableanswerdef = str_replace('&#', '&\#', $wrapped->questiontext); } - $question->questiontext = str_replace("{#$key}", $parsableanswerdef, $question->questiontext); + $question->questiontext = str_replace("{#$key}", $parsableanswerdef, + $question->questiontext); } } } // set default to $questiondisplay questions elements - if ( $this->reload ){ + if ($this->reload) { if (isset($this->questiondisplay->options->questions)) { - $subquestions = fullclone($this->questiondisplay->options->questions) ; + $subquestions = fullclone($this->questiondisplay->options->questions); if (count($subquestions)) { - $sub =1; + $sub = 1; foreach ($subquestions as $subquestion) { - $prefix = 'sub_'.$sub.'_' ; + $prefix = 'sub_'.$sub.'_'; // validate parameters $answercount = 0; $maxgrade = false; $maxfraction = -1; - if ($subquestion->qtype =='shortanswer' ) { + if ($subquestion->qtype == 'shortanswer') { switch ($subquestion->usecase) { - case '1': - $default_values[$prefix.'usecase']= get_string('caseyes', 'qtype_shortanswer'); - break; - case '0': - default : - $default_values[$prefix.'usecase']= get_string('caseno', 'qtype_shortanswer'); + case '1': + $default_values[$prefix.'usecase'] = + get_string('caseyes', 'qtype_shortanswer'); + break; + case '0': + default : + $default_values[$prefix.'usecase'] = + get_string('caseno', 'qtype_shortanswer'); } } - if ($subquestion->qtype == 'multichoice' ) { - $default_values[$prefix.'layout'] = $subquestion->layout ; + if ($subquestion->qtype == 'multichoice') { + $default_values[$prefix.'layout'] = $subquestion->layout; switch ($subquestion->layout) { - case '0': - $default_values[$prefix.'layout']= get_string('layoutselectinline', 'qtype_multianswer'); - break; - case '1': - $default_values[$prefix.'layout']= get_string('layoutvertical', 'qtype_multianswer'); - break; - case '2': - $default_values[$prefix.'layout']= get_string('layouthorizontal', 'qtype_multianswer'); - break; - default: - $default_values[$prefix.'layout']= get_string('layoutundefined', 'qtype_multianswer'); + case '0': + $default_values[$prefix.'layout'] = + get_string('layoutselectinline', 'qtype_multianswer'); + break; + case '1': + $default_values[$prefix.'layout'] = + get_string('layoutvertical', 'qtype_multianswer'); + break; + case '2': + $default_values[$prefix.'layout'] = + get_string('layouthorizontal', 'qtype_multianswer'); + break; + default: + $default_values[$prefix.'layout'] = + get_string('layoutundefined', 'qtype_multianswer'); } } - foreach ($subquestion->answer as $key=>$answer) { - if ( $subquestion->qtype == 'numerical' && $key == 0 ) { - $default_values[$prefix.'tolerance['.$key.']'] = $subquestion->tolerance[0] ; + foreach ($subquestion->answer as $key => $answer) { + if ($subquestion->qtype == 'numerical' && $key == 0) { + $default_values[$prefix.'tolerance['.$key.']'] = + $subquestion->tolerance[0]; } $trimmedanswer = trim($answer); if ($trimmedanswer !== '') { $answercount++; - if ($subquestion->qtype == 'numerical' && !(is_numeric($trimmedanswer) || $trimmedanswer == '*')) { - $this->_form->setElementError($prefix.'answer['.$key.']' , get_string('answermustbenumberorstar', 'qtype_numerical')); + if ($subquestion->qtype == 'numerical' && + !(is_numeric($trimmedanswer) || $trimmedanswer == '*')) { + $this->_form->setElementError($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]; } } - $default_values[$prefix.'answer['.$key.']'] = htmlspecialchars ($answer); + $default_values[$prefix.'answer['.$key.']'] = + htmlspecialchars($answer); } if ($answercount == 0) { - if ($subquestion->qtype == 'multichoice' ) { - $this->_form->setElementError($prefix.'answer[0]' , get_string('notenoughanswers', 'qtype_multichoice', 2)); + if ($subquestion->qtype == 'multichoice') { + $this->_form->setElementError($prefix.'answer[0]', + get_string('notenoughanswers', 'qtype_multichoice', 2)); } else { - $this->_form->setElementError($prefix.'answer[0]' , get_string('notenoughanswers', 'question', 1)); + $this->_form->setElementError($prefix.'answer[0]', + get_string('notenoughanswers', 'question', 1)); } } if ($maxgrade == false) { - $this->_form->setElementError($prefix.'fraction[0]' ,get_string('fractionsnomax', 'question')); + $this->_form->setElementError($prefix.'fraction[0]', + get_string('fractionsnomax', 'question')); } - foreach ($subquestion->feedback as $key=>$answer) { + foreach ($subquestion->feedback as $key => $answer) { - $default_values[$prefix.'feedback['.$key.']'] = htmlspecialchars ($answer['text']); + $default_values[$prefix.'feedback['.$key.']'] = + htmlspecialchars ($answer['text']); } - foreach ( $subquestion->fraction as $key=>$answer) { - $default_values[$prefix.'fraction['.$key.']'] = $answer; + foreach ($subquestion->fraction as $key => $answer) { + $default_values[$prefix.'fraction['.$key.']'] = $answer; } - $sub++; } } } } - $default_values['alertas']= "".get_string('questioninquiz','qtype_multianswer').""; + $default_values['alertas']= "".get_string('questioninquiz', 'qtype_multianswer'). + ""; - if( $default_values != "") { + if ($default_values != "") { $question = (object)((array)$question + $default_values); } parent::set_data($question); } - function validation($data, $files) { + public function validation($data, $files) { $errors = parent::validation($data, $files); $questiondisplay = qtype_multianswer_extract_question($data['questiontext']); -// echo "

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'] = '