mod quiz + questions MDL-22370 convert JavaScript to YUI3 and modules.

Note, quiz editing JS has not been done yet.
This commit is contained in:
Tim Hunt 2010-08-04 15:22:04 +00:00
parent 24f17d7588
commit ff065f96bc
15 changed files with 418 additions and 478 deletions

65
question/flags.js Normal file
View file

@ -0,0 +1,65 @@
// This script, and the YUI libraries that it needs, are inluded by
// the $PAGE->requires->js calls in question_get_html_head_contributions in lib/questionlib.php.
M.core_question_flags = {
flagattributes: null,
actionurl: null,
listeners: [],
init: function(Y, actionurl, flagattributes) {
M.core_question_flags.flagattributes = flagattributes;
M.core_question_flags.actionurl = actionurl;
Y.all('div.questionflag').each(function(flagdiv, i) {
var checkbox = flagdiv.one('input.questionflagcheckbox');
if (!checkbox) {
return;
}
var input = Y.Node.create('<input type="hidden" />');
input.set('id', checkbox.get('id'));
input.set('name', checkbox.get('name'));
input.set('value', checkbox.get('checked') ? 1 : 0);
// Create an image input to replace the img tag.
var image = Y.Node.create('<input type="image" class="questionflagimage" />');
M.core_question_flags.update_flag(input, image);
checkbox.remove();
flagdiv.one('label').remove();
flagdiv.append(input);
flagdiv.append(image);
});
Y.delegate('click', function(e) {
var input = this.previous('input');
input.set('value', 1 - input.get('value'));
M.core_question_flags.update_flag(input, this);
var postdata = this.previous('input.questionflagpostdata').get('value') +
input.get('value')
e.halt();
Y.io(M.core_question_flags.actionurl , {method: 'POST', 'data': postdata});
M.core_question_flags.fire_listeners(postdata);
}, document.body, 'input.questionflagimage');
},
update_flag: function(input, image) {
image.setAttrs(M.core_question_flags.flagattributes[input.get('value')]);
},
add_listener: function(listener) {
M.core_question_flags.listeners.push(listener);
},
fire_listeners: function(postdata) {
for (var i = 0; i < M.core_question_flags.listeners.length; i++) {
M.core_question_flags.listeners[i](
postdata.match(/\baid=(\d+)\b/)[1],
postdata.match(/\bqid=(\d+)\b/)[1],
postdata.match(/\bnewstate=(\d+)\b/)[1]
);
}
}
};

View file

@ -207,7 +207,7 @@
$strpreview = get_string('preview', 'quiz').' '.format_string($questions[$id]->name);
$questionlist = array($id);
get_html_head_contributions($questionlist, $questions, $states[$historylength]);
question_get_html_head_contributions($questionlist, $questions, $states[$historylength]);
$PAGE->set_title($strpreview);
$PAGE->set_heading($COURSE->fullname);
echo $OUTPUT->header();

View file

@ -1,92 +0,0 @@
// This script, and the YUI libraries that it needs, are inluded by
// the $PAGE->requires->js calls in get_html_head_contributions in lib/questionlib.php.
question_flag_changer = {
flag_state_listeners: new Object(),
init_flag: function(checkboxid, postdata) {
// Create a hidden input - you can't just repurpose the old checkbox, IE
// does not cope - and put it in place of the checkbox.
var checkbox = document.getElementById(checkboxid);
var input = document.createElement('input');
input.type = 'hidden';
checkbox.parentNode.appendChild(input);
checkbox.parentNode.removeChild(checkbox);
input.id = checkbox.id;
input.name = checkbox.name;
input.value = checkbox.checked ? 1 : 0;
input.ajaxpostdata = postdata;
// Create an image input to replace the img tag.
var image = document.createElement('input');
image.type = 'image';
image.statestore = input;
question_flag_changer.update_image(image);
input.parentNode.appendChild(image);
// Remove the label.
var label = document.getElementById(checkboxid + 'label');
label.parentNode.removeChild(label);
// Add the event handler.
YAHOO.util.Event.addListener(image, 'click', this.flag_state_change);
},
init_flag_save_form: function(submitbuttonid) {
// With JS on, we just want to remove all visible traces of the form.
var button = document.getElementById(submitbuttonid);
button.parentNode.removeChild(button);
},
flag_state_change: function(e) {
var image = e.target ? e.target : e.srcElement;
var input = image.statestore;
input.value = 1 - input.value;
question_flag_changer.update_image(image);
var postdata = input.ajaxpostdata
if (input.value == 1) {
postdata += '&newstate=1'
} else {
postdata += '&newstate=0'
}
YAHOO.util.Event.preventDefault(e);
question_flag_changer.fire_state_changed(input);
YAHOO.util.Connect.asyncRequest('POST', qengine_config.actionurl, null, postdata);
},
update_image: function(image) {
if (image.statestore.value == 1) {
image.src = qengine_config.flagicon;
image.alt = qengine_config.flaggedalt;
image.title = qengine_config.unflagtooltip;
} else {
image.src = qengine_config.unflagicon;
image.alt = qengine_config.unflaggedalt;
image.title = qengine_config.flagtooltip;
}
},
add_flag_state_listener: function(questionid, listener) {
var key = 'q' + questionid;
if (!question_flag_changer.flag_state_listeners.hasOwnProperty(key)) {
question_flag_changer.flag_state_listeners[key] = [];
}
question_flag_changer.flag_state_listeners[key].push(listener);
},
questionid_from_inputid: function(inputid) {
return inputid.replace(/resp(\d+)__flagged/, '$1');
},
fire_state_changed: function(input) {
var questionid = question_flag_changer.questionid_from_inputid(input.id);
var key = 'q' + questionid;
if (!question_flag_changer.flag_state_listeners.hasOwnProperty(key)) {
return;
}
var newstate = input.value == 1;
for (var i = 0; i < question_flag_changer.flag_state_listeners[key].length; i++) {
question_flag_changer.flag_state_listeners[key][i].flag_state_changed(newstate);
}
}
};

View file

@ -967,12 +967,15 @@ class default_questiontype {
$aid = $state->attempt;
$qid = $state->question;
$checksum = question_get_toggleflag_checksum($aid, $qid, $qsid);
$postdata = "qsid=$qsid&aid=$aid&qid=$qid&checksum=$checksum&sesskey=" . sesskey();
$postdata = "qsid=$qsid&aid=$aid&qid=$qid&checksum=$checksum&sesskey=" .
sesskey() . '&newstate=';
$flagcontent = '<input type="checkbox" id="' . $id . '" name="' . $id .
'" value="1" ' . $checked . ' />' .
'<label id="' . $id . 'label" for="' . $id . '">' . $this->get_question_flag_tag(
'" class="questionflagcheckbox" value="1" ' . $checked . ' />' .
'<input type="hidden" value="' . s($postdata) . '" class="questionflagpostdata" />' .
'<label id="' . $id . 'label" for="' . $id .
'" class="questionflaglabel">' . $this->get_question_flag_tag(
$state->flagged, $id . 'img') . '</label>' . "\n";
$PAGE->requires->js_function_call('question_flag_changer.init_flag', array($id, $postdata));
question_init_qengine_js();
break;
default:
$flagcontent = '';