Now firstname and lastname fields are trimmed before saving them to db.
[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
9fa49e22 587/// SQL FUNCTIONS ///////////////////////////////////////////////////////////
588
1f48942e 589function forum_get_post_full($postid) {
590/// Gets a post with all info ready for forum_print_post
591 global $CFG;
592
ebc3bd2b 593 return get_record_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 594 FROM {$CFG->prefix}forum_posts p,
595 {$CFG->prefix}user u
596 WHERE p.id = '$postid'
ebc3bd2b 597 AND p.userid = u.id");
1f48942e 598}
599
600function forum_get_discussion_posts($discussion, $sort) {
601/// Gets posts with all info ready for forum_print_post
602 global $CFG;
603
ebc3bd2b 604 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 605 FROM {$CFG->prefix}forum_posts p,
606 {$CFG->prefix}user u
607 WHERE p.discussion = $discussion
608 AND p.parent > 0
ebc3bd2b 609 AND p.userid = u.id $sort");
1f48942e 610}
611
612function forum_get_child_posts($parent) {
613/// Gets posts with all info ready for forum_print_post
614 global $CFG;
615
ebc3bd2b 616 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 617 FROM {$CFG->prefix}forum_posts p,
618 {$CFG->prefix}user u
619 WHERE p.parent = '$parent'
ebc3bd2b 620 AND p.userid = u.id
1f48942e 621 ORDER BY p.created ASC");
622}
623
624
8b9c7aa0 625function forum_search_posts($searchterms, $courseid, $page=0, $recordsperpage=50, &$totalcount) {
626/// Returns a list of posts found using an array of search terms
627/// eg word +word -word
628///
629
9fa49e22 630 global $CFG;
631
632 if (!isteacher($courseid)) {
633 $notteacherforum = "AND f.type <> 'teacher'";
4cabd355 634
635 $forummodule = get_record("modules", "name", "forum");
636 $onlyvisible = " AND f.id = cm.instance AND cm.visible = 1 AND cm.module = $forummodule->id";
637 $onlyvisibletable = ", {$CFG->prefix}course_modules cm";
9fa49e22 638 } else {
639 $notteacherforum = "";
4cabd355 640
641 $onlyvisible = "";
642 $onlyvisibletable = "";
9fa49e22 643 }
644
c2a96d6b 645 switch ($CFG->dbtype) {
646 case "mysql":
647 $limit = "LIMIT $page,$recordsperpage";
648 break;
649 case "postgres7":
650 $limit = "LIMIT $recordsperpage OFFSET ".($page * $recordsperpage);
651 break;
652 default:
653 $limit = "LIMIT $recordsperpage,$page";
97485d07 654 }
655
63d99fcb 656 /// Some differences in syntax for PostgreSQL
657 if ($CFG->dbtype == "postgres7") {
a74f950c 658 $LIKE = "ILIKE"; // case-insensitive
2b4c64e9 659 $NOTLIKE = "NOT ILIKE"; // case-insensitive
660 $REGEXP = "~*";
661 $NOTREGEXP = "!~*";
b800ac5a 662 } else {
a74f950c 663 $LIKE = "LIKE";
2b4c64e9 664 $NOTLIKE = "NOT LIKE";
a74f950c 665 $REGEXP = "REGEXP";
2b4c64e9 666 $NOTREGEXP = "NOT REGEXP";
c4c57597 667 }
668
b800ac5a 669 $messagesearch = "";
670 $subjectsearch = "";
671
b800ac5a 672
673 foreach ($searchterms as $searchterm) {
8b9c7aa0 674 if (strlen($searchterm) < 2) {
675 continue;
676 }
b800ac5a 677 if ($messagesearch) {
678 $messagesearch .= " AND ";
679 }
b800ac5a 680 if ($subjectsearch) {
681 $subjectsearch .= " AND ";
682 }
63d99fcb 683
a74f950c 684 if (substr($searchterm,0,1) == "+") {
685 $searchterm = substr($searchterm,1);
63d99fcb 686 $messagesearch .= " p.message $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
687 $subjectsearch .= " p.subject $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
a74f950c 688 } else if (substr($searchterm,0,1) == "-") {
689 $searchterm = substr($searchterm,1);
2b4c64e9 690 $messagesearch .= " p.message $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
691 $subjectsearch .= " p.subject $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
63d99fcb 692 } else {
693 $messagesearch .= " p.message $LIKE '%$searchterm%' ";
694 $subjectsearch .= " p.subject $LIKE '%$searchterm%' ";
695 }
b800ac5a 696 }
697
8b9c7aa0 698 $selectsql = "{$CFG->prefix}forum_posts p,
699 {$CFG->prefix}forum_discussions d,
700 {$CFG->prefix}user u,
4cabd355 701 {$CFG->prefix}forum f $onlyvisibletable
8b9c7aa0 702 WHERE ($messagesearch OR $subjectsearch)
703 AND p.userid = u.id
704 AND p.discussion = d.id
705 AND d.course = '$courseid'
4cabd355 706 AND d.forum = f.id $notteacherforum $onlyvisible";
8b9c7aa0 707
708 $totalcount = count_records_sql("SELECT COUNT(*) FROM $selectsql");
b800ac5a 709
8b9c7aa0 710 return get_records_sql("SELECT p.*,u.firstname,u.lastname,u.email,u.picture FROM
711 $selectsql ORDER BY p.modified DESC $limit");
9fa49e22 712}
713
63d99fcb 714
9fa49e22 715function forum_get_ratings($postid, $sort="u.firstname ASC") {
716/// Returns a list of ratings for a particular post - sorted.
717 global $CFG;
718 return get_records_sql("SELECT u.*, r.rating, r.time
719 FROM {$CFG->prefix}forum_ratings r,
720 {$CFG->prefix}user u
1f48942e 721 WHERE r.post = '$postid'
ebc3bd2b 722 AND r.userid = u.id
9fa49e22 723 ORDER BY $sort");
724}
725
1f48942e 726function forum_get_unmailed_posts($cutofftime) {
727/// Returns a list of all new posts that have not been mailed yet
728 global $CFG;
729 return get_records_sql("SELECT p.*, d.course
730 FROM {$CFG->prefix}forum_posts p,
731 {$CFG->prefix}forum_discussions d
732 WHERE p.mailed = 0
733 AND p.created < '$cutofftime'
734 AND p.discussion = d.id");
735}
736
737function forum_get_user_posts($forumid, $userid) {
738/// Get all the posts for a user in a forum suitable for forum_print_post
739 global $CFG;
740
ebc3bd2b 741 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 742 FROM {$CFG->prefix}forum f,
743 {$CFG->prefix}forum_discussions d,
744 {$CFG->prefix}forum_posts p,
745 {$CFG->prefix}user u
746 WHERE f.id = '$forumid'
747 AND d.forum = f.id
748 AND p.discussion = d.id
ebc3bd2b 749 AND p.userid = '$userid'
750 AND p.userid = u.id
1f48942e 751 ORDER BY p.modified ASC");
752}
753
754function forum_get_post_from_log($log) {
755/// Given a log entry, return the forum post details for it.
756 global $CFG;
757
758 if ($log->action == "add post") {
759
2f9c977e 760 return get_record_sql("SELECT p.*, d.forum, d.groupid, u.firstname, u.lastname, u.email, u.picture
1f48942e 761 FROM {$CFG->prefix}forum_discussions d,
762 {$CFG->prefix}forum_posts p,
763 {$CFG->prefix}user u
764 WHERE p.id = '$log->info'
765 AND d.id = p.discussion
ebc3bd2b 766 AND p.userid = u.id
1f48942e 767 AND u.deleted <> '1'");
768
769
770 } else if ($log->action == "add discussion") {
771
2f9c977e 772 return get_record_sql("SELECT p.*, d.forum, d.groupid, u.firstname, u.lastname, u.email, u.picture
1f48942e 773 FROM {$CFG->prefix}forum_discussions d,
774 {$CFG->prefix}forum_posts p,
775 {$CFG->prefix}user u
776 WHERE d.id = '$log->info'
777 AND d.firstpost = p.id
ebc3bd2b 778 AND p.userid = u.id
1f48942e 779 AND u.deleted <> '1'");
780 }
781 return NULL;
782}
783
d05956ac 784function forum_get_firstpost_from_discussion($discussionid) {
785/// Given a discussion id, return the first post from the discussion
786 global $CFG;
787
788 return get_record_sql("SELECT p.*
789 FROM {$CFG->prefix}forum_discussions d,
790 {$CFG->prefix}forum_posts p
791 WHERE d.id = '$discussionid'
792 AND d.firstpost = p.id ");
793}
794
1f48942e 795
796function forum_get_user_grades($forumid) {
797/// Get all user grades for a forum
798 global $CFG;
799
ebc3bd2b 800 return get_records_sql("SELECT r.id, p.userid, r.rating
1f48942e 801 FROM {$CFG->prefix}forum_discussions d,
802 {$CFG->prefix}forum_posts p,
803 {$CFG->prefix}forum_ratings r
804 WHERE d.forum = '$forumid'
805 AND p.discussion = d.id
02ebf404 806 AND r.post = p.id
807 ORDER by p.userid ");
1f48942e 808}
809
810
811function forum_count_discussion_replies($forum="0") {
812// Returns an array of counts of replies to each discussion (optionally in one forum)
813 global $CFG;
814
815 if ($forum) {
816 $forumselect = " AND d.forum = '$forum'";
817 }
818 return get_records_sql("SELECT p.discussion, (count(*)) as replies
819 FROM {$CFG->prefix}forum_posts p,
820 {$CFG->prefix}forum_discussions d
821 WHERE p.parent > 0
822 AND p.discussion = d.id
823 GROUP BY p.discussion");
824}
825
826function forum_count_unrated_posts($discussionid, $userid) {
827// How many unrated posts are in the given discussion for a given user?
828 global $CFG;
829 if ($posts = get_record_sql("SELECT count(*) as num
830 FROM {$CFG->prefix}forum_posts
831 WHERE parent > 0
832 AND discussion = '$discussionid'
ebc3bd2b 833 AND userid <> '$userid' ")) {
1f48942e 834
835 if ($rated = get_record_sql("SELECT count(*) as num
836 FROM {$CFG->prefix}forum_posts p,
837 {$CFG->prefix}forum_ratings r
838 WHERE p.discussion = '$discussionid'
839 AND p.id = r.post
ebc3bd2b 840 AND r.userid = '$userid'")) {
1f48942e 841 $difference = $posts->num - $rated->num;
842 if ($difference > 0) {
843 return $difference;
844 } else {
845 return 0; // Just in case there was a counting error
846 }
847 } else {
848 return $posts->num;
849 }
850 } else {
851 return 0;
852 }
853}
854
9197e147 855function forum_get_discussions($forum="0", $forumsort="d.timemodified DESC",
856 $user=0, $fullpost=true, $currentgroup=0) {
1f48942e 857/// Get all discussions in a forum
858 global $CFG;
859
860 if ($user) {
861 $userselect = " AND u.id = '$user' ";
862 } else {
863 $userselect = "";
864 }
02509fe6 865 if ($currentgroup) {
866 $groupselect = " AND d.groupid = '$currentgroup' ";
867 } else {
868 $groupselect = "";
869 }
29507631 870 if (empty($forumsort)) {
871 $forumsort = "d.timemodified DESC";
872 }
2ab968e9 873 if (empty($fullpost)) {
b879effb 874 $postdata = "p.id,p.subject,p.modified,p.discussion,p.userid";
2ab968e9 875 } else {
876 $postdata = "p.*";
877 }
9197e147 878
016cd6af 879 return get_records_sql("SELECT $postdata, d.timemodified, d.usermodified,
880 u.firstname, u.lastname, u.email, u.picture
1f48942e 881 FROM {$CFG->prefix}forum_discussions d,
9197e147 882 {$CFG->prefix}forum_posts p,
02509fe6 883 {$CFG->prefix}user u
1f48942e 884 WHERE d.forum = '$forum'
885 AND p.discussion = d.id
2ab968e9 886 AND p.parent = 0
9197e147 887 AND p.userid = u.id $groupselect $userselect
29507631 888 ORDER BY $forumsort");
1f48942e 889}
890
891
892
b656e2a9 893function forum_get_user_discussions($courseid, $userid, $groupid=0) {
894/// Get all discussions started by a particular user in a course (or group)
1f48942e 895 global $CFG;
896
b656e2a9 897 if ($groupid) {
898 $groupselect = " AND d.groupid = '$groupid' ";
899 } else {
900 $groupselect = "";
901 }
902
1f48942e 903 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture,
ebc3bd2b 904 f.type as forumtype, f.name as forumname, f.id as forumid
1f48942e 905 FROM {$CFG->prefix}forum_discussions d,
906 {$CFG->prefix}forum_posts p,
907 {$CFG->prefix}user u,
908 {$CFG->prefix}forum f
909 WHERE d.course = '$courseid'
910 AND p.discussion = d.id
911 AND p.parent = 0
ebc3bd2b 912 AND p.userid = u.id
1f48942e 913 AND u.id = '$userid'
b656e2a9 914 AND d.forum = f.id $groupselect
b8bf90c5 915 ORDER BY p.created DESC");
1f48942e 916}
917
918
6673d7bd 919function forum_subscribed_users($course, $forum, $groupid=0) {
1f48942e 920/// Returns list of user objects that are subscribed to this forum
921 global $CFG;
922
6673d7bd 923 if ($groupid) {
924 $grouptables = ", {$CFG->prefix}groups_members g";
925 $groupselect = " AND g.groupid = '$groupid' AND u.id = g.userid";
926 } else {
927 $grouptables = "";
928 $groupselect = "";
929 }
930
adaf3928 931 if ($forum->forcesubscribe) {
932 if ($course->category) {
170a9c46 933 if ($forum->type == "teacher") {
934 return get_course_teachers($course->id); // Only teachers can be subscribed to teacher forums
935 } else {
936 return get_course_users($course->id); // Otherwise get everyone in the course
937 }
adaf3928 938 } else {
2d0b30a0 939 return get_site_users();
1f48942e 940 }
941 }
c3862e8f 942 return get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, u.emailstop,
0351b1f9 943 u.email, u.city, u.country, u.lastaccess, u.lastlogin, u.picture, u.timezone, u.lang
1f48942e 944 FROM {$CFG->prefix}user u,
6673d7bd 945 {$CFG->prefix}forum_subscriptions s $grouptables
1f48942e 946 WHERE s.forum = '$forum->id'
ebc3bd2b 947 AND s.userid = u.id
6673d7bd 948 AND u.deleted <> 1 $groupselect
7c81b6e2 949 ORDER BY u.email ASC");
1f48942e 950}
9fa49e22 951
caadf009 952/// OTHER FUNCTIONS ///////////////////////////////////////////////////////////
f93f848a 953
954
11b0c469 955function forum_get_course_forum($courseid, $type) {
956// How to set up special 1-per-course forums
a6fcdf98 957 global $CFG;
958
29cbd93a 959 if ($forums = get_records_select("forum", "course = '$courseid' AND type = '$type'", "id ASC")) {
960 // There should always only be ONE, but with the right combination of
961 // errors there might be more. In this case, just return the oldest one (lowest ID).
962 foreach ($forums as $forum) {
963 return $forum; // ie the first one
11b0c469 964 }
8daaf761 965 }
e6874d9f 966
8daaf761 967 // Doesn't exist, so create one now.
968 $forum->course = $courseid;
969 $forum->type = "$type";
970 switch ($forum->type) {
971 case "news":
65a3ef30 972 $forum->name = addslashes(get_string("namenews", "forum"));
973 $forum->intro = addslashes(get_string("intronews", "forum"));
8daaf761 974 $forum->forcesubscribe = 1;
8daaf761 975 $forum->open = 1; // 0 - no, 1 - posts only, 2 - discuss and post
976 $forum->assessed = 0;
977 if ($site = get_site()) {
978 if ($courseid == $site->id) {
979 $forum->name = get_string("sitenews");
980 $forum->forcesubscribe = 0;
981 }
a6fcdf98 982 }
8daaf761 983 break;
984 case "social":
65a3ef30 985 $forum->name = addslashes(get_string("namesocial", "forum"));
986 $forum->intro = addslashes(get_string("introsocial", "forum"));
8daaf761 987 $forum->open = 2; // 0 - no, 1 - posts only, 2 - discuss and post
988 $forum->assessed = 0;
989 $forum->forcesubscribe = 0;
990 break;
991 case "teacher":
65a3ef30 992 $forum->name = addslashes(get_string("nameteacher", "forum"));
993 $forum->intro = addslashes(get_string("introteacher", "forum"));
8daaf761 994 $forum->open = 0; // 0 - no, 1 - posts only, 2 - discuss and post
995 $forum->assessed = 0;
996 $forum->forcesubscribe = 0;
997 break;
998 default:
999 notify("That forum type doesn't exist!");
1000 return false;
1001 break;
1002 }
1003
1004 $forum->timemodified = time();
1005 $forum->id = insert_record("forum", $forum);
1006
1007 if ($forum->type != "teacher") {
1008 if (! $module = get_record("modules", "name", "forum")) {
1009 notify("Could not find forum module!!");
1010 return false;
1011 }
1012 $mod->course = $courseid;
1013 $mod->module = $module->id;
1014 $mod->instance = $forum->id;
1015 $mod->section = 0;
1016 if (! $mod->coursemodule = add_course_module($mod) ) { // assumes course/lib.php is loaded
1017 notify("Could not add a new course module to the course '$course->fullname'");
1018 return false;
1019 }
1020 if (! $sectionid = add_mod_to_section($mod) ) { // assumes course/lib.php is loaded
1021 notify("Could not add the new course module to that section");
1022 return false;
1023 }
1024 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
1025 notify("Could not update the course module with the correct section");
1026 return false;
1027 }
1028 include_once("$CFG->dirroot/course/lib.php");
d769d2ee 1029 rebuild_course_cache($courseid);
82aa0e8d 1030 }
8daaf761 1031
1032 return get_record("forum", "id", "$forum->id");
82aa0e8d 1033}
1034
f93f848a 1035
11b0c469 1036function forum_make_mail_post(&$post, $user, $touser, $course,
1037 $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
501cdbd8 1038// Given the data about a posting, builds up the HTML to display it and
1039// returns the HTML in a string. This is designed for sending via HTML email.
1040
1041 global $THEME, $CFG;
1042
0d851f90 1043 static $formattedtext; // Cached version of formatted text for a post
1044 static $formattedtextid; // The ID number of the post
1045
1046 if (empty($formattedtextid) or $formattedtextid != $post->id) { // Recalculate the formatting
1047 $formattedtext = format_text($post->message, $post->format, NULL, $course->id);
1048 $formattedtextid = $post->id;
1049 }
1050
501cdbd8 1051 $output = "";
1052
218b4433 1053 $output .= "<style> <!--"; /// Styles for autolinks
94d712f1 1054 $output .= "a.autolink:link {text-decoration: none; color: black; background-color: $THEME->autolink}\n";
1055 $output .= "a.autolink:visited {text-decoration: none; color: black; background-color: $THEME->autolink}\n";
218b4433 1056 $output .= "a.autolink:hover {text-decoration: underline; color: red}\n";
1057 $output .= "--> </style>\n\n";
1058
b6650a25 1059 $output .= '<table border="0" cellpadding="1" cellspacing="1"><tr><td bgcolor="'.$THEME->borders.'">';
1060 $output .= '<table border="0" cellpadding="3" cellspacing="0">';
501cdbd8 1061
72d497d4 1062 $output .= "<tr><td bgcolor=\"$THEME->cellcontent2\" width=\"35\" valign=\"top\">";
501cdbd8 1063 $output .= print_user_picture($user->id, $course->id, $user->picture, false, true);
72d497d4 1064 $output .= "</td>";
501cdbd8 1065
1066 if ($post->parent) {
72d497d4 1067 $output .= "<td nowrap bgcolor=\"$THEME->cellheading\">";
501cdbd8 1068 } else {
72d497d4 1069 $output .= "<td nowrap bgcolor=\"$THEME->cellheading2\">";
501cdbd8 1070 }
72d497d4 1071 $output .= "<p>";
1072 $output .= "<font size=3><b>$post->subject</b></font><br />";
1073 $output .= "<font size=2>";
1b26d5e7 1074
1075 $fullname = fullname($user, isteacher($course->id));
1076 $by->name = "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$fullname</a>";
d62413e8 1077 $by->date = userdate($post->modified, "", $touser->timezone);
ffe11640 1078 $output .= get_string("bynameondate", "forum", $by);
1b26d5e7 1079
72d497d4 1080 $output .= "</font></p></td></tr>";
1081 $output .= "<tr><td bgcolor=\"$THEME->cellcontent2\" width=10>";
501cdbd8 1082 $output .= "&nbsp;";
72d497d4 1083 $output .= "</td><td bgcolor=\"$THEME->cellcontent\">\n";
501cdbd8 1084
7f6689e4 1085 if ($post->attachment) {
1086 $post->course = $course->id;
1087 $post->forum = get_field("forum_discussions", "forum", "id", $post->discussion);
72d497d4 1088 $output .= "<div align=right>";
7f6689e4 1089 $output .= forum_print_attachments($post, "html");
72d497d4 1090 $output .= "</div>";
7f6689e4 1091 }
1092
0d851f90 1093 $output .= $formattedtext;
501cdbd8 1094
72d497d4 1095 $output .= "<p align=right><font size=-1>";
501cdbd8 1096
2e2e71a8 1097 if ($post->parent) {
ce45515e 1098 $output .= "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">".get_string("parent", "forum")."</a> | ";
2e2e71a8 1099 }
ce45515e 1100
501cdbd8 1101 $age = time() - $post->created;
1102 if ($ownpost) {
ce45515e 1103 $output .= "<a href=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."</a>";
501cdbd8 1104 if ($reply) {
02509fe6 1105 $output .= " | <a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("replyforum", "forum")."</a>";
501cdbd8 1106 }
1107 $output .= "&nbsp;&nbsp;";
1108 } else {
1109 if ($reply) {
02509fe6 1110 $output .= "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("replyforum", "forum")."</a>&nbsp;&nbsp;";
501cdbd8 1111 }
1112 }
1113
72d497d4 1114 $output .= "</p>";
1115 $output .= "<div align=right><p align=right>";
501cdbd8 1116
1117 if ($link) {
1118 if ($post->replies == 1) {
ffe11640 1119 $replystring = get_string("repliesone", "forum", $post->replies);
501cdbd8 1120 } else {
ffe11640 1121 $replystring = get_string("repliesmany", "forum", $post->replies);
501cdbd8 1122 }
72d497d4 1123 $output .= "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><B>".get_string("discussthistopic", "forum")."</b></a> ($replystring)&nbsp;&nbsp;";
501cdbd8 1124 }
72d497d4 1125 $output .= "</p></div>";
501cdbd8 1126 if ($footer) {
72d497d4 1127 $output .= "<p>$footer</p>";
501cdbd8 1128 }
72d497d4 1129 $output .= "</td></tr></table>\n";
1130 $output .= "</td></tr></table>\n\n";
501cdbd8 1131
1132 return $output;
1133}
1134
1135
98914efd 1136function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link=false,
1137 $ratings=NULL, $footer="", $highlight="") {
74f5d1e3 1138
c585fa17 1139 global $THEME, $USER, $CFG;
501cdbd8 1140
b8be40ce 1141 static $stredit, $strdelete, $strreply, $strparent, $threadedmode, $isteacher, $adminedit;
2e2e71a8 1142
1143 if (empty($stredit)) {
1144 $stredit = get_string("edit", "forum");
1145 $strdelete = get_string("delete", "forum");
1146 $strreply = get_string("reply", "forum");
1147 $strparent = get_string("parent", "forum");
1148 $threadedmode = (!empty($USER->mode) and ($USER->mode == FORUM_MODE_THREADED));
3bd98ad4 1149 $isteacher = isteacher($courseid);
b8be40ce 1150 $adminedit = (isadmin() and !empty($CFG->admineditalways));
2e2e71a8 1151 }
1152
b22b0e61 1153 echo "<a name=\"$post->id\"></a>";
501cdbd8 1154 if ($post->parent) {
72d497d4 1155 echo '<table border="0" cellpadding="3" cellspacing="0" class="forumpost">';
501cdbd8 1156 } else {
72d497d4 1157 echo '<table border="0" cellpadding="3" cellspacing="0" class="forumpost" width="100%">';
501cdbd8 1158 }
1159
72d497d4 1160 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
501cdbd8 1161 print_user_picture($post->userid, $courseid, $post->picture);
72d497d4 1162 echo "</td>";
501cdbd8 1163
1164 if ($post->parent) {
834f1c7f 1165 echo "<td bgcolor=\"$THEME->cellheading\" class=\"forumpostheader\" width=\"100%\">";
501cdbd8 1166 } else {
834f1c7f 1167 echo "<td bgcolor=\"$THEME->cellheading2\" class=\"forumpostheadertopic\" width=\"100%\">";
501cdbd8 1168 }
83ec9098 1169
1170 if (!empty($CFG->filterall)) { /// Put the subject through the filters
34e934a9 1171 $post->subject = filter_text("<nolink>$post->subject</nolink>", $courseid);
83ec9098 1172 }
72d497d4 1173 echo "<p>";
d7143408 1174 echo "<font size=3><b>$post->subject</b></font><br />";
834f1c7f 1175 echo "<font size=2>";
1b26d5e7 1176
3bd98ad4 1177 $fullname = fullname($post, $isteacher);
1b26d5e7 1178 $by->name = "<a href=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$fullname</a>";
d62413e8 1179 $by->date = userdate($post->modified);
ffe11640 1180 print_string("bynameondate", "forum", $by);
1b26d5e7 1181
72d497d4 1182 echo "</font></p></td></tr>";
507407a7 1183 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" valign=\"top\" class=\"forumpostside\" width=\"10\">";
1184 if ($group = user_group($courseid, $post->userid)) {
1185 print_group_picture($group, $courseid, false, false, false);
1186 } else {
1187 echo "&nbsp;";
1188 }
72d497d4 1189 echo "</td><td bgcolor=\"$THEME->cellcontent\" class=\"forumpostmessage\">\n";
501cdbd8 1190
7f6689e4 1191 if ($post->attachment) {
1192 $post->course = $courseid;
1193 $post->forum = get_field("forum_discussions", "forum", "id", $post->discussion);
72d497d4 1194 echo "<div align=\"right\">";
1195 $attachedimages = forum_print_attachments($post);
1196 echo "</div>";
e9c2dc1f 1197 } else {
1198 $attachedimages = "";
7f6689e4 1199 }
1200
5be7800c 1201 if ($link and (strlen(strip_tags($post->message)) > $CFG->forum_longpost)) {
aa153f29 1202 // Print shortened version
71767204 1203 echo format_text(forum_shorten_post($post->message), $post->format, NULL, $courseid);
c585fa17 1204 $numwords = count_words(strip_tags($post->message));
72d497d4 1205 echo "<p><a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">";
ffe11640 1206 echo get_string("readtherest", "forum");
72d497d4 1207 echo "</a> (".get_string("numwords", "", $numwords).")...</p>";
501cdbd8 1208 } else {
aa153f29 1209 // Print whole message
88438a58 1210 if ($highlight) {
71767204 1211 echo highlight($highlight, format_text($post->message, $post->format, NULL, $courseid));
88438a58 1212 } else {
71767204 1213 echo format_text($post->message, $post->format, NULL, $courseid);
88438a58 1214 }
c99ce77b 1215 echo $attachedimages;
501cdbd8 1216 }
1217
72d497d4 1218 echo "<p align=right><font size=-1>";
501cdbd8 1219
2e2e71a8 1220 if ($post->parent) {
1221 if ($threadedmode) {
1222 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">$strparent</a> | ";
1223 } else {
1224 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion#$post->parent\">$strparent</a> | ";
1225 }
1226 }
1227
501cdbd8 1228 $age = time() - $post->created;
b8be40ce 1229 if ($ownpost or $adminedit) {
1230 if (($age < $CFG->maxeditingtime) or $adminedit) {
2e2e71a8 1231 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?edit=$post->id\">$stredit</a> | ";
501cdbd8 1232 }
64eacd6f 1233 }
3bd98ad4 1234 if ($ownpost or $isteacher) {
2e2e71a8 1235 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">$strdelete</a>";
501cdbd8 1236 if ($reply) {
64eacd6f 1237 echo "| ";
1238 } else {
1239 echo "&nbsp;&nbsp;";
501cdbd8 1240 }
64eacd6f 1241 }
1242 if ($reply) {
2e2e71a8 1243 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">$strreply</a>";
501cdbd8 1244 echo "&nbsp;&nbsp;";
501cdbd8 1245 }
72d497d4 1246 echo "</p>";
501cdbd8 1247
72d497d4 1248 echo "<div align=right><p align=right>";
74f5d1e3 1249
1250 $ratingsmenuused = false;
02ebf404 1251 if (!empty($ratings) and !empty($USER->id)) {
98914efd 1252 $useratings = true;
1253 if ($ratings->assesstimestart and $ratings->assesstimefinish) {
1254 if ($post->created < $ratings->assesstimestart or $post->created > $ratings->assesstimefinish) {
1255 $useratings = false;
1256 }
1257 }
1258 if ($useratings) {
3bd98ad4 1259 $mypost = ($USER->id == $post->userid);
1260
1261 if (($isteacher or $ratings->assesspublic) and !$mypost) {
1262 forum_print_ratings_mean($post->id, $ratings->scale, $isteacher);
1263 forum_print_rating_menu($post->id, $USER->id, $ratings->scale);
1264 $ratingsmenuused = true;
1265
1266 } else if ($mypost) {
1267 forum_print_ratings_mean($post->id, $ratings->scale, true);
1268
98914efd 1269 } else if (!empty($ratings->allow) ) {
1270 forum_print_rating_menu($post->id, $USER->id, $ratings->scale);
74f5d1e3 1271 $ratingsmenuused = true;
2a3cda19 1272 }
501cdbd8 1273 }
1274 }
1275
1276 if ($link) {
1277 if ($post->replies == 1) {
ffe11640 1278 $replystring = get_string("repliesone", "forum", $post->replies);
501cdbd8 1279 } else {
ffe11640 1280 $replystring = get_string("repliesmany", "forum", $post->replies);
501cdbd8 1281 }
74f5d1e3 1282 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><b>".
1283 get_string("discussthistopic", "forum")."</b></a> ($replystring)&nbsp;&nbsp;";
501cdbd8 1284 }
72d497d4 1285 echo "</p>";
501cdbd8 1286 if ($footer) {
72d497d4 1287 echo "<p>$footer</p>";
501cdbd8 1288 }
72d497d4 1289 echo "</div>";
1290 echo "</td></tr>\n</table>\n\n";
74f5d1e3 1291
1292 return $ratingsmenuused;
501cdbd8 1293}
1294
3335f6fb 1295
2ab968e9 1296function forum_print_discussion_header(&$post, $courseid, $datestring="") {
c585fa17 1297 global $THEME, $USER, $CFG;
3335f6fb 1298
436a7cae 1299 if (!empty($CFG->filterall)) {
34e934a9 1300 $post->subject = filter_text("<nolink>$post->subject</nolink>", $courseid);
436a7cae 1301 }
1302
29507631 1303 echo "<tr class=\"forumpostheader\">";
3335f6fb 1304
29507631 1305 // Topic
546be6dd 1306 echo "<td bgcolor=\"$THEME->cellheading2\" class=\"forumpostheadertopic\" width=\"100%\">";
29507631 1307 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">$post->subject</a>";
1308 echo "</td>\n";
1309
1310 // Picture
96e301a7 1311 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderpicture\" width=35>";
3335f6fb 1312 print_user_picture($post->userid, $courseid, $post->picture);
29507631 1313 echo "</td>\n";
3335f6fb 1314
29507631 1315 // User name
1b26d5e7 1316 $fullname = fullname($post, isteacher($courseid));
96e301a7 1317 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheadername\" align=left nowrap>";
1b26d5e7 1318 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$fullname</a>";
29507631 1319 echo "</td>\n";
1320
1321 // Replies
96e301a7 1322 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderreplies\" align=center nowrap>";
2ab968e9 1323 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">$post->replies</a>";
29507631 1324 echo "</td>\n";
3335f6fb 1325
96e301a7 1326 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderdate\" align=right nowrap>";
29507631 1327 if (!empty($post->timemodified)) {
2ab968e9 1328 echo userdate($post->timemodified, $datestring);
3335f6fb 1329 } else {
2ab968e9 1330 echo userdate($post->modified, $datestring);
3335f6fb 1331 }
29507631 1332 echo "</td>\n";
1333
1334 echo "</tr>\n";
3335f6fb 1335
3335f6fb 1336}
1337
1338
aa153f29 1339function forum_shorten_post($message) {
c585fa17 1340// Given a post object that we already know has a long message
1341// this function truncates the message nicely to the first
5be7800c 1342// sane place between $CFG->forum_longpost and $CFG->forum_shortpost
1343
1344 global $CFG;
c585fa17 1345
1346 $i = 0;
1347 $tag = false;
1348 $length = strlen($message);
1349 $count = 0;
1350 $stopzone = false;
1351 $truncate = 0;
1352
1353 for ($i=0; $i<$length; $i++) {
a8afb411 1354 $char = $message[$i];
c585fa17 1355
1356 switch ($char) {
1357 case "<":
1358 $tag = true;
1359 break;
1360 case ">":
1361 $tag = false;
1362 break;
1363 default:
1364 if (!$tag) {
1365 if ($stopzone) {
67f0b4cc 1366 if ($char == ".") {
a8afb411 1367 $truncate = $i+1;
c585fa17 1368 break 2;
1369 }
1370 }
1371 $count++;
1372 }
a8afb411 1373 break;
c585fa17 1374 }
1375 if (!$stopzone) {
5be7800c 1376 if ($count > $CFG->forum_shortpost) {
c585fa17 1377 $stopzone = true;
1378 }
1379 }
1380 }
aa153f29 1381
c585fa17 1382 if (!$truncate) {
a8afb411 1383 $truncate = $i;
c585fa17 1384 }
1385
67f0b4cc 1386 return substr($message, 0, $truncate);
aa153f29 1387}
1388
501cdbd8 1389
3bd98ad4 1390function forum_print_ratings_mean($postid, $scale, $link=true) {
02ebf404 1391/// Print the multiple ratings on a post given to the current user by others.
1392/// Scale is an array of ratings
1393
1394 static $strrate;
05c47ef7 1395
1396 $mean = forum_get_ratings_mean($postid, $scale);
02ebf404 1397
05c47ef7 1398 if ($mean !== "") {
02ebf404 1399
1400 if (empty($strratings)) {
1401 $strratings = get_string("ratings", "forum");
501cdbd8 1402 }
501cdbd8 1403
02ebf404 1404 echo "$strratings: ";
3bd98ad4 1405 if ($link) {
1406 link_to_popup_window ("/mod/forum/report.php?id=$postid", "ratings", $mean, 400, 600);
1407 } else {
1408 echo "$mean ";
1409 }
501cdbd8 1410 }
1411}
1412
501cdbd8 1413
0761d83f 1414function forum_get_ratings_mean($postid, $scale, $ratings=NULL) {
1415/// Return the mean rating of a post given to the current user by others.
02ebf404 1416/// Scale is an array of possible ratings in the scale
1417/// Ratings is an optional simple array of actual ratings (just integers)
1418
1419 if (!$ratings) {
1420 $ratings = array();
1421 if ($rates = get_records("forum_ratings", "post", $postid)) {
1422 foreach ($rates as $rate) {
1423 $ratings[] = $rate->rating;
1424 }
501cdbd8 1425 }
501cdbd8 1426 }
02ebf404 1427
0761d83f 1428 $count = count($ratings);
1429
1430 if ($count == 0) {
02ebf404 1431 return "";
02ebf404 1432
0761d83f 1433 } else if ($count == 1) {
1434 return $scale[$ratings[0]];
1435
02ebf404 1436 } else {
0761d83f 1437 $total = 0;
1438 foreach ($ratings as $rating) {
1439 $total += $rating;
1440 }
1441 $mean = round( ((float)$total/(float)$count) + 0.001); // Little fudge factor so that 0.5 goes UP
1442
1443 if (isset($scale[$mean])) {
1444 return $scale[$mean]." ($count)";
1445 } else {
1446 return "$mean ($count)"; // Should never happen, hopefully
1447 }
02ebf404 1448 }
1449}
1450
1451function forum_get_ratings_summary($postid, $scale, $ratings=NULL) {
1452/// Return a summary of post ratings given to the current user by others.
1453/// Scale is an array of possible ratings in the scale
1454/// Ratings is an optional simple array of actual ratings (just integers)
1455
1456 if (!$ratings) {
1457 $ratings = array();
1458 if ($rates = get_records("forum_ratings", "post", $postid)) {
1459 foreach ($rates as $rate) {
1460 $rating[] = $rate->rating;
1461 }
1462 }
1463 }
1464
1465
1466 if (!$count = count($ratings)) {
1467 return "";
1468 }
1469
1470
1471 foreach ($scale as $key => $scaleitem) {
1472 $sumrating[$key] = 0;
1473 }
1474
1475 foreach ($ratings as $rating) {
1476 $sumrating[$rating]++;
1477 }
1478
1479 $summary = "";
1480 foreach ($scale as $key => $scaleitem) {
1481 $summary = $sumrating[$key].$summary;
1482 if ($key > 1) {
1483 $summary = "/$summary";
1484 }
1485 }
1486 return $summary;
1487}
1488
1489function forum_print_rating_menu($postid, $userid, $scale) {
1490/// Print the menu of ratings as part of a larger form.
1491/// If the post has already been - set that value.
1492/// Scale is an array of ratings
1493
1494 static $strrate;
1495
1496 if (!$rating = get_record("forum_ratings", "userid", $userid, "post", $postid)) {
1497 $rating->rating = 0;
1498 }
1499
1500 if (empty($strrate)) {
1501 $strrate = get_string("rate", "forum");
1502 }
1503
1504 choose_from_menu($scale, $postid, $rating->rating, "$strrate...");
501cdbd8 1505}
1506
7a12aab4 1507function forum_print_mode_form($discussion, $mode) {
1508 GLOBAL $FORUM_LAYOUT_MODES;
501cdbd8 1509
8b9c7aa0 1510 echo "<center><p>";
7a12aab4 1511 popup_form("discuss.php?d=$discussion&mode=", $FORUM_LAYOUT_MODES, "mode", $mode, "");
8b9c7aa0 1512 echo "</p></center>\n";
501cdbd8 1513}
1514
97485d07 1515function forum_print_search_form($course, $search="", $return=false, $type="") {
501cdbd8 1516 global $CFG;
1517
97485d07 1518 if ($type == "plain") {
8b9c7aa0 1519 $output = "<table border=0 cellpadding=0 cellspacing=0><tr><td nowrap>";
1520 $output .= "<form name=search action=\"$CFG->wwwroot/mod/forum/search.php\">";
1521 $output .= "<font size=\"-1\">";
1522 $output .= "<input name=search type=text size=20 value=\"$search\">";
1523 $output .= "<input value=\"".get_string("searchforums", "forum")."\" type=submit>";
1524 $output .= "</font>";
1525 $output .= "<input name=id type=hidden value=\"$course->id\">";
1526 $output .= "</form>";
1527 $output .= "</td></tr></table>";
97485d07 1528 } else {
8b9c7aa0 1529 $output = "<table border=0 cellpadding=10 cellspacing=0><tr><td align=center>";
1530 $output .= "<form name=search action=\"$CFG->wwwroot/mod/forum/search.php\">";
1531 $output .= "<font size=\"-1\">";
1532 $output .= "<input name=search type=text size=20 value=\"$search\"><br>";
1533 $output .= "<input value=\"".get_string("searchforums", "forum")."\" type=submit>";
1534 $output .= "</font>";
1535 $output .= "<input name=id type=hidden value=\"$course->id\">";
1536 $output .= "</form>";
1537 $output .= "</td></tr></table>";
97485d07 1538 }
5e367a2d 1539
1540 if ($return) {
1541 return $output;
1542 }
1543 echo $output;
501cdbd8 1544}
1545
1546
11b0c469 1547function forum_set_return() {
607809b3 1548 global $CFG, $SESSION;
501cdbd8 1549
28e1e8b9 1550 if (! isset($SESSION->fromdiscussion)) {
48d38fad 1551 if (!empty($_SERVER['HTTP_REFERER'])) {
1552 $referer = $_SERVER['HTTP_REFERER'];
1553 } else {
1554 $referer = "";
1555 }
28e1e8b9 1556 // If the referer is NOT a login screen then save it.
48d38fad 1557 if (! strncasecmp("$CFG->wwwroot/login", $referer, 300)) {
607809b3 1558 $SESSION->fromdiscussion = $_SERVER["HTTP_REFERER"];
28e1e8b9 1559 }
501cdbd8 1560 }
1561}
1562
1563
11b0c469 1564function forum_go_back_to($default) {
501cdbd8 1565 global $SESSION;
1566
9c9f7d77 1567 if (!empty($SESSION->fromdiscussion)) {
501cdbd8 1568 $returnto = $SESSION->fromdiscussion;
1569 unset($SESSION->fromdiscussion);
1570 return $returnto;
1571 } else {
1572 return $default;
1573 }
1574}
1575
7f6689e4 1576function forum_file_area_name($post) {
1577// Creates a directory file name, suitable for make_upload_directory()
1578 global $CFG;
1579
1580 return "$post->course/$CFG->moddata/forum/$post->forum/$post->id";
1581}
1582
1583function forum_file_area($post) {
1584 return make_upload_directory( forum_file_area_name($post) );
1585}
1586
1587function forum_delete_old_attachments($post, $exception="") {
1588// Deletes all the user files in the attachments area for a post
1589// EXCEPT for any file named $exception
1590
1591 if ($basedir = forum_file_area($post)) {
1592 if ($files = get_directory_list($basedir)) {
1593 foreach ($files as $file) {
1594 if ($file != $exception) {
1595 unlink("$basedir/$file");
1596 notify("Existing file '$file' has been deleted!");
1597 }
1598 }
1599 }
1600 if (!$exception) { // Delete directory as well, if empty
1601 rmdir("$basedir");
1602 }
1603 }
1604}
1605
cc2b7ea5 1606function forum_move_attachments($discussion, $forumid) {
1607/// Given a discussion object that is being moved to forumid,
1608/// this function checks all posts in that discussion
1609/// for attachments, and if any are found, these are
1610/// moved to the new forum directory.
1611
1612 global $CFG;
1613
1614 $return = true;
1615
1616 if ($posts = get_records_select("forum_posts", "discussion = '$discussion->id' AND attachment <> ''")) {
1617 foreach ($posts as $oldpost) {
1618 $oldpost->course = $discussion->course;
1619 $oldpost->forum = $discussion->forum;
1620 $oldpostdir = "$CFG->dataroot/".forum_file_area_name($oldpost);
1621 if (is_dir($oldpostdir)) {
1622 $newpost = $oldpost;
1623 $newpost->forum = $forumid;
7f0c3a44 1624 $newpostdir = forum_file_area($newpost);
cc2b7ea5 1625 if (! @rename($oldpostdir, $newpostdir)) {
1626 $return = false;
1627 }
1628 }
1629 }
1630 }
1631 return $return;
1632}
1633
7f6689e4 1634function forum_print_attachments($post, $return=NULL) {
1635// if return=html, then return a html string.
1636// if return=text, then return a text-only string.
72d497d4 1637// otherwise, print HTML for non-images, and return image HTML
7f6689e4 1638
1639 global $CFG;
1640
1641 $filearea = forum_file_area_name($post);
1642
72d497d4 1643 $imagereturn = "";
1644 $output = "";
1645
7f6689e4 1646 if ($basedir = forum_file_area($post)) {
1647 if ($files = get_directory_list($basedir)) {
1648 $strattachment = get_string("attachment", "forum");
6afe8558 1649 $strpopupwindow = get_string("popupwindow");
7f6689e4 1650 foreach ($files as $file) {
1651 $icon = mimeinfo("icon", $file);
1652 if ($CFG->slasharguments) {
1653 $ffurl = "file.php/$filearea/$file";
1654 } else {
1655 $ffurl = "file.php?file=/$filearea/$file";
1656 }
096b5432 1657 $image = "<img border=\"0\" src=\"$CFG->pixpath/f/$icon\" height=\"16\" width=\"16\" alt=\"$strpopupwindow\">";
7f6689e4 1658
1659 if ($return == "html") {
6afe8558 1660 $output .= "<a href=\"$CFG->wwwroot/$ffurl\">$image</a> ";
1661 $output .= "<a href=\"$CFG->wwwroot/$ffurl\">$file</a><br />";
7f6689e4 1662
1663 } else if ($return == "text") {
1664 $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
1665
1666 } else {
72d497d4 1667 if ($icon == "image.gif") { // Image attachments don't get printed as links
1668 $imagereturn .= "<br /><img src=\"$CFG->wwwroot/$ffurl\">";
1669 } else {
1670 link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
1671 echo "<a href=\"$CFG->wwwroot/$ffurl\">$file</a>";
1672 echo "<br />";
1673 }
7f6689e4 1674 }
1675 }
1676 }
1677 }
72d497d4 1678
7f6689e4 1679 if ($return) {
1680 return $output;
1681 }
72d497d4 1682
1683 return $imagereturn;
7f6689e4 1684}
1685
1686function forum_add_attachment($post, $newfile) {
1687// $post is a full post record, including course and forum
3b7c1de9 1688// $newfile is a full upload array from $_FILES
7f6689e4 1689// If successful, this function returns the name of the file
1690
9dd0b378 1691 global $CFG;
1692
3b7c1de9 1693 if (empty($newfile['name'])) {
7f6689e4 1694 return "";
1695 }
1696
4909e176 1697 if (!$forum = get_record("forum", "id", $post->forum)) {
1698 return "";
1699 }
1700
1701 if (!$course = get_record("course", "id", $forum->course)) {
1702 return "";
1703 }
1704
1705 $maxbytes = get_max_upload_file_size($CFG->maxbytes, $course->maxbytes, $forum->maxbytes);
1706
7f6689e4 1707 $newfile_name = clean_filename($newfile['name']);
1708
1709 if (valid_uploaded_file($newfile)) {
4909e176 1710 if ($maxbytes and $newfile['size'] > $maxbytes) {
1711 return "";
1712 }
7f6689e4 1713 if (! $newfile_name) {
1714 notify("This file had a wierd filename and couldn't be uploaded");
1715
1716 } else if (! $dir = forum_file_area($post)) {
1717 notify("Attachment could not be stored");
1718 $newfile_name = "";
1719
1720 } else {
1721 if (move_uploaded_file($newfile['tmp_name'], "$dir/$newfile_name")) {
9dd0b378 1722 chmod("$dir/$newfile_name", $CFG->directorypermissions);
7f6689e4 1723 forum_delete_old_attachments($post, $newfile_name);
1724 } else {
1725 notify("An error happened while saving the file on the server");
1726 $newfile_name = "";
1727 }
1728 }
1729 } else {
1730 $newfile_name = "";
1731 }
1732
1733 return $newfile_name;
1734}
501cdbd8 1735
11b0c469 1736function forum_add_new_post($post) {
501cdbd8 1737
ffe11640 1738 $post->created = $post->modified = time();
501cdbd8 1739 $post->mailed = "0";
1740
7f6689e4 1741 $newfile = $post->attachment;
1742 $post->attachment = "";
1743
1744 if (! $post->id = insert_record("forum_posts", $post)) {
1745 return false;
1746 }
1747
1748 if ($post->attachment = forum_add_attachment($post, $newfile)) {
1749 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id);
1750 }
29507631 1751
1752 // Update discussion modified date
1753 set_field("forum_discussions", "timemodified", $post->modified, "id", $post->discussion);
016cd6af 1754 set_field("forum_discussions", "usermodified", $post->userid, "id", $post->discussion);
7f6689e4 1755
1756 return $post->id;
501cdbd8 1757}
1758
11b0c469 1759function forum_update_post($post) {
501cdbd8 1760
ffe11640 1761 $post->modified = time();
501cdbd8 1762
0ab85112 1763 if (!$post->parent) { // Post is a discussion starter - update discussion title too
1764 set_field("forum_discussions", "name", $post->subject, "id", $post->discussion);
1765 }
29507631 1766
7f6689e4 1767 if ($newfilename = forum_add_attachment($post, $post->attachment)) {
1768 $post->attachment = $newfilename;
1769 } else {
1770 unset($post->attachment);
1771 }
29507631 1772
1773 // Update discussion modified date
1774 set_field("forum_discussions", "timemodified", $post->modified, "id", $post->discussion);
016cd6af 1775 set_field("forum_discussions", "usermodified", $post->userid, "id", $post->discussion);
29507631 1776
ffe11640 1777 return update_record("forum_posts", $post);
501cdbd8 1778}
1779
1780function forum_add_discussion($discussion) {
1781// Given an object containing all the necessary data,
1782// create a new discussion and return the id
1783
1784 GLOBAL $USER;
1785
1786 $timenow = time();
1787
1788 // The first post is stored as a real post, and linked
1789 // to from the discuss entry.
1790
1791 $post->discussion = 0;
1792 $post->parent = 0;
ebc3bd2b 1793 $post->userid = $USER->id;
501cdbd8 1794 $post->created = $timenow;
1795 $post->modified = $timenow;
1796 $post->mailed = 0;
1797 $post->subject = $discussion->name;
1798 $post->message = $discussion->intro;
7f6689e4 1799 $post->attachment = "";
1800 $post->forum = $discussion->forum;
1801 $post->course = $discussion->course;
9f0b8269 1802 $post->format = $discussion->format;
501cdbd8 1803
1804 if (! $post->id = insert_record("forum_posts", $post) ) {
1805 return 0;
1806 }
1807
7f6689e4 1808 if ($post->attachment = forum_add_attachment($post, $discussion->attachment)) {
1809 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id); //ignore errors
1810 }
1811
02509fe6 1812 // Now do the main entry for the discussion,
1813 // linking to this first post
04eba58f 1814
caadf009 1815 $discussion->firstpost = $post->id;
1816 $discussion->timemodified = $timenow;
016cd6af 1817 $discussion->usermodified = $post->userid;
04eba58f 1818
caadf009 1819 if (! $discussion->id = insert_record("forum_discussions", $discussion) ) {
1820 delete_records("forum_posts", "id", $post->id);
1821 return 0;
04eba58f 1822 }
1823
caadf009 1824 // Finally, set the pointer on the post.
1825 if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) {
1826 delete_records("forum_posts", "id", $post->id);
1827 delete_records("forum_discussions", "id", $discussion->id);
1828 return 0;
04eba58f 1829 }
04eba58f 1830
caadf009 1831 return $discussion->id;
1832}
04eba58f 1833
04eba58f 1834
caadf009 1835function forum_delete_discussion($discussion) {
1836// $discussion is a discussion record object
04eba58f 1837
1838 $result = true;
1839
caadf009 1840 if ($posts = get_records("forum_posts", "discussion", $discussion->id)) {
1841 foreach ($posts as $post) {
1842 $post->course = $discussion->course;
1843 $post->forum = $discussion->forum;
1844 if (! delete_records("forum_ratings", "post", "$post->id")) {
1845 $result = false;
1846 }
1847 if (! forum_delete_post($post)) {
04eba58f 1848 $result = false;
1849 }
1850 }
1851 }
1852
caadf009 1853 if (! delete_records("forum_discussions", "id", "$discussion->id")) {
04eba58f 1854 $result = false;
1855 }
1856
1857 return $result;
1858}
1859
1860
caadf009 1861function forum_delete_post($post) {
1862 if (delete_records("forum_posts", "id", $post->id)) {
1863 delete_records("forum_ratings", "post", $post->id); // Just in case
1864 if ($post->attachment) {
1865 $discussion = get_record("forum_discussions", "id", $post->discussion);
1866 $post->course = $discussion->course;
1867 $post->forum = $discussion->forum;
1868 forum_delete_old_attachments($post);
1869 }
1870 return true;
1871 }
1872 return false;
1873}
501cdbd8 1874
501cdbd8 1875
b656e2a9 1876function forum_print_user_discussions($courseid, $userid, $groupid=0) {
caadf009 1877 global $CFG, $USER;
501cdbd8 1878
b8bf90c5 1879 $maxdiscussions = 10;
1880 $countdiscussions = 0;
1881
1f48942e 1882
b656e2a9 1883 if ($discussions = forum_get_user_discussions($courseid, $userid, $groupid=0)) {
caadf009 1884 $user = get_record("user", "id", $userid);
b656e2a9 1885 echo "<hr />";
1b26d5e7 1886 $fullname = fullname($user, isteacher($courseid));
1887 print_heading( get_string("discussionsstartedbyrecent", "forum", $fullname) );
caadf009 1888 $replies = forum_count_discussion_replies();
1889 foreach ($discussions as $discussion) {
b8bf90c5 1890 $countdiscussions++;
1891 if ($countdiscussions > $maxdiscussions) {
1892 break;
1893 }
caadf009 1894 if (($discussion->forumtype == "teacher") and !isteacher($courseid)) {
ffe11640 1895 continue;
1896 }
47f1da80 1897 if (!empty($replies[$discussion->discussion])) {
caadf009 1898 $discussion->replies = $replies[$discussion->discussion]->replies;
1899 } else {
1900 $discussion->replies = 0;
501cdbd8 1901 }
caadf009 1902 $inforum = get_string("inforum", "forum", "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname</A>");
1903 $discussion->subject .= " ($inforum)";
61e96406 1904 if (!empty($USER->id)) {
1905 $ownpost = ($discussion->userid == $USER->id);
1906 } else {
1907 $ownpost = false;
1908 }
caadf009 1909 forum_print_post($discussion, $courseid, $ownpost, $reply=0, $link=1, $assessed=false);
1910 echo "<BR>\n";
501cdbd8 1911 }
1912 }
501cdbd8 1913}
1914
501cdbd8 1915function forum_forcesubscribe($forumid, $value=1) {
1916 return set_field("forum", "forcesubscribe", $value, "id", $forumid);
1917}
1918
1919function forum_is_forcesubscribed($forumid) {
1920 return get_field("forum", "forcesubscribe", "id", $forumid);
1921}
1922
1923function forum_is_subscribed($userid, $forumid) {
1924 if (forum_is_forcesubscribed($forumid)) {
1925 return true;
1926 }
ebc3bd2b 1927 return record_exists("forum_subscriptions", "userid", $userid, "forum", $forumid);
86970225 1928}
1929
501cdbd8 1930function forum_subscribe($userid, $forumid) {
9fa49e22 1931/// Adds user to the subscriber list
1932
6673d7bd 1933 if (record_exists("forum_subscriptions", "userid", $userid, "forum", $forumid)) {
1934 return true;
1935 }
1936
ebc3bd2b 1937 $sub->userid = $userid;
9fa49e22 1938 $sub->forum = $forumid;
501cdbd8 1939
9fa49e22 1940 return insert_record("forum_subscriptions", $sub);
501cdbd8 1941}
1942
1943function forum_unsubscribe($userid, $forumid) {
9fa49e22 1944/// Removes user from the subscriber list
ebc3bd2b 1945 return delete_records("forum_subscriptions", "userid", $userid, "forum", $forumid);
501cdbd8 1946}
1947
0a9f61b5 1948function forum_post_subscription($post) {
1949/// Given a new post, subscribes or unsubscribes as appropriate.
1950/// Returns some text which describes what happened.
1951
1952 global $USER;
1953
1954 if (empty($post->subscribe) and empty($post->unsubscribe)) {
1955 return "";
1956 }
1957
1958 if (!$forum = get_record("forum", "id", $post->forum)) {
1959 return "";
1960 }
1961
1962 $info->name = "$USER->firstname $USER->lastname";
1963 $info->forum = $forum->name;
1964
1965 if (!empty($post->subscribe)) {
1966 forum_subscribe($USER->id, $post->forum);
1967 return "<p>".get_string("nowsubscribed", "forum", $info)."</p>";
1968 }
1969
1970 forum_unsubscribe($USER->id, $post->forum);
1971 return "<p>".get_string("nownotsubscribed", "forum", $info)."</p>";
1972}
1973
501cdbd8 1974
11b0c469 1975function forum_user_has_posted_discussion($forumid, $userid) {
29507631 1976 if ($discussions = forum_get_discussions($forumid, "", $userid)) {
501cdbd8 1977 return true;
1978 } else {
1979 return false;
1980 }
1981}
1982
9197e147 1983function forum_user_can_post_discussion($forum, $currentgroup=false) {
501cdbd8 1984// $forum is an object
1985 global $USER;
1986
1987 if ($forum->type == "eachuser") {
11b0c469 1988 return (! forum_user_has_posted_discussion($forum->id, $USER->id));
501cdbd8 1989 } else if ($forum->type == "teacher") {
1990 return isteacher($forum->course);
9197e147 1991 } else if ($currentgroup) {
d591b40f 1992 return (isteacheredit($forum->course) or (ismember($currentgroup) and $forum->open == 2));
501cdbd8 1993 } else if (isteacher($forum->course)) {
1994 return true;
1995 } else {
70c476a7 1996 return ($forum->open == 2);
501cdbd8 1997 }
1998}
1999
f690562f 2000function forum_user_can_post($forum, $user=NULL) {
2001// $forum, $user are objects
2002
2003 if ($user) {
2004 $isteacher = isteacher($forum->course, $user->id);
2005 } else {
2006 $isteacher = isteacher($forum->course);
2007 }
70c476a7 2008
2009 if ($forum->type == "teacher") {
f690562f 2010 return $isteacher;
2011 } else if ($isteacher) {
70c476a7 2012 return true;
2013 } else {
2014 return $forum->open;
2015 }
2016}
501cdbd8 2017
501cdbd8 2018
9197e147 2019function forum_print_latest_discussions($forum_id=0, $forum_numdiscussions=5,
2020 $forum_style="plain", $forum_sort="",
2021 $currentgroup=0) {
c585fa17 2022 global $CFG, $USER;
f93f848a 2023
2024 if ($forum_id) {
2025 if (! $forum = get_record("forum", "id", $forum_id)) {
2026 error("Forum ID was incorrect");
2027 }
2028 if (! $course = get_record("course", "id", $forum->course)) {
2029 error("Could not find the course this forum belongs to!");
2030 }
2031
2032 if ($course->category) {
2033 require_login($course->id);
2034 }
2035
2036 } else {
2037 if (! $course = get_record("course", "category", 0)) {
2038 error("Could not find a top-level course!");
2039 }
7beb45d8 2040 if (! $forum = forum_get_course_forum($course->id, "news")) {
f93f848a 2041 error("Could not find or create a main forum in this course (id $course->id)");
2042 }
2043 }
2044
9197e147 2045 if (forum_user_can_post_discussion($forum, $currentgroup)) {
b879effb 2046 echo "<p align=center>";
2047 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?forum=$forum->id\">";
0351b1f9 2048 if ($forum->type == "news") {
2049 echo get_string("addanewtopic", "forum")."</a>...";
2050 } else {
2051 echo get_string("addanewdiscussion", "forum")."</a>...";
2052 }
b879effb 2053 echo "</p>\n";
77305fe6 2054 }
2055
3c8a606d 2056 if ((!$forum_numdiscussions) && ($forum_style == "plain")) {
2057 $forum_style = "header"; // Abbreviate display by default
3335f6fb 2058 }
f93f848a 2059
2ab968e9 2060 if ($forum_style == "minimal") {
2061 $forum_sort = "p.modified DESC";
2062 }
2063
2064 $fullpost = false;
2065 if ($forum_style == "plain") {
2066 $fullpost = true;
2067 }
2068
9197e147 2069 if (! $discussions = forum_get_discussions($forum->id, $forum_sort, 0, $fullpost, $currentgroup) ) {
0351b1f9 2070 if ($forum->type == "news") {
2071 echo "<p align=center><b>(".get_string("nonews", "forum").")</b></p>";
2072 } else {
2073 echo "<p align=center><b>(".get_string("nodiscussions", "forum").")</b></p>";
2074 }
2ab968e9 2075 return;
2076 }
2077
3335f6fb 2078 $replies = forum_count_discussion_replies($forum->id);
f93f848a 2079
3260de67 2080 $canreply = forum_user_can_post($forum);
2081
3335f6fb 2082 $discussioncount = 0;
c20b762a 2083 $olddiscussionlink = false;
2ab968e9 2084 $strdatestring = get_string("strftimedaydatetime");
f93f848a 2085
dcde9f02 2086 if ($forum_style == "minimal") {
2087 $strftimerecent = get_string("strftimerecent");
2088 $strmore = get_string("more", "forum");
2089 }
2090
29507631 2091 if ($forum_style == "header") {
ba1ce4c0 2092 echo "<table width=\"100%\" border=0 cellpadding=3 cellspacing=1 class=\"forumheaderlist\">";
03aa0dfa 2093 echo "<tr class=\"forumpostheader\">";
9e0a08bd 2094 echo "<th>".get_string("discussion", "forum")."</th>";
29507631 2095 echo "<th colspan=2>".get_string("startedby", "forum")."</th>";
2096 echo "<th>".get_string("replies", "forum")."</th>";
2097 echo "<th>".get_string("lastpost", "forum")."</th>";
2098 echo "</tr>";
2099 }
2100
3335f6fb 2101 foreach ($discussions as $discussion) {
2102 $discussioncount++;
f93f848a 2103
3335f6fb 2104 if ($forum_numdiscussions && ($discussioncount > $forum_numdiscussions)) {
c20b762a 2105 $olddiscussionlink = true;
3335f6fb 2106 break;
2107 }
9c9f7d77 2108 if (!empty($replies[$discussion->discussion])) {
3335f6fb 2109 $discussion->replies = $replies[$discussion->discussion]->replies;
2110 } else {
2111 $discussion->replies = 0;
2112 }
f7477444 2113 if (!empty($USER->id)) {
2114 $ownpost = ($discussion->userid == $USER->id);
2115 } else {
2116 $ownpost=false;
2117 }
3335f6fb 2118 switch ($forum_style) {
2119 case "minimal":
436a7cae 2120 if (!empty($CFG->filterall)) {
2121 $discussion->subject = filter_text($discussion->subject, $forum->course);
2122 }
b656e2a9 2123 echo "<p><span class=\"smallinfohead\">".
2124 userdate($discussion->modified, $strftimerecent).
2125 " - ".
2126 fullname($discussion).
2127 "</span><br />";
00363389 2128 echo "<span class=\"smallinfo\">$discussion->subject ";
b879effb 2129 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->discussion\">";
00363389 2130 echo $strmore."...</a></span>";
b879effb 2131 echo "</p>\n";
3335f6fb 2132 break;
2133 case "header":
2ab968e9 2134 forum_print_discussion_header($discussion, $forum->course, $strdatestring);
3335f6fb 2135 break;
2136 default:
3260de67 2137 if ($canreply or $discussion->replies) {
2138 $link = true;
2139 } else {
2140 $link = false;
2141 }
2142 forum_print_post($discussion, $forum->course, $ownpost, $reply=0, $link, $assessed=false);
b879effb 2143 echo "<br>\n";
3335f6fb 2144 break;
f93f848a 2145 }
2146 }
29507631 2147
2148 if ($forum_style == "header") {
2149 echo "</table>";
2150 }
c20b762a 2151
2152 if ($olddiscussionlink) {
e2d41062 2153 if ($forum_style == "minimal") {
2154 echo '<p align="center">';
2155 } else {
2156 echo '<p align="right">';
2157 }
2158 echo "<a href=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id&showall=1\">";
c20b762a 2159 echo get_string("olderdiscussions", "forum")."</a> ...</p>";
2160 }
f93f848a 2161}
2162
c6d691dc 2163function forum_print_discussion($course, $forum, $discussion, $post, $mode, $canreply=NULL) {
501cdbd8 2164
2165 global $USER;
2166
61e96406 2167 if (!empty($USER->id)) {
2168 $ownpost = ($USER->id == $post->userid);
2169 } else {
2170 $ownpost = false;
2171 }
c6d691dc 2172 if ($canreply === NULL) {
2173 $reply = forum_user_can_post($forum);
2174 } else {
2175 $reply = $canreply;
2176 }
501cdbd8 2177
02ebf404 2178 $ratings = NULL;
74f5d1e3 2179 $ratingsmenuused = false;
61e96406 2180 if ($forum->assessed and !empty($USER->id)) {
d6bdd9d5 2181 if ($ratings->scale = make_grades_menu($forum->scale)) {
3bd98ad4 2182 $ratings->assesspublic = $forum->assesspublic;
98914efd 2183 $ratings->assesstimestart = $forum->assesstimestart;
2184 $ratings->assesstimefinish = $forum->assesstimefinish;
3bd98ad4 2185 $ratings->allow = ($forum->assessed != 2 or isteacher($course->id));
2186
02ebf404 2187 echo "<form name=form method=post action=rate.php>";
2188 echo "<input type=hidden name=id value=\"$course->id\">";
9d1b97c5 2189 }
2190 }
2191
74f5d1e3 2192 if (forum_print_post($post, $course->id, $ownpost, $reply, $link=false, $ratings)) {
2193 $ratingsmenuused = true;
2194 }
501cdbd8 2195
2196 switch ($mode) {
2e2e71a8 2197 case FORUM_MODE_FLATOLDEST :
2198 case FORUM_MODE_FLATNEWEST :
501cdbd8 2199 default:
02ebf404 2200 echo "<ul>";
74f5d1e3 2201 if (forum_print_posts_flat($post->discussion, $course->id, $mode, $ratings, $reply)) {
2202 $ratingsmenuused = true;
2203 }
02ebf404 2204 echo "</ul>";
501cdbd8 2205 break;
2206
2e2e71a8 2207 case FORUM_MODE_THREADED :
74f5d1e3 2208 if (forum_print_posts_threaded($post->id, $course->id, 0, $ratings, $reply)) {
2209 $ratingsmenuused = true;
2210 }
501cdbd8 2211 break;
2212
2e2e71a8 2213 case FORUM_MODE_NESTED :
74f5d1e3 2214 if (forum_print_posts_nested($post->id, $course->id, $ratings, $reply)) {
2215 $ratingsmenuused = true;
2216 }
501cdbd8 2217 break;
2218 }
2219
74f5d1e3 2220 if ($ratingsmenuused) {
02ebf404 2221 echo "<center><input type=\"submit\" value=\"".get_string("sendinratings", "forum")."\">";
fa01ae8b 2222 if ($forum->scale < 0) {
2223 if ($scale = get_record("scale", "id", abs($forum->scale))) {
2224 print_scale_menu_helpbutton($course->id, $scale );
2225 }
2226 }
02ebf404 2227 echo "</center>";
2228 echo "</form>";
501cdbd8 2229 }
2230}
2231
02ebf404 2232function forum_print_posts_flat($discussion, $course, $direction, $ratings, $reply) {
501cdbd8 2233 global $USER;
2234
501cdbd8 2235 $link = false;
74f5d1e3 2236 $ratingsmenuused = false;
501cdbd8 2237
2238 if ($direction < 0) {
2239 $sort = "ORDER BY created DESC";
2240 } else {
2241 $sort = "ORDER BY created ASC";
2242 }
2243
1f48942e 2244 if ($posts = forum_get_discussion_posts($discussion, $sort)) {
501cdbd8 2245 foreach ($posts as $post) {
ebc3bd2b 2246 $ownpost = ($USER->id == $post->userid);
74f5d1e3 2247 if (forum_print_post($post, $course, $ownpost, $reply, $link, $ratings)) {
2248 $ratingsmenuused = true;
2249 }
501cdbd8 2250 }
501cdbd8 2251 }
74f5d1e3 2252
2253 return $ratingsmenuused;
501cdbd8 2254}
2255
02ebf404 2256function forum_print_posts_threaded($parent, $course, $depth, $ratings, $reply) {
501cdbd8 2257 global $USER;
2258
501cdbd8 2259 $link = false;
74f5d1e3 2260 $ratingsmenuused = false;
501cdbd8 2261
1f48942e 2262 if ($posts = forum_get_child_posts($parent)) {
501cdbd8 2263 foreach ($posts as $post) {
2264
f95c2a73 2265 echo "<ul>";
501cdbd8 2266 if ($depth > 0) {
ebc3bd2b 2267 $ownpost = ($USER->id == $post->userid);
74f5d1e3 2268 if (forum_print_post($post, $course, $ownpost, $reply, $link, $ratings)) {
2269 $ratingsmenuused = true;
2270 }
f95c2a73 2271 echo "<br />";
501cdbd8 2272 } else {
1b26d5e7 2273 $by->name = fullname($post, isteacher($course->id));
d62413e8 2274 $by->date = userdate($post->modified);
f95c2a73 2275 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 2276 print_string("bynameondate", "forum", $by);
f95c2a73 2277 echo "</font></p></li>";
501cdbd8 2278 }
2279
74f5d1e3 2280 if (forum_print_posts_threaded($post->id, $course, $depth-1, $ratings, $reply)) {
2281 $ratingsmenuused = true;
2282 }
f95c2a73 2283 echo "</ul>\n";
501cdbd8 2284 }
501cdbd8 2285 }
74f5d1e3 2286 return $ratingsmenuused;
501cdbd8 2287}
2288
02ebf404 2289function forum_print_posts_nested($parent, $course, $ratings, $reply) {
501cdbd8 2290 global $USER;
2291
501cdbd8 2292 $link = false;
74f5d1e3 2293 $ratingsmenuused = false;
501cdbd8 2294
1f48942e 2295 if ($posts = forum_get_child_posts($parent)) {
501cdbd8 2296 foreach ($posts as $post) {
2297
61e96406 2298 if (empty($USER->id)) {
2299 $ownpost = false;
2300 } else {
2301 $ownpost = ($USER->id == $post->userid);
2302 }
501cdbd8 2303
74f5d1e3 2304 echo "<ul>";
2305 if (forum_print_post($post, $course, $ownpost, $reply, $link, $ratings)) {
2306 $ratingsmenuused = true;
2307 }
2308 echo "<br />";
2309 if (forum_print_posts_nested($post->id, $course, $ratings, $reply)) {
2310 $ratingsmenuused = true;
2311 }
2312 echo "</ul>\n";
501cdbd8 2313 }
501cdbd8 2314 }
74f5d1e3 2315 return $ratingsmenuused;
501cdbd8 2316}
2317
a57a7794 2318function forum_get_recent_mod_activity(&$activities, &$index, $sincetime, $courseid, $forum="0", $user="", $groupid="") {
2319// Returns all forum posts since a given time. If forum is specified then
2320// this restricts the results
2321
2322 global $CFG;
2323
2324 if ($forum) {
2325 $forumselect = " AND cm.id = '$forum'";
2326 } else {
2327 $forumselect = "";
2328 }
2329
2330 if ($user) {
2331 $userselect = " AND u.id = '$user'";
2332 } else {
2333 $userselect = "";
2334 }
2335
a57a7794 2336 $posts = get_records_sql("SELECT p.*, d.name, u.firstname, u.lastname,
2337 u.picture, d.groupid, cm.instance, f.name, cm.section
2338 FROM {$CFG->prefix}forum_posts p,
2339 {$CFG->prefix}forum_discussions d,
2340 {$CFG->prefix}user u,
2341 {$CFG->prefix}course_modules cm,
2342 {$CFG->prefix}forum f
2343 WHERE p.modified > '$sincetime' $forumselect
2344 AND p.userid = u.id $userselect
2345 AND d.course = '$courseid'
2346 AND p.discussion = d.id $groupselect
2347 AND cm.instance = f.id
2348 AND cm.course = d.course
2349 AND cm.course = f.course
2350 AND f.id = d.forum
2351 ORDER BY d.id");
2352
90708fc1 2353 if (empty($posts))
2354 return;
a57a7794 2355
90708fc1 2356 foreach ($posts as $post) {
a57a7794 2357
90708fc1 2358 if (empty($groupid) || ismember($groupid, $post->userid)) {
2359 $tmpactivity->type = "forum";
2360 $tmpactivity->defaultindex = $index;
2361 $tmpactivity->instance = $post->instance;
2362 $tmpactivity->name = $post->name;
2363 $tmpactivity->section = $post->section;
a57a7794 2364
90708fc1 2365 $tmpactivity->content->id = $post->id;
2366 $tmpactivity->content->discussion = $post->discussion;
2367 $tmpactivity->content->subject = $post->subject;
2368 $tmpactivity->content->parent = $post->parent;
2369
2370 $tmpactivity->user->userid = $post->userid;
2371 $tmpactivity->user->fullname = fullname($post);
2372 $tmpactivity->user->picture = $post->picture;
a57a7794 2373
90708fc1 2374 $tmpactivity->timestamp = $post->modified;
2375 $activities[] = $tmpactivity;
2376
2377 $index++;
2378 }
2379 }
a57a7794 2380
90708fc1 2381 return;
a57a7794 2382}
2383
2384function forum_print_recent_mod_activity($activity, $course, $detail=false) {
2385
2386 global $CFG;
2387
2388 echo '<table border="0" cellpadding="3" cellspacing="0">';
2389
2390 if ($activity->content->parent) {
2391 $openformat = "<font size=\"2\"><i>";
2392 $closeformat = "</i></font>";
2393 } else {
2394 $openformat = "<b>";
2395 $closeformat = "</b>";
2396 }
2397
2398 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
2399 print_user_picture($activity->user->userid, $course, $activity->user->picture);
2400 echo "</td><td>$openformat";
2401
2402 if ($detail) {
2403 echo "<img src=\"$CFG->modpixpath/$activity->type/icon.gif\" ".
2404 "height=16 width=16 alt=\"$activity->name\"> ";
2405 }
2406 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=" . $activity->content->discussion
2407 . "#" . $activity->content->id . "\">";
2408
2409 echo $activity->content->subject;
2410 echo "</a>$closeformat";
2411
2412 echo "<br><font size=\"2\">";
2413 echo "<a href=\"$CFG->wwwroot/user/view.php?id=" . $activity->user->userid . "&course=" . "$course\">"
2414 . $activity->user->fullname . "</a>";
2415 echo " - " . userdate($activity->timestamp) . "</font></td></tr>";
2416 echo "</table>";
2417
2418 return;
2419}
2420
f93f848a 2421?>