1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 * JavaScript library for dealing with the question flags.
19 * This script, and the YUI libraries that it needs, are inluded by
20 * the $PAGE->requires->js calls in question_get_html_head_contributions in lib/questionlib.php.
23 * @subpackage questionengine
24 * @copyright 2010 The Open University
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 M.core_question_flags = {
33 init: function(Y, actionurl, flagattributes) {
34 M.core_question_flags.flagattributes = flagattributes;
35 M.core_question_flags.actionurl = actionurl;
37 Y.all('div.questionflag').each(function(flagdiv, i) {
38 var checkbox = flagdiv.one('input[type=checkbox]');
43 var input = Y.Node.create('<input type="hidden" />');
44 input.set('id', checkbox.get('id'));
45 input.set('name', checkbox.get('name'));
46 input.set('value', checkbox.get('checked') ? 1 : 0);
48 // Create an image input to replace the img tag.
49 var image = Y.Node.create('<input type="image" class="questionflagimage" />');
50 M.core_question_flags.update_flag(input, image);
53 flagdiv.one('label').remove();
54 flagdiv.append(input);
55 flagdiv.append(image);
58 Y.delegate('click', function(e) {
59 var input = this.previous('input');
60 input.set('value', 1 - input.get('value'));
61 M.core_question_flags.update_flag(input, this);
62 var postdata = this.previous('input.questionflagpostdata').get('value') +
66 Y.io(M.core_question_flags.actionurl , {method: 'POST', 'data': postdata});
67 M.core_question_flags.fire_listeners(postdata);
68 }, document.body, 'input.questionflagimage');
71 update_flag: function(input, image) {
72 image.setAttrs(M.core_question_flags.flagattributes[input.get('value')]);
75 add_listener: function(listener) {
76 M.core_question_flags.listeners.push(listener);
79 fire_listeners: function(postdata) {
80 for (var i = 0; i < M.core_question_flags.listeners.length; i++) {
81 M.core_question_flags.listeners[i](
82 postdata.match(/\bqubaid=(\d+)\b/)[1],
83 postdata.match(/\bslot=(\d+)\b/)[1],
84 postdata.match(/\bnewstate=(\d+)\b/)[1]