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