Location: PHPKode > projects > Whiteboard > whiteboard1.0.3/program/restricted/students.php
<?php
/* Whiteboard Courseware System v1.0.3
   Copyright (C) 2002-2003 Todd Templeton

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 2 of the License, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
   more details.

   You should have received a copy of the GNU General Public License along with
   this program; if not, write to the Free Software Foundation, Inc., 59 Temple
   Place, Suite 330, Boston, MA 02111-1307 USA

   Contact info: my paper address changes often, but you can contact me at
   hide@address.com
*/


define ("MODULE", "students");
define ("PATHTOHTTPROOT", "../");

unset ($page);

require (PATHTOHTTPROOT . "include/include.php");
require (PATHTOHTTPROOT . "include/gradesinclude.php");


/* self link (address)
   returns generated string
   $action is an action (bool for none)
   $studentid is a student netid (bool for none)
   $assignmentid is an assignment id (bool for none)
*/
function selflink ($action, $studentid, $assignmentid) {
  global $course;
  global $semester;
  global $currentsemester;
  $link = "students.php?course=" . $course;
  if ($semester != $currentsemester)
    $link .= "&semester=" . $semester;
  if (!is_bool ($action) && !isemptystring ($action))
    $link .= "&action=" . $action;
  if (!is_bool ($studentid) && !isemptystring ($studentid))
    $link .= "&studentid=" . $studentid;
  if (!is_bool ($assignmentid) && !isemptystring ($assignmentid))
    $link .= "&assignmentid=" . $assignmentid;
  return $link;
}


/* adds link back to main students page to bottom of page as bottomlink
   returns nothing
*/
function backlink () {
  global $page;
  $page['bottomlink']['text'] = "Back To Students Page";
  $link = selflink (false, false, false);
  $page['bottomlink']['address'] = $link;
}


/* gets all grades
   returns associative array of grades indexed by studentid and then
     assignmentid--eliminates deleted students and assignments
   $assignments is an array of assignment info arrays
*/
function getallgrades ($assignments) {
  global $course;
  global $semester;
  $table = $course . "_" . $semester . "_grades as a";
  $table .= ", " . $course . "_" . $semester . "_students as b";
  $where = "a.netid = b.netid and b.deleted = '0'";
  $grades = getdbassoc ($table, "a.*", false, false, "netid");
  if (is_array ($grades)) {
    reset ($grades);
    while ($elementinfo = each ($grades)) {
      $studentid = $elementinfo['key'];
      if (!is_array ($elementinfo['value'])) {
	unset ($grades[$studentid]);
	continue;
      }
      while ($elementinfo2 = each ($elementinfo['value'])) {
	$assignmentid = $elementinfo2['key'];
	if (!is_array ($assignments[$assignmentid]))
	  unset ($grades[$studentid][$assignmentid]);
      }
      if (!is_array ($grades[$studentid]))
	unset ($grades[$studentid]);
      else if (count ($grades[$studentid]) <= 0)
	unset ($grades[$studentid]);
    }
    if (!is_array ($grades) || (count ($grades) <= 0))
      $grades = false;
  }
  return $grades;
}


/* clears student sort
   returns nothing
*/
function clearsort () {
  global $netid;
  global $semester;
  global $course;
  $table = $course . "_" . $semester . "_students_sort";
  if (!deleterows ($table, "netid = '" . $netid . "'")) {
    $errormsg = "Unable to clear students sort.";
    $detailedmsg = "failed query is '" . lastdbquery () . "'";
    fatalerror ($errormsg, $detailedmsg);
  }
}


/* stores student sort
   returns nothing
   $sort is a student sort array
*/
function storesort ($sort) {
  global $netid;
  global $semester;
  global $course;
  $table = $course . "_" . $semester . "_students_sort";
  if (!storevariable ($table, "netid", $netid, $sort)) {
    $errormsg = "Unable to store students sort.";
    $detailedmsg = "failed query is '" . lastdbquery () . "'";
    fatalerror ($errormsg, $detailedmsg);
  }
}


/* stores student
   returns nothing
   $vals are the values to be stored
   $overwrite is a student netid to overwrite (bool for none)
*/
function storestudent ($vals, $overwrite) {
  global $course;
  global $semester;
  global $studentoptions;
  $valueoptions = $studentoptions;
  $table = $course . "_" . $semester . "_students";
  if (!simplestorevals ($table, $vals, $valueoptions, "netid", $overwrite)) {
    $errormsg = "Unable to store student.";
    $detailedmsg = "storevals\(\) says: " . storevalserror ();
    fatalerror ($errormsg, $detailedmsg);
  }
  if (is_bool ($overwrite)) {
    $table = $course . "_" . $semester . "_grades";
    $where = "netid = '" . $vals['netid'] . "'";
    if (!rowsexist ($table, "netid", $where)) {
      $gradescolumns = getcolumns ($table);
      if (!is_array ($gradescolumns) || (count ($gradescolumns) < 1)) {
	$errormsg = "Unable to count assignment columns.";
	$detailedmsg = "trying to count columns in '" . $table . "'";
	$detailedmsg .= ", failed query is '" . lastdbquery () . "'";
	fatalerror ($errormsg, $detailedmsg);
      }
      $numassignments = count ($gradescolumns) - 1;
      if (!addemptyrow ($table, $vals['netid'], false, $numassignments)) {
        $errormsg = "Unable to add row to grades table.";
        $detailedmsg = "failed query is '" . lastdbquery () . "'";
        fatalerror ($errormsg, $detailedmsg);
      }
    }
  }
  else if ($overwrite != $vals['netid']) {
    $where = "netid = '" . $overwrite . "'";
    $table = $course . "_" . $semester . "_grades";
    if (!updateitem ($table, "netid", $vals['netid'], $where, true, true, false, false)) {
      $errormsg = "Unable to change grades netid.";
      $detailedmsg = "uptateitem\(\) says: " . updateitemerror ();
      fatalerror ($errormsg, $detailedmsg);
    }
    $table = $course . "_" . $semester . "_students_sort";
    if (!updateitem ($table, "netid", $vals['netid'], $where, true, true, false, false)) {
      $errormsg = "Unable to change students sort netid.";
      $detailedmsg = "updateitem\(\) says: " . updateitemerror ();
      fatalerror ($errormsg, $detailedmsg);
    }
    $table = "/\A" . $course . "_" . $semester . "_discussion_\d+_";
    $table .= "(?:(?:expanded)|(?:read))\Z/";
    if (!updateitem ($table, "netid", $vals['netid'], $where, true, true, false, 0)) {
      $errormsg = "Unable to change some discussion expanded/read netids.";
      $detailedmsg = "updateitem\(\) says: " . updateitemerror ();
      fatalerror ($errormsg, $detailedmsg);
    }
    $olddir = SUBMISSIONSDIR . "/" . $overwrite;
    $escapedolddir = escapeshellarg ($olddir);
    $newdir = SUBMISSIONSDIR . "/" . $vals['netid'];
    $escapednewdir = escapeshellarg ($newdir);
    if (file_exists ($olddir)) {
      if (file_exists ($newdir)) {
	$errormsg = "New submissions directory already exists.";
	$detailedmsg = "trying to move '" . $olddir . "' to '" . $newdir . "'";
	fatalerror ($errormsg, $detailedmsg);
      }
      @exec (MVCOMMAND . " " . $escapedolddir . " " . $escapednewdir);
      if (!file_exists ($newdir)) {
	$errormsg = "Unable to move submissions directory.";
	$detailedmsg = "trying to move '" . $olddir . "' to '" . $newdir . "'";
	fatalerror ($errormsg, $detailedmsg);
      }
    }
  }
}


/* checks values and stores student
   returns nothing
   $submitted are submitted values
   $studentid is a student netid to attempt to overwrite (bool for none)--
     creates new student if $studentid does not exist
*/
function checkandstorestudent ($submitted, $studentid) {
  global $studentoptions;
  global $course;
  global $semester;
  checkdbdata (&$submitted, false, $studentoptions, false, true);
  if (!is_string ($studentid) || isemptystring ($studentid)) {
    $submitted['numeric_grade'] = 100.000;
    $overwrite = false;
  }
  else if (!isstudent ($studentid, $course, $semester)) {
    $submitted['numeric_grade'] = 100.000;
    $overwrite = false;
  }
  else if (!is_array ($studentinfo = getstudentinfo ($studentid))) {
    $submitted['numeric_grade'] = 100.000;
    $overwrite = false;
  }
  else {
    $submitted['numeric_grade'] = $studentinfo['numeric_grade'];
    $overwrite = $studentid;
  }
  storestudent ($submitted, $overwrite);
}


/* stores individual grades
   returns nothing
   $grades is an associative array of grades indexed by student netid and then
     assignmentid
*/
function storegrades ($grades) {
  global $course;
  global $semester;
  if (!is_array ($grades))
    return;
  $query = "update " . $course . "_" . $semester  . "_grades set ";
  reset ($grades);
  while ($elementinfo = each ($grades)) {
    if (!is_array ($elementinfo['value']))
      continue;
    if (count ($elementinfo['value']) <= 0)
      continue;
    $studentid = $elementinfo['key'];
    $query2 = $query;
    reset ($elementinfo['value']);
    for ($i = 0; $elementinfo2 = each ($elementinfo['value']); $i++) {
      $assignmentid = $elementinfo2['key'];
      $grade = $elementinfo2['value'];
      if ($assignmentid == "netid") {
	$i--;
	continue;
      }
      if ($i != 0)
	$query2 .= ", ";
      $query2 .= $assignmentid . " = ";
      if (isemptystring ($grade))
	$query2 .= "NULL";
      else
	$query2 .= "'" . $grade . "'";
    }
    $query2 .= " where netid = '" . $studentid . "'";
    if (!dbquery ($query2)) {
      $errormsg = "Unable to change some student grades.";
      $detailedmsg = "failed query is '" . lastdbquery () . "'";
      fatalerror ($errormsg, $detailedmsg);
    }
  }
}


/* mark student as deleted
   returns nothing
   $netid is a student netid
*/
function deletestudent ($netid) {
  global $course;
  global $semester;
  global $time;
  $table = $course . "_" . $semester . "_students";
  $where = "netid = '" . $netid . "'";
  if (!markdeleted ($table, $where)) {
    $errormsg = "Unable to delete student.";
    $detailedmsg = "student " . $netid;
    $detailedmsg .= ", updateitem\(\) says: " . updateitemerror ();
    fatalerror ($errormsg, $detailedmsg);
  }
  $olddir = SUBMISSIONSDIR . "/" . $netid;
  $escapedolddir = escapeshellarg ($olddir);
  $newdir = $olddir . "#" . $time;
  $escapednewdir = escapeshellarg ($newdir);
  if (file_exists ($olddir)) {
    @exec (MVCOMMAND . " " . $escapedolddir . " " . $escapednewdir);
    if (!file_exists ($newdir)) {
      $errormsg = "Unable to delete student directory.";
      $detailedmsg = "trying to move '" . $olddir . "'";
      $detailedmsg .= " to '" . $newdir . "'";
      fatalerror ($errormsg, $detailedmsg);
    }
  }
}


/* prints students info table
   returns nothing
   $studentsinfo is an array of student info arrays
*/
function studentsinfotable ($studentsinfo) {
  global $options;
  global $studentoptions;
  global $priv;
  $table['title'] = "Basic Information";
  $table['tablewidth'] = "100%";
  if ($priv['isnonstudent'])
    $table['numcolumns'] = 7;
  else
    $table['numcolumns'] = 6;
  if ($priv['isnonstudent']) {
    $link['address'] = selflink ("addstudent", false, false);
    $link['text'] = "Add Student";
    $table['righttoplinks'][] = $link;
  }
  $link['address'] = selflink ("sort", false, false);
  $link['text'] = "Sort";
  $table['righttoplinks'][] = $link;
  $heading['align'] = "left";
  $heading['text'] = $studentoptions['netid']['description'];
  $table['headings'][] = $heading;
  $heading['text'] = $studentoptions['last_name']['description'];
  $table['headings'][] = $heading;
  $heading['text'] = $studentoptions['first_name']['description'];
  $table['headings'][] = $heading;
  $heading['text'] = $studentoptions['middle_name']['description'];
  $table['headings'][] = $heading;
  unset ($heading);
  $heading['text'] = $studentoptions['class_year']['description'];
  $table['headings'][] = $heading;
  $heading['text'] = $studentoptions['precept']['description'];
  $table['headings'][] = $heading;
  if ($priv['isnonstudent']) {
    $heading['text'] = $studentoptions['numeric_grade']['description'];
    $table['headings'][] = $heading;
  }
  if (is_array ($studentsinfo) && (count ($studentsinfo) > 0)) {
    reset ($studentsinfo);
    while ($elementinfo = each ($studentsinfo)) {
      $data = $elementinfo['value'];
      $student = $data['netid'];
      if ($priv['isnonstudent'])
        $row[0]['address'] = selflink ("editstudent", $student, false);
      $row[0]['text'] = htmlsafe ($student);
      $row[1]['text'] = htmlsafe ($data['last_name']);
      $row[2]['text'] = htmlsafe ($data['first_name']);
      $row[3]['text'] = htmlsafe ($data['middle_name']);
      $row[4]['align'] = "center";
      $row[4]['text'] = htmlsafe ($data['class_year']);
      $row[5]['align'] = "center";
      $row[5]['text'] = htmlsafe ($data['precept']);
      if ($priv['isnonstudent']) {
        $row[6]['align'] = "center";
        $row[6]['text'] = htmlsafe (decimalfix ($data['numeric_grade'], $options['floatround']));
      }
      $table['rows'][] = $row;
    }
  }
  else {
    $row[0]['text'] = "<em>No Students</em>";
    $row[0]['colspan'] = $table['numcolumns'];
    $table['rows'][] = $row;
  }
  prnt ("<tr><td>\n");
  prnt (printtable ($table));
  prnt ("</td></tr>\n");
}


/* prints students grades tables
   returns nothing
   $studentinfo is an array of student info arrays
   $grades is an associative array of grades indexed by student netid and then
     assignmentid
   $orderedassignments is the return value from makeorderedassignments ()
*/
function printgrades ($studentinfo, $grades, $orderedassignments) {
  global $options;
  global $gradesoptions;
  if (!is_array ($studentinfo) || (count ($studentinfo) <= 0))
    return;
  if (!is_array ($grades) || (count ($grades) <= 0))
    return;
  if (!is_array ($orderedassignments) || (count ($orderedassignments) <= 0))
    return;
  $table['title'] = "Grades";
  $table['tablewidth'] = "100%";
  $table['numcolumns'] = 6;
  $link['address'] = selflink ("sort", false, false);
  $link['text'] = "Sort";
  $table['righttoplinks'][] = $link;
  $table['widths'][0] = "10%";
  $table['widths'][1] = "18%";
  $table['widths'][2] = "18%";
  $table['widths'][3] = "18%";
  $table['widths'][4] = "18%";
  $table['widths'][5] = "18%";
  for ($i = 0; $i < count ($orderedassignments); $i++)
    $assignmentinfo[floor ($i / 5)][] = $orderedassignments[$i];
  for ($i = 0; $i < count ($assignmentinfo); $i++) {
    $columns = $assignmentinfo[$i];
    $count = count ($columns);
    unset ($table['rows']);
    unset ($table['headings']);
    if ($i != 0)
      prnt (EMPTYTABLEROW . "\n");
    unset ($heading);
    $heading['align'] = "left";
    $heading['text'] = $gradesoptions['netid']['description'];
    $table['headings'][] = $heading;
    unset ($heading);
    for ($j = 0; $j < $count; $j++) {
      $heading['address'] = selflink ("editgrades", false, $columns[$j]['assignment']);
      $heading['text'] = htmlsafe ($columns[$j]['name']);
      $table['headings'][] = $heading;
    }
    reset ($studentinfo);
    while ($elementinfo = each ($studentinfo)) {
      $student = $elementinfo['value'];
      unset ($row);
      $row[0]['address'] = selflink ("editgrades", $student['netid'], false);
      $row[0]['text'] = $student['netid'];
      for ($j = 0; $j < $count; $j++) {
        $grade = $grades[$student['netid']][$columns[$j]['assignment']];
        if (!isset ($grade) || isemptystring ($grade))
          $grade = "NG";
	else if ($columns[$j]['type'] == 0) {
	  if (isneginteger ($grade))
	    $grade = "Dropped";
	  else if (isposnzdecimal ($grade))
	    $grade = decimalfix ($grade, $options['floatround']);
	  else
	    $grade = integerfix ($grade);
	}
	else if (isemptystring ($grade) || !islettergrade ($grade))
	  $grade = "NG";
	$data['align'] = "center";
        $data['address'] = selflink ("editgrades", $student['netid'], $columns[$j]['assignment']);
        $data['text'] = htmlsafe ($grade);
	$row[] = $data;
      }
      $table['rows'][] = $row;
    }
    prnt ("<tr><td>\n");
    prnt (printtable ($table));
    prnt ("</td></tr>\n");
  }
}


/* dumps course grades into a text file
   returns nothing
   $studentinfo is an array of student info arrays
   $grades is an associative array of grades indexed by student netid and then
     assignmentid
   $orderedassignments is the return value from makeorderedassignments ()
*/
function gradesfile ($studentinfo, $grades, $orderedassignments) {
  global $options;
  global $studentinfofields;
  $studentsexist = true;
  if (!is_array ($studentinfo) || (count ($studentinfo) <= 0))
    $studentsexist = false;
  $gradesexist = true;
  if (!is_array ($grades) || (count ($grades) <= 0))
    $gradesexist = false;
  if (!is_array ($orderedassignments) || (count ($orderedassignments) <= 0))
    $gradesexist = false;
  $string = "";
  reset ($studentinfofields);
  while ($elementinfo = each ($studentinfofields))
    $string .= $elementinfo['value'] . "/";
  if ($gradesexist) {
    for ($i = 0; $i < count ($orderedassignments); $i++)
      $string .= $orderedassignments[$i]['name'] . "/";
  }
  $string .= "\n";
  if ($studentsexist) {
    reset ($studentinfo);
    while ($elementinfo = each ($studentinfo)) {
      $studentid = $elementinfo['value']['netid'];
      reset ($studentinfofields);
      for ($i = 0; $elementinfo2 = each ($studentinfofields); $i++) {
	if ($i != 0)
	  $string .= "/";
        $string .= $studentinfo[$studentid][$elementinfo2['key']];
      }
      if ($gradesexist) {
        for ($i = 0; $i < count ($orderedassignments); $i++) {
	  $assignmentid = $orderedassignments[$i]['assignment'];
	  $grade = $grades[$studentid][$assignmentid];
          if (!isset ($grade) || isemptystring ($grade))
            $grade = "NG";
	  else if ($orderedassignments[$i]['type'] == 0) {
	    if (isneginteger ($grade))
	      $grade = "Dropped";
	    else if (isposnzdecimal ($grade))
	      $grade = decimalfix ($grade, $options['floatround']);
	    else
	      $grade = integerfix ($grade);
	  }
	  else if (isemptystring ($grade) || !islettergrade ($grade))
	    $grade = "NG";
          $string .= "/" . $grade;
        }
      }
      $string .= "\n";
    }
  }
  return $string;
}


/* prints students sort page
   returns nothing
   $orderedassignments is the return value from makeorderedassignments ()
   $oldsort is the current students sort array
*/
function sortpage ($orderedassignments, $oldsort) {
  global $studentinfofields;
  global $options;
  global $priv;
  $i = 0;
  $selectoptions[$i]['value'] = "";
  $selectoptions[$i]['text'] = "&nbsp;";
  $i++;
  reset ($studentinfofields);
  while ($elementinfo = each ($studentinfofields)) {
    if (($elementinfo['key'] == "numeric_grade") && !$priv['isnonstudent'])
      continue;
    if ($elementinfo['key'] == "deleted")
      continue;
    $selectoptions[$i]['value'] = $elementinfo['key'];
    $selectoptions[$i]['text'] = $elementinfo['value'];
    $i++;
  }
  if (is_array ($orderedassignments) && $priv['isnonstudent']) {
    for ($j = 0; $j < count ($orderedassignments); $j++) {
      $selectoptions[$i]['value'] = $orderedassignments[$j]['assignment'];
      $selectoptions[$i]['text'] = $orderedassignments[$j]['name'];
      $i++;
    }
  }
  $i--;
  if ($i > 10)
    $i = 10;
  for ($j = 0; $j < $i; $j++) {
    $sortoptions[$j]['name'] = "sort[" . $j . "]";
    if (isset ($oldsort[$j]))
      $sortoptions[$j]['default'] = $oldsort[$j];
    else
      $sortoptions[$j]['default'] = "";
    $sortoptions[$j]['formtype'] = 2;
    if ($j == 0)
      $sortoptions[$j]['description'] = "First";
    else
      $sortoptions[$j]['description'] = "Then";
    $sortoptions[$j]['value'] = $selectoptions;
  }
  $link = selflink (false, false, false);
  configtable ("Sort By", false, $sortoptions, $link, false);
}


/* creates assignment info string (name, numeric/letter, number of points if
     applicable
   returns generated string
   $assignmentinfo is an assignment info array
*/
function assignmentinfostring ($assignmentinfo) {
  $info = $assignmentinfo['name'] . " (";
  if ($assignmentinfo['type'] == 0) {
    $info .= "Numeric, ";
    if (isposinteger ($assignmentinfo['num_points']))
      $info .= $assignmentinfo['num_points'] . " Points";
  }
  else
    $info .= "Letter";
  $info .= ")";
  return $info;
}


if (isemptystring ($netid)) {
  $options = getdefaults ($courseconfigoptions);
  loginpage (courselink ("students.php", false));
}
if (isemptystring ($course))
  inputerror ("Unknown course.");
if ($priv['isvisitor'])
  inputerror ("You do not have permission to access this module.");
$options = getcourseoptions ($department, $coursenumber);
if (!is_array ($options)) {
  $errormsg = "No course options available.";
  fatalerror ($errormsg, false);
}
if (!isset ($action))
  $action = "";
else if (($action != "addstudent") && ($action != "editstudent")) {
  if (($action != "storestudent") && ($action != "deletestudent")) {
    if (($action != "editgrades") && ($action != "storegrades")) {
      if (($action != "sort") && ($action != "gradesfile")) {
	if (($action != "filestorestudent") && ($action != "filedeletestudent"))
          $action = "";
      }
    }
  }
}
$assignments = getassignments ();
$categories = getcategories ();
$orderedassignments = makeorderedassignments ($assignments, $categories);
/*store student(s) from file*/
if ($action == "filestorestudent") {
  if (!$priv['isnonstudent'])
    inputerror ("You do not have permission to store students.");
  if (!isset ($submitted['file']) || isemptystring ($submitted['file']))
    inputerror ("No file uploaded.");
  if (!file_exists ($submitted['file']))
    inputerror ("No file uploaded.");
  if (!is_array ($file = @file ($submitted['file']))) {
    @unlink ($submitted['file']);
    $errormsg = "Unable to read file contents.";
    $detailedmsg = "trying to read '" . $submitted['file'] . "'";
    fatalerror ($errormsg, $detailedmsg);
  }
  for ($i = 0; $i < count ($file); $i++) {
    $file[$i] = rtrim ($file[$i]);
    if (isemptystring ($file[$i]))
      continue;
    $line = explode ("/", $file[$i]);
    unset ($submitted);
    $j = 0;
    reset ($studentoptions);
    while ($elementinfo = each ($studentoptions)) {
      $key = $elementinfo['key'];
      $value = $elementinfo['value'];
      if (isset ($value['form']) && !$value['form'])
	continue;
      if (isset ($line[$j]))
	$submitted[$key] = $line[$j];
      else
	$submitted[$key] = "";
      $j++;
    }
    checkandstorestudent ($submitted, trim ($submitted['netid']));
  }
  @unlink ($submitted['file']);
  $action = "";
}
/*store student*/
if ($action == "storestudent") {
  if (!$priv['isnonstudent'])
    inputerror ("You do not have permission to store a student.");
  checkandstorestudent ($submitted, $studentid);
  if ($more)
    $action = "addstudent";
  else
    $action = "";
}
/*delete student(s) from file*/
if ($action == "filedeletestudent") {
  if (!$priv['isnonstudent'])
    inputerror ("You do not have permission to delete students.");
  if (!isset ($submitted['file']) || isemptystring ($submitted['file']))
    inputerror ("No file uploaded.");
  if (!file_exists ($submitted['file']))
    inputerror ("No file uploaded.");
  if (!is_array ($file = @file ($submitted['file']))) {
    @unlink ($submitted['file']);
    $errormsg = "Unable to read file contents.";
    $detailedmsg = "trying to read '" . $submitted['file'] . "'";
    fatalerror ($errormsg, $detailedmsg);
  }
  for ($i = 0; $i < count ($file); $i++) {
    $file[$i] = explode ("/", $file[$i]);
    $file[$i] = trim ($file[$i][0]);
    if (!isemptystring ($file[$i])) {
      if (isstudent ($file[$i], $course, $semester))
	deletestudent ($file[$i]);
      else
        registererror ($file[$i] . " is not a student in this class.", 0);
    }
  }
  @unlink ($submitted['file']);
  $action = "";
}
/*delete student*/
if ($action == "deletestudent") {
  if (!$priv['isnonstudent'])
    inputerror ("You do not have permission to delete a student.");
  if (!isset ($studentid) || isemptystring ($studentid))
    inputerror ("Invalid studentid to delete.");
  if (!isstudent ($studentid, $course, $semester))
    inputerror ("Unknown studentid to delete.");
  deletestudent ($studentid);
  $action = "";
}
/*store grades*/
if ($action == "storegrades") {
  if (!$priv['isnonstudent'])
    inputerror ("You do not have permission to store grades.");
  if (!is_array ($submitted) || (count ($submitted) <= 0))
    inputerror ("No grades to store.");
  $studentinfo = getallstudentinfo (false, false);
  if (isset ($studentid)) {
    if (isemptystring ($studentid) || !is_array ($studentinfo[$studentid]))
      unset ($studentid);
  }
  if (isset ($assignmentid)) {
    if (isemptystring ($assignmentid))
      unset ($assignmentid);
    else if (!is_array ($assignments[$assignmentid]))
      unset ($assignmentid);
  }
  if (!isset ($studentid) && isset ($assignmentid))
    $action = "storeassignmentgrades";
  else if (isset ($studentid) && !isset ($assignmentid))
    $action = "storestudentgrades";
  else if (isset ($studentid) && isset ($assignmentid))
    $action = "storespecificgrade";
  else
    inputerror ("Unable to determine what grades to store.");
}
/*store grades by assignment*/
if ($action == "storeassignmentgrades") {
  unset ($realgrades);
  reset ($studentinfo);
  while ($elementinfo = each ($studentinfo)) {
    $studentid = $elementinfo['value']['netid'];
    if (!isset ($submitted[$studentid][$assignmentid])) {
      $realgrades[$studentid][$assignmentid] = "";
      continue;
    }
    $grade = $submitted[$studentid][$assignmentid];
    if ($assignments[$assignmentid]['type'] == 0) {
      if (isemptystring ($grade) || isnumber ($grade))
        $realgrades[$studentid][$assignmentid] = $grade;
    }
    else {
      if (isemptystring ($grade) || islettergrade ($grade))
        $realgrades[$studentid][$assignmentid] = $grade;
    }
  }
  if (isset ($realgrades)) {
    storegrades ($realgrades);
    updateallnumericgrades ($assignments, $categories, false);
  }
  $action = "";
}
/*store grades by student*/
if ($action == "storestudentgrades") {
  unset ($realgrades);
  reset ($assignments);
  while ($elementinfo = each ($assignments)) {
    $assignmentid = $elementinfo['value']['assignment'];
    if (!isset ($submitted[$studentid][$assignmentid])) {
      $realgrades[$studentid][$assignmentid] = "";
      continue;
    }
    $grade = $submitted[$studentid][$assignmentid];
    if ($assignments[$assignmentid]['type'] == 0) {
      if (isemptystring ($grade) || isnumber ($grade))
        $realgrades[$studentid][$assignmentid] = $grade;
    }
    else {
      if (isemptystring ($grade) || islettergrade ($grade))
        $realgrades[$studentid][$assignmentid] = $grade;
    }
  }
  if (isset ($realgrades)) {
    storegrades ($realgrades);
    updatenumericgrade ($studentid, $assignments, $categories, false);
  }
  $action = "";
}
/*store specific grade*/
if ($action == "storespecificgrade") {
  if (isset ($submitted[$studentid][$assignmentid]))
    $grade = $submitted[$studentid][$assignmentid];
  else
    $grade = "";
  unset ($realgrades);
  if ($assignments[$assignmentid]['type'] == 0) {
    if (isemptystring ($grade) || isnumber ($grade))
      $realgrades[$studentid][$assignmentid] = $grade;
  }
  else {
    if (isemptystring ($grade) || islettergrade ($grade))
      $realgrades[$studentid][$assignmentid] = $grade;
  }
  if (isset ($realgrades)) {
    storegrades ($realgrades);
    updatenumericgrade ($studentid, $assignments, $categories, false);
  }
  $action = "";
}
if (is_array ($sort))
  storesort ($sort);
else
  $sort = getsort ();
$studentinfo = getallstudentinfo ($sort, $assignments);
$grades = getallgrades ($assignments);
if (isset ($studentid) && !isemptystring ($studentid))
  $studentgrades = getgrades ($studentid);
$page['browsertitle'] = "Students";
$page['title'] = "Students";
/*edit student*/
if ($action == "editstudent") {
  if (!$priv['isnonstudent'])
    inputerror ("You do not have permission to edit a student.");
  if (!isset ($studentid) || isemptystring ($studentid))
    inputerror ("Invalid studentid to edit.");
  if (!is_array ($studentinfo[$studentid]))
    inputerror ("Unknown studentid to edit.");
  setdefaults ($studentinfo[$studentid], &$studentoptions);
  $toplinks = false;
  $toplinks[0]['address'] = selflink ("deletestudent", $studentid, false);
  $toplinks[0]['text'] = "Delete Student";
  $link = selflink ("storestudent", $studentid, false);
  configtable ("Edit Student", $toplinks, $studentoptions, $link, false);
  prnt (EMPTYTABLEROW . "\n");
  prnt (EMPTYTABLEROW . "\n");
  $valueoptions['file']['name'] = "submitted[file]";
  $valueoptions['file']['formtype'] = 6;
  $valueoptions['file']['description'] = "Data File";
  $valueoptions['file']['default'] = "";
  $valueoptions['file']['value'] = "";
  $link = selflink ("filedeletestudent", false, false);
  configtable ("Delete Multiple Students", false, $valueoptions, $link, false);
  backlink ();
}
/*add student*/
if ($action == "addstudent") {
  if (!$priv['isnonstudent'])
    inputerror ("You do not have permission to add a student.");
  $link = selflink ("storestudent", false, false);
  configtable ("Add Student", false, $studentoptions, $link, true);
  prnt (EMPTYTABLEROW . "\n");
  prnt (EMPTYTABLEROW . "\n");
  $valueoptions['file']['name'] = "submitted[file]";
  $valueoptions['file']['formtype'] = 6;
  $valueoptions['file']['description'] = "Data File";
  $valueoptions['file']['default'] = "";
  $valueoptions['file']['value'] = "";
  $link = selflink ("filestorestudent", false, false);
  configtable ("Add Multiple Students", false, $valueoptions, $link, false);
  backlink ();
}
/*edit grades*/
if ($action == "editgrades") {
  if (!$priv['isnonstudent'])
    inputerror ("You do not have permission to edit grades.");
  if (isset ($studentid)) {
    if (isemptystring ($studentid) || !is_array ($studentinfo[$studentid]))
      unset ($studentid);
  }
  if (isset ($assignmentid)) {
    if (isemptystring ($assignmentid))
      unset ($assignmentid);
    else if (!is_array ($assignments[$assignmentid]))
      unset ($assignmentid);
  }
  if (!isset ($studentid) && isset ($assignmentid))
    $action = "editassignmentgrades";
  else if (isset ($studentid) && !isset ($assignmentid))
    $action = "editstudentgrades";
  else if (isset ($studentid) && isset ($assignmentid))
    $action = "editspecificgrade";
  else
    inputerror ("Unable to determine what grades to edit.");
}
/*edit grades by assignment*/
if ($action == "editassignmentgrades") {
  reset ($studentinfo);
  while ($elementinfo = each ($studentinfo)) {
    $student = $elementinfo['value'];
    $studentid = $student['netid'];
    $gradesinfo[$studentid]['name'] = "submitted[" . $studentid . "]";
    $gradesinfo[$studentid]['name'] .= "[" . $assignmentid . "]";
    $gradesinfo[$studentid]['description'] = $studentid;
    $gradesinfo[$studentid]['formtype'] = 4;
    $gradesinfo[$studentid]['default'] = $grades[$studentid][$assignmentid];
    $gradesinfo[$studentid]['value'] = "";
  }
  $info = htmlsafe (assignmentinfostring ($assignments[$assignmentid]));
  $link = selflink ("storegrades", false, $assignmentid);
  configtable ($info, false, $gradesinfo, $link, false);
  backlink ();
}
/*edit grades by student*/
if ($action == "editstudentgrades") {
  for ($i = 0; $i < count ($orderedassignments); $i++) {
    $assignmentinfo = $orderedassignments[$i];
    $assignment = $assignmentinfo['assignment'];
    $info = htmlsafe (assignmentinfostring ($assignmentinfo));
    $gradesinfo[$assignment]['name'] = "submitted[" . $studentid . "]";
    $gradesinfo[$assignment]['name'] .= "[" . $assignment . "]";
    $gradesinfo[$assignment]['description'] = $info;
    $gradesinfo[$assignment]['formtype'] = 4;
    $gradesinfo[$assignment]['default'] = $grades[$studentid][$assignment];
    $gradesinfo[$assignment]['value'] = "";
  }
  $link = selflink ("storegrades", $studentid, false);
  configtable ($studentid, false, $gradesinfo, $link, false);
  backlink ();
}
/*edit specific grade*/
if ($action == "editspecificgrade") {
  $assignmentinfo = $assignments[$assignmentid];
  $info = htmlsafe (assignmentinfostring ($assignmentinfo));
  $gradesinfo[$assignmentid]['name'] = "submitted[" . $studentid . "]";
  $gradesinfo[$assignmentid]['name'] .= "[" . $assignmentid . "]";
  $gradesinfo[$assignmentid]['description'] = $info;
  $gradesinfo[$assignmentid]['formtype'] = 4;
  $gradesinfo[$assignmentid]['default'] = $grades[$studentid][$assignmentid];
  $gradesinfo[$assignmentid]['value'] = "";
  $link = selflink ("storegrades", $studentid, $assignmentid);
  configtable ($studentid, false, $gradesinfo, $link, false);
  backlink ();
}
/*dump grades into text file*/
if ($action == "gradesfile") {
  if (!$priv['isnonstudent'])
    inputerror ("You do not have permission to make a grades file.");
  $filename = $course . "_" . $semester . "_grades.txt";
  header ("Cache-control: private");
  header ("Content-Type: text/plain; name=\"" . $filename . "\"");
  header ("Content-Disposition: ; filename=\"" . $filename . "\"");
  $gradesfile = gradesfile ($studentinfo, $grades, $orderedassignments);
  header ("Content-Length: " . strlen ($gradesfile));
  print ($gradesfile);
  exit ();
}
/*students sort page*/
if ($action == "sort") {
  sortpage ($orderedassignments, $sort);
  backlink ();
}
/*main students page*/
if (isemptystring ($action)) {
  if ($priv['isnonstudent']) {
    $page['toplinks'][0]['address'] = selflink ("gradesfile", false, false);
    $page['toplinks'][0]['text'] = "Get Grades File";
    $page['toplinks'][0]['target'] = "_blank";
  }
  studentsinfotable ($studentinfo);
  if ($priv['isnonstudent']) {
    prnt (EMPTYTABLEROW . "\n");
    printgrades ($studentinfo, $grades, $orderedassignments);
  }
}
printpage ($page);
?>
Return current item: Whiteboard