|
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_truefalse_renderer extends qtype_renderer { 00037 public function formulation_and_controls(question_attempt $qa, 00038 question_display_options $options) { 00039 00040 $question = $qa->get_question(); 00041 $response = $qa->get_last_qt_var('answer', ''); 00042 00043 $inputname = $qa->get_qt_field_name('answer'); 00044 $trueattributes = array( 00045 'type' => 'radio', 00046 'name' => $inputname, 00047 'value' => 1, 00048 'id' => $inputname . 'true', 00049 ); 00050 $falseattributes = array( 00051 'type' => 'radio', 00052 'name' => $inputname, 00053 'value' => 0, 00054 'id' => $inputname . 'false', 00055 ); 00056 00057 if ($options->readonly) { 00058 $trueattributes['disabled'] = 'disabled'; 00059 $falseattributes['disabled'] = 'disabled'; 00060 } 00061 00062 // Work out which radio button to select (if any) 00063 $truechecked = false; 00064 $falsechecked = false; 00065 $responsearray = array(); 00066 if ($response) { 00067 $trueattributes['checked'] = 'checked'; 00068 $truechecked = true; 00069 $responsearray = array('answer' => 1); 00070 } else if ($response !== '') { 00071 $falseattributes['checked'] = 'checked'; 00072 $falsechecked = true; 00073 $responsearray = array('answer' => 1); 00074 } 00075 00076 // Work out visual feedback for answer correctness. 00077 $trueclass = ''; 00078 $falseclass = ''; 00079 $truefeedbackimg = ''; 00080 $falsefeedbackimg = ''; 00081 if ($options->correctness) { 00082 if ($truechecked) { 00083 $trueclass = ' ' . $this->feedback_class((int) $question->rightanswer); 00084 $truefeedbackimg = $this->feedback_image((int) $question->rightanswer); 00085 } else if ($falsechecked) { 00086 $falseclass = ' ' . $this->feedback_class((int) (!$question->rightanswer)); 00087 $falsefeedbackimg = $this->feedback_image((int) (!$question->rightanswer)); 00088 } 00089 } 00090 00091 $radiotrue = html_writer::empty_tag('input', $trueattributes) . 00092 html_writer::tag('label', get_string('true', 'qtype_truefalse'), 00093 array('for' => $trueattributes['id'])); 00094 $radiofalse = html_writer::empty_tag('input', $falseattributes) . 00095 html_writer::tag('label', get_string('false', 'qtype_truefalse'), 00096 array('for' => $falseattributes['id'])); 00097 00098 $result = ''; 00099 $result .= html_writer::tag('div', $question->format_questiontext($qa), 00100 array('class' => 'qtext')); 00101 00102 $result .= html_writer::start_tag('div', array('class' => 'ablock')); 00103 $result .= html_writer::tag('div', get_string('selectone', 'qtype_truefalse'), 00104 array('class' => 'prompt')); 00105 00106 $result .= html_writer::start_tag('div', array('class' => 'answer')); 00107 $result .= html_writer::tag('div', $radiotrue . ' ' . $truefeedbackimg, 00108 array('class' => 'r0' . $trueclass)); 00109 $result .= html_writer::tag('div', $radiofalse . ' ' . $falsefeedbackimg, 00110 array('class' => 'r1' . $falseclass)); 00111 $result .= html_writer::end_tag('div'); // answer 00112 00113 $result .= html_writer::end_tag('div'); // ablock 00114 00115 if ($qa->get_state() == question_state::$invalid) { 00116 $result .= html_writer::nonempty_tag('div', 00117 $question->get_validation_error($responsearray), 00118 array('class' => 'validationerror')); 00119 } 00120 00121 return $result; 00122 } 00123 00124 public function specific_feedback(question_attempt $qa) { 00125 $question = $qa->get_question(); 00126 $response = $qa->get_last_qt_var('answer', ''); 00127 00128 if ($response) { 00129 return $question->format_text($question->truefeedback, $question->truefeedbackformat, 00130 $qa, 'question', 'answerfeedback', $question->trueanswerid); 00131 } else if ($response !== '') { 00132 return $question->format_text($question->falsefeedback, $question->falsefeedbackformat, 00133 $qa, 'question', 'answerfeedback', $question->falseanswerid); 00134 } 00135 } 00136 00137 public function correct_response(question_attempt $qa) { 00138 $question = $qa->get_question(); 00139 00140 if ($question->rightanswer) { 00141 return get_string('correctanswertrue', 'qtype_truefalse'); 00142 } else { 00143 return get_string('correctanswerfalse', 'qtype_truefalse'); 00144 } 00145 } 00146 }