PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/question/type/multichoice/renderer.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 301 lines | 198 code | 46 blank | 57 comment | 22 complexity | b4b50043fd5621178dd990ead71637dd MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Multiple choice question renderer classes.
  18. *
  19. * @package qtype
  20. * @subpackage multichoice
  21. * @copyright 2009 The Open University
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. /**
  26. * Base class for generating the bits of output common to multiple choice
  27. * single and multiple questions.
  28. *
  29. * @copyright 2009 The Open University
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. abstract class qtype_multichoice_renderer_base extends qtype_with_combined_feedback_renderer {
  33. protected abstract function get_input_type();
  34. protected abstract function get_input_name(question_attempt $qa, $value);
  35. protected abstract function get_input_value($value);
  36. protected abstract function get_input_id(question_attempt $qa, $value);
  37. /**
  38. * Whether a choice should be considered right, wrong or partially right.
  39. * @param question_answer $ans representing one of the choices.
  40. * @return fload 1.0, 0.0 or something in between, respectively.
  41. */
  42. protected abstract function is_right(question_answer $ans);
  43. protected abstract function prompt();
  44. public function formulation_and_controls(question_attempt $qa,
  45. question_display_options $options) {
  46. $question = $qa->get_question();
  47. $response = $question->get_response($qa);
  48. $inputname = $qa->get_qt_field_name('answer');
  49. $inputattributes = array(
  50. 'type' => $this->get_input_type(),
  51. 'name' => $inputname,
  52. );
  53. if ($options->readonly) {
  54. $inputattributes['disabled'] = 'disabled';
  55. }
  56. $radiobuttons = array();
  57. $feedbackimg = array();
  58. $feedback = array();
  59. $classes = array();
  60. foreach ($question->get_order($qa) as $value => $ansid) {
  61. $ans = $question->answers[$ansid];
  62. $inputattributes['name'] = $this->get_input_name($qa, $value);
  63. $inputattributes['value'] = $this->get_input_value($value);
  64. $inputattributes['id'] = $this->get_input_id($qa, $value);
  65. $isselected = $question->is_choice_selected($response, $value);
  66. if ($isselected) {
  67. $inputattributes['checked'] = 'checked';
  68. } else {
  69. unset($inputattributes['checked']);
  70. }
  71. $hidden = '';
  72. if (!$options->readonly && $this->get_input_type() == 'checkbox') {
  73. $hidden = html_writer::empty_tag('input', array(
  74. 'type' => 'hidden',
  75. 'name' => $inputattributes['name'],
  76. 'value' => 0,
  77. ));
  78. }
  79. $radiobuttons[] = $hidden . html_writer::empty_tag('input', $inputattributes) .
  80. html_writer::tag('label',
  81. $this->number_in_style($value, $question->answernumbering) .
  82. $question->make_html_inline($question->format_text(
  83. $ans->answer, $ans->answerformat,
  84. $qa, 'question', 'answer', $ansid)),
  85. array('for' => $inputattributes['id']));
  86. // Param $options->suppresschoicefeedback is a hack specific to the
  87. // oumultiresponse question type. It would be good to refactor to
  88. // avoid refering to it here.
  89. if ($options->feedback && empty($options->suppresschoicefeedback) &&
  90. $isselected && trim($ans->feedback)) {
  91. $feedback[] = html_writer::tag('div',
  92. $question->make_html_inline($question->format_text(
  93. $ans->feedback, $ans->feedbackformat,
  94. $qa, 'question', 'answerfeedback', $ansid)),
  95. array('class' => 'specificfeedback'));
  96. } else {
  97. $feedback[] = '';
  98. }
  99. $class = 'r' . ($value % 2);
  100. if ($options->correctness && $isselected) {
  101. $feedbackimg[] = $this->feedback_image($this->is_right($ans));
  102. $class .= ' ' . $this->feedback_class($this->is_right($ans));
  103. } else {
  104. $feedbackimg[] = '';
  105. }
  106. $classes[] = $class;
  107. }
  108. $result = '';
  109. $result .= html_writer::tag('div', $question->format_questiontext($qa),
  110. array('class' => 'qtext'));
  111. $result .= html_writer::start_tag('div', array('class' => 'ablock'));
  112. $result .= html_writer::tag('div', $this->prompt(), array('class' => 'prompt'));
  113. $result .= html_writer::start_tag('div', array('class' => 'answer'));
  114. foreach ($radiobuttons as $key => $radio) {
  115. $result .= html_writer::tag('div', $radio . ' ' . $feedbackimg[$key] . $feedback[$key],
  116. array('class' => $classes[$key])) . "\n";
  117. }
  118. $result .= html_writer::end_tag('div'); // Answer.
  119. $result .= html_writer::end_tag('div'); // Ablock.
  120. if ($qa->get_state() == question_state::$invalid) {
  121. $result .= html_writer::nonempty_tag('div',
  122. $question->get_validation_error($qa->get_last_qt_data()),
  123. array('class' => 'validationerror'));
  124. }
  125. return $result;
  126. }
  127. protected function number_html($qnum) {
  128. return $qnum . '. ';
  129. }
  130. /**
  131. * @param int $num The number, starting at 0.
  132. * @param string $style The style to render the number in. One of the
  133. * options returned by {@link qtype_multichoice:;get_numbering_styles()}.
  134. * @return string the number $num in the requested style.
  135. */
  136. protected function number_in_style($num, $style) {
  137. switch($style) {
  138. case 'abc':
  139. $number = chr(ord('a') + $num);
  140. break;
  141. case 'ABCD':
  142. $number = chr(ord('A') + $num);
  143. break;
  144. case '123':
  145. $number = $num + 1;
  146. break;
  147. case 'iii':
  148. $number = question_utils::int_to_roman($num + 1);
  149. break;
  150. case 'IIII':
  151. $number = strtoupper(question_utils::int_to_roman($num + 1));
  152. break;
  153. case 'none':
  154. return '';
  155. default:
  156. return 'ERR';
  157. }
  158. return $this->number_html($number);
  159. }
  160. public function specific_feedback(question_attempt $qa) {
  161. return $this->combined_feedback($qa);
  162. }
  163. }
  164. /**
  165. * Subclass for generating the bits of output specific to multiple choice
  166. * single questions.
  167. *
  168. * @copyright 2009 The Open University
  169. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  170. */
  171. class qtype_multichoice_single_renderer extends qtype_multichoice_renderer_base {
  172. protected function get_input_type() {
  173. return 'radio';
  174. }
  175. protected function get_input_name(question_attempt $qa, $value) {
  176. return $qa->get_qt_field_name('answer');
  177. }
  178. protected function get_input_value($value) {
  179. return $value;
  180. }
  181. protected function get_input_id(question_attempt $qa, $value) {
  182. return $qa->get_qt_field_name('answer' . $value);
  183. }
  184. protected function is_right(question_answer $ans) {
  185. return $ans->fraction;
  186. }
  187. protected function prompt() {
  188. return get_string('selectone', 'qtype_multichoice');
  189. }
  190. public function correct_response(question_attempt $qa) {
  191. $question = $qa->get_question();
  192. foreach ($question->answers as $ansid => $ans) {
  193. if (question_state::graded_state_for_fraction($ans->fraction) ==
  194. question_state::$gradedright) {
  195. return get_string('correctansweris', 'qtype_multichoice',
  196. $question->format_text($ans->answer, $ans->answerformat,
  197. $qa, 'question', 'answer', $ansid));
  198. }
  199. }
  200. return '';
  201. }
  202. }
  203. /**
  204. * Subclass for generating the bits of output specific to multiple choice
  205. * multi=select questions.
  206. *
  207. * @copyright 2009 The Open University
  208. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  209. */
  210. class qtype_multichoice_multi_renderer extends qtype_multichoice_renderer_base {
  211. protected function get_input_type() {
  212. return 'checkbox';
  213. }
  214. protected function get_input_name(question_attempt $qa, $value) {
  215. return $qa->get_qt_field_name('choice' . $value);
  216. }
  217. protected function get_input_value($value) {
  218. return 1;
  219. }
  220. protected function get_input_id(question_attempt $qa, $value) {
  221. return $this->get_input_name($qa, $value);
  222. }
  223. protected function is_right(question_answer $ans) {
  224. if ($ans->fraction > 0) {
  225. return 1;
  226. } else {
  227. return 0;
  228. }
  229. }
  230. protected function prompt() {
  231. return get_string('selectmulti', 'qtype_multichoice');
  232. }
  233. public function correct_response(question_attempt $qa) {
  234. $question = $qa->get_question();
  235. $right = array();
  236. foreach ($question->answers as $ansid => $ans) {
  237. if ($ans->fraction > 0) {
  238. $right[] = $question->format_text($ans->answer, $ans->answerformat,
  239. $qa, 'question', 'answer', $ansid);
  240. }
  241. }
  242. if (!empty($right)) {
  243. return get_string('correctansweris', 'qtype_multichoice',
  244. implode(', ', $right));
  245. }
  246. return '';
  247. }
  248. protected function num_parts_correct(question_attempt $qa) {
  249. if ($qa->get_question()->get_num_selected_choices($qa->get_last_qt_data()) >
  250. $qa->get_question()->get_num_correct_choices()) {
  251. return get_string('toomanyselected', 'qtype_multichoice');
  252. }
  253. return parent::num_parts_correct($qa);
  254. }
  255. }