"Display replies flat, with oldest first", "-1" => "Display replies flat, wth newest first", "2" => "Display replies in threaded form", "3" => "Display replies in nested form"); $FORUM_TYPE = array ("general" => "General forum", "eachuser" => "Each $student posts a topic", "single" => "A single simple discussion"); $FORUM_POST_RATINGS = array ("3" => "Outstanding", "2" => "Satisfactory", "1" => "Not satisfactory"); $FORUM_LONG_POST = 600; /// FUNCTIONS /////////////////////////////////////////////////////////// // How to set up special 1-per-course forums function get_course_news_forum($courseid) { if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'news'")) { return $forum; } else { // Doesn't exist, so create one now. $forum->course = $courseid; $forum->type = "news"; $forum->name = "News"; $forum->intro= "General news about this course"; $forum->open = 0; $forum->assessed = 0; $forum->forcesubscribe = 1; $forum->timemodified = time(); $forum->id = insert_record("forum", $forum); return get_record_sql("SELECT * from forum WHERE id = '$forum->id'"); } } function get_course_social_forum($courseid) { if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'social'")) { return $forum; } else { // Doesn't exist, so create one now. $forum->course = $courseid; $forum->type = "social"; $forum->name = "Social"; $forum->intro= "A forum for general socialising. Talk about anything you like!"; $forum->open = 1; $forum->assessed = 0; $forum->forcesubscribe = 0; $forum->timemodified = time(); $forum->id = insert_record("forum", $forum); return get_record_sql("SELECT * from forum WHERE id = '$forum->id'"); } } function get_course_teacher_forum($courseid) { if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'teacher'")) { return $forum; } else { // Doesn't exist, so create one now. $forum->course = $courseid; $forum->type = "teacher"; $forum->name = "Teacher Forum"; $forum->intro= "For teacher-only notes and discussion"; $forum->open = 0; $forum->assessed = 0; $forum->forcesubscribe = 0; $forum->timemodified = time(); $forum->id = insert_record("forum", $forum); return get_record_sql("SELECT * from forum WHERE id = '$forum->id'"); } } function 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 $THEME, $CFG; $output = ""; if ($post->parent) { $output .= "
"; $output .= ""; } else { $output .= "
"; $output .= ""; } $output .= ""; if ($post->parent) { $output .= ""; $output .= "
body\" WIDTH=35 VALIGN=TOP>"; $output .= print_user_picture($user->id, $course->id, $user->picture, false, true); $output .= "cellheading\">"; } else { $output .= "cellheading2\">"; } $output .= "

"; $output .= "$post->subject
"; $output .= "by wwwroot/user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname"; $output .= " on ".userdate($post->created, "", $touser->timezone); $output .= "

body\" WIDTH=10>"; $output .= " "; $output .= "\n"; $output .= text_to_html($post->message); $output .= "

"; $age = time() - $post->created; if ($ownpost) { $output .= "wwwroot/mod/forum/post.php?delete=$post->id\">Delete"; if ($reply) { $output .= "| wwwroot/mod/forum/post.php?reply=$post->id\">Reply"; } $output .= "  "; } else { if ($reply) { $output .= "wwwroot/mod/forum/post.php?reply=$post->id\">Reply  "; } } $output .= "

"; if ($link) { if ($post->replies == 1) { $replystring = "reply"; } else { $replystring = "replies"; } $output .= "wwwroot/mod/forum/discuss.php?d=$post->discussion\">Discuss this topic ($post->replies $replystring so far)  "; } $output .= "

"; if ($footer) { $output .= "

$footer

"; } $output .= "
\n"; $output .= "
\n\n"; return $output; } function print_post(&$post, $courseid, $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") { global $THEME, $USER, $CFG; if ($post->parent) { echo "
"; echo ""; } else { echo "
\n
"; echo ""; } echo ""; if ($post->parent) { echo ""; echo "
body\" WIDTH=35 VALIGN=TOP>"; print_user_picture($post->userid, $courseid, $post->picture); echo "cellheading\">"; } else { echo "cellheading2\">"; } echo "

"; echo "$post->subject
"; echo "by wwwroot/user/view.php?id=$post->userid&course=$courseid\">$post->firstname $post->lastname"; echo " on ".userdate($post->created); echo "

body\" WIDTH=10>"; echo " "; echo "\n"; if ($link && (strlen($post->message) > $FORUM_LONG_POST)) { // Print shortened version echo text_to_html(forum_shorten_post($post->message)); $numwords = count_words($post->message); echo "wwwroot/mod/forum/discuss.php?d=$post->discussion\">"; echo "Read the rest of this topic ($numwords words)..."; } else { // Print whole message echo text_to_html($post->message); } echo "

"; $age = time() - $post->created; if ($ownpost) { if ($age < $CFG->maxeditingtime) { echo "wwwroot/mod/forum/post.php?edit=$post->id\">Edit | "; } echo "wwwroot/mod/forum/post.php?delete=$post->id\">Delete"; if ($reply) { echo "| wwwroot/mod/forum/post.php?reply=$post->id\">Reply"; } echo "  "; } else { if ($reply) { echo "wwwroot/mod/forum/post.php?reply=$post->id\">Reply  "; } } echo "

"; if ($rate && $USER->id) { if ($USER->id == $post->userid) { print_ratings($post->id); } else { print_rating($post->id, $USER->id); } } if ($link) { if ($post->replies == 1) { $replystring = "reply"; } else { $replystring = "replies"; } echo "wwwroot/mod/forum/discuss.php?d=$post->discussion\">Discuss this topic ($post->replies $replystring so far)  "; } echo "

"; if ($footer) { echo "

$footer

"; } echo "
"; echo "
"; echo "
\n\n"; } function forum_shorten_post($message) { global $FORUM_LONG_POST; if (strlen($message) > $FORUM_LONG_POST) { // Look for the first return between 50 and $FORUM_LONG_POST $shortmessage = substr($message, 50, $FORUM_LONG_POST); if ($pos = strpos($shortmessage, "\n")) { return substr($message, 0, 50 + $pos); } else { return substr($message, 0, $FORUM_LONG_POST). "..."; } } else { return $message; } } function print_ratings($post) { global $CFG, $PHPSESSID; $notsatisfactory = 0; $satisfactory = 0; $outstanding = 0; if ($ratings = get_records_sql("SELECT * from forum_ratings WHERE post='$post'")) { foreach ($ratings as $rating) { switch ($rating->rating) { case 1: $notsatisfactory++; break; case 2: $satisfactory++; break; case 3: $outstanding++; break; } } $summary = "$outstanding/$satisfactory/$notsatisfactory"; echo "Ratings: "; link_to_popup_window ("/mod/forum/report.php?id=$post", "ratings", $summary, 400, 550); } else { echo ""; } } function print_rating($post, $user) { global $FORUM_POST_RATINGS; if ($rs = get_record_sql("SELECT rating from forum_ratings WHERE user='$user' AND post='$post'")) { echo "You rated this: "; if ($FORUM_POST_RATINGS[$rs->rating]) { echo $FORUM_POST_RATINGS[$rs->rating]; } else { echo "Error"; } echo ""; } else { choose_from_menu($FORUM_POST_RATINGS, $post, "", "Rate..."); } } function print_mode_form($discussion, $mode) { GLOBAL $FORUM_DISCUSS_MODES; echo "

"; popup_form("discuss.php?d=$discussion&mode=", $FORUM_DISCUSS_MODES, "mode", $mode, ""); echo "

\n"; } function print_forum_search_form($course, $search="") { global $CFG; echo "
"; echo "
wwwroot/mod/forum/search.php\">"; echo "
"; echo ""; echo "id\">"; echo "
"; echo "
"; } function count_discussion_replies($forum="0") { if ($forum) { $forumselect = " AND d.forum = '$forum'"; } return get_records_sql("SELECT p.discussion, (count(*)) as replies FROM forum_posts p, forum_discussions d WHERE p.parent > 0 AND p.discussion = d.id GROUP BY p.discussion"); } function set_fromdiscussion() { global $SESSION, $HTTP_REFERER; if (! $SESSION->fromdiscussion) { $SESSION->fromdiscussion = $HTTP_REFERER; } } function go_back_to($default) { global $SESSION; if ($SESSION->fromdiscussion) { $returnto = $SESSION->fromdiscussion; unset($SESSION->fromdiscussion); return $returnto; } else { return $default; } } function get_forum_post_full($postid) { return get_record_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.id as userid FROM forum_posts p, user u WHERE p.id = '$postid' AND p.user = u.id"); } function add_new_post_to_database($post) { $timenow = time(); $post->created = $timenow; $post->modified = $timenow; $post->mailed = "0"; return insert_record("forum_posts", $post); } function update_post_in_database($post) { global $db; $timenow = time(); $rs = $db->Execute("UPDATE forum_posts SET message='$post->message', subject='$post->subject', modified='$timenow' WHERE id = '$post->id'"); return $rs; } function forum_add_discussion($discussion) { // Given an object containing all the necessary data, // create a new discussion and return the id GLOBAL $USER; $timenow = time(); // The first post is stored as a real post, and linked // to from the discuss entry. $post->discussion = 0; $post->parent = 0; $post->user = $USER->id; $post->created = $timenow; $post->modified = $timenow; $post->mailed = 0; $post->subject = $discussion->name; $post->message = $discussion->intro; if (! $post->id = insert_record("forum_posts", $post) ) { return 0; } // Now do the real module entry $discussion->firstpost = $post->id; $discussion->timemodified = $timenow; if (! $discussion->id = insert_record("forum_discussions", $discussion) ) { return 0; } // Finally, set the pointer on the post. if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) { return 0; } return $discussion->id; } function forum_delete_discussion($discussion) { // $discussion is a discussion record object $result = true; if ($posts = get_records("forum_posts", "discussion", $discussion->id)) { foreach ($posts as $post) { if (! delete_records("forum_ratings", "post", "$post->id")) { $result = false; } } } if (! delete_records("forum_posts", "discussion", "$discussion->id")) { $result = false; } if (! delete_records("forum_discussions", "id", "$discussion->id")) { $result = false; } return $result; } function print_user_discussions($course, $user) { global $CFG; $topics = get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.id as userid FROM forum_discussions d, forum_posts p, user u, forum f WHERE d.course = '$course->id' AND p.discussion = d.id AND p.parent = 0 AND p.user = u.id AND u.id = '$user->id' AND d.forum = f.id AND f.type = 'eachuser' ORDER BY p.created DESC"); if ($topics) { echo "
"; print_heading("Discussion topics"); $replies = count_discussion_replies(); foreach ($topics as $topic) { if ($replies[$topic->discussion]) { $topic->replies = $replies[$topic->discussion]->replies; } else { $topic->replies = 0; } $ownpost = ($topic->userid == $USER->id); print_post($topic, $course->id, $ownpost, $reply=0, $link=1, $assessed=false); echo "
\n"; } } } function forum_user_summary($course, $user, $mod, $forum) { global $CFG; } function forum_user_outline($course, $user, $mod, $forum) { ///XXX global $CFG; if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture FROM forum_posts p, user u WHERE p.discussion='$discussion->id' AND p.user = '$user->id' AND p.user = u.id ORDER BY p.modified ASC")) { $result->info = count($posts)." posts"; $lastpost = array_pop($posts); $result->time = $lastpost->modified; return $result; } return NULL; } function forum_user_complete($course, $user, $mod, $forum) { global $CFG; ///XXX if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture FROM forum_posts p, user u WHERE p.discussion='$discussion->id' AND p.user = '$user->id' AND p.user = u.id ORDER BY p.modified")) { foreach ($posts as $post) { if ($post->parent) { $footer = "wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">Parent of this post"; } else { $footer = ""; } print_post($post, $course->id, $ownpost=false, $reply=false, $link=false, $rate=false, $footer); } } else { echo "

No posts

"; } } 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 global $CFG; echo "Processing posts...\n"; $cutofftime = time() - $CFG->maxeditingtime; if ($posts = get_records_sql("SELECT p.*, d.course FROM forum_posts p, forum_discussions d WHERE p.mailed = '0' AND p.created < '$cutofftime' AND p.discussion = d.id")) { $timenow = time(); foreach ($posts as $post) { echo "Processing post $post->id\n"; if (! $userfrom = get_record("user", "id", "$post->user")) { echo "Could not find user $post->user\n"; continue; } if (! $course = get_record("course", "id", "$post->course")) { echo "Could not find course $post->course\n"; continue; } if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) { echo "Could not find discussion $post->discussion\n"; continue; } if (! $forum = get_record("forum", "id", "$discussion->forum")) { echo "Could not find forum $discussion->forum\n"; continue; } if ($users = get_records_sql("SELECT u.* FROM user u, forum_subscriptions s WHERE s.user = u.id AND s.forum = '$discussion->forum'")) { foreach ($users as $userto) { $postsubject = "$course->shortname: $post->subject"; $posttext = "$course->shortname -> Forums -> $forum->name -> $discussion->name\n"; $posttext .= "---------------------------------------------------------------------\n"; $posttext .= "$post->subject\n"; $posttext .= "by $userfrom->firstname $userfrom->lastname, on ".userdate($post->created, "", $userto->timezone)."\n"; $posttext .= "---------------------------------------------------------------------\n"; $posttext .= strip_tags($post->message); $posttext .= "\n\n"; $posttext .= "---------------------------------------------------------------------\n"; $posttext .= "This is a copy of a message posted on the $course->shortname website.\n"; $posttext .= "To add your reply via the website, click on this link:\n"; $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id"; if ($userto->mailformat == 1) { // HTML $posthtml = "

". "wwwroot/course/view.php?id=$course->id\">$course->shortname ->". "wwwroot/mod/forum/index.php?id=$course->id\">Forums ->". "wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name ->". "wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name

"; $posthtml .= make_mail_post($post, $userfrom, $userto, $course, false, true, false, false); } else { $posthtml = ""; } if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) { echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email)\n"; } } } if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) { echo "Could not update the mailed field for id $post->id\n"; } } } return true; } function forum_forcesubscribe($forumid, $value=1) { return set_field("forum", "forcesubscribe", $value, "id", $forumid); } function forum_is_forcesubscribed($forumid) { return get_field("forum", "forcesubscribe", "id", $forumid); } function forum_is_subscribed($userid, $forumid) { if (forum_is_forcesubscribed($forumid)) { return true; } return record_exists_sql("SELECT * FROM forum_subscriptions WHERE user='$userid' AND forum='$forumid'"); } function forum_subscribe($userid, $forumid) { global $db; return $db->Execute("INSERT INTO forum_subscriptions SET user = '$userid', forum = '$forumid'"); } function forum_unsubscribe($userid, $forumid) { global $db; return $db->Execute("DELETE FROM forum_subscriptions WHERE user = '$userid' AND forum = '$forumid'"); } function user_has_posted_discussion($forumid, $userid) { if ($topics = get_all_topics($forumid, "DESC", $userid)) { return true; } else { return false; } } function user_can_post_discussion($forum) { // $forum is an object global $USER; if ($forum->type == "eachuser") { return (! user_has_posted_discussion($forum->id, $USER->id)); } else if ($forum->type == "teacher") { return isteacher($forum->course); } else if (isteacher($forum->course)) { return true; } else { return $forum->open; } } function get_all_topics($forum="0", $forum_sort="DESC", $user=0) { if ($user) { $userselect = " AND u.id = '$user' "; } else { $userselect = ""; } return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.id as userid FROM forum_discussions d, forum_posts p, user u WHERE d.forum = '$forum' AND p.discussion = d.id AND p.parent= 0 AND p.user = u.id $userselect ORDER BY p.created $forum_sort"); } function print_forum_latest_topics($forum_id=0, $forum_numtopics=5, $forum_style="plain", $forum_sort="DESC") { global $CFG, $USER; if ($forum_id) { if (! $forum = get_record("forum", "id", $forum_id)) { error("Forum ID was incorrect"); } if (! $course = get_record("course", "id", $forum->course)) { error("Could not find the course this forum belongs to!"); } if ($course->category) { require_login($course->id); } } else { if (! $course = get_record("course", "category", 0)) { error("Could not find a top-level course!"); } if (! $forum = get_course_news_forum($course->id)) { error("Could not find or create a main forum in this course (id $course->id)"); } } if (user_can_post_discussion($forum)) { echo "

"; echo "wwwroot/mod/forum/post.php?forum=$forum->id\">Add a new topic..."; echo "

\n"; } if (! $topics = get_all_topics($forum->id, $forum_sort) ) { echo "

There are no discussion topics yet in this forum.

"; } else { $replies = count_discussion_replies($forum->id); $topiccount = 0; foreach ($topics as $topic) { $topiccount++; if ($forum_numtopics && ($topiccount > $forum_numtopics)) { echo "

wwwroot/mod/forum/view.php?f=$forum->id\">Older topics ...

"; break; } if ($replies[$topic->discussion]) { $topic->replies = $replies[$topic->discussion]->replies; } else { $topic->replies = 0; } $ownpost = ($topic->userid == $USER->id); switch ($forum_style) { case "minimal": echo "

".userdate($topic->modified, "%e %B, %H:%M").""; echo "
$topic->subject "; echo "wwwroot/mod/forum/discuss.php?d=$topic->discussion\">more..."; echo "

\n"; break; default: print_post($topic, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false); echo "
\n"; break; } } } } function print_discussion($course, $discussion, $post, $mode) { global $USER; $ownpost = ($USER->id == $post->user); print_post($post, $course->id, $ownpost, $reply=true, $link=false, $rate=false); print_mode_form($discussion->id, $mode); if ($discussion->assessed && $USER->id) { echo "
"; echo "id\">"; } switch ($mode) { case 1 : // Flat ascending case -1 : // Flat descending default: echo "
    "; print_posts_flat($post->discussion, $course->id, $mode, $discussion->assessed); echo "
"; break; case 2 : // Threaded print_posts_threaded($post->id, $course->id, 0, $discussion->assessed); break; case 3 : // Nested print_posts_nested($post->id, $course->id, $discussion->assessed); break; } if ($discussion->assessed && $USER->id) { echo "

"; echo "
"; } } function print_posts_flat($discussion, $course, $direction, $assessed) { global $USER; $reply = true; $link = false; if ($direction < 0) { $sort = "ORDER BY created DESC"; } else { $sort = "ORDER BY created ASC"; } if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture FROM forum_posts p, user u WHERE p.discussion = $discussion AND p.parent > 0 AND p.user = u.id $sort")) { foreach ($posts as $post) { $ownpost = ($USER->id == $post->user); print_post($post, $course, $ownpost, $reply, $link, $assessed); } } else { return; } } function print_posts_threaded($parent, $course, $depth, $assessed) { global $USER; $reply = true; $link = false; if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture FROM forum_posts p, user u WHERE p.parent = '$parent' AND p.user = u.id")) { foreach ($posts as $post) { echo "
    "; if ($depth > 0) { $ownpost = ($USER->id == $post->user); print_post($post, $course, $ownpost, $reply, $link, $assessed); // link=true? echo "
    "; } else { echo "
  • discussion&parent=$post->id\">$post->subject by $post->firstname $post->lastname, ".userdate($post->created)."

    "; } print_posts_threaded($post->id, $course, $depth-1, $assessed); echo "
\n"; } } else { return; } } function print_posts_nested($parent, $course, $assessed) { global $USER; $reply = true; $link = false; if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture FROM forum_posts p, user u WHERE p.parent = $parent AND p.user = u.id ORDER BY p.created ASC ")) { foreach ($posts as $post) { $ownpost = ($USER->id == $post->user); echo "
    "; print_post($post, $course, $ownpost, $reply, $link, $assessed); echo "
    "; print_posts_nested($post->id, $course, $assessed); echo "
\n"; } } else { return; } } function forum_set_display_mode($mode=0) { global $USER; if ($mode) { $USER->mode = $mode; } else if (!$USER->mode) { $USER->mode = $FORUM_DEFAULT_DISPLAY_MODE; } } ?>