renderer.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * Renderers for outputting parts of the question bank.
  18. *
  19. * @package moodlecore
  20. * @subpackage questionbank
  21. * @copyright 2011 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. * This renderer outputs parts of the question bank.
  27. *
  28. * @copyright 2011 The Open University
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. */
  31. class core_question_bank_renderer extends plugin_renderer_base {
  32. /**
  33. * Output the icon for a question type.
  34. *
  35. * @param string $qtype the question type.
  36. * @return string HTML fragment.
  37. */
  38. public function qtype_icon($qtype) {
  39. $qtype = question_bank::get_qtype($qtype, false);
  40. $namestr = $qtype->local_name();
  41. return $this->pix_icon('icon', $namestr, $qtype->plugin_name(), array('title' => $namestr));
  42. }
  43. /**
  44. * Build the HTML for the question chooser javascript popup.
  45. *
  46. * @param array $real A set of real question types
  47. * @param array $fake A set of fake question types
  48. * @param object $course The course that will be displayed
  49. * @param array $hiddenparams Any hidden parameters to add to the form
  50. * @return string The composed HTML for the questionbank chooser
  51. */
  52. public function qbank_chooser($real, $fake, $course, $hiddenparams) {
  53. global $OUTPUT;
  54. // Start the form content.
  55. $formcontent = html_writer::start_tag('form', array('action' => new moodle_url('/question/question.php'),
  56. 'id' => 'chooserform', 'method' => 'get'));
  57. // Add the hidden fields.
  58. $hiddenfields = '';
  59. $hiddenfields .= html_writer::tag('input', '', array('type' => 'hidden', 'name' => 'category', 'id' => 'qbankcategory'));
  60. $hiddenfields .= html_writer::tag('input', '', array('type' => 'hidden', 'name' => 'courseid', 'value' => $course->id));
  61. foreach ($hiddenparams as $k => $v) {
  62. $hiddenfields .= html_writer::tag('input', '', array('type' => 'hidden', 'name' => $k, 'value' => $v));
  63. }
  64. $hiddenfields .= html_writer::tag('input', '', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
  65. $formcontent .= html_writer::div($hiddenfields, '', array('id' => 'typeformdiv'));
  66. // Put everything into one tag 'options'.
  67. $formcontent .= html_writer::start_tag('div', array('class' => 'options'));
  68. $formcontent .= html_writer::div(get_string('selectaqtypefordescription', 'question'), 'instruction');
  69. // Put all options into one tag 'qoptions' to allow us to handle scrolling.
  70. $formcontent .= html_writer::start_tag('div', array('class' => 'alloptions'));
  71. // First display real questions.
  72. $formcontent .= $this->qbank_chooser_title('questions', 'question');
  73. $formcontent .= $this->qbank_chooser_types($real);
  74. $formcontent .= html_writer::div('', 'separator');
  75. // Then fake questions.
  76. $formcontent .= $this->qbank_chooser_title('other');
  77. $formcontent .= $this->qbank_chooser_types($fake);
  78. // Options.
  79. $formcontent .= html_writer::end_tag('div');
  80. // Types.
  81. $formcontent .= html_writer::end_tag('div');
  82. // Add the form submission buttons.
  83. $submitbuttons = '';
  84. $submitbuttons .= html_writer::tag('input', '',
  85. array('type' => 'submit', 'name' => 'submitbutton', 'class' => 'submitbutton', 'value' => get_string('add')));
  86. $submitbuttons .= html_writer::tag('input', '',
  87. array('type' => 'submit', 'name' => 'addcancel', 'class' => 'addcancel', 'value' => get_string('cancel')));
  88. $formcontent .= html_writer::div($submitbuttons, 'submitbuttons');
  89. $formcontent .= html_writer::end_tag('form');
  90. // Wrap the whole form in a div.
  91. $formcontent = html_writer::tag('div', $formcontent, array('id' => 'chooseform'));
  92. // Generate the header and return the whole form.
  93. $header = html_writer::div(get_string('chooseqtypetoadd', 'question'), 'choosertitle hd');
  94. return $header . html_writer::div(html_writer::div($formcontent, 'choosercontainer'), 'chooserdialogue');
  95. }
  96. /**
  97. * Build the HTML for a specified set of question types.
  98. *
  99. * @param array $types A set of question types as used by the qbank_chooser_module function
  100. * @return string The composed HTML for the module
  101. */
  102. protected function qbank_chooser_types($types) {
  103. $return = '';
  104. foreach ($types as $type) {
  105. $return .= $this->qbank_chooser_qtype($type);
  106. }
  107. return $return;
  108. }
  109. /**
  110. * Return the HTML for the specified question type, adding any required classes.
  111. *
  112. * @param object $qtype An object containing the title, and link. An icon, and help text may optionally be specified.
  113. * If the module contains subtypes in the types option, then these will also be displayed.
  114. * @param array $classes Additional classes to add to the encompassing div element
  115. * @return string The composed HTML for the question type
  116. */
  117. protected function qbank_chooser_qtype($qtype, $classes = array()) {
  118. $output = '';
  119. $classes[] = 'option';
  120. $output .= html_writer::start_tag('div', array('class' => implode(' ', $classes)));
  121. $output .= html_writer::start_tag('label', array('for' => 'qtype_' . $qtype->plugin_name()));
  122. $output .= html_writer::tag('input', '', array('type' => 'radio',
  123. 'name' => 'qtype', 'id' => 'qtype_' . $qtype->plugin_name(), 'value' => $qtype->name()));
  124. $output .= html_writer::start_tag('span', array('class' => 'modicon'));
  125. // Add an icon if we have one.
  126. $output .= $this->pix_icon('icon', $qtype->local_name(), $qtype->plugin_name(),
  127. array('title' => $qtype->local_name(), 'class' => 'icon'));
  128. $output .= html_writer::end_tag('span');
  129. $output .= html_writer::span($qtype->menu_name(), 'typename');
  130. // Format the help text using markdown with the following options.
  131. $options = new stdClass();
  132. $options->trusted = false;
  133. $options->noclean = false;
  134. $options->smiley = false;
  135. $options->filter = false;
  136. $options->para = true;
  137. $options->newlines = false;
  138. $options->overflowdiv = false;
  139. $qtype->help = format_text(get_string('pluginnamesummary', $qtype->plugin_name()), FORMAT_MARKDOWN, $options);
  140. $output .= html_writer::span($qtype->help, 'typesummary');
  141. $output .= html_writer::end_tag('label');
  142. $output .= html_writer::end_tag('div');
  143. return $output;
  144. }
  145. /**
  146. * Return the title for the question bank chooser.
  147. *
  148. * @param string $title The language string identifier
  149. * @param string $identifier The component identifier
  150. * @return string The composed HTML for the title
  151. */
  152. protected function qbank_chooser_title($title, $identifier = null) {
  153. $span = html_writer::span('', 'modicon');
  154. $span .= html_writer::span(get_string($title, $identifier), 'typename');
  155. return html_writer::div($span, 'option moduletypetitle');
  156. }
  157. }