Merge branch 'MDL-26365' of git://github.com/timhunt/moodle
[moodle.git] / course / completion.js
blob16df5a7d3d7cb0c5cd53dee391c7f822784a3661
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');
22             if (current == 1) {
23                 args.state.set('value', 0);
24                 args.image.set('src', M.util.image_url('i/completion-manual-y', 'moodle'));
25                 args.image.set('alt', M.str.completion['completion-alt-manual-y']);
26                 args.image.set('title', M.str.completion['completion-title-manual-y']);
27             } else {
28                 args.state.set('value', 1);
29                 args.image.set('src', M.util.image_url('i/completion-manual-n', 'moodle'));
30                 args.image.set('alt', M.str.completion['completion-alt-manual-n']);
31                 args.image.set('title', M.str.completion['completion-title-manual-n']);
32             }
33         }
35         args.ajax.remove();
36     };
38     var handle_failure = function(id, o, args) {
39         alert('An error occurred when attempting to save your tick mark.\n\n('+o.responseText+'.)'); //TODO: localize
40         args.ajax.remove();
41     };
43     var toggle = function(e) {
44         e.preventDefault();
46         var form = e.target;
47         var cmid = 0;
48         var completionstate = 0;
49         var state = null;
50         var image = null;
52         var inputs = Y.Node.getDOMNode(form).getElementsByTagName('input');
53         for (var i=0; i<inputs.length; i++) {
54             switch (inputs[i].name) {
55                  case 'id':
56                      cmid = inputs[i].value;
57                      break;
58                   case 'completionstate':
59                      completionstate = inputs[i].value;
60                      state = Y.one(inputs[i]);
61                      break;
62             }
63             if (inputs[i].type == 'image') {
64                 image = Y.one(inputs[i]);
65             }
66         }
68         // start spinning the ajax indicator
69         var ajax = Y.Node.create('<div class="ajaxworking" />');
70         form.append(ajax);
72         var cfg = {
73             method: "POST",
74             data: 'id='+cmid+'&completionstate='+completionstate+'&fromajax=1&sesskey='+M.cfg.sesskey,
75             on: {
76                 success: handle_success,
77                 failure: handle_failure
78             },
79             arguments: {state: state, image: image, ajax: ajax}
80         };
82         Y.use('io', function(Y) {
83             Y.io(M.cfg.wwwroot+'/course/togglecompletion.php', cfg);
84         });
85     };
87     // register submit handlers on manual tick completion forms
88     Y.all('form.togglecompletion').each(function(form) {
89         if (!form.hasClass('preventjs')) {
90             Y.on('submit', toggle, form);
91         }
92     });
94     // hide the help if there are no completion toggles or icons
95     var help = Y.one('#completionprogressid');
96     if (help && !(Y.one('form.togglecompletion') || Y.one('.autocompletion'))) {
97         help.setStyle('display', 'none');
98     }