f93f848a |
1 | <?PHP // $Id$ |
2 | |
b0e3a925 |
3 | require_once("$CFG->dirroot/files/mimetypes.php"); |
7f6689e4 |
4 | |
501cdbd8 |
5 | /// CONSTANTS /////////////////////////////////////////////////////////// |
f93f848a |
6 | |
501cdbd8 |
7 | $FORUM_DEFAULT_DISPLAY_MODE = 3; |
f93f848a |
8 | |
ffe11640 |
9 | $FORUM_LAYOUT_MODES = array ( "1" => get_string("modeflatoldestfirst", "forum"), |
10 | "-1" => get_string("modeflatnewestfirst", "forum"), |
11 | "2" => get_string("modethreaded", "forum"), |
12 | "3" => get_string("modenested", "forum") ); |
f93f848a |
13 | |
11b0c469 |
14 | // These are course content forums that can be added to the course manually |
ffe11640 |
15 | $FORUM_TYPES = array ("general" => get_string("generalforum", "forum"), |
16 | "eachuser" => get_string("eachuserforum", "forum"), |
17 | "single" => get_string("singleforum", "forum") ); |
f93f848a |
18 | |
ffe11640 |
19 | $FORUM_POST_RATINGS = array ("3" => get_string("postrating3", "forum"), |
20 | "2" => get_string("postrating2", "forum"), |
21 | "1" => get_string("postrating1", "forum") ); |
f93f848a |
22 | |
70c476a7 |
23 | $FORUM_OPEN_MODES = array ("2" => get_string("openmode2", "forum"), |
24 | "1" => get_string("openmode1", "forum"), |
25 | "0" => get_string("openmode0", "forum") ); |
26 | |
73bb0835 |
27 | |
c585fa17 |
28 | define("FORUM_SHORT_POST", 300); // Less non-HTML characters than this is short |
4d871a72 |
29 | |
c585fa17 |
30 | define("FORUM_LONG_POST", 600); // More non-HTML characters than this is long |
4d871a72 |
31 | |
3c8a606d |
32 | define("FORUM_MANY_DISCUSSIONS", 100); |
e07635f4 |
33 | |
34 | |
caadf009 |
35 | /// STANDARD FUNCTIONS /////////////////////////////////////////////////////////// |
36 | |
37 | function forum_add_instance($forum) { |
38 | // Given an object containing all the necessary data, |
39 | // (defined by the form in mod.html) this function |
40 | // will create a new instance and return the id number |
41 | // of the new instance. |
42 | |
43 | global $CFG; |
44 | |
45 | $forum->timemodified = time(); |
46 | |
47 | if (! $forum->id = insert_record("forum", $forum)) { |
48 | return false; |
49 | } |
50 | |
51 | if ($forum->type == "single") { // Create related discussion. |
52 | |
53 | $discussion->course = $forum->course; |
54 | $discussion->forum = $forum->id; |
55 | $discussion->name = $forum->name; |
56 | $discussion->intro = $forum->intro; |
57 | $discussion->assessed = $forum->assessed; |
58 | |
59 | if (! forum_add_discussion($discussion)) { |
60 | error("Could not add the discussion for this forum"); |
61 | } |
62 | } |
63 | add_to_log($forum->course, "forum", "add", "index.php?f=$forum->id", "$forum->id"); |
64 | |
65 | return $forum->id; |
66 | } |
67 | |
68 | |
69 | function forum_update_instance($forum) { |
70 | // Given an object containing all the necessary data, |
71 | // (defined by the form in mod.html) this function |
72 | // will update an existing instance with new data. |
73 | |
74 | $forum->timemodified = time(); |
75 | $forum->id = $forum->instance; |
76 | |
77 | if ($forum->type == "single") { // Update related discussion and post. |
78 | if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) { |
79 | if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) { |
80 | notify("Warning! There is more than one discussion in this forum - using the most recent"); |
81 | $discussion = array_pop($discussions); |
82 | } else { |
83 | error("Could not find the discussion in this forum"); |
84 | } |
85 | } |
86 | if (! $post = get_record("forum_posts", "id", $discussion->firstpost)) { |
87 | error("Could not find the first post in this forum discussion"); |
88 | } |
89 | |
90 | $post->subject = $forum->name; |
91 | $post->message = $forum->intro; |
92 | $post->modified = $forum->timemodified; |
93 | |
94 | if (! update_record("forum_posts", $post)) { |
95 | error("Could not update the first post"); |
96 | } |
97 | |
98 | $discussion->name = $forum->name; |
99 | |
100 | if (! update_record("forum_discussions", $discussion)) { |
101 | error("Could not update the discussion"); |
102 | } |
103 | } |
104 | |
105 | if (update_record("forum", $forum)) { |
106 | add_to_log($forum->course, "forum", "update", "index.php?f=$forum->id", "$forum->id"); |
107 | return true; |
108 | } else { |
109 | return false; |
110 | } |
111 | } |
112 | |
113 | |
114 | function forum_delete_instance($id) { |
115 | // Given an ID of an instance of this module, |
116 | // this function will permanently delete the instance |
117 | // and any data that depends on it. |
118 | |
119 | if (! $forum = get_record("forum", "id", "$id")) { |
120 | return false; |
121 | } |
122 | |
123 | $result = true; |
124 | |
125 | if ($discussions = get_records("forum_discussions", "forum", $forum->id)) { |
126 | foreach ($discussions as $discussion) { |
127 | if (! forum_delete_discussion($discussion)) { |
128 | $result = false; |
129 | } |
130 | } |
131 | } |
132 | |
133 | if (! delete_records("forum_subscriptions", "forum", "$forum->id")) { |
134 | $result = false; |
135 | } |
136 | |
137 | if (! delete_records("forum", "id", "$forum->id")) { |
138 | $result = false; |
139 | } |
140 | |
141 | return $result; |
142 | } |
143 | |
144 | |
145 | function forum_cron () { |
edffca15 |
146 | /// Function to be run periodically according to the moodle cron |
147 | /// Finds all posts that have yet to be mailed out, and mails them |
148 | /// out to all subscribers |
caadf009 |
149 | |
150 | global $CFG, $USER; |
151 | |
152 | $cutofftime = time() - $CFG->maxeditingtime; |
153 | |
1f48942e |
154 | if ($posts = forum_get_unmailed_posts($cutofftime)) { |
caadf009 |
155 | |
edffca15 |
156 | /// Mark them all now as being mailed. It's unlikely but possible there |
157 | /// might be an error later so that a post is NOT actually mailed out, |
158 | /// but since mail isn't crucial, we can accept this risk. Doing it now |
159 | /// prevents the risk of duplicated mails, which is a worse problem. |
160 | |
161 | foreach ($posts as $key => $post) { |
162 | if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) { |
163 | echo "Error marking post id post->id as being mailed. This post will not be mailed.\n"; |
164 | unset($posts[$key]); |
165 | } |
166 | } |
167 | |
caadf009 |
168 | $timenow = time(); |
169 | |
170 | foreach ($posts as $post) { |
171 | |
edffca15 |
172 | echo "\n"; |
caadf009 |
173 | print_string("processingpost", "forum", $post->id); |
edffca15 |
174 | echo "\n"; |
caadf009 |
175 | |
ebc3bd2b |
176 | if (! $userfrom = get_record("user", "id", "$post->userid")) { |
177 | echo "Could not find user $post->userid\n"; |
caadf009 |
178 | continue; |
179 | } |
180 | |
181 | if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) { |
182 | echo "Could not find discussion $post->discussion\n"; |
183 | continue; |
184 | } |
185 | |
186 | if (! $forum = get_record("forum", "id", "$discussion->forum")) { |
187 | echo "Could not find forum $discussion->forum\n"; |
188 | continue; |
189 | } |
190 | |
191 | if (! $course = get_record("course", "id", "$forum->course")) { |
192 | echo "Could not find course $forum->course\n"; |
193 | continue; |
194 | } |
195 | |
196 | if ($users = forum_subscribed_users($course, $forum)) { |
197 | $canunsubscribe = ! forum_is_forcesubscribed($forum->id); |
198 | |
199 | $mailcount=0; |
200 | foreach ($users as $userto) { |
edffca15 |
201 | /// Override the language of get_string, so that mail is in correct language for the receiver. |
202 | $USER->lang = $userto->lang; |
f690562f |
203 | $canreply = forum_user_can_post($forum, $userto); |
caadf009 |
204 | |
caadf009 |
205 | $by->name = "$userfrom->firstname $userfrom->lastname"; |
d62413e8 |
206 | $by->date = userdate($post->modified, "", $userto->timezone); |
caadf009 |
207 | $strbynameondate = get_string("bynameondate", "forum", $by); |
208 | |
209 | $strforums = get_string("forums", "forum"); |
210 | |
211 | $postsubject = "$course->shortname: $post->subject"; |
212 | $posttext = "$course->shortname -> $strforums -> $forum->name"; |
213 | |
214 | if ($discussion->name == $forum->name) { |
215 | $posttext .= "\n"; |
216 | } else { |
217 | $posttext .= " -> $discussion->name\n"; |
218 | } |
219 | $posttext .= "---------------------------------------------------------------------\n"; |
220 | $posttext .= "$post->subject\n"; |
221 | $posttext .= $strbynameondate."\n"; |
222 | $posttext .= "---------------------------------------------------------------------\n"; |
d342c763 |
223 | $posttext .= format_text_email($post->message, $post->format); |
caadf009 |
224 | $posttext .= "\n\n"; |
225 | if ($post->attachment) { |
226 | $post->course = $course->id; |
227 | $post->forum = $forum->id; |
228 | $posttext .= forum_print_attachments($post, "text"); |
229 | } |
f690562f |
230 | if ($canreply) { |
231 | $posttext .= "---------------------------------------------------------------------\n"; |
232 | $posttext .= get_string("postmailinfo", "forum", $course->shortname)."\n"; |
233 | $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id\n"; |
234 | } |
caadf009 |
235 | if ($canunsubscribe) { |
236 | $posttext .= "\n---------------------------------------------------------------------\n"; |
237 | $posttext .= get_string("unsubscribe", "forum"); |
238 | $posttext .= ": $CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\n"; |
239 | } |
240 | |
241 | if ($userto->mailformat == 1) { // HTML |
edffca15 |
242 | $posthtml = "<p><font face=\"sans-serif\">". |
243 | "<a target=\"_blank\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ". |
244 | "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</a> -> ". |
245 | "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name</a>"; |
caadf009 |
246 | if ($discussion->name == $forum->name) { |
edffca15 |
247 | $posthtml .= "</font></p>"; |
caadf009 |
248 | } else { |
edffca15 |
249 | $posthtml .= " -> <a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name</a></font></p>"; |
caadf009 |
250 | } |
f690562f |
251 | $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, $canreply, false, false); |
caadf009 |
252 | |
253 | if ($canunsubscribe) { |
edffca15 |
254 | $posthtml .= "\n<br /><hr size=\"1\" noshade /><p align=\"right\"><font size=\"1\"><a href=\"$CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\">".get_string("unsubscribe", "forum")."</a></font></p>"; |
caadf009 |
255 | } |
256 | |
257 | } else { |
258 | $posthtml = ""; |
259 | } |
260 | |
261 | if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) { |
edffca15 |
262 | echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email) .. not trying again.\n"; |
caadf009 |
263 | } else { |
264 | $mailcount++; |
265 | } |
266 | } |
caadf009 |
267 | |
edffca15 |
268 | echo ".... mailed to $mailcount users.\n"; |
caadf009 |
269 | } |
caadf009 |
270 | } |
271 | } |
272 | |
273 | return true; |
274 | } |
275 | |
276 | function forum_user_outline($course, $user, $mod, $forum) { |
277 | |
1f48942e |
278 | if ($posts = forum_get_user_posts($forum->id, $user->id)) { |
caadf009 |
279 | $result->info = get_string("numposts", "forum", count($posts)); |
280 | |
281 | $lastpost = array_pop($posts); |
282 | $result->time = $lastpost->modified; |
283 | return $result; |
284 | } |
285 | return NULL; |
286 | } |
287 | |
288 | |
289 | function forum_user_complete($course, $user, $mod, $forum) { |
290 | global $CFG; |
291 | |
1f48942e |
292 | if ($posts = forum_get_user_posts($forum->id, $user->id)) { |
caadf009 |
293 | foreach ($posts as $post) { |
294 | if ($post->parent) { |
295 | $footer = "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">". |
296 | get_string("parentofthispost", "forum")."</A>"; |
297 | } else { |
298 | $footer = ""; |
299 | } |
300 | |
301 | forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false, $rate=false, $footer); |
302 | } |
303 | |
304 | } else { |
305 | echo "<P>".get_string("noposts", "forum")."</P>"; |
306 | } |
caadf009 |
307 | } |
308 | |
1b5910c4 |
309 | function forum_print_recent_activity($course, $isteacher, $timestart) { |
cc3655a2 |
310 | /// Given a course and a date, prints a summary of all the new |
311 | /// messages posted in the course since that date |
312 | |
3d891989 |
313 | global $CFG; |
caadf009 |
314 | |
315 | $heading = false; |
316 | $content = false; |
317 | |
1b5910c4 |
318 | if (!$logs = get_records_select("log", "time > '$timestart' AND ". |
319 | "course = '$course->id' AND ". |
320 | "module = 'forum' AND ". |
321 | "action LIKE 'add %' ", "time ASC")){ |
322 | return false; |
323 | } |
324 | |
dcde9f02 |
325 | $strftimerecent = get_string("strftimerecent"); |
326 | |
1b5910c4 |
327 | foreach ($logs as $log) { |
328 | //Get post info, I'll need it later |
329 | $post = forum_get_post_from_log($log); |
330 | |
331 | //Create a temp valid module structure (course,id) |
332 | $tempmod->course = $log->course; |
333 | $tempmod->id = $post->forum; |
334 | //Obtain the visible property from the instance |
335 | $modvisible = instance_is_visible($log->module,$tempmod); |
336 | |
337 | //Only if the mod is visible |
338 | if ($modvisible) { |
339 | if ($post) { |
340 | $teacheronly = ""; |
341 | if ($forum = get_record("forum", "id", $post->forum) ) { |
342 | if ($forum->type == "teacher") { |
343 | if ($isteacher) { |
344 | $teacheronly = "class=\"teacheronly\""; |
ad08fdc2 |
345 | } else { |
1b5910c4 |
346 | continue; |
ad08fdc2 |
347 | } |
caadf009 |
348 | } |
349 | } |
1b5910c4 |
350 | if (! $heading) { |
351 | print_headline(get_string("newforumposts", "forum").":"); |
352 | $heading = true; |
353 | $content = true; |
354 | } |
355 | $date = userdate($post->modified, $strftimerecent); |
356 | echo "<p $teacheronly><font size=1>$date - $post->firstname $post->lastname<br>"; |
357 | echo "\"<a href=\"$CFG->wwwroot/mod/forum/$log->url\">"; |
358 | if ($log->action == "add discussion") { |
359 | echo "<b>$post->subject</b>"; |
360 | } else { |
361 | echo "$post->subject"; |
362 | } |
363 | echo "</a>\"</font></p>"; |
caadf009 |
364 | } |
365 | } |
366 | } |
1b5910c4 |
367 | |
caadf009 |
368 | return $content; |
369 | } |
370 | |
371 | |
10696dc1 |
372 | |
373 | function forum_print_recent_instance_activity($forum, $timestart, $detail=false) { |
374 | |
375 | global $CFG, $THEME; |
376 | |
377 | if (!$posts = forum_get_recent_posts($timestart, $forum->id)) { |
378 | return false; |
379 | } |
380 | |
381 | foreach ($posts as $post) { |
382 | echo '<table border="0" cellpadding="3" cellspacing="0" class="forumpost">'; |
383 | echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostpicture\" width=\"35\" valign=\"top\">"; |
384 | print_user_picture($post->userid, $forum->course, $post->picture); |
385 | echo "</td>"; |
386 | |
387 | if ($post->parent) { |
388 | echo "<td nowrap bgcolor=\"$THEME->cellheading\" class=\"forumpostheader\" width=\"100%\">"; |
389 | } else { |
390 | echo "<td nowrap bgcolor=\"$THEME->cellheading2\" class=\"forumpostheadertopic\" width=\"100%\">"; |
391 | } |
392 | echo "<p>"; |
393 | echo "<font size=3>"; |
394 | echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion#$post->id\">"; |
395 | echo $post->subject; |
396 | echo "</a></font><br>"; |
397 | echo "<font size=2>"; |
398 | $by->name = "<a href=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$post->firstname $post->lastname</a>"; |
399 | $by->date = userdate($post->modified); |
400 | print_string("bynameondate", "forum", $by); |
401 | echo "</font></p></td></tr></table>"; |
402 | } |
403 | } |
404 | |
405 | |
caadf009 |
406 | function forum_grades($forumid) { |
407 | /// Must return an array of grades, indexed by user, and a max grade. |
408 | global $FORUM_POST_RATINGS; |
409 | |
4db9d14d |
410 | if (!$forum = get_record("forum", "id", $forumid)) { |
411 | return false; |
412 | } |
413 | if (!$forum->assessed) { |
414 | return false; |
415 | } |
1f48942e |
416 | if ($ratings = forum_get_user_grades($forumid)) { |
36df40d6 |
417 | foreach ($ratings as $rating) { |
ebc3bd2b |
418 | $u = $rating->userid; |
36df40d6 |
419 | $r = $rating->rating; |
420 | if (!isset($sumrating[$u])) { |
421 | $sumrating[$u][1] = 0; |
422 | $sumrating[$u][2] = 0; |
423 | $sumrating[$u][3] = 0; |
caadf009 |
424 | } |
36df40d6 |
425 | $sumrating[$u][$r]++; |
caadf009 |
426 | } |
427 | foreach ($sumrating as $user => $rating) { |
428 | $return->grades[$user] = $rating[1]."s/".$rating[2]."/".$rating[3]."c"; |
429 | } |
430 | } else { |
431 | $return->grades = array(); |
432 | } |
433 | |
434 | $return->maxgrade = ""; |
435 | return $return; |
436 | } |
437 | |
438 | |
9fa49e22 |
439 | /// SQL FUNCTIONS /////////////////////////////////////////////////////////// |
440 | |
1f48942e |
441 | function forum_get_post_full($postid) { |
442 | /// Gets a post with all info ready for forum_print_post |
443 | global $CFG; |
444 | |
ebc3bd2b |
445 | return get_record_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture |
1f48942e |
446 | FROM {$CFG->prefix}forum_posts p, |
447 | {$CFG->prefix}user u |
448 | WHERE p.id = '$postid' |
ebc3bd2b |
449 | AND p.userid = u.id"); |
1f48942e |
450 | } |
451 | |
452 | function forum_get_discussion_posts($discussion, $sort) { |
453 | /// Gets posts with all info ready for forum_print_post |
454 | global $CFG; |
455 | |
ebc3bd2b |
456 | return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture |
1f48942e |
457 | FROM {$CFG->prefix}forum_posts p, |
458 | {$CFG->prefix}user u |
459 | WHERE p.discussion = $discussion |
460 | AND p.parent > 0 |
ebc3bd2b |
461 | AND p.userid = u.id $sort"); |
1f48942e |
462 | } |
463 | |
464 | function forum_get_child_posts($parent) { |
465 | /// Gets posts with all info ready for forum_print_post |
466 | global $CFG; |
467 | |
ebc3bd2b |
468 | return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture |
1f48942e |
469 | FROM {$CFG->prefix}forum_posts p, |
470 | {$CFG->prefix}user u |
471 | WHERE p.parent = '$parent' |
ebc3bd2b |
472 | AND p.userid = u.id |
1f48942e |
473 | ORDER BY p.created ASC"); |
474 | } |
475 | |
476 | |
a74f950c |
477 | function forum_search_posts($search, $courseid, $page=0, $recordsperpage=50) { |
9fa49e22 |
478 | /// Returns a list of posts that were found |
479 | global $CFG; |
480 | |
481 | if (!isteacher($courseid)) { |
482 | $notteacherforum = "AND f.type <> 'teacher'"; |
483 | } else { |
484 | $notteacherforum = ""; |
485 | } |
486 | |
c2a96d6b |
487 | switch ($CFG->dbtype) { |
488 | case "mysql": |
489 | $limit = "LIMIT $page,$recordsperpage"; |
490 | break; |
491 | case "postgres7": |
492 | $limit = "LIMIT $recordsperpage OFFSET ".($page * $recordsperpage); |
493 | break; |
494 | default: |
495 | $limit = "LIMIT $recordsperpage,$page"; |
97485d07 |
496 | } |
497 | |
63d99fcb |
498 | /// Some differences in syntax for PostgreSQL |
499 | if ($CFG->dbtype == "postgres7") { |
a74f950c |
500 | $LIKE = "ILIKE"; // case-insensitive |
2b4c64e9 |
501 | $NOTLIKE = "NOT ILIKE"; // case-insensitive |
502 | $REGEXP = "~*"; |
503 | $NOTREGEXP = "!~*"; |
b800ac5a |
504 | } else { |
a74f950c |
505 | $LIKE = "LIKE"; |
2b4c64e9 |
506 | $NOTLIKE = "NOT LIKE"; |
a74f950c |
507 | $REGEXP = "REGEXP"; |
2b4c64e9 |
508 | $NOTREGEXP = "NOT REGEXP"; |
c4c57597 |
509 | } |
510 | |
b800ac5a |
511 | $messagesearch = ""; |
512 | $subjectsearch = ""; |
513 | |
514 | $searchterms = explode(" ", $search); // Search for words independently |
515 | |
516 | foreach ($searchterms as $searchterm) { |
517 | if ($messagesearch) { |
518 | $messagesearch .= " AND "; |
519 | } |
b800ac5a |
520 | if ($subjectsearch) { |
521 | $subjectsearch .= " AND "; |
522 | } |
63d99fcb |
523 | |
a74f950c |
524 | if (substr($searchterm,0,1) == "+") { |
525 | $searchterm = substr($searchterm,1); |
63d99fcb |
526 | $messagesearch .= " p.message $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; |
527 | $subjectsearch .= " p.subject $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; |
a74f950c |
528 | } else if (substr($searchterm,0,1) == "-") { |
529 | $searchterm = substr($searchterm,1); |
2b4c64e9 |
530 | $messagesearch .= " p.message $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; |
531 | $subjectsearch .= " p.subject $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; |
63d99fcb |
532 | } else { |
533 | $messagesearch .= " p.message $LIKE '%$searchterm%' "; |
534 | $subjectsearch .= " p.subject $LIKE '%$searchterm%' "; |
535 | } |
b800ac5a |
536 | } |
537 | |
538 | |
ebc3bd2b |
539 | return get_records_sql("SELECT p.*,u.firstname,u.lastname,u.email,u.picture |
9fa49e22 |
540 | FROM {$CFG->prefix}forum_posts p, |
541 | {$CFG->prefix}forum_discussions d, |
542 | {$CFG->prefix}user u, |
543 | {$CFG->prefix}forum f |
b800ac5a |
544 | WHERE ($messagesearch OR $subjectsearch) |
ebc3bd2b |
545 | AND p.userid = u.id |
9fa49e22 |
546 | AND p.discussion = d.id |
547 | AND d.course = '$courseid' |
548 | AND d.forum = f.id |
549 | $notteacherforum |
97485d07 |
550 | ORDER BY p.modified DESC $limit"); |
9fa49e22 |
551 | } |
552 | |
63d99fcb |
553 | |
9fa49e22 |
554 | function forum_get_ratings($postid, $sort="u.firstname ASC") { |
555 | /// Returns a list of ratings for a particular post - sorted. |
556 | global $CFG; |
557 | return get_records_sql("SELECT u.*, r.rating, r.time |
558 | FROM {$CFG->prefix}forum_ratings r, |
559 | {$CFG->prefix}user u |
1f48942e |
560 | WHERE r.post = '$postid' |
ebc3bd2b |
561 | AND r.userid = u.id |
9fa49e22 |
562 | ORDER BY $sort"); |
563 | } |
564 | |
1f48942e |
565 | function forum_get_unmailed_posts($cutofftime) { |
566 | /// Returns a list of all new posts that have not been mailed yet |
567 | global $CFG; |
568 | return get_records_sql("SELECT p.*, d.course |
569 | FROM {$CFG->prefix}forum_posts p, |
570 | {$CFG->prefix}forum_discussions d |
571 | WHERE p.mailed = 0 |
572 | AND p.created < '$cutofftime' |
573 | AND p.discussion = d.id"); |
574 | } |
575 | |
576 | function forum_get_user_posts($forumid, $userid) { |
577 | /// Get all the posts for a user in a forum suitable for forum_print_post |
578 | global $CFG; |
579 | |
ebc3bd2b |
580 | return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture |
1f48942e |
581 | FROM {$CFG->prefix}forum f, |
582 | {$CFG->prefix}forum_discussions d, |
583 | {$CFG->prefix}forum_posts p, |
584 | {$CFG->prefix}user u |
585 | WHERE f.id = '$forumid' |
586 | AND d.forum = f.id |
587 | AND p.discussion = d.id |
ebc3bd2b |
588 | AND p.userid = '$userid' |
589 | AND p.userid = u.id |
1f48942e |
590 | ORDER BY p.modified ASC"); |
591 | } |
592 | |
593 | function forum_get_post_from_log($log) { |
594 | /// Given a log entry, return the forum post details for it. |
595 | global $CFG; |
596 | |
597 | if ($log->action == "add post") { |
598 | |
ebc3bd2b |
599 | return get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname, u.email, u.picture |
1f48942e |
600 | FROM {$CFG->prefix}forum_discussions d, |
601 | {$CFG->prefix}forum_posts p, |
602 | {$CFG->prefix}user u |
603 | WHERE p.id = '$log->info' |
604 | AND d.id = p.discussion |
ebc3bd2b |
605 | AND p.userid = u.id |
1f48942e |
606 | AND u.deleted <> '1'"); |
607 | |
608 | |
609 | } else if ($log->action == "add discussion") { |
610 | |
ebc3bd2b |
611 | return get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname, u.email, u.picture |
1f48942e |
612 | FROM {$CFG->prefix}forum_discussions d, |
613 | {$CFG->prefix}forum_posts p, |
614 | {$CFG->prefix}user u |
615 | WHERE d.id = '$log->info' |
616 | AND d.firstpost = p.id |
ebc3bd2b |
617 | AND p.userid = u.id |
1f48942e |
618 | AND u.deleted <> '1'"); |
619 | } |
620 | return NULL; |
621 | } |
622 | |
623 | |
624 | function forum_get_user_grades($forumid) { |
625 | /// Get all user grades for a forum |
626 | global $CFG; |
627 | |
ebc3bd2b |
628 | return get_records_sql("SELECT r.id, p.userid, r.rating |
1f48942e |
629 | FROM {$CFG->prefix}forum_discussions d, |
630 | {$CFG->prefix}forum_posts p, |
631 | {$CFG->prefix}forum_ratings r |
632 | WHERE d.forum = '$forumid' |
633 | AND p.discussion = d.id |
634 | AND r.post = p.id"); |
635 | } |
636 | |
637 | |
638 | function forum_count_discussion_replies($forum="0") { |
639 | // Returns an array of counts of replies to each discussion (optionally in one forum) |
640 | global $CFG; |
641 | |
642 | if ($forum) { |
643 | $forumselect = " AND d.forum = '$forum'"; |
644 | } |
645 | return get_records_sql("SELECT p.discussion, (count(*)) as replies |
646 | FROM {$CFG->prefix}forum_posts p, |
647 | {$CFG->prefix}forum_discussions d |
648 | WHERE p.parent > 0 |
649 | AND p.discussion = d.id |
650 | GROUP BY p.discussion"); |
651 | } |
652 | |
653 | function forum_count_unrated_posts($discussionid, $userid) { |
654 | // How many unrated posts are in the given discussion for a given user? |
655 | global $CFG; |
656 | if ($posts = get_record_sql("SELECT count(*) as num |
657 | FROM {$CFG->prefix}forum_posts |
658 | WHERE parent > 0 |
659 | AND discussion = '$discussionid' |
ebc3bd2b |
660 | AND userid <> '$userid' ")) { |
1f48942e |
661 | |
662 | if ($rated = get_record_sql("SELECT count(*) as num |
663 | FROM {$CFG->prefix}forum_posts p, |
664 | {$CFG->prefix}forum_ratings r |
665 | WHERE p.discussion = '$discussionid' |
666 | AND p.id = r.post |
ebc3bd2b |
667 | AND r.userid = '$userid'")) { |
1f48942e |
668 | $difference = $posts->num - $rated->num; |
669 | if ($difference > 0) { |
670 | return $difference; |
671 | } else { |
672 | return 0; // Just in case there was a counting error |
673 | } |
674 | } else { |
675 | return $posts->num; |
676 | } |
677 | } else { |
678 | return 0; |
679 | } |
680 | } |
681 | |
2ab968e9 |
682 | function forum_get_discussions($forum="0", $forumsort="d.timemodified DESC", $user=0, $fullpost=true) { |
1f48942e |
683 | /// Get all discussions in a forum |
684 | global $CFG; |
685 | |
686 | if ($user) { |
687 | $userselect = " AND u.id = '$user' "; |
688 | } else { |
689 | $userselect = ""; |
690 | } |
29507631 |
691 | if (empty($forumsort)) { |
692 | $forumsort = "d.timemodified DESC"; |
693 | } |
2ab968e9 |
694 | if (empty($fullpost)) { |
b879effb |
695 | $postdata = "p.id,p.subject,p.modified,p.discussion,p.userid"; |
2ab968e9 |
696 | } else { |
697 | $postdata = "p.*"; |
698 | } |
699 | return get_records_sql("SELECT $postdata, d.timemodified, u.firstname, u.lastname, u.email, u.picture |
1f48942e |
700 | FROM {$CFG->prefix}forum_discussions d, |
701 | {$CFG->prefix}forum_posts p, |
702 | {$CFG->prefix}user u |
703 | WHERE d.forum = '$forum' |
704 | AND p.discussion = d.id |
2ab968e9 |
705 | AND p.parent = 0 |
ebc3bd2b |
706 | AND p.userid = u.id $userselect |
29507631 |
707 | ORDER BY $forumsort"); |
1f48942e |
708 | } |
709 | |
710 | |
711 | |
712 | function forum_get_user_discussions($courseid, $userid) { |
713 | /// Get all discussions started by a particular user in a course |
714 | global $CFG; |
715 | |
716 | return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, |
ebc3bd2b |
717 | f.type as forumtype, f.name as forumname, f.id as forumid |
1f48942e |
718 | FROM {$CFG->prefix}forum_discussions d, |
719 | {$CFG->prefix}forum_posts p, |
720 | {$CFG->prefix}user u, |
721 | {$CFG->prefix}forum f |
722 | WHERE d.course = '$courseid' |
723 | AND p.discussion = d.id |
724 | AND p.parent = 0 |
ebc3bd2b |
725 | AND p.userid = u.id |
1f48942e |
726 | AND u.id = '$userid' |
727 | AND d.forum = f.id |
b8bf90c5 |
728 | ORDER BY p.created DESC"); |
1f48942e |
729 | } |
730 | |
731 | |
732 | function forum_subscribed_users($course, $forum) { |
733 | /// Returns list of user objects that are subscribed to this forum |
734 | global $CFG; |
735 | |
adaf3928 |
736 | if ($forum->forcesubscribe) { |
737 | if ($course->category) { |
170a9c46 |
738 | if ($forum->type == "teacher") { |
739 | return get_course_teachers($course->id); // Only teachers can be subscribed to teacher forums |
740 | } else { |
741 | return get_course_users($course->id); // Otherwise get everyone in the course |
742 | } |
adaf3928 |
743 | } else { |
2d0b30a0 |
744 | return get_site_users(); |
1f48942e |
745 | } |
746 | } |
713386aa |
747 | return get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, |
0351b1f9 |
748 | u.email, u.city, u.country, u.lastaccess, u.lastlogin, u.picture, u.timezone, u.lang |
1f48942e |
749 | FROM {$CFG->prefix}user u, |
750 | {$CFG->prefix}forum_subscriptions s |
751 | WHERE s.forum = '$forum->id' |
ebc3bd2b |
752 | AND s.userid = u.id |
7c81b6e2 |
753 | AND u.deleted <> 1 |
754 | ORDER BY u.email ASC"); |
1f48942e |
755 | } |
9fa49e22 |
756 | |
10696dc1 |
757 | function forum_get_recent_posts($sincetime, $forum="0") { |
758 | // Returns all forum posts since a given time. If forum is specified then |
759 | // this restricts the results |
760 | |
761 | global $CFG; |
762 | |
763 | if ($forum) { |
764 | $forumselect = " AND d.forum = '$forum'"; |
765 | } else { |
766 | $forumselect = ""; |
767 | } |
768 | |
9400ee33 |
769 | return get_records_sql("SELECT p.*, d.name, u.id, u.firstname, u.lastname, u.picture |
10696dc1 |
770 | FROM {$CFG->prefix}forum_posts p, |
771 | {$CFG->prefix}forum_discussions d, |
772 | {$CFG->prefix}user u |
773 | WHERE p.modified > '$sincetime' $forumselect |
774 | AND p.userid = u.id |
775 | AND p.discussion = d.id |
776 | ORDER BY p.discussion ASC"); |
777 | } |
778 | |
9fa49e22 |
779 | |
caadf009 |
780 | /// OTHER FUNCTIONS /////////////////////////////////////////////////////////// |
f93f848a |
781 | |
782 | |
11b0c469 |
783 | function forum_get_course_forum($courseid, $type) { |
784 | // How to set up special 1-per-course forums |
a6fcdf98 |
785 | global $CFG; |
786 | |
29cbd93a |
787 | if ($forums = get_records_select("forum", "course = '$courseid' AND type = '$type'", "id ASC")) { |
788 | // There should always only be ONE, but with the right combination of |
789 | // errors there might be more. In this case, just return the oldest one (lowest ID). |
790 | foreach ($forums as $forum) { |
791 | return $forum; // ie the first one |
11b0c469 |
792 | } |
8daaf761 |
793 | } |
e6874d9f |
794 | |
8daaf761 |
795 | // Doesn't exist, so create one now. |
796 | $forum->course = $courseid; |
797 | $forum->type = "$type"; |
798 | switch ($forum->type) { |
799 | case "news": |
800 | $forum->name = get_string("namenews", "forum"); |
801 | $forum->forcesubscribe = 1; |
802 | $forum->intro = get_string("intronews", "forum"); |
803 | $forum->open = 1; // 0 - no, 1 - posts only, 2 - discuss and post |
804 | $forum->assessed = 0; |
805 | if ($site = get_site()) { |
806 | if ($courseid == $site->id) { |
807 | $forum->name = get_string("sitenews"); |
808 | $forum->forcesubscribe = 0; |
809 | } |
a6fcdf98 |
810 | } |
8daaf761 |
811 | break; |
812 | case "social": |
813 | $forum->name = get_string("namesocial", "forum"); |
814 | $forum->intro = get_string("introsocial", "forum"); |
815 | $forum->open = 2; // 0 - no, 1 - posts only, 2 - discuss and post |
816 | $forum->assessed = 0; |
817 | $forum->forcesubscribe = 0; |
818 | break; |
819 | case "teacher": |
820 | $forum->name = get_string("nameteacher", "forum"); |
821 | $forum->intro = get_string("introteacher", "forum"); |
822 | $forum->open = 0; // 0 - no, 1 - posts only, 2 - discuss and post |
823 | $forum->assessed = 0; |
824 | $forum->forcesubscribe = 0; |
825 | break; |
826 | default: |
827 | notify("That forum type doesn't exist!"); |
828 | return false; |
829 | break; |
830 | } |
831 | |
832 | $forum->timemodified = time(); |
833 | $forum->id = insert_record("forum", $forum); |
834 | |
835 | if ($forum->type != "teacher") { |
836 | if (! $module = get_record("modules", "name", "forum")) { |
837 | notify("Could not find forum module!!"); |
838 | return false; |
839 | } |
840 | $mod->course = $courseid; |
841 | $mod->module = $module->id; |
842 | $mod->instance = $forum->id; |
843 | $mod->section = 0; |
844 | if (! $mod->coursemodule = add_course_module($mod) ) { // assumes course/lib.php is loaded |
845 | notify("Could not add a new course module to the course '$course->fullname'"); |
846 | return false; |
847 | } |
848 | if (! $sectionid = add_mod_to_section($mod) ) { // assumes course/lib.php is loaded |
849 | notify("Could not add the new course module to that section"); |
850 | return false; |
851 | } |
852 | if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) { |
853 | notify("Could not update the course module with the correct section"); |
854 | return false; |
855 | } |
856 | include_once("$CFG->dirroot/course/lib.php"); |
d769d2ee |
857 | rebuild_course_cache($courseid); |
82aa0e8d |
858 | } |
8daaf761 |
859 | |
860 | return get_record("forum", "id", "$forum->id"); |
82aa0e8d |
861 | } |
862 | |
f93f848a |
863 | |
11b0c469 |
864 | function forum_make_mail_post(&$post, $user, $touser, $course, |
865 | $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") { |
501cdbd8 |
866 | // Given the data about a posting, builds up the HTML to display it and |
867 | // returns the HTML in a string. This is designed for sending via HTML email. |
868 | |
869 | global $THEME, $CFG; |
870 | |
871 | $output = ""; |
872 | |
873 | if ($post->parent) { |
72d497d4 |
874 | $output .= '<table border="0" cellpadding="1" cellspacing="1"><tr><td bgcolor="#888888">'; |
875 | $output .= '<table border="0" cellpadding="3" cellspacing="0">'; |
501cdbd8 |
876 | } else { |
72d497d4 |
877 | $output .= '<table border="0" cellpadding="1" cellspacing="1" width="100%"><tr><td bgcolor="#888888">'; |
878 | $output .= '<table border="0" cellpadding="3" cellspacing="0" width="100%">'; |
501cdbd8 |
879 | } |
880 | |
72d497d4 |
881 | $output .= "<tr><td bgcolor=\"$THEME->cellcontent2\" width=\"35\" valign=\"top\">"; |
501cdbd8 |
882 | $output .= print_user_picture($user->id, $course->id, $user->picture, false, true); |
72d497d4 |
883 | $output .= "</td>"; |
501cdbd8 |
884 | |
885 | if ($post->parent) { |
72d497d4 |
886 | $output .= "<td nowrap bgcolor=\"$THEME->cellheading\">"; |
501cdbd8 |
887 | } else { |
72d497d4 |
888 | $output .= "<td nowrap bgcolor=\"$THEME->cellheading2\">"; |
501cdbd8 |
889 | } |
72d497d4 |
890 | $output .= "<p>"; |
891 | $output .= "<font size=3><b>$post->subject</b></font><br />"; |
892 | $output .= "<font size=2>"; |
893 | $by->name = "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</a>"; |
d62413e8 |
894 | $by->date = userdate($post->modified, "", $touser->timezone); |
ffe11640 |
895 | $output .= get_string("bynameondate", "forum", $by); |
72d497d4 |
896 | $output .= "</font></p></td></tr>"; |
897 | $output .= "<tr><td bgcolor=\"$THEME->cellcontent2\" width=10>"; |
501cdbd8 |
898 | $output .= " "; |
72d497d4 |
899 | $output .= "</td><td bgcolor=\"$THEME->cellcontent\">\n"; |
501cdbd8 |
900 | |
7f6689e4 |
901 | if ($post->attachment) { |
902 | $post->course = $course->id; |
903 | $post->forum = get_field("forum_discussions", "forum", "id", $post->discussion); |
72d497d4 |
904 | $output .= "<div align=right>"; |
7f6689e4 |
905 | $output .= forum_print_attachments($post, "html"); |
72d497d4 |
906 | $output .= "</div>"; |
7f6689e4 |
907 | } |
908 | |
73bb0835 |
909 | $output .= format_text($post->message, $post->format); |
501cdbd8 |
910 | |
72d497d4 |
911 | $output .= "<p align=right><font size=-1>"; |
501cdbd8 |
912 | |
913 | $age = time() - $post->created; |
914 | if ($ownpost) { |
72d497d4 |
915 | $output .= "<a href=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."</a>"; |
501cdbd8 |
916 | if ($reply) { |
72d497d4 |
917 | $output .= " | <a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</a>"; |
501cdbd8 |
918 | } |
919 | $output .= " "; |
920 | } else { |
921 | if ($reply) { |
72d497d4 |
922 | $output .= "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</a> "; |
501cdbd8 |
923 | } |
924 | } |
925 | |
72d497d4 |
926 | $output .= "</p>"; |
927 | $output .= "<div align=right><p align=right>"; |
501cdbd8 |
928 | |
929 | if ($link) { |
930 | if ($post->replies == 1) { |
ffe11640 |
931 | $replystring = get_string("repliesone", "forum", $post->replies); |
501cdbd8 |
932 | } else { |
ffe11640 |
933 | $replystring = get_string("repliesmany", "forum", $post->replies); |
501cdbd8 |
934 | } |
72d497d4 |
935 | $output .= "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><B>".get_string("discussthistopic", "forum")."</b></a> ($replystring) "; |
501cdbd8 |
936 | } |
72d497d4 |
937 | $output .= "</p></div>"; |
501cdbd8 |
938 | if ($footer) { |
72d497d4 |
939 | $output .= "<p>$footer</p>"; |
501cdbd8 |
940 | } |
72d497d4 |
941 | $output .= "</td></tr></table>\n"; |
942 | $output .= "</td></tr></table>\n\n"; |
501cdbd8 |
943 | |
944 | return $output; |
945 | } |
946 | |
947 | |
88438a58 |
948 | function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link=false, $rate=false, $footer="", $highlight="") { |
c585fa17 |
949 | global $THEME, $USER, $CFG; |
501cdbd8 |
950 | |
b22b0e61 |
951 | echo "<a name=\"$post->id\"></a>"; |
501cdbd8 |
952 | if ($post->parent) { |
72d497d4 |
953 | echo '<table border="0" cellpadding="3" cellspacing="0" class="forumpost">'; |
501cdbd8 |
954 | } else { |
72d497d4 |
955 | echo '<table border="0" cellpadding="3" cellspacing="0" class="forumpost" width="100%">'; |
501cdbd8 |
956 | } |
957 | |
72d497d4 |
958 | echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostpicture\" width=\"35\" valign=\"top\">"; |
501cdbd8 |
959 | print_user_picture($post->userid, $courseid, $post->picture); |
72d497d4 |
960 | echo "</td>"; |
501cdbd8 |
961 | |
962 | if ($post->parent) { |
72d497d4 |
963 | echo "<td nowrap bgcolor=\"$THEME->cellheading\" class=\"forumpostheader\" width=\"100%\">"; |
501cdbd8 |
964 | } else { |
72d497d4 |
965 | echo "<td nowrap bgcolor=\"$THEME->cellheading2\" class=\"forumpostheadertopic\" width=\"100%\">"; |
501cdbd8 |
966 | } |
72d497d4 |
967 | echo "<p>"; |
968 | echo "<font size=3><b>$post->subject</b></font><br>"; |
969 | echo "<font size=2>"; |
970 | $by->name = "<a href=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$post->firstname $post->lastname</a>"; |
d62413e8 |
971 | $by->date = userdate($post->modified); |
ffe11640 |
972 | print_string("bynameondate", "forum", $by); |
72d497d4 |
973 | echo "</font></p></td></tr>"; |
974 | echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostside\" width=\"10\">"; |
501cdbd8 |
975 | echo " "; |
72d497d4 |
976 | echo "</td><td bgcolor=\"$THEME->cellcontent\" class=\"forumpostmessage\">\n"; |
501cdbd8 |
977 | |
7f6689e4 |
978 | if ($post->attachment) { |
979 | $post->course = $courseid; |
980 | $post->forum = get_field("forum_discussions", "forum", "id", $post->discussion); |
72d497d4 |
981 | echo "<div align=\"right\">"; |
982 | $attachedimages = forum_print_attachments($post); |
983 | echo "</div>"; |
e9c2dc1f |
984 | } else { |
985 | $attachedimages = ""; |
7f6689e4 |
986 | } |
987 | |
c585fa17 |
988 | if ($link and (strlen(strip_tags($post->message)) > FORUM_LONG_POST)) { |
aa153f29 |
989 | // Print shortened version |
73bb0835 |
990 | echo format_text(forum_shorten_post($post->message), $post->format); |
c585fa17 |
991 | $numwords = count_words(strip_tags($post->message)); |
72d497d4 |
992 | echo "<p><a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">"; |
ffe11640 |
993 | echo get_string("readtherest", "forum"); |
72d497d4 |
994 | echo "</a> (".get_string("numwords", "", $numwords).")...</p>"; |
501cdbd8 |
995 | } else { |
aa153f29 |
996 | // Print whole message |
88438a58 |
997 | if ($highlight) { |
998 | echo highlight($highlight, format_text($post->message, $post->format)); |
999 | } else { |
1000 | echo format_text($post->message, $post->format); |
1001 | } |
c99ce77b |
1002 | echo $attachedimages; |
501cdbd8 |
1003 | } |
1004 | |
72d497d4 |
1005 | echo "<p align=right><font size=-1>"; |
501cdbd8 |
1006 | |
1007 | $age = time() - $post->created; |
1008 | if ($ownpost) { |
1009 | if ($age < $CFG->maxeditingtime) { |
72d497d4 |
1010 | echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?edit=$post->id\">".get_string("edit", "forum")."</a> | "; |
501cdbd8 |
1011 | } |
64eacd6f |
1012 | } |
1013 | if ($ownpost or isteacher($courseid)) { |
72d497d4 |
1014 | echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."</a>"; |
501cdbd8 |
1015 | if ($reply) { |
64eacd6f |
1016 | echo "| "; |
1017 | } else { |
1018 | echo " "; |
501cdbd8 |
1019 | } |
64eacd6f |
1020 | } |
1021 | if ($reply) { |
72d497d4 |
1022 | echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</a>"; |
501cdbd8 |
1023 | echo " "; |
501cdbd8 |
1024 | } |
72d497d4 |
1025 | echo "</p>"; |
501cdbd8 |
1026 | |
72d497d4 |
1027 | echo "<div align=right><p align=right>"; |
501cdbd8 |
1028 | if ($rate && $USER->id) { |
2a3cda19 |
1029 | if (isteacher($courseid)) { |
1030 | forum_print_ratings($post->id); |
1031 | if ($USER->id != $post->userid) { |
1032 | forum_print_rating($post->id, $USER->id); |
1033 | } |
1034 | } else if ($USER->id == $post->userid) { |
7a12aab4 |
1035 | forum_print_ratings($post->id); |
501cdbd8 |
1036 | } else { |
7a12aab4 |
1037 | forum_print_rating($post->id, $USER->id); |
501cdbd8 |
1038 | } |
1039 | } |
1040 | |
1041 | if ($link) { |
1042 | if ($post->replies == 1) { |
ffe11640 |
1043 | $replystring = get_string("repliesone", "forum", $post->replies); |
501cdbd8 |
1044 | } else { |
ffe11640 |
1045 | $replystring = get_string("repliesmany", "forum", $post->replies); |
501cdbd8 |
1046 | } |
72d497d4 |
1047 | echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><b>".get_string("discussthistopic", "forum")."</b></a> ($replystring) "; |
501cdbd8 |
1048 | } |
72d497d4 |
1049 | echo "</p>"; |
501cdbd8 |
1050 | if ($footer) { |
72d497d4 |
1051 | echo "<p>$footer</p>"; |
501cdbd8 |
1052 | } |
72d497d4 |
1053 | echo "</div>"; |
1054 | echo "</td></tr>\n</table>\n\n"; |
501cdbd8 |
1055 | } |
1056 | |
3335f6fb |
1057 | |
2ab968e9 |
1058 | function forum_print_discussion_header(&$post, $courseid, $datestring="") { |
c585fa17 |
1059 | global $THEME, $USER, $CFG; |
3335f6fb |
1060 | |
29507631 |
1061 | echo "<tr class=\"forumpostheader\">"; |
3335f6fb |
1062 | |
29507631 |
1063 | // Topic |
546be6dd |
1064 | echo "<td bgcolor=\"$THEME->cellheading2\" class=\"forumpostheadertopic\" width=\"100%\">"; |
29507631 |
1065 | echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">$post->subject</a>"; |
1066 | echo "</td>\n"; |
1067 | |
1068 | // Picture |
96e301a7 |
1069 | echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderpicture\" width=35>"; |
3335f6fb |
1070 | print_user_picture($post->userid, $courseid, $post->picture); |
29507631 |
1071 | echo "</td>\n"; |
3335f6fb |
1072 | |
29507631 |
1073 | // User name |
96e301a7 |
1074 | echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheadername\" align=left nowrap>"; |
29507631 |
1075 | echo "<a href=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$post->firstname $post->lastname</a>"; |
1076 | echo "</td>\n"; |
1077 | |
1078 | // Replies |
96e301a7 |
1079 | echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderreplies\" align=center nowrap>"; |
2ab968e9 |
1080 | echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">$post->replies</a>"; |
29507631 |
1081 | echo "</td>\n"; |
3335f6fb |
1082 | |
96e301a7 |
1083 | echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderdate\" align=right nowrap>"; |
29507631 |
1084 | if (!empty($post->timemodified)) { |
2ab968e9 |
1085 | echo userdate($post->timemodified, $datestring); |
3335f6fb |
1086 | } else { |
2ab968e9 |
1087 | echo userdate($post->modified, $datestring); |
3335f6fb |
1088 | } |
29507631 |
1089 | echo "</td>\n"; |
1090 | |
1091 | echo "</tr>\n"; |
3335f6fb |
1092 | |
3335f6fb |
1093 | } |
1094 | |
1095 | |
aa153f29 |
1096 | function forum_shorten_post($message) { |
c585fa17 |
1097 | // Given a post object that we already know has a long message |
1098 | // this function truncates the message nicely to the first |
1099 | // sane place between FORUM_LONG_POST and FORUM_SHORT_POST |
1100 | |
1101 | $i = 0; |
1102 | $tag = false; |
1103 | $length = strlen($message); |
1104 | $count = 0; |
1105 | $stopzone = false; |
1106 | $truncate = 0; |
1107 | |
1108 | for ($i=0; $i<$length; $i++) { |
a8afb411 |
1109 | $char = $message[$i]; |
c585fa17 |
1110 | |
1111 | switch ($char) { |
1112 | case "<": |
1113 | $tag = true; |
1114 | break; |
1115 | case ">": |
1116 | $tag = false; |
1117 | break; |
1118 | default: |
1119 | if (!$tag) { |
1120 | if ($stopzone) { |
67f0b4cc |
1121 | if ($char == ".") { |
a8afb411 |
1122 | $truncate = $i+1; |
c585fa17 |
1123 | break 2; |
1124 | } |
1125 | } |
1126 | $count++; |
1127 | } |
a8afb411 |
1128 | break; |
c585fa17 |
1129 | } |
1130 | if (!$stopzone) { |
1131 | if ($count > FORUM_SHORT_POST) { |
1132 | $stopzone = true; |
1133 | } |
1134 | } |
1135 | } |
aa153f29 |
1136 | |
c585fa17 |
1137 | if (!$truncate) { |
a8afb411 |
1138 | $truncate = $i; |
c585fa17 |
1139 | } |
1140 | |
67f0b4cc |
1141 | return substr($message, 0, $truncate); |
aa153f29 |
1142 | } |
1143 | |
501cdbd8 |
1144 | |
7a12aab4 |
1145 | function forum_print_ratings($post) { |
1f48942e |
1146 | if ($ratings = get_records("forum_ratings", "post", $post)) { |
ffe11640 |
1147 | $sumrating[1] = 0; |
1148 | $sumrating[2] = 0; |
1149 | $sumrating[3] = 0; |
501cdbd8 |
1150 | foreach ($ratings as $rating) { |
ffe11640 |
1151 | $sumrating[$rating->rating]++; |
501cdbd8 |
1152 | } |
ffe11640 |
1153 | $summary = $sumrating[1]."s/".$sumrating[2]."/".$sumrating[3]."c"; |
501cdbd8 |
1154 | |
ffe11640 |
1155 | echo get_string("ratings", "forum").": "; |
501cdbd8 |
1156 | link_to_popup_window ("/mod/forum/report.php?id=$post", "ratings", $summary, 400, 550); |
501cdbd8 |
1157 | } |
1158 | } |
1159 | |
7a12aab4 |
1160 | function forum_print_rating($post, $user) { |
501cdbd8 |
1161 | global $FORUM_POST_RATINGS; |
1162 | |
ebc3bd2b |
1163 | if ($rating = get_record("forum_ratings", "userid", $user, "post", $post)) { |
1f48942e |
1164 | if ($FORUM_POST_RATINGS[$rating->rating]) { |
ffe11640 |
1165 | echo "<FONT SIZE=-1>".get_string("youratedthis", "forum").": <FONT COLOR=green>"; |
1f48942e |
1166 | echo $FORUM_POST_RATINGS[$rating->rating]; |
ffe11640 |
1167 | echo "</FONT></FONT>"; |
1168 | return; |
501cdbd8 |
1169 | } |
501cdbd8 |
1170 | } |
ffe11640 |
1171 | choose_from_menu($FORUM_POST_RATINGS, $post, "", get_string("rate", "forum")."..."); |
501cdbd8 |
1172 | } |
1173 | |
7a12aab4 |
1174 | function forum_print_mode_form($discussion, $mode) { |
1175 | GLOBAL $FORUM_LAYOUT_MODES; |
501cdbd8 |
1176 | |
1177 | echo "<CENTER><P>"; |
7a12aab4 |
1178 | popup_form("discuss.php?d=$discussion&mode=", $FORUM_LAYOUT_MODES, "mode", $mode, ""); |
501cdbd8 |
1179 | echo "</P></CENTER>\n"; |
1180 | } |
1181 | |
97485d07 |
1182 | function forum_print_search_form($course, $search="", $return=false, $type="") { |
501cdbd8 |
1183 | global $CFG; |
1184 | |
97485d07 |
1185 | if ($type == "plain") { |
1186 | $output = "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0><TR><TD NOWRAP>"; |
1187 | $output .= "<FORM NAME=search ACTION=\"$CFG->wwwroot/mod/forum/search.php\">"; |
1188 | $output .= "<FONT SIZE=\"-1\">"; |
1189 | $output .= "<INPUT NAME=search TYPE=text SIZE=15 VALUE=\"$search\">"; |
1190 | $output .= "<INPUT VALUE=\"".get_string("searchforums", "forum")."\" TYPE=submit>"; |
1191 | $output .= "</FONT>"; |
1192 | $output .= "<INPUT NAME=id TYPE=hidden VALUE=\"$course->id\">"; |
1193 | $output .= "</FORM>"; |
1194 | $output .= "</TD></TR></TABLE>"; |
1195 | } else { |
1196 | $output = "<TABLE BORDER=0 CELLPADDING=10 CELLSPACING=0><TR><TD ALIGN=CENTER>"; |
1197 | $output .= "<FORM NAME=search ACTION=\"$CFG->wwwroot/mod/forum/search.php\">"; |
1198 | $output .= "<FONT SIZE=\"-1\">"; |
1199 | $output .= "<INPUT NAME=search TYPE=text SIZE=15 VALUE=\"$search\"><BR>"; |
1200 | $output .= "<INPUT VALUE=\"".get_string("searchforums", "forum")."\" TYPE=submit>"; |
1201 | $output .= "</FONT>"; |
1202 | $output .= "<INPUT NAME=id TYPE=hidden VALUE=\"$course->id\">"; |
1203 | $output .= "</FORM>"; |
1204 | $output .= "</TD></TR></TABLE>"; |
1205 | } |
5e367a2d |
1206 | |
1207 | if ($return) { |
1208 | return $output; |
1209 | } |
1210 | echo $output; |
501cdbd8 |
1211 | } |
1212 | |
1213 | |
11b0c469 |
1214 | function forum_set_return() { |
607809b3 |
1215 | global $CFG, $SESSION; |
501cdbd8 |
1216 | |
28e1e8b9 |
1217 | if (! isset($SESSION->fromdiscussion)) { |
48d38fad |
1218 | if (!empty($_SERVER['HTTP_REFERER'])) { |
1219 | $referer = $_SERVER['HTTP_REFERER']; |
1220 | } else { |
1221 | $referer = ""; |
1222 | } |
28e1e8b9 |
1223 | // If the referer is NOT a login screen then save it. |
48d38fad |
1224 | if (! strncasecmp("$CFG->wwwroot/login", $referer, 300)) { |
607809b3 |
1225 | $SESSION->fromdiscussion = $_SERVER["HTTP_REFERER"]; |
28e1e8b9 |
1226 | } |
501cdbd8 |
1227 | } |
1228 | } |
1229 | |
1230 | |
11b0c469 |
1231 | function forum_go_back_to($default) { |
501cdbd8 |
1232 | global $SESSION; |
1233 | |
9c9f7d77 |
1234 | if (!empty($SESSION->fromdiscussion)) { |
501cdbd8 |
1235 | $returnto = $SESSION->fromdiscussion; |
1236 | unset($SESSION->fromdiscussion); |
1237 | return $returnto; |
1238 | } else { |
1239 | return $default; |
1240 | } |
1241 | } |
1242 | |
7f6689e4 |
1243 | function forum_file_area_name($post) { |
1244 | // Creates a directory file name, suitable for make_upload_directory() |
1245 | global $CFG; |
1246 | |
1247 | return "$post->course/$CFG->moddata/forum/$post->forum/$post->id"; |
1248 | } |
1249 | |
1250 | function forum_file_area($post) { |
1251 | return make_upload_directory( forum_file_area_name($post) ); |
1252 | } |
1253 | |
1254 | function forum_delete_old_attachments($post, $exception="") { |
1255 | // Deletes all the user files in the attachments area for a post |
1256 | // EXCEPT for any file named $exception |
1257 | |
1258 | if ($basedir = forum_file_area($post)) { |
1259 | if ($files = get_directory_list($basedir)) { |
1260 | foreach ($files as $file) { |
1261 | if ($file != $exception) { |
1262 | unlink("$basedir/$file"); |
1263 | notify("Existing file '$file' has been deleted!"); |
1264 | } |
1265 | } |
1266 | } |
1267 | if (!$exception) { // Delete directory as well, if empty |
1268 | rmdir("$basedir"); |
1269 | } |
1270 | } |
1271 | } |
1272 | |
1273 | function forum_print_attachments($post, $return=NULL) { |
1274 | // if return=html, then return a html string. |
1275 | // if return=text, then return a text-only string. |
72d497d4 |
1276 | // otherwise, print HTML for non-images, and return image HTML |
7f6689e4 |
1277 | |
1278 | global $CFG; |
1279 | |
1280 | $filearea = forum_file_area_name($post); |
1281 | |
72d497d4 |
1282 | $imagereturn = ""; |
1283 | $output = ""; |
1284 | |
7f6689e4 |
1285 | if ($basedir = forum_file_area($post)) { |
1286 | if ($files = get_directory_list($basedir)) { |
1287 | $strattachment = get_string("attachment", "forum"); |
6afe8558 |
1288 | $strpopupwindow = get_string("popupwindow"); |
7f6689e4 |
1289 | foreach ($files as $file) { |
1290 | $icon = mimeinfo("icon", $file); |
1291 | if ($CFG->slasharguments) { |
1292 | $ffurl = "file.php/$filearea/$file"; |
1293 | } else { |
1294 | $ffurl = "file.php?file=/$filearea/$file"; |
1295 | } |
6afe8558 |
1296 | $image = "<img border=0 src=\"$CFG->wwwroot/files/pix/$icon\" height=16 width=16 alt=\"$strpopupwindow\">"; |
7f6689e4 |
1297 | |
1298 | if ($return == "html") { |
6afe8558 |
1299 | $output .= "<a href=\"$CFG->wwwroot/$ffurl\">$image</a> "; |
1300 | $output .= "<a href=\"$CFG->wwwroot/$ffurl\">$file</a><br />"; |
7f6689e4 |
1301 | |
1302 | } else if ($return == "text") { |
1303 | $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n"; |
1304 | |
1305 | } else { |
72d497d4 |
1306 | if ($icon == "image.gif") { // Image attachments don't get printed as links |
1307 | $imagereturn .= "<br /><img src=\"$CFG->wwwroot/$ffurl\">"; |
1308 | } else { |
1309 | link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment); |
1310 | echo "<a href=\"$CFG->wwwroot/$ffurl\">$file</a>"; |
1311 | echo "<br />"; |
1312 | } |
7f6689e4 |
1313 | } |
1314 | } |
1315 | } |
1316 | } |
72d497d4 |
1317 | |
7f6689e4 |
1318 | if ($return) { |
1319 | return $output; |
1320 | } |
72d497d4 |
1321 | |
1322 | return $imagereturn; |
7f6689e4 |
1323 | } |
1324 | |
1325 | function forum_add_attachment($post, $newfile) { |
1326 | // $post is a full post record, including course and forum |
3b7c1de9 |
1327 | // $newfile is a full upload array from $_FILES |
7f6689e4 |
1328 | // If successful, this function returns the name of the file |
1329 | |
9dd0b378 |
1330 | global $CFG; |
1331 | |
3b7c1de9 |
1332 | if (empty($newfile['name'])) { |
7f6689e4 |
1333 | return ""; |
1334 | } |
1335 | |
1336 | $newfile_name = clean_filename($newfile['name']); |
1337 | |
1338 | if (valid_uploaded_file($newfile)) { |
1339 | if (! $newfile_name) { |
1340 | notify("This file had a wierd filename and couldn't be uploaded"); |
1341 | |
1342 | } else if (! $dir = forum_file_area($post)) { |
1343 | notify("Attachment could not be stored"); |
1344 | $newfile_name = ""; |
1345 | |
1346 | } else { |
1347 | if (move_uploaded_file($newfile['tmp_name'], "$dir/$newfile_name")) { |
9dd0b378 |
1348 | chmod("$dir/$newfile_name", $CFG->directorypermissions); |
7f6689e4 |
1349 | forum_delete_old_attachments($post, $newfile_name); |
1350 | } else { |
1351 | notify("An error happened while saving the file on the server"); |
1352 | $newfile_name = ""; |
1353 | } |
1354 | } |
1355 | } else { |
1356 | $newfile_name = ""; |
1357 | } |
1358 | |
1359 | return $newfile_name; |
1360 | } |
501cdbd8 |
1361 | |
11b0c469 |
1362 | function forum_add_new_post($post) { |
501cdbd8 |
1363 | |
ffe11640 |
1364 | $post->created = $post->modified = time(); |
501cdbd8 |
1365 | $post->mailed = "0"; |
1366 | |
7f6689e4 |
1367 | $newfile = $post->attachment; |
1368 | $post->attachment = ""; |
1369 | |
1370 | if (! $post->id = insert_record("forum_posts", $post)) { |
1371 | return false; |
1372 | } |
1373 | |
1374 | if ($post->attachment = forum_add_attachment($post, $newfile)) { |
1375 | set_field("forum_posts", "attachment", $post->attachment, "id", $post->id); |
1376 | } |
29507631 |
1377 | |
1378 | // Update discussion modified date |
1379 | set_field("forum_discussions", "timemodified", $post->modified, "id", $post->discussion); |
7f6689e4 |
1380 | |
1381 | return $post->id; |
501cdbd8 |
1382 | } |
1383 | |
11b0c469 |
1384 | function forum_update_post($post) { |
501cdbd8 |
1385 | |
ffe11640 |
1386 | $post->modified = time(); |
501cdbd8 |
1387 | |
0ab85112 |
1388 | if (!$post->parent) { // Post is a discussion starter - update discussion title too |
1389 | set_field("forum_discussions", "name", $post->subject, "id", $post->discussion); |
1390 | } |
29507631 |
1391 | |
7f6689e4 |
1392 | if ($newfilename = forum_add_attachment($post, $post->attachment)) { |
1393 | $post->attachment = $newfilename; |
1394 | } else { |
1395 | unset($post->attachment); |
1396 | } |
29507631 |
1397 | |
1398 | // Update discussion modified date |
1399 | set_field("forum_discussions", "timemodified", $post->modified, "id", $post->discussion); |
1400 | |
ffe11640 |
1401 | return update_record("forum_posts", $post); |
501cdbd8 |
1402 | } |
1403 | |
1404 | function forum_add_discussion($discussion) { |
1405 | // Given an object containing all the necessary data, |
1406 | // create a new discussion and return the id |
1407 | |
1408 | GLOBAL $USER; |
1409 | |
1410 | $timenow = time(); |
1411 | |
1412 | // The first post is stored as a real post, and linked |
1413 | // to from the discuss entry. |
1414 | |
1415 | $post->discussion = 0; |
1416 | $post->parent = 0; |
ebc3bd2b |
1417 | $post->userid = $USER->id; |
501cdbd8 |
1418 | $post->created = $timenow; |
1419 | $post->modified = $timenow; |
1420 | $post->mailed = 0; |
1421 | $post->subject = $discussion->name; |
1422 | $post->message = $discussion->intro; |
7f6689e4 |
1423 | $post->attachment = ""; |
1424 | $post->forum = $discussion->forum; |
1425 | $post->course = $discussion->course; |
9f0b8269 |
1426 | $post->format = $discussion->format; |
501cdbd8 |
1427 | |
1428 | if (! $post->id = insert_record("forum_posts", $post) ) { |
1429 | return 0; |
1430 | } |
1431 | |
7f6689e4 |
1432 | if ($post->attachment = forum_add_attachment($post, $discussion->attachment)) { |
1433 | set_field("forum_posts", "attachment", $post->attachment, "id", $post->id); //ignore errors |
1434 | } |
1435 | |
caadf009 |
1436 | // Now do the real module entry |
04eba58f |
1437 | |
caadf009 |
1438 | $discussion->firstpost = $post->id; |
1439 | $discussion->timemodified = $timenow; |
04eba58f |
1440 | |
caadf009 |
1441 | if (! $discussion->id = insert_record("forum_discussions", $discussion) ) { |
1442 | delete_records("forum_posts", "id", $post->id); |
1443 | return 0; |
04eba58f |
1444 | } |
1445 | |
caadf009 |
1446 | // Finally, set the pointer on the post. |
1447 | if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) { |
1448 | delete_records("forum_posts", "id", $post->id); |
1449 | delete_records("forum_discussions", "id", $discussion->id); |
1450 | return 0; |
04eba58f |
1451 | } |
04eba58f |
1452 | |
caadf009 |
1453 | return $discussion->id; |
1454 | } |
04eba58f |
1455 | |
04eba58f |
1456 | |
caadf009 |
1457 | function forum_delete_discussion($discussion) { |
1458 | // $discussion is a discussion record object |
04eba58f |
1459 | |
1460 | $result = true; |
1461 | |
caadf009 |
1462 | if ($posts = get_records("forum_posts", "discussion", $discussion->id)) { |
1463 | foreach ($posts as $post) { |
1464 | $post->course = $discussion->course; |
1465 | $post->forum = $discussion->forum; |
1466 | if (! delete_records("forum_ratings", "post", "$post->id")) { |
1467 | $result = false; |
1468 | } |
1469 | if (! forum_delete_post($post)) { |
04eba58f |
1470 | $result = false; |
1471 | } |
1472 | } |
1473 | } |
1474 | |
caadf009 |
1475 | if (! delete_records("forum_discussions", "id", "$discussion->id")) { |
04eba58f |
1476 | $result = false; |
1477 | } |
1478 | |
1479 | return $result; |
1480 | } |
1481 | |
1482 | |
caadf009 |
1483 | function forum_delete_post($post) { |
1484 | if (delete_records("forum_posts", "id", $post->id)) { |
1485 | delete_records("forum_ratings", "post", $post->id); // Just in case |
1486 | if ($post->attachment) { |
1487 | $discussion = get_record("forum_discussions", "id", $post->discussion); |
1488 | $post->course = $discussion->course; |
1489 | $post->forum = $discussion->forum; |
1490 | forum_delete_old_attachments($post); |
1491 | } |
1492 | return true; |
1493 | } |
1494 | return false; |
1495 | } |
501cdbd8 |
1496 | |
501cdbd8 |
1497 | |
caadf009 |
1498 | function forum_print_user_discussions($courseid, $userid) { |
1499 | global $CFG, $USER; |
501cdbd8 |
1500 | |
b8bf90c5 |
1501 | $maxdiscussions = 10; |
1502 | $countdiscussions = 0; |
1503 | |
1f48942e |
1504 | |
1505 | if ($discussions = forum_get_user_discussions($courseid, $userid)) { |
caadf009 |
1506 | $user = get_record("user", "id", $userid); |
1507 | echo "<HR>"; |
b8bf90c5 |
1508 | print_heading( get_string("discussionsstartedbyrecent", "forum", "$user->firstname $user->lastname") ); |
caadf009 |
1509 | $replies = forum_count_discussion_replies(); |
1510 | foreach ($discussions as $discussion) { |
b8bf90c5 |
1511 | $countdiscussions++; |
1512 | if ($countdiscussions > $maxdiscussions) { |
1513 | break; |
1514 | } |
caadf009 |
1515 | if (($discussion->forumtype == "teacher") and !isteacher($courseid)) { |
ffe11640 |
1516 | continue; |
1517 | } |
47f1da80 |
1518 | if (!empty($replies[$discussion->discussion])) { |
caadf009 |
1519 | $discussion->replies = $replies[$discussion->discussion]->replies; |
1520 | } else { |
1521 | $discussion->replies = 0; |
501cdbd8 |
1522 | } |
caadf009 |
1523 | $inforum = get_string("inforum", "forum", "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname</A>"); |
1524 | $discussion->subject .= " ($inforum)"; |
61e96406 |
1525 | if (!empty($USER->id)) { |
1526 | $ownpost = ($discussion->userid == $USER->id); |
1527 | } else { |
1528 | $ownpost = false; |
1529 | } |
caadf009 |
1530 | forum_print_post($discussion, $courseid, $ownpost, $reply=0, $link=1, $assessed=false); |
1531 | echo "<BR>\n"; |
501cdbd8 |
1532 | } |
1533 | } |
501cdbd8 |
1534 | } |
1535 | |
501cdbd8 |
1536 | function forum_forcesubscribe($forumid, $value=1) { |
1537 | return set_field("forum", "forcesubscribe", $value, "id", $forumid); |
1538 | } |
1539 | |
1540 | function forum_is_forcesubscribed($forumid) { |
1541 | return get_field("forum", "forcesubscribe", "id", $forumid); |
1542 | } |
1543 | |
1544 | function forum_is_subscribed($userid, $forumid) { |
1545 | if (forum_is_forcesubscribed($forumid)) { |
1546 | return true; |
1547 | } |
ebc3bd2b |
1548 | return record_exists("forum_subscriptions", "userid", $userid, "forum", $forumid); |
86970225 |
1549 | } |
1550 | |
501cdbd8 |
1551 | function forum_subscribe($userid, $forumid) { |
9fa49e22 |
1552 | /// Adds user to the subscriber list |
1553 | |
ebc3bd2b |
1554 | $sub->userid = $userid; |
9fa49e22 |
1555 | $sub->forum = $forumid; |
501cdbd8 |
1556 | |
9fa49e22 |
1557 | return insert_record("forum_subscriptions", $sub); |
501cdbd8 |
1558 | } |
1559 | |
1560 | function forum_unsubscribe($userid, $forumid) { |
9fa49e22 |
1561 | /// Removes user from the subscriber list |
ebc3bd2b |
1562 | return delete_records("forum_subscriptions", "userid", $userid, "forum", $forumid); |
501cdbd8 |
1563 | } |
1564 | |
0a9f61b5 |
1565 | function forum_post_subscription($post) { |
1566 | /// Given a new post, subscribes or unsubscribes as appropriate. |
1567 | /// Returns some text which describes what happened. |
1568 | |
1569 | global $USER; |
1570 | |
1571 | if (empty($post->subscribe) and empty($post->unsubscribe)) { |
1572 | return ""; |
1573 | } |
1574 | |
1575 | if (!$forum = get_record("forum", "id", $post->forum)) { |
1576 | return ""; |
1577 | } |
1578 | |
1579 | $info->name = "$USER->firstname $USER->lastname"; |
1580 | $info->forum = $forum->name; |
1581 | |
1582 | if (!empty($post->subscribe)) { |
1583 | forum_subscribe($USER->id, $post->forum); |
1584 | return "<p>".get_string("nowsubscribed", "forum", $info)."</p>"; |
1585 | } |
1586 | |
1587 | forum_unsubscribe($USER->id, $post->forum); |
1588 | return "<p>".get_string("nownotsubscribed", "forum", $info)."</p>"; |
1589 | } |
1590 | |
501cdbd8 |
1591 | |
11b0c469 |
1592 | function forum_user_has_posted_discussion($forumid, $userid) { |
29507631 |
1593 | if ($discussions = forum_get_discussions($forumid, "", $userid)) { |
501cdbd8 |
1594 | return true; |
1595 | } else { |
1596 | return false; |
1597 | } |
1598 | } |
1599 | |
11b0c469 |
1600 | function forum_user_can_post_discussion($forum) { |
501cdbd8 |
1601 | // $forum is an object |
1602 | global $USER; |
1603 | |
1604 | if ($forum->type == "eachuser") { |
11b0c469 |
1605 | return (! forum_user_has_posted_discussion($forum->id, $USER->id)); |
501cdbd8 |
1606 | } else if ($forum->type == "teacher") { |
1607 | return isteacher($forum->course); |
1608 | } else if (isteacher($forum->course)) { |
1609 | return true; |
1610 | } else { |
70c476a7 |
1611 | return ($forum->open == 2); |
501cdbd8 |
1612 | } |
1613 | } |
1614 | |
f690562f |
1615 | function forum_user_can_post($forum, $user=NULL) { |
1616 | // $forum, $user are objects |
1617 | |
1618 | if ($user) { |
1619 | $isteacher = isteacher($forum->course, $user->id); |
1620 | } else { |
1621 | $isteacher = isteacher($forum->course); |
1622 | } |
70c476a7 |
1623 | |
1624 | if ($forum->type == "teacher") { |
f690562f |
1625 | return $isteacher; |
1626 | } else if ($isteacher) { |
70c476a7 |
1627 | return true; |
1628 | } else { |
1629 | return $forum->open; |
1630 | } |
1631 | } |
501cdbd8 |
1632 | |
501cdbd8 |
1633 | |
29507631 |
1634 | function forum_print_latest_discussions($forum_id=0, $forum_numdiscussions=5, $forum_style="plain", $forum_sort="") { |
c585fa17 |
1635 | global $CFG, $USER; |
f93f848a |
1636 | |
1637 | if ($forum_id) { |
1638 | if (! $forum = get_record("forum", "id", $forum_id)) { |
1639 | error("Forum ID was incorrect"); |
1640 | } |
1641 | if (! $course = get_record("course", "id", $forum->course)) { |
1642 | error("Could not find the course this forum belongs to!"); |
1643 | } |
1644 | |
1645 | if ($course->category) { |
1646 | require_login($course->id); |
1647 | } |
1648 | |
1649 | } else { |
1650 | if (! $course = get_record("course", "category", 0)) { |
1651 | error("Could not find a top-level course!"); |
1652 | } |
7beb45d8 |
1653 | if (! $forum = forum_get_course_forum($course->id, "news")) { |
f93f848a |
1654 | error("Could not find or create a main forum in this course (id $course->id)"); |
1655 | } |
1656 | } |
1657 | |
11b0c469 |
1658 | if (forum_user_can_post_discussion($forum)) { |
b879effb |
1659 | echo "<p align=center>"; |
1660 | echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?forum=$forum->id\">"; |
0351b1f9 |
1661 | if ($forum->type == "news") { |
1662 | echo get_string("addanewtopic", "forum")."</a>..."; |
1663 | } else { |
1664 | echo get_string("addanewdiscussion", "forum")."</a>..."; |
1665 | } |
b879effb |
1666 | echo "</p>\n"; |
77305fe6 |
1667 | } |
1668 | |
3c8a606d |
1669 | if ((!$forum_numdiscussions) && ($forum_style == "plain")) { |
1670 | $forum_style = "header"; // Abbreviate display by default |
3335f6fb |
1671 | } |
f93f848a |
1672 | |
2ab968e9 |
1673 | if ($forum_style == "minimal") { |
1674 | $forum_sort = "p.modified DESC"; |
1675 | } |
1676 | |
1677 | $fullpost = false; |
1678 | if ($forum_style == "plain") { |
1679 | $fullpost = true; |
1680 | } |
1681 | |
1682 | if (! $discussions = forum_get_discussions($forum->id, $forum_sort, 0, $fullpost) ) { |
0351b1f9 |
1683 | if ($forum->type == "news") { |
1684 | echo "<p align=center><b>(".get_string("nonews", "forum").")</b></p>"; |
1685 | } else { |
1686 | echo "<p align=center><b>(".get_string("nodiscussions", "forum").")</b></p>"; |
1687 | } |
2ab968e9 |
1688 | return; |
1689 | } |
1690 | |
3335f6fb |
1691 | $replies = forum_count_discussion_replies($forum->id); |
f93f848a |
1692 | |
3260de67 |
1693 | $canreply = forum_user_can_post($forum); |
1694 | |
3335f6fb |
1695 | $discussioncount = 0; |
c20b762a |
1696 | $olddiscussionlink = false; |
2ab968e9 |
1697 | $strdatestring = get_string("strftimedaydatetime"); |
f93f848a |
1698 | |
dcde9f02 |
1699 | if ($forum_style == "minimal") { |
1700 | $strftimerecent = get_string("strftimerecent"); |
1701 | $strmore = get_string("more", "forum"); |
1702 | } |
1703 | |
29507631 |
1704 | if ($forum_style == "header") { |
1705 | echo "<table width=\"100%\" border=0 cellpadding=3 cellspacing=1 class=\"forumpost\">"; |
03aa0dfa |
1706 | echo "<tr class=\"forumpostheader\">"; |
9e0a08bd |
1707 | echo "<th>".get_string("discussion", "forum")."</th>"; |
29507631 |
1708 | echo "<th colspan=2>".get_string("startedby", "forum")."</th>"; |
1709 | echo "<th>".get_string("replies", "forum")."</th>"; |
1710 | echo "<th>".get_string("lastpost", "forum")."</th>"; |
1711 | echo "</tr>"; |
1712 | } |
1713 | |
3335f6fb |
1714 | foreach ($discussions as $discussion) { |
1715 | $discussioncount++; |
f93f848a |
1716 | |
3335f6fb |
1717 | if ($forum_numdiscussions && ($discussioncount > $forum_numdiscussions)) { |
c20b762a |
1718 | $olddiscussionlink = true; |
3335f6fb |
1719 | break; |
1720 | } |
9c9f7d77 |
1721 | if (!empty($replies[$discussion->discussion])) { |
3335f6fb |
1722 | $discussion->replies = $replies[$discussion->discussion]->replies; |
1723 | } else { |
1724 | $discussion->replies = 0; |
1725 | } |
f7477444 |
1726 | if (!empty($USER->id)) { |
1727 | $ownpost = ($discussion->userid == $USER->id); |
1728 | } else { |
1729 | $ownpost=false; |
1730 | } |
3335f6fb |
1731 | switch ($forum_style) { |
1732 | case "minimal": |
b879effb |
1733 | echo "<p><font color=#555555>".userdate($discussion->modified, $strftimerecent)." - $discussion->firstname</font>"; |
1734 | echo "<br>$discussion->subject "; |
1735 | echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->discussion\">"; |
1736 | echo $strmore."...</a>"; |
1737 | echo "</p>\n"; |
3335f6fb |
1738 | break; |
1739 | case "header": |
2ab968e9 |
1740 | forum_print_discussion_header($discussion, $forum->course, $strdatestring); |
3335f6fb |
1741 | break; |
1742 | default: |
3260de67 |
1743 | if ($canreply or $discussion->replies) { |
1744 | $link = true; |
1745 | } else { |
1746 | $link = false; |
1747 | } |
1748 | forum_print_post($discussion, $forum->course, $ownpost, $reply=0, $link, $assessed=false); |
b879effb |
1749 | echo "<br>\n"; |
3335f6fb |
1750 | break; |
f93f848a |
1751 | } |
1752 | } |
29507631 |
1753 | |
1754 | if ($forum_style == "header") { |
1755 | echo "</table>"; |
1756 | } |
c20b762a |
1757 | |
1758 | if ($olddiscussionlink) { |
1759 | echo "<p align=right><a href=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id&showall=1\">"; |
1760 | echo get_string("olderdiscussions", "forum")."</a> ...</p>"; |
1761 | } |
f93f848a |
1762 | } |
1763 | |
fe25fc9b |
1764 | function forum_print_discussion($course, $forum, $discussion, $post, $mode) { |
501cdbd8 |
1765 | |
1766 | global $USER; |
1767 | |
61e96406 |
1768 | if (!empty($USER->id)) { |
1769 | $ownpost = ($USER->id == $post->userid); |
1770 | } else { |
1771 | $ownpost = false; |
1772 | } |
70c476a7 |
1773 | $reply = forum_user_can_post($forum); |
501cdbd8 |
1774 | |
70c476a7 |
1775 | forum_print_post($post, $course->id, $ownpost, $reply, $link=false, $rate=false); |
501cdbd8 |
1776 | |
7a12aab4 |
1777 | forum_print_mode_form($discussion->id, $mode); |
501cdbd8 |
1778 | |
9d1b97c5 |
1779 | $ratingform = false; |
61e96406 |
1780 | if ($forum->assessed and !empty($USER->id)) { |
9d1b97c5 |
1781 | $unrated = forum_count_unrated_posts($discussion->id, $USER->id); |
1782 | if ($unrated > 0) { |
1783 | $ratingform = true; |
1784 | } |
1785 | } |
1786 | |
1787 | if ($ratingform) { |
501cdbd8 |
1788 | echo "<FORM NAME=form METHOD=POST ACTION=rate.php>"; |
1789 | echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">"; |
1790 | } |
1791 | |
1792 | switch ($mode) { |
1793 | case 1 : // Flat ascending |
1794 | case -1 : // Flat descending |
1795 | default: |
1796 | echo "<UL>"; |
70c476a7 |
1797 | forum_print_posts_flat($post->discussion, $course->id, $mode, $forum->assessed, $reply); |
501cdbd8 |
1798 | echo "</UL>"; |
1799 | break; |
1800 | |
1801 | case 2 : // Threaded |
70c476a7 |
1802 | forum_print_posts_threaded($post->id, $course->id, 0, $forum->assessed, $reply); |
501cdbd8 |
1803 | break; |
1804 | |
1805 | case 3 : // Nested |
70c476a7 |
1806 | forum_print_posts_nested($post->id, $course->id, $forum->assessed, $reply); |
501cdbd8 |
1807 | break; |
1808 | } |
1809 | |
9d1b97c5 |
1810 | if ($ratingform) { |
31d160d3 |
1811 | echo "<CENTER><P ALIGN=center><INPUT TYPE=submit VALUE=\"".get_string("sendinratings", "forum")."\">"; |
61ee082f |
1812 | helpbutton("ratings", get_string("separateandconnected"), "forum"); |
31d160d3 |
1813 | echo "</P></CENTER>"; |
501cdbd8 |
1814 | echo "</FORM>"; |
1815 | } |
1816 | } |
1817 | |
70c476a7 |
1818 | function forum_print_posts_flat($discussion, $course, $direction, $assessed, $reply) { |
501cdbd8 |
1819 | global $USER; |
1820 | |
501cdbd8 |
1821 | $link = false; |
1822 | |
1823 | if ($direction < 0) { |
1824 | $sort = "ORDER BY created DESC"; |
1825 | } else { |
1826 | $sort = "ORDER BY created ASC"; |
1827 | } |
1828 | |
1f48942e |
1829 | if ($posts = forum_get_discussion_posts($discussion, $sort)) { |
501cdbd8 |
1830 | foreach ($posts as $post) { |
ebc3bd2b |
1831 | $ownpost = ($USER->id == $post->userid); |
11b0c469 |
1832 | forum_print_post($post, $course, $ownpost, $reply, $link, $assessed); |
501cdbd8 |
1833 | } |
1834 | } else { |
1835 | return; |
1836 | } |
1837 | } |
1838 | |
70c476a7 |
1839 | function forum_print_posts_threaded($parent, $course, $depth, $assessed, $reply) { |
501cdbd8 |
1840 | global $USER; |
1841 | |
501cdbd8 |
1842 | $link = false; |
1843 | |
1f48942e |
1844 | if ($posts = forum_get_child_posts($parent)) { |
501cdbd8 |
1845 | foreach ($posts as $post) { |
1846 | |
f95c2a73 |
1847 | echo "<ul>"; |
501cdbd8 |
1848 | if ($depth > 0) { |
ebc3bd2b |
1849 | $ownpost = ($USER->id == $post->userid); |
11b0c469 |
1850 | forum_print_post($post, $course, $ownpost, $reply, $link, $assessed); // link=true? |
f95c2a73 |
1851 | echo "<br />"; |
501cdbd8 |
1852 | } else { |
8c3c8481 |
1853 | $by->name = "$post->firstname $post->lastname"; |
d62413e8 |
1854 | $by->date = userdate($post->modified); |
f95c2a73 |
1855 | echo "<li><p><a name=\"$post->id\"></a><font size=-1><b><a href=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</a></b> "; |
8c3c8481 |
1856 | print_string("bynameondate", "forum", $by); |
f95c2a73 |
1857 | echo "</font></p></li>"; |
501cdbd8 |
1858 | } |
1859 | |
70c476a7 |
1860 | forum_print_posts_threaded($post->id, $course, $depth-1, $assessed, $reply); |
f95c2a73 |
1861 | echo "</ul>\n"; |
501cdbd8 |
1862 | } |
1863 | } else { |
1864 | return; |
1865 | } |
1866 | } |
1867 | |
70c476a7 |
1868 | function forum_print_posts_nested($parent, $course, $assessed, $reply) { |
501cdbd8 |
1869 | global $USER; |
1870 | |
501cdbd8 |
1871 | $link = false; |
1872 | |
1f48942e |
1873 | if ($posts = forum_get_child_posts($parent)) { |
501cdbd8 |
1874 | foreach ($posts as $post) { |
1875 | |
61e96406 |
1876 | if (empty($USER->id)) { |
1877 | $ownpost = false; |
1878 | } else { |
1879 | $ownpost = ($USER->id == $post->userid); |
1880 | } |
501cdbd8 |
1881 | |
1882 | echo "<UL>"; |
11b0c469 |
1883 | forum_print_post($post, $course, $ownpost, $reply, $link, $assessed); |
501cdbd8 |
1884 | echo "<BR>"; |
70c476a7 |
1885 | forum_print_posts_nested($post->id, $course, $assessed, $reply); |
501cdbd8 |
1886 | echo "</UL>\n"; |
1887 | } |
1888 | } else { |
1889 | return; |
1890 | } |
1891 | } |
1892 | |
1893 | function forum_set_display_mode($mode=0) { |
5a83a0a8 |
1894 | global $USER, $FORUM_DEFAULT_DISPLAY_MODE; |
501cdbd8 |
1895 | |
1896 | if ($mode) { |
1897 | $USER->mode = $mode; |
9c9f7d77 |
1898 | } else if (empty($USER->mode)) { |
501cdbd8 |
1899 | $USER->mode = $FORUM_DEFAULT_DISPLAY_MODE; |
1900 | } |
1901 | } |
f93f848a |
1902 | |
3869a2ac |
1903 | |
f93f848a |
1904 | ?> |