CWIS Developer Documentation
Forum.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: Forum.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2002-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9 
14 class Forum
15 {
16 
17  # ---- PUBLIC INTERFACE --------------------------------------------------
18 
19  # Error codes for the forum object
20  const OK = 0;
21  const NONEXISTENT = 1;
22  const NOSUCHTOPIC = 2;
23  const NOSUCHMESSAGE = 3;
24 
27 
35  public function __construct($ForumId = NULL)
36  {
37  $this->ErrorStatus = self::OK;
38  # locate class in database
39  $this->DB = new Database();
40  $DB = $this->DB;
41  # if ID supplied
42  if ($ForumId !== NULL)
43  {
44  $this->ForumId = intval($ForumId);
45  $DB->Query("SELECT * FROM Forums WHERE ForumId = "
46  .$this->ForumId);
47 
48  # if row was loaded
49  if ($DB->NumRowsSelected() > 0)
50  {
51  # set attributes to values returned by database
52  $this->DBFields = $DB->FetchRow();
53  }
54  else
55  {
56  $this->ErrorStatus = self::NONEXISTENT;
57  }
58  }
59  elseif (func_num_args()==0)
60  {
61  # add record to database with that ID
62  $DB->Query("INSERT INTO Forums (ForumId) VALUES (NULL)");
63  $this->ForumId = $DB->Query("SELECT LAST_INSERT_ID() AS ForumId"
64  ." FROM Forums", "ForumId");
65  }
66  else
67  {
68  $this->ErrorStatus = self::NONEXISTENT;
69  }
70 
71  }
72 
76  public function Delete()
77  {
78  if ($this->ErrorStatus == self::OK)
79  {
80  $this->DB->Query("Select * from Topics where ForumId = ".
81  $this->ForumId." ORDER BY DateCreated Desc");
82 
83  # get list of topics for this forum
84  while ($Entry = $this->DB->FetchRow())
85  {
86  $Topic = new Topic($Entry["TopicId"]);
87  $Topic->Delete();
88  }
89  # delete record from database
90  $this->DB->Query("DELETE FROM Forums WHERE ForumId = ".$this->ForumId);
91  }
92  }
97 
102  public function ForumId()
103  {
104  return $this->ForumId; }
105 
111  public function LastMessageDate()
112  {
113  $Message = $this->GetLastMessage();
114  if (isset($Message)) {
115  return $Message->DatePosted()." by "; }
116  else {
117  return "None"; }
118  }
119 
124  public function LastMessagePoster()
125  {
126  $Message = $this->GetLastMessage();
127  if (isset($Message)) {
128  return $Message->PosterName(); }
129  }
130 
135  public function LastMessagePosterEmail()
136  {
137  $Message = $this->GetLastMessage();
138  if (isset($Message)) {
139  return $Message->PosterEmail(); }
140  }
141 
146  public function ModeratorName()
147  {
148  $ModeratorName = new CWUser((int)$this->ModeratorId());
149  return $ModeratorName->Get("UserName");
150  }
151 
156  public function ModeratorEmail()
157  {
158  $ModeratorName = new CWUser((int)$this->ModeratorId());
159  return $ModeratorName->Get("EMail");
160  }
161 
166  public function GetTopicList()
167  {
168  $Topics = array();
169 
170  $this->DB->Query("Select * from Topics where ForumId = ".
171  $this->ForumId." ORDER BY DateCreated Desc");
172 
173  # get list of topics for this forum
174  while ($Entry = $this->DB->FetchRow())
175  {
176  $Topics[$Entry["TopicId"]] = new Topic($Entry["TopicId"]);
177  }
178  return $Topics;
179  }
180 
186  public function GetLastMessage()
187  {
188  $Message = NULL;
189 
190  $this->DB->Query("
191  SELECT M.* FROM Messages M
192  LEFT JOIN Topics T
193  ON M.ParentId = T.TopicId
194  WHERE M.ParentType = ".addslashes(Message::PARENTTYPE_TOPIC)."
195  AND T.ForumId = '".addslashes($this->ForumId)."'
196  ORDER BY DatePosted DESC
197  LIMIT 1");
198 
199  if ($this->DB->NumRowsSelected())
200  {
201  $Row = $this->DB->FetchRow();
202  $Message = new Message($Row["MessageId"]);
203  }
204 
205  return $Message;
206  }
207 
213  public function ForumName($NewValue = DB_NOVALUE)
214  {
215  return $this->UpdateValue("ForumName", $NewValue);
216  }
217 
223  public function ForumDescription($NewValue = DB_NOVALUE)
224  {
225  return $this->UpdateValue("ForumDescription", $NewValue);
226  }
227 
233  public function TopicCount($NewValue = DB_NOVALUE)
234  {
235  return $this->UpdateValue("TopicCount", $NewValue);
236  }
237 
243  public function MessageCount($NewValue = DB_NOVALUE)
244  {
245  return $this->UpdateValue("MessageCount", $NewValue);
246  }
247 
254  public function ModeratorId($NewValue = DB_NOVALUE)
255  {
256  return $this->UpdateValue("ModeratorId", $NewValue);
257  }
258 
263  public function GetErrorStatus()
264  {
265  return $this->ErrorStatus;
266  }
267 
278  public function AddTopic($Author, $TopicName, $Subject, $Body)
279  {
280  $Topic = new Topic();
281 
282  $Topic->TopicName($TopicName);
283  $Topic->DateCreated(date("YmdHis"));
284  $Topic->ForumId($this->ForumId);
285  $Topic->ViewCount("0");
286  $Topic->MessageCount("0");
287  $Topic->CreatorId($Author->Id() );
288 
289  $this->TopicCount( $this->TopicCount() + 1 );
290 
291  $this->PostMessage($Topic->TopicId(), $Author, $Subject, $Body);
292 
293  return $Topic->TopicId();
294  }
295 
303  public function PostMessage($TopicId, $Author, $Subject, $Body )
304  {
305  $Topic = new Topic($TopicId);
306  if ($Topic->GetErrorStatus() == Topic::OK)
307  {
308  # If the selected topic and forum exist
309  $Message = new Message();
310  $Message->ParentId( $TopicId );
311  $Message->ParentType(Message::PARENTTYPE_TOPIC );
312  $Message->DatePosted(date("YmdHis"));
313  $Message->PosterId( $Author->Id() );
314  $Message->Subject( $Subject );
315  $Message->Body( $Body );
316 
317  # update counts for topic and forum
318  $this->MessageCount($this->MessageCount() + 1 );
319 
320  $Topic->MessageCount($Topic->MessageCount() + 1);
321 
322  return self::OK;
323  }
324  else
325  {
326  return self::NOSUCHTOPIC;
327  }
328  }
329 
336  public static function DeleteMessage($MessageId)
337  {
338  $Message = new Message($MessageId);
339 
340  if ($Message->GetErrorStatus() == Message::OK)
341  {
342  $TopicId = $Message->ParentId();
343  $Topic = new Topic($TopicId);
344  if ($Topic->GetErrorStatus() == Topic::OK)
345  {
346  $ForumId = $Topic->ForumId();
347  $Forum = new Forum($ForumId);
348  if ($Forum->GetErrorStatus() == self::OK)
349  {
350  # update count for topic and forum and delete message
351  $Topic->MessageCount($Topic->MessageCount() - 1 );
352  $Forum->MessageCount($Forum->MessageCount() - 1 );
353 
354  $Message->Delete();
355 
356  return self::OK;
357  }
358  else
359  {
360  return self::NONEXISTENT;
361  }
362  }
363  else
364  {
365  return self::NOSUCHTOPIC;
366  }
367  }
368  else
369  {
370  return self::NOSUCHMESSAGE;
371  }
372  }
373 
374  # ---- PRIVATE INTERFACE -------------------------------------------------
375 
376  private $ForumId;
377  private $DB;
378  private $DBFields;
379  private $ErrorStatus;
380 
386  private function UpdateValue($FieldName, $NewValue)
387  {
388  if ($this->ErrorStatus==self::OK)
389  {
390  return $this->DB->UpdateValue("Forums", $FieldName, $NewValue,
391  "ForumId = '".$this->ForumId."'", $this->DBFields, TRUE);
392  }
393  else
394  {
395  return NULL;
396  }
397  }
398 }
AddTopic($Author, $TopicName, $Subject, $Body)
Add topic to forum.
Definition: Forum.php:278
Abstraction for forum messages and resource comments.
Definition: Message.php:14
MessageCount($NewValue=DB_NOVALUE)
Get or set the forum&#39;s message count.
Definition: Forum.php:243
const NONEXISTENT
Definition: Forum.php:21
A converastion forum which includes topics and messages.
Definition: Forum.php:14
SQL database abstraction object with smart query caching.
Definition: Database.php:22
const PARENTTYPE_TOPIC
Definition: Message.php:22
LastMessageDate()
Get the date of the most recent post to the forum.
Definition: Forum.php:111
GetErrorStatus()
Retrieve error codes associated with the creation of the forum.
Definition: Forum.php:263
ModeratorEmail()
Get the e-mail address of the forum&#39;s moderator.
Definition: Forum.php:156
const OK
Definition: Forum.php:20
ForumDescription($NewValue=DB_NOVALUE)
Get or modify the forum description.
Definition: Forum.php:223
__construct($ForumId=NULL)
Object Constructor.
Definition: Forum.php:35
const NOSUCHTOPIC
Definition: Forum.php:22
Abstraction for topics within a Forum.
Definition: Topic.php:14
const NOSUCHMESSAGE
Definition: Forum.php:23
TopicCount($NewValue=DB_NOVALUE)
Get or set the forum&#39;s topic count.
Definition: Forum.php:233
LastMessagePosterEmail()
Get the e-mail address of the user with the most recent post.
Definition: Forum.php:135
GetLastMessage()
Get the last message posted in the forum.
Definition: Forum.php:186
const OK
Definition: Message.php:19
const DB_NOVALUE
Definition: Database.php:1541
PostMessage($TopicId, $Author, $Subject, $Body)
Post new message to topic.
Definition: Forum.php:303
static DeleteMessage($MessageId)
Delete a message from a forum, updating the message counts for the associated forum and topic...
Definition: Forum.php:336
ForumId()
Get the forum&#39;s ID.
Definition: Forum.php:102
ModeratorId($NewValue=DB_NOVALUE)
Get or set the forum&#39;s moderator.
Definition: Forum.php:254
Delete()
Remove this forum, deleting all assocated topics and messages.
Definition: Forum.php:76
GetTopicList()
Get the list of the topics in this forum.
Definition: Forum.php:166
ForumName($NewValue=DB_NOVALUE)
Get or modify the forum&#39;s name.
Definition: Forum.php:213
ModeratorName()
Get the CWIS username of the forum&#39;s moderator.
Definition: Forum.php:146
LastMessagePoster()
Get the CWIS username of the user with the most recent post.
Definition: Forum.php:124
CWIS-specific user class.
Definition: CWUser.php:13
const OK
Definition: Topic.php:19