Skip to content

Commit

Permalink
adding attachment for blogs
Browse files Browse the repository at this point in the history
  • Loading branch information
toyomoyo committed Nov 20, 2006
1 parent 89d30d0 commit 7d0e5a9
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 12 deletions.
13 changes: 12 additions & 1 deletion blog/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
?>

<form name="entry" method="post" action="edit.php" >
<form name="entry" method="post" action="edit.php" enctype="multipart/form-data">

<table border="0" cellpadding="5">
<tr valign="top">
Expand Down Expand Up @@ -146,6 +146,17 @@
</table>
</td>
</tr>
<tr valign="top">
<td align="right"><b><?php print_string("attachment", "forum") ?>:<br />(<?php print_string("optional") ?>)&nbsp;</b></td>
<td>
<?php $maxbytes = get_max_upload_file_size($CFG->maxbytes); ?>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $maxbytes ?>" />
<input type="file" name="attachment" size="40" alt="<?php print_string("attachment", "forum") ?>" />
<?php
print_string("maxsize", "", display_size($maxbytes));
?>
</td>
</tr>
<tr valign="top">
<td align="right">
<strong><?php print_string('publishto', 'blog'); ?>:</strong>
Expand Down
15 changes: 13 additions & 2 deletions blog/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,14 @@ function do_add(&$post, &$errors) {
$post->userid = $USER->id;
$post->lastmodified = time();
$post->created = time();

// Insert the new blog entry.
if ($id = insert_record('post', $post)) {
$post->id = $id;
// add blog attachment
if ($post->attachment = blog_add_attachment($post, 'attachment',$message)) {
set_field("post", "attachment", $post->attachment, "id", $post->id);
}
add_tags_info($post->id);
add_to_log(SITEID, 'blog', 'add', 'index.php?userid='.$post->userid.'&postid='.$posz->id, $post->subject);

Expand Down Expand Up @@ -234,13 +238,20 @@ function do_edit(&$post, &$errors) {
}

$post->lastmodified = time();


if ($newfilename = blog_add_attachment($post, 'attachment',$message)) {
$post->attachment = $newfilename;
} else {
unset($post->attachment);
}
// update record
if (update_record('post', $post)) {
// delete all tags associated with this entry
delete_records('blog_tag_instance', 'entryid', $post->id);
// add them back
add_tags_info($post->id);


add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);

} else {
Expand Down
96 changes: 95 additions & 1 deletion blog/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ function blog_print_entry($blogEntry, $viewtype='full', $filtertype='', $filters
/// Actual content

echo '</td><td class="content">'."\n";

if ($blogEntry->attachment) {
echo '<div class="attachments">';
$attachedimages = blog_print_attachments($blogEntry);
echo '</div>';
} else {
$attachedimages = '';
}

switch ($template['publishstate']) {
case 'draft':
Expand All @@ -219,7 +227,9 @@ function blog_print_entry($blogEntry, $viewtype='full', $filtertype='', $filters

// Print whole message
echo format_text($template['body']);


/// Print attachments
echo $attachedimages;
/// Links to tags

if ($blogtags = get_records_sql('SELECT t.* FROM '.$CFG->prefix.'tags t, '.$CFG->prefix.'blog_tag_instance ti
Expand Down Expand Up @@ -251,7 +261,91 @@ function blog_print_entry($blogEntry, $viewtype='full', $filtertype='', $filters
echo '</td></tr></table>'."\n\n";

}

function blog_file_area_name($blogentry) {
// Creates a directory file name, suitable for make_upload_directory()
global $CFG;
// $CFG->dataroot/blog/attachments/xxxx/file.jpg
return "blog/attachments/$blogentry->id";
}

function blog_file_area($blogentry) {
return make_upload_directory( blog_file_area_name($blogentry) );
}


function blog_print_attachments($blogentry, $return=NULL) {
// if return=html, then return a html string.
// if return=text, then return a text-only string.
// otherwise, print HTML for non-images, and return image HTML

global $CFG;

$filearea = blog_file_area_name($blogentry);

$imagereturn = "";
$output = "";

if ($basedir = blog_file_area($blogentry)) {
if ($files = get_directory_list($basedir)) {
$strattachment = get_string("attachment", "forum");
foreach ($files as $file) {
include_once($CFG->libdir.'/filelib.php');
$icon = mimeinfo("icon", $file);
if ($CFG->slasharguments) {
$ffurl = "$CFG->wwwroot/file.php/$filearea/$file";
} else {
$ffurl = "$CFG->wwwroot/file.php?file=/$filearea/$file";
}
$image = "<img border=\"0\" src=\"$CFG->pixpath/f/$icon\" height=\"16\" width=\"16\" alt=\"\" />";

if ($return == "html") {
$output .= "<a href=\"$ffurl\">$image</a> ";
$output .= "<a href=\"$ffurl\">$file</a><br />";

} else if ($return == "text") {
$output .= "$strattachment $file:\n$ffurl\n";

} else {
if ($icon == "image.gif") { // Image attachments don't get printed as links
$imagereturn .= "<br /><img src=\"$ffurl\" alt=\"\" />";
} else {
echo "<a href=\"$ffurl\">$image</a> ";
echo filter_text("<a href=\"$ffurl\">$file</a><br />");
}
}
}
}
}

if ($return) {
return $output;
}

return $imagereturn;
}

/**
* If successful, this function returns the name of the file
* @param $post is a full post record, including course and forum
* @param $newfile is a full upload array from $_FILES
* @param $message is a string to hold the messages.
*/

function blog_add_attachment($blogentry, $inputname, &$message) {

global $CFG;

require_once($CFG->dirroot.'/lib/uploadlib.php');
$um = new upload_manager($inputname,true,false,null,false,$CFG->maxbytes,true,true);
$dir = blog_file_area_name($blogentry);
if ($um->process_file_uploads($dir)) {
$message .= $um->get_errors();
return $um->get_new_filename();
}
$message .= $um->get_errors();
echo $message;
}

/**
* Use this function to retrieve a list of publish states available for
Expand Down
8 changes: 5 additions & 3 deletions file.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@
if (count($args) == 0) { // always at least courseid, may search for index.html in course root
error('No valid arguments supplied');
}

// security: limit access to existing course subdirectories
if (!$course = get_record_sql("SELECT * FROM {$CFG->prefix}course WHERE id='".(int)$args[0]."'")) {
// hack for blogs, needs proper security check too
if ((!$course = get_record_sql("SELECT * FROM {$CFG->prefix}course WHERE id='".(int)$args[0]."'")) && $args[0]!='blog') {
error('Invalid course ID');
}

// security: prevent access to "000" or "1 something" directories
if ($args[0] != $course->id) {
// hack for blogs, needs proper security check too
if ($args[0] != $course->id && $args[0]!='blog') {
error('Invalid course ID');
}

Expand Down
9 changes: 5 additions & 4 deletions lib/db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20061005" COMMENT="XMLDB file for core Moodle tables"
<XMLDB PATH="lib/db" VERSION="20061120" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -782,8 +782,9 @@
<FIELD NAME="content" TYPE="text" LENGTH="big" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="summary" NEXT="uniquehash"/>
<FIELD NAME="uniquehash" TYPE="char" LENGTH="128" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="content" NEXT="rating"/>
<FIELD NAME="rating" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="uniquehash" NEXT="format"/>
<FIELD NAME="format" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="rating" NEXT="publishstate"/>
<FIELD NAME="publishstate" TYPE="char" LENGTH="20" NOTNULL="true" DEFAULT="draft" SEQUENCE="false" ENUM="true" ENUMVALUES="'draft', 'site', 'public'" PREVIOUS="format" NEXT="lastmodified"/>
<FIELD NAME="format" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="rating" NEXT="attachment"/>
<FIELD NAME="attachment" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" ENUM="false" COMMENT="attachment" PREVIOUS="format" NEXT="publishstate"/>
<FIELD NAME="publishstate" TYPE="char" LENGTH="20" NOTNULL="true" DEFAULT="draft" SEQUENCE="false" ENUM="true" ENUMVALUES="'draft', 'site', 'public'" PREVIOUS="attachment" NEXT="lastmodified"/>
<FIELD NAME="lastmodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="publishstate" NEXT="created"/>
<FIELD NAME="created" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="lastmodified"/>
</FIELDS>
Expand Down Expand Up @@ -1031,4 +1032,4 @@
</SENTENCES>
</STATEMENT>
</STATEMENTS>
</XMLDB>
</XMLDB>
10 changes: 10 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ function xmldb_main_upgrade($oldversion=0) {
$result = $result && add_field($table, $field5);
}

if ($result && $oldversion < 2006112000) {

/// Define field attachment to be added to post
$table = new XMLDBTable('post');
$field = new XMLDBField('attachment');
$field->setAttributes(XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null, 'format');

/// Launch add field attachment
$result = $result && add_field($table, $field);
}

return $result;

Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// This is compared against the values stored in the database to determine
// whether upgrades should be performed (see lib/db/*.php)

$version = 2006102600; // YYYYMMDD = date
$version = 2006112000; // YYYYMMDD = date
// XY = increments within a single day

$release = '1.8 dev'; // Human-friendly version name
Expand Down

0 comments on commit 7d0e5a9

Please sign in to comment.