Text window a bit bigger
[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() {
501cdbd8 416 global $SESSION, $HTTP_REFERER;
417
418 if (! $SESSION->fromdiscussion) {
419 $SESSION->fromdiscussion = $HTTP_REFERER;
8223d271 420 save_session("SESSION");
501cdbd8 421 }
422}
423
424
11b0c469 425function forum_go_back_to($default) {
501cdbd8 426 global $SESSION;
427
428 if ($SESSION->fromdiscussion) {
429 $returnto = $SESSION->fromdiscussion;
430 unset($SESSION->fromdiscussion);
8223d271 431 save_session("SESSION");
501cdbd8 432 return $returnto;
433 } else {
434 return $default;
435 }
436}
437
11b0c469 438function forum_get_post_full($postid) {
501cdbd8 439 return get_record_sql("SELECT p.*, u.firstname, u.lastname,
440 u.email, u.picture, u.id as userid
441 FROM forum_posts p, user u
442 WHERE p.id = '$postid' AND p.user = u.id");
443}
444
7f6689e4 445function forum_file_area_name($post) {
446// Creates a directory file name, suitable for make_upload_directory()
447 global $CFG;
448
449 return "$post->course/$CFG->moddata/forum/$post->forum/$post->id";
450}
451
452function forum_file_area($post) {
453 return make_upload_directory( forum_file_area_name($post) );
454}
455
456function forum_delete_old_attachments($post, $exception="") {
457// Deletes all the user files in the attachments area for a post
458// EXCEPT for any file named $exception
459
460 if ($basedir = forum_file_area($post)) {
461 if ($files = get_directory_list($basedir)) {
462 foreach ($files as $file) {
463 if ($file != $exception) {
464 unlink("$basedir/$file");
465 notify("Existing file '$file' has been deleted!");
466 }
467 }
468 }
469 if (!$exception) { // Delete directory as well, if empty
470 rmdir("$basedir");
471 }
472 }
473}
474
475function forum_print_attachments($post, $return=NULL) {
476// if return=html, then return a html string.
477// if return=text, then return a text-only string.
478// otherwise, print HTML
479
480 global $CFG;
481
482 $filearea = forum_file_area_name($post);
483
484 if ($basedir = forum_file_area($post)) {
485 if ($files = get_directory_list($basedir)) {
486 $strattachment = get_string("attachment", "forum");
487 foreach ($files as $file) {
488 $icon = mimeinfo("icon", $file);
489 if ($CFG->slasharguments) {
490 $ffurl = "file.php/$filearea/$file";
491 } else {
492 $ffurl = "file.php?file=/$filearea/$file";
493 }
494 $image = "<IMG BORDER=0 SRC=\"$CFG->wwwroot/files/pix/$icon\" HEIGHT=16 WIDTH=16 ALT=\"File\">";
495
496 if ($return == "html") {
497 $output .= "<A HREF=\"$CFG->wwwroot/$ffurl\">$image</A> ";
498 $output .= "<A HREF=\"$CFG->wwwroot/$ffurl\">$file</A><BR>";
499
500 } else if ($return == "text") {
501 $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
502
503 } else {
504 link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
505 echo "<A HREF=\"$CFG->wwwroot/$ffurl\">$file</A>";
506 echo "<BR>";
507 }
508 }
509 }
510 }
511 if ($return) {
512 return $output;
513 }
514}
515
516function forum_add_attachment($post, $newfile) {
517// $post is a full post record, including course and forum
518// $newfile is a full upload array from HTTP_POST_FILES
519// If successful, this function returns the name of the file
520
521 if (!isset($newfile['name'])) {
522 return "";
523 }
524
525 $newfile_name = clean_filename($newfile['name']);
526
527 if (valid_uploaded_file($newfile)) {
528 if (! $newfile_name) {
529 notify("This file had a wierd filename and couldn't be uploaded");
530
531 } else if (! $dir = forum_file_area($post)) {
532 notify("Attachment could not be stored");
533 $newfile_name = "";
534
535 } else {
536 if (move_uploaded_file($newfile['tmp_name'], "$dir/$newfile_name")) {
537 forum_delete_old_attachments($post, $newfile_name);
538 } else {
539 notify("An error happened while saving the file on the server");
540 $newfile_name = "";
541 }
542 }
543 } else {
544 $newfile_name = "";
545 }
546
547 return $newfile_name;
548}
501cdbd8 549
11b0c469 550function forum_add_new_post($post) {
501cdbd8 551
ffe11640 552 $post->created = $post->modified = time();
501cdbd8 553 $post->mailed = "0";
554
7f6689e4 555 $newfile = $post->attachment;
556 $post->attachment = "";
557
558 if (! $post->id = insert_record("forum_posts", $post)) {
559 return false;
560 }
561
562 if ($post->attachment = forum_add_attachment($post, $newfile)) {
563 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id);
564 }
565
566 return $post->id;
501cdbd8 567}
568
11b0c469 569function forum_update_post($post) {
501cdbd8 570
ffe11640 571 $post->modified = time();
501cdbd8 572
0ab85112 573 if (!$post->parent) { // Post is a discussion starter - update discussion title too
574 set_field("forum_discussions", "name", $post->subject, "id", $post->discussion);
575 }
7f6689e4 576 if ($newfilename = forum_add_attachment($post, $post->attachment)) {
577 $post->attachment = $newfilename;
578 } else {
579 unset($post->attachment);
580 }
ffe11640 581 return update_record("forum_posts", $post);
501cdbd8 582}
583
584function forum_add_discussion($discussion) {
585// Given an object containing all the necessary data,
586// create a new discussion and return the id
587
588 GLOBAL $USER;
589
590 $timenow = time();
591
592 // The first post is stored as a real post, and linked
593 // to from the discuss entry.
594
595 $post->discussion = 0;
596 $post->parent = 0;
597 $post->user = $USER->id;
598 $post->created = $timenow;
599 $post->modified = $timenow;
600 $post->mailed = 0;
601 $post->subject = $discussion->name;
602 $post->message = $discussion->intro;
7f6689e4 603 $post->attachment = "";
604 $post->forum = $discussion->forum;
605 $post->course = $discussion->course;
501cdbd8 606
607 if (! $post->id = insert_record("forum_posts", $post) ) {
608 return 0;
609 }
610
7f6689e4 611 if ($post->attachment = forum_add_attachment($post, $discussion->attachment)) {
612 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id); //ignore errors
613 }
614
501cdbd8 615 // Now do the real module entry
616
617 $discussion->firstpost = $post->id;
618 $discussion->timemodified = $timenow;
619
620 if (! $discussion->id = insert_record("forum_discussions", $discussion) ) {
7f6689e4 621 delete_records("forum_posts", "id", $post->id);
501cdbd8 622 return 0;
623 }
624
625 // Finally, set the pointer on the post.
626 if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) {
7f6689e4 627 delete_records("forum_posts", "id", $post->id);
628 delete_records("forum_discussions", "id", $discussion->id);
501cdbd8 629 return 0;
630 }
631
632 return $discussion->id;
633}
634
635
636function forum_delete_discussion($discussion) {
637// $discussion is a discussion record object
638
639 $result = true;
640
641 if ($posts = get_records("forum_posts", "discussion", $discussion->id)) {
642 foreach ($posts as $post) {
7f6689e4 643 $post->course = $discussion->course;
644 $post->forum = $discussion->forum;
501cdbd8 645 if (! delete_records("forum_ratings", "post", "$post->id")) {
646 $result = false;
647 }
7f6689e4 648 if (! forum_delete_post($post)) {
649 $result = false;
650 }
501cdbd8 651 }
652 }
653
501cdbd8 654 if (! delete_records("forum_discussions", "id", "$discussion->id")) {
655 $result = false;
656 }
657
658 return $result;
659}
660
661
7f6689e4 662function forum_delete_post($post) {
663 if (delete_records("forum_posts", "id", $post->id)) {
664 delete_records("forum_ratings", "post", $post->id); // Just in case
665 if ($post->attachment) {
666 $discussion = get_record("forum_discussions", "id", $post->discussion);
667 $post->course = $discussion->course;
668 $post->forum = $discussion->forum;
669 forum_delete_old_attachments($post);
670 }
64eacd6f 671 return true;
672 }
673 return false;
674}
675
501cdbd8 676
11b0c469 677function forum_print_user_discussions($courseid, $userid) {
8f9c8aaf 678 global $CFG, $USER;
501cdbd8 679
b76128b0 680 $discussions = get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture,
8f9c8aaf 681 u.id as userid, f.type as forumtype, f.name as forumname, f.id as forumid
4ca09091 682 FROM forum_discussions d, forum_posts p, user u, forum f
11b0c469 683 WHERE d.course = '$courseid' AND p.discussion = d.id AND
4ca09091 684 p.parent = 0 AND p.user = u.id AND u.id = '$userid' AND
685 d.forum = f.id
b76128b0 686 ORDER BY p.created ASC");
501cdbd8 687
11b0c469 688 if ($discussions) {
b76128b0 689 $user = get_record("user", "id", $userid);
501cdbd8 690 echo "<HR>";
ffe11640 691 print_heading( get_string("discussionsstartedby", "forum", "$user->firstname $user->lastname") );
11b0c469 692 $replies = forum_count_discussion_replies();
693 foreach ($discussions as $discussion) {
4ca09091 694 if (($discussion->forumtype == "teacher") and !isteacher($courseid)) {
695 continue;
696 }
11b0c469 697 if ($replies[$discussion->discussion]) {
698 $discussion->replies = $replies[$discussion->discussion]->replies;
501cdbd8 699 } else {
11b0c469 700 $discussion->replies = 0;
501cdbd8 701 }
ffe11640 702 $inforum = get_string("inforum", "forum", "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname</A>");
703 $discussion->subject .= " ($inforum)";
11b0c469 704 $ownpost = ($discussion->userid == $USER->id);
7f6689e4 705 forum_print_post($discussion, $courseid, $ownpost, $reply=0, $link=1, $assessed=false);
501cdbd8 706 echo "<BR>\n";
707 }
708 }
709}
710
711
501cdbd8 712function forum_user_outline($course, $user, $mod, $forum) {
713
501cdbd8 714 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
498178e4 715 FROM forum f, forum_discussions d, forum_posts p, user u
716 WHERE f.id = '$forum->id' AND d.forum = f.id AND p.discussion = d.id
717 AND p.user = '$user->id' AND p.user = u.id
501cdbd8 718 ORDER BY p.modified ASC")) {
719
ffe11640 720 $result->info = get_string("numposts", "forum", count($posts));
501cdbd8 721
722 $lastpost = array_pop($posts);
723 $result->time = $lastpost->modified;
724 return $result;
725 }
726 return NULL;
727}
728
729
730function forum_user_complete($course, $user, $mod, $forum) {
731 global $CFG;
732
501cdbd8 733 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
498178e4 734 FROM forum f, forum_discussions d, forum_posts p, user u
735 WHERE f.id = '$forum->id' AND d.forum = f.id AND p.discussion = d.id
736 AND p.user = '$user->id' AND p.user = u.id
737 ORDER BY p.modified ASC")) {
501cdbd8 738
739 foreach ($posts as $post) {
740 if ($post->parent) {
ffe11640 741 $footer = "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">".
742 get_string("parentofthispost", "forum")."</A>";
501cdbd8 743 } else {
744 $footer = "";
745 }
746
77db7e4c 747 forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false, $rate=false, $footer);
501cdbd8 748 }
749
750 } else {
ffe11640 751 echo "<P>".get_string("noposts", "forum")."</P>";
501cdbd8 752 }
753
754}
755
04eba58f 756
757function forum_add_instance($forum) {
758// Given an object containing all the necessary data,
759// (defined by the form in mod.html) this function
760// will create a new instance and return the id number
761// of the new instance.
762
763 global $CFG;
764
765 $forum->timemodified = time();
766
767 if (! $forum->id = insert_record("forum", $forum)) {
768 return false;
769 }
770
771 if ($forum->type == "single") { // Create related discussion.
772
773 $discussion->course = $forum->course;
774 $discussion->forum = $forum->id;
775 $discussion->name = $forum->name;
776 $discussion->intro = $forum->intro;
777 $discussion->assessed = $forum->assessed;
778
779 if (! forum_add_discussion($discussion)) {
780 error("Could not add the discussion for this forum");
781 }
782 }
783 add_to_log($forum->course, "forum", "add", "index.php?f=$forum->id", "$forum->id");
784
785 return $forum->id;
786}
787
788
789function forum_update_instance($forum) {
790// Given an object containing all the necessary data,
791// (defined by the form in mod.html) this function
792// will update an existing instance with new data.
793
794 $forum->timemodified = time();
795 $forum->id = $forum->instance;
796
797 if ($forum->type == "single") { // Update related discussion and post.
798 if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
799 if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
800 notify("Warning! There is more than one discussion in this forum - using the most recent");
801 $discussion = array_pop($discussions);
802 } else {
803 error("Could not find the discussion in this forum");
804 }
805 }
806 if (! $post = get_record("forum_posts", "id", $discussion->firstpost)) {
807 error("Could not find the first post in this forum discussion");
808 }
809
810 $post->subject = $forum->name;
811 $post->message = $forum->intro;
812 $post->modified = $forum->timemodified;
813
814 if (! update_record("forum_posts", $post)) {
815 error("Could not update the first post");
816 }
817
818 $discussion->name = $forum->name;
819
820 if (! update_record("forum_discussions", $discussion)) {
821 error("Could not update the discussion");
822 }
823 }
824
825 if (update_record("forum", $forum)) {
826 add_to_log($forum->course, "forum", "update", "index.php?f=$forum->id", "$forum->id");
827 return true;
828 } else {
829 return false;
830 }
831}
832
833
834function forum_delete_instance($id) {
835// Given an ID of an instance of this module,
836// this function will permanently delete the instance
837// and any data that depends on it.
838
839 if (! $forum = get_record("forum", "id", "$id")) {
840 return false;
841 }
842
843 $result = true;
844
845 if ($discussions = get_records("forum_discussions", "forum", $forum->id)) {
846 foreach ($discussions as $discussion) {
847 if (! forum_delete_discussion($discussion)) {
848 $result = false;
849 }
850 }
851 }
852
853 if (! delete_records("forum_subscriptions", "forum", "$forum->id")) {
854 $result = false;
855 }
856
857 if (! delete_records("forum", "id", "$forum->id")) {
858 $result = false;
859 }
860
861 return $result;
862}
863
864
501cdbd8 865function forum_cron () {
866// Function to be run periodically according to the moodle cron
867// Finds all posts that have yet to be mailed out, and mails them
868
869 global $CFG;
870
501cdbd8 871 $cutofftime = time() - $CFG->maxeditingtime;
872
873 if ($posts = get_records_sql("SELECT p.*, d.course FROM forum_posts p, forum_discussions d
874 WHERE p.mailed = '0' AND p.created < '$cutofftime' AND p.discussion = d.id")) {
875
876 $timenow = time();
877
878 foreach ($posts as $post) {
879
ffe11640 880 print_string("processingpost", "forum", $post->id);
881 echo " ... ";
501cdbd8 882
883 if (! $userfrom = get_record("user", "id", "$post->user")) {
884 echo "Could not find user $post->user\n";
885 continue;
886 }
887
501cdbd8 888 if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) {
889 echo "Could not find discussion $post->discussion\n";
890 continue;
891 }
892
893 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
894 echo "Could not find forum $discussion->forum\n";
895 continue;
896 }
897
ffe11640 898 if (! $course = get_record("course", "id", "$forum->course")) {
899 echo "Could not find course $forum->course\n";
900 continue;
901 }
501cdbd8 902
86970225 903 if ($users = forum_subscribed_users($course, $forum)) {
ffe11640 904 $strforums = get_string("forums", "forum");
501cdbd8 905
ffe11640 906 $mailcount=0;
501cdbd8 907 foreach ($users as $userto) {
ffe11640 908 $by->name = "$userfrom->firstname $userfrom->lastname";
909 $by->date = userdate($post->created, "", $userto->timezone);
910 $strbynameondate = get_string("bynameondate", "forum", $by);
911
86970225 912 $postsubject = "$course->shortname: $post->subject";
913 $posttext = "$course->shortname -> $strforums -> $forum->name";
914
915 if ($discussion->name == $forum->name) {
916 $posttext .= "\n";
917 } else {
918 $posttext .= " -> $discussion->name\n";
919 }
920 $posttext .= "---------------------------------------------------------------------\n";
921 $posttext .= "$post->subject\n";
922 $posttext .= $strbynameondate."\n";
923 $posttext .= "---------------------------------------------------------------------\n";
924 $posttext .= strip_tags($post->message);
925 $posttext .= "\n\n";
7f6689e4 926 if ($post->attachment) {
927 $post->course = $course->id;
928 $post->forum = $forum->id;
929 $posttext .= forum_print_attachments($post, "text");
930 }
86970225 931 $posttext .= "---------------------------------------------------------------------\n";
932 $posttext .= get_string("postmailinfo", "forum", $course->shortname)."\n";
933 $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id";
934
935 if ($userto->mailformat == 1) { // HTML
936 $posthtml = "<P><FONT FACE=sans-serif>".
937 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> -> ".
938 "<A HREF=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</A> -> ".
939 "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name</A>";
ffe11640 940 if ($discussion->name == $forum->name) {
86970225 941 $posthtml .= "</FONT></P>";
ffe11640 942 } else {
86970225 943 $posthtml .= " -> <A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name</A></FONT></P>";
ffe11640 944 }
86970225 945 $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, true, false, false);
946 } else {
947 $posthtml = "";
948 }
949
950 if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) {
951 echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email)\n";
952 } else {
953 $mailcount++;
501cdbd8 954 }
955 }
ffe11640 956 echo "mailed to $mailcount users ...";
501cdbd8 957 }
958
959 if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) {
960 echo "Could not update the mailed field for id $post->id\n";
961 }
ffe11640 962 echo "\n";
501cdbd8 963 }
964 }
965
966 return true;
967}
968
969
970function forum_forcesubscribe($forumid, $value=1) {
971 return set_field("forum", "forcesubscribe", $value, "id", $forumid);
972}
973
974function forum_is_forcesubscribed($forumid) {
975 return get_field("forum", "forcesubscribe", "id", $forumid);
976}
977
978function forum_is_subscribed($userid, $forumid) {
979 if (forum_is_forcesubscribed($forumid)) {
980 return true;
981 }
982 return record_exists_sql("SELECT * FROM forum_subscriptions WHERE user='$userid' AND forum='$forumid'");
983}
984
86970225 985function forum_subscribed_users($course, $forum) {
986// Returns list of user objects that are subscribed to this forum
987
988 if ($course->category) { // normal course
989 if ($forum->forcesubscribe) {
990 return get_course_users($course->id);
991 }
992 }
993 return get_records_sql("SELECT u.* FROM user u, forum_subscriptions s
994 WHERE s.forum = '$forum->id'
995 AND s.user = u.id");
996}
997
501cdbd8 998function forum_subscribe($userid, $forumid) {
999 global $db;
1000
1001 return $db->Execute("INSERT INTO forum_subscriptions SET user = '$userid', forum = '$forumid'");
1002}
1003
1004function forum_unsubscribe($userid, $forumid) {
1005 global $db;
1006
1007 return $db->Execute("DELETE FROM forum_subscriptions WHERE user = '$userid' AND forum = '$forumid'");
1008}
1009
1010
11b0c469 1011function forum_user_has_posted_discussion($forumid, $userid) {
1012 if ($discussions = forum_get_discussions($forumid, "DESC", $userid)) {
501cdbd8 1013 return true;
1014 } else {
1015 return false;
1016 }
1017}
1018
11b0c469 1019function forum_user_can_post_discussion($forum) {
501cdbd8 1020// $forum is an object
1021 global $USER;
1022
1023 if ($forum->type == "eachuser") {
11b0c469 1024 return (! forum_user_has_posted_discussion($forum->id, $USER->id));
501cdbd8 1025 } else if ($forum->type == "teacher") {
1026 return isteacher($forum->course);
1027 } else if (isteacher($forum->course)) {
1028 return true;
1029 } else {
1030 return $forum->open;
1031 }
1032}
1033
1034
11b0c469 1035function forum_get_discussions($forum="0", $forum_sort="DESC", $user=0) {
501cdbd8 1036 if ($user) {
1037 $userselect = " AND u.id = '$user' ";
1038 } else {
1039 $userselect = "";
1040 }
1041 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.id as userid
1042 FROM forum_discussions d, forum_posts p, user u
1043 WHERE d.forum = '$forum' AND p.discussion = d.id AND
1044 p.parent= 0 AND p.user = u.id $userselect
1045 ORDER BY p.created $forum_sort");
1046}
1047
1048
1049
11b0c469 1050function forum_print_latest_discussions($forum_id=0, $forum_numdiscussions=5, $forum_style="plain", $forum_sort="DESC") {
3335f6fb 1051 global $CFG, $USER, $FORUM_MANY_DISCUSSIONS;
f93f848a 1052
1053 if ($forum_id) {
1054 if (! $forum = get_record("forum", "id", $forum_id)) {
1055 error("Forum ID was incorrect");
1056 }
1057 if (! $course = get_record("course", "id", $forum->course)) {
1058 error("Could not find the course this forum belongs to!");
1059 }
1060
1061 if ($course->category) {
1062 require_login($course->id);
1063 }
1064
1065 } else {
1066 if (! $course = get_record("course", "category", 0)) {
1067 error("Could not find a top-level course!");
1068 }
11b0c469 1069 if (! $forum = forum_get_course_news_forum($course->id)) {
f93f848a 1070 error("Could not find or create a main forum in this course (id $course->id)");
1071 }
1072 }
1073
11b0c469 1074 if (forum_user_can_post_discussion($forum)) {
3b9af3dd 1075 echo "<P ALIGN=CENTER>";
ffe11640 1076 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?forum=$forum->id\">";
1077 echo get_string("addanewdiscussion", "forum")."</A>...";
501cdbd8 1078 echo "</P>\n";
77305fe6 1079 }
1080
11b0c469 1081 if (! $discussions = forum_get_discussions($forum->id, $forum_sort) ) {
ffe11640 1082 echo "<P ALIGN=CENTER><B>(".get_string("nodiscussions", "forum").")</B></P>";
3335f6fb 1083 return;
f93f848a 1084
3335f6fb 1085 }
1086
1087 if ((!$forum_numdiscussions) && ($forum_style == "plain") && (count($discussions) > $FORUM_MANY_DISCUSSIONS) ) {
1088 $forum_style = "header"; // Abbreviate display if it's going to be long.
1089 }
f93f848a 1090
3335f6fb 1091 $replies = forum_count_discussion_replies($forum->id);
f93f848a 1092
3335f6fb 1093 $discussioncount = 0;
f93f848a 1094
3335f6fb 1095 foreach ($discussions as $discussion) {
1096 $discussioncount++;
f93f848a 1097
3335f6fb 1098 if ($forum_numdiscussions && ($discussioncount > $forum_numdiscussions)) {
1099 echo "<P ALIGN=right><A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">";
1100 echo get_string("olderdiscussions", "forum")."</A> ...</P>";
1101 break;
1102 }
1103 if ($replies[$discussion->discussion]) {
1104 $discussion->replies = $replies[$discussion->discussion]->replies;
1105 } else {
1106 $discussion->replies = 0;
1107 }
1108 $ownpost = ($discussion->userid == $USER->id);
1109 switch ($forum_style) {
1110 case "minimal":
1111 echo "<P><FONT COLOR=#555555>".userdate($discussion->modified, "%e %b, %H:%M")." - $discussion->firstname</FONT>";
1112 echo "<BR>$discussion->subject ";
1113 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->discussion\">";
1114 echo get_string("more", "forum")."...</A>";
1115 echo "</P>\n";
1116 break;
1117 case "header":
1118 forum_print_post_header($discussion, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
1119 break;
1120 default:
1121 forum_print_post($discussion, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
1122 echo "<BR>\n";
1123 break;
f93f848a 1124 }
1125 }
f93f848a 1126}
1127
fe25fc9b 1128function forum_print_discussion($course, $forum, $discussion, $post, $mode) {
501cdbd8 1129
1130 global $USER;
1131
1132 $ownpost = ($USER->id == $post->user);
1133
11b0c469 1134 forum_print_post($post, $course->id, $ownpost, $reply=true, $link=false, $rate=false);
501cdbd8 1135
7a12aab4 1136 forum_print_mode_form($discussion->id, $mode);
501cdbd8 1137
9d1b97c5 1138 $ratingform = false;
fe25fc9b 1139 if ($forum->assessed && $USER->id) {
9d1b97c5 1140 $unrated = forum_count_unrated_posts($discussion->id, $USER->id);
1141 if ($unrated > 0) {
1142 $ratingform = true;
1143 }
1144 }
1145
1146 if ($ratingform) {
501cdbd8 1147 echo "<FORM NAME=form METHOD=POST ACTION=rate.php>";
1148 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
1149 }
1150
1151 switch ($mode) {
1152 case 1 : // Flat ascending
1153 case -1 : // Flat descending
1154 default:
1155 echo "<UL>";
75d10a02 1156 forum_print_posts_flat($post->discussion, $course->id, $mode, $forum->assessed);
501cdbd8 1157 echo "</UL>";
1158 break;
1159
1160 case 2 : // Threaded
75d10a02 1161 forum_print_posts_threaded($post->id, $course->id, 0, $forum->assessed);
501cdbd8 1162 break;
1163
1164 case 3 : // Nested
75d10a02 1165 forum_print_posts_nested($post->id, $course->id, $forum->assessed);
501cdbd8 1166 break;
1167 }
1168
9d1b97c5 1169 if ($ratingform) {
31d160d3 1170 echo "<CENTER><P ALIGN=center><INPUT TYPE=submit VALUE=\"".get_string("sendinratings", "forum")."\">";
61ee082f 1171 helpbutton("ratings", get_string("separateandconnected"), "forum");
31d160d3 1172 echo "</P></CENTER>";
501cdbd8 1173 echo "</FORM>";
1174 }
1175}
1176
11b0c469 1177function forum_print_posts_flat($discussion, $course, $direction, $assessed) {
501cdbd8 1178 global $USER;
1179
1180 $reply = true;
1181 $link = false;
1182
1183 if ($direction < 0) {
1184 $sort = "ORDER BY created DESC";
1185 } else {
1186 $sort = "ORDER BY created ASC";
1187 }
1188
1189 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
1190 FROM forum_posts p, user u
1191 WHERE p.discussion = $discussion AND p.parent > 0 AND p.user = u.id $sort")) {
1192
1193 foreach ($posts as $post) {
1194 $ownpost = ($USER->id == $post->user);
11b0c469 1195 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
501cdbd8 1196 }
1197 } else {
1198 return;
1199 }
1200}
1201
11b0c469 1202function forum_print_posts_threaded($parent, $course, $depth, $assessed) {
501cdbd8 1203 global $USER;
1204
1205 $reply = true;
1206 $link = false;
1207
1208 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
1209 FROM forum_posts p, user u
1210 WHERE p.parent = '$parent' AND p.user = u.id")) {
1211
1212 foreach ($posts as $post) {
1213
1214 echo "<UL>";
1215 if ($depth > 0) {
1216 $ownpost = ($USER->id == $post->user);
11b0c469 1217 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed); // link=true?
501cdbd8 1218 echo "<BR>";
1219 } else {
8c3c8481 1220 $by->name = "$post->firstname $post->lastname";
1221 $by->date = userdate($post->created);
1222 echo "<LI><P><FONT SIZE=-1><B><A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</A></B> ";
1223 print_string("bynameondate", "forum", $by);
1224 echo "</FONT></P></LI>";
501cdbd8 1225 }
1226
11b0c469 1227 forum_print_posts_threaded($post->id, $course, $depth-1, $assessed);
501cdbd8 1228 echo "</UL>\n";
1229 }
1230 } else {
1231 return;
1232 }
1233}
1234
11b0c469 1235function forum_print_posts_nested($parent, $course, $assessed) {
501cdbd8 1236 global $USER;
1237
1238 $reply = true;
1239 $link = false;
1240
1241 if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
1242 FROM forum_posts p, user u
1243 WHERE p.parent = $parent AND p.user = u.id
1244 ORDER BY p.created ASC ")) {
1245
1246 foreach ($posts as $post) {
1247
1248 $ownpost = ($USER->id == $post->user);
1249
1250 echo "<UL>";
11b0c469 1251 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
501cdbd8 1252 echo "<BR>";
11b0c469 1253 forum_print_posts_nested($post->id, $course, $assessed);
501cdbd8 1254 echo "</UL>\n";
1255 }
1256 } else {
1257 return;
1258 }
1259}
1260
1261function forum_set_display_mode($mode=0) {
5a83a0a8 1262 global $USER, $FORUM_DEFAULT_DISPLAY_MODE;
501cdbd8 1263
1264 if ($mode) {
1265 $USER->mode = $mode;
8223d271 1266 save_session("USER");
501cdbd8 1267 } else if (!$USER->mode) {
1268 $USER->mode = $FORUM_DEFAULT_DISPLAY_MODE;
8223d271 1269 save_session("USER");
501cdbd8 1270 }
1271}
f93f848a 1272
1273?>