Exclude "db" dir when detecting plugins (to be used in blocks
[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
2e2e71a8 7define("FORUM_MODE_FLATOLDEST", 1);
8define("FORUM_MODE_FLATNEWEST", -1);
9define("FORUM_MODE_THREADED", 2);
10define("FORUM_MODE_NESTED", 3);
11
12$FORUM_LAYOUT_MODES = array ( FORUM_MODE_FLATOLDEST => get_string("modeflatoldestfirst", "forum"),
13 FORUM_MODE_FLATNEWEST => get_string("modeflatnewestfirst", "forum"),
14 FORUM_MODE_THREADED => get_string("modethreaded", "forum"),
15 FORUM_MODE_NESTED => get_string("modenested", "forum") );
f93f848a 16
11b0c469 17// These are course content forums that can be added to the course manually
ffe11640 18$FORUM_TYPES = array ("general" => get_string("generalforum", "forum"),
19 "eachuser" => get_string("eachuserforum", "forum"),
20 "single" => get_string("singleforum", "forum") );
f93f848a 21
70c476a7 22$FORUM_OPEN_MODES = array ("2" => get_string("openmode2", "forum"),
23 "1" => get_string("openmode1", "forum"),
24 "0" => get_string("openmode0", "forum") );
25
5be7800c 26if (!isset($CFG->forum_displaymode)) {
2e2e71a8 27 set_config("forum_displaymode", FORUM_MODE_NESTED);
5be7800c 28}
73bb0835 29
5be7800c 30if (!isset($CFG->forum_shortpost)) {
31 set_config("forum_shortpost", 300); // Less non-HTML characters than this is short
32}
4d871a72 33
5be7800c 34if (!isset($CFG->forum_longpost)) {
35 set_config("forum_longpost", 600); // More non-HTML characters than this is long
36}
4d871a72 37
5be7800c 38if (!isset($CFG->forum_manydiscussions)) {
39 set_config("forum_manydiscussions", 100); // Number of discussions on a page
40}
e07635f4 41
4909e176 42if (!isset($CFG->forum_maxbytes)) {
43 set_config("forum_maxbytes", 512000); // Default maximum size for all forums
44}
45
46
e07635f4 47
caadf009 48/// STANDARD FUNCTIONS ///////////////////////////////////////////////////////////
49
50function forum_add_instance($forum) {
51// Given an object containing all the necessary data,
52// (defined by the form in mod.html) this function
53// will create a new instance and return the id number
54// of the new instance.
55
56 global $CFG;
57
58 $forum->timemodified = time();
59
60 if (! $forum->id = insert_record("forum", $forum)) {
61 return false;
62 }
63
f2f56406 64 if (!$forum->userating) {
65 $forum->assessed = 0;
66 }
67
98914efd 68 if (!empty($forum->ratingtime)) {
69 $forum->assesstimestart = make_timestamp($forum->startyear, $forum->startmonth, $forum->startday,
70 $forum->starthour, $forum->startminute, 0);
71 $forum->assesstimefinish = make_timestamp($forum->finishyear, $forum->finishmonth, $forum->finishday,
72 $forum->finishhour, $forum->finishminute, 0);
73 } else {
74 $forum->assesstimestart = 0;
75 $forum->assesstimefinish = 0;
76 }
caadf009 77
98914efd 78 if ($forum->type == "single") { // Create related discussion.
caadf009 79 $discussion->course = $forum->course;
80 $discussion->forum = $forum->id;
81 $discussion->name = $forum->name;
82 $discussion->intro = $forum->intro;
83 $discussion->assessed = $forum->assessed;
84
85 if (! forum_add_discussion($discussion)) {
86 error("Could not add the discussion for this forum");
87 }
88 }
caadf009 89
90 return $forum->id;
91}
92
93
94function forum_update_instance($forum) {
95// Given an object containing all the necessary data,
96// (defined by the form in mod.html) this function
97// will update an existing instance with new data.
98
99 $forum->timemodified = time();
100 $forum->id = $forum->instance;
101
f2f56406 102 if (!$forum->userating) {
103 $forum->assessed = 0;
104 }
105
98914efd 106 if (!empty($forum->ratingtime)) {
107 $forum->assesstimestart = make_timestamp($forum->startyear, $forum->startmonth, $forum->startday,
108 $forum->starthour, $forum->startminute, 0);
109 $forum->assesstimefinish = make_timestamp($forum->finishyear, $forum->finishmonth, $forum->finishday,
110 $forum->finishhour, $forum->finishminute, 0);
111 } else {
112 $forum->assesstimestart = 0;
113 $forum->assesstimefinish = 0;
114 }
115
caadf009 116 if ($forum->type == "single") { // Update related discussion and post.
117 if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
118 if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
119 notify("Warning! There is more than one discussion in this forum - using the most recent");
120 $discussion = array_pop($discussions);
121 } else {
122 error("Could not find the discussion in this forum");
123 }
124 }
125 if (! $post = get_record("forum_posts", "id", $discussion->firstpost)) {
126 error("Could not find the first post in this forum discussion");
127 }
128
129 $post->subject = $forum->name;
130 $post->message = $forum->intro;
131 $post->modified = $forum->timemodified;
132
133 if (! update_record("forum_posts", $post)) {
134 error("Could not update the first post");
135 }
136
137 $discussion->name = $forum->name;
138
139 if (! update_record("forum_discussions", $discussion)) {
140 error("Could not update the discussion");
141 }
142 }
143
69d79bc3 144 return update_record("forum", $forum);
caadf009 145}
146
147
148function forum_delete_instance($id) {
149// Given an ID of an instance of this module,
150// this function will permanently delete the instance
151// and any data that depends on it.
152
153 if (! $forum = get_record("forum", "id", "$id")) {
154 return false;
155 }
156
157 $result = true;
158
159 if ($discussions = get_records("forum_discussions", "forum", $forum->id)) {
160 foreach ($discussions as $discussion) {
161 if (! forum_delete_discussion($discussion)) {
162 $result = false;
163 }
164 }
165 }
166
167 if (! delete_records("forum_subscriptions", "forum", "$forum->id")) {
168 $result = false;
169 }
170
171 if (! delete_records("forum", "id", "$forum->id")) {
172 $result = false;
173 }
174
175 return $result;
176}
177
178
179function forum_cron () {
edffca15 180/// Function to be run periodically according to the moodle cron
181/// Finds all posts that have yet to be mailed out, and mails them
182/// out to all subscribers
caadf009 183
184 global $CFG, $USER;
185
ec2137ba 186 if (!empty($USER)) { // Remember real USER account if necessary
187 $realuser = $USER;
188 }
189
caadf009 190 $cutofftime = time() - $CFG->maxeditingtime;
191
1f48942e 192 if ($posts = forum_get_unmailed_posts($cutofftime)) {
caadf009 193
edffca15 194 /// Mark them all now as being mailed. It's unlikely but possible there
195 /// might be an error later so that a post is NOT actually mailed out,
196 /// but since mail isn't crucial, we can accept this risk. Doing it now
197 /// prevents the risk of duplicated mails, which is a worse problem.
198
199 foreach ($posts as $key => $post) {
200 if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) {
201 echo "Error marking post id post->id as being mailed. This post will not be mailed.\n";
202 unset($posts[$key]);
203 }
204 }
205
caadf009 206 $timenow = time();
207
208 foreach ($posts as $post) {
209
edffca15 210 echo "\n";
caadf009 211 print_string("processingpost", "forum", $post->id);
edffca15 212 echo "\n";
caadf009 213
ebc3bd2b 214 if (! $userfrom = get_record("user", "id", "$post->userid")) {
215 echo "Could not find user $post->userid\n";
caadf009 216 continue;
217 }
218
219 if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) {
220 echo "Could not find discussion $post->discussion\n";
221 continue;
222 }
223
224 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
225 echo "Could not find forum $discussion->forum\n";
226 continue;
227 }
228
229 if (! $course = get_record("course", "id", "$forum->course")) {
230 echo "Could not find course $forum->course\n";
231 continue;
232 }
233
e1fb2e6e 234 if (!empty($course->lang)) {
235 $CFG->courselang = $course->lang;
236 } else {
237 unset($CFG->courselang);
238 }
9c84314e 239
9197e147 240 $groupmode = false;
241 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
242 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
2f9c977e 243 if (!$group = get_record("groups", "id", $discussion->groupid)) { // Can't find group
9197e147 244 continue; // Be safe and don't send it to anyone
245 }
246 }
69d79bc3 247 } else {
248 $cm->id = 0;
249 }
9197e147 250
251
caadf009 252 if ($users = forum_subscribed_users($course, $forum)) {
253 $canunsubscribe = ! forum_is_forcesubscribed($forum->id);
254
255 $mailcount=0;
3d94772d 256 $errorcount=0;
caadf009 257 foreach ($users as $userto) {
9197e147 258 if ($groupmode) { // Look for a reason not to send this email
259 if (!isteacheredit($course->id, $userto->id)) {
260 if (!ismember($group->id, $userto->id)) {
261 continue;
262 }
263 }
264 }
8c0ef41d 265
266 /// GWD: reset timelimit so that script does not get timed out when posting to many users
267 set_time_limit(10);
9197e147 268
ec2137ba 269 /// Override the language and timezone of the "current" user, so that
270 /// mail is customised for the receiver.
271 $USER->lang = $userto->lang;
272 $USER->timezone = $userto->timezone;
273
f690562f 274 $canreply = forum_user_can_post($forum, $userto);
caadf009 275
1b26d5e7 276 $by->name = fullname($userfrom, isteacher($course->id, $userto->id));
d62413e8 277 $by->date = userdate($post->modified, "", $userto->timezone);
caadf009 278 $strbynameondate = get_string("bynameondate", "forum", $by);
279
280 $strforums = get_string("forums", "forum");
281
282 $postsubject = "$course->shortname: $post->subject";
283 $posttext = "$course->shortname -> $strforums -> $forum->name";
284
285 if ($discussion->name == $forum->name) {
286 $posttext .= "\n";
287 } else {
288 $posttext .= " -> $discussion->name\n";
289 }
290 $posttext .= "---------------------------------------------------------------------\n";
291 $posttext .= "$post->subject\n";
292 $posttext .= $strbynameondate."\n";
293 $posttext .= "---------------------------------------------------------------------\n";
d342c763 294 $posttext .= format_text_email($post->message, $post->format);
caadf009 295 $posttext .= "\n\n";
296 if ($post->attachment) {
297 $post->course = $course->id;
298 $post->forum = $forum->id;
299 $posttext .= forum_print_attachments($post, "text");
300 }
f690562f 301 if ($canreply) {
302 $posttext .= "---------------------------------------------------------------------\n";
303 $posttext .= get_string("postmailinfo", "forum", $course->shortname)."\n";
304 $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id\n";
305 }
caadf009 306 if ($canunsubscribe) {
307 $posttext .= "\n---------------------------------------------------------------------\n";
308 $posttext .= get_string("unsubscribe", "forum");
309 $posttext .= ": $CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\n";
310 }
311
312 if ($userto->mailformat == 1) { // HTML
edffca15 313 $posthtml = "<p><font face=\"sans-serif\">".
314 "<a target=\"_blank\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ".
315 "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</a> -> ".
316 "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name</a>";
caadf009 317 if ($discussion->name == $forum->name) {
edffca15 318 $posthtml .= "</font></p>";
caadf009 319 } else {
edffca15 320 $posthtml .= " -> <a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name</a></font></p>";
caadf009 321 }
f690562f 322 $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, $canreply, false, false);
caadf009 323
324 if ($canunsubscribe) {
edffca15 325 $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 326 }
327
328 } else {
329 $posthtml = "";
330 }
331
332 if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) {
edffca15 333 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";
69d79bc3 334 add_to_log($course->id, 'forum', 'mail error', "discuss.php?d=$discussion->id#$post->id", substr($post->subject,0,15), $cm->id, $userto->id);
3d94772d 335 $errorcount++;
caadf009 336 } else {
337 $mailcount++;
338 }
339 }
caadf009 340
edffca15 341 echo ".... mailed to $mailcount users.\n";
3d94772d 342 if ($errorcount) {
343 set_field("forum_posts", "mailed", "2", "id", "$post->id");
344 }
caadf009 345 }
caadf009 346 }
347 }
348
ec2137ba 349 if (!empty($realuser)) { // Restore real USER if necessary
350 $USER = $realuser;
351 }
352
caadf009 353 return true;
354}
355
356function forum_user_outline($course, $user, $mod, $forum) {
357
1f48942e 358 if ($posts = forum_get_user_posts($forum->id, $user->id)) {
caadf009 359 $result->info = get_string("numposts", "forum", count($posts));
360
361 $lastpost = array_pop($posts);
362 $result->time = $lastpost->modified;
363 return $result;
364 }
365 return NULL;
366}
367
368
369function forum_user_complete($course, $user, $mod, $forum) {
370 global $CFG;
371
1f48942e 372 if ($posts = forum_get_user_posts($forum->id, $user->id)) {
caadf009 373 foreach ($posts as $post) {
497588fe 374 forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false, $rate=false);
caadf009 375 }
376
377 } else {
378 echo "<P>".get_string("noposts", "forum")."</P>";
379 }
caadf009 380}
381
1b5910c4 382function forum_print_recent_activity($course, $isteacher, $timestart) {
cc3655a2 383/// Given a course and a date, prints a summary of all the new
384/// messages posted in the course since that date
385
3d891989 386 global $CFG;
caadf009 387
388 $heading = false;
389 $content = false;
390
1b5910c4 391 if (!$logs = get_records_select("log", "time > '$timestart' AND ".
392 "course = '$course->id' AND ".
393 "module = 'forum' AND ".
394 "action LIKE 'add %' ", "time ASC")){
395 return false;
396 }
397
dcde9f02 398 $strftimerecent = get_string("strftimerecent");
399
d05956ac 400 $isteacheredit = isteacheredit($course->id);
401 $mygroupid = mygroupid($course->id);
402
403 $groupmode = array(); /// To cache group modes
404
1b5910c4 405 foreach ($logs as $log) {
406 //Get post info, I'll need it later
407 $post = forum_get_post_from_log($log);
408
409 //Create a temp valid module structure (course,id)
410 $tempmod->course = $log->course;
411 $tempmod->id = $post->forum;
412 //Obtain the visible property from the instance
b91d6dcd 413 $modvisible = instance_is_visible($log->module, $tempmod);
1b5910c4 414
415 //Only if the mod is visible
416 if ($modvisible) {
417 if ($post) {
b91d6dcd 418 /// Check whether this is for teachers only
1b5910c4 419 $teacheronly = "";
b91d6dcd 420 if ($forum = get_record("forum", "id", $post->forum)) {
1b5910c4 421 if ($forum->type == "teacher") {
422 if ($isteacher) {
423 $teacheronly = "class=\"teacheronly\"";
ad08fdc2 424 } else {
1b5910c4 425 continue;
ad08fdc2 426 }
caadf009 427 }
428 }
b91d6dcd 429 /// Check whether this is belongs to a discussion in a group that
d05956ac 430 /// should NOT be accessible to the current user
431
2f9c977e 432 if (!$isteacheredit) { /// Because editing teachers can see everything anyway
d05956ac 433 if (!isset($cm[$post->forum])) {
434 $cm[$forum->id] = get_coursemodule_from_instance("forum", $forum->id, $course->id);
435 $groupmode[$forum->id] = groupmode($course, $cm[$forum->id]);
436 }
c3fcf3f3 437 if ($groupmode[$forum->id]) {
2f9c977e 438 if ($mygroupid != $post->groupid) {
d05956ac 439 continue;
b91d6dcd 440 }
441 }
442 }
443
1b5910c4 444 if (! $heading) {
445 print_headline(get_string("newforumposts", "forum").":");
446 $heading = true;
447 $content = true;
448 }
449 $date = userdate($post->modified, $strftimerecent);
1b26d5e7 450 $fullname = fullname($post, $isteacher);
451 echo "<p $teacheronly><font size=1>$date - $fullname<br>";
1b5910c4 452 echo "\"<a href=\"$CFG->wwwroot/mod/forum/$log->url\">";
436a7cae 453 if (!empty($CFG->filterall)) {
34e934a9 454 $post->subject = filter_text("<nolink>$post->subject</nolink>", $course->id);
436a7cae 455 }
1b5910c4 456 if ($log->action == "add discussion") {
457 echo "<b>$post->subject</b>";
458 } else {
459 echo "$post->subject";
460 }
461 echo "</a>\"</font></p>";
caadf009 462 }
463 }
464 }
1b5910c4 465
caadf009 466 return $content;
467}
468
469
470function forum_grades($forumid) {
471/// Must return an array of grades, indexed by user, and a max grade.
caadf009 472
4db9d14d 473 if (!$forum = get_record("forum", "id", $forumid)) {
474 return false;
475 }
476 if (!$forum->assessed) {
477 return false;
478 }
d6bdd9d5 479 $scalemenu = make_grades_menu($forum->scale);
02ebf404 480
481 $currentuser = 0;
482 $ratingsuser = array();
483
1f48942e 484 if ($ratings = forum_get_user_grades($forumid)) {
02ebf404 485 foreach ($ratings as $rating) { // Ordered by user
486 if ($currentuser and $rating->userid != $currentuser) {
487 if (!empty($ratingsuser)) {
d6bdd9d5 488 if ($forum->scale < 0) {
489 $return->grades[$currentuser] = forum_get_ratings_mean(0, $scalemenu, $ratingsuser);
490 $return->grades[$currentuser] .= "<br />".forum_get_ratings_summary(0, $scalemenu, $ratingsuser);
491 } else {
492 $total = 0;
493 $count = 0;
494 foreach ($ratingsuser as $ra) {
495 $total += $ra;
496 $count ++;
497 }
498 $return->grades[$currentuser] = format_float($total/$count, 2);
499 }
02ebf404 500 } else {
501 $return->grades[$currentuser] = "";
502 }
503 $ratingsuser = array();
caadf009 504 }
02ebf404 505 $ratingsuser[] = $rating->rating;
506 $currentuser = $rating->userid;
caadf009 507 }
02ebf404 508 if (!empty($ratingsuser)) {
d6bdd9d5 509 if ($forum->scale < 0) {
510 $return->grades[$currentuser] = forum_get_ratings_mean(0, $scalemenu, $ratingsuser);
511 $return->grades[$currentuser] .= "<br />".forum_get_ratings_summary(0, $scalemenu, $ratingsuser);
512 } else {
513 $total = 0;
514 $count = 0;
515 foreach ($ratingsuser as $ra) {
516 $total += $ra;
517 $count ++;
518 }
519 $return->grades[$currentuser] = format_float((float)$total/(float)$count, 2);
520 }
02ebf404 521 } else {
522 $return->grades[$currentuser] = "";
caadf009 523 }
524 } else {
525 $return->grades = array();
526 }
527
d6bdd9d5 528 if ($forum->scale < 0) {
529 $return->maxgrade = "";
530 } else {
531 $return->maxgrade = $forum->scale;
532 }
caadf009 533 return $return;
534}
535
05855091 536function forum_get_participants($forumid) {
537//Returns the users with data in one forum
538//(users with records in forum_subscriptions, forum_posts and forum_ratings, students)
539
540 global $CFG;
541
542 //Get students from forum_subscriptions
543 $st_subscriptions = get_records_sql("SELECT DISTINCT u.*
544 FROM {$CFG->prefix}user u,
545 {$CFG->prefix}forum_subscriptions s
546 WHERE s.forum = '$forumid' and
547 u.id = s.userid");
548 //Get students from forum_posts
549 $st_posts = get_records_sql("SELECT DISTINCT u.*
550 FROM {$CFG->prefix}user u,
551 {$CFG->prefix}forum_discussions d,
552 {$CFG->prefix}forum_posts p
553 WHERE d.forum = '$forumid' and
554 p.discussion = d.id and
555 u.id = p.userid");
556
557 //Get students from forum_ratings
558 $st_ratings = get_records_sql("SELECT DISTINCT u.*
559 FROM {$CFG->prefix}user u,
560 {$CFG->prefix}forum_discussions d,
561 {$CFG->prefix}forum_posts p,
562 {$CFG->prefix}forum_ratings r
563 WHERE d.forum = '$forumid' and
564 p.discussion = d.id and
565 r.post = p.id and
566 u.id = r.userid");
567
568 //Add st_posts to st_subscriptions
569 if ($st_posts) {
570 foreach ($st_posts as $st_post) {
571 $st_subscriptions[$st_post->id] = $st_post;
572 }
573 }
574 //Add st_ratings to st_subscriptions
575 if ($st_ratings) {
576 foreach ($st_ratings as $st_rating) {
577 $st_subscriptions[$st_rating->id] = $st_rating;
578 }
579 }
580 //Return st_subscriptions array (it contains an array of unique users)
581 return ($st_subscriptions);
582}
caadf009 583
9fa49e22 584/// SQL FUNCTIONS ///////////////////////////////////////////////////////////
585
1f48942e 586function forum_get_post_full($postid) {
587/// Gets a post with all info ready for forum_print_post
588 global $CFG;
589
ebc3bd2b 590 return get_record_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 591 FROM {$CFG->prefix}forum_posts p,
592 {$CFG->prefix}user u
593 WHERE p.id = '$postid'
ebc3bd2b 594 AND p.userid = u.id");
1f48942e 595}
596
597function forum_get_discussion_posts($discussion, $sort) {
598/// Gets posts with all info ready for forum_print_post
599 global $CFG;
600
ebc3bd2b 601 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 602 FROM {$CFG->prefix}forum_posts p,
603 {$CFG->prefix}user u
604 WHERE p.discussion = $discussion
605 AND p.parent > 0
ebc3bd2b 606 AND p.userid = u.id $sort");
1f48942e 607}
608
609function forum_get_child_posts($parent) {
610/// Gets posts with all info ready for forum_print_post
611 global $CFG;
612
ebc3bd2b 613 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 614 FROM {$CFG->prefix}forum_posts p,
615 {$CFG->prefix}user u
616 WHERE p.parent = '$parent'
ebc3bd2b 617 AND p.userid = u.id
1f48942e 618 ORDER BY p.created ASC");
619}
620
621
8b9c7aa0 622function forum_search_posts($searchterms, $courseid, $page=0, $recordsperpage=50, &$totalcount) {
623/// Returns a list of posts found using an array of search terms
624/// eg word +word -word
625///
626
9fa49e22 627 global $CFG;
628
629 if (!isteacher($courseid)) {
630 $notteacherforum = "AND f.type <> 'teacher'";
4cabd355 631
632 $forummodule = get_record("modules", "name", "forum");
633 $onlyvisible = " AND f.id = cm.instance AND cm.visible = 1 AND cm.module = $forummodule->id";
634 $onlyvisibletable = ", {$CFG->prefix}course_modules cm";
9fa49e22 635 } else {
636 $notteacherforum = "";
4cabd355 637
638 $onlyvisible = "";
639 $onlyvisibletable = "";
9fa49e22 640 }
641
c2a96d6b 642 switch ($CFG->dbtype) {
643 case "mysql":
644 $limit = "LIMIT $page,$recordsperpage";
645 break;
646 case "postgres7":
647 $limit = "LIMIT $recordsperpage OFFSET ".($page * $recordsperpage);
648 break;
649 default:
650 $limit = "LIMIT $recordsperpage,$page";
97485d07 651 }
652
63d99fcb 653 /// Some differences in syntax for PostgreSQL
654 if ($CFG->dbtype == "postgres7") {
a74f950c 655 $LIKE = "ILIKE"; // case-insensitive
2b4c64e9 656 $NOTLIKE = "NOT ILIKE"; // case-insensitive
657 $REGEXP = "~*";
658 $NOTREGEXP = "!~*";
b800ac5a 659 } else {
a74f950c 660 $LIKE = "LIKE";
2b4c64e9 661 $NOTLIKE = "NOT LIKE";
a74f950c 662 $REGEXP = "REGEXP";
2b4c64e9 663 $NOTREGEXP = "NOT REGEXP";
c4c57597 664 }
665
b800ac5a 666 $messagesearch = "";
667 $subjectsearch = "";
668
b800ac5a 669
670 foreach ($searchterms as $searchterm) {
8b9c7aa0 671 if (strlen($searchterm) < 2) {
672 continue;
673 }
b800ac5a 674 if ($messagesearch) {
675 $messagesearch .= " AND ";
676 }
b800ac5a 677 if ($subjectsearch) {
678 $subjectsearch .= " AND ";
679 }
63d99fcb 680
a74f950c 681 if (substr($searchterm,0,1) == "+") {
682 $searchterm = substr($searchterm,1);
63d99fcb 683 $messagesearch .= " p.message $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
684 $subjectsearch .= " p.subject $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
a74f950c 685 } else if (substr($searchterm,0,1) == "-") {
686 $searchterm = substr($searchterm,1);
2b4c64e9 687 $messagesearch .= " p.message $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
688 $subjectsearch .= " p.subject $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
63d99fcb 689 } else {
690 $messagesearch .= " p.message $LIKE '%$searchterm%' ";
691 $subjectsearch .= " p.subject $LIKE '%$searchterm%' ";
692 }
b800ac5a 693 }
694
8b9c7aa0 695 $selectsql = "{$CFG->prefix}forum_posts p,
696 {$CFG->prefix}forum_discussions d,
697 {$CFG->prefix}user u,
4cabd355 698 {$CFG->prefix}forum f $onlyvisibletable
8b9c7aa0 699 WHERE ($messagesearch OR $subjectsearch)
700 AND p.userid = u.id
701 AND p.discussion = d.id
702 AND d.course = '$courseid'
4cabd355 703 AND d.forum = f.id $notteacherforum $onlyvisible";
8b9c7aa0 704
705 $totalcount = count_records_sql("SELECT COUNT(*) FROM $selectsql");
b800ac5a 706
8b9c7aa0 707 return get_records_sql("SELECT p.*,u.firstname,u.lastname,u.email,u.picture FROM
708 $selectsql ORDER BY p.modified DESC $limit");
9fa49e22 709}
710
63d99fcb 711
9fa49e22 712function forum_get_ratings($postid, $sort="u.firstname ASC") {
713/// Returns a list of ratings for a particular post - sorted.
714 global $CFG;
715 return get_records_sql("SELECT u.*, r.rating, r.time
716 FROM {$CFG->prefix}forum_ratings r,
717 {$CFG->prefix}user u
1f48942e 718 WHERE r.post = '$postid'
ebc3bd2b 719 AND r.userid = u.id
9fa49e22 720 ORDER BY $sort");
721}
722
1f48942e 723function forum_get_unmailed_posts($cutofftime) {
724/// Returns a list of all new posts that have not been mailed yet
725 global $CFG;
726 return get_records_sql("SELECT p.*, d.course
727 FROM {$CFG->prefix}forum_posts p,
728 {$CFG->prefix}forum_discussions d
729 WHERE p.mailed = 0
730 AND p.created < '$cutofftime'
731 AND p.discussion = d.id");
732}
733
734function forum_get_user_posts($forumid, $userid) {
735/// Get all the posts for a user in a forum suitable for forum_print_post
736 global $CFG;
737
ebc3bd2b 738 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 739 FROM {$CFG->prefix}forum f,
740 {$CFG->prefix}forum_discussions d,
741 {$CFG->prefix}forum_posts p,
742 {$CFG->prefix}user u
743 WHERE f.id = '$forumid'
744 AND d.forum = f.id
745 AND p.discussion = d.id
ebc3bd2b 746 AND p.userid = '$userid'
747 AND p.userid = u.id
1f48942e 748 ORDER BY p.modified ASC");
749}
750
751function forum_get_post_from_log($log) {
752/// Given a log entry, return the forum post details for it.
753 global $CFG;
754
755 if ($log->action == "add post") {
756
2f9c977e 757 return get_record_sql("SELECT p.*, d.forum, d.groupid, u.firstname, u.lastname, u.email, u.picture
1f48942e 758 FROM {$CFG->prefix}forum_discussions d,
759 {$CFG->prefix}forum_posts p,
760 {$CFG->prefix}user u
761 WHERE p.id = '$log->info'
762 AND d.id = p.discussion
ebc3bd2b 763 AND p.userid = u.id
1f48942e 764 AND u.deleted <> '1'");
765
766
767 } else if ($log->action == "add discussion") {
768
2f9c977e 769 return get_record_sql("SELECT p.*, d.forum, d.groupid, u.firstname, u.lastname, u.email, u.picture
1f48942e 770 FROM {$CFG->prefix}forum_discussions d,
771 {$CFG->prefix}forum_posts p,
772 {$CFG->prefix}user u
773 WHERE d.id = '$log->info'
774 AND d.firstpost = p.id
ebc3bd2b 775 AND p.userid = u.id
1f48942e 776 AND u.deleted <> '1'");
777 }
778 return NULL;
779}
780
d05956ac 781function forum_get_firstpost_from_discussion($discussionid) {
782/// Given a discussion id, return the first post from the discussion
783 global $CFG;
784
785 return get_record_sql("SELECT p.*
786 FROM {$CFG->prefix}forum_discussions d,
787 {$CFG->prefix}forum_posts p
788 WHERE d.id = '$discussionid'
789 AND d.firstpost = p.id ");
790}
791
1f48942e 792
793function forum_get_user_grades($forumid) {
794/// Get all user grades for a forum
795 global $CFG;
796
ebc3bd2b 797 return get_records_sql("SELECT r.id, p.userid, r.rating
1f48942e 798 FROM {$CFG->prefix}forum_discussions d,
799 {$CFG->prefix}forum_posts p,
800 {$CFG->prefix}forum_ratings r
801 WHERE d.forum = '$forumid'
802 AND p.discussion = d.id
02ebf404 803 AND r.post = p.id
804 ORDER by p.userid ");
1f48942e 805}
806
807
808function forum_count_discussion_replies($forum="0") {
809// Returns an array of counts of replies to each discussion (optionally in one forum)
810 global $CFG;
811
812 if ($forum) {
813 $forumselect = " AND d.forum = '$forum'";
814 }
815 return get_records_sql("SELECT p.discussion, (count(*)) as replies
816 FROM {$CFG->prefix}forum_posts p,
817 {$CFG->prefix}forum_discussions d
818 WHERE p.parent > 0
819 AND p.discussion = d.id
820 GROUP BY p.discussion");
821}
822
823function forum_count_unrated_posts($discussionid, $userid) {
824// How many unrated posts are in the given discussion for a given user?
825 global $CFG;
826 if ($posts = get_record_sql("SELECT count(*) as num
827 FROM {$CFG->prefix}forum_posts
828 WHERE parent > 0
829 AND discussion = '$discussionid'
ebc3bd2b 830 AND userid <> '$userid' ")) {
1f48942e 831
832 if ($rated = get_record_sql("SELECT count(*) as num
833 FROM {$CFG->prefix}forum_posts p,
834 {$CFG->prefix}forum_ratings r
835 WHERE p.discussion = '$discussionid'
836 AND p.id = r.post
ebc3bd2b 837 AND r.userid = '$userid'")) {
1f48942e 838 $difference = $posts->num - $rated->num;
839 if ($difference > 0) {
840 return $difference;
841 } else {
842 return 0; // Just in case there was a counting error
843 }
844 } else {
845 return $posts->num;
846 }
847 } else {
848 return 0;
849 }
850}
851
9197e147 852function forum_get_discussions($forum="0", $forumsort="d.timemodified DESC",
853 $user=0, $fullpost=true, $currentgroup=0) {
1f48942e 854/// Get all discussions in a forum
855 global $CFG;
856
857 if ($user) {
858 $userselect = " AND u.id = '$user' ";
859 } else {
860 $userselect = "";
861 }
02509fe6 862 if ($currentgroup) {
863 $groupselect = " AND d.groupid = '$currentgroup' ";
864 } else {
865 $groupselect = "";
866 }
29507631 867 if (empty($forumsort)) {
868 $forumsort = "d.timemodified DESC";
869 }
2ab968e9 870 if (empty($fullpost)) {
b879effb 871 $postdata = "p.id,p.subject,p.modified,p.discussion,p.userid";
2ab968e9 872 } else {
873 $postdata = "p.*";
874 }
9197e147 875
016cd6af 876 return get_records_sql("SELECT $postdata, d.timemodified, d.usermodified,
877 u.firstname, u.lastname, u.email, u.picture
1f48942e 878 FROM {$CFG->prefix}forum_discussions d,
9197e147 879 {$CFG->prefix}forum_posts p,
02509fe6 880 {$CFG->prefix}user u
1f48942e 881 WHERE d.forum = '$forum'
882 AND p.discussion = d.id
2ab968e9 883 AND p.parent = 0
9197e147 884 AND p.userid = u.id $groupselect $userselect
29507631 885 ORDER BY $forumsort");
1f48942e 886}
887
888
889
890function forum_get_user_discussions($courseid, $userid) {
891/// Get all discussions started by a particular user in a course
892 global $CFG;
893
894 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture,
ebc3bd2b 895 f.type as forumtype, f.name as forumname, f.id as forumid
1f48942e 896 FROM {$CFG->prefix}forum_discussions d,
897 {$CFG->prefix}forum_posts p,
898 {$CFG->prefix}user u,
899 {$CFG->prefix}forum f
900 WHERE d.course = '$courseid'
901 AND p.discussion = d.id
902 AND p.parent = 0
ebc3bd2b 903 AND p.userid = u.id
1f48942e 904 AND u.id = '$userid'
905 AND d.forum = f.id
b8bf90c5 906 ORDER BY p.created DESC");
1f48942e 907}
908
909
910function forum_subscribed_users($course, $forum) {
911/// Returns list of user objects that are subscribed to this forum
912 global $CFG;
913
adaf3928 914 if ($forum->forcesubscribe) {
915 if ($course->category) {
170a9c46 916 if ($forum->type == "teacher") {
917 return get_course_teachers($course->id); // Only teachers can be subscribed to teacher forums
918 } else {
919 return get_course_users($course->id); // Otherwise get everyone in the course
920 }
adaf3928 921 } else {
2d0b30a0 922 return get_site_users();
1f48942e 923 }
924 }
c3862e8f 925 return get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, u.emailstop,
0351b1f9 926 u.email, u.city, u.country, u.lastaccess, u.lastlogin, u.picture, u.timezone, u.lang
1f48942e 927 FROM {$CFG->prefix}user u,
928 {$CFG->prefix}forum_subscriptions s
929 WHERE s.forum = '$forum->id'
ebc3bd2b 930 AND s.userid = u.id
7c81b6e2 931 AND u.deleted <> 1
932 ORDER BY u.email ASC");
1f48942e 933}
9fa49e22 934
caadf009 935/// OTHER FUNCTIONS ///////////////////////////////////////////////////////////
f93f848a 936
937
11b0c469 938function forum_get_course_forum($courseid, $type) {
939// How to set up special 1-per-course forums
a6fcdf98 940 global $CFG;
941
29cbd93a 942 if ($forums = get_records_select("forum", "course = '$courseid' AND type = '$type'", "id ASC")) {
943 // There should always only be ONE, but with the right combination of
944 // errors there might be more. In this case, just return the oldest one (lowest ID).
945 foreach ($forums as $forum) {
946 return $forum; // ie the first one
11b0c469 947 }
8daaf761 948 }
e6874d9f 949
8daaf761 950 // Doesn't exist, so create one now.
951 $forum->course = $courseid;
952 $forum->type = "$type";
953 switch ($forum->type) {
954 case "news":
65a3ef30 955 $forum->name = addslashes(get_string("namenews", "forum"));
956 $forum->intro = addslashes(get_string("intronews", "forum"));
8daaf761 957 $forum->forcesubscribe = 1;
8daaf761 958 $forum->open = 1; // 0 - no, 1 - posts only, 2 - discuss and post
959 $forum->assessed = 0;
960 if ($site = get_site()) {
961 if ($courseid == $site->id) {
962 $forum->name = get_string("sitenews");
963 $forum->forcesubscribe = 0;
964 }
a6fcdf98 965 }
8daaf761 966 break;
967 case "social":
65a3ef30 968 $forum->name = addslashes(get_string("namesocial", "forum"));
969 $forum->intro = addslashes(get_string("introsocial", "forum"));
8daaf761 970 $forum->open = 2; // 0 - no, 1 - posts only, 2 - discuss and post
971 $forum->assessed = 0;
972 $forum->forcesubscribe = 0;
973 break;
974 case "teacher":
65a3ef30 975 $forum->name = addslashes(get_string("nameteacher", "forum"));
976 $forum->intro = addslashes(get_string("introteacher", "forum"));
8daaf761 977 $forum->open = 0; // 0 - no, 1 - posts only, 2 - discuss and post
978 $forum->assessed = 0;
979 $forum->forcesubscribe = 0;
980 break;
981 default:
982 notify("That forum type doesn't exist!");
983 return false;
984 break;
985 }
986
987 $forum->timemodified = time();
988 $forum->id = insert_record("forum", $forum);
989
990 if ($forum->type != "teacher") {
991 if (! $module = get_record("modules", "name", "forum")) {
992 notify("Could not find forum module!!");
993 return false;
994 }
995 $mod->course = $courseid;
996 $mod->module = $module->id;
997 $mod->instance = $forum->id;
998 $mod->section = 0;
999 if (! $mod->coursemodule = add_course_module($mod) ) { // assumes course/lib.php is loaded
1000 notify("Could not add a new course module to the course '$course->fullname'");
1001 return false;
1002 }
1003 if (! $sectionid = add_mod_to_section($mod) ) { // assumes course/lib.php is loaded
1004 notify("Could not add the new course module to that section");
1005 return false;
1006 }
1007 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
1008 notify("Could not update the course module with the correct section");
1009 return false;
1010 }
1011 include_once("$CFG->dirroot/course/lib.php");
d769d2ee 1012 rebuild_course_cache($courseid);
82aa0e8d 1013 }
8daaf761 1014
1015 return get_record("forum", "id", "$forum->id");
82aa0e8d 1016}
1017
f93f848a 1018
11b0c469 1019function forum_make_mail_post(&$post, $user, $touser, $course,
1020 $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
501cdbd8 1021// Given the data about a posting, builds up the HTML to display it and
1022// returns the HTML in a string. This is designed for sending via HTML email.
1023
1024 global $THEME, $CFG;
1025
0d851f90 1026 static $formattedtext; // Cached version of formatted text for a post
1027 static $formattedtextid; // The ID number of the post
1028
1029 if (empty($formattedtextid) or $formattedtextid != $post->id) { // Recalculate the formatting
1030 $formattedtext = format_text($post->message, $post->format, NULL, $course->id);
1031 $formattedtextid = $post->id;
1032 }
1033
501cdbd8 1034 $output = "";
1035
218b4433 1036 $output .= "<style> <!--"; /// Styles for autolinks
94d712f1 1037 $output .= "a.autolink:link {text-decoration: none; color: black; background-color: $THEME->autolink}\n";
1038 $output .= "a.autolink:visited {text-decoration: none; color: black; background-color: $THEME->autolink}\n";
218b4433 1039 $output .= "a.autolink:hover {text-decoration: underline; color: red}\n";
1040 $output .= "--> </style>\n\n";
1041
501cdbd8 1042 if ($post->parent) {
72d497d4 1043 $output .= '<table border="0" cellpadding="1" cellspacing="1"><tr><td bgcolor="#888888">';
1044 $output .= '<table border="0" cellpadding="3" cellspacing="0">';
501cdbd8 1045 } else {
72d497d4 1046 $output .= '<table border="0" cellpadding="1" cellspacing="1" width="100%"><tr><td bgcolor="#888888">';
1047 $output .= '<table border="0" cellpadding="3" cellspacing="0" width="100%">';
501cdbd8 1048 }
1049
72d497d4 1050 $output .= "<tr><td bgcolor=\"$THEME->cellcontent2\" width=\"35\" valign=\"top\">";
501cdbd8 1051 $output .= print_user_picture($user->id, $course->id, $user->picture, false, true);
72d497d4 1052 $output .= "</td>";
501cdbd8 1053
1054 if ($post->parent) {
72d497d4 1055 $output .= "<td nowrap bgcolor=\"$THEME->cellheading\">";
501cdbd8 1056 } else {
72d497d4 1057 $output .= "<td nowrap bgcolor=\"$THEME->cellheading2\">";
501cdbd8 1058 }
72d497d4 1059 $output .= "<p>";
1060 $output .= "<font size=3><b>$post->subject</b></font><br />";
1061 $output .= "<font size=2>";
1b26d5e7 1062
1063 $fullname = fullname($user, isteacher($course->id));
1064 $by->name = "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$fullname</a>";
d62413e8 1065 $by->date = userdate($post->modified, "", $touser->timezone);
ffe11640 1066 $output .= get_string("bynameondate", "forum", $by);
1b26d5e7 1067
72d497d4 1068 $output .= "</font></p></td></tr>";
1069 $output .= "<tr><td bgcolor=\"$THEME->cellcontent2\" width=10>";
501cdbd8 1070 $output .= "&nbsp;";
72d497d4 1071 $output .= "</td><td bgcolor=\"$THEME->cellcontent\">\n";
501cdbd8 1072
7f6689e4 1073 if ($post->attachment) {
1074 $post->course = $course->id;
1075 $post->forum = get_field("forum_discussions", "forum", "id", $post->discussion);
72d497d4 1076 $output .= "<div align=right>";
7f6689e4 1077 $output .= forum_print_attachments($post, "html");
72d497d4 1078 $output .= "</div>";
7f6689e4 1079 }
1080
0d851f90 1081 $output .= $formattedtext;
501cdbd8 1082
72d497d4 1083 $output .= "<p align=right><font size=-1>";
501cdbd8 1084
2e2e71a8 1085 if ($post->parent) {
ce45515e 1086 $output .= "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">".get_string("parent", "forum")."</a> | ";
2e2e71a8 1087 }
ce45515e 1088
501cdbd8 1089 $age = time() - $post->created;
1090 if ($ownpost) {
ce45515e 1091 $output .= "<a href=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."</a>";
501cdbd8 1092 if ($reply) {
02509fe6 1093 $output .= " | <a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("replyforum", "forum")."</a>";
501cdbd8 1094 }
1095 $output .= "&nbsp;&nbsp;";
1096 } else {
1097 if ($reply) {
02509fe6 1098 $output .= "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("replyforum", "forum")."</a>&nbsp;&nbsp;";
501cdbd8 1099 }
1100 }
1101
72d497d4 1102 $output .= "</p>";
1103 $output .= "<div align=right><p align=right>";
501cdbd8 1104
1105 if ($link) {
1106 if ($post->replies == 1) {
ffe11640 1107 $replystring = get_string("repliesone", "forum", $post->replies);
501cdbd8 1108 } else {
ffe11640 1109 $replystring = get_string("repliesmany", "forum", $post->replies);
501cdbd8 1110 }
72d497d4 1111 $output .= "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><B>".get_string("discussthistopic", "forum")."</b></a> ($replystring)&nbsp;&nbsp;";
501cdbd8 1112 }
72d497d4 1113 $output .= "</p></div>";
501cdbd8 1114 if ($footer) {
72d497d4 1115 $output .= "<p>$footer</p>";
501cdbd8 1116 }
72d497d4 1117 $output .= "</td></tr></table>\n";
1118 $output .= "</td></tr></table>\n\n";
501cdbd8 1119
1120 return $output;
1121}
1122
1123
98914efd 1124function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link=false,
1125 $ratings=NULL, $footer="", $highlight="") {
74f5d1e3 1126
c585fa17 1127 global $THEME, $USER, $CFG;
501cdbd8 1128
b8be40ce 1129 static $stredit, $strdelete, $strreply, $strparent, $threadedmode, $isteacher, $adminedit;
2e2e71a8 1130
1131 if (empty($stredit)) {
1132 $stredit = get_string("edit", "forum");
1133 $strdelete = get_string("delete", "forum");
1134 $strreply = get_string("reply", "forum");
1135 $strparent = get_string("parent", "forum");
1136 $threadedmode = (!empty($USER->mode) and ($USER->mode == FORUM_MODE_THREADED));
3bd98ad4 1137 $isteacher = isteacher($courseid);
b8be40ce 1138 $adminedit = (isadmin() and !empty($CFG->admineditalways));
2e2e71a8 1139 }
1140
b22b0e61 1141 echo "<a name=\"$post->id\"></a>";
501cdbd8 1142 if ($post->parent) {
72d497d4 1143 echo '<table border="0" cellpadding="3" cellspacing="0" class="forumpost">';
501cdbd8 1144 } else {
72d497d4 1145 echo '<table border="0" cellpadding="3" cellspacing="0" class="forumpost" width="100%">';
501cdbd8 1146 }
1147
72d497d4 1148 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
501cdbd8 1149 print_user_picture($post->userid, $courseid, $post->picture);
72d497d4 1150 echo "</td>";
501cdbd8 1151
1152 if ($post->parent) {
834f1c7f 1153 echo "<td bgcolor=\"$THEME->cellheading\" class=\"forumpostheader\" width=\"100%\">";
501cdbd8 1154 } else {
834f1c7f 1155 echo "<td bgcolor=\"$THEME->cellheading2\" class=\"forumpostheadertopic\" width=\"100%\">";
501cdbd8 1156 }
83ec9098 1157
1158 if (!empty($CFG->filterall)) { /// Put the subject through the filters
34e934a9 1159 $post->subject = filter_text("<nolink>$post->subject</nolink>", $courseid);
83ec9098 1160 }
72d497d4 1161 echo "<p>";
d7143408 1162 echo "<font size=3><b>$post->subject</b></font><br />";
834f1c7f 1163 echo "<font size=2>";
1b26d5e7 1164
3bd98ad4 1165 $fullname = fullname($post, $isteacher);
1b26d5e7 1166 $by->name = "<a href=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$fullname</a>";
d62413e8 1167 $by->date = userdate($post->modified);
ffe11640 1168 print_string("bynameondate", "forum", $by);
1b26d5e7 1169
72d497d4 1170 echo "</font></p></td></tr>";
507407a7 1171 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" valign=\"top\" class=\"forumpostside\" width=\"10\">";
1172 if ($group = user_group($courseid, $post->userid)) {
1173 print_group_picture($group, $courseid, false, false, false);
1174 } else {
1175 echo "&nbsp;";
1176 }
72d497d4 1177 echo "</td><td bgcolor=\"$THEME->cellcontent\" class=\"forumpostmessage\">\n";
501cdbd8 1178
7f6689e4 1179 if ($post->attachment) {
1180 $post->course = $courseid;
1181 $post->forum = get_field("forum_discussions", "forum", "id", $post->discussion);
72d497d4 1182 echo "<div align=\"right\">";
1183 $attachedimages = forum_print_attachments($post);
1184 echo "</div>";
e9c2dc1f 1185 } else {
1186 $attachedimages = "";
7f6689e4 1187 }
1188
5be7800c 1189 if ($link and (strlen(strip_tags($post->message)) > $CFG->forum_longpost)) {
aa153f29 1190 // Print shortened version
71767204 1191 echo format_text(forum_shorten_post($post->message), $post->format, NULL, $courseid);
c585fa17 1192 $numwords = count_words(strip_tags($post->message));
72d497d4 1193 echo "<p><a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">";
ffe11640 1194 echo get_string("readtherest", "forum");
72d497d4 1195 echo "</a> (".get_string("numwords", "", $numwords).")...</p>";
501cdbd8 1196 } else {
aa153f29 1197 // Print whole message
88438a58 1198 if ($highlight) {
71767204 1199 echo highlight($highlight, format_text($post->message, $post->format, NULL, $courseid));
88438a58 1200 } else {
71767204 1201 echo format_text($post->message, $post->format, NULL, $courseid);
88438a58 1202 }
c99ce77b 1203 echo $attachedimages;
501cdbd8 1204 }
1205
72d497d4 1206 echo "<p align=right><font size=-1>";
501cdbd8 1207
2e2e71a8 1208 if ($post->parent) {
1209 if ($threadedmode) {
1210 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">$strparent</a> | ";
1211 } else {
1212 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion#$post->parent\">$strparent</a> | ";
1213 }
1214 }
1215
501cdbd8 1216 $age = time() - $post->created;
b8be40ce 1217 if ($ownpost or $adminedit) {
1218 if (($age < $CFG->maxeditingtime) or $adminedit) {
2e2e71a8 1219 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?edit=$post->id\">$stredit</a> | ";
501cdbd8 1220 }
64eacd6f 1221 }
3bd98ad4 1222 if ($ownpost or $isteacher) {
2e2e71a8 1223 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">$strdelete</a>";
501cdbd8 1224 if ($reply) {
64eacd6f 1225 echo "| ";
1226 } else {
1227 echo "&nbsp;&nbsp;";
501cdbd8 1228 }
64eacd6f 1229 }
1230 if ($reply) {
2e2e71a8 1231 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">$strreply</a>";
501cdbd8 1232 echo "&nbsp;&nbsp;";
501cdbd8 1233 }
72d497d4 1234 echo "</p>";
501cdbd8 1235
72d497d4 1236 echo "<div align=right><p align=right>";
74f5d1e3 1237
1238 $ratingsmenuused = false;
02ebf404 1239 if (!empty($ratings) and !empty($USER->id)) {
98914efd 1240 $useratings = true;
1241 if ($ratings->assesstimestart and $ratings->assesstimefinish) {
1242 if ($post->created < $ratings->assesstimestart or $post->created > $ratings->assesstimefinish) {
1243 $useratings = false;
1244 }
1245 }
1246 if ($useratings) {
3bd98ad4 1247 $mypost = ($USER->id == $post->userid);
1248
1249 if (($isteacher or $ratings->assesspublic) and !$mypost) {
1250 forum_print_ratings_mean($post->id, $ratings->scale, $isteacher);
1251 forum_print_rating_menu($post->id, $USER->id, $ratings->scale);
1252 $ratingsmenuused = true;
1253
1254 } else if ($mypost) {
1255 forum_print_ratings_mean($post->id, $ratings->scale, true);
1256
98914efd 1257 } else if (!empty($ratings->allow) ) {
1258 forum_print_rating_menu($post->id, $USER->id, $ratings->scale);
74f5d1e3 1259 $ratingsmenuused = true;
2a3cda19 1260 }
501cdbd8 1261 }
1262 }
1263
1264 if ($link) {
1265 if ($post->replies == 1) {
ffe11640 1266 $replystring = get_string("repliesone", "forum", $post->replies);
501cdbd8 1267 } else {
ffe11640 1268 $replystring = get_string("repliesmany", "forum", $post->replies);
501cdbd8 1269 }
74f5d1e3 1270 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><b>".
1271 get_string("discussthistopic", "forum")."</b></a> ($replystring)&nbsp;&nbsp;";
501cdbd8 1272 }
72d497d4 1273 echo "</p>";
501cdbd8 1274 if ($footer) {
72d497d4 1275 echo "<p>$footer</p>";
501cdbd8 1276 }
72d497d4 1277 echo "</div>";
1278 echo "</td></tr>\n</table>\n\n";
74f5d1e3 1279
1280 return $ratingsmenuused;
501cdbd8 1281}
1282
3335f6fb 1283
2ab968e9 1284function forum_print_discussion_header(&$post, $courseid, $datestring="") {
c585fa17 1285 global $THEME, $USER, $CFG;
3335f6fb 1286
436a7cae 1287 if (!empty($CFG->filterall)) {
34e934a9 1288 $post->subject = filter_text("<nolink>$post->subject</nolink>", $courseid);
436a7cae 1289 }
1290
29507631 1291 echo "<tr class=\"forumpostheader\">";
3335f6fb 1292
29507631 1293 // Topic
546be6dd 1294 echo "<td bgcolor=\"$THEME->cellheading2\" class=\"forumpostheadertopic\" width=\"100%\">";
29507631 1295 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">$post->subject</a>";
1296 echo "</td>\n";
1297
1298 // Picture
96e301a7 1299 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderpicture\" width=35>";
3335f6fb 1300 print_user_picture($post->userid, $courseid, $post->picture);
29507631 1301 echo "</td>\n";
3335f6fb 1302
29507631 1303 // User name
1b26d5e7 1304 $fullname = fullname($post, isteacher($courseid));
96e301a7 1305 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheadername\" align=left nowrap>";
1b26d5e7 1306 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$fullname</a>";
29507631 1307 echo "</td>\n";
1308
1309 // Replies
96e301a7 1310 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderreplies\" align=center nowrap>";
2ab968e9 1311 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">$post->replies</a>";
29507631 1312 echo "</td>\n";
3335f6fb 1313
96e301a7 1314 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderdate\" align=right nowrap>";
29507631 1315 if (!empty($post->timemodified)) {
2ab968e9 1316 echo userdate($post->timemodified, $datestring);
3335f6fb 1317 } else {
2ab968e9 1318 echo userdate($post->modified, $datestring);
3335f6fb 1319 }
29507631 1320 echo "</td>\n";
1321
1322 echo "</tr>\n";
3335f6fb 1323
3335f6fb 1324}
1325
1326
aa153f29 1327function forum_shorten_post($message) {
c585fa17 1328// Given a post object that we already know has a long message
1329// this function truncates the message nicely to the first
5be7800c 1330// sane place between $CFG->forum_longpost and $CFG->forum_shortpost
1331
1332 global $CFG;
c585fa17 1333
1334 $i = 0;
1335 $tag = false;
1336 $length = strlen($message);
1337 $count = 0;
1338 $stopzone = false;
1339 $truncate = 0;
1340
1341 for ($i=0; $i<$length; $i++) {
a8afb411 1342 $char = $message[$i];
c585fa17 1343
1344 switch ($char) {
1345 case "<":
1346 $tag = true;
1347 break;
1348 case ">":
1349 $tag = false;
1350 break;
1351 default:
1352 if (!$tag) {
1353 if ($stopzone) {
67f0b4cc 1354 if ($char == ".") {
a8afb411 1355 $truncate = $i+1;
c585fa17 1356 break 2;
1357 }
1358 }
1359 $count++;
1360 }
a8afb411 1361 break;
c585fa17 1362 }
1363 if (!$stopzone) {
5be7800c 1364 if ($count > $CFG->forum_shortpost) {
c585fa17 1365 $stopzone = true;
1366 }
1367 }
1368 }
aa153f29 1369
c585fa17 1370 if (!$truncate) {
a8afb411 1371 $truncate = $i;
c585fa17 1372 }
1373
67f0b4cc 1374 return substr($message, 0, $truncate);
aa153f29 1375}
1376
501cdbd8 1377
3bd98ad4 1378function forum_print_ratings_mean($postid, $scale, $link=true) {
02ebf404 1379/// Print the multiple ratings on a post given to the current user by others.
1380/// Scale is an array of ratings
1381
1382 static $strrate;
05c47ef7 1383
1384 $mean = forum_get_ratings_mean($postid, $scale);
02ebf404 1385
05c47ef7 1386 if ($mean !== "") {
02ebf404 1387
1388 if (empty($strratings)) {
1389 $strratings = get_string("ratings", "forum");
501cdbd8 1390 }
501cdbd8 1391
02ebf404 1392 echo "$strratings: ";
3bd98ad4 1393 if ($link) {
1394 link_to_popup_window ("/mod/forum/report.php?id=$postid", "ratings", $mean, 400, 600);
1395 } else {
1396 echo "$mean ";
1397 }
501cdbd8 1398 }
1399}
1400
501cdbd8 1401
0761d83f 1402function forum_get_ratings_mean($postid, $scale, $ratings=NULL) {
1403/// Return the mean rating of a post given to the current user by others.
02ebf404 1404/// Scale is an array of possible ratings in the scale
1405/// Ratings is an optional simple array of actual ratings (just integers)
1406
1407 if (!$ratings) {
1408 $ratings = array();
1409 if ($rates = get_records("forum_ratings", "post", $postid)) {
1410 foreach ($rates as $rate) {
1411 $ratings[] = $rate->rating;
1412 }
501cdbd8 1413 }
501cdbd8 1414 }
02ebf404 1415
0761d83f 1416 $count = count($ratings);
1417
1418 if ($count == 0) {
02ebf404 1419 return "";
02ebf404 1420
0761d83f 1421 } else if ($count == 1) {
1422 return $scale[$ratings[0]];
1423
02ebf404 1424 } else {
0761d83f 1425 $total = 0;
1426 foreach ($ratings as $rating) {
1427 $total += $rating;
1428 }
1429 $mean = round( ((float)$total/(float)$count) + 0.001); // Little fudge factor so that 0.5 goes UP
1430
1431 if (isset($scale[$mean])) {
1432 return $scale[$mean]." ($count)";
1433 } else {
1434 return "$mean ($count)"; // Should never happen, hopefully
1435 }
02ebf404 1436 }
1437}
1438
1439function forum_get_ratings_summary($postid, $scale, $ratings=NULL) {
1440/// Return a summary of post ratings given to the current user by others.
1441/// Scale is an array of possible ratings in the scale
1442/// Ratings is an optional simple array of actual ratings (just integers)
1443
1444 if (!$ratings) {
1445 $ratings = array();
1446 if ($rates = get_records("forum_ratings", "post", $postid)) {
1447 foreach ($rates as $rate) {
1448 $rating[] = $rate->rating;
1449 }
1450 }
1451 }
1452
1453
1454 if (!$count = count($ratings)) {
1455 return "";
1456 }
1457
1458
1459 foreach ($scale as $key => $scaleitem) {
1460 $sumrating[$key] = 0;
1461 }
1462
1463 foreach ($ratings as $rating) {
1464 $sumrating[$rating]++;
1465 }
1466
1467 $summary = "";
1468 foreach ($scale as $key => $scaleitem) {
1469 $summary = $sumrating[$key].$summary;
1470 if ($key > 1) {
1471 $summary = "/$summary";
1472 }
1473 }
1474 return $summary;
1475}
1476
1477function forum_print_rating_menu($postid, $userid, $scale) {
1478/// Print the menu of ratings as part of a larger form.
1479/// If the post has already been - set that value.
1480/// Scale is an array of ratings
1481
1482 static $strrate;
1483
1484 if (!$rating = get_record("forum_ratings", "userid", $userid, "post", $postid)) {
1485 $rating->rating = 0;
1486 }
1487
1488 if (empty($strrate)) {
1489 $strrate = get_string("rate", "forum");
1490 }
1491
1492 choose_from_menu($scale, $postid, $rating->rating, "$strrate...");
501cdbd8 1493}
1494
7a12aab4 1495function forum_print_mode_form($discussion, $mode) {
1496 GLOBAL $FORUM_LAYOUT_MODES;
501cdbd8 1497
8b9c7aa0 1498 echo "<center><p>";
7a12aab4 1499 popup_form("discuss.php?d=$discussion&mode=", $FORUM_LAYOUT_MODES, "mode", $mode, "");
8b9c7aa0 1500 echo "</p></center>\n";
501cdbd8 1501}
1502
97485d07 1503function forum_print_search_form($course, $search="", $return=false, $type="") {
501cdbd8 1504 global $CFG;
1505
97485d07 1506 if ($type == "plain") {
8b9c7aa0 1507 $output = "<table border=0 cellpadding=0 cellspacing=0><tr><td nowrap>";
1508 $output .= "<form name=search action=\"$CFG->wwwroot/mod/forum/search.php\">";
1509 $output .= "<font size=\"-1\">";
1510 $output .= "<input name=search type=text size=20 value=\"$search\">";
1511 $output .= "<input value=\"".get_string("searchforums", "forum")."\" type=submit>";
1512 $output .= "</font>";
1513 $output .= "<input name=id type=hidden value=\"$course->id\">";
1514 $output .= "</form>";
1515 $output .= "</td></tr></table>";
97485d07 1516 } else {
8b9c7aa0 1517 $output = "<table border=0 cellpadding=10 cellspacing=0><tr><td align=center>";
1518 $output .= "<form name=search action=\"$CFG->wwwroot/mod/forum/search.php\">";
1519 $output .= "<font size=\"-1\">";
1520 $output .= "<input name=search type=text size=20 value=\"$search\"><br>";
1521 $output .= "<input value=\"".get_string("searchforums", "forum")."\" type=submit>";
1522 $output .= "</font>";
1523 $output .= "<input name=id type=hidden value=\"$course->id\">";
1524 $output .= "</form>";
1525 $output .= "</td></tr></table>";
97485d07 1526 }
5e367a2d 1527
1528 if ($return) {
1529 return $output;
1530 }
1531 echo $output;
501cdbd8 1532}
1533
1534
11b0c469 1535function forum_set_return() {
607809b3 1536 global $CFG, $SESSION;
501cdbd8 1537
28e1e8b9 1538 if (! isset($SESSION->fromdiscussion)) {
48d38fad 1539 if (!empty($_SERVER['HTTP_REFERER'])) {
1540 $referer = $_SERVER['HTTP_REFERER'];
1541 } else {
1542 $referer = "";
1543 }
28e1e8b9 1544 // If the referer is NOT a login screen then save it.
48d38fad 1545 if (! strncasecmp("$CFG->wwwroot/login", $referer, 300)) {
607809b3 1546 $SESSION->fromdiscussion = $_SERVER["HTTP_REFERER"];
28e1e8b9 1547 }
501cdbd8 1548 }
1549}
1550
1551
11b0c469 1552function forum_go_back_to($default) {
501cdbd8 1553 global $SESSION;
1554
9c9f7d77 1555 if (!empty($SESSION->fromdiscussion)) {
501cdbd8 1556 $returnto = $SESSION->fromdiscussion;
1557 unset($SESSION->fromdiscussion);
1558 return $returnto;
1559 } else {
1560 return $default;
1561 }
1562}
1563
7f6689e4 1564function forum_file_area_name($post) {
1565// Creates a directory file name, suitable for make_upload_directory()
1566 global $CFG;
1567
1568 return "$post->course/$CFG->moddata/forum/$post->forum/$post->id";
1569}
1570
1571function forum_file_area($post) {
1572 return make_upload_directory( forum_file_area_name($post) );
1573}
1574
1575function forum_delete_old_attachments($post, $exception="") {
1576// Deletes all the user files in the attachments area for a post
1577// EXCEPT for any file named $exception
1578
1579 if ($basedir = forum_file_area($post)) {
1580 if ($files = get_directory_list($basedir)) {
1581 foreach ($files as $file) {
1582 if ($file != $exception) {
1583 unlink("$basedir/$file");
1584 notify("Existing file '$file' has been deleted!");
1585 }
1586 }
1587 }
1588 if (!$exception) { // Delete directory as well, if empty
1589 rmdir("$basedir");
1590 }
1591 }
1592}
1593
cc2b7ea5 1594function forum_move_attachments($discussion, $forumid) {
1595/// Given a discussion object that is being moved to forumid,
1596/// this function checks all posts in that discussion
1597/// for attachments, and if any are found, these are
1598/// moved to the new forum directory.
1599
1600 global $CFG;
1601
1602 $return = true;
1603
1604 if ($posts = get_records_select("forum_posts", "discussion = '$discussion->id' AND attachment <> ''")) {
1605 foreach ($posts as $oldpost) {
1606 $oldpost->course = $discussion->course;
1607 $oldpost->forum = $discussion->forum;
1608 $oldpostdir = "$CFG->dataroot/".forum_file_area_name($oldpost);
1609 if (is_dir($oldpostdir)) {
1610 $newpost = $oldpost;
1611 $newpost->forum = $forumid;
7f0c3a44 1612 $newpostdir = forum_file_area($newpost);
cc2b7ea5 1613 if (! @rename($oldpostdir, $newpostdir)) {
1614 $return = false;
1615 }
1616 }
1617 }
1618 }
1619 return $return;
1620}
1621
7f6689e4 1622function forum_print_attachments($post, $return=NULL) {
1623// if return=html, then return a html string.
1624// if return=text, then return a text-only string.
72d497d4 1625// otherwise, print HTML for non-images, and return image HTML
7f6689e4 1626
1627 global $CFG;
1628
1629 $filearea = forum_file_area_name($post);
1630
72d497d4 1631 $imagereturn = "";
1632 $output = "";
1633
7f6689e4 1634 if ($basedir = forum_file_area($post)) {
1635 if ($files = get_directory_list($basedir)) {
1636 $strattachment = get_string("attachment", "forum");
6afe8558 1637 $strpopupwindow = get_string("popupwindow");
7f6689e4 1638 foreach ($files as $file) {
1639 $icon = mimeinfo("icon", $file);
1640 if ($CFG->slasharguments) {
1641 $ffurl = "file.php/$filearea/$file";
1642 } else {
1643 $ffurl = "file.php?file=/$filearea/$file";
1644 }
096b5432 1645 $image = "<img border=\"0\" src=\"$CFG->pixpath/f/$icon\" height=\"16\" width=\"16\" alt=\"$strpopupwindow\">";
7f6689e4 1646
1647 if ($return == "html") {
6afe8558 1648 $output .= "<a href=\"$CFG->wwwroot/$ffurl\">$image</a> ";
1649 $output .= "<a href=\"$CFG->wwwroot/$ffurl\">$file</a><br />";
7f6689e4 1650
1651 } else if ($return == "text") {
1652 $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
1653
1654 } else {
72d497d4 1655 if ($icon == "image.gif") { // Image attachments don't get printed as links
1656 $imagereturn .= "<br /><img src=\"$CFG->wwwroot/$ffurl\">";
1657 } else {
1658 link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
1659 echo "<a href=\"$CFG->wwwroot/$ffurl\">$file</a>";
1660 echo "<br />";
1661 }
7f6689e4 1662 }
1663 }
1664 }
1665 }
72d497d4 1666
7f6689e4 1667 if ($return) {
1668 return $output;
1669 }
72d497d4 1670
1671 return $imagereturn;
7f6689e4 1672}
1673
1674function forum_add_attachment($post, $newfile) {
1675// $post is a full post record, including course and forum
3b7c1de9 1676// $newfile is a full upload array from $_FILES
7f6689e4 1677// If successful, this function returns the name of the file
1678
9dd0b378 1679 global $CFG;
1680
3b7c1de9 1681 if (empty($newfile['name'])) {
7f6689e4 1682 return "";
1683 }
1684
4909e176 1685 if (!$forum = get_record("forum", "id", $post->forum)) {
1686 return "";
1687 }
1688
1689 if (!$course = get_record("course", "id", $forum->course)) {
1690 return "";
1691 }
1692
1693 $maxbytes = get_max_upload_file_size($CFG->maxbytes, $course->maxbytes, $forum->maxbytes);
1694
7f6689e4 1695 $newfile_name = clean_filename($newfile['name']);
1696
1697 if (valid_uploaded_file($newfile)) {
4909e176 1698 if ($maxbytes and $newfile['size'] > $maxbytes) {
1699 return "";
1700 }
7f6689e4 1701 if (! $newfile_name) {
1702 notify("This file had a wierd filename and couldn't be uploaded");
1703
1704 } else if (! $dir = forum_file_area($post)) {
1705 notify("Attachment could not be stored");
1706 $newfile_name = "";
1707
1708 } else {
1709 if (move_uploaded_file($newfile['tmp_name'], "$dir/$newfile_name")) {
9dd0b378 1710 chmod("$dir/$newfile_name", $CFG->directorypermissions);
7f6689e4 1711 forum_delete_old_attachments($post, $newfile_name);
1712 } else {
1713 notify("An error happened while saving the file on the server");
1714 $newfile_name = "";
1715 }
1716 }
1717 } else {
1718 $newfile_name = "";
1719 }
1720
1721 return $newfile_name;
1722}
501cdbd8 1723
11b0c469 1724function forum_add_new_post($post) {
501cdbd8 1725
ffe11640 1726 $post->created = $post->modified = time();
501cdbd8 1727 $post->mailed = "0";
1728
7f6689e4 1729 $newfile = $post->attachment;
1730 $post->attachment = "";
1731
1732 if (! $post->id = insert_record("forum_posts", $post)) {
1733 return false;
1734 }
1735
1736 if ($post->attachment = forum_add_attachment($post, $newfile)) {
1737 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id);
1738 }
29507631 1739
1740 // Update discussion modified date
1741 set_field("forum_discussions", "timemodified", $post->modified, "id", $post->discussion);
016cd6af 1742 set_field("forum_discussions", "usermodified", $post->userid, "id", $post->discussion);
7f6689e4 1743
1744 return $post->id;
501cdbd8 1745}
1746
11b0c469 1747function forum_update_post($post) {
501cdbd8 1748
ffe11640 1749 $post->modified = time();
501cdbd8 1750
0ab85112 1751 if (!$post->parent) { // Post is a discussion starter - update discussion title too
1752 set_field("forum_discussions", "name", $post->subject, "id", $post->discussion);
1753 }
29507631 1754
7f6689e4 1755 if ($newfilename = forum_add_attachment($post, $post->attachment)) {
1756 $post->attachment = $newfilename;
1757 } else {
1758 unset($post->attachment);
1759 }
29507631 1760
1761 // Update discussion modified date
1762 set_field("forum_discussions", "timemodified", $post->modified, "id", $post->discussion);
016cd6af 1763 set_field("forum_discussions", "usermodified", $post->userid, "id", $post->discussion);
29507631 1764
ffe11640 1765 return update_record("forum_posts", $post);
501cdbd8 1766}
1767
1768function forum_add_discussion($discussion) {
1769// Given an object containing all the necessary data,
1770// create a new discussion and return the id
1771
1772 GLOBAL $USER;
1773
1774 $timenow = time();
1775
1776 // The first post is stored as a real post, and linked
1777 // to from the discuss entry.
1778
1779 $post->discussion = 0;
1780 $post->parent = 0;
ebc3bd2b 1781 $post->userid = $USER->id;
501cdbd8 1782 $post->created = $timenow;
1783 $post->modified = $timenow;
1784 $post->mailed = 0;
1785 $post->subject = $discussion->name;
1786 $post->message = $discussion->intro;
7f6689e4 1787 $post->attachment = "";
1788 $post->forum = $discussion->forum;
1789 $post->course = $discussion->course;
9f0b8269 1790 $post->format = $discussion->format;
501cdbd8 1791
1792 if (! $post->id = insert_record("forum_posts", $post) ) {
1793 return 0;
1794 }
1795
7f6689e4 1796 if ($post->attachment = forum_add_attachment($post, $discussion->attachment)) {
1797 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id); //ignore errors
1798 }
1799
02509fe6 1800 // Now do the main entry for the discussion,
1801 // linking to this first post
04eba58f 1802
caadf009 1803 $discussion->firstpost = $post->id;
1804 $discussion->timemodified = $timenow;
016cd6af 1805 $discussion->usermodified = $post->userid;
04eba58f 1806
caadf009 1807 if (! $discussion->id = insert_record("forum_discussions", $discussion) ) {
1808 delete_records("forum_posts", "id", $post->id);
1809 return 0;
04eba58f 1810 }
1811
caadf009 1812 // Finally, set the pointer on the post.
1813 if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) {
1814 delete_records("forum_posts", "id", $post->id);
1815 delete_records("forum_discussions", "id", $discussion->id);
1816 return 0;
04eba58f 1817 }
04eba58f 1818
caadf009 1819 return $discussion->id;
1820}
04eba58f 1821
04eba58f 1822
caadf009 1823function forum_delete_discussion($discussion) {
1824// $discussion is a discussion record object
04eba58f 1825
1826 $result = true;
1827
caadf009 1828 if ($posts = get_records("forum_posts", "discussion", $discussion->id)) {
1829 foreach ($posts as $post) {
1830 $post->course = $discussion->course;
1831 $post->forum = $discussion->forum;
1832 if (! delete_records("forum_ratings", "post", "$post->id")) {
1833 $result = false;
1834 }
1835 if (! forum_delete_post($post)) {
04eba58f 1836 $result = false;
1837 }
1838 }
1839 }
1840
caadf009 1841 if (! delete_records("forum_discussions", "id", "$discussion->id")) {
04eba58f 1842 $result = false;
1843 }
1844
1845 return $result;
1846}
1847
1848
caadf009 1849function forum_delete_post($post) {
1850 if (delete_records("forum_posts", "id", $post->id)) {
1851 delete_records("forum_ratings", "post", $post->id); // Just in case
1852 if ($post->attachment) {
1853 $discussion = get_record("forum_discussions", "id", $post->discussion);
1854 $post->course = $discussion->course;
1855 $post->forum = $discussion->forum;
1856 forum_delete_old_attachments($post);
1857 }
1858 return true;
1859 }
1860 return false;
1861}
501cdbd8 1862
501cdbd8 1863
caadf009 1864function forum_print_user_discussions($courseid, $userid) {
1865 global $CFG, $USER;
501cdbd8 1866
b8bf90c5 1867 $maxdiscussions = 10;
1868 $countdiscussions = 0;
1869
1f48942e 1870
1871 if ($discussions = forum_get_user_discussions($courseid, $userid)) {
caadf009 1872 $user = get_record("user", "id", $userid);
1873 echo "<HR>";
1b26d5e7 1874 $fullname = fullname($user, isteacher($courseid));
1875 print_heading( get_string("discussionsstartedbyrecent", "forum", $fullname) );
caadf009 1876 $replies = forum_count_discussion_replies();
1877 foreach ($discussions as $discussion) {
b8bf90c5 1878 $countdiscussions++;
1879 if ($countdiscussions > $maxdiscussions) {
1880 break;
1881 }
caadf009 1882 if (($discussion->forumtype == "teacher") and !isteacher($courseid)) {
ffe11640 1883 continue;
1884 }
47f1da80 1885 if (!empty($replies[$discussion->discussion])) {
caadf009 1886 $discussion->replies = $replies[$discussion->discussion]->replies;
1887 } else {
1888 $discussion->replies = 0;
501cdbd8 1889 }
caadf009 1890 $inforum = get_string("inforum", "forum", "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname</A>");
1891 $discussion->subject .= " ($inforum)";
61e96406 1892 if (!empty($USER->id)) {
1893 $ownpost = ($discussion->userid == $USER->id);
1894 } else {
1895 $ownpost = false;
1896 }
caadf009 1897 forum_print_post($discussion, $courseid, $ownpost, $reply=0, $link=1, $assessed=false);
1898 echo "<BR>\n";
501cdbd8 1899 }
1900 }
501cdbd8 1901}
1902
501cdbd8 1903function forum_forcesubscribe($forumid, $value=1) {
1904 return set_field("forum", "forcesubscribe", $value, "id", $forumid);
1905}
1906
1907function forum_is_forcesubscribed($forumid) {
1908 return get_field("forum", "forcesubscribe", "id", $forumid);
1909}
1910
1911function forum_is_subscribed($userid, $forumid) {
1912 if (forum_is_forcesubscribed($forumid)) {
1913 return true;
1914 }
ebc3bd2b 1915 return record_exists("forum_subscriptions", "userid", $userid, "forum", $forumid);
86970225 1916}
1917
501cdbd8 1918function forum_subscribe($userid, $forumid) {
9fa49e22 1919/// Adds user to the subscriber list
1920
ebc3bd2b 1921 $sub->userid = $userid;
9fa49e22 1922 $sub->forum = $forumid;
501cdbd8 1923
9fa49e22 1924 return insert_record("forum_subscriptions", $sub);
501cdbd8 1925}
1926
1927function forum_unsubscribe($userid, $forumid) {
9fa49e22 1928/// Removes user from the subscriber list
ebc3bd2b 1929 return delete_records("forum_subscriptions", "userid", $userid, "forum", $forumid);
501cdbd8 1930}
1931
0a9f61b5 1932function forum_post_subscription($post) {
1933/// Given a new post, subscribes or unsubscribes as appropriate.
1934/// Returns some text which describes what happened.
1935
1936 global $USER;
1937
1938 if (empty($post->subscribe) and empty($post->unsubscribe)) {
1939 return "";
1940 }
1941
1942 if (!$forum = get_record("forum", "id", $post->forum)) {
1943 return "";
1944 }
1945
1946 $info->name = "$USER->firstname $USER->lastname";
1947 $info->forum = $forum->name;
1948
1949 if (!empty($post->subscribe)) {
1950 forum_subscribe($USER->id, $post->forum);
1951 return "<p>".get_string("nowsubscribed", "forum", $info)."</p>";
1952 }
1953
1954 forum_unsubscribe($USER->id, $post->forum);
1955 return "<p>".get_string("nownotsubscribed", "forum", $info)."</p>";
1956}
1957
501cdbd8 1958
11b0c469 1959function forum_user_has_posted_discussion($forumid, $userid) {
29507631 1960 if ($discussions = forum_get_discussions($forumid, "", $userid)) {
501cdbd8 1961 return true;
1962 } else {
1963 return false;
1964 }
1965}
1966
9197e147 1967function forum_user_can_post_discussion($forum, $currentgroup=false) {
501cdbd8 1968// $forum is an object
1969 global $USER;
1970
1971 if ($forum->type == "eachuser") {
11b0c469 1972 return (! forum_user_has_posted_discussion($forum->id, $USER->id));
501cdbd8 1973 } else if ($forum->type == "teacher") {
1974 return isteacher($forum->course);
9197e147 1975 } else if ($currentgroup) {
1976 return (isteacheredit($forum->course) or ismember($currentgroup));
501cdbd8 1977 } else if (isteacher($forum->course)) {
1978 return true;
1979 } else {
70c476a7 1980 return ($forum->open == 2);
501cdbd8 1981 }
1982}
1983
f690562f 1984function forum_user_can_post($forum, $user=NULL) {
1985// $forum, $user are objects
1986
1987 if ($user) {
1988 $isteacher = isteacher($forum->course, $user->id);
1989 } else {
1990 $isteacher = isteacher($forum->course);
1991 }
70c476a7 1992
1993 if ($forum->type == "teacher") {
f690562f 1994 return $isteacher;
1995 } else if ($isteacher) {
70c476a7 1996 return true;
1997 } else {
1998 return $forum->open;
1999 }
2000}
501cdbd8 2001
501cdbd8 2002
9197e147 2003function forum_print_latest_discussions($forum_id=0, $forum_numdiscussions=5,
2004 $forum_style="plain", $forum_sort="",
2005 $currentgroup=0) {
c585fa17 2006 global $CFG, $USER;
f93f848a 2007
2008 if ($forum_id) {
2009 if (! $forum = get_record("forum", "id", $forum_id)) {
2010 error("Forum ID was incorrect");
2011 }
2012 if (! $course = get_record("course", "id", $forum->course)) {
2013 error("Could not find the course this forum belongs to!");
2014 }
2015
2016 if ($course->category) {
2017 require_login($course->id);
2018 }
2019
2020 } else {
2021 if (! $course = get_record("course", "category", 0)) {
2022 error("Could not find a top-level course!");
2023 }
7beb45d8 2024 if (! $forum = forum_get_course_forum($course->id, "news")) {
f93f848a 2025 error("Could not find or create a main forum in this course (id $course->id)");
2026 }
2027 }
2028
9197e147 2029 if (forum_user_can_post_discussion($forum, $currentgroup)) {
b879effb 2030 echo "<p align=center>";
2031 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?forum=$forum->id\">";
0351b1f9 2032 if ($forum->type == "news") {
2033 echo get_string("addanewtopic", "forum")."</a>...";
2034 } else {
2035 echo get_string("addanewdiscussion", "forum")."</a>...";
2036 }
b879effb 2037 echo "</p>\n";
77305fe6 2038 }
2039
3c8a606d 2040 if ((!$forum_numdiscussions) && ($forum_style == "plain")) {
2041 $forum_style = "header"; // Abbreviate display by default
3335f6fb 2042 }
f93f848a 2043
2ab968e9 2044 if ($forum_style == "minimal") {
2045 $forum_sort = "p.modified DESC";
2046 }
2047
2048 $fullpost = false;
2049 if ($forum_style == "plain") {
2050 $fullpost = true;
2051 }
2052
9197e147 2053 if (! $discussions = forum_get_discussions($forum->id, $forum_sort, 0, $fullpost, $currentgroup) ) {
0351b1f9 2054 if ($forum->type == "news") {
2055 echo "<p align=center><b>(".get_string("nonews", "forum").")</b></p>";
2056 } else {
2057 echo "<p align=center><b>(".get_string("nodiscussions", "forum").")</b></p>";
2058 }
2ab968e9 2059 return;
2060 }
2061
3335f6fb 2062 $replies = forum_count_discussion_replies($forum->id);
f93f848a 2063
3260de67 2064 $canreply = forum_user_can_post($forum);
2065
3335f6fb 2066 $discussioncount = 0;
c20b762a 2067 $olddiscussionlink = false;
2ab968e9 2068 $strdatestring = get_string("strftimedaydatetime");
f93f848a 2069
dcde9f02 2070 if ($forum_style == "minimal") {
2071 $strftimerecent = get_string("strftimerecent");
2072 $strmore = get_string("more", "forum");
2073 }
2074
29507631 2075 if ($forum_style == "header") {
ba1ce4c0 2076 echo "<table width=\"100%\" border=0 cellpadding=3 cellspacing=1 class=\"forumheaderlist\">";
03aa0dfa 2077 echo "<tr class=\"forumpostheader\">";
9e0a08bd 2078 echo "<th>".get_string("discussion", "forum")."</th>";
29507631 2079 echo "<th colspan=2>".get_string("startedby", "forum")."</th>";
2080 echo "<th>".get_string("replies", "forum")."</th>";
2081 echo "<th>".get_string("lastpost", "forum")."</th>";
2082 echo "</tr>";
2083 }
2084
3335f6fb 2085 foreach ($discussions as $discussion) {
2086 $discussioncount++;
f93f848a 2087
3335f6fb 2088 if ($forum_numdiscussions && ($discussioncount > $forum_numdiscussions)) {
c20b762a 2089 $olddiscussionlink = true;
3335f6fb 2090 break;
2091 }
9c9f7d77 2092 if (!empty($replies[$discussion->discussion])) {
3335f6fb 2093 $discussion->replies = $replies[$discussion->discussion]->replies;
2094 } else {
2095 $discussion->replies = 0;
2096 }
f7477444 2097 if (!empty($USER->id)) {
2098 $ownpost = ($discussion->userid == $USER->id);
2099 } else {
2100 $ownpost=false;
2101 }
3335f6fb 2102 switch ($forum_style) {
2103 case "minimal":
436a7cae 2104 if (!empty($CFG->filterall)) {
2105 $discussion->subject = filter_text($discussion->subject, $forum->course);
2106 }
00363389 2107 echo "<p><span class=\"smallinfohead\">".userdate($discussion->modified, $strftimerecent)." - $discussion->firstname</span><br />";
2108 echo "<span class=\"smallinfo\">$discussion->subject ";
b879effb 2109 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->discussion\">";
00363389 2110 echo $strmore."...</a></span>";
b879effb 2111 echo "</p>\n";
3335f6fb 2112 break;
2113 case "header":
2ab968e9 2114 forum_print_discussion_header($discussion, $forum->course, $strdatestring);
3335f6fb 2115 break;
2116 default:
3260de67 2117 if ($canreply or $discussion->replies) {
2118 $link = true;
2119 } else {
2120 $link = false;
2121 }
2122 forum_print_post($discussion, $forum->course, $ownpost, $reply=0, $link, $assessed=false);
b879effb 2123 echo "<br>\n";
3335f6fb 2124 break;
f93f848a 2125 }
2126 }
29507631 2127
2128 if ($forum_style == "header") {
2129 echo "</table>";
2130 }
c20b762a 2131
2132 if ($olddiscussionlink) {
2133 echo "<p align=right><a href=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id&showall=1\">";
2134 echo get_string("olderdiscussions", "forum")."</a> ...</p>";
2135 }
f93f848a 2136}
2137
c6d691dc 2138function forum_print_discussion($course, $forum, $discussion, $post, $mode, $canreply=NULL) {
501cdbd8 2139
2140 global $USER;
2141
61e96406 2142 if (!empty($USER->id)) {
2143 $ownpost = ($USER->id == $post->userid);
2144 } else {
2145 $ownpost = false;
2146 }
c6d691dc 2147 if ($canreply === NULL) {
2148 $reply = forum_user_can_post($forum);
2149 } else {
2150 $reply = $canreply;
2151 }
501cdbd8 2152
02ebf404 2153 $ratings = NULL;
74f5d1e3 2154 $ratingsmenuused = false;
61e96406 2155 if ($forum->assessed and !empty($USER->id)) {
d6bdd9d5 2156 if ($ratings->scale = make_grades_menu($forum->scale)) {
3bd98ad4 2157 $ratings->assesspublic = $forum->assesspublic;
98914efd 2158 $ratings->assesstimestart = $forum->assesstimestart;
2159 $ratings->assesstimefinish = $forum->assesstimefinish;
3bd98ad4 2160 $ratings->allow = ($forum->assessed != 2 or isteacher($course->id));
2161
02ebf404 2162 echo "<form name=form method=post action=rate.php>";
2163 echo "<input type=hidden name=id value=\"$course->id\">";
9d1b97c5 2164 }
2165 }
2166
74f5d1e3 2167 if (forum_print_post($post, $course->id, $ownpost, $reply, $link=false, $ratings)) {
2168 $ratingsmenuused = true;
2169 }
501cdbd8 2170
2171 switch ($mode) {
2e2e71a8 2172 case FORUM_MODE_FLATOLDEST :
2173 case FORUM_MODE_FLATNEWEST :
501cdbd8 2174 default:
02ebf404 2175 echo "<ul>";
74f5d1e3 2176 if (forum_print_posts_flat($post->discussion, $course->id, $mode, $ratings, $reply)) {
2177 $ratingsmenuused = true;
2178 }
02ebf404 2179 echo "</ul>";
501cdbd8 2180 break;
2181
2e2e71a8 2182 case FORUM_MODE_THREADED :
74f5d1e3 2183 if (forum_print_posts_threaded($post->id, $course->id, 0, $ratings, $reply)) {
2184 $ratingsmenuused = true;
2185 }
501cdbd8 2186 break;
2187
2e2e71a8 2188 case FORUM_MODE_NESTED :
74f5d1e3 2189 if (forum_print_posts_nested($post->id, $course->id, $ratings, $reply)) {
2190 $ratingsmenuused = true;
2191 }
501cdbd8 2192 break;
2193 }
2194
74f5d1e3 2195 if ($ratingsmenuused) {
02ebf404 2196 echo "<center><input type=\"submit\" value=\"".get_string("sendinratings", "forum")."\">";
fa01ae8b 2197 if ($forum->scale < 0) {
2198 if ($scale = get_record("scale", "id", abs($forum->scale))) {
2199 print_scale_menu_helpbutton($course->id, $scale );
2200 }
2201 }
02ebf404 2202 echo "</center>";
2203 echo "</form>";
501cdbd8 2204 }
2205}
2206
02ebf404 2207function forum_print_posts_flat($discussion, $course, $direction, $ratings, $reply) {
501cdbd8 2208 global $USER;
2209
501cdbd8 2210 $link = false;
74f5d1e3 2211 $ratingsmenuused = false;
501cdbd8 2212
2213 if ($direction < 0) {
2214 $sort = "ORDER BY created DESC";
2215 } else {
2216 $sort = "ORDER BY created ASC";
2217 }
2218
1f48942e 2219 if ($posts = forum_get_discussion_posts($discussion, $sort)) {
501cdbd8 2220 foreach ($posts as $post) {
ebc3bd2b 2221 $ownpost = ($USER->id == $post->userid);
74f5d1e3 2222 if (forum_print_post($post, $course, $ownpost, $reply, $link, $ratings)) {
2223 $ratingsmenuused = true;
2224 }
501cdbd8 2225 }
501cdbd8 2226 }
74f5d1e3 2227
2228 return $ratingsmenuused;
501cdbd8 2229}
2230
02ebf404 2231function forum_print_posts_threaded($parent, $course, $depth, $ratings, $reply) {
501cdbd8 2232 global $USER;
2233
501cdbd8 2234 $link = false;
74f5d1e3 2235 $ratingsmenuused = false;
501cdbd8 2236
1f48942e 2237 if ($posts = forum_get_child_posts($parent)) {
501cdbd8 2238 foreach ($posts as $post) {
2239
f95c2a73 2240 echo "<ul>";
501cdbd8 2241 if ($depth > 0) {
ebc3bd2b 2242 $ownpost = ($USER->id == $post->userid);
74f5d1e3 2243 if (forum_print_post($post, $course, $ownpost, $reply, $link, $ratings)) {
2244 $ratingsmenuused = true;
2245 }
f95c2a73 2246 echo "<br />";
501cdbd8 2247 } else {
1b26d5e7 2248 $by->name = fullname($post, isteacher($course->id));
d62413e8 2249 $by->date = userdate($post->modified);
f95c2a73 2250 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 2251 print_string("bynameondate", "forum", $by);
f95c2a73 2252 echo "</font></p></li>";
501cdbd8 2253 }
2254
74f5d1e3 2255 if (forum_print_posts_threaded($post->id, $course, $depth-1, $ratings, $reply)) {
2256 $ratingsmenuused = true;
2257 }
f95c2a73 2258 echo "</ul>\n";
501cdbd8 2259 }
501cdbd8 2260 }
74f5d1e3 2261 return $ratingsmenuused;
501cdbd8 2262}
2263
02ebf404 2264function forum_print_posts_nested($parent, $course, $ratings, $reply) {
501cdbd8 2265 global $USER;
2266
501cdbd8 2267 $link = false;
74f5d1e3 2268 $ratingsmenuused = false;
501cdbd8 2269
1f48942e 2270 if ($posts = forum_get_child_posts($parent)) {
501cdbd8 2271 foreach ($posts as $post) {
2272
61e96406 2273 if (empty($USER->id)) {
2274 $ownpost = false;
2275 } else {
2276 $ownpost = ($USER->id == $post->userid);
2277 }
501cdbd8 2278
74f5d1e3 2279 echo "<ul>";
2280 if (forum_print_post($post, $course, $ownpost, $reply, $link, $ratings)) {
2281 $ratingsmenuused = true;
2282 }
2283 echo "<br />";
2284 if (forum_print_posts_nested($post->id, $course, $ratings, $reply)) {
2285 $ratingsmenuused = true;
2286 }
2287 echo "</ul>\n";
501cdbd8 2288 }
501cdbd8 2289 }
74f5d1e3 2290 return $ratingsmenuused;
501cdbd8 2291}
2292
a57a7794 2293function forum_get_recent_mod_activity(&$activities, &$index, $sincetime, $courseid, $forum="0", $user="", $groupid="") {
2294// Returns all forum posts since a given time. If forum is specified then
2295// this restricts the results
2296
2297 global $CFG;
2298
2299 if ($forum) {
2300 $forumselect = " AND cm.id = '$forum'";
2301 } else {
2302 $forumselect = "";
2303 }
2304
2305 if ($user) {
2306 $userselect = " AND u.id = '$user'";
2307 } else {
2308 $userselect = "";
2309 }
2310
a57a7794 2311 $posts = get_records_sql("SELECT p.*, d.name, u.firstname, u.lastname,
2312 u.picture, d.groupid, cm.instance, f.name, cm.section
2313 FROM {$CFG->prefix}forum_posts p,
2314 {$CFG->prefix}forum_discussions d,
2315 {$CFG->prefix}user u,
2316 {$CFG->prefix}course_modules cm,
2317 {$CFG->prefix}forum f
2318 WHERE p.modified > '$sincetime' $forumselect
2319 AND p.userid = u.id $userselect
2320 AND d.course = '$courseid'
2321 AND p.discussion = d.id $groupselect
2322 AND cm.instance = f.id
2323 AND cm.course = d.course
2324 AND cm.course = f.course
2325 AND f.id = d.forum
2326 ORDER BY d.id");
2327
90708fc1 2328 if (empty($posts))
2329 return;
a57a7794 2330
90708fc1 2331 foreach ($posts as $post) {
a57a7794 2332
90708fc1 2333 if (empty($groupid) || ismember($groupid, $post->userid)) {
2334 $tmpactivity->type = "forum";
2335 $tmpactivity->defaultindex = $index;
2336 $tmpactivity->instance = $post->instance;
2337 $tmpactivity->name = $post->name;
2338 $tmpactivity->section = $post->section;
a57a7794 2339
90708fc1 2340 $tmpactivity->content->id = $post->id;
2341 $tmpactivity->content->discussion = $post->discussion;
2342 $tmpactivity->content->subject = $post->subject;
2343 $tmpactivity->content->parent = $post->parent;
2344
2345 $tmpactivity->user->userid = $post->userid;
2346 $tmpactivity->user->fullname = fullname($post);
2347 $tmpactivity->user->picture = $post->picture;
a57a7794 2348
90708fc1 2349 $tmpactivity->timestamp = $post->modified;
2350 $activities[] = $tmpactivity;
2351
2352 $index++;
2353 }
2354 }
a57a7794 2355
90708fc1 2356 return;
a57a7794 2357}
2358
2359function forum_print_recent_mod_activity($activity, $course, $detail=false) {
2360
2361 global $CFG;
2362
2363 echo '<table border="0" cellpadding="3" cellspacing="0">';
2364
2365 if ($activity->content->parent) {
2366 $openformat = "<font size=\"2\"><i>";
2367 $closeformat = "</i></font>";
2368 } else {
2369 $openformat = "<b>";
2370 $closeformat = "</b>";
2371 }
2372
2373 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
2374 print_user_picture($activity->user->userid, $course, $activity->user->picture);
2375 echo "</td><td>$openformat";
2376
2377 if ($detail) {
2378 echo "<img src=\"$CFG->modpixpath/$activity->type/icon.gif\" ".
2379 "height=16 width=16 alt=\"$activity->name\"> ";
2380 }
2381 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=" . $activity->content->discussion
2382 . "#" . $activity->content->id . "\">";
2383
2384 echo $activity->content->subject;
2385 echo "</a>$closeformat";
2386
2387 echo "<br><font size=\"2\">";
2388 echo "<a href=\"$CFG->wwwroot/user/view.php?id=" . $activity->user->userid . "&course=" . "$course\">"
2389 . $activity->user->fullname . "</a>";
2390 echo " - " . userdate($activity->timestamp) . "</font></td></tr>";
2391 echo "</table>";
2392
2393 return;
2394}
2395
f93f848a 2396?>