";
$output .= "";
} else {
$output .= "";
$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\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 "";
}
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 "";
}
}
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 "\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;
}
}
?>
|
|