";
$output .= "";
} else {
$output .= "";
$output .= "";
}
$output .= "body\" WIDTH=35 VALIGN=TOP>";
$output .= print_user_picture($user->id, $course->id, $user->picture, false, true);
$output .= " | ";
if ($post->parent) {
$output .= "cellheading\">";
} else {
$output .= " | cellheading2\">";
}
$output .= " ";
$output .= "$post->subject ";
$output .= "";
$by->name = "wwwroot/user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname";
$by->date = userdate($post->created, "", $touser->timezone);
$output .= get_string("bynameondate", "forum", $by);
$output .= " | ";
$output .= "body\" WIDTH=10>";
$output .= " ";
$output .= " | cellcontent\">\n";
if ($post->attachment) {
$post->course = $course->id;
$post->forum = get_field("forum_discussions", "forum", "id", $post->discussion);
$output .= " ";
$output .= forum_print_attachments($post, "html");
$output .= " ";
}
$output .= format_text($post->message, $post->format);
$output .= "
";
$age = time() - $post->created;
if ($ownpost) {
$output .= "wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."";
if ($reply) {
$output .= " | wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."";
}
$output .= " ";
} else {
if ($reply) {
$output .= "wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")." ";
}
}
$output .= " ";
$output .= "";
if ($footer) {
$output .= "$footer ";
}
$output .= " | \n";
$output .= " | \n\n";
return $output;
}
function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
global $THEME, $USER, $CFG, $FORUM_LONG_POST;
if ($post->parent) {
echo "";
echo "";
} else {
echo "";
echo "";
}
echo "body\" WIDTH=35 VALIGN=TOP>";
print_user_picture($post->userid, $courseid, $post->picture);
echo " | ";
if ($post->parent) {
echo "cellheading\" WIDTH=\"100%\">";
} else {
echo " | cellheading2\" WIDTH=\"100%*\">";
}
echo " ";
echo "$post->subject ";
echo "";
$by->name = "wwwroot/user/view.php?id=$post->userid&course=$courseid\">$post->firstname $post->lastname";
$by->date = userdate($post->created);
print_string("bynameondate", "forum", $by);
echo " | ";
echo "body\" WIDTH=10>";
echo " ";
echo " | cellcontent\">\n";
if ($post->attachment) {
$post->course = $courseid;
$post->forum = get_field("forum_discussions", "forum", "id", $post->discussion);
echo " ";
forum_print_attachments($post);
echo " ";
}
if ($link && (strlen($post->message) > $FORUM_LONG_POST)) {
// Print shortened version
echo format_text(forum_shorten_post($post->message), $post->format);
$numwords = count_words($post->message);
echo "wwwroot/mod/forum/discuss.php?d=$post->discussion\">";
echo get_string("readtherest", "forum");
echo " (".get_string("numwords", "", $numwords).")...";
} else {
// Print whole message
echo format_text($post->message, $post->format);
}
echo "
";
$age = time() - $post->created;
if ($ownpost) {
if ($age < $CFG->maxeditingtime) {
echo "wwwroot/mod/forum/post.php?edit=$post->id\">".get_string("edit", "forum")." | ";
}
}
if ($ownpost or isteacher($courseid)) {
echo "wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."";
if ($reply) {
echo "| ";
} else {
echo " ";
}
}
if ($reply) {
echo "wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."";
echo " ";
}
echo " ";
echo "";
if ($rate && $USER->id) {
if (isteacher($courseid)) {
forum_print_ratings($post->id);
if ($USER->id != $post->userid) {
forum_print_rating($post->id, $USER->id);
}
} else if ($USER->id == $post->userid) {
forum_print_ratings($post->id);
} else {
forum_print_rating($post->id, $USER->id);
}
}
if ($link) {
if ($post->replies == 1) {
$replystring = get_string("repliesone", "forum", $post->replies);
} else {
$replystring = get_string("repliesmany", "forum", $post->replies);
}
echo "wwwroot/mod/forum/discuss.php?d=$post->discussion\">".get_string("discussthistopic", "forum")." ($replystring) ";
}
echo " ";
if ($footer) {
echo " $footer ";
}
echo " ";
echo " | ";
echo " | \n \n\n";
}
function forum_print_post_header(&$post, $courseid, $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
global $THEME, $USER, $CFG, $FORUM_LONG_POST;
if ($post->parent) {
echo "";
echo "";
} else {
echo "\n\n";
}
function forum_shorten_post($message) {
global $FORUM_LONG_POST, $FORUM_SHORT_POST;
if (strlen($message) > $FORUM_LONG_POST) {
// Look for the first return between $FORUM_SHORT_POST and $FORUM_LONG_POST
$shortmessage = substr($message, $FORUM_SHORT_POST, $FORUM_LONG_POST);
if ($pos = strpos($shortmessage, "\n")) {
return substr($message, 0, $FORUM_SHORT_POST + $pos);
} else {
return substr($message, 0, $FORUM_LONG_POST)."...";
}
} else {
return $message;
}
}
function forum_print_ratings($post) {
if ($ratings = get_records_sql("SELECT * from forum_ratings WHERE post='$post'")) {
$sumrating[1] = 0;
$sumrating[2] = 0;
$sumrating[3] = 0;
foreach ($ratings as $rating) {
$sumrating[$rating->rating]++;
}
$summary = $sumrating[1]."s/".$sumrating[2]."/".$sumrating[3]."c";
echo get_string("ratings", "forum").": ";
link_to_popup_window ("/mod/forum/report.php?id=$post", "ratings", $summary, 400, 550);
}
}
function forum_print_rating($post, $user) {
global $FORUM_POST_RATINGS;
if ($rs = get_record_sql("SELECT rating from forum_ratings WHERE user='$user' AND post='$post'")) {
if ($FORUM_POST_RATINGS[$rs->rating]) {
echo "".get_string("youratedthis", "forum").": ";
echo $FORUM_POST_RATINGS[$rs->rating];
echo "";
return;
}
}
choose_from_menu($FORUM_POST_RATINGS, $post, "", get_string("rate", "forum")."...");
}
function forum_print_mode_form($discussion, $mode) {
GLOBAL $FORUM_LAYOUT_MODES;
echo "";
popup_form("discuss.php?d=$discussion&mode=", $FORUM_LAYOUT_MODES, "mode", $mode, "");
echo " \n";
}
function forum_print_search_form($course, $search="") {
global $CFG;
echo "";
}
function forum_count_discussion_replies($forum="0") {
// Returns an array of counts of replies to each discussion (optionally in one forum)
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 forum_count_unrated_posts($discussionid, $userid) {
// How many unrated posts are in the given discussion for a given user?
if ($posts = get_record_sql("SELECT count(*) as num
FROM forum_posts
WHERE parent > 0 AND
discussion = '$discussionid' AND
user <> '$userid' ")) {
if ($rated = get_record_sql("SELECT count(*) as num
FROM forum_posts p, forum_ratings r
WHERE p.discussion = '$discussionid'
AND p.id = r.post
AND r.user = '$userid'")) {
$difference = $posts->num - $rated->num;
if ($difference > 0) {
return $difference;
} else {
return 0; // Just in case there was a counting error
}
} else {
return $posts->num;
}
} else {
return 0;
}
}
function forum_set_return() {
global $CFG, $SESSION, $HTTP_REFERER;
if (! isset($SESSION->fromdiscussion)) {
// If the referer is NOT a login screen then save it.
if (! strncasecmp("$CFG->wwwroot/login", $HTTP_REFERER, 300)) {
$SESSION->fromdiscussion = $HTTP_REFERER;
save_session("SESSION");
}
}
}
function forum_go_back_to($default) {
global $SESSION;
if ($SESSION->fromdiscussion) {
$returnto = $SESSION->fromdiscussion;
unset($SESSION->fromdiscussion);
save_session("SESSION");
return $returnto;
} else {
return $default;
}
}
function forum_get_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 forum_file_area_name($post) {
// Creates a directory file name, suitable for make_upload_directory()
global $CFG;
return "$post->course/$CFG->moddata/forum/$post->forum/$post->id";
}
function forum_file_area($post) {
return make_upload_directory( forum_file_area_name($post) );
}
function forum_delete_old_attachments($post, $exception="") {
// Deletes all the user files in the attachments area for a post
// EXCEPT for any file named $exception
if ($basedir = forum_file_area($post)) {
if ($files = get_directory_list($basedir)) {
foreach ($files as $file) {
if ($file != $exception) {
unlink("$basedir/$file");
notify("Existing file '$file' has been deleted!");
}
}
}
if (!$exception) { // Delete directory as well, if empty
rmdir("$basedir");
}
}
}
function forum_print_attachments($post, $return=NULL) {
// if return=html, then return a html string.
// if return=text, then return a text-only string.
// otherwise, print HTML
global $CFG;
$filearea = forum_file_area_name($post);
if ($basedir = forum_file_area($post)) {
if ($files = get_directory_list($basedir)) {
$strattachment = get_string("attachment", "forum");
foreach ($files as $file) {
$icon = mimeinfo("icon", $file);
if ($CFG->slasharguments) {
$ffurl = "file.php/$filearea/$file";
} else {
$ffurl = "file.php?file=/$filearea/$file";
}
$image = "wwwroot/files/pix/$icon\" HEIGHT=16 WIDTH=16 ALT=\"File\">";
if ($return == "html") {
$output .= "wwwroot/$ffurl\">$image ";
$output .= "wwwroot/$ffurl\">$file ";
} else if ($return == "text") {
$output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
} else {
link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
echo "wwwroot/$ffurl\">$file";
echo " ";
}
}
}
}
if ($return) {
return $output;
}
}
function forum_add_attachment($post, $newfile) {
// $post is a full post record, including course and forum
// $newfile is a full upload array from HTTP_POST_FILES
// If successful, this function returns the name of the file
if (!isset($newfile['name'])) {
return "";
}
$newfile_name = clean_filename($newfile['name']);
if (valid_uploaded_file($newfile)) {
if (! $newfile_name) {
notify("This file had a wierd filename and couldn't be uploaded");
} else if (! $dir = forum_file_area($post)) {
notify("Attachment could not be stored");
$newfile_name = "";
} else {
if (move_uploaded_file($newfile['tmp_name'], "$dir/$newfile_name")) {
forum_delete_old_attachments($post, $newfile_name);
} else {
notify("An error happened while saving the file on the server");
$newfile_name = "";
}
}
} else {
$newfile_name = "";
}
return $newfile_name;
}
function forum_add_new_post($post) {
$post->created = $post->modified = time();
$post->mailed = "0";
$newfile = $post->attachment;
$post->attachment = "";
if (! $post->id = insert_record("forum_posts", $post)) {
return false;
}
if ($post->attachment = forum_add_attachment($post, $newfile)) {
set_field("forum_posts", "attachment", $post->attachment, "id", $post->id);
}
return $post->id;
}
function forum_update_post($post) {
$post->modified = time();
if (!$post->parent) { // Post is a discussion starter - update discussion title too
set_field("forum_discussions", "name", $post->subject, "id", $post->discussion);
}
if ($newfilename = forum_add_attachment($post, $post->attachment)) {
$post->attachment = $newfilename;
} else {
unset($post->attachment);
}
return update_record("forum_posts", $post);
}
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;
$post->attachment = "";
$post->forum = $discussion->forum;
$post->course = $discussion->course;
if (! $post->id = insert_record("forum_posts", $post) ) {
return 0;
}
if ($post->attachment = forum_add_attachment($post, $discussion->attachment)) {
set_field("forum_posts", "attachment", $post->attachment, "id", $post->id); //ignore errors
}
// Now do the real module entry
$discussion->firstpost = $post->id;
$discussion->timemodified = $timenow;
if (! $discussion->id = insert_record("forum_discussions", $discussion) ) {
delete_records("forum_posts", "id", $post->id);
return 0;
}
// Finally, set the pointer on the post.
if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) {
delete_records("forum_posts", "id", $post->id);
delete_records("forum_discussions", "id", $discussion->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) {
$post->course = $discussion->course;
$post->forum = $discussion->forum;
if (! delete_records("forum_ratings", "post", "$post->id")) {
$result = false;
}
if (! forum_delete_post($post)) {
$result = false;
}
}
}
if (! delete_records("forum_discussions", "id", "$discussion->id")) {
$result = false;
}
return $result;
}
function forum_delete_post($post) {
if (delete_records("forum_posts", "id", $post->id)) {
delete_records("forum_ratings", "post", $post->id); // Just in case
if ($post->attachment) {
$discussion = get_record("forum_discussions", "id", $post->discussion);
$post->course = $discussion->course;
$post->forum = $discussion->forum;
forum_delete_old_attachments($post);
}
return true;
}
return false;
}
function forum_print_user_discussions($courseid, $userid) {
global $CFG, $USER;
$discussions = get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture,
u.id as userid, f.type as forumtype, f.name as forumname, f.id as forumid
FROM forum_discussions d, forum_posts p, user u, forum f
WHERE d.course = '$courseid' AND p.discussion = d.id AND
p.parent = 0 AND p.user = u.id AND u.id = '$userid' AND
d.forum = f.id
ORDER BY p.created ASC");
if ($discussions) {
$user = get_record("user", "id", $userid);
echo " ";
print_heading( get_string("discussionsstartedby", "forum", "$user->firstname $user->lastname") );
$replies = forum_count_discussion_replies();
foreach ($discussions as $discussion) {
if (($discussion->forumtype == "teacher") and !isteacher($courseid)) {
continue;
}
if ($replies[$discussion->discussion]) {
$discussion->replies = $replies[$discussion->discussion]->replies;
} else {
$discussion->replies = 0;
}
$inforum = get_string("inforum", "forum", "wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname");
$discussion->subject .= " ($inforum)";
$ownpost = ($discussion->userid == $USER->id);
forum_print_post($discussion, $courseid, $ownpost, $reply=0, $link=1, $assessed=false);
echo " \n";
}
}
}
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_subscribed_users($course, $forum) {
// Returns list of user objects that are subscribed to this forum
if ($course->category) { // normal course
if ($forum->forcesubscribe) {
return get_course_users($course->id);
}
}
return get_records_sql("SELECT u.* FROM user u, forum_subscriptions s
WHERE s.forum = '$forum->id'
AND s.user = u.id AND u.deleted <> '1'");
}
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 forum_user_has_posted_discussion($forumid, $userid) {
if ($discussions = forum_get_discussions($forumid, "DESC", $userid)) {
return true;
} else {
return false;
}
}
function forum_user_can_post_discussion($forum) {
// $forum is an object
global $USER;
if ($forum->type == "eachuser") {
return (! forum_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 == 2);
}
}
function forum_user_can_post($forum) {
// $forum is an object
global $USER;
if ($forum->type == "teacher") {
return isteacher($forum->course);
} else if (isteacher($forum->course)) {
return true;
} else {
return $forum->open;
}
}
function forum_get_discussions($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 forum_print_latest_discussions($forum_id=0, $forum_numdiscussions=5, $forum_style="plain", $forum_sort="DESC") {
global $CFG, $USER, $FORUM_MANY_DISCUSSIONS;
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 = forum_get_course_news_forum($course->id)) {
error("Could not find or create a main forum in this course (id $course->id)");
}
}
if (forum_user_can_post_discussion($forum)) {
echo "";
echo "wwwroot/mod/forum/post.php?forum=$forum->id\">";
echo get_string("addanewdiscussion", "forum")."...";
echo " \n";
}
if (! $discussions = forum_get_discussions($forum->id, $forum_sort) ) {
echo "(".get_string("nodiscussions", "forum").") ";
return;
}
if ((!$forum_numdiscussions) && ($forum_style == "plain") && (count($discussions) > $FORUM_MANY_DISCUSSIONS) ) {
$forum_style = "header"; // Abbreviate display if it's going to be long.
}
$replies = forum_count_discussion_replies($forum->id);
$discussioncount = 0;
foreach ($discussions as $discussion) {
$discussioncount++;
if ($forum_numdiscussions && ($discussioncount > $forum_numdiscussions)) {
echo "wwwroot/mod/forum/view.php?f=$forum->id\">";
echo get_string("olderdiscussions", "forum")." ... ";
break;
}
if ($replies[$discussion->discussion]) {
$discussion->replies = $replies[$discussion->discussion]->replies;
} else {
$discussion->replies = 0;
}
$ownpost = ($discussion->userid == $USER->id);
switch ($forum_style) {
case "minimal":
echo "".userdate($discussion->modified, "%e %b, %H:%M")." - $discussion->firstname";
echo " $discussion->subject ";
echo "wwwroot/mod/forum/discuss.php?d=$discussion->discussion\">";
echo get_string("more", "forum")."...";
echo " \n";
break;
case "header":
forum_print_post_header($discussion, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
break;
default:
forum_print_post($discussion, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
echo " \n";
break;
}
}
}
function forum_print_discussion($course, $forum, $discussion, $post, $mode) {
global $USER;
$ownpost = ($USER->id == $post->user);
$reply = forum_user_can_post($forum);
forum_print_post($post, $course->id, $ownpost, $reply, $link=false, $rate=false);
forum_print_mode_form($discussion->id, $mode);
$ratingform = false;
if ($forum->assessed && $USER->id) {
$unrated = forum_count_unrated_posts($discussion->id, $USER->id);
if ($unrated > 0) {
$ratingform = true;
}
}
if ($ratingform) {
echo "";
}
}
function forum_print_posts_flat($discussion, $course, $direction, $assessed, $reply) {
global $USER;
$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);
forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
}
} else {
return;
}
}
function forum_print_posts_threaded($parent, $course, $depth, $assessed, $reply) {
global $USER;
$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 forum_print_posts_nested($parent, $course, $assessed, $reply) {
global $USER;
$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 "";
forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
echo " ";
forum_print_posts_nested($post->id, $course, $assessed, $reply);
echo " \n";
}
} else {
return;
}
}
function forum_set_display_mode($mode=0) {
global $USER, $FORUM_DEFAULT_DISPLAY_MODE;
if ($mode) {
$USER->mode = $mode;
save_session("USER");
} else if (!$USER->mode) {
$USER->mode = $FORUM_DEFAULT_DISPLAY_MODE;
save_session("USER");
}
}
?>
|
|
|