bcf3d0a8d538fc7c1b5d0e7781b851ac3b39f785
[moodle.git] / mod / forum / lib.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * @package    mod
19  * @subpackage forum
20  * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
21  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22  */
24 defined('MOODLE_INTERNAL') || die();
26 /** Include required files */
27 require_once($CFG->libdir.'/filelib.php');
28 require_once($CFG->libdir.'/eventslib.php');
29 require_once($CFG->dirroot.'/user/selector/lib.php');
30 require_once($CFG->dirroot.'/mod/forum/post_form.php');
32 /// CONSTANTS ///////////////////////////////////////////////////////////
34 define('FORUM_MODE_FLATOLDEST', 1);
35 define('FORUM_MODE_FLATNEWEST', -1);
36 define('FORUM_MODE_THREADED', 2);
37 define('FORUM_MODE_NESTED', 3);
39 define('FORUM_CHOOSESUBSCRIBE', 0);
40 define('FORUM_FORCESUBSCRIBE', 1);
41 define('FORUM_INITIALSUBSCRIBE', 2);
42 define('FORUM_DISALLOWSUBSCRIBE',3);
44 define('FORUM_TRACKING_OFF', 0);
45 define('FORUM_TRACKING_OPTIONAL', 1);
46 define('FORUM_TRACKING_ON', 2);
48 define('FORUM_MAILED_PENDING', 0);
49 define('FORUM_MAILED_SUCCESS', 1);
50 define('FORUM_MAILED_ERROR', 2);
52 if (!defined('FORUM_CRON_USER_CACHE')) {
53     /** Defines how many full user records are cached in forum cron. */
54     define('FORUM_CRON_USER_CACHE', 5000);
55 }
57 /// STANDARD FUNCTIONS ///////////////////////////////////////////////////////////
59 /**
60  * Given an object containing all the necessary data,
61  * (defined by the form in mod_form.php) this function
62  * will create a new instance and return the id number
63  * of the new instance.
64  *
65  * @param stdClass $forum add forum instance
66  * @param mod_forum_mod_form $mform
67  * @return int intance id
68  */
69 function forum_add_instance($forum, $mform = null) {
70     global $CFG, $DB;
72     $forum->timemodified = time();
74     if (empty($forum->assessed)) {
75         $forum->assessed = 0;
76     }
78     if (empty($forum->ratingtime) or empty($forum->assessed)) {
79         $forum->assesstimestart  = 0;
80         $forum->assesstimefinish = 0;
81     }
83     $forum->id = $DB->insert_record('forum', $forum);
84     $modcontext = context_module::instance($forum->coursemodule);
86     if ($forum->type == 'single') {  // Create related discussion.
87         $discussion = new stdClass();
88         $discussion->course        = $forum->course;
89         $discussion->forum         = $forum->id;
90         $discussion->name          = $forum->name;
91         $discussion->assessed      = $forum->assessed;
92         $discussion->message       = $forum->intro;
93         $discussion->messageformat = $forum->introformat;
94         $discussion->messagetrust  = trusttext_trusted(context_course::instance($forum->course));
95         $discussion->mailnow       = false;
96         $discussion->groupid       = -1;
98         $message = '';
100         $discussion->id = forum_add_discussion($discussion, null, $message);
102         if ($mform and $draftid = file_get_submitted_draft_itemid('introeditor')) {
103             // Ugly hack - we need to copy the files somehow.
104             $discussion = $DB->get_record('forum_discussions', array('id'=>$discussion->id), '*', MUST_EXIST);
105             $post = $DB->get_record('forum_posts', array('id'=>$discussion->firstpost), '*', MUST_EXIST);
107             $options = array('subdirs'=>true); // Use the same options as intro field!
108             $post->message = file_save_draft_area_files($draftid, $modcontext->id, 'mod_forum', 'post', $post->id, $options, $post->message);
109             $DB->set_field('forum_posts', 'message', $post->message, array('id'=>$post->id));
110         }
111     }
113     if ($forum->forcesubscribe == FORUM_INITIALSUBSCRIBE) {
114         $users = forum_get_potential_subscribers($modcontext, 0, 'u.id, u.email');
115         foreach ($users as $user) {
116             forum_subscribe($user->id, $forum->id);
117         }
118     }
120     forum_grade_item_update($forum);
122     return $forum->id;
126 /**
127  * Given an object containing all the necessary data,
128  * (defined by the form in mod_form.php) this function
129  * will update an existing instance with new data.
130  *
131  * @global object
132  * @param object $forum forum instance (with magic quotes)
133  * @return bool success
134  */
135 function forum_update_instance($forum, $mform) {
136     global $DB, $OUTPUT, $USER;
138     $forum->timemodified = time();
139     $forum->id           = $forum->instance;
141     if (empty($forum->assessed)) {
142         $forum->assessed = 0;
143     }
145     if (empty($forum->ratingtime) or empty($forum->assessed)) {
146         $forum->assesstimestart  = 0;
147         $forum->assesstimefinish = 0;
148     }
150     $oldforum = $DB->get_record('forum', array('id'=>$forum->id));
152     // MDL-3942 - if the aggregation type or scale (i.e. max grade) changes then recalculate the grades for the entire forum
153     // if  scale changes - do we need to recheck the ratings, if ratings higher than scale how do we want to respond?
154     // for count and sum aggregation types the grade we check to make sure they do not exceed the scale (i.e. max score) when calculating the grade
155     if (($oldforum->assessed<>$forum->assessed) or ($oldforum->scale<>$forum->scale)) {
156         forum_update_grades($forum); // recalculate grades for the forum
157     }
159     if ($forum->type == 'single') {  // Update related discussion and post.
160         $discussions = $DB->get_records('forum_discussions', array('forum'=>$forum->id), 'timemodified ASC');
161         if (!empty($discussions)) {
162             if (count($discussions) > 1) {
163                 echo $OUTPUT->notification(get_string('warnformorepost', 'forum'));
164             }
165             $discussion = array_pop($discussions);
166         } else {
167             // try to recover by creating initial discussion - MDL-16262
168             $discussion = new stdClass();
169             $discussion->course          = $forum->course;
170             $discussion->forum           = $forum->id;
171             $discussion->name            = $forum->name;
172             $discussion->assessed        = $forum->assessed;
173             $discussion->message         = $forum->intro;
174             $discussion->messageformat   = $forum->introformat;
175             $discussion->messagetrust    = true;
176             $discussion->mailnow         = false;
177             $discussion->groupid         = -1;
179             $message = '';
181             forum_add_discussion($discussion, null, $message);
183             if (! $discussion = $DB->get_record('forum_discussions', array('forum'=>$forum->id))) {
184                 print_error('cannotadd', 'forum');
185             }
186         }
187         if (! $post = $DB->get_record('forum_posts', array('id'=>$discussion->firstpost))) {
188             print_error('cannotfindfirstpost', 'forum');
189         }
191         $cm         = get_coursemodule_from_instance('forum', $forum->id);
192         $modcontext = context_module::instance($cm->id, MUST_EXIST);
194         $post = $DB->get_record('forum_posts', array('id'=>$discussion->firstpost), '*', MUST_EXIST);
195         $post->subject       = $forum->name;
196         $post->message       = $forum->intro;
197         $post->messageformat = $forum->introformat;
198         $post->messagetrust  = trusttext_trusted($modcontext);
199         $post->modified      = $forum->timemodified;
200         $post->userid        = $USER->id;    // MDL-18599, so that current teacher can take ownership of activities.
202         if ($mform and $draftid = file_get_submitted_draft_itemid('introeditor')) {
203             // Ugly hack - we need to copy the files somehow.
204             $options = array('subdirs'=>true); // Use the same options as intro field!
205             $post->message = file_save_draft_area_files($draftid, $modcontext->id, 'mod_forum', 'post', $post->id, $options, $post->message);
206         }
208         $DB->update_record('forum_posts', $post);
209         $discussion->name = $forum->name;
210         $DB->update_record('forum_discussions', $discussion);
211     }
213     $DB->update_record('forum', $forum);
215     $modcontext = context_module::instance($forum->coursemodule);
216     if (($forum->forcesubscribe == FORUM_INITIALSUBSCRIBE) && ($oldforum->forcesubscribe <> $forum->forcesubscribe)) {
217         $users = forum_get_potential_subscribers($modcontext, 0, 'u.id, u.email', '');
218         foreach ($users as $user) {
219             forum_subscribe($user->id, $forum->id);
220         }
221     }
223     forum_grade_item_update($forum);
225     return true;
229 /**
230  * Given an ID of an instance of this module,
231  * this function will permanently delete the instance
232  * and any data that depends on it.
233  *
234  * @global object
235  * @param int $id forum instance id
236  * @return bool success
237  */
238 function forum_delete_instance($id) {
239     global $DB;
241     if (!$forum = $DB->get_record('forum', array('id'=>$id))) {
242         return false;
243     }
244     if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) {
245         return false;
246     }
247     if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
248         return false;
249     }
251     $context = context_module::instance($cm->id);
253     // now get rid of all files
254     $fs = get_file_storage();
255     $fs->delete_area_files($context->id);
257     $result = true;
259     if ($discussions = $DB->get_records('forum_discussions', array('forum'=>$forum->id))) {
260         foreach ($discussions as $discussion) {
261             if (!forum_delete_discussion($discussion, true, $course, $cm, $forum)) {
262                 $result = false;
263             }
264         }
265     }
267     if (!$DB->delete_records('forum_subscriptions', array('forum'=>$forum->id))) {
268         $result = false;
269     }
271     forum_tp_delete_read_records(-1, -1, -1, $forum->id);
273     if (!$DB->delete_records('forum', array('id'=>$forum->id))) {
274         $result = false;
275     }
277     forum_grade_item_delete($forum);
279     return $result;
283 /**
284  * Indicates API features that the forum supports.
285  *
286  * @uses FEATURE_GROUPS
287  * @uses FEATURE_GROUPINGS
288  * @uses FEATURE_GROUPMEMBERSONLY
289  * @uses FEATURE_MOD_INTRO
290  * @uses FEATURE_COMPLETION_TRACKS_VIEWS
291  * @uses FEATURE_COMPLETION_HAS_RULES
292  * @uses FEATURE_GRADE_HAS_GRADE
293  * @uses FEATURE_GRADE_OUTCOMES
294  * @param string $feature
295  * @return mixed True if yes (some features may use other values)
296  */
297 function forum_supports($feature) {
298     switch($feature) {
299         case FEATURE_GROUPS:                  return true;
300         case FEATURE_GROUPINGS:               return true;
301         case FEATURE_GROUPMEMBERSONLY:        return true;
302         case FEATURE_MOD_INTRO:               return true;
303         case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
304         case FEATURE_COMPLETION_HAS_RULES:    return true;
305         case FEATURE_GRADE_HAS_GRADE:         return true;
306         case FEATURE_GRADE_OUTCOMES:          return true;
307         case FEATURE_RATE:                    return true;
308         case FEATURE_BACKUP_MOODLE2:          return true;
309         case FEATURE_SHOW_DESCRIPTION:        return true;
310         case FEATURE_PLAGIARISM:              return true;
312         default: return null;
313     }
317 /**
318  * Obtains the automatic completion state for this forum based on any conditions
319  * in forum settings.
320  *
321  * @global object
322  * @global object
323  * @param object $course Course
324  * @param object $cm Course-module
325  * @param int $userid User ID
326  * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
327  * @return bool True if completed, false if not. (If no conditions, then return
328  *   value depends on comparison type)
329  */
330 function forum_get_completion_state($course,$cm,$userid,$type) {
331     global $CFG,$DB;
333     // Get forum details
334     if (!($forum=$DB->get_record('forum',array('id'=>$cm->instance)))) {
335         throw new Exception("Can't find forum {$cm->instance}");
336     }
338     $result=$type; // Default return value
340     $postcountparams=array('userid'=>$userid,'forumid'=>$forum->id);
341     $postcountsql="
342 SELECT
343     COUNT(1)
344 FROM
345     {forum_posts} fp
346     INNER JOIN {forum_discussions} fd ON fp.discussion=fd.id
347 WHERE
348     fp.userid=:userid AND fd.forum=:forumid";
350     if ($forum->completiondiscussions) {
351         $value = $forum->completiondiscussions <=
352                  $DB->count_records('forum_discussions',array('forum'=>$forum->id,'userid'=>$userid));
353         if ($type == COMPLETION_AND) {
354             $result = $result && $value;
355         } else {
356             $result = $result || $value;
357         }
358     }
359     if ($forum->completionreplies) {
360         $value = $forum->completionreplies <=
361                  $DB->get_field_sql( $postcountsql.' AND fp.parent<>0',$postcountparams);
362         if ($type==COMPLETION_AND) {
363             $result = $result && $value;
364         } else {
365             $result = $result || $value;
366         }
367     }
368     if ($forum->completionposts) {
369         $value = $forum->completionposts <= $DB->get_field_sql($postcountsql,$postcountparams);
370         if ($type == COMPLETION_AND) {
371             $result = $result && $value;
372         } else {
373             $result = $result || $value;
374         }
375     }
377     return $result;
380 /**
381  * Create a message-id string to use in the custom headers of forum notification emails
382  *
383  * message-id is used by email clients to identify emails and to nest conversations
384  *
385  * @param int $postid The ID of the forum post we are notifying the user about
386  * @param int $usertoid The ID of the user being notified
387  * @param string $hostname The server's hostname
388  * @return string A unique message-id
389  */
390 function forum_get_email_message_id($postid, $usertoid, $hostname) {
391     return '<'.hash('sha256',$postid.'to'.$usertoid).'@'.$hostname.'>';
394 /**
395  * Removes properties from user record that are not necessary
396  * for sending post notifications.
397  * @param stdClass $user
398  * @return void, $user parameter is modified
399  */
400 function forum_cron_minimise_user_record(stdClass $user) {
402     // We store large amount of users in one huge array,
403     // make sure we do not store info there we do not actually need
404     // in mail generation code or messaging.
406     unset($user->institution);
407     unset($user->department);
408     unset($user->address);
409     unset($user->city);
410     unset($user->url);
411     unset($user->currentlogin);
412     unset($user->description);
413     unset($user->descriptionformat);
416 /**
417  * Function to be run periodically according to the moodle cron
418  * Finds all posts that have yet to be mailed out, and mails them
419  * out to all subscribers
420  *
421  * @global object
422  * @global object
423  * @global object
424  * @uses CONTEXT_MODULE
425  * @uses CONTEXT_COURSE
426  * @uses SITEID
427  * @uses FORMAT_PLAIN
428  * @return void
429  */
430 function forum_cron() {
431     global $CFG, $USER, $DB;
433     $site = get_site();
435     // All users that are subscribed to any post that needs sending,
436     // please increase $CFG->extramemorylimit on large sites that
437     // send notifications to a large number of users.
438     $users = array();
439     $userscount = 0; // Cached user counter - count($users) in PHP is horribly slow!!!
441     // status arrays
442     $mailcount  = array();
443     $errorcount = array();
445     // caches
446     $discussions     = array();
447     $forums          = array();
448     $courses         = array();
449     $coursemodules   = array();
450     $subscribedusers = array();
453     // Posts older than 2 days will not be mailed.  This is to avoid the problem where
454     // cron has not been running for a long time, and then suddenly people are flooded
455     // with mail from the past few weeks or months
456     $timenow   = time();
457     $endtime   = $timenow - $CFG->maxeditingtime;
458     $starttime = $endtime - 48 * 3600;   // Two days earlier
460     if ($posts = forum_get_unmailed_posts($starttime, $endtime, $timenow)) {
461         // Mark them all now as being mailed.  It's unlikely but possible there
462         // might be an error later so that a post is NOT actually mailed out,
463         // but since mail isn't crucial, we can accept this risk.  Doing it now
464         // prevents the risk of duplicated mails, which is a worse problem.
466         if (!forum_mark_old_posts_as_mailed($endtime)) {
467             mtrace('Errors occurred while trying to mark some posts as being mailed.');
468             return false;  // Don't continue trying to mail them, in case we are in a cron loop
469         }
471         // checking post validity, and adding users to loop through later
472         foreach ($posts as $pid => $post) {
474             $discussionid = $post->discussion;
475             if (!isset($discussions[$discussionid])) {
476                 if ($discussion = $DB->get_record('forum_discussions', array('id'=> $post->discussion))) {
477                     $discussions[$discussionid] = $discussion;
478                 } else {
479                     mtrace('Could not find discussion '.$discussionid);
480                     unset($posts[$pid]);
481                     continue;
482                 }
483             }
484             $forumid = $discussions[$discussionid]->forum;
485             if (!isset($forums[$forumid])) {
486                 if ($forum = $DB->get_record('forum', array('id' => $forumid))) {
487                     $forums[$forumid] = $forum;
488                 } else {
489                     mtrace('Could not find forum '.$forumid);
490                     unset($posts[$pid]);
491                     continue;
492                 }
493             }
494             $courseid = $forums[$forumid]->course;
495             if (!isset($courses[$courseid])) {
496                 if ($course = $DB->get_record('course', array('id' => $courseid))) {
497                     $courses[$courseid] = $course;
498                 } else {
499                     mtrace('Could not find course '.$courseid);
500                     unset($posts[$pid]);
501                     continue;
502                 }
503             }
504             if (!isset($coursemodules[$forumid])) {
505                 if ($cm = get_coursemodule_from_instance('forum', $forumid, $courseid)) {
506                     $coursemodules[$forumid] = $cm;
507                 } else {
508                     mtrace('Could not find course module for forum '.$forumid);
509                     unset($posts[$pid]);
510                     continue;
511                 }
512             }
515             // caching subscribed users of each forum
516             if (!isset($subscribedusers[$forumid])) {
517                 $modcontext = context_module::instance($coursemodules[$forumid]->id);
518                 if ($subusers = forum_subscribed_users($courses[$courseid], $forums[$forumid], 0, $modcontext, "u.*")) {
519                     foreach ($subusers as $postuser) {
520                         // this user is subscribed to this forum
521                         $subscribedusers[$forumid][$postuser->id] = $postuser->id;
522                         $userscount++;
523                         if ($userscount > FORUM_CRON_USER_CACHE) {
524                             // Store minimal user info.
525                             $minuser = new stdClass();
526                             $minuser->id = $postuser->id;
527                             $users[$postuser->id] = $minuser;
528                         } else {
529                             // Cache full user record.
530                             forum_cron_minimise_user_record($postuser);
531                             $users[$postuser->id] = $postuser;
532                         }
533                     }
534                     // Release memory.
535                     unset($subusers);
536                     unset($postuser);
537                 }
538             }
540             $mailcount[$pid] = 0;
541             $errorcount[$pid] = 0;
542         }
543     }
545     if ($users && $posts) {
547         $urlinfo = parse_url($CFG->wwwroot);
548         $hostname = $urlinfo['host'];
550         foreach ($users as $userto) {
552             @set_time_limit(120); // terminate if processing of any account takes longer than 2 minutes
554             mtrace('Processing user '.$userto->id);
556             // Init user caches - we keep the cache for one cycle only,
557             // otherwise it could consume too much memory.
558             if (isset($userto->username)) {
559                 $userto = clone($userto);
560             } else {
561                 $userto = $DB->get_record('user', array('id' => $userto->id));
562                 forum_cron_minimise_user_record($userto);
563             }
564             $userto->viewfullnames = array();
565             $userto->canpost       = array();
566             $userto->markposts     = array();
568             // set this so that the capabilities are cached, and environment matches receiving user
569             cron_setup_user($userto);
571             // reset the caches
572             foreach ($coursemodules as $forumid=>$unused) {
573                 $coursemodules[$forumid]->cache       = new stdClass();
574                 $coursemodules[$forumid]->cache->caps = array();
575                 unset($coursemodules[$forumid]->uservisible);
576             }
578             foreach ($posts as $pid => $post) {
580                 // Set up the environment for the post, discussion, forum, course
581                 $discussion = $discussions[$post->discussion];
582                 $forum      = $forums[$discussion->forum];
583                 $course     = $courses[$forum->course];
584                 $cm         =& $coursemodules[$forum->id];
586                 // Do some checks  to see if we can bail out now
587                 // Only active enrolled users are in the list of subscribers
588                 if (!isset($subscribedusers[$forum->id][$userto->id])) {
589                     continue; // user does not subscribe to this forum
590                 }
592                 // Don't send email if the forum is Q&A and the user has not posted
593                 // Initial topics are still mailed
594                 if ($forum->type == 'qanda' && !forum_get_user_posted_time($discussion->id, $userto->id) && $pid != $discussion->firstpost) {
595                     mtrace('Did not email '.$userto->id.' because user has not posted in discussion');
596                     continue;
597                 }
599                 // Get info about the sending user
600                 if (array_key_exists($post->userid, $users)) { // we might know him/her already
601                     $userfrom = $users[$post->userid];
602                     if (!isset($userfrom->idnumber)) {
603                         // Minimalised user info, fetch full record.
604                         $userfrom = $DB->get_record('user', array('id' => $userfrom->id));
605                         forum_cron_minimise_user_record($userfrom);
606                     }
608                 } else if ($userfrom = $DB->get_record('user', array('id' => $post->userid))) {
609                     forum_cron_minimise_user_record($userfrom);
610                     // Fetch only once if possible, we can add it to user list, it will be skipped anyway.
611                     if ($userscount <= FORUM_CRON_USER_CACHE) {
612                         $userscount++;
613                         $users[$userfrom->id] = $userfrom;
614                     }
616                 } else {
617                     mtrace('Could not find user '.$post->userid);
618                     continue;
619                 }
621                 //if we want to check that userto and userfrom are not the same person this is probably the spot to do it
623                 // setup global $COURSE properly - needed for roles and languages
624                 cron_setup_user($userto, $course);
626                 // Fill caches
627                 if (!isset($userto->viewfullnames[$forum->id])) {
628                     $modcontext = context_module::instance($cm->id);
629                     $userto->viewfullnames[$forum->id] = has_capability('moodle/site:viewfullnames', $modcontext);
630                 }
631                 if (!isset($userto->canpost[$discussion->id])) {
632                     $modcontext = context_module::instance($cm->id);
633                     $userto->canpost[$discussion->id] = forum_user_can_post($forum, $discussion, $userto, $cm, $course, $modcontext);
634                 }
635                 if (!isset($userfrom->groups[$forum->id])) {
636                     if (!isset($userfrom->groups)) {
637                         $userfrom->groups = array();
638                         if (isset($users[$userfrom->id])) {
639                             $users[$userfrom->id]->groups = array();
640                         }
641                     }
642                     $userfrom->groups[$forum->id] = groups_get_all_groups($course->id, $userfrom->id, $cm->groupingid);
643                     if (isset($users[$userfrom->id])) {
644                         $users[$userfrom->id]->groups[$forum->id] = $userfrom->groups[$forum->id];
645                     }
646                 }
648                 // Make sure groups allow this user to see this email
649                 if ($discussion->groupid > 0 and $groupmode = groups_get_activity_groupmode($cm, $course)) {   // Groups are being used
650                     if (!groups_group_exists($discussion->groupid)) { // Can't find group
651                         continue;                           // Be safe and don't send it to anyone
652                     }
654                     if (!groups_is_member($discussion->groupid) and !has_capability('moodle/site:accessallgroups', $modcontext)) {
655                         // do not send posts from other groups when in SEPARATEGROUPS or VISIBLEGROUPS
656                         continue;
657                     }
658                 }
660                 // Make sure we're allowed to see it...
661                 if (!forum_user_can_see_post($forum, $discussion, $post, NULL, $cm)) {
662                     mtrace('user '.$userto->id. ' can not see '.$post->id);
663                     continue;
664                 }
666                 // OK so we need to send the email.
668                 // Does the user want this post in a digest?  If so postpone it for now.
669                 if ($userto->maildigest > 0) {
670                     // This user wants the mails to be in digest form
671                     $queue = new stdClass();
672                     $queue->userid       = $userto->id;
673                     $queue->discussionid = $discussion->id;
674                     $queue->postid       = $post->id;
675                     $queue->timemodified = $post->created;
676                     $DB->insert_record('forum_queue', $queue);
677                     continue;
678                 }
681                 // Prepare to actually send the post now, and build up the content
683                 $cleanforumname = str_replace('"', "'", strip_tags(format_string($forum->name)));
685                 $userfrom->customheaders = array (  // Headers to make emails easier to track
686                            'Precedence: Bulk',
687                            'List-Id: "'.$cleanforumname.'" <moodleforum'.$forum->id.'@'.$hostname.'>',
688                            'List-Help: '.$CFG->wwwroot.'/mod/forum/view.php?f='.$forum->id,
689                            'Message-ID: '.forum_get_email_message_id($post->id, $userto->id, $hostname),
690                            'X-Course-Id: '.$course->id,
691                            'X-Course-Name: '.format_string($course->fullname, true)
692                 );
694                 if ($post->parent) {  // This post is a reply, so add headers for threading (see MDL-22551)
695                     $userfrom->customheaders[] = 'In-Reply-To: '.forum_get_email_message_id($post->parent, $userto->id, $hostname);
696                     $userfrom->customheaders[] = 'References: '.forum_get_email_message_id($post->parent, $userto->id, $hostname);
697                 }
699                 $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
701                 $postsubject = html_to_text("$shortname: ".format_string($post->subject, true));
702                 $posttext = forum_make_mail_text($course, $cm, $forum, $discussion, $post, $userfrom, $userto);
703                 $posthtml = forum_make_mail_html($course, $cm, $forum, $discussion, $post, $userfrom, $userto);
705                 // Send the post now!
707                 mtrace('Sending ', '');
709                 $eventdata = new stdClass();
710                 $eventdata->component        = 'mod_forum';
711                 $eventdata->name             = 'posts';
712                 $eventdata->userfrom         = $userfrom;
713                 $eventdata->userto           = $userto;
714                 $eventdata->subject          = $postsubject;
715                 $eventdata->fullmessage      = $posttext;
716                 $eventdata->fullmessageformat = FORMAT_PLAIN;
717                 $eventdata->fullmessagehtml  = $posthtml;
718                 $eventdata->notification = 1;
720                 // If forum_replytouser is not set then send mail using the noreplyaddress.
721                 if (empty($CFG->forum_replytouser)) {
722                     // Clone userfrom as it is referenced by $users.
723                     $cloneduserfrom = clone($userfrom);
724                     $cloneduserfrom->email = $CFG->noreplyaddress;
725                     $eventdata->userfrom = $cloneduserfrom;
726                 }
728                 $smallmessagestrings = new stdClass();
729                 $smallmessagestrings->user = fullname($userfrom);
730                 $smallmessagestrings->forumname = "$shortname: ".format_string($forum->name,true).": ".$discussion->name;
731                 $smallmessagestrings->message = $post->message;
732                 //make sure strings are in message recipients language
733                 $eventdata->smallmessage = get_string_manager()->get_string('smallmessage', 'forum', $smallmessagestrings, $userto->lang);
735                 $eventdata->contexturl = "{$CFG->wwwroot}/mod/forum/discuss.php?d={$discussion->id}#p{$post->id}";
736                 $eventdata->contexturlname = $discussion->name;
738                 $mailresult = message_send($eventdata);
739                 if (!$mailresult){
740                     mtrace("Error: mod/forum/lib.php forum_cron(): Could not send out mail for id $post->id to user $userto->id".
741                          " ($userto->email) .. not trying again.");
742                     add_to_log($course->id, 'forum', 'mail error', "discuss.php?d=$discussion->id#p$post->id",
743                                substr(format_string($post->subject,true),0,30), $cm->id, $userto->id);
744                     $errorcount[$post->id]++;
745                 } else {
746                     $mailcount[$post->id]++;
748                 // Mark post as read if forum_usermarksread is set off
749                     if (!$CFG->forum_usermarksread) {
750                         $userto->markposts[$post->id] = $post->id;
751                     }
752                 }
754                 mtrace('post '.$post->id. ': '.$post->subject);
755             }
757             // mark processed posts as read
758             forum_tp_mark_posts_read($userto, $userto->markposts);
759             unset($userto);
760         }
761     }
763     if ($posts) {
764         foreach ($posts as $post) {
765             mtrace($mailcount[$post->id]." users were sent post $post->id, '$post->subject'");
766             if ($errorcount[$post->id]) {
767                 $DB->set_field('forum_posts', 'mailed', FORUM_MAILED_ERROR, array('id' => $post->id));
768             }
769         }
770     }
772     // release some memory
773     unset($subscribedusers);
774     unset($mailcount);
775     unset($errorcount);
777     cron_setup_user();
779     $sitetimezone = $CFG->timezone;
781     // Now see if there are any digest mails waiting to be sent, and if we should send them
783     mtrace('Starting digest processing...');
785     @set_time_limit(300); // terminate if not able to fetch all digests in 5 minutes
787     if (!isset($CFG->digestmailtimelast)) {    // To catch the first time
788         set_config('digestmailtimelast', 0);
789     }
791     $timenow = time();
792     $digesttime = usergetmidnight($timenow, $sitetimezone) + ($CFG->digestmailtime * 3600);
794     // Delete any really old ones (normally there shouldn't be any)
795     $weekago = $timenow - (7 * 24 * 3600);
796     $DB->delete_records_select('forum_queue', "timemodified < ?", array($weekago));
797     mtrace ('Cleaned old digest records');
799     if ($CFG->digestmailtimelast < $digesttime and $timenow > $digesttime) {
801         mtrace('Sending forum digests: '.userdate($timenow, '', $sitetimezone));
803         $digestposts_rs = $DB->get_recordset_select('forum_queue', "timemodified < ?", array($digesttime));
805         if ($digestposts_rs->valid()) {
807             // We have work to do
808             $usermailcount = 0;
810             //caches - reuse the those filled before too
811             $discussionposts = array();
812             $userdiscussions = array();
814             foreach ($digestposts_rs as $digestpost) {
815                 if (!isset($posts[$digestpost->postid])) {
816                     if ($post = $DB->get_record('forum_posts', array('id' => $digestpost->postid))) {
817                         $posts[$digestpost->postid] = $post;
818                     } else {
819                         continue;
820                     }
821                 }
822                 $discussionid = $digestpost->discussionid;
823                 if (!isset($discussions[$discussionid])) {
824                     if ($discussion = $DB->get_record('forum_discussions', array('id' => $discussionid))) {
825                         $discussions[$discussionid] = $discussion;
826                     } else {
827                         continue;
828                     }
829                 }
830                 $forumid = $discussions[$discussionid]->forum;
831                 if (!isset($forums[$forumid])) {
832                     if ($forum = $DB->get_record('forum', array('id' => $forumid))) {
833                         $forums[$forumid] = $forum;
834                     } else {
835                         continue;
836                     }
837                 }
839                 $courseid = $forums[$forumid]->course;
840                 if (!isset($courses[$courseid])) {
841                     if ($course = $DB->get_record('course', array('id' => $courseid))) {
842                         $courses[$courseid] = $course;
843                     } else {
844                         continue;
845                     }
846                 }
848                 if (!isset($coursemodules[$forumid])) {
849                     if ($cm = get_coursemodule_from_instance('forum', $forumid, $courseid)) {
850                         $coursemodules[$forumid] = $cm;
851                     } else {
852                         continue;
853                     }
854                 }
855                 $userdiscussions[$digestpost->userid][$digestpost->discussionid] = $digestpost->discussionid;
856                 $discussionposts[$digestpost->discussionid][$digestpost->postid] = $digestpost->postid;
857             }
858             $digestposts_rs->close(); /// Finished iteration, let's close the resultset
860             // Data collected, start sending out emails to each user
861             foreach ($userdiscussions as $userid => $thesediscussions) {
863                 @set_time_limit(120); // terminate if processing of any account takes longer than 2 minutes
865                 cron_setup_user();
867                 mtrace(get_string('processingdigest', 'forum', $userid), '... ');
869                 // First of all delete all the queue entries for this user
870                 $DB->delete_records_select('forum_queue', "userid = ? AND timemodified < ?", array($userid, $digesttime));
872                 // Init user caches - we keep the cache for one cycle only,
873                 // otherwise it would unnecessarily consume memory.
874                 if (array_key_exists($userid, $users) and isset($users[$userid]->username)) {
875                     $userto = clone($users[$userid]);
876                 } else {
877                     $userto = $DB->get_record('user', array('id' => $userid));
878                     forum_cron_minimise_user_record($userto);
879                 }
880                 $userto->viewfullnames = array();
881                 $userto->canpost       = array();
882                 $userto->markposts     = array();
884                 // Override the language and timezone of the "current" user, so that
885                 // mail is customised for the receiver.
886                 cron_setup_user($userto);
888                 $postsubject = get_string('digestmailsubject', 'forum', format_string($site->shortname, true));
890                 $headerdata = new stdClass();
891                 $headerdata->sitename = format_string($site->fullname, true);
892                 $headerdata->userprefs = $CFG->wwwroot.'/user/edit.php?id='.$userid.'&amp;course='.$site->id;
894                 $posttext = get_string('digestmailheader', 'forum', $headerdata)."\n\n";
895                 $headerdata->userprefs = '<a target="_blank" href="'.$headerdata->userprefs.'">'.get_string('digestmailprefs', 'forum').'</a>';
897                 $posthtml = "<head>";
898 /*                foreach ($CFG->stylesheets as $stylesheet) {
899                     //TODO: MDL-21120
900                     $posthtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
901                 }*/
902                 $posthtml .= "</head>\n<body id=\"email\">\n";
903                 $posthtml .= '<p>'.get_string('digestmailheader', 'forum', $headerdata).'</p><br /><hr size="1" noshade="noshade" />';
905                 foreach ($thesediscussions as $discussionid) {
907                     @set_time_limit(120);   // to be reset for each post
909                     $discussion = $discussions[$discussionid];
910                     $forum      = $forums[$discussion->forum];
911                     $course     = $courses[$forum->course];
912                     $cm         = $coursemodules[$forum->id];
914                     //override language
915                     cron_setup_user($userto, $course);
917                     // Fill caches
918                     if (!isset($userto->viewfullnames[$forum->id])) {
919                         $modcontext = context_module::instance($cm->id);
920                         $userto->viewfullnames[$forum->id] = has_capability('moodle/site:viewfullnames', $modcontext);
921                     }
922                     if (!isset($userto->canpost[$discussion->id])) {
923                         $modcontext = context_module::instance($cm->id);
924                         $userto->canpost[$discussion->id] = forum_user_can_post($forum, $discussion, $userto, $cm, $course, $modcontext);
925                     }
927                     $strforums      = get_string('forums', 'forum');
928                     $canunsubscribe = ! forum_is_forcesubscribed($forum);
929                     $canreply       = $userto->canpost[$discussion->id];
930                     $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
932                     $posttext .= "\n \n";
933                     $posttext .= '=====================================================================';
934                     $posttext .= "\n \n";
935                     $posttext .= "$shortname -> $strforums -> ".format_string($forum->name,true);
936                     if ($discussion->name != $forum->name) {
937                         $posttext  .= " -> ".format_string($discussion->name,true);
938                     }
939                     $posttext .= "\n";
940                     $posttext .= $CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id;
941                     $posttext .= "\n";
943                     $posthtml .= "<p><font face=\"sans-serif\">".
944                     "<a target=\"_blank\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$shortname</a> -> ".
945                     "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</a> -> ".
946                     "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
947                     if ($discussion->name == $forum->name) {
948                         $posthtml .= "</font></p>";
949                     } else {
950                         $posthtml .= " -> <a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a></font></p>";
951                     }
952                     $posthtml .= '<p>';
954                     $postsarray = $discussionposts[$discussionid];
955                     sort($postsarray);
957                     foreach ($postsarray as $postid) {
958                         $post = $posts[$postid];
960                         if (array_key_exists($post->userid, $users)) { // we might know him/her already
961                             $userfrom = $users[$post->userid];
962                             if (!isset($userfrom->idnumber)) {
963                                 $userfrom = $DB->get_record('user', array('id' => $userfrom->id));
964                                 forum_cron_minimise_user_record($userfrom);
965                             }
967                         } else if ($userfrom = $DB->get_record('user', array('id' => $post->userid))) {
968                             forum_cron_minimise_user_record($userfrom);
969                             if ($userscount <= FORUM_CRON_USER_CACHE) {
970                                 $userscount++;
971                                 $users[$userfrom->id] = $userfrom;
972                             }
974                         } else {
975                             mtrace('Could not find user '.$post->userid);
976                             continue;
977                         }
979                         if (!isset($userfrom->groups[$forum->id])) {
980                             if (!isset($userfrom->groups)) {
981                                 $userfrom->groups = array();
982                                 if (isset($users[$userfrom->id])) {
983                                     $users[$userfrom->id]->groups = array();
984                                 }
985                             }
986                             $userfrom->groups[$forum->id] = groups_get_all_groups($course->id, $userfrom->id, $cm->groupingid);
987                             if (isset($users[$userfrom->id])) {
988                                 $users[$userfrom->id]->groups[$forum->id] = $userfrom->groups[$forum->id];
989                             }
990                         }
992                         $userfrom->customheaders = array ("Precedence: Bulk");
994                         if ($userto->maildigest == 2) {
995                             // Subjects and link only
996                             $posttext .= "\n";
997                             $posttext .= $CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id;
998                             $by = new stdClass();
999                             $by->name = fullname($userfrom);
1000                             $by->date = userdate($post->modified);
1001                             $posttext .= "\n".format_string($post->subject,true).' '.get_string("bynameondate", "forum", $by);
1002                             $posttext .= "\n---------------------------------------------------------------------";
1004                             $by->name = "<a target=\"_blank\" href=\"$CFG->wwwroot/user/view.php?id=$userfrom->id&amp;course=$course->id\">$by->name</a>";
1005                             $posthtml .= '<div><a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id.'#p'.$post->id.'">'.format_string($post->subject,true).'</a> '.get_string("bynameondate", "forum", $by).'</div>';
1007                         } else {
1008                             // The full treatment
1009                             $posttext .= forum_make_mail_text($course, $cm, $forum, $discussion, $post, $userfrom, $userto, true);
1010                             $posthtml .= forum_make_mail_post($course, $cm, $forum, $discussion, $post, $userfrom, $userto, false, $canreply, true, false);
1012                         // Create an array of postid's for this user to mark as read.
1013                             if (!$CFG->forum_usermarksread) {
1014                                 $userto->markposts[$post->id] = $post->id;
1015                             }
1016                         }
1017                     }
1018                     if ($canunsubscribe) {
1019                         $posthtml .= "\n<div class='mdl-right'><font size=\"1\"><a href=\"$CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\">".get_string("unsubscribe", "forum")."</a></font></div>";
1020                     } else {
1021                         $posthtml .= "\n<div class='mdl-right'><font size=\"1\">".get_string("everyoneissubscribed", "forum")."</font></div>";
1022                     }
1023                     $posthtml .= '<hr size="1" noshade="noshade" /></p>';
1024                 }
1025                 $posthtml .= '</body>';
1027                 if (empty($userto->mailformat) || $userto->mailformat != 1) {
1028                     // This user DOESN'T want to receive HTML
1029                     $posthtml = '';
1030                 }
1032                 $attachment = $attachname='';
1033                 // Directly email forum digests rather than sending them via messaging, use the
1034                 // site shortname as 'from name', the noreply address will be used by email_to_user.
1035                 $mailresult = email_to_user($userto, $site->shortname, $postsubject, $posttext, $posthtml, $attachment, $attachname);
1037                 if (!$mailresult) {
1038                     mtrace("ERROR!");
1039                     echo "Error: mod/forum/cron.php: Could not send out digest mail to user $userto->id ($userto->email)... not trying again.\n";
1040                     add_to_log($course->id, 'forum', 'mail digest error', '', '', $cm->id, $userto->id);
1041                 } else {
1042                     mtrace("success.");
1043                     $usermailcount++;
1045                     // Mark post as read if forum_usermarksread is set off
1046                     forum_tp_mark_posts_read($userto, $userto->markposts);
1047                 }
1048             }
1049         }
1050     /// We have finishied all digest emails, update $CFG->digestmailtimelast
1051         set_config('digestmailtimelast', $timenow);
1052     }
1054     cron_setup_user();
1056     if (!empty($usermailcount)) {
1057         mtrace(get_string('digestsentusers', 'forum', $usermailcount));
1058     }
1060     if (!empty($CFG->forum_lastreadclean)) {
1061         $timenow = time();
1062         if ($CFG->forum_lastreadclean + (24*3600) < $timenow) {
1063             set_config('forum_lastreadclean', $timenow);
1064             mtrace('Removing old forum read tracking info...');
1065             forum_tp_clean_read_records();
1066         }
1067     } else {
1068         set_config('forum_lastreadclean', time());
1069     }
1072     return true;
1075 /**
1076  * Builds and returns the body of the email notification in plain text.
1077  *
1078  * @global object
1079  * @global object
1080  * @uses CONTEXT_MODULE
1081  * @param object $course
1082  * @param object $cm
1083  * @param object $forum
1084  * @param object $discussion
1085  * @param object $post
1086  * @param object $userfrom
1087  * @param object $userto
1088  * @param boolean $bare
1089  * @return string The email body in plain text format.
1090  */
1091 function forum_make_mail_text($course, $cm, $forum, $discussion, $post, $userfrom, $userto, $bare = false) {
1092     global $CFG, $USER;
1094     $modcontext = context_module::instance($cm->id);
1096     if (!isset($userto->viewfullnames[$forum->id])) {
1097         $viewfullnames = has_capability('moodle/site:viewfullnames', $modcontext, $userto->id);
1098     } else {
1099         $viewfullnames = $userto->viewfullnames[$forum->id];
1100     }
1102     if (!isset($userto->canpost[$discussion->id])) {
1103         $canreply = forum_user_can_post($forum, $discussion, $userto, $cm, $course, $modcontext);
1104     } else {
1105         $canreply = $userto->canpost[$discussion->id];
1106     }
1108     $by = New stdClass;
1109     $by->name = fullname($userfrom, $viewfullnames);
1110     $by->date = userdate($post->modified, "", $userto->timezone);
1112     $strbynameondate = get_string('bynameondate', 'forum', $by);
1114     $strforums = get_string('forums', 'forum');
1116     $canunsubscribe = ! forum_is_forcesubscribed($forum);
1118     $posttext = '';
1120     if (!$bare) {
1121         $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
1122         $posttext  = "$shortname -> $strforums -> ".format_string($forum->name,true);
1124         if ($discussion->name != $forum->name) {
1125             $posttext  .= " -> ".format_string($discussion->name,true);
1126         }
1127     }
1129     // add absolute file links
1130     $post->message = file_rewrite_pluginfile_urls($post->message, 'pluginfile.php', $modcontext->id, 'mod_forum', 'post', $post->id);
1132     $posttext .= "\n";
1133     $posttext .= $CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id;
1134     $posttext .= "\n---------------------------------------------------------------------\n";
1135     $posttext .= format_string($post->subject,true);
1136     if ($bare) {
1137         $posttext .= " ($CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id#p$post->id)";
1138     }
1139     $posttext .= "\n".$strbynameondate."\n";
1140     $posttext .= "---------------------------------------------------------------------\n";
1141     $posttext .= format_text_email($post->message, $post->messageformat);
1142     $posttext .= "\n\n";
1143     $posttext .= forum_print_attachments($post, $cm, "text");
1145     if (!$bare && $canreply) {
1146         $posttext .= "---------------------------------------------------------------------\n";
1147         $posttext .= get_string("postmailinfo", "forum", $shortname)."\n";
1148         $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id\n";
1149     }
1150     if (!$bare && $canunsubscribe) {
1151         $posttext .= "\n---------------------------------------------------------------------\n";
1152         $posttext .= get_string("unsubscribe", "forum");
1153         $posttext .= ": $CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\n";
1154     }
1156     return $posttext;
1159 /**
1160  * Builds and returns the body of the email notification in html format.
1161  *
1162  * @global object
1163  * @param object $course
1164  * @param object $cm
1165  * @param object $forum
1166  * @param object $discussion
1167  * @param object $post
1168  * @param object $userfrom
1169  * @param object $userto
1170  * @return string The email text in HTML format
1171  */
1172 function forum_make_mail_html($course, $cm, $forum, $discussion, $post, $userfrom, $userto) {
1173     global $CFG;
1175     if ($userto->mailformat != 1) {  // Needs to be HTML
1176         return '';
1177     }
1179     if (!isset($userto->canpost[$discussion->id])) {
1180         $canreply = forum_user_can_post($forum, $discussion, $userto, $cm, $course);
1181     } else {
1182         $canreply = $userto->canpost[$discussion->id];
1183     }
1185     $strforums = get_string('forums', 'forum');
1186     $canunsubscribe = ! forum_is_forcesubscribed($forum);
1187     $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
1189     $posthtml = '<head>';
1190 /*    foreach ($CFG->stylesheets as $stylesheet) {
1191         //TODO: MDL-21120
1192         $posthtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
1193     }*/
1194     $posthtml .= '</head>';
1195     $posthtml .= "\n<body id=\"email\">\n\n";
1197     $posthtml .= '<div class="navbar">'.
1198     '<a target="_blank" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.$shortname.'</a> &raquo; '.
1199     '<a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/index.php?id='.$course->id.'">'.$strforums.'</a> &raquo; '.
1200     '<a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/view.php?f='.$forum->id.'">'.format_string($forum->name,true).'</a>';
1201     if ($discussion->name == $forum->name) {
1202         $posthtml .= '</div>';
1203     } else {
1204         $posthtml .= ' &raquo; <a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id.'">'.
1205                      format_string($discussion->name,true).'</a></div>';
1206     }
1207     $posthtml .= forum_make_mail_post($course, $cm, $forum, $discussion, $post, $userfrom, $userto, false, $canreply, true, false);
1209     if ($canunsubscribe) {
1210         $posthtml .= '<hr /><div class="mdl-align unsubscribelink">
1211                       <a href="'.$CFG->wwwroot.'/mod/forum/subscribe.php?id='.$forum->id.'">'.get_string('unsubscribe', 'forum').'</a>&nbsp;
1212                       <a href="'.$CFG->wwwroot.'/mod/forum/unsubscribeall.php">'.get_string('unsubscribeall', 'forum').'</a></div>';
1213     }
1215     $posthtml .= '</body>';
1217     return $posthtml;
1221 /**
1222  *
1223  * @param object $course
1224  * @param object $user
1225  * @param object $mod TODO this is not used in this function, refactor
1226  * @param object $forum
1227  * @return object A standard object with 2 variables: info (number of posts for this user) and time (last modified)
1228  */
1229 function forum_user_outline($course, $user, $mod, $forum) {
1230     global $CFG;
1231     require_once("$CFG->libdir/gradelib.php");
1232     $grades = grade_get_grades($course->id, 'mod', 'forum', $forum->id, $user->id);
1233     if (empty($grades->items[0]->grades)) {
1234         $grade = false;
1235     } else {
1236         $grade = reset($grades->items[0]->grades);
1237     }
1239     $count = forum_count_user_posts($forum->id, $user->id);
1241     if ($count && $count->postcount > 0) {
1242         $result = new stdClass();
1243         $result->info = get_string("numposts", "forum", $count->postcount);
1244         $result->time = $count->lastpost;
1245         if ($grade) {
1246             $result->info .= ', ' . get_string('grade') . ': ' . $grade->str_long_grade;
1247         }
1248         return $result;
1249     } else if ($grade) {
1250         $result = new stdClass();
1251         $result->info = get_string('grade') . ': ' . $grade->str_long_grade;
1253         //datesubmitted == time created. dategraded == time modified or time overridden
1254         //if grade was last modified by the user themselves use date graded. Otherwise use date submitted
1255         //TODO: move this copied & pasted code somewhere in the grades API. See MDL-26704
1256         if ($grade->usermodified == $user->id || empty($grade->datesubmitted)) {
1257             $result->time = $grade->dategraded;
1258         } else {
1259             $result->time = $grade->datesubmitted;
1260         }
1262         return $result;
1263     }
1264     return NULL;
1268 /**
1269  * @global object
1270  * @global object
1271  * @param object $coure
1272  * @param object $user
1273  * @param object $mod
1274  * @param object $forum
1275  */
1276 function forum_user_complete($course, $user, $mod, $forum) {
1277     global $CFG,$USER, $OUTPUT;
1278     require_once("$CFG->libdir/gradelib.php");
1280     $grades = grade_get_grades($course->id, 'mod', 'forum', $forum->id, $user->id);
1281     if (!empty($grades->items[0]->grades)) {
1282         $grade = reset($grades->items[0]->grades);
1283         echo $OUTPUT->container(get_string('grade').': '.$grade->str_long_grade);
1284         if ($grade->str_feedback) {
1285             echo $OUTPUT->container(get_string('feedback').': '.$grade->str_feedback);
1286         }
1287     }
1289     if ($posts = forum_get_user_posts($forum->id, $user->id)) {
1291         if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) {
1292             print_error('invalidcoursemodule');
1293         }
1294         $discussions = forum_get_user_involved_discussions($forum->id, $user->id);
1296         foreach ($posts as $post) {
1297             if (!isset($discussions[$post->discussion])) {
1298                 continue;
1299             }
1300             $discussion = $discussions[$post->discussion];
1302             forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
1303         }
1304     } else {
1305         echo "<p>".get_string("noposts", "forum")."</p>";
1306     }
1314 /**
1315  * @global object
1316  * @global object
1317  * @global object
1318  * @param array $courses
1319  * @param array $htmlarray
1320  */
1321 function forum_print_overview($courses,&$htmlarray) {
1322     global $USER, $CFG, $DB, $SESSION;
1324     if (empty($courses) || !is_array($courses) || count($courses) == 0) {
1325         return array();
1326     }
1328     if (!$forums = get_all_instances_in_courses('forum',$courses)) {
1329         return;
1330     }
1332     // Courses to search for new posts
1333     $coursessqls = array();
1334     $params = array();
1335     foreach ($courses as $course) {
1337         // If the user has never entered into the course all posts are pending
1338         if ($course->lastaccess == 0) {
1339             $coursessqls[] = '(f.course = ?)';
1340             $params[] = $course->id;
1342         // Only posts created after the course last access
1343         } else {
1344             $coursessqls[] = '(f.course = ? AND p.created > ?)';
1345             $params[] = $course->id;
1346             $params[] = $course->lastaccess;
1347         }
1348     }
1349     $params[] = $USER->id;
1350     $coursessql = implode(' OR ', $coursessqls);
1352     $sql = "SELECT f.id, COUNT(*) as count "
1353                 .'FROM {forum} f '
1354                 .'JOIN {forum_discussions} d ON d.forum  = f.id '
1355                 .'JOIN {forum_posts} p ON p.discussion = d.id '
1356                 ."WHERE ($coursessql) "
1357                 .'AND p.userid != ? '
1358                 .'GROUP BY f.id';
1360     if (!$new = $DB->get_records_sql($sql, $params)) {
1361         $new = array(); // avoid warnings
1362     }
1364     // also get all forum tracking stuff ONCE.
1365     $trackingforums = array();
1366     foreach ($forums as $forum) {
1367         if (forum_tp_can_track_forums($forum)) {
1368             $trackingforums[$forum->id] = $forum;
1369         }
1370     }
1372     if (count($trackingforums) > 0) {
1373         $cutoffdate = isset($CFG->forum_oldpostdays) ? (time() - ($CFG->forum_oldpostdays*24*60*60)) : 0;
1374         $sql = 'SELECT d.forum,d.course,COUNT(p.id) AS count '.
1375             ' FROM {forum_posts} p '.
1376             ' JOIN {forum_discussions} d ON p.discussion = d.id '.
1377             ' LEFT JOIN {forum_read} r ON r.postid = p.id AND r.userid = ? WHERE (';
1378         $params = array($USER->id);
1380         foreach ($trackingforums as $track) {
1381             $sql .= '(d.forum = ? AND (d.groupid = -1 OR d.groupid = 0 OR d.groupid = ?)) OR ';
1382             $params[] = $track->id;
1383             if (isset($SESSION->currentgroup[$track->course])) {
1384                 $groupid =  $SESSION->currentgroup[$track->course];
1385             } else {
1386                 // get first groupid
1387                 $groupids = groups_get_all_groups($track->course, $USER->id);
1388                 if ($groupids) {
1389                     reset($groupids);
1390                     $groupid = key($groupids);
1391                     $SESSION->currentgroup[$track->course] = $groupid;
1392                 } else {
1393                     $groupid = 0;
1394                 }
1395                 unset($groupids);
1396             }
1397             $params[] = $groupid;
1398         }
1399         $sql = substr($sql,0,-3); // take off the last OR
1400         $sql .= ') AND p.modified >= ? AND r.id is NULL GROUP BY d.forum,d.course';
1401         $params[] = $cutoffdate;
1403         if (!$unread = $DB->get_records_sql($sql, $params)) {
1404             $unread = array();
1405         }
1406     } else {
1407         $unread = array();
1408     }
1410     if (empty($unread) and empty($new)) {
1411         return;
1412     }
1414     $strforum = get_string('modulename','forum');
1416     foreach ($forums as $forum) {
1417         $str = '';
1418         $count = 0;
1419         $thisunread = 0;
1420         $showunread = false;
1421         // either we have something from logs, or trackposts, or nothing.
1422         if (array_key_exists($forum->id, $new) && !empty($new[$forum->id])) {
1423             $count = $new[$forum->id]->count;
1424         }
1425         if (array_key_exists($forum->id,$unread)) {
1426             $thisunread = $unread[$forum->id]->count;
1427             $showunread = true;
1428         }
1429         if ($count > 0 || $thisunread > 0) {
1430             $str .= '<div class="overview forum"><div class="name">'.$strforum.': <a title="'.$strforum.'" href="'.$CFG->wwwroot.'/mod/forum/view.php?f='.$forum->id.'">'.
1431                 $forum->name.'</a></div>';
1432             $str .= '<div class="info"><span class="postsincelogin">';
1433             $str .= get_string('overviewnumpostssince', 'forum', $count)."</span>";
1434             if (!empty($showunread)) {
1435                 $str .= '<div class="unreadposts">'.get_string('overviewnumunread', 'forum', $thisunread).'</div>';
1436             }
1437             $str .= '</div></div>';
1438         }
1439         if (!empty($str)) {
1440             if (!array_key_exists($forum->course,$htmlarray)) {
1441                 $htmlarray[$forum->course] = array();
1442             }
1443             if (!array_key_exists('forum',$htmlarray[$forum->course])) {
1444                 $htmlarray[$forum->course]['forum'] = ''; // initialize, avoid warnings
1445             }
1446             $htmlarray[$forum->course]['forum'] .= $str;
1447         }
1448     }
1451 /**
1452  * Given a course and a date, prints a summary of all the new
1453  * messages posted in the course since that date
1454  *
1455  * @global object
1456  * @global object
1457  * @global object
1458  * @uses CONTEXT_MODULE
1459  * @uses VISIBLEGROUPS
1460  * @param object $course
1461  * @param bool $viewfullnames capability
1462  * @param int $timestart
1463  * @return bool success
1464  */
1465 function forum_print_recent_activity($course, $viewfullnames, $timestart) {
1466     global $CFG, $USER, $DB, $OUTPUT;
1468     // do not use log table if possible, it may be huge and is expensive to join with other tables
1470     if (!$posts = $DB->get_records_sql("SELECT p.*, f.type AS forumtype, d.forum, d.groupid,
1471                                               d.timestart, d.timeend, d.userid AS duserid,
1472                                               u.firstname, u.lastname, u.email, u.picture
1473                                          FROM {forum_posts} p
1474                                               JOIN {forum_discussions} d ON d.id = p.discussion
1475                                               JOIN {forum} f             ON f.id = d.forum
1476                                               JOIN {user} u              ON u.id = p.userid
1477                                         WHERE p.created > ? AND f.course = ?
1478                                      ORDER BY p.id ASC", array($timestart, $course->id))) { // order by initial posting date
1479          return false;
1480     }
1482     $modinfo = get_fast_modinfo($course);
1484     $groupmodes = array();
1485     $cms    = array();
1487     $strftimerecent = get_string('strftimerecent');
1489     $printposts = array();
1490     foreach ($posts as $post) {
1491         if (!isset($modinfo->instances['forum'][$post->forum])) {
1492             // not visible
1493             continue;
1494         }
1495         $cm = $modinfo->instances['forum'][$post->forum];
1496         if (!$cm->uservisible) {
1497             continue;
1498         }
1499         $context = context_module::instance($cm->id);
1501         if (!has_capability('mod/forum:viewdiscussion', $context)) {
1502             continue;
1503         }
1505         if (!empty($CFG->forum_enabletimedposts) and $USER->id != $post->duserid
1506           and (($post->timestart > 0 and $post->timestart > time()) or ($post->timeend > 0 and $post->timeend < time()))) {
1507             if (!has_capability('mod/forum:viewhiddentimedposts', $context)) {
1508                 continue;
1509             }
1510         }
1512         $groupmode = groups_get_activity_groupmode($cm, $course);
1514         if ($groupmode) {
1515             if ($post->groupid == -1 or $groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
1516                 // oki (Open discussions have groupid -1)
1517             } else {
1518                 // separate mode
1519                 if (isguestuser()) {
1520                     // shortcut
1521                     continue;
1522                 }
1524                 if (is_null($modinfo->groups)) {
1525                     $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
1526                 }
1528                 if (!in_array($post->groupid, $modinfo->get_groups($cm->groupingid))) {
1529                     continue;
1530                 }
1531             }
1532         }
1534         $printposts[] = $post;
1535     }
1536     unset($posts);
1538     if (!$printposts) {
1539         return false;
1540     }
1542     echo $OUTPUT->heading(get_string('newforumposts', 'forum').':', 3);
1543     echo "\n<ul class='unlist'>\n";
1545     foreach ($printposts as $post) {
1546         $subjectclass = empty($post->parent) ? ' bold' : '';
1548         echo '<li><div class="head">'.
1549                '<div class="date">'.userdate($post->modified, $strftimerecent).'</div>'.
1550                '<div class="name">'.fullname($post, $viewfullnames).'</div>'.
1551              '</div>';
1552         echo '<div class="info'.$subjectclass.'">';
1553         if (empty($post->parent)) {
1554             echo '"<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'">';
1555         } else {
1556             echo '"<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'&amp;parent='.$post->parent.'#p'.$post->id.'">';
1557         }
1558         $post->subject = break_up_long_words(format_string($post->subject, true));
1559         echo $post->subject;
1560         echo "</a>\"</div></li>\n";
1561     }
1563     echo "</ul>\n";
1565     return true;
1568 /**
1569  * Return grade for given user or all users.
1570  *
1571  * @global object
1572  * @global object
1573  * @param object $forum
1574  * @param int $userid optional user id, 0 means all users
1575  * @return array array of grades, false if none
1576  */
1577 function forum_get_user_grades($forum, $userid = 0) {
1578     global $CFG;
1580     require_once($CFG->dirroot.'/rating/lib.php');
1582     $ratingoptions = new stdClass;
1583     $ratingoptions->component = 'mod_forum';
1584     $ratingoptions->ratingarea = 'post';
1586     //need these to work backwards to get a context id. Is there a better way to get contextid from a module instance?
1587     $ratingoptions->modulename = 'forum';
1588     $ratingoptions->moduleid   = $forum->id;
1589     $ratingoptions->userid = $userid;
1590     $ratingoptions->aggregationmethod = $forum->assessed;
1591     $ratingoptions->scaleid = $forum->scale;
1592     $ratingoptions->itemtable = 'forum_posts';
1593     $ratingoptions->itemtableusercolumn = 'userid';
1595     $rm = new rating_manager();
1596     return $rm->get_user_grades($ratingoptions);
1599 /**
1600  * Update activity grades
1601  *
1602  * @category grade
1603  * @param object $forum
1604  * @param int $userid specific user only, 0 means all
1605  * @param boolean $nullifnone return null if grade does not exist
1606  * @return void
1607  */
1608 function forum_update_grades($forum, $userid=0, $nullifnone=true) {
1609     global $CFG, $DB;
1610     require_once($CFG->libdir.'/gradelib.php');
1612     if (!$forum->assessed) {
1613         forum_grade_item_update($forum);
1615     } else if ($grades = forum_get_user_grades($forum, $userid)) {
1616         forum_grade_item_update($forum, $grades);
1618     } else if ($userid and $nullifnone) {
1619         $grade = new stdClass();
1620         $grade->userid   = $userid;
1621         $grade->rawgrade = NULL;
1622         forum_grade_item_update($forum, $grade);
1624     } else {
1625         forum_grade_item_update($forum);
1626     }
1629 /**
1630  * Update all grades in gradebook.
1631  * @global object
1632  */
1633 function forum_upgrade_grades() {
1634     global $DB;
1636     $sql = "SELECT COUNT('x')
1637               FROM {forum} f, {course_modules} cm, {modules} m
1638              WHERE m.name='forum' AND m.id=cm.module AND cm.instance=f.id";
1639     $count = $DB->count_records_sql($sql);
1641     $sql = "SELECT f.*, cm.idnumber AS cmidnumber, f.course AS courseid
1642               FROM {forum} f, {course_modules} cm, {modules} m
1643              WHERE m.name='forum' AND m.id=cm.module AND cm.instance=f.id";
1644     $rs = $DB->get_recordset_sql($sql);
1645     if ($rs->valid()) {
1646         $pbar = new progress_bar('forumupgradegrades', 500, true);
1647         $i=0;
1648         foreach ($rs as $forum) {
1649             $i++;
1650             upgrade_set_timeout(60*5); // set up timeout, may also abort execution
1651             forum_update_grades($forum, 0, false);
1652             $pbar->update($i, $count, "Updating Forum grades ($i/$count).");
1653         }
1654     }
1655     $rs->close();
1658 /**
1659  * Create/update grade item for given forum
1660  *
1661  * @category grade
1662  * @uses GRADE_TYPE_NONE
1663  * @uses GRADE_TYPE_VALUE
1664  * @uses GRADE_TYPE_SCALE
1665  * @param stdClass $forum Forum object with extra cmidnumber
1666  * @param mixed $grades Optional array/object of grade(s); 'reset' means reset grades in gradebook
1667  * @return int 0 if ok
1668  */
1669 function forum_grade_item_update($forum, $grades=NULL) {
1670     global $CFG;
1671     if (!function_exists('grade_update')) { //workaround for buggy PHP versions
1672         require_once($CFG->libdir.'/gradelib.php');
1673     }
1675     $params = array('itemname'=>$forum->name, 'idnumber'=>$forum->cmidnumber);
1677     if (!$forum->assessed or $forum->scale == 0) {
1678         $params['gradetype'] = GRADE_TYPE_NONE;
1680     } else if ($forum->scale > 0) {
1681         $params['gradetype'] = GRADE_TYPE_VALUE;
1682         $params['grademax']  = $forum->scale;
1683         $params['grademin']  = 0;
1685     } else if ($forum->scale < 0) {
1686         $params['gradetype'] = GRADE_TYPE_SCALE;
1687         $params['scaleid']   = -$forum->scale;
1688     }
1690     if ($grades  === 'reset') {
1691         $params['reset'] = true;
1692         $grades = NULL;
1693     }
1695     return grade_update('mod/forum', $forum->course, 'mod', 'forum', $forum->id, 0, $grades, $params);
1698 /**
1699  * Delete grade item for given forum
1700  *
1701  * @category grade
1702  * @param stdClass $forum Forum object
1703  * @return grade_item
1704  */
1705 function forum_grade_item_delete($forum) {
1706     global $CFG;
1707     require_once($CFG->libdir.'/gradelib.php');
1709     return grade_update('mod/forum', $forum->course, 'mod', 'forum', $forum->id, 0, NULL, array('deleted'=>1));
1713 /**
1714  * This function returns if a scale is being used by one forum
1715  *
1716  * @global object
1717  * @param int $forumid
1718  * @param int $scaleid negative number
1719  * @return bool
1720  */
1721 function forum_scale_used ($forumid,$scaleid) {
1722     global $DB;
1723     $return = false;
1725     $rec = $DB->get_record("forum",array("id" => "$forumid","scale" => "-$scaleid"));
1727     if (!empty($rec) && !empty($scaleid)) {
1728         $return = true;
1729     }
1731     return $return;
1734 /**
1735  * Checks if scale is being used by any instance of forum
1736  *
1737  * This is used to find out if scale used anywhere
1738  *
1739  * @global object
1740  * @param $scaleid int
1741  * @return boolean True if the scale is used by any forum
1742  */
1743 function forum_scale_used_anywhere($scaleid) {
1744     global $DB;
1745     if ($scaleid and $DB->record_exists('forum', array('scale' => -$scaleid))) {
1746         return true;
1747     } else {
1748         return false;
1749     }
1752 // SQL FUNCTIONS ///////////////////////////////////////////////////////////
1754 /**
1755  * Gets a post with all info ready for forum_print_post
1756  * Most of these joins are just to get the forum id
1757  *
1758  * @global object
1759  * @global object
1760  * @param int $postid
1761  * @return mixed array of posts or false
1762  */
1763 function forum_get_post_full($postid) {
1764     global $CFG, $DB;
1766     $allnames = get_all_user_name_fields(true, 'u');
1767     return $DB->get_record_sql("SELECT p.*, d.forum, $allnames, u.email, u.picture, u.imagealt
1768                              FROM {forum_posts} p
1769                                   JOIN {forum_discussions} d ON p.discussion = d.id
1770                                   LEFT JOIN {user} u ON p.userid = u.id
1771                             WHERE p.id = ?", array($postid));
1774 /**
1775  * Gets posts with all info ready for forum_print_post
1776  * We pass forumid in because we always know it so no need to make a
1777  * complicated join to find it out.
1778  *
1779  * @global object
1780  * @global object
1781  * @return mixed array of posts or false
1782  */
1783 function forum_get_discussion_posts($discussion, $sort, $forumid) {
1784     global $CFG, $DB;
1786     $allnames = get_all_user_name_fields(true, 'u');
1787     return $DB->get_records_sql("SELECT p.*, $forumid AS forum, $allnames, u.email, u.picture, u.imagealt
1788                               FROM {forum_posts} p
1789                          LEFT JOIN {user} u ON p.userid = u.id
1790                              WHERE p.discussion = ?
1791                                AND p.parent > 0 $sort", array($discussion));
1794 /**
1795  * Gets all posts in discussion including top parent.
1796  *
1797  * @global object
1798  * @global object
1799  * @global object
1800  * @param int $discussionid
1801  * @param string $sort
1802  * @param bool $tracking does user track the forum?
1803  * @return array of posts
1804  */
1805 function forum_get_all_discussion_posts($discussionid, $sort, $tracking=false) {
1806     global $CFG, $DB, $USER;
1808     $tr_sel  = "";
1809     $tr_join = "";
1810     $params = array();
1812     if ($tracking) {
1813         $now = time();
1814         $cutoffdate = $now - ($CFG->forum_oldpostdays * 24 * 3600);
1815         $tr_sel  = ", fr.id AS postread";
1816         $tr_join = "LEFT JOIN {forum_read} fr ON (fr.postid = p.id AND fr.userid = ?)";
1817         $params[] = $USER->id;
1818     }
1820     $allnames = get_all_user_name_fields(true, 'u');
1821     $params[] = $discussionid;
1822     if (!$posts = $DB->get_records_sql("SELECT p.*, $allnames, u.email, u.picture, u.imagealt $tr_sel
1823                                      FROM {forum_posts} p
1824                                           LEFT JOIN {user} u ON p.userid = u.id
1825                                           $tr_join
1826                                     WHERE p.discussion = ?
1827                                  ORDER BY $sort", $params)) {
1828         return array();
1829     }
1831     foreach ($posts as $pid=>$p) {
1832         if ($tracking) {
1833             if (forum_tp_is_post_old($p)) {
1834                  $posts[$pid]->postread = true;
1835             }
1836         }
1837         if (!$p->parent) {
1838             continue;
1839         }
1840         if (!isset($posts[$p->parent])) {
1841             continue; // parent does not exist??
1842         }
1843         if (!isset($posts[$p->parent]->children)) {
1844             $posts[$p->parent]->children = array();
1845         }
1846         $posts[$p->parent]->children[$pid] =& $posts[$pid];
1847     }
1849     return $posts;
1852 /**
1853  * Gets posts with all info ready for forum_print_post
1854  * We pass forumid in because we always know it so no need to make a
1855  * complicated join to find it out.
1856  *
1857  * @global object
1858  * @global object
1859  * @param int $parent
1860  * @param int $forumid
1861  * @return array
1862  */
1863 function forum_get_child_posts($parent, $forumid) {
1864     global $CFG, $DB;
1866     $allnames = get_all_user_name_fields(true, 'u');
1867     return $DB->get_records_sql("SELECT p.*, $forumid AS forum, $allnames, u.email, u.picture, u.imagealt
1868                               FROM {forum_posts} p
1869                          LEFT JOIN {user} u ON p.userid = u.id
1870                              WHERE p.parent = ?
1871                           ORDER BY p.created ASC", array($parent));
1874 /**
1875  * An array of forum objects that the user is allowed to read/search through.
1876  *
1877  * @global object
1878  * @global object
1879  * @global object
1880  * @param int $userid
1881  * @param int $courseid if 0, we look for forums throughout the whole site.
1882  * @return array of forum objects, or false if no matches
1883  *         Forum objects have the following attributes:
1884  *         id, type, course, cmid, cmvisible, cmgroupmode, accessallgroups,
1885  *         viewhiddentimedposts
1886  */
1887 function forum_get_readable_forums($userid, $courseid=0) {
1889     global $CFG, $DB, $USER;
1890     require_once($CFG->dirroot.'/course/lib.php');
1892     if (!$forummod = $DB->get_record('modules', array('name' => 'forum'))) {
1893         print_error('notinstalled', 'forum');
1894     }
1896     if ($courseid) {
1897         $courses = $DB->get_records('course', array('id' => $courseid));
1898     } else {
1899         // If no course is specified, then the user can see SITE + his courses.
1900         $courses1 = $DB->get_records('course', array('id' => SITEID));
1901         $courses2 = enrol_get_users_courses($userid, true, array('modinfo'));
1902         $courses = array_merge($courses1, $courses2);
1903     }
1904     if (!$courses) {
1905         return array();
1906     }
1908     $readableforums = array();
1910     foreach ($courses as $course) {
1912         $modinfo = get_fast_modinfo($course);
1913         if (is_null($modinfo->groups)) {
1914             $modinfo->groups = groups_get_user_groups($course->id, $userid);
1915         }
1917         if (empty($modinfo->instances['forum'])) {
1918             // hmm, no forums?
1919             continue;
1920         }
1922         $courseforums = $DB->get_records('forum', array('course' => $course->id));
1924         foreach ($modinfo->instances['forum'] as $forumid => $cm) {
1925             if (!$cm->uservisible or !isset($courseforums[$forumid])) {
1926                 continue;
1927             }
1928             $context = context_module::instance($cm->id);
1929             $forum = $courseforums[$forumid];
1930             $forum->context = $context;
1931             $forum->cm = $cm;
1933             if (!has_capability('mod/forum:viewdiscussion', $context)) {
1934                 continue;
1935             }
1937          /// group access
1938             if (groups_get_activity_groupmode($cm, $course) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
1939                 if (is_null($modinfo->groups)) {
1940                     $modinfo->groups = groups_get_user_groups($course->id, $USER->id);
1941                 }
1942                 if (isset($modinfo->groups[$cm->groupingid])) {
1943                     $forum->onlygroups = $modinfo->groups[$cm->groupingid];
1944                     $forum->onlygroups[] = -1;
1945                 } else {
1946                     $forum->onlygroups = array(-1);
1947                 }
1948             }
1950         /// hidden timed discussions
1951             $forum->viewhiddentimedposts = true;
1952             if (!empty($CFG->forum_enabletimedposts)) {
1953                 if (!has_capability('mod/forum:viewhiddentimedposts', $context)) {
1954                     $forum->viewhiddentimedposts = false;
1955                 }
1956             }
1958         /// qanda access
1959             if ($forum->type == 'qanda'
1960                     && !has_capability('mod/forum:viewqandawithoutposting', $context)) {
1962                 // We need to check whether the user has posted in the qanda forum.
1963                 $forum->onlydiscussions = array();  // Holds discussion ids for the discussions
1964                                                     // the user is allowed to see in this forum.
1965                 if ($discussionspostedin = forum_discussions_user_has_posted_in($forum->id, $USER->id)) {
1966                     foreach ($discussionspostedin as $d) {
1967                         $forum->onlydiscussions[] = $d->id;
1968                     }
1969                 }
1970             }
1972             $readableforums[$forum->id] = $forum;
1973         }
1975         unset($modinfo);
1977     } // End foreach $courses
1979     return $readableforums;
1982 /**
1983  * Returns a list of posts found using an array of search terms.
1984  *
1985  * @global object
1986  * @global object
1987  * @global object
1988  * @param array $searchterms array of search terms, e.g. word +word -word
1989  * @param int $courseid if 0, we search through the whole site
1990  * @param int $limitfrom
1991  * @param int $limitnum
1992  * @param int &$totalcount
1993  * @param string $extrasql
1994  * @return array|bool Array of posts found or false
1995  */
1996 function forum_search_posts($searchterms, $courseid=0, $limitfrom=0, $limitnum=50,
1997                             &$totalcount, $extrasql='') {
1998     global $CFG, $DB, $USER;
1999     require_once($CFG->libdir.'/searchlib.php');
2001     $forums = forum_get_readable_forums($USER->id, $courseid);
2003     if (count($forums) == 0) {
2004         $totalcount = 0;
2005         return false;
2006     }
2008     $now = round(time(), -2); // db friendly
2010     $fullaccess = array();
2011     $where = array();
2012     $params = array();
2014     foreach ($forums as $forumid => $forum) {
2015         $select = array();
2017         if (!$forum->viewhiddentimedposts) {
2018             $select[] = "(d.userid = :userid{$forumid} OR (d.timestart < :timestart{$forumid} AND (d.timeend = 0 OR d.timeend > :timeend{$forumid})))";
2019             $params = array_merge($params, array('userid'.$forumid=>$USER->id, 'timestart'.$forumid=>$now, 'timeend'.$forumid=>$now));
2020         }
2022         $cm = $forum->cm;
2023         $context = $forum->context;
2025         if ($forum->type == 'qanda'
2026             && !has_capability('mod/forum:viewqandawithoutposting', $context)) {
2027             if (!empty($forum->onlydiscussions)) {
2028                 list($discussionid_sql, $discussionid_params) = $DB->get_in_or_equal($forum->onlydiscussions, SQL_PARAMS_NAMED, 'qanda'.$forumid.'_');
2029                 $params = array_merge($params, $discussionid_params);
2030                 $select[] = "(d.id $discussionid_sql OR p.parent = 0)";
2031             } else {
2032                 $select[] = "p.parent = 0";
2033             }
2034         }
2036         if (!empty($forum->onlygroups)) {
2037             list($groupid_sql, $groupid_params) = $DB->get_in_or_equal($forum->onlygroups, SQL_PARAMS_NAMED, 'grps'.$forumid.'_');
2038             $params = array_merge($params, $groupid_params);
2039             $select[] = "d.groupid $groupid_sql";
2040         }
2042         if ($select) {
2043             $selects = implode(" AND ", $select);
2044             $where[] = "(d.forum = :forum{$forumid} AND $selects)";
2045             $params['forum'.$forumid] = $forumid;
2046         } else {
2047             $fullaccess[] = $forumid;
2048         }
2049     }
2051     if ($fullaccess) {
2052         list($fullid_sql, $fullid_params) = $DB->get_in_or_equal($fullaccess, SQL_PARAMS_NAMED, 'fula');
2053         $params = array_merge($params, $fullid_params);
2054         $where[] = "(d.forum $fullid_sql)";
2055     }
2057     $selectdiscussion = "(".implode(" OR ", $where).")";
2059     $messagesearch = '';
2060     $searchstring = '';
2062     // Need to concat these back together for parser to work.
2063     foreach($searchterms as $searchterm){
2064         if ($searchstring != '') {
2065             $searchstring .= ' ';
2066         }
2067         $searchstring .= $searchterm;
2068     }
2070     // We need to allow quoted strings for the search. The quotes *should* be stripped
2071     // by the parser, but this should be examined carefully for security implications.
2072     $searchstring = str_replace("\\\"","\"",$searchstring);
2073     $parser = new search_parser();
2074     $lexer = new search_lexer($parser);
2076     if ($lexer->parse($searchstring)) {
2077         $parsearray = $parser->get_parsed_array();
2078     // Experimental feature under 1.8! MDL-8830
2079     // Use alternative text searches if defined
2080     // This feature only works under mysql until properly implemented for other DBs
2081     // Requires manual creation of text index for forum_posts before enabling it:
2082     // CREATE FULLTEXT INDEX foru_post_tix ON [prefix]forum_posts (subject, message)
2083     // Experimental feature under 1.8! MDL-8830
2084         if (!empty($CFG->forum_usetextsearches)) {
2085             list($messagesearch, $msparams) = search_generate_text_SQL($parsearray, 'p.message', 'p.subject',
2086                                                  'p.userid', 'u.id', 'u.firstname',
2087                                                  'u.lastname', 'p.modified', 'd.forum');
2088         } else {
2089             list($messagesearch, $msparams) = search_generate_SQL($parsearray, 'p.message', 'p.subject',
2090                                                  'p.userid', 'u.id', 'u.firstname',
2091                                                  'u.lastname', 'p.modified', 'd.forum');
2092         }
2093         $params = array_merge($params, $msparams);
2094     }
2096     $fromsql = "{forum_posts} p,
2097                   {forum_discussions} d,
2098                   {user} u";
2100     $selectsql = " $messagesearch
2101                AND p.discussion = d.id
2102                AND p.userid = u.id
2103                AND $selectdiscussion
2104                    $extrasql";
2106     $countsql = "SELECT COUNT(*)
2107                    FROM $fromsql
2108                   WHERE $selectsql";
2110     $allnames = get_all_user_name_fields(true, 'u');
2111     $searchsql = "SELECT p.*,
2112                          d.forum,
2113                          $allnames,
2114                          u.email,
2115                          u.picture,
2116                          u.imagealt
2117                     FROM $fromsql
2118                    WHERE $selectsql
2119                 ORDER BY p.modified DESC";
2121     $totalcount = $DB->count_records_sql($countsql, $params);
2123     return $DB->get_records_sql($searchsql, $params, $limitfrom, $limitnum);
2126 /**
2127  * Returns a list of ratings for a particular post - sorted.
2128  *
2129  * TODO: Check if this function is actually used anywhere.
2130  * Up until the fix for MDL-27471 this function wasn't even returning.
2131  *
2132  * @param stdClass $context
2133  * @param int $postid
2134  * @param string $sort
2135  * @return array Array of ratings or false
2136  */
2137 function forum_get_ratings($context, $postid, $sort = "u.firstname ASC") {
2138     $options = new stdClass;
2139     $options->context = $context;
2140     $options->component = 'mod_forum';
2141     $options->ratingarea = 'post';
2142     $options->itemid = $postid;
2143     $options->sort = "ORDER BY $sort";
2145     $rm = new rating_manager();
2146     return $rm->get_all_ratings_for_item($options);
2149 /**
2150  * Returns a list of all new posts that have not been mailed yet
2151  *
2152  * @param int $starttime posts created after this time
2153  * @param int $endtime posts created before this
2154  * @param int $now used for timed discussions only
2155  * @return array
2156  */
2157 function forum_get_unmailed_posts($starttime, $endtime, $now=null) {
2158     global $CFG, $DB;
2160     $params = array();
2161     $params['mailed'] = FORUM_MAILED_PENDING;
2162     $params['ptimestart'] = $starttime;
2163     $params['ptimeend'] = $endtime;
2164     $params['mailnow'] = 1;
2166     if (!empty($CFG->forum_enabletimedposts)) {
2167         if (empty($now)) {
2168             $now = time();
2169         }
2170         $timedsql = "AND (d.timestart < :dtimestart AND (d.timeend = 0 OR d.timeend > :dtimeend))";
2171         $params['dtimestart'] = $now;
2172         $params['dtimeend'] = $now;
2173     } else {
2174         $timedsql = "";
2175     }
2177     return $DB->get_records_sql("SELECT p.*, d.course, d.forum
2178                                  FROM {forum_posts} p
2179                                  JOIN {forum_discussions} d ON d.id = p.discussion
2180                                  WHERE p.mailed = :mailed
2181                                  AND p.created >= :ptimestart
2182                                  AND (p.created < :ptimeend OR p.mailnow = :mailnow)
2183                                  $timedsql
2184                                  ORDER BY p.modified ASC", $params);
2187 /**
2188  * Marks posts before a certain time as being mailed already
2189  *
2190  * @global object
2191  * @global object
2192  * @param int $endtime
2193  * @param int $now Defaults to time()
2194  * @return bool
2195  */
2196 function forum_mark_old_posts_as_mailed($endtime, $now=null) {
2197     global $CFG, $DB;
2199     if (empty($now)) {
2200         $now = time();
2201     }
2203     $params = array();
2204     $params['mailedsuccess'] = FORUM_MAILED_SUCCESS;
2205     $params['now'] = $now;
2206     $params['endtime'] = $endtime;
2207     $params['mailnow'] = 1;
2208     $params['mailedpending'] = FORUM_MAILED_PENDING;
2210     if (empty($CFG->forum_enabletimedposts)) {
2211         return $DB->execute("UPDATE {forum_posts}
2212                              SET mailed = :mailedsuccess
2213                              WHERE (created < :endtime OR mailnow = :mailnow)
2214                              AND mailed = :mailedpending", $params);
2215     } else {
2216         return $DB->execute("UPDATE {forum_posts}
2217                              SET mailed = :mailedsuccess
2218                              WHERE discussion NOT IN (SELECT d.id
2219                                                       FROM {forum_discussions} d
2220                                                       WHERE d.timestart > :now)
2221                              AND (created < :endtime OR mailnow = :mailnow)
2222                              AND mailed = :mailedpending", $params);
2223     }
2226 /**
2227  * Get all the posts for a user in a forum suitable for forum_print_post
2228  *
2229  * @global object
2230  * @global object
2231  * @uses CONTEXT_MODULE
2232  * @return array
2233  */
2234 function forum_get_user_posts($forumid, $userid) {
2235     global $CFG, $DB;
2237     $timedsql = "";
2238     $params = array($forumid, $userid);
2240     if (!empty($CFG->forum_enabletimedposts)) {
2241         $cm = get_coursemodule_from_instance('forum', $forumid);
2242         if (!has_capability('mod/forum:viewhiddentimedposts' , context_module::instance($cm->id))) {
2243             $now = time();
2244             $timedsql = "AND (d.timestart < ? AND (d.timeend = 0 OR d.timeend > ?))";
2245             $params[] = $now;
2246             $params[] = $now;
2247         }
2248     }
2250     $allnames = get_all_user_name_fields(true, 'u');
2251     return $DB->get_records_sql("SELECT p.*, d.forum, $allnames, u.email, u.picture, u.imagealt
2252                               FROM {forum} f
2253                                    JOIN {forum_discussions} d ON d.forum = f.id
2254                                    JOIN {forum_posts} p       ON p.discussion = d.id
2255                                    JOIN {user} u              ON u.id = p.userid
2256                              WHERE f.id = ?
2257                                    AND p.userid = ?
2258                                    $timedsql
2259                           ORDER BY p.modified ASC", $params);
2262 /**
2263  * Get all the discussions user participated in
2264  *
2265  * @global object
2266  * @global object
2267  * @uses CONTEXT_MODULE
2268  * @param int $forumid
2269  * @param int $userid
2270  * @return array Array or false
2271  */
2272 function forum_get_user_involved_discussions($forumid, $userid) {
2273     global $CFG, $DB;
2275     $timedsql = "";
2276     $params = array($forumid, $userid);
2277     if (!empty($CFG->forum_enabletimedposts)) {
2278         $cm = get_coursemodule_from_instance('forum', $forumid);
2279         if (!has_capability('mod/forum:viewhiddentimedposts' , context_module::instance($cm->id))) {
2280             $now = time();
2281             $timedsql = "AND (d.timestart < ? AND (d.timeend = 0 OR d.timeend > ?))";
2282             $params[] = $now;
2283             $params[] = $now;
2284         }
2285     }
2287     return $DB->get_records_sql("SELECT DISTINCT d.*
2288                               FROM {forum} f
2289                                    JOIN {forum_discussions} d ON d.forum = f.id
2290                                    JOIN {forum_posts} p       ON p.discussion = d.id
2291                              WHERE f.id = ?
2292                                    AND p.userid = ?
2293                                    $timedsql", $params);
2296 /**
2297  * Get all the posts for a user in a forum suitable for forum_print_post
2298  *
2299  * @global object
2300  * @global object
2301  * @param int $forumid
2302  * @param int $userid
2303  * @return array of counts or false
2304  */
2305 function forum_count_user_posts($forumid, $userid) {
2306     global $CFG, $DB;
2308     $timedsql = "";
2309     $params = array($forumid, $userid);
2310     if (!empty($CFG->forum_enabletimedposts)) {
2311         $cm = get_coursemodule_from_instance('forum', $forumid);
2312         if (!has_capability('mod/forum:viewhiddentimedposts' , context_module::instance($cm->id))) {
2313             $now = time();
2314             $timedsql = "AND (d.timestart < ? AND (d.timeend = 0 OR d.timeend > ?))";
2315             $params[] = $now;
2316             $params[] = $now;
2317         }
2318     }
2320     return $DB->get_record_sql("SELECT COUNT(p.id) AS postcount, MAX(p.modified) AS lastpost
2321                              FROM {forum} f
2322                                   JOIN {forum_discussions} d ON d.forum = f.id
2323                                   JOIN {forum_posts} p       ON p.discussion = d.id
2324                                   JOIN {user} u              ON u.id = p.userid
2325                             WHERE f.id = ?
2326                                   AND p.userid = ?
2327                                   $timedsql", $params);
2330 /**
2331  * Given a log entry, return the forum post details for it.
2332  *
2333  * @global object
2334  * @global object
2335  * @param object $log
2336  * @return array|null
2337  */
2338 function forum_get_post_from_log($log) {
2339     global $CFG, $DB;
2341     $allnames = get_all_user_name_fields(true, 'u');
2342     if ($log->action == "add post") {
2344         return $DB->get_record_sql("SELECT p.*, f.type AS forumtype, d.forum, d.groupid, $allnames, u.email, u.picture
2345                                  FROM {forum_discussions} d,
2346                                       {forum_posts} p,
2347                                       {forum} f,
2348                                       {user} u
2349                                 WHERE p.id = ?
2350                                   AND d.id = p.discussion
2351                                   AND p.userid = u.id
2352                                   AND u.deleted <> '1'
2353                                   AND f.id = d.forum", array($log->info));
2356     } else if ($log->action == "add discussion") {
2358         return $DB->get_record_sql("SELECT p.*, f.type AS forumtype, d.forum, d.groupid, $allnames, u.email, u.picture
2359                                  FROM {forum_discussions} d,
2360                                       {forum_posts} p,
2361                                       {forum} f,
2362                                       {user} u
2363                                 WHERE d.id = ?
2364                                   AND d.firstpost = p.id
2365                                   AND p.userid = u.id
2366                                   AND u.deleted <> '1'
2367                                   AND f.id = d.forum", array($log->info));
2368     }
2369     return NULL;
2372 /**
2373  * Given a discussion id, return the first post from the discussion
2374  *
2375  * @global object
2376  * @global object
2377  * @param int $dicsussionid
2378  * @return array
2379  */
2380 function forum_get_firstpost_from_discussion($discussionid) {
2381     global $CFG, $DB;
2383     return $DB->get_record_sql("SELECT p.*
2384                              FROM {forum_discussions} d,
2385                                   {forum_posts} p
2386                             WHERE d.id = ?
2387                               AND d.firstpost = p.id ", array($discussionid));
2390 /**
2391  * Returns an array of counts of replies to each discussion
2392  *
2393  * @global object
2394  * @global object
2395  * @param int $forumid
2396  * @param string $forumsort
2397  * @param int $limit
2398  * @param int $page
2399  * @param int $perpage
2400  * @return array
2401  */
2402 function forum_count_discussion_replies($forumid, $forumsort="", $limit=-1, $page=-1, $perpage=0) {
2403     global $CFG, $DB;
2405     if ($limit > 0) {
2406         $limitfrom = 0;
2407         $limitnum  = $limit;
2408     } else if ($page != -1) {
2409         $limitfrom = $page*$perpage;
2410         $limitnum  = $perpage;
2411     } else {
2412         $limitfrom = 0;
2413         $limitnum  = 0;
2414     }
2416     if ($forumsort == "") {
2417         $orderby = "";
2418         $groupby = "";
2420     } else {
2421         $orderby = "ORDER BY $forumsort";
2422         $groupby = ", ".strtolower($forumsort);
2423         $groupby = str_replace('desc', '', $groupby);
2424         $groupby = str_replace('asc', '', $groupby);
2425     }
2427     if (($limitfrom == 0 and $limitnum == 0) or $forumsort == "") {
2428         $sql = "SELECT p.discussion, COUNT(p.id) AS replies, MAX(p.id) AS lastpostid
2429                   FROM {forum_posts} p
2430                        JOIN {forum_discussions} d ON p.discussion = d.id
2431                  WHERE p.parent > 0 AND d.forum = ?
2432               GROUP BY p.discussion";
2433         return $DB->get_records_sql($sql, array($forumid));
2435     } else {
2436         $sql = "SELECT p.discussion, (COUNT(p.id) - 1) AS replies, MAX(p.id) AS lastpostid
2437                   FROM {forum_posts} p
2438                        JOIN {forum_discussions} d ON p.discussion = d.id
2439                  WHERE d.forum = ?
2440               GROUP BY p.discussion $groupby
2441               $orderby";
2442         return $DB->get_records_sql("SELECT * FROM ($sql) sq", array($forumid), $limitfrom, $limitnum);
2443     }
2446 /**
2447  * @global object
2448  * @global object
2449  * @global object
2450  * @staticvar array $cache
2451  * @param object $forum
2452  * @param object $cm
2453  * @param object $course
2454  * @return mixed
2455  */
2456 function forum_count_discussions($forum, $cm, $course) {
2457     global $CFG, $DB, $USER;
2459     static $cache = array();
2461     $now = round(time(), -2); // db cache friendliness
2463     $params = array($course->id);
2465     if (!isset($cache[$course->id])) {
2466         if (!empty($CFG->forum_enabletimedposts)) {
2467             $timedsql = "AND d.timestart < ? AND (d.timeend = 0 OR d.timeend > ?)";
2468             $params[] = $now;
2469             $params[] = $now;
2470         } else {
2471             $timedsql = "";
2472         }
2474         $sql = "SELECT f.id, COUNT(d.id) as dcount
2475                   FROM {forum} f
2476                        JOIN {forum_discussions} d ON d.forum = f.id
2477                  WHERE f.course = ?
2478                        $timedsql
2479               GROUP BY f.id";
2481         if ($counts = $DB->get_records_sql($sql, $params)) {
2482             foreach ($counts as $count) {
2483                 $counts[$count->id] = $count->dcount;
2484             }
2485             $cache[$course->id] = $counts;
2486         } else {
2487             $cache[$course->id] = array();
2488         }
2489     }
2491     if (empty($cache[$course->id][$forum->id])) {
2492         return 0;
2493     }
2495     $groupmode = groups_get_activity_groupmode($cm, $course);
2497     if ($groupmode != SEPARATEGROUPS) {
2498         return $cache[$course->id][$forum->id];
2499     }
2501     if (has_capability('moodle/site:accessallgroups', context_module::instance($cm->id))) {
2502         return $cache[$course->id][$forum->id];
2503     }
2505     require_once($CFG->dirroot.'/course/lib.php');
2507     $modinfo = get_fast_modinfo($course);
2508     if (is_null($modinfo->groups)) {
2509         $modinfo->groups = groups_get_user_groups($course->id, $USER->id);
2510     }
2512     if (array_key_exists($cm->groupingid, $modinfo->groups)) {
2513         $mygroups = $modinfo->groups[$cm->groupingid];
2514     } else {
2515         $mygroups = false; // Will be set below
2516     }
2518     // add all groups posts
2519     if (empty($mygroups)) {
2520         $mygroups = array(-1=>-1);
2521     } else {
2522         $mygroups[-1] = -1;
2523     }
2525     list($mygroups_sql, $params) = $DB->get_in_or_equal($mygroups);
2526     $params[] = $forum->id;
2528     if (!empty($CFG->forum_enabletimedposts)) {
2529         $timedsql = "AND d.timestart < $now AND (d.timeend = 0 OR d.timeend > $now)";
2530         $params[] = $now;
2531         $params[] = $now;
2532     } else {
2533         $timedsql = "";
2534     }
2536     $sql = "SELECT COUNT(d.id)
2537               FROM {forum_discussions} d
2538              WHERE d.groupid $mygroups_sql AND d.forum = ?
2539                    $timedsql";
2541     return $DB->get_field_sql($sql, $params);
2544 /**
2545  * How many posts by other users are unrated by a given user in the given discussion?
2546  *
2547  * TODO: Is this function still used anywhere?
2548  *
2549  * @param int $discussionid
2550  * @param int $userid
2551  * @return mixed
2552  */
2553 function forum_count_unrated_posts($discussionid, $userid) {
2554     global $CFG, $DB;
2556     $sql = "SELECT COUNT(*) as num
2557               FROM {forum_posts}
2558              WHERE parent > 0
2559                AND discussion = :discussionid
2560                AND userid <> :userid";
2561     $params = array('discussionid' => $discussionid, 'userid' => $userid);
2562     $posts = $DB->get_record_sql($sql, $params);
2563     if ($posts) {
2564         $sql = "SELECT count(*) as num
2565                   FROM {forum_posts} p,
2566                        {rating} r
2567                  WHERE p.discussion = :discussionid AND
2568                        p.id = r.itemid AND
2569                        r.userid = userid AND
2570                        r.component = 'mod_forum' AND
2571                        r.ratingarea = 'post'";
2572         $rated = $DB->get_record_sql($sql, $params);
2573         if ($rated) {
2574             if ($posts->num > $rated->num) {
2575                 return $posts->num - $rated->num;
2576             } else {
2577                 return 0;    // Just in case there was a counting error
2578             }
2579         } else {
2580             return $posts->num;
2581         }
2582     } else {
2583         return 0;
2584     }
2587 /**
2588  * Get all discussions in a forum
2589  *
2590  * @global object
2591  * @global object
2592  * @global object
2593  * @uses CONTEXT_MODULE
2594  * @uses VISIBLEGROUPS
2595  * @param object $cm
2596  * @param string $forumsort
2597  * @param bool $fullpost
2598  * @param int $unused
2599  * @param int $limit
2600  * @param bool $userlastmodified
2601  * @param int $page
2602  * @param int $perpage
2603  * @return array
2604  */
2605 function forum_get_discussions($cm, $forumsort="d.timemodified DESC", $fullpost=true, $unused=-1, $limit=-1, $userlastmodified=false, $page=-1, $perpage=0) {
2606     global $CFG, $DB, $USER;
2608     $timelimit = '';
2610     $now = round(time(), -2);
2611     $params = array($cm->instance);
2613     $modcontext = context_module::instance($cm->id);
2615     if (!has_capability('mod/forum:viewdiscussion', $modcontext)) { /// User must have perms to view discussions
2616         return array();
2617     }
2619     if (!empty($CFG->forum_enabletimedposts)) { /// Users must fulfill timed posts
2621         if (!has_capability('mod/forum:viewhiddentimedposts', $modcontext)) {
2622             $timelimit = " AND ((d.timestart <= ? AND (d.timeend = 0 OR d.timeend > ?))";
2623             $params[] = $now;
2624             $params[] = $now;
2625             if (isloggedin()) {
2626                 $timelimit .= " OR d.userid = ?";
2627                 $params[] = $USER->id;
2628             }
2629             $timelimit .= ")";
2630         }
2631     }
2633     if ($limit > 0) {
2634         $limitfrom = 0;
2635         $limitnum  = $limit;
2636     } else if ($page != -1) {
2637         $limitfrom = $page*$perpage;
2638         $limitnum  = $perpage;
2639     } else {
2640         $limitfrom = 0;
2641         $limitnum  = 0;
2642     }
2644     $groupmode    = groups_get_activity_groupmode($cm);
2645     $currentgroup = groups_get_activity_group($cm);
2647     if ($groupmode) {
2648         if (empty($modcontext)) {
2649             $modcontext = context_module::instance($cm->id);
2650         }
2652         if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $modcontext)) {
2653             if ($currentgroup) {
2654                 $groupselect = "AND (d.groupid = ? OR d.groupid = -1)";
2655                 $params[] = $currentgroup;
2656             } else {
2657                 $groupselect = "";
2658             }
2660         } else {
2661             //seprate groups without access all
2662             if ($currentgroup) {
2663                 $groupselect = "AND (d.groupid = ? OR d.groupid = -1)";
2664                 $params[] = $currentgroup;
2665             } else {
2666                 $groupselect = "AND d.groupid = -1";
2667             }
2668         }
2669     } else {
2670         $groupselect = "";
2671     }
2674     if (empty($forumsort)) {
2675         $forumsort = "d.timemodified DESC";
2676     }
2677     if (empty($fullpost)) {
2678         $postdata = "p.id,p.subject,p.modified,p.discussion,p.userid";
2679     } else {
2680         $postdata = "p.*";
2681     }
2683     if (empty($userlastmodified)) {  // We don't need to know this
2684         $umfields = "";
2685         $umtable  = "";
2686     } else {
2687         $umfields = '';
2688         $umnames = get_all_user_name_fields();
2689         foreach ($umnames as $umname) {
2690             $umfields .= ', um.' . $umname . ' AS um' . $umname;
2691         }
2692         $umtable  = " LEFT JOIN {user} um ON (d.usermodified = um.id)";
2693     }
2695     $allnames = get_all_user_name_fields(true, 'u');
2696     $sql = "SELECT $postdata, d.name, d.timemodified, d.usermodified, d.groupid, d.timestart, d.timeend, $allnames,
2697                    u.email, u.picture, u.imagealt $umfields
2698               FROM {forum_discussions} d
2699                    JOIN {forum_posts} p ON p.discussion = d.id
2700                    JOIN {user} u ON p.userid = u.id
2701                    $umtable
2702              WHERE d.forum = ? AND p.parent = 0
2703                    $timelimit $groupselect
2704           ORDER BY $forumsort";
2705     return $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
2708 /**
2709  *
2710  * @global object
2711  * @global object
2712  * @global object
2713  * @uses CONTEXT_MODULE
2714  * @uses VISIBLEGROUPS
2715  * @param object $cm
2716  * @return array
2717  */
2718 function forum_get_discussions_unread($cm) {
2719     global $CFG, $DB, $USER;
2721     $now = round(time(), -2);
2722     $cutoffdate = $now - ($CFG->forum_oldpostdays*24*60*60);
2724     $params = array();
2725     $groupmode    = groups_get_activity_groupmode($cm);
2726     $currentgroup = groups_get_activity_group($cm);
2728     if ($groupmode) {
2729         $modcontext = context_module::instance($cm->id);
2731         if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $modcontext)) {
2732             if ($currentgroup) {
2733                 $groupselect = "AND (d.groupid = :currentgroup OR d.groupid = -1)";
2734                 $params['currentgroup'] = $currentgroup;
2735             } else {
2736                 $groupselect = "";
2737             }
2739         } else {
2740             //separate groups without access all
2741             if ($currentgroup) {
2742                 $groupselect = "AND (d.groupid = :currentgroup OR d.groupid = -1)";
2743                 $params['currentgroup'] = $currentgroup;
2744             } else {
2745                 $groupselect = "AND d.groupid = -1";
2746             }
2747         }
2748     } else {
2749         $groupselect = "";
2750     }
2752     if (!empty($CFG->forum_enabletimedposts)) {
2753         $timedsql = "AND d.timestart < :now1 AND (d.timeend = 0 OR d.timeend > :now2)";
2754         $params['now1'] = $now;
2755         $params['now2'] = $now;
2756     } else {
2757         $timedsql = "";
2758     }
2760     $sql = "SELECT d.id, COUNT(p.id) AS unread
2761               FROM {forum_discussions} d
2762                    JOIN {forum_posts} p     ON p.discussion = d.id
2763                    LEFT JOIN {forum_read} r ON (r.postid = p.id AND r.userid = $USER->id)
2764              WHERE d.forum = {$cm->instance}
2765                    AND p.modified >= :cutoffdate AND r.id is NULL
2766                    $groupselect
2767                    $timedsql
2768           GROUP BY d.id";
2769     $params['cutoffdate'] = $cutoffdate;
2771     if ($unreads = $DB->get_records_sql($sql, $params)) {
2772         foreach ($unreads as $unread) {
2773             $unreads[$unread->id] = $unread->unread;
2774         }
2775         return $unreads;
2776     } else {
2777         return array();
2778     }
2781 /**
2782  * @global object
2783  * @global object
2784  * @global object
2785  * @uses CONEXT_MODULE
2786  * @uses VISIBLEGROUPS
2787  * @param object $cm
2788  * @return array
2789  */
2790 function forum_get_discussions_count($cm) {
2791     global $CFG, $DB, $USER;
2793     $now = round(time(), -2);
2794     $params = array($cm->instance);
2795     $groupmode    = groups_get_activity_groupmode($cm);
2796     $currentgroup = groups_get_activity_group($cm);
2798     if ($groupmode) {
2799         $modcontext = context_module::instance($cm->id);
2801         if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $modcontext)) {
2802             if ($currentgroup) {
2803                 $groupselect = "AND (d.groupid = ? OR d.groupid = -1)";
2804                 $params[] = $currentgroup;
2805             } else {
2806                 $groupselect = "";
2807             }
2809         } else {
2810             //seprate groups without access all
2811             if ($currentgroup) {
2812                 $groupselect = "AND (d.groupid = ? OR d.groupid = -1)";
2813                 $params[] = $currentgroup;
2814             } else {
2815                 $groupselect = "AND d.groupid = -1";
2816             }
2817         }
2818     } else {
2819         $groupselect = "";
2820     }
2822     $cutoffdate = $now - ($CFG->forum_oldpostdays*24*60*60);
2824     $timelimit = "";
2826     if (!empty($CFG->forum_enabletimedposts)) {
2828         $modcontext = context_module::instance($cm->id);
2830         if (!has_capability('mod/forum:viewhiddentimedposts', $modcontext)) {
2831             $timelimit = " AND ((d.timestart <= ? AND (d.timeend = 0 OR d.timeend > ?))";
2832             $params[] = $now;
2833             $params[] = $now;
2834             if (isloggedin()) {
2835                 $timelimit .= " OR d.userid = ?";
2836                 $params[] = $USER->id;
2837             }
2838             $timelimit .= ")";
2839         }
2840     }
2842     $sql = "SELECT COUNT(d.id)
2843               FROM {forum_discussions} d
2844                    JOIN {forum_posts} p ON p.discussion = d.id
2845              WHERE d.forum = ? AND p.parent = 0
2846                    $groupselect $timelimit";
2848     return $DB->get_field_sql($sql, $params);
2852 /**
2853  * Get all discussions started by a particular user in a course (or group)
2854  * This function no longer used ...
2855  *
2856  * @todo Remove this function if no longer used
2857  * @global object
2858  * @global object
2859  * @param int $courseid
2860  * @param int $userid
2861  * @param int $groupid
2862  * @return array
2863  */
2864 function forum_get_user_discussions($courseid, $userid, $groupid=0) {
2865     global $CFG, $DB;
2866     $params = array($courseid, $userid);
2867     if ($groupid) {
2868         $groupselect = " AND d.groupid = ? ";
2869         $params[] = $groupid;
2870     } else  {
2871         $groupselect = "";
2872     }
2874     $allnames = get_all_user_name_fields(true, 'u');
2875     return $DB->get_records_sql("SELECT p.*, d.groupid, $allnames, u.email, u.picture, u.imagealt,
2876                                    f.type as forumtype, f.name as forumname, f.id as forumid
2877                               FROM {forum_discussions} d,
2878                                    {forum_posts} p,
2879                                    {user} u,
2880                                    {forum} f
2881                              WHERE d.course = ?
2882                                AND p.discussion = d.id
2883                                AND p.parent = 0
2884                                AND p.userid = u.id
2885                                AND u.id = ?
2886                                AND d.forum = f.id $groupselect
2887                           ORDER BY p.created DESC", $params);
2890 /**
2891  * Get the list of potential subscribers to a forum.
2892  *
2893  * @param object $forumcontext the forum context.
2894  * @param integer $groupid the id of a group, or 0 for all groups.
2895  * @param string $fields the list of fields to return for each user. As for get_users_by_capability.
2896  * @param string $sort sort order. As for get_users_by_capability.
2897  * @return array list of users.
2898  */
2899 function forum_get_potential_subscribers($forumcontext, $groupid, $fields, $sort = '') {
2900     global $DB;
2902     // only active enrolled users or everybody on the frontpage
2903     list($esql, $params) = get_enrolled_sql($forumcontext, 'mod/forum:allowforcesubscribe', $groupid, true);
2904     if (!$sort) {
2905         list($sort, $sortparams) = users_order_by_sql('u');
2906         $params = array_merge($params, $sortparams);
2907     }
2909     $sql = "SELECT $fields
2910               FROM {user} u
2911               JOIN ($esql) je ON je.id = u.id
2912           ORDER BY $sort";
2914     return $DB->get_records_sql($sql, $params);
2917 /**
2918  * Returns list of user objects that are subscribed to this forum
2919  *
2920  * @global object
2921  * @global object
2922  * @param object $course the course
2923  * @param forum $forum the forum
2924  * @param integer $groupid group id, or 0 for all.
2925  * @param object $context the forum context, to save re-fetching it where possible.
2926  * @param string $fields requested user fields (with "u." table prefix)
2927  * @return array list of users.
2928  */
2929 function forum_subscribed_users($course, $forum, $groupid=0, $context = null, $fields = null) {
2930     global $CFG, $DB;
2932     $allnames = get_all_user_name_fields(true, 'u');
2933     if (empty($fields)) {
2934         $fields ="u.id,
2935                   u.username,
2936                   $allnames,
2937                   u.maildisplay,
2938                   u.mailformat,
2939                   u.maildigest,
2940                   u.imagealt,
2941                   u.email,
2942                   u.emailstop,
2943                   u.city,
2944                   u.country,
2945                   u.lastaccess,
2946                   u.lastlogin,
2947                   u.picture,
2948                   u.timezone,
2949                   u.theme,
2950                   u.lang,
2951                   u.trackforums,
2952                   u.mnethostid";
2953     }
2955     if (empty($context)) {
2956         $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id);
2957         $context = context_module::instance($cm->id);
2958     }
2960     if (forum_is_forcesubscribed($forum)) {
2961         $results = forum_get_potential_subscribers($context, $groupid, $fields, "u.email ASC");
2963     } else {
2964         // only active enrolled users or everybody on the frontpage
2965         list($esql, $params) = get_enrolled_sql($context, '', $groupid, true);
2966         $params['forumid'] = $forum->id;
2967         $results = $DB->get_records_sql("SELECT $fields
2968                                            FROM {user} u
2969                                            JOIN ($esql) je ON je.id = u.id
2970                                            JOIN {forum_subscriptions} s ON s.userid = u.id
2971                                           WHERE s.forum = :forumid
2972                                        ORDER BY u.email ASC", $params);
2973     }
2975     // Guest user should never be subscribed to a forum.
2976     unset($results[$CFG->siteguest]);
2978     return $results;
2983 // OTHER FUNCTIONS ///////////////////////////////////////////////////////////
2986 /**
2987  * @global object
2988  * @global object
2989  * @param int $courseid
2990  * @param string $type
2991  */
2992 function forum_get_course_forum($courseid, $type) {
2993 // How to set up special 1-per-course forums
2994     global $CFG, $DB, $OUTPUT, $USER;
2996     if ($forums = $DB->get_records_select("forum", "course = ? AND type = ?", array($courseid, $type), "id ASC")) {
2997         // There should always only be ONE, but with the right combination of
2998         // errors there might be more.  In this case, just return the oldest one (lowest ID).
2999         foreach ($forums as $forum) {
3000             return $forum;   // ie the first one
3001         }
3002     }
3004     // Doesn't exist, so create one now.
3005     $forum = new stdClass();
3006     $forum->course = $courseid;
3007     $forum->type = "$type";
3008     if (!empty($USER->htmleditor)) {
3009         $forum->introformat = $USER->htmleditor;
3010     }
3011     switch ($forum->type) {
3012         case "news":
3013             $forum->name  = get_string("namenews", "forum");
3014             $forum->intro = get_string("intronews", "forum");
3015             $forum->forcesubscribe = FORUM_FORCESUBSCRIBE;
3016             $forum->assessed = 0;
3017             if ($courseid == SITEID) {
3018                 $forum->name  = get_string("sitenews");
3019                 $forum->forcesubscribe = 0;
3020             }
3021             break;
3022         case "social":
3023             $forum->name  = get_string("namesocial", "forum");
3024             $forum->intro = get_string("introsocial", "forum");
3025             $forum->assessed = 0;
3026             $forum->forcesubscribe = 0;
3027             break;
3028         case "blog":
3029             $forum->name = get_string('blogforum', 'forum');
3030             $forum->intro = get_string('introblog', 'forum');
3031             $forum->assessed = 0;
3032             $forum->forcesubscribe = 0;
3033             break;
3034         default:
3035             echo $OUTPUT->notification("That forum type doesn't exist!");
3036             return false;
3037             break;
3038     }
3040     $forum->timemodified = time();
3041     $forum->id = $DB->insert_record("forum", $forum);
3043     if (! $module = $DB->get_record("modules", array("name" => "forum"))) {
3044         echo $OUTPUT->notification("Could not find forum module!!");
3045         return false;
3046     }
3047     $mod = new stdClass();
3048     $mod->course = $courseid;
3049     $mod->module = $module->id;
3050     $mod->instance = $forum->id;
3051     $mod->section = 0;
3052     include_once("$CFG->dirroot/course/lib.php");
3053     if (! $mod->coursemodule = add_course_module($mod) ) {
3054         echo $OUTPUT->notification("Could not add a new course module to the course '" . $courseid . "'");
3055         return false;
3056     }
3057     $sectionid = course_add_cm_to_section($courseid, $mod->coursemodule, 0);
3058     return $DB->get_record("forum", array("id" => "$forum->id"));
3062 /**
3063  * Given the data about a posting, builds up the HTML to display it and
3064  * returns the HTML in a string.  This is designed for sending via HTML email.
3065  *
3066  * @global object
3067  * @param object $course
3068  * @param object $cm
3069  * @param object $forum
3070  * @param object $discussion
3071  * @param object $post
3072  * @param object $userform
3073  * @param object $userto
3074  * @param bool $ownpost
3075  * @param bool $reply
3076  * @param bool $link
3077  * @param bool $rate
3078  * @param string $footer
3079  * @return string
3080  */
3081 function forum_make_mail_post($course, $cm, $forum, $discussion, $post, $userfrom, $userto,
3082                               $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
3084     global $CFG, $OUTPUT;
3086     $modcontext = context_module::instance($cm->id);
3088     if (!isset($userto->viewfullnames[$forum->id])) {
3089         $viewfullnames = has_capability('moodle/site:viewfullnames', $modcontext, $userto->id);
3090     } else {
3091         $viewfullnames = $userto->viewfullnames[$forum->id];
3092     }
3094     // add absolute file links
3095     $post->message = file_rewrite_pluginfile_urls($post->message, 'pluginfile.php', $modcontext->id, 'mod_forum', 'post', $post->id);
3097     // format the post body
3098     $options = new stdClass();
3099     $options->para = true;
3100     $formattedtext = format_text($post->message, $post->messageformat, $options, $course->id);
3102     $output = '<table border="0" cellpadding="3" cellspacing="0" class="forumpost">';
3104     $output .= '<tr class="header"><td width="35" valign="top" class="picture left">';
3105     $output .= $OUTPUT->user_picture($userfrom, array('courseid'=>$course->id));
3106     $output .= '</td>';
3108     if ($post->parent) {
3109         $output .= '<td class="topic">';
3110     } else {
3111         $output .= '<td class="topic starter">';
3112     }
3113     $output .= '<div class="subject">'.format_string($post->subject).'</div>';
3115     $fullname = fullname($userfrom, $viewfullnames);
3116     $by = new stdClass();
3117     $by->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$userfrom->id.'&amp;course='.$course->id.'">'.$fullname.'</a>';
3118     $by->date = userdate($post->modified, '', $userto->timezone);
3119     $output .= '<div class="author">'.get_string('bynameondate', 'forum', $by).'</div>';
3121     $output .= '</td></tr>';
3123     $output .= '<tr><td class="left side" valign="top">';
3125     if (isset($userfrom->groups)) {
3126         $groups = $userfrom->groups[$forum->id];
3127     } else {
3128         $groups = groups_get_all_groups($course->id, $userfrom->id, $cm->groupingid);
3129     }
3131     if ($groups) {
3132         $output .= print_group_picture($groups, $course->id, false, true, true);
3133     } else {
3134         $output .= '&nbsp;';
3135     }
3137     $output .= '</td><td class="content">';
3139     $attachments = forum_print_attachments($post, $cm, 'html');
3140     if ($attachments !== '') {
3141         $output .= '<div class="attachments">';
3142         $output .= $attachments;
3143         $output .= '</div>';
3144     }
3146     $output .= $formattedtext;
3148 // Commands
3149     $commands = array();
3151     if ($post->parent) {
3152         $commands[] = '<a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.
3153                       $post->discussion.'&amp;parent='.$post->parent.'">'.get_string('parent', 'forum').'</a>';
3154     }
3156     if ($reply) {
3157         $commands[] = '<a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/post.php?reply='.$post->id.'">'.
3158                       get_string('reply', 'forum').'</a>';
3159     }
3161     $output .= '<div class="commands">';
3162     $output .= implode(' | ', $commands);
3163     $output .= '</div>';
3165 // Context link to post if required
3166     if ($link) {
3167         $output .= '<div class="link">';
3168         $output .= '<a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id.'">'.
3169                      get_string('postincontext', 'forum').'</a>';
3170         $output .= '</div>';
3171     }
3173     if ($footer) {
3174         $output .= '<div class="footer">'.$footer.'</div>';
3175     }
3176     $output .= '</td></tr></table>'."\n\n";
3178     return $output;
3181 /**
3182  * Print a forum post
3183  *
3184  * @global object
3185  * @global object
3186  * @uses FORUM_MODE_THREADED
3187  * @uses PORTFOLIO_FORMAT_PLAINHTML
3188  * @uses PORTFOLIO_FORMAT_FILE
3189  * @uses PORTFOLIO_FORMAT_RICHHTML
3190  * @uses PORTFOLIO_ADD_TEXT_LINK
3191  * @uses CONTEXT_MODULE
3192  * @param object $post The post to print.
3193  * @param object $discussion
3194  * @param object $forum
3195  * @param object $cm
3196  * @param object $course
3197  * @param boolean $ownpost Whether this post belongs to the current user.
3198  * @param boolean $reply Whether to print a 'reply' link at the bottom of the message.
3199  * @param boolean $link Just print a shortened version of the post as a link to the full post.
3200  * @param string $footer Extra stuff to print after the message.
3201  * @param string $highlight Space-separated list of terms to highlight.
3202  * @param int $post_read true, false or -99. If we already know whether this user
3203  *          has read this post, pass that in, otherwise, pass in -99, and this
3204  *          function will work it out.
3205  * @param boolean $dummyifcantsee When forum_user_can_see_post says that
3206  *          the current user can't see this post, if this argument is true
3207  *          (the default) then print a dummy 'you can't see this post' post.
3208  *          If false, don't output anything at all.
3209  * @param bool|null $istracked
3210  * @return void
3211  */
3212 function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=false, $reply=false, $link=false,
3213                           $footer="", $highlight="", $postisread=null, $dummyifcantsee=true, $istracked=null, $return=false) {
3214     global $USER, $CFG, $OUTPUT;
3216     require_once($CFG->libdir . '/filelib.php');
3218     // String cache
3219     static $str;
3221     $modcontext = context_module::instance($cm->id);
3223     $post->course = $course->id;
3224     $post->forum  = $forum->id;
3225     $post->message = file_rewrite_pluginfile_urls($post->message, 'pluginfile.php', $modcontext->id, 'mod_forum', 'post', $post->id);
3226     if (!empty($CFG->enableplagiarism)) {
3227         require_once($CFG->libdir.'/plagiarismlib.php');
3228         $post->message .= plagiarism_get_links(array('userid' => $post->userid,
3229             'content' => $post->message,
3230             'cmid' => $cm->id,
3231             'course' => $post->course,
3232             'forum' => $post->forum));
3233     }
3235     // caching
3236     if (!isset($cm->cache)) {
3237         $cm->cache = new stdClass;
3238     }
3240     if (!isset($cm->cache->caps)) {
3241         $cm->cache->caps = array();
3242         $cm->cache->caps['mod/forum:viewdiscussion']   = has_capability('mod/forum:viewdiscussion', $modcontext);
3243         $cm->cache->caps['moodle/site:viewfullnames']  = has_capability('moodle/site:viewfullnames', $modcontext);
3244         $cm->cache->caps['mod/forum:editanypost']      = has_capability('mod/forum:editanypost', $modcontext);
3245         $cm->cache->caps['mod/forum:splitdiscussions'] = has_capability('mod/forum:splitdiscussions', $modcontext);
3246         $cm->cache->caps['mod/forum:deleteownpost']    = has_capability('mod/forum:deleteownpost', $modcontext);
3247         $cm->cache->caps['mod/forum:deleteanypost']    = has_capability('mod/forum:deleteanypost', $modcontext);
3248         $cm->cache->caps['mod/forum:viewanyrating']    = has_capability('mod/forum:viewanyrating', $modcontext);
3249         $cm->cache->caps['mod/forum:exportpost']       = has_capability('mod/forum:exportpost', $modcontext);
3250         $cm->cache->caps['mod/forum:exportownpost']    = has_capability('mod/forum:exportownpost', $modcontext);
3251     }
3253     if (!isset($cm->uservisible)) {
3254         $cm->uservisible = coursemodule_visible_for_user($cm);
3255     }
3257     if ($istracked && is_null($postisread)) {
3258         $postisread = forum_tp_is_post_read($USER->id, $post);
3259     }
3261     if (!forum_user_can_see_post($forum, $discussion, $post, NULL, $cm)) {
3262         $output = '';
3263         if (!$dummyifcantsee) {
3264             if ($return) {
3265                 return $output;
3266             }
3267             echo $output;
3268             return;
3269         }
3270         $output .= html_writer::tag('a', '', array('id'=>'p'.$post->id));
3271         $output .= html_writer::start_tag('div', array('class'=>'forumpost clearfix'));
3272         $output .= html_writer::start_tag('div', array('class'=>'row header'));
3273         $output .= html_writer::tag('div', '', array('class'=>'left picture')); // Picture
3274         if ($post->parent) {
3275             $output .= html_writer::start_tag('div', array('class'=>'topic'));
3276         } else {
3277             $output .= html_writer::start_tag('div', array('class'=>'topic starter'));
3278         }
3279         $output .= html_writer::tag('div', get_string('forumsubjecthidden','forum'), array('class'=>'subject')); // Subject
3280         $output .= html_writer::tag('div', get_string('forumauthorhidden','forum'), array('class'=>'author')); // author
3281         $output .= html_writer::end_tag('div');
3282         $output .= html_writer::end_tag('div'); // row
3283         $output .= html_writer::start_tag('div', array('class'=>'row'));
3284         $output .= html_writer::tag('div', '&nbsp;', array('class'=>'left side')); // Groups
3285         $output .= html_writer::tag('div', get_string('forumbodyhidden','forum'), array('class'=>'content')); // Content
3286         $output .= html_writer::end_tag('div'); // row
3287         $output .= html_writer::end_tag('div'); // forumpost
3289         if ($return) {
3290             return $output;
3291         }
3292         echo $output;
3293         return;
3294     }
3296     if (empty($str)) {
3297         $str = new stdClass;
3298         $str->edit         = get_string('edit', 'forum');
3299         $str->delete       = get_string('delete', 'forum');
3300         $str->reply        = get_string('reply', 'forum');
3301         $str->parent       = get_string('parent', 'forum');
3302         $str->pruneheading = get_string('pruneheading', 'forum');
3303         $str->prune        = get_string('prune', 'forum');
3304         $str->displaymode     = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
3305         $str->markread     = get_string('markread', 'forum');
3306         $str->markunread   = get_string('markunread', 'forum');
3307     }
3309     $discussionlink = new moodle_url('/mod/forum/discuss.php', array('d'=>$post->discussion));
3311     // Build an object that represents the posting user
3312     $postuser = new stdClass;
3313     $postuser->id        = $post->userid;
3314     foreach (get_all_user_name_fields() as $addname) {
3315         $postuser->$addname  = $post->$addname;
3316     }
3317     $postuser->imagealt  = $post->imagealt;
3318     $postuser->picture   = $post->picture;
3319     $postuser->email     = $post->email;
3320     // Some handy things for later on
3321     $postuser->fullname    = fullname($postuser, $cm->cache->caps['moodle/site:viewfullnames']);
3322     $postuser->profilelink = new moodle_url('/user/view.php', array('id'=>$post->userid, 'course'=>$course->id));
3324     // Prepare the groups the posting user belongs to
3325     if (isset($cm->cache->usersgroups)) {
3326         $groups = array();
3327         if (isset($cm->cache->usersgroups[$post->userid])) {
3328             foreach ($cm->cache->usersgroups[$post->userid] as $gid) {
3329                 $groups[$gid] = $cm->cache->groups[$gid];
3330             }
3331         }
3332     } else {
3333         $groups = groups_get_all_groups($course->id, $post->userid, $cm->groupingid);
3334     }
3336     // Prepare the attachements for the post, files then images
3337     list($attachments, $attachedimages) = forum_print_attachments($post, $cm, 'separateimages');
3339     // Determine if we need to shorten this post
3340     $shortenpost = ($link && (strlen(strip_tags($post->message)) > $CFG->forum_longpost));
3343     // Prepare an array of commands
3344     $commands = array();
3346     // SPECIAL CASE: The front page can display a news item post to non-logged in users.
3347     // Don't display the mark read / unread controls in this case.
3348     if ($istracked && $CFG->forum_usermarksread && isloggedin()) {
3349         $url = new moodle_url($discussionlink, array('postid'=>$post->id, 'mark'=>'unread'));
3350         $text = $str->markunread;
3351         if (!$postisread) {
3352             $url->param('mark', 'read');
3353             $text = $str->markread;
3354         }
3355         if ($str->displaymode == FORUM_MODE_THREADED) {
3356             $url->param('parent', $post->parent);
3357         } else {
3358             $url->set_anchor('p'.$post->id);
3359         }
3360         $commands[] = array('url'=>$url, 'text'=>$text);
3361     }
3363     // Zoom in to the parent specifically
3364     if ($post->parent) {
3365         $url = new moodle_url($discussionlink);
3366         if ($str->displaymode == FORUM_MODE_THREADED) {
3367             $url->param('parent', $post->parent);
3368         } else {
3369             $url->set_anchor('p'.$post->parent);
3370         }
3371         $commands[] = array('url'=>$url, 'text'=>$str->parent);
3372     }
3374     // Hack for allow to edit news posts those are not displayed yet until they are displayed
3375     $age = time() - $post->created;
3376     if (!$post->parent && $forum->type == 'news' && $discussion->timestart > time()) {
3377         $age = 0;
3378     }
3380     if ($forum->type == 'single' and $discussion->firstpost == $post->id) {
3381         if (has_capability('moodle/course:manageactivities', $modcontext)) {
3382             // The first post in single simple is the forum description.
3383             $commands[] = array('url'=>new moodle_url('/course/modedit.php', array('update'=>$cm->id, 'sesskey'=>sesskey(), 'return'=>1)), 'text'=>$str->edit);
3384         }
3385     } else if (($ownpost && $age < $CFG->maxeditingtime) || $cm->cache->caps['mod/forum:editanypost']) {
3386         $commands[] = array('url'=>new moodle_url('/mod/forum/post.php', array('edit'=>$post->id)), 'text'=>$str->edit);
3387     }
3389     if ($cm->cache->caps['mod/forum:splitdiscussions'] && $post->parent && $forum->type != 'single') {
3390         $commands[] = array('url'=>new moodle_url('/mod/forum/post.php', array('prune'=>$post->id)), 'text'=>$str->prune, 'title'=>$str->pruneheading);
3391     }
3393     if ($forum->type == 'single' and $discussion->firstpost == $post->id) {
3394         // Do not allow deleting of first post in single simple type.
3395     } else if (($ownpost && $age < $CFG->maxeditingtime && $cm->cache->caps['mod/forum:deleteownpost']) || $cm->cache->caps['mod/forum:deleteanypost']) {
3396         $commands[] = array('url'=>new moodle_url('/mod/forum/post.php', array('delete'=>$post->id)), 'text'=>$str->delete);
3397     }
3399     if ($reply) {
3400         $commands[] = array('url'=>new moodle_url('/mod/forum/post.php#mformforum', array('reply'=>$post->id)), 'text'=>$str->reply);
3401     }
3403     if ($CFG->enableportfolios && ($cm->cache->caps['mod/forum:exportpost'] || ($ownpost && $cm->cache->caps['mod/forum:exportownpost']))) {
3404         $p = array('postid' => $post->id);
3405         require_once($CFG->libdir.'/portfoliolib.php');
3406         $button = new portfolio_add_button();
3407         $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id), 'mod_forum');
3408         if (empty($attachments)) {
3409             $button->set_formats(PORTFOLIO_FORMAT_PLAINHTML);
3410         } else {
3411             $button->set_formats(PORTFOLIO_FORMAT_RICHHTML);
3412         }
3414         $porfoliohtml = $button->to_html(PORTFOLIO_ADD_TEXT_LINK);
3415         if (!empty($porfoliohtml)) {
3416             $commands[] = $porfoliohtml;
3417         }
3418     }
3419     // Finished building commands
3422     // Begin output
3424     $output  = '';
3426     if ($istracked) {
3427         if ($postisread) {
3428             $forumpostclass = ' read';
3429         } else {
3430             $forumpostclass = ' unread';
3431             $output .= html_writer::tag('a', '', array('name'=>'unread'));
3432         }
3433     } else {
3434         // ignore trackign status if not tracked or tracked param missing
3435         $forumpostclass = '';
3436     }
3438     $topicclass = '';
3439     if (empty($post->parent)) {
3440         $topicclass = ' firstpost starter';
3441     }
3443     $output .= html_writer::tag('a', '', array('id'=>'p'.$post->id));
3444     $output .= html_writer::start_tag('div', array('class'=>'forumpost clearfix'.$forumpostclass.$topicclass));
3445     $output .= html_writer::start_tag('div', array('class'=>'row header clearfix'));
3446     $output .= html_writer::start_tag('div', array('class'=>'left picture'));
3447     $output .= $OUTPUT->user_picture($postuser, array('courseid'=>$course->id));
3448     $output .= html_writer::end_tag('div');
3451     $output .= html_writer::start_tag('div', array('class'=>'topic'.$topicclass));
3453     $postsubject = $post->subject;
3454     if (empty($post->subjectnoformat)) {
3455         $postsubject = format_string($postsubject);
3456     }
3457     $output .= html_writer::tag('div', $postsubject, array('class'=>'subject'));
3459     $by = new stdClass();
3460     $by->name = html_writer::link($postuser->profilelink, $postuser->fullname);
3461     $by->date = userdate($post->modified);
3462     $output .= html_writer::tag('div', get_string('bynameondate', 'forum', $by), array('class'=>'author'));
3464     $output .= html_writer::end_tag('div'); //topic
3465     $output .= html_writer::end_tag('div'); //row
3467     $output .= html_writer::start_tag('div', array('class'=>'row maincontent clearfix'));
3468     $output .= html_writer::start_tag('div', array('class'=>'left'));
3470     $groupoutput = '';
3471     if ($groups) {
3472         $groupoutput = print_group_picture($groups, $course->id, false, true, true);
3473     }
3474     if (empty($groupoutput)) {
3475         $groupoutput = '&nbsp;';
3476     }
3477     $output .= html_writer::tag('div', $groupoutput, array('class'=>'grouppictures'));
3479     $output .= html_writer::end_tag('div'); //left side
3480     $output .= html_writer::start_tag('div', array('class'=>'no-overflow'));
3481     $output .= html_writer::start_tag('div', array('class'=>'content'));
3482     if (!empty($attachments)) {
3483         $output .= html_writer::tag('div', $attachments, array('class'=>'attachments'));
3484     }
3486     $options = new stdClass;
3487     $options->para    = false;
3488     $options->trusted = $post->messagetrust;
3489     $options->context = $modcontext;
3490     if ($shortenpost) {
3491         // Prepare shortened version
3492         $postclass    = 'shortenedpost';
3493         $postcontent  = format_text(forum_shorten_post($post->message), $post->messageformat, $options, $course->id);
3494         $postcontent .= html_writer::link($discussionlink, get_string('readtherest', 'forum'));
3495         $postcontent .= html_writer::tag('div', '('.get_string('numwords', 'moodle', count_words($post->message)).')',
3496             array('class'=>'post-word-count'));
3497     } else {
3498         // Prepare whole post
3499         $postclass    = 'fullpost';
3500         $postcontent  = format_text($post->message, $post->messageformat, $options, $course->id);
3501         if (!empty($highlight)) {
3502             $postcontent = highlight($highlight, $postcontent);
3503         }
3504         if (!empty($forum->displaywordcount)) {
3505             $postcontent .= html_writer::tag('div', get_string('numwords', 'moodle', count_words($post->message)),
3506                 array('class'=>'post-word-count'));
3507         }
3508         $postcontent .= html_writer::tag('div', $attachedimages, array('class'=>'attachedimages'));
3509     }
3511     // Output the post content
3512     $output .= html_writer::tag('div', $postcontent, array('class'=>'posting '.$postclass));
3513     $output .= html_writer::end_tag('div'); // Content
3514     $output .= html_writer::end_tag('div'); // Content mask
3515     $output .= html_writer::end_tag('div'); // Row
3517     $output .= html_writer::start_tag('div', array('class'=>'row side'));
3518     $output .= html_writer::tag('div','&nbsp;', array('class'=>'left'));
3519     $output .= html_writer::start_tag('div', array('class'=>'options clearfix'));
3521     // Output ratings
3522     if (!empty($post->rating)) {
3523         $output .= html_writer::tag('div', $OUTPUT->render($post->rating), array('class'=>'forum-post-rating'));
3524     }
3526     // Output the commands
3527     $commandhtml = array();
3528     foreach ($commands as $command) {
3529         if (is_array($command)) {
3530             $commandhtml[] = html_writer::link($command['url'], $command['text']);
3531         } else {
3532             $commandhtml[] = $command;
3533         }
3534     }
3535     $output .= html_writer::tag('div', implode(' | ', $commandhtml), array('class'=>'commands'));
3537     // Output link to post if required
3538     if ($link) {
3539         if ($post->replies == 1) {
3540             $replystring = get_string('repliesone', 'forum', $post->replies);
3541         } else {
3542             $replystring = get_string('repliesmany', 'forum', $post->replies);
3543         }
3545         $output .= html_writer::start_tag('div', array('class'=>'link'));
3546         $output .= html_writer::link($discussionlink, get_string('discussthistopic', 'forum'));
3547         $output .= '&nbsp;('.$replystring.')';
3548         $output .= html_writer::end_tag('div'); // link
3549     }
3551     // Output footer if required
3552     if ($footer) {
3553         $output .= html_writer::tag('div', $footer, array('class'=>'footer'));
3554     }
3556     // Close remaining open divs
3557     $output .= html_writer::end_tag('div'); // content
3558     $output .= html_writer::end_tag('div'); // row
3559     $output .= html_writer::end_tag('div'); // forumpost
3561     // Mark the forum post as read if required
3562     if ($istracked && !$CFG->forum_usermarksread && !$postisread) {
3563         forum_tp_mark_post_read($USER->id, $post, $forum->id);
3564     }
3566     if ($return) {
3567         return $output;
3568     }
3569     echo $output;
3570     return;
3573 /**
3574  * Return rating related permissions
3575  *
3576  * @param string $options the context id
3577  * @return array an associative array of the user's rating permissions
3578  */
3579 function forum_rating_permissions($contextid, $component, $ratingarea) {
3580     $context = context::instance_by_id($contextid, MUST_EXIST);
3581     if ($component != 'mod_forum' || $ratingarea != 'post') {
3582         // We don't know about this component/ratingarea so just return null to get the
3583         // default restrictive permissions.
3584         return null;
3585     }
3586     return array(
3587         'view'    => has_capability('mod/forum:viewrating', $context),
3588         'viewany' => has_capability('mod/forum:viewanyrating', $context),
3589         'viewall' => has_capability('mod/forum:viewallratings', $context),
3590         'rate'    => has_capability('mod/forum:rate', $context)
3591     );
3594 /**
3595  * Validates a submitted rating
3596  * @param array $params submitted data
3597  *            context => object the context in which the rated items exists [required]
3598  *            component => The component for this module - should always be mod_forum [required]
3599  *            ratingarea => object the context in which the rated items exists [required]
3600  *            itemid => int the ID of the object being rated [required]
3601  *            scaleid => int the scale from which the user can select a rating. Used for bounds checking. [required]
3602  *            rating => int the submitted rating [required]
3603  *            rateduserid => int the id of the user whose items have been rated. NOT the user who submitted the ratings. 0 to update all. [required]
3604  *            aggregation => int the aggregation method to apply when calculating grades ie RATING_AGGREGATE_AVERAGE [required]
3605  * @return boolean true if the rating is valid. Will throw rating_exception if not
3606  */
3607 function forum_rating_validate($params) {
3608     global $DB, $USER;
3610     // Check the component is mod_forum
3611     if ($params['component'] != 'mod_forum') {
3612         throw new rating_exception('invalidcomponent');
3613     }
3615     // Check the ratingarea is post (the only rating area in forum)
3616     if ($params['ratingarea'] != 'post') {
3617         throw new rating_exception('invalidratingarea');
3618     }
3620     // Check the rateduserid is not the current user .. you can't rate your own posts
3621     if ($params['rateduserid'] == $USER->id) {
3622         throw new rating_exception('nopermissiontorate');
3623     }
3625     // Fetch all the related records ... we need to do this anyway to call forum_user_can_see_post
3626     $post = $DB->get_record('forum_posts', array('id' => $params['itemid'], 'userid' => $params['rateduserid']), '*', MUST_EXIST);
3627     $discussion = $DB->get_record('forum_discussions', array('id' => $post->discussion), '*', MUST_EXIST);
3628     $forum = $DB->get_record('forum', array('id' => $discussion->forum), '*', MUST_EXIST);
3629     $course = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST);
3630     $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id , false, MUST_EXIST);
3631     $context = context_module::instance($cm->id);
3633     // Make sure the context provided is the context of the forum
3634     if ($context->id != $params['context']->id) {
3635         throw new rating_exception('invalidcontext');
3636     }
3638     if ($forum->scale != $params['scaleid']) {
3639         //the scale being submitted doesnt match the one in the database
3640         throw new rating_exception('invalidscaleid');
3641     }
3643     // check the item we're rating was created in the assessable time window
3644     if (!empty($forum->assesstimestart) && !empty($forum->assesstimefinish)) {
3645         if ($post->created < $forum->assesstimestart || $post->created > $forum->assesstimefinish) {
3646             throw new rating_exception('notavailable');
3647         }
3648     }
3650     //check that the submitted rating is valid for the scale
3652     // lower limit
3653     if ($params['rating'] < 0  && $params['rating'] != RATING_UNSET_RATING) {
3654         throw new rating_exception('invalidnum');
3655     }
3657     // upper limit
3658     if ($forum->scale < 0) {
3659         //its a custom scale
3660         $scalerecord = $DB->get_record('scale', array('id' => -$forum->scale));
3661         if ($scalerecord) {
3662             $scalearray = explode(',', $scalerecord->scale);
3663             if ($params['rating'] > count($scalearray)) {
3664                 throw new rating_exception('invalidnum');
3665             }
3666         } else {
3667             throw new rating_exception('invalidscaleid');
3668         }
3669     } else if ($params['rating'] > $forum->scale) {
3670         //if its numeric and submitted rating is above maximum
3671         throw new rating_exception('invalidnum');
3672     }
3674     // Make sure groups allow this user to see the item they're rating
3675     if ($discussion->groupid > 0 and $groupmode = groups_get_activity_groupmode($cm, $course)) {   // Groups are being used
3676         if (!groups_group_exists($discussion->groupid)) { // Can't find group
3677             throw new rating_exception('cannotfindgroup');//something is wrong
3678         }
3680         if (!groups_is_member($discussion->groupid) and !has_capability('moodle/site:accessallgroups', $context)) {
3681             // do not allow rating of posts from other groups when in SEPARATEGROUPS or VISIBLEGROUPS
3682             throw new rating_exception('notmemberofgroup');
3683         }
3684     }
3686     // perform some final capability checks
3687     if (!forum_user_can_see_post($forum, $discussion, $post, $USER, $cm)) {
3688         throw new rating_exception('nopermissiontorate');
3689     }
3691     return true;
3695 /**
3696  * This function prints the overview of a discussion in the forum listing.
3697  * It needs some discussion information and some post information, these
3698  * happen to be combined for efficiency in the $post parameter by the function
3699  * that calls this one: forum_print_latest_discussions()
3700  *
3701  * @global object
3702  * @global object
3703  * @param object $post The post object (passed by reference for speed).
3704  * @param object $forum The forum object.
3705  * @param int $group Current group.
3706  * @param string $datestring Format to use for the dates.
3707  * @param boolean $cantrack Is tracking enabled for this forum.
3708  * @param boolean $forumtracked Is the user tracking this forum.
3709  * @param boolean $canviewparticipants True if user has the viewparticipants permission for this course
3710  */
3711 function forum_print_discussion_header(&$post, $forum, $group=-1, $datestring="",
3712                                         $cantrack=true, $forumtracked=true, $canviewparticipants=true, $modcontext=NULL) {
3714     global $USER, $CFG, $OUTPUT;
3716     static $rowcount;
3717     static $strmarkalldread;
3719     if (empty($modcontext)) {
3720         if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course)) {
3721             print_error('invalidcoursemodule');
3722         }
3723         $modcontext = context_module::instance($cm->id);
3724     }
3726     if (!isset($rowcount)) {
3727         $rowcount = 0;
3728         $strmarkalldread = get_string('markalldread', 'forum');
3729     } else {
3730         $rowcount = ($rowcount + 1) % 2;
3731     }
3733     $post->subject = format_string($post->subject,true);
3735     echo "\n\n";
3736     echo '<tr class="discussion r'.$rowcount.'">';
3738     // Topic
3739     echo '<td class="topic starter">';
3740     echo '<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'">'.$post->subject.'</a>';
3741     echo "</td>\n";
3743     // Picture
3744     $postuser = new stdClass();
3745     $postuser->id = $post->userid;
3746     foreach (get_all_user_name_fields() as $addname) {
3747         $postuser->$addname = $post->$addname;
3748     }
3749     $postuser->imagealt = $post->imagealt;
3750     $postuser->picture = $post->picture;
3751     $postuser->email = $post->email;
3753     echo '<td class="picture">';
3754     echo $OUTPUT->user_picture($postuser, array('courseid'=>$forum->course));
3755     echo "</td>\n";
3757     // User name
3758     $fullname = fullname($postuser, has_capability('moodle/site:viewfullnames', $modcontext));
3759     echo '<td class="author">';
3760     echo '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$post->userid.'&amp;course='.$forum->course.'">'.$fullname.'</a>';
3761     echo "</td>\n";
3763     // Group picture
3764     if ($group !== -1) {  // Groups are active - group is a group data object or NULL
3765         echo '<td class="picture group">';
3766         if (!empty($group->picture) and empty($group->hidepicture)) {
3767             print_group_picture($group, $forum->course, false, false, true);
3768         } else if (isset($group->id)) {
3769             if($canviewparticipants) {
3770                 echo '<a href="'.$CFG->wwwroot.'/user/index.php?id='.$forum->course.'&amp;group='.$group->id.'">'.$group->name.'</a>';
3771             } else {
3772                 echo $group->name;
3773             }
3774         }
3775         echo "</td>\n";
3776     }
3778     if (has_capability('mod/forum:viewdiscussion', $modcontext)) {   // Show the column with replies
3779         echo '<td class="replies">';
3780         echo '<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'">';
3781         echo $post->replies.'</a>';
3782         echo "</td>\n";
3784         if ($cantrack) {
3785             echo '<td class="replies">';
3786             if ($forumtracked) {
3787                 if ($post->unread > 0) {
3788                     echo '<span class="unread">';
3789                     echo '<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#unread">';
3790                     echo $post->unread;
3791                     echo '</a>';
3792                     echo '<a title="'.$strmarkalldread.'" href="'.$CFG->wwwroot.'/mod/forum/markposts.php?f='.
3793                          $forum->id.'&amp;d='.$post->discussion.'&amp;mark=read&amp;returnpage=view.php">' .
3794                          '<img src="'.$OUTPUT->pix_url('t/markasread') . '" class="iconsmall" alt="'.$strmarkalldread.'" /></a>';
3795                     echo '</span>';
3796                 } else {
3797                     echo '<span class="read">';
3798                     echo $post->unread;
3799                     echo '</span>';
3800                 }
3801             } else {
3802                 echo '<span class="read">';
3803                 echo '-';
3804                 echo '</span>';
3805             }
3806             echo "</td>\n";
3807         }
3808     }
3810     echo '<td class="lastpost">';
3811     $usedate = (empty($post->timemodified)) ? $post->modified : $post->timemodified;  // Just in case
3812     $parenturl = (empty($post->lastpostid)) ? '' : '&amp;parent='.$post->lastpostid;
3813     $usermodified = new stdClass();
3814     $usermodified->id        = $post->usermodified;
3815     foreach (get_all_user_name_fields() as $addname) {
3816         $temp = 'um' . $addname;
3817         $usermodified->$addname = $post->$temp;
3818     }
3819     echo '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$post->usermodified.'&amp;course='.$forum->course.'">'.
3820          fullname($usermodified).'</a><br />';
3821     echo '<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.$parenturl.'">'.
3822           userdate($usedate, $datestring).'</a>';
3823     echo "</td>\n";
3825     echo "</tr>\n\n";
3830 /**
3831  * Given a post object that we already know has a long message
3832  * this function truncates the message nicely to the first
3833  * sane place between $CFG->forum_longpost and $CFG->forum_shortpost
3834  *
3835  * @global object
3836  * @param string $message
3837  * @return string
3838  */
3839 function forum_shorten_post($message) {
3841    global $CFG;
3843    $i = 0;
3844    $tag = false;
3845    $length = strlen($message);
3846    $count = 0;
3847    $stopzone = false;
3848    $truncate = 0;
3850    for ($i=0; $i<$length; $i++) {
3851        $char = $message[$i];
3853        switch ($char) {
3854            case "<":
3855                $tag = true;
3856                break;
3857            case ">":
3858                $tag = false;
3859                break;
3860            default:
3861                if (!$tag) {
3862                    if ($stopzone) {
3863                        if ($char == ".") {
3864                            $truncate = $i+1;
3865                            break 2;
3866                        }
3867                    }
3868                    $count++;
3869                }
3870                break;
3871        }
3872        if (!$stopzone) {
3873            if ($count > $CFG->forum_shortpost) {
3874                $stopzone = true;
3875            }
3876        }
3877    }
3879    if (!$truncate) {
3880        $truncate = $i;
3881    }
3883    return substr($message, 0, $truncate);
3886 /**
3887  * Print the drop down that allows the user to select how they want to have
3888  * the discussion displayed.
3889  *
3890  * @param int $id forum id if $forumtype is 'single',
3891  *              discussion id for any other forum type
3892  * @param mixed $mode forum layout mode
3893  * @param string $forumtype optional
3894  */
3895 function forum_print_mode_form($id, $mode, $forumtype='') {
3896     global $OUTPUT;
3897     if ($forumtype == 'single') {
3898         $select = new single_select(new moodle_url("/mod/forum/view.php", array('f'=>$id)), 'mode', forum_get_layout_modes(), $mode, null, "mode");
3899         $select->set_label(get_string('displaymode', 'forum'), array('class' => 'accesshide'));
3900         $select->class = "forummode";
3901     } else {
3902         $select = new single_select(new moodle_url("/mod/forum/discuss.php", array('d'=>$id)), 'mode', forum_get_layout_modes(), $mode, null, "mode");
3903         $select->set_label(get_string('displaymode', 'forum'), array('class' => 'accesshide'));
3904     }
3905     echo $OUTPUT->render($select);
3908 /**
3909  * @global object
3910  * @param object $course
3911  * @param string $search
3912  * @return string
3913  */
3914 function forum_search_form($course, $search='') {
3915     global $CFG, $OUTPUT;
3917     $output  = '<div class="forumsearch">';
3918     $output .= '<form action="'.$CFG->wwwroot.'/mod/forum/search.php" style="display:inline">';
3919     $output .= '<fieldset class="invisiblefieldset">';
3920     $output .= $OUTPUT->help_icon('search');
3921     $output .= '<label class="accesshide" for="search" >'.get_string('search', 'forum').'</label>';
3922     $output .= '<input id="search" name="search" type="text" size="18" value="'.s($search, true).'" alt="search" />';
3923     $output .= '<label class="accesshide" for="searchforums" >'.get_string('searchforums', 'forum').'</label>';
3924     $output .= '<input id="searchforums" value="'.get_string('searchforums', 'forum').'" type="submit" />';
3925     $output .= '<input name="id" type="hidden" value="'.$course->id.'" />';
3926     $output .= '</fieldset>';
3927     $output .= '</form>';
3928     $output .= '</div>';
3930     return $output;
3934 /**
3935  * @global object
3936  * @global object
3937  */
3938 function forum_set_return() {
3939     global $CFG, $SESSION;
3941     if (! isset($SESSION->fromdiscussion)) {
3942         if (!empty($_SERVER['HTTP_REFERER'])) {
3943             $referer = $_SERVER['HTTP_REFERER'];
3944         } else {
3945             $referer = "";
3946         }
3947         // If the referer is NOT a login screen then save it.
3948         if (! strncasecmp("$CFG->wwwroot/login", $referer, 300)) {
3949             $SESSION->fromdiscussion = $_SERVER["HTTP_REFERER"];
3950         }
3951     }
3955 /**
3956  * @global object
3957  * @param string $default
3958  * @return string
3959  */
3960 function forum_go_back_to($default) {
3961     global $SESSION;
3963     if (!empty($SESSION->fromdiscussion)) {
3964         $returnto = $SESSION->fromdiscussion;
3965         unset($SESSION->fromdiscussion);
3966         return $returnto;
3967     } else {
3968         return $default;
3969     }
3972 /**
3973  * Given a discussion object that is being moved to $forumto,
3974  * this function checks all posts in that discussion
3975  * for attachments, and if any are found, these are
3976  * moved to the new forum directory.
3977  *
3978  * @global object
3979  * @param object $discussion
3980  * @param int $forumfrom source forum id
3981  * @param int $forumto target forum id
3982  * @return bool success
3983  */
3984 function forum_move_attachments($discussion, $forumfrom, $forumto) {
3985     global $DB;
3987     $fs = get_file_storage();
3989     $newcm = get_coursemodule_from_instance('forum', $forumto);
3990     $oldcm = get_coursemodule_from_instance('forum', $forumfrom);
3992     $newcontext = context_module::instance($newcm->id);
3993     $oldcontext = context_module::instance($oldcm->id);
3995     // loop through all posts, better not use attachment flag ;-)
3996     if ($posts = $DB->get_records('forum_posts', array('discussion'=>$discussion->id), '', 'id, attachment')) {
3997         foreach ($posts as $post) {
3998             $fs->move_area_files_to_new_context($oldcontext->id,
3999                     $newcontext->id, 'mod_forum', 'post', $post->id);
4000             $attachmentsmoved = $fs->move_area_files_to_new_context($oldcontext->id,
4001                     $newcontext->id, 'mod_forum', 'attachment', $post->id);
4002             if ($attachmentsmoved > 0 && $post->attachment != '1') {
4003                 // Weird - let's fix it
4004                 $post->attachment = '1';
4005                 $DB->update_record('forum_posts', $post);
4006             } else if ($attachmentsmoved == 0 && $post->attachment != '') {
4007                 // Weird - let's fix it
4008                 $post->attachment = '';
4009                 $DB->update_record('forum_posts', $post);
4010             }
4011         }
4012     }
4014     return true;
4017 /**
4018  * Returns attachments as formated text/html optionally with separate images
4019  *
4020  * @global object
4021  * @global object
4022  * @global object
4023  * @param object $post
4024  * @param object $cm
4025  * @param string $type html/text/separateimages
4026  * @return mixed string or array of (html text withouth images and image HTML)
4027  */
4028 function forum_print_attachments($post, $cm, $type) {
4029     global $CFG, $DB, $USER, $OUTPUT;
4031     if (empty($post->attachment)) {
4032         return $type !== 'separateimages' ? '' : array('', '');
4033     }
4035     if (!in_array($type, array('separateimages', 'html', 'text'))) {
4036         return $type !== 'separateimages' ? '' : array('', '');
4037     }
4039     if (!$context = context_module::instance($cm->id)) {
4040         return $type !== 'separateimages' ? '' : array('', '');
4041     }
4042     $strattachment = get_string('attachment', 'forum');
4044     $fs = get_file_storage();
4046     $imagereturn = '';
4047     $output = '';
4049     $canexport = !empty($CFG->enableportfolios) && (has_capability('mod/forum:exportpost', $context) || ($post->userid == $USER->id && has_capability('mod/forum:exportownpost', $context)));
4051     if ($canexport) {
4052         require_once($CFG->libdir.'/portfoliolib.php');
4053     }
4055     $files = $fs->get_area_files($context->id, 'mod_forum', 'attachment', $post->id, "timemodified", false);
4056     if ($files) {
4057         if ($canexport) {
4058             $button = new portfolio_add_button();
4059         }
4060         foreach ($files as $file) {
4061             $filename = $file->get_filename();
4062             $mimetype = $file->get_mimetype();
4063             $iconimage = $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon'));
4064             $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$context->id.'/mod_forum/attachment/'.$post->id.'/'.$filename);
4066             if ($type == 'html') {
4067                 $output .= "<a href=\"$path\">$iconimage</a> ";
4068                 $output .= "<a href=\"$path\">".s($filename)."</a>";
4069                 if ($canexport) {
4070                     $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id, 'attachment' => $file->get_id()), 'mod_forum');
4071                     $button->set_format_by_file($file);
4072                     $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
4073                 }
4074                 $output .= "<br />";
4076             } else if ($type == 'text') {
4077                 $output .= "$strattachment ".s($filename).":\n$path\n";
4079             } else { //'returnimages'
4080                 if (in_array($mimetype, array('image/gif', 'image/jpeg', 'image/png'))) {
4081                     // Image attachments don't get printed as links
4082                     $imagereturn .= "<br /><img src=\"$path\" alt=\"\" />";
4083                     if ($canexport) {
4084                         $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id, 'attachment' => $file->get_id()), 'mod_forum');
4085                         $button->set_format_by_file($file);
4086                         $imagereturn .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
4087                     }
4088                 } else {
4089                     $output .= "<a href=\"$path\">$iconimage</a> ";
4090                     $output .= format_text("<a href=\"$path\">".s($filename)."</a>", FORMAT_HTML, array('context'=>$context));
4091                     if ($canexport) {
4092                         $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id, 'attachment' => $file->get_id()), 'mod_forum');
4093                         $button->set_format_by_file($file);
4094                         $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
4095                     }
4096                     $output .= '<br />';
4097                 }
4098             }
4100             if (!empty($CFG->enableplagiarism)) {
4101                 require_once($CFG->libdir.'/plagiarismlib.php');
4102                 $output .= plagiarism_get_links(array('userid' => $post->userid,
4103                     'file' => $file,
4104                     'cmid' => $cm->id,
4105                     'course' => $post->course,
4106                     'forum' => $post->forum));
4107                 $output .= '<br />';
4108             }
4109         }