first translation
[moodle.git] / mod / forum / lib.php
CommitLineData
41905731 1<?php // $Id$
f93f848a 2
f1e0649c 3require_once($CFG->libdir.'/filelib.php');
7f6689e4 4
501cdbd8 5/// CONSTANTS ///////////////////////////////////////////////////////////
f93f848a 6
d3583b41 7define('FORUM_MODE_FLATOLDEST', 1);
8define('FORUM_MODE_FLATNEWEST', -1);
9define('FORUM_MODE_THREADED', 2);
10define('FORUM_MODE_NESTED', 3);
2e2e71a8 11
d3583b41 12define('FORUM_FORCESUBSCRIBE', 1);
13define('FORUM_INITIALSUBSCRIBE', 2);
709f0ec8 14
d3583b41 15$FORUM_LAYOUT_MODES = array ( FORUM_MODE_FLATOLDEST => get_string('modeflatoldestfirst', 'forum'),
16 FORUM_MODE_FLATNEWEST => get_string('modeflatnewestfirst', 'forum'),
17 FORUM_MODE_THREADED => get_string('modethreaded', 'forum'),
18 FORUM_MODE_NESTED => get_string('modenested', 'forum') );
f93f848a 19
11b0c469 20// These are course content forums that can be added to the course manually
d3583b41 21$FORUM_TYPES = array ('general' => get_string('generalforum', 'forum'),
22 'eachuser' => get_string('eachuserforum', 'forum'),
23 'single' => get_string('singleforum', 'forum') );
f93f848a 24
d3583b41 25$FORUM_OPEN_MODES = array ('2' => get_string('openmode2', 'forum'),
26 '1' => get_string('openmode1', 'forum'),
27 '0' => get_string('openmode0', 'forum') );
70c476a7 28
5be7800c 29if (!isset($CFG->forum_displaymode)) {
d3583b41 30 set_config('forum_displaymode', FORUM_MODE_NESTED);
65b0e537 31}
73bb0835 32
5be7800c 33if (!isset($CFG->forum_shortpost)) {
d3583b41 34 set_config('forum_shortpost', 300); // Less non-HTML characters than this is short
65b0e537 35}
4d871a72 36
5be7800c 37if (!isset($CFG->forum_longpost)) {
d3583b41 38 set_config('forum_longpost', 600); // More non-HTML characters than this is long
65b0e537 39}
4d871a72 40
5be7800c 41if (!isset($CFG->forum_manydiscussions)) {
d3583b41 42 set_config('forum_manydiscussions', 100); // Number of discussions on a page
65b0e537 43}
e07635f4 44
4909e176 45if (!isset($CFG->forum_maxbytes)) {
d3583b41 46 set_config('forum_maxbytes', 512000); // Default maximum size for all forums
65b0e537 47}
4909e176 48
f37da850 49if (!isset($CFG->forum_trackreadposts)) {
d3583b41 50 set_config('forum_trackreadposts', true); // Default whether user needs to mark a post as read
f37da850 51}
52
53if (!isset($CFG->forum_oldpostdays)) {
81c679ce 54 set_config('forum_oldpostdays', 14); // Default number of days that a post is considered old
f37da850 55}
56
57if (!isset($CFG->forum_usermarksread)) {
d3583b41 58 set_config('forum_usermarksread', false); // Default whether user needs to mark a post as read
f37da850 59}
60
61if (!isset($CFG->forum_cleanreadtime)) {
d3583b41 62 set_config('forum_cleanreadtime', 2); // Default time (hour) to execute 'clean_read_records' cron
f37da850 63}
64
1246ea56 65if (!isset($CFG->forum_replytouser)) {
d3583b41 66 set_config('forum_replytouser', true); // Default maximum size for all forums
1246ea56 67}
68
27888a20 69if (empty($USER->id) or isguest()) {
70 $CFG->forum_trackreadposts = false; // This feature never works when a user isn't logged in
71}
4909e176 72
e07635f4 73
caadf009 74/// STANDARD FUNCTIONS ///////////////////////////////////////////////////////////
75
76function forum_add_instance($forum) {
65b0e537 77// Given an object containing all the necessary data,
78// (defined by the form in mod.html) this function
79// will create a new instance and return the id number
caadf009 80// of the new instance.
81
82 global $CFG;
83
84 $forum->timemodified = time();
85
f2f56406 86 if (!$forum->userating) {
87 $forum->assessed = 0;
88 }
89
98914efd 90 if (!empty($forum->ratingtime)) {
65b0e537 91 $forum->assesstimestart = make_timestamp($forum->startyear, $forum->startmonth, $forum->startday,
98914efd 92 $forum->starthour, $forum->startminute, 0);
65b0e537 93 $forum->assesstimefinish = make_timestamp($forum->finishyear, $forum->finishmonth, $forum->finishday,
98914efd 94 $forum->finishhour, $forum->finishminute, 0);
95 } else {
96 $forum->assesstimestart = 0;
97 $forum->assesstimefinish = 0;
98 }
caadf009 99
d3583b41 100 if (! $forum->id = insert_record('forum', $forum)) {
cb9a975f 101 return false;
102 }
103
d3583b41 104 if ($forum->type == 'single') { // Create related discussion.
caadf009 105 $discussion->course = $forum->course;
106 $discussion->forum = $forum->id;
107 $discussion->name = $forum->name;
108 $discussion->intro = $forum->intro;
109 $discussion->assessed = $forum->assessed;
110
111 if (! forum_add_discussion($discussion)) {
d3583b41 112 error('Could not add the discussion for this forum');
caadf009 113 }
114 }
8f0cd6ef 115
709f0ec8 116 if ($forum->forcesubscribe == FORUM_INITIALSUBSCRIBE) { // all users should be subscribed initially
117 $users = get_course_users($forum->course);
118 foreach ($users as $user) {
119 forum_subscribe($user->id, $forum->id);
120 }
121 }
caadf009 122
123 return $forum->id;
124}
125
126
127function forum_update_instance($forum) {
65b0e537 128// Given an object containing all the necessary data,
129// (defined by the form in mod.html) this function
caadf009 130// will update an existing instance with new data.
131
132 $forum->timemodified = time();
133 $forum->id = $forum->instance;
134
f2f56406 135 if (!$forum->userating) {
136 $forum->assessed = 0;
137 }
138
98914efd 139 if (!empty($forum->ratingtime)) {
65b0e537 140 $forum->assesstimestart = make_timestamp($forum->startyear, $forum->startmonth, $forum->startday,
98914efd 141 $forum->starthour, $forum->startminute, 0);
65b0e537 142 $forum->assesstimefinish = make_timestamp($forum->finishyear, $forum->finishmonth, $forum->finishday,
98914efd 143 $forum->finishhour, $forum->finishminute, 0);
144 } else {
145 $forum->assesstimestart = 0;
146 $forum->assesstimefinish = 0;
147 }
148
d3583b41 149 if ($forum->type == 'single') { // Update related discussion and post.
150 if (! $discussion = get_record('forum_discussions', 'forum', $forum->id)) {
151 if ($discussions = get_records('forum_discussions', 'forum', $forum->id, 'timemodified ASC')) {
152 notify('Warning! There is more than one discussion in this forum - using the most recent');
caadf009 153 $discussion = array_pop($discussions);
154 } else {
d3583b41 155 error('Could not find the discussion in this forum');
caadf009 156 }
157 }
d3583b41 158 if (! $post = get_record('forum_posts', 'id', $discussion->firstpost)) {
159 error('Could not find the first post in this forum discussion');
caadf009 160 }
161
162 $post->subject = $forum->name;
163 $post->message = $forum->intro;
164 $post->modified = $forum->timemodified;
165
d3583b41 166 if (! update_record('forum_posts', $post)) {
167 error('Could not update the first post');
caadf009 168 }
169
170 $discussion->name = $forum->name;
171
d3583b41 172 if (! update_record('forum_discussions', $discussion)) {
173 error('Could not update the discussion');
caadf009 174 }
175 }
176
d3583b41 177 return update_record('forum', $forum);
caadf009 178}
179
180
181function forum_delete_instance($id) {
65b0e537 182// Given an ID of an instance of this module,
183// this function will permanently delete the instance
184// and any data that depends on it.
caadf009 185
d3583b41 186 if (! $forum = get_record('forum', 'id', $id)) {
caadf009 187 return false;
188 }
189
190 $result = true;
191
d3583b41 192 if ($discussions = get_records('forum_discussions', 'forum', $forum->id)) {
caadf009 193 foreach ($discussions as $discussion) {
194 if (! forum_delete_discussion($discussion)) {
195 $result = false;
196 }
197 }
198 }
199
d3583b41 200 if (! delete_records('forum_subscriptions', 'forum', $forum->id)) {
caadf009 201 $result = false;
202 }
203
f37da850 204 forum_tp_delete_read_records(-1, -1, -1, $forum->id);
205
d3583b41 206 if (! delete_records('forum', 'id', $forum->id)) {
caadf009 207 $result = false;
208 }
209
210 return $result;
211}
212
213
214function forum_cron () {
edffca15 215/// Function to be run periodically according to the moodle cron
216/// Finds all posts that have yet to be mailed out, and mails them
217/// out to all subscribers
caadf009 218
d30867b0 219 global $CFG, $USER;
aaf7a9dc 220 static $strforums = NULL;
857b798b 221
222 if ($strforums === NULL) {
aaf7a9dc 223 $strforums = get_string('forums', 'forum');
224 }
225
1066e0dc 226 if (!empty($USER->id)) { // Remember real USER account if necessary
ec2137ba 227 $realuser = $USER;
228 }
229
3ecca1ee 230 /// Posts older than 2 days will not be mailed. This is to avoid the problem where
231 /// cron has not been running for a long time, and then suddenly people are flooded
232 /// with mail from the past few weeks or months
caadf009 233
3ecca1ee 234 $timenow = time();
235 $endtime = $timenow - $CFG->maxeditingtime;
38ad519c 236 $starttime = $endtime - 48 * 3600; /// Two days earlier
3ecca1ee 237
238 if ($posts = forum_get_unmailed_posts($starttime, $endtime)) {
caadf009 239
65b0e537 240 /// Mark them all now as being mailed. It's unlikely but possible there
241 /// might be an error later so that a post is NOT actually mailed out,
edffca15 242 /// but since mail isn't crucial, we can accept this risk. Doing it now
243 /// prevents the risk of duplicated mails, which is a worse problem.
244
3ecca1ee 245 if (!forum_mark_old_posts_as_mailed($endtime)) {
d3583b41 246 mtrace('Errors occurred while trying to mark some posts as being mailed.');
3ecca1ee 247 return false; // Don't continue trying to mail them, in case we are in a cron loop
edffca15 248 }
249
ca8e8a10 250 @set_time_limit(0); /// so that script does not get timed out when posting to many users
aaf7a9dc 251
857b798b 252 $urlinfo = parse_url($CFG->wwwroot);
253 $hostname = $urlinfo['host'];
254
caadf009 255 foreach ($posts as $post) {
256
d3583b41 257 mtrace(get_string('processingpost', 'forum', $post->id), '');
caadf009 258
d3583b41 259 if (! $userfrom = get_record('user', 'id', $post->userid)) {
260 mtrace('Could not find user '.$post->userid);
caadf009 261 continue;
262 }
263
857b798b 264
265 $userfrom->customheaders = array ( // Headers to make emails easier to track
266 "Precedence: Bulk",
267 "List-id: <moodlecourse{$post->course}@$hostname>",
268 "Message-Id: <moodlepost{$post->id}@$hostname>",
269 "In-Reply-To: <moodlepost{$post->parent}@$hostname>",
8f0cd6ef 270 "References: <moodlepost{$post->parent}@$hostname>"
857b798b 271 );
272
49d9b738 273
caadf009 274 if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) {
b140ae85 275 mtrace("Could not find discussion $post->discussion");
caadf009 276 continue;
277 }
278
279 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
b140ae85 280 mtrace("Could not find forum $discussion->forum");
caadf009 281 continue;
282 }
283
284 if (! $course = get_record("course", "id", "$forum->course")) {
b140ae85 285 mtrace("Could not find course $forum->course");
caadf009 286 continue;
287 }
288
e1fb2e6e 289 if (!empty($course->lang)) {
290 $CFG->courselang = $course->lang;
291 } else {
292 unset($CFG->courselang);
293 }
9c84314e 294
9197e147 295 $groupmode = false;
296 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
2862b309 297 if ($groupmode = groupmode($course, $cm) and $discussion->groupid > 0) { // Groups are being used
2f9c977e 298 if (!$group = get_record("groups", "id", $discussion->groupid)) { // Can't find group
9197e147 299 continue; // Be safe and don't send it to anyone
300 }
301 }
69d79bc3 302 } else {
303 $cm->id = 0;
304 }
9197e147 305
caadf009 306 if ($users = forum_subscribed_users($course, $forum)) {
caadf009 307
308 $mailcount=0;
3d94772d 309 $errorcount=0;
caadf009 310 foreach ($users as $userto) {
9197e147 311 if ($groupmode) { // Look for a reason not to send this email
312 if (!isteacheredit($course->id, $userto->id)) {
2862b309 313 if (!empty($group->id)) {
314 if (!ismember($group->id, $userto->id)) {
315 continue;
316 }
9197e147 317 }
318 }
319 }
65b0e537 320
aaf7a9dc 321 if ($userto->maildigest > 0) {
322 // This user wants the mails to be in digest form
323 $queue = New stdClass;
324 $queue->userid = $userto->id;
325 $queue->discussionid = $discussion->id;
326 $queue->postid = $post->id;
327 if(!insert_record('forum_queue', $queue)) {
b140ae85 328 mtrace("Error: mod/forum/cron.php: Could not queue for digest mail for id $post->id to user $userto->id ($userto->email) .. not trying again.");
aaf7a9dc 329 }
330 continue;
331 }
65b0e537 332
333 /// Override the language and timezone of the "current" user, so that
ec2137ba 334 /// mail is customised for the receiver.
335 $USER->lang = $userto->lang;
336 $USER->timezone = $userto->timezone;
337
17dc3f3c 338 $postsubject = "$course->shortname: ".format_string($post->subject,true);
aaf7a9dc 339 $posttext = forum_make_mail_text($course, $forum, $discussion, $post, $userfrom, $userto);
340 $posthtml = forum_make_mail_html($course, $forum, $discussion, $post, $userfrom, $userto);
caadf009 341
8f0cd6ef 342 if (!$mailresult = email_to_user($userto, $userfrom, $postsubject, $posttext,
50a26903 343 $posthtml, '', '', $CFG->forum_replytouser)) {
b140ae85 344 mtrace("Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id".
345 " ($userto->email) .. not trying again.");
8f0cd6ef 346 add_to_log($course->id, 'forum', 'mail error', "discuss.php?d=$discussion->id#$post->id",
17dc3f3c 347 substr(format_string($post->subject,true),0,30), $cm->id, $userto->id);
aaf7a9dc 348 $errorcount++;
b6268a0e 349 } else if ($mailresult === 'emailstop') {
8f0cd6ef 350 add_to_log($course->id, 'forum', 'mail blocked', "discuss.php?d=$discussion->id#$post->id",
17dc3f3c 351 substr(format_string($post->subject,true),0,30), $cm->id, $userto->id);
caadf009 352 } else {
aaf7a9dc 353 $mailcount++;
f37da850 354
355 /// Mark post as read if forum_usermarksread is set off
356 if ($CFG->forum_trackreadposts && !$CFG->forum_usermarksread) {
357 if (!forum_tp_mark_post_read($userto->id, $post, $forum->id)) {
358 mtrace("Error: mod/forum/cron.php: Could not mark post $post->id read for user $userto->id".
359 " while sending email.");
360 }
361 }
caadf009 362 }
aaf7a9dc 363 }
364
b140ae85 365 mtrace(".... mailed to $mailcount users.");
aaf7a9dc 366 if ($errorcount) {
367 set_field("forum_posts", "mailed", "2", "id", "$post->id");
368 }
369 }
370 }
371 }
372
6e6a64b3 373 unset($CFG->courselang);
374
944a2b28 375 if (!empty($realuser)) { // Restore real USER timezone if necessary
376 $sitetimezone = $realuser->timezone;
b140ae85 377 $USER->lang = $realuser->lang;
944a2b28 378 } else {
379 $sitetimezone = $CFG->timezone;
b140ae85 380 $USER->lang = $CFG->lang;
944a2b28 381 }
382
aaf7a9dc 383 /// Now see if there are any digest mails waiting to be sent, and if we should send them
aaf7a9dc 384
8f0cd6ef 385 if (!isset($CFG->digestmailtimelast)) { // To catch the first time
ca8e8a10 386 set_config('digestmailtimelast', 0);
387 }
388
389 $timenow = time();
944a2b28 390 $digesttime = usergetmidnight($timenow, $sitetimezone) + ($CFG->digestmailtime * 3600);
ca8e8a10 391
392 if ($CFG->digestmailtimelast < $digesttime and $timenow > $digesttime) {
b140ae85 393
ca8e8a10 394 set_config('digestmailtimelast', $timenow);
aaf7a9dc 395
b140ae85 396 mtrace('Sending forum digests: '.userdate($timenow, '', $sitetimezone));
397
aaf7a9dc 398 $digestposts = get_records('forum_queue');
399 if(!empty($digestposts)) {
400
401 // We have work to do
402 $usermailcount = 0;
403 $site = get_site();
404
405 $users = array();
406 $posts = array();
407 $discussions = array();
408 $discussionposts = array();
409 $userdiscussions = array();
410 foreach($digestposts as $digestpost) {
411 if(!isset($users[$digestpost->userid])) {
412 $users[$digestpost->userid] = get_record('user', 'id', $digestpost->userid);
413 }
414 if(!isset($discussions[$digestpost->discussionid])) {
415 $discussions[$digestpost->discussionid] = get_record('forum_discussions', 'id', $digestpost->discussionid);
416 }
417 if(!isset($posts[$digestpost->postid])) {
418 $posts[$digestpost->postid] = get_record('forum_posts', 'id', $digestpost->postid);
419 }
420 $userdiscussions[$digestpost->userid][$digestpost->discussionid] = $digestpost->discussionid;
421 $discussionposts[$digestpost->discussionid][$digestpost->postid] = $digestpost->postid;
422 }
423
424 // Data collected, start sending out emails to each user
425
426 foreach($userdiscussions as $userid => $thesediscussions) {
427
b140ae85 428 mtrace(get_string('processingdigest', 'forum', $userid),'... ');
aaf7a9dc 429
430 // First of all delete all the queue entries for this user
431 delete_records('forum_queue', 'userid', $userid);
432 $userto = $users[$userid];
433
434 /// Override the language and timezone of the "current" user, so that
435 /// mail is customised for the receiver.
a0330747 436 $USER->lang = $userto->lang;
437 $USER->timezone = $userto->timezone;
aaf7a9dc 438
439
440 $postsubject = get_string('digestmailsubject', 'forum', $site->shortname);
441 $headerdata = New stdClass;
442 $headerdata->sitename = $site->fullname;
839f2456 443 $headerdata->userprefs = $CFG->wwwroot.'/user/edit.php?id='.$userid.'&amp;course='.$site->id;
aaf7a9dc 444
445 $posttext = get_string('digestmailheader', 'forum', $headerdata)."\n\n";
446 $headerdata->userprefs = '<a target="_blank" href="'.$headerdata->userprefs.'">'.get_string('digestmailprefs', 'forum').'</a>';
9c674431 447
78c0d909 448 $posthtml = "<head>";
449 foreach ($CFG->stylesheets as $stylesheet) {
450 $posthtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
451 }
452 $posthtml .= "</head>\n<body>\n";
a0330747 453 $posthtml .= '<p>'.get_string('digestmailheader', 'forum', $headerdata).'</p><br /><hr size="1" noshade="noshade" />';
aaf7a9dc 454
455 foreach($thesediscussions as $discussionid) {
456 $discussion = $discussions[$discussionid];
457 if(empty($discussion)) {
b140ae85 458 mtrace("Error: Could not find discussion $discussionid");
aaf7a9dc 459 continue;
caadf009 460 }
aaf7a9dc 461
462 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
b140ae85 463 mtrace("Could not find forum $discussion->forum");
aaf7a9dc 464 continue;
f690562f 465 }
aaf7a9dc 466 if (! $course = get_record("course", "id", "$forum->course")) {
b140ae85 467 mtrace("Could not find course $forum->course");
aaf7a9dc 468 continue;
caadf009 469 }
65b0e537 470
aaf7a9dc 471 $canunsubscribe = ! forum_is_forcesubscribed($forum->id);
472 $canreply = forum_user_can_post($forum, $userto);
caadf009 473
caadf009 474
aaf7a9dc 475 $posttext .= "\n \n";
476 $posttext .= '=====================================================================';
477 $posttext .= "\n \n";
3849dae8 478 $posttext .= "$course->shortname -> $strforums -> ".format_string($forum->name,true);
aaf7a9dc 479 if ($discussion->name != $forum->name) {
c78ac798 480 $posttext .= " -> ".format_string($discussion->name,true);
caadf009 481 }
aaf7a9dc 482 $posttext .= "\n";
65b0e537 483
aaf7a9dc 484 $posthtml .= "<p><font face=\"sans-serif\">".
485 "<a target=\"_blank\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ".
486 "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</a> -> ".
3849dae8 487 "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
aaf7a9dc 488 if ($discussion->name == $forum->name) {
489 $posthtml .= "</font></p>";
caadf009 490 } else {
c78ac798 491 $posthtml .= " -> <a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a></font></p>";
caadf009 492 }
aaf7a9dc 493 $posthtml .= '<p>';
494
e1c6dde1 495 $postsarray = $discussionposts[$discussionid];
496 sort($postsarray);
497
f37da850 498 /// Create an empty array to use for marking read posts.
499 /// (I'm sure there's already a structure I can use here, but I can't be sure.)
500 $markread = array();
501
857b798b 502 foreach ($postsarray as $postid) {
aaf7a9dc 503 if (! $post = get_record("forum_posts", "id", "$postid")) {
b140ae85 504 mtrace("Error: Could not find post $postid");
aaf7a9dc 505 continue;
506 }
507 if (! $userfrom = get_record("user", "id", "$post->userid")) {
b140ae85 508 mtrace("Error: Could not find user $post->userid");
aaf7a9dc 509 continue;
510 }
511
857b798b 512 $userfrom->customheaders = array ("Precedence: Bulk");
513
514 if ($userto->maildigest == 2) {
aaf7a9dc 515 // Subjects only
516 $by = New stdClass;
517 $by->name = fullname($userfrom);
518 $by->date = userdate($post->modified);
17dc3f3c 519 $posttext .= "\n".format_string($post->subject,true).' '.get_string("bynameondate", "forum", $by);
aaf7a9dc 520 $posttext .= "\n---------------------------------------------------------------------";
521
839f2456 522 $by->name = "<a target=\"_blank\" href=\"$CFG->wwwroot/user/view.php?id=$userfrom->id&amp;course=$course->id\">$by->name</a>";
17dc3f3c 523 $posthtml .= '<div><a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id.'#'.$post->id.'">'.format_string($post->subject,true).'</a> '.get_string("bynameondate", "forum", $by).'</div>';
857b798b 524
525 } else {
aaf7a9dc 526 // The full treatment
527 $posttext .= forum_make_mail_text($course, $forum, $discussion, $post, $userfrom, $userto, true);
add3201e 528 $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, $canreply, true, false);
f37da850 529
530 /// Create an array of postid's for this user to mark as read.
531 if ($CFG->forum_trackreadposts && !$CFG->forum_usermarksread) {
532 $markread[$post->id]->post = $post;
533 $markread[$post->id]->forumid = $forum->id;
534 }
aaf7a9dc 535 }
536 }
537 if ($canunsubscribe) {
538 $posthtml .= "\n<div align=\"right\"><font size=\"1\"><a href=\"$CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\">".get_string("unsubscribe", "forum")."</a></font></div>";
857b798b 539 } else {
aaf7a9dc 540 $posthtml .= "\n<div align=\"right\"><font size=\"1\">".get_string("everyoneissubscribed", "forum")."</font></div>";
541 }
542 $posthtml .= '<hr size="1" noshade="noshade" /></p>';
caadf009 543 }
a0330747 544 $posthtml .= '</body>';
caadf009 545
379a42cb 546 if($userto->mailformat != 1) {
547 // This user DOESN'T want to receive HTML
548 $posthtml = '';
549 }
550
8f0cd6ef 551 if (!$mailresult = email_to_user($userto, $site->shortname, $postsubject, $posttext, $posthtml,
50a26903 552 '', '', $CFG->forum_replytouser)) {
b140ae85 553 mtrace("ERROR!");
aaf7a9dc 554 echo "Error: mod/forum/cron.php: Could not send out digest mail to user $userto->id ($userto->email)... not trying again.\n";
555 add_to_log($course->id, 'forum', 'mail digest error', '', '', $cm->id, $userto->id);
b6268a0e 556 } else if ($mailresult === 'emailstop') {
50a26903 557 add_to_log($course->id, 'forum', 'mail digest blocked', '', '', $cm->id, $userto->id);
aaf7a9dc 558 } else {
b140ae85 559 mtrace("success.");
aaf7a9dc 560 $usermailcount++;
e3ff14ca 561
f37da850 562 /// Mark post as read if forum_usermarksread is set off
563 if ($CFG->forum_trackreadposts && !$CFG->forum_usermarksread) {
564 foreach ($markread as $postinfo) {
565 if (!forum_tp_mark_post_read($userto->id, $postinfo->post, $postinfo->forumid)) {
566 mtrace("Error: mod/forum/cron.php: Could not mark post $postid read for user $userto->id".
567 " while sending digest email.");
568 }
569 }
570 }
3d94772d 571 }
aaf7a9dc 572
caadf009 573 }
aaf7a9dc 574
caadf009 575 }
576 }
577
d658167f 578 if(!empty($usermailcount)) {
b140ae85 579 mtrace(get_string('digestsentusers', 'forum', $usermailcount));
aaf7a9dc 580 }
581
ec2137ba 582 if (!empty($realuser)) { // Restore real USER if necessary
583 $USER = $realuser;
584 }
585
f37da850 586 if (($lastreadclean = get_record("config", "name", "forum_lastreadclean"))) {
587 $cleantime = mktime($CFG->forum_cleanreadtime, 0);
588 $timenow = time();
589 if (($timenow > $cleantime) && ($lastreadclean->value+(24*3600) < $timenow)) {
590 $lastreadclean->value = $timenow;
591 update_record("config", $lastreadclean);
592 forum_tp_clean_read_records();
593 }
594 } else {
595 $lastreadclean->name = 'forum_lastreadclean';
596 $lastreadclean->value = time();
597 insert_record("config", $lastreadclean);
598 }
599
600
caadf009 601 return true;
602}
603
aaf7a9dc 604function forum_make_mail_text($course, $forum, $discussion, $post, $userfrom, $userto, $bare = false) {
605 global $CFG;
606
aaf7a9dc 607 $by = New stdClass;
608 $by->name = fullname($userfrom, isteacher($course->id, $userto->id));
609 $by->date = userdate($post->modified, "", $userto->timezone);
610
611 $strbynameondate = get_string('bynameondate', 'forum', $by);
612
64762ddc 613 $strforums = get_string('forums', 'forum');
614
aaf7a9dc 615 $canunsubscribe = ! forum_is_forcesubscribed($forum->id);
616 $canreply = forum_user_can_post($forum, $userto);
617
618 $posttext = '';
619
620 if(!$bare) {
3849dae8 621 $posttext = "$course->shortname -> $strforums -> ".format_string($forum->name,true);
aaf7a9dc 622
623 if ($discussion->name != $forum->name) {
c78ac798 624 $posttext .= " -> ".format_string($discussion->name,true);
aaf7a9dc 625 }
626 }
627
628 $posttext .= "\n---------------------------------------------------------------------\n";
17dc3f3c 629 $posttext .= format_string($post->subject,true);
aaf7a9dc 630 if($bare) {
631 $posttext .= " ($CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id#$post->id)";
632 }
633 $posttext .= "\n".$strbynameondate."\n";
634 $posttext .= "---------------------------------------------------------------------\n";
635 $posttext .= format_text_email($post->message, $post->format);
636 $posttext .= "\n\n";
637 if ($post->attachment) {
638 $post->course = $course->id;
639 $post->forum = $forum->id;
640 $posttext .= forum_print_attachments($post, "text");
641 }
642 if (!$bare && $canreply) {
643 $posttext .= "---------------------------------------------------------------------\n";
644 $posttext .= get_string("postmailinfo", "forum", $course->shortname)."\n";
645 $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id\n";
646 }
647 if (!$bare && $canunsubscribe) {
648 $posttext .= "\n---------------------------------------------------------------------\n";
649 $posttext .= get_string("unsubscribe", "forum");
650 $posttext .= ": $CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\n";
651 }
652
653 return $posttext;
654}
655
656function forum_make_mail_html($course, $forum, $discussion, $post, $userfrom, $userto) {
657 global $CFG;
658
aaf7a9dc 659 if ($userto->mailformat == 1) { // HTML
660
64762ddc 661 $strforums = get_string('forums', 'forum');
aaf7a9dc 662 $canreply = forum_user_can_post($forum, $userto);
663 $canunsubscribe = ! forum_is_forcesubscribed($forum->id);
664
9c674431 665 $posthtml = '';
78c0d909 666 $posthtml .= '<head>';
667 foreach ($CFG->stylesheets as $stylesheet) {
668 $posthtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
669 }
670 $posthtml .= '</head>';
671 $posthtml .= "\n<body>\n\n";
9c674431 672
673 $posthtml .= "<p><font face=\"sans-serif\">".
674 "<a target=\"_blank\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> &raquo; ".
675 "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</a> &raquo; ".
3849dae8 676 "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
aaf7a9dc 677 if ($discussion->name == $forum->name) {
678 $posthtml .= "</font></p>";
679 } else {
c78ac798 680 $posthtml .= " &raquo; <a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a></font></p>";
aaf7a9dc 681 }
add3201e 682 $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, $canreply, true, false);
aaf7a9dc 683
684 if ($canunsubscribe) {
685 $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>";
686 }
687
688 return $posthtml;
689
690 } else {
64762ddc 691 return '';
aaf7a9dc 692 }
693}
694
caadf009 695function forum_user_outline($course, $user, $mod, $forum) {
696
1f48942e 697 if ($posts = forum_get_user_posts($forum->id, $user->id)) {
caadf009 698 $result->info = get_string("numposts", "forum", count($posts));
699
700 $lastpost = array_pop($posts);
701 $result->time = $lastpost->modified;
702 return $result;
703 }
704 return NULL;
705}
706
707
708function forum_user_complete($course, $user, $mod, $forum) {
709 global $CFG;
710
1f48942e 711 if ($posts = forum_get_user_posts($forum->id, $user->id)) {
caadf009 712 foreach ($posts as $post) {
e3ff14ca 713
f37da850 714 /// Add the forum id to the post object - used by read tracking.
715 $post->forum = $forum->id;
716
497588fe 717 forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false, $rate=false);
caadf009 718 }
719
720 } else {
41905731 721 echo "<p>".get_string("noposts", "forum")."</p>";
caadf009 722 }
caadf009 723}
724
1b5910c4 725function forum_print_recent_activity($course, $isteacher, $timestart) {
65b0e537 726/// Given a course and a date, prints a summary of all the new
cc3655a2 727/// messages posted in the course since that date
728
3d891989 729 global $CFG;
caadf009 730
731 $heading = false;
732 $content = false;
733
8f7dc7f1 734 if (!$logs = get_records_select('log', 'time > \''.$timestart.'\' AND '.
735 'course = \''.$course->id.'\' AND '.
736 'module = \'forum\' AND '.
737 'action LIKE \'add %\' ', 'time ASC')){
1b5910c4 738 return false;
739 }
740
8f7dc7f1 741 $strftimerecent = get_string('strftimerecent');
dcde9f02 742
d05956ac 743 $isteacheredit = isteacheredit($course->id);
744 $mygroupid = mygroupid($course->id);
745
746 $groupmode = array(); /// To cache group modes
747
1b5910c4 748 foreach ($logs as $log) {
749 //Get post info, I'll need it later
ac1d9a22 750 if ($post = forum_get_post_from_log($log)) {
751 //Create a temp valid module structure (course,id)
752 $tempmod->course = $log->course;
753 $tempmod->id = $post->forum;
754 //Obtain the visible property from the instance
8f7dc7f1 755 $modvisible = instance_is_visible('forum', $tempmod);
ac1d9a22 756 }
d05956ac 757
ac1d9a22 758 //Only if the post exists and mod is visible
759 if ($post && $modvisible) {
760 /// Check whether this is for teachers only
8f7dc7f1 761 $teacheronly = '';
762 if ($post->forumtype == 'teacher') {
763 if ($isteacher) {
764 $teacheronly = 'class=\'teacheronly\'';
765 } else {
766 continue;
b91d6dcd 767 }
ac1d9a22 768 }
769 /// Check whether this is belongs to a discussion in a group that
770 /// should NOT be accessible to the current user
b91d6dcd 771
ac1d9a22 772 if (!$isteacheredit and $post->groupid != -1) { /// Editing teachers or open discussions
773 if (!isset($cm[$post->forum])) {
8f7dc7f1 774 $cm[$post->forum] = get_coursemodule_from_instance('forum', $post->forum, $course->id);
775 $groupmode[$post->forum] = groupmode($course, $cm[$post->forum]);
436a7cae 776 }
8f7dc7f1 777 if ($groupmode[$post->forum]) {
ac1d9a22 778 if ($mygroupid != $post->groupid) {
779 continue;
780 }
1b5910c4 781 }
caadf009 782 }
ac1d9a22 783
784 if (! $heading) {
8f7dc7f1 785 print_headline(get_string('newforumposts', 'forum').':');
ac1d9a22 786 $heading = true;
787 $content = true;
788 }
789 $date = userdate($post->modified, $strftimerecent);
8f7dc7f1 790
791 $subjectclass = ($log->action == 'add discussion') ? ' bold' : '';
792
793 echo '<div class="head'.$teacheronly.'"><span class="date">';
794 echo $date.'</span> - <span class="name">'.fullname($post, $isteacher);
795 echo '</span></div><div class="info'.$subjectclass.'">';
796 echo '"<a href="'.$CFG->wwwroot.'/mod/forum/'.str_replace('&', '&amp;', $log->url).'">';
17dc3f3c 797 $post->subject = break_up_long_words(format_string($post->subject,true));
8f7dc7f1 798 echo $post->subject;
799 echo '</a>"</div>';
caadf009 800 }
801 }
1b5910c4 802
caadf009 803 return $content;
804}
805
806
807function forum_grades($forumid) {
808/// Must return an array of grades, indexed by user, and a max grade.
caadf009 809
4db9d14d 810 if (!$forum = get_record("forum", "id", $forumid)) {
811 return false;
812 }
813 if (!$forum->assessed) {
814 return false;
815 }
d6bdd9d5 816 $scalemenu = make_grades_menu($forum->scale);
02ebf404 817
818 $currentuser = 0;
819 $ratingsuser = array();
820
1f48942e 821 if ($ratings = forum_get_user_grades($forumid)) {
02ebf404 822 foreach ($ratings as $rating) { // Ordered by user
823 if ($currentuser and $rating->userid != $currentuser) {
824 if (!empty($ratingsuser)) {
d6bdd9d5 825 if ($forum->scale < 0) {
826 $return->grades[$currentuser] = forum_get_ratings_mean(0, $scalemenu, $ratingsuser);
827 $return->grades[$currentuser] .= "<br />".forum_get_ratings_summary(0, $scalemenu, $ratingsuser);
828 } else {
829 $total = 0;
830 $count = 0;
831 foreach ($ratingsuser as $ra) {
832 $total += $ra;
833 $count ++;
834 }
835 $return->grades[$currentuser] = format_float($total/$count, 2);
836 }
02ebf404 837 } else {
838 $return->grades[$currentuser] = "";
839 }
840 $ratingsuser = array();
caadf009 841 }
02ebf404 842 $ratingsuser[] = $rating->rating;
843 $currentuser = $rating->userid;
caadf009 844 }
02ebf404 845 if (!empty($ratingsuser)) {
d6bdd9d5 846 if ($forum->scale < 0) {
847 $return->grades[$currentuser] = forum_get_ratings_mean(0, $scalemenu, $ratingsuser);
848 $return->grades[$currentuser] .= "<br />".forum_get_ratings_summary(0, $scalemenu, $ratingsuser);
849 } else {
850 $total = 0;
851 $count = 0;
852 foreach ($ratingsuser as $ra) {
853 $total += $ra;
854 $count ++;
855 }
856 $return->grades[$currentuser] = format_float((float)$total/(float)$count, 2);
857 }
02ebf404 858 } else {
859 $return->grades[$currentuser] = "";
caadf009 860 }
861 } else {
862 $return->grades = array();
863 }
864
d6bdd9d5 865 if ($forum->scale < 0) {
866 $return->maxgrade = "";
867 } else {
868 $return->maxgrade = $forum->scale;
869 }
caadf009 870 return $return;
871}
872
05855091 873function forum_get_participants($forumid) {
874//Returns the users with data in one forum
65b0e537 875//(users with records in forum_subscriptions, forum_posts and forum_ratings, students)
05855091 876
877 global $CFG;
878
879 //Get students from forum_subscriptions
95e72c12 880 $st_subscriptions = get_records_sql("SELECT DISTINCT u.id, u.id
05855091 881 FROM {$CFG->prefix}user u,
882 {$CFG->prefix}forum_subscriptions s
883 WHERE s.forum = '$forumid' and
65b0e537 884 u.id = s.userid");
05855091 885 //Get students from forum_posts
95e72c12 886 $st_posts = get_records_sql("SELECT DISTINCT u.id, u.id
05855091 887 FROM {$CFG->prefix}user u,
888 {$CFG->prefix}forum_discussions d,
889 {$CFG->prefix}forum_posts p
890 WHERE d.forum = '$forumid' and
891 p.discussion = d.id and
892 u.id = p.userid");
893
894 //Get students from forum_ratings
95e72c12 895 $st_ratings = get_records_sql("SELECT DISTINCT u.id, u.id
05855091 896 FROM {$CFG->prefix}user u,
897 {$CFG->prefix}forum_discussions d,
898 {$CFG->prefix}forum_posts p,
899 {$CFG->prefix}forum_ratings r
900 WHERE d.forum = '$forumid' and
901 p.discussion = d.id and
902 r.post = p.id and
903 u.id = r.userid");
904
905 //Add st_posts to st_subscriptions
906 if ($st_posts) {
907 foreach ($st_posts as $st_post) {
908 $st_subscriptions[$st_post->id] = $st_post;
909 }
910 }
911 //Add st_ratings to st_subscriptions
912 if ($st_ratings) {
913 foreach ($st_ratings as $st_rating) {
914 $st_subscriptions[$st_rating->id] = $st_rating;
915 }
916 }
917 //Return st_subscriptions array (it contains an array of unique users)
918 return ($st_subscriptions);
919}
caadf009 920
0f1a97c2 921function forum_scale_used ($forumid,$scaleid) {
922//This function returns if a scale is being used by one forum
65b0e537 923
0f1a97c2 924 $return = false;
65b0e537 925
0f1a97c2 926 $rec = get_record("forum","id","$forumid","scale","-$scaleid");
65b0e537 927
928 if (!empty($rec) && !empty($scaleid)) {
0f1a97c2 929 $return = true;
930 }
65b0e537 931
0f1a97c2 932 return $return;
933}
934
9fa49e22 935/// SQL FUNCTIONS ///////////////////////////////////////////////////////////
936
1f48942e 937function forum_get_post_full($postid) {
938/// Gets a post with all info ready for forum_print_post
939 global $CFG;
940
ebc3bd2b 941 return get_record_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
65b0e537 942 FROM {$CFG->prefix}forum_posts p,
943 {$CFG->prefix}user u
944 WHERE p.id = '$postid'
ebc3bd2b 945 AND p.userid = u.id");
1f48942e 946}
947
948function forum_get_discussion_posts($discussion, $sort) {
949/// Gets posts with all info ready for forum_print_post
950 global $CFG;
951
ebc3bd2b 952 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
65b0e537 953 FROM {$CFG->prefix}forum_posts p,
954 {$CFG->prefix}user u
955 WHERE p.discussion = $discussion
956 AND p.parent > 0
ebc3bd2b 957 AND p.userid = u.id $sort");
1f48942e 958}
959
960function forum_get_child_posts($parent) {
961/// Gets posts with all info ready for forum_print_post
962 global $CFG;
963
ebc3bd2b 964 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
65b0e537 965 FROM {$CFG->prefix}forum_posts p,
966 {$CFG->prefix}user u
967 WHERE p.parent = '$parent'
ebc3bd2b 968 AND p.userid = u.id
1f48942e 969 ORDER BY p.created ASC");
970}
971
972
4f321bdb 973function forum_search_posts($searchterms, $courseid, $page=0, $recordsperpage=50, &$totalcount, $groupid=0, $extrasql='') {
8b9c7aa0 974/// Returns a list of posts found using an array of search terms
975/// eg word +word -word
976///
977
9fa49e22 978 global $CFG;
855c0839 979 require_once($CFG->libdir.'/searchlib.php');
e3ff14ca 980
9fa49e22 981 if (!isteacher($courseid)) {
982 $notteacherforum = "AND f.type <> 'teacher'";
4cabd355 983 $forummodule = get_record("modules", "name", "forum");
984 $onlyvisible = " AND f.id = cm.instance AND cm.visible = 1 AND cm.module = $forummodule->id";
985 $onlyvisibletable = ", {$CFG->prefix}course_modules cm";
c7a5b3b1 986 if ($groupid) {
987 $separategroups = SEPARATEGROUPS;
988 $selectgroup = " AND (cm.groupmode <> '$separategroups' OR d.groupid = '$groupid')";
989 } else {
990 $selectgroup = '';
991 }
a702e657 992 $selectcourse = " AND d.course = '$courseid'";
9fa49e22 993 } else {
994 $notteacherforum = "";
c7a5b3b1 995 $selectgroup = '';
4cabd355 996 $onlyvisible = "";
997 $onlyvisibletable = "";
a702e657 998 if ($courseid == SITEID && isadmin()) {
999 $selectcourse = '';
1000 } else {
1001 $selectcourse = " AND d.course = '$courseid'";
1002 }
9fa49e22 1003 }
1004
e6ae4dc8 1005 $limit = sql_paging_limit($page, $recordsperpage);
97485d07 1006
63d99fcb 1007 /// Some differences in syntax for PostgreSQL
1008 if ($CFG->dbtype == "postgres7") {
a74f950c 1009 $LIKE = "ILIKE"; // case-insensitive
2b4c64e9 1010 $NOTLIKE = "NOT ILIKE"; // case-insensitive
1011 $REGEXP = "~*";
1012 $NOTREGEXP = "!~*";
b800ac5a 1013 } else {
a74f950c 1014 $LIKE = "LIKE";
2b4c64e9 1015 $NOTLIKE = "NOT LIKE";
a74f950c 1016 $REGEXP = "REGEXP";
2b4c64e9 1017 $NOTREGEXP = "NOT REGEXP";
c4c57597 1018 }
1019
b800ac5a 1020 $messagesearch = "";
855c0839 1021 $searchstring = "";
1022 // Need to concat these back together for parser to work.
1023 foreach($searchterms as $searchterm){
1024 if ($searchstring != "") {
1025 $searchstring .= " ";
63d99fcb 1026 }
855c0839 1027 $searchstring .= $searchterm;
b800ac5a 1028 }
1029
855c0839 1030 // We need to allow quoted strings for the search. The quotes *should* be stripped
1031 // by the parser, but this should be examined carefully for security implications.
1032 $searchstring = str_replace("\\\"","\"",$searchstring);
1033 $parser = new search_parser();
f99fcffe 1034 $lexer = new search_lexer($parser);
e6ae4dc8 1035
855c0839 1036 if ($lexer->parse($searchstring)) {
1037 $parsearray = $parser->get_parsed_array();
77ffdf4b 1038 $messagesearch = search_generate_SQL($parsearray,'p.message','p.subject','p.userid','u.id','u.firstname','u.lastname','p.modified', 'd.forum');
855c0839 1039 }
e3ff14ca 1040
65b0e537 1041 $selectsql = "{$CFG->prefix}forum_posts p,
1042 {$CFG->prefix}forum_discussions d,
1043 {$CFG->prefix}user u,
4cabd355 1044 {$CFG->prefix}forum f $onlyvisibletable
855c0839 1045 WHERE ($messagesearch)
65b0e537 1046 AND p.userid = u.id
a702e657 1047 AND p.discussion = d.id $selectcourse
4f321bdb 1048 AND d.forum = f.id $notteacherforum $onlyvisible $selectgroup $extrasql";
65b0e537 1049
8b9c7aa0 1050 $totalcount = count_records_sql("SELECT COUNT(*) FROM $selectsql");
b800ac5a 1051
8b9c7aa0 1052 return get_records_sql("SELECT p.*,u.firstname,u.lastname,u.email,u.picture FROM
1053 $selectsql ORDER BY p.modified DESC $limit");
9fa49e22 1054}
1055
63d99fcb 1056
9fa49e22 1057function forum_get_ratings($postid, $sort="u.firstname ASC") {
1058/// Returns a list of ratings for a particular post - sorted.
1059 global $CFG;
65b0e537 1060 return get_records_sql("SELECT u.*, r.rating, r.time
1061 FROM {$CFG->prefix}forum_ratings r,
9fa49e22 1062 {$CFG->prefix}user u
65b0e537 1063 WHERE r.post = '$postid'
1064 AND r.userid = u.id
9fa49e22 1065 ORDER BY $sort");
1066}
1067
3ecca1ee 1068function forum_get_unmailed_posts($starttime, $endtime) {
1f48942e 1069/// Returns a list of all new posts that have not been mailed yet
1070 global $CFG;
65b0e537 1071 return get_records_sql("SELECT p.*, d.course
1072 FROM {$CFG->prefix}forum_posts p,
1f48942e 1073 {$CFG->prefix}forum_discussions d
65b0e537 1074 WHERE p.mailed = 0
3ecca1ee 1075 AND p.created >= '$starttime'
1076 AND p.created < '$endtime'
c274b0e0 1077 AND p.discussion = d.id
1078 ORDER BY p.modified ASC");
1f48942e 1079}
1080
3ecca1ee 1081function forum_mark_old_posts_as_mailed($endtime) {
1082/// Marks posts before a certain time as being mailed already
1083 global $CFG;
c274b0e0 1084 return execute_sql("UPDATE {$CFG->prefix}forum_posts
1085 SET mailed = '1'
0362d553 1086 WHERE created < '$endtime' AND mailed ='0'");
3ecca1ee 1087}
1088
1f48942e 1089function forum_get_user_posts($forumid, $userid) {
1090/// Get all the posts for a user in a forum suitable for forum_print_post
1091 global $CFG;
1092
ebc3bd2b 1093 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
65b0e537 1094 FROM {$CFG->prefix}forum f,
1095 {$CFG->prefix}forum_discussions d,
1096 {$CFG->prefix}forum_posts p,
1097 {$CFG->prefix}user u
1098 WHERE f.id = '$forumid'
1099 AND d.forum = f.id
1f48942e 1100 AND p.discussion = d.id
65b0e537 1101 AND p.userid = '$userid'
ebc3bd2b 1102 AND p.userid = u.id
1f48942e 1103 ORDER BY p.modified ASC");
1104}
1105
1106function forum_get_post_from_log($log) {
1107/// Given a log entry, return the forum post details for it.
1108 global $CFG;
1109
1110 if ($log->action == "add post") {
1111
8f7dc7f1 1112 return get_record_sql("SELECT p.*, f.type AS forumtype, d.forum, d.groupid,
1113 u.firstname, u.lastname, u.email, u.picture
65b0e537 1114 FROM {$CFG->prefix}forum_discussions d,
1115 {$CFG->prefix}forum_posts p,
8f7dc7f1 1116 {$CFG->prefix}forum f,
65b0e537 1117 {$CFG->prefix}user u
1118 WHERE p.id = '$log->info'
1119 AND d.id = p.discussion
1120 AND p.userid = u.id
8f7dc7f1 1121 AND u.deleted <> '1'
1122 AND f.id = d.forum");
1f48942e 1123
1124
1125 } else if ($log->action == "add discussion") {
1126
8f7dc7f1 1127 return get_record_sql("SELECT p.*, f.type AS forumtype, d.forum, d.groupid,
1128 u.firstname, u.lastname, u.email, u.picture
65b0e537 1129 FROM {$CFG->prefix}forum_discussions d,
1130 {$CFG->prefix}forum_posts p,
8f7dc7f1 1131 {$CFG->prefix}forum f,
65b0e537 1132 {$CFG->prefix}user u
1133 WHERE d.id = '$log->info'
1134 AND d.firstpost = p.id
1135 AND p.userid = u.id
8f7dc7f1 1136 AND u.deleted <> '1'
1137 AND f.id = d.forum");
1f48942e 1138 }
1139 return NULL;
1140}
1141
d05956ac 1142function forum_get_firstpost_from_discussion($discussionid) {
1143/// Given a discussion id, return the first post from the discussion
1144 global $CFG;
1145
1146 return get_record_sql("SELECT p.*
65b0e537 1147 FROM {$CFG->prefix}forum_discussions d,
d05956ac 1148 {$CFG->prefix}forum_posts p
65b0e537 1149 WHERE d.id = '$discussionid'
d05956ac 1150 AND d.firstpost = p.id ");
1151}
1152
1f48942e 1153
1154function forum_get_user_grades($forumid) {
1155/// Get all user grades for a forum
1156 global $CFG;
1157
ebc3bd2b 1158 return get_records_sql("SELECT r.id, p.userid, r.rating
65b0e537 1159 FROM {$CFG->prefix}forum_discussions d,
1160 {$CFG->prefix}forum_posts p,
1f48942e 1161 {$CFG->prefix}forum_ratings r
65b0e537 1162 WHERE d.forum = '$forumid'
1f48942e 1163 AND p.discussion = d.id
02ebf404 1164 AND r.post = p.id
1165 ORDER by p.userid ");
1f48942e 1166}
1167
1168
a48e8c4b 1169function forum_count_discussion_replies($forum='0', $course='0', $user='0') {
1170// Returns an array of counts of replies to each discussion (optionally in one forum or course and/or user)
1f48942e 1171 global $CFG;
1172
a48e8c4b 1173 $forumselect = $courseselect = $userselect = '';
1174
1f48942e 1175 if ($forum) {
1176 $forumselect = " AND d.forum = '$forum'";
a48e8c4b 1177 }
1178 if ($course) {
1179 $courseselect = " AND d.course = '$course'";
1180 }
1181 if ($user) {
1182 $userselect = " AND d.userid = '$user'";
1f48942e 1183 }
3599e487 1184 return get_records_sql("SELECT p.discussion, (count(*)) as replies, max(p.id) as lastpostid
65b0e537 1185 FROM {$CFG->prefix}forum_posts p,
1f48942e 1186 {$CFG->prefix}forum_discussions d
a48e8c4b 1187 WHERE p.parent > 0 $forumselect $courseselect $userselect
1c887009 1188 AND p.discussion = d.id
1f48942e 1189 GROUP BY p.discussion");
1190}
1191
1192function forum_count_unrated_posts($discussionid, $userid) {
1193// How many unrated posts are in the given discussion for a given user?
1194 global $CFG;
1195 if ($posts = get_record_sql("SELECT count(*) as num
1196 FROM {$CFG->prefix}forum_posts
65b0e537 1197 WHERE parent > 0
1198 AND discussion = '$discussionid'
ebc3bd2b 1199 AND userid <> '$userid' ")) {
1f48942e 1200
65b0e537 1201 if ($rated = get_record_sql("SELECT count(*) as num
1202 FROM {$CFG->prefix}forum_posts p,
1f48942e 1203 {$CFG->prefix}forum_ratings r
1204 WHERE p.discussion = '$discussionid'
65b0e537 1205 AND p.id = r.post
ebc3bd2b 1206 AND r.userid = '$userid'")) {
1f48942e 1207 $difference = $posts->num - $rated->num;
1208 if ($difference > 0) {
1209 return $difference;
1210 } else {
1211 return 0; // Just in case there was a counting error
1212 }
1213 } else {
1214 return $posts->num;
1215 }
1216 } else {
1217 return 0;
1218 }
1219}
1220
65b0e537 1221function forum_get_discussions($forum="0", $forumsort="d.timemodified DESC",
13597d01 1222 $user=0, $fullpost=true, $visiblegroups=-1, $limit=0, $userlastmodified=false) {
1f48942e 1223/// Get all discussions in a forum
1224 global $CFG;
1225
1226 if ($user) {
1227 $userselect = " AND u.id = '$user' ";
1228 } else {
1229 $userselect = "";
1230 }
2862b309 1231
90ec387a 1232 if ($limit) {
1233 $limit = " LIMIT $limit ";
1234 } else {
1235 $limit = "";
1236 }
8f0cd6ef 1237
2862b309 1238 if ($visiblegroups == -1) {
02509fe6 1239 $groupselect = "";
2862b309 1240 } else {
1241 $groupselect = " AND (d.groupid = '$visiblegroups' OR d.groupid = '-1') ";
02509fe6 1242 }
2862b309 1243
29507631 1244 if (empty($forumsort)) {
1245 $forumsort = "d.timemodified DESC";
1246 }
2ab968e9 1247 if (empty($fullpost)) {
b879effb 1248 $postdata = "p.id,p.subject,p.modified,p.discussion,p.userid";
2ab968e9 1249 } else {
1250 $postdata = "p.*";
1251 }
9197e147 1252
13597d01 1253 if (empty($userlastmodified)) { // We don't need to know this
1254 $umfields = '';
1255 $umtable = '';
13597d01 1256 } else {
096de9ad 1257 $umfields = ', um.firstname AS umfirstname, um.lastname AS umlastname';
cfdc10e6 1258 $umtable = ' LEFT JOIN '.$CFG->prefix.'user um on (d.usermodified = um.id)';
13597d01 1259 }
1260
3a68fbbb 1261 return get_records_sql("SELECT $postdata, d.name, d.timemodified, d.usermodified, d.groupid,
13597d01 1262 u.firstname, u.lastname, u.email, u.picture $umfields
65b0e537 1263 FROM {$CFG->prefix}forum_discussions d,
9197e147 1264 {$CFG->prefix}forum_posts p,
13597d01 1265 {$CFG->prefix}user u
1266 $umtable
65b0e537 1267 WHERE d.forum = '$forum'
1268 AND p.discussion = d.id
1269 AND p.parent = 0
096de9ad 1270 AND p.userid = u.id $groupselect $userselect
90ec387a 1271 ORDER BY $forumsort $limit");
1f48942e 1272}
1273
1274
1275
b656e2a9 1276function forum_get_user_discussions($courseid, $userid, $groupid=0) {
1277/// Get all discussions started by a particular user in a course (or group)
4f321bdb 1278/// This function no longer used ...
1f48942e 1279 global $CFG;
1280
b656e2a9 1281 if ($groupid) {
1282 $groupselect = " AND d.groupid = '$groupid' ";
1283 } else {
1284 $groupselect = "";
1285 }
1286
2862b309 1287 return get_records_sql("SELECT p.*, d.groupid, u.firstname, u.lastname, u.email, u.picture,
ebc3bd2b 1288 f.type as forumtype, f.name as forumname, f.id as forumid
65b0e537 1289 FROM {$CFG->prefix}forum_discussions d,
1290 {$CFG->prefix}forum_posts p,
1291 {$CFG->prefix}user u,
1f48942e 1292 {$CFG->prefix}forum f
65b0e537 1293 WHERE d.course = '$courseid'
1294 AND p.discussion = d.id
1295 AND p.parent = 0
1296 AND p.userid = u.id
1297 AND u.id = '$userid'
b656e2a9 1298 AND d.forum = f.id $groupselect
b8bf90c5 1299 ORDER BY p.created DESC");
1f48942e 1300}
1301
1302
6673d7bd 1303function forum_subscribed_users($course, $forum, $groupid=0) {
1f48942e 1304/// Returns list of user objects that are subscribed to this forum
1305 global $CFG;
1306
6673d7bd 1307 if ($groupid) {
1308 $grouptables = ", {$CFG->prefix}groups_members g";
1309 $groupselect = " AND g.groupid = '$groupid' AND u.id = g.userid";
1310 } else {
1311 $grouptables = "";
1312 $groupselect = "";
1313 }
1314
709f0ec8 1315 if (forum_is_forcesubscribed($forum->id)) {
7b5944cd 1316 if ($forum->type == "teacher") {
1317 return get_course_teachers($course->id); // Only teachers can be subscribed to teacher forums
adaf3928 1318 } else {
7b5944cd 1319 return get_course_users($course->id); // Otherwise get everyone in the course
1f48942e 1320 }
1321 }
aaf7a9dc 1322 return get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, u.maildigest, u.emailstop,
0351b1f9 1323 u.email, u.city, u.country, u.lastaccess, u.lastlogin, u.picture, u.timezone, u.lang
65b0e537 1324 FROM {$CFG->prefix}user u,
6673d7bd 1325 {$CFG->prefix}forum_subscriptions s $grouptables
1f48942e 1326 WHERE s.forum = '$forum->id'
65b0e537 1327 AND s.userid = u.id
6673d7bd 1328 AND u.deleted <> 1 $groupselect
7c81b6e2 1329 ORDER BY u.email ASC");
1f48942e 1330}
9fa49e22 1331
067675c0 1332
1333
caadf009 1334/// OTHER FUNCTIONS ///////////////////////////////////////////////////////////
f93f848a 1335
1336
11b0c469 1337function forum_get_course_forum($courseid, $type) {
1338// How to set up special 1-per-course forums
a6fcdf98 1339 global $CFG;
1340
29cbd93a 1341 if ($forums = get_records_select("forum", "course = '$courseid' AND type = '$type'", "id ASC")) {
65b0e537 1342 // There should always only be ONE, but with the right combination of
29cbd93a 1343 // errors there might be more. In this case, just return the oldest one (lowest ID).
1344 foreach ($forums as $forum) {
1345 return $forum; // ie the first one
11b0c469 1346 }
8daaf761 1347 }
e6874d9f 1348
8daaf761 1349 // Doesn't exist, so create one now.
1350 $forum->course = $courseid;
1351 $forum->type = "$type";
1352 switch ($forum->type) {
1353 case "news":
65a3ef30 1354 $forum->name = addslashes(get_string("namenews", "forum"));
1355 $forum->intro = addslashes(get_string("intronews", "forum"));
906fef94 1356 $forum->forcesubscribe = FORUM_FORCESUBSCRIBE;
8daaf761 1357 $forum->open = 1; // 0 - no, 1 - posts only, 2 - discuss and post
1358 $forum->assessed = 0;
709f0ec8 1359 if ($courseid == SITEID) {
1360 $forum->name = get_string("sitenews");
1361 $forum->forcesubscribe = 0;
8f0cd6ef 1362 }
8daaf761 1363 break;
1364 case "social":
65a3ef30 1365 $forum->name = addslashes(get_string("namesocial", "forum"));
1366 $forum->intro = addslashes(get_string("introsocial", "forum"));
8daaf761 1367 $forum->open = 2; // 0 - no, 1 - posts only, 2 - discuss and post
1368 $forum->assessed = 0;
1369 $forum->forcesubscribe = 0;
1370 break;
1371 case "teacher":
65a3ef30 1372 $forum->name = addslashes(get_string("nameteacher", "forum"));
1373 $forum->intro = addslashes(get_string("introteacher", "forum"));
a796d0b8 1374 $forum->open = 2; // 0 - no, 1 - posts only, 2 - discuss and post
8daaf761 1375 $forum->assessed = 0;
1376 $forum->forcesubscribe = 0;
1377 break;
1378 default:
1379 notify("That forum type doesn't exist!");
1380 return false;
1381 break;
1382 }
1383
1384 $forum->timemodified = time();
1385 $forum->id = insert_record("forum", $forum);
1386
1387 if ($forum->type != "teacher") {
1388 if (! $module = get_record("modules", "name", "forum")) {
1389 notify("Could not find forum module!!");
1390 return false;
1391 }
1392 $mod->course = $courseid;
1393 $mod->module = $module->id;
1394 $mod->instance = $forum->id;
1395 $mod->section = 0;
1396 if (! $mod->coursemodule = add_course_module($mod) ) { // assumes course/lib.php is loaded
1397 notify("Could not add a new course module to the course '$course->fullname'");
1398 return false;
1399 }
1400 if (! $sectionid = add_mod_to_section($mod) ) { // assumes course/lib.php is loaded
1401 notify("Could not add the new course module to that section");
1402 return false;
1403 }
1404 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
1405 notify("Could not update the course module with the correct section");
1406 return false;
1407 }
1408 include_once("$CFG->dirroot/course/lib.php");
d769d2ee 1409 rebuild_course_cache($courseid);
82aa0e8d 1410 }
65b0e537 1411
8daaf761 1412 return get_record("forum", "id", "$forum->id");
82aa0e8d 1413}
1414
f93f848a 1415
65b0e537 1416function forum_make_mail_post(&$post, $user, $touser, $course,
11b0c469 1417 $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
65b0e537 1418// Given the data about a posting, builds up the HTML to display it and
501cdbd8 1419// returns the HTML in a string. This is designed for sending via HTML email.
1420
d30867b0 1421 global $CFG;
501cdbd8 1422
0d851f90 1423 static $formattedtext; // Cached version of formatted text for a post
1424 static $formattedtextid; // The ID number of the post
1425
1426 if (empty($formattedtextid) or $formattedtextid != $post->id) { // Recalculate the formatting
1427 $formattedtext = format_text($post->message, $post->format, NULL, $course->id);
1428 $formattedtextid = $post->id;
1429 }
1430
add3201e 1431 $output = '<table border="0" cellpadding="3" cellspacing="0" class="forumpost">';
501cdbd8 1432
add3201e 1433 $output .= '<tr class="header"><td width="35" valign="top" class="picture left">';
501cdbd8 1434 $output .= print_user_picture($user->id, $course->id, $user->picture, false, true);
add3201e 1435 $output .= '</td>';
501cdbd8 1436
1437 if ($post->parent) {
add3201e 1438 $output .= '<td class="topic">';
501cdbd8 1439 } else {
add3201e 1440 $output .= '<td class="topic starter">';
501cdbd8 1441 }
add3201e 1442 $output .= '<div class="subject">'.format_string($post->subject).'</div>';
1b26d5e7 1443
1444 $fullname = fullname($user, isteacher($course->id));
add3201e 1445 $by->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.$fullname.'</a>';
1446 $by->date = userdate($post->modified, '', $touser->timezone);
1447 $output .= '<div class="author">'.get_string('bynameondate', 'forum', $by).'</div>';
1448
1449 $output .= '</td></tr>';
1450
1451 $output .= '<tr><td class="left side">';
1452 if ($group = user_group($course->id, $user->id)) {
1453 $output .= print_group_picture($group, $courseid, false, true, true);
1454 } else {
1455 $output .= '&nbsp;';
1456 }
1b26d5e7 1457
add3201e 1458 $output .= '</td><td class="content">';
501cdbd8 1459
7f6689e4 1460 if ($post->attachment) {
1461 $post->course = $course->id;
add3201e 1462 $post->forum = get_field('forum_discussions', 'forum', 'id', $post->discussion);
1463 $output .= '<div class="attachments">';
1464 $output .= forum_print_attachments($post, 'html');
72d497d4 1465 $output .= "</div>";
7f6689e4 1466 }
1467
0d851f90 1468 $output .= $formattedtext;
501cdbd8 1469
add3201e 1470
1471/// Commands
1472
1473 $commands = array();
501cdbd8 1474
2e2e71a8 1475 if ($post->parent) {
add3201e 1476 $commands[] = '<a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.
1477 $post->discussion.'&amp;parent='.$post->parent.'">'.get_string('parent', 'forum').'</a>';
2e2e71a8 1478 }
ce45515e 1479
501cdbd8 1480 if ($ownpost) {
add3201e 1481 $commands[] = '<a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/post.php?delete='.$post->id.'">'.
1482 get_string('delete', 'forum').'</a>';
1483 }
1484
1485 if ($reply) {
1486 $commands[] = '<a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/post.php?reply='.$post->id.'">'.
1487 get_string('reply', 'forum').'</a>';
501cdbd8 1488 }
1489
add3201e 1490 $output .= '<div class="commands">';
1491 $output .= implode(' | ', $commands);
1492 $output .= '</div>';
1493
1494
1495/// Context link to post if required
65b0e537 1496
501cdbd8 1497 if ($link) {
add3201e 1498 $output .= '<div class="link">';
77e75a76 1499 $output .= '<a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#'.$post->id.'">'.
add3201e 1500 get_string('postincontext', 'forum').'</a>';
1501 $output .= '</div>';
501cdbd8 1502 }
add3201e 1503
501cdbd8 1504 if ($footer) {
add3201e 1505 $output .= '<div class="footer">'.$footer.'</div>';
501cdbd8 1506 }
add3201e 1507 $output .= '</td></tr></table>'."\n\n";
1508
501cdbd8 1509
1510 return $output;
1511}
1512
1513
65b0e537 1514function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link=false,
f37da850 1515 $ratings=NULL, $footer="", $highlight="", $post_read=-99) {
74f5d1e3 1516
d30867b0 1517 global $USER, $CFG;
501cdbd8 1518
359f2758 1519 static $stredit, $strdelete, $strreply, $strparent, $strprune, $strpruneheading, $threadedmode, $isteacher, $adminedit;
2e2e71a8 1520
f37da850 1521 static $strmarkread, $strmarkunread;
1522
2e2e71a8 1523 if (empty($stredit)) {
359f2758 1524 $stredit = get_string('edit', 'forum');
1525 $strdelete = get_string('delete', 'forum');
1526 $strreply = get_string('reply', 'forum');
1527 $strparent = get_string('parent', 'forum');
1528 $strpruneheading = get_string('pruneheading', 'forum');
1529 $strprune = get_string('prune', 'forum');
2e2e71a8 1530 $threadedmode = (!empty($USER->mode) and ($USER->mode == FORUM_MODE_THREADED));
3bd98ad4 1531 $isteacher = isteacher($courseid);
b8be40ce 1532 $adminedit = (isadmin() and !empty($CFG->admineditalways));
f37da850 1533 $strmarkread = get_string('markread', 'forum');
1534 $strmarkunread = get_string('markunread', 'forum');
1535 }
1536
143b43cb 1537 if ($CFG->forum_trackreadposts && forum_tp_is_tracked($post->forum, $USER->id)) {
f37da850 1538 if ($post_read == -99) { // If we don't know yet...
489de4ae 1539 /// The front page can display a news item post to non-logged in users. This should
1540 /// always appear as 'read'.
1541 $post_read = empty($USER) || forum_tp_is_post_read($USER->id, $post);
f37da850 1542 }
1543 if ($post_read) {
1544 $read_style = ' read';
1545 } else {
1546 $read_style = ' unread';
1547 echo '<a name="unread"></a>';
1548 }
1549 } else {
1550 $read_style = '';
2e2e71a8 1551 }
1552
f37da850 1553 echo '<a name="'.$post->id.'"></a>';
33200577 1554 echo '<table cellspacing="0" class="forumpost'.$read_style.'">';
501cdbd8 1555
d3583b41 1556 echo '<tr class="header"><td class="picture left">';
501cdbd8 1557 print_user_picture($post->userid, $courseid, $post->picture);
d3583b41 1558 echo '</td>';
501cdbd8 1559
1560 if ($post->parent) {
d3583b41 1561 echo '<td class="topic">';
501cdbd8 1562 } else {
d3583b41 1563 echo '<td class="topic starter">';
501cdbd8 1564 }
83ec9098 1565
d3583b41 1566 echo '<div class="subject">'.$post->subject.'</div>';
e3ff14ca 1567
d3583b41 1568 echo '<div class="author">';
3bd98ad4 1569 $fullname = fullname($post, $isteacher);
d3583b41 1570 $by->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.
1571 $post->userid.'&amp;course='.$courseid.'">'.$fullname.'</a>';
d62413e8 1572 $by->date = userdate($post->modified);
d3583b41 1573 print_string('bynameondate', 'forum', $by);
1574 echo '</div></td></tr>';
1b26d5e7 1575
d3583b41 1576 echo '<tr><td class="left side">';
507407a7 1577 if ($group = user_group($courseid, $post->userid)) {
a4fbb0b2 1578 print_group_picture($group, $courseid, false, false, true);
507407a7 1579 } else {
d3583b41 1580 echo '&nbsp;';
507407a7 1581 }
d3583b41 1582
1583/// Actual content
1584
1585 echo '</td><td class="content">'."\n";
501cdbd8 1586
7f6689e4 1587 if ($post->attachment) {
1588 $post->course = $courseid;
d3583b41 1589 $post->forum = get_field('forum_discussions', 'forum', 'id', $post->discussion);
1590 echo '<div class="attachments">';
72d497d4 1591 $attachedimages = forum_print_attachments($post);
d3583b41 1592 echo '</div>';
e9c2dc1f 1593 } else {
d3583b41 1594 $attachedimages = '';
7f6689e4 1595 }
1596
d3583b41 1597
5be7800c 1598 if ($link and (strlen(strip_tags($post->message)) > $CFG->forum_longpost)) {
aa153f29 1599 // Print shortened version
71767204 1600 echo format_text(forum_shorten_post($post->message), $post->format, NULL, $courseid);
c585fa17 1601 $numwords = count_words(strip_tags($post->message));
d3583b41 1602 echo '<p><a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'">';
1603 echo get_string('readtherest', 'forum');
1604 echo '</a> ('.get_string('numwords', '', $numwords).')...</p>';
501cdbd8 1605 } else {
aa153f29 1606 // Print whole message
88438a58 1607 if ($highlight) {
71767204 1608 echo highlight($highlight, format_text($post->message, $post->format, NULL, $courseid));
88438a58 1609 } else {
71767204 1610 echo format_text($post->message, $post->format, NULL, $courseid);
88438a58 1611 }
65b0e537 1612 echo $attachedimages;
501cdbd8 1613 }
1614
d3583b41 1615
1616/// Commands
1617
359f2758 1618 $commands = array();
501cdbd8 1619
f37da850 1620 if ($CFG->forum_trackreadposts) {
489de4ae 1621 /// SPECIAL CASE: The front page can display a news item post to non-logged in users.
1622 /// Don't display the mark read / unread controls in this case.
1623 if ($CFG->forum_usermarksread && !empty($USER)) {
f37da850 1624 if ($post_read) {
1625 $mcmd = '&amp;mark=unread&amp;postid='.$post->id;
1626 $mtxt = $strmarkunread;
1627 } else {
1628 $mcmd = '&amp;mark=read&amp;postid='.$post->id;
1629 $mtxt = $strmarkread;
1630 }
1631 if ($threadedmode) {
d3583b41 1632 $commands[] = '<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.
1633 $post->discussion.'&amp;parent='.$post->id.$mcmd.'">'.$mtxt.'</a>';
f37da850 1634 } else {
d3583b41 1635 $commands[] = '<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.
1636 $post->discussion.$mcmd.'#'.$post->id.'">'.$mtxt.'</a>';
f37da850 1637 }
1638 }
1639 }
1640
2e2e71a8 1641 if ($post->parent) {
1642 if ($threadedmode) {
d3583b41 1643 $commands[] = '<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.
1644 $post->discussion.'&amp;parent='.$post->parent.'">'.$strparent.'</a>';
2e2e71a8 1645 } else {
d3583b41 1646 $commands[] = '<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.
1647 $post->discussion.'#'.$post->parent.'">'.$strparent.'</a>';
2e2e71a8 1648 }
1649 }
1650
501cdbd8 1651 $age = time() - $post->created;
b8be40ce 1652 if ($ownpost or $adminedit) {
1653 if (($age < $CFG->maxeditingtime) or $adminedit) {
d3583b41 1654 $commands[] = '<a href="'.$CFG->wwwroot.'/mod/forum/post.php?edit='.$post->id.'">'.$stredit.'</a>';
501cdbd8 1655 }
64eacd6f 1656 }
aaf7a9dc 1657
cf84431b 1658 if (isteacheredit($courseid) and $post->parent) {
d3583b41 1659 $commands[] = '<a href="'.$CFG->wwwroot.'/mod/forum/post.php?prune='.$post->id.
1660 '" title="'.$strpruneheading.'">'.$strprune.'</a>';
cf84431b 1661 }
aaf7a9dc 1662
ac00b904 1663 if (($ownpost and $age < $CFG->maxeditingtime) or $isteacher) {
d3583b41 1664 $commands[] = '<a href="'.$CFG->wwwroot.'/mod/forum/post.php?delete='.$post->id.'">'.$strdelete.'</a>';
64eacd6f 1665 }
359f2758 1666
64eacd6f 1667 if ($reply) {
d3583b41 1668 $commands[] = '<a href="'.$CFG->wwwroot.'/mod/forum/post.php?reply='.$post->id.'">'.$strreply.'</a>';
501cdbd8 1669 }
501cdbd8 1670
d3583b41 1671 echo '<div class="commands">';
1672 echo implode(' | ', $commands);
1673 echo '</div>';
1674
1675
1676/// Ratings
74f5d1e3 1677
1678 $ratingsmenuused = false;
02ebf404 1679 if (!empty($ratings) and !empty($USER->id)) {
d3583b41 1680 echo '<div class="ratings">';
98914efd 1681 $useratings = true;
1682 if ($ratings->assesstimestart and $ratings->assesstimefinish) {
1683 if ($post->created < $ratings->assesstimestart or $post->created > $ratings->assesstimefinish) {
1684 $useratings = false;
1685 }
1686 }
1687 if ($useratings) {
3bd98ad4 1688 $mypost = ($USER->id == $post->userid);
1689
1690 if (($isteacher or $ratings->assesspublic) and !$mypost) {
1691 forum_print_ratings_mean($post->id, $ratings->scale, $isteacher);
d395046a 1692 if (!empty($ratings->allow)) {
d3583b41 1693 echo '&nbsp;';
d395046a 1694 forum_print_rating_menu($post->id, $USER->id, $ratings->scale);
1695 $ratingsmenuused = true;
1696 }
3bd98ad4 1697
1698 } else if ($mypost) {
1699 forum_print_ratings_mean($post->id, $ratings->scale, true);
1700
98914efd 1701 } else if (!empty($ratings->allow) ) {
1702 forum_print_rating_menu($post->id, $USER->id, $ratings->scale);
74f5d1e3 1703 $ratingsmenuused = true;
2a3cda19 1704 }
501cdbd8 1705 }
d3583b41 1706 echo '</div>';
501cdbd8 1707 }
65b0e537 1708
d3583b41 1709/// Link to post if required
1710
501cdbd8 1711 if ($link) {
d3583b41 1712 echo '<div class="link">';
501cdbd8 1713 if ($post->replies == 1) {
d3583b41 1714 $replystring = get_string('repliesone', 'forum', $post->replies);
501cdbd8 1715 } else {
d3583b41 1716 $replystring = get_string('repliesmany', 'forum', $post->replies);
501cdbd8 1717 }
d3583b41 1718 echo '<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'">'.
1719 get_string('discussthistopic', 'forum').'</a>&nbsp;('.$replystring.')';
1720 echo '</div>';
501cdbd8 1721 }
d3583b41 1722
501cdbd8 1723 if ($footer) {
d3583b41 1724 echo '<div class="footer">'.$footer.'</div>';
501cdbd8 1725 }
d3583b41 1726 echo '</td></tr></table>'."\n\n";
74f5d1e3 1727
f37da850 1728 if ($CFG->forum_trackreadposts && !$CFG->forum_usermarksread && !empty($post->forum)) {
1729 forum_tp_mark_post_read($USER->id, $post, $post->forum);
1730 }
1731
74f5d1e3 1732 return $ratingsmenuused;
501cdbd8 1733}
1734
3335f6fb 1735
1d2256d0 1736function forum_print_discussion_header(&$post, $forum, $group=-1, $datestring="", $forumtracked=true) {
43921b8a 1737/// This function prints the overview of a discussion in the forum listing.
1738/// It needs some discussion information and some post information, these
8f0cd6ef 1739/// happen to be combined for efficiency in the $post parameter by the function
43921b8a 1740/// that calls this one
1741/// forum_print_latest_discussions()
1742
d30867b0 1743 global $USER, $CFG;
3335f6fb 1744
f51e8d7e 1745 static $rowcount;
1746
1747 if (!isset($rowcount)) {
1748 $rowcount = 0;
1749 } else {
1750 $rowcount = ($rowcount + 1) % 2;
1751 }
1752
17dc3f3c 1753 $post->subject = format_string($post->subject,true);
436a7cae 1754
f51e8d7e 1755 echo "\n\n";
1756 echo '<tr class="discussion r'.$rowcount.'">';
3335f6fb 1757
29507631 1758 // Topic
f51e8d7e 1759 echo '<td class="topic starter">';
1760 echo '<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'">'.$post->subject.'</a>';
29507631 1761 echo "</td>\n";
1762
1763 // Picture
f51e8d7e 1764 echo '<td class="picture">';
a796d0b8 1765 print_user_picture($post->userid, $forum->course, $post->picture);
29507631 1766 echo "</td>\n";
3335f6fb 1767
29507631 1768 // User name
a796d0b8 1769 $fullname = fullname($post, isteacher($forum->course));
f51e8d7e 1770 echo '<td class="author">';
1771 echo '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$post->userid.'&amp;course='.$forum->course.'">'.$fullname.'</a>';
29507631 1772 echo "</td>\n";
1773
3a68fbbb 1774 // Group picture
425b4f1a 1775 if ($group !== -1) { // Groups are active - group is a group data object or NULL
f51e8d7e 1776 echo '<td class="picture group">';
c2b552fe 1777 if (!empty($group->picture) and empty($group->hidepicture)) {
3a68fbbb 1778 print_group_picture($group, $forum->course, false, false, true);
1779 } else if (isset($group->id)) {
1780 echo '<a href="'.$CFG->wwwroot.'/course/group.php?id='.$forum->course.'&amp;group='.$group->id.'">'.$group->name.'</a>';
1781 }
1782 echo "</td>\n";
1783 }
1784
f51e8d7e 1785 if ($forum->open or $forum->type == 'teacher') { // Show the column with replies
1786 echo '<td class="replies">';
1787 echo '<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'">';
1788 echo $post->replies.'</a>';
a796d0b8 1789 echo "</td>\n";
e3ff14ca 1790
f37da850 1791 if ($CFG->forum_trackreadposts) {
f51e8d7e 1792 echo '<td class="replies">';
1d2256d0 1793 if ($forumtracked) {
1794 echo '<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#unread">';
1795 if ($post->unread > 0) {
1796 echo '<span class="unread">'.$post->unread.'</span>';
1797 } else {
1798 echo $post->unread;
1799 }
1800 echo '</a>';
f51e8d7e 1801 } else {
1d2256d0 1802 echo 'n/a';
3a68fbbb 1803 }
f37da850 1804 echo "</td>\n";
1805 }
a796d0b8 1806 }
3335f6fb 1807
f51e8d7e 1808 echo '<td class="lastpost">';
43921b8a 1809 $usedate = (empty($post->timemodified)) ? $post->modified : $post->timemodified; // Just in case
839f2456 1810 $parenturl = (empty($post->lastpostid)) ? '' : '&amp;parent='.$post->lastpostid;
268c6485 1811 $usermodified->firstname = $post->umfirstname;
1812 $usermodified->lastname = $post->umlastname;
f51e8d7e 1813 echo '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$post->usermodified.'&amp;course='.$forum->course.'">'.
1814 fullname($usermodified).'</a><br />';
ac00b904 1815 echo '<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.$parenturl.'">'.
43921b8a 1816 userdate($usedate, $datestring).'</a>';
29507631 1817 echo "</td>\n";
1818
f51e8d7e 1819 echo "</tr>\n\n";
3335f6fb 1820
3335f6fb 1821}
1822
1823
aa153f29 1824function forum_shorten_post($message) {
c585fa17 1825// Given a post object that we already know has a long message
65b0e537 1826// this function truncates the message nicely to the first
5be7800c 1827// sane place between $CFG->forum_longpost and $CFG->forum_shortpost
1828
1829 global $CFG;
c585fa17 1830
1831 $i = 0;
1832 $tag = false;
1833 $length = strlen($message);
1834 $count = 0;
1835 $stopzone = false;
1836 $truncate = 0;
1837
1838 for ($i=0; $i<$length; $i++) {
a8afb411 1839 $char = $message[$i];
c585fa17 1840
1841 switch ($char) {
65b0e537 1842 case "<":
c585fa17 1843 $tag = true;
1844 break;
65b0e537 1845 case ">":
c585fa17 1846 $tag = false;
1847 break;
1848 default:
1849 if (!$tag) {
1850 if ($stopzone) {
67f0b4cc 1851 if ($char == ".") {
a8afb411 1852 $truncate = $i+1;
c585fa17 1853 break 2;
1854 }
1855 }
1856 $count++;
1857 }
a8afb411 1858 break;
c585fa17 1859 }
1860 if (!$stopzone) {
5be7800c 1861 if ($count > $CFG->forum_shortpost) {
c585fa17 1862 $stopzone = true;
1863 }
1864 }
1865 }
aa153f29 1866
c585fa17 1867 if (!$truncate) {
a8afb411 1868 $truncate = $i;
c585fa17 1869 }
1870
67f0b4cc 1871 return substr($message, 0, $truncate);
aa153f29 1872}
1873
501cdbd8 1874
3bd98ad4 1875function forum_print_ratings_mean($postid, $scale, $link=true) {
02ebf404 1876/// Print the multiple ratings on a post given to the current user by others.
1877/// Scale is an array of ratings
1878
1879 static $strrate;
05c47ef7 1880
1881 $mean = forum_get_ratings_mean($postid, $scale);
65b0e537 1882
05c47ef7 1883 if ($mean !== "") {
02ebf404 1884
1885 if (empty($strratings)) {
1886 $strratings = get_string("ratings", "forum");
501cdbd8 1887 }
501cdbd8 1888
02ebf404 1889 echo "$strratings: ";
3bd98ad4 1890 if ($link) {
1891 link_to_popup_window ("/mod/forum/report.php?id=$postid", "ratings", $mean, 400, 600);
1892 } else {
1893 echo "$mean ";
1894 }
501cdbd8 1895 }
1896}
1897
501cdbd8 1898
0761d83f 1899function forum_get_ratings_mean($postid, $scale, $ratings=NULL) {
1900/// Return the mean rating of a post given to the current user by others.
02ebf404 1901/// Scale is an array of possible ratings in the scale
1902/// Ratings is an optional simple array of actual ratings (just integers)
1903
1904 if (!$ratings) {
1905 $ratings = array();
1906 if ($rates = get_records("forum_ratings", "post", $postid)) {
1907 foreach ($rates as $rate) {
1908 $ratings[] = $rate->rating;
1909 }
501cdbd8 1910 }
501cdbd8 1911 }
02ebf404 1912
0761d83f 1913 $count = count($ratings);
1914
1915 if ($count == 0) {
02ebf404 1916 return "";
02ebf404 1917
0761d83f 1918 } else if ($count == 1) {
1919 return $scale[$ratings[0]];
1920
02ebf404 1921 } else {
0761d83f 1922 $total = 0;
1923 foreach ($ratings as $rating) {
1924 $total += $rating;
1925 }
1926 $mean = round( ((float)$total/(float)$count) + 0.001); // Little fudge factor so that 0.5 goes UP
65b0e537 1927
0761d83f 1928 if (isset($scale[$mean])) {
1929 return $scale[$mean]." ($count)";
1930 } else {
1931 return "$mean ($count)"; // Should never happen, hopefully
1932 }
02ebf404 1933 }
1934}
1935
1936function forum_get_ratings_summary($postid, $scale, $ratings=NULL) {
1937/// Return a summary of post ratings given to the current user by others.
1938/// Scale is an array of possible ratings in the scale
1939/// Ratings is an optional simple array of actual ratings (just integers)
1940
1941 if (!$ratings) {
1942 $ratings = array();
1943 if ($rates = get_records("forum_ratings", "post", $postid)) {
1944 foreach ($rates as $rate) {
1945 $rating[] = $rate->rating;
1946 }
1947 }
1948 }
1949
1950
1951 if (!$count = count($ratings)) {
1952 return "";
1953 }
1954
1955
1956 foreach ($scale as $key => $scaleitem) {
1957 $sumrating[$key] = 0;
1958 }
1959
1960 foreach ($ratings as $rating) {
1961 $sumrating[$rating]++;
1962 }
1963
1964 $summary = "";
1965 foreach ($scale as $key => $scaleitem) {
1966 $summary = $sumrating[$key].$summary;
1967 if ($key > 1) {
1968 $summary = "/$summary";
1969 }
1970 }
1971 return $summary;
1972}
1973
1974function forum_print_rating_menu($postid, $userid, $scale) {
65b0e537 1975/// Print the menu of ratings as part of a larger form.
02ebf404 1976/// If the post has already been - set that value.
1977/// Scale is an array of ratings
1978
1979 static $strrate;
1980
1981 if (!$rating = get_record("forum_ratings", "userid", $userid, "post", $postid)) {
1982 $rating->rating = 0;
1983 }
1984
1985 if (empty($strrate)) {
1986 $strrate = get_string("rate", "forum");
1987 }
1988
1989 choose_from_menu($scale, $postid, $rating->rating, "$strrate...");
501cdbd8 1990}
1991
7a12aab4 1992function forum_print_mode_form($discussion, $mode) {
1993 GLOBAL $FORUM_LAYOUT_MODES;
501cdbd8 1994
839f2456 1995 echo "<div align=\"center\">";
1996 popup_form("discuss.php?d=$discussion&amp;mode=", $FORUM_LAYOUT_MODES, "mode", $mode, "");
1997 echo "</div>\n";
501cdbd8 1998}
1999
6f1cc8d6 2000function forum_search_form($course, $search='') {
501cdbd8 2001 global $CFG;
2002
6f1cc8d6 2003 $output = '<table border="0" cellpadding="0" cellspacing="0"><tr><td nowrap="nowrap">';
2004 $output .= helpbutton('search', get_string('search'), 'moodle', true, false, '', true);
2005 $output .= '&nbsp;<form name="search" action="'.$CFG->wwwroot.'/mod/forum/search.php" style="display:inline">';
2006 $output .= '<input name="search" type="text" size="18" value="'.$search.'" alt="search" />';
2007 $output .= '<input value="'.get_string('searchforums', 'forum').'" type="submit" />';
2008 $output .= '<input name="id" type="hidden" value="'.$course->id.'" />';
2009 $output .= '</form>';
2010 $output .= '</td></tr></table>';
5e367a2d 2011
6f1cc8d6 2012 return $output;
501cdbd8 2013}
2014
2015
11b0c469 2016function forum_set_return() {
607809b3 2017 global $CFG, $SESSION;
501cdbd8 2018
28e1e8b9 2019 if (! isset($SESSION->fromdiscussion)) {
48d38fad 2020 if (!empty($_SERVER['HTTP_REFERER'])) {
2021 $referer = $_SERVER['HTTP_REFERER'];
2022 } else {
2023 $referer = "";
2024 }
28e1e8b9 2025 // If the referer is NOT a login screen then save it.
48d38fad 2026 if (! strncasecmp("$CFG->wwwroot/login", $referer, 300)) {
607809b3 2027 $SESSION->fromdiscussion = $_SERVER["HTTP_REFERER"];
28e1e8b9 2028 }
501cdbd8 2029 }
2030}
2031
2032
11b0c469 2033function forum_go_back_to($default) {
501cdbd8 2034 global $SESSION;
2035
9c9f7d77 2036 if (!empty($SESSION->fromdiscussion)) {
501cdbd8 2037 $returnto = $SESSION->fromdiscussion;
2038 unset($SESSION->fromdiscussion);
2039 return $returnto;
2040 } else {
2041 return $default;
2042 }
2043}
2044
7f6689e4 2045function forum_file_area_name($post) {
2046// Creates a directory file name, suitable for make_upload_directory()
2047 global $CFG;
2048
2049 return "$post->course/$CFG->moddata/forum/$post->forum/$post->id";
2050}
2051
2052function forum_file_area($post) {
2053 return make_upload_directory( forum_file_area_name($post) );
2054}
2055
2056function forum_delete_old_attachments($post, $exception="") {
2057// Deletes all the user files in the attachments area for a post
2058// EXCEPT for any file named $exception
2059
2060 if ($basedir = forum_file_area($post)) {
2061 if ($files = get_directory_list($basedir)) {
2062 foreach ($files as $file) {
2063 if ($file != $exception) {
2064 unlink("$basedir/$file");
2065 notify("Existing file '$file' has been deleted!");
2066 }
2067 }
2068 }
2069 if (!$exception) { // Delete directory as well, if empty
2070 rmdir("$basedir");
2071 }
2072 }
2073}
2074
cc2b7ea5 2075function forum_move_attachments($discussion, $forumid) {
65b0e537 2076/// Given a discussion object that is being moved to forumid,
2077/// this function checks all posts in that discussion
2078/// for attachments, and if any are found, these are
cc2b7ea5 2079/// moved to the new forum directory.
2080
2081 global $CFG;
2082
18b8fbfa 2083 require_once($CFG->dirroot.'/lib/uploadlib.php');
2084
cc2b7ea5 2085 $return = true;
2086
2087 if ($posts = get_records_select("forum_posts", "discussion = '$discussion->id' AND attachment <> ''")) {
2088 foreach ($posts as $oldpost) {
2089 $oldpost->course = $discussion->course;
2090 $oldpost->forum = $discussion->forum;
2091 $oldpostdir = "$CFG->dataroot/".forum_file_area_name($oldpost);
2092 if (is_dir($oldpostdir)) {
2093 $newpost = $oldpost;
2094 $newpost->forum = $forumid;
7f0c3a44 2095 $newpostdir = forum_file_area($newpost);
18b8fbfa 2096 $files = get_directory_list($oldpostdir); // get it before we rename it.
cc2b7ea5 2097 if (! @rename($oldpostdir, $newpostdir)) {
2098 $return = false;
2099 }
18b8fbfa 2100 foreach ($files as $file) {
2101 clam_change_log($oldpostdir.'/'.$file,$newpostdir.'/'.$file);
2102 }
cc2b7ea5 2103 }
2104 }
2105 }
2106 return $return;
2107}
2108
7f6689e4 2109function forum_print_attachments($post, $return=NULL) {
2110// if return=html, then return a html string.
2111// if return=text, then return a text-only string.
72d497d4 2112// otherwise, print HTML for non-images, and return image HTML
7f6689e4 2113
2114 global $CFG;
2115
2116 $filearea = forum_file_area_name($post);
2117
72d497d4 2118 $imagereturn = "";
2119 $output = "";
2120
7f6689e4 2121 if ($basedir = forum_file_area($post)) {
2122 if ($files = get_directory_list($basedir)) {
2123 $strattachment = get_string("attachment", "forum");
6afe8558 2124 $strpopupwindow = get_string("popupwindow");
7f6689e4 2125 foreach ($files as $file) {
2126 $icon = mimeinfo("icon", $file);
2127 if ($CFG->slasharguments) {
2128 $ffurl = "file.php/$filearea/$file";
2129 } else {
2130 $ffurl = "file.php?file=/$filearea/$file";
2131 }
839f2456 2132 $image = "<img border=\"0\" src=\"$CFG->pixpath/f/$icon\" height=\"16\" width=\"16\" alt=\"$strpopupwindow\" />";
7f6689e4 2133
2134 if ($return == "html") {
6afe8558 2135 $output .= "<a href=\"$CFG->wwwroot/$ffurl\">$image</a> ";
2136 $output .= "<a href=\"$CFG->wwwroot/$ffurl\">$file</a><br />";
7f6689e4 2137
2138 } else if ($return == "text") {
2139 $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
2140
2141 } else {
72d497d4 2142 if ($icon == "image.gif") { // Image attachments don't get printed as links
839f2456 2143 $imagereturn .= "<br /><img src=\"$CFG->wwwroot/$ffurl\" alt=\"\" />";
72d497d4 2144 } else {
2145 link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
2146 echo "<a href=\"$CFG->wwwroot/$ffurl\">$file</a>";
2147 echo "<br />";
2148 }
7f6689e4 2149 }
2150 }
2151 }
2152 }
72d497d4 2153
7f6689e4 2154 if ($return) {
2155 return $output;
2156 }
72d497d4 2157
2158 return $imagereturn;
7f6689e4 2159}
db290a6e 2160/**
e3ff14ca 2161 * If successful, this function returns the name of the file
db290a6e 2162 * @param $post is a full post record, including course and forum
2163 * @param $newfile is a full upload array from $_FILES
2164 * @param $message is a string to hold the messages.
e3ff14ca 2165 */
7f6689e4 2166
db290a6e 2167function forum_add_attachment($post, $inputname,&$message) {
7f6689e4 2168
9dd0b378 2169 global $CFG;
2170
4909e176 2171 if (!$forum = get_record("forum", "id", $post->forum)) {
2172 return "";
2173 }
2174
2175 if (!$course = get_record("course", "id", $forum->course)) {
2176 return "";
2177 }
2178
5ddaa2e2 2179 require_once($CFG->dirroot.'/lib/uploadlib.php');
db290a6e 2180 $um = new upload_manager($inputname,true,false,$course,false,$forum->maxbytes,true,true);
5ddaa2e2 2181 $dir = forum_file_area_name($post);
2182 if ($um->process_file_uploads($dir)) {
db290a6e 2183 $message .= $um->get_errors();
5ddaa2e2 2184 return $um->get_new_filename();
7f6689e4 2185 }
db290a6e 2186 $message .= $um->get_errors();
7f6689e4 2187}
501cdbd8 2188
db290a6e 2189function forum_add_new_post($post,&$message) {
501cdbd8 2190
f37da850 2191 global $USER, $CFG;
e3ff14ca 2192
ffe11640 2193 $post->created = $post->modified = time();
501cdbd8 2194 $post->mailed = "0";
a56f0d60 2195 $post->userid = $USER->id;
7f6689e4 2196 $post->attachment = "";
2197
65b0e537 2198 if (! $post->id = insert_record("forum_posts", $post)) {
7f6689e4 2199 return false;
2200 }
2201
db290a6e 2202 if ($post->attachment = forum_add_attachment($post, 'attachment',$message)) {
7f6689e4 2203 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id);
2204 }
29507631 2205
2206 // Update discussion modified date
2207 set_field("forum_discussions", "timemodified", $post->modified, "id", $post->discussion);
016cd6af 2208 set_field("forum_discussions", "usermodified", $post->userid, "id", $post->discussion);
65b0e537 2209
f37da850 2210 if ($CFG->forum_trackreadposts) {
2211 forum_tp_mark_post_read($post->userid, $post, $post->forum);
2212 }
2213
7f6689e4 2214 return $post->id;
501cdbd8 2215}
2216
db290a6e 2217function forum_update_post($post,&$message) {
501cdbd8 2218
f37da850 2219 global $USER, $CFG;
a56f0d60 2220
ffe11640 2221 $post->modified = time();
0936de64 2222
2223 if ($USER->id != $post->userid) { // Not the original author
2224 $data->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'.
2225 fullname($USER).'</a>';
2226 $data->date = userdate($post->modified);
2227 $editedby = get_string('editedby', 'forum', $data);
2228 if ($post->format == FORMAT_HTML) {
0d2c0cfd 2229 $post->message .= '<p>(<i><span class="edited">'.$editedby.'</span></i>)</p>';
0936de64 2230 } else {
2231 $post->message .= "\n\n(".$editedby.')';
2232 }
2233 }
501cdbd8 2234
0ab85112 2235 if (!$post->parent) { // Post is a discussion starter - update discussion title too
2236 set_field("forum_discussions", "name", $post->subject, "id", $post->discussion);
2237 }
29507631 2238
db290a6e 2239 if ($newfilename = forum_add_attachment($post, 'attachment',$message)) {
7f6689e4 2240 $post->attachment = $newfilename;
2241 } else {
2242 unset($post->attachment);
2243 }
29507631 2244
2245 // Update discussion modified date
2246 set_field("forum_discussions", "timemodified", $post->modified, "id", $post->discussion);
016cd6af 2247 set_field("forum_discussions", "usermodified", $post->userid, "id", $post->discussion);
29507631 2248
f37da850 2249 if ($CFG->forum_trackreadposts) {
2250 forum_tp_mark_post_read($post->userid, $post, $post->forum);
2251 }
2252
ffe11640 2253 return update_record("forum_posts", $post);
501cdbd8 2254}
2255
db290a6e 2256function forum_add_discussion($discussion,&$message) {
65b0e537 2257// Given an object containing all the necessary data,
501cdbd8 2258// create a new discussion and return the id
2259
f37da850 2260 GLOBAL $USER, $CFG;
501cdbd8 2261
2262 $timenow = time();
2263
65b0e537 2264 // The first post is stored as a real post, and linked
501cdbd8 2265 // to from the discuss entry.
2266
2267 $post->discussion = 0;
2268 $post->parent = 0;
ebc3bd2b 2269 $post->userid = $USER->id;
501cdbd8 2270 $post->created = $timenow;
2271 $post->modified = $timenow;
2272 $post->mailed = 0;
2273 $post->subject = $discussion->name;
2274 $post->message = $discussion->intro;
7f6689e4 2275 $post->attachment = "";
2276 $post->forum = $discussion->forum;
2277 $post->course = $discussion->course;
9f0b8269 2278 $post->format = $discussion->format;
501cdbd8 2279
2280 if (! $post->id = insert_record("forum_posts", $post) ) {
2281 return 0;
2282 }
2283
db290a6e 2284 if ($post->attachment = forum_add_attachment($post, 'attachment',$message)) {
7f6689e4 2285 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id); //ignore errors
2286 }
2287
65b0e537 2288 // Now do the main entry for the discussion,
02509fe6 2289 // linking to this first post
04eba58f 2290
caadf009 2291 $discussion->firstpost = $post->id;
2292 $discussion->timemodified = $timenow;
016cd6af 2293 $discussion->usermodified = $post->userid;
a56f0d60 2294 $discussion->userid = $USER->id;
04eba58f 2295
4e00a4d9 2296 if (! $post->discussion = insert_record("forum_discussions", $discussion) ) {
caadf009 2297 delete_records("forum_posts", "id", $post->id);
2298 return 0;
04eba58f 2299 }
2300
caadf009 2301 // Finally, set the pointer on the post.
4e00a4d9 2302 if (! set_field("forum_posts", "discussion", $post->discussion, "id", $post->id)) {
caadf009 2303 delete_records("forum_posts", "id", $post->id);
4e00a4d9 2304 delete_records("forum_discussions", "id", $post->discussion);
caadf009 2305 return 0;
04eba58f 2306 }
04eba58f 2307
f37da850 2308 if ($CFG->forum_trackreadposts) {
2309 forum_tp_mark_post_read($post->userid, $post, $post->forum);
2310 }
2311
4e00a4d9 2312 return $post->discussion;
caadf009 2313}
04eba58f 2314
04eba58f 2315
caadf009 2316function forum_delete_discussion($discussion) {
2317// $discussion is a discussion record object
04eba58f 2318
2319 $result = true;
2320
caadf009 2321 if ($posts = get_records("forum_posts", "discussion", $discussion->id)) {
2322 foreach ($posts as $post) {
2323 $post->course = $discussion->course;
2324 $post->forum = $discussion->forum;
2325 if (! delete_records("forum_ratings", "post", "$post->id")) {
2326 $result = false;
2327 }
2328 if (! forum_delete_post($post)) {
04eba58f 2329 $result = false;
2330 }
2331 }
2332 }
2333
f37da850 2334 forum_tp_delete_read_records(-1, -1, $discussion->id);
2335
caadf009 2336 if (! delete_records("forum_discussions", "id", "$discussion->id")) {
04eba58f 2337 $result = false;
2338 }
2339
2340 return $result;
2341}
2342
2343
b82faacd 2344function forum_delete_post($post, $children=false) {
2345 if ($children) {
2346 if ($childposts = get_records('forum_posts', 'parent', $post->id)) {
2347 foreach ($childposts as $childpost) {
2348 forum_delete_post($childpost, true);
2349 }
2350 }
2351 }
caadf009 2352 if (delete_records("forum_posts", "id", $post->id)) {
2353 delete_records("forum_ratings", "post", $post->id); // Just in case
f37da850 2354
b82faacd 2355 forum_tp_delete_read_records(-1, $post->id);
f37da850 2356
caadf009 2357 if ($post->attachment) {
2358 $discussion = get_record("forum_discussions", "id", $post->discussion);
2359 $post->course = $discussion->course;
2360 $post->forum = $discussion->forum;
2361 forum_delete_old_attachments($post);
2362 }
2363 return true;
2364 }
2365 return false;
2366}
501cdbd8 2367
b82faacd 2368function forum_count_replies($post, $children=true) {
2369 $count = 0;
2370
2371 if ($children) {
2372 if ($childposts = get_records('forum_posts', 'parent', $post->id)) {
2373 foreach ($childposts as $childpost) {
2374 $count ++; // For this child
2375 $count += forum_count_replies($childpost, true);
2376 }
2377 }
2378 } else {
2379 $count += count_records('forum_posts', 'parent', $post->id);
2380 }
2381
2382 return $count;
2383}
2384
2385
501cdbd8 2386function forum_forcesubscribe($forumid, $value=1) {
2387 return set_field("forum", "forcesubscribe", $value, "id", $forumid);
2388}
2389
2390function forum_is_forcesubscribed($forumid) {
709f0ec8 2391 return (get_field("forum", "forcesubscribe", "id", $forumid) == 1);
501cdbd8 2392}
2393
2394function forum_is_subscribed($userid, $forumid) {
2395 if (forum_is_forcesubscribed($forumid)) {
2396 return true;
2397 }
ebc3bd2b 2398 return record_exists("forum_subscriptions", "userid", $userid, "forum", $forumid);
86970225 2399}
2400
501cdbd8 2401function forum_subscribe($userid, $forumid) {
9fa49e22 2402/// Adds user to the subscriber list
2403
6673d7bd 2404 if (record_exists("forum_subscriptions", "userid", $userid, "forum", $forumid)) {
2405 return true;
2406 }
2407
ebc3bd2b 2408 $sub->userid = $userid;
9fa49e22 2409 $sub->forum = $forumid;
501cdbd8 2410
9fa49e22 2411 return insert_record("forum_subscriptions", $sub);
501cdbd8 2412}
2413
2414function forum_unsubscribe($userid, $forumid) {
9fa49e22 2415/// Removes user from the subscriber list
ebc3bd2b 2416 return delete_records("forum_subscriptions", "userid", $userid, "forum", $forumid);
501cdbd8 2417}
2418
0a9f61b5 2419function forum_post_subscription($post) {
2420/// Given a new post, subscribes or unsubscribes as appropriate.
2421/// Returns some text which describes what happened.
2422
2423 global $USER;
2424
2425 if (empty($post->subscribe) and empty($post->unsubscribe)) {
2426 return "";
2427 }
2428
2429 if (!$forum = get_record("forum", "id", $post->forum)) {
2430 return "";
2431 }
2432
067675c0 2433 $info->name = fullname($USER);
0a9f61b5 2434 $info->forum = $forum->name;
2435
2436 if (!empty($post->subscribe)) {
2437 forum_subscribe($USER->id, $post->forum);
2438 return "<p>".get_string("nowsubscribed", "forum", $info)."</p>";
2439 }
2440
2441 forum_unsubscribe($USER->id, $post->forum);
2442 return "<p>".get_string("nownotsubscribed", "forum", $info)."</p>";
2443}
2444
501cdbd8 2445
11b0c469 2446function forum_user_has_posted_discussion($forumid, $userid) {
29507631 2447 if ($discussions = forum_get_discussions($forumid, "", $userid)) {
501cdbd8 2448 return true;
2449 } else {
2450 return false;
2451 }
2452}
2453
9197e147 2454function forum_user_can_post_discussion($forum, $currentgroup=false) {
501cdbd8 2455// $forum is an object
2456 global $USER;
2457
2458 if ($forum->type == "eachuser") {
11b0c469 2459 return (! forum_user_has_posted_discussion($forum->id, $USER->id));
501cdbd8 2460 } else if ($forum->type == "teacher") {
2461 return isteacher($forum->course);
9197e147 2462 } else if ($currentgroup) {
d591b40f 2463 return (isteacheredit($forum->course) or (ismember($currentgroup) and $forum->open == 2));
501cdbd8 2464 } else if (isteacher($forum->course)) {
2465 return true;
2466 } else {
70c476a7 2467 return ($forum->open == 2);
501cdbd8 2468 }
2469}
2470
f690562f 2471function forum_user_can_post($forum, $user=NULL) {
2472// $forum, $user are objects
2473
2474 if ($user) {
2475 $isteacher = isteacher($forum->course, $user->id);
2476 } else {
2477 $isteacher = isteacher($forum->course);
2478 }
70c476a7 2479
2480 if ($forum->type == "teacher") {
f690562f 2481 return $isteacher;
2482 } else if ($isteacher) {
70c476a7 2483 return true;
2484 } else {
2485 return $forum->open;
2486 }
2487}
501cdbd8 2488
501cdbd8 2489
af328e4b 2490function forum_print_latest_discussions($course, $forum, $maxdiscussions=5, $displayformat='plain', $sort='',
2491 $currentgroup=-1, $groupmode=-1, $page=-1) {
c585fa17 2492 global $CFG, $USER;
65b0e537 2493
f93f848a 2494
af328e4b 2495/// Sort out some defaults
2496
2497 if ((!$maxdiscussions) && ($displayformat == 'plain')) {
2498 $displayformat = 'header'; // Abbreviate display by default
2499 }
2500
2501 $fullpost = false;
2502 if ($displayformat == 'plain') {
2503 $fullpost = true;
f93f848a 2504 }
2505
af328e4b 2506
2507/// Decide if current user is allowed to see ALL the current discussions or not
2508
2509/// First check the group stuff
2510
2862b309 2511 if ($groupmode == -1) { /// We need to reconstruct groupmode because none was given
d5bbc556 2512 if ($cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) {
2862b309 2513 $groupmode = groupmode($course, $cm);
2514 } else {
2515 $groupmode = SEPARATEGROUPS;
2516 }
2517 }
8f0cd6ef 2518
af328e4b 2519 if ($currentgroup == -1) { /// We need to reconstruct currentgroup because none was given
2520 $currentgroup = get_current_group($course->id);
2521 }
2522
2523 if (!$currentgroup and ($groupmode != SEPARATEGROUPS or isteacheredit($course->id)) ) {
2524 $visiblegroups = -1;
2525 } else {
2526 $visiblegroups = $currentgroup;
2527 }
2528
2529/// If the user can post discussions, then this is a good place to put the button for it
2862b309 2530
9197e147 2531 if (forum_user_can_post_discussion($forum, $currentgroup)) {
af328e4b 2532 echo '<div class="singlebutton forumaddnew">';
49aa3742 2533 echo "<form name=\"newdiscussionform\" method=\"get\" action=\"$CFG->wwwroot/mod/forum/post.php\">";
2534 echo "<input type=\"hidden\" name=\"forum\" value=\"$forum->id\" />";
2535 echo '<input type="submit" value="';
d5bbc556 2536 echo ($forum->type == 'news') ? get_string('addanewtopic', 'forum') : get_string('addanewdiscussion', 'forum');
49aa3742 2537 echo '" />';
2538 echo '</form>';
3479c82f 2539 echo "</div>\n";
77305fe6 2540 }
2541
2862b309 2542
2543/// Get all the recent discussions we're allowed to see
2544
096de9ad 2545 $getuserlastmodified = ($displayformat == 'header');
2546
2547 if (! $discussions = forum_get_discussions($forum->id, $sort, 0, $fullpost, $visiblegroups,0,$getuserlastmodified) ) {
af328e4b 2548 echo '<div class="forumnodiscuss">';
d5bbc556 2549 if ($forum->type == 'news') {
2550 echo '('.get_string('nonews', 'forum').')';
0351b1f9 2551 } else {
d5bbc556 2552 echo '('.get_string('nodiscussions', 'forum').')';
0351b1f9 2553 }
f13075f1 2554 echo "</div>\n";
2ab968e9 2555 return;
2556 }
65b0e537 2557
af328e4b 2558/// If no discussions then don't use paging (to avoid some divide by 0 errors)
2559
2560 if ($maxdiscussions <= 0) {
f6bb7eaa 2561 $page = -1;
af328e4b 2562 $maxdiscussions = 0;
f6bb7eaa 2563 }
2564
af328e4b 2565/// If we want paging
2566
f6bb7eaa 2567 if ($page != -1) {
2568 ///Get the number of discussions found
2569 $numdiscussions = count($discussions);
2570
2571 ///Show the paging bar
af328e4b 2572 print_paging_bar($numdiscussions, $page, $maxdiscussions, "view.php?f=$forum->id&");
f6bb7eaa 2573
2574 //Calculate the page "window"
af328e4b 2575 $pagestart = ($page * $maxdiscussions) + 1;
2576 $pageend = $pagestart + $maxdiscussions - 1;
f6bb7eaa 2577 }
2578
af328e4b 2579
3335f6fb 2580 $replies = forum_count_discussion_replies($forum->id);
f93f848a 2581
3260de67 2582 $canreply = forum_user_can_post($forum);
2583
af328e4b 2584
3335f6fb 2585 $discussioncount = 0;
c20b762a 2586 $olddiscussionlink = false;
f51e8d7e 2587 $strdatestring = get_string('strftimerecentfull');
f93f848a 2588
1d2256d0 2589 /// Check if the forum is tracked.
2590 if ($CFG->forum_trackreadposts) {
2591 $forumtracked = forum_tp_is_tracked($forum->id, $USER->id);
2592 } else {
2593 $forumtracked = false;
2594 }
2595
af328e4b 2596 if ($displayformat == 'header') {
5c8d1866 2597 echo '<table cellspacing="0" class="forumheaderlist">';
f51e8d7e 2598 echo '<thead>';
2599 echo '<tr>';
7ff9860d 2600 echo '<th class="header topic">'.get_string('discussion', 'forum').'</th>';
2601 echo '<th class="header author" colspan="2">'.get_string('startedby', 'forum').'</th>';
3a68fbbb 2602 if ($groupmode > 0) {
7ff9860d 2603 echo '<th class="header group">'.get_string('group').'</th>';
3a68fbbb 2604 }
f51e8d7e 2605 if ($forum->open or $forum->type == 'teacher') {
7ff9860d 2606 echo '<th class="header replies">'.get_string('replies', 'forum').'</th>';
47d06301 2607 if ($CFG->forum_trackreadposts) {
2608 echo '<th class="header replies">'.get_string('unread', 'forum').'</th>';
2609 }
f37da850 2610 }
7ff9860d 2611 echo '<th class="header lastpost">'.get_string('lastpost', 'forum').'</th>';
f51e8d7e 2612 echo '</tr>';
2613 echo '</thead>';
2614 echo '<tbody>';
29507631 2615 }
2616
3335f6fb 2617 foreach ($discussions as $discussion) {
2618 $discussioncount++;
f93f848a 2619
af328e4b 2620 if ($page != -1) { // We are using paging
2621 if ($discussioncount < $pagestart) { // Not there yet
f6bb7eaa 2622 continue;
2623 }
af328e4b 2624 if ($discussioncount > $pageend) { // All done, finish the loop
2625 break;
2626 }
f6bb7eaa 2627 //Without paging, old approach
af328e4b 2628 } else if ($maxdiscussions && ($discussioncount > $maxdiscussions)) {
c20b762a 2629 $olddiscussionlink = true;
3335f6fb 2630 break;
2631 }
e3ff14ca 2632
9c9f7d77 2633 if (!empty($replies[$discussion->discussion])) {
3335f6fb 2634 $discussion->replies = $replies[$discussion->discussion]->replies;
3599e487 2635 $discussion->lastpostid = $replies[$discussion->discussion]->lastpostid;
3335f6fb 2636 } else {
2637 $discussion->replies = 0;
2638 }
af328e4b 2639
f37da850 2640 if ($CFG->forum_trackreadposts) {
489de4ae 2641 /// SPECIAL CASE: The front page can display a news item post to non-logged in users.
2642 /// All posts are read in this case.
1d2256d0 2643 if (!$forumtracked) {
2644 $discussion->unread = 'n/a';
2645 } else if (empty($USER)) {
489de4ae 2646 $discussion->unread = 0;
2647 } else {
1051a2a4 2648 $discussion->unread = forum_tp_count_discussion_unread_posts($USER->id, $discussion->discussion);
489de4ae 2649 }
f37da850 2650 }
2651
f7477444 2652 if (!empty($USER->id)) {
2653 $ownpost = ($discussion->userid == $USER->id);
2654 } else {
2655 $ownpost=false;
2656 }
cf84431b 2657 // Use discussion name instead of subject of first post
2658 $discussion->subject = $discussion->name;
2659
af328e4b 2660 switch ($displayformat) {
2661 case 'header':
3a68fbbb 2662 if ($groupmode > 0) {
2663 if (isset($groups[$discussion->groupid])) {
2664 $group = $groups[$discussion->groupid];
2665 } else {
2666 $group = $groups[$discussion->groupid] = get_record('groups', 'id', $discussion->groupid);
2667 }
2668 } else {
2669 $group = -1;
2670 }
1d2256d0 2671 forum_print_discussion_header($discussion, $forum, $group, $strdatestring, $forumtracked);
3335f6fb 2672 break;
2673 default:
3260de67 2674 if ($canreply or $discussion->replies) {
2675 $link = true;
2676 } else {
2677 $link = false;
2678 }
f37da850 2679
2680 /// Need to add in the forum id for forum_print_post.
af328e4b 2681 $discussion->forum = $forum->id;
f37da850 2682
af328e4b 2683 forum_print_post($discussion, $course->id, $ownpost, $reply=0, $link, $assessed=false);
3335f6fb 2684 break;
f93f848a 2685 }
2686 }
29507631 2687
af328e4b 2688 if ($displayformat == "header") {
f51e8d7e 2689 echo '</tbody>';
2690 echo '</table>';
29507631 2691 }
c20b762a 2692
2693 if ($olddiscussionlink) {
af328e4b 2694 echo '<div class="forumolddiscuss">';
2695 echo '<a href="'.$CFG->wwwroot.'/mod/forum/view.php?f='.$forum->id.'&amp;showall=1">';
2696 echo get_string('olderdiscussions', 'forum').'</a> ...</div>';
c20b762a 2697 }
f6bb7eaa 2698
c8680d86 2699 if ($page != -1) { ///Show the paging bar
af328e4b 2700 print_paging_bar($numdiscussions, $page, $maxdiscussions, "view.php?f=$forum->id&");
f6bb7eaa 2701 }
f93f848a 2702}
2703
af328e4b 2704
c6d691dc 2705function forum_print_discussion($course, $forum, $discussion, $post, $mode, $canreply=NULL) {
501cdbd8 2706
f37da850 2707 global $USER, $CFG;
501cdbd8 2708
61e96406 2709 if (!empty($USER->id)) {
2710 $ownpost = ($USER->id == $post->userid);
2711 } else {
2712 $ownpost = false;
2713 }
c6d691dc 2714 if ($canreply === NULL) {
2715 $reply = forum_user_can_post($forum);
2716 } else {
2717 $reply = $canreply;
2718 }
501cdbd8 2719
02ebf404 2720 $ratings = NULL;
74f5d1e3 2721 $ratingsmenuused = false;
61e96406 2722 if ($forum->assessed and !empty($USER->id)) {
d6bdd9d5 2723 if ($ratings->scale = make_grades_menu($forum->scale)) {
3bd98ad4 2724 $ratings->assesspublic = $forum->assesspublic;
98914efd 2725 $ratings->assesstimestart = $forum->assesstimestart;
2726 $ratings->assesstimefinish = $forum->assesstimefinish;
0efb3c3d 2727 $ratings->allow = (($forum->assessed != 2 or isteacher($course->id)) && !isguest());
3bd98ad4 2728
0efb3c3d 2729 if ($ratings->allow) {
2730 echo '<form name="form" method="post" action="rate.php">';
2731 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
2732 }
9d1b97c5 2733 }
2734 }
2735
f37da850 2736/// Add the forum id to the post object - used by read tracking.
2737 $post->forum = $forum->id;
2738
c78ac798 2739 $post->subject = format_string($post->subject);
2740
143b43cb 2741 $forumtracked = forum_tp_is_tracked($forum->id, $USER->id);
2742 if ($CFG->forum_trackreadposts && $forumtracked) {
f37da850 2743 $user_read_array = forum_tp_get_discussion_read_records($USER->id, $post->discussion);
2744 } else {
2745 $user_read_array = array();
2746 }
2747
2748 if (forum_print_post($post, $course->id, $ownpost, $reply, $link=false, $ratings,
143b43cb 2749 '', '', (!$forumtracked || isset($user_read_array[$post->id]) || forum_tp_is_post_old($post)))) {
74f5d1e3 2750 $ratingsmenuused = true;
2751 }
501cdbd8 2752
2753 switch ($mode) {
2e2e71a8 2754 case FORUM_MODE_FLATOLDEST :
2755 case FORUM_MODE_FLATNEWEST :
65b0e537 2756 default:
f37da850 2757 if (forum_print_posts_flat($post->discussion, $course->id, $mode, $ratings, $reply,
2758 $user_read_array, $post->forum)) {
74f5d1e3 2759 $ratingsmenuused = true;
2760 }
501cdbd8 2761 break;
2762
2e2e71a8 2763 case FORUM_MODE_THREADED :
f37da850 2764 if (forum_print_posts_threaded($post->id, $course->id, 0, $ratings, $reply,
2765 $user_read_array, $post->forum)) {
74f5d1e3 2766 $ratingsmenuused = true;
2767 }
501cdbd8 2768 break;
2769
2e2e71a8 2770 case FORUM_MODE_NESTED :
f37da850 2771 if (forum_print_posts_nested($post->id, $course->id, $ratings, $reply,
2772 $user_read_array, $post->forum)) {
74f5d1e3 2773 $ratingsmenuused = true;
2774 }
501cdbd8 2775 break;
2776 }
2777
74f5d1e3 2778 if ($ratingsmenuused) {
b4510d53 2779 echo "<center><input type=\"submit\" value=\"".get_string("sendinratings", "forum")."\" />";
fa01ae8b 2780 if ($forum->scale < 0) {
2781 if ($scale = get_record("scale", "id", abs($forum->scale))) {
2782 print_scale_menu_helpbutton($course->id, $scale );
2783 }
2784 }
02ebf404 2785 echo "</center>";
2786 echo "</form>";
501cdbd8 2787 }
2788}
2789
f37da850 2790/// Add the forum id to the argument list, for use in 'forum_print_post'.
2791/// Add the user_read_array to the argument list.
83a305a7 2792function forum_print_posts_flat($discussion, $courseid, $direction, $ratings, $reply, &$user_read_array, $forumid=0) {
f37da850 2793 global $USER, $CFG;
501cdbd8 2794
501cdbd8 2795 $link = false;
74f5d1e3 2796 $ratingsmenuused = false;
501cdbd8 2797
2798 if ($direction < 0) {
2799 $sort = "ORDER BY created DESC";
2800 } else {
2801 $sort = "ORDER BY created ASC";
2802 }
2803
1f48942e 2804 if ($posts = forum_get_discussion_posts($discussion, $sort)) {
501cdbd8 2805 foreach ($posts as $post) {
f37da850 2806
2807 /// Add the forum id to the post object - used by read tracking.
2808 if (!$CFG->forum_usermarksread) {
2809 $post->forum = $forumid;
2810 } else {
2811 $post->forum = 0;
2812 }
2813
c78ac798 2814 $post->subject = format_string($post->subject);
2815
ebc3bd2b 2816 $ownpost = ($USER->id == $post->userid);
83a305a7 2817 if (forum_print_post($post, $courseid, $ownpost, $reply, $link, $ratings,
f37da850 2818 '', '', (isset($user_read_array[$post->id]) || forum_tp_is_post_old($post)))) {
74f5d1e3 2819 $ratingsmenuused = true;
2820 }
501cdbd8 2821 }
501cdbd8 2822 }
74f5d1e3 2823
2824 return $ratingsmenuused;
501cdbd8 2825}
2826
f37da850 2827/// Add the forum id to the argument list, for use in 'forum_print_post'.
2828/// Add the user_read_array to the argument list.
83a305a7 2829function forum_print_posts_threaded($parent, $courseid, $depth, $ratings, $reply, &$user_read_array, $forumid=0) {
f37da850 2830 global $USER, $CFG;
501cdbd8 2831
501cdbd8 2832 $link = false;
74f5d1e3 2833 $ratingsmenuused = false;
501cdbd8 2834
1f48942e 2835 if ($posts = forum_get_child_posts($parent)) {
501cdbd8 2836 foreach ($posts as $post) {
2837
7105cf8d 2838 echo '<div class="indent">';
501cdbd8 2839 if ($depth > 0) {
ebc3bd2b 2840 $ownpost = ($USER->id == $post->userid);
f37da850 2841
2842 if (!$CFG->forum_usermarksread) {
2843 $post->forum = $forumid;
2844 } else {
2845 $post->forum = 0;
2846 }
2847
c78ac798 2848 $post->subject = format_string($post->subject);
2849
83a305a7 2850 if (forum_print_post($post, $courseid, $ownpost, $reply, $link, $ratings,
f37da850 2851 '', '', (isset($user_read_array[$post->id]) || forum_tp_is_post_old($post)))) {
74f5d1e3 2852 $ratingsmenuused = true;
2853 }
501cdbd8 2854 } else {
83a305a7 2855 $by->name = fullname($post, isteacher($courseid));
d62413e8 2856 $by->date = userdate($post->modified);
f37da850 2857
2858 if ($CFG->forum_trackreadposts) {
2859 if (isset($user_read_array[$post->id]) || forum_tp_is_post_old($post)) {
2860 $style = '<span class="forumthread read">';
2861 } else {
2862 $style = '<span class="forumthread unread">';
2863 }
2864 } else {
2865 $style = '<span class="forumthread">';
2866 }
2867 echo $style."<a name=\"$post->id\"></a>".
17dc3f3c 2868 "<a href=\"discuss.php?d=$post->discussion&amp;parent=$post->id\">".format_string($post->subject,true)."</a> ";
8c3c8481 2869 print_string("bynameondate", "forum", $by);
f37da850 2870 echo "</span>";
501cdbd8 2871 }
2872
83a305a7 2873 if (forum_print_posts_threaded($post->id, $courseid, $depth-1, $ratings, $reply,
f37da850 2874 $user_read_array, $forumid)) {
74f5d1e3 2875 $ratingsmenuused = true;
2876 }
bb3b56c6 2877 echo "</div>\n";
501cdbd8 2878 }
501cdbd8 2879 }
74f5d1e3 2880 return $ratingsmenuused;
501cdbd8 2881}
2882
f37da850 2883/// Add the forum id to the argument list, for use in 'forum_print_post'.
2884/// Add the user_read_array to the argument list.
83a305a7 2885function forum_print_posts_nested($parent, $courseid, $ratings, $reply, &$user_read_array, $forumid=0) {
f37da850 2886 global $USER, $CFG;
501cdbd8 2887
501cdbd8 2888 $link = false;
74f5d1e3 2889 $ratingsmenuused = false;
501cdbd8 2890
1f48942e 2891 if ($posts = forum_get_child_posts($parent)) {
501cdbd8 2892 foreach ($posts as $post) {
2893
7105cf8d 2894 echo '<div class="indent">';
61e96406 2895 if (empty($USER->id)) {
2896 $ownpost = false;
2897 } else {
2898 $ownpost = ($USER->id == $post->userid);
2899 }
501cdbd8 2900
f37da850 2901 /// Add the forum id to the post object - used by read tracking.
2902 if (!$CFG->forum_usermarksread) {
2903 $post->forum = $forumid;
2904 } else {
2905 $post->forum = 0;
2906 }
c78ac798 2907
2908 $post->subject = format_string($post->subject);
f37da850 2909
83a305a7 2910 if (forum_print_post($post, $courseid, $ownpost, $reply, $link, $ratings,
f37da850 2911 '', '', (isset($user_read_array[$post->id]) || forum_tp_is_post_old($post)))) {
74f5d1e3 2912 $ratingsmenuused = true;
2913 }
83a305a7 2914 if (forum_print_posts_nested($post->id, $courseid, $ratings, $reply, $user_read_array, $forumid)) {
74f5d1e3 2915 $ratingsmenuused = true;
2916 }
bb3b56c6 2917 echo "</div>\n";
501cdbd8 2918 }
501cdbd8 2919 }
74f5d1e3 2920 return $ratingsmenuused;
501cdbd8 2921}
2922
2862b309 2923function forum_get_recent_mod_activity(&$activities, &$index, $sincetime, $courseid, $cmid="0", $user="", $groupid="") {
a57a7794 2924// Returns all forum posts since a given time. If forum is specified then
2925// this restricts the results
2926
2927 global $CFG;
2928
2862b309 2929 if ($cmid) {
2930 $forumselect = " AND cm.id = '$cmid'";
a57a7794 2931 } else {
2932 $forumselect = "";
2933 }
2934
2935 if ($user) {
2936 $userselect = " AND u.id = '$user'";
2937 } else {
2938 $userselect = "";
2939 }
2940
a57a7794 2941 $posts = get_records_sql("SELECT p.*, d.name, u.firstname, u.lastname,
2942 u.picture, d.groupid, cm.instance, f.name, cm.section
2943 FROM {$CFG->prefix}forum_posts p,
2944 {$CFG->prefix}forum_discussions d,
2945 {$CFG->prefix}user u,
2946 {$CFG->prefix}course_modules cm,
2947 {$CFG->prefix}forum f
2948 WHERE p.modified > '$sincetime' $forumselect
2949 AND p.userid = u.id $userselect
2950 AND d.course = '$courseid'
8f0cd6ef 2951 AND p.discussion = d.id
a57a7794 2952 AND cm.instance = f.id
2953 AND cm.course = d.course
2954 AND cm.course = f.course
2955 AND f.id = d.forum
5b257acd 2956 ORDER BY p.discussion ASC,p.created ASC");
a57a7794 2957
2862b309 2958 if (empty($posts)) {
2959 return;
2960 }
2961
2962 $isteacheredit = isteacheredit($courseid);
a57a7794 2963
90708fc1 2964 foreach ($posts as $post) {
a57a7794 2965
2862b309 2966 if ($groupid and ($post->groupid != -1 and $groupid != $post->groupid and !$isteacheredit)) {
2967 continue;
2968 }
a57a7794 2969
ac21ad39 2970 $tmpactivity = new Object;
2971
2862b309 2972 $tmpactivity->type = "forum";
2973 $tmpactivity->defaultindex = $index;
2974 $tmpactivity->instance = $post->instance;
2975 $tmpactivity->name = $post->name;
2976 $tmpactivity->section = $post->section;
65b0e537 2977
2862b309 2978 $tmpactivity->content->id = $post->id;
2979 $tmpactivity->content->discussion = $post->discussion;
2980 $tmpactivity->content->subject = $post->subject;
2981 $tmpactivity->content->parent = $post->parent;
a57a7794 2982
2862b309 2983 $tmpactivity->user->userid = $post->userid;
2984 $tmpactivity->user->fullname = fullname($post);
2985 $tmpactivity->user->picture = $post->picture;
65b0e537 2986
2862b309 2987 $tmpactivity->timestamp = $post->modified;
2988 $activities[] = $tmpactivity;
2989
2990 $index++;
90708fc1 2991 }
a57a7794 2992
90708fc1 2993 return;
a57a7794 2994}
2995
2996function forum_print_recent_mod_activity($activity, $course, $detail=false) {
2997
d30867b0 2998 global $CFG;
a57a7794 2999
3000 echo '<table border="0" cellpadding="3" cellspacing="0">';
3001
3002 if ($activity->content->parent) {
3003 $openformat = "<font size=\"2\"><i>";
3004 $closeformat = "</i></font>";
3005 } else {
3006 $openformat = "<b>";
3007 $closeformat = "</b>";
3008 }
3009
d30867b0 3010 echo "<tr><td class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
a57a7794 3011 print_user_picture($activity->user->userid, $course, $activity->user->picture);
3012 echo "</td><td>$openformat";
3013
3014 if ($detail) {
3015 echo "<img src=\"$CFG->modpixpath/$activity->type/icon.gif\" ".
95c67e2f 3016 "height=\"16\" width=\"16\" alt=\"".strip_tags(format_string($activity->name,true))."\" /> ";
a57a7794 3017 }
3018 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=" . $activity->content->discussion
3019 . "#" . $activity->content->id . "\">";
3020
95c67e2f 3021 echo format_string($activity->content->subject,true);
a57a7794 3022 echo "</a>$closeformat";
3023
a9ef4a63 3024 echo "<br /><font size=\"2\">";
839f2456 3025 echo "<a href=\"$CFG->wwwroot/user/view.php?id=" . $activity->user->userid . "&amp;course=" . "$course\">"
a57a7794 3026 . $activity->user->fullname . "</a>";
3027 echo " - " . userdate($activity->timestamp) . "</font></td></tr>";
3028 echo "</table>";
3029
3030 return;
3031}
3032
cf84431b 3033function forum_change_discussionid($postid, $discussionid) {
3034/// recursively sets the discussion field to $discussionid on $postid and all its children
3035/// used when pruning a post
3036 set_field('forum_posts', 'discussion', $discussionid, 'id', $postid);
3037 if ($posts = get_records('forum_posts', 'parent', $postid)) {
3038 foreach ($posts as $post) {
3039 forum_change_discussionid($post->id, $discussionid);
3040 }
3041 }
3042 return true;
3043}
3044
90e7063e 3045function forum_update_subscriptions_button($courseid, $forumid) {
3046// Prints the editing button on subscribers page
3047 global $CFG, $USER;
3048
f88803e0 3049 if (isteacher($courseid)) {
90e7063e 3050 if (!empty($USER->subscriptionsediting)) {
3051 $string = get_string("turneditingoff");
3052 $edit = "off";
3053 } else {
3054 $string = get_string("turneditingon");
3055 $edit = "on";
3056 }
3057 return "<form target=\"$CFG->framename\" method=\"get\" action=\"$CFG->wwwroot/mod/forum/subscribers.php\">".
3058 "<input type=\"hidden\" name=\"id\" value=\"$forumid\" />".
3059 "<input type=\"hidden\" name=\"edit\" value=\"$edit\" />".
3060 "<input type=\"submit\" value=\"$string\" /></form>";
3061 }
3062}
3063
709f0ec8 3064function forum_add_user($userid, $courseid) {
3065/// Add subscriptions for new users
906fef94 3066 if ($forums = get_records_select('forum', "course = '$courseid' AND forcesubscribe = '".FORUM_INITIALSUBSCRIBE."'")) {
709f0ec8 3067 foreach ($forums as $forum) {
3068 forum_subscribe($userid, $forum->id);
3069 }
3070 }
906fef94 3071 if ($forums = get_records_select('forum', "course = '".SITEID."' AND forcesubscribe = '".FORUM_INITIALSUBSCRIBE."'")) {
7b5944cd 3072 foreach ($forums as $forum) {
3073 forum_subscribe($userid, $forum->id);
3074 }
3075 }
709f0ec8 3076}
3077
38994d6f 3078/// Functions to do with read tracking.
3079
f37da850 3080function forum_tp_add_read_record($userid, $postid, $discussionid=-1, $forumid=-1) {
3081 if (($readrecord = forum_tp_get_read_records($userid, $postid)) === false) {
3082 /// New read record
3083 unset($readrecord);
3084 $readrecord->userid = $userid;
3085 $readrecord->postid = $postid;
3086 $readrecord->discussionid = $discussionid;
3087 $readrecord->forumid = $forumid;
3088 $readrecord->firstread = time();
3089 $readrecord->lastread = $readrecord->firstread;
90fd2b51 3090 return insert_record('forum_read', $readrecord, true);
4abef03b 3091
3092 } else {
f37da850 3093 /// Update read record
3094 $readrecord = reset($readrecord);
3095 $readrecord->lastread = time();
3096
4abef03b 3097 $update = NULL;
3098 $update->id = $readrecord->id;
3099 $update->lastread = $readrecord->lastread;
3100
f37da850 3101 /// This shouldn't happen, but just in case...
3102 if (!$readrecord->firstread) {
f37da850 3103 /// Update the 'firstread' field.
4abef03b 3104 $update->firstread = $readrecord->lastread;
f37da850 3105 }
3106 if ($discussionid > -1) {
3107 /// Update the 'discussionid' field.
4abef03b 3108 $update->discussionid = $discussionid;
f37da850 3109 }
3110 if ($forumid > -1) {
3111 /// Update the 'forumid' field.
cb9ef4a0 3112 $update->forumid = $forumid;
f37da850 3113 }
3114
4abef03b 3115 return update_record('forum_read', $update);
f37da850 3116 }
3117}
3118
3119function forum_tp_get_read_records($userid=-1, $postid=-1, $discussionid=-1, $forumid=-1) {
3120 /// Returns all records in the 'forum_read' table matching the passed keys, indexed
3121 /// by userid.
3122 $select = '';
3123 if ($userid > -1) {
3124 if ($select != '') $select .= ' AND ';
3125 $select .= 'userid = \''.$userid.'\'';
3126 }
3127 if ($postid > -1) {
3128 if ($select != '') $select .= ' AND ';
3129 $select .= 'postid = \''.$postid.'\'';
3130 }
3131 if ($discussionid > -1) {
3132 if ($select != '') $select .= ' AND ';
3133 $select .= 'discussionid = \''.$discussionid.'\'';
3134 }
3135 if ($forumid > -1) {
3136 if ($select != '') $select .= ' AND ';
3137 $select .= 'forumid = \''.$forumid.'\'';
3138 }
3139
3140 return get_records_select('forum_read', $select);
3141}
3142
3143function forum_tp_get_discussion_read_records($userid, $discussionid) {
3144 /// Returns all read records for the provided user and discussion, indexed by postid.
3145 $select = 'userid = \''.$userid.'\' AND discussionid = \''.$discussionid.'\'';
3146 $fields = 'postid, firstread, lastread';
3147 return get_records_select('forum_read', $select, '', $fields);
3148}
3149
3150