Merge branch 'MDL-66648-310' of https://github.com/paulholden/moodle into MOODLE_310_...
[moodle.git] / course / completion.js
bloba5b5c8e3aa56e27f90ef7c86c22afe1e79634a01
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');
26             if (current == 1) {
27                 altstr = M.util.get_string('completion-alt-manual-y', 'completion', modulename);
28                 iconkey = 'i/completion-manual-y';
29                 args.state.set('value', 0);
30             } else {
31                 altstr = M.util.get_string('completion-alt-manual-n', 'completion', modulename);
32                 iconkey = 'i/completion-manual-n';
33                 args.state.set('value', 1);
34             }
36             require(['core/templates', 'core/notification'], function(Templates, Notification) {
37                 Templates.renderPix(iconkey, 'core', altstr).then(function(html) {
38                     var id = button.get('id'),
39                         postFocus = '$(document.getElementById("' + id + '")).focus();';
41                     Templates.replaceNode(args.image.getDOMNode(), html, postFocus);
42                 }).catch(Notification.exception);
43             });
44         }
46         args.ajax.remove();
47     };
49     var handle_failure = function(id, o, args) {
50         alert('An error occurred when attempting to save your tick mark.\n\n('+o.responseText+'.)'); //TODO: localize
51         args.ajax.remove();
52     };
54     var toggle = function(e) {
55         e.preventDefault();
57         var form = e.target;
58         var cmid = 0;
59         var completionstate = 0;
60         var state = null;
61         var image = null;
62         var modulename = null;
64         var inputs = Y.Node.getDOMNode(form).getElementsByTagName('input');
65         for (var i=0; i<inputs.length; i++) {
66             switch (inputs[i].name) {
67                  case 'id':
68                      cmid = inputs[i].value;
69                      break;
70                   case 'completionstate':
71                      completionstate = inputs[i].value;
72                      state = Y.one(inputs[i]);
73                      break;
74                   case 'modulename':
75                      modulename = Y.one(inputs[i]);
76                      break;
77             }
78         }
79         image = form.one('button .icon');
81         // start spinning the ajax indicator
82         var ajax = Y.Node.create('<div class="ajaxworking" />');
83         form.append(ajax);
85         var cfg = {
86             method: "POST",
87             data: 'id='+cmid+'&completionstate='+completionstate+'&fromajax=1&sesskey='+M.cfg.sesskey,
88             on: {
89                 success: handle_success,
90                 failure: handle_failure
91             },
92             arguments: {state: state, image: image, ajax: ajax, modulename: modulename}
93         };
95         Y.use('io-base', function(Y) {
96             Y.io(M.cfg.wwwroot+'/course/togglecompletion.php', cfg);
97         });
98     };
100     // register submit handlers on manual tick completion forms
101     Y.all('form.togglecompletion').each(function(form) {
102         if (!form.hasClass('preventjs')) {
103             Y.on('submit', toggle, form);
104         }
105     });
107     // hide the help if there are no completion toggles or icons
108     var help = Y.one('#completionprogressid');
109     if (help && !(Y.one('form.togglecompletion') || Y.one('.autocompletion'))) {
110         help.setStyle('display', 'none');
111     }