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 extends Item
15 {
16 
17  # ---- PUBLIC INTERFACE --------------------------------------------------
18 
21 
30  public static function Create($Name, $ModeratorId, $Description = NULL)
31  {
32  $DB = new Database();
33  $ModeratorId = intval($ModeratorId);
34 
35  # name is required
36  $Name = trim($Name);
37  if (!strlen($Name))
38  {
39  throw new InvalidArgumentException("Specified ForumName is empty.");
40  }
41 
42  # check if the ID provided is a valid user ID and has privilege
43  $Factory = new UserFactory();
44  if ($Factory->UserExists($ModeratorId))
45  {
46  $Moderator = new CWUser($ModeratorId);
47  if (!$Moderator->HasPriv(PRIV_FORUMADMIN))
48  {
49  throw new InvalidArgumentException("Specified moderator ID ("
50  .$ModeratorId.") is not a forum administrator.");
51  }
52  }
53  else
54  {
55  throw new InvalidArgumentException("Specified moderator ID ("
56  .$ModeratorId.") is not found.");
57  }
58 
59  # set description if there is any
60  if (!is_null($Description))
61  {
62  $Query = "INSERT INTO Forums (ForumName, ModeratorId, ForumDescription)".
63  " VALUES ('".addslashes($Name)."', '".addslashes($ModeratorId)."', '"
64  .addslashes($Description)."')";
65  }
66  else
67  {
68  $Query = "INSERT INTO Forums (ForumName, ModeratorId) VALUES ('"
69  .addslashes($Name)."', '".addslashes($ModeratorId)."')";
70  }
71 
72  # create a new record
73  $DB->Query($Query);
74 
75  # get its forum id
76  $ForumId = $DB->LastInsertId();
77 
78  return (new Forum($ForumId));
79  }
80 
84  public function Delete()
85  {
86  $this->DB->Query("SELECT * FROM Topics WHERE ForumId = ".
87  $this->Id()." ORDER BY DateCreated DESC");
88 
89  # get list of topics for this forum
90  while ($Entry = $this->DB->FetchRow())
91  {
92  $Topic = new Topic($Entry["TopicId"]);
93  $Topic->Delete();
94  }
95  # delete record from database
96  $this->Destroy();
97  }
102 
108  public function ForumId()
109  {
110  return $this->Id();
111  }
112 
118  public function LastMessageDate()
119  {
120  $Message = $this->GetLastMessage();
121  if (isset($Message))
122  {
123  return GetPrettyTimestamp($Message->DatePosted())." by ";
124  }
125  else
126  {
127  return "None";
128  }
129  }
130 
135  public function LastMessagePoster()
136  {
137  $Message = $this->GetLastMessage();
138  if (isset($Message))
139  {
140  return $Message->PosterName();
141  }
142  }
143 
148  public function LastMessagePosterEmail()
149  {
150  $Message = $this->GetLastMessage();
151  if (isset($Message))
152  {
153  return $Message->PosterEmail();
154  }
155  }
156 
162  public function ModeratorName()
163  {
164  $UserId = (int)$this->ModeratorId();
165  $UserFactory = new CWUserFactory();
166 
167  if ($UserFactory->UserExists($UserId))
168  {
169  return (new CWUser($UserId))->Get("UserName");
170  }
171  else
172  {
173  return NULL;
174  }
175  }
176 
182  public function ModeratorEmail()
183  {
184  $UserId = (int)$this->ModeratorId();
185  $UserFactory = new CWUserFactory();
186 
187  if ($UserFactory->UserExists($UserId))
188  {
189  return (new CWUser($UserId))->Get("EMail");
190  }
191  else
192  {
193  return NULL;
194  }
195  }
196 
201  public function GetTopicList()
202  {
203  $Topics = array();
204 
205  $this->DB->Query("SELECT * FROM Topics WHERE ForumId = '".
206  addslashes($this->Id())."' ORDER BY DateCreated DESC");
207 
208  # get list of topics for this forum
209  while ($Entry = $this->DB->FetchRow())
210  {
211  $Topics[$Entry["TopicId"]] = new Topic($Entry["TopicId"]);
212  }
213  return $Topics;
214  }
215 
221  public function GetLastMessage()
222  {
223  $Message = NULL;
224 
225  $this->DB->Query("
226  SELECT M.* FROM Messages M
227  LEFT JOIN Topics T
228  ON M.ParentId = T.TopicId
229  WHERE M.ParentType = ".Message::PARENTTYPE_TOPIC."
230  AND T.ForumId = '".addslashes($this->Id())."'
231  ORDER BY DatePosted DESC
232  LIMIT 1");
233 
234  if ($this->DB->NumRowsSelected())
235  {
236  $Row = $this->DB->FetchRow();
237  $Message = new Message($Row["MessageId"]);
238  }
239 
240  return $Message;
241  }
242 
248  public function ForumName($NewValue = DB_NOVALUE)
249  {
250  return $this->UpdateValue("ForumName", $NewValue);
251  }
252 
258  public function ForumDescription($NewValue = DB_NOVALUE)
259  {
260  return $this->UpdateValue("ForumDescription", $NewValue);
261  }
262 
268  public function TopicCount($NewValue = DB_NOVALUE)
269  {
270  return $this->UpdateValue("TopicCount", $NewValue);
271  }
272 
278  public function MessageCount($NewValue = DB_NOVALUE)
279  {
280  return $this->UpdateValue("MessageCount", $NewValue);
281  }
282 
289  public function ModeratorId($NewValue = DB_NOVALUE)
290  {
291  return $this->UpdateValue("ModeratorId", $NewValue);
292  }
293 
304  public function AddTopic($Author, $TopicName, $Subject, $Body)
305  {
306  $Topic = Topic::Create();
307  $Topic->TopicName($TopicName);
308  $Topic->DateCreated(date("YmdHis"));
309  $Topic->ForumId($this->Id());
310  $Topic->CreatorId($Author->Id());
311 
312  $this->TopicCount($this->TopicCount() + 1);
313 
314  $this->PostMessage($Topic->TopicId(), $Author, $Subject, $Body);
315 
316  return $Topic->TopicId();
317  }
318 
326  public function PostMessage($TopicId, $Author, $Subject, $Body )
327  {
328  $Topic = new Topic($TopicId);
329 
330  # create a new message
331  $Message = Message::Create();
332  $Message->ParentId($TopicId);
333  $Message->ParentType(Message::PARENTTYPE_TOPIC);
334  $Message->DatePosted(date("YmdHis"));
335  $Message->PosterId($Author->Id());
336  $Message->Subject($Subject);
337  $Message->Body($Body);
338 
339  # update counts for topic and forum
340  $this->MessageCount($this->MessageCount() + 1 );
341 
342  $Topic->MessageCount($Topic->MessageCount() + 1);
343 
344  }
345 
351  public static function DeleteMessage($MessageId)
352  {
353  $Message = new Message($MessageId);
354 
355  $TopicId = $Message->ParentId();
356  $Topic = new Topic($TopicId);
357 
358  $ForumId = $Topic->ForumId();
359  $Forum = new Forum($ForumId);
360 
361  # update count for topic and forum and delete message
362  $Topic->MessageCount($Topic->MessageCount() - 1 );
363  $Forum->MessageCount($Forum->MessageCount() - 1 );
364 
365  $Message->Delete();
366  }
367 }
AddTopic($Author, $TopicName, $Subject, $Body)
Add topic to forum.
Definition: Forum.php:304
Id()
Get item ID.
Definition: Item.php:81
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:278
UpdateValue($ColumnName, $NewValue=DB_NOVALUE)
Convenience function to supply parameters to Database::UpdateValue().
Definition: Item.php:284
A converastion forum which includes topics and messages.
Definition: Forum.php:14
static Create()
Create an empty message object.
Definition: Message.php:28
SQL database abstraction object with smart query caching.
Definition: Database.php:22
const PARENTTYPE_TOPIC
Definition: Message.php:16
LastMessageDate()
Get the date of the most recent post to the forum.
Definition: Forum.php:118
$DB
Definition: Item.php:210
ModeratorEmail()
Get the e-mail address of the forum&#39;s moderator.
Definition: Forum.php:182
ForumDescription($NewValue=DB_NOVALUE)
Get or modify the forum description.
Definition: Forum.php:258
Abstraction for topics within a Forum.
Definition: Topic.php:14
TopicCount($NewValue=DB_NOVALUE)
Get or set the forum&#39;s topic count.
Definition: Forum.php:268
Destroy()
Destroy item.
Definition: Item.php:60
LastMessagePosterEmail()
Get the e-mail address of the user with the most recent post.
Definition: Forum.php:148
GetLastMessage()
Get the last message posted in the forum.
Definition: Forum.php:221
CWIS-specific user factory class.
static Create()
Create an empty new topic.
Definition: Topic.php:25
Common base class for persistent items store in database.
Definition: Item.php:13
const DB_NOVALUE
Definition: Database.php:1738
static Create($Name, $ModeratorId, $Description=NULL)
Create a forum.
Definition: Forum.php:30
PostMessage($TopicId, $Author, $Subject, $Body)
Post new message to topic.
Definition: Forum.php:326
static DeleteMessage($MessageId)
Delete a message from a forum, updating the message counts for the associated forum and topic...
Definition: Forum.php:351
ForumId()
Get the forum&#39;s ID.
Definition: Forum.php:108
ModeratorId($NewValue=DB_NOVALUE)
Get or set the forum&#39;s moderator.
Definition: Forum.php:289
Delete()
Remove this forum, deleting all assocated topics and messages.
Definition: Forum.php:84
GetTopicList()
Get the list of the topics in this forum.
Definition: Forum.php:201
ForumName($NewValue=DB_NOVALUE)
Get or modify the forum&#39;s name.
Definition: Forum.php:248
ModeratorName()
Get the CWIS username of the forum&#39;s moderator.
Definition: Forum.php:162
LastMessagePoster()
Get the CWIS username of the user with the most recent post.
Definition: Forum.php:135
CWIS-specific user class.
Definition: CWUser.php:13