Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/question/type/numerical/renderer.php
Go to the documentation of this file.
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 
00017 
00033 class qtype_numerical_renderer extends qtype_renderer {
00034     public function formulation_and_controls(question_attempt $qa,
00035             question_display_options $options) {
00036 
00037         $question = $qa->get_question();
00038         $currentanswer = $qa->get_last_qt_var('answer');
00039         if ($question->has_separate_unit_field()) {
00040             $selectedunit = $qa->get_last_qt_var('unit');
00041         } else {
00042             $selectedunit = null;
00043         }
00044 
00045         $inputname = $qa->get_qt_field_name('answer');
00046         $inputattributes = array(
00047             'type' => 'text',
00048             'name' => $inputname,
00049             'value' => $currentanswer,
00050             'id' => $inputname,
00051             'size' => 80,
00052         );
00053 
00054         if ($options->readonly) {
00055             $inputattributes['readonly'] = 'readonly';
00056         }
00057 
00058         $feedbackimg = '';
00059         if ($options->correctness) {
00060             list($value, $unit, $multiplier) = $question->ap->apply_units(
00061                     $currentanswer, $selectedunit);
00062             $answer = $question->get_matching_answer($value, $multiplier);
00063             if ($answer) {
00064                 $fraction = $question->apply_unit_penalty($answer->fraction, $answer->unitisright);
00065             } else {
00066                 $fraction = 0;
00067             }
00068             $inputattributes['class'] = $this->feedback_class($fraction);
00069             $feedbackimg = $this->feedback_image($fraction);
00070         }
00071 
00072         $questiontext = $question->format_questiontext($qa);
00073         $placeholder = false;
00074         if (preg_match('/_____+/', $questiontext, $matches)) {
00075             $placeholder = $matches[0];
00076             $inputattributes['size'] = round(strlen($placeholder) * 1.1);
00077         }
00078 
00079         $input = html_writer::empty_tag('input', $inputattributes) . $feedbackimg;
00080 
00081         if ($question->has_separate_unit_field()) {
00082             if ($question->unitdisplay == qtype_numerical::UNITRADIO) {
00083                 $choices = array();
00084                 $i = 1;
00085                 foreach ($question->ap->get_unit_options() as $unit) {
00086                     $id = $qa->get_qt_field_name('unit') . '_' . $i++;
00087                     $radioattrs = array('type' => 'radio', 'id' => $id, 'value' => $unit,
00088                             'name' => $qa->get_qt_field_name('unit'));
00089                     if ($unit == $selectedunit) {
00090                         $radioattrs['checked'] = 'checked';
00091                     }
00092                     $choices[] = html_writer::tag('label',
00093                             html_writer::empty_tag('input', $radioattrs) . $unit,
00094                             array('for' => $id, 'class' => 'unitchoice'));
00095                 }
00096 
00097                 $unitchoice = html_writer::tag('span', implode(' ', $choices),
00098                         array('class' => 'unitchoices'));
00099 
00100             } else if ($question->unitdisplay == qtype_numerical::UNITSELECT) {
00101                 $unitchoice = html_writer::select($question->ap->get_unit_options(),
00102                         $qa->get_qt_field_name('unit'), $selectedunit, array(''=>'choosedots'),
00103                         array('disabled' => $options->readonly));
00104             }
00105 
00106             if ($question->ap->are_units_before()) {
00107                 $input = $unitchoice . ' ' . $input;
00108             } else {
00109                 $input = $input . ' ' . $unitchoice;
00110             }
00111         }
00112 
00113         if ($placeholder) {
00114             $questiontext = substr_replace($questiontext, $input,
00115                     strpos($questiontext, $placeholder), strlen($placeholder));
00116         }
00117 
00118         $result = html_writer::tag('div', $questiontext, array('class' => 'qtext'));
00119 
00120         if (!$placeholder) {
00121             $result .= html_writer::start_tag('div', array('class' => 'ablock'));
00122             $result .= get_string('answer', 'qtype_shortanswer',
00123                     html_writer::tag('div', $input, array('class' => 'answer')));
00124             $result .= html_writer::end_tag('div');
00125         }
00126 
00127         if ($qa->get_state() == question_state::$invalid) {
00128             $result .= html_writer::nonempty_tag('div',
00129                     $question->get_validation_error(array('answer' => $currentanswer)),
00130                     array('class' => 'validationerror'));
00131         }
00132 
00133         return $result;
00134     }
00135 
00136     public function specific_feedback(question_attempt $qa) {
00137         $question = $qa->get_question();
00138 
00139         if ($question->has_separate_unit_field()) {
00140             $selectedunit = $qa->get_last_qt_var('unit');
00141         } else {
00142             $selectedunit = null;
00143         }
00144         list($value, $unit, $multiplier) = $question->ap->apply_units(
00145                 $qa->get_last_qt_var('answer'), $selectedunit);
00146         $answer = $question->get_matching_answer($value, $multiplier);
00147 
00148         if ($answer && $answer->feedback) {
00149             $feedback = $question->format_text($answer->feedback, $answer->feedbackformat,
00150                     $qa, 'question', 'answerfeedback', $answer->id);
00151         } else {
00152             $feedback = '';
00153         }
00154 
00155         if ($question->unitgradingtype && !$question->ap->is_known_unit($unit)) {
00156             $feedback .= html_writer::tag('p', get_string('unitincorrect', 'qtype_numerical'));
00157         }
00158 
00159         return $feedback;
00160     }
00161 
00162     public function correct_response(question_attempt $qa) {
00163         $question = $qa->get_question();
00164         $answer = $question->get_correct_answer();
00165         if (!$answer) {
00166             return '';
00167         }
00168 
00169         $response = str_replace('.', $question->ap->get_point(), $answer->answer);
00170         if ($question->unitdisplay != qtype_numerical::UNITNONE) {
00171             $response = $question->ap->add_unit($response);
00172         }
00173 
00174         return get_string('correctansweris', 'qtype_shortanswer', $response);
00175     }
00176 }
 All Data Structures Namespaces Files Functions Variables Enumerations