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