MDL-32729 Question: Switch to the YUI3 chooser dialogue

This uses the core chooserdialogue and associated style changes to make an
easily generated chooser with question descriptions.
This commit is contained in:
Andrew Robert Nicols 2012-05-02 12:33:06 +01:00 committed by Andrew Nicols
parent 6d402310a4
commit ac32c27e24
11 changed files with 311 additions and 167 deletions

View File

@ -93,7 +93,7 @@ if ($cm !== null) {
// Display a form to choose the question type.
echo $OUTPUT->notification(get_string('youmustselectaqtype', 'question'));
echo $OUTPUT->box_start('generalbox boxwidthnormal boxaligncenter', 'chooseqtypebox');
print_choose_qtype_to_add_form($hiddenparams);
print_choose_qtype_to_add_form($hiddenparams, null, false);
echo $OUTPUT->box_end();
echo $OUTPUT->footer();

View File

@ -1930,75 +1930,32 @@ function require_login_in_context($contextorid = null){
* @param $allowedqtypes optional list of qtypes that are allowed. If given, only
* those qtypes will be shown. Example value array('description', 'multichoice').
*/
function print_choose_qtype_to_add_form($hiddenparams, array $allowedqtypes = null) {
function print_choose_qtype_to_add_form($hiddenparams, array $allowedqtypes = null, $enablejs = true) {
global $CFG, $PAGE, $OUTPUT;
echo '<div id="chooseqtypehead" class="hd">' . "\n";
echo $OUTPUT->heading(get_string('chooseqtypetoadd', 'question'), 3);
echo "</div>\n";
echo '<div id="chooseqtype">' . "\n";
echo '<form action="' . $CFG->wwwroot . '/question/question.php" method="get"><div id="qtypeformdiv">' . "\n";
foreach ($hiddenparams as $name => $value) {
echo '<input type="hidden" name="' . s($name) . '" value="' . s($value) . '" />' . "\n";
if ($enablejs) {
// Add the chooser.
$PAGE->requires->yui_module('moodle-question-chooser',
'M.question.init_chooser',
array(array('courseid' => $PAGE->course->id))
);
}
echo "</div>\n";
echo '<div class="qtypes">' . "\n";
echo '<div class="instruction">' . get_string('selectaqtypefordescription', 'question') . "</div>\n";
echo '<div class="alloptions">' . "\n";
echo '<div class="realqtypes">' . "\n";
$realqtypes = array();
$fakeqtypes = array();
foreach (question_bank::get_creatable_qtypes() as $qtypename => $qtype) {
if ($allowedqtypes && !in_array($qtypename, $allowedqtypes)) {
continue;
}
if ($qtype->is_real_question_type()) {
print_qtype_to_add_option($qtype);
$realqtypes[] = $qtype;
} else {
$fakeqtypes[] = $qtype;
}
}
echo "</div>\n";
echo '<div class="fakeqtypes">' . "\n";
foreach ($fakeqtypes as $qtype) {
print_qtype_to_add_option($qtype);
}
echo "</div>\n";
echo "</div>\n";
echo "</div>\n";
echo '<div class="submitbuttons">' . "\n";
echo '<input type="submit" value="' . get_string('next') . '" id="chooseqtype_submit" />' . "\n";
echo '<input type="submit" id="chooseqtypecancel" name="addcancel" value="' . get_string('cancel') . '" />' . "\n";
echo "</div></form>\n";
echo "</div>\n";
$PAGE->requires->js('/question/qengine.js');
$module = array(
'name' => 'qbank',
'fullpath' => '/question/qbank.js',
'requires' => array('yui2-dom', 'yui2-event', 'yui2-container'),
'strings' => array(),
'async' => false,
);
$PAGE->requires->js_init_call('qtype_chooser.init', array('chooseqtype'), false, $module);
}
/**
* Private function used by the preceding one.
* @param question_type $qtype the question type.
*/
function print_qtype_to_add_option($qtype) {
echo '<div class="qtypeoption">' . "\n";
echo '<label for="' . $qtype->plugin_name() . '">';
echo '<input type="radio" name="qtype" id="' . $qtype->plugin_name() .
'" value="' . $qtype->name() . '" />';
echo '<span class="qtypename">';
$fakequestion = new stdClass();
$fakequestion->qtype = $qtype->name();
echo print_question_icon($fakequestion);
echo $qtype->menu_name() . '</span><span class="qtypesummary">' .
get_string('pluginnamesummary', $qtype->plugin_name());
echo "</span></label>\n";
echo "</div>\n";
$renderer = $PAGE->get_renderer('question', 'bank');
echo $renderer->qbank_chooser($realqtypes, $fakeqtypes, $PAGE->course, $hiddenparams);
}
/**

View File

@ -58,113 +58,3 @@ question_bank = {
question_bank.headercheckbox.checked = false;
}
};
// JavaScript to make the list of question types pop-up when you click an add
// add question button.
qtype_chooser = {
radiobuttons: [],
labels: [],
container: null,
submitbutton: null,
yui3: null,
init: function(Y, boxid) {
// Store Y reference.
qtype_chooser.yui3 = Y;
// Find the radio buttons.
qtype_chooser.radiobuttons = Y.YUI2.util.Dom.getElementsBy(
function(el) { return el.type == 'radio'; }, 'input' , boxid);
qtype_chooser.labels = Y.YUI2.util.Dom.getElementsByClassName('qtypeoption', 'div', boxid);
// Find the submit button.
qtype_chooser.submitbutton = document.getElementById(boxid + '_submit');
qtype_chooser.enable_disable_submit();
// Add the event handlers.
Y.YUI2.util.Event.addListener(boxid, 'click', qtype_chooser.enable_disable_submit);
Y.YUI2.util.Event.addListener(boxid, 'key_down', qtype_chooser.enable_disable_submit);
Y.YUI2.util.Event.addListener(boxid, 'key_up', qtype_chooser.enable_disable_submit);
Y.YUI2.util.Event.addListener(boxid, 'dblclick', function(e) {
if (!qtype_chooser.submitbutton.disabled) {
M.core_scroll_manager.save_scroll_pos(Y, Y.one(qtype_chooser.submitbutton));
qtype_chooser.submitbutton.form.submit();
}
});
Y.YUI2.util.Event.onDOMReady(qtype_chooser.init_container);
Y.on('submit', function(e) {
M.core_scroll_manager.save_scroll_pos(Y, Y.one(qtype_chooser.submitbutton));
}, qtype_chooser.submitbutton.form);
},
enable_disable_submit: function() {
var Y = qtype_chooser.yui3;
var ok = false;
for (var i = 0; i < qtype_chooser.radiobuttons.length; i++) {
if (qtype_chooser.radiobuttons[i].checked) {
ok = true;
Y.YUI2.util.Dom.addClass(qtype_chooser.labels[i], 'selected');
} else {
Y.YUI2.util.Dom.removeClass(qtype_chooser.labels[i], 'selected');
}
}
qtype_chooser.submitbutton.disabled = !ok;
},
init_container: function() {
var Y = qtype_chooser.yui3;
if (!document.getElementById('qtypechoicecontainer')) {
return;
}
var qtypechoicecontainer = document.getElementById('qtypechoicecontainer');
qtypechoicecontainer.style.display = 'block';
qtypechoicecontainer.parentNode.removeChild(qtypechoicecontainer);
document.body.appendChild(qtypechoicecontainer);
qtype_chooser.container = new Y.YUI2.widget.Dialog(qtypechoicecontainer, {
constraintoviewport: true,
visible: false,
modal: true,
fixedcenter: true,
close: true,
draggable: true,
dragOnly: true,
postmethod: 'form',
zIndex: 1000
});
qtype_chooser.container.render();
Y.YUI2.util.Event.addListener('chooseqtypecancel', 'click', qtype_chooser.cancel_popup);
var addforms = Y.YUI2.util.Dom.getElementsBy(function(el) {
return /question\/addquestion\.php/.test(el.action); }, 'form', document.body);
for (var i = 0; i < addforms.length; i++) {
Y.YUI2.util.Event.addListener(addforms[i], 'submit', qtype_chooser.add_button_click);
}
},
add_button_click: function(e) {
var Y = qtype_chooser.yui3;
var form = document.getElementById('qtypeformdiv');
var oldhidden = Y.YUI2.util.Dom.getElementsBy(
function(el) { return el.type == 'hidden'; }, 'input', form);
for (var i = 0; i < oldhidden.length; i++) {
oldhidden[i].parentNode.removeChild(oldhidden[i]);
}
var wantedhidden = Y.YUI2.util.Dom.getElementsBy(
function(el) { return el.type == 'hidden'; }, 'input', this);
for (i = 0; i < wantedhidden.length; i++) {
form.appendChild(wantedhidden[i].cloneNode(true));
}
qtype_chooser.container.show();
Y.YUI2.util.Event.preventDefault(e);
},
cancel_popup: function(e) {
var Y = qtype_chooser.yui3;
qtype_chooser.container.hide();
Y.YUI2.util.Event.preventDefault(e);
}
};

View File

@ -36,7 +36,8 @@ defined('MOODLE_INTERNAL') || die();
class core_question_bank_renderer extends plugin_renderer_base {
/**
* Output the icon for a question type
* Output the icon for a question type.
*
* @param string $qtype the question type.
* @return string HTML fragment.
*/
@ -46,4 +47,141 @@ class core_question_bank_renderer extends plugin_renderer_base {
return $this->pix_icon('icon', $namestr, $qtype->plugin_name(), array('title' => $namestr));
}
/**
* Build the HTML for the question chooser javascript popup.
*
* @param array $real A set of real question types
* @param array $fake A set of fake question types
* @param object $course The course that will be displayed
* @param array $hiddenparams Any hidden parameters to add to the form
* @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');
}
/**
* Build the HTML for a specified set of question types.
*
* @param array $types A set of question types as used by the qbank_chooser_module function
* @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;
}
/**
* Return the HTML for the specified question type, adding any required classes.
*
* @param object $qtype An object containing the title, and link. An icon, and help text may optionally be specified.
* If the module contains subtypes in the types option, then these will also be displayed.
* @param array $classes Additional classes to add to the encompassing div element
* @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;
}
/**
* Return the title for the question bank chooser.
*
* @param string $title The language string identifier
* @param string $identifier The component identifier
* @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');
}
}

View File

@ -0,0 +1,47 @@
YUI.add('moodle-question-chooser', function (Y, NAME) {
var SELECTORS = {
CREATENEWQUESTION: 'div.createnewquestion',
CREATENEWQUESTIONFORM: 'div.createnewquestion form',
CHOOSERDIALOGUE: 'div.chooserdialogue',
CHOOSERHEADER: 'div.choosertitle',
QBANKCATEGORY: '#qbankcategory'
};
function Chooser() {
Chooser.superclass.constructor.apply(this, arguments);
}
Y.extend(Chooser, M.core.chooserdialogue, {
initializer: function() {
Y.one(SELECTORS.CREATENEWQUESTIONFORM).on('submit', this.displayQuestionChooser, this);
},
displayQuestionChooser: function(e) {
var dialogue = Y.one(SELECTORS.CREATENEWQUESTION + ' ' + SELECTORS.CHOOSERDIALOGUE),
header = Y.one(SELECTORS.CREATENEWQUESTION + ' ' + SELECTORS.CHOOSERHEADER);
if (this.container === null) {
// Setup the dialogue, and then prepare the chooser if it's not already been set up.
this.setup_chooser_dialogue(dialogue, header, {});
this.prepare_chooser();
}
// Set the category ID in the form - this may have been updated since the dialogue
// was previously displayed so we must update it here.
this.container.one(SELECTORS.QBANKCATEGORY).set('value',
Y.one(SELECTORS.CREATENEWQUESTIONFORM).get('category').get('value'));
// Display the chooser dialogue.
this.display_chooser(e);
}
}, {
NAME: 'questionChooser'
});
M.question = M.question || {};
M.question.init_chooser = function(config) {
return new Chooser(config);
};
}, '@VERSION@', {"requires": ["moodle-core-chooserdialogue"]});

View File

@ -0,0 +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",QBANKCATEGORY:"#qbankcategory"};e.extend(r,M.core.chooserdialogue,{initializer:function(){e.one(n.CREATENEWQUESTIONFORM).on("submit",this.displayQuestionChooser,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()),this.container.one(n.QBANKCATEGORY).set("value",e.one(n.CREATENEWQUESTIONFORM).get("category").get("value")),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

@ -0,0 +1,47 @@
YUI.add('moodle-question-chooser', function (Y, NAME) {
var SELECTORS = {
CREATENEWQUESTION: 'div.createnewquestion',
CREATENEWQUESTIONFORM: 'div.createnewquestion form',
CHOOSERDIALOGUE: 'div.chooserdialogue',
CHOOSERHEADER: 'div.choosertitle',
QBANKCATEGORY: '#qbankcategory'
};
function Chooser() {
Chooser.superclass.constructor.apply(this, arguments);
}
Y.extend(Chooser, M.core.chooserdialogue, {
initializer: function() {
Y.one(SELECTORS.CREATENEWQUESTIONFORM).on('submit', this.displayQuestionChooser, this);
},
displayQuestionChooser: function(e) {
var dialogue = Y.one(SELECTORS.CREATENEWQUESTION + ' ' + SELECTORS.CHOOSERDIALOGUE),
header = Y.one(SELECTORS.CREATENEWQUESTION + ' ' + SELECTORS.CHOOSERHEADER);
if (this.container === null) {
// Setup the dialogue, and then prepare the chooser if it's not already been set up.
this.setup_chooser_dialogue(dialogue, header, {});
this.prepare_chooser();
}
// Set the category ID in the form - this may have been updated since the dialogue
// was previously displayed so we must update it here.
this.container.one(SELECTORS.QBANKCATEGORY).set('value',
Y.one(SELECTORS.CREATENEWQUESTIONFORM).get('category').get('value'));
// Display the chooser dialogue.
this.display_chooser(e);
}
}, {
NAME: 'questionChooser'
});
M.question = M.question || {};
M.question.init_chooser = function(config) {
return new Chooser(config);
};
}, '@VERSION@', {"requires": ["moodle-core-chooserdialogue"]});

View File

@ -0,0 +1,10 @@
{
"name": "moodle-question-chooser",
"builds": {
"moodle-question-chooser": {
"jsfiles": [
"chooser.js"
]
}
}
}

42
question/yui/src/chooser/js/chooser.js vendored Normal file
View File

@ -0,0 +1,42 @@
var SELECTORS = {
CREATENEWQUESTION: 'div.createnewquestion',
CREATENEWQUESTIONFORM: 'div.createnewquestion form',
CHOOSERDIALOGUE: 'div.chooserdialogue',
CHOOSERHEADER: 'div.choosertitle',
QBANKCATEGORY: '#qbankcategory'
};
function Chooser() {
Chooser.superclass.constructor.apply(this, arguments);
}
Y.extend(Chooser, M.core.chooserdialogue, {
initializer: function() {
Y.one(SELECTORS.CREATENEWQUESTIONFORM).on('submit', this.displayQuestionChooser, this);
},
displayQuestionChooser: function(e) {
var dialogue = Y.one(SELECTORS.CREATENEWQUESTION + ' ' + SELECTORS.CHOOSERDIALOGUE),
header = Y.one(SELECTORS.CREATENEWQUESTION + ' ' + SELECTORS.CHOOSERHEADER);
if (this.container === null) {
// Setup the dialogue, and then prepare the chooser if it's not already been set up.
this.setup_chooser_dialogue(dialogue, header, {});
this.prepare_chooser();
}
// Set the category ID in the form - this may have been updated since the dialogue
// was previously displayed so we must update it here.
this.container.one(SELECTORS.QBANKCATEGORY).set('value',
Y.one(SELECTORS.CREATENEWQUESTIONFORM).get('category').get('value'));
// Display the chooser dialogue.
this.display_chooser(e);
}
}, {
NAME: 'questionChooser'
});
M.question = M.question || {};
M.question.init_chooser = function(config) {
return new Chooser(config);
};

View File

@ -0,0 +1,7 @@
{
"moodle-question-chooser": {
"requires": [
"moodle-core-chooserdialogue"
]
}
}

View File

@ -56,6 +56,11 @@
.questionbank .singleselect { margin: 0; }
/* Question editing forms. */
#page-question-addquestion #chooserdialogue,
#page-question-addquestion #choosertitle {
display: block;
}
#combinedfeedbackhdr div.fhtmleditor {padding: 0;}
#combinedfeedbackhdr div.fcheckbox {margin-bottom: 1em;}