]> git.mjollnir.org Git - moodle.git/commitdiff
adding attachment for blogs
authortoyomoyo <toyomoyo>
Mon, 20 Nov 2006 08:31:48 +0000 (08:31 +0000)
committertoyomoyo <toyomoyo>
Mon, 20 Nov 2006 08:31:48 +0000 (08:31 +0000)
blog/edit.html
blog/edit.php
blog/lib.php
file.php
lib/db/install.xml
lib/db/upgrade.php
version.php

index 9890eab424a3832c7b205078e93ec2c0f8a4a801..a91e26a8df94bc42483093cb3ad21f644b8e66b9 100755 (executable)
@@ -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">
         </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>
index 66a4d4285e78a6d3c2eb3252fab4fd5a57034827..5c1639f716f736f6ffae3f02a22f28a5abed3488 100755 (executable)
@@ -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);
 
@@ -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 {
index 9162d2d6cb9cd83142bb89cbd208bbae3774e985..55fbe953922973a9a06a8a1e76ef07c25e7e3dd3 100755 (executable)
     /// 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':
 
         // 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
         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
index e6bb63af2b72105c6bd25980ab3fbb40d1369beb..6c78f789f972e8d61709547ccab88810d4409927 100644 (file)
--- a/file.php
+++ b/file.php
     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');
     }
 
index 75ac4af4f48c2bc12f6324d726bbb8ff0cc8a04b..0ba18ccce36d7795f7333ecfe52d5427663aa4d8 100644 (file)
@@ -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"
 >
         <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>
       </SENTENCES>
     </STATEMENT>
   </STATEMENTS>
-</XMLDB>
+</XMLDB>
\ No newline at end of file
index 5646699ec47e06cab4837bdcb539cd60b1c16c86..a4823f05c7858a213584fb78d68ed3589a0b8638 100644 (file)
@@ -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;
 
index bf5283d619cfc6dc475e12bf08ed33bb2d16167f..5bc6592021d8f79640a66e123a4df2ea5751ae8f 100644 (file)
@@ -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