rendererbase.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. * Defines the renderer base class for question behaviours.
  18. *
  19. * @package moodlecore
  20. * @subpackage questionbehaviours
  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. * Renderer base class for question behaviours.
  27. *
  28. * The methods in this class are mostly called from {@link core_question_renderer}
  29. * which coordinates the overall output of questions.
  30. *
  31. * @copyright 2009 The Open University
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. */
  34. abstract class qbehaviour_renderer extends plugin_renderer_base {
  35. /**
  36. * Generate some HTML (which may be blank) that appears in the question
  37. * formulation area, afer the question type generated output.
  38. *
  39. * For example.
  40. * immediatefeedback and interactive mode use this to show the Submit button,
  41. * and CBM use this to display the certainty choices.
  42. *
  43. * @param question_attempt $qa a question attempt.
  44. * @param question_display_options $options controls what should and should not be displayed.
  45. * @return string HTML fragment.
  46. */
  47. public function controls(question_attempt $qa, question_display_options $options) {
  48. return '';
  49. }
  50. /**
  51. * Generate some HTML (which may be blank) that appears in the outcome area,
  52. * after the question-type generated output.
  53. *
  54. * For example, the CBM models use this to display an explanation of the score
  55. * adjustment that was made based on the certainty selected.
  56. *
  57. * @param question_attempt $qa a question attempt.
  58. * @param question_display_options $options controls what should and should not be displayed.
  59. * @return string HTML fragment.
  60. */
  61. public function feedback(question_attempt $qa, question_display_options $options) {
  62. return '';
  63. }
  64. public function manual_comment_fields(question_attempt $qa, question_display_options $options) {
  65. $inputname = $qa->get_behaviour_field_name('comment');
  66. $id = $inputname . '_id';
  67. list($commenttext, $commentformat) = $qa->get_current_manual_comment();
  68. $editor = editors_get_preferred_editor($commentformat);
  69. $strformats = format_text_menu();
  70. $formats = $editor->get_supported_formats();
  71. foreach ($formats as $fid) {
  72. $formats[$fid] = $strformats[$fid];
  73. }
  74. $commenttext = format_text($commenttext, $commentformat, array('para' => false));
  75. $editor->set_text($commenttext);
  76. $editor->use_editor($id, array('context' => $options->context));
  77. $commenteditor = html_writer::tag('div', html_writer::tag('textarea', s($commenttext),
  78. array('id' => $id, 'name' => $inputname, 'rows' => 10, 'cols' => 60)));
  79. $editorformat = '';
  80. if (count($formats) == 1) {
  81. reset($formats);
  82. $editorformat .= html_writer::empty_tag('input', array('type' => 'hidden',
  83. 'name' => $inputname . 'format', 'value' => key($formats)));
  84. } else {
  85. $editorformat = html_writer::start_tag('div', array('class' => 'fitem'));
  86. $editorformat .= html_writer::start_tag('div', array('class' => 'fitemtitle'));
  87. $editorformat .= html_writer::tag('label', get_string('format'), array('for'=>'menu'.$inputname.'format'));
  88. $editorformat .= html_writer::end_tag('div');
  89. $editorformat .= html_writer::start_tag('div', array('class' => 'felement fhtmleditor'));
  90. $editorformat .= html_writer::select($formats, $inputname.'format', $commentformat, '');
  91. $editorformat .= html_writer::end_tag('div');
  92. $editorformat .= html_writer::end_tag('div');
  93. }
  94. $comment = html_writer::tag('div', html_writer::tag('div',
  95. html_writer::tag('label', get_string('comment', 'question'),
  96. array('for' => $id)), array('class' => 'fitemtitle')) .
  97. html_writer::tag('div', $commenteditor, array('class' => 'felement fhtmleditor')),
  98. array('class' => 'fitem'));
  99. $comment .= $editorformat;
  100. $mark = '';
  101. if ($qa->get_max_mark()) {
  102. $currentmark = $qa->get_current_manual_mark();
  103. $maxmark = $qa->get_max_mark();
  104. $fieldsize = strlen($qa->format_max_mark($options->markdp)) - 1;
  105. $markfield = $qa->get_behaviour_field_name('mark');
  106. $attributes = array(
  107. 'type' => 'text',
  108. 'size' => $fieldsize,
  109. 'name' => $markfield,
  110. 'id'=> $markfield
  111. );
  112. if (!is_null($currentmark)) {
  113. $attributes['value'] = $currentmark;
  114. }
  115. $a = new stdClass();
  116. $a->max = $qa->format_max_mark($options->markdp);
  117. $a->mark = html_writer::empty_tag('input', $attributes);
  118. $markrange = html_writer::empty_tag('input', array(
  119. 'type' => 'hidden',
  120. 'name' => $qa->get_behaviour_field_name('maxmark'),
  121. 'value' => $maxmark,
  122. )) . html_writer::empty_tag('input', array(
  123. 'type' => 'hidden',
  124. 'name' => $qa->get_control_field_name('minfraction'),
  125. 'value' => $qa->get_min_fraction(),
  126. )) . html_writer::empty_tag('input', array(
  127. 'type' => 'hidden',
  128. 'name' => $qa->get_control_field_name('maxfraction'),
  129. 'value' => $qa->get_max_fraction(),
  130. ));
  131. $error = $qa->validate_manual_mark($currentmark);
  132. $errorclass = '';
  133. if ($error !== '') {
  134. $erroclass = ' error';
  135. $error = html_writer::tag('span', $error,
  136. array('class' => 'error')) . html_writer::empty_tag('br');
  137. }
  138. $mark = html_writer::tag('div', html_writer::tag('div',
  139. html_writer::tag('label', get_string('mark', 'question'),
  140. array('for' => $markfield)),
  141. array('class' => 'fitemtitle')) .
  142. html_writer::tag('div', $error . get_string('xoutofmax', 'question', $a) .
  143. $markrange, array('class' => 'felement ftext' . $errorclass)
  144. ), array('class' => 'fitem'));
  145. }
  146. return html_writer::tag('fieldset', html_writer::tag('div', $comment . $mark,
  147. array('class' => 'fcontainer clearfix')), array('class' => 'hidden'));
  148. }
  149. public function manual_comment_view(question_attempt $qa, question_display_options $options) {
  150. $output = '';
  151. if ($qa->has_manual_comment()) {
  152. $output .= get_string('commentx', 'question', $qa->get_behaviour()->format_comment());
  153. }
  154. if ($options->manualcommentlink) {
  155. $url = new moodle_url($options->manualcommentlink, array('slot' => $qa->get_slot()));
  156. $link = $this->output->action_link($url, get_string('commentormark', 'question'),
  157. new popup_action('click', $url, 'commentquestion',
  158. array('width' => 600, 'height' => 800)));
  159. $output .= html_writer::tag('div', $link, array('class' => 'commentlink'));
  160. }
  161. return $output;
  162. }
  163. /**
  164. * Display the manual comment, and a link to edit it, if appropriate.
  165. *
  166. * @param question_attempt $qa a question attempt.
  167. * @param question_display_options $options controls what should and should not be displayed.
  168. * @return string HTML fragment.
  169. */
  170. public function manual_comment(question_attempt $qa, question_display_options $options) {
  171. if ($options->manualcomment == question_display_options::EDITABLE) {
  172. return $this->manual_comment_fields($qa, $options);
  173. } else if ($options->manualcomment == question_display_options::VISIBLE) {
  174. return $this->manual_comment_view($qa, $options);
  175. } else {
  176. return '';
  177. }
  178. }
  179. /**
  180. * Several behaviours need a submit button, so put the common code here.
  181. * The button is disabled if the question is displayed read-only.
  182. * @param question_display_options $options controls what should and should not be displayed.
  183. * @return string HTML fragment.
  184. */
  185. protected function submit_button(question_attempt $qa, question_display_options $options) {
  186. if (!$qa->get_state()->is_active()) {
  187. return '';
  188. }
  189. $attributes = array(
  190. 'type' => 'submit',
  191. 'id' => $qa->get_behaviour_field_name('submit'),
  192. 'name' => $qa->get_behaviour_field_name('submit'),
  193. 'value' => get_string('check', 'question'),
  194. 'class' => 'submit btn',
  195. );
  196. if ($options->readonly) {
  197. $attributes['disabled'] = 'disabled';
  198. }
  199. $output = html_writer::empty_tag('input', $attributes);
  200. if (!$options->readonly) {
  201. $this->page->requires->js_init_call('M.core_question_engine.init_submit_button',
  202. array($attributes['id'], $qa->get_slot()));
  203. }
  204. return $output;
  205. }
  206. /**
  207. * Return any HTML that needs to be included in the page's <head> when
  208. * questions using this model are used.
  209. * @param $qa the question attempt that will be displayed on the page.
  210. * @return string HTML fragment.
  211. */
  212. public function head_code(question_attempt $qa) {
  213. return '';
  214. }
  215. /**
  216. * Generate the display of the marks for this question.
  217. * @param question_attempt $qa the question attempt to display.
  218. * @param core_question_renderer $qoutput the renderer for standard parts of questions.
  219. * @param question_display_options $options controls what should and should not be displayed.
  220. * @return HTML fragment.
  221. */
  222. public function mark_summary(question_attempt $qa, core_question_renderer $qoutput,
  223. question_display_options $options) {
  224. return $qoutput->standard_mark_summary($qa, $this, $options);
  225. }
  226. /**
  227. * Generate the display of the available marks for this question.
  228. * @param question_attempt $qa the question attempt to display.
  229. * @param core_question_renderer $qoutput the renderer for standard parts of questions.
  230. * @param question_display_options $options controls what should and should not be displayed.
  231. * @return HTML fragment.
  232. */
  233. public function marked_out_of_max(question_attempt $qa, core_question_renderer $qoutput,
  234. question_display_options $options) {
  235. return $qoutput->standard_marked_out_of_max($qa, $options);
  236. }
  237. /**
  238. * Generate the display of the marks for this question out of the available marks.
  239. * @param question_attempt $qa the question attempt to display.
  240. * @param core_question_renderer $qoutput the renderer for standard parts of questions.
  241. * @param question_display_options $options controls what should and should not be displayed.
  242. * @return HTML fragment.
  243. */
  244. public function mark_out_of_max(question_attempt $qa, core_question_renderer $qoutput,
  245. question_display_options $options) {
  246. return $qoutput->standard_mark_out_of_max($qa, $options);
  247. }
  248. }