From 6f0993ccbcc95b492a6a57ac42fd5eafff435dd0 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Thu, 1 Sep 2016 16:39:08 +0800 Subject: [PATCH] MDL-55797 core_question: Convert the chooser to template Part of MDL-55071 --- question/classes/output/qbank_chooser.php | 69 +++++++++++ .../classes/output/qbank_chooser_item.php | 56 +++++++++ question/editlib.php | 5 - question/renderer.php | 117 ++++-------------- question/templates/qbank_chooser.mustache | 29 +++++ .../moodle-question-chooser-debug.js | 2 +- .../moodle-question-chooser-min.js | 2 +- .../moodle-question-chooser.js | 2 +- question/yui/src/chooser/js/chooser.js | 2 +- 9 files changed, 180 insertions(+), 104 deletions(-) create mode 100644 question/classes/output/qbank_chooser.php create mode 100644 question/classes/output/qbank_chooser_item.php create mode 100644 question/templates/qbank_chooser.mustache diff --git a/question/classes/output/qbank_chooser.php b/question/classes/output/qbank_chooser.php new file mode 100644 index 00000000000..645aeb2c693 --- /dev/null +++ b/question/classes/output/qbank_chooser.php @@ -0,0 +1,69 @@ +. + +/** + * The qbank_chooser renderable. + * + * @package core_question + * @copyright 2016 Frédéric Massart - FMCorz.net + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core_question\output; +defined('MOODLE_INTERNAL') || die(); + +use core\output\chooser_section; +use lang_string; +use moodle_url; + + +/** + * The qbank_chooser renderable class. + * + * @package core_question + * @copyright 2016 Frédéric Massart - FMCorz.net + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class qbank_chooser extends \core\output\chooser { + + /** + * Constructor. + */ + public function __construct($real, $fake, $course, $hiddenparams, $context) { + $sections = []; + $sections[] = new chooser_section('questions', new lang_string('questions', 'question'), + array_map(function($qtype) use ($context) { + return new qbank_chooser_item($qtype, $context); + }, $real)); + + $sections[] = new chooser_section('other', new lang_string('other'), + array_map(function($qtype) use ($context) { + return new qbank_chooser_item($qtype, $context); + }, $fake)); + + parent::__construct(new moodle_url('/question/question.php'), + new lang_string('chooseqtypetoadd', 'question'), $sections, 'qtype'); + + $this->set_instructions(new lang_string('selectaqtypefordescription', 'question')); + + $this->set_method('get'); + $this->add_param('courseid', $course->id); + foreach ($hiddenparams as $k => $v) { + $this->add_param($k, $v); + } + } + +} diff --git a/question/classes/output/qbank_chooser_item.php b/question/classes/output/qbank_chooser_item.php new file mode 100644 index 00000000000..da834d1adc1 --- /dev/null +++ b/question/classes/output/qbank_chooser_item.php @@ -0,0 +1,56 @@ +. + +/** + * The qbank_chooser_item renderable. + * + * @package core_question + * @copyright 2016 Frédéric Massart - FMCorz.net + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core_question\output; +defined('MOODLE_INTERNAL') || die(); + +use lang_string; +use pix_icon; + + +/** + * The qbank_chooser_item renderable class. + * + * @package core_question + * @copyright 2016 Frédéric Massart - FMCorz.net + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class qbank_chooser_item extends \core\output\chooser_item { + + /** + * Constructor. + * + * @param object $qtype The question type. + * @param context $context The relevant context. + */ + public function __construct($qtype, $context) { + $icon = new pix_icon('icon', $qtype->local_name(), $qtype->plugin_name(), [ + 'class' => 'icon', + 'title' => $qtype->local_name() + ]); + $help = new lang_string('pluginnamesummary', $qtype->plugin_name()); + parent::__construct($qtype->plugin_name(), $qtype->menu_name(), $qtype->name(), $icon, $help, $context); + } + +} diff --git a/question/editlib.php b/question/editlib.php index c7d8290ac16..fe4ce7728a5 100644 --- a/question/editlib.php +++ b/question/editlib.php @@ -450,11 +450,6 @@ function require_login_in_context($contextorid = null){ function print_choose_qtype_to_add_form($hiddenparams, array $allowedqtypes = null, $enablejs = true) { global $CFG, $PAGE, $OUTPUT; - if ($enablejs) { - // Add the chooser. - $PAGE->requires->yui_module('moodle-question-chooser', 'M.question.init_chooser', array(array())); - } - $realqtypes = array(); $fakeqtypes = array(); foreach (question_bank::get_creatable_qtypes() as $qtypename => $qtype) { diff --git a/question/renderer.php b/question/renderer.php index 621d5ada755..f6961bbcbd4 100644 --- a/question/renderer.php +++ b/question/renderer.php @@ -48,6 +48,16 @@ class core_question_bank_renderer extends plugin_renderer_base { return $this->pix_icon('icon', $namestr, $qtype->plugin_name(), array('title' => $namestr)); } + /** + * Render a qbank_chooser. + * + * @param renderable $qbankchooser The chooser. + * @return string + */ + public function render_qbank_chooser(renderable $qbankchooser) { + return $this->render_from_template('core_question/qbank_chooser', $qbankchooser->export_for_template($this)); + } + /** * Build the HTML for the question chooser javascript popup. * @@ -58,61 +68,9 @@ class core_question_bank_renderer extends plugin_renderer_base { * @return string The composed HTML for the questionbank chooser */ public function qbank_chooser($real, $fake, $course, $hiddenparams) { - global $OUTPUT; - - // Start the form content. - $formcontent = html_writer::start_tag('form', array('action' => new moodle_url('/question/question.php'), - 'id' => 'chooserform', 'method' => 'get')); - - // Add the hidden fields. - $hiddenfields = ''; - $hiddenfields .= html_writer::tag('input', '', array('type' => 'hidden', 'name' => 'category', 'id' => 'qbankcategory')); - $hiddenfields .= html_writer::tag('input', '', array('type' => 'hidden', 'name' => 'courseid', 'value' => $course->id)); - foreach ($hiddenparams as $k => $v) { - $hiddenfields .= html_writer::tag('input', '', array('type' => 'hidden', 'name' => $k, 'value' => $v)); - } - $hiddenfields .= html_writer::tag('input', '', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())); - $formcontent .= html_writer::div($hiddenfields, '', array('id' => 'typeformdiv')); - - // Put everything into one tag 'options'. - $formcontent .= html_writer::start_tag('div', array('class' => 'options')); - $formcontent .= html_writer::div(get_string('selectaqtypefordescription', 'question'), 'instruction'); - - // Put all options into one tag 'qoptions' to allow us to handle scrolling. - $formcontent .= html_writer::start_tag('div', array('class' => 'alloptions')); - - // First display real questions. - $formcontent .= $this->qbank_chooser_title('questions', 'question'); - $formcontent .= $this->qbank_chooser_types($real); - - $formcontent .= html_writer::div('', 'separator'); - - // Then fake questions. - $formcontent .= $this->qbank_chooser_title('other'); - $formcontent .= $this->qbank_chooser_types($fake); - - // Options. - $formcontent .= html_writer::end_tag('div'); - - // Types. - $formcontent .= html_writer::end_tag('div'); - - // Add the form submission buttons. - $submitbuttons = ''; - $submitbuttons .= html_writer::tag('input', '', - array('type' => 'submit', 'name' => 'submitbutton', 'class' => 'submitbutton', 'value' => get_string('add'))); - $submitbuttons .= html_writer::tag('input', '', - array('type' => 'submit', 'name' => 'addcancel', 'class' => 'addcancel', 'value' => get_string('cancel'))); - $formcontent .= html_writer::div($submitbuttons, 'submitbuttons'); - - $formcontent .= html_writer::end_tag('form'); - - // Wrap the whole form in a div. - $formcontent = html_writer::tag('div', $formcontent, array('id' => 'chooseform')); - - // Generate the header and return the whole form. - $header = html_writer::div(get_string('chooseqtypetoadd', 'question'), 'choosertitle hd'); - return $header . html_writer::div(html_writer::div($formcontent, 'choosercontainer'), 'chooserdialogue'); + $chooser = new \core_question\output\qbank_chooser($real, $fake, $course, $hiddenparams, + context_course::instance($course->id)); + return $this->render($chooser); } /** @@ -122,11 +80,9 @@ class core_question_bank_renderer extends plugin_renderer_base { * @return string The composed HTML for the module */ protected function qbank_chooser_types($types) { - $return = ''; - foreach ($types as $type) { - $return .= $this->qbank_chooser_qtype($type); - } - return $return; + debugging('Method core_question_bank_renderer::qbank_chooser_types() is deprecated, ' . + 'see core_question_bank_renderer::render_qbank_chooser().', DEBUG_DEVELOPER); + return ''; } /** @@ -138,37 +94,9 @@ class core_question_bank_renderer extends plugin_renderer_base { * @return string The composed HTML for the question type */ protected function qbank_chooser_qtype($qtype, $classes = array()) { - $output = ''; - $classes[] = 'option'; - $output .= html_writer::start_tag('div', array('class' => implode(' ', $classes))); - $output .= html_writer::start_tag('label', array('for' => 'qtype_' . $qtype->plugin_name())); - $output .= html_writer::tag('input', '', array('type' => 'radio', - 'name' => 'qtype', 'id' => 'qtype_' . $qtype->plugin_name(), 'value' => $qtype->name())); - - $output .= html_writer::start_tag('span', array('class' => 'modicon')); - // Add an icon if we have one. - $output .= $this->pix_icon('icon', $qtype->local_name(), $qtype->plugin_name(), - array('title' => $qtype->local_name(), 'class' => 'icon')); - $output .= html_writer::end_tag('span'); - - $output .= html_writer::span($qtype->menu_name(), 'typename'); - - // Format the help text using markdown with the following options. - $options = new stdClass(); - $options->trusted = false; - $options->noclean = false; - $options->smiley = false; - $options->filter = false; - $options->para = true; - $options->newlines = false; - $options->overflowdiv = false; - $qtype->help = format_text(get_string('pluginnamesummary', $qtype->plugin_name()), FORMAT_MARKDOWN, $options); - - $output .= html_writer::span($qtype->help, 'typesummary'); - $output .= html_writer::end_tag('label'); - $output .= html_writer::end_tag('div'); - - return $output; + debugging('Method core_question_bank_renderer::qbank_chooser_qtype() is deprecated, ' . + 'see core_question_bank_renderer::render_qbank_chooser().', DEBUG_DEVELOPER); + return ''; } /** @@ -179,9 +107,8 @@ class core_question_bank_renderer extends plugin_renderer_base { * @return string The composed HTML for the title */ protected function qbank_chooser_title($title, $identifier = null) { - $span = html_writer::span('', 'modicon'); - $span .= html_writer::span(get_string($title, $identifier), 'typename'); - - return html_writer::div($span, 'option moduletypetitle'); + debugging('Method core_question_bank_renderer::qbank_chooser_title() is deprecated, ' . + 'see core_question_bank_renderer::render_qbank_chooser().', DEBUG_DEVELOPER); + return ''; } } diff --git a/question/templates/qbank_chooser.mustache b/question/templates/qbank_chooser.mustache new file mode 100644 index 00000000000..2343d89b0fe --- /dev/null +++ b/question/templates/qbank_chooser.mustache @@ -0,0 +1,29 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + Question bank chooser. +}} +{{> core/chooser }} +{{#js}} +require([ + 'core/yui' +], function(Y) { + Y.use('moodle-question-chooser', function() { + M.question.init_chooser(); + }); +}); +{{/js}} diff --git a/question/yui/build/moodle-question-chooser/moodle-question-chooser-debug.js b/question/yui/build/moodle-question-chooser/moodle-question-chooser-debug.js index dc9bac7a839..0c927881572 100644 --- a/question/yui/build/moodle-question-chooser/moodle-question-chooser-debug.js +++ b/question/yui/build/moodle-question-chooser/moodle-question-chooser-debug.js @@ -3,7 +3,7 @@ YUI.add('moodle-question-chooser', function (Y, NAME) { var SELECTORS = { CREATENEWQUESTION: 'div.createnewquestion', CREATENEWQUESTIONFORM: 'div.createnewquestion form', - CHOOSERDIALOGUE: 'div.chooserdialogue', + CHOOSERDIALOGUE: 'div.chooserdialoguebody', CHOOSERHEADER: 'div.choosertitle' }; diff --git a/question/yui/build/moodle-question-chooser/moodle-question-chooser-min.js b/question/yui/build/moodle-question-chooser/moodle-question-chooser-min.js index e80bd32f936..91a6d64764d 100644 --- a/question/yui/build/moodle-question-chooser/moodle-question-chooser-min.js +++ b/question/yui/build/moodle-question-chooser/moodle-question-chooser-min.js @@ -1 +1 @@ -YUI.add("moodle-question-chooser",function(e,t){function r(){r.superclass.constructor.apply(this,arguments)}var n={CREATENEWQUESTION:"div.createnewquestion",CREATENEWQUESTIONFORM:"div.createnewquestion form",CHOOSERDIALOGUE:"div.chooserdialogue",CHOOSERHEADER:"div.choosertitle"};e.extend(r,M.core.chooserdialogue,{initializer:function(){e.all("form").each(function(e){/question\/addquestion\.php/.test(e.getAttribute("action"))&&e.on("submit",this.displayQuestionChooser,this)},this)},displayQuestionChooser:function(t){var r=e.one(n.CREATENEWQUESTION+" "+n.CHOOSERDIALOGUE),i=e.one(n.CREATENEWQUESTION+" "+n.CHOOSERHEADER);this.container===null&&(this.setup_chooser_dialogue(r,i,{}),this.prepare_chooser());var s=t.target.ancestor("form",!0),o=this.container.one("form"),u=s.all('input[type="hidden"]');o.all("input.customfield").remove(),u.each(function(e){o.appendChild(e.cloneNode()).removeAttribute("id").addClass("customfield")}),this.display_chooser(t)}},{NAME:"questionChooser"}),M.question=M.question||{},M.question.init_chooser=function(e){return new r(e)}},"@VERSION@",{requires:["moodle-core-chooserdialogue"]}); +YUI.add("moodle-question-chooser",function(e,t){function r(){r.superclass.constructor.apply(this,arguments)}var n={CREATENEWQUESTION:"div.createnewquestion",CREATENEWQUESTIONFORM:"div.createnewquestion form",CHOOSERDIALOGUE:"div.chooserdialoguebody",CHOOSERHEADER:"div.choosertitle"};e.extend(r,M.core.chooserdialogue,{initializer:function(){e.all("form").each(function(e){/question\/addquestion\.php/.test(e.getAttribute("action"))&&e.on("submit",this.displayQuestionChooser,this)},this)},displayQuestionChooser:function(t){var r=e.one(n.CREATENEWQUESTION+" "+n.CHOOSERDIALOGUE),i=e.one(n.CREATENEWQUESTION+" "+n.CHOOSERHEADER);this.container===null&&(this.setup_chooser_dialogue(r,i,{}),this.prepare_chooser());var s=t.target.ancestor("form",!0),o=this.container.one("form"),u=s.all('input[type="hidden"]');o.all("input.customfield").remove(),u.each(function(e){o.appendChild(e.cloneNode()).removeAttribute("id").addClass("customfield")}),this.display_chooser(t)}},{NAME:"questionChooser"}),M.question=M.question||{},M.question.init_chooser=function(e){return new r(e)}},"@VERSION@",{requires:["moodle-core-chooserdialogue"]}); diff --git a/question/yui/build/moodle-question-chooser/moodle-question-chooser.js b/question/yui/build/moodle-question-chooser/moodle-question-chooser.js index dc9bac7a839..0c927881572 100644 --- a/question/yui/build/moodle-question-chooser/moodle-question-chooser.js +++ b/question/yui/build/moodle-question-chooser/moodle-question-chooser.js @@ -3,7 +3,7 @@ YUI.add('moodle-question-chooser', function (Y, NAME) { var SELECTORS = { CREATENEWQUESTION: 'div.createnewquestion', CREATENEWQUESTIONFORM: 'div.createnewquestion form', - CHOOSERDIALOGUE: 'div.chooserdialogue', + CHOOSERDIALOGUE: 'div.chooserdialoguebody', CHOOSERHEADER: 'div.choosertitle' }; diff --git a/question/yui/src/chooser/js/chooser.js b/question/yui/src/chooser/js/chooser.js index 8ab920006cd..4ae91a652ae 100644 --- a/question/yui/src/chooser/js/chooser.js +++ b/question/yui/src/chooser/js/chooser.js @@ -1,7 +1,7 @@ var SELECTORS = { CREATENEWQUESTION: 'div.createnewquestion', CREATENEWQUESTIONFORM: 'div.createnewquestion form', - CHOOSERDIALOGUE: 'div.chooserdialogue', + CHOOSERDIALOGUE: 'div.chooserdialoguebody', CHOOSERHEADER: 'div.choosertitle' };