Commit | Line | Data |
---|---|---|
aa9bdbe3 TH |
1 | // This file is part of Moodle - http://moodle.org/ |
2 | // | |
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. | |
7 | // | |
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. | |
12 | // | |
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/>. | |
15 | ||
16 | /** | |
17 | * JavaScript library for dealing with the question flags. | |
18 | * | |
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. | |
21 | * | |
22 | * @package moodlecore | |
23 | * @subpackage questionengine | |
24 | * @copyright 2010 The Open University | |
25 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
26 | */ | |
ff065f96 TH |
27 | |
28 | M.core_question_flags = { | |
29 | flagattributes: null, | |
30 | actionurl: null, | |
22c97c64 | 31 | flagtext: null, |
ff065f96 TH |
32 | listeners: [], |
33 | ||
22c97c64 | 34 | init: function(Y, actionurl, flagattributes, flagtext) { |
ff065f96 TH |
35 | M.core_question_flags.flagattributes = flagattributes; |
36 | M.core_question_flags.actionurl = actionurl; | |
22c97c64 | 37 | M.core_question_flags.flagtext = flagtext; |
ff065f96 TH |
38 | |
39 | Y.all('div.questionflag').each(function(flagdiv, i) { | |
404da238 | 40 | var checkbox = flagdiv.one('input[type=checkbox]'); |
ff065f96 TH |
41 | if (!checkbox) { |
42 | return; | |
43 | } | |
44 | ||
22c97c64 | 45 | var input = Y.Node.create('<input type="hidden" class="questionflagvalue" />'); |
ff065f96 TH |
46 | input.set('id', checkbox.get('id')); |
47 | input.set('name', checkbox.get('name')); | |
48 | input.set('value', checkbox.get('checked') ? 1 : 0); | |
49 | ||
50 | // Create an image input to replace the img tag. | |
51 | var image = Y.Node.create('<input type="image" class="questionflagimage" />'); | |
22c97c64 TH |
52 | var flagtext = Y.Node.create('<span class="questionflagtext">.</span>'); |
53 | M.core_question_flags.update_flag(input, image, flagtext); | |
474ee938 | 54 | |
ff065f96 TH |
55 | checkbox.remove(); |
56 | flagdiv.one('label').remove(); | |
57 | flagdiv.append(input); | |
58 | flagdiv.append(image); | |
474ee938 | 59 | flagdiv.append(flagtext); |
ff065f96 TH |
60 | }); |
61 | ||
62 | Y.delegate('click', function(e) { | |
22c97c64 | 63 | var input = this.one('input.questionflagvalue'); |
ff065f96 | 64 | input.set('value', 1 - input.get('value')); |
22c97c64 TH |
65 | M.core_question_flags.update_flag(input, this.one('input.questionflagimage'), |
66 | this.one('span.questionflagtext')); | |
67 | var postdata = this.one('input.questionflagpostdata').get('value') + | |
2be77520 | 68 | input.get('value'); |
ff065f96 TH |
69 | |
70 | e.halt(); | |
71 | Y.io(M.core_question_flags.actionurl , {method: 'POST', 'data': postdata}); | |
72 | M.core_question_flags.fire_listeners(postdata); | |
22c97c64 | 73 | }, document.body, 'div.questionflag'); |
ff065f96 TH |
74 | }, |
75 | ||
22c97c64 TH |
76 | update_flag: function(input, image, flagtext) { |
77 | var value = input.get('value'); | |
78 | image.setAttrs(M.core_question_flags.flagattributes[value]); | |
79 | flagtext.replaceChild(flagtext.create(M.core_question_flags.flagtext[value]), | |
80 | flagtext.get('firstChild')); | |
81 | flagtext.set('title', M.core_question_flags.flagattributes[value].title); | |
ff065f96 TH |
82 | }, |
83 | ||
84 | add_listener: function(listener) { | |
85 | M.core_question_flags.listeners.push(listener); | |
86 | }, | |
87 | ||
88 | fire_listeners: function(postdata) { | |
89 | for (var i = 0; i < M.core_question_flags.listeners.length; i++) { | |
90 | M.core_question_flags.listeners[i]( | |
06f8ed54 TH |
91 | postdata.match(/\bqubaid=(\d+)\b/)[1], |
92 | postdata.match(/\bslot=(\d+)\b/)[1], | |
ff065f96 TH |
93 | postdata.match(/\bnewstate=(\d+)\b/)[1] |
94 | ); | |
95 | } | |
96 | } | |
97 | }; |