Fixed up some forum functions and constants (consistency)
[moodle.git] / mod / forum / lib.php
CommitLineData
f93f848a 1<?PHP // $Id$
2
501cdbd8 3/// CONSTANTS ///////////////////////////////////////////////////////////
f93f848a 4
501cdbd8 5$FORUM_DEFAULT_DISPLAY_MODE = 3;
f93f848a 6
7a12aab4 7$FORUM_LAYOUT_MODES = array ( "1" => "Display replies flat, with oldest first",
8 "-1" => "Display replies flat, wth newest first",
9 "2" => "Display replies in threaded form",
10 "3" => "Display replies in nested form");
f93f848a 11
11b0c469 12// These are course content forums that can be added to the course manually
7a12aab4 13$FORUM_TYPES = array ("general" => "General forum",
14 "eachuser" => "Each person posts one discussion",
15 "single" => "A single simple discussion");
f93f848a 16
bc9ab4dd 17$FORUM_POST_RATINGS = array ("3" => "Shows a more connected approach",
18 "2" => "Shows a more separate approach",
19 "1" => "I can't decide between these two");
f93f848a 20
501cdbd8 21$FORUM_LONG_POST = 600;
e07635f4 22
23
501cdbd8 24/// FUNCTIONS ///////////////////////////////////////////////////////////
f93f848a 25
26
11b0c469 27function forum_get_course_forum($courseid, $type) {
28// How to set up special 1-per-course forums
29 if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = '$type'")) {
f93f848a 30 return $forum;
31 } else {
32 // Doesn't exist, so create one now.
33 $forum->course = $courseid;
11b0c469 34 $forum->type = "$type";
35 switch ($forum->type) {
36 case "news":
37 $forum->name = "News";
38 $forum->intro= "General news about this course";
39 $forum->open = 0;
40 $forum->assessed = 0;
41 $forum->forcesubscribe = 1;
42 break;
43 case "social":
44 $forum->name = "Social";
45 $forum->intro= "A forum for general socialising. Talk about anything you like!";
46 $forum->open = 1;
47 $forum->assessed = 0;
48 $forum->forcesubscribe = 0;
49 break;
50 case "teacher":
51 $forum->name = "Teacher Forum";
52 $forum->intro= "For teacher-only notes and discussion";
53 $forum->open = 0;
54 $forum->assessed = 0;
55 $forum->forcesubscribe = 0;
56 break;
57 default:
58 notify("That forum type doesn't exist!");
59 return false;
60 break;
f93f848a 61
11b0c469 62 }
82aa0e8d 63 $forum->timemodified = time();
64 $forum->id = insert_record("forum", $forum);
65 return get_record_sql("SELECT * from forum WHERE id = '$forum->id'");
66 }
67}
68
f93f848a 69
11b0c469 70function forum_make_mail_post(&$post, $user, $touser, $course,
71 $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
501cdbd8 72// Given the data about a posting, builds up the HTML to display it and
73// returns the HTML in a string. This is designed for sending via HTML email.
74
75 global $THEME, $CFG;
76
77 $output = "";
78
79 if ($post->parent) {
80 $output .= "<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1><TR><TD BGCOLOR=#888888>";
81 $output .= "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0>";
82 } else {
83 $output .= "<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1 WIDTH=100%><TR><TD BGCOLOR=#888888>";
84 $output .= "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 WIDTH=100%>";
85 }
86
87 $output .= "<TR><TD BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
88 $output .= print_user_picture($user->id, $course->id, $user->picture, false, true);
89 $output .= "</TD>";
90
91 if ($post->parent) {
92 $output .= "<TD NOWRAP BGCOLOR=\"$THEME->cellheading\">";
93 } else {
94 $output .= "<TD NOWRAP BGCOLOR=\"$THEME->cellheading2\">";
95 }
96 $output .= "<P>";
97 $output .= "<FONT SIZE=3><B>$post->subject</B></FONT><BR>";
98 $output .= "<FONT SIZE=2>by <A HREF=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A>";
99 $output .= " on ".userdate($post->created, "", $touser->timezone);
100 $output .= "</FONT></P></TD></TR>";
101 $output .= "<TR><TD BGCOLOR=\"$THEME->body\" WIDTH=10>";
102 $output .= "&nbsp;";
103 $output .= "</TD><TD BGCOLOR=\"#FFFFFF\">\n";
104
105 $output .= text_to_html($post->message);
106
107 $output .= "<P ALIGN=right><FONT SIZE=-1>";
108
109 $age = time() - $post->created;
110 if ($ownpost) {
111 $output .= "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">Delete</A>";
112 if ($reply) {
113 $output .= "| <A HREF=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">Reply</A>";
114 }
115 $output .= "&nbsp;&nbsp;";
116 } else {
117 if ($reply) {
118 $output .= "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">Reply</A>&nbsp;&nbsp;";
119 }
120 }
121
122 $output .= "<DIV ALIGN=right><P ALIGN=right>";
123
124 if ($link) {
125 if ($post->replies == 1) {
126 $replystring = "reply";
127 } else {
128 $replystring = "replies";
129 }
130 $output .= "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><B>Discuss this topic</B></A> ($post->replies $replystring so far)&nbsp;&nbsp;";
131 }
132 $output .= "</P></DIV>";
133 if ($footer) {
134 $output .= "<P>$footer</P>";
135 }
136 $output .= "</TD></TR></TABLE>\n";
137 $output .= "</TD></TR></TABLE>\n\n";
138
139 return $output;
140}
141
142
11b0c469 143function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
6bebaa64 144 global $THEME, $USER, $CFG, $FORUM_LONG_POST;
501cdbd8 145
146 if ($post->parent) {
147 echo "<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1><TR><TD BGCOLOR=#888888>";
148 echo "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0>";
149 } else {
150 echo "<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1 WIDTH=100%><TR><TD BGCOLOR=#888888>";
151 echo "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 WIDTH=100%>";
152 }
153
154 echo "<TR><TD BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
155 print_user_picture($post->userid, $courseid, $post->picture);
156 echo "</TD>";
157
158 if ($post->parent) {
159 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading\">";
160 } else {
161 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading2\">";
162 }
163 echo "<P>";
164 echo "<FONT SIZE=3><B>$post->subject</B></FONT><BR>";
165 echo "<FONT SIZE=2>by <A HREF=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$post->firstname $post->lastname</A>";
166 echo " on ".userdate($post->created);
167 echo "</FONT></P></TD></TR>";
168 echo "<TR><TD BGCOLOR=\"$THEME->body\" WIDTH=10>";
169 echo "&nbsp;";
170 echo "</TD><TD BGCOLOR=\"#FFFFFF\">\n";
171
172 if ($link && (strlen($post->message) > $FORUM_LONG_POST)) {
aa153f29 173 // Print shortened version
174 echo text_to_html(forum_shorten_post($post->message));
501cdbd8 175 $numwords = count_words($post->message);
aa153f29 176 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">";
177 echo "Read the rest of this topic</A> ($numwords words)...";
501cdbd8 178 } else {
aa153f29 179 // Print whole message
501cdbd8 180 echo text_to_html($post->message);
181 }
182
183 echo "<P ALIGN=right><FONT SIZE=-1>";
184
185 $age = time() - $post->created;
186 if ($ownpost) {
187 if ($age < $CFG->maxeditingtime) {
188 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?edit=$post->id\">Edit</A> | ";
189 }
190 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">Delete</A>";
191 if ($reply) {
192 echo "| <A HREF=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">Reply</A>";
193 }
194 echo "&nbsp;&nbsp;";
195 } else {
196 if ($reply) {
197 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">Reply</A>&nbsp;&nbsp;";
198 }
199 }
200
201
202 echo "<DIV ALIGN=right><P ALIGN=right>";
203 if ($rate && $USER->id) {
204 if ($USER->id == $post->userid) {
7a12aab4 205 forum_print_ratings($post->id);
501cdbd8 206 } else {
7a12aab4 207 forum_print_rating($post->id, $USER->id);
501cdbd8 208 }
209 }
210
211 if ($link) {
212 if ($post->replies == 1) {
213 $replystring = "reply";
214 } else {
215 $replystring = "replies";
216 }
217 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><B>Discuss this topic</B></A> ($post->replies $replystring so far)&nbsp;&nbsp;";
218 }
219 echo "</P>";
220 if ($footer) {
221 echo "<P>$footer</P>";
222 }
223 echo "</DIV>";
224 echo "</TD></TR></TABLE>";
225 echo "</TD></TR>\n</TABLE>\n\n";
226}
227
aa153f29 228function forum_shorten_post($message) {
229 global $FORUM_LONG_POST;
230
231 if (strlen($message) > $FORUM_LONG_POST) {
232 // Look for the first return between 50 and $FORUM_LONG_POST
233 $shortmessage = substr($message, 50, $FORUM_LONG_POST);
234 if ($pos = strpos($shortmessage, "\n")) {
37c65278 235 return substr($message, 0, 50 + $pos). " ...";
aa153f29 236 } else {
237 return substr($message, 0, $FORUM_LONG_POST). "...";
238 }
239 } else {
240 return $message;
241 }
242}
243
501cdbd8 244
7a12aab4 245function forum_print_ratings($post) {
501cdbd8 246
247 global $CFG, $PHPSESSID;
248
249 $notsatisfactory = 0;
250 $satisfactory = 0;
251 $outstanding = 0;
252 if ($ratings = get_records_sql("SELECT * from forum_ratings WHERE post='$post'")) {
253 foreach ($ratings as $rating) {
254 switch ($rating->rating) {
255 case 1: $notsatisfactory++; break;
256 case 2: $satisfactory++; break;
257 case 3: $outstanding++; break;
258 }
259 }
260 $summary = "$outstanding/$satisfactory/$notsatisfactory";
261
262 echo "Ratings: ";
263 link_to_popup_window ("/mod/forum/report.php?id=$post", "ratings", $summary, 400, 550);
264
265 } else {
266 echo "";
267 }
268}
269
7a12aab4 270function forum_print_rating($post, $user) {
501cdbd8 271 global $FORUM_POST_RATINGS;
272
273 if ($rs = get_record_sql("SELECT rating from forum_ratings WHERE user='$user' AND post='$post'")) {
274 echo "<FONT SIZE=-1>You rated this: <FONT COLOR=green>";
275 if ($FORUM_POST_RATINGS[$rs->rating]) {
276 echo $FORUM_POST_RATINGS[$rs->rating];
277 } else {
278 echo "Error";
279 }
280 echo "</FONT></FONT>";
281
282 } else {
283 choose_from_menu($FORUM_POST_RATINGS, $post, "", "Rate...");
284 }
285}
286
7a12aab4 287function forum_print_mode_form($discussion, $mode) {
288 GLOBAL $FORUM_LAYOUT_MODES;
501cdbd8 289
290 echo "<CENTER><P>";
7a12aab4 291 popup_form("discuss.php?d=$discussion&mode=", $FORUM_LAYOUT_MODES, "mode", $mode, "");
501cdbd8 292 echo "</P></CENTER>\n";
293}
294
7a12aab4 295function forum_print_search_form($course, $search="") {
501cdbd8 296 global $CFG;
297
298 echo "<TABLE BORDER=0 CELLPADDING=10 CELLSPACING=0><TR><TD ALIGN=CENTER>";
299 echo "<FORM NAME=search ACTION=\"$CFG->wwwroot/mod/forum/search.php\">";
300 echo "<INPUT NAME=search TYPE=text SIZE=15 VALUE=\"$search\"><BR>";
301 echo "<INPUT VALUE=\"".get_string("search", "forum")."\" TYPE=submit>";
302 echo "<INPUT NAME=id TYPE=hidden VALUE=\"$course->id\">";
303 echo "</FORM>";
304 echo "</TD></TR></TABLE>";
305}
306
307
11b0c469 308function forum_count_discussion_replies($forum="0") {
501cdbd8 309 if ($forum) {
310 $forumselect = " AND d.forum = '$forum'";
311 }
312 return get_records_sql("SELECT p.discussion, (count(*)) as replies
313 FROM forum_posts p, forum_discussions d
314 WHERE p.parent > 0 AND p.discussion = d.id
315 GROUP BY p.discussion");
316}
317
318
11b0c469 319function forum_set_return() {
501cdbd8 320 global $SESSION, $HTTP_REFERER;
321
322 if (! $SESSION->fromdiscussion) {
323 $SESSION->fromdiscussion = $HTTP_REFERER;
324 }
325}
326
327
11b0c469 328function forum_go_back_to($default) {
501cdbd8 329 global $SESSION;
330
331 if ($SESSION->fromdiscussion) {
332 $returnto = $SESSION->fromdiscussion;
333 unset($SESSION->fromdiscussion);
334 return $returnto;
335 } else {
336 return $default;
337 }
338}
339
11b0c469 340function forum_get_post_full($postid) {
501cdbd8 341 return get_record_sql("SELECT p.*, u.firstname, u.lastname,
342 u.email, u.picture, u.id as userid
343 FROM forum_posts p, user u
344 WHERE p.id = '$postid' AND p.user = u.id");
345}
346
347
11b0c469 348function forum_add_new_post($post) {
501cdbd8 349
350 $timenow = time();
351 $post->created = $timenow;
352 $post->modified = $timenow;
353 $post->mailed = "0";
354
355 return insert_record("forum_posts", $post);
356}
357
11b0c469 358function forum_update_post($post) {
501cdbd8 359 global $db;
360
361 $timenow = time();
362
363 $rs = $db->Execute("UPDATE forum_posts
364 SET message='$post->message', subject='$post->subject', modified='$timenow'
365 WHERE id = '$post->id'");
366 return $rs;
367}
368
369function forum_add_discussion($discussion) {
370// Given an object containing all the necessary data,
371// create a new discussion and return the id
372
373 GLOBAL $USER;
374
375 $timenow = time();
376
377 // The first post is stored as a real post, and linked
378 // to from the discuss entry.
379
380 $post->discussion = 0;
381 $post->parent = 0;
382 $post->user = $USER->id;
383 $post->created = $timenow;
384 $post->modified = $timenow;
385 $post->mailed = 0;
386 $post->subject = $discussion->name;
387 $post->message = $discussion->intro;
388
389 if (! $post->id = insert_record("forum_posts", $post) ) {
390 return 0;
391 }
392
393 // Now do the real module entry
394
395 $discussion->firstpost = $post->id;
396 $discussion->timemodified = $timenow;
397
398 if (! $discussion->id = insert_record("forum_discussions", $discussion) ) {
399 return 0;
400 }
401
402 // Finally, set the pointer on the post.
403 if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) {
404 return 0;
405 }
406
407 return $discussion->id;
408}
409
410
411function forum_delete_discussion($discussion) {
412// $discussion is a discussion record object
413
414 $result = true;
415
416 if ($posts = get_records("forum_posts", "discussion", $discussion->id)) {
417 foreach ($posts as $post) {
418 if (! delete_records("forum_ratings", "post", "$post->id")) {
419 $result = false;
420 }
421 }
422 }
423
424 if (! delete_records("forum_posts", "discussion", "$discussion->id")) {
425 $result = false;
426 }
427
428 if (! delete_records("forum_discussions", "id", "$discussion->id")) {
429 $result = false;
430 }
431
432 return $result;
433}
434
435
436
11b0c469 437function forum_print_user_discussions($courseid, $userid) {
8f9c8aaf 438 global $CFG, $USER;
501cdbd8 439
b76128b0 440 $discussions = get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture,
8f9c8aaf 441 u.id as userid, f.type as forumtype, f.name as forumname, f.id as forumid
4ca09091 442 FROM forum_discussions d, forum_posts p, user u, forum f
11b0c469 443 WHERE d.course = '$courseid' AND p.discussion = d.id AND
4ca09091 444 p.parent = 0 AND p.user = u.id AND u.id = '$userid' AND
445 d.forum = f.id
b76128b0 446 ORDER BY p.created ASC");
501cdbd8 447
11b0c469 448 if ($discussions) {
b76128b0 449 $user = get_record("user", "id", $userid);
501cdbd8 450 echo "<HR>";
b76128b0 451 print_heading("Discussions started by $user->firstname $user->lastname");
11b0c469 452 $replies = forum_count_discussion_replies();
453 foreach ($discussions as $discussion) {
4ca09091 454 if (($discussion->forumtype == "teacher") and !isteacher($courseid)) {
455 continue;
456 }
11b0c469 457 if ($replies[$discussion->discussion]) {
458 $discussion->replies = $replies[$discussion->discussion]->replies;
501cdbd8 459 } else {
11b0c469 460 $discussion->replies = 0;
501cdbd8 461 }
8f9c8aaf 462 $discussion->subject .= " (in <A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname</A>)";
11b0c469 463 $ownpost = ($discussion->userid == $USER->id);
464 forum_print_post($discussion, $course->id, $ownpost, $reply=0, $link=1, $assessed=false);
501cdbd8 465 echo "<BR>\n";
466 }
467 }
468}
469
470
471function forum_user_summary($course, $user, $mod, $forum) {
472 global $CFG;
473}
474
475
476function forum_user_outline($course, $user, $mod, $forum) {
477
501cdbd8 478 global $CFG;
479 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
498178e4 480 FROM forum f, forum_discussions d, forum_posts p, user u
481 WHERE f.id = '$forum->id' AND d.forum = f.id AND p.discussion = d.id
482 AND p.user = '$user->id' AND p.user = u.id
501cdbd8 483 ORDER BY p.modified ASC")) {
484
485 $result->info = count($posts)." posts";
486
487 $lastpost = array_pop($posts);
488 $result->time = $lastpost->modified;
489 return $result;
490 }
491 return NULL;
492}
493
494
495function forum_user_complete($course, $user, $mod, $forum) {
496 global $CFG;
497
501cdbd8 498 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
498178e4 499 FROM forum f, forum_discussions d, forum_posts p, user u
500 WHERE f.id = '$forum->id' AND d.forum = f.id AND p.discussion = d.id
501 AND p.user = '$user->id' AND p.user = u.id
502 ORDER BY p.modified ASC")) {
501cdbd8 503
504 foreach ($posts as $post) {
505 if ($post->parent) {
506 $footer = "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">Parent of this post</A>";
507 } else {
508 $footer = "";
509 }
510
11b0c469 511 pirint_post($post, $course->id, $ownpost=false, $reply=false, $link=false, $rate=false, $footer);
501cdbd8 512 }
513
514 } else {
515 echo "<P>No posts</P>";
516 }
517
518}
519
520function forum_cron () {
521// Function to be run periodically according to the moodle cron
522// Finds all posts that have yet to be mailed out, and mails them
523
524 global $CFG;
525
526 echo "Processing posts...\n";
527
528 $cutofftime = time() - $CFG->maxeditingtime;
529
530 if ($posts = get_records_sql("SELECT p.*, d.course FROM forum_posts p, forum_discussions d
531 WHERE p.mailed = '0' AND p.created < '$cutofftime' AND p.discussion = d.id")) {
532
533 $timenow = time();
534
535 foreach ($posts as $post) {
536
537 echo "Processing post $post->id\n";
538
539 if (! $userfrom = get_record("user", "id", "$post->user")) {
540 echo "Could not find user $post->user\n";
541 continue;
542 }
543
544 if (! $course = get_record("course", "id", "$post->course")) {
545 echo "Could not find course $post->course\n";
546 continue;
547 }
548
549 if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) {
550 echo "Could not find discussion $post->discussion\n";
551 continue;
552 }
553
554 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
555 echo "Could not find forum $discussion->forum\n";
556 continue;
557 }
558
559
560 if ($users = get_records_sql("SELECT u.* FROM user u, forum_subscriptions s
561 WHERE s.user = u.id AND s.forum = '$discussion->forum'")) {
562
563 foreach ($users as $userto) {
564 $postsubject = "$course->shortname: $post->subject";
565 $posttext = "$course->shortname -> Forums -> $forum->name -> $discussion->name\n";
566 $posttext .= "---------------------------------------------------------------------\n";
567 $posttext .= "$post->subject\n";
568 $posttext .= "by $userfrom->firstname $userfrom->lastname, on ".userdate($post->created, "", $userto->timezone)."\n";
569 $posttext .= "---------------------------------------------------------------------\n";
570 $posttext .= strip_tags($post->message);
571 $posttext .= "\n\n";
572 $posttext .= "---------------------------------------------------------------------\n";
573 $posttext .= "This is a copy of a message posted on the $course->shortname website.\n";
574 $posttext .= "To add your reply via the website, click on this link:\n";
575 $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id";
576
577 if ($userto->mailformat == 1) { // HTML
578 $posthtml = "<P><FONT FACE=sans-serif>".
579 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> ->".
580 "<A HREF=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">Forums</A> ->".
581 "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name</A> ->".
582 "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name</A></FONT></P>";
11b0c469 583 $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, true, false, false);
501cdbd8 584 } else {
585 $posthtml = "";
586 }
587
588 if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) {
589 echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email)\n";
590 }
591 }
592 }
593
594 if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) {
595 echo "Could not update the mailed field for id $post->id\n";
596 }
597 }
598 }
599
600 return true;
601}
602
603
604function forum_forcesubscribe($forumid, $value=1) {
605 return set_field("forum", "forcesubscribe", $value, "id", $forumid);
606}
607
608function forum_is_forcesubscribed($forumid) {
609 return get_field("forum", "forcesubscribe", "id", $forumid);
610}
611
612function forum_is_subscribed($userid, $forumid) {
613 if (forum_is_forcesubscribed($forumid)) {
614 return true;
615 }
616 return record_exists_sql("SELECT * FROM forum_subscriptions WHERE user='$userid' AND forum='$forumid'");
617}
618
619function forum_subscribe($userid, $forumid) {
620 global $db;
621
622 return $db->Execute("INSERT INTO forum_subscriptions SET user = '$userid', forum = '$forumid'");
623}
624
625function forum_unsubscribe($userid, $forumid) {
626 global $db;
627
628 return $db->Execute("DELETE FROM forum_subscriptions WHERE user = '$userid' AND forum = '$forumid'");
629}
630
631
11b0c469 632function forum_user_has_posted_discussion($forumid, $userid) {
633 if ($discussions = forum_get_discussions($forumid, "DESC", $userid)) {
501cdbd8 634 return true;
635 } else {
636 return false;
637 }
638}
639
11b0c469 640function forum_user_can_post_discussion($forum) {
501cdbd8 641// $forum is an object
642 global $USER;
643
644 if ($forum->type == "eachuser") {
11b0c469 645 return (! forum_user_has_posted_discussion($forum->id, $USER->id));
501cdbd8 646 } else if ($forum->type == "teacher") {
647 return isteacher($forum->course);
648 } else if (isteacher($forum->course)) {
649 return true;
650 } else {
651 return $forum->open;
652 }
653}
654
655
11b0c469 656function forum_get_discussions($forum="0", $forum_sort="DESC", $user=0) {
501cdbd8 657 if ($user) {
658 $userselect = " AND u.id = '$user' ";
659 } else {
660 $userselect = "";
661 }
662 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.id as userid
663 FROM forum_discussions d, forum_posts p, user u
664 WHERE d.forum = '$forum' AND p.discussion = d.id AND
665 p.parent= 0 AND p.user = u.id $userselect
666 ORDER BY p.created $forum_sort");
667}
668
669
670
11b0c469 671function forum_print_latest_discussions($forum_id=0, $forum_numdiscussions=5, $forum_style="plain", $forum_sort="DESC") {
f93f848a 672 global $CFG, $USER;
673
674 if ($forum_id) {
675 if (! $forum = get_record("forum", "id", $forum_id)) {
676 error("Forum ID was incorrect");
677 }
678 if (! $course = get_record("course", "id", $forum->course)) {
679 error("Could not find the course this forum belongs to!");
680 }
681
682 if ($course->category) {
683 require_login($course->id);
684 }
685
686 } else {
687 if (! $course = get_record("course", "category", 0)) {
688 error("Could not find a top-level course!");
689 }
11b0c469 690 if (! $forum = forum_get_course_news_forum($course->id)) {
f93f848a 691 error("Could not find or create a main forum in this course (id $course->id)");
692 }
693 }
694
11b0c469 695 if (forum_user_can_post_discussion($forum)) {
3b9af3dd 696 echo "<P ALIGN=CENTER>";
11b0c469 697 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?forum=$forum->id\">Add a new discussion topic...</A>";
501cdbd8 698 echo "</P>\n";
77305fe6 699 }
700
11b0c469 701 if (! $discussions = forum_get_discussions($forum->id, $forum_sort) ) {
e07635f4 702 echo "<P ALIGN=CENTER><B>There are no discussion topics yet in this forum.</B></P>";
f93f848a 703
704 } else {
705
11b0c469 706 $replies = forum_count_discussion_replies($forum->id);
f93f848a 707
11b0c469 708 $discussioncount = 0;
f93f848a 709
11b0c469 710 foreach ($discussions as $discussion) {
711 $discussioncount++;
f93f848a 712
11b0c469 713 if ($forum_numdiscussions && ($discussioncount > $forum_numdiscussions)) {
714 echo "<P ALIGN=right><A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">Older discussions</A> ...</P>";
f93f848a 715 break;
716 }
11b0c469 717 if ($replies[$discussion->discussion]) {
718 $discussion->replies = $replies[$discussion->discussion]->replies;
f93f848a 719 } else {
11b0c469 720 $discussion->replies = 0;
f93f848a 721 }
11b0c469 722 $ownpost = ($discussion->userid == $USER->id);
f93f848a 723 switch ($forum_style) {
724 case "minimal":
11b0c469 725 echo "<P><FONT COLOR=#555555>".userdate($discussion->modified, "%e %b, %H:%M")." - $discussion->firstname</FONT>";
726 echo "<BR>$discussion->subject ";
727 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->discussion\">more...</A>";
f93f848a 728 echo "</P>\n";
729 break;
730 default:
11b0c469 731 forum_print_post($discussion, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
f93f848a 732 echo "<BR>\n";
733 break;
734 }
735 }
736 }
f93f848a 737}
738
11b0c469 739function forum_print_discussion($course, $discussion, $post, $mode) {
501cdbd8 740
741 global $USER;
742
743 $ownpost = ($USER->id == $post->user);
744
11b0c469 745 forum_print_post($post, $course->id, $ownpost, $reply=true, $link=false, $rate=false);
501cdbd8 746
7a12aab4 747 forum_print_mode_form($discussion->id, $mode);
501cdbd8 748
749 if ($discussion->assessed && $USER->id) {
750 echo "<FORM NAME=form METHOD=POST ACTION=rate.php>";
751 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
752 }
753
754 switch ($mode) {
755 case 1 : // Flat ascending
756 case -1 : // Flat descending
757 default:
758 echo "<UL>";
11b0c469 759 forum_print_posts_flat($post->discussion, $course->id, $mode, $discussion->assessed);
501cdbd8 760 echo "</UL>";
761 break;
762
763 case 2 : // Threaded
11b0c469 764 forum_print_posts_threaded($post->id, $course->id, 0, $discussion->assessed);
501cdbd8 765 break;
766
767 case 3 : // Nested
11b0c469 768 forum_print_posts_nested($post->id, $course->id, $discussion->assessed);
501cdbd8 769 break;
770 }
771
772 if ($discussion->assessed && $USER->id) {
773 echo "<CENTER><P ALIGN=center><INPUT TYPE=submit VALUE=\"Send in my latest ratings\"></P></CENTER>";
774 echo "</FORM>";
775 }
776}
777
11b0c469 778function forum_print_posts_flat($discussion, $course, $direction, $assessed) {
501cdbd8 779 global $USER;
780
781 $reply = true;
782 $link = false;
783
784 if ($direction < 0) {
785 $sort = "ORDER BY created DESC";
786 } else {
787 $sort = "ORDER BY created ASC";
788 }
789
790 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
791 FROM forum_posts p, user u
792 WHERE p.discussion = $discussion AND p.parent > 0 AND p.user = u.id $sort")) {
793
794 foreach ($posts as $post) {
795 $ownpost = ($USER->id == $post->user);
11b0c469 796 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
501cdbd8 797 }
798 } else {
799 return;
800 }
801}
802
11b0c469 803function forum_print_posts_threaded($parent, $course, $depth, $assessed) {
501cdbd8 804 global $USER;
805
806 $reply = true;
807 $link = false;
808
809 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
810 FROM forum_posts p, user u
811 WHERE p.parent = '$parent' AND p.user = u.id")) {
812
813 foreach ($posts as $post) {
814
815 echo "<UL>";
816 if ($depth > 0) {
817 $ownpost = ($USER->id == $post->user);
11b0c469 818 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed); // link=true?
501cdbd8 819 echo "<BR>";
820 } else {
821 echo "<LI><P><B><A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</A></B> by $post->firstname $post->lastname, ".userdate($post->created)."</P>";
822 }
823
11b0c469 824 forum_print_posts_threaded($post->id, $course, $depth-1, $assessed);
501cdbd8 825 echo "</UL>\n";
826 }
827 } else {
828 return;
829 }
830}
831
11b0c469 832function forum_print_posts_nested($parent, $course, $assessed) {
501cdbd8 833 global $USER;
834
835 $reply = true;
836 $link = false;
837
838 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
839 FROM forum_posts p, user u
840 WHERE p.parent = $parent AND p.user = u.id
841 ORDER BY p.created ASC ")) {
842
843 foreach ($posts as $post) {
844
845 $ownpost = ($USER->id == $post->user);
846
847 echo "<UL>";
11b0c469 848 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
501cdbd8 849 echo "<BR>";
11b0c469 850 forum_print_posts_nested($post->id, $course, $assessed);
501cdbd8 851 echo "</UL>\n";
852 }
853 } else {
854 return;
855 }
856}
857
858function forum_set_display_mode($mode=0) {
859 global $USER;
860
861 if ($mode) {
862 $USER->mode = $mode;
863 } else if (!$USER->mode) {
864 $USER->mode = $FORUM_DEFAULT_DISPLAY_MODE;
865 }
866}
f93f848a 867
868?>