Fixes to list of subscribers (language fixes, and fix for site-level forums)
[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;";
109 $output .= "</TD><TD BGCOLOR=\"#FFFFFF\">\n";
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) {
165 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading\">";
166 } else {
167 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading2\">";
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;";
178 echo "</TD><TD BGCOLOR=\"#FFFFFF\">\n";
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
ffe11640 389 return update_record("forum_posts", $post);
501cdbd8 390}
391
392function forum_add_discussion($discussion) {
393// Given an object containing all the necessary data,
394// create a new discussion and return the id
395
396 GLOBAL $USER;
397
398 $timenow = time();
399
400 // The first post is stored as a real post, and linked
401 // to from the discuss entry.
402
403 $post->discussion = 0;
404 $post->parent = 0;
405 $post->user = $USER->id;
406 $post->created = $timenow;
407 $post->modified = $timenow;
408 $post->mailed = 0;
409 $post->subject = $discussion->name;
410 $post->message = $discussion->intro;
411
412 if (! $post->id = insert_record("forum_posts", $post) ) {
413 return 0;
414 }
415
416 // Now do the real module entry
417
418 $discussion->firstpost = $post->id;
419 $discussion->timemodified = $timenow;
420
421 if (! $discussion->id = insert_record("forum_discussions", $discussion) ) {
422 return 0;
423 }
424
425 // Finally, set the pointer on the post.
426 if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) {
427 return 0;
428 }
429
430 return $discussion->id;
431}
432
433
434function forum_delete_discussion($discussion) {
435// $discussion is a discussion record object
436
437 $result = true;
438
439 if ($posts = get_records("forum_posts", "discussion", $discussion->id)) {
440 foreach ($posts as $post) {
441 if (! delete_records("forum_ratings", "post", "$post->id")) {
442 $result = false;
443 }
444 }
445 }
446
447 if (! delete_records("forum_posts", "discussion", "$discussion->id")) {
448 $result = false;
449 }
450
451 if (! delete_records("forum_discussions", "id", "$discussion->id")) {
452 $result = false;
453 }
454
455 return $result;
456}
457
458
64eacd6f 459function forum_delete_post($postid) {
460 if (delete_records("forum_posts", "id", $postid)) {
461 delete_records("forum_ratings", "post", $postid); // Just in case
462 return true;
463 }
464 return false;
465}
466
501cdbd8 467
11b0c469 468function forum_print_user_discussions($courseid, $userid) {
8f9c8aaf 469 global $CFG, $USER;
501cdbd8 470
b76128b0 471 $discussions = get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture,
8f9c8aaf 472 u.id as userid, f.type as forumtype, f.name as forumname, f.id as forumid
4ca09091 473 FROM forum_discussions d, forum_posts p, user u, forum f
11b0c469 474 WHERE d.course = '$courseid' AND p.discussion = d.id AND
4ca09091 475 p.parent = 0 AND p.user = u.id AND u.id = '$userid' AND
476 d.forum = f.id
b76128b0 477 ORDER BY p.created ASC");
501cdbd8 478
11b0c469 479 if ($discussions) {
b76128b0 480 $user = get_record("user", "id", $userid);
501cdbd8 481 echo "<HR>";
ffe11640 482 print_heading( get_string("discussionsstartedby", "forum", "$user->firstname $user->lastname") );
11b0c469 483 $replies = forum_count_discussion_replies();
484 foreach ($discussions as $discussion) {
4ca09091 485 if (($discussion->forumtype == "teacher") and !isteacher($courseid)) {
486 continue;
487 }
11b0c469 488 if ($replies[$discussion->discussion]) {
489 $discussion->replies = $replies[$discussion->discussion]->replies;
501cdbd8 490 } else {
11b0c469 491 $discussion->replies = 0;
501cdbd8 492 }
ffe11640 493 $inforum = get_string("inforum", "forum", "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname</A>");
494 $discussion->subject .= " ($inforum)";
11b0c469 495 $ownpost = ($discussion->userid == $USER->id);
496 forum_print_post($discussion, $course->id, $ownpost, $reply=0, $link=1, $assessed=false);
501cdbd8 497 echo "<BR>\n";
498 }
499 }
500}
501
502
501cdbd8 503function forum_user_outline($course, $user, $mod, $forum) {
504
501cdbd8 505 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
498178e4 506 FROM forum f, forum_discussions d, forum_posts p, user u
507 WHERE f.id = '$forum->id' AND d.forum = f.id AND p.discussion = d.id
508 AND p.user = '$user->id' AND p.user = u.id
501cdbd8 509 ORDER BY p.modified ASC")) {
510
ffe11640 511 $result->info = get_string("numposts", "forum", count($posts));
501cdbd8 512
513 $lastpost = array_pop($posts);
514 $result->time = $lastpost->modified;
515 return $result;
516 }
517 return NULL;
518}
519
520
521function forum_user_complete($course, $user, $mod, $forum) {
522 global $CFG;
523
501cdbd8 524 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
498178e4 525 FROM forum f, forum_discussions d, forum_posts p, user u
526 WHERE f.id = '$forum->id' AND d.forum = f.id AND p.discussion = d.id
527 AND p.user = '$user->id' AND p.user = u.id
528 ORDER BY p.modified ASC")) {
501cdbd8 529
530 foreach ($posts as $post) {
531 if ($post->parent) {
ffe11640 532 $footer = "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">".
533 get_string("parentofthispost", "forum")."</A>";
501cdbd8 534 } else {
535 $footer = "";
536 }
537
77db7e4c 538 forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false, $rate=false, $footer);
501cdbd8 539 }
540
541 } else {
ffe11640 542 echo "<P>".get_string("noposts", "forum")."</P>";
501cdbd8 543 }
544
545}
546
04eba58f 547
548function forum_add_instance($forum) {
549// Given an object containing all the necessary data,
550// (defined by the form in mod.html) this function
551// will create a new instance and return the id number
552// of the new instance.
553
554 global $CFG;
555
556 $forum->timemodified = time();
557
558 if (! $forum->id = insert_record("forum", $forum)) {
559 return false;
560 }
561
562 if ($forum->type == "single") { // Create related discussion.
563
564 $discussion->course = $forum->course;
565 $discussion->forum = $forum->id;
566 $discussion->name = $forum->name;
567 $discussion->intro = $forum->intro;
568 $discussion->assessed = $forum->assessed;
569
570 if (! forum_add_discussion($discussion)) {
571 error("Could not add the discussion for this forum");
572 }
573 }
574 add_to_log($forum->course, "forum", "add", "index.php?f=$forum->id", "$forum->id");
575
576 return $forum->id;
577}
578
579
580function forum_update_instance($forum) {
581// Given an object containing all the necessary data,
582// (defined by the form in mod.html) this function
583// will update an existing instance with new data.
584
585 $forum->timemodified = time();
586 $forum->id = $forum->instance;
587
588 if ($forum->type == "single") { // Update related discussion and post.
589 if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
590 if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
591 notify("Warning! There is more than one discussion in this forum - using the most recent");
592 $discussion = array_pop($discussions);
593 } else {
594 error("Could not find the discussion in this forum");
595 }
596 }
597 if (! $post = get_record("forum_posts", "id", $discussion->firstpost)) {
598 error("Could not find the first post in this forum discussion");
599 }
600
601 $post->subject = $forum->name;
602 $post->message = $forum->intro;
603 $post->modified = $forum->timemodified;
604
605 if (! update_record("forum_posts", $post)) {
606 error("Could not update the first post");
607 }
608
609 $discussion->name = $forum->name;
610
611 if (! update_record("forum_discussions", $discussion)) {
612 error("Could not update the discussion");
613 }
614 }
615
616 if (update_record("forum", $forum)) {
617 add_to_log($forum->course, "forum", "update", "index.php?f=$forum->id", "$forum->id");
618 return true;
619 } else {
620 return false;
621 }
622}
623
624
625function forum_delete_instance($id) {
626// Given an ID of an instance of this module,
627// this function will permanently delete the instance
628// and any data that depends on it.
629
630 if (! $forum = get_record("forum", "id", "$id")) {
631 return false;
632 }
633
634 $result = true;
635
636 if ($discussions = get_records("forum_discussions", "forum", $forum->id)) {
637 foreach ($discussions as $discussion) {
638 if (! forum_delete_discussion($discussion)) {
639 $result = false;
640 }
641 }
642 }
643
644 if (! delete_records("forum_subscriptions", "forum", "$forum->id")) {
645 $result = false;
646 }
647
648 if (! delete_records("forum", "id", "$forum->id")) {
649 $result = false;
650 }
651
652 return $result;
653}
654
655
501cdbd8 656function forum_cron () {
657// Function to be run periodically according to the moodle cron
658// Finds all posts that have yet to be mailed out, and mails them
659
660 global $CFG;
661
501cdbd8 662 $cutofftime = time() - $CFG->maxeditingtime;
663
664 if ($posts = get_records_sql("SELECT p.*, d.course FROM forum_posts p, forum_discussions d
665 WHERE p.mailed = '0' AND p.created < '$cutofftime' AND p.discussion = d.id")) {
666
667 $timenow = time();
668
669 foreach ($posts as $post) {
670
ffe11640 671 print_string("processingpost", "forum", $post->id);
672 echo " ... ";
501cdbd8 673
674 if (! $userfrom = get_record("user", "id", "$post->user")) {
675 echo "Could not find user $post->user\n";
676 continue;
677 }
678
501cdbd8 679 if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) {
680 echo "Could not find discussion $post->discussion\n";
681 continue;
682 }
683
684 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
685 echo "Could not find forum $discussion->forum\n";
686 continue;
687 }
688
ffe11640 689 if (! $course = get_record("course", "id", "$forum->course")) {
690 echo "Could not find course $forum->course\n";
691 continue;
692 }
501cdbd8 693
ffe11640 694 if ($users = get_course_users($course->id)) {
695 $strforums = get_string("forums", "forum");
501cdbd8 696
ffe11640 697 $mailcount=0;
501cdbd8 698 foreach ($users as $userto) {
ffe11640 699 $by->name = "$userfrom->firstname $userfrom->lastname";
700 $by->date = userdate($post->created, "", $userto->timezone);
701 $strbynameondate = get_string("bynameondate", "forum", $by);
702
703 if (forum_is_subscribed($userto->id, $forum->id)) {
704 $postsubject = "$course->shortname: $post->subject";
705 $posttext = "$course->shortname -> $strforums -> $forum->name";
706 if ($discussion->name == $forum->name) {
707 $posttext .= "\n";
708 } else {
709 $posttext .= " -> $discussion->name\n";
710 }
711 $posttext .= "---------------------------------------------------------------------\n";
712 $posttext .= "$post->subject\n";
713 $posttext .= $strbynameondate."\n";
714 $posttext .= "---------------------------------------------------------------------\n";
715 $posttext .= strip_tags($post->message);
716 $posttext .= "\n\n";
717 $posttext .= "---------------------------------------------------------------------\n";
718 $posttext .= get_string("postmailinfo", "forum", $course->shortname)."\n";
719 $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id";
720
721 if ($userto->mailformat == 1) { // HTML
722 $posthtml = "<P><FONT FACE=sans-serif>".
723 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> -> ".
724 "<A HREF=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</A> -> ".
725 "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name</A>";
726 if ($discussion->name == $forum->name) {
727 $posthtml .= "</FONT></P>";
728 } else {
729 $posthtml .= " -> <A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name</A></FONT></P>";
730 }
731 $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, true, false, false);
732 } else {
733 $posthtml = "";
734 }
735
736 if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) {
737 echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email)\n";
738 } else {
739 $mailcount++;
740 }
501cdbd8 741 }
742 }
ffe11640 743 echo "mailed to $mailcount users ...";
501cdbd8 744 }
745
746 if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) {
747 echo "Could not update the mailed field for id $post->id\n";
748 }
ffe11640 749 echo "\n";
501cdbd8 750 }
751 }
752
753 return true;
754}
755
756
757function forum_forcesubscribe($forumid, $value=1) {
758 return set_field("forum", "forcesubscribe", $value, "id", $forumid);
759}
760
761function forum_is_forcesubscribed($forumid) {
762 return get_field("forum", "forcesubscribe", "id", $forumid);
763}
764
765function forum_is_subscribed($userid, $forumid) {
766 if (forum_is_forcesubscribed($forumid)) {
767 return true;
768 }
769 return record_exists_sql("SELECT * FROM forum_subscriptions WHERE user='$userid' AND forum='$forumid'");
770}
771
772function forum_subscribe($userid, $forumid) {
773 global $db;
774
775 return $db->Execute("INSERT INTO forum_subscriptions SET user = '$userid', forum = '$forumid'");
776}
777
778function forum_unsubscribe($userid, $forumid) {
779 global $db;
780
781 return $db->Execute("DELETE FROM forum_subscriptions WHERE user = '$userid' AND forum = '$forumid'");
782}
783
784
11b0c469 785function forum_user_has_posted_discussion($forumid, $userid) {
786 if ($discussions = forum_get_discussions($forumid, "DESC", $userid)) {
501cdbd8 787 return true;
788 } else {
789 return false;
790 }
791}
792
11b0c469 793function forum_user_can_post_discussion($forum) {
501cdbd8 794// $forum is an object
795 global $USER;
796
797 if ($forum->type == "eachuser") {
11b0c469 798 return (! forum_user_has_posted_discussion($forum->id, $USER->id));
501cdbd8 799 } else if ($forum->type == "teacher") {
800 return isteacher($forum->course);
801 } else if (isteacher($forum->course)) {
802 return true;
803 } else {
804 return $forum->open;
805 }
806}
807
808
11b0c469 809function forum_get_discussions($forum="0", $forum_sort="DESC", $user=0) {
501cdbd8 810 if ($user) {
811 $userselect = " AND u.id = '$user' ";
812 } else {
813 $userselect = "";
814 }
815 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.id as userid
816 FROM forum_discussions d, forum_posts p, user u
817 WHERE d.forum = '$forum' AND p.discussion = d.id AND
818 p.parent= 0 AND p.user = u.id $userselect
819 ORDER BY p.created $forum_sort");
820}
821
822
823
11b0c469 824function forum_print_latest_discussions($forum_id=0, $forum_numdiscussions=5, $forum_style="plain", $forum_sort="DESC") {
f93f848a 825 global $CFG, $USER;
826
827 if ($forum_id) {
828 if (! $forum = get_record("forum", "id", $forum_id)) {
829 error("Forum ID was incorrect");
830 }
831 if (! $course = get_record("course", "id", $forum->course)) {
832 error("Could not find the course this forum belongs to!");
833 }
834
835 if ($course->category) {
836 require_login($course->id);
837 }
838
839 } else {
840 if (! $course = get_record("course", "category", 0)) {
841 error("Could not find a top-level course!");
842 }
11b0c469 843 if (! $forum = forum_get_course_news_forum($course->id)) {
f93f848a 844 error("Could not find or create a main forum in this course (id $course->id)");
845 }
846 }
847
11b0c469 848 if (forum_user_can_post_discussion($forum)) {
3b9af3dd 849 echo "<P ALIGN=CENTER>";
ffe11640 850 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?forum=$forum->id\">";
851 echo get_string("addanewdiscussion", "forum")."</A>...";
501cdbd8 852 echo "</P>\n";
77305fe6 853 }
854
11b0c469 855 if (! $discussions = forum_get_discussions($forum->id, $forum_sort) ) {
ffe11640 856 echo "<P ALIGN=CENTER><B>(".get_string("nodiscussions", "forum").")</B></P>";
f93f848a 857
858 } else {
859
11b0c469 860 $replies = forum_count_discussion_replies($forum->id);
f93f848a 861
11b0c469 862 $discussioncount = 0;
f93f848a 863
11b0c469 864 foreach ($discussions as $discussion) {
865 $discussioncount++;
f93f848a 866
11b0c469 867 if ($forum_numdiscussions && ($discussioncount > $forum_numdiscussions)) {
ffe11640 868 echo "<P ALIGN=right><A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">";
869 echo get_string("olderdiscussions", "forum")."</A> ...</P>";
f93f848a 870 break;
871 }
11b0c469 872 if ($replies[$discussion->discussion]) {
873 $discussion->replies = $replies[$discussion->discussion]->replies;
f93f848a 874 } else {
11b0c469 875 $discussion->replies = 0;
f93f848a 876 }
11b0c469 877 $ownpost = ($discussion->userid == $USER->id);
f93f848a 878 switch ($forum_style) {
879 case "minimal":
11b0c469 880 echo "<P><FONT COLOR=#555555>".userdate($discussion->modified, "%e %b, %H:%M")." - $discussion->firstname</FONT>";
881 echo "<BR>$discussion->subject ";
ffe11640 882 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->discussion\">";
883 echo get_string("more", "forum")."...</A>";
f93f848a 884 echo "</P>\n";
885 break;
886 default:
11b0c469 887 forum_print_post($discussion, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
f93f848a 888 echo "<BR>\n";
889 break;
890 }
891 }
892 }
f93f848a 893}
894
fe25fc9b 895function forum_print_discussion($course, $forum, $discussion, $post, $mode) {
501cdbd8 896
897 global $USER;
898
899 $ownpost = ($USER->id == $post->user);
900
11b0c469 901 forum_print_post($post, $course->id, $ownpost, $reply=true, $link=false, $rate=false);
501cdbd8 902
7a12aab4 903 forum_print_mode_form($discussion->id, $mode);
501cdbd8 904
9d1b97c5 905 $ratingform = false;
fe25fc9b 906 if ($forum->assessed && $USER->id) {
9d1b97c5 907 $unrated = forum_count_unrated_posts($discussion->id, $USER->id);
908 if ($unrated > 0) {
909 $ratingform = true;
910 }
911 }
912
913 if ($ratingform) {
501cdbd8 914 echo "<FORM NAME=form METHOD=POST ACTION=rate.php>";
915 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
916 }
917
918 switch ($mode) {
919 case 1 : // Flat ascending
920 case -1 : // Flat descending
921 default:
922 echo "<UL>";
75d10a02 923 forum_print_posts_flat($post->discussion, $course->id, $mode, $forum->assessed);
501cdbd8 924 echo "</UL>";
925 break;
926
927 case 2 : // Threaded
75d10a02 928 forum_print_posts_threaded($post->id, $course->id, 0, $forum->assessed);
501cdbd8 929 break;
930
931 case 3 : // Nested
75d10a02 932 forum_print_posts_nested($post->id, $course->id, $forum->assessed);
501cdbd8 933 break;
934 }
935
9d1b97c5 936 if ($ratingform) {
8c3c8481 937 echo "<CENTER><P ALIGN=center><INPUT TYPE=submit VALUE=\"".get_string("sendinratings", "forum")."\"></P></CENTER>";
501cdbd8 938 echo "</FORM>";
939 }
940}
941
11b0c469 942function forum_print_posts_flat($discussion, $course, $direction, $assessed) {
501cdbd8 943 global $USER;
944
945 $reply = true;
946 $link = false;
947
948 if ($direction < 0) {
949 $sort = "ORDER BY created DESC";
950 } else {
951 $sort = "ORDER BY created ASC";
952 }
953
954 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
955 FROM forum_posts p, user u
956 WHERE p.discussion = $discussion AND p.parent > 0 AND p.user = u.id $sort")) {
957
958 foreach ($posts as $post) {
959 $ownpost = ($USER->id == $post->user);
11b0c469 960 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
501cdbd8 961 }
962 } else {
963 return;
964 }
965}
966
11b0c469 967function forum_print_posts_threaded($parent, $course, $depth, $assessed) {
501cdbd8 968 global $USER;
969
970 $reply = true;
971 $link = false;
972
973 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
974 FROM forum_posts p, user u
975 WHERE p.parent = '$parent' AND p.user = u.id")) {
976
977 foreach ($posts as $post) {
978
979 echo "<UL>";
980 if ($depth > 0) {
981 $ownpost = ($USER->id == $post->user);
11b0c469 982 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed); // link=true?
501cdbd8 983 echo "<BR>";
984 } else {
8c3c8481 985 $by->name = "$post->firstname $post->lastname";
986 $by->date = userdate($post->created);
987 echo "<LI><P><FONT SIZE=-1><B><A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</A></B> ";
988 print_string("bynameondate", "forum", $by);
989 echo "</FONT></P></LI>";
501cdbd8 990 }
991
11b0c469 992 forum_print_posts_threaded($post->id, $course, $depth-1, $assessed);
501cdbd8 993 echo "</UL>\n";
994 }
995 } else {
996 return;
997 }
998}
999
11b0c469 1000function forum_print_posts_nested($parent, $course, $assessed) {
501cdbd8 1001 global $USER;
1002
1003 $reply = true;
1004 $link = false;
1005
1006 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
1007 FROM forum_posts p, user u
1008 WHERE p.parent = $parent AND p.user = u.id
1009 ORDER BY p.created ASC ")) {
1010
1011 foreach ($posts as $post) {
1012
1013 $ownpost = ($USER->id == $post->user);
1014
1015 echo "<UL>";
11b0c469 1016 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
501cdbd8 1017 echo "<BR>";
11b0c469 1018 forum_print_posts_nested($post->id, $course, $assessed);
501cdbd8 1019 echo "</UL>\n";
1020 }
1021 } else {
1022 return;
1023 }
1024}
1025
1026function forum_set_display_mode($mode=0) {
1027 global $USER;
1028
1029 if ($mode) {
1030 $USER->mode = $mode;
8223d271 1031 save_session("USER");
501cdbd8 1032 } else if (!$USER->mode) {
1033 $USER->mode = $FORUM_DEFAULT_DISPLAY_MODE;
8223d271 1034 save_session("USER");
501cdbd8 1035 }
1036}
f93f848a 1037
1038?>