Location: PHPKode > projects > Whiteboard > whiteboard1.0.3/program/restricted/administrators.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", "administrators");
define ("PATHTOHTTPROOT", "../");

unset ($options);
unset ($page);

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


/* self link (address)
   returns generated string
   $action is an action (bool for none)
   $adminid is an administrator netid (bool for none)
*/
function selflink ($action, $adminid) {
  $link = "administrators.php";
  if (!is_bool ($action) && !isemptystring ($action))
    $link .= "?action=" . $action;
  if (!is_bool ($adminid) && !isemptystring ($adminid)) {
    if (!is_bool ($action) && !isemptystring ($action))
      $link .= "&";
    else
      $link .= "?";
    $link .= "adminid=" . $adminid;
  }
  return $link;
}


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


/* gets all administrator info
   returns an associative array (indexed by netid) of administrator info arrays
*/
function getalladministratorinfo () {
  global $administratoroptions;
  $table = "administrators";
  $where = "deleted = '0'";
  $administrators = getdbassoc ($table, "*", $where, "netid", "netid");
  if (is_array ($administrators)) {
    $valueoptions = $administratoroptions;
    reset ($administrators);
    while ($elementinfo = each ($administrators)) {
      $key = $elementinfo['key'];
      checkdbdata (&$administrators[$key], "netid", $valueoptions, "administrator", false);
    }
  }
  return $administrators;
}


/* add administrator
   returns nothing
   $vals are the vals to be stored
   $overwrite is the administrator netid to overwrite (bool for none)
*/
function storeadmin ($vals, $overwrite) {
  global $administratoroptions;
  $errors = false;
  $valueoptions = $administratoroptions;
  $table = "administrators";
  $vals['deleted'] = 0;
  if (!simplestorevals ($table, $vals, $valueoptions, "netid", $overwrite)) {
    $errormsg = "Unable to store administrator.";
    $detailedmsg = "storevals\(\) says: " . storevalserror ();
    fatalerror ($errormsg, $detailedmsg);
  }
  if (!is_bool ($overwrite) && ($overwrite != $vals['netid'])) {
    $where = "netid = '" . $overwrite . "'";
    $table = "/\A[A-Z]{" . DEPARTMENTLENGTH . "}\d+_" . SEMESTERREGEX;
    $table .= "_students_sort\Z/";
    if (!updateitem ($table, "netid", $vals['netid'], $where, true, true, false, 0)) {
      $errormsg = "Unable to change students sort netid.";
      $detailedmsg = "updateitem\(\) says: " . updateitemerror ();
      fatalerror ($errormsg, $detailedmsg);
    }
    $table = "/\A[A-Z]{" . DEPARTMENTLENGTH . "}\d+_" . SEMESTERREGEX;
    $table .= "_discussion_\d+_(?:(?: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);
    }
  }
}


/* mark administrator as deleted
   returns nothing
   $adminid is the netid of the administrator
*/
function deleteadmin ($adminid) {
  $table = "administrators";
  $where = "netid = '" . $adminid . "'";
  if (!markdeleted ($table, $where)) {
    $errormsg = "Unable to delete administrator.";
    $detailedmsg = "updateitem\(\) says: " . updateitemerror (); 
    fatalerror ($errormsg, $detailedmsg);
  }
}


/* prints administrator table
   returns nothing
   $admininfo is an associative array (indexed by netid) of administrator info
     arrays
*/
function administratorinfotable ($admininfo) {
  global $administratoroptions;
  global $priv;
  $table['tablewidth'] = "100%";
  $table['numcolumns'] = 4;
  if ($priv['isadmin']) {
    $link['address'] = selflink ("addadmin", false);
    $link['text'] = "Add Administrator";
    $table['righttoplinks'][] = $link;
  }
  $table['widths'][0] = "10%";
  $table['widths'][1] = "30%";
  $table['widths'][2] = "20%";
  $table['widths'][3] = "40%";
  $heading['align'] = "left";
  $heading['text'] = $administratoroptions['netid']['description'];
  $table['headings'][] = $heading;
  $heading['text'] = $administratoroptions['last_name']['description'];
  $table['headings'][] = $heading;
  $heading['text'] = $administratoroptions['first_name']['description'];
  $table['headings'][] = $heading;
  $heading['text'] = $administratoroptions['office']['description'];
  $table['headings'][] = $heading;
  if (is_array ($admininfo) && (count ($admininfo) > 0)) {
    reset ($admininfo);
    while ($elementinfo = each ($admininfo)) {
      $data = $elementinfo['value'];
      $adminid = $data['netid'];
      if ($priv['isadmin'])
        $row[0]['address'] = selflink ("editadmin", $adminid);
      $row[0]['text'] = $adminid;
      $row[1]['text'] = htmlsafe ($data['last_name']);
      $row[2]['text'] = htmlsafe ($data['first_name']);
      $row[3]['text'] = htmlsafe ($data['office']);
      $table['rows'][] = $row;
    }
  }
  else {
    $row[0]['text'] = "<em>No Administrators</em>";
    $row[0]['colspan'] = 4;
    $table['rows'][] = $row;
  }
  prnt ("<tr><td>\n");
  prnt (printtable ($table));
  prnt ("</td></tr>\n");
}


$options = getdefaults ($courseconfigoptions);
/*add first administrator if it doesn't exist*/
if (!rowsexist ("administrators", "netid", "deleted = '0'")) {
  unset ($submitted);
  $submitted['netid'] = FIRSTADMINISTRATORNETID;
  $submitted['last_name'] = FIRSTADMINISTRATORLASTNAME;
  $submitted['first_name'] = FIRSTADMINISTRATORFIRSTNAME;
  $submitted['office'] = FIRSTADMINISTRATOROFFICE;
  checkdbdata (&$submitted, false, $administratoroptions, false, true);
  storeadmin ($submitted, false);
  if (!isemptystring ($netid) && ($netid == FIRSTADMINISTRATORNETID)) {
    $priv['isadmin'] = true;
    $priv['isnonstudent'] = true;
  }
  $action = "";
}
if (isemptystring ($netid))
  loginpage (courselink ("administrators.php", false));
if (!$priv['isnonstudent'])
  inputerror ("You do not have permission to access this module.");
if (!isset ($action))
  $action = "";
else if (($action != "addadmin") && ($action != "editadmin")) {
  if (($action != "storeadmin") && ($action != "deleteadmin"))
    $action = "";
}
/*store administrator*/
if ($action == "storeadmin") {
  if (!$priv['isadmin'])
    inputerror ("You do not have permission to store an administrator.");
  checkdbdata (&$submitted, false, $administratoroptions, false, true);
  if (!isset ($adminid) || isemptystring ($adminid) || !isadmin ($adminid))
    $overwrite = false;
  else
    $overwrite = $adminid;
  storeadmin ($submitted, $overwrite);
  if ($more)
    $action = "addadmin";
  else
    $action = "";
}
/*delete administrator*/
if ($action == "deleteadmin") {
  if (!$priv['isadmin'])
    inputerror ("You do not have permission to delete an administrator.");
  if (!isset ($adminid) || isemptystring ($adminid) || !isadmin ($adminid))
    inputerror ("Invalid adminid to delete.");
  deleteadmin ($adminid);
  $action = "";
}
$admininfo = getalladministratorinfo ();
$page['browsertitle'] = "Administrators";
$page['title'] = "Administrators";
/*edit administrator*/
if ($action == "editadmin") {
  if (!$priv['isadmin'])
    inputerror ("You do not have permission to edit an administrator.");
  if (!isset ($adminid) || isemptystring ($adminid))
    $action = "addadmin";
  else if (!is_array ($admininfo) || !is_array ($admininfo[$adminid]))
    $action = "addadmin";
  else {
    setdefaults ($admininfo[$adminid], &$administratoroptions);
    $toplinks = false;
    $toplinks[0]['address'] = selflink ("deleteadmin", $adminid, false);
    $toplinks[0]['text'] = "Delete Administrator";
    $title = "Edit Administrator";
    $storelink = selflink ("storeadmin", $adminid);
    configtable ($title, $toplinks, $administratoroptions, $storelink, false);
    backlink ();
  }
}
/*add administrator*/
if ($action == "addadmin") {
  if (!$priv['isadmin'])
    inputerror ("You do not have permission to add an administrator.");
  $title = "Add Administrator";
  $storelink = selflink ("storeadmin", false);
  configtable ($title, false, $administratoroptions, $storelink, true);
  backlink ();
}
/*main administrator page*/
if (isemptystring ($action))
  administratorinfotable ($admininfo);
printpage ($page);
?>
Return current item: Whiteboard