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

unset ($page);

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


/* self link (address)
   returns generated string
   $linkboard is a board id (false for none, true to use global $board)
   $linkaction is an action (false for none, true to use global $action)
   $linkmsgid is a massage id (false for none, true to use global $msgid)
   $switchsort is whether to switch the sort option
   $switchorder is whether to switch the order option
   $switchshow is whether to switch the show option
*/
function selflink ($linkboard, $linkaction, $linkmsgid, $switchsort, $switchorder, $switchshow) {
  global $course;
  global $semester;
  global $currentsemester;
  global $board;
  global $sort;
  global $order;
  global $show;
  global $action;
  global $msgid;
  $link = "discussion.php?course=" . $course;
  if ($semester != $currentsemester)
    $link .= "&semester=" . $semester;
  if (!is_bool ($linkboard))
    $link .= "&board=" . $linkboard;
  else if ($linkboard) {
    $link .= "&board=" . $board;
    if (!is_bool ($linkmsgid))
      $link .= "&msgid=" . $linkmsgid;
    else if ($linkmsgid && isset ($msgid) && !isemptystring ($msgid))
      $link .= "&msgid=" . $msgid;
  }
  if (!is_bool ($linkaction))
    $link .= "&action=" . $linkaction;
  else if ($linkaction && isset ($action) && !isemptystring ($action))
    $link .= "&action=" . $action;
  if ($switchsort && isset ($sort) && ($sort != "date"))
    $link .= "&sort=date";
  if (!$switchsort && isset ($sort) && ($sort == "date"))
    $link .= "&sort=date";
  if ($switchorder && isset ($order) && ($order != "asc"))
    $link .= "&order=asc";
  if (!$switchorder && isset ($order) && ($order == "asc"))
    $link .= "&order=asc";
  if ($switchshow && isset ($show) && ($show != "unread"))
    $link .= "&show=unread";
  if (!$switchshow && isset ($show) && ($show == "unread"))
    $link .= "&show=unread";
  return $link;
}


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


/* adds link back to discussion board select page to bottom of page as
     bottomlink
   returns nothing
*/
function backlink2 () {
  global $page;
  $page['bottomlink']['text'] = "Back To Discussion Board List";
  $link = selflink (false, false, false, false, false, false);
  $page['bottomlink']['address'] = $link;
}


/* stores discussion board
   $vals are the vals to store
   $overwrite is a discussion board id to overwrite (bool for none)
*/
function storeboard ($vals, $overwrite) {
  global $course;
  global $semester;
  global $discussionmessageoptions;
  global $discussionboardoptions;
  global $discussionreadoptions;
  global $discussionexpandedoptions;
  $valueoptions = $discussionboardoptions;
  $table = $course . "_" . $semester . "_discussionboards";
  $where = "name = '" . $vals['name'] . "'";
  if (!is_bool ($overwrite))
    $where .= " and id != '" . $overwrite . "'";
  $where .= " and deleted = '0'";
  if (rowsexist ($table, "*", $where))
    inputerror ("Discussion board with same name already exists.");
  if (is_bool ($overwrite))
    $vals['id'] = "";
  else
    $vals['id'] = $overwrite;
  if (!simplestorevals ($table, $vals, $valueoptions, "id", $overwrite)) {
    $errormsg = "Unable to store discussion board.";
    if (is_bool ($overwrite))
      $detailedmsg = "new board";
    else
      $detailedmsg = "board " . $overwrite;
    $detailedmsg .= ", storevals\(\) says: " . storevalserror ();
    fatalerror ($errormsg, $detailedmsg);
  }
  if (is_bool ($overwrite)) {
    $where = "name = '" . $vals['name'] . "' and deleted = '0'";
    $row = getdbfirstmatch ($table, "id", $where);
    if (!is_array ($row)) {
      $errormsg = "Discussion board not added to table.";
      fatalerror ($errormsg, false);
    }
    $id = $row['id'];
    $table2 = $course . "_" . $semester . "_discussion_" . $id;
    if (!maketable ($table2, $discussionmessageoptions, "(id)", "fulltext (subject, body, netid, attachment_name)", false)) {
      $errormsg = "Unable to create discussion board table.";
      $detailedmsg = "board '" . $id . "'";
      $detailedmsg = ", failed query is '" . lastdbquery () . "'";
      fatalerror ($errormsg, $detailedmsg);
    }
    if (!simplemaketable ($table2 . "_read", $discussionreadoptions, "(netid, id)")) {
      $errormsg = "Unable to create discussion board read table.";
      $detailedmsg = "board '" . $id . "'";
      $detailedmsg = ", failed query is '" . lastdbquery () . "'";
      fatalerror ($errormsg, $detailedmsg);
    }
    if (!simplemaketable ($table2 . "_expanded", $discussionexpandedoptions, "(netid)")) {
      $errormsg = "Unable to create discussion board expanded table.";
      $detailedmsg = "board '" . $id . "'";
      $detailedmsg = ", failed query is '" . lastdbquery () . "'";
      fatalerror ($errormsg, $detailedmsg);
    }
    if (!makedirectory (DISCUSSIONDOCUMENTSDIR, $id)) {
      $errormsg = "Unable to create discussion board attachments directory.";
      $detailedmsg = "board '" . $id . "'";
      $detailedmsg = ", tried to create ";
      $detailedmsg .= "'" . DISCUSSIONDOCUMENTSDIR . "/" . $id . "'";
      fatalerror ($errormsg, $detailedmsg);
    }
  }
  return true;
}


/* marks discussion board as deleted
   returns nothing
   $board is a discussion board id
*/
function deleteboard ($board) {
  global $course;
  global $semester;
  $table = $course . "_" . $semester . "_discussionboards";
  $where = "id = '" . $board . "'";
  if (!markdeleted ($table, $where)) {
    $errormsg = "Unable to delete discussion board.";
    $detailedmsg = "updateitem\(\) says: " . updateitemerror (); 
    fatalerror ($errormsg, $detailedmsg);
  }
}


/* retrieves expanded array from database
   returns expanded array
*/
function getexpanded () {
  global $netid;
  global $board;
  global $semester;
  global $course;
  $table = $course . "_" . $semester . "_discussion_" . $board . "_expanded";
  $where = "netid = '" . $netid . "'";
  return getstoredvariable ($table, "expanded", $where);
}


/* clears expanded array in database
   returns nothing
   $board is a discussion board id
*/
function clearexpanded ($board) {
  global $netid;
  global $semester;
  global $course;
  $table = $course . "_" . $semester . "_discussion_" . $board . "_expanded";
  if (!deleterows ($table, "netid = '" . $netid . "'")) {
    $errormsg = "Unable to delete expanded data.";
    $detailedmsg = "board " . $board;
    $detailedmsg .= ": failed query is '" . lastdbquery () . "'";
    fatalerror ($errormsg, $detailedmsg);
  }
}


/* stores expanded array in database
   returns nothing
   $expanded is an expanded array
*/
function storeexpanded ($expanded) {
  global $netid;
  global $board;
  global $semester;
  global $course;
  $table = $course . "_" . $semester . "_discussion_" . $board . "_expanded";
  if (!storevariable ($table, "netid", $netid, $expanded)) {
    $errormsg = "Unable to store expanded data.";
    $detailedmsg = "board " . $board;
    $detailedmsg .= ": failed query is '" . lastdbquery () . "'";
    fatalerror ($errormsg, $detailedmsg);
  }
}


/* determines whether a message is read
   returns true/false (returns false on error)
   $msgid is a message id
*/
function isread ($msgid) {
  global $netid;
  global $board;
  global $course;
  global $semester;
  $table = $course . "_" . $semester . "_discussion_" . $board . "_read";
  $where = "netid = '" . $netid . "' and id = '" . $msgid . "'";
  return rowsexist ($table, "id", $where);
}


/* marks message as read
   returns nothing
   $msgid is a message id
*/
function markread ($msgid) {
  global $netid;
  global $board;
  global $course;
  global $semester;
  if (isread ($msgid))
    return;
  $table = $course . "_" . $semester . "_discussion_" . $board . "_read";
  $query = "insert into " . $table . " values ('" . $netid . "'";
  $query .= ", '" . $msgid . "')";
  if (!dbquery ($query)) {
    $errormsg = "Unable to mark message as read.";
    $detailedmsg = "board " . $board . ", msgid " . $msgid;
    fatalerror ($errormsg, $detailedmsg);
  }
}


/* determines number of messages in a discussion board
   returns an integer number
   $board is a discussion board id
*/
function nummessages ($board) {
  global $course;
  global $semester;
  $table = $course . "_" . $semester . "_discussion_" . $board;
  return numrows ($table, "id", false);
}


/* determines the number of messages in a discussion board read by a user
   returns an integer number
   $board is a discussion board id
*/
function numreadmessages ($board) {
  global $netid;
  global $course;
  global $semester;
  $table = $course . "_" . $semester . "_discussion_" . $board;
  $table .= " as a, " . $table . "_read as b";
  $where = "a.id = b.id and b.netid = '" . $netid . "'";
  return numrows ($table, "a.id", $where);
}


/* determines the threadparent of a message
   returns a message number (false on error)
   $msgid is a message id
*/
function threadparent ($msgid) {
  global $course;
  global $semester;
  global $board;
  if (!isset ($msgid) || !isposinteger ($msgid))
    return false;
  $table = $course . "_" . $semester . "_discussion_" . $board;
  $where = "id = '" . $msgid . "' and deleted = '0'";
  $row = getdbfirstmatch ($table, "threadparent", $where);
  if (is_array ($row))
    return $row['threadparent'];
  return false;
}


/* makes tree out of discussion board messages
   returns flat array of msgids corresponding to threads
   $messages is an associative array (indexed by msgid) of indices into
     $sortedmessages
   $sortedmessages is a sorted flat array of message info arrays (pass this by
     reference--will add 'parent' and 'children' (flat array of message id's of
     child messages) elements
*/
function maketree ($messages, $sortedmessages) {
  $threads = false;
  for ($i = 0; $i < count ($sortedmessages); $i++) {
    if (isneginteger ($sortedmessages[$i]['parent']))
      $threads[] = $sortedmessages[$i]['id'];
    else if (isset ($messages[$sortedmessages[$i]['parent']])) {
      $msgindex = $messages[$sortedmessages[$i]['parent']];
      $sortedmessages[$msgindex]['children'][] = $sortedmessages[$i]['id'];
    }
    else {
      global $board;
      $errormsg = "Found message with no parent.";
      $detailedmsg = "board " . $board;
      $detailedmsg .= ", msgid " . $sortedmessages[$i]['id'];
      fatalerror ($errormsg, $detailedmsg);
    }
  }
  return $threads;
}


/* recursive function called by makeflatmessages ()
   returns nothing
   $root is current tree root
   $messages is an associative array (indexed by msgid) of indices into
     $sortedmessages
   $sortedmessages is a sorted flat array of message info arrays including the
     'children' element
   $flatmessages should be passed by reference--will contain flat array of
     message info arrays in order suitable for display in discussion board
     (these message info arrays also contain a 'depth' element for depth from
     their thread root)
   $useexpanded is whether to check whether message is expanded
   $depth is current depth
*/
function makeflatmessagesrec ($root, $messages, $sortedmessages, $flatmessages, $useexpanded, $depth) {
  $msgindex = $messages[$root];
  $sortedmessages[$msgindex]['depth'] = $depth;
  $flatmessages[] = $sortedmessages[$msgindex];
  if (!is_array ($sortedmessages[$msgindex]['children']))
    return;
  if ($useexpanded && !isexpanded ($sortedmessages[$msgindex]['id']))
    return;
  $children = $sortedmessages[$msgindex]['children'];
  for ($i = 0; $i < count ($children); $i++)
    makeflatmessagesrec ($children[$i], $messages, $sortedmessages, &$flatmessages, $useexpanded, $depth + 1);
}


/* transforms message tree into flat structure suitable for discussion board
   returns flat array of message info arrays in order suitable for display in
     discussion board (these message info arrays also contain a 'depth' element
     for depth from their thread root)
   $messages is an associative array (indexed by msgid) of indices into
     $sortedmessages
   $sortedmessages is a sorted flat array of message info arrays including the
     'children' element
   $useexpanded is whether to check whether message is expanded
*/
function makeflatmessages ($root, $messages, $sortedmessages, $useexpanded) {
  makeflatmessagesrec ($root, $messages, $sortedmessages, &$flatmessages, $useexpanded, 0);
  return $flatmessages;
}


/* prints discussion board list
   returns nothing
   $boardinfo is the return value from getboards ()
*/
function boardlist ($boardinfo) {
  global $options;
  global $priv;
  $colors[0] = $options['tablecolor1'];
  $colors[1] = $options['tablecolor2'];
  if ($priv['isnonstudent']) {
    prnt ("<tr><td>");
    prnt ("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">\n");
    prnt ("<tr><td align=\"left\" class=\"tabletoplink\">");
    $linktext = "New Discussion Board";
    $link = selflink (false, "newboard", false, false, false, false);
    prnt (linktags ($link, $linktext, "tabletoplink"));
    prnt ("</td></tr>");
    prnt ("</table>");
    prnt ("</td></tr>\n");
  }
  prnt ("<tr><td bgcolor=\"" . $options['tablebordercolor'] . "\">\n");
  prnt ("<table border=\"0\" cellspacing=\"0\" cellpadding=\"10\" width=\"100%\">\n");
  reset ($boardinfo);
  for ($i = 0; $elementinfo = each ($boardinfo); $i++) {
    $key = $elementinfo['key'];
    $value = $elementinfo['value'];
    $j = $i % 2;
    clearexpanded ($key);
    prnt ("<tr><td bgcolor=\"" . $colors[$j] . "\" class=\"discussionboardlink\">");
    $linktext = $value['name'];
    $link = selflink ($key, false, false, false, false, false);
    prnt (linktags ($link, $linktext, "discussionboardlink"));
    prnt ("&nbsp;&nbsp;<span class=\"tabledata\">[" . $value['nummessages'] . " " . plural ($value['nummessages'], "message", "messages") . ", " . $value['numunreadmessages'] . " unread]</span>");
    prnt ("</td></tr>\n");
    prnt ("<tr><td bgcolor=\"" . $colors[$j] . "\">");
    prnt ("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"right\" width=\"95%\">");
    prnt ("<tr><td class=\"tablemessage\">" . $value['description'] . "</td></tr>");
    prnt ("<tr><td>&nbsp;</td></tr>");
    prnt ("</table>");
    prnt ("</td></tr>\n");
  }
  prnt ("</table>\n");
  prnt ("</td></tr>\n");
}


/* prints discussion board search box
   returns nothing
   $options is options array
*/
function searchbox ($options) {
  prnt ("<tr><td>");
  prnt ("<table align=\"right\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\">");
  prnt ("<tr><td bgcolor=\"" . $options['tablebordercolor'] . "\">");
  prnt ("<table align=\"center\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\">");
  prnt ("<tr><td bgcolor=\"" . $options['tablecolor2'] . "\">");
  prnt ("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
  prnt ("<form action=\"" . selflink (true, "search", false, false, false, false));
  prnt ("\" method=POST enctype=\"multipart/form-data\">");
  prnt ("<tr><td bgcolor=\"" . $options['tablecolor2'] . "\" align=\"center\">");
  prnt ("<input type=\"text\" size=\"15\" name=\"search\"></td></tr>");
  prnt ("<tr><td bgcolor=\"" . $options['tablecolor2'] . "\" align=\"center\">");
  prnt ("<input type=\"submit\" name=\"searchbutton\" value=\"Search\"></td></tr>");
  prnt ("</form>");
  prnt ("</table>");
  prnt ("</td></tr>");
  prnt ("</table>");
  prnt ("</td></tr>");
  prnt ("</table>");
  prnt ("</td></tr>");
}


/* determines whether a message is expanded
   returns true/false (returns false on error)
   $msgid is a message id
*/
function isexpanded ($msgid) {
  global $expanded;
  global $newexpanded;
  if (is_array ($expanded) && is_bool ($expanded[$msgid]) && $expanded[$msgid]) {
    $newexpanded[$msgid] = true;
    return true;
  }
  if (is_bool ($expanded) && $expanded) {
    $newexpanded[$msgid] = true;
    return true;
  }
  return false;
}


/* generates one row of discussion board message list
   returns table row (returns false if row should not appear)
   $message is a message info array
   $isexpanded is whether the message is expanded
   $msgid is a message id for a message that should appear in bold
*/
function printmessagerow ($message, $isexpanded, $msgid) {
  global $sort;
  global $show;
  if (!is_bool ($msgid) && ($show == "unread") && ($message['id'] == $msgid))
    return false;
  $string = "<table border=\"0\" align=\"left\" cellspacing=\"0\" cellpadding=\"0\">";
  $string .= "<tr>";
  if ($sort != "date") {
    $string .= "<td><a name=\"msg" . $message['id'] . "\"></a>";
    if (isset ($message['depth'])) {
      for ($i = 0; $i < $message['depth']; $i++)
	$string .= imagetag ("tree0.gif");
    }
    if (isset ($message['children'])) {
      if ($isexpanded) {
	$link = selflink (true, true, true, false, false, false) . "&collapse=" . $message['id'] . "#msg" . $message['id'];
	$string .= linktags ($link, imagetag ("tree2.gif"), false);
      }
      else {
	$link = selflink (true, true, true, false, false, false) . "&expand=" . $message['id'] . "#msg" . $message['id'];
	$string .= linktags ($link, imagetag ("tree3.gif"), false);
      }
    }
    else
      $string .= imagetag ("tree1.gif");
    $string .= "</td>";
  }
  $string .= "<td>";
  if (!is_bool ($msgid) && ($message['id'] == $msgid)) {
    $message['isread'] = true;
    $row[0]['text'] = $string;
    $row[0]['text'] .= "<b class=\"tabledata\">" . $message['subject'] . "</b>";
    $string2 = "";
  }
  else {
    $row[0]['beforelink'] = $string;
    $row[0]['beforelink'] .= "<span class=\"tabledata\">";
    $row[0]['nospacebeforelink'] = true;
    $row[0]['nospaceafterlink'] = true;
    $row[0]['address'] = selflink (true, "viewmsg", $message['id'], false, false, false);
    $row[0]['text'] = htmlsafe ($message['subject']);
    $string2 = "</span>";
  }
  $string2 .= "</td>";
  if (isset ($message['attachment_name']) && !isemptystring ($message['attachment_name'])) {
    $string2 .= "<td>&nbsp;</td>";
    $string2 .= "<td>" . imagetag ("clip.gif") . "</td>";
  }
  if (isset ($message['isread']) && !$message['isread']) {
    $string2 .= "<td>&nbsp;</td>";
    $string2 .= "<td>" . imagetag ("unread.gif") . "</td>";
  }
  $string2 .= "</tr>";
  $string2 .= "</table>";
  if (!is_bool ($msgid) && ($message['id'] == $msgid))
    $row[0]['text'] .= $string2;
  else
    $row[0]['afterlink'] = $string2;
  $row[1]['text'] = htmlsafe ($message['netid']);
  $row[2]['text'] = htmlsafe ($message['date']);
  return $row;
}


/* prints discussion board message list
   returns nothing
   $messages is an associative array (indexed by msgid) of indices into
     $sortedmessages
   $sortedmessages is a sorted flat array of message info arrays including the
     'depth' and 'children' elements
   $threads is a flat array of message id's corresponding to threads
   $msgid is a message id for a message that should appear in bold
*/ 
function viewboard ($messages, $sortedmessages, $threads, $msgid) {
  global $sort;
  global $order;
  global $show;
  global $expanded;
  global $discussionmessageoptions;
  $table['tablewidth'] = "100%";
  $table['numcolumns'] = 3;
  if ($sort == "thread") {
    $link['address'] = selflink (true, "newmsg", false, false, false, false);
    $link['text'] = "New Thread";
    $table['lefttoplinks'][] = $link;
    $link['address'] = selflink (true, true, true, false, false, false);
    if ((is_array ($expanded) && (count ($expanded) > 0)) || (is_bool ($expanded) && $expanded)) {
      $link['address'] .= "&collapse=all";
      $link['text'] = "Collapse";
    }
    else {
      $link['address'] .= "&expand=all";
      $link['text'] = "Expand";
    }
    $link['text'] .= " All";
    $table['lefttoplinks'][] = $link;
  }
  $link['address'] = selflink (true, true, true, false, false, true);
  if ($show == "all")
    $link['text'] = "Unread";
  else
    $link['text'] = "All";
  $link['text'] .= " Messages";
  $table['righttoplinks'][] = $link;
  if ($show == "all") {
    $link['address'] = selflink (true, true, true, true, false, false);
    $link['text'] = "Sort By ";
    if ($sort == "date")
      $link['text'] .= "Thread";
    else
      $link['text'] .= "Date";
    $table['righttoplinks'][] = $link;
  }
  $link['address'] = selflink (true, true, true, false, true, false);
  if ($order == "asc")
    $link['text'] = "Descending";
  else
    $link['text'] = "Ascending";
  $table['righttoplinks'][] = $link;
  $table['widths'][0] = "55%";
  $table['widths'][1] = "15%";
  $table['widths'][2] = "30%";
  $heading['align'] = "left";
  $heading['text'] = "Message";
  $table['headings'][] = $heading;
  $heading['text'] = $discussionmessageoptions['netid']['description'];
  $table['headings'][] = $heading;
  $heading['text'] = $discussionmessageoptions['date']['description'];
  $table['headings'][] = $heading;
  if ($sort == "date") {
    for ($i = 0; $i < count ($sortedmessages); $i++) {
      $row = printmessagerow ($sortedmessages[$i], false, $msgid);
      if (is_array ($row) && (count ($row) > 0))
	$table['rows'][] = $row;
    }
  }
  else if (is_array ($threads)) {
    for ($i = 0; $i < count ($threads); $i++) {
      $flatmessages = makeflatmessages ($threads[$i], $messages, $sortedmessages, true);
      for ($j = 0; $j < count ($flatmessages); $j++) {
        $isexpanded = isexpanded ($flatmessages[$j]['id']);
        $row = printmessagerow ($flatmessages[$j], $isexpanded, $msgid);
	if (is_array ($row) && (count ($row) > 0))
	  $table['rows'][] = $row;
      }
    }
  }
  if (!is_array ($table['rows']) || (count ($table['rows']) <= 0)) {
    $table['rows'][0][0]['colspan'] = 3;
    $table['rows'][0][0]['text'] = "<em>No Messages</em>";
  }
  prnt ("<tr><td>\n");
  prnt (printtable ($table));
  prnt ("</td></tr>\n");
}


/* prints a message
   returns nothing
   $message is a message info array
   $toplinks is whether to include top links
*/
function printmsg ($message, $toplinks) {
  global $priv;
  $lefttoplinks = false;
  if ($toplinks) {
    $link['address'] = selflink (true, "reply", $message['id'], false, false, false);
    $link['text'] = "Reply";
    $lefttoplinks[] = $link;
    if ($priv['isnonstudent']) {
      $link['address'] = selflink (true, "deletemsg", $message['id'], false, false, false);
      $link['text'] = "Delete";
      $lefttoplinks[] = $link;
    }
    $link['address'] = selflink (true, "viewthread", $message['id'], false, false, false);
    $link['text'] = "View Thread";
    $lefttoplinks[] = $link;
  }
  $link['address'] = selflink (true, false, false, false, false, false);
  $link['address'] .= "#msg" . $message['id'];
  $link['text'] = "Back To Discussion Board";
  $righttoplinks[] = $link;
  $titles[0] = "Title";
  $titles[1] = "Message";
  $titles[2] = "Attachment";
  $announcement = db2announcement ($message, false);
  announcement ($announcement, $titles, $lefttoplinks, $righttoplinks);
}


if (defined ("ENABLEDISCUSSIONBOARD") && !ENABLEDISCUSSIONBOARD) {
  if (!$priv['isnonstudent'])
    inputerror ("This module is disabled.");
}
if (isemptystring ($netid)) {
  $options = getdefaults ($courseconfigoptions);
  loginpage (courselink ("discussion.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 != "deleteboard") && ($action != "storeboard")) {
  if (($action != "editboard") && ($action != "newboard")) {
    if (($action != "storemsg") && ($action != "deletemsg")) {
      if (($action != "newmsg") && ($action != "search")) {
        if (($action != "viewmsg") && ($action != "viewthread")) {
	  if ($action != "reply")
	    $action = "";
	}
      }
    }
  }
}
if (!isset ($sort) || (($sort != "date") && ($sort != "thread")))
  $sort = "thread";
if (!isset ($order) || (($order != "asc") && ($order != "desc")))
  $order = "desc";
if (!isset ($show) || (($show != "all") && ($show != "unread")))
  $show = "all";
if ($show == "unread")
  $sort = "date";
/*delete discussion board*/
if ($action == "deleteboard") {
  if (!$priv['isnonstudent'])
    inputerror ("You do not have permission to delete a discussion board.");
  if (!isset ($board) || !isposinteger ($board))
    inputerror ("Invalid board to delete.");
  if (!isboard ($board))
    inputerror ("Unknown board to delete.");
  deleteboard ($board);
  $action = "";
}
/*store discussion board*/
if ($action == "storeboard") {
  if (!$priv['isnonstudent'])
    inputerror ("You do not have permission to store a discussion board.");
  checkdbdata (&$submitted, false, $discussionboardoptions, false, true);
  if (isset ($board) && isposinteger ($board) && isboard ($board))
    $overwrite = $board;
  else
    $overwrite = false;
  storeboard ($submitted, $overwrite);
  if ($more)
    $action = "newboard";
  else
    $action = "";
}
$boardinfo = getboards ($course);
if (!is_array ($boardinfo))
  $numboards = 0;
else
  $numboards = count ($boardinfo);
if ($numboards == 1) {
  reset ($boardinfo);
  $elementinfo = each ($boardinfo);
  $board = $elementinfo['key'];
}
$page['browsertitle'] = "Discussion Board";
$page['title'] = "Discussion Board";
/*edit discussion board*/
if ($action == "editboard") {
  if (!$priv['isnonstudent'])
    inputerror ("You do not have permission to edit a discussion board.");
  if (!isset ($board) || !isposinteger ($board))
    inputerror ("Invalid board to edit.");
  if (!isset ($boardinfo[$board]))
    inputerror ("Unknown board to edit.");
  $page['subtitle'] = $boardinfo[$board]['name'];
  setdefaults ($boardinfo[$board], &$discussionboardoptions);
  $toplinks = false;
  $toplinks[0]['text'] = "Delete Board";
  $link = selflink (true, "deleteboard", false, false, false, false);
  $toplinks[0]['address'] = $link;
  $link = selflink (true, "storeboard", false, false, false, false);
  configtable ("Edit Discussion Board", $toplinks, $discussionboardoptions, $link, false);
  backlink ();
}
/*new discussion board*/
else if ($action == "newboard") {
  if (!$priv['isnonstudent'])
    inputerror ("You do not have permission to add a discussion board.");
  $link = selflink (false, "storeboard", false, false, false, false);
  configtable ("New Discussion Board", false, $discussionboardoptions, $link, true);
  backlink2 ();
}
else if ($numboards == 0) {
  $action = "noboardsexist";
  if ($priv['isnonstudent']) {
    $page['toplinks'][0]['text'] = "New Discussion Board";
    $link = selflink (false, "newboard", false, false, false, false);
    $page['toplinks'][0]['address'] = $link;
  }
  else
    prnt ("<tr><td class=\"pagetext\">No Discussion Boards</td></tr>\n");
}
/*discussion board list*/
else if (!isset ($board) || !isposinteger ($board) || !isset ($boardinfo[$board])) {
  $action = "boardlist";
  boardlist ($boardinfo);
}
else
  $page['subtitle'] = $boardinfo[$board]['name'];
/*store message*/
if ($action == "storemsg") {
  if (defined ("DEMOMODE") && DEMOMODE && !$priv['isnonstudent'])
    inputerror ("Message posting is disabled.");
  if (($semester != $currentsemester) && !$priv['isnonstudent'])
    inputerror ("Discussion board changes only allowed for current semester.");
  checkdbdata (&$submitted, false, $discussionmessageoptions, false, true);
  if (isset ($parent) && isposinteger ($parent) && is_array (getmessageinfo ($parent)))
    $submitted['parent'] = $parent;
  else
    $submitted['parent'] = -1;
  if (!isemptystring ($submitted['attachment']) && file_exists ($submitted['attachment'])) {
    $submitted['attachment_name'] = $submitted_name['attachment'];
    $submitted['attachment_size'] = $submitted_size['attachment'];
  }
  else {
    $submitted['attachment_name'] = "";
    $submitted['attachment_size'] = "";
  }
  addmsg ($submitted);
  if (isposinteger ($submitted['parent']))
    $expand = $submitted['parent'];
  $action = "";
}
/*delete message*/
if ($action == "deletemsg") {
  if (!$priv['isnonstudent'])
    inputerror ("You do not have permission to delete a message.");
  if (!isposinteger ($msgid))
    inputerror ("Invalid msgid to delete.");
  unset ($messages);
  $sortedmessages = getmessages (false, &$messages, false, false, true);
  maketree ($messages, &$sortedmessages);
  if (!isset ($messages[$msgid]))
    inputerror ("Invalid msgid to delete.");
  deletemsg ($msgid, $messages, $sortedmessages);
  $action = "";
}
/*new message*/
if ($action == "newmsg") {
  if (defined ("DEMOMODE") && DEMOMODE && !$priv['isnonstudent'])
    inputerror ("Message posting is disabled.");
  if (($semester != $currentsemester) && !$priv['isnonstudent'])
    inputerror ("Discussion board changes only allowed for current semester.");
  $link = selflink (true, "storemsg", false, false, false, false);
  configtable ("New Thread", false, $discussionmessageoptions, $link, false);
  backlink ();
}
/*discussion board search*/
if ($action == "search") {
  if (defined ("DISCUSSIONSEARCH") && !DISCUSSIONSEARCH)
    inputerror ("Discussion search disabled.");
  if (!isset ($search) || isemptystring ($search))
    inputerror ("Invalid search string.");
  unset ($messages);
  $sortedmessages = getmessages (false, &$messages, false, true, false);
  if (!is_array ($sortedmessages) || (count ($sortedmessages) <= 0))
    prnt ("<tr><td class=\"tabledata\">No Matches or Matches Include More Than Half of Messages</td></tr>\n");
  else
    viewthread ($sortedmessages, false);
}
/*view message, view thread*/
if (($action == "viewmsg") || ($action == "viewthread")) {
  if (!isposinteger ($msgid))
    inputerror ("Invalid msgid to view.");
  if (isset ($messages[$msgid]))
    $messageinfo = $messages[$msgid];
  else if (!is_array ($messageinfo = getmessageinfo ($msgid)))
    inputerror ("Unknown msgid to view.");
  $threadparent = threadparent ($msgid);
  if (is_bool ($threadparent))
    inputerror ("Invalid msgid to view.");
  if (!isposinteger ($threadparent))
    $threadparent = $msgid;
  unset ($messages);
  $sortedmessages = getmessages ($threadparent, &$messages, false, false, false);
  $threads = maketree ($messages, &$sortedmessages);
  if ($action == "viewmsg") {
    $expanded = getexpanded ();
    if (isset ($expand)) {
      if (isposinteger ($expand) && isset ($messages[$expand]))
        $expanded[$expand] = true;
      else if ($expand == "all")
	$expanded = true;
    }
    if (isset ($collapse)) {
      if (isposinteger ($collapse))
        unset ($expanded[$collapse]);
      else if ($collapse == "all")
	$expanded = false;
    }
    $noneexpanded = true;
    for ($i = 0; $i < count ($threads); $i++) {
      if (isexpanded ($threads[$i]) && isset ($sortedmessages[$messages[$threads[$i]]]['children'])) {
        $noneexpanded = false;
	break;
      }
    }
    if ($noneexpanded)
      $expanded = false;
    $newexpanded = false;
    printmsg ($messageinfo, true);
    prnt (EMPTYTABLEROW . "\n");
    $onethread[0] = $threadparent;
    viewboard ($messages, $sortedmessages, $onethread, $msgid);
    if ($sort == "thread")
      storeexpanded ($newexpanded);
  }
  else {
    if ($sort == "thread") {
      $flatmessages = makeflatmessages ($threadparent, $messages, $sortedmessages, false);
      viewthread ($flatmessages, false);
    }
    else
      viewthread ($sortedmessages, $threadparent);
  }
}
/*reply*/
if ($action == "reply") {
  if (defined ("DEMOMODE") && DEMOMODE && !$priv['isnonstudent'])
    inputerror ("Message posting is disabled.");
  if (($semester != $currentsemester) && !$priv['isnonstudent'])
    inputerror ("Discussion board changes only allowed for current semester.");
  if (!isposinteger ($msgid))
    inputerror ("Invalid msgid to reply to.");
  unset ($messsages);
  $sortedmessages = getmessages (false, &$messages, false, false, false);
  if (isset ($messages[$msgid]))
    $messageinfo = $sortedmessages[$messages[$msgid]];
  else if (!is_array ($messageinfo = getmessageinfo ($msgid)))
    inputerror ("Invalid msgid to reply to.");
  printmsg ($messageinfo, false);
  prnt (EMPTYTABLEROW . "\n");
  $substr = substr ($messageinfo['subject'], 0, 3);
  if (strtolower ($substr) == "re:")
    $discussionmessageoptions['subject']['default'] = $messageinfo['subject'];
  else
    $discussionmessageoptions['subject']['default'] = "Re: " . $messageinfo['subject'];
  $link = selflink (true, "storemsg", false, false, false, false);
  $link .= "&parent=" . $msgid;
  configtable ("Reply", false, $discussionmessageoptions, $link, false);
}
/*main discussion board page (message list)*/
if (isemptystring ($action)) {
  if ($priv['isnonstudent']) {
    $page['toplinks'][0]['text'] = "New Discussion Board";
    $link = selflink (false, "newboard", false, false, false, false);
    $page['toplinks'][0]['address'] = $link;
    $page['toplinks'][1]['text'] = "Edit Discussion Board Options";
    $link = selflink (true, "editboard", false, false, false, false);
    $page['toplinks'][1]['address'] = $link;
  }
  if (!defined ("DISCUSSIONSEARCH") || DISCUSSIONSEARCH) {
    searchbox ($options);
    prnt (EMPTYTABLEROW . "\n");
  }
  unset ($messages);
  $sortedmessages = getmessages (false, &$messages, false, false, false);
  $threads = maketree ($messages, &$sortedmessages);
  $expanded = getexpanded ();
  if (isset ($expand)) {
    if (isposinteger ($expand) && isset ($messages[$expand]))
      $expanded[$expand] = true;
    else if ($expand == "all")
      $expanded = true;
  }
  if (isset ($collapse)) {
    if (isposinteger ($collapse))
      unset ($expanded[$collapse]);
    else if ($collapse == "all")
      $expanded = false;
  }
  $noneexpanded = true;
  for ($i = 0; $i < count ($threads); $i++) {
    if (isexpanded ($threads[$i]) && isset ($sortedmessages[$messages[$threads[$i]]]['children'])) {
      $noneexpanded = false;
      break;
    }
  }
  if ($noneexpanded)
    $expanded = false;
  $newexpanded = false;
  viewboard ($messages, $sortedmessages, $threads, false);
  if ($sort == "thread")
    storeexpanded ($newexpanded);
}
printpage ($page);
?>
Return current item: Whiteboard