Check for existence of directory as well to avoid PHP warnings
[moodle.git] / mod / forum / lib.php
CommitLineData
f93f848a 1<?PHP // $Id$
2
b0e3a925 3require_once("$CFG->dirroot/files/mimetypes.php");
7f6689e4 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
c585fa17 28define("FORUM_SHORT_POST", 300); // Less non-HTML characters than this is short
4d871a72 29
c585fa17 30define("FORUM_LONG_POST", 600); // More non-HTML characters than this is long
4d871a72 31
3c8a606d 32define("FORUM_MANY_DISCUSSIONS", 100);
e07635f4 33
34
caadf009 35/// STANDARD FUNCTIONS ///////////////////////////////////////////////////////////
36
37function forum_add_instance($forum) {
38// Given an object containing all the necessary data,
39// (defined by the form in mod.html) this function
40// will create a new instance and return the id number
41// of the new instance.
42
43 global $CFG;
44
45 $forum->timemodified = time();
46
47 if (! $forum->id = insert_record("forum", $forum)) {
48 return false;
49 }
50
51 if ($forum->type == "single") { // Create related discussion.
52
53 $discussion->course = $forum->course;
54 $discussion->forum = $forum->id;
55 $discussion->name = $forum->name;
56 $discussion->intro = $forum->intro;
57 $discussion->assessed = $forum->assessed;
58
59 if (! forum_add_discussion($discussion)) {
60 error("Could not add the discussion for this forum");
61 }
62 }
63 add_to_log($forum->course, "forum", "add", "index.php?f=$forum->id", "$forum->id");
64
65 return $forum->id;
66}
67
68
69function forum_update_instance($forum) {
70// Given an object containing all the necessary data,
71// (defined by the form in mod.html) this function
72// will update an existing instance with new data.
73
74 $forum->timemodified = time();
75 $forum->id = $forum->instance;
76
77 if ($forum->type == "single") { // Update related discussion and post.
78 if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
79 if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
80 notify("Warning! There is more than one discussion in this forum - using the most recent");
81 $discussion = array_pop($discussions);
82 } else {
83 error("Could not find the discussion in this forum");
84 }
85 }
86 if (! $post = get_record("forum_posts", "id", $discussion->firstpost)) {
87 error("Could not find the first post in this forum discussion");
88 }
89
90 $post->subject = $forum->name;
91 $post->message = $forum->intro;
92 $post->modified = $forum->timemodified;
93
94 if (! update_record("forum_posts", $post)) {
95 error("Could not update the first post");
96 }
97
98 $discussion->name = $forum->name;
99
100 if (! update_record("forum_discussions", $discussion)) {
101 error("Could not update the discussion");
102 }
103 }
104
105 if (update_record("forum", $forum)) {
106 add_to_log($forum->course, "forum", "update", "index.php?f=$forum->id", "$forum->id");
107 return true;
108 } else {
109 return false;
110 }
111}
112
113
114function forum_delete_instance($id) {
115// Given an ID of an instance of this module,
116// this function will permanently delete the instance
117// and any data that depends on it.
118
119 if (! $forum = get_record("forum", "id", "$id")) {
120 return false;
121 }
122
123 $result = true;
124
125 if ($discussions = get_records("forum_discussions", "forum", $forum->id)) {
126 foreach ($discussions as $discussion) {
127 if (! forum_delete_discussion($discussion)) {
128 $result = false;
129 }
130 }
131 }
132
133 if (! delete_records("forum_subscriptions", "forum", "$forum->id")) {
134 $result = false;
135 }
136
137 if (! delete_records("forum", "id", "$forum->id")) {
138 $result = false;
139 }
140
141 return $result;
142}
143
144
145function forum_cron () {
edffca15 146/// Function to be run periodically according to the moodle cron
147/// Finds all posts that have yet to be mailed out, and mails them
148/// out to all subscribers
caadf009 149
150 global $CFG, $USER;
151
152 $cutofftime = time() - $CFG->maxeditingtime;
153
1f48942e 154 if ($posts = forum_get_unmailed_posts($cutofftime)) {
caadf009 155
edffca15 156 /// Mark them all now as being mailed. It's unlikely but possible there
157 /// might be an error later so that a post is NOT actually mailed out,
158 /// but since mail isn't crucial, we can accept this risk. Doing it now
159 /// prevents the risk of duplicated mails, which is a worse problem.
160
161 foreach ($posts as $key => $post) {
162 if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) {
163 echo "Error marking post id post->id as being mailed. This post will not be mailed.\n";
164 unset($posts[$key]);
165 }
166 }
167
caadf009 168 $timenow = time();
169
170 foreach ($posts as $post) {
171
edffca15 172 echo "\n";
caadf009 173 print_string("processingpost", "forum", $post->id);
edffca15 174 echo "\n";
caadf009 175
ebc3bd2b 176 if (! $userfrom = get_record("user", "id", "$post->userid")) {
177 echo "Could not find user $post->userid\n";
caadf009 178 continue;
179 }
180
181 if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) {
182 echo "Could not find discussion $post->discussion\n";
183 continue;
184 }
185
186 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
187 echo "Could not find forum $discussion->forum\n";
188 continue;
189 }
190
191 if (! $course = get_record("course", "id", "$forum->course")) {
192 echo "Could not find course $forum->course\n";
193 continue;
194 }
195
196 if ($users = forum_subscribed_users($course, $forum)) {
197 $canunsubscribe = ! forum_is_forcesubscribed($forum->id);
198
199 $mailcount=0;
200 foreach ($users as $userto) {
edffca15 201 /// Override the language of get_string, so that mail is in correct language for the receiver.
202 $USER->lang = $userto->lang;
f690562f 203 $canreply = forum_user_can_post($forum, $userto);
caadf009 204
caadf009 205 $by->name = "$userfrom->firstname $userfrom->lastname";
d62413e8 206 $by->date = userdate($post->modified, "", $userto->timezone);
caadf009 207 $strbynameondate = get_string("bynameondate", "forum", $by);
208
209 $strforums = get_string("forums", "forum");
210
211 $postsubject = "$course->shortname: $post->subject";
212 $posttext = "$course->shortname -> $strforums -> $forum->name";
213
214 if ($discussion->name == $forum->name) {
215 $posttext .= "\n";
216 } else {
217 $posttext .= " -> $discussion->name\n";
218 }
219 $posttext .= "---------------------------------------------------------------------\n";
220 $posttext .= "$post->subject\n";
221 $posttext .= $strbynameondate."\n";
222 $posttext .= "---------------------------------------------------------------------\n";
d342c763 223 $posttext .= format_text_email($post->message, $post->format);
caadf009 224 $posttext .= "\n\n";
225 if ($post->attachment) {
226 $post->course = $course->id;
227 $post->forum = $forum->id;
228 $posttext .= forum_print_attachments($post, "text");
229 }
f690562f 230 if ($canreply) {
231 $posttext .= "---------------------------------------------------------------------\n";
232 $posttext .= get_string("postmailinfo", "forum", $course->shortname)."\n";
233 $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id\n";
234 }
caadf009 235 if ($canunsubscribe) {
236 $posttext .= "\n---------------------------------------------------------------------\n";
237 $posttext .= get_string("unsubscribe", "forum");
238 $posttext .= ": $CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\n";
239 }
240
241 if ($userto->mailformat == 1) { // HTML
edffca15 242 $posthtml = "<p><font face=\"sans-serif\">".
243 "<a target=\"_blank\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ".
244 "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</a> -> ".
245 "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name</a>";
caadf009 246 if ($discussion->name == $forum->name) {
edffca15 247 $posthtml .= "</font></p>";
caadf009 248 } else {
edffca15 249 $posthtml .= " -> <a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name</a></font></p>";
caadf009 250 }
f690562f 251 $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, $canreply, false, false);
caadf009 252
253 if ($canunsubscribe) {
edffca15 254 $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>";
caadf009 255 }
256
257 } else {
258 $posthtml = "";
259 }
260
261 if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) {
edffca15 262 echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email) .. not trying again.\n";
caadf009 263 } else {
264 $mailcount++;
265 }
266 }
caadf009 267
edffca15 268 echo ".... mailed to $mailcount users.\n";
caadf009 269 }
caadf009 270 }
271 }
272
273 return true;
274}
275
276function forum_user_outline($course, $user, $mod, $forum) {
277
1f48942e 278 if ($posts = forum_get_user_posts($forum->id, $user->id)) {
caadf009 279 $result->info = get_string("numposts", "forum", count($posts));
280
281 $lastpost = array_pop($posts);
282 $result->time = $lastpost->modified;
283 return $result;
284 }
285 return NULL;
286}
287
288
289function forum_user_complete($course, $user, $mod, $forum) {
290 global $CFG;
291
1f48942e 292 if ($posts = forum_get_user_posts($forum->id, $user->id)) {
caadf009 293 foreach ($posts as $post) {
294 if ($post->parent) {
295 $footer = "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">".
296 get_string("parentofthispost", "forum")."</A>";
297 } else {
298 $footer = "";
299 }
300
301 forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false, $rate=false, $footer);
302 }
303
304 } else {
305 echo "<P>".get_string("noposts", "forum")."</P>";
306 }
caadf009 307}
308
1b5910c4 309function forum_print_recent_activity($course, $isteacher, $timestart) {
cc3655a2 310/// Given a course and a date, prints a summary of all the new
311/// messages posted in the course since that date
312
3d891989 313 global $CFG;
caadf009 314
315 $heading = false;
316 $content = false;
317
1b5910c4 318 if (!$logs = get_records_select("log", "time > '$timestart' AND ".
319 "course = '$course->id' AND ".
320 "module = 'forum' AND ".
321 "action LIKE 'add %' ", "time ASC")){
322 return false;
323 }
324
dcde9f02 325 $strftimerecent = get_string("strftimerecent");
326
1b5910c4 327 foreach ($logs as $log) {
328 //Get post info, I'll need it later
329 $post = forum_get_post_from_log($log);
330
331 //Create a temp valid module structure (course,id)
332 $tempmod->course = $log->course;
333 $tempmod->id = $post->forum;
334 //Obtain the visible property from the instance
335 $modvisible = instance_is_visible($log->module,$tempmod);
336
337 //Only if the mod is visible
338 if ($modvisible) {
339 if ($post) {
340 $teacheronly = "";
341 if ($forum = get_record("forum", "id", $post->forum) ) {
342 if ($forum->type == "teacher") {
343 if ($isteacher) {
344 $teacheronly = "class=\"teacheronly\"";
ad08fdc2 345 } else {
1b5910c4 346 continue;
ad08fdc2 347 }
caadf009 348 }
349 }
1b5910c4 350 if (! $heading) {
351 print_headline(get_string("newforumposts", "forum").":");
352 $heading = true;
353 $content = true;
354 }
355 $date = userdate($post->modified, $strftimerecent);
356 echo "<p $teacheronly><font size=1>$date - $post->firstname $post->lastname<br>";
357 echo "\"<a href=\"$CFG->wwwroot/mod/forum/$log->url\">";
358 if ($log->action == "add discussion") {
359 echo "<b>$post->subject</b>";
360 } else {
361 echo "$post->subject";
362 }
363 echo "</a>\"</font></p>";
caadf009 364 }
365 }
366 }
1b5910c4 367
caadf009 368 return $content;
369}
370
371
10696dc1 372
373function forum_print_recent_instance_activity($forum, $timestart, $detail=false) {
374
375 global $CFG, $THEME;
376
377 if (!$posts = forum_get_recent_posts($timestart, $forum->id)) {
378 return false;
379 }
380
381 foreach ($posts as $post) {
382 echo '<table border="0" cellpadding="3" cellspacing="0" class="forumpost">';
383 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
384 print_user_picture($post->userid, $forum->course, $post->picture);
385 echo "</td>";
386
387 if ($post->parent) {
388 echo "<td nowrap bgcolor=\"$THEME->cellheading\" class=\"forumpostheader\" width=\"100%\">";
389 } else {
390 echo "<td nowrap bgcolor=\"$THEME->cellheading2\" class=\"forumpostheadertopic\" width=\"100%\">";
391 }
392 echo "<p>";
393 echo "<font size=3>";
394 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion#$post->id\">";
395 echo $post->subject;
396 echo "</a></font><br>";
397 echo "<font size=2>";
398 $by->name = "<a href=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$post->firstname $post->lastname</a>";
399 $by->date = userdate($post->modified);
400 print_string("bynameondate", "forum", $by);
401 echo "</font></p></td></tr></table>";
402 }
403}
404
405
caadf009 406function forum_grades($forumid) {
407/// Must return an array of grades, indexed by user, and a max grade.
caadf009 408
4db9d14d 409 if (!$forum = get_record("forum", "id", $forumid)) {
410 return false;
411 }
412 if (!$forum->assessed) {
413 return false;
414 }
02ebf404 415 if (!$scale = get_record("scale", "id", $forum->scale)) {
416 return false;
417 }
418 $scalemenu = make_menu_from_list($scale->scale);
419
420 $currentuser = 0;
421 $ratingsuser = array();
422
1f48942e 423 if ($ratings = forum_get_user_grades($forumid)) {
02ebf404 424 foreach ($ratings as $rating) { // Ordered by user
425 if ($currentuser and $rating->userid != $currentuser) {
426 if (!empty($ratingsuser)) {
427 $return->grades[$currentuser] = forum_get_ratings_median(0, $scalemenu, $ratingsuser);
428 $return->grades[$currentuser] .= "<br />".forum_get_ratings_summary(0, $scalemenu, $ratingsuser);
429 } else {
430 $return->grades[$currentuser] = "";
431 }
432 $ratingsuser = array();
caadf009 433 }
02ebf404 434 $ratingsuser[] = $rating->rating;
435 $currentuser = $rating->userid;
caadf009 436 }
02ebf404 437 if (!empty($ratingsuser)) {
438 $return->grades[$currentuser] = forum_get_ratings_median(0, $scalemenu, $ratingsuser);
439 $return->grades[$currentuser] .= "<br />".forum_get_ratings_summary(0, $scalemenu, $ratingsuser);
440 } else {
441 $return->grades[$currentuser] = "";
caadf009 442 }
443 } else {
444 $return->grades = array();
445 }
446
447 $return->maxgrade = "";
448 return $return;
449}
450
451
9fa49e22 452/// SQL FUNCTIONS ///////////////////////////////////////////////////////////
453
1f48942e 454function forum_get_post_full($postid) {
455/// Gets a post with all info ready for forum_print_post
456 global $CFG;
457
ebc3bd2b 458 return get_record_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 459 FROM {$CFG->prefix}forum_posts p,
460 {$CFG->prefix}user u
461 WHERE p.id = '$postid'
ebc3bd2b 462 AND p.userid = u.id");
1f48942e 463}
464
465function forum_get_discussion_posts($discussion, $sort) {
466/// Gets posts with all info ready for forum_print_post
467 global $CFG;
468
ebc3bd2b 469 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 470 FROM {$CFG->prefix}forum_posts p,
471 {$CFG->prefix}user u
472 WHERE p.discussion = $discussion
473 AND p.parent > 0
ebc3bd2b 474 AND p.userid = u.id $sort");
1f48942e 475}
476
477function forum_get_child_posts($parent) {
478/// Gets posts with all info ready for forum_print_post
479 global $CFG;
480
ebc3bd2b 481 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 482 FROM {$CFG->prefix}forum_posts p,
483 {$CFG->prefix}user u
484 WHERE p.parent = '$parent'
ebc3bd2b 485 AND p.userid = u.id
1f48942e 486 ORDER BY p.created ASC");
487}
488
489
a74f950c 490function forum_search_posts($search, $courseid, $page=0, $recordsperpage=50) {
9fa49e22 491/// Returns a list of posts that were found
492 global $CFG;
493
494 if (!isteacher($courseid)) {
495 $notteacherforum = "AND f.type <> 'teacher'";
496 } else {
497 $notteacherforum = "";
498 }
499
c2a96d6b 500 switch ($CFG->dbtype) {
501 case "mysql":
502 $limit = "LIMIT $page,$recordsperpage";
503 break;
504 case "postgres7":
505 $limit = "LIMIT $recordsperpage OFFSET ".($page * $recordsperpage);
506 break;
507 default:
508 $limit = "LIMIT $recordsperpage,$page";
97485d07 509 }
510
63d99fcb 511 /// Some differences in syntax for PostgreSQL
512 if ($CFG->dbtype == "postgres7") {
a74f950c 513 $LIKE = "ILIKE"; // case-insensitive
2b4c64e9 514 $NOTLIKE = "NOT ILIKE"; // case-insensitive
515 $REGEXP = "~*";
516 $NOTREGEXP = "!~*";
b800ac5a 517 } else {
a74f950c 518 $LIKE = "LIKE";
2b4c64e9 519 $NOTLIKE = "NOT LIKE";
a74f950c 520 $REGEXP = "REGEXP";
2b4c64e9 521 $NOTREGEXP = "NOT REGEXP";
c4c57597 522 }
523
b800ac5a 524 $messagesearch = "";
525 $subjectsearch = "";
526
527 $searchterms = explode(" ", $search); // Search for words independently
528
529 foreach ($searchterms as $searchterm) {
530 if ($messagesearch) {
531 $messagesearch .= " AND ";
532 }
b800ac5a 533 if ($subjectsearch) {
534 $subjectsearch .= " AND ";
535 }
63d99fcb 536
a74f950c 537 if (substr($searchterm,0,1) == "+") {
538 $searchterm = substr($searchterm,1);
63d99fcb 539 $messagesearch .= " p.message $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
540 $subjectsearch .= " p.subject $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
a74f950c 541 } else if (substr($searchterm,0,1) == "-") {
542 $searchterm = substr($searchterm,1);
2b4c64e9 543 $messagesearch .= " p.message $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
544 $subjectsearch .= " p.subject $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
63d99fcb 545 } else {
546 $messagesearch .= " p.message $LIKE '%$searchterm%' ";
547 $subjectsearch .= " p.subject $LIKE '%$searchterm%' ";
548 }
b800ac5a 549 }
550
551
ebc3bd2b 552 return get_records_sql("SELECT p.*,u.firstname,u.lastname,u.email,u.picture
9fa49e22 553 FROM {$CFG->prefix}forum_posts p,
554 {$CFG->prefix}forum_discussions d,
555 {$CFG->prefix}user u,
556 {$CFG->prefix}forum f
b800ac5a 557 WHERE ($messagesearch OR $subjectsearch)
ebc3bd2b 558 AND p.userid = u.id
9fa49e22 559 AND p.discussion = d.id
560 AND d.course = '$courseid'
561 AND d.forum = f.id
562 $notteacherforum
97485d07 563 ORDER BY p.modified DESC $limit");
9fa49e22 564}
565
63d99fcb 566
9fa49e22 567function forum_get_ratings($postid, $sort="u.firstname ASC") {
568/// Returns a list of ratings for a particular post - sorted.
569 global $CFG;
570 return get_records_sql("SELECT u.*, r.rating, r.time
571 FROM {$CFG->prefix}forum_ratings r,
572 {$CFG->prefix}user u
1f48942e 573 WHERE r.post = '$postid'
ebc3bd2b 574 AND r.userid = u.id
9fa49e22 575 ORDER BY $sort");
576}
577
1f48942e 578function forum_get_unmailed_posts($cutofftime) {
579/// Returns a list of all new posts that have not been mailed yet
580 global $CFG;
581 return get_records_sql("SELECT p.*, d.course
582 FROM {$CFG->prefix}forum_posts p,
583 {$CFG->prefix}forum_discussions d
584 WHERE p.mailed = 0
585 AND p.created < '$cutofftime'
586 AND p.discussion = d.id");
587}
588
589function forum_get_user_posts($forumid, $userid) {
590/// Get all the posts for a user in a forum suitable for forum_print_post
591 global $CFG;
592
ebc3bd2b 593 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 594 FROM {$CFG->prefix}forum f,
595 {$CFG->prefix}forum_discussions d,
596 {$CFG->prefix}forum_posts p,
597 {$CFG->prefix}user u
598 WHERE f.id = '$forumid'
599 AND d.forum = f.id
600 AND p.discussion = d.id
ebc3bd2b 601 AND p.userid = '$userid'
602 AND p.userid = u.id
1f48942e 603 ORDER BY p.modified ASC");
604}
605
606function forum_get_post_from_log($log) {
607/// Given a log entry, return the forum post details for it.
608 global $CFG;
609
610 if ($log->action == "add post") {
611
ebc3bd2b 612 return get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname, u.email, u.picture
1f48942e 613 FROM {$CFG->prefix}forum_discussions d,
614 {$CFG->prefix}forum_posts p,
615 {$CFG->prefix}user u
616 WHERE p.id = '$log->info'
617 AND d.id = p.discussion
ebc3bd2b 618 AND p.userid = u.id
1f48942e 619 AND u.deleted <> '1'");
620
621
622 } else if ($log->action == "add discussion") {
623
ebc3bd2b 624 return get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname, u.email, u.picture
1f48942e 625 FROM {$CFG->prefix}forum_discussions d,
626 {$CFG->prefix}forum_posts p,
627 {$CFG->prefix}user u
628 WHERE d.id = '$log->info'
629 AND d.firstpost = p.id
ebc3bd2b 630 AND p.userid = u.id
1f48942e 631 AND u.deleted <> '1'");
632 }
633 return NULL;
634}
635
636
637function forum_get_user_grades($forumid) {
638/// Get all user grades for a forum
639 global $CFG;
640
ebc3bd2b 641 return get_records_sql("SELECT r.id, p.userid, r.rating
1f48942e 642 FROM {$CFG->prefix}forum_discussions d,
643 {$CFG->prefix}forum_posts p,
644 {$CFG->prefix}forum_ratings r
645 WHERE d.forum = '$forumid'
646 AND p.discussion = d.id
02ebf404 647 AND r.post = p.id
648 ORDER by p.userid ");
1f48942e 649}
650
651
652function forum_count_discussion_replies($forum="0") {
653// Returns an array of counts of replies to each discussion (optionally in one forum)
654 global $CFG;
655
656 if ($forum) {
657 $forumselect = " AND d.forum = '$forum'";
658 }
659 return get_records_sql("SELECT p.discussion, (count(*)) as replies
660 FROM {$CFG->prefix}forum_posts p,
661 {$CFG->prefix}forum_discussions d
662 WHERE p.parent > 0
663 AND p.discussion = d.id
664 GROUP BY p.discussion");
665}
666
667function forum_count_unrated_posts($discussionid, $userid) {
668// How many unrated posts are in the given discussion for a given user?
669 global $CFG;
670 if ($posts = get_record_sql("SELECT count(*) as num
671 FROM {$CFG->prefix}forum_posts
672 WHERE parent > 0
673 AND discussion = '$discussionid'
ebc3bd2b 674 AND userid <> '$userid' ")) {
1f48942e 675
676 if ($rated = get_record_sql("SELECT count(*) as num
677 FROM {$CFG->prefix}forum_posts p,
678 {$CFG->prefix}forum_ratings r
679 WHERE p.discussion = '$discussionid'
680 AND p.id = r.post
ebc3bd2b 681 AND r.userid = '$userid'")) {
1f48942e 682 $difference = $posts->num - $rated->num;
683 if ($difference > 0) {
684 return $difference;
685 } else {
686 return 0; // Just in case there was a counting error
687 }
688 } else {
689 return $posts->num;
690 }
691 } else {
692 return 0;
693 }
694}
695
2ab968e9 696function forum_get_discussions($forum="0", $forumsort="d.timemodified DESC", $user=0, $fullpost=true) {
1f48942e 697/// Get all discussions in a forum
698 global $CFG;
699
700 if ($user) {
701 $userselect = " AND u.id = '$user' ";
702 } else {
703 $userselect = "";
704 }
29507631 705 if (empty($forumsort)) {
706 $forumsort = "d.timemodified DESC";
707 }
2ab968e9 708 if (empty($fullpost)) {
b879effb 709 $postdata = "p.id,p.subject,p.modified,p.discussion,p.userid";
2ab968e9 710 } else {
711 $postdata = "p.*";
712 }
713 return get_records_sql("SELECT $postdata, d.timemodified, u.firstname, u.lastname, u.email, u.picture
1f48942e 714 FROM {$CFG->prefix}forum_discussions d,
715 {$CFG->prefix}forum_posts p,
716 {$CFG->prefix}user u
717 WHERE d.forum = '$forum'
718 AND p.discussion = d.id
2ab968e9 719 AND p.parent = 0
ebc3bd2b 720 AND p.userid = u.id $userselect
29507631 721 ORDER BY $forumsort");
1f48942e 722}
723
724
725
726function forum_get_user_discussions($courseid, $userid) {
727/// Get all discussions started by a particular user in a course
728 global $CFG;
729
730 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture,
ebc3bd2b 731 f.type as forumtype, f.name as forumname, f.id as forumid
1f48942e 732 FROM {$CFG->prefix}forum_discussions d,
733 {$CFG->prefix}forum_posts p,
734 {$CFG->prefix}user u,
735 {$CFG->prefix}forum f
736 WHERE d.course = '$courseid'
737 AND p.discussion = d.id
738 AND p.parent = 0
ebc3bd2b 739 AND p.userid = u.id
1f48942e 740 AND u.id = '$userid'
741 AND d.forum = f.id
b8bf90c5 742 ORDER BY p.created DESC");
1f48942e 743}
744
745
746function forum_subscribed_users($course, $forum) {
747/// Returns list of user objects that are subscribed to this forum
748 global $CFG;
749
adaf3928 750 if ($forum->forcesubscribe) {
751 if ($course->category) {
170a9c46 752 if ($forum->type == "teacher") {
753 return get_course_teachers($course->id); // Only teachers can be subscribed to teacher forums
754 } else {
755 return get_course_users($course->id); // Otherwise get everyone in the course
756 }
adaf3928 757 } else {
2d0b30a0 758 return get_site_users();
1f48942e 759 }
760 }
713386aa 761 return get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat,
0351b1f9 762 u.email, u.city, u.country, u.lastaccess, u.lastlogin, u.picture, u.timezone, u.lang
1f48942e 763 FROM {$CFG->prefix}user u,
764 {$CFG->prefix}forum_subscriptions s
765 WHERE s.forum = '$forum->id'
ebc3bd2b 766 AND s.userid = u.id
7c81b6e2 767 AND u.deleted <> 1
768 ORDER BY u.email ASC");
1f48942e 769}
9fa49e22 770
10696dc1 771function forum_get_recent_posts($sincetime, $forum="0") {
772// Returns all forum posts since a given time. If forum is specified then
773// this restricts the results
774
775 global $CFG;
776
777 if ($forum) {
778 $forumselect = " AND d.forum = '$forum'";
779 } else {
780 $forumselect = "";
781 }
782
9400ee33 783 return get_records_sql("SELECT p.*, d.name, u.id, u.firstname, u.lastname, u.picture
10696dc1 784 FROM {$CFG->prefix}forum_posts p,
785 {$CFG->prefix}forum_discussions d,
786 {$CFG->prefix}user u
787 WHERE p.modified > '$sincetime' $forumselect
788 AND p.userid = u.id
789 AND p.discussion = d.id
790 ORDER BY p.discussion ASC");
791}
792
9fa49e22 793
caadf009 794/// OTHER FUNCTIONS ///////////////////////////////////////////////////////////
f93f848a 795
796
11b0c469 797function forum_get_course_forum($courseid, $type) {
798// How to set up special 1-per-course forums
a6fcdf98 799 global $CFG;
800
29cbd93a 801 if ($forums = get_records_select("forum", "course = '$courseid' AND type = '$type'", "id ASC")) {
802 // There should always only be ONE, but with the right combination of
803 // errors there might be more. In this case, just return the oldest one (lowest ID).
804 foreach ($forums as $forum) {
805 return $forum; // ie the first one
11b0c469 806 }
8daaf761 807 }
e6874d9f 808
8daaf761 809 // Doesn't exist, so create one now.
810 $forum->course = $courseid;
811 $forum->type = "$type";
812 switch ($forum->type) {
813 case "news":
814 $forum->name = get_string("namenews", "forum");
815 $forum->forcesubscribe = 1;
816 $forum->intro = get_string("intronews", "forum");
817 $forum->open = 1; // 0 - no, 1 - posts only, 2 - discuss and post
818 $forum->assessed = 0;
819 if ($site = get_site()) {
820 if ($courseid == $site->id) {
821 $forum->name = get_string("sitenews");
822 $forum->forcesubscribe = 0;
823 }
a6fcdf98 824 }
8daaf761 825 break;
826 case "social":
827 $forum->name = get_string("namesocial", "forum");
828 $forum->intro = get_string("introsocial", "forum");
829 $forum->open = 2; // 0 - no, 1 - posts only, 2 - discuss and post
830 $forum->assessed = 0;
831 $forum->forcesubscribe = 0;
832 break;
833 case "teacher":
834 $forum->name = get_string("nameteacher", "forum");
835 $forum->intro = get_string("introteacher", "forum");
836 $forum->open = 0; // 0 - no, 1 - posts only, 2 - discuss and post
837 $forum->assessed = 0;
838 $forum->forcesubscribe = 0;
839 break;
840 default:
841 notify("That forum type doesn't exist!");
842 return false;
843 break;
844 }
845
846 $forum->timemodified = time();
847 $forum->id = insert_record("forum", $forum);
848
849 if ($forum->type != "teacher") {
850 if (! $module = get_record("modules", "name", "forum")) {
851 notify("Could not find forum module!!");
852 return false;
853 }
854 $mod->course = $courseid;
855 $mod->module = $module->id;
856 $mod->instance = $forum->id;
857 $mod->section = 0;
858 if (! $mod->coursemodule = add_course_module($mod) ) { // assumes course/lib.php is loaded
859 notify("Could not add a new course module to the course '$course->fullname'");
860 return false;
861 }
862 if (! $sectionid = add_mod_to_section($mod) ) { // assumes course/lib.php is loaded
863 notify("Could not add the new course module to that section");
864 return false;
865 }
866 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
867 notify("Could not update the course module with the correct section");
868 return false;
869 }
870 include_once("$CFG->dirroot/course/lib.php");
d769d2ee 871 rebuild_course_cache($courseid);
82aa0e8d 872 }
8daaf761 873
874 return get_record("forum", "id", "$forum->id");
82aa0e8d 875}
876
f93f848a 877
11b0c469 878function forum_make_mail_post(&$post, $user, $touser, $course,
879 $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
501cdbd8 880// Given the data about a posting, builds up the HTML to display it and
881// returns the HTML in a string. This is designed for sending via HTML email.
882
883 global $THEME, $CFG;
884
885 $output = "";
886
887 if ($post->parent) {
72d497d4 888 $output .= '<table border="0" cellpadding="1" cellspacing="1"><tr><td bgcolor="#888888">';
889 $output .= '<table border="0" cellpadding="3" cellspacing="0">';
501cdbd8 890 } else {
72d497d4 891 $output .= '<table border="0" cellpadding="1" cellspacing="1" width="100%"><tr><td bgcolor="#888888">';
892 $output .= '<table border="0" cellpadding="3" cellspacing="0" width="100%">';
501cdbd8 893 }
894
72d497d4 895 $output .= "<tr><td bgcolor=\"$THEME->cellcontent2\" width=\"35\" valign=\"top\">";
501cdbd8 896 $output .= print_user_picture($user->id, $course->id, $user->picture, false, true);
72d497d4 897 $output .= "</td>";
501cdbd8 898
899 if ($post->parent) {
72d497d4 900 $output .= "<td nowrap bgcolor=\"$THEME->cellheading\">";
501cdbd8 901 } else {
72d497d4 902 $output .= "<td nowrap bgcolor=\"$THEME->cellheading2\">";
501cdbd8 903 }
72d497d4 904 $output .= "<p>";
905 $output .= "<font size=3><b>$post->subject</b></font><br />";
906 $output .= "<font size=2>";
907 $by->name = "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</a>";
d62413e8 908 $by->date = userdate($post->modified, "", $touser->timezone);
ffe11640 909 $output .= get_string("bynameondate", "forum", $by);
72d497d4 910 $output .= "</font></p></td></tr>";
911 $output .= "<tr><td bgcolor=\"$THEME->cellcontent2\" width=10>";
501cdbd8 912 $output .= "&nbsp;";
72d497d4 913 $output .= "</td><td bgcolor=\"$THEME->cellcontent\">\n";
501cdbd8 914
7f6689e4 915 if ($post->attachment) {
916 $post->course = $course->id;
917 $post->forum = get_field("forum_discussions", "forum", "id", $post->discussion);
72d497d4 918 $output .= "<div align=right>";
7f6689e4 919 $output .= forum_print_attachments($post, "html");
72d497d4 920 $output .= "</div>";
7f6689e4 921 }
922
73bb0835 923 $output .= format_text($post->message, $post->format);
501cdbd8 924
72d497d4 925 $output .= "<p align=right><font size=-1>";
501cdbd8 926
927 $age = time() - $post->created;
928 if ($ownpost) {
72d497d4 929 $output .= "<a href=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."</a>";
501cdbd8 930 if ($reply) {
72d497d4 931 $output .= " | <a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</a>";
501cdbd8 932 }
933 $output .= "&nbsp;&nbsp;";
934 } else {
935 if ($reply) {
72d497d4 936 $output .= "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</a>&nbsp;&nbsp;";
501cdbd8 937 }
938 }
939
72d497d4 940 $output .= "</p>";
941 $output .= "<div align=right><p align=right>";
501cdbd8 942
943 if ($link) {
944 if ($post->replies == 1) {
ffe11640 945 $replystring = get_string("repliesone", "forum", $post->replies);
501cdbd8 946 } else {
ffe11640 947 $replystring = get_string("repliesmany", "forum", $post->replies);
501cdbd8 948 }
72d497d4 949 $output .= "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><B>".get_string("discussthistopic", "forum")."</b></a> ($replystring)&nbsp;&nbsp;";
501cdbd8 950 }
72d497d4 951 $output .= "</p></div>";
501cdbd8 952 if ($footer) {
72d497d4 953 $output .= "<p>$footer</p>";
501cdbd8 954 }
72d497d4 955 $output .= "</td></tr></table>\n";
956 $output .= "</td></tr></table>\n\n";
501cdbd8 957
958 return $output;
959}
960
961
02ebf404 962function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link=false, $ratings=NULL, $footer="", $highlight="") {
c585fa17 963 global $THEME, $USER, $CFG;
501cdbd8 964
b22b0e61 965 echo "<a name=\"$post->id\"></a>";
501cdbd8 966 if ($post->parent) {
72d497d4 967 echo '<table border="0" cellpadding="3" cellspacing="0" class="forumpost">';
501cdbd8 968 } else {
72d497d4 969 echo '<table border="0" cellpadding="3" cellspacing="0" class="forumpost" width="100%">';
501cdbd8 970 }
971
72d497d4 972 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
501cdbd8 973 print_user_picture($post->userid, $courseid, $post->picture);
72d497d4 974 echo "</td>";
501cdbd8 975
976 if ($post->parent) {
72d497d4 977 echo "<td nowrap bgcolor=\"$THEME->cellheading\" class=\"forumpostheader\" width=\"100%\">";
501cdbd8 978 } else {
72d497d4 979 echo "<td nowrap bgcolor=\"$THEME->cellheading2\" class=\"forumpostheadertopic\" width=\"100%\">";
501cdbd8 980 }
72d497d4 981 echo "<p>";
982 echo "<font size=3><b>$post->subject</b></font><br>";
983 echo "<font size=2>";
984 $by->name = "<a href=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$post->firstname $post->lastname</a>";
d62413e8 985 $by->date = userdate($post->modified);
ffe11640 986 print_string("bynameondate", "forum", $by);
72d497d4 987 echo "</font></p></td></tr>";
988 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostside\" width=\"10\">";
501cdbd8 989 echo "&nbsp;";
72d497d4 990 echo "</td><td bgcolor=\"$THEME->cellcontent\" class=\"forumpostmessage\">\n";
501cdbd8 991
7f6689e4 992 if ($post->attachment) {
993 $post->course = $courseid;
994 $post->forum = get_field("forum_discussions", "forum", "id", $post->discussion);
72d497d4 995 echo "<div align=\"right\">";
996 $attachedimages = forum_print_attachments($post);
997 echo "</div>";
e9c2dc1f 998 } else {
999 $attachedimages = "";
7f6689e4 1000 }
1001
c585fa17 1002 if ($link and (strlen(strip_tags($post->message)) > FORUM_LONG_POST)) {
aa153f29 1003 // Print shortened version
73bb0835 1004 echo format_text(forum_shorten_post($post->message), $post->format);
c585fa17 1005 $numwords = count_words(strip_tags($post->message));
72d497d4 1006 echo "<p><a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">";
ffe11640 1007 echo get_string("readtherest", "forum");
72d497d4 1008 echo "</a> (".get_string("numwords", "", $numwords).")...</p>";
501cdbd8 1009 } else {
aa153f29 1010 // Print whole message
88438a58 1011 if ($highlight) {
1012 echo highlight($highlight, format_text($post->message, $post->format));
1013 } else {
1014 echo format_text($post->message, $post->format);
1015 }
c99ce77b 1016 echo $attachedimages;
501cdbd8 1017 }
1018
72d497d4 1019 echo "<p align=right><font size=-1>";
501cdbd8 1020
1021 $age = time() - $post->created;
1022 if ($ownpost) {
1023 if ($age < $CFG->maxeditingtime) {
72d497d4 1024 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?edit=$post->id\">".get_string("edit", "forum")."</a> | ";
501cdbd8 1025 }
64eacd6f 1026 }
1027 if ($ownpost or isteacher($courseid)) {
72d497d4 1028 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."</a>";
501cdbd8 1029 if ($reply) {
64eacd6f 1030 echo "| ";
1031 } else {
1032 echo "&nbsp;&nbsp;";
501cdbd8 1033 }
64eacd6f 1034 }
1035 if ($reply) {
72d497d4 1036 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</a>";
501cdbd8 1037 echo "&nbsp;&nbsp;";
501cdbd8 1038 }
72d497d4 1039 echo "</p>";
501cdbd8 1040
72d497d4 1041 echo "<div align=right><p align=right>";
02ebf404 1042 if (!empty($ratings) and !empty($USER->id)) {
2a3cda19 1043 if (isteacher($courseid)) {
02ebf404 1044 forum_print_ratings_median($post->id, $ratings->scale);
2a3cda19 1045 if ($USER->id != $post->userid) {
02ebf404 1046 forum_print_rating_menu($post->id, $USER->id, $ratings->scale);
2a3cda19 1047 }
1048 } else if ($USER->id == $post->userid) {
02ebf404 1049 forum_print_ratings_median($post->id, $ratings->scale);
1050 } else if (!empty($ratings->allow) ) {
1051 forum_print_rating_menu($post->id, $USER->id, $ratings->scale);
501cdbd8 1052 }
1053 }
1054
1055 if ($link) {
1056 if ($post->replies == 1) {
ffe11640 1057 $replystring = get_string("repliesone", "forum", $post->replies);
501cdbd8 1058 } else {
ffe11640 1059 $replystring = get_string("repliesmany", "forum", $post->replies);
501cdbd8 1060 }
72d497d4 1061 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><b>".get_string("discussthistopic", "forum")."</b></a> ($replystring)&nbsp;&nbsp;";
501cdbd8 1062 }
72d497d4 1063 echo "</p>";
501cdbd8 1064 if ($footer) {
72d497d4 1065 echo "<p>$footer</p>";
501cdbd8 1066 }
72d497d4 1067 echo "</div>";
1068 echo "</td></tr>\n</table>\n\n";
501cdbd8 1069}
1070
3335f6fb 1071
2ab968e9 1072function forum_print_discussion_header(&$post, $courseid, $datestring="") {
c585fa17 1073 global $THEME, $USER, $CFG;
3335f6fb 1074
29507631 1075 echo "<tr class=\"forumpostheader\">";
3335f6fb 1076
29507631 1077 // Topic
546be6dd 1078 echo "<td bgcolor=\"$THEME->cellheading2\" class=\"forumpostheadertopic\" width=\"100%\">";
29507631 1079 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">$post->subject</a>";
1080 echo "</td>\n";
1081
1082 // Picture
96e301a7 1083 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderpicture\" width=35>";
3335f6fb 1084 print_user_picture($post->userid, $courseid, $post->picture);
29507631 1085 echo "</td>\n";
3335f6fb 1086
29507631 1087 // User name
96e301a7 1088 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheadername\" align=left nowrap>";
29507631 1089 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$post->firstname $post->lastname</a>";
1090 echo "</td>\n";
1091
1092 // Replies
96e301a7 1093 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderreplies\" align=center nowrap>";
2ab968e9 1094 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">$post->replies</a>";
29507631 1095 echo "</td>\n";
3335f6fb 1096
96e301a7 1097 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderdate\" align=right nowrap>";
29507631 1098 if (!empty($post->timemodified)) {
2ab968e9 1099 echo userdate($post->timemodified, $datestring);
3335f6fb 1100 } else {
2ab968e9 1101 echo userdate($post->modified, $datestring);
3335f6fb 1102 }
29507631 1103 echo "</td>\n";
1104
1105 echo "</tr>\n";
3335f6fb 1106
3335f6fb 1107}
1108
1109
aa153f29 1110function forum_shorten_post($message) {
c585fa17 1111// Given a post object that we already know has a long message
1112// this function truncates the message nicely to the first
1113// sane place between FORUM_LONG_POST and FORUM_SHORT_POST
1114
1115 $i = 0;
1116 $tag = false;
1117 $length = strlen($message);
1118 $count = 0;
1119 $stopzone = false;
1120 $truncate = 0;
1121
1122 for ($i=0; $i<$length; $i++) {
a8afb411 1123 $char = $message[$i];
c585fa17 1124
1125 switch ($char) {
1126 case "<":
1127 $tag = true;
1128 break;
1129 case ">":
1130 $tag = false;
1131 break;
1132 default:
1133 if (!$tag) {
1134 if ($stopzone) {
67f0b4cc 1135 if ($char == ".") {
a8afb411 1136 $truncate = $i+1;
c585fa17 1137 break 2;
1138 }
1139 }
1140 $count++;
1141 }
a8afb411 1142 break;
c585fa17 1143 }
1144 if (!$stopzone) {
1145 if ($count > FORUM_SHORT_POST) {
1146 $stopzone = true;
1147 }
1148 }
1149 }
aa153f29 1150
c585fa17 1151 if (!$truncate) {
a8afb411 1152 $truncate = $i;
c585fa17 1153 }
1154
67f0b4cc 1155 return substr($message, 0, $truncate);
aa153f29 1156}
1157
501cdbd8 1158
02ebf404 1159function forum_print_ratings_median($postid, $scale) {
1160/// Print the multiple ratings on a post given to the current user by others.
1161/// Scale is an array of ratings
1162
1163 static $strrate;
1164
1165 if ($median = forum_get_ratings_median($postid, $scale)) {
1166
1167 if (empty($strratings)) {
1168 $strratings = get_string("ratings", "forum");
501cdbd8 1169 }
501cdbd8 1170
02ebf404 1171 echo "$strratings: ";
1172 link_to_popup_window ("/mod/forum/report.php?id=$postid", "ratings", $median, 400, 600);
501cdbd8 1173 }
1174}
1175
501cdbd8 1176
02ebf404 1177function forum_get_ratings_median($postid, $scale, $ratings=NULL) {
1178/// Return the median rating of a post given to the current user by others.
1179/// Scale is an array of possible ratings in the scale
1180/// Ratings is an optional simple array of actual ratings (just integers)
1181
1182 if (!$ratings) {
1183 $ratings = array();
1184 if ($rates = get_records("forum_ratings", "post", $postid)) {
1185 foreach ($rates as $rate) {
1186 $ratings[] = $rate->rating;
1187 }
501cdbd8 1188 }
501cdbd8 1189 }
02ebf404 1190
1191 if (!$count = count($ratings)) {
1192 return "";
1193 }
1194
1195 sort($ratings, SORT_NUMERIC);
1196
1197 if ($count == 1) {
1198 return $scale[$ratings[0]]." (1)";
1199 } else {
1200 $median = $ratings[ceil($count/2)];
1201 return $scale[$median]." ($count)";
1202 }
1203}
1204
1205function forum_get_ratings_summary($postid, $scale, $ratings=NULL) {
1206/// Return a summary of post ratings given to the current user by others.
1207/// Scale is an array of possible ratings in the scale
1208/// Ratings is an optional simple array of actual ratings (just integers)
1209
1210 if (!$ratings) {
1211 $ratings = array();
1212 if ($rates = get_records("forum_ratings", "post", $postid)) {
1213 foreach ($rates as $rate) {
1214 $rating[] = $rate->rating;
1215 }
1216 }
1217 }
1218
1219
1220 if (!$count = count($ratings)) {
1221 return "";
1222 }
1223
1224
1225 foreach ($scale as $key => $scaleitem) {
1226 $sumrating[$key] = 0;
1227 }
1228
1229 foreach ($ratings as $rating) {
1230 $sumrating[$rating]++;
1231 }
1232
1233 $summary = "";
1234 foreach ($scale as $key => $scaleitem) {
1235 $summary = $sumrating[$key].$summary;
1236 if ($key > 1) {
1237 $summary = "/$summary";
1238 }
1239 }
1240 return $summary;
1241}
1242
1243function forum_print_rating_menu($postid, $userid, $scale) {
1244/// Print the menu of ratings as part of a larger form.
1245/// If the post has already been - set that value.
1246/// Scale is an array of ratings
1247
1248 static $strrate;
1249
1250 if (!$rating = get_record("forum_ratings", "userid", $userid, "post", $postid)) {
1251 $rating->rating = 0;
1252 }
1253
1254 if (empty($strrate)) {
1255 $strrate = get_string("rate", "forum");
1256 }
1257
1258 choose_from_menu($scale, $postid, $rating->rating, "$strrate...");
501cdbd8 1259}
1260
7a12aab4 1261function forum_print_mode_form($discussion, $mode) {
1262 GLOBAL $FORUM_LAYOUT_MODES;
501cdbd8 1263
1264 echo "<CENTER><P>";
7a12aab4 1265 popup_form("discuss.php?d=$discussion&mode=", $FORUM_LAYOUT_MODES, "mode", $mode, "");
501cdbd8 1266 echo "</P></CENTER>\n";
1267}
1268
97485d07 1269function forum_print_search_form($course, $search="", $return=false, $type="") {
501cdbd8 1270 global $CFG;
1271
97485d07 1272 if ($type == "plain") {
1273 $output = "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0><TR><TD NOWRAP>";
1274 $output .= "<FORM NAME=search ACTION=\"$CFG->wwwroot/mod/forum/search.php\">";
1275 $output .= "<FONT SIZE=\"-1\">";
1276 $output .= "<INPUT NAME=search TYPE=text SIZE=15 VALUE=\"$search\">";
1277 $output .= "<INPUT VALUE=\"".get_string("searchforums", "forum")."\" TYPE=submit>";
1278 $output .= "</FONT>";
1279 $output .= "<INPUT NAME=id TYPE=hidden VALUE=\"$course->id\">";
1280 $output .= "</FORM>";
1281 $output .= "</TD></TR></TABLE>";
1282 } else {
1283 $output = "<TABLE BORDER=0 CELLPADDING=10 CELLSPACING=0><TR><TD ALIGN=CENTER>";
1284 $output .= "<FORM NAME=search ACTION=\"$CFG->wwwroot/mod/forum/search.php\">";
1285 $output .= "<FONT SIZE=\"-1\">";
1286 $output .= "<INPUT NAME=search TYPE=text SIZE=15 VALUE=\"$search\"><BR>";
1287 $output .= "<INPUT VALUE=\"".get_string("searchforums", "forum")."\" TYPE=submit>";
1288 $output .= "</FONT>";
1289 $output .= "<INPUT NAME=id TYPE=hidden VALUE=\"$course->id\">";
1290 $output .= "</FORM>";
1291 $output .= "</TD></TR></TABLE>";
1292 }
5e367a2d 1293
1294 if ($return) {
1295 return $output;
1296 }
1297 echo $output;
501cdbd8 1298}
1299
1300
11b0c469 1301function forum_set_return() {
607809b3 1302 global $CFG, $SESSION;
501cdbd8 1303
28e1e8b9 1304 if (! isset($SESSION->fromdiscussion)) {
48d38fad 1305 if (!empty($_SERVER['HTTP_REFERER'])) {
1306 $referer = $_SERVER['HTTP_REFERER'];
1307 } else {
1308 $referer = "";
1309 }
28e1e8b9 1310 // If the referer is NOT a login screen then save it.
48d38fad 1311 if (! strncasecmp("$CFG->wwwroot/login", $referer, 300)) {
607809b3 1312 $SESSION->fromdiscussion = $_SERVER["HTTP_REFERER"];
28e1e8b9 1313 }
501cdbd8 1314 }
1315}
1316
1317
11b0c469 1318function forum_go_back_to($default) {
501cdbd8 1319 global $SESSION;
1320
9c9f7d77 1321 if (!empty($SESSION->fromdiscussion)) {
501cdbd8 1322 $returnto = $SESSION->fromdiscussion;
1323 unset($SESSION->fromdiscussion);
1324 return $returnto;
1325 } else {
1326 return $default;
1327 }
1328}
1329
7f6689e4 1330function forum_file_area_name($post) {
1331// Creates a directory file name, suitable for make_upload_directory()
1332 global $CFG;
1333
1334 return "$post->course/$CFG->moddata/forum/$post->forum/$post->id";
1335}
1336
1337function forum_file_area($post) {
1338 return make_upload_directory( forum_file_area_name($post) );
1339}
1340
1341function forum_delete_old_attachments($post, $exception="") {
1342// Deletes all the user files in the attachments area for a post
1343// EXCEPT for any file named $exception
1344
1345 if ($basedir = forum_file_area($post)) {
1346 if ($files = get_directory_list($basedir)) {
1347 foreach ($files as $file) {
1348 if ($file != $exception) {
1349 unlink("$basedir/$file");
1350 notify("Existing file '$file' has been deleted!");
1351 }
1352 }
1353 }
1354 if (!$exception) { // Delete directory as well, if empty
1355 rmdir("$basedir");
1356 }
1357 }
1358}
1359
1360function forum_print_attachments($post, $return=NULL) {
1361// if return=html, then return a html string.
1362// if return=text, then return a text-only string.
72d497d4 1363// otherwise, print HTML for non-images, and return image HTML
7f6689e4 1364
1365 global $CFG;
1366
1367 $filearea = forum_file_area_name($post);
1368
72d497d4 1369 $imagereturn = "";
1370 $output = "";
1371
7f6689e4 1372 if ($basedir = forum_file_area($post)) {
1373 if ($files = get_directory_list($basedir)) {
1374 $strattachment = get_string("attachment", "forum");
6afe8558 1375 $strpopupwindow = get_string("popupwindow");
7f6689e4 1376 foreach ($files as $file) {
1377 $icon = mimeinfo("icon", $file);
1378 if ($CFG->slasharguments) {
1379 $ffurl = "file.php/$filearea/$file";
1380 } else {
1381 $ffurl = "file.php?file=/$filearea/$file";
1382 }
6afe8558 1383 $image = "<img border=0 src=\"$CFG->wwwroot/files/pix/$icon\" height=16 width=16 alt=\"$strpopupwindow\">";
7f6689e4 1384
1385 if ($return == "html") {
6afe8558 1386 $output .= "<a href=\"$CFG->wwwroot/$ffurl\">$image</a> ";
1387 $output .= "<a href=\"$CFG->wwwroot/$ffurl\">$file</a><br />";
7f6689e4 1388
1389 } else if ($return == "text") {
1390 $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
1391
1392 } else {
72d497d4 1393 if ($icon == "image.gif") { // Image attachments don't get printed as links
1394 $imagereturn .= "<br /><img src=\"$CFG->wwwroot/$ffurl\">";
1395 } else {
1396 link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
1397 echo "<a href=\"$CFG->wwwroot/$ffurl\">$file</a>";
1398 echo "<br />";
1399 }
7f6689e4 1400 }
1401 }
1402 }
1403 }
72d497d4 1404
7f6689e4 1405 if ($return) {
1406 return $output;
1407 }
72d497d4 1408
1409 return $imagereturn;
7f6689e4 1410}
1411
1412function forum_add_attachment($post, $newfile) {
1413// $post is a full post record, including course and forum
3b7c1de9 1414// $newfile is a full upload array from $_FILES
7f6689e4 1415// If successful, this function returns the name of the file
1416
9dd0b378 1417 global $CFG;
1418
3b7c1de9 1419 if (empty($newfile['name'])) {
7f6689e4 1420 return "";
1421 }
1422
1423 $newfile_name = clean_filename($newfile['name']);
1424
1425 if (valid_uploaded_file($newfile)) {
1426 if (! $newfile_name) {
1427 notify("This file had a wierd filename and couldn't be uploaded");
1428
1429 } else if (! $dir = forum_file_area($post)) {
1430 notify("Attachment could not be stored");
1431 $newfile_name = "";
1432
1433 } else {
1434 if (move_uploaded_file($newfile['tmp_name'], "$dir/$newfile_name")) {
9dd0b378 1435 chmod("$dir/$newfile_name", $CFG->directorypermissions);
7f6689e4 1436 forum_delete_old_attachments($post, $newfile_name);
1437 } else {
1438 notify("An error happened while saving the file on the server");
1439 $newfile_name = "";
1440 }
1441 }
1442 } else {
1443 $newfile_name = "";
1444 }
1445
1446 return $newfile_name;
1447}
501cdbd8 1448
11b0c469 1449function forum_add_new_post($post) {
501cdbd8 1450
ffe11640 1451 $post->created = $post->modified = time();
501cdbd8 1452 $post->mailed = "0";
1453
7f6689e4 1454 $newfile = $post->attachment;
1455 $post->attachment = "";
1456
1457 if (! $post->id = insert_record("forum_posts", $post)) {
1458 return false;
1459 }
1460
1461 if ($post->attachment = forum_add_attachment($post, $newfile)) {
1462 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id);
1463 }
29507631 1464
1465 // Update discussion modified date
1466 set_field("forum_discussions", "timemodified", $post->modified, "id", $post->discussion);
7f6689e4 1467
1468 return $post->id;
501cdbd8 1469}
1470
11b0c469 1471function forum_update_post($post) {
501cdbd8 1472
ffe11640 1473 $post->modified = time();
501cdbd8 1474
0ab85112 1475 if (!$post->parent) { // Post is a discussion starter - update discussion title too
1476 set_field("forum_discussions", "name", $post->subject, "id", $post->discussion);
1477 }
29507631 1478
7f6689e4 1479 if ($newfilename = forum_add_attachment($post, $post->attachment)) {
1480 $post->attachment = $newfilename;
1481 } else {
1482 unset($post->attachment);
1483 }
29507631 1484
1485 // Update discussion modified date
1486 set_field("forum_discussions", "timemodified", $post->modified, "id", $post->discussion);
1487
ffe11640 1488 return update_record("forum_posts", $post);
501cdbd8 1489}
1490
1491function forum_add_discussion($discussion) {
1492// Given an object containing all the necessary data,
1493// create a new discussion and return the id
1494
1495 GLOBAL $USER;
1496
1497 $timenow = time();
1498
1499 // The first post is stored as a real post, and linked
1500 // to from the discuss entry.
1501
1502 $post->discussion = 0;
1503 $post->parent = 0;
ebc3bd2b 1504 $post->userid = $USER->id;
501cdbd8 1505 $post->created = $timenow;
1506 $post->modified = $timenow;
1507 $post->mailed = 0;
1508 $post->subject = $discussion->name;
1509 $post->message = $discussion->intro;
7f6689e4 1510 $post->attachment = "";
1511 $post->forum = $discussion->forum;
1512 $post->course = $discussion->course;
9f0b8269 1513 $post->format = $discussion->format;
501cdbd8 1514
1515 if (! $post->id = insert_record("forum_posts", $post) ) {
1516 return 0;
1517 }
1518
7f6689e4 1519 if ($post->attachment = forum_add_attachment($post, $discussion->attachment)) {
1520 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id); //ignore errors
1521 }
1522
caadf009 1523 // Now do the real module entry
04eba58f 1524
caadf009 1525 $discussion->firstpost = $post->id;
1526 $discussion->timemodified = $timenow;
04eba58f 1527
caadf009 1528 if (! $discussion->id = insert_record("forum_discussions", $discussion) ) {
1529 delete_records("forum_posts", "id", $post->id);
1530 return 0;
04eba58f 1531 }
1532
caadf009 1533 // Finally, set the pointer on the post.
1534 if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) {
1535 delete_records("forum_posts", "id", $post->id);
1536 delete_records("forum_discussions", "id", $discussion->id);
1537 return 0;
04eba58f 1538 }
04eba58f 1539
caadf009 1540 return $discussion->id;
1541}
04eba58f 1542
04eba58f 1543
caadf009 1544function forum_delete_discussion($discussion) {
1545// $discussion is a discussion record object
04eba58f 1546
1547 $result = true;
1548
caadf009 1549 if ($posts = get_records("forum_posts", "discussion", $discussion->id)) {
1550 foreach ($posts as $post) {
1551 $post->course = $discussion->course;
1552 $post->forum = $discussion->forum;
1553 if (! delete_records("forum_ratings", "post", "$post->id")) {
1554 $result = false;
1555 }
1556 if (! forum_delete_post($post)) {
04eba58f 1557 $result = false;
1558 }
1559 }
1560 }
1561
caadf009 1562 if (! delete_records("forum_discussions", "id", "$discussion->id")) {
04eba58f 1563 $result = false;
1564 }
1565
1566 return $result;
1567}
1568
1569
caadf009 1570function forum_delete_post($post) {
1571 if (delete_records("forum_posts", "id", $post->id)) {
1572 delete_records("forum_ratings", "post", $post->id); // Just in case
1573 if ($post->attachment) {
1574 $discussion = get_record("forum_discussions", "id", $post->discussion);
1575 $post->course = $discussion->course;
1576 $post->forum = $discussion->forum;
1577 forum_delete_old_attachments($post);
1578 }
1579 return true;
1580 }
1581 return false;
1582}
501cdbd8 1583
501cdbd8 1584
caadf009 1585function forum_print_user_discussions($courseid, $userid) {
1586 global $CFG, $USER;
501cdbd8 1587
b8bf90c5 1588 $maxdiscussions = 10;
1589 $countdiscussions = 0;
1590
1f48942e 1591
1592 if ($discussions = forum_get_user_discussions($courseid, $userid)) {
caadf009 1593 $user = get_record("user", "id", $userid);
1594 echo "<HR>";
b8bf90c5 1595 print_heading( get_string("discussionsstartedbyrecent", "forum", "$user->firstname $user->lastname") );
caadf009 1596 $replies = forum_count_discussion_replies();
1597 foreach ($discussions as $discussion) {
b8bf90c5 1598 $countdiscussions++;
1599 if ($countdiscussions > $maxdiscussions) {
1600 break;
1601 }
caadf009 1602 if (($discussion->forumtype == "teacher") and !isteacher($courseid)) {
ffe11640 1603 continue;
1604 }
47f1da80 1605 if (!empty($replies[$discussion->discussion])) {
caadf009 1606 $discussion->replies = $replies[$discussion->discussion]->replies;
1607 } else {
1608 $discussion->replies = 0;
501cdbd8 1609 }
caadf009 1610 $inforum = get_string("inforum", "forum", "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname</A>");
1611 $discussion->subject .= " ($inforum)";
61e96406 1612 if (!empty($USER->id)) {
1613 $ownpost = ($discussion->userid == $USER->id);
1614 } else {
1615 $ownpost = false;
1616 }
caadf009 1617 forum_print_post($discussion, $courseid, $ownpost, $reply=0, $link=1, $assessed=false);
1618 echo "<BR>\n";
501cdbd8 1619 }
1620 }
501cdbd8 1621}
1622
501cdbd8 1623function forum_forcesubscribe($forumid, $value=1) {
1624 return set_field("forum", "forcesubscribe", $value, "id", $forumid);
1625}
1626
1627function forum_is_forcesubscribed($forumid) {
1628 return get_field("forum", "forcesubscribe", "id", $forumid);
1629}
1630
1631function forum_is_subscribed($userid, $forumid) {
1632 if (forum_is_forcesubscribed($forumid)) {
1633 return true;
1634 }
ebc3bd2b 1635 return record_exists("forum_subscriptions", "userid", $userid, "forum", $forumid);
86970225 1636}
1637
501cdbd8 1638function forum_subscribe($userid, $forumid) {
9fa49e22 1639/// Adds user to the subscriber list
1640
ebc3bd2b 1641 $sub->userid = $userid;
9fa49e22 1642 $sub->forum = $forumid;
501cdbd8 1643
9fa49e22 1644 return insert_record("forum_subscriptions", $sub);
501cdbd8 1645}
1646
1647function forum_unsubscribe($userid, $forumid) {
9fa49e22 1648/// Removes user from the subscriber list
ebc3bd2b 1649 return delete_records("forum_subscriptions", "userid", $userid, "forum", $forumid);
501cdbd8 1650}
1651
0a9f61b5 1652function forum_post_subscription($post) {
1653/// Given a new post, subscribes or unsubscribes as appropriate.
1654/// Returns some text which describes what happened.
1655
1656 global $USER;
1657
1658 if (empty($post->subscribe) and empty($post->unsubscribe)) {
1659 return "";
1660 }
1661
1662 if (!$forum = get_record("forum", "id", $post->forum)) {
1663 return "";
1664 }
1665
1666 $info->name = "$USER->firstname $USER->lastname";
1667 $info->forum = $forum->name;
1668
1669 if (!empty($post->subscribe)) {
1670 forum_subscribe($USER->id, $post->forum);
1671 return "<p>".get_string("nowsubscribed", "forum", $info)."</p>";
1672 }
1673
1674 forum_unsubscribe($USER->id, $post->forum);
1675 return "<p>".get_string("nownotsubscribed", "forum", $info)."</p>";
1676}
1677
501cdbd8 1678
11b0c469 1679function forum_user_has_posted_discussion($forumid, $userid) {
29507631 1680 if ($discussions = forum_get_discussions($forumid, "", $userid)) {
501cdbd8 1681 return true;
1682 } else {
1683 return false;
1684 }
1685}
1686
11b0c469 1687function forum_user_can_post_discussion($forum) {
501cdbd8 1688// $forum is an object
1689 global $USER;
1690
1691 if ($forum->type == "eachuser") {
11b0c469 1692 return (! forum_user_has_posted_discussion($forum->id, $USER->id));
501cdbd8 1693 } else if ($forum->type == "teacher") {
1694 return isteacher($forum->course);
1695 } else if (isteacher($forum->course)) {
1696 return true;
1697 } else {
70c476a7 1698 return ($forum->open == 2);
501cdbd8 1699 }
1700}
1701
f690562f 1702function forum_user_can_post($forum, $user=NULL) {
1703// $forum, $user are objects
1704
1705 if ($user) {
1706 $isteacher = isteacher($forum->course, $user->id);
1707 } else {
1708 $isteacher = isteacher($forum->course);
1709 }
70c476a7 1710
1711 if ($forum->type == "teacher") {
f690562f 1712 return $isteacher;
1713 } else if ($isteacher) {
70c476a7 1714 return true;
1715 } else {
1716 return $forum->open;
1717 }
1718}
501cdbd8 1719
501cdbd8 1720
29507631 1721function forum_print_latest_discussions($forum_id=0, $forum_numdiscussions=5, $forum_style="plain", $forum_sort="") {
c585fa17 1722 global $CFG, $USER;
f93f848a 1723
1724 if ($forum_id) {
1725 if (! $forum = get_record("forum", "id", $forum_id)) {
1726 error("Forum ID was incorrect");
1727 }
1728 if (! $course = get_record("course", "id", $forum->course)) {
1729 error("Could not find the course this forum belongs to!");
1730 }
1731
1732 if ($course->category) {
1733 require_login($course->id);
1734 }
1735
1736 } else {
1737 if (! $course = get_record("course", "category", 0)) {
1738 error("Could not find a top-level course!");
1739 }
7beb45d8 1740 if (! $forum = forum_get_course_forum($course->id, "news")) {
f93f848a 1741 error("Could not find or create a main forum in this course (id $course->id)");
1742 }
1743 }
1744
11b0c469 1745 if (forum_user_can_post_discussion($forum)) {
b879effb 1746 echo "<p align=center>";
1747 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?forum=$forum->id\">";
0351b1f9 1748 if ($forum->type == "news") {
1749 echo get_string("addanewtopic", "forum")."</a>...";
1750 } else {
1751 echo get_string("addanewdiscussion", "forum")."</a>...";
1752 }
b879effb 1753 echo "</p>\n";
77305fe6 1754 }
1755
3c8a606d 1756 if ((!$forum_numdiscussions) && ($forum_style == "plain")) {
1757 $forum_style = "header"; // Abbreviate display by default
3335f6fb 1758 }
f93f848a 1759
2ab968e9 1760 if ($forum_style == "minimal") {
1761 $forum_sort = "p.modified DESC";
1762 }
1763
1764 $fullpost = false;
1765 if ($forum_style == "plain") {
1766 $fullpost = true;
1767 }
1768
1769 if (! $discussions = forum_get_discussions($forum->id, $forum_sort, 0, $fullpost) ) {
0351b1f9 1770 if ($forum->type == "news") {
1771 echo "<p align=center><b>(".get_string("nonews", "forum").")</b></p>";
1772 } else {
1773 echo "<p align=center><b>(".get_string("nodiscussions", "forum").")</b></p>";
1774 }
2ab968e9 1775 return;
1776 }
1777
3335f6fb 1778 $replies = forum_count_discussion_replies($forum->id);
f93f848a 1779
3260de67 1780 $canreply = forum_user_can_post($forum);
1781
3335f6fb 1782 $discussioncount = 0;
c20b762a 1783 $olddiscussionlink = false;
2ab968e9 1784 $strdatestring = get_string("strftimedaydatetime");
f93f848a 1785
dcde9f02 1786 if ($forum_style == "minimal") {
1787 $strftimerecent = get_string("strftimerecent");
1788 $strmore = get_string("more", "forum");
1789 }
1790
29507631 1791 if ($forum_style == "header") {
1792 echo "<table width=\"100%\" border=0 cellpadding=3 cellspacing=1 class=\"forumpost\">";
03aa0dfa 1793 echo "<tr class=\"forumpostheader\">";
9e0a08bd 1794 echo "<th>".get_string("discussion", "forum")."</th>";
29507631 1795 echo "<th colspan=2>".get_string("startedby", "forum")."</th>";
1796 echo "<th>".get_string("replies", "forum")."</th>";
1797 echo "<th>".get_string("lastpost", "forum")."</th>";
1798 echo "</tr>";
1799 }
1800
3335f6fb 1801 foreach ($discussions as $discussion) {
1802 $discussioncount++;
f93f848a 1803
3335f6fb 1804 if ($forum_numdiscussions && ($discussioncount > $forum_numdiscussions)) {
c20b762a 1805 $olddiscussionlink = true;
3335f6fb 1806 break;
1807 }
9c9f7d77 1808 if (!empty($replies[$discussion->discussion])) {
3335f6fb 1809 $discussion->replies = $replies[$discussion->discussion]->replies;
1810 } else {
1811 $discussion->replies = 0;
1812 }
f7477444 1813 if (!empty($USER->id)) {
1814 $ownpost = ($discussion->userid == $USER->id);
1815 } else {
1816 $ownpost=false;
1817 }
3335f6fb 1818 switch ($forum_style) {
1819 case "minimal":
b879effb 1820 echo "<p><font color=#555555>".userdate($discussion->modified, $strftimerecent)." - $discussion->firstname</font>";
1821 echo "<br>$discussion->subject ";
1822 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->discussion\">";
1823 echo $strmore."...</a>";
1824 echo "</p>\n";
3335f6fb 1825 break;
1826 case "header":
2ab968e9 1827 forum_print_discussion_header($discussion, $forum->course, $strdatestring);
3335f6fb 1828 break;
1829 default:
3260de67 1830 if ($canreply or $discussion->replies) {
1831 $link = true;
1832 } else {
1833 $link = false;
1834 }
1835 forum_print_post($discussion, $forum->course, $ownpost, $reply=0, $link, $assessed=false);
b879effb 1836 echo "<br>\n";
3335f6fb 1837 break;
f93f848a 1838 }
1839 }
29507631 1840
1841 if ($forum_style == "header") {
1842 echo "</table>";
1843 }
c20b762a 1844
1845 if ($olddiscussionlink) {
1846 echo "<p align=right><a href=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id&showall=1\">";
1847 echo get_string("olderdiscussions", "forum")."</a> ...</p>";
1848 }
f93f848a 1849}
1850
fe25fc9b 1851function forum_print_discussion($course, $forum, $discussion, $post, $mode) {
501cdbd8 1852
1853 global $USER;
1854
61e96406 1855 if (!empty($USER->id)) {
1856 $ownpost = ($USER->id == $post->userid);
1857 } else {
1858 $ownpost = false;
1859 }
70c476a7 1860 $reply = forum_user_can_post($forum);
501cdbd8 1861
02ebf404 1862 $ratings = NULL;
61e96406 1863 if ($forum->assessed and !empty($USER->id)) {
02ebf404 1864 if ($scale = get_record("scale", "id", $forum->scale)) {
1865 $ratings->scale = make_menu_from_list($scale->scale);
1866 if ($forum->assessed == 2 and !isteacher($course->id)) {
1867 $ratings->allow = false;
1868 } else {
1869 $ratings->allow = true;
1870 }
1871 echo "<form name=form method=post action=rate.php>";
1872 echo "<input type=hidden name=id value=\"$course->id\">";
9d1b97c5 1873 }
1874 }
1875
02ebf404 1876 forum_print_post($post, $course->id, $ownpost, $reply, $link=false, $ratings);
501cdbd8 1877
1878 switch ($mode) {
1879 case 1 : // Flat ascending
1880 case -1 : // Flat descending
1881 default:
02ebf404 1882 echo "<ul>";
1883 forum_print_posts_flat($post->discussion, $course->id, $mode, $ratings, $reply);
1884 echo "</ul>";
501cdbd8 1885 break;
1886
1887 case 2 : // Threaded
02ebf404 1888 forum_print_posts_threaded($post->id, $course->id, 0, $ratings, $reply);
501cdbd8 1889 break;
1890
1891 case 3 : // Nested
02ebf404 1892 forum_print_posts_nested($post->id, $course->id, $ratings, $reply);
501cdbd8 1893 break;
1894 }
1895
02ebf404 1896 if ($ratings) {
1897 echo "<center><input type=\"submit\" value=\"".get_string("sendinratings", "forum")."\">";
1898 print_scale_menu_helpbutton($course->id, $scale);
1899 echo "</center>";
1900 echo "</form>";
501cdbd8 1901 }
1902}
1903
02ebf404 1904function forum_print_posts_flat($discussion, $course, $direction, $ratings, $reply) {
501cdbd8 1905 global $USER;
1906
501cdbd8 1907 $link = false;
1908
1909 if ($direction < 0) {
1910 $sort = "ORDER BY created DESC";
1911 } else {
1912 $sort = "ORDER BY created ASC";
1913 }
1914
1f48942e 1915 if ($posts = forum_get_discussion_posts($discussion, $sort)) {
501cdbd8 1916 foreach ($posts as $post) {
ebc3bd2b 1917 $ownpost = ($USER->id == $post->userid);
02ebf404 1918 forum_print_post($post, $course, $ownpost, $reply, $link, $ratings);
501cdbd8 1919 }
1920 } else {
1921 return;
1922 }
1923}
1924
02ebf404 1925function forum_print_posts_threaded($parent, $course, $depth, $ratings, $reply) {
501cdbd8 1926 global $USER;
1927
501cdbd8 1928 $link = false;
1929
1f48942e 1930 if ($posts = forum_get_child_posts($parent)) {
501cdbd8 1931 foreach ($posts as $post) {
1932
f95c2a73 1933 echo "<ul>";
501cdbd8 1934 if ($depth > 0) {
ebc3bd2b 1935 $ownpost = ($USER->id == $post->userid);
02ebf404 1936 forum_print_post($post, $course, $ownpost, $reply, $link, $ratings); // link=true?
f95c2a73 1937 echo "<br />";
501cdbd8 1938 } else {
8c3c8481 1939 $by->name = "$post->firstname $post->lastname";
d62413e8 1940 $by->date = userdate($post->modified);
f95c2a73 1941 echo "<li><p><a name=\"$post->id\"></a><font size=-1><b><a href=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</a></b> ";
8c3c8481 1942 print_string("bynameondate", "forum", $by);
f95c2a73 1943 echo "</font></p></li>";
501cdbd8 1944 }
1945
02ebf404 1946 forum_print_posts_threaded($post->id, $course, $depth-1, $ratings, $reply);
f95c2a73 1947 echo "</ul>\n";
501cdbd8 1948 }
1949 } else {
1950 return;
1951 }
1952}
1953
02ebf404 1954function forum_print_posts_nested($parent, $course, $ratings, $reply) {
501cdbd8 1955 global $USER;
1956
501cdbd8 1957 $link = false;
1958
1f48942e 1959 if ($posts = forum_get_child_posts($parent)) {
501cdbd8 1960 foreach ($posts as $post) {
1961
61e96406 1962 if (empty($USER->id)) {
1963 $ownpost = false;
1964 } else {
1965 $ownpost = ($USER->id == $post->userid);
1966 }
501cdbd8 1967
1968 echo "<UL>";
02ebf404 1969 forum_print_post($post, $course, $ownpost, $reply, $link, $ratings);
501cdbd8 1970 echo "<BR>";
02ebf404 1971 forum_print_posts_nested($post->id, $course, $ratings, $reply);
501cdbd8 1972 echo "</UL>\n";
1973 }
1974 } else {
1975 return;
1976 }
1977}
1978
1979function forum_set_display_mode($mode=0) {
5a83a0a8 1980 global $USER, $FORUM_DEFAULT_DISPLAY_MODE;
501cdbd8 1981
1982 if ($mode) {
1983 $USER->mode = $mode;
9c9f7d77 1984 } else if (empty($USER->mode)) {
501cdbd8 1985 $USER->mode = $FORUM_DEFAULT_DISPLAY_MODE;
1986 }
1987}
f93f848a 1988
3869a2ac 1989
f93f848a 1990?>