User's name in footer is now a link
[moodle.git] / mod / forum / lib.php
CommitLineData
f93f848a 1<?PHP // $Id$
2
7f6689e4 3include_once("$CFG->dirroot/files/mimetypes.php");
4
501cdbd8 5/// CONSTANTS ///////////////////////////////////////////////////////////
f93f848a 6
501cdbd8 7$FORUM_DEFAULT_DISPLAY_MODE = 3;
f93f848a 8
ffe11640 9$FORUM_LAYOUT_MODES = array ( "1" => get_string("modeflatoldestfirst", "forum"),
10 "-1" => get_string("modeflatnewestfirst", "forum"),
11 "2" => get_string("modethreaded", "forum"),
12 "3" => get_string("modenested", "forum") );
f93f848a 13
11b0c469 14// These are course content forums that can be added to the course manually
ffe11640 15$FORUM_TYPES = array ("general" => get_string("generalforum", "forum"),
16 "eachuser" => get_string("eachuserforum", "forum"),
17 "single" => get_string("singleforum", "forum") );
f93f848a 18
ffe11640 19$FORUM_POST_RATINGS = array ("3" => get_string("postrating3", "forum"),
20 "2" => get_string("postrating2", "forum"),
21 "1" => get_string("postrating1", "forum") );
f93f848a 22
4d871a72 23$FORUM_SHORT_POST = 300; // Less than this is "short"
24
25$FORUM_LONG_POST = 600; // More than this is "long"
26
3335f6fb 27$FORUM_MANY_DISCUSSIONS = 10;
e07635f4 28
29
501cdbd8 30/// FUNCTIONS ///////////////////////////////////////////////////////////
f93f848a 31
32
11b0c469 33function forum_get_course_forum($courseid, $type) {
34// How to set up special 1-per-course forums
35 if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = '$type'")) {
f93f848a 36 return $forum;
ffe11640 37
f93f848a 38 } else {
39 // Doesn't exist, so create one now.
40 $forum->course = $courseid;
11b0c469 41 $forum->type = "$type";
42 switch ($forum->type) {
43 case "news":
ffe11640 44 $forum->name = get_string("namenews", "forum");
45 $forum->intro = get_string("intronews", "forum");
46 $forum->open = 0;
11b0c469 47 $forum->assessed = 0;
48 $forum->forcesubscribe = 1;
49 break;
50 case "social":
ffe11640 51 $forum->name = get_string("namesocial", "forum");
52 $forum->intro = get_string("introsocial", "forum");
53 $forum->open = 1;
11b0c469 54 $forum->assessed = 0;
55 $forum->forcesubscribe = 0;
56 break;
57 case "teacher":
ffe11640 58 $forum->name = get_string("nameteacher", "forum");
59 $forum->intro = get_string("introteacher", "forum");
60 $forum->open = 0;
11b0c469 61 $forum->assessed = 0;
62 $forum->forcesubscribe = 0;
63 break;
64 default:
65 notify("That forum type doesn't exist!");
66 return false;
67 break;
f93f848a 68
11b0c469 69 }
82aa0e8d 70 $forum->timemodified = time();
71 $forum->id = insert_record("forum", $forum);
72 return get_record_sql("SELECT * from forum WHERE id = '$forum->id'");
73 }
74}
75
f93f848a 76
11b0c469 77function forum_make_mail_post(&$post, $user, $touser, $course,
78 $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
501cdbd8 79// Given the data about a posting, builds up the HTML to display it and
80// returns the HTML in a string. This is designed for sending via HTML email.
81
82 global $THEME, $CFG;
83
84 $output = "";
85
86 if ($post->parent) {
87 $output .= "<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1><TR><TD BGCOLOR=#888888>";
88 $output .= "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0>";
89 } else {
90 $output .= "<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1 WIDTH=100%><TR><TD BGCOLOR=#888888>";
91 $output .= "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 WIDTH=100%>";
92 }
93
94 $output .= "<TR><TD BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
95 $output .= print_user_picture($user->id, $course->id, $user->picture, false, true);
96 $output .= "</TD>";
97
98 if ($post->parent) {
99 $output .= "<TD NOWRAP BGCOLOR=\"$THEME->cellheading\">";
100 } else {
101 $output .= "<TD NOWRAP BGCOLOR=\"$THEME->cellheading2\">";
102 }
103 $output .= "<P>";
104 $output .= "<FONT SIZE=3><B>$post->subject</B></FONT><BR>";
ffe11640 105 $output .= "<FONT SIZE=2>";
106 $by->name = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A>";
107 $by->date = userdate($post->created, "", $touser->timezone);
108 $output .= get_string("bynameondate", "forum", $by);
501cdbd8 109 $output .= "</FONT></P></TD></TR>";
110 $output .= "<TR><TD BGCOLOR=\"$THEME->body\" WIDTH=10>";
111 $output .= "&nbsp;";
86970225 112 $output .= "</TD><TD BGCOLOR=\"$THEME->cellcontent\">\n";
501cdbd8 113
7f6689e4 114 if ($post->attachment) {
115 $post->course = $course->id;
116 $post->forum = get_field("forum_discussions", "forum", "id", $post->discussion);
117 $output .= "<DIV ALIGN=right>";
118 $output .= forum_print_attachments($post, "html");
119 $output .= "</DIV>";
120 }
121
501cdbd8 122 $output .= text_to_html($post->message);
123
124 $output .= "<P ALIGN=right><FONT SIZE=-1>";
125
126 $age = time() - $post->created;
127 if ($ownpost) {
ffe11640 128 $output .= "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."</A>";
501cdbd8 129 if ($reply) {
cf38360f 130 $output .= " | <A HREF=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</A>";
501cdbd8 131 }
132 $output .= "&nbsp;&nbsp;";
133 } else {
134 if ($reply) {
ffe11640 135 $output .= "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</A>&nbsp;&nbsp;";
501cdbd8 136 }
137 }
138
139 $output .= "<DIV ALIGN=right><P ALIGN=right>";
140
141 if ($link) {
142 if ($post->replies == 1) {
ffe11640 143 $replystring = get_string("repliesone", "forum", $post->replies);
501cdbd8 144 } else {
ffe11640 145 $replystring = get_string("repliesmany", "forum", $post->replies);
501cdbd8 146 }
ffe11640 147 $output .= "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><B>".get_string("discussthistopic", "forum")."</B></A> ($replystring)&nbsp;&nbsp;";
501cdbd8 148 }
149 $output .= "</P></DIV>";
150 if ($footer) {
151 $output .= "<P>$footer</P>";
152 }
153 $output .= "</TD></TR></TABLE>\n";
154 $output .= "</TD></TR></TABLE>\n\n";
155
156 return $output;
157}
158
159
11b0c469 160function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
6bebaa64 161 global $THEME, $USER, $CFG, $FORUM_LONG_POST;
501cdbd8 162
163 if ($post->parent) {
164 echo "<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1><TR><TD BGCOLOR=#888888>";
165 echo "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0>";
166 } else {
167 echo "<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1 WIDTH=100%><TR><TD BGCOLOR=#888888>";
168 echo "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 WIDTH=100%>";
169 }
170
171 echo "<TR><TD BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
172 print_user_picture($post->userid, $courseid, $post->picture);
173 echo "</TD>";
174
175 if ($post->parent) {
19a55d67 176 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading\" WIDTH=\"100%\">";
501cdbd8 177 } else {
19a55d67 178 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading2\" WIDTH=\"100%*\">";
501cdbd8 179 }
180 echo "<P>";
181 echo "<FONT SIZE=3><B>$post->subject</B></FONT><BR>";
ffe11640 182 echo "<FONT SIZE=2>";
183 $by->name = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$post->firstname $post->lastname</A>";
184 $by->date = userdate($post->created);
185 print_string("bynameondate", "forum", $by);
501cdbd8 186 echo "</FONT></P></TD></TR>";
187 echo "<TR><TD BGCOLOR=\"$THEME->body\" WIDTH=10>";
188 echo "&nbsp;";
86970225 189 echo "</TD><TD BGCOLOR=\"$THEME->cellcontent\">\n";
501cdbd8 190
7f6689e4 191 if ($post->attachment) {
192 $post->course = $courseid;
193 $post->forum = get_field("forum_discussions", "forum", "id", $post->discussion);
194 echo "<DIV ALIGN=right>";
195 forum_print_attachments($post);
196 echo "</DIV>";
197 }
198
501cdbd8 199 if ($link && (strlen($post->message) > $FORUM_LONG_POST)) {
aa153f29 200 // Print shortened version
201 echo text_to_html(forum_shorten_post($post->message));
501cdbd8 202 $numwords = count_words($post->message);
aa153f29 203 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">";
ffe11640 204 echo get_string("readtherest", "forum");
3db1919b 205 echo "</A> (".get_string("numwords", "", $numwords).")...";
501cdbd8 206 } else {
aa153f29 207 // Print whole message
501cdbd8 208 echo text_to_html($post->message);
209 }
210
211 echo "<P ALIGN=right><FONT SIZE=-1>";
212
213 $age = time() - $post->created;
214 if ($ownpost) {
215 if ($age < $CFG->maxeditingtime) {
ffe11640 216 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?edit=$post->id\">".get_string("edit", "forum")."</A> | ";
501cdbd8 217 }
64eacd6f 218 }
219 if ($ownpost or isteacher($courseid)) {
ffe11640 220 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."</A>";
501cdbd8 221 if ($reply) {
64eacd6f 222 echo "| ";
223 } else {
224 echo "&nbsp;&nbsp;";
501cdbd8 225 }
64eacd6f 226 }
227 if ($reply) {
228 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</A>";
501cdbd8 229 echo "&nbsp;&nbsp;";
501cdbd8 230 }
231
232
233 echo "<DIV ALIGN=right><P ALIGN=right>";
234 if ($rate && $USER->id) {
235 if ($USER->id == $post->userid) {
7a12aab4 236 forum_print_ratings($post->id);
501cdbd8 237 } else {
7a12aab4 238 forum_print_rating($post->id, $USER->id);
501cdbd8 239 }
240 }
241
242 if ($link) {
243 if ($post->replies == 1) {
ffe11640 244 $replystring = get_string("repliesone", "forum", $post->replies);
501cdbd8 245 } else {
ffe11640 246 $replystring = get_string("repliesmany", "forum", $post->replies);
501cdbd8 247 }
ffe11640 248 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><B>".get_string("discussthistopic", "forum")."</B></A> ($replystring)&nbsp;&nbsp;";
501cdbd8 249 }
250 echo "</P>";
251 if ($footer) {
252 echo "<P>$footer</P>";
253 }
254 echo "</DIV>";
255 echo "</TD></TR></TABLE>";
256 echo "</TD></TR>\n</TABLE>\n\n";
257}
258
3335f6fb 259
260function forum_print_post_header(&$post, $courseid, $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
261 global $THEME, $USER, $CFG, $FORUM_LONG_POST;
262
263 if ($post->parent) {
264 echo "<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1><TR><TD BGCOLOR=#888888>";
265 echo "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0>";
266 } else {
267 echo "<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1 WIDTH=100%><TR><TD BGCOLOR=#888888>";
268 echo "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 WIDTH=100%>";
269 }
270
271 echo "<TR><TD BGCOLOR=\"$THEME->body\" WIDTH=35 VALIGN=TOP>";
272 print_user_picture($post->userid, $courseid, $post->picture);
273 echo "</TD>";
274
275 if ($post->parent) {
276 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading\">";
277 } else {
278 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading2\">";
279 }
280 echo "<P>";
281 echo "<FONT SIZE=3><B>$post->subject</B></FONT><BR>";
282 echo "<FONT SIZE=2>";
283 $by->name = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$post->firstname $post->lastname</A>";
284 $by->date = userdate($post->created);
285 print_string("bynameondate", "forum", $by);
286 echo "</FONT></P></TD>";
287
288 if ($post->parent) {
289 echo "<TD VALIGN=BOTTOM BGCOLOR=\"$THEME->cellheading\">";
290 } else {
291 echo "<TD VALIGN=BOTTOM BGCOLOR=\"$THEME->cellheading2\">";
292 }
293 echo "<P ALIGN=right><FONT SIZE=-1>";
294
295 if ($link) {
296 if ($post->replies == 1) {
297 $replystring = get_string("repliesone", "forum", $post->replies);
298 } else {
299 $replystring = get_string("repliesmany", "forum", $post->replies);
300 }
301 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><B>".get_string("discussthistopic", "forum")."</B></A> ($replystring)&nbsp;&nbsp;";
302 }
303 echo "</P>";
304 echo "</TD></TR></TABLE>";
305 echo "</TD></TR>\n</TABLE>\n\n";
306}
307
308
aa153f29 309function forum_shorten_post($message) {
4d871a72 310 global $FORUM_LONG_POST, $FORUM_SHORT_POST;
aa153f29 311
312 if (strlen($message) > $FORUM_LONG_POST) {
4d871a72 313 // Look for the first return between $FORUM_SHORT_POST and $FORUM_LONG_POST
314 $shortmessage = substr($message, $FORUM_SHORT_POST, $FORUM_LONG_POST);
aa153f29 315 if ($pos = strpos($shortmessage, "\n")) {
4d871a72 316 return substr($message, 0, $FORUM_SHORT_POST + $pos);
aa153f29 317 } else {
ffe11640 318 return substr($message, 0, $FORUM_LONG_POST)."...";
aa153f29 319 }
320 } else {
321 return $message;
322 }
323}
324
501cdbd8 325
7a12aab4 326function forum_print_ratings($post) {
501cdbd8 327 if ($ratings = get_records_sql("SELECT * from forum_ratings WHERE post='$post'")) {
ffe11640 328 $sumrating[1] = 0;
329 $sumrating[2] = 0;
330 $sumrating[3] = 0;
501cdbd8 331 foreach ($ratings as $rating) {
ffe11640 332 $sumrating[$rating->rating]++;
501cdbd8 333 }
ffe11640 334 $summary = $sumrating[1]."s/".$sumrating[2]."/".$sumrating[3]."c";
501cdbd8 335
ffe11640 336 echo get_string("ratings", "forum").": ";
501cdbd8 337 link_to_popup_window ("/mod/forum/report.php?id=$post", "ratings", $summary, 400, 550);
501cdbd8 338 }
339}
340
7a12aab4 341function forum_print_rating($post, $user) {
501cdbd8 342 global $FORUM_POST_RATINGS;
343
344 if ($rs = get_record_sql("SELECT rating from forum_ratings WHERE user='$user' AND post='$post'")) {
501cdbd8 345 if ($FORUM_POST_RATINGS[$rs->rating]) {
ffe11640 346 echo "<FONT SIZE=-1>".get_string("youratedthis", "forum").": <FONT COLOR=green>";
501cdbd8 347 echo $FORUM_POST_RATINGS[$rs->rating];
ffe11640 348 echo "</FONT></FONT>";
349 return;
501cdbd8 350 }
501cdbd8 351 }
ffe11640 352 choose_from_menu($FORUM_POST_RATINGS, $post, "", get_string("rate", "forum")."...");
501cdbd8 353}
354
7a12aab4 355function forum_print_mode_form($discussion, $mode) {
356 GLOBAL $FORUM_LAYOUT_MODES;
501cdbd8 357
358 echo "<CENTER><P>";
7a12aab4 359 popup_form("discuss.php?d=$discussion&mode=", $FORUM_LAYOUT_MODES, "mode", $mode, "");
501cdbd8 360 echo "</P></CENTER>\n";
361}
362
7a12aab4 363function forum_print_search_form($course, $search="") {
501cdbd8 364 global $CFG;
365
366 echo "<TABLE BORDER=0 CELLPADDING=10 CELLSPACING=0><TR><TD ALIGN=CENTER>";
367 echo "<FORM NAME=search ACTION=\"$CFG->wwwroot/mod/forum/search.php\">";
368 echo "<INPUT NAME=search TYPE=text SIZE=15 VALUE=\"$search\"><BR>";
5d13db8a 369 echo "<INPUT VALUE=\"".get_string("searchforums", "forum")."\" TYPE=submit>";
501cdbd8 370 echo "<INPUT NAME=id TYPE=hidden VALUE=\"$course->id\">";
371 echo "</FORM>";
372 echo "</TD></TR></TABLE>";
373}
374
375
11b0c469 376function forum_count_discussion_replies($forum="0") {
9d1b97c5 377// Returns an array of counts of replies to each discussion (optionally in one forum)
501cdbd8 378 if ($forum) {
379 $forumselect = " AND d.forum = '$forum'";
380 }
381 return get_records_sql("SELECT p.discussion, (count(*)) as replies
382 FROM forum_posts p, forum_discussions d
383 WHERE p.parent > 0 AND p.discussion = d.id
384 GROUP BY p.discussion");
385}
386
9d1b97c5 387function forum_count_unrated_posts($discussionid, $userid) {
388// How many unrated posts are in the given discussion for a given user?
389 if ($posts = get_record_sql("SELECT count(*) as num
390 FROM forum_posts
391 WHERE parent > 0 AND
392 discussion = '$discussionid' AND
393 user <> '$userid' ")) {
394
395 if ($rated = get_record_sql("SELECT count(*) as num
396 FROM forum_posts p, forum_ratings r
1ebede32 397 WHERE p.discussion = '$discussionid'
398 AND p.id = r.post
399 AND r.user = '$userid'")) {
9d1b97c5 400 $difference = $posts->num - $rated->num;
401 if ($difference > 0) {
402 return $difference;
403 } else {
404 return 0; // Just in case there was a counting error
405 }
406 } else {
407 return $posts->num;
408 }
409 } else {
410 return 0;
411 }
412}
413
501cdbd8 414
11b0c469 415function forum_set_return() {
28e1e8b9 416 global $CFG, $SESSION, $HTTP_REFERER;
501cdbd8 417
28e1e8b9 418 if (! isset($SESSION->fromdiscussion)) {
419 // If the referer is NOT a login screen then save it.
420 if (! strncasecmp("$CFG->wwwroot/login", $HTTP_REFERER, 300)) {
421 $SESSION->fromdiscussion = $HTTP_REFERER;
422 save_session("SESSION");
423 }
501cdbd8 424 }
425}
426
427
11b0c469 428function forum_go_back_to($default) {
501cdbd8 429 global $SESSION;
430
431 if ($SESSION->fromdiscussion) {
432 $returnto = $SESSION->fromdiscussion;
433 unset($SESSION->fromdiscussion);
8223d271 434 save_session("SESSION");
501cdbd8 435 return $returnto;
436 } else {
437 return $default;
438 }
439}
440
11b0c469 441function forum_get_post_full($postid) {
501cdbd8 442 return get_record_sql("SELECT p.*, u.firstname, u.lastname,
443 u.email, u.picture, u.id as userid
444 FROM forum_posts p, user u
445 WHERE p.id = '$postid' AND p.user = u.id");
446}
447
7f6689e4 448function forum_file_area_name($post) {
449// Creates a directory file name, suitable for make_upload_directory()
450 global $CFG;
451
452 return "$post->course/$CFG->moddata/forum/$post->forum/$post->id";
453}
454
455function forum_file_area($post) {
456 return make_upload_directory( forum_file_area_name($post) );
457}
458
459function forum_delete_old_attachments($post, $exception="") {
460// Deletes all the user files in the attachments area for a post
461// EXCEPT for any file named $exception
462
463 if ($basedir = forum_file_area($post)) {
464 if ($files = get_directory_list($basedir)) {
465 foreach ($files as $file) {
466 if ($file != $exception) {
467 unlink("$basedir/$file");
468 notify("Existing file '$file' has been deleted!");
469 }
470 }
471 }
472 if (!$exception) { // Delete directory as well, if empty
473 rmdir("$basedir");
474 }
475 }
476}
477
478function forum_print_attachments($post, $return=NULL) {
479// if return=html, then return a html string.
480// if return=text, then return a text-only string.
481// otherwise, print HTML
482
483 global $CFG;
484
485 $filearea = forum_file_area_name($post);
486
487 if ($basedir = forum_file_area($post)) {
488 if ($files = get_directory_list($basedir)) {
489 $strattachment = get_string("attachment", "forum");
490 foreach ($files as $file) {
491 $icon = mimeinfo("icon", $file);
492 if ($CFG->slasharguments) {
493 $ffurl = "file.php/$filearea/$file";
494 } else {
495 $ffurl = "file.php?file=/$filearea/$file";
496 }
497 $image = "<IMG BORDER=0 SRC=\"$CFG->wwwroot/files/pix/$icon\" HEIGHT=16 WIDTH=16 ALT=\"File\">";
498
499 if ($return == "html") {
500 $output .= "<A HREF=\"$CFG->wwwroot/$ffurl\">$image</A> ";
501 $output .= "<A HREF=\"$CFG->wwwroot/$ffurl\">$file</A><BR>";
502
503 } else if ($return == "text") {
504 $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
505
506 } else {
507 link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
508 echo "<A HREF=\"$CFG->wwwroot/$ffurl\">$file</A>";
509 echo "<BR>";
510 }
511 }
512 }
513 }
514 if ($return) {
515 return $output;
516 }
517}
518
519function forum_add_attachment($post, $newfile) {
520// $post is a full post record, including course and forum
521// $newfile is a full upload array from HTTP_POST_FILES
522// If successful, this function returns the name of the file
523
524 if (!isset($newfile['name'])) {
525 return "";
526 }
527
528 $newfile_name = clean_filename($newfile['name']);
529
530 if (valid_uploaded_file($newfile)) {
531 if (! $newfile_name) {
532 notify("This file had a wierd filename and couldn't be uploaded");
533
534 } else if (! $dir = forum_file_area($post)) {
535 notify("Attachment could not be stored");
536 $newfile_name = "";
537
538 } else {
539 if (move_uploaded_file($newfile['tmp_name'], "$dir/$newfile_name")) {
540 forum_delete_old_attachments($post, $newfile_name);
541 } else {
542 notify("An error happened while saving the file on the server");
543 $newfile_name = "";
544 }
545 }
546 } else {
547 $newfile_name = "";
548 }
549
550 return $newfile_name;
551}
501cdbd8 552
11b0c469 553function forum_add_new_post($post) {
501cdbd8 554
ffe11640 555 $post->created = $post->modified = time();
501cdbd8 556 $post->mailed = "0";
557
7f6689e4 558 $newfile = $post->attachment;
559 $post->attachment = "";
560
561 if (! $post->id = insert_record("forum_posts", $post)) {
562 return false;
563 }
564
565 if ($post->attachment = forum_add_attachment($post, $newfile)) {
566 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id);
567 }
568
569 return $post->id;
501cdbd8 570}
571
11b0c469 572function forum_update_post($post) {
501cdbd8 573
ffe11640 574 $post->modified = time();
501cdbd8 575
0ab85112 576 if (!$post->parent) { // Post is a discussion starter - update discussion title too
577 set_field("forum_discussions", "name", $post->subject, "id", $post->discussion);
578 }
7f6689e4 579 if ($newfilename = forum_add_attachment($post, $post->attachment)) {
580 $post->attachment = $newfilename;
581 } else {
582 unset($post->attachment);
583 }
ffe11640 584 return update_record("forum_posts", $post);
501cdbd8 585}
586
587function forum_add_discussion($discussion) {
588// Given an object containing all the necessary data,
589// create a new discussion and return the id
590
591 GLOBAL $USER;
592
593 $timenow = time();
594
595 // The first post is stored as a real post, and linked
596 // to from the discuss entry.
597
598 $post->discussion = 0;
599 $post->parent = 0;
600 $post->user = $USER->id;
601 $post->created = $timenow;
602 $post->modified = $timenow;
603 $post->mailed = 0;
604 $post->subject = $discussion->name;
605 $post->message = $discussion->intro;
7f6689e4 606 $post->attachment = "";
607 $post->forum = $discussion->forum;
608 $post->course = $discussion->course;
501cdbd8 609
610 if (! $post->id = insert_record("forum_posts", $post) ) {
611 return 0;
612 }
613
7f6689e4 614 if ($post->attachment = forum_add_attachment($post, $discussion->attachment)) {
615 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id); //ignore errors
616 }
617
501cdbd8 618 // Now do the real module entry
619
620 $discussion->firstpost = $post->id;
621 $discussion->timemodified = $timenow;
622
623 if (! $discussion->id = insert_record("forum_discussions", $discussion) ) {
7f6689e4 624 delete_records("forum_posts", "id", $post->id);
501cdbd8 625 return 0;
626 }
627
628 // Finally, set the pointer on the post.
629 if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) {
7f6689e4 630 delete_records("forum_posts", "id", $post->id);
631 delete_records("forum_discussions", "id", $discussion->id);
501cdbd8 632 return 0;
633 }
634
635 return $discussion->id;
636}
637
638
639function forum_delete_discussion($discussion) {
640// $discussion is a discussion record object
641
642 $result = true;
643
644 if ($posts = get_records("forum_posts", "discussion", $discussion->id)) {
645 foreach ($posts as $post) {
7f6689e4 646 $post->course = $discussion->course;
647 $post->forum = $discussion->forum;
501cdbd8 648 if (! delete_records("forum_ratings", "post", "$post->id")) {
649 $result = false;
650 }
7f6689e4 651 if (! forum_delete_post($post)) {
652 $result = false;
653 }
501cdbd8 654 }
655 }
656
501cdbd8 657 if (! delete_records("forum_discussions", "id", "$discussion->id")) {
658 $result = false;
659 }
660
661 return $result;
662}
663
664
7f6689e4 665function forum_delete_post($post) {
666 if (delete_records("forum_posts", "id", $post->id)) {
667 delete_records("forum_ratings", "post", $post->id); // Just in case
668 if ($post->attachment) {
669 $discussion = get_record("forum_discussions", "id", $post->discussion);
670 $post->course = $discussion->course;
671 $post->forum = $discussion->forum;
672 forum_delete_old_attachments($post);
673 }
64eacd6f 674 return true;
675 }
676 return false;
677}
678
501cdbd8 679
11b0c469 680function forum_print_user_discussions($courseid, $userid) {
8f9c8aaf 681 global $CFG, $USER;
501cdbd8 682
b76128b0 683 $discussions = get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture,
8f9c8aaf 684 u.id as userid, f.type as forumtype, f.name as forumname, f.id as forumid
4ca09091 685 FROM forum_discussions d, forum_posts p, user u, forum f
11b0c469 686 WHERE d.course = '$courseid' AND p.discussion = d.id AND
4ca09091 687 p.parent = 0 AND p.user = u.id AND u.id = '$userid' AND
688 d.forum = f.id
b76128b0 689 ORDER BY p.created ASC");
501cdbd8 690
11b0c469 691 if ($discussions) {
b76128b0 692 $user = get_record("user", "id", $userid);
501cdbd8 693 echo "<HR>";
ffe11640 694 print_heading( get_string("discussionsstartedby", "forum", "$user->firstname $user->lastname") );
11b0c469 695 $replies = forum_count_discussion_replies();
696 foreach ($discussions as $discussion) {
4ca09091 697 if (($discussion->forumtype == "teacher") and !isteacher($courseid)) {
698 continue;
699 }
11b0c469 700 if ($replies[$discussion->discussion]) {
701 $discussion->replies = $replies[$discussion->discussion]->replies;
501cdbd8 702 } else {
11b0c469 703 $discussion->replies = 0;
501cdbd8 704 }
ffe11640 705 $inforum = get_string("inforum", "forum", "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname</A>");
706 $discussion->subject .= " ($inforum)";
11b0c469 707 $ownpost = ($discussion->userid == $USER->id);
7f6689e4 708 forum_print_post($discussion, $courseid, $ownpost, $reply=0, $link=1, $assessed=false);
501cdbd8 709 echo "<BR>\n";
710 }
711 }
712}
713
714
501cdbd8 715function forum_user_outline($course, $user, $mod, $forum) {
716
501cdbd8 717 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
498178e4 718 FROM forum f, forum_discussions d, forum_posts p, user u
719 WHERE f.id = '$forum->id' AND d.forum = f.id AND p.discussion = d.id
720 AND p.user = '$user->id' AND p.user = u.id
501cdbd8 721 ORDER BY p.modified ASC")) {
722
ffe11640 723 $result->info = get_string("numposts", "forum", count($posts));
501cdbd8 724
725 $lastpost = array_pop($posts);
726 $result->time = $lastpost->modified;
727 return $result;
728 }
729 return NULL;
730}
731
732
733function forum_user_complete($course, $user, $mod, $forum) {
734 global $CFG;
735
501cdbd8 736 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
498178e4 737 FROM forum f, forum_discussions d, forum_posts p, user u
738 WHERE f.id = '$forum->id' AND d.forum = f.id AND p.discussion = d.id
739 AND p.user = '$user->id' AND p.user = u.id
740 ORDER BY p.modified ASC")) {
501cdbd8 741
742 foreach ($posts as $post) {
743 if ($post->parent) {
ffe11640 744 $footer = "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">".
745 get_string("parentofthispost", "forum")."</A>";
501cdbd8 746 } else {
747 $footer = "";
748 }
749
77db7e4c 750 forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false, $rate=false, $footer);
501cdbd8 751 }
752
753 } else {
ffe11640 754 echo "<P>".get_string("noposts", "forum")."</P>";
501cdbd8 755 }
756
757}
758
04eba58f 759
760function forum_add_instance($forum) {
761// Given an object containing all the necessary data,
762// (defined by the form in mod.html) this function
763// will create a new instance and return the id number
764// of the new instance.
765
766 global $CFG;
767
768 $forum->timemodified = time();
769
770 if (! $forum->id = insert_record("forum", $forum)) {
771 return false;
772 }
773
774 if ($forum->type == "single") { // Create related discussion.
775
776 $discussion->course = $forum->course;
777 $discussion->forum = $forum->id;
778 $discussion->name = $forum->name;
779 $discussion->intro = $forum->intro;
780 $discussion->assessed = $forum->assessed;
781
782 if (! forum_add_discussion($discussion)) {
783 error("Could not add the discussion for this forum");
784 }
785 }
786 add_to_log($forum->course, "forum", "add", "index.php?f=$forum->id", "$forum->id");
787
788 return $forum->id;
789}
790
791
792function forum_update_instance($forum) {
793// Given an object containing all the necessary data,
794// (defined by the form in mod.html) this function
795// will update an existing instance with new data.
796
797 $forum->timemodified = time();
798 $forum->id = $forum->instance;
799
800 if ($forum->type == "single") { // Update related discussion and post.
801 if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
802 if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
803 notify("Warning! There is more than one discussion in this forum - using the most recent");
804 $discussion = array_pop($discussions);
805 } else {
806 error("Could not find the discussion in this forum");
807 }
808 }
809 if (! $post = get_record("forum_posts", "id", $discussion->firstpost)) {
810 error("Could not find the first post in this forum discussion");
811 }
812
813 $post->subject = $forum->name;
814 $post->message = $forum->intro;
815 $post->modified = $forum->timemodified;
816
817 if (! update_record("forum_posts", $post)) {
818 error("Could not update the first post");
819 }
820
821 $discussion->name = $forum->name;
822
823 if (! update_record("forum_discussions", $discussion)) {
824 error("Could not update the discussion");
825 }
826 }
827
828 if (update_record("forum", $forum)) {
829 add_to_log($forum->course, "forum", "update", "index.php?f=$forum->id", "$forum->id");
830 return true;
831 } else {
832 return false;
833 }
834}
835
836
837function forum_delete_instance($id) {
838// Given an ID of an instance of this module,
839// this function will permanently delete the instance
840// and any data that depends on it.
841
842 if (! $forum = get_record("forum", "id", "$id")) {
843 return false;
844 }
845
846 $result = true;
847
848 if ($discussions = get_records("forum_discussions", "forum", $forum->id)) {
849 foreach ($discussions as $discussion) {
850 if (! forum_delete_discussion($discussion)) {
851 $result = false;
852 }
853 }
854 }
855
856 if (! delete_records("forum_subscriptions", "forum", "$forum->id")) {
857 $result = false;
858 }
859
860 if (! delete_records("forum", "id", "$forum->id")) {
861 $result = false;
862 }
863
864 return $result;
865}
866
867
501cdbd8 868function forum_cron () {
869// Function to be run periodically according to the moodle cron
870// Finds all posts that have yet to be mailed out, and mails them
871
a16c2180 872 global $CFG, $USER;
501cdbd8 873
501cdbd8 874 $cutofftime = time() - $CFG->maxeditingtime;
875
876 if ($posts = get_records_sql("SELECT p.*, d.course FROM forum_posts p, forum_discussions d
877 WHERE p.mailed = '0' AND p.created < '$cutofftime' AND p.discussion = d.id")) {
878
879 $timenow = time();
880
881 foreach ($posts as $post) {
882
ffe11640 883 print_string("processingpost", "forum", $post->id);
884 echo " ... ";
501cdbd8 885
886 if (! $userfrom = get_record("user", "id", "$post->user")) {
887 echo "Could not find user $post->user\n";
888 continue;
889 }
890
501cdbd8 891 if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) {
892 echo "Could not find discussion $post->discussion\n";
893 continue;
894 }
895
896 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
897 echo "Could not find forum $discussion->forum\n";
898 continue;
899 }
900
ffe11640 901 if (! $course = get_record("course", "id", "$forum->course")) {
902 echo "Could not find course $forum->course\n";
903 continue;
904 }
501cdbd8 905
86970225 906 if ($users = forum_subscribed_users($course, $forum)) {
8f66d49a 907 $canunsubscribe = ! forum_is_forcesubscribed($forum->id);
501cdbd8 908
ffe11640 909 $mailcount=0;
501cdbd8 910 foreach ($users as $userto) {
5319c6ad 911 $USER->lang = $userto->lang; // Affects the language of get_string
912
a5a4cd60 913
ffe11640 914 $by->name = "$userfrom->firstname $userfrom->lastname";
915 $by->date = userdate($post->created, "", $userto->timezone);
916 $strbynameondate = get_string("bynameondate", "forum", $by);
917
a5a4cd60 918 $strforums = get_string("forums", "forum");
919
86970225 920 $postsubject = "$course->shortname: $post->subject";
921 $posttext = "$course->shortname -> $strforums -> $forum->name";
922
923 if ($discussion->name == $forum->name) {
924 $posttext .= "\n";
925 } else {
926 $posttext .= " -> $discussion->name\n";
927 }
928 $posttext .= "---------------------------------------------------------------------\n";
929 $posttext .= "$post->subject\n";
930 $posttext .= $strbynameondate."\n";
931 $posttext .= "---------------------------------------------------------------------\n";
932 $posttext .= strip_tags($post->message);
933 $posttext .= "\n\n";
7f6689e4 934 if ($post->attachment) {
935 $post->course = $course->id;
936 $post->forum = $forum->id;
937 $posttext .= forum_print_attachments($post, "text");
938 }
86970225 939 $posttext .= "---------------------------------------------------------------------\n";
940 $posttext .= get_string("postmailinfo", "forum", $course->shortname)."\n";
b362b85e 941 $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id\n";
942 if ($canunsubscribe) {
943 $posttext .= "\n---------------------------------------------------------------------\n";
944 $posttext .= get_string("unsubscribe", "forum");
945 $posttext .= ": $CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\n";
946 }
86970225 947
948 if ($userto->mailformat == 1) { // HTML
949 $posthtml = "<P><FONT FACE=sans-serif>".
950 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> -> ".
951 "<A HREF=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</A> -> ".
952 "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name</A>";
ffe11640 953 if ($discussion->name == $forum->name) {
86970225 954 $posthtml .= "</FONT></P>";
ffe11640 955 } else {
86970225 956 $posthtml .= " -> <A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name</A></FONT></P>";
ffe11640 957 }
86970225 958 $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, true, false, false);
8f66d49a 959
960 if ($canunsubscribe) {
b362b85e 961 $posthtml .= "\n<BR><HR SIZE=1 NOSHADE><P ALIGN=RIGHT><FONT SIZE=1><A HREF=\"$CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\">".get_string("unsubscribe", "forum")."</A></FONT></P>";
8f66d49a 962 }
963
86970225 964 } else {
965 $posthtml = "";
966 }
967
968 if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) {
969 echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email)\n";
970 } else {
971 $mailcount++;
501cdbd8 972 }
973 }
ffe11640 974 echo "mailed to $mailcount users ...";
501cdbd8 975 }
976
977 if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) {
978 echo "Could not update the mailed field for id $post->id\n";
979 }
ffe11640 980 echo "\n";
501cdbd8 981 }
982 }
983
984 return true;
985}
986
987
988function forum_forcesubscribe($forumid, $value=1) {
989 return set_field("forum", "forcesubscribe", $value, "id", $forumid);
990}
991
992function forum_is_forcesubscribed($forumid) {
993 return get_field("forum", "forcesubscribe", "id", $forumid);
994}
995
996function forum_is_subscribed($userid, $forumid) {
997 if (forum_is_forcesubscribed($forumid)) {
998 return true;
999 }
1000 return record_exists_sql("SELECT * FROM forum_subscriptions WHERE user='$userid' AND forum='$forumid'");
1001}
1002
86970225 1003function forum_subscribed_users($course, $forum) {
1004// Returns list of user objects that are subscribed to this forum
1005
1006 if ($course->category) { // normal course
1007 if ($forum->forcesubscribe) {
1008 return get_course_users($course->id);
1009 }
1010 }
1011 return get_records_sql("SELECT u.* FROM user u, forum_subscriptions s
1012 WHERE s.forum = '$forum->id'
ae2e0ddd 1013 AND s.user = u.id AND u.deleted <> '1'");
86970225 1014}
1015
501cdbd8 1016function forum_subscribe($userid, $forumid) {
1017 global $db;
1018
1019 return $db->Execute("INSERT INTO forum_subscriptions SET user = '$userid', forum = '$forumid'");
1020}
1021
1022function forum_unsubscribe($userid, $forumid) {
1023 global $db;
1024
1025 return $db->Execute("DELETE FROM forum_subscriptions WHERE user = '$userid' AND forum = '$forumid'");
1026}
1027
1028
11b0c469 1029function forum_user_has_posted_discussion($forumid, $userid) {
1030 if ($discussions = forum_get_discussions($forumid, "DESC", $userid)) {
501cdbd8 1031 return true;
1032 } else {
1033 return false;
1034 }
1035}
1036
11b0c469 1037function forum_user_can_post_discussion($forum) {
501cdbd8 1038// $forum is an object
1039 global $USER;
1040
1041 if ($forum->type == "eachuser") {
11b0c469 1042 return (! forum_user_has_posted_discussion($forum->id, $USER->id));
501cdbd8 1043 } else if ($forum->type == "teacher") {
1044 return isteacher($forum->course);
1045 } else if (isteacher($forum->course)) {
1046 return true;
1047 } else {
1048 return $forum->open;
1049 }
1050}
1051
1052
11b0c469 1053function forum_get_discussions($forum="0", $forum_sort="DESC", $user=0) {
501cdbd8 1054 if ($user) {
1055 $userselect = " AND u.id = '$user' ";
1056 } else {
1057 $userselect = "";
1058 }
1059 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.id as userid
1060 FROM forum_discussions d, forum_posts p, user u
1061 WHERE d.forum = '$forum' AND p.discussion = d.id AND
1062 p.parent= 0 AND p.user = u.id $userselect
1063 ORDER BY p.created $forum_sort");
1064}
1065
1066
1067
11b0c469 1068function forum_print_latest_discussions($forum_id=0, $forum_numdiscussions=5, $forum_style="plain", $forum_sort="DESC") {
3335f6fb 1069 global $CFG, $USER, $FORUM_MANY_DISCUSSIONS;
f93f848a 1070
1071 if ($forum_id) {
1072 if (! $forum = get_record("forum", "id", $forum_id)) {
1073 error("Forum ID was incorrect");
1074 }
1075 if (! $course = get_record("course", "id", $forum->course)) {
1076 error("Could not find the course this forum belongs to!");
1077 }
1078
1079 if ($course->category) {
1080 require_login($course->id);
1081 }
1082
1083 } else {
1084 if (! $course = get_record("course", "category", 0)) {
1085 error("Could not find a top-level course!");
1086 }
11b0c469 1087 if (! $forum = forum_get_course_news_forum($course->id)) {
f93f848a 1088 error("Could not find or create a main forum in this course (id $course->id)");
1089 }
1090 }
1091
11b0c469 1092 if (forum_user_can_post_discussion($forum)) {
3b9af3dd 1093 echo "<P ALIGN=CENTER>";
ffe11640 1094 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?forum=$forum->id\">";
1095 echo get_string("addanewdiscussion", "forum")."</A>...";
501cdbd8 1096 echo "</P>\n";
77305fe6 1097 }
1098
11b0c469 1099 if (! $discussions = forum_get_discussions($forum->id, $forum_sort) ) {
ffe11640 1100 echo "<P ALIGN=CENTER><B>(".get_string("nodiscussions", "forum").")</B></P>";
3335f6fb 1101 return;
f93f848a 1102
3335f6fb 1103 }
1104
1105 if ((!$forum_numdiscussions) && ($forum_style == "plain") && (count($discussions) > $FORUM_MANY_DISCUSSIONS) ) {
1106 $forum_style = "header"; // Abbreviate display if it's going to be long.
1107 }
f93f848a 1108
3335f6fb 1109 $replies = forum_count_discussion_replies($forum->id);
f93f848a 1110
3335f6fb 1111 $discussioncount = 0;
f93f848a 1112
3335f6fb 1113 foreach ($discussions as $discussion) {
1114 $discussioncount++;
f93f848a 1115
3335f6fb 1116 if ($forum_numdiscussions && ($discussioncount > $forum_numdiscussions)) {
1117 echo "<P ALIGN=right><A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">";
1118 echo get_string("olderdiscussions", "forum")."</A> ...</P>";
1119 break;
1120 }
1121 if ($replies[$discussion->discussion]) {
1122 $discussion->replies = $replies[$discussion->discussion]->replies;
1123 } else {
1124 $discussion->replies = 0;
1125 }
1126 $ownpost = ($discussion->userid == $USER->id);
1127 switch ($forum_style) {
1128 case "minimal":
1129 echo "<P><FONT COLOR=#555555>".userdate($discussion->modified, "%e %b, %H:%M")." - $discussion->firstname</FONT>";
1130 echo "<BR>$discussion->subject ";
1131 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->discussion\">";
1132 echo get_string("more", "forum")."...</A>";
1133 echo "</P>\n";
1134 break;
1135 case "header":
1136 forum_print_post_header($discussion, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
1137 break;
1138 default:
1139 forum_print_post($discussion, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
1140 echo "<BR>\n";
1141 break;
f93f848a 1142 }
1143 }
f93f848a 1144}
1145
fe25fc9b 1146function forum_print_discussion($course, $forum, $discussion, $post, $mode) {
501cdbd8 1147
1148 global $USER;
1149
1150 $ownpost = ($USER->id == $post->user);
1151
11b0c469 1152 forum_print_post($post, $course->id, $ownpost, $reply=true, $link=false, $rate=false);
501cdbd8 1153
7a12aab4 1154 forum_print_mode_form($discussion->id, $mode);
501cdbd8 1155
9d1b97c5 1156 $ratingform = false;
fe25fc9b 1157 if ($forum->assessed && $USER->id) {
9d1b97c5 1158 $unrated = forum_count_unrated_posts($discussion->id, $USER->id);
1159 if ($unrated > 0) {
1160 $ratingform = true;
1161 }
1162 }
1163
1164 if ($ratingform) {
501cdbd8 1165 echo "<FORM NAME=form METHOD=POST ACTION=rate.php>";
1166 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
1167 }
1168
1169 switch ($mode) {
1170 case 1 : // Flat ascending
1171 case -1 : // Flat descending
1172 default:
1173 echo "<UL>";
75d10a02 1174 forum_print_posts_flat($post->discussion, $course->id, $mode, $forum->assessed);
501cdbd8 1175 echo "</UL>";
1176 break;
1177
1178 case 2 : // Threaded
75d10a02 1179 forum_print_posts_threaded($post->id, $course->id, 0, $forum->assessed);
501cdbd8 1180 break;
1181
1182 case 3 : // Nested
75d10a02 1183 forum_print_posts_nested($post->id, $course->id, $forum->assessed);
501cdbd8 1184 break;
1185 }
1186
9d1b97c5 1187 if ($ratingform) {
31d160d3 1188 echo "<CENTER><P ALIGN=center><INPUT TYPE=submit VALUE=\"".get_string("sendinratings", "forum")."\">";
61ee082f 1189 helpbutton("ratings", get_string("separateandconnected"), "forum");
31d160d3 1190 echo "</P></CENTER>";
501cdbd8 1191 echo "</FORM>";
1192 }
1193}
1194
11b0c469 1195function forum_print_posts_flat($discussion, $course, $direction, $assessed) {
501cdbd8 1196 global $USER;
1197
1198 $reply = true;
1199 $link = false;
1200
1201 if ($direction < 0) {
1202 $sort = "ORDER BY created DESC";
1203 } else {
1204 $sort = "ORDER BY created ASC";
1205 }
1206
1207 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
1208 FROM forum_posts p, user u
1209 WHERE p.discussion = $discussion AND p.parent > 0 AND p.user = u.id $sort")) {
1210
1211 foreach ($posts as $post) {
1212 $ownpost = ($USER->id == $post->user);
11b0c469 1213 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
501cdbd8 1214 }
1215 } else {
1216 return;
1217 }
1218}
1219
11b0c469 1220function forum_print_posts_threaded($parent, $course, $depth, $assessed) {
501cdbd8 1221 global $USER;
1222
1223 $reply = true;
1224 $link = false;
1225
1226 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
1227 FROM forum_posts p, user u
1228 WHERE p.parent = '$parent' AND p.user = u.id")) {
1229
1230 foreach ($posts as $post) {
1231
1232 echo "<UL>";
1233 if ($depth > 0) {
1234 $ownpost = ($USER->id == $post->user);
11b0c469 1235 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed); // link=true?
501cdbd8 1236 echo "<BR>";
1237 } else {
8c3c8481 1238 $by->name = "$post->firstname $post->lastname";
1239 $by->date = userdate($post->created);
1240 echo "<LI><P><FONT SIZE=-1><B><A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</A></B> ";
1241 print_string("bynameondate", "forum", $by);
1242 echo "</FONT></P></LI>";
501cdbd8 1243 }
1244
11b0c469 1245 forum_print_posts_threaded($post->id, $course, $depth-1, $assessed);
501cdbd8 1246 echo "</UL>\n";
1247 }
1248 } else {
1249 return;
1250 }
1251}
1252
11b0c469 1253function forum_print_posts_nested($parent, $course, $assessed) {
501cdbd8 1254 global $USER;
1255
1256 $reply = true;
1257 $link = false;
1258
1259 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
1260 FROM forum_posts p, user u
1261 WHERE p.parent = $parent AND p.user = u.id
1262 ORDER BY p.created ASC ")) {
1263
1264 foreach ($posts as $post) {
1265
1266 $ownpost = ($USER->id == $post->user);
1267
1268 echo "<UL>";
11b0c469 1269 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
501cdbd8 1270 echo "<BR>";
11b0c469 1271 forum_print_posts_nested($post->id, $course, $assessed);
501cdbd8 1272 echo "</UL>\n";
1273 }
1274 } else {
1275 return;
1276 }
1277}
1278
1279function forum_set_display_mode($mode=0) {
5a83a0a8 1280 global $USER, $FORUM_DEFAULT_DISPLAY_MODE;
501cdbd8 1281
1282 if ($mode) {
1283 $USER->mode = $mode;
8223d271 1284 save_session("USER");
501cdbd8 1285 } else if (!$USER->mode) {
1286 $USER->mode = $FORUM_DEFAULT_DISPLAY_MODE;
8223d271 1287 save_session("USER");
501cdbd8 1288 }
1289}
f93f848a 1290
3869a2ac 1291function forum_print_recent_activity(&$logs, $isteacher=false) {
1292 global $CFG, $COURSE_TEACHER_COLOR;
1293
1294 $heading = false;
1295 $content = false;
1296
1297 foreach ($logs as $log) {
1298 if ($log->module == "forum") {
1299 $post = NULL;
1300
1301 if ($log->action == "add post") {
1302 $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
1303 u.email, u.picture, u.id as userid
1304 FROM forum_discussions d, forum_posts p, user u
ae2e0ddd 1305 WHERE p.id = '$log->info' AND d.id = p.discussion
1306 AND p.user = u.id and u.deleted <> '1'");
3869a2ac 1307
1308 } else if ($log->action == "add discussion") {
1309 $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
1310 u.email, u.picture, u.id as userid
1311 FROM forum_discussions d, forum_posts p, user u
ae2e0ddd 1312 WHERE d.id = '$log->info' AND d.firstpost = p.id
1313 AND p.user = u.id and u.deleted <> '1'");
3869a2ac 1314 }
1315
1316 if ($post) {
1317 $teacherpost = "";
1318 if ($forum = get_record("forum", "id", $post->forum) ) {
1319 if ($forum->type == "teacher") {
1320 if ($isteacher) {
3869a2ac 1321 $teacherpost = "COLOR=$COURSE_TEACHER_COLOR";
7fd6bb7c 1322 } else {
1323 continue;
3869a2ac 1324 }
1325 }
1326 }
1327 if (! $heading) {
1328 print_headline(get_string("newforumposts", "forum").":");
1329 $heading = true;
1330 $content = true;
1331 }
1332 $date = userdate($post->modified, "%e %b, %H:%M");
1333 echo "<P><FONT SIZE=1 $teacherpost>$date - $post->firstname $post->lastname<BR>";
1334 echo "\"<A HREF=\"$CFG->wwwroot/mod/forum/$log->url\">";
1335 if ($log->action == "add") {
1336 echo "<B>$post->subject</B>";
1337 } else {
1338 echo "$post->subject";
1339 }
1340 echo "</A>\"</FONT></P>";
1341 }
1342 }
1343 }
1344 return $content;
1345}
1346
f93f848a 1347?>