|
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_renderer extends qtype_with_combined_feedback_renderer { 00037 00038 public function formulation_and_controls(question_attempt $qa, 00039 question_display_options $options) { 00040 00041 $question = $qa->get_question(); 00042 $stemorder = $question->get_stem_order(); 00043 $response = $qa->get_last_qt_data(); 00044 00045 $choices = $this->format_choices($question); 00046 00047 $result = ''; 00048 $result .= html_writer::tag('div', $question->format_questiontext($qa), 00049 array('class' => 'qtext')); 00050 00051 $result .= html_writer::start_tag('div', array('class' => 'ablock')); 00052 $result .= html_writer::start_tag('table', array('class' => 'answer')); 00053 $result .= html_writer::start_tag('tbody'); 00054 00055 $parity = 0; 00056 foreach ($stemorder as $key => $stemid) { 00057 00058 $result .= html_writer::start_tag('tr', array('class' => 'r' . $parity)); 00059 $fieldname = 'sub' . $key; 00060 00061 $result .= html_writer::tag('td', $question->format_text( 00062 $question->stems[$stemid], $question->stemformat[$stemid], 00063 $qa, 'qtype_match', 'subquestion', $stemid), 00064 array('class' => 'text')); 00065 00066 $classes = 'control'; 00067 $feedbackimage = ''; 00068 00069 if (array_key_exists($fieldname, $response)) { 00070 $selected = $response[$fieldname]; 00071 } else { 00072 $selected = 0; 00073 } 00074 00075 $fraction = (int) ($selected && $selected == $question->get_right_choice_for($stemid)); 00076 00077 if ($options->correctness && $selected) { 00078 $classes .= ' ' . $this->feedback_class($fraction); 00079 $feedbackimage = $this->feedback_image($fraction); 00080 } 00081 00082 $result .= html_writer::tag('td', 00083 html_writer::select($choices, $qa->get_qt_field_name('sub' . $key), $selected, 00084 array('0' => 'choose'), array('disabled' => $options->readonly)) . 00085 ' ' . $feedbackimage, array('class' => $classes)); 00086 00087 $result .= html_writer::end_tag('tr'); 00088 $parity = 1 - $parity; 00089 } 00090 $result .= html_writer::end_tag('tbody'); 00091 $result .= html_writer::end_tag('table'); 00092 00093 $result .= html_writer::end_tag('div'); // ablock 00094 00095 if ($qa->get_state() == question_state::$invalid) { 00096 $result .= html_writer::nonempty_tag('div', 00097 $question->get_validation_error($response), 00098 array('class' => 'validationerror')); 00099 } 00100 00101 return $result; 00102 } 00103 00104 public function specific_feedback(question_attempt $qa) { 00105 return $this->combined_feedback($qa); 00106 } 00107 00108 protected function format_choices($question) { 00109 $choices = array(); 00110 foreach ($question->get_choice_order() as $key => $choiceid) { 00111 $choices[$key] = format_string($question->choices[$choiceid], true, 00112 array('context' => $question->contextid)); 00113 } 00114 return $choices; 00115 } 00116 00117 public function correct_response(question_attempt $qa) { 00118 $question = $qa->get_question(); 00119 $stemorder = $question->get_stem_order(); 00120 00121 $choices = $this->format_choices($question); 00122 $right = array(); 00123 foreach ($stemorder as $key => $stemid) { 00124 $right[] = $question->format_text($question->stems[$stemid], 00125 $question->stemformat[$stemid], $qa, 00126 'qtype_match', 'subquestion', $stemid) . ' – ' . 00127 $choices[$question->get_right_choice_for($stemid)]; 00128 } 00129 00130 if (!empty($right)) { 00131 return get_string('correctansweris', 'qtype_match', implode(', ', $right)); 00132 } 00133 } 00134 }