Sindbad~EG File Manager

Current Path : /usr/home/beeson/public_html/helpwithmath_old/WebGrades/
Upload File :
Current File : /usr/home/beeson/public_html/helpwithmath_old/WebGrades/WebGradesViewGrades.php

<?php 
 
 // Shows the grades for a given class in a table with rows labeled by secretNumber and columns by assignment.
 // 9.10.10, added code to process late submissions.
 
require_once('DB.php');
require_once('../queryWebGrades.php');
 
$ClassName = $_GET['ClassName'];
$Teacher = $_GET['Teacher'];
$School = $_GET['School'];
$StartDate = $_GET['StartDate'];
$AssignmentFile = $_GET['AssignmentFile'];
$ShowLetterGrades = $_GET['ShowLetterGrades'];  // "yes" or not set or "no"  or a number such as 20 to use for scaling, so 100% converts to a grade of 20. 
if(!isset($_GET['ShowLetterGrades']))
    $ShowLetterGrades = "no";
$errorflag = 0;
$LatePenalty = 0.5;   // two late problems equals one on-time problem
$RequiredNumbers = array();
$NumberOfProblems = array();
$DueDates = array();

function dumpTable($table)
{ 

		$sql = "SELECT * FROM `$table`";
		$result = QueryWebGrades($sql);
		echo "<table border = \"2\">";
		while($r=$result->fetchRow())
		   { echo "<tr>"; foreach($r as $item) { echo "<td> $item </td>";} echo "</tr>"; }
		echo "</table>";
}
 
/*__________________________________________________*/			 
function getStudentNumber($SecretNumber)
// the secret number is congruent to 17 mod 23
// but student numbers auto-increment from 0
// Do not change this scheme as it is built into WebGrades.php
{   return (int) ($SecretNumber / 23);
}
/*__________________________________________________*/			
function getSecretNumber($StudentNumber)
{  return 23 * $StudentNumber + 17;
}
/*__________________________________________________*/			    
function getCourseID(  $ClassName, $Teacher, $School, $StartDate )  
{  
  $sql = "SELECT * FROM `Classes` WHERE ClassName='$ClassName' AND Teacher='$Teacher' AND School='$School' AND StartDate='$StartDate';";
  $r = QueryWebGrades($sql);
  $found = false;
  $row = $r->fetchRow();
  if($row==NULL)
      { return "Class not found.  Check the data specified in the web page that contained the form you submitted";
	  }
  else 
      return $row[0];
}
/*__________________________________________________*/
function oldTopic($topic)
// Return the MathXpert32 topic number corresponding to the MathXpert64 topic number $topic
{ if($topic < 155)
     return $topic + 5;
  return $topic + 6;
}
/*__________________________________________________*/
function displaySecretNumbers()
// for professor's use in setting up his or her spreadsheet
{
 $sql = "SELECT * FROM `Students`";
   $result = QueryWebGrades($sql);
   echo "<table border=\"2\">";
   while($r = $result->fetchRow())
      { $StudentNumber = $r[0];
	    $Email = $r[1];
		$FirstName = $r[2];
		$LastName = $r[3];
		$SecretNumber = getSecretNumber($StudentNumber);
		echo "<tr><td>$SecretNumber</td><td>$LastName, </td><td>$FirstName  </td><td>$Email</td></tr>";
	   }
   echo "</table>";
}
/*__________________________________________________*/			
function getGrades( $ClassName, $Teacher, $School, $StartDate, $AssignmentFile)
// fills the global array $Grades where $Grades[$AssignmentNumber][$StudentNumber] is the percentage of problems on that assignment solved by that student.
// counting late submissions as half a submission 
{ $CourseID = getCourseID($ClassName, $Teacher, $School, $StartDate); 
  $L = file($AssignmentFile);  // read the assignment file into an array of lines
  $count = 0;
  // Construct an array $Assignments of arrays of strings, each entry containing two or more lines of $AssignmentFile defining one assignment.
  global $AssignmentNumbers;
  global $StudentNumbers;
  global $errorflag;
  global $LatePenalty;
  global $RequiredNumbers;
  global $NumberOfProblems;
  global $DueDates;
  $Assignments=array();
  foreach ($L as $line)
      { $line = trim($line); 
	    if(substr($line,0,2)=="//") continue;   // a comment-only line
		if($line == "") continue;    // a blank line
	    $line = split("//", $line);
		$line = $line[0];    // discard comments
	    // is this a line starting a new assignment?
	    $start = substr($line,0,3);
		if($start == "Ass")
		    { // yes, it does start a new assignment
			   $count++;
			   $Assignments[$count] = array();
			   $Assignments[$count][0] = $line;
			}
	     else
		    {  $Assignments[$count][] = $line;  
			}
	   }
 
	// Now process each assignment one at a time
	foreach($Assignments as $A)
	   {  // get the assignment number and due date
	      $line = $A[0]; 
		  $p = explode(":",$line);
		  $p1 = preg_split("/[\s]+/",trim($p[0]));   // allowing multiple spaces as in "Assignment    5"
		  $AssignmentNumber = trim($p1[1]);  // it will be a number if the assignment file is correctly formatted.
		     // echo "AssignmentNumber is $AssignmentNumber now<br>";
		  if(isset($p[2]))
		     { // there is a second colon as in "Assignment 3: due 2010-09-10 : 20"
			   $RequiredNumber = trim($p[2]);
			   if(is_numeric($RequiredNumber))
			      $RequiredNumbers[$AssignmentNumber] = trim($p[2]);
			 }
		  if(!isset($RequiredNumbers[$AssignmentNumber]))
		     $RequiredNumbers[$AssignmentNumber] = "all";
		  $AssignmentNumbers[] = $AssignmentNumber;
		  $DueDate = trim($p[1]);    // it will be in format yyyy-mm-dd
		  if(substr($DueDate,0,3) == "due" || substr($DueDate,0,3) == "Due")
		      { $x = preg_split("/[\s]+/", $DueDate);
			    $DueDate = trim($x[1]);  // discard "due" 
			  }
	      $DueDates[$AssignmentNumber] = $DueDate;
		  $flag = 0;
		  $sql = "";
		  $sql32 = "";
		  $countproblems = 0;  // number of problems in this assignment 
		  foreach($A as $S)
		      { if($flag == 0)
			        $flag = 1;   // ignore the first entry, which is the line beginning with "Assignment" 
				else 
				   { if($flag > 1)
				        $sql = $sql . " OR ";
					 else 
					    $flag = 2;
				     $p = explode(":",$S);
					 $p[0] = trim($p[0]);
				     if(is_numeric($p[0]))
					    { $Topic = $p[0];
					      $OldTopic = oldtopic($Topic);
						  $Filename = "";
						  $sql = $sql . "((Topic='$Topic' AND  Filename='') OR (Topic='$OldTopic' AND NOT Filename='') AND (";
						  
						  /* That code deals with the issue that MathXpert32 uses different topic numbers than MathXpert64,
						     so we accept problems with the 'wrong' topic number $OldTopic, which is the MathXpert32 topic number,
						     provided a filename is mentioned; in MathXpert64 the filename is empty.
						  */
						 
						}
					 else
					    { $Filename = $p[0];
						  $Topic = "";
						  $sql = $sql . "(Filename='$Filename' AND (";
						}	 
				     $q = explode(",",$p[1]);
				     $count = 0;   // count the entries of $q as processed
					 $savecountproblems = $countproblems;
					 foreach($q as $e)
					     { $e = trim($e);
						   if($count > 0)
						       $sql = $sql . " OR ";
						   if(is_numeric($e))
						      { $sql = $sql . "ProblemNumber='$e'";
							    ++$countproblems;
							  }
						   else if (substr($e,0,4) == "even")
						      { // as in  "evens 2-40"
							     $R = explode("-", trim(substr($e,5)));
								 $first = trim($R[0]);
								 $second = trim($R[1]);
								if(!is_numeric($first)  || !is_numeric($second)) 
								    { echo("error in assignment file in assignment number $AssignmentNumber");
									  echo "first is <br>$first<br> and second is <br>$second<br>";
									  $errorflag = 1;
									}
							    else if(($first % 2) == 1 || ($second % 2) == 1)
								    { $errorflag = 1;
									  echo("error in assignment file in assignment number $AssignmentNumber");
									  echo("After the word 'evens', expecting two even numbers separated by a hyphen.");
									}
								else
								    { $countproblems += ($second - $first)/2 + 1;
									  $sql = $sql . "(ProblemNumber BETWEEN '$first' AND  '$second' AND (ProblemNumber % 2) == 0)";
									}
							  }
						   else if (substr($e,0,3) == "odd")
						      { // as in  "odds 1-41"
							     $R = explode("-", trim(substr($e,4)));
								 $first = trim($R[0]);
								 $second = trim($R[1]);
								if(!is_numeric($first)  || !is_numeric($second)) 
								    { echo("error in assignment file in assignment number $AssignmentNumber");
									  echo "first is <br>$first<br> and second is <br>$second<br>";
									  $errorflag = 1;
									}
							    else if(($first % 2) == 0 || ($second % 2) == 0)
								    { $errorflag = 1;
									  echo("error in assignment file in assignment number $AssignmentNumber");
									  echo("After the word 'odds', expecting two odd numbers separated by a hyphen.");
									}
								else
								    { $countproblems += ($second - $first)/2 + 1;
									  $sql = $sql . "(ProblemNumber BETWEEN '$first' AND  '$second' AND (ProblemNumber % 2) == 1)";
									}
							  }
						   else if (substr($e,0,3) == "all")
						      { // as in  "evens 2-40"
							     $R =  trim(substr($e,3));
								if(!is_numeric($R) ) 
								    { echo("error in assignment file in assignment number $AssignmentNumber");
									  echo "Expecting a number after the word 'all'.";
									  $errorflag = 1;
									}
								else
								    { $countproblems += $R;
									  $sql = $sql . "(ProblemNumber BETWEEN '$first' AND  '$second' AND (ProblemNumber % 2) == 0)";
									}
							  }
						   else
						      { $R = explode("-",$e);
							    $first = trim($R[0]);
								$second = trim($R[1]);
								if(!is_numeric($first)  || !is_numeric($second)) 
								    { echo("error in assignment file in assignment number $AssignmentNumber");
									  echo "first is <br>$first<br> and second is <br>$second<br>";
									  $errorflag = 1;
									}
								else
								    { $countproblems += $second - $first + 1;
									  $sql = $sql . "(ProblemNumber BETWEEN '$first' AND  '$second')";
									}
							  }
							++$count;
					     }  // end processing this entry 
					 $sql = $sql . "))";   // close the parens opened 18 or 22 lines above here
					} // end processing this line 
		        }   // finished reading this assignment 
		   // echo("$sql <br>");
		   $NumberOfProblems[$AssignmentNumber] = $countproblems; 
		   $sqlquery = "SELECT Homework.StudentNumber, ProblemNumber, Topic, Filename FROM `Homework`,`Enrolled` WHERE (" . $sql . ") AND SubmissionDate <= '$DueDate' AND '$StartDate' <= SubmissionDate AND Enrolled.StudentNumber=Homework.StudentNumber AND Enrolled.ClassID='$CourseID'  ORDER BY Homework.StudentNumber;";
		   $sqllate =  "SELECT Homework.StudentNumber, ProblemNumber, Topic, Filename FROM `Homework`,`Enrolled` WHERE (" . $sql . ") AND SubmissionDate > '$DueDate'  AND '$StartDate' <= SubmissionDate AND Enrolled.StudentNumber=Homework.StudentNumber AND Enrolled.ClassID='$CourseID'  ORDER BY Homework.StudentNumber;";
		   // echo("Constructed query:<br>$sqllate<br>");
           $r = QueryWebGrades($sqlquery);
		   $rlate = QueryWebGrades($sqllate);
		   $problemsSolved = array();
		   $problemsSolvedLate = array();
		   $cnt = 0;
		   while($q =  $r->fetchRow())
		     { $problemsSolved[]=$q;
		        ++$cnt; 
			 }
		    // echo "$cnt on-time submissions for Assignment $AssignmentNumber <br>";
		   while($qlate = $rlate->fetchRow())
			 { $problemsSolvedLate[] = $qlate;
			 }
			// These data suffice for one column of the eventual table, but we still need percentages computed for each student.
		   $StudentNumber = 1;   
		   $countsolutions = 0;
		   foreach($problemsSolved as $record)
			   { if($record[0] != $StudentNumber)
				    { // starting a new student, so record the grade of the student we just finished with			   
					   $percent = (int)((100 * $countsolutions)/ $countproblems  + 0.5);
				 	   if($percent > 100) 
			                $percent = 100;  // initially there were some duplicate entries due to a bug in WebGrades.php, so we could get more than 100 percent.
					   $Grades[$AssignmentNumber][$StudentNumber] = $percent;
					   if($StudentNumber==200 && $AssignmentNumber==10) echo "percent on time is $percent";
					   if(!in_array($StudentNumber,$StudentNumbers))
					      { $StudentNumbers[] = $StudentNumber;
						  }
					   $StudentNumber = $record[0];
					   $countsolutions = 0;
					}
				 ++$countsolutions;
			    }
			// last student has still not been recorded
			$percent = (int)((100 * $countsolutions)/ $countproblems  + 0.5); 
			if($percent > 100) 
			   $percent = 100;  // initially there were some duplicate entries due to a bug in WebGrades.php, so we could get more than 100 percent.
			$Grades[$AssignmentNumber][$StudentNumber] = $percent;
			if(!in_array($StudentNumber,$StudentNumbers))
			    { $StudentNumbers[] = $StudentNumber;
				} 
			$StudentNumber = 1;
			$countsolutions = 0;
		
		    foreach($problemsSolvedLate as $record)
			   { if($record[0] != $StudentNumber)
				    { // starting a new student, so record the late grades of the previous student.			   
					   $percent = (int)((100 * $countsolutions)/ $countproblems  + 0.5);
				 	   if($percent > 100) 
			                $percent = 100;  // initially there were some duplicate entries due to a bug in WebGrades.php, so we could get more than 100 percent.
					   $percent = $LatePenalty * $percent;   // late problems count less
					   if(!isset($Grades[$AssignmentNumber][$StudentNumber]))
					       $Grades[$AssignmentNumber][$StudentNumber] = 0;
					   $Grades[$AssignmentNumber][$StudentNumber] += (int)($percent + 0.5);;
					   if(!in_array($StudentNumber,$StudentNumbers))
					      { $StudentNumbers[] = $StudentNumber;
						  }
					   $StudentNumber = $record[0];
					   $countsolutions = 0;
					}
				 ++$countsolutions;
			    }
			// last student has still not been recorded
			$percent = (int)((100 * $countsolutions)/ $countproblems  + 0.5); 
			if($percent > 100) 
			   $percent = 100;  // initially there were some duplicate entries due to a bug in WebGrades.php, so we could get more than 100 percent.
			$percent = $LatePenalty * $percent;   // late problems count less
			if(!isset($Grades[$AssignmentNumber][$StudentNumber]))
		       $Grades[$AssignmentNumber][$StudentNumber] = 0;
			$Grades[$AssignmentNumber][$StudentNumber] +=(int)($percent + 0.5);	
			if(!in_array($StudentNumber,$StudentNumbers))
			    { $StudentNumbers[] = $StudentNumber;
				} 
		}  
  return $Grades;
}
/*__________________________________________________*/					        
   
function dumpRawGrades($SecretNumber)
// display all the submissions of one student
{ $StudentNumber = getStudentNumber($SecretNumber);
  $sql = "SELECT * FROM `Homework` WHERE StudentNumber='$StudentNumber' ORDER BY SubmissionDate";
  $r = QueryWebGrades($sql);
  while( $row = $r->fetchRow())
     { foreach($row as $item)
          { echo "$item ";
          }
	   echo "<br>";
     }
}	      
/*__________________________________________________*/	
function TestSubmitHomework($SecretNumber, $Filename, $Topic, $ProblemNumber) 
// Store the given number in the webgrades database
// return 0 for success or 1 for invalid studentNumber
// or 2 if for some reason the database entry failed.

{  $SubmissionDate = date("Y-m-d");   // today's date  in format 2010-08-13  (SQL wants that format for insertion)
   $StudentNumber = (int) ($SecretNumber / 23);  // secret numbers are congruent to 17 mod 23.  StudentNumbers use all numbers starting at 1.
   $sql = "SELECT * FROM `Homework`  WHERE StudentNumber='$StudentNumber' AND Filename='$Filename' AND Topic='$Topic' AND ProblemNumber='$ProblemNumber';";   
   echo "$sql <br>";
   $r = QueryWebGrades($sql);
   if(is_string($r))
        return 2;
   $result = $r->fetchRow();
   if($result==NULL)  // it hasn't already been submitted  
      {  var_dump($result); echo "<br>";
	     $sql = "INSERT INTO `Homework` (Topic, Filename, SubmissionDate, StudentNumber, ProblemNumber) VALUES('$Topic','$Filename','$SubmissionDate','$StudentNumber','$ProblemNumber');";
         $r=QueryWebGrades($sql);  // insert this data into the database. 
		 if(is_string($r))
		      return 2; 
	  }
    return 0;  // If we get here, it succeeded
} 

/*_____________________________________________________*/
function PercentToLetterGrade($percent, $AssignmentNumber, $RequiredNumber)
// convert percentage grade to letter grade
// later the assignment file might specify how this is to be done.  For now we use a fixed formula.
{  
  global $NumberOfProblems;
  $TotalNumber = $NumberOfProblems[$AssignmentNumber];
  if($RequiredNumber != "all" && $RequiredNumber > 0)
      $percent = $percent * $TotalNumber / $RequiredNumber;
  // 98 is an A,  90 is a B,  82 is a C, 74 is a D, 66 is an F  
  // so y = (x-66)/8  for x between 66 and 100
  $LetterGrade = ($percent - 66) / 8;
  if($LetterGrade > 4.25) 
     $LetterGrade = 4.25;  
  if($LetterGrade < 0)
     $LetterGrade = 0;
  
  return number_format($LetterGrade,2);
}   

/*_____________________________________________________*/
function PercentToPortugeseGrade($percent, $AssignmentNumber, $RequiredNumber, $BestGrade)
// convert percentage grade to grade on scale 1 to $BestGrade, which is 20 in Portugal 
 
{  
  global $NumberOfProblems;
  $TotalNumber = $NumberOfProblems[$AssignmentNumber];
  if($percent == 0)
     return 0;
  $nproblemsworked = ($percent * $TotalNumber)/100;  // for example, 0.84 if you worked one problem and 42 are possible
  $nproblemsworked = (int) ($nproblemsworked + 0.5);  // for example, 1, the true number worked
  if ($RequiredNumber == "all")
      $RequiredNumber = $TotalNumber;
  $grade =  $nproblemsworked * $BestGrade / $RequiredNumber;
return $grade;
 }   
/*_________________________________________________________*/
function past($datestring)
// return true if the specified date is today or in the past
// otherwise return false
{ $today = date("Y-m-d");
  if ($datestring <= $today)
      return true;
  return false;
}	   
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Homework Grades</title>
</head>

<body>
<?php 
 // echo $RequiredNumbers[3]; echo "<br>";  
 //dumpRawGrades(40);
 $StudentNumbers = array();
 $Grades = getGrades( $ClassName, $Teacher, $School, $StartDate, $AssignmentFile); 
 sort($StudentNumbers);  // they may not be in order, e.g. if some students didn't submit the first assignment
 if($ShowLetterGrades=="yes")
 { 
?>

<p>
The following table shows the letter grades.  Two late submissions counts as one on-time submission.   To compute these grades, first we compute the percent of the possible problems you submitted.  In case, for example, the assignment said to work 25 out of 50 problems, and you did so,  you would have worked 50 percent.   But, you actually worked 100% of the number you were required to work.  Therefore, we adjust your 50% to 100%, and then compute the letter grade based on this adjusted percentage.</p>
<p> The conversion rule is, 98 is an A, 90 is a B, 82 is a C, 66 is an F.<br >
Between 66 and 100 the conversion is linear:  Letter grade = (percent - 66)/8. </p>
<p> The numbers shown are the standard University equivalents of letter grades: A = 4, B = 3, C = 2, D = 1, F = 0.  When these are displayed in alphabetic form, the range 2.7 to 3.3 is a B, the range 3.31 to 3.49 is B+, 3.5 to 3.69 is A-, 3.7 to 4.0 is A, and similarly for B-, C+, etc.   The highest possible grade is 4.25, which corresponds to 100 percent by the above formula.   The lowest possible grade is 0.   
 </p>
 <p> The last column shows the average of the other grades in that row <em>for assignments whose due dates are today or in the past</em>.
    Thus problems you may have already submitted that are due in the future do not enter into this calculation.  This is your homework grade up to and including today.
   <?php
 }
 else if (is_numeric($ShowLetterGrades) && $ShowLetterGrades > 0)
{
 ?> 
<p>
The following table shows the grade on each assignment,  where 20 is a perfect score, and 0 means no problems have been submitted.    Two late submissions counts as one on-time submission.  Percentages are rounded off to the nearest integer.  If you submit 
more than the required number of problems, you will see a score higher than 20. </p>
 <?php 
 }
 else
 {
 ?> 
<p>
The following table shows the percentage score  on each assignment.   These are percentages of the problems that could be submitted.  Two late submissions counts as one on-time submission.  Percentages are rounded off to the nearest integer.  </p>

 <p>In case it was not required to do all the problems, a low percentage score might still correspond to an A grade.  For example in Assignment 3, you only have to do 10 of 50 problems, so 20% will be an A.
 </p>
 <?php 
 }
 ?>

<table border="2">
<?php
// Now output the table 
// make a header row with the assignment numbers
echo "<tr>";
echo "<th bgcolor = '#FF0000'>Student \ Assignment</th>";
foreach($AssignmentNumbers as $AssignmentNumber)
    echo "<th bgcolor = '#FF0000'>$AssignmentNumber</th>";
if($ShowLetterGrades=="yes")
   { echo "<th bgcolor = '#FF0000'>Overall So Far </th>";
   }
echo "</tr>";
// now make a row for each student
$rownumber = 0;
foreach($StudentNumbers as $StudentNumber)
  { if($StudentNumber==1 && is_numeric($ShowLetterGrades))
       continue;  // don't show secret number 40 in Portugal
    $color = ($rownumber % 2 == 0) ? "#CCCCFF" : "#FFFFCC";
    echo "<tr align='center' bgcolor='$color'>";
	$SecretNumber = getSecretNumber($StudentNumber);
    echo "<td>  $SecretNumber   </td>";
	if($ShowLetterGrades == "yes")
	   { $total = 0.0;
	     $nPast = 0;
         foreach($AssignmentNumbers as $AssignmentNumber)
            { $percent = $Grades[$AssignmentNumber][$StudentNumber];
		      $grade = PercentToLetterGrade($percent, $AssignmentNumber, $RequiredNumbers[$AssignmentNumber]);
			  if(past($DueDates[$AssignmentNumber]))
			     { $total += $grade;
				   ++$nPast;
				 }
		      echo "<td width=40>  $grade  </td>";
            }
		  $total = number_format($total /$nPast,2);
		  echo "<td width=40> $total </td>";
	   }
	else if($ShowLetterGrades == "no")
	   {  // show percentage grades
         foreach($AssignmentNumbers as $AssignmentNumber)
             { $percent = $Grades[$AssignmentNumber][$StudentNumber];
		       echo "<td width=40>  $percent  </td>";
             }
       }
    else if(is_numeric($ShowLetterGrades) && $ShowLetterGrades > 0)
       { foreach($AssignmentNumbers as $AssignmentNumber)
             { $percent = $Grades[$AssignmentNumber][$StudentNumber];
               $required = $RequiredNumbers[$AssignmentNumber];
               $grade = PercentToPortugeseGrade($percent, $AssignmentNumber, $required, $ShowLetterGrades);
		       echo "<td width=40>  $grade </td>";
             }
       }
    echo "</tr>";
  } 
?>
</table>
 
  
</body>
</html>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists