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');
23 args.state.set('value', 0);
24 args.image.set('src', M.util.image_url('i/completion-manual-y', 'moodle'));
25 args.image.set('alt', M.str.completion['completion-alt-manual-y']);
26 args.image.set('title', M.str.completion['completion-title-manual-y']);
28 args.state.set('value', 1);
29 args.image.set('src', M.util.image_url('i/completion-manual-n', 'moodle'));
30 args.image.set('alt', M.str.completion['completion-alt-manual-n']);
31 args.image.set('title', M.str.completion['completion-title-manual-n']);
38 var handle_failure = function(id, o, args) {
39 alert('An error occurred when attempting to save your tick mark.\n\n('+o.responseText+'.)'); //TODO: localize
43 var toggle = function(e) {
48 var completionstate = 0;
52 var inputs = Y.Node.getDOMNode(form).getElementsByTagName('input');
53 for (var i=0; i<inputs.length; i++) {
54 switch (inputs[i].name) {
56 cmid = inputs[i].value;
58 case 'completionstate':
59 completionstate = inputs[i].value;
60 state = Y.one(inputs[i]);
63 if (inputs[i].type == 'image') {
64 image = Y.one(inputs[i]);
68 // start spinning the ajax indicator
69 var ajax = Y.Node.create('<div class="ajaxworking" />');
74 data: 'id='+cmid+'&completionstate='+completionstate+'&fromajax=1&sesskey='+M.cfg.sesskey,
76 success: handle_success,
77 failure: handle_failure
79 arguments: {state: state, image: image, ajax: ajax}
82 Y.use('io', function(Y) {
83 Y.io(M.cfg.wwwroot+'/course/togglecompletion.php', cfg);
87 // register submit handlers on manual tick completion forms
88 Y.all('form.togglecompletion').each(function(form) {
89 if (!form.hasClass('preventjs')) {
90 Y.on('submit', toggle, form);
94 // hide the help if there are no completion toggles or icons
95 var help = Y.one('#completionprogressid');
96 if (help && !(Y.one('form.togglecompletion') || Y.one('.autocompletion'))) {
97 help.setStyle('display', 'none');