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