|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 00036 class qtype_multichoice_edit_form extends question_edit_form { 00042 protected function definition_inner($mform) { 00043 $menu = array( 00044 get_string('answersingleno', 'qtype_multichoice'), 00045 get_string('answersingleyes', 'qtype_multichoice'), 00046 ); 00047 $mform->addElement('select', 'single', 00048 get_string('answerhowmany', 'qtype_multichoice'), $menu); 00049 $mform->setDefault('single', 1); 00050 00051 $mform->addElement('advcheckbox', 'shuffleanswers', 00052 get_string('shuffleanswers', 'qtype_multichoice'), null, null, array(0, 1)); 00053 $mform->addHelpButton('shuffleanswers', 'shuffleanswers', 'qtype_multichoice'); 00054 $mform->setDefault('shuffleanswers', 1); 00055 00056 $mform->addElement('select', 'answernumbering', 00057 get_string('answernumbering', 'qtype_multichoice'), 00058 qtype_multichoice::get_numbering_styles()); 00059 $mform->setDefault('answernumbering', 'abc'); 00060 00061 $this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'), 00062 question_bank::fraction_options_full(), max(5, QUESTION_NUMANS_START)); 00063 00064 $this->add_combined_feedback_fields(true); 00065 $mform->disabledIf('shownumcorrect', 'single', 'eq', 1); 00066 00067 $this->add_interactive_settings(true, true); 00068 } 00069 00070 protected function get_per_answer_fields($mform, $label, $gradeoptions, 00071 &$repeatedoptions, &$answersoption) { 00072 $repeated = array(); 00073 $repeated[] = $mform->createElement('header', 'answerhdr', $label); 00074 $repeated[] = $mform->createElement('editor', 'answer', 00075 get_string('answer', 'question'), array('rows' => 1), $this->editoroptions); 00076 $repeated[] = $mform->createElement('select', 'fraction', 00077 get_string('grade'), $gradeoptions); 00078 $repeated[] = $mform->createElement('editor', 'feedback', 00079 get_string('feedback', 'question'), array('rows' => 1), $this->editoroptions); 00080 $repeatedoptions['answer']['type'] = PARAM_RAW; 00081 $repeatedoptions['fraction']['default'] = 0; 00082 $answersoption = 'answers'; 00083 return $repeated; 00084 } 00085 00086 protected function data_preprocessing($question) { 00087 $question = parent::data_preprocessing($question); 00088 $question = $this->data_preprocessing_answers($question, true); 00089 $question = $this->data_preprocessing_combined_feedback($question, true); 00090 $question = $this->data_preprocessing_hints($question, true, true); 00091 00092 if (!empty($question->options)) { 00093 $question->single = $question->options->single; 00094 $question->shuffleanswers = $question->options->shuffleanswers; 00095 $question->answernumbering = $question->options->answernumbering; 00096 } 00097 00098 return $question; 00099 } 00100 00101 public function validation($data, $files) { 00102 $errors = parent::validation($data, $files); 00103 $answers = $data['answer']; 00104 $answercount = 0; 00105 00106 $totalfraction = 0; 00107 $maxfraction = -1; 00108 00109 foreach ($answers as $key => $answer) { 00110 //check no of choices 00111 $trimmedanswer = trim($answer['text']); 00112 $fraction = (float) $data['fraction'][$key]; 00113 if (empty($trimmedanswer) && empty($fraction)) { 00114 continue; 00115 } 00116 if (empty($trimmedanswer)) { 00117 $errors['fraction['.$key.']'] = get_string('errgradesetanswerblank', 'qtype_multichoice'); 00118 } 00119 00120 $answercount++; 00121 00122 //check grades 00123 if ($data['fraction'][$key] > 0) { 00124 $totalfraction += $data['fraction'][$key]; 00125 } 00126 if ($data['fraction'][$key] > $maxfraction) { 00127 $maxfraction = $data['fraction'][$key]; 00128 } 00129 } 00130 00131 if ($answercount == 0) { 00132 $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2); 00133 $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2); 00134 } else if ($answercount == 1) { 00135 $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2); 00136 00137 } 00138 00140 if ($data['single']) { 00141 if ($maxfraction != 1) { 00142 $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', 00143 $maxfraction * 100); 00144 } 00145 } else { 00146 $totalfraction = round($totalfraction, 2); 00147 if ($totalfraction != 1) { 00148 $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', 00149 $totalfraction * 100); 00150 } 00151 } 00152 return $errors; 00153 } 00154 00155 public function qtype() { 00156 return 'multichoice'; 00157 } 00158 }