MDL-67264 core_course: Activity chooser new feature
[moodle.git] / course / completion.js
CommitLineData
4e781c7b 1
76c0123b
PS
2M.core_completion = {};
3
4M.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;
aa6c1ced 11 }
76c0123b 12
80df5744
PS
13 var handle_success = function(id, o, args) {
14 Y.one('#completion_dynamic_change').set('value', 1);
76c0123b 15
80df5744
PS
16 if (o.responseText != 'OK') {
17 alert('An error occurred when attempting to save your tick mark.\n\n('+o.responseText+'.)'); //TODO: localize
4e781c7b 18
80df5744
PS
19 } else {
20 var current = args.state.get('value');
64e7aa4d
AN
21 var modulename = args.modulename.get('value'),
22 altstr,
d2d82a9d
H
23 iconkey,
24 button = args.image.get('parentNode');
8857c715 25
80df5744 26 if (current == 1) {
64e7aa4d 27 altstr = M.util.get_string('completion-alt-manual-y', 'completion', modulename);
8857c715 28 iconkey = 'i/completion-manual-y';
80df5744 29 args.state.set('value', 0);
80df5744 30 } else {
64e7aa4d 31 altstr = M.util.get_string('completion-alt-manual-n', 'completion', modulename);
8857c715 32 iconkey = 'i/completion-manual-n';
80df5744 33 args.state.set('value', 1);
80df5744 34 }
8857c715
DW
35
36 require(['core/templates', 'core/notification'], function(Templates, Notification) {
37 Templates.renderPix(iconkey, 'core', altstr).then(function(html) {
7219f590
DW
38 var id = button.get('id'),
39 postFocus = '$(document.getElementById("' + id + '")).focus();';
40
41 Templates.replaceNode(args.image.getDOMNode(), html, postFocus);
8857c715
DW
42 }).catch(Notification.exception);
43 });
76c0123b 44 }
4e781c7b 45
80df5744 46 args.ajax.remove();
9e5c8a84 47 };
4454447d 48
80df5744 49 var handle_failure = function(id, o, args) {
76c0123b 50 alert('An error occurred when attempting to save your tick mark.\n\n('+o.responseText+'.)'); //TODO: localize
80df5744 51 args.ajax.remove();
9e5c8a84 52 };
76c0123b 53
80df5744
PS
54 var toggle = function(e) {
55 e.preventDefault();
56
57 var form = e.target;
58 var cmid = 0;
59 var completionstate = 0;
60 var state = null;
61 var image = null;
9e4baaf9 62 var modulename = null;
80df5744
PS
63
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;
9e4baaf9
RT
74 case 'modulename':
75 modulename = Y.one(inputs[i]);
76 break;
80df5744 77 }
905b1de6 78 }
8857c715 79 image = form.one('button .icon');
80df5744
PS
80
81 // start spinning the ajax indicator
82 var ajax = Y.Node.create('<div class="ajaxworking" />');
83 form.append(ajax);
84
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 },
9e4baaf9 92 arguments: {state: state, image: image, ajax: ajax, modulename: modulename}
80df5744
PS
93 };
94
0958759d 95 Y.use('io-base', function(Y) {
80df5744
PS
96 Y.io(M.cfg.wwwroot+'/course/togglecompletion.php', cfg);
97 });
9e5c8a84 98 };
76c0123b 99
80df5744
PS
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 });
76c0123b 106
80df5744
PS
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 }
9e5c8a84 112};
76c0123b
PS
113
114