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