|
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_match_edit_form extends question_edit_form { 00037 00038 protected function get_per_answer_fields($mform, $label, $gradeoptions, 00039 &$repeatedoptions, &$answersoption) { 00040 $repeated = array(); 00041 $repeated[] = $mform->createElement('header', 'answerhdr', $label); 00042 $repeated[] = $mform->createElement('editor', 'subquestions', 00043 get_string('question'), array('rows'=>3), $this->editoroptions); 00044 $repeated[] = $mform->createElement('text', 'subanswers', 00045 get_string('answer', 'question'), array('size'=>50)); 00046 $repeatedoptions['subquestions']['type'] = PARAM_RAW; 00047 $repeatedoptions['subanswers']['type'] = PARAM_TEXT; 00048 $answersoption = 'subquestions'; 00049 return $repeated; 00050 } 00051 00057 protected function definition_inner($mform) { 00058 $mform->addElement('advcheckbox', 'shuffleanswers', 00059 get_string('shuffle', 'qtype_match'), null, null, array(0, 1)); 00060 $mform->addHelpButton('shuffleanswers', 'shuffle', 'qtype_match'); 00061 $mform->setDefault('shuffleanswers', 1); 00062 00063 $mform->addElement('static', 'answersinstruct', 00064 get_string('availablechoices', 'qtype_match'), 00065 get_string('filloutthreeqsandtwoas', 'qtype_match')); 00066 $mform->closeHeaderBefore('answersinstruct'); 00067 00068 $this->add_per_answer_fields($mform, get_string('questionno', 'question', '{no}'), 0); 00069 00070 $this->add_combined_feedback_fields(true); 00071 $this->add_interactive_settings(true, true); 00072 } 00073 00074 protected function data_preprocessing($question) { 00075 $question = parent::data_preprocessing($question); 00076 $question = $this->data_preprocessing_combined_feedback($question, true); 00077 $question = $this->data_preprocessing_hints($question, true, true); 00078 00079 if (empty($question->options)) { 00080 return $question; 00081 } 00082 00083 $question->shuffleanswers = $question->options->shuffleanswers; 00084 00085 $key = 0; 00086 foreach ($question->options->subquestions as $subquestion) { 00087 $question->subanswers[$key] = $subquestion->answertext; 00088 00089 $draftid = file_get_submitted_draft_itemid('subquestions[' . $key . ']'); 00090 $question->subquestions[$key] = array(); 00091 $question->subquestions[$key]['text'] = file_prepare_draft_area( 00092 $draftid, // draftid 00093 $this->context->id, // context 00094 'qtype_match', // component 00095 'subquestion', // filarea 00096 !empty($subquestion->id) ? (int) $subquestion->id : null, // itemid 00097 $this->fileoptions, // options 00098 $subquestion->questiontext // text 00099 ); 00100 $question->subquestions[$key]['format'] = $subquestion->questiontextformat; 00101 $question->subquestions[$key]['itemid'] = $draftid; 00102 $key++; 00103 } 00104 00105 return $question; 00106 } 00107 00108 public function validation($data, $files) { 00109 $errors = parent::validation($data, $files); 00110 $answers = $data['subanswers']; 00111 $questions = $data['subquestions']; 00112 $questioncount = 0; 00113 $answercount = 0; 00114 foreach ($questions as $key => $question) { 00115 $trimmedquestion = trim($question['text']); 00116 $trimmedanswer = trim($answers[$key]); 00117 if ($trimmedquestion != '') { 00118 $questioncount++; 00119 } 00120 if ($trimmedanswer != '' || $trimmedquestion != '') { 00121 $answercount++; 00122 } 00123 if ($trimmedquestion != '' && $trimmedanswer == '') { 00124 $errors['subanswers['.$key.']'] = 00125 get_string('nomatchinganswerforq', 'qtype_match', $trimmedquestion); 00126 } 00127 } 00128 $numberqanda = new stdClass(); 00129 $numberqanda->q = 2; 00130 $numberqanda->a = 3; 00131 if ($questioncount < 1) { 00132 $errors['subquestions[0]'] = 00133 get_string('notenoughqsandas', 'qtype_match', $numberqanda); 00134 } 00135 if ($questioncount < 2) { 00136 $errors['subquestions[1]'] = 00137 get_string('notenoughqsandas', 'qtype_match', $numberqanda); 00138 } 00139 if ($answercount < 3) { 00140 $errors['subanswers[2]'] = 00141 get_string('notenoughqsandas', 'qtype_match', $numberqanda); 00142 } 00143 return $errors; 00144 } 00145 00146 public function qtype() { 00147 return 'match'; 00148 } 00149 }