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