MDL-80203 courseformat: Fix some typos and PHPDoc
[moodle.git] / question / flags.js
blob3720d2409b17fd3ab84e2a6362e3924c5d3bf6d2
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.
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/>.
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  */
28 M.core_question_flags = {
29     flagattributes: null,
30     actionurl: null,
31     listeners: [],
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]');
39             if (!checkbox) {
40                 return;
41             }
43             var input = Y.Node.create('<input type="hidden" class="questionflagvalue" />');
44             input.set('id', checkbox.get('id'));
45             input.set('name', checkbox.get('name'));
46             input.set('value', checkbox.get('checked') ? 1 : 0);
48             var ariaPressed = checkbox.get('checked') ? 'true' : 'false';
49             var toggle = Y.Node.create('<a ' +
50                 'tabindex="0" ' +
51                 'class="aabtn" ' +
52                 'role="button" ' +
53                 'aria-pressed="' + ariaPressed + '">' +
54                     '.' +
55                 '</a>');
56             M.core_question_flags.update_flag(input, toggle);
58             checkbox.remove();
59             flagdiv.one('label').remove();
60             flagdiv.append(input);
61             flagdiv.append(toggle);
62         });
64         Y.delegate('click', function(e) {
65             e.halt();
66             M.core_question_flags.process(this);
67         }, document.body, 'div.questionflag');
68         Y.delegate('key', function(e) {
69             e.halt();
70             if (e.keyCode == 13) {
71                 M.core_question_flags.process(this);
72             }
73         }, document.body, 'down:enter, space', 'div.questionflag');
74         Y.delegate('key', function(e) {
75             e.halt();
76             M.core_question_flags.process(this);
77         }, document.body, 'up:space', 'div.questionflag');
78     },
80     update_flag: function(input, toggle) {
81         var value = input.get('value');
82         toggle.setContent(
83             '<img class="questionflagimage" src="' + M.core_question_flags.flagattributes[value].src + '" alt="" />' +
84             M.core_question_flags.flagattributes[value].text
85         );
86         toggle.set('aria-pressed', parseInt(value) ? 'true' : 'false');
87         toggle.set('aria-label', M.core_question_flags.flagattributes[value].alt);
88         if (M.core_question_flags.flagattributes[value].title != M.core_question_flags.flagattributes[value].text) {
89             toggle.set('title', M.core_question_flags.flagattributes[value].title);
90         } else {
91             toggle.removeAttribute('title');
92         }
93     },
95     /**
96      * Process the change of flag status.
97      *
98      * @param {Y.Node} target The root element
99      */
100     process: function(target) {
101         var input = target.one('input.questionflagvalue');
102         input.set('value', 1 - input.get('value'));
103         M.core_question_flags.update_flag(input, target.one('[aria-pressed]'));
104         var postdata = target.one('input.questionflagpostdata').get('value') +
105             input.get('value');
107         Y.io(M.core_question_flags.actionurl, {method: 'POST', 'data': postdata});
108         M.core_question_flags.fire_listeners(postdata);
109     },
111     add_listener: function(listener) {
112         M.core_question_flags.listeners.push(listener);
113     },
115     fire_listeners: function(postdata) {
116         for (var i = 0; i < M.core_question_flags.listeners.length; i++) {
117             M.core_question_flags.listeners[i](
118                 postdata.match(/\bqubaid=(\d+)\b/)[1],
119                 postdata.match(/\bslot=(\d+)\b/)[1],
120                 postdata.match(/\bnewstate=(\d+)\b/)[1]
121             );
122         }
123     }