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