MDL-61138 mod_quiz: use AMD modal for add random question

This commit is contained in:
Ryan Wyllie
2018-04-18 11:45:00 +08:00
parent f046b9a4c5
commit 1e46fb428f
7 changed files with 158 additions and 73 deletions
+1
View File
@@ -0,0 +1 @@
define(["jquery","core/notification","core/modal_factory"],function(a,b,c){return{init:function(d,e,f,g){var h=a("body");return c.create({type:d,large:!0,preShowCallback:function(b,c){b=a(b),c.setContextId(f),c.setAddOnPageId(b.attr("data-addonpage")),c.setTitle(b.attr("data-header")),g&&g(b,c)}},[h,e]).fail(b.exception)}}});
+1
View File
@@ -0,0 +1 @@
define(["mod_quiz/add_question_modal_launcher","mod_quiz/modal_add_random_question"],function(a,b){return{init:function(c,d,e,f){a.init(b.TYPE,'.menu [data-action="addarandomquestion"]',c,function(a,b){b.setCategory(d),b.setReturnUrl(e),b.setCMID(f)})}}});
+1 -1
View File
@@ -1 +1 @@
define(["jquery","core/notification","core/custom_interaction_events","core/modal_factory","mod_quiz/modal_quiz_question_bank"],function(a,b,c,d,e){var f={ADD_QUESTION_LINKS:'.menu [data-action="questionbank"]'};return{init:function(g){var h=a("body");d.create({type:e.TYPE,large:!0},[h,f.ADD_QUESTION_LINKS]).then(function(b){return b.setContextId(g),h.on(c.events.activate,f.ADD_QUESTION_LINKS,function(c){var d=a(c.target).closest(f.ADD_QUESTION_LINKS);b.setAddOnPageId(d.attr("data-addonpage")),b.setTitle(d.attr("data-header"))}),b}).fail(b.exception)}}});
define(["mod_quiz/add_question_modal_launcher","mod_quiz/modal_quiz_question_bank"],function(a,b){return{init:function(c){a.init(b.TYPE,'.menu [data-action="questionbank"]',c)}}});
@@ -0,0 +1,78 @@
// 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/>.
/**
* Initialise the an add question modal on the quiz page.
*
* @module mod_quiz/add_question_modal_launcher
* @package mod_quiz
* @copyright 2018 Ryan Wyllie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(
[
'jquery',
'core/notification',
'core/modal_factory',
],
function(
$,
Notification,
ModalFactory
) {
return {
/**
* Create a modal using the modal factory and add listeners to launch the
* modal when clicked.
*
* @param {string} modalType Which modal to create
* @param {string} selector The selectors for the elements that trigger the modal
* @param {int} contextId The current context id
* @param {function} preShowCallback A callback to execute before the modal is shown
* @return {promise} Resolved with the modal
*/
init: function(modalType, selector, contextId, preShowCallback) {
var body = $('body');
// Create a question bank modal using the factory.
// The same modal will be used by all of the add question
// links that match "selector" on the page. The content
// of the modal will be changed depending on which link is
// clicked.
return ModalFactory.create(
{
type: modalType,
large: true,
// This callback executes before the modal is shown when the
// trigger element is clicked.
preShowCallback: function(triggerElement, modal) {
triggerElement = $(triggerElement);
modal.setContextId(contextId);
modal.setAddOnPageId(triggerElement.attr('data-addonpage'));
modal.setTitle(triggerElement.attr('data-header'));
if (preShowCallback) {
preShowCallback(triggerElement, modal);
}
}
},
// Created a deligated listener rather than a single
// trigger element.
[body, selector]
).fail(Notification.exception);
}
};
});
+57
View File
@@ -0,0 +1,57 @@
// 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/>.
/**
* Initialise the add random question modal on the quiz page.
*
* @module mod_quiz/add_random_question
* @package mod_quiz
* @copyright 2018 Ryan Wyllie <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(
[
'mod_quiz/add_question_modal_launcher',
'mod_quiz/modal_add_random_question'
],
function(
AddQuestionModalLauncher,
ModalAddRandomQuestion
) {
return {
/**
* Create the add random question modal.
*
* @param {int} contextId Current context id.
* @param {string} category Category id and category context id comma separated.
* @param {string} returnUrl URL to return to after form submission.
* @param {int} cmid Current course module id.
*/
init: function(contextId, category, returnUrl, cmid) {
AddQuestionModalLauncher.init(
ModalAddRandomQuestion.TYPE,
'.menu [data-action="addarandomquestion"]',
contextId,
// Additional values that should be set before the modal is shown.
function(triggerElement, modal) {
modal.setCategory(category);
modal.setReturnUrl(returnUrl);
modal.setCMID(cmid);
}
);
}
};
});
+12 -41
View File
@@ -23,55 +23,26 @@
*/
define(
[
'jquery',
'core/notification',
'core/custom_interaction_events',
'core/modal_factory',
'mod_quiz/add_question_modal_launcher',
'mod_quiz/modal_quiz_question_bank'
],
function(
$,
Notification,
CustomEvents,
ModalFactory,
AddQuestionModalLauncher,
ModalQuizQuestionBank
) {
var SELECTORS = {
ADD_QUESTION_LINKS: '.menu [data-action="questionbank"]',
};
return {
/**
* Create the question bank modal.
*
* @param {int} contextId Current context id.
*/
init: function(contextId) {
var body = $('body');
// Create a question bank modal using the factory.
// The same modal will be used by all of the add question
// links on the page. The content of the modal will be
// changed depending on which link is clicked.
ModalFactory.create(
{
type: ModalQuizQuestionBank.TYPE,
large: true
},
// Created a deligated listener rather than a single
// trigger element.
[body, SELECTORS.ADD_QUESTION_LINKS]
).then(function(modal) {
// Save the Moodle context id that the modal is being rendered in.
modal.setContextId(contextId);
body.on(CustomEvents.events.activate, SELECTORS.ADD_QUESTION_LINKS, function(e) {
// We need to listen for activations on the trigger elements because there are
// several on the page and we need to know which one was activated in order to
// set some relevant data on the modal.
var triggerElement = $(e.target).closest(SELECTORS.ADD_QUESTION_LINKS);
modal.setAddOnPageId(triggerElement.attr('data-addonpage'));
modal.setTitle(triggerElement.attr('data-header'));
});
return modal;
}).fail(Notification.exception);
AddQuestionModalLauncher.init(
ModalQuizQuestionBank.TYPE,
'.menu [data-action="questionbank"]',
contextId
);
}
};
});
+8 -31
View File
@@ -98,17 +98,17 @@ class edit_renderer extends \plugin_renderer_base {
// Include the contents of any other popups required.
if ($structure->can_be_edited()) {
$popups = '';
$thiscontext = $contexts->lowest();
$this->page->requires->js_call_amd('mod_quiz/quizquestionbank', 'init', [
$contexts->lowest()->id
$thiscontext->id
]);
$popups .= $this->random_question_form($pageurl, $contexts, $pagevars);
$this->page->requires->yui_module('moodle-mod_quiz-randomquestion',
'M.mod_quiz.randomquestion.init');
$output .= html_writer::div($popups, 'mod_quiz_edit_forms');
$this->page->requires->js_call_amd('mod_quiz/add_random_question', 'init', [
$thiscontext->id,
$pagevars['cat'],
$pageurl->out_as_local_url(true),
$pageurl->param('cmid')
]);
// Include the question chooser.
$output .= $this->question_chooser();
@@ -1059,29 +1059,6 @@ class edit_renderer extends \plugin_renderer_base {
return html_writer::div($this->pix_icon('i/loading', get_string('loading')), 'questionbankloading');
}
/**
* Return random question form.
* @param \moodle_url $thispageurl the canonical URL of this page.
* @param \question_edit_contexts $contexts the relevant question bank contexts.
* @param array $pagevars the variables from {@link \question_edit_setup()}.
* @return string HTML to output.
*/
protected function random_question_form(\moodle_url $thispageurl, \question_edit_contexts $contexts, array $pagevars) {
if (!$contexts->have_cap('moodle/question:useall')) {
return '';
}
$randomform = new \quiz_add_random_form(new \moodle_url('/mod/quiz/addrandom.php'),
array('contexts' => $contexts, 'cat' => $pagevars['cat']));
$randomform->set_data(array(
'category' => $pagevars['cat'],
'returnurl' => $thispageurl->out_as_local_url(true),
'randomnumber' => 1,
'cmid' => $thispageurl->param('cmid'),
));
return html_writer::div($randomform->render(), 'randomquestionformforpopup');
}
/**
* Initialise the JavaScript for the general editing. (JavaScript for popups
* is handled with the specific code for those.)