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'),
27 altstr = M.util.get_string('completion-alt-manual-y', 'completion', modulename);
28 iconkey = 'i/completion-manual-y';
29 args.state.set('value', 0);
31 altstr = M.util.get_string('completion-alt-manual-n', 'completion', modulename);
32 iconkey = 'i/completion-manual-n';
33 args.state.set('value', 1);
36 require(['core/templates', 'core/notification'], function(Templates, Notification) {
37 Templates.renderPix(iconkey, 'core', altstr).then(function(html) {
38 Templates.replaceNode(args.image.getDOMNode(), html, '');
39 }).catch(Notification.exception);
46 var handle_failure = function(id, o, args) {
47 alert('An error occurred when attempting to save your tick mark.\n\n('+o.responseText+'.)'); //TODO: localize
51 var toggle = function(e) {
56 var completionstate = 0;
59 var modulename = null;
61 var inputs = Y.Node.getDOMNode(form).getElementsByTagName('input');
62 for (var i=0; i<inputs.length; i++) {
63 switch (inputs[i].name) {
65 cmid = inputs[i].value;
67 case 'completionstate':
68 completionstate = inputs[i].value;
69 state = Y.one(inputs[i]);
72 modulename = Y.one(inputs[i]);
76 image = form.one('button .icon');
78 // start spinning the ajax indicator
79 var ajax = Y.Node.create('<div class="ajaxworking" />');
84 data: 'id='+cmid+'&completionstate='+completionstate+'&fromajax=1&sesskey='+M.cfg.sesskey,
86 success: handle_success,
87 failure: handle_failure
89 arguments: {state: state, image: image, ajax: ajax, modulename: modulename}
92 Y.use('io-base', function(Y) {
93 Y.io(M.cfg.wwwroot+'/course/togglecompletion.php', cfg);
97 // register submit handlers on manual tick completion forms
98 Y.all('form.togglecompletion').each(function(form) {
99 if (!form.hasClass('preventjs')) {
100 Y.on('submit', toggle, form);
104 // hide the help if there are no completion toggles or icons
105 var help = Y.one('#completionprogressid');
106 if (help && !(Y.one('form.togglecompletion') || Y.one('.autocompletion'))) {
107 help.setStyle('display', 'none');