Skip to content

Commit

Permalink
MDL-21400 completion manual toggle ajax converted to YUI3, simplified…
Browse files Browse the repository at this point in the history
… a bit, replaced textual feedback with standard ajax progress indicator spinning icon (fully themable)
  • Loading branch information
skodak committed Feb 4, 2010
1 parent 9cd2295 commit 76c0123
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 130 deletions.
182 changes: 87 additions & 95 deletions course/completion.js
Original file line number Diff line number Diff line change
@@ -1,109 +1,101 @@
function completion_init() {
// Check the reload-forcing
var changeDetector=document.getElementById('completion_dynamic_change');
if(changeDetector.value==1) {
changeDetector.value=0;
window.location.reload();
return;
}

var toggles=YAHOO.util.Dom.getElementsByClassName('togglecompletion', 'form');
for(var i=0;i<toggles.length;i++) {
if(toggles[i].className.indexOf('preventjs')==-1) {
completion_init_toggle(toggles[i]);
}
}
}

function completion_init_toggle(form) {
// Store all necessary references for easy access
var inputs=form.getElementsByTagName('input');
for(var i=0;i<inputs.length;i++) {
switch(inputs[i].name) {
case 'id' : form.cmid=inputs[i].value; break;
case 'completionstate' : form.otherState=inputs[i].value; break;
M.core_completion = {};

M.core_completion.init = function(Y) {
// Check the reload-forcing
var changeDetector = Y.one('#completion_dynamic_change');
if (changeDetector.get('value') > 0) {
changeDetector.set('value', 0);
window.location.reload();
return;
}
if(inputs[i].type=='image') {
form.image=inputs[i];

// register submit handlers on manual tick completion forms
Y.all('form.togglecompletion').each(function(form) {
if (!form.hasClass('preventjs')) {
Y.on('submit', M.core_completion.toggle, form);
}
});

// hide the help if there are no completion toggles or icons
var help = Y.one('#completionprogressid');
if (help && !(Y.one('form.togglecompletion') || Y.one('.autocompletion'))) {
help.setStyle('display', 'none');
}
}

// Create and position 'Saved' text
var saved=document.createElement('div');
YAHOO.util.Dom.addClass(saved,'completion-saved-display');
YAHOO.util.Dom.setStyle(saved,'display','none');
saved.appendChild(document.createTextNode(completion_strsaved));
form.appendChild(saved);
form.saved=saved;

// Add event handler
YAHOO.util.Event.addListener(form, "submit", completion_toggle);
}

function completion_handle_response(o) {
document.getElementById('completion_dynamic_change').value=1;
if(o.responseText!='OK') {
alert('An error occurred when attempting to save your tick mark.\n\n('+o.responseText+'.)');
return;
}
// Change image
if(this.otherState==1) {
this.image.src = M.util.image_url('i/completion-manual-y', 'moodle');
this.image.title=completion_strtitley;
this.image.alt=completion_stralty;
this.otherState=0;
} else {
this.image.src = M.util.image_url('i/completion-manual-n', 'moodle');
this.image.title=completion_strtitlen;
this.image.alt=completion_straltn;
this.otherState=1;
}
// Start animation
completion_update_animation(this,1.0);
}
M.core_completion.toggle = function(e) {
e.preventDefault();

function completion_update_animation(form,opacity) {
if(opacity<0.001) {
YAHOO.util.Dom.setStyle(form.saved,'display','none');
return;
}
YAHOO.util.Dom.setStyle(form.saved,'opacity',opacity);
if(opacity>0.999) {
var pos=YAHOO.util.Dom.getXY(form.image);
pos[0]+=20; // Icon size + 4px border
YAHOO.util.Dom.setStyle(form.saved,'display','block');
YAHOO.util.Dom.setXY(form.saved,pos);
}
setTimeout(function() { completion_update_animation(form,opacity-0.1); },100);
}
var form = e.target;
var cmid = 0;
var completionstate = 0;
var state = null;
var image = null;

function completion_handle_failure(o) {
alert('An error occurred when attempting to connect to our server. The tick mark will not be saved.\n\n('+
o.status+' '+o.statusText+')');
}
var 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;
}
if (inputs[i].type == 'image') {
image = Y.one(inputs[i]);
}
}

function completion_toggle(e) {
YAHOO.util.Event.preventDefault(e);
// By setting completion_wwwroot you can cause it to use absolute path
// otherwise script assumes it is called from somewhere in /course
var target = M.cfg.wwwroot + '/course/togglecompletion.php';
YAHOO.util.Connect.asyncRequest('POST',target,
{success:completion_handle_response,failure:completion_handle_failure,scope:this},
'id='+this.cmid+'&completionstate='+this.otherState+'&fromajax=1&sesskey='+M.cfg.sesskey);
// start spinning the ajax indicator
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: M.core_completion.handle_success,
failure: M.core_completion.handle_failure
},
arguments: {state: state, image: image, ajax: ajax}
};

Y.use('io', function(Y) {
Y.io(M.cfg.wwwroot+'/course/togglecompletion.php', cfg);
});
}

function completion_set_progressicon_visibility(spanid,displaystatus) {
// Check if the progress icon exists
if (document.getElementById(spanid)!= null) {
if (displaystatus=='show') {
document.getElementById(spanid).style.display="block";
}
else if (displaystatus=='hide') {
document.getElementById(spanid).style.display="none";
M.core_completion.handle_success = function(id, o, args) {
Y.one('#completion_dynamic_change').set('value', 1);

}
else {
alert ("An error occurred when calling completion_set_progressicon_visibility() function.");
if (o.responseText != 'OK') {
alert('An error occurred when attempting to save your tick mark.\n\n('+o.responseText+'.)'); //TODO: localize

} else {
var current = args.state.get('value');

if (current == 1) {
args.state.set('value', 0);
args.image.set('src', M.util.image_url('i/completion-manual-y', 'moodle'));
args.image.set('alt', mstr.completion['completion-alt-manual-y']);
args.image.set('title', mstr.completion['completion-title-manual-y']);
} else {
args.state.set('value', 1);
args.image.set('src', M.util.image_url('i/completion-manual-n', 'moodle'));
args.image.set('alt', mstr.completion['completion-alt-manual-n']);
args.image.set('title', mstr.completion['completion-title-manual-n']);
}
}

args.ajax.remove();
}

M.core_completion.handle_failure = function(id, o, args) {
alert('An error occurred when attempting to save your tick mark.\n\n('+o.responseText+'.)'); //TODO: localize
args.ajax.remove();
}


25 changes: 5 additions & 20 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1237,9 +1237,6 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false,
static $strunreadpostsone;
static $usetracking;
static $groupings;
static $shownhelp=false;
static $hiddenhelp=false;


if (!isset($initialised)) {
$groupbuttons = ($course->groupmode or (!$course->groupmodeforce));
Expand Down Expand Up @@ -1499,9 +1496,9 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false,
}
if ($completionicon) {
$imgsrc = $OUTPUT->pix_url('i/completion-'.$completionicon);
$imgalt = get_string('completion-alt-'.$completionicon,'completion');
if ($completion==COMPLETION_TRACKING_MANUAL && !$isediting) {
$imgtitle = get_string('completion-title-'.$completionicon,'completion');
$imgalt = s(get_string('completion-alt-'.$completionicon, 'completion'));
if ($completion == COMPLETION_TRACKING_MANUAL && !$isediting) {
$imgtitle = s(get_string('completion-title-'.$completionicon, 'completion'));
$newstate =
$completiondata->completionstate==COMPLETION_COMPLETE
? COMPLETION_INCOMPLETE
Expand All @@ -1512,15 +1509,13 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false,
// conditional activities system, we need to turn
// off the JS.
if (!empty($CFG->enableavailability) &&
condition_info::completion_value_used_as_condition(
$course, $mod)) {
condition_info::completion_value_used_as_condition($course, $mod)) {
$extraclass = ' preventjs';
} else {
$extraclass = '';
}
echo "
<form class='togglecompletion$extraclass' method='post' action='togglecompletion.php'><div>";
echo "
<form class='togglecompletion$extraclass' method='post' action='togglecompletion.php'><div>
<input type='hidden' name='id' value='{$mod->id}' />
<input type='hidden' name='sesskey' value='".sesskey()."' />
<input type='hidden' name='completionstate' value='$newstate' />
Expand All @@ -1531,10 +1526,6 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false,
echo "<span class='autocompletion'>";
echo "<img src='$imgsrc' alt='$imgalt' title='$imgalt' /></span>";
}
if (!$shownhelp && !$isediting) {
$PAGE->requires->js_function_call('completion_set_progressicon_visibility', array('completionprogressid', 'show'));
$shownhelp=true;
}
}
}

Expand Down Expand Up @@ -1570,12 +1561,6 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false,
if (!empty($section->sequence) || $ismoving) {
echo "</ul><!--class='section'-->\n\n";
}

//use javascript to hide the progress help button when no progress tick boxes have been displayed
if (!$shownhelp && !$hiddenhelp && $completioninfo->is_enabled()&& !$isediting && isloggedin() && !isguestuser()) {
$PAGE->requires->js_function_call('completion_set_progressicon_visibility', array('completionprogressid', 'hide'));
$hiddenhelp = true;
}
}

/**
Expand Down
14 changes: 6 additions & 8 deletions course/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,12 @@

$completion = new completion_info($course);
if ($completion->is_enabled() && ajaxenabled()) {
$PAGE->requires->yui2_lib('connection');
$PAGE->requires->js('/course/completion.js')->in_head();
$PAGE->requires->js_function_call('completion_init')->on_dom_ready();
$PAGE->requires->data_for_js('completion_strsaved', get_string('saved', 'completion'));
$PAGE->requires->data_for_js('completion_strtitley', get_string('completion-title-manual-y', 'completion'));
$PAGE->requires->data_for_js('completion_strtitlen', get_string('completion-title-manual-n', 'completion'));
$PAGE->requires->data_for_js('completion_stralty', get_string('completion-alt-manual-y', 'completion'));
$PAGE->requires->data_for_js('completion_straltn', get_string('completion-alt-manual-n', 'completion'));
$PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
$PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
$PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
$PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');

$PAGE->requires->js_init_call('M.core_completion.init');
}

// The "Editing On" button will be appearing only in the "main" course screen
Expand Down
2 changes: 1 addition & 1 deletion group/clientlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ var createLoaderImg = function (elClass, parentId, wwwRoot) {
}
var loadingImg = document.createElement("img");

loadingImg.setAttribute("src", wwwRoot+"/pix/i/ajaxloader.gif");
loadingImg.setAttribute("src", M.util.image_url('moodle', '/i/ajaxloader');
loadingImg.setAttribute("class", elClass);
loadingImg.setAttribute("alt", "Loading");
loadingImg.setAttribute("id", "loaderImg");
Expand Down
2 changes: 2 additions & 0 deletions lib/ajax/ajaxlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ protected function find_module($name) {
$module = array('name'=>$name, 'fullpath'=>'/comment/comment.js', 'requires' => array('base', 'io', 'node', 'json', 'yui2-animation'));
} else if($name === 'core_role') {
$module = array('name'=>$name, 'fullpath'=>'/admin/roles/module.js');
} else if($name === 'core_completion') {
$module = array('name'=>$name, 'fullpath'=>'/course/completion.js');
}
} else {
if ($dir = get_component_directory($name, false)) {
Expand Down
10 changes: 4 additions & 6 deletions theme/standardold/style/styles_layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -2375,13 +2375,11 @@ border-width:0px;
.course-view li.activity form.togglecompletion div {
display:inline;
}
.course-view .completion-saved-display {
.course-view .togglecompletion .ajaxworking {
position:absolute;
top:0; left:0;
border:1px solid black;
padding: 1px 2px;
background:white;
font-size:0.85em;
top:0; left:20px;
width: 20px; height: 20px;
background: url([[pix:i/ajaxloader]]) no-repeat;
}
.course-view .completionprogress {
font-size:0.80em;
Expand Down

0 comments on commit 76c0123

Please sign in to comment.