Attachments can now be added to postings.

They are stored in the course moddata area.
They appear as links within the message, including mailed-out messages.
They are deleted when the post is deleted.
This commit is contained in:
martin 2002-09-01 14:34:38 +00:00
parent e6e769a9ea
commit 7f6689e4e0
4 changed files with 204 additions and 22 deletions

View File

@ -1,5 +1,7 @@
<?PHP // $Id$
include_once("$CFG->dirroot/files/mimetypes.php");
/// CONSTANTS ///////////////////////////////////////////////////////////
$FORUM_DEFAULT_DISPLAY_MODE = 3;
@ -109,6 +111,14 @@ function forum_make_mail_post(&$post, $user, $touser, $course,
$output .= "&nbsp;";
$output .= "</TD><TD BGCOLOR=\"$THEME->cellcontent\">\n";
if ($post->attachment) {
$post->course = $course->id;
$post->forum = get_field("forum_discussions", "forum", "id", $post->discussion);
$output .= "<DIV ALIGN=right>";
$output .= forum_print_attachments($post, "html");
$output .= "</DIV>";
}
$output .= text_to_html($post->message);
$output .= "<P ALIGN=right><FONT SIZE=-1>";
@ -178,6 +188,14 @@ function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link
echo "&nbsp;";
echo "</TD><TD BGCOLOR=\"$THEME->cellcontent\">\n";
if ($post->attachment) {
$post->course = $courseid;
$post->forum = get_field("forum_discussions", "forum", "id", $post->discussion);
echo "<DIV ALIGN=right>";
forum_print_attachments($post);
echo "</DIV>";
}
if ($link && (strlen($post->message) > $FORUM_LONG_POST)) {
// Print shortened version
echo text_to_html(forum_shorten_post($post->message));
@ -424,13 +442,128 @@ function forum_get_post_full($postid) {
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 = "<IMG BORDER=0 SRC=\"$CFG->wwwroot/files/pix/$icon\" HEIGHT=16 WIDTH=16 ALT=\"File\">";
if ($return == "html") {
$output .= "<A HREF=\"$CFG->wwwroot/$ffurl\">$image</A> ";
$output .= "<A HREF=\"$CFG->wwwroot/$ffurl\">$file</A><BR>";
} else if ($return == "text") {
$output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
} else {
link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
echo "<A HREF=\"$CFG->wwwroot/$ffurl\">$file</A>";
echo "<BR>";
}
}
}
}
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";
return insert_record("forum_posts", $post);
$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) {
@ -440,6 +573,11 @@ function forum_update_post($post) {
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);
}
@ -462,22 +600,32 @@ function forum_add_discussion($discussion) {
$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;
}
@ -492,16 +640,17 @@ function forum_delete_discussion($discussion) {
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_posts", "discussion", "$discussion->id")) {
$result = false;
}
if (! delete_records("forum_discussions", "id", "$discussion->id")) {
$result = false;
}
@ -510,9 +659,15 @@ function forum_delete_discussion($discussion) {
}
function forum_delete_post($postid) {
if (delete_records("forum_posts", "id", $postid)) {
delete_records("forum_ratings", "post", $postid); // Just in case
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;
@ -547,7 +702,7 @@ function forum_print_user_discussions($courseid, $userid) {
$inforum = get_string("inforum", "forum", "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname</A>");
$discussion->subject .= " ($inforum)";
$ownpost = ($discussion->userid == $USER->id);
forum_print_post($discussion, $course->id, $ownpost, $reply=0, $link=1, $assessed=false);
forum_print_post($discussion, $courseid, $ownpost, $reply=0, $link=1, $assessed=false);
echo "<BR>\n";
}
}
@ -768,6 +923,11 @@ function forum_cron () {
$posttext .= "---------------------------------------------------------------------\n";
$posttext .= strip_tags($post->message);
$posttext .= "\n\n";
if ($post->attachment) {
$post->course = $course->id;
$post->forum = $forum->id;
$posttext .= forum_print_attachments($post, "text");
}
$posttext .= "---------------------------------------------------------------------\n";
$posttext .= get_string("postmailinfo", "forum", $course->shortname)."\n";
$posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id";

View File

@ -1,4 +1,4 @@
<form name="form" method="post" action="post.php">
<form name="form" method="post" action="post.php" enctype="multipart/form-data">
<table cellpadding=5>
<tr valign=top>
<td align=right><P><B><? print_string("subject", "forum"); ?>:</B></P></TD>
@ -13,21 +13,32 @@
<td align=right><P><B><? print_string("message", "forum"); ?>:</B><BR><BR><? helpbutton("text", get_string("helptext")) ?></P></TD>
<td>
<textarea name=message rows=15 cols=50 wrap="virtual"><? p($post->message) ?></textarea>
<BR><BR>
<input type="hidden" name=course value="<? p($post->course) ?>">
<input type="hidden" name=forum value="<? p($post->forum) ?>">
<input type="hidden" name=discussion value="<? p($post->discussion) ?>">
<input type="hidden" name=parent value="<? p($post->parent) ?>">
<input type="hidden" name=user value="<? p($post->user) ?>">
<input type="hidden" name=edit value="<? p($post->edit) ?>">
<input type="submit" value=" Save my post ">
</td>
<td>
<td rowspan=2>
<FONT SIZE=1>
<? print_string("postingtip", "forum"); ?>
<DIV ALIGN=RIGHT><? helpbutton("questions", get_string("helpquestions"), "moodle", false) ?></DIV>
</FONT>
</td>
</tr>
<tr valign=top>
<td><P><B><? print_string("attachment", "forum") ?>:<BR>(<? print_string("optional") ?>)</B></P></td>
<td>
<INPUT type="hidden" name="MAX_FILE_SIZE" value="<? echo get_max_upload_file_size() ?>">
<input type="file" name="attachment" size=40>
<? helpbutton("attachment", get_string("attachment", "forum"), "forum" );
print_string("maxsize", "", display_size(get_max_upload_file_size()));
?>
<BR><BR>
<input type="hidden" name=course value="<? p($post->course) ?>">
<input type="hidden" name=forum value="<? p($post->forum) ?>">
<input type="hidden" name=discussion value="<? p($post->discussion) ?>">
<input type="hidden" name=parent value="<? p($post->parent) ?>">
<input type="hidden" name=user value="<? p($post->user) ?>">
<input type="hidden" name=edit value="<? p($post->edit) ?>">
<input type="submit" value="<? print_string("savechanges"); ?>">
</td>
</tr>
<tr valign=top>
</table>
</FORM>

View File

@ -16,16 +16,23 @@
$post->subject = strip_tags($post->subject); // Strip all tags
$post->message = cleantext($post->message); // Clean up any bad tags
$post->attachment = $HTTP_POST_FILES["attachment"];
if (!$post->subject and !$post->message) {
error("Something was wrong with your post. Perhaps you left it blank, or the attachment was too big. Your changes have NOT been saved.");
}
require_login();
if ($post->edit) { // Updating a post
$post->id = $post->edit;
if (forum_update_post($post) ) {
if (forum_update_post($post)) {
add_to_log($post->course, "forum", "update post", "discuss.php?d=$post->discussion&parent=$post->id", "$post->id");
redirect(forum_go_back_to("discuss.php?d=$post->discussion"), "Your post was updated", 1);
} else {
error("Could not update your post due to an unknown error");
}
} else if ($post->discussion) { // Adding a new post to an existing discussion
if ($post->id = forum_add_new_post($post)) {
if ( ! forum_is_subscribed($USER->id, $post->forum) ) {
@ -190,7 +197,7 @@
redirect("view.php?f=$discussion->forum",
"The discussion topic has been deleted", 1);
} else if (forum_delete_post($post->id)) {
} else if (forum_delete_post($post)) {
add_to_log($discussion->course, "forum", "delete post", "discuss.php?d=$post->discussion", "$post->id");
redirect(forum_go_back_to("discuss.php?d=$post->discussion"),

View File

@ -5,7 +5,7 @@
// This fragment is called by /admin/index.php
////////////////////////////////////////////////////////////////////////////////
$module->version = 2002082000;
$module->version = 2002082900;
$module->cron = 60;
function forum_upgrade($oldversion) {
@ -39,6 +39,10 @@ function forum_upgrade($oldversion) {
execute_sql("INSERT INTO log_display VALUES ('forum', 'update', 'forum', 'name') ");
}
if ($oldversion < 2002082900) {
execute_sql(" ALTER TABLE `forum_posts` ADD `attachment` VARCHAR(100) NOT NULL AFTER `message` ");
}
return true;
}