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