CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 # ---- PUBLIC INTERFACE --------------------------------------------------
17
18 # Error codes for the forum object
19 const OK = 0;
20 const NONEXISTENT = 1;
21 const NOSUCHTOPIC = 2;
22 const NOSUCHMESSAGE = 3;
23
26
34 function Forum($ForumId = NULL)
35 {
36 $this->ErrorStatus = Forum::OK;
37 # locate class in database
38 $this->DB = new Database();
39 $DB = $this->DB;
40 # if ID supplied
41 if ($ForumId !== NULL)
42 {
43 $this->ForumId = intval($ForumId);
44 $DB->Query("SELECT * FROM Forums WHERE ForumId = "
45 .$this->ForumId);
46
47 # if row was loaded
48 if ($DB->NumRowsSelected() > 0)
49 {
50 # set attributes to values returned by database
51 $this->DBFields = $DB->FetchRow();
52 }
53 else
54 {
55 $this->ErrorStatus = Forum::NONEXISTENT;
56 }
57 }
58 elseif (func_num_args()==0)
59 {
60 # add record to database with that ID
61 $DB->Query("INSERT INTO Forums (ForumId) VALUES (NULL)");
62 $this->ForumId = $DB->Query("SELECT LAST_INSERT_ID() AS ForumId"
63 ." FROM Forums", "ForumId");
64 }
65 else
66 {
67 $this->ErrorStatus = Forum::NONEXISTENT;
68 }
69
70 }
71
75 function Delete()
76 {
77 if ($this->ErrorStatus == Forum::OK)
78 {
79 $this->DB->Query("Select * from Topics where ForumId = ".
80 $this->ForumId." ORDER BY DateCreated Desc");
81
82 # get list of topics for this forum
83 while ($Entry = $this->DB->FetchRow())
84 {
85 $Topic = new Topic($Entry["TopicId"]);
86 $Topic->Delete();
87 }
88 # delete record from database
89 $this->DB->Query("DELETE FROM Forums WHERE ForumId = ".$this->ForumId);
90 }
91 }
96
101 function ForumId() { return $this->ForumId; }
102
108 function LastMessageDate()
109 {
110 $Message = $this->GetLastMessage();
111 if (isset($Message))
112 return $Message->DatePosted()." by ";
113 else
114 return "None";
115 }
116
121 function LastMessagePoster()
122 {
123 $Message = $this->GetLastMessage();
124 if (isset($Message))
125 return $Message->PosterName();
126 }
127
132 function LastMessagePosterEmail()
133 {
134 $Message = $this->GetLastMessage();
135 if (isset($Message))
136 return $Message->PosterEmail();
137 }
138
143 function ModeratorName()
144 {
145 $ModeratorName = new CWUser((int)$this->ModeratorId());
146 return $ModeratorName->Get("UserName");
147 }
148
153 function ModeratorEmail()
154 {
155 $ModeratorName = new CWUser((int)$this->ModeratorId());
156 return $ModeratorName->Get("EMail");
157 }
158
163 function GetTopicList()
164 {
165 $Topics = array();
166
167 $this->DB->Query("Select * from Topics where ForumId = ".
168 $this->ForumId." ORDER BY DateCreated Desc");
169
170 # get list of topics for this forum
171 while ($Entry = $this->DB->FetchRow())
172 {
173 $Topics[$Entry["TopicId"]] = new Topic($Entry["TopicId"]);
174 }
175 return $Topics;
176 }
177
183 function GetLastMessage()
184 {
185 $Message = NULL;
186
187 $this->DB->Query("
188 SELECT M.* FROM Messages M
189 LEFT JOIN Topics T
190 ON M.ParentId = T.TopicId
191 WHERE M.ParentType = ".addslashes(Message::PARENTTYPE_TOPIC)."
192 AND T.ForumId = '".addslashes($this->ForumId)."'
193 ORDER BY DatePosted DESC
194 LIMIT 1");
195
196 if ($this->DB->NumRowsSelected())
197 {
198 $Row = $this->DB->FetchRow();
199 $Message = new Message($Row["MessageId"]);
200 }
201
202 return $Message;
203 }
204
210 function ForumName($NewValue = DB_NOVALUE) { return $this->UpdateValue("ForumName", $NewValue); }
211
217 function ForumDescription($NewValue = DB_NOVALUE) { return $this->UpdateValue("ForumDescription", $NewValue); }
218
224 function TopicCount($NewValue = DB_NOVALUE) { return $this->UpdateValue("TopicCount", $NewValue); }
225
231 function MessageCount($NewValue = DB_NOVALUE) { return $this->UpdateValue("MessageCount", $NewValue); }
232
239 function ModeratorId($NewValue = DB_NOVALUE) { return $this->UpdateValue("ModeratorId", $NewValue); }
240
245 function GetErrorStatus() { return $this->ErrorStatus; }
246
249 function AddTopic($Author, $TopicName, $Subject, $Body)
250 {
251 $Topic = new Topic();
252
253 $Topic->TopicName($TopicName);
254 $Topic->DateCreated(date("YmdHis"));
255 $Topic->ForumId($this->ForumId);
256 $Topic->ViewCount("0");
257 $Topic->MessageCount("0");
258 $Topic->CreatorId($Author->Id() );
259
260 $this->TopicCount( $this->TopicCount() + 1 );
261
262 $this->PostMessage($Topic->TopicId(), $Author, $Subject, $Body);
263
264 return $Topic->TopicId();
265 }
266
267 function PostMessage($TopicId, $Author, $Subject, $Body )
268 {
269 $Topic = new Topic($TopicId);
270 if ($Topic->GetErrorStatus() == Topic::OK)
271 {
272 # If the selected topic and forum exist
273 $Message = new Message();
274 $Message->ParentId( $TopicId );
275 $Message->ParentType(Message::PARENTTYPE_TOPIC );
276 $Message->DatePosted(date("YmdHis"));
277 $Message->PosterId( $Author->Id() );
278 $Message->Subject( $Subject );
279 $Message->Body( $Body );
280
281 # update counts for topic and forum
282 $this->MessageCount($this->MessageCount() + 1 );
283
284 $Topic->MessageCount($Topic->MessageCount() + 1);
285
286 return Forum::OK;
287 }
288 else
289 {
290 return Forum::NOSUCHTOPIC;
291 }
292 }
293
300 static function DeleteMessage($MessageId)
301 {
302 $Message = new Message($MessageId);
303
304 if ($Message->GetErrorStatus() == Message::OK)
305 {
306 $TopicId = $Message->ParentId();
307 $Topic = new Topic($TopicId);
308 if ($Topic->GetErrorStatus() == Topic::OK)
309 {
310 $ForumId = $Topic->ForumId();
311 $Forum = new Forum($ForumId);
312 if ($Forum->GetErrorStatus() == self::OK)
313 {
314 # update count for topic and forum and delete message
315 $Topic->MessageCount($Topic->MessageCount() - 1 );
316 $Forum->MessageCount($Forum->MessageCount() - 1 );
317
318 $Message->Delete();
319
320 return self::OK;
321 }
322 else
323 {
324 return self::NONEXISTENT;
325 }
326 }
327 else
328 {
329 return self::NOSUCHTOPIC;
330 }
331 }
332 else
333 {
334 return self::NOSUCHMESSAGE;
335 }
336 }
337
338 # ---- PRIVATE INTERFACE -------------------------------------------------
339
340 private $ForumId;
341 private $DB;
342 private $DBFields;
343 private $ErrorStatus;
344
350 private function UpdateValue($FieldName, $NewValue)
351 {
352 if ($this->ErrorStatus==Forum::OK)
353 {
354 return $this->DB->UpdateValue("Forums", $FieldName, $NewValue,
355 "ForumId = '".$this->ForumId."'", $this->DBFields, TRUE);
356 }
357 else
358 {
359 return NULL;
360 }
361 }
362
363 }
Forum\AddTopic
AddTopic($Author, $TopicName, $Subject, $Body)
Definition: Forum.php:249
Message
Abstraction for forum messages and resource comments.
Definition: Message.php:15
Forum\MessageCount
MessageCount($NewValue=DB_NOVALUE)
Get or set the forum's message count.
Definition: Forum.php:231
Forum\NONEXISTENT
const NONEXISTENT
Definition: Forum.php:20
Forum
A converastion forum which includes topics and messages.
Definition: Forum.php:14
Database
SQL database abstraction object with smart query caching.
Definition: Axis--Database.php:22
Message\PARENTTYPE_TOPIC
const PARENTTYPE_TOPIC
Definition: Message.php:22
Forum\LastMessageDate
LastMessageDate()
Get the date of the most recent post to the forum.
Definition: Forum.php:108
DB_NOVALUE
const DB_NOVALUE
Definition: Axis--Database.php:1331
Forum\GetErrorStatus
GetErrorStatus()
Retrieve error codes associated with the creation of the forum.
Definition: Forum.php:245
Forum\ModeratorEmail
ModeratorEmail()
Get the e-mail address of the forum's moderator.
Definition: Forum.php:153
Forum\OK
const OK
Definition: Forum.php:19
Forum\ForumDescription
ForumDescription($NewValue=DB_NOVALUE)
Get or modify the forum description.
Definition: Forum.php:217
Forum\NOSUCHTOPIC
const NOSUCHTOPIC
Definition: Forum.php:21
Topic
Abstraction for topics within a Forum.
Definition: Topic.php:14
Forum\NOSUCHMESSAGE
const NOSUCHMESSAGE
Definition: Forum.php:22
Forum\TopicCount
TopicCount($NewValue=DB_NOVALUE)
Get or set the forum's topic count.
Definition: Forum.php:224
Forum\LastMessagePosterEmail
LastMessagePosterEmail()
Get the e-mail address of the user with the most recent post.
Definition: Forum.php:132
Forum\GetLastMessage
GetLastMessage()
Get the last message posted in the forum.
Definition: Forum.php:183
Forum\Forum
Forum($ForumId=NULL)
Object Constructor.
Definition: Forum.php:34
Message\OK
const OK
Definition: Message.php:19
PHP
PHP
Definition: RSSClient--Test.php:12
Forum\PostMessage
PostMessage($TopicId, $Author, $Subject, $Body)
Definition: Forum.php:267
Forum\DeleteMessage
static DeleteMessage($MessageId)
Delete a message from a forum, updating the message counts for the associated forum and topic...
Definition: Forum.php:300
Forum\ForumId
ForumId()
Get the forum's ID.
Definition: Forum.php:101
Forum\ModeratorId
ModeratorId($NewValue=DB_NOVALUE)
Get or set the forum's moderator.
Definition: Forum.php:239
Forum\Delete
Delete()
Remove this forum, deleting all assocated topics and messages.
Definition: Forum.php:75
Forum\GetTopicList
GetTopicList()
Get the list of the topics in this forum.
Definition: Forum.php:163
Forum\ForumName
ForumName($NewValue=DB_NOVALUE)
Get or modify the forum's name.
Definition: Forum.php:210
Forum\ModeratorName
ModeratorName()
Get the CWIS username of the forum's moderator.
Definition: Forum.php:143
Forum\LastMessagePoster
LastMessagePoster()
Get the CWIS username of the user with the most recent post.
Definition: Forum.php:121
CWUser
CWIS-specific user class.
Definition: CWUser.php:13
Topic\OK
const OK
Definition: Topic.php:19