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');
23 var altstr = M.str.completion['completion-alt-manual-y'].replace('{$a}', modulename);
24 var titlestr = M.str.completion['completion-title-manual-y'].replace('{$a}', modulename);
25 args.state.set('value', 0);
26 args.image.set('src', M.util.image_url('i/completion-manual-y', 'moodle'));
27 args.image.set('alt', altstr);
28 args.image.set('title', titlestr);
30 var altstr = M.str.completion['completion-alt-manual-n'].replace('{$a}', modulename);
31 var titlestr = M.str.completion['completion-title-manual-n'].replace('{$a}', modulename);
32 args.state.set('value', 1);
33 args.image.set('src', M.util.image_url('i/completion-manual-n', 'moodle'));
34 args.image.set('alt', altstr);
35 args.image.set('title', titlestr);
42 var handle_failure = function(id, o, args) {
43 alert('An error occurred when attempting to save your tick mark.\n\n('+o.responseText+'.)'); //TODO: localize
47 var toggle = function(e) {
52 var completionstate = 0;
55 var modulename = null;
57 var inputs = Y.Node.getDOMNode(form).getElementsByTagName('input');
58 for (var i=0; i<inputs.length; i++) {
59 switch (inputs[i].name) {
61 cmid = inputs[i].value;
63 case 'completionstate':
64 completionstate = inputs[i].value;
65 state = Y.one(inputs[i]);
68 modulename = Y.one(inputs[i]);
71 if (inputs[i].type == 'image') {
72 image = Y.one(inputs[i]);
76 // start spinning the ajax indicator
77 var ajax = Y.Node.create('<div class="ajaxworking" />');
82 data: 'id='+cmid+'&completionstate='+completionstate+'&fromajax=1&sesskey='+M.cfg.sesskey,
84 success: handle_success,
85 failure: handle_failure
87 arguments: {state: state, image: image, ajax: ajax, modulename: modulename}
90 Y.use('io-base', function(Y) {
91 Y.io(M.cfg.wwwroot+'/course/togglecompletion.php', cfg);
95 // register submit handlers on manual tick completion forms
96 Y.all('form.togglecompletion').each(function(form) {
97 if (!form.hasClass('preventjs')) {
98 Y.on('submit', toggle, form);
102 // hide the help if there are no completion toggles or icons
103 var help = Y.one('#completionprogressid');
104 if (help && !(Y.one('form.togglecompletion') || Y.one('.autocompletion'))) {
105 help.setStyle('display', 'none');