More direct link to index.php just in case they don't have index.php
[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
ffe11640 7$FORUM_LAYOUT_MODES = array ( "1" => get_string("modeflatoldestfirst", "forum"),
8 "-1" => get_string("modeflatnewestfirst", "forum"),
9 "2" => get_string("modethreaded", "forum"),
10 "3" => get_string("modenested", "forum") );
f93f848a 11
11b0c469 12// These are course content forums that can be added to the course manually
ffe11640 13$FORUM_TYPES = array ("general" => get_string("generalforum", "forum"),
14 "eachuser" => get_string("eachuserforum", "forum"),
15 "single" => get_string("singleforum", "forum") );
f93f848a 16
ffe11640 17$FORUM_POST_RATINGS = array ("3" => get_string("postrating3", "forum"),
18 "2" => get_string("postrating2", "forum"),
19 "1" => get_string("postrating1", "forum") );
f93f848a 20
4d871a72 21$FORUM_SHORT_POST = 300; // Less than this is "short"
22
23$FORUM_LONG_POST = 600; // More than this is "long"
24
e07635f4 25
26
501cdbd8 27/// FUNCTIONS ///////////////////////////////////////////////////////////
f93f848a 28
29
11b0c469 30function forum_get_course_forum($courseid, $type) {
31// How to set up special 1-per-course forums
32 if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = '$type'")) {
f93f848a 33 return $forum;
ffe11640 34
f93f848a 35 } else {
36 // Doesn't exist, so create one now.
37 $forum->course = $courseid;
11b0c469 38 $forum->type = "$type";
39 switch ($forum->type) {
40 case "news":
ffe11640 41 $forum->name = get_string("namenews", "forum");
42 $forum->intro = get_string("intronews", "forum");
43 $forum->open = 0;
11b0c469 44 $forum->assessed = 0;
45 $forum->forcesubscribe = 1;
46 break;
47 case "social":
ffe11640 48 $forum->name = get_string("namesocial", "forum");
49 $forum->intro = get_string("introsocial", "forum");
50 $forum->open = 1;
11b0c469 51 $forum->assessed = 0;
52 $forum->forcesubscribe = 0;
53 break;
54 case "teacher":
ffe11640 55 $forum->name = get_string("nameteacher", "forum");
56 $forum->intro = get_string("introteacher", "forum");
57 $forum->open = 0;
11b0c469 58 $forum->assessed = 0;
59 $forum->forcesubscribe = 0;
60 break;
61 default:
62 notify("That forum type doesn't exist!");
63 return false;
64 break;
f93f848a 65
11b0c469 66 }
82aa0e8d 67 $forum->timemodified = time();
68 $forum->id = insert_record("forum", $forum);
69 return get_record_sql("SELECT * from forum WHERE id = '$forum->id'");
70 }
71}
72
f93f848a 73
11b0c469 74function forum_make_mail_post(&$post, $user, $touser, $course,
75 $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
501cdbd8 76// Given the data about a posting, builds up the HTML to display it and
77// returns the HTML in a string. This is designed for sending via HTML email.
78
79 global $THEME, $CFG;
80
81 $output = "";
82
83 if ($post->parent) {
84 $output .= "<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1><TR><TD BGCOLOR=#888888>";
85 $output .= "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0>";
86 } else {
87 $output .= "<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1 WIDTH=100%><TR><TD BGCOLOR=#888888>";
88 $output .= "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 WIDTH=100%>";
89 }
90
91 $output .= "<TR><TD BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
92 $output .= print_user_picture($user->id, $course->id, $user->picture, false, true);
93 $output .= "</TD>";
94
95 if ($post->parent) {
96 $output .= "<TD NOWRAP BGCOLOR=\"$THEME->cellheading\">";
97 } else {
98 $output .= "<TD NOWRAP BGCOLOR=\"$THEME->cellheading2\">";
99 }
100 $output .= "<P>";
101 $output .= "<FONT SIZE=3><B>$post->subject</B></FONT><BR>";
ffe11640 102 $output .= "<FONT SIZE=2>";
103 $by->name = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A>";
104 $by->date = userdate($post->created, "", $touser->timezone);
105 $output .= get_string("bynameondate", "forum", $by);
501cdbd8 106 $output .= "</FONT></P></TD></TR>";
107 $output .= "<TR><TD BGCOLOR=\"$THEME->body\" WIDTH=10>";
108 $output .= "&nbsp;";
86970225 109 $output .= "</TD><TD BGCOLOR=\"$THEME->cellcontent\">\n";
501cdbd8 110
111 $output .= text_to_html($post->message);
112
113 $output .= "<P ALIGN=right><FONT SIZE=-1>";
114
115 $age = time() - $post->created;
116 if ($ownpost) {
ffe11640 117 $output .= "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."</A>";
501cdbd8 118 if ($reply) {
ffe11640 119 $output .= "| <A HREF=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</A>";
501cdbd8 120 }
121 $output .= "&nbsp;&nbsp;";
122 } else {
123 if ($reply) {
ffe11640 124 $output .= "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</A>&nbsp;&nbsp;";
501cdbd8 125 }
126 }
127
128 $output .= "<DIV ALIGN=right><P ALIGN=right>";
129
130 if ($link) {
131 if ($post->replies == 1) {
ffe11640 132 $replystring = get_string("repliesone", "forum", $post->replies);
501cdbd8 133 } else {
ffe11640 134 $replystring = get_string("repliesmany", "forum", $post->replies);
501cdbd8 135 }
ffe11640 136 $output .= "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><B>".get_string("discussthistopic", "forum")."</B></A> ($replystring)&nbsp;&nbsp;";
501cdbd8 137 }
138 $output .= "</P></DIV>";
139 if ($footer) {
140 $output .= "<P>$footer</P>";
141 }
142 $output .= "</TD></TR></TABLE>\n";
143 $output .= "</TD></TR></TABLE>\n\n";
144
145 return $output;
146}
147
148
11b0c469 149function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
6bebaa64 150 global $THEME, $USER, $CFG, $FORUM_LONG_POST;
501cdbd8 151
152 if ($post->parent) {
153 echo "<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1><TR><TD BGCOLOR=#888888>";
154 echo "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0>";
155 } else {
156 echo "<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1 WIDTH=100%><TR><TD BGCOLOR=#888888>";
157 echo "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 WIDTH=100%>";
158 }
159
160 echo "<TR><TD BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
161 print_user_picture($post->userid, $courseid, $post->picture);
162 echo "</TD>";
163
164 if ($post->parent) {
19a55d67 165 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading\" WIDTH=\"100%\">";
501cdbd8 166 } else {
19a55d67 167 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading2\" WIDTH=\"100%*\">";
501cdbd8 168 }
169 echo "<P>";
170 echo "<FONT SIZE=3><B>$post->subject</B></FONT><BR>";
ffe11640 171 echo "<FONT SIZE=2>";
172 $by->name = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$post->firstname $post->lastname</A>";
173 $by->date = userdate($post->created);
174 print_string("bynameondate", "forum", $by);
501cdbd8 175 echo "</FONT></P></TD></TR>";
176 echo "<TR><TD BGCOLOR=\"$THEME->body\" WIDTH=10>";
177 echo "&nbsp;";
86970225 178 echo "</TD><TD BGCOLOR=\"$THEME->cellcontent\">\n";
501cdbd8 179
180 if ($link && (strlen($post->message) > $FORUM_LONG_POST)) {
aa153f29 181 // Print shortened version
182 echo text_to_html(forum_shorten_post($post->message));
501cdbd8 183 $numwords = count_words($post->message);
aa153f29 184 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">";
ffe11640 185 echo get_string("readtherest", "forum");
3db1919b 186 echo "</A> (".get_string("numwords", "", $numwords).")...";
501cdbd8 187 } else {
aa153f29 188 // Print whole message
501cdbd8 189 echo text_to_html($post->message);
190 }
191
192 echo "<P ALIGN=right><FONT SIZE=-1>";
193
194 $age = time() - $post->created;
195 if ($ownpost) {
196 if ($age < $CFG->maxeditingtime) {
ffe11640 197 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?edit=$post->id\">".get_string("edit", "forum")."</A> | ";
501cdbd8 198 }
64eacd6f 199 }
200 if ($ownpost or isteacher($courseid)) {
ffe11640 201 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."</A>";
501cdbd8 202 if ($reply) {
64eacd6f 203 echo "| ";
204 } else {
205 echo "&nbsp;&nbsp;";
501cdbd8 206 }
64eacd6f 207 }
208 if ($reply) {
209 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</A>";
501cdbd8 210 echo "&nbsp;&nbsp;";
501cdbd8 211 }
212
213
214 echo "<DIV ALIGN=right><P ALIGN=right>";
215 if ($rate && $USER->id) {
216 if ($USER->id == $post->userid) {
7a12aab4 217 forum_print_ratings($post->id);
501cdbd8 218 } else {
7a12aab4 219 forum_print_rating($post->id, $USER->id);
501cdbd8 220 }
221 }
222
223 if ($link) {
224 if ($post->replies == 1) {
ffe11640 225 $replystring = get_string("repliesone", "forum", $post->replies);
501cdbd8 226 } else {
ffe11640 227 $replystring = get_string("repliesmany", "forum", $post->replies);
501cdbd8 228 }
ffe11640 229 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><B>".get_string("discussthistopic", "forum")."</B></A> ($replystring)&nbsp;&nbsp;";
501cdbd8 230 }
231 echo "</P>";
232 if ($footer) {
233 echo "<P>$footer</P>";
234 }
235 echo "</DIV>";
236 echo "</TD></TR></TABLE>";
237 echo "</TD></TR>\n</TABLE>\n\n";
238}
239
aa153f29 240function forum_shorten_post($message) {
4d871a72 241 global $FORUM_LONG_POST, $FORUM_SHORT_POST;
aa153f29 242
243 if (strlen($message) > $FORUM_LONG_POST) {
4d871a72 244 // Look for the first return between $FORUM_SHORT_POST and $FORUM_LONG_POST
245 $shortmessage = substr($message, $FORUM_SHORT_POST, $FORUM_LONG_POST);
aa153f29 246 if ($pos = strpos($shortmessage, "\n")) {
4d871a72 247 return substr($message, 0, $FORUM_SHORT_POST + $pos);
aa153f29 248 } else {
ffe11640 249 return substr($message, 0, $FORUM_LONG_POST)."...";
aa153f29 250 }
251 } else {
252 return $message;
253 }
254}
255
501cdbd8 256
7a12aab4 257function forum_print_ratings($post) {
501cdbd8 258 if ($ratings = get_records_sql("SELECT * from forum_ratings WHERE post='$post'")) {
ffe11640 259 $sumrating[1] = 0;
260 $sumrating[2] = 0;
261 $sumrating[3] = 0;
501cdbd8 262 foreach ($ratings as $rating) {
ffe11640 263 $sumrating[$rating->rating]++;
501cdbd8 264 }
ffe11640 265 $summary = $sumrating[1]."s/".$sumrating[2]."/".$sumrating[3]."c";
501cdbd8 266
ffe11640 267 echo get_string("ratings", "forum").": ";
501cdbd8 268 link_to_popup_window ("/mod/forum/report.php?id=$post", "ratings", $summary, 400, 550);
501cdbd8 269 }
270}
271
7a12aab4 272function forum_print_rating($post, $user) {
501cdbd8 273 global $FORUM_POST_RATINGS;
274
275 if ($rs = get_record_sql("SELECT rating from forum_ratings WHERE user='$user' AND post='$post'")) {
501cdbd8 276 if ($FORUM_POST_RATINGS[$rs->rating]) {
ffe11640 277 echo "<FONT SIZE=-1>".get_string("youratedthis", "forum").": <FONT COLOR=green>";
501cdbd8 278 echo $FORUM_POST_RATINGS[$rs->rating];
ffe11640 279 echo "</FONT></FONT>";
280 return;
501cdbd8 281 }
501cdbd8 282 }
ffe11640 283 choose_from_menu($FORUM_POST_RATINGS, $post, "", get_string("rate", "forum")."...");
501cdbd8 284}
285
7a12aab4 286function forum_print_mode_form($discussion, $mode) {
287 GLOBAL $FORUM_LAYOUT_MODES;
501cdbd8 288
289 echo "<CENTER><P>";
7a12aab4 290 popup_form("discuss.php?d=$discussion&mode=", $FORUM_LAYOUT_MODES, "mode", $mode, "");
501cdbd8 291 echo "</P></CENTER>\n";
292}
293
7a12aab4 294function forum_print_search_form($course, $search="") {
501cdbd8 295 global $CFG;
296
297 echo "<TABLE BORDER=0 CELLPADDING=10 CELLSPACING=0><TR><TD ALIGN=CENTER>";
298 echo "<FORM NAME=search ACTION=\"$CFG->wwwroot/mod/forum/search.php\">";
299 echo "<INPUT NAME=search TYPE=text SIZE=15 VALUE=\"$search\"><BR>";
5d13db8a 300 echo "<INPUT VALUE=\"".get_string("searchforums", "forum")."\" TYPE=submit>";
501cdbd8 301 echo "<INPUT NAME=id TYPE=hidden VALUE=\"$course->id\">";
302 echo "</FORM>";
303 echo "</TD></TR></TABLE>";
304}
305
306
11b0c469 307function forum_count_discussion_replies($forum="0") {
9d1b97c5 308// Returns an array of counts of replies to each discussion (optionally in one forum)
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
9d1b97c5 318function forum_count_unrated_posts($discussionid, $userid) {
319// How many unrated posts are in the given discussion for a given user?
320 if ($posts = get_record_sql("SELECT count(*) as num
321 FROM forum_posts
322 WHERE parent > 0 AND
323 discussion = '$discussionid' AND
324 user <> '$userid' ")) {
325
326 if ($rated = get_record_sql("SELECT count(*) as num
327 FROM forum_posts p, forum_ratings r
1ebede32 328 WHERE p.discussion = '$discussionid'
329 AND p.id = r.post
330 AND r.user = '$userid'")) {
9d1b97c5 331 $difference = $posts->num - $rated->num;
332 if ($difference > 0) {
333 return $difference;
334 } else {
335 return 0; // Just in case there was a counting error
336 }
337 } else {
338 return $posts->num;
339 }
340 } else {
341 return 0;
342 }
343}
344
501cdbd8 345
11b0c469 346function forum_set_return() {
501cdbd8 347 global $SESSION, $HTTP_REFERER;
348
349 if (! $SESSION->fromdiscussion) {
350 $SESSION->fromdiscussion = $HTTP_REFERER;
8223d271 351 save_session("SESSION");
501cdbd8 352 }
353}
354
355
11b0c469 356function forum_go_back_to($default) {
501cdbd8 357 global $SESSION;
358
359 if ($SESSION->fromdiscussion) {
360 $returnto = $SESSION->fromdiscussion;
361 unset($SESSION->fromdiscussion);
8223d271 362 save_session("SESSION");
501cdbd8 363 return $returnto;
364 } else {
365 return $default;
366 }
367}
368
11b0c469 369function forum_get_post_full($postid) {
501cdbd8 370 return get_record_sql("SELECT p.*, u.firstname, u.lastname,
371 u.email, u.picture, u.id as userid
372 FROM forum_posts p, user u
373 WHERE p.id = '$postid' AND p.user = u.id");
374}
375
376
11b0c469 377function forum_add_new_post($post) {
501cdbd8 378
ffe11640 379 $post->created = $post->modified = time();
501cdbd8 380 $post->mailed = "0";
381
382 return insert_record("forum_posts", $post);
383}
384
11b0c469 385function forum_update_post($post) {
501cdbd8 386
ffe11640 387 $post->modified = time();
501cdbd8 388
0ab85112 389 if (!$post->parent) { // Post is a discussion starter - update discussion title too
390 set_field("forum_discussions", "name", $post->subject, "id", $post->discussion);
391 }
ffe11640 392 return update_record("forum_posts", $post);
501cdbd8 393}
394
395function forum_add_discussion($discussion) {
396// Given an object containing all the necessary data,
397// create a new discussion and return the id
398
399 GLOBAL $USER;
400
401 $timenow = time();
402
403 // The first post is stored as a real post, and linked
404 // to from the discuss entry.
405
406 $post->discussion = 0;
407 $post->parent = 0;
408 $post->user = $USER->id;
409 $post->created = $timenow;
410 $post->modified = $timenow;
411 $post->mailed = 0;
412 $post->subject = $discussion->name;
413 $post->message = $discussion->intro;
414
415 if (! $post->id = insert_record("forum_posts", $post) ) {
416 return 0;
417 }
418
419 // Now do the real module entry
420
421 $discussion->firstpost = $post->id;
422 $discussion->timemodified = $timenow;
423
424 if (! $discussion->id = insert_record("forum_discussions", $discussion) ) {
425 return 0;
426 }
427
428 // Finally, set the pointer on the post.
429 if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) {
430 return 0;
431 }
432
433 return $discussion->id;
434}
435
436
437function forum_delete_discussion($discussion) {
438// $discussion is a discussion record object
439
440 $result = true;
441
442 if ($posts = get_records("forum_posts", "discussion", $discussion->id)) {
443 foreach ($posts as $post) {
444 if (! delete_records("forum_ratings", "post", "$post->id")) {
445 $result = false;
446 }
447 }
448 }
449
450 if (! delete_records("forum_posts", "discussion", "$discussion->id")) {
451 $result = false;
452 }
453
454 if (! delete_records("forum_discussions", "id", "$discussion->id")) {
455 $result = false;
456 }
457
458 return $result;
459}
460
461
64eacd6f 462function forum_delete_post($postid) {
463 if (delete_records("forum_posts", "id", $postid)) {
464 delete_records("forum_ratings", "post", $postid); // Just in case
465 return true;
466 }
467 return false;
468}
469
501cdbd8 470
11b0c469 471function forum_print_user_discussions($courseid, $userid) {
8f9c8aaf 472 global $CFG, $USER;
501cdbd8 473
b76128b0 474 $discussions = get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture,
8f9c8aaf 475 u.id as userid, f.type as forumtype, f.name as forumname, f.id as forumid
4ca09091 476 FROM forum_discussions d, forum_posts p, user u, forum f
11b0c469 477 WHERE d.course = '$courseid' AND p.discussion = d.id AND
4ca09091 478 p.parent = 0 AND p.user = u.id AND u.id = '$userid' AND
479 d.forum = f.id
b76128b0 480 ORDER BY p.created ASC");
501cdbd8 481
11b0c469 482 if ($discussions) {
b76128b0 483 $user = get_record("user", "id", $userid);
501cdbd8 484 echo "<HR>";
ffe11640 485 print_heading( get_string("discussionsstartedby", "forum", "$user->firstname $user->lastname") );
11b0c469 486 $replies = forum_count_discussion_replies();
487 foreach ($discussions as $discussion) {
4ca09091 488 if (($discussion->forumtype == "teacher") and !isteacher($courseid)) {
489 continue;
490 }
11b0c469 491 if ($replies[$discussion->discussion]) {
492 $discussion->replies = $replies[$discussion->discussion]->replies;
501cdbd8 493 } else {
11b0c469 494 $discussion->replies = 0;
501cdbd8 495 }
ffe11640 496 $inforum = get_string("inforum", "forum", "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname</A>");
497 $discussion->subject .= " ($inforum)";
11b0c469 498 $ownpost = ($discussion->userid == $USER->id);
499 forum_print_post($discussion, $course->id, $ownpost, $reply=0, $link=1, $assessed=false);
501cdbd8 500 echo "<BR>\n";
501 }
502 }
503}
504
505
501cdbd8 506function forum_user_outline($course, $user, $mod, $forum) {
507
501cdbd8 508 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
498178e4 509 FROM forum f, forum_discussions d, forum_posts p, user u
510 WHERE f.id = '$forum->id' AND d.forum = f.id AND p.discussion = d.id
511 AND p.user = '$user->id' AND p.user = u.id
501cdbd8 512 ORDER BY p.modified ASC")) {
513
ffe11640 514 $result->info = get_string("numposts", "forum", count($posts));
501cdbd8 515
516 $lastpost = array_pop($posts);
517 $result->time = $lastpost->modified;
518 return $result;
519 }
520 return NULL;
521}
522
523
524function forum_user_complete($course, $user, $mod, $forum) {
525 global $CFG;
526
501cdbd8 527 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
498178e4 528 FROM forum f, forum_discussions d, forum_posts p, user u
529 WHERE f.id = '$forum->id' AND d.forum = f.id AND p.discussion = d.id
530 AND p.user = '$user->id' AND p.user = u.id
531 ORDER BY p.modified ASC")) {
501cdbd8 532
533 foreach ($posts as $post) {
534 if ($post->parent) {
ffe11640 535 $footer = "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">".
536 get_string("parentofthispost", "forum")."</A>";
501cdbd8 537 } else {
538 $footer = "";
539 }
540
77db7e4c 541 forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false, $rate=false, $footer);
501cdbd8 542 }
543
544 } else {
ffe11640 545 echo "<P>".get_string("noposts", "forum")."</P>";
501cdbd8 546 }
547
548}
549
04eba58f 550
551function forum_add_instance($forum) {
552// Given an object containing all the necessary data,
553// (defined by the form in mod.html) this function
554// will create a new instance and return the id number
555// of the new instance.
556
557 global $CFG;
558
559 $forum->timemodified = time();
560
561 if (! $forum->id = insert_record("forum", $forum)) {
562 return false;
563 }
564
565 if ($forum->type == "single") { // Create related discussion.
566
567 $discussion->course = $forum->course;
568 $discussion->forum = $forum->id;
569 $discussion->name = $forum->name;
570 $discussion->intro = $forum->intro;
571 $discussion->assessed = $forum->assessed;
572
573 if (! forum_add_discussion($discussion)) {
574 error("Could not add the discussion for this forum");
575 }
576 }
577 add_to_log($forum->course, "forum", "add", "index.php?f=$forum->id", "$forum->id");
578
579 return $forum->id;
580}
581
582
583function forum_update_instance($forum) {
584// Given an object containing all the necessary data,
585// (defined by the form in mod.html) this function
586// will update an existing instance with new data.
587
588 $forum->timemodified = time();
589 $forum->id = $forum->instance;
590
591 if ($forum->type == "single") { // Update related discussion and post.
592 if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
593 if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
594 notify("Warning! There is more than one discussion in this forum - using the most recent");
595 $discussion = array_pop($discussions);
596 } else {
597 error("Could not find the discussion in this forum");
598 }
599 }
600 if (! $post = get_record("forum_posts", "id", $discussion->firstpost)) {
601 error("Could not find the first post in this forum discussion");
602 }
603
604 $post->subject = $forum->name;
605 $post->message = $forum->intro;
606 $post->modified = $forum->timemodified;
607
608 if (! update_record("forum_posts", $post)) {
609 error("Could not update the first post");
610 }
611
612 $discussion->name = $forum->name;
613
614 if (! update_record("forum_discussions", $discussion)) {
615 error("Could not update the discussion");
616 }
617 }
618
619 if (update_record("forum", $forum)) {
620 add_to_log($forum->course, "forum", "update", "index.php?f=$forum->id", "$forum->id");
621 return true;
622 } else {
623 return false;
624 }
625}
626
627
628function forum_delete_instance($id) {
629// Given an ID of an instance of this module,
630// this function will permanently delete the instance
631// and any data that depends on it.
632
633 if (! $forum = get_record("forum", "id", "$id")) {
634 return false;
635 }
636
637 $result = true;
638
639 if ($discussions = get_records("forum_discussions", "forum", $forum->id)) {
640 foreach ($discussions as $discussion) {
641 if (! forum_delete_discussion($discussion)) {
642 $result = false;
643 }
644 }
645 }
646
647 if (! delete_records("forum_subscriptions", "forum", "$forum->id")) {
648 $result = false;
649 }
650
651 if (! delete_records("forum", "id", "$forum->id")) {
652 $result = false;
653 }
654
655 return $result;
656}
657
658
501cdbd8 659function forum_cron () {
660// Function to be run periodically according to the moodle cron
661// Finds all posts that have yet to be mailed out, and mails them
662
663 global $CFG;
664
501cdbd8 665 $cutofftime = time() - $CFG->maxeditingtime;
666
667 if ($posts = get_records_sql("SELECT p.*, d.course FROM forum_posts p, forum_discussions d
668 WHERE p.mailed = '0' AND p.created < '$cutofftime' AND p.discussion = d.id")) {
669
670 $timenow = time();
671
672 foreach ($posts as $post) {
673
ffe11640 674 print_string("processingpost", "forum", $post->id);
675 echo " ... ";
501cdbd8 676
677 if (! $userfrom = get_record("user", "id", "$post->user")) {
678 echo "Could not find user $post->user\n";
679 continue;
680 }
681
501cdbd8 682 if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) {
683 echo "Could not find discussion $post->discussion\n";
684 continue;
685 }
686
687 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
688 echo "Could not find forum $discussion->forum\n";
689 continue;
690 }
691
ffe11640 692 if (! $course = get_record("course", "id", "$forum->course")) {
693 echo "Could not find course $forum->course\n";
694 continue;
695 }
501cdbd8 696
86970225 697 if ($users = forum_subscribed_users($course, $forum)) {
ffe11640 698 $strforums = get_string("forums", "forum");
501cdbd8 699
ffe11640 700 $mailcount=0;
501cdbd8 701 foreach ($users as $userto) {
ffe11640 702 $by->name = "$userfrom->firstname $userfrom->lastname";
703 $by->date = userdate($post->created, "", $userto->timezone);
704 $strbynameondate = get_string("bynameondate", "forum", $by);
705
86970225 706 $postsubject = "$course->shortname: $post->subject";
707 $posttext = "$course->shortname -> $strforums -> $forum->name";
708
709 if ($discussion->name == $forum->name) {
710 $posttext .= "\n";
711 } else {
712 $posttext .= " -> $discussion->name\n";
713 }
714 $posttext .= "---------------------------------------------------------------------\n";
715 $posttext .= "$post->subject\n";
716 $posttext .= $strbynameondate."\n";
717 $posttext .= "---------------------------------------------------------------------\n";
718 $posttext .= strip_tags($post->message);
719 $posttext .= "\n\n";
720 $posttext .= "---------------------------------------------------------------------\n";
721 $posttext .= get_string("postmailinfo", "forum", $course->shortname)."\n";
722 $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id";
723
724 if ($userto->mailformat == 1) { // HTML
725 $posthtml = "<P><FONT FACE=sans-serif>".
726 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> -> ".
727 "<A HREF=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</A> -> ".
728 "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name</A>";
ffe11640 729 if ($discussion->name == $forum->name) {
86970225 730 $posthtml .= "</FONT></P>";
ffe11640 731 } else {
86970225 732 $posthtml .= " -> <A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name</A></FONT></P>";
ffe11640 733 }
86970225 734 $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, true, false, false);
735 } else {
736 $posthtml = "";
737 }
738
739 if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) {
740 echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email)\n";
741 } else {
742 $mailcount++;
501cdbd8 743 }
744 }
ffe11640 745 echo "mailed to $mailcount users ...";
501cdbd8 746 }
747
748 if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) {
749 echo "Could not update the mailed field for id $post->id\n";
750 }
ffe11640 751 echo "\n";
501cdbd8 752 }
753 }
754
755 return true;
756}
757
758
759function forum_forcesubscribe($forumid, $value=1) {
760 return set_field("forum", "forcesubscribe", $value, "id", $forumid);
761}
762
763function forum_is_forcesubscribed($forumid) {
764 return get_field("forum", "forcesubscribe", "id", $forumid);
765}
766
767function forum_is_subscribed($userid, $forumid) {
768 if (forum_is_forcesubscribed($forumid)) {
769 return true;
770 }
771 return record_exists_sql("SELECT * FROM forum_subscriptions WHERE user='$userid' AND forum='$forumid'");
772}
773
86970225 774function forum_subscribed_users($course, $forum) {
775// Returns list of user objects that are subscribed to this forum
776
777 if ($course->category) { // normal course
778 if ($forum->forcesubscribe) {
779 return get_course_users($course->id);
780 }
781 }
782 return get_records_sql("SELECT u.* FROM user u, forum_subscriptions s
783 WHERE s.forum = '$forum->id'
784 AND s.user = u.id");
785}
786
501cdbd8 787function forum_subscribe($userid, $forumid) {
788 global $db;
789
790 return $db->Execute("INSERT INTO forum_subscriptions SET user = '$userid', forum = '$forumid'");
791}
792
793function forum_unsubscribe($userid, $forumid) {
794 global $db;
795
796 return $db->Execute("DELETE FROM forum_subscriptions WHERE user = '$userid' AND forum = '$forumid'");
797}
798
799
11b0c469 800function forum_user_has_posted_discussion($forumid, $userid) {
801 if ($discussions = forum_get_discussions($forumid, "DESC", $userid)) {
501cdbd8 802 return true;
803 } else {
804 return false;
805 }
806}
807
11b0c469 808function forum_user_can_post_discussion($forum) {
501cdbd8 809// $forum is an object
810 global $USER;
811
812 if ($forum->type == "eachuser") {
11b0c469 813 return (! forum_user_has_posted_discussion($forum->id, $USER->id));
501cdbd8 814 } else if ($forum->type == "teacher") {
815 return isteacher($forum->course);
816 } else if (isteacher($forum->course)) {
817 return true;
818 } else {
819 return $forum->open;
820 }
821}
822
823
11b0c469 824function forum_get_discussions($forum="0", $forum_sort="DESC", $user=0) {
501cdbd8 825 if ($user) {
826 $userselect = " AND u.id = '$user' ";
827 } else {
828 $userselect = "";
829 }
830 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.id as userid
831 FROM forum_discussions d, forum_posts p, user u
832 WHERE d.forum = '$forum' AND p.discussion = d.id AND
833 p.parent= 0 AND p.user = u.id $userselect
834 ORDER BY p.created $forum_sort");
835}
836
837
838
11b0c469 839function forum_print_latest_discussions($forum_id=0, $forum_numdiscussions=5, $forum_style="plain", $forum_sort="DESC") {
f93f848a 840 global $CFG, $USER;
841
842 if ($forum_id) {
843 if (! $forum = get_record("forum", "id", $forum_id)) {
844 error("Forum ID was incorrect");
845 }
846 if (! $course = get_record("course", "id", $forum->course)) {
847 error("Could not find the course this forum belongs to!");
848 }
849
850 if ($course->category) {
851 require_login($course->id);
852 }
853
854 } else {
855 if (! $course = get_record("course", "category", 0)) {
856 error("Could not find a top-level course!");
857 }
11b0c469 858 if (! $forum = forum_get_course_news_forum($course->id)) {
f93f848a 859 error("Could not find or create a main forum in this course (id $course->id)");
860 }
861 }
862
11b0c469 863 if (forum_user_can_post_discussion($forum)) {
3b9af3dd 864 echo "<P ALIGN=CENTER>";
ffe11640 865 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?forum=$forum->id\">";
866 echo get_string("addanewdiscussion", "forum")."</A>...";
501cdbd8 867 echo "</P>\n";
77305fe6 868 }
869
11b0c469 870 if (! $discussions = forum_get_discussions($forum->id, $forum_sort) ) {
ffe11640 871 echo "<P ALIGN=CENTER><B>(".get_string("nodiscussions", "forum").")</B></P>";
f93f848a 872
873 } else {
874
11b0c469 875 $replies = forum_count_discussion_replies($forum->id);
f93f848a 876
11b0c469 877 $discussioncount = 0;
f93f848a 878
11b0c469 879 foreach ($discussions as $discussion) {
880 $discussioncount++;
f93f848a 881
11b0c469 882 if ($forum_numdiscussions && ($discussioncount > $forum_numdiscussions)) {
ffe11640 883 echo "<P ALIGN=right><A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">";
884 echo get_string("olderdiscussions", "forum")."</A> ...</P>";
f93f848a 885 break;
886 }
11b0c469 887 if ($replies[$discussion->discussion]) {
888 $discussion->replies = $replies[$discussion->discussion]->replies;
f93f848a 889 } else {
11b0c469 890 $discussion->replies = 0;
f93f848a 891 }
11b0c469 892 $ownpost = ($discussion->userid == $USER->id);
f93f848a 893 switch ($forum_style) {
894 case "minimal":
11b0c469 895 echo "<P><FONT COLOR=#555555>".userdate($discussion->modified, "%e %b, %H:%M")." - $discussion->firstname</FONT>";
896 echo "<BR>$discussion->subject ";
ffe11640 897 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->discussion\">";
898 echo get_string("more", "forum")."...</A>";
f93f848a 899 echo "</P>\n";
900 break;
901 default:
11b0c469 902 forum_print_post($discussion, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
f93f848a 903 echo "<BR>\n";
904 break;
905 }
906 }
907 }
f93f848a 908}
909
fe25fc9b 910function forum_print_discussion($course, $forum, $discussion, $post, $mode) {
501cdbd8 911
912 global $USER;
913
914 $ownpost = ($USER->id == $post->user);
915
11b0c469 916 forum_print_post($post, $course->id, $ownpost, $reply=true, $link=false, $rate=false);
501cdbd8 917
7a12aab4 918 forum_print_mode_form($discussion->id, $mode);
501cdbd8 919
9d1b97c5 920 $ratingform = false;
fe25fc9b 921 if ($forum->assessed && $USER->id) {
9d1b97c5 922 $unrated = forum_count_unrated_posts($discussion->id, $USER->id);
923 if ($unrated > 0) {
924 $ratingform = true;
925 }
926 }
927
928 if ($ratingform) {
501cdbd8 929 echo "<FORM NAME=form METHOD=POST ACTION=rate.php>";
930 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
931 }
932
933 switch ($mode) {
934 case 1 : // Flat ascending
935 case -1 : // Flat descending
936 default:
937 echo "<UL>";
75d10a02 938 forum_print_posts_flat($post->discussion, $course->id, $mode, $forum->assessed);
501cdbd8 939 echo "</UL>";
940 break;
941
942 case 2 : // Threaded
75d10a02 943 forum_print_posts_threaded($post->id, $course->id, 0, $forum->assessed);
501cdbd8 944 break;
945
946 case 3 : // Nested
75d10a02 947 forum_print_posts_nested($post->id, $course->id, $forum->assessed);
501cdbd8 948 break;
949 }
950
9d1b97c5 951 if ($ratingform) {
31d160d3 952 echo "<CENTER><P ALIGN=center><INPUT TYPE=submit VALUE=\"".get_string("sendinratings", "forum")."\">";
61ee082f 953 helpbutton("ratings", get_string("separateandconnected"), "forum");
31d160d3 954 echo "</P></CENTER>";
501cdbd8 955 echo "</FORM>";
956 }
957}
958
11b0c469 959function forum_print_posts_flat($discussion, $course, $direction, $assessed) {
501cdbd8 960 global $USER;
961
962 $reply = true;
963 $link = false;
964
965 if ($direction < 0) {
966 $sort = "ORDER BY created DESC";
967 } else {
968 $sort = "ORDER BY created ASC";
969 }
970
971 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
972 FROM forum_posts p, user u
973 WHERE p.discussion = $discussion AND p.parent > 0 AND p.user = u.id $sort")) {
974
975 foreach ($posts as $post) {
976 $ownpost = ($USER->id == $post->user);
11b0c469 977 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
501cdbd8 978 }
979 } else {
980 return;
981 }
982}
983
11b0c469 984function forum_print_posts_threaded($parent, $course, $depth, $assessed) {
501cdbd8 985 global $USER;
986
987 $reply = true;
988 $link = false;
989
990 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
991 FROM forum_posts p, user u
992 WHERE p.parent = '$parent' AND p.user = u.id")) {
993
994 foreach ($posts as $post) {
995
996 echo "<UL>";
997 if ($depth > 0) {
998 $ownpost = ($USER->id == $post->user);
11b0c469 999 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed); // link=true?
501cdbd8 1000 echo "<BR>";
1001 } else {
8c3c8481 1002 $by->name = "$post->firstname $post->lastname";
1003 $by->date = userdate($post->created);
1004 echo "<LI><P><FONT SIZE=-1><B><A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</A></B> ";
1005 print_string("bynameondate", "forum", $by);
1006 echo "</FONT></P></LI>";
501cdbd8 1007 }
1008
11b0c469 1009 forum_print_posts_threaded($post->id, $course, $depth-1, $assessed);
501cdbd8 1010 echo "</UL>\n";
1011 }
1012 } else {
1013 return;
1014 }
1015}
1016
11b0c469 1017function forum_print_posts_nested($parent, $course, $assessed) {
501cdbd8 1018 global $USER;
1019
1020 $reply = true;
1021 $link = false;
1022
1023 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
1024 FROM forum_posts p, user u
1025 WHERE p.parent = $parent AND p.user = u.id
1026 ORDER BY p.created ASC ")) {
1027
1028 foreach ($posts as $post) {
1029
1030 $ownpost = ($USER->id == $post->user);
1031
1032 echo "<UL>";
11b0c469 1033 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
501cdbd8 1034 echo "<BR>";
11b0c469 1035 forum_print_posts_nested($post->id, $course, $assessed);
501cdbd8 1036 echo "</UL>\n";
1037 }
1038 } else {
1039 return;
1040 }
1041}
1042
1043function forum_set_display_mode($mode=0) {
1044 global $USER;
1045
1046 if ($mode) {
1047 $USER->mode = $mode;
8223d271 1048 save_session("USER");
501cdbd8 1049 } else if (!$USER->mode) {
1050 $USER->mode = $FORUM_DEFAULT_DISPLAY_MODE;
8223d271 1051 save_session("USER");
501cdbd8 1052 }
1053}
f93f848a 1054
1055?>