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: SPT--Forum.php
4 #
5 # FUNCTIONS PROVIDED:
6 # Forum->Forum($ForumId)
7 # - constructor
8 # Forum->ForumId()
9 # Forum->ForumName()
10 # Forum->ForumDescription()
11 # Forum->TopicCount()
12 # Forum->MessageCount()
13 # Forum->ModeratorId()
14 # - methods to retrieve resource attributes
15 #
16 # Part of the Scout Portal Toolkit
17 # Copyright 2002 Internet Scout Project
18 # http://scout.cs.wisc.edu
19 #
20
26 class Forum {
27
28 # ---- PUBLIC INTERFACE --------------------------------------------------
29
30 # Error codes for the forum object
31 const OK = 0;
32 const NONEXISTENT = 1;
33
36
43 function Forum($ForumId = NULL)
44 {
45 $this->ErrorStatus = Forum::OK;
46 # locate class in database
47 $this->DB = new Database();
48 $DB = $this->DB;
49 # if ID supplied
50 if ($ForumId !== NULL)
51 {
52 $this->ForumId = intval($ForumId);
53 $DB->Query("SELECT * FROM Forums WHERE ForumId = "
54 .$this->ForumId);
55
56 # if row was loaded
57 if ($DB->NumRowsSelected() > 0)
58 {
59 # set attributes to values returned by database
60 $this->DBFields = $DB->FetchRow();
61 }
62 else
63 {
64 $this->ErrorStatus = Forum::NONEXISTENT;
65 }
66 }
67 elseif (func_num_args()==0)
68 {
69 # add record to database with that ID
70 $DB->Query("INSERT INTO Forums (ForumId) VALUES (NULL)");
71 $this->ForumId = $DB->Query("SELECT LAST_INSERT_ID() AS ForumId"
72 ." FROM Forums", "ForumId");
73 }
74 else
75 {
76 $this->ErrorStatus = Forum::NONEXISTENT;
77 }
78
79 }
80
84 function Delete()
85 {
86 if ($this->ErrorStatus == Forum::OK)
87 {
88 $this->DB->Query("Select * from Topics where ForumId = ".
89 $this->ForumId." ORDER BY DateCreated Desc");
90
91 # get list of topics for this forum
92 while ($Entry = $this->DB->FetchRow())
93 {
94 $Topic = new Topic($Entry["TopicId"]);
95 $Topic->Delete();
96 }
97 # delete record from database
98 $this->DB->Query("DELETE FROM Forums WHERE ForumId = ".$this->ForumId);
99 }
100 }
105
110 function ForumId() { return $this->ForumId; }
111
116 function LastMessageDate()
117 {
118 $Message = $this->GetLastMessage();
119 if (isset($Message))
120 return $Message->DatePosted()." by ";
121 else
122 return "None";
123 }
124
129 function LastMessagePoster()
130 {
131 $Message = $this->GetLastMessage();
132 if (isset($Message))
133 return $Message->PosterName();
134 }
135
140 function LastMessagePosterEmail()
141 {
142 $Message = $this->GetLastMessage();
143 if (isset($Message))
144 return $Message->PosterEmail();
145 }
146
151 function ModeratorName()
152 {
153 $ModeratorName = new User($this->DB, (int)$this->ModeratorId());
154 return $ModeratorName->Get("UserName");
155 }
156
161 function ModeratorEmail()
162 {
163 $ModeratorName = new User($this->DB, (int)$this->ModeratorId());
164 return $ModeratorName->Get("EMail");
165 }
166
171 function GetTopicList()
172 {
173 $Topics = array();
174
175 $this->DB->Query("Select * from Topics where ForumId = ".
176 $this->ForumId." ORDER BY DateCreated Desc");
177
178 # get list of topics for this forum
179 while ($Entry = $this->DB->FetchRow())
180 {
181 $Topics[$Entry["TopicId"]] = new Topic($Entry["TopicId"]);
182 }
183 return $Topics;
184 }
185
190 function GetLastMessage()
191 {
192 $Message = NULL;
193
194 $this->DB->Query("
195 SELECT M.* FROM Messages M
196 LEFT JOIN Topics T
197 ON M.ParentId = T.TopicId
198 WHERE M.ParentType = ".addslashes(Message::PARENTTYPE_TOPIC)."
199 AND T.ForumId = '".addslashes($this->ForumId)."'
200 ORDER BY DatePosted DESC
201 LIMIT 1");
202
203 if ($this->DB->NumRowsSelected())
204 {
205 $Row = $this->DB->FetchRow();
206 $Message = new Message($Row["MessageId"]);
207 }
208
209 return $Message;
210 }
211
217 function ForumName($NewValue = DB_NOVALUE) { return $this->UpdateValue("ForumName", $NewValue); }
218
224 function ForumDescription($NewValue = DB_NOVALUE) { return $this->UpdateValue("ForumDescription", $NewValue); }
225
231 function TopicCount($NewValue = DB_NOVALUE) { return $this->UpdateValue("TopicCount", $NewValue); }
232
238 function MessageCount($NewValue = DB_NOVALUE) { return $this->UpdateValue("MessageCount", $NewValue); }
239
245 function ModeratorId($NewValue = DB_NOVALUE) { return $this->UpdateValue("ModeratorId", $NewValue); }
246
251 function GetErrorStatus() { return $this->ErrorStatus; }
252
255 # ---- PRIVATE INTERFACE -------------------------------------------------
256
257 private $ForumId;
258 private $DB;
259 private $DBFields;
260 private $ErrorStatus;
261
262 # convenience function to supply parameters to Database->UpdateValue()
263 private function UpdateValue($FieldName, $NewValue)
264 {
265 if ($this->ErrorStatus==Forum::OK)
266 {
267 return $this->DB->UpdateValue("Forums", $FieldName, $NewValue,
268 "ForumId = '".$this->ForumId."'", $this->DBFields, TRUE);
269 }
270 else
271 {
272 return NULL;
273 }
274 }
275 }