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