MDL-63854 competencies, themes: misplaced dropdown arrows
[moodle.git] / course / completion.js
blob8e741423fbd9dcbdccfbab1fd003a1629e28a278
2 M.core_completion = {};
4 M.core_completion.init = function(Y) {
5     // Check the reload-forcing
6     var changeDetector = Y.one('#completion_dynamic_change');
7     if (changeDetector.get('value') > 0) {
8         changeDetector.set('value', 0);
9         window.location.reload();
10         return;
11     }
13     var handle_success = function(id, o, args) {
14         Y.one('#completion_dynamic_change').set('value', 1);
16         if (o.responseText != 'OK') {
17             alert('An error occurred when attempting to save your tick mark.\n\n('+o.responseText+'.)'); //TODO: localize
19         } else {
20             var current = args.state.get('value');
21             var modulename = args.modulename.get('value'),
22                 altstr,
23                 iconkey,
24                 button = args.image.get('parentNode');
27             if (current == 1) {
28                 altstr = M.util.get_string('completion-alt-manual-y', 'completion', modulename);
29                 iconkey = 'i/completion-manual-y';
30                 args.state.set('value', 0);
31             } else {
32                 altstr = M.util.get_string('completion-alt-manual-n', 'completion', modulename);
33                 iconkey = 'i/completion-manual-n';
34                 args.state.set('value', 1);
35             }
36             button.set('title', altstr);
38             require(['core/templates', 'core/notification'], function(Templates, Notification) {
39                 Templates.renderPix(iconkey, 'core', altstr).then(function(html) {
40                     Templates.replaceNode(args.image.getDOMNode(), html, '');
41                 }).catch(Notification.exception);
42             });
43         }
45         args.ajax.remove();
46     };
48     var handle_failure = function(id, o, args) {
49         alert('An error occurred when attempting to save your tick mark.\n\n('+o.responseText+'.)'); //TODO: localize
50         args.ajax.remove();
51     };
53     var toggle = function(e) {
54         e.preventDefault();
56         var form = e.target;
57         var cmid = 0;
58         var completionstate = 0;
59         var state = null;
60         var image = null;
61         var modulename = null;
63         var inputs = Y.Node.getDOMNode(form).getElementsByTagName('input');
64         for (var i=0; i<inputs.length; i++) {
65             switch (inputs[i].name) {
66                  case 'id':
67                      cmid = inputs[i].value;
68                      break;
69                   case 'completionstate':
70                      completionstate = inputs[i].value;
71                      state = Y.one(inputs[i]);
72                      break;
73                   case 'modulename':
74                      modulename = Y.one(inputs[i]);
75                      break;
76             }
77         }
78         image = form.one('button .icon');
80         // start spinning the ajax indicator
81         var ajax = Y.Node.create('<div class="ajaxworking" />');
82         form.append(ajax);
84         var cfg = {
85             method: "POST",
86             data: 'id='+cmid+'&completionstate='+completionstate+'&fromajax=1&sesskey='+M.cfg.sesskey,
87             on: {
88                 success: handle_success,
89                 failure: handle_failure
90             },
91             arguments: {state: state, image: image, ajax: ajax, modulename: modulename}
92         };
94         Y.use('io-base', function(Y) {
95             Y.io(M.cfg.wwwroot+'/course/togglecompletion.php', cfg);
96         });
97     };
99     // register submit handlers on manual tick completion forms
100     Y.all('form.togglecompletion').each(function(form) {
101         if (!form.hasClass('preventjs')) {
102             Y.on('submit', toggle, form);
103         }
104     });
106     // hide the help if there are no completion toggles or icons
107     var help = Y.one('#completionprogressid');
108     if (help && !(Y.one('form.togglecompletion') || Y.one('.autocompletion'))) {
109         help.setStyle('display', 'none');
110     }