Search:

# CWIS Developers Documentation

- Main Page
- Classes
- Files

- File List
- File Members

# Forum.php

Go to the documentation of this file.

    00001 <?PHP
    00002 #
    00003 #   FILE:  SPT--Forum.php
    00004 #
    00005 #   FUNCTIONS PROVIDED:
    00006 #       Forum->Forum($ForumId)
    00007 #           - constructor
    00008 #       Forum->ForumId()
    00009 #       Forum->ForumName()
    00010 #       Forum->ForumDescription()
    00011 #       Forum->TopicCount()
    00012 #       Forum->MessageCount()
    00013 #       Forum->ModeratorId()
    00014 #           - methods to retrieve resource attributes
    00015 #
    00016 #   Part of the Scout Portal Toolkit
    00017 #   Copyright 2002 Internet Scout Project
    00018 #   http://scout.cs.wisc.edu
    00019 #
    00020
    00026 class Forum {
    00027
    00028     # ---- PUBLIC INTERFACE --------------------------------------------------
    00029
    00030     # Error codes for the forum object
    00031     const OK = 0;
    00032     const NONEXISTENT = 1;
    00033
    00036
    00043     function Forum($ForumId = NULL)
    00044     {
    00045         $this->ErrorStatus = Forum::OK;
    00046         # locate class in database
    00047         $this->DB = new SPTDatabase();
    00048         $DB = $this->DB;
    00049         # if ID supplied
    00050         if ($ForumId !== NULL)
    00051         {
    00052             $this->ForumId = intval($ForumId);
    00053             $DB->Query("SELECT * FROM Forums WHERE ForumId = "
    00054                     .$this->ForumId);
    00055
    00056             # if row was loaded
    00057             if ($DB->NumRowsSelected() > 0)
    00058             {
    00059                 # set attributes to values returned by database
    00060                 $this->DBFields = $DB->FetchRow();
    00061             }
    00062             else
    00063             {
    00064                 $this->ErrorStatus = Forum::NONEXISTENT;
    00065             }
    00066         }
    00067         elseif (func_num_args()==0)
    00068         {
    00069             # add record to database with that ID
    00070             $DB->Query("INSERT INTO Forums (ForumId) VALUES (NULL)");
    00071             $this->ForumId = $DB->Query("SELECT LAST_INSERT_ID() AS ForumId"
    00072                     ." FROM Forums", "ForumId");
    00073         }
    00074         else
    00075         {
    00076             $this->ErrorStatus = Forum::NONEXISTENT;
    00077         }
    00078
    00079     }
    00080
    00084     function Delete()
    00085     {
    00086         if ($this->ErrorStatus == Forum::OK)
    00087         {
    00088             $this->DB->Query("Select * from Topics where ForumId = ".
    00089                              $this->ForumId." ORDER BY DateCreated Desc");
    00090
    00091             # get list of topics for this forum
    00092             while ($Entry = $this->DB->FetchRow())
    00093             {
    00094                 $Topic = new Topic($Entry["TopicId"]);
    00095                 $Topic->Delete();
    00096             }
    00097             # delete record from database
    00098             $this->DB->Query("DELETE FROM Forums WHERE ForumId = ".$this->ForumId);
    00099         }
    00100     }
    00105
    00110     function ForumId()          {  return $this->ForumId;  }
    00111
    00116     function LastMessageDate()
    00117     {
    00118         $Message = $this->GetLastMessage();
    00119         if (isset($Message))
    00120             return $Message->DatePosted()." by ";
    00121         else
    00122             return "None";
    00123     }
    00124
    00129     function LastMessagePoster()
    00130     {
    00131         $Message = $this->GetLastMessage();
    00132         if (isset($Message))
    00133             return $Message->PosterName();
    00134     }
    00135
    00140     function LastMessagePosterEmail()
    00141     {
    00142         $Message = $this->GetLastMessage();
    00143         if (isset($Message))
    00144             return $Message->PosterEmail();
    00145     }
    00146
    00151     function ModeratorName()
    00152     {
    00153         $ModeratorName = new User($this->DB, (int)$this->ModeratorId());
    00154         return $ModeratorName->Get("UserName");
    00155     }
    00156
    00161     function ModeratorEmail()
    00162     {
    00163         $ModeratorName = new User($this->DB, (int)$this->ModeratorId());
    00164         return $ModeratorName->Get("EMail");
    00165     }
    00166
    00171     function GetTopicList()
    00172     {
    00173         $Topics = array();
    00174
    00175         $this->DB->Query("Select * from Topics where ForumId = ".
    00176                     $this->ForumId." ORDER BY DateCreated Desc");
    00177
    00178         # get list of topics for this forum
    00179         while ($Entry = $this->DB->FetchRow())
    00180         {
    00181             $Topics[$Entry["TopicId"]] = new Topic($Entry["TopicId"]);
    00182         }
    00183         return $Topics;
    00184     }
    00185
    00190     function GetLastMessage()
    00191     {
    00192         $Message = NULL;
    00193
    00194         $this->DB->Query("
    00195             SELECT M.* FROM Messages M
    00196             LEFT JOIN Topics T
    00197             ON M.ParentId = T.TopicId
    00198             WHERE M.ParentType = ".addslashes(Message::PARENTTYPE_TOPIC)."
    00199             AND T.ForumId = '".addslashes($this->ForumId)."'
    00200             ORDER BY DatePosted DESC
    00201             LIMIT 1");
    00202
    00203         if ($this->DB->NumRowsSelected())
    00204         {
    00205             $Row = $this->DB->FetchRow();
    00206             $Message = new Message($Row["MessageId"]);
    00207         }
    00208
    00209         return $Message;
    00210     }
    00211
    00217    function ForumName($NewValue = DB_NOVALUE) {  return $this->UpdateValue("ForumName", $NewValue);  }
    00218
    00224    function ForumDescription($NewValue = DB_NOVALUE) {  return $this->UpdateValue("ForumDescription", $NewValue);  }
    00225
    00231    function TopicCount($NewValue = DB_NOVALUE) {  return $this->UpdateValue("TopicCount", $NewValue);  }
    00232
    00238    function MessageCount($NewValue = DB_NOVALUE) {  return $this->UpdateValue("MessageCount", $NewValue);  }
    00239
    00245    function ModeratorId($NewValue = DB_NOVALUE) {  return $this->UpdateValue("ModeratorId", $NewValue);  }
    00246
    00251    function GetErrorStatus() { return $this->ErrorStatus; }
    00252
    00255     # ---- PRIVATE INTERFACE -------------------------------------------------
    00256
    00257    private $ForumId;
    00258    private $DB;
    00259    private $DBFields;
    00260    private $ErrorStatus;
    00261
    00262    # convenience function to supply parameters to Database->UpdateValue()
    00263    private function UpdateValue($FieldName, $NewValue)
    00264    {
    00265        if ($this->ErrorStatus==Forum::OK)
    00266        {
    00267            return $this->DB->UpdateValue("Forums", $FieldName, $NewValue,
    00268                                          "ForumId = '".$this->ForumId."'", $this->DBFields, TRUE);
    00269        }
    00270        else
    00271        {
    00272            return NULL;
    00273        }
    00274    }
    00275 }