Needed to delete forum_subscriptions as well.
[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");
186 echo "</A> (".get_string("numwords", "forum", $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 }
ffe11640 199 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."</A>";
501cdbd8 200 if ($reply) {
ffe11640 201 echo "| <A HREF=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</A>";
501cdbd8 202 }
203 echo "&nbsp;&nbsp;";
204 } else {
205 if ($reply) {
ffe11640 206 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</A>&nbsp;&nbsp;";
501cdbd8 207 }
208 }
209
210
211 echo "<DIV ALIGN=right><P ALIGN=right>";
212 if ($rate && $USER->id) {
213 if ($USER->id == $post->userid) {
7a12aab4 214 forum_print_ratings($post->id);
501cdbd8 215 } else {
7a12aab4 216 forum_print_rating($post->id, $USER->id);
501cdbd8 217 }
218 }
219
220 if ($link) {
221 if ($post->replies == 1) {
ffe11640 222 $replystring = get_string("repliesone", "forum", $post->replies);
501cdbd8 223 } else {
ffe11640 224 $replystring = get_string("repliesmany", "forum", $post->replies);
501cdbd8 225 }
ffe11640 226 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><B>".get_string("discussthistopic", "forum")."</B></A> ($replystring)&nbsp;&nbsp;";
501cdbd8 227 }
228 echo "</P>";
229 if ($footer) {
230 echo "<P>$footer</P>";
231 }
232 echo "</DIV>";
233 echo "</TD></TR></TABLE>";
234 echo "</TD></TR>\n</TABLE>\n\n";
235}
236
aa153f29 237function forum_shorten_post($message) {
4d871a72 238 global $FORUM_LONG_POST, $FORUM_SHORT_POST;
aa153f29 239
240 if (strlen($message) > $FORUM_LONG_POST) {
4d871a72 241 // Look for the first return between $FORUM_SHORT_POST and $FORUM_LONG_POST
242 $shortmessage = substr($message, $FORUM_SHORT_POST, $FORUM_LONG_POST);
aa153f29 243 if ($pos = strpos($shortmessage, "\n")) {
4d871a72 244 return substr($message, 0, $FORUM_SHORT_POST + $pos);
aa153f29 245 } else {
ffe11640 246 return substr($message, 0, $FORUM_LONG_POST)."...";
aa153f29 247 }
248 } else {
249 return $message;
250 }
251}
252
501cdbd8 253
7a12aab4 254function forum_print_ratings($post) {
501cdbd8 255 if ($ratings = get_records_sql("SELECT * from forum_ratings WHERE post='$post'")) {
ffe11640 256 $sumrating[1] = 0;
257 $sumrating[2] = 0;
258 $sumrating[3] = 0;
501cdbd8 259 foreach ($ratings as $rating) {
ffe11640 260 $sumrating[$rating->rating]++;
501cdbd8 261 }
ffe11640 262 $summary = $sumrating[1]."s/".$sumrating[2]."/".$sumrating[3]."c";
501cdbd8 263
ffe11640 264 echo get_string("ratings", "forum").": ";
501cdbd8 265 link_to_popup_window ("/mod/forum/report.php?id=$post", "ratings", $summary, 400, 550);
501cdbd8 266 }
267}
268
7a12aab4 269function forum_print_rating($post, $user) {
501cdbd8 270 global $FORUM_POST_RATINGS;
271
272 if ($rs = get_record_sql("SELECT rating from forum_ratings WHERE user='$user' AND post='$post'")) {
501cdbd8 273 if ($FORUM_POST_RATINGS[$rs->rating]) {
ffe11640 274 echo "<FONT SIZE=-1>".get_string("youratedthis", "forum").": <FONT COLOR=green>";
501cdbd8 275 echo $FORUM_POST_RATINGS[$rs->rating];
ffe11640 276 echo "</FONT></FONT>";
277 return;
501cdbd8 278 }
501cdbd8 279 }
ffe11640 280 choose_from_menu($FORUM_POST_RATINGS, $post, "", get_string("rate", "forum")."...");
501cdbd8 281}
282
7a12aab4 283function forum_print_mode_form($discussion, $mode) {
284 GLOBAL $FORUM_LAYOUT_MODES;
501cdbd8 285
286 echo "<CENTER><P>";
7a12aab4 287 popup_form("discuss.php?d=$discussion&mode=", $FORUM_LAYOUT_MODES, "mode", $mode, "");
501cdbd8 288 echo "</P></CENTER>\n";
289}
290
7a12aab4 291function forum_print_search_form($course, $search="") {
501cdbd8 292 global $CFG;
293
294 echo "<TABLE BORDER=0 CELLPADDING=10 CELLSPACING=0><TR><TD ALIGN=CENTER>";
295 echo "<FORM NAME=search ACTION=\"$CFG->wwwroot/mod/forum/search.php\">";
296 echo "<INPUT NAME=search TYPE=text SIZE=15 VALUE=\"$search\"><BR>";
297 echo "<INPUT VALUE=\"".get_string("search", "forum")."\" TYPE=submit>";
298 echo "<INPUT NAME=id TYPE=hidden VALUE=\"$course->id\">";
299 echo "</FORM>";
300 echo "</TD></TR></TABLE>";
301}
302
303
11b0c469 304function forum_count_discussion_replies($forum="0") {
501cdbd8 305 if ($forum) {
306 $forumselect = " AND d.forum = '$forum'";
307 }
308 return get_records_sql("SELECT p.discussion, (count(*)) as replies
309 FROM forum_posts p, forum_discussions d
310 WHERE p.parent > 0 AND p.discussion = d.id
311 GROUP BY p.discussion");
312}
313
314
11b0c469 315function forum_set_return() {
501cdbd8 316 global $SESSION, $HTTP_REFERER;
317
318 if (! $SESSION->fromdiscussion) {
319 $SESSION->fromdiscussion = $HTTP_REFERER;
320 }
321}
322
323
11b0c469 324function forum_go_back_to($default) {
501cdbd8 325 global $SESSION;
326
327 if ($SESSION->fromdiscussion) {
328 $returnto = $SESSION->fromdiscussion;
329 unset($SESSION->fromdiscussion);
330 return $returnto;
331 } else {
332 return $default;
333 }
334}
335
11b0c469 336function forum_get_post_full($postid) {
501cdbd8 337 return get_record_sql("SELECT p.*, u.firstname, u.lastname,
338 u.email, u.picture, u.id as userid
339 FROM forum_posts p, user u
340 WHERE p.id = '$postid' AND p.user = u.id");
341}
342
343
11b0c469 344function forum_add_new_post($post) {
501cdbd8 345
ffe11640 346 $post->created = $post->modified = time();
501cdbd8 347 $post->mailed = "0";
348
349 return insert_record("forum_posts", $post);
350}
351
11b0c469 352function forum_update_post($post) {
501cdbd8 353
ffe11640 354 $post->modified = time();
501cdbd8 355
ffe11640 356 return update_record("forum_posts", $post);
501cdbd8 357}
358
359function forum_add_discussion($discussion) {
360// Given an object containing all the necessary data,
361// create a new discussion and return the id
362
363 GLOBAL $USER;
364
365 $timenow = time();
366
367 // The first post is stored as a real post, and linked
368 // to from the discuss entry.
369
370 $post->discussion = 0;
371 $post->parent = 0;
372 $post->user = $USER->id;
373 $post->created = $timenow;
374 $post->modified = $timenow;
375 $post->mailed = 0;
376 $post->subject = $discussion->name;
377 $post->message = $discussion->intro;
378
379 if (! $post->id = insert_record("forum_posts", $post) ) {
380 return 0;
381 }
382
383 // Now do the real module entry
384
385 $discussion->firstpost = $post->id;
386 $discussion->timemodified = $timenow;
387
388 if (! $discussion->id = insert_record("forum_discussions", $discussion) ) {
389 return 0;
390 }
391
392 // Finally, set the pointer on the post.
393 if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) {
394 return 0;
395 }
396
397 return $discussion->id;
398}
399
400
401function forum_delete_discussion($discussion) {
402// $discussion is a discussion record object
403
404 $result = true;
405
406 if ($posts = get_records("forum_posts", "discussion", $discussion->id)) {
407 foreach ($posts as $post) {
408 if (! delete_records("forum_ratings", "post", "$post->id")) {
409 $result = false;
410 }
411 }
412 }
413
414 if (! delete_records("forum_posts", "discussion", "$discussion->id")) {
415 $result = false;
416 }
417
418 if (! delete_records("forum_discussions", "id", "$discussion->id")) {
419 $result = false;
420 }
421
422 return $result;
423}
424
425
426
11b0c469 427function forum_print_user_discussions($courseid, $userid) {
8f9c8aaf 428 global $CFG, $USER;
501cdbd8 429
b76128b0 430 $discussions = get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture,
8f9c8aaf 431 u.id as userid, f.type as forumtype, f.name as forumname, f.id as forumid
4ca09091 432 FROM forum_discussions d, forum_posts p, user u, forum f
11b0c469 433 WHERE d.course = '$courseid' AND p.discussion = d.id AND
4ca09091 434 p.parent = 0 AND p.user = u.id AND u.id = '$userid' AND
435 d.forum = f.id
b76128b0 436 ORDER BY p.created ASC");
501cdbd8 437
11b0c469 438 if ($discussions) {
b76128b0 439 $user = get_record("user", "id", $userid);
501cdbd8 440 echo "<HR>";
ffe11640 441 print_heading( get_string("discussionsstartedby", "forum", "$user->firstname $user->lastname") );
11b0c469 442 $replies = forum_count_discussion_replies();
443 foreach ($discussions as $discussion) {
4ca09091 444 if (($discussion->forumtype == "teacher") and !isteacher($courseid)) {
445 continue;
446 }
11b0c469 447 if ($replies[$discussion->discussion]) {
448 $discussion->replies = $replies[$discussion->discussion]->replies;
501cdbd8 449 } else {
11b0c469 450 $discussion->replies = 0;
501cdbd8 451 }
ffe11640 452 $inforum = get_string("inforum", "forum", "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname</A>");
453 $discussion->subject .= " ($inforum)";
11b0c469 454 $ownpost = ($discussion->userid == $USER->id);
455 forum_print_post($discussion, $course->id, $ownpost, $reply=0, $link=1, $assessed=false);
501cdbd8 456 echo "<BR>\n";
457 }
458 }
459}
460
461
501cdbd8 462function forum_user_outline($course, $user, $mod, $forum) {
463
501cdbd8 464 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
498178e4 465 FROM forum f, forum_discussions d, forum_posts p, user u
466 WHERE f.id = '$forum->id' AND d.forum = f.id AND p.discussion = d.id
467 AND p.user = '$user->id' AND p.user = u.id
501cdbd8 468 ORDER BY p.modified ASC")) {
469
ffe11640 470 $result->info = get_string("numposts", "forum", count($posts));
501cdbd8 471
472 $lastpost = array_pop($posts);
473 $result->time = $lastpost->modified;
474 return $result;
475 }
476 return NULL;
477}
478
479
480function forum_user_complete($course, $user, $mod, $forum) {
481 global $CFG;
482
501cdbd8 483 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
498178e4 484 FROM forum f, forum_discussions d, forum_posts p, user u
485 WHERE f.id = '$forum->id' AND d.forum = f.id AND p.discussion = d.id
486 AND p.user = '$user->id' AND p.user = u.id
487 ORDER BY p.modified ASC")) {
501cdbd8 488
489 foreach ($posts as $post) {
490 if ($post->parent) {
ffe11640 491 $footer = "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">".
492 get_string("parentofthispost", "forum")."</A>";
501cdbd8 493 } else {
494 $footer = "";
495 }
496
77db7e4c 497 forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false, $rate=false, $footer);
501cdbd8 498 }
499
500 } else {
ffe11640 501 echo "<P>".get_string("noposts", "forum")."</P>";
501cdbd8 502 }
503
504}
505
04eba58f 506
507function forum_add_instance($forum) {
508// Given an object containing all the necessary data,
509// (defined by the form in mod.html) this function
510// will create a new instance and return the id number
511// of the new instance.
512
513 global $CFG;
514
515 $forum->timemodified = time();
516
517 if (! $forum->id = insert_record("forum", $forum)) {
518 return false;
519 }
520
521 if ($forum->type == "single") { // Create related discussion.
522
523 $discussion->course = $forum->course;
524 $discussion->forum = $forum->id;
525 $discussion->name = $forum->name;
526 $discussion->intro = $forum->intro;
527 $discussion->assessed = $forum->assessed;
528
529 if (! forum_add_discussion($discussion)) {
530 error("Could not add the discussion for this forum");
531 }
532 }
533 add_to_log($forum->course, "forum", "add", "index.php?f=$forum->id", "$forum->id");
534
535 return $forum->id;
536}
537
538
539function forum_update_instance($forum) {
540// Given an object containing all the necessary data,
541// (defined by the form in mod.html) this function
542// will update an existing instance with new data.
543
544 $forum->timemodified = time();
545 $forum->id = $forum->instance;
546
547 if ($forum->type == "single") { // Update related discussion and post.
548 if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
549 if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
550 notify("Warning! There is more than one discussion in this forum - using the most recent");
551 $discussion = array_pop($discussions);
552 } else {
553 error("Could not find the discussion in this forum");
554 }
555 }
556 if (! $post = get_record("forum_posts", "id", $discussion->firstpost)) {
557 error("Could not find the first post in this forum discussion");
558 }
559
560 $post->subject = $forum->name;
561 $post->message = $forum->intro;
562 $post->modified = $forum->timemodified;
563
564 if (! update_record("forum_posts", $post)) {
565 error("Could not update the first post");
566 }
567
568 $discussion->name = $forum->name;
569
570 if (! update_record("forum_discussions", $discussion)) {
571 error("Could not update the discussion");
572 }
573 }
574
575 if (update_record("forum", $forum)) {
576 add_to_log($forum->course, "forum", "update", "index.php?f=$forum->id", "$forum->id");
577 return true;
578 } else {
579 return false;
580 }
581}
582
583
584function forum_delete_instance($id) {
585// Given an ID of an instance of this module,
586// this function will permanently delete the instance
587// and any data that depends on it.
588
589 if (! $forum = get_record("forum", "id", "$id")) {
590 return false;
591 }
592
593 $result = true;
594
595 if ($discussions = get_records("forum_discussions", "forum", $forum->id)) {
596 foreach ($discussions as $discussion) {
597 if (! forum_delete_discussion($discussion)) {
598 $result = false;
599 }
600 }
601 }
602
603 if (! delete_records("forum_subscriptions", "forum", "$forum->id")) {
604 $result = false;
605 }
606
607 if (! delete_records("forum", "id", "$forum->id")) {
608 $result = false;
609 }
610
611 return $result;
612}
613
614
501cdbd8 615function forum_cron () {
616// Function to be run periodically according to the moodle cron
617// Finds all posts that have yet to be mailed out, and mails them
618
619 global $CFG;
620
501cdbd8 621 $cutofftime = time() - $CFG->maxeditingtime;
622
623 if ($posts = get_records_sql("SELECT p.*, d.course FROM forum_posts p, forum_discussions d
624 WHERE p.mailed = '0' AND p.created < '$cutofftime' AND p.discussion = d.id")) {
625
626 $timenow = time();
627
628 foreach ($posts as $post) {
629
ffe11640 630 print_string("processingpost", "forum", $post->id);
631 echo " ... ";
501cdbd8 632
633 if (! $userfrom = get_record("user", "id", "$post->user")) {
634 echo "Could not find user $post->user\n";
635 continue;
636 }
637
501cdbd8 638 if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) {
639 echo "Could not find discussion $post->discussion\n";
640 continue;
641 }
642
643 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
644 echo "Could not find forum $discussion->forum\n";
645 continue;
646 }
647
ffe11640 648 if (! $course = get_record("course", "id", "$forum->course")) {
649 echo "Could not find course $forum->course\n";
650 continue;
651 }
501cdbd8 652
ffe11640 653 if ($users = get_course_users($course->id)) {
654 $strforums = get_string("forums", "forum");
501cdbd8 655
ffe11640 656 $mailcount=0;
501cdbd8 657 foreach ($users as $userto) {
ffe11640 658 $by->name = "$userfrom->firstname $userfrom->lastname";
659 $by->date = userdate($post->created, "", $userto->timezone);
660 $strbynameondate = get_string("bynameondate", "forum", $by);
661
662 if (forum_is_subscribed($userto->id, $forum->id)) {
663 $postsubject = "$course->shortname: $post->subject";
664 $posttext = "$course->shortname -> $strforums -> $forum->name";
665 if ($discussion->name == $forum->name) {
666 $posttext .= "\n";
667 } else {
668 $posttext .= " -> $discussion->name\n";
669 }
670 $posttext .= "---------------------------------------------------------------------\n";
671 $posttext .= "$post->subject\n";
672 $posttext .= $strbynameondate."\n";
673 $posttext .= "---------------------------------------------------------------------\n";
674 $posttext .= strip_tags($post->message);
675 $posttext .= "\n\n";
676 $posttext .= "---------------------------------------------------------------------\n";
677 $posttext .= get_string("postmailinfo", "forum", $course->shortname)."\n";
678 $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id";
679
680 if ($userto->mailformat == 1) { // HTML
681 $posthtml = "<P><FONT FACE=sans-serif>".
682 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> -> ".
683 "<A HREF=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</A> -> ".
684 "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name</A>";
685 if ($discussion->name == $forum->name) {
686 $posthtml .= "</FONT></P>";
687 } else {
688 $posthtml .= " -> <A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name</A></FONT></P>";
689 }
690 $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, true, false, false);
691 } else {
692 $posthtml = "";
693 }
694
695 if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) {
696 echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email)\n";
697 } else {
698 $mailcount++;
699 }
501cdbd8 700 }
701 }
ffe11640 702 echo "mailed to $mailcount users ...";
501cdbd8 703 }
704
705 if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) {
706 echo "Could not update the mailed field for id $post->id\n";
707 }
ffe11640 708 echo "\n";
501cdbd8 709 }
710 }
711
712 return true;
713}
714
715
716function forum_forcesubscribe($forumid, $value=1) {
717 return set_field("forum", "forcesubscribe", $value, "id", $forumid);
718}
719
720function forum_is_forcesubscribed($forumid) {
721 return get_field("forum", "forcesubscribe", "id", $forumid);
722}
723
724function forum_is_subscribed($userid, $forumid) {
725 if (forum_is_forcesubscribed($forumid)) {
726 return true;
727 }
728 return record_exists_sql("SELECT * FROM forum_subscriptions WHERE user='$userid' AND forum='$forumid'");
729}
730
731function forum_subscribe($userid, $forumid) {
732 global $db;
733
734 return $db->Execute("INSERT INTO forum_subscriptions SET user = '$userid', forum = '$forumid'");
735}
736
737function forum_unsubscribe($userid, $forumid) {
738 global $db;
739
740 return $db->Execute("DELETE FROM forum_subscriptions WHERE user = '$userid' AND forum = '$forumid'");
741}
742
743
11b0c469 744function forum_user_has_posted_discussion($forumid, $userid) {
745 if ($discussions = forum_get_discussions($forumid, "DESC", $userid)) {
501cdbd8 746 return true;
747 } else {
748 return false;
749 }
750}
751
11b0c469 752function forum_user_can_post_discussion($forum) {
501cdbd8 753// $forum is an object
754 global $USER;
755
756 if ($forum->type == "eachuser") {
11b0c469 757 return (! forum_user_has_posted_discussion($forum->id, $USER->id));
501cdbd8 758 } else if ($forum->type == "teacher") {
759 return isteacher($forum->course);
760 } else if (isteacher($forum->course)) {
761 return true;
762 } else {
763 return $forum->open;
764 }
765}
766
767
11b0c469 768function forum_get_discussions($forum="0", $forum_sort="DESC", $user=0) {
501cdbd8 769 if ($user) {
770 $userselect = " AND u.id = '$user' ";
771 } else {
772 $userselect = "";
773 }
774 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.id as userid
775 FROM forum_discussions d, forum_posts p, user u
776 WHERE d.forum = '$forum' AND p.discussion = d.id AND
777 p.parent= 0 AND p.user = u.id $userselect
778 ORDER BY p.created $forum_sort");
779}
780
781
782
11b0c469 783function forum_print_latest_discussions($forum_id=0, $forum_numdiscussions=5, $forum_style="plain", $forum_sort="DESC") {
f93f848a 784 global $CFG, $USER;
785
786 if ($forum_id) {
787 if (! $forum = get_record("forum", "id", $forum_id)) {
788 error("Forum ID was incorrect");
789 }
790 if (! $course = get_record("course", "id", $forum->course)) {
791 error("Could not find the course this forum belongs to!");
792 }
793
794 if ($course->category) {
795 require_login($course->id);
796 }
797
798 } else {
799 if (! $course = get_record("course", "category", 0)) {
800 error("Could not find a top-level course!");
801 }
11b0c469 802 if (! $forum = forum_get_course_news_forum($course->id)) {
f93f848a 803 error("Could not find or create a main forum in this course (id $course->id)");
804 }
805 }
806
11b0c469 807 if (forum_user_can_post_discussion($forum)) {
3b9af3dd 808 echo "<P ALIGN=CENTER>";
ffe11640 809 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?forum=$forum->id\">";
810 echo get_string("addanewdiscussion", "forum")."</A>...";
501cdbd8 811 echo "</P>\n";
77305fe6 812 }
813
11b0c469 814 if (! $discussions = forum_get_discussions($forum->id, $forum_sort) ) {
ffe11640 815 echo "<P ALIGN=CENTER><B>(".get_string("nodiscussions", "forum").")</B></P>";
f93f848a 816
817 } else {
818
11b0c469 819 $replies = forum_count_discussion_replies($forum->id);
f93f848a 820
11b0c469 821 $discussioncount = 0;
f93f848a 822
11b0c469 823 foreach ($discussions as $discussion) {
824 $discussioncount++;
f93f848a 825
11b0c469 826 if ($forum_numdiscussions && ($discussioncount > $forum_numdiscussions)) {
ffe11640 827 echo "<P ALIGN=right><A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">";
828 echo get_string("olderdiscussions", "forum")."</A> ...</P>";
f93f848a 829 break;
830 }
11b0c469 831 if ($replies[$discussion->discussion]) {
832 $discussion->replies = $replies[$discussion->discussion]->replies;
f93f848a 833 } else {
11b0c469 834 $discussion->replies = 0;
f93f848a 835 }
11b0c469 836 $ownpost = ($discussion->userid == $USER->id);
f93f848a 837 switch ($forum_style) {
838 case "minimal":
11b0c469 839 echo "<P><FONT COLOR=#555555>".userdate($discussion->modified, "%e %b, %H:%M")." - $discussion->firstname</FONT>";
840 echo "<BR>$discussion->subject ";
ffe11640 841 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->discussion\">";
842 echo get_string("more", "forum")."...</A>";
f93f848a 843 echo "</P>\n";
844 break;
845 default:
11b0c469 846 forum_print_post($discussion, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
f93f848a 847 echo "<BR>\n";
848 break;
849 }
850 }
851 }
f93f848a 852}
853
11b0c469 854function forum_print_discussion($course, $discussion, $post, $mode) {
501cdbd8 855
856 global $USER;
857
858 $ownpost = ($USER->id == $post->user);
859
11b0c469 860 forum_print_post($post, $course->id, $ownpost, $reply=true, $link=false, $rate=false);
501cdbd8 861
7a12aab4 862 forum_print_mode_form($discussion->id, $mode);
501cdbd8 863
864 if ($discussion->assessed && $USER->id) {
865 echo "<FORM NAME=form METHOD=POST ACTION=rate.php>";
866 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
867 }
868
869 switch ($mode) {
870 case 1 : // Flat ascending
871 case -1 : // Flat descending
872 default:
873 echo "<UL>";
11b0c469 874 forum_print_posts_flat($post->discussion, $course->id, $mode, $discussion->assessed);
501cdbd8 875 echo "</UL>";
876 break;
877
878 case 2 : // Threaded
11b0c469 879 forum_print_posts_threaded($post->id, $course->id, 0, $discussion->assessed);
501cdbd8 880 break;
881
882 case 3 : // Nested
11b0c469 883 forum_print_posts_nested($post->id, $course->id, $discussion->assessed);
501cdbd8 884 break;
885 }
886
887 if ($discussion->assessed && $USER->id) {
8c3c8481 888 echo "<CENTER><P ALIGN=center><INPUT TYPE=submit VALUE=\"".get_string("sendinratings", "forum")."\"></P></CENTER>";
501cdbd8 889 echo "</FORM>";
890 }
891}
892
11b0c469 893function forum_print_posts_flat($discussion, $course, $direction, $assessed) {
501cdbd8 894 global $USER;
895
896 $reply = true;
897 $link = false;
898
899 if ($direction < 0) {
900 $sort = "ORDER BY created DESC";
901 } else {
902 $sort = "ORDER BY created ASC";
903 }
904
905 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
906 FROM forum_posts p, user u
907 WHERE p.discussion = $discussion AND p.parent > 0 AND p.user = u.id $sort")) {
908
909 foreach ($posts as $post) {
910 $ownpost = ($USER->id == $post->user);
11b0c469 911 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
501cdbd8 912 }
913 } else {
914 return;
915 }
916}
917
11b0c469 918function forum_print_posts_threaded($parent, $course, $depth, $assessed) {
501cdbd8 919 global $USER;
920
921 $reply = true;
922 $link = false;
923
924 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
925 FROM forum_posts p, user u
926 WHERE p.parent = '$parent' AND p.user = u.id")) {
927
928 foreach ($posts as $post) {
929
930 echo "<UL>";
931 if ($depth > 0) {
932 $ownpost = ($USER->id == $post->user);
11b0c469 933 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed); // link=true?
501cdbd8 934 echo "<BR>";
935 } else {
8c3c8481 936 $by->name = "$post->firstname $post->lastname";
937 $by->date = userdate($post->created);
938 echo "<LI><P><FONT SIZE=-1><B><A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</A></B> ";
939 print_string("bynameondate", "forum", $by);
940 echo "</FONT></P></LI>";
501cdbd8 941 }
942
11b0c469 943 forum_print_posts_threaded($post->id, $course, $depth-1, $assessed);
501cdbd8 944 echo "</UL>\n";
945 }
946 } else {
947 return;
948 }
949}
950
11b0c469 951function forum_print_posts_nested($parent, $course, $assessed) {
501cdbd8 952 global $USER;
953
954 $reply = true;
955 $link = false;
956
957 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
958 FROM forum_posts p, user u
959 WHERE p.parent = $parent AND p.user = u.id
960 ORDER BY p.created ASC ")) {
961
962 foreach ($posts as $post) {
963
964 $ownpost = ($USER->id == $post->user);
965
966 echo "<UL>";
11b0c469 967 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
501cdbd8 968 echo "<BR>";
11b0c469 969 forum_print_posts_nested($post->id, $course, $assessed);
501cdbd8 970 echo "</UL>\n";
971 }
972 } else {
973 return;
974 }
975}
976
977function forum_set_display_mode($mode=0) {
978 global $USER;
979
980 if ($mode) {
981 $USER->mode = $mode;
982 } else if (!$USER->mode) {
983 $USER->mode = $FORUM_DEFAULT_DISPLAY_MODE;
984 }
985}
f93f848a 986
987?>