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