completion.js 2.4 KB

1
  1. M.core_completion={};M.core_completion.init=function(Y){var changeDetector=Y.one('#completion_dynamic_change');if(changeDetector.get('value')>0){changeDetector.set('value',0);window.location.reload();return};var handle_success=function(id,o,args){Y.one('#completion_dynamic_change').set('value',1);if(o.responseText!='OK'){alert('An error occurred when attempting to save your tick mark.\n\n('+o.responseText+'.)')}else{var current=args.state.get('value'),modulename=args.modulename.get('value'),altstr,titlestr;if(current==1){altstr=M.util.get_string('completion-alt-manual-y','completion',modulename);titlestr=M.util.get_string('completion-title-manual-y','completion',modulename);args.state.set('value',0);args.image.set('src',M.util.image_url('i/completion-manual-y','moodle'));args.image.set('alt',altstr);args.image.set('title',titlestr)}else{altstr=M.util.get_string('completion-alt-manual-n','completion',modulename);titlestr=M.util.get_string('completion-title-manual-n','completion',modulename);args.state.set('value',1);args.image.set('src',M.util.image_url('i/completion-manual-n','moodle'));args.image.set('alt',altstr);args.image.set('title',titlestr)}};args.ajax.remove()},handle_failure=function(id,o,args){alert('An error occurred when attempting to save your tick mark.\n\n('+o.responseText+'.)');args.ajax.remove()},toggle=function(e){e.preventDefault();var form=e.target,cmid=0,completionstate=0,state=null,image=null,modulename=null,inputs=Y.Node.getDOMNode(form).getElementsByTagName('input');for(var i=0;i<inputs.length;i++){switch(inputs[i].name){case'id':cmid=inputs[i].value;break;case'completionstate':completionstate=inputs[i].value;state=Y.one(inputs[i]);break;case'modulename':modulename=Y.one(inputs[i]);break};if(inputs[i].type=='image')image=Y.one(inputs[i])};var ajax=Y.Node.create('<div class="ajaxworking" />');form.append(ajax);var cfg={method:"POST",data:'id='+cmid+'&completionstate='+completionstate+'&fromajax=1&sesskey='+M.cfg.sesskey,on:{success:handle_success,failure:handle_failure},arguments:{state:state,image:image,ajax:ajax,modulename:modulename}};Y.use('io-base',function(Y){Y.io(M.cfg.wwwroot+'/course/togglecompletion.php',cfg)})};Y.all('form.togglecompletion').each(function(form){if(!form.hasClass('preventjs'))Y.on('submit',toggle,form)});var help=Y.one('#completionprogressid');if(help&&!(Y.one('form.togglecompletion')||Y.one('.autocompletion')))help.setStyle('display','none')}