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');
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 var id = button.get('id'),
39 postFocus = '$(document.getElementById("' + id + '")).focus();';
41 Templates.replaceNode(args.image.getDOMNode(), html, postFocus);
42 }).catch(Notification.exception);
49 var handle_failure = function(id, o, args) {
50 alert('An error occurred when attempting to save your tick mark.\n\n('+o.responseText+'.)'); //TODO: localize
54 var toggle = function(e) {
59 var completionstate = 0;
62 var modulename = null;
64 var inputs = Y.Node.getDOMNode(form).getElementsByTagName('input');
65 for (var i=0; i<inputs.length; i++) {
66 switch (inputs[i].name) {
68 cmid = inputs[i].value;
70 case 'completionstate':
71 completionstate = inputs[i].value;
72 state = Y.one(inputs[i]);
75 modulename = Y.one(inputs[i]);
79 image = form.one('button .icon');
81 // start spinning the ajax indicator
82 var ajax = Y.Node.create('<div class="ajaxworking" />');
87 data: 'id='+cmid+'&completionstate='+completionstate+'&fromajax=1&sesskey='+M.cfg.sesskey,
89 success: handle_success,
90 failure: handle_failure
92 arguments: {state: state, image: image, ajax: ajax, modulename: modulename}
95 Y.use('io-base', function(Y) {
96 Y.io(M.cfg.wwwroot+'/course/togglecompletion.php', cfg);
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);
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');