MDL-55797 core_question: Convert the chooser to template

Part of MDL-55071
This commit is contained in:
Frederic Massart 2016-09-01 16:39:08 +08:00 committed by Dan Poltawski
parent 370e7637c6
commit 6f0993ccbc
9 changed files with 180 additions and 104 deletions

View File

@ -0,0 +1,69 @@
<?php
// 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 <http://www.gnu.org/licenses/>.
/**
* 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);
}
}
}

View File

@ -0,0 +1,56 @@
<?php
// 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 <http://www.gnu.org/licenses/>.
/**
* 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);
}
}

View File

@ -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) {

View File

@ -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 '';
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
}}
{{!
Question bank chooser.
}}
{{> core/chooser }}
{{#js}}
require([
'core/yui'
], function(Y) {
Y.use('moodle-question-chooser', function() {
M.question.init_chooser();
});
});
{{/js}}

View File

@ -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'
};

View File

@ -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"]});

View File

@ -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'
};

View File

@ -1,7 +1,7 @@
var SELECTORS = {
CREATENEWQUESTION: 'div.createnewquestion',
CREATENEWQUESTIONFORM: 'div.createnewquestion form',
CHOOSERDIALOGUE: 'div.chooserdialogue',
CHOOSERDIALOGUE: 'div.chooserdialoguebody',
CHOOSERHEADER: 'div.choosertitle'
};