[LON-CAPA-cvs] cvs: loncom(version_2_1_X) /homework bridgetask.pm task_grading.js

albertel lon-capa-cvs@mail.lon-capa.org
Wed, 19 Apr 2006 20:36:19 -0000


albertel		Wed Apr 19 16:36:19 2006 EDT

  Modified files:              (Branch: version_2_1_X)
    /loncom/homework	bridgetask.pm task_grading.js 
  Log:
  - added keyboard support for the grading radio buttons
  - submit validation. If a partially graded task is
    submitted the JS will ask for user confirmation
  - 'Fail Rest' button statically added to html
  
  
Index: loncom/homework/bridgetask.pm
diff -u loncom/homework/bridgetask.pm:1.128.2.8 loncom/homework/bridgetask.pm:1.128.2.9
--- loncom/homework/bridgetask.pm:1.128.2.8	Mon Apr 10 14:08:50 2006
+++ loncom/homework/bridgetask.pm	Wed Apr 19 16:36:19 2006
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA 
 # definition of tags that give a structure to a document
 #
-# $Id: bridgetask.pm,v 1.128.2.8 2006/04/10 18:08:50 albertel Exp $
+# $Id: bridgetask.pm,v 1.128.2.9 2006/04/19 20:36:19 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -349,12 +349,15 @@
 
     my %lt=&Apache::lonlocal::texthash('done' => 'Next Item',
 				       'stop' => 'Quit Grading',
+				       'fail' => 'Fail Rest',
 				       );
 				       
     my $result=<<INFO;
   <div class="LC_GRADING_maincontrols">
     <input type="submit" name="next" value="$lt{'done'}" />
     <input type="submit" name="stop" value="$lt{'stop'}" />
+    <input type="button" name="fail" value="$lt{'fail'}" 
+           onclick="javascript:onFailRest()" />
   </div>
   $file_list
 INFO
Index: loncom/homework/task_grading.js
diff -u loncom/homework/task_grading.js:1.1 loncom/homework/task_grading.js:1.2
--- loncom/homework/task_grading.js:1.1	Tue Mar 28 13:33:24 2006
+++ loncom/homework/task_grading.js	Wed Apr 19 16:36:19 2006
@@ -68,16 +68,24 @@
 }
 
 //Attach an onClick event handler to a node
-function attachClickEvent(node, func) {
-	node.onclick = func; //Add event
-	
+function attachEvent(node, eventName, func) {
+	node[eventName] = func; //Add event
+
 	if (!isIE) {
 		//Allow me to raise events
-		node.addEventListener("onclick", func, true); //false to get it in bubble not capture
+		node.addEventListener(eventName, func, true); //false to get it in bubble not capture
+	}
+	if (node.captureEvents) {
+		var eventMap = new Object;
+		eventMap["onclick"] = Event.CLICK;
+		eventMap["onchange"] = Event.CHANGE;
+		eventMap["onsubmit"] = Event.SUBMIT;
+		if (! eventMap[eventName]) { return false; }
+		node.captureEvents(eventMap[eventName]);
 	}
-	if (node.captureEvents) { node.captureEvents(Event.CLICK); }
 }
 
+
 //Fire an event on a given node
 function dispatchEvent(node, eventName) {
 	if (document.createEvent) {
@@ -111,7 +119,7 @@
 	var a = document.createElement('a');
 	var txt = document.createTextNode('Collapse');
 	a.setAttribute('href', '#');
-	attachClickEvent(a, onSlideDrawer);
+	attachEvent(a, "onchange", onSlideDrawer);
 	a.appendChild(txt);
 	li.appendChild(a);
 	
@@ -149,8 +157,9 @@
 	for (var i = 0; i < inputs.length; i++)  {
 		if (inputs[i].type == "radio") {
 			var val = inputs[i].value;
-			if (val == "pass" || val == "fail" || val == "review") {
-				attachClickEvent(inputs[i], onSetGrade); //Add event
+			if (val == "pass" || val == "fail" || val == "review" || val == "ungraded") {
+				attachEvent(inputs[i], "onchange", onSetGrade);
+				attachEvent(inputs[i], "onclick", onSetGrade);
 			}
 			if (inputs[i].checked) {
 				dispatchEvent(inputs[i], 'onclick');
@@ -161,15 +170,13 @@
 
 //Adjust the Done/Stop/Fail All button set
 function setupButtons() {
-    //Create Fail All button
-	var btn = document.createElement('input');
-	btn.setAttribute('type', 'button');
-	btn.setAttribute('value', 'Fail Rest');
-	attachClickEvent(btn, onFailRest);
-
-    //Add the button in
-	var div = myGetElementsByClass('div','LC_GRADING_maincontrols', 1);
-    if (div.length > 0) { div[0].appendChild(btn); }
+	//Setup the onSubmit validation
+	var frms = document.getElementsByTagName('form');
+	for(var i=0; i < frms.length; i++ ) {
+		if (frms[i].name == "lonhomework") {
+			attachEvent(frms[i], "onsubmit", onValidate);
+		}
+	}
 }
 
 /*
@@ -188,8 +195,7 @@
 }
 
 //Fail all ungraded criteria
-function onFailRest(e) {
-	var obj = getEventObject(e);	
+function onFailRest() {
 	var inputs = document.getElementsByTagName('input');
 	
 	var graded = false;
@@ -210,25 +216,46 @@
 	}
 }
 
-//Set bacground for grade chosen
+//Set background for grade chosen
 function onSetGrade( e ) {
 	var obj = getEventObject(e);
 	var grade;
 	var gradediv;
 	
-	//Find the Radio button and get it's value
-	if (obj.tagName == 'INPUT') {
-		grade = obj.value;
-	} else {
-		rdo = obj.getElementsByTagName('INPUT');
-		if (rdo.length > 0) { grade = rdo[0].value; }
-	}
-	
 	//Search for parent DIV
 	gradediv = obj;
 	while (gradediv.tagName != 'DIV') {
 		gradediv = gradediv.parentNode;
 	}
+	
+	rdo = gradediv.getElementsByTagName('INPUT');
+	for(var i=0; i < rdo.length; i++) {
+		if ( rdo[i].checked ) {
+			grade = rdo[i].value;
+			break;
+		}
+	}
 
 	gradediv.className = "LC_GRADING_grade LC_GRADING_" + grade;
 }
+
+//Validate form before submit
+function onValidate(e) {
+	var obj = getEventObject(e);
+	var ok = true;
+	var inputs = document.getElementsByTagName('input');
+	
+	var cnt = 0;
+	for (var i = 0; i < inputs.length; i++)  {
+		if (inputs[i].type == "radio") {
+			var val = inputs[i].value;
+			if (val == "ungraded" && inputs[i].checked) {
+					cnt++;
+			}
+		}
+	}
+	if (cnt) {
+		ok = confirm("You have " + cnt + " questions still ungraded.\nThis will return an ungraded task to the queue?");
+	}
+	return ok;
+}