From b9a7a37ec0669cdacb74bc80ef9d60cd277d6716 Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Thu, 12 Apr 2007 04:21:41 +0000 Subject: [PATCH] Converted all function comments to PHPdoc style, and added stub phpdocs for functions without comments. --- mod/forum/lib.php | 686 ++++++++++++++++++++++++++++++---------------- 1 file changed, 457 insertions(+), 229 deletions(-) diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 854d23d3b570d..968fbd364b45f 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -86,12 +86,13 @@ /// STANDARD FUNCTIONS /////////////////////////////////////////////////////////// +/** + * Given an object containing all the necessary data, + * (defined by the form in mod.html) this function + * will create a new instance and return the id number + * of the new instance. +*/ function forum_add_instance($forum) { -// Given an object containing all the necessary data, -// (defined by the form in mod.html) this function -// will create a new instance and return the id number -// of the new instance. - global $CFG; $forum->timemodified = time(); @@ -134,11 +135,12 @@ function forum_add_instance($forum) { } +/** + * Given an object containing all the necessary data, + * (defined by the form in mod.html) this function + * will update an existing instance with new data. +*/ function forum_update_instance($forum) { -// Given an object containing all the necessary data, -// (defined by the form in mod.html) this function -// will update an existing instance with new data. - $forum->timemodified = time(); $forum->id = $forum->instance; @@ -183,10 +185,12 @@ function forum_update_instance($forum) { } +/** + * Given an ID of an instance of this module, + * this function will permanently delete the instance + * and any data that depends on it. + */ function forum_delete_instance($id) { -// Given an ID of an instance of this module, -// this function will permanently delete the instance -// and any data that depends on it. if (!$forum = get_record('forum', 'id', $id)) { return false; @@ -216,12 +220,12 @@ function forum_delete_instance($id) { } +/** + * Function to be run periodically according to the moodle cron + * Finds all posts that have yet to be mailed out, and mails them + * out to all subscribers +*/ function forum_cron() { - -/// Function to be run periodically according to the moodle cron -/// Finds all posts that have yet to be mailed out, and mails them -/// out to all subscribers - global $CFG, $USER; $CFG->enablerecordcache = true; // We want all the caching we can get @@ -245,18 +249,18 @@ function forum_cron() { $subscribedusers = array(); - /// Posts older than 2 days will not be mailed. This is to avoid the problem where - /// cron has not been running for a long time, and then suddenly people are flooded - /// with mail from the past few weeks or months + // Posts older than 2 days will not be mailed. This is to avoid the problem where + // cron has not been running for a long time, and then suddenly people are flooded + // with mail from the past few weeks or months $timenow = time(); $endtime = $timenow - $CFG->maxeditingtime; - $starttime = $endtime - 48 * 3600; /// Two days earlier + $starttime = $endtime - 48 * 3600; // Two days earlier if ($posts = forum_get_unmailed_posts($starttime, $endtime)) { - /// Mark them all now as being mailed. It's unlikely but possible there - /// might be an error later so that a post is NOT actually mailed out, - /// but since mail isn't crucial, we can accept this risk. Doing it now - /// prevents the risk of duplicated mails, which is a worse problem. + // Mark them all now as being mailed. It's unlikely but possible there + // might be an error later so that a post is NOT actually mailed out, + // but since mail isn't crucial, we can accept this risk. Doing it now + // prevents the risk of duplicated mails, which is a worse problem. if (!forum_mark_old_posts_as_mailed($endtime)) { mtrace('Errors occurred while trying to mark some posts as being mailed.'); @@ -343,8 +347,8 @@ function forum_cron() { mtrace('Processing user '.$userto->id); - /// we might want to add another layer - forums here (by checking array_keys($subscribedusers)) - /// so that we can skip many posts + // we might want to add another layer - forums here (by checking array_keys($subscribedusers)) + // so that we can skip many posts foreach ($posts as $pid => $post) { @@ -452,7 +456,7 @@ function forum_cron() { } else { $mailcount[$post->id]++; - /// Mark post as read if forum_usermarksread is set off + // Mark post as read if forum_usermarksread is set off if (!$CFG->forum_usermarksread && forum_tp_can_track_forums($forum, $userto) && forum_tp_is_tracked($forum, $userto->id)) { if (!forum_tp_mark_post_read($userto->id, $post, $forum->id)) { @@ -481,7 +485,7 @@ function forum_cron() { $sitetimezone = $CFG->timezone; - /// Now see if there are any digest mails waiting to be sent, and if we should send them + // Now see if there are any digest mails waiting to be sent, and if we should send them if (!isset($CFG->digestmailtimelast)) { // To catch the first time set_config('digestmailtimelast', 0); @@ -574,8 +578,8 @@ function forum_cron() { delete_records('forum_queue', 'userid', $userid); $userto = $users[$userid]; - /// Override the language and timezone of the "current" user, so that - /// mail is customised for the receiver. + // Override the language and timezone of the "current" user, so that + // mail is customised for the receiver. $USER = $userto; course_setup(SITEID); @@ -597,7 +601,7 @@ function forum_cron() { foreach ($thesediscussions as $discussionid) { - @set_time_limit(120); /// to be reset for each post + @set_time_limit(120); // to be reset for each post $discussion = $discussions[$discussionid]; $forum = $forums[$discussion->forum]; @@ -635,8 +639,8 @@ function forum_cron() { $postsarray = $discussionposts[$discussionid]; sort($postsarray); - /// Create an empty array to use for marking read posts. - /// (I'm sure there's already a structure I can use here, but I can't be sure.) + // Create an empty array to use for marking read posts. + // (I'm sure there's already a structure I can use here, but I can't be sure.) $markread = array(); foreach ($postsarray as $postid) { @@ -667,7 +671,7 @@ function forum_cron() { $posttext .= forum_make_mail_text($course, $forum, $discussion, $post, $userfrom, $userto, true); $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, $canreply, true, false); - /// Create an array of postid's for this user to mark as read. + // Create an array of postid's for this user to mark as read. if (!$CFG->forum_usermarksread && forum_tp_can_track_forums($forum, $userto) && forum_tp_is_tracked($forum, $userto->id)) { @@ -701,7 +705,7 @@ function forum_cron() { mtrace("success."); $usermailcount++; - /// Mark post as read if forum_usermarksread is set off + // Mark post as read if forum_usermarksread is set off if (!$CFG->forum_usermarksread && forum_tp_can_track_forums($forum->id, $userto) && forum_tp_is_tracked($forum->id, $userto->id)) { @@ -738,6 +742,9 @@ function forum_cron() { return true; } +/** + * TODO document + */ function forum_make_mail_text($course, $forum, $discussion, $post, $userfrom, $userto, $bare = false) { global $CFG, $USER; @@ -796,6 +803,9 @@ function forum_make_mail_text($course, $forum, $discussion, $post, $userfrom, $u return $posttext; } +/** + * TODO document + */ function forum_make_mail_html($course, $forum, $discussion, $post, $userfrom, $userto) { global $CFG; @@ -835,7 +845,9 @@ function forum_make_mail_html($course, $forum, $discussion, $post, $userfrom, $u return $posthtml; } - +/** + * TODO document + */ function forum_user_outline($course, $user, $mod, $forum) { if ($posts = forum_get_user_posts($forum->id, $user->id)) { @@ -849,6 +861,9 @@ function forum_user_outline($course, $user, $mod, $forum) { } +/** + * TODO document + */ function forum_user_complete($course, $user, $mod, $forum) { global $CFG; @@ -864,6 +879,9 @@ function forum_user_complete($course, $user, $mod, $forum) { } } +/** + * TODO document + */ function forum_print_overview($courses,&$htmlarray) { global $USER, $CFG; $LIKE = sql_ilike(); @@ -963,9 +981,11 @@ function forum_print_overview($courses,&$htmlarray) { } } +/** + * Given a course and a date, prints a summary of all the new + * messages posted in the course since that date + */ function forum_print_recent_activity($course, $isteacher, $timestart) { -/// Given a course and a date, prints a summary of all the new -/// messages posted in the course since that date global $CFG; $LIKE = sql_ilike(); @@ -983,7 +1003,7 @@ function forum_print_recent_activity($course, $isteacher, $timestart) { $strftimerecent = get_string('strftimerecent'); $mygroupid = mygroupid($course->id); - $groupmode = array(); /// To cache group modes + $groupmode = array(); // To cache group modes $count = 0; foreach ($logs as $log) { @@ -1007,10 +1027,10 @@ function forum_print_recent_activity($course, $isteacher, $timestart) { } $modcontext = get_context_instance(CONTEXT_MODULE, $cm[$post->forum]->id); - /// Check whether this is belongs to a discussion in a group that - /// should NOT be accessible to the current user + // Check whether this is belongs to a discussion in a group that + // should NOT be accessible to the current user if (!has_capability('moodle/site:accessallgroups', $modcontext) - && $post->groupid != -1) { /// Open discussions have groupid -1 + && $post->groupid != -1) { // Open discussions have groupid -1 $groupmode[$post->forum] = groupmode($course, $cm[$post->forum]); @@ -1052,8 +1072,10 @@ function forum_print_recent_activity($course, $isteacher, $timestart) { } +/** + * Must return an array of grades, indexed by user, and a max grade. + */ function forum_grades($forumid) { -/// Must return an array of grades, indexed by user, and a max grade. if (!$forum = get_record("forum", "id", $forumid)) { return false; @@ -1118,9 +1140,11 @@ function forum_grades($forumid) { return $return; } +/** + * Returns the users with data in one forum + * (users with records in forum_subscriptions, forum_posts and forum_ratings, students) + */ function forum_get_participants($forumid) { -//Returns the users with data in one forum -//(users with records in forum_subscriptions, forum_posts and forum_ratings, students) global $CFG; @@ -1166,6 +1190,9 @@ function forum_get_participants($forumid) { return ($st_subscriptions); } +/** + * TODO document + */ function forum_scale_used ($forumid,$scaleid) { //This function returns if a scale is being used by one forum @@ -1180,11 +1207,13 @@ function forum_scale_used ($forumid,$scaleid) { return $return; } -/// SQL FUNCTIONS /////////////////////////////////////////////////////////// +// SQL FUNCTIONS /////////////////////////////////////////////////////////// +/** + * Gets a post with all info ready for forum_print_post + * Most of these joins are just to get the forum id + */ function forum_get_post_full($postid) { -/// Gets a post with all info ready for forum_print_post -/// Most of these joins are just to get the forum id global $CFG; return get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname, u.email, u.picture @@ -1194,10 +1223,12 @@ function forum_get_post_full($postid) { WHERE p.id = '$postid'"); } +/** + * Gets posts with all info ready for forum_print_post + * We pass forumid in because we always know it so no need to make a + * complicated join to find it out. + */ function forum_get_discussion_posts($discussion, $sort, $forumid) { -/// Gets posts with all info ready for forum_print_post -/// We pass forumid in because we always know it so no need to make a -/// complicated join to find it out. global $CFG; return get_records_sql("SELECT p.*, $forumid AS forum, u.firstname, u.lastname, u.email, u.picture @@ -1207,10 +1238,12 @@ function forum_get_discussion_posts($discussion, $sort, $forumid) { AND p.parent > 0 $sort"); } +/** + * Gets posts with all info ready for forum_print_post + * We pass forumid in because we always know it so no need to make a + * complicated join to find it out. + */ function forum_get_child_posts($parent, $forumid) { -/// Gets posts with all info ready for forum_print_post -/// We pass forumid in because we always know it so no need to make a -/// complicated join to find it out. global $CFG; return get_records_sql("SELECT p.*, $forumid AS forum, u.firstname, u.lastname, u.email, u.picture @@ -1240,8 +1273,8 @@ function forum_get_readable_forums($userid, $courseid=0) { if ($courseid) { $courses = get_records('course', 'id', $courseid); } else { - /// If no course is specified, then the user can see SITE + his courses. - /// And admins can see all courses, so pass the $doanything flag enabled + // If no course is specified, then the user can see SITE + his courses. + // And admins can see all courses, so pass the $doanything flag enabled $courses1 = get_records('course', 'id', SITEID); $courses2 = get_my_courses($userid, 'visible DESC,sortorder ASC', '*', true); $courses = array_merge($courses1, $courses2); @@ -1420,12 +1453,12 @@ function forum_search_posts($searchterms, $courseid=0, $limitfrom=0, $limitnum=5 if ($lexer->parse($searchstring)) { $parsearray = $parser->get_parsed_array(); - /// Experimental feature under 1.8! MDL-8830 - /// Use alternative text searches if defined - /// This feature only works under mysql until properly implemented for other DBs - /// Requires manual creation of text index for forum_posts before enabling it: - /// CREATE FULLTEXT INDEX foru_post_tix ON [prefix]forum_posts (subject, message) - /// Experimental feature under 1.8! MDL-8830 + // Experimental feature under 1.8! MDL-8830 + // Use alternative text searches if defined + // This feature only works under mysql until properly implemented for other DBs + // Requires manual creation of text index for forum_posts before enabling it: + // CREATE FULLTEXT INDEX foru_post_tix ON [prefix]forum_posts (subject, message) + // Experimental feature under 1.8! MDL-8830 if (!empty($CFG->forum_usetextsearches)) { $messagesearch = search_generate_text_SQL($parsearray, 'p.message', 'p.subject', 'p.userid', 'u.id', 'u.firstname', @@ -1466,8 +1499,10 @@ function forum_search_posts($searchterms, $courseid=0, $limitfrom=0, $limitnum=5 return get_records_sql($searchsql, $limitfrom, $limitnum); } +/** + * Returns a list of ratings for a particular post - sorted. + */ function forum_get_ratings($postid, $sort="u.firstname ASC") { -/// Returns a list of ratings for a particular post - sorted. global $CFG; return get_records_sql("SELECT u.*, r.rating, r.time FROM {$CFG->prefix}forum_ratings r, @@ -1477,8 +1512,10 @@ function forum_get_ratings($postid, $sort="u.firstname ASC") { ORDER BY $sort"); } +/** + * Returns a list of all new posts that have not been mailed yet + */ function forum_get_unmailed_posts($starttime, $endtime) { -/// Returns a list of all new posts that have not been mailed yet global $CFG; $now = time(); return get_records_sql("SELECT p.*, d.course @@ -1493,10 +1530,12 @@ function forum_get_unmailed_posts($starttime, $endtime) { ORDER BY p.modified ASC"); } +/** + * Marks posts before a certain time as being mailed already + */ function forum_mark_old_posts_as_mailed($endtime) { -/// Marks posts before a certain time as being mailed already global $CFG; -/// Find out posts those are not showing immediately so we can exclude them +// Find out posts those are not showing immediately so we can exclude them $now = time(); $delayed_posts = get_records_sql("SELECT p.id, p.discussion FROM {$CFG->prefix}forum_posts p, @@ -1519,8 +1558,10 @@ function forum_mark_old_posts_as_mailed($endtime) { AND mailed ='0'", false); } +/** + * Get all the posts for a user in a forum suitable for forum_print_post + */ function forum_get_user_posts($forumid, $userid) { -/// Get all the posts for a user in a forum suitable for forum_print_post global $CFG; return get_records_sql("SELECT p.*, d.forum, u.firstname, u.lastname, u.email, u.picture @@ -1536,8 +1577,10 @@ function forum_get_user_posts($forumid, $userid) { ORDER BY p.modified ASC"); } +/** + * Given a log entry, return the forum post details for it. + */ function forum_get_post_from_log($log) { -/// Given a log entry, return the forum post details for it. global $CFG; if ($log->action == "add post") { @@ -1572,8 +1615,10 @@ function forum_get_post_from_log($log) { return NULL; } +/** + * Given a discussion id, return the first post from the discussion + */ function forum_get_firstpost_from_discussion($discussionid) { -/// Given a discussion id, return the first post from the discussion global $CFG; return get_record_sql("SELECT p.* @@ -1584,8 +1629,10 @@ function forum_get_firstpost_from_discussion($discussionid) { } +/** + * Get all user grades for a forum + */ function forum_get_user_grades($forumid) { -/// Get all user grades for a forum global $CFG; return get_records_sql("SELECT r.id, p.userid, r.rating @@ -1599,8 +1646,10 @@ function forum_get_user_grades($forumid) { } +/** + * Returns an array of counts of replies to each discussion (optionally in one forum or course and/or user) + */ function forum_count_discussion_replies($forum='0', $course='0', $user='0') { -// Returns an array of counts of replies to each discussion (optionally in one forum or course and/or user) global $CFG; $forumselect = $courseselect = $userselect = ''; @@ -1622,8 +1671,10 @@ function forum_count_discussion_replies($forum='0', $course='0', $user='0') { GROUP BY p.discussion"); } +/** + * How many unrated posts are in the given discussion for a given user? + */ function forum_count_unrated_posts($discussionid, $userid) { -// How many unrated posts are in the given discussion for a given user? global $CFG; if ($posts = get_record_sql("SELECT count(*) as num FROM {$CFG->prefix}forum_posts @@ -1651,9 +1702,11 @@ function forum_count_unrated_posts($discussionid, $userid) { } } +/** + * Get all discussions in a forum + */ function forum_get_discussions($forum="0", $forumsort="d.timemodified DESC", $user=0, $fullpost=true, $visiblegroups=-1, $limit=0, $userlastmodified=false) { -/// Get all discussions in a forum global $CFG, $USER; $timelimit = ''; @@ -1739,9 +1792,11 @@ function forum_get_discussions($forum="0", $forumsort="d.timemodified DESC", +/** + * Get all discussions started by a particular user in a course (or group) + * This function no longer used ... + */ function forum_get_user_discussions($courseid, $userid, $groupid=0) { -/// Get all discussions started by a particular user in a course (or group) -/// This function no longer used ... global $CFG; if ($groupid) { @@ -1765,8 +1820,11 @@ function forum_get_user_discussions($courseid, $userid, $groupid=0) { ORDER BY p.created DESC"); } +/** + * Returns list of user objects that are subscribed to this forum + */ function forum_subscribed_users($course, $forum, $groupid=0, $cache=false) { -/// Returns list of user objects that are subscribed to this forum + global $CFG; static $resultscache = array(); @@ -1809,7 +1867,7 @@ function forum_subscribed_users($course, $forum, $groupid=0, $cache=false) { -/// OTHER FUNCTIONS /////////////////////////////////////////////////////////// +// OTHER FUNCTIONS /////////////////////////////////////////////////////////// function forum_get_course_forum($courseid, $type) { @@ -1880,11 +1938,13 @@ function forum_get_course_forum($courseid, $type) { } +/** +* Given the data about a posting, builds up the HTML to display it and +* returns the HTML in a string. This is designed for sending via HTML email. +*/ function forum_make_mail_post(&$post, $user, $touser, $course, $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") { - // Given the data about a posting, builds up the HTML to display it and - // returns the HTML in a string. This is designed for sending via HTML email. global $CFG, $USER; @@ -1941,7 +2001,7 @@ function forum_make_mail_post(&$post, $user, $touser, $course, $output .= $formattedtext; -/// Commands +// Commands $commands = array(); if ($post->parent) { @@ -1958,7 +2018,7 @@ function forum_make_mail_post(&$post, $user, $touser, $course, $output .= implode(' | ', $commands); $output .= ''; -/// Context link to post if required +// Context link to post if required if ($link) { $output .= ''; -/// Ratings +// Ratings $ratingsmenuused = false; if (!empty($ratings) and !empty($USER->id)) { @@ -2235,7 +2298,7 @@ function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link echo ''; } -/// Link to post if required +// Link to post if required if ($link) { echo '