Just a couple of changes
[moodle.git] / mod / forum / lib.php
CommitLineData
f93f848a 1<?PHP // $Id$
2
501cdbd8 3/// CONSTANTS ///////////////////////////////////////////////////////////
f93f848a 4
501cdbd8 5$FORUM_DEFAULT_DISPLAY_MODE = 3;
f93f848a 6
ffe11640 7$FORUM_LAYOUT_MODES = array ( "1" => get_string("modeflatoldestfirst", "forum"),
8 "-1" => get_string("modeflatnewestfirst", "forum"),
9 "2" => get_string("modethreaded", "forum"),
10 "3" => get_string("modenested", "forum") );
f93f848a 11
11b0c469 12// These are course content forums that can be added to the course manually
ffe11640 13$FORUM_TYPES = array ("general" => get_string("generalforum", "forum"),
14 "eachuser" => get_string("eachuserforum", "forum"),
15 "single" => get_string("singleforum", "forum") );
f93f848a 16
ffe11640 17$FORUM_POST_RATINGS = array ("3" => get_string("postrating3", "forum"),
18 "2" => get_string("postrating2", "forum"),
19 "1" => get_string("postrating1", "forum") );
f93f848a 20
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
503function forum_cron () {
504// Function to be run periodically according to the moodle cron
505// Finds all posts that have yet to be mailed out, and mails them
506
507 global $CFG;
508
501cdbd8 509 $cutofftime = time() - $CFG->maxeditingtime;
510
511 if ($posts = get_records_sql("SELECT p.*, d.course FROM forum_posts p, forum_discussions d
512 WHERE p.mailed = '0' AND p.created < '$cutofftime' AND p.discussion = d.id")) {
513
514 $timenow = time();
515
516 foreach ($posts as $post) {
517
ffe11640 518 print_string("processingpost", "forum", $post->id);
519 echo " ... ";
501cdbd8 520
521 if (! $userfrom = get_record("user", "id", "$post->user")) {
522 echo "Could not find user $post->user\n";
523 continue;
524 }
525
501cdbd8 526 if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) {
527 echo "Could not find discussion $post->discussion\n";
528 continue;
529 }
530
531 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
532 echo "Could not find forum $discussion->forum\n";
533 continue;
534 }
535
ffe11640 536 if (! $course = get_record("course", "id", "$forum->course")) {
537 echo "Could not find course $forum->course\n";
538 continue;
539 }
501cdbd8 540
ffe11640 541 if ($users = get_course_users($course->id)) {
542 $strforums = get_string("forums", "forum");
501cdbd8 543
ffe11640 544 $mailcount=0;
501cdbd8 545 foreach ($users as $userto) {
ffe11640 546 $by->name = "$userfrom->firstname $userfrom->lastname";
547 $by->date = userdate($post->created, "", $userto->timezone);
548 $strbynameondate = get_string("bynameondate", "forum", $by);
549
550 if (forum_is_subscribed($userto->id, $forum->id)) {
551 $postsubject = "$course->shortname: $post->subject";
552 $posttext = "$course->shortname -> $strforums -> $forum->name";
553 if ($discussion->name == $forum->name) {
554 $posttext .= "\n";
555 } else {
556 $posttext .= " -> $discussion->name\n";
557 }
558 $posttext .= "---------------------------------------------------------------------\n";
559 $posttext .= "$post->subject\n";
560 $posttext .= $strbynameondate."\n";
561 $posttext .= "---------------------------------------------------------------------\n";
562 $posttext .= strip_tags($post->message);
563 $posttext .= "\n\n";
564 $posttext .= "---------------------------------------------------------------------\n";
565 $posttext .= get_string("postmailinfo", "forum", $course->shortname)."\n";
566 $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id";
567
568 if ($userto->mailformat == 1) { // HTML
569 $posthtml = "<P><FONT FACE=sans-serif>".
570 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> -> ".
571 "<A HREF=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</A> -> ".
572 "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name</A>";
573 if ($discussion->name == $forum->name) {
574 $posthtml .= "</FONT></P>";
575 } else {
576 $posthtml .= " -> <A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name</A></FONT></P>";
577 }
578 $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, true, false, false);
579 } else {
580 $posthtml = "";
581 }
582
583 if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) {
584 echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email)\n";
585 } else {
586 $mailcount++;
587 }
501cdbd8 588 }
589 }
ffe11640 590 echo "mailed to $mailcount users ...";
501cdbd8 591 }
592
593 if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) {
594 echo "Could not update the mailed field for id $post->id\n";
595 }
ffe11640 596 echo "\n";
501cdbd8 597 }
598 }
599
600 return true;
601}
602
603
604function forum_forcesubscribe($forumid, $value=1) {
605 return set_field("forum", "forcesubscribe", $value, "id", $forumid);
606}
607
608function forum_is_forcesubscribed($forumid) {
609 return get_field("forum", "forcesubscribe", "id", $forumid);
610}
611
612function forum_is_subscribed($userid, $forumid) {
613 if (forum_is_forcesubscribed($forumid)) {
614 return true;
615 }
616 return record_exists_sql("SELECT * FROM forum_subscriptions WHERE user='$userid' AND forum='$forumid'");
617}
618
619function forum_subscribe($userid, $forumid) {
620 global $db;
621
622 return $db->Execute("INSERT INTO forum_subscriptions SET user = '$userid', forum = '$forumid'");
623}
624
625function forum_unsubscribe($userid, $forumid) {
626 global $db;
627
628 return $db->Execute("DELETE FROM forum_subscriptions WHERE user = '$userid' AND forum = '$forumid'");
629}
630
631
11b0c469 632function forum_user_has_posted_discussion($forumid, $userid) {
633 if ($discussions = forum_get_discussions($forumid, "DESC", $userid)) {
501cdbd8 634 return true;
635 } else {
636 return false;
637 }
638}
639
11b0c469 640function forum_user_can_post_discussion($forum) {
501cdbd8 641// $forum is an object
642 global $USER;
643
644 if ($forum->type == "eachuser") {
11b0c469 645 return (! forum_user_has_posted_discussion($forum->id, $USER->id));
501cdbd8 646 } else if ($forum->type == "teacher") {
647 return isteacher($forum->course);
648 } else if (isteacher($forum->course)) {
649 return true;
650 } else {
651 return $forum->open;
652 }
653}
654
655
11b0c469 656function forum_get_discussions($forum="0", $forum_sort="DESC", $user=0) {
501cdbd8 657 if ($user) {
658 $userselect = " AND u.id = '$user' ";
659 } else {
660 $userselect = "";
661 }
662 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.id as userid
663 FROM forum_discussions d, forum_posts p, user u
664 WHERE d.forum = '$forum' AND p.discussion = d.id AND
665 p.parent= 0 AND p.user = u.id $userselect
666 ORDER BY p.created $forum_sort");
667}
668
669
670
11b0c469 671function forum_print_latest_discussions($forum_id=0, $forum_numdiscussions=5, $forum_style="plain", $forum_sort="DESC") {
f93f848a 672 global $CFG, $USER;
673
674 if ($forum_id) {
675 if (! $forum = get_record("forum", "id", $forum_id)) {
676 error("Forum ID was incorrect");
677 }
678 if (! $course = get_record("course", "id", $forum->course)) {
679 error("Could not find the course this forum belongs to!");
680 }
681
682 if ($course->category) {
683 require_login($course->id);
684 }
685
686 } else {
687 if (! $course = get_record("course", "category", 0)) {
688 error("Could not find a top-level course!");
689 }
11b0c469 690 if (! $forum = forum_get_course_news_forum($course->id)) {
f93f848a 691 error("Could not find or create a main forum in this course (id $course->id)");
692 }
693 }
694
11b0c469 695 if (forum_user_can_post_discussion($forum)) {
3b9af3dd 696 echo "<P ALIGN=CENTER>";
ffe11640 697 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?forum=$forum->id\">";
698 echo get_string("addanewdiscussion", "forum")."</A>...";
501cdbd8 699 echo "</P>\n";
77305fe6 700 }
701
11b0c469 702 if (! $discussions = forum_get_discussions($forum->id, $forum_sort) ) {
ffe11640 703 echo "<P ALIGN=CENTER><B>(".get_string("nodiscussions", "forum").")</B></P>";
f93f848a 704
705 } else {
706
11b0c469 707 $replies = forum_count_discussion_replies($forum->id);
f93f848a 708
11b0c469 709 $discussioncount = 0;
f93f848a 710
11b0c469 711 foreach ($discussions as $discussion) {
712 $discussioncount++;
f93f848a 713
11b0c469 714 if ($forum_numdiscussions && ($discussioncount > $forum_numdiscussions)) {
ffe11640 715 echo "<P ALIGN=right><A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">";
716 echo get_string("olderdiscussions", "forum")."</A> ...</P>";
f93f848a 717 break;
718 }
11b0c469 719 if ($replies[$discussion->discussion]) {
720 $discussion->replies = $replies[$discussion->discussion]->replies;
f93f848a 721 } else {
11b0c469 722 $discussion->replies = 0;
f93f848a 723 }
11b0c469 724 $ownpost = ($discussion->userid == $USER->id);
f93f848a 725 switch ($forum_style) {
726 case "minimal":
11b0c469 727 echo "<P><FONT COLOR=#555555>".userdate($discussion->modified, "%e %b, %H:%M")." - $discussion->firstname</FONT>";
728 echo "<BR>$discussion->subject ";
ffe11640 729 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->discussion\">";
730 echo get_string("more", "forum")."...</A>";
f93f848a 731 echo "</P>\n";
732 break;
733 default:
11b0c469 734 forum_print_post($discussion, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
f93f848a 735 echo "<BR>\n";
736 break;
737 }
738 }
739 }
f93f848a 740}
741
11b0c469 742function forum_print_discussion($course, $discussion, $post, $mode) {
501cdbd8 743
744 global $USER;
745
746 $ownpost = ($USER->id == $post->user);
747
11b0c469 748 forum_print_post($post, $course->id, $ownpost, $reply=true, $link=false, $rate=false);
501cdbd8 749
7a12aab4 750 forum_print_mode_form($discussion->id, $mode);
501cdbd8 751
752 if ($discussion->assessed && $USER->id) {
753 echo "<FORM NAME=form METHOD=POST ACTION=rate.php>";
754 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
755 }
756
757 switch ($mode) {
758 case 1 : // Flat ascending
759 case -1 : // Flat descending
760 default:
761 echo "<UL>";
11b0c469 762 forum_print_posts_flat($post->discussion, $course->id, $mode, $discussion->assessed);
501cdbd8 763 echo "</UL>";
764 break;
765
766 case 2 : // Threaded
11b0c469 767 forum_print_posts_threaded($post->id, $course->id, 0, $discussion->assessed);
501cdbd8 768 break;
769
770 case 3 : // Nested
11b0c469 771 forum_print_posts_nested($post->id, $course->id, $discussion->assessed);
501cdbd8 772 break;
773 }
774
775 if ($discussion->assessed && $USER->id) {
8c3c8481 776 echo "<CENTER><P ALIGN=center><INPUT TYPE=submit VALUE=\"".get_string("sendinratings", "forum")."\"></P></CENTER>";
501cdbd8 777 echo "</FORM>";
778 }
779}
780
11b0c469 781function forum_print_posts_flat($discussion, $course, $direction, $assessed) {
501cdbd8 782 global $USER;
783
784 $reply = true;
785 $link = false;
786
787 if ($direction < 0) {
788 $sort = "ORDER BY created DESC";
789 } else {
790 $sort = "ORDER BY created ASC";
791 }
792
793 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
794 FROM forum_posts p, user u
795 WHERE p.discussion = $discussion AND p.parent > 0 AND p.user = u.id $sort")) {
796
797 foreach ($posts as $post) {
798 $ownpost = ($USER->id == $post->user);
11b0c469 799 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
501cdbd8 800 }
801 } else {
802 return;
803 }
804}
805
11b0c469 806function forum_print_posts_threaded($parent, $course, $depth, $assessed) {
501cdbd8 807 global $USER;
808
809 $reply = true;
810 $link = false;
811
812 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
813 FROM forum_posts p, user u
814 WHERE p.parent = '$parent' AND p.user = u.id")) {
815
816 foreach ($posts as $post) {
817
818 echo "<UL>";
819 if ($depth > 0) {
820 $ownpost = ($USER->id == $post->user);
11b0c469 821 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed); // link=true?
501cdbd8 822 echo "<BR>";
823 } else {
8c3c8481 824 $by->name = "$post->firstname $post->lastname";
825 $by->date = userdate($post->created);
826 echo "<LI><P><FONT SIZE=-1><B><A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</A></B> ";
827 print_string("bynameondate", "forum", $by);
828 echo "</FONT></P></LI>";
501cdbd8 829 }
830
11b0c469 831 forum_print_posts_threaded($post->id, $course, $depth-1, $assessed);
501cdbd8 832 echo "</UL>\n";
833 }
834 } else {
835 return;
836 }
837}
838
11b0c469 839function forum_print_posts_nested($parent, $course, $assessed) {
501cdbd8 840 global $USER;
841
842 $reply = true;
843 $link = false;
844
845 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
846 FROM forum_posts p, user u
847 WHERE p.parent = $parent AND p.user = u.id
848 ORDER BY p.created ASC ")) {
849
850 foreach ($posts as $post) {
851
852 $ownpost = ($USER->id == $post->user);
853
854 echo "<UL>";
11b0c469 855 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
501cdbd8 856 echo "<BR>";
11b0c469 857 forum_print_posts_nested($post->id, $course, $assessed);
501cdbd8 858 echo "</UL>\n";
859 }
860 } else {
861 return;
862 }
863}
864
865function forum_set_display_mode($mode=0) {
866 global $USER;
867
868 if ($mode) {
869 $USER->mode = $mode;
870 } else if (!$USER->mode) {
871 $USER->mode = $FORUM_DEFAULT_DISPLAY_MODE;
872 }
873}
f93f848a 874
875?>