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();
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
20 var current = args.state.get('value');
21 var modulename = args.modulename.get('value'),
24 button = args.image.get('parentNode');
28 altstr = M.util.get_string('completion-alt-manual-y', 'completion', modulename);
29 iconkey = 'i/completion-manual-y';
30 args.state.set('value', 0);
32 altstr = M.util.get_string('completion-alt-manual-n', 'completion', modulename);
33 iconkey = 'i/completion-manual-n';
34 args.state.set('value', 1);
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);
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
53 var toggle = function(e) {
58 var completionstate = 0;
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) {
67 cmid = inputs[i].value;
69 case 'completionstate':
70 completionstate = inputs[i].value;
71 state = Y.one(inputs[i]);
74 modulename = Y.one(inputs[i]);
78 image = form.one('button .icon');
80 // start spinning the ajax indicator
81 var ajax = Y.Node.create('<div class="ajaxworking" />');
86 data: 'id='+cmid+'&completionstate='+completionstate+'&fromajax=1&sesskey='+M.cfg.sesskey,
88 success: handle_success,
89 failure: handle_failure
91 arguments: {state: state, image: image, ajax: ajax, modulename: modulename}
94 Y.use('io-base', function(Y) {
95 Y.io(M.cfg.wwwroot+'/course/togglecompletion.php', cfg);
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);
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');