Make sure course list block is never empty.
[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);
65b0e537 28}
73bb0835 29
5be7800c 30if (!isset($CFG->forum_shortpost)) {
31 set_config("forum_shortpost", 300); // Less non-HTML characters than this is short
65b0e537 32}
4d871a72 33
5be7800c 34if (!isset($CFG->forum_longpost)) {
35 set_config("forum_longpost", 600); // More non-HTML characters than this is long
65b0e537 36}
4d871a72 37
5be7800c 38if (!isset($CFG->forum_manydiscussions)) {
39 set_config("forum_manydiscussions", 100); // Number of discussions on a page
65b0e537 40}
e07635f4 41
4909e176 42if (!isset($CFG->forum_maxbytes)) {
43 set_config("forum_maxbytes", 512000); // Default maximum size for all forums
65b0e537 44}
4909e176 45
46
e07635f4 47
caadf009 48/// STANDARD FUNCTIONS ///////////////////////////////////////////////////////////
49
50function forum_add_instance($forum) {
65b0e537 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
caadf009 54// of the new instance.
55
56 global $CFG;
57
58 $forum->timemodified = time();
274e05db 59 $forum->intro = clean_text($forum->intro);
caadf009 60
f2f56406 61 if (!$forum->userating) {
62 $forum->assessed = 0;
63 }
64
98914efd 65 if (!empty($forum->ratingtime)) {
65b0e537 66 $forum->assesstimestart = make_timestamp($forum->startyear, $forum->startmonth, $forum->startday,
98914efd 67 $forum->starthour, $forum->startminute, 0);
65b0e537 68 $forum->assesstimefinish = make_timestamp($forum->finishyear, $forum->finishmonth, $forum->finishday,
98914efd 69 $forum->finishhour, $forum->finishminute, 0);
70 } else {
71 $forum->assesstimestart = 0;
72 $forum->assesstimefinish = 0;
73 }
caadf009 74
cb9a975f 75 if (! $forum->id = insert_record("forum", $forum)) {
76 return false;
77 }
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) {
65b0e537 96// Given an object containing all the necessary data,
97// (defined by the form in mod.html) this function
caadf009 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)) {
65b0e537 108 $forum->assesstimestart = make_timestamp($forum->startyear, $forum->startmonth, $forum->startday,
98914efd 109 $forum->starthour, $forum->startminute, 0);
65b0e537 110 $forum->assesstimefinish = make_timestamp($forum->finishyear, $forum->finishmonth, $forum->finishday,
98914efd 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) {
65b0e537 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.
caadf009 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
65b0e537 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,
edffca15 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
65b0e537 200 foreach ($posts as $key => $post) {
edffca15 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 }
65b0e537 268
8c0ef41d 269 /// GWD: reset timelimit so that script does not get timed out when posting to many users
1bf4c862 270 @set_time_limit(0);
65b0e537 271
272 /// Override the language and timezone of the "current" user, so that
ec2137ba 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 }
65b0e537 314
caadf009 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 }
65b0e537 334
caadf009 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) {
65b0e537 386/// Given a course and a date, prints a summary of all the new
cc3655a2 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);
65b0e537 411
1b5910c4 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);
65b0e537 417
1b5910c4 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 }
65b0e537 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
65b0e537 541//(users with records in forum_subscriptions, forum_posts and forum_ratings, students)
05855091 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
65b0e537 550 u.id = s.userid");
05855091 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
65b0e537 589
0f1a97c2 590 $return = false;
65b0e537 591
0f1a97c2 592 $rec = get_record("forum","id","$forumid","scale","-$scaleid");
65b0e537 593
594 if (!empty($rec) && !empty($scaleid)) {
0f1a97c2 595 $return = true;
596 }
65b0e537 597
0f1a97c2 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
65b0e537 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
65b0e537 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
65b0e537 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;
65b0e537 666 default:
c2a96d6b 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
65b0e537 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)
65b0e537 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";
65b0e537 721
8b9c7aa0 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;
65b0e537 732 return get_records_sql("SELECT u.*, r.rating, r.time
733 FROM {$CFG->prefix}forum_ratings r,
9fa49e22 734 {$CFG->prefix}user u
65b0e537 735 WHERE r.post = '$postid'
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;
65b0e537 743 return get_records_sql("SELECT p.*, d.course
744 FROM {$CFG->prefix}forum_posts p,
1f48942e 745 {$CFG->prefix}forum_discussions d
65b0e537 746 WHERE p.mailed = 0
747 AND p.created < '$cutofftime'
1f48942e 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
65b0e537 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
1f48942e 762 AND p.discussion = d.id
65b0e537 763 AND p.userid = '$userid'
ebc3bd2b 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
65b0e537 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
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
65b0e537 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
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.*
65b0e537 803 FROM {$CFG->prefix}forum_discussions d,
d05956ac 804 {$CFG->prefix}forum_posts p
65b0e537 805 WHERE d.id = '$discussionid'
d05956ac 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
65b0e537 815 FROM {$CFG->prefix}forum_discussions d,
816 {$CFG->prefix}forum_posts p,
1f48942e 817 {$CFG->prefix}forum_ratings r
65b0e537 818 WHERE d.forum = '$forumid'
1f48942e 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
65b0e537 833 FROM {$CFG->prefix}forum_posts p,
1f48942e 834 {$CFG->prefix}forum_discussions d
65b0e537 835 WHERE p.parent > 0
836 AND p.discussion = d.id
1f48942e 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
65b0e537 845 WHERE parent > 0
846 AND discussion = '$discussionid'
ebc3bd2b 847 AND userid <> '$userid' ")) {
1f48942e 848
65b0e537 849 if ($rated = get_record_sql("SELECT count(*) as num
850 FROM {$CFG->prefix}forum_posts p,
1f48942e 851 {$CFG->prefix}forum_ratings r
852 WHERE p.discussion = '$discussionid'
65b0e537 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
65b0e537 869function forum_get_discussions($forum="0", $forumsort="d.timemodified DESC",
9197e147 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
cf84431b 893 return get_records_sql("SELECT $postdata, d.name, d.timemodified, d.usermodified,
016cd6af 894 u.firstname, u.lastname, u.email, u.picture
65b0e537 895 FROM {$CFG->prefix}forum_discussions d,
9197e147 896 {$CFG->prefix}forum_posts p,
65b0e537 897 {$CFG->prefix}user u
898 WHERE d.forum = '$forum'
899 AND p.discussion = d.id
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
65b0e537 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
65b0e537 919 FROM {$CFG->prefix}forum_discussions d,
920 {$CFG->prefix}forum_posts p,
921 {$CFG->prefix}user u,
1f48942e 922 {$CFG->prefix}forum f
65b0e537 923 WHERE d.course = '$courseid'
924 AND p.discussion = d.id
925 AND p.parent = 0
926 AND p.userid = u.id
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
65b0e537 958 FROM {$CFG->prefix}user u,
6673d7bd 959 {$CFG->prefix}forum_subscriptions s $grouptables
1f48942e 960 WHERE s.forum = '$forum->id'
65b0e537 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")) {
65b0e537 974 // There should always only be ONE, but with the right combination of
29cbd93a 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"));
a796d0b8 1008 $forum->open = 2; // 0 - no, 1 - posts only, 2 - discuss and post
8daaf761 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 }
65b0e537 1045
8daaf761 1046 return get_record("forum", "id", "$forum->id");
82aa0e8d 1047}
1048
f93f848a 1049
65b0e537 1050function forum_make_mail_post(&$post, $user, $touser, $course,
11b0c469 1051 $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
65b0e537 1052// Given the data about a posting, builds up the HTML to display it and
501cdbd8 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) {
14eef464 1117 $output .= "<a href=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."</a>";
501cdbd8 1118 if ($reply) {
cf84431b 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>";
65b0e537 1130
501cdbd8 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
65b0e537 1150function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link=false,
98914efd 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 }
65b0e537 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 }
cf84431b 1248
1249 if (isteacheredit($courseid) and $post->parent) {
1250 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?prune=$post->id\" title=\"".get_string('pruneheading', 'forum').'">'.
1251 get_string("prune", "forum")."</a> | ";
1252 }
1253
3bd98ad4 1254 if ($ownpost or $isteacher) {
14eef464 1255 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">$strdelete</a>";
1256 if ($reply) {
1257 echo " | ";
1258 } else {
1259 echo "&nbsp;&nbsp;";
501cdbd8 1260 }
64eacd6f 1261 }
1262 if ($reply) {
2e2e71a8 1263 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">$strreply</a>";
501cdbd8 1264 echo "&nbsp;&nbsp;";
501cdbd8 1265 }
72d497d4 1266 echo "</p>";
501cdbd8 1267
72d497d4 1268 echo "<div align=right><p align=right>";
74f5d1e3 1269
1270 $ratingsmenuused = false;
02ebf404 1271 if (!empty($ratings) and !empty($USER->id)) {
98914efd 1272 $useratings = true;
1273 if ($ratings->assesstimestart and $ratings->assesstimefinish) {
1274 if ($post->created < $ratings->assesstimestart or $post->created > $ratings->assesstimefinish) {
1275 $useratings = false;
1276 }
1277 }
1278 if ($useratings) {
3bd98ad4 1279 $mypost = ($USER->id == $post->userid);
1280
1281 if (($isteacher or $ratings->assesspublic) and !$mypost) {
1282 forum_print_ratings_mean($post->id, $ratings->scale, $isteacher);
1283 forum_print_rating_menu($post->id, $USER->id, $ratings->scale);
1284 $ratingsmenuused = true;
1285
1286 } else if ($mypost) {
1287 forum_print_ratings_mean($post->id, $ratings->scale, true);
1288
98914efd 1289 } else if (!empty($ratings->allow) ) {
1290 forum_print_rating_menu($post->id, $USER->id, $ratings->scale);
74f5d1e3 1291 $ratingsmenuused = true;
2a3cda19 1292 }
501cdbd8 1293 }
1294 }
65b0e537 1295
501cdbd8 1296 if ($link) {
1297 if ($post->replies == 1) {
ffe11640 1298 $replystring = get_string("repliesone", "forum", $post->replies);
501cdbd8 1299 } else {
ffe11640 1300 $replystring = get_string("repliesmany", "forum", $post->replies);
501cdbd8 1301 }
74f5d1e3 1302 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><b>".
1303 get_string("discussthistopic", "forum")."</b></a> ($replystring)&nbsp;&nbsp;";
501cdbd8 1304 }
72d497d4 1305 echo "</p>";
501cdbd8 1306 if ($footer) {
72d497d4 1307 echo "<p>$footer</p>";
501cdbd8 1308 }
72d497d4 1309 echo "</div>";
1310 echo "</td></tr>\n</table>\n\n";
74f5d1e3 1311
1312 return $ratingsmenuused;
501cdbd8 1313}
1314
3335f6fb 1315
a796d0b8 1316function forum_print_discussion_header(&$post, $forum, $datestring="") {
c585fa17 1317 global $THEME, $USER, $CFG;
3335f6fb 1318
436a7cae 1319 if (!empty($CFG->filterall)) {
a796d0b8 1320 $post->subject = filter_text("<nolink>$post->subject</nolink>", $forum->course);
436a7cae 1321 }
1322
29507631 1323 echo "<tr class=\"forumpostheader\">";
3335f6fb 1324
29507631 1325 // Topic
546be6dd 1326 echo "<td bgcolor=\"$THEME->cellheading2\" class=\"forumpostheadertopic\" width=\"100%\">";
29507631 1327 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">$post->subject</a>";
1328 echo "</td>\n";
1329
1330 // Picture
96e301a7 1331 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderpicture\" width=35>";
a796d0b8 1332 print_user_picture($post->userid, $forum->course, $post->picture);
29507631 1333 echo "</td>\n";
3335f6fb 1334
29507631 1335 // User name
a796d0b8 1336 $fullname = fullname($post, isteacher($forum->course));
96e301a7 1337 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheadername\" align=left nowrap>";
a796d0b8 1338 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$forum->course\">$fullname</a>";
29507631 1339 echo "</td>\n";
1340
a796d0b8 1341 if ($forum->open or $forum->type == "teacher") { // Show the column with replies
1342 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderreplies\" align=center nowrap>";
1343 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">$post->replies</a>";
1344 echo "</td>\n";
1345 }
3335f6fb 1346
96e301a7 1347 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderdate\" align=right nowrap>";
29507631 1348 if (!empty($post->timemodified)) {
2ab968e9 1349 echo userdate($post->timemodified, $datestring);
3335f6fb 1350 } else {
2ab968e9 1351 echo userdate($post->modified, $datestring);
3335f6fb 1352 }
29507631 1353 echo "</td>\n";
1354
1355 echo "</tr>\n";
3335f6fb 1356
3335f6fb 1357}
1358
1359
aa153f29 1360function forum_shorten_post($message) {
c585fa17 1361// Given a post object that we already know has a long message
65b0e537 1362// this function truncates the message nicely to the first
5be7800c 1363// sane place between $CFG->forum_longpost and $CFG->forum_shortpost
1364
1365 global $CFG;
c585fa17 1366
1367 $i = 0;
1368 $tag = false;
1369 $length = strlen($message);
1370 $count = 0;
1371 $stopzone = false;
1372 $truncate = 0;
1373
1374 for ($i=0; $i<$length; $i++) {
a8afb411 1375 $char = $message[$i];
c585fa17 1376
1377 switch ($char) {
65b0e537 1378 case "<":
c585fa17 1379 $tag = true;
1380 break;
65b0e537 1381 case ">":
c585fa17 1382 $tag = false;
1383 break;
1384 default:
1385 if (!$tag) {
1386 if ($stopzone) {
67f0b4cc 1387 if ($char == ".") {
a8afb411 1388 $truncate = $i+1;
c585fa17 1389 break 2;
1390 }
1391 }
1392 $count++;
1393 }
a8afb411 1394 break;
c585fa17 1395 }
1396 if (!$stopzone) {
5be7800c 1397 if ($count > $CFG->forum_shortpost) {
c585fa17 1398 $stopzone = true;
1399 }
1400 }
1401 }
aa153f29 1402
c585fa17 1403 if (!$truncate) {
a8afb411 1404 $truncate = $i;
c585fa17 1405 }
1406
67f0b4cc 1407 return substr($message, 0, $truncate);
aa153f29 1408}
1409
501cdbd8 1410
3bd98ad4 1411function forum_print_ratings_mean($postid, $scale, $link=true) {
02ebf404 1412/// Print the multiple ratings on a post given to the current user by others.
1413/// Scale is an array of ratings
1414
1415 static $strrate;
05c47ef7 1416
1417 $mean = forum_get_ratings_mean($postid, $scale);
65b0e537 1418
05c47ef7 1419 if ($mean !== "") {
02ebf404 1420
1421 if (empty($strratings)) {
1422 $strratings = get_string("ratings", "forum");
501cdbd8 1423 }
501cdbd8 1424
02ebf404 1425 echo "$strratings: ";
3bd98ad4 1426 if ($link) {
1427 link_to_popup_window ("/mod/forum/report.php?id=$postid", "ratings", $mean, 400, 600);
1428 } else {
1429 echo "$mean ";
1430 }
501cdbd8 1431 }
1432}
1433
501cdbd8 1434
0761d83f 1435function forum_get_ratings_mean($postid, $scale, $ratings=NULL) {
1436/// Return the mean rating of a post given to the current user by others.
02ebf404 1437/// Scale is an array of possible ratings in the scale
1438/// Ratings is an optional simple array of actual ratings (just integers)
1439
1440 if (!$ratings) {
1441 $ratings = array();
1442 if ($rates = get_records("forum_ratings", "post", $postid)) {
1443 foreach ($rates as $rate) {
1444 $ratings[] = $rate->rating;
1445 }
501cdbd8 1446 }
501cdbd8 1447 }
02ebf404 1448
0761d83f 1449 $count = count($ratings);
1450
1451 if ($count == 0) {
02ebf404 1452 return "";
02ebf404 1453
0761d83f 1454 } else if ($count == 1) {
1455 return $scale[$ratings[0]];
1456
02ebf404 1457 } else {
0761d83f 1458 $total = 0;
1459 foreach ($ratings as $rating) {
1460 $total += $rating;
1461 }
1462 $mean = round( ((float)$total/(float)$count) + 0.001); // Little fudge factor so that 0.5 goes UP
65b0e537 1463
0761d83f 1464 if (isset($scale[$mean])) {
1465 return $scale[$mean]." ($count)";
1466 } else {
1467 return "$mean ($count)"; // Should never happen, hopefully
1468 }
02ebf404 1469 }
1470}
1471
1472function forum_get_ratings_summary($postid, $scale, $ratings=NULL) {
1473/// Return a summary of post ratings given to the current user by others.
1474/// Scale is an array of possible ratings in the scale
1475/// Ratings is an optional simple array of actual ratings (just integers)
1476
1477 if (!$ratings) {
1478 $ratings = array();
1479 if ($rates = get_records("forum_ratings", "post", $postid)) {
1480 foreach ($rates as $rate) {
1481 $rating[] = $rate->rating;
1482 }
1483 }
1484 }
1485
1486
1487 if (!$count = count($ratings)) {
1488 return "";
1489 }
1490
1491
1492 foreach ($scale as $key => $scaleitem) {
1493 $sumrating[$key] = 0;
1494 }
1495
1496 foreach ($ratings as $rating) {
1497 $sumrating[$rating]++;
1498 }
1499
1500 $summary = "";
1501 foreach ($scale as $key => $scaleitem) {
1502 $summary = $sumrating[$key].$summary;
1503 if ($key > 1) {
1504 $summary = "/$summary";
1505 }
1506 }
1507 return $summary;
1508}
1509
1510function forum_print_rating_menu($postid, $userid, $scale) {
65b0e537 1511/// Print the menu of ratings as part of a larger form.
02ebf404 1512/// If the post has already been - set that value.
1513/// Scale is an array of ratings
1514
1515 static $strrate;
1516
1517 if (!$rating = get_record("forum_ratings", "userid", $userid, "post", $postid)) {
1518 $rating->rating = 0;
1519 }
1520
1521 if (empty($strrate)) {
1522 $strrate = get_string("rate", "forum");
1523 }
1524
1525 choose_from_menu($scale, $postid, $rating->rating, "$strrate...");
501cdbd8 1526}
1527
7a12aab4 1528function forum_print_mode_form($discussion, $mode) {
1529 GLOBAL $FORUM_LAYOUT_MODES;
501cdbd8 1530
8b9c7aa0 1531 echo "<center><p>";
7a12aab4 1532 popup_form("discuss.php?d=$discussion&mode=", $FORUM_LAYOUT_MODES, "mode", $mode, "");
8b9c7aa0 1533 echo "</p></center>\n";
501cdbd8 1534}
1535
97485d07 1536function forum_print_search_form($course, $search="", $return=false, $type="") {
501cdbd8 1537 global $CFG;
1538
97485d07 1539 if ($type == "plain") {
8b9c7aa0 1540 $output = "<table border=0 cellpadding=0 cellspacing=0><tr><td nowrap>";
1541 $output .= "<form name=search action=\"$CFG->wwwroot/mod/forum/search.php\">";
1542 $output .= "<font size=\"-1\">";
1543 $output .= "<input name=search type=text size=20 value=\"$search\">";
1544 $output .= "<input value=\"".get_string("searchforums", "forum")."\" type=submit>";
1545 $output .= "</font>";
1546 $output .= "<input name=id type=hidden value=\"$course->id\">";
1547 $output .= "</form>";
1548 $output .= "</td></tr></table>";
97485d07 1549 } else {
8b9c7aa0 1550 $output = "<table border=0 cellpadding=10 cellspacing=0><tr><td align=center>";
1551 $output .= "<form name=search action=\"$CFG->wwwroot/mod/forum/search.php\">";
1552 $output .= "<font size=\"-1\">";
1553 $output .= "<input name=search type=text size=20 value=\"$search\"><br>";
1554 $output .= "<input value=\"".get_string("searchforums", "forum")."\" type=submit>";
1555 $output .= "</font>";
1556 $output .= "<input name=id type=hidden value=\"$course->id\">";
1557 $output .= "</form>";
1558 $output .= "</td></tr></table>";
97485d07 1559 }
5e367a2d 1560
1561 if ($return) {
1562 return $output;
1563 }
1564 echo $output;
501cdbd8 1565}
1566
1567
11b0c469 1568function forum_set_return() {
607809b3 1569 global $CFG, $SESSION;
501cdbd8 1570
28e1e8b9 1571 if (! isset($SESSION->fromdiscussion)) {
48d38fad 1572 if (!empty($_SERVER['HTTP_REFERER'])) {
1573 $referer = $_SERVER['HTTP_REFERER'];
1574 } else {
1575 $referer = "";
1576 }
28e1e8b9 1577 // If the referer is NOT a login screen then save it.
48d38fad 1578 if (! strncasecmp("$CFG->wwwroot/login", $referer, 300)) {
607809b3 1579 $SESSION->fromdiscussion = $_SERVER["HTTP_REFERER"];
28e1e8b9 1580 }
501cdbd8 1581 }
1582}
1583
1584
11b0c469 1585function forum_go_back_to($default) {
501cdbd8 1586 global $SESSION;
1587
9c9f7d77 1588 if (!empty($SESSION->fromdiscussion)) {
501cdbd8 1589 $returnto = $SESSION->fromdiscussion;
1590 unset($SESSION->fromdiscussion);
1591 return $returnto;
1592 } else {
1593 return $default;
1594 }
1595}
1596
7f6689e4 1597function forum_file_area_name($post) {
1598// Creates a directory file name, suitable for make_upload_directory()
1599 global $CFG;
1600
1601 return "$post->course/$CFG->moddata/forum/$post->forum/$post->id";
1602}
1603
1604function forum_file_area($post) {
1605 return make_upload_directory( forum_file_area_name($post) );
1606}
1607
1608function forum_delete_old_attachments($post, $exception="") {
1609// Deletes all the user files in the attachments area for a post
1610// EXCEPT for any file named $exception
1611
1612 if ($basedir = forum_file_area($post)) {
1613 if ($files = get_directory_list($basedir)) {
1614 foreach ($files as $file) {
1615 if ($file != $exception) {
1616 unlink("$basedir/$file");
1617 notify("Existing file '$file' has been deleted!");
1618 }
1619 }
1620 }
1621 if (!$exception) { // Delete directory as well, if empty
1622 rmdir("$basedir");
1623 }
1624 }
1625}
1626
cc2b7ea5 1627function forum_move_attachments($discussion, $forumid) {
65b0e537 1628/// Given a discussion object that is being moved to forumid,
1629/// this function checks all posts in that discussion
1630/// for attachments, and if any are found, these are
cc2b7ea5 1631/// moved to the new forum directory.
1632
1633 global $CFG;
1634
1635 $return = true;
1636
1637 if ($posts = get_records_select("forum_posts", "discussion = '$discussion->id' AND attachment <> ''")) {
1638 foreach ($posts as $oldpost) {
1639 $oldpost->course = $discussion->course;
1640 $oldpost->forum = $discussion->forum;
1641 $oldpostdir = "$CFG->dataroot/".forum_file_area_name($oldpost);
1642 if (is_dir($oldpostdir)) {
1643 $newpost = $oldpost;
1644 $newpost->forum = $forumid;
7f0c3a44 1645 $newpostdir = forum_file_area($newpost);
cc2b7ea5 1646 if (! @rename($oldpostdir, $newpostdir)) {
1647 $return = false;
1648 }
1649 }
1650 }
1651 }
1652 return $return;
1653}
1654
7f6689e4 1655function forum_print_attachments($post, $return=NULL) {
1656// if return=html, then return a html string.
1657// if return=text, then return a text-only string.
72d497d4 1658// otherwise, print HTML for non-images, and return image HTML
7f6689e4 1659
1660 global $CFG;
1661
1662 $filearea = forum_file_area_name($post);
1663
72d497d4 1664 $imagereturn = "";
1665 $output = "";
1666
7f6689e4 1667 if ($basedir = forum_file_area($post)) {
1668 if ($files = get_directory_list($basedir)) {
1669 $strattachment = get_string("attachment", "forum");
6afe8558 1670 $strpopupwindow = get_string("popupwindow");
7f6689e4 1671 foreach ($files as $file) {
1672 $icon = mimeinfo("icon", $file);
1673 if ($CFG->slasharguments) {
1674 $ffurl = "file.php/$filearea/$file";
1675 } else {
1676 $ffurl = "file.php?file=/$filearea/$file";
1677 }
096b5432 1678 $image = "<img border=\"0\" src=\"$CFG->pixpath/f/$icon\" height=\"16\" width=\"16\" alt=\"$strpopupwindow\">";
7f6689e4 1679
1680 if ($return == "html") {
6afe8558 1681 $output .= "<a href=\"$CFG->wwwroot/$ffurl\">$image</a> ";
1682 $output .= "<a href=\"$CFG->wwwroot/$ffurl\">$file</a><br />";
7f6689e4 1683
1684 } else if ($return == "text") {
1685 $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
1686
1687 } else {
72d497d4 1688 if ($icon == "image.gif") { // Image attachments don't get printed as links
1689 $imagereturn .= "<br /><img src=\"$CFG->wwwroot/$ffurl\">";
1690 } else {
1691 link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
1692 echo "<a href=\"$CFG->wwwroot/$ffurl\">$file</a>";
1693 echo "<br />";
1694 }
7f6689e4 1695 }
1696 }
1697 }
1698 }
72d497d4 1699
7f6689e4 1700 if ($return) {
1701 return $output;
1702 }
72d497d4 1703
1704 return $imagereturn;
7f6689e4 1705}
1706
1707function forum_add_attachment($post, $newfile) {
1708// $post is a full post record, including course and forum
3b7c1de9 1709// $newfile is a full upload array from $_FILES
7f6689e4 1710// If successful, this function returns the name of the file
1711
9dd0b378 1712 global $CFG;
1713
3b7c1de9 1714 if (empty($newfile['name'])) {
7f6689e4 1715 return "";
1716 }
1717
4909e176 1718 if (!$forum = get_record("forum", "id", $post->forum)) {
1719 return "";
1720 }
1721
1722 if (!$course = get_record("course", "id", $forum->course)) {
1723 return "";
1724 }
1725
1726 $maxbytes = get_max_upload_file_size($CFG->maxbytes, $course->maxbytes, $forum->maxbytes);
1727
7f6689e4 1728 $newfile_name = clean_filename($newfile['name']);
1729
1730 if (valid_uploaded_file($newfile)) {
4909e176 1731 if ($maxbytes and $newfile['size'] > $maxbytes) {
1732 return "";
1733 }
7f6689e4 1734 if (! $newfile_name) {
1735 notify("This file had a wierd filename and couldn't be uploaded");
1736
1737 } else if (! $dir = forum_file_area($post)) {
1738 notify("Attachment could not be stored");
1739 $newfile_name = "";
1740
1741 } else {
1742 if (move_uploaded_file($newfile['tmp_name'], "$dir/$newfile_name")) {
9dd0b378 1743 chmod("$dir/$newfile_name", $CFG->directorypermissions);
7f6689e4 1744 forum_delete_old_attachments($post, $newfile_name);
1745 } else {
1746 notify("An error happened while saving the file on the server");
1747 $newfile_name = "";
1748 }
1749 }
1750 } else {
1751 $newfile_name = "";
1752 }
1753
1754 return $newfile_name;
1755}
501cdbd8 1756
11b0c469 1757function forum_add_new_post($post) {
501cdbd8 1758
ffe11640 1759 $post->created = $post->modified = time();
501cdbd8 1760 $post->mailed = "0";
1761
7f6689e4 1762 $newfile = $post->attachment;
1763 $post->attachment = "";
1764
65b0e537 1765 if (! $post->id = insert_record("forum_posts", $post)) {
7f6689e4 1766 return false;
1767 }
1768
1769 if ($post->attachment = forum_add_attachment($post, $newfile)) {
1770 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id);
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);
65b0e537 1776
7f6689e4 1777 return $post->id;
501cdbd8 1778}
1779
11b0c469 1780function forum_update_post($post) {
501cdbd8 1781
ffe11640 1782 $post->modified = time();
501cdbd8 1783
0ab85112 1784 if (!$post->parent) { // Post is a discussion starter - update discussion title too
1785 set_field("forum_discussions", "name", $post->subject, "id", $post->discussion);
1786 }
29507631 1787
7f6689e4 1788 if ($newfilename = forum_add_attachment($post, $post->attachment)) {
1789 $post->attachment = $newfilename;
1790 } else {
1791 unset($post->attachment);
1792 }
29507631 1793
1794 // Update discussion modified date
1795 set_field("forum_discussions", "timemodified", $post->modified, "id", $post->discussion);
016cd6af 1796 set_field("forum_discussions", "usermodified", $post->userid, "id", $post->discussion);
29507631 1797
ffe11640 1798 return update_record("forum_posts", $post);
501cdbd8 1799}
1800
1801function forum_add_discussion($discussion) {
65b0e537 1802// Given an object containing all the necessary data,
501cdbd8 1803// create a new discussion and return the id
1804
1805 GLOBAL $USER;
1806
1807 $timenow = time();
1808
65b0e537 1809 // The first post is stored as a real post, and linked
501cdbd8 1810 // to from the discuss entry.
1811
1812 $post->discussion = 0;
1813 $post->parent = 0;
ebc3bd2b 1814 $post->userid = $USER->id;
501cdbd8 1815 $post->created = $timenow;
1816 $post->modified = $timenow;
1817 $post->mailed = 0;
1818 $post->subject = $discussion->name;
1819 $post->message = $discussion->intro;
7f6689e4 1820 $post->attachment = "";
1821 $post->forum = $discussion->forum;
1822 $post->course = $discussion->course;
9f0b8269 1823 $post->format = $discussion->format;
501cdbd8 1824
1825 if (! $post->id = insert_record("forum_posts", $post) ) {
1826 return 0;
1827 }
1828
7f6689e4 1829 if ($post->attachment = forum_add_attachment($post, $discussion->attachment)) {
1830 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id); //ignore errors
1831 }
1832
65b0e537 1833 // Now do the main entry for the discussion,
02509fe6 1834 // linking to this first post
04eba58f 1835
caadf009 1836 $discussion->firstpost = $post->id;
1837 $discussion->timemodified = $timenow;
016cd6af 1838 $discussion->usermodified = $post->userid;
04eba58f 1839
caadf009 1840 if (! $discussion->id = insert_record("forum_discussions", $discussion) ) {
1841 delete_records("forum_posts", "id", $post->id);
1842 return 0;
04eba58f 1843 }
1844
caadf009 1845 // Finally, set the pointer on the post.
1846 if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) {
1847 delete_records("forum_posts", "id", $post->id);
1848 delete_records("forum_discussions", "id", $discussion->id);
1849 return 0;
04eba58f 1850 }
04eba58f 1851
caadf009 1852 return $discussion->id;
1853}
04eba58f 1854
04eba58f 1855
caadf009 1856function forum_delete_discussion($discussion) {
1857// $discussion is a discussion record object
04eba58f 1858
1859 $result = true;
1860
caadf009 1861 if ($posts = get_records("forum_posts", "discussion", $discussion->id)) {
1862 foreach ($posts as $post) {
1863 $post->course = $discussion->course;
1864 $post->forum = $discussion->forum;
1865 if (! delete_records("forum_ratings", "post", "$post->id")) {
1866 $result = false;
1867 }
1868 if (! forum_delete_post($post)) {
04eba58f 1869 $result = false;
1870 }
1871 }
1872 }
1873
caadf009 1874 if (! delete_records("forum_discussions", "id", "$discussion->id")) {
04eba58f 1875 $result = false;
1876 }
1877
1878 return $result;
1879}
1880
1881
caadf009 1882function forum_delete_post($post) {
1883 if (delete_records("forum_posts", "id", $post->id)) {
1884 delete_records("forum_ratings", "post", $post->id); // Just in case
1885 if ($post->attachment) {
1886 $discussion = get_record("forum_discussions", "id", $post->discussion);
1887 $post->course = $discussion->course;
1888 $post->forum = $discussion->forum;
1889 forum_delete_old_attachments($post);
1890 }
1891 return true;
1892 }
1893 return false;
1894}
501cdbd8 1895
501cdbd8 1896
b656e2a9 1897function forum_print_user_discussions($courseid, $userid, $groupid=0) {
caadf009 1898 global $CFG, $USER;
501cdbd8 1899
b8bf90c5 1900 $maxdiscussions = 10;
1901 $countdiscussions = 0;
1902
65b0e537 1903 $visible = array();
1f48942e 1904
b656e2a9 1905 if ($discussions = forum_get_user_discussions($courseid, $userid, $groupid=0)) {
caadf009 1906 $user = get_record("user", "id", $userid);
b656e2a9 1907 echo "<hr />";
1b26d5e7 1908 $fullname = fullname($user, isteacher($courseid));
1909 print_heading( get_string("discussionsstartedbyrecent", "forum", $fullname) );
caadf009 1910 $replies = forum_count_discussion_replies();
1911 foreach ($discussions as $discussion) {
b8bf90c5 1912 $countdiscussions++;
1913 if ($countdiscussions > $maxdiscussions) {
1914 break;
1915 }
caadf009 1916 if (($discussion->forumtype == "teacher") and !isteacher($courseid)) {
ffe11640 1917 continue;
1918 }
65b0e537 1919 if(!isset($visible[$discussion->forumid])) {
1920 $mod = New stdClass;
1921 $mod->course = $courseid;
1922 $mod->id = $discussion->forumid;
1923 $visible[$discussion->forumid] = instance_is_visible('forum', $mod);
1924 }
1925 if(!$visible[$discussion->forumid] && !isteacheredit($courseid, $USER->id)) {
1926 continue;
1927 }
47f1da80 1928 if (!empty($replies[$discussion->discussion])) {
caadf009 1929 $discussion->replies = $replies[$discussion->discussion]->replies;
1930 } else {
1931 $discussion->replies = 0;
501cdbd8 1932 }
caadf009 1933 $inforum = get_string("inforum", "forum", "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname</A>");
1934 $discussion->subject .= " ($inforum)";
61e96406 1935 if (!empty($USER->id)) {
1936 $ownpost = ($discussion->userid == $USER->id);
1937 } else {
1938 $ownpost = false;
1939 }
caadf009 1940 forum_print_post($discussion, $courseid, $ownpost, $reply=0, $link=1, $assessed=false);
1941 echo "<BR>\n";
501cdbd8 1942 }
1943 }
501cdbd8 1944}
1945
501cdbd8 1946function forum_forcesubscribe($forumid, $value=1) {
1947 return set_field("forum", "forcesubscribe", $value, "id", $forumid);
1948}
1949
1950function forum_is_forcesubscribed($forumid) {
1951 return get_field("forum", "forcesubscribe", "id", $forumid);
1952}
1953
1954function forum_is_subscribed($userid, $forumid) {
1955 if (forum_is_forcesubscribed($forumid)) {
1956 return true;
1957 }
ebc3bd2b 1958 return record_exists("forum_subscriptions", "userid", $userid, "forum", $forumid);
86970225 1959}
1960
501cdbd8 1961function forum_subscribe($userid, $forumid) {
9fa49e22 1962/// Adds user to the subscriber list
1963
6673d7bd 1964 if (record_exists("forum_subscriptions", "userid", $userid, "forum", $forumid)) {
1965 return true;
1966 }
1967
ebc3bd2b 1968 $sub->userid = $userid;
9fa49e22 1969 $sub->forum = $forumid;
501cdbd8 1970
9fa49e22 1971 return insert_record("forum_subscriptions", $sub);
501cdbd8 1972}
1973
1974function forum_unsubscribe($userid, $forumid) {
9fa49e22 1975/// Removes user from the subscriber list
ebc3bd2b 1976 return delete_records("forum_subscriptions", "userid", $userid, "forum", $forumid);
501cdbd8 1977}
1978
0a9f61b5 1979function forum_post_subscription($post) {
1980/// Given a new post, subscribes or unsubscribes as appropriate.
1981/// Returns some text which describes what happened.
1982
1983 global $USER;
1984
1985 if (empty($post->subscribe) and empty($post->unsubscribe)) {
1986 return "";
1987 }
1988
1989 if (!$forum = get_record("forum", "id", $post->forum)) {
1990 return "";
1991 }
1992
1993 $info->name = "$USER->firstname $USER->lastname";
1994 $info->forum = $forum->name;
1995
1996 if (!empty($post->subscribe)) {
1997 forum_subscribe($USER->id, $post->forum);
1998 return "<p>".get_string("nowsubscribed", "forum", $info)."</p>";
1999 }
2000
2001 forum_unsubscribe($USER->id, $post->forum);
2002 return "<p>".get_string("nownotsubscribed", "forum", $info)."</p>";
2003}
2004
501cdbd8 2005
11b0c469 2006function forum_user_has_posted_discussion($forumid, $userid) {
29507631 2007 if ($discussions = forum_get_discussions($forumid, "", $userid)) {
501cdbd8 2008 return true;
2009 } else {
2010 return false;
2011 }
2012}
2013
9197e147 2014function forum_user_can_post_discussion($forum, $currentgroup=false) {
501cdbd8 2015// $forum is an object
2016 global $USER;
2017
2018 if ($forum->type == "eachuser") {
11b0c469 2019 return (! forum_user_has_posted_discussion($forum->id, $USER->id));
501cdbd8 2020 } else if ($forum->type == "teacher") {
2021 return isteacher($forum->course);
9197e147 2022 } else if ($currentgroup) {
d591b40f 2023 return (isteacheredit($forum->course) or (ismember($currentgroup) and $forum->open == 2));
501cdbd8 2024 } else if (isteacher($forum->course)) {
2025 return true;
2026 } else {
70c476a7 2027 return ($forum->open == 2);
501cdbd8 2028 }
2029}
2030
f690562f 2031function forum_user_can_post($forum, $user=NULL) {
2032// $forum, $user are objects
2033
2034 if ($user) {
2035 $isteacher = isteacher($forum->course, $user->id);
2036 } else {
2037 $isteacher = isteacher($forum->course);
2038 }
70c476a7 2039
2040 if ($forum->type == "teacher") {
f690562f 2041 return $isteacher;
2042 } else if ($isteacher) {
70c476a7 2043 return true;
2044 } else {
2045 return $forum->open;
2046 }
2047}
501cdbd8 2048
501cdbd8 2049
65b0e537 2050function forum_print_latest_discussions($forum_id=0, $forum_numdiscussions=5,
2051 $forum_style="plain", $forum_sort="",
9197e147 2052 $currentgroup=0) {
c585fa17 2053 global $CFG, $USER;
65b0e537 2054
f93f848a 2055 if ($forum_id) {
2056 if (! $forum = get_record("forum", "id", $forum_id)) {
2057 error("Forum ID was incorrect");
2058 }
2059 if (! $course = get_record("course", "id", $forum->course)) {
2060 error("Could not find the course this forum belongs to!");
2061 }
2062
2063 if ($course->category) {
2064 require_login($course->id);
2065 }
2066
2067 } else {
2068 if (! $course = get_record("course", "category", 0)) {
2069 error("Could not find a top-level course!");
2070 }
7beb45d8 2071 if (! $forum = forum_get_course_forum($course->id, "news")) {
f93f848a 2072 error("Could not find or create a main forum in this course (id $course->id)");
2073 }
2074 }
2075
9197e147 2076 if (forum_user_can_post_discussion($forum, $currentgroup)) {
b879effb 2077 echo "<p align=center>";
2078 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?forum=$forum->id\">";
0351b1f9 2079 if ($forum->type == "news") {
2080 echo get_string("addanewtopic", "forum")."</a>...";
2081 } else {
2082 echo get_string("addanewdiscussion", "forum")."</a>...";
2083 }
b879effb 2084 echo "</p>\n";
77305fe6 2085 }
2086
65b0e537 2087 if ((!$forum_numdiscussions) && ($forum_style == "plain")) {
3c8a606d 2088 $forum_style = "header"; // Abbreviate display by default
3335f6fb 2089 }
f93f848a 2090
65b0e537 2091 if ($forum_style == "minimal") {
2ab968e9 2092 $forum_sort = "p.modified DESC";
2093 }
2094
2095 $fullpost = false;
65b0e537 2096 if ($forum_style == "plain") {
2ab968e9 2097 $fullpost = true;
2098 }
2099
9197e147 2100 if (! $discussions = forum_get_discussions($forum->id, $forum_sort, 0, $fullpost, $currentgroup) ) {
0351b1f9 2101 if ($forum->type == "news") {
2102 echo "<p align=center><b>(".get_string("nonews", "forum").")</b></p>";
2103 } else {
2104 echo "<p align=center><b>(".get_string("nodiscussions", "forum").")</b></p>";
2105 }
2ab968e9 2106 return;
2107 }
65b0e537 2108
3335f6fb 2109 $replies = forum_count_discussion_replies($forum->id);
f93f848a 2110
3260de67 2111 $canreply = forum_user_can_post($forum);
2112
3335f6fb 2113 $discussioncount = 0;
c20b762a 2114 $olddiscussionlink = false;
2ab968e9 2115 $strdatestring = get_string("strftimedaydatetime");
f93f848a 2116
dcde9f02 2117 if ($forum_style == "minimal") {
2118 $strftimerecent = get_string("strftimerecent");
2119 $strmore = get_string("more", "forum");
2120 }
2121
29507631 2122 if ($forum_style == "header") {
ba1ce4c0 2123 echo "<table width=\"100%\" border=0 cellpadding=3 cellspacing=1 class=\"forumheaderlist\">";
03aa0dfa 2124 echo "<tr class=\"forumpostheader\">";
65b0e537 2125 echo "<th>".get_string("discussion", "forum")."</th>";
29507631 2126 echo "<th colspan=2>".get_string("startedby", "forum")."</th>";
a796d0b8 2127 if ($forum->open or $forum->type == "teacher") {
2128 echo "<th>".get_string("replies", "forum")."</th>";
2129 }
29507631 2130 echo "<th>".get_string("lastpost", "forum")."</th>";
2131 echo "</tr>";
2132 }
2133
3335f6fb 2134 foreach ($discussions as $discussion) {
2135 $discussioncount++;
f93f848a 2136
3335f6fb 2137 if ($forum_numdiscussions && ($discussioncount > $forum_numdiscussions)) {
c20b762a 2138 $olddiscussionlink = true;
3335f6fb 2139 break;
2140 }
9c9f7d77 2141 if (!empty($replies[$discussion->discussion])) {
3335f6fb 2142 $discussion->replies = $replies[$discussion->discussion]->replies;
2143 } else {
2144 $discussion->replies = 0;
2145 }
f7477444 2146 if (!empty($USER->id)) {
2147 $ownpost = ($discussion->userid == $USER->id);
2148 } else {
2149 $ownpost=false;
2150 }
cf84431b 2151 // Use discussion name instead of subject of first post
2152 $discussion->subject = $discussion->name;
2153
3335f6fb 2154 switch ($forum_style) {
2155 case "minimal":
436a7cae 2156 if (!empty($CFG->filterall)) {
2157 $discussion->subject = filter_text($discussion->subject, $forum->course);
2158 }
b656e2a9 2159 echo "<p><span class=\"smallinfohead\">".
2160 userdate($discussion->modified, $strftimerecent).
2161 " - ".
2162 fullname($discussion).
2163 "</span><br />";
00363389 2164 echo "<span class=\"smallinfo\">$discussion->subject ";
b879effb 2165 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->discussion\">";
00363389 2166 echo $strmore."...</a></span>";
b879effb 2167 echo "</p>\n";
3335f6fb 2168 break;
2169 case "header":
a796d0b8 2170 forum_print_discussion_header($discussion, $forum, $strdatestring);
3335f6fb 2171 break;
2172 default:
3260de67 2173 if ($canreply or $discussion->replies) {
2174 $link = true;
2175 } else {
2176 $link = false;
2177 }
2178 forum_print_post($discussion, $forum->course, $ownpost, $reply=0, $link, $assessed=false);
b879effb 2179 echo "<br>\n";
3335f6fb 2180 break;
f93f848a 2181 }
2182 }
29507631 2183
2184 if ($forum_style == "header") {
2185 echo "</table>";
2186 }
c20b762a 2187
2188 if ($olddiscussionlink) {
e2d41062 2189 if ($forum_style == "minimal") {
2190 echo '<p align="center">';
2191 } else {
2192 echo '<p align="right">';
2193 }
2194 echo "<a href=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id&showall=1\">";
c20b762a 2195 echo get_string("olderdiscussions", "forum")."</a> ...</p>";
2196 }
f93f848a 2197}
2198
c6d691dc 2199function forum_print_discussion($course, $forum, $discussion, $post, $mode, $canreply=NULL) {
501cdbd8 2200
2201 global $USER;
2202
61e96406 2203 if (!empty($USER->id)) {
2204 $ownpost = ($USER->id == $post->userid);
2205 } else {
2206 $ownpost = false;
2207 }
c6d691dc 2208 if ($canreply === NULL) {
2209 $reply = forum_user_can_post($forum);
2210 } else {
2211 $reply = $canreply;
2212 }
501cdbd8 2213
02ebf404 2214 $ratings = NULL;
74f5d1e3 2215 $ratingsmenuused = false;
61e96406 2216 if ($forum->assessed and !empty($USER->id)) {
d6bdd9d5 2217 if ($ratings->scale = make_grades_menu($forum->scale)) {
3bd98ad4 2218 $ratings->assesspublic = $forum->assesspublic;
98914efd 2219 $ratings->assesstimestart = $forum->assesstimestart;
2220 $ratings->assesstimefinish = $forum->assesstimefinish;
3bd98ad4 2221 $ratings->allow = ($forum->assessed != 2 or isteacher($course->id));
2222
02ebf404 2223 echo "<form name=form method=post action=rate.php>";
2224 echo "<input type=hidden name=id value=\"$course->id\">";
9d1b97c5 2225 }
2226 }
2227
74f5d1e3 2228 if (forum_print_post($post, $course->id, $ownpost, $reply, $link=false, $ratings)) {
2229 $ratingsmenuused = true;
2230 }
501cdbd8 2231
2232 switch ($mode) {
2e2e71a8 2233 case FORUM_MODE_FLATOLDEST :
2234 case FORUM_MODE_FLATNEWEST :
65b0e537 2235 default:
02ebf404 2236 echo "<ul>";
74f5d1e3 2237 if (forum_print_posts_flat($post->discussion, $course->id, $mode, $ratings, $reply)) {
2238 $ratingsmenuused = true;
2239 }
02ebf404 2240 echo "</ul>";
501cdbd8 2241 break;
2242
2e2e71a8 2243 case FORUM_MODE_THREADED :
74f5d1e3 2244 if (forum_print_posts_threaded($post->id, $course->id, 0, $ratings, $reply)) {
2245 $ratingsmenuused = true;
2246 }
501cdbd8 2247 break;
2248
2e2e71a8 2249 case FORUM_MODE_NESTED :
74f5d1e3 2250 if (forum_print_posts_nested($post->id, $course->id, $ratings, $reply)) {
2251 $ratingsmenuused = true;
2252 }
501cdbd8 2253 break;
2254 }
2255
74f5d1e3 2256 if ($ratingsmenuused) {
02ebf404 2257 echo "<center><input type=\"submit\" value=\"".get_string("sendinratings", "forum")."\">";
fa01ae8b 2258 if ($forum->scale < 0) {
2259 if ($scale = get_record("scale", "id", abs($forum->scale))) {
2260 print_scale_menu_helpbutton($course->id, $scale );
2261 }
2262 }
02ebf404 2263 echo "</center>";
2264 echo "</form>";
501cdbd8 2265 }
2266}
2267
65b0e537 2268function forum_print_posts_flat($discussion, $course, $direction, $ratings, $reply) {
501cdbd8 2269 global $USER;
2270
501cdbd8 2271 $link = false;
74f5d1e3 2272 $ratingsmenuused = false;
501cdbd8 2273
2274 if ($direction < 0) {
2275 $sort = "ORDER BY created DESC";
2276 } else {
2277 $sort = "ORDER BY created ASC";
2278 }
2279
1f48942e 2280 if ($posts = forum_get_discussion_posts($discussion, $sort)) {
501cdbd8 2281 foreach ($posts as $post) {
ebc3bd2b 2282 $ownpost = ($USER->id == $post->userid);
74f5d1e3 2283 if (forum_print_post($post, $course, $ownpost, $reply, $link, $ratings)) {
2284 $ratingsmenuused = true;
2285 }
501cdbd8 2286 }
501cdbd8 2287 }
74f5d1e3 2288
2289 return $ratingsmenuused;
501cdbd8 2290}
2291
65b0e537 2292function forum_print_posts_threaded($parent, $course, $depth, $ratings, $reply) {
501cdbd8 2293 global $USER;
2294
501cdbd8 2295 $link = false;
74f5d1e3 2296 $ratingsmenuused = false;
501cdbd8 2297
1f48942e 2298 if ($posts = forum_get_child_posts($parent)) {
501cdbd8 2299 foreach ($posts as $post) {
2300
f95c2a73 2301 echo "<ul>";
501cdbd8 2302 if ($depth > 0) {
ebc3bd2b 2303 $ownpost = ($USER->id == $post->userid);
74f5d1e3 2304 if (forum_print_post($post, $course, $ownpost, $reply, $link, $ratings)) {
2305 $ratingsmenuused = true;
2306 }
f95c2a73 2307 echo "<br />";
501cdbd8 2308 } else {
1b26d5e7 2309 $by->name = fullname($post, isteacher($course->id));
d62413e8 2310 $by->date = userdate($post->modified);
f95c2a73 2311 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 2312 print_string("bynameondate", "forum", $by);
f95c2a73 2313 echo "</font></p></li>";
501cdbd8 2314 }
2315
74f5d1e3 2316 if (forum_print_posts_threaded($post->id, $course, $depth-1, $ratings, $reply)) {
2317 $ratingsmenuused = true;
2318 }
f95c2a73 2319 echo "</ul>\n";
501cdbd8 2320 }
501cdbd8 2321 }
74f5d1e3 2322 return $ratingsmenuused;
501cdbd8 2323}
2324
65b0e537 2325function forum_print_posts_nested($parent, $course, $ratings, $reply) {
501cdbd8 2326 global $USER;
2327
501cdbd8 2328 $link = false;
74f5d1e3 2329 $ratingsmenuused = false;
501cdbd8 2330
1f48942e 2331 if ($posts = forum_get_child_posts($parent)) {
501cdbd8 2332 foreach ($posts as $post) {
2333
61e96406 2334 if (empty($USER->id)) {
2335 $ownpost = false;
2336 } else {
2337 $ownpost = ($USER->id == $post->userid);
2338 }
501cdbd8 2339
74f5d1e3 2340 echo "<ul>";
2341 if (forum_print_post($post, $course, $ownpost, $reply, $link, $ratings)) {
2342 $ratingsmenuused = true;
2343 }
2344 echo "<br />";
2345 if (forum_print_posts_nested($post->id, $course, $ratings, $reply)) {
2346 $ratingsmenuused = true;
2347 }
2348 echo "</ul>\n";
501cdbd8 2349 }
501cdbd8 2350 }
74f5d1e3 2351 return $ratingsmenuused;
501cdbd8 2352}
2353
a57a7794 2354function forum_get_recent_mod_activity(&$activities, &$index, $sincetime, $courseid, $forum="0", $user="", $groupid="") {
2355// Returns all forum posts since a given time. If forum is specified then
2356// this restricts the results
2357
2358 global $CFG;
2359
2360 if ($forum) {
2361 $forumselect = " AND cm.id = '$forum'";
2362 } else {
2363 $forumselect = "";
2364 }
2365
2366 if ($user) {
2367 $userselect = " AND u.id = '$user'";
2368 } else {
2369 $userselect = "";
2370 }
2371
a57a7794 2372 $posts = get_records_sql("SELECT p.*, d.name, u.firstname, u.lastname,
2373 u.picture, d.groupid, cm.instance, f.name, cm.section
2374 FROM {$CFG->prefix}forum_posts p,
2375 {$CFG->prefix}forum_discussions d,
2376 {$CFG->prefix}user u,
2377 {$CFG->prefix}course_modules cm,
2378 {$CFG->prefix}forum f
2379 WHERE p.modified > '$sincetime' $forumselect
2380 AND p.userid = u.id $userselect
2381 AND d.course = '$courseid'
2382 AND p.discussion = d.id $groupselect
2383 AND cm.instance = f.id
2384 AND cm.course = d.course
2385 AND cm.course = f.course
2386 AND f.id = d.forum
2387 ORDER BY d.id");
2388
90708fc1 2389 if (empty($posts))
2390 return;
a57a7794 2391
90708fc1 2392 foreach ($posts as $post) {
a57a7794 2393
90708fc1 2394 if (empty($groupid) || ismember($groupid, $post->userid)) {
2395 $tmpactivity->type = "forum";
2396 $tmpactivity->defaultindex = $index;
2397 $tmpactivity->instance = $post->instance;
2398 $tmpactivity->name = $post->name;
2399 $tmpactivity->section = $post->section;
a57a7794 2400
90708fc1 2401 $tmpactivity->content->id = $post->id;
2402 $tmpactivity->content->discussion = $post->discussion;
2403 $tmpactivity->content->subject = $post->subject;
2404 $tmpactivity->content->parent = $post->parent;
65b0e537 2405
90708fc1 2406 $tmpactivity->user->userid = $post->userid;
2407 $tmpactivity->user->fullname = fullname($post);
2408 $tmpactivity->user->picture = $post->picture;
a57a7794 2409
90708fc1 2410 $tmpactivity->timestamp = $post->modified;
2411 $activities[] = $tmpactivity;
65b0e537 2412
90708fc1 2413 $index++;
2414 }
2415 }
a57a7794 2416
90708fc1 2417 return;
a57a7794 2418}
2419
2420function forum_print_recent_mod_activity($activity, $course, $detail=false) {
2421
2422 global $CFG;
2423
2424 echo '<table border="0" cellpadding="3" cellspacing="0">';
2425
2426 if ($activity->content->parent) {
2427 $openformat = "<font size=\"2\"><i>";
2428 $closeformat = "</i></font>";
2429 } else {
2430 $openformat = "<b>";
2431 $closeformat = "</b>";
2432 }
2433
2434 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
2435 print_user_picture($activity->user->userid, $course, $activity->user->picture);
2436 echo "</td><td>$openformat";
2437
2438 if ($detail) {
2439 echo "<img src=\"$CFG->modpixpath/$activity->type/icon.gif\" ".
2440 "height=16 width=16 alt=\"$activity->name\"> ";
2441 }
2442 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=" . $activity->content->discussion
2443 . "#" . $activity->content->id . "\">";
2444
2445 echo $activity->content->subject;
2446 echo "</a>$closeformat";
2447
2448 echo "<br><font size=\"2\">";
2449 echo "<a href=\"$CFG->wwwroot/user/view.php?id=" . $activity->user->userid . "&course=" . "$course\">"
2450 . $activity->user->fullname . "</a>";
2451 echo " - " . userdate($activity->timestamp) . "</font></td></tr>";
2452 echo "</table>";
2453
2454 return;
2455}
2456
cf84431b 2457function forum_change_discussionid($postid, $discussionid) {
2458/// recursively sets the discussion field to $discussionid on $postid and all its children
2459/// used when pruning a post
2460 set_field('forum_posts', 'discussion', $discussionid, 'id', $postid);
2461 if ($posts = get_records('forum_posts', 'parent', $postid)) {
2462 foreach ($posts as $post) {
2463 forum_change_discussionid($post->id, $discussionid);
2464 }
2465 }
2466 return true;
2467}
2468
f93f848a 2469?>