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