]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-14722 Added new maxattachments setting at admin and forum level to
authormoodler <moodler>
Mon, 8 Sep 2008 05:46:42 +0000 (05:46 +0000)
committermoodler <moodler>
Mon, 8 Sep 2008 05:46:42 +0000 (05:46 +0000)
set the maximum number of attachments for forum posts.   Default: 1.

Works, but still needs a bit of polishing to deal with re-editing posts (to stop people going above maxattachments)

Also fixed documentation for attachments.

lang/en_utf8/forum.php
lang/en_utf8/help/forum/attachment2.html [new file with mode: 0644]
lang/en_utf8/help/forum/maxattachments.html [new file with mode: 0644]
mod/forum/db/upgrade.php
mod/forum/lib.php
mod/forum/mod_form.php
mod/forum/post.php
mod/forum/post_form.php
mod/forum/settings.php
mod/forum/version.php

index e5fd92af3133efd57790ef60a1dd8e2e9bee4229..8e9808f4efea5c2e207c038165b09fb7722d1602 100644 (file)
@@ -56,6 +56,7 @@ $string['configenabletimedposts'] = 'Set to \'yes\' if you want to allow setting
 $string['configlogblocked'] = 'Forum cron may log blocked attempts to send emails to users with disabled email.';
 $string['configlongpost'] = 'Any post over this length (in characters not including HTML) is considered long. Posts displayed on the site front page, social format course pages, or user profiles are shortened to a natural break somewhere between the forum_shortpost and forum_longpost values.';
 $string['configmanydiscussions'] = 'Maximum number of discussions shown in a forum per page';
+$string['configmaxattachments'] = 'Default maximum number of attachments allowed per post.';
 $string['configmaxbytes'] = 'Default maximum size for all forum attachments on the site (subject to course limits and other local settings)';
 $string['configoldpostdays'] = 'Number of days old any post is considered read.';
 $string['configreplytouser'] = 'When a forum post is mailed out, should it contain the user\'s email address so that recipients can reply personally rather than via the forum? Even if set to \'Yes\' users can choose in their profile to keep their email address secret.';
@@ -160,6 +161,7 @@ $string['markread'] = 'Mark read';
 $string['markreadbutton'] = 'Mark<br />read';
 $string['markunread'] = 'Mark unread';
 $string['markunreadbutton'] = 'Mark<br />unread';
+$string['maxattachments'] = 'Maximum attachments';
 $string['maxattachmentsize'] = 'Maximum attachment size';
 $string['maxtimehaspassed'] = 'Sorry, but the maximum time for editing this post ($a) has passed!';
 $string['message'] = 'Message';
diff --git a/lang/en_utf8/help/forum/attachment2.html b/lang/en_utf8/help/forum/attachment2.html
new file mode 100644 (file)
index 0000000..dc8e25d
--- /dev/null
@@ -0,0 +1,13 @@
+<h1>Attachments for posts</h1>
+
+<p>You can optionally attach one or more files (the number depends 
+   on the forum settings) to any post in the forums.</p>
+
+<p>This can useful when you want to share a picture or a 
+   word processing document, for example.</p>
+
+<p>Your files can be of any type, however it is highly recommended
+   that the file is named using standard 3-letter internet 
+   suffixes such as .doc for a Word document, .jpg or .png 
+   for an image, and so on.  This will make it easier for others 
+   to download and view your attachment in their browsers.</p>
diff --git a/lang/en_utf8/help/forum/maxattachments.html b/lang/en_utf8/help/forum/maxattachments.html
new file mode 100644 (file)
index 0000000..bbbea77
--- /dev/null
@@ -0,0 +1,3 @@
+<h1>Maximum number of attachments</h1>
+
+<p>This setting allows you to control how many attachments are allowed for each post in this forum.</p>
index 64b8e02c3e7542c76bd0b4c209cb70145ed4816b..d5f13c1dd92a8cdd568e6dcd46ce09c9ee7b8357 100644 (file)
@@ -166,6 +166,22 @@ function xmldb_forum_upgrade($oldversion) {
         upgrade_mod_savepoint($result, 2008081900, 'forum');
     }
 
+    if ($result && $oldversion < 2008090800) {
+
+    /// Define field maxattachments to be added to forum
+        $table = new xmldb_table('forum');
+        $field = new xmldb_field('maxattachments', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1', 'maxbytes');
+
+    /// Conditionally launch add field maxattachments
+        if (!$dbman->field_exists($table, $field)) {
+            $dbman->add_field($table, $field);
+        }
+
+    /// forum savepoint reached
+        upgrade_mod_savepoint($result, 2008090800, 'forum');
+    }
+
+
 
     return $result;
 }
index 3c40fa8bd27e54688b33c2cace4f75322e1c16b6..485ea4a8ae9fa9eff45cca6edb16707b88fb742a 100644 (file)
@@ -4067,10 +4067,9 @@ function forum_pluginfile($course, $cminfo, $context, $filearea, $args) {
  * @param $newfile is a full upload array from $_FILES
  * @param $message is a string to hold the messages.
  */
-function forum_add_attachment($post, $cm, $mform=null, &$message=null, $remove_previous=true) {
+function forum_add_attachment($post, $forum, $cm, $mform=null, &$message=null, $remove_previous=true) {
     global $CFG, $DB, $USER;
 
-    //TODO: add message when overwriting
     if (empty($mform)) {
         return false;
     }
@@ -4078,29 +4077,43 @@ function forum_add_attachment($post, $cm, $mform=null, &$message=null, $remove_p
     $filearea = 'forum_attachment';
 
     $fs = get_file_storage();
-
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 
-    if ($remove_previous) {
+    // The logic here really needs work to handle re-editing properly  TODO XXX
+
+    if ($remove_previous) {  // Remove ALL previous files for this post
         $fs->delete_area_files($context->id, $filearea, $post->id);
-        $post->attachment = '';
-        $DB->update_record('forum_posts', $post);
+        $post->attachment = 0;
+        $DB->set_field('forum_posts', 'attachment', 0, array('id'=>$post->id));  // Update it now in case we fail later on
     }
 
-    if ($mform->save_stored_file('attachment', $context->id, $filearea, $post->id, '/', null, true, $USER->id)) {
-        $post->attachment = '1';
-        $DB->update_record('forum_posts', $post);
-        return true;
+    $values = $mform->get_data();
 
-    } else {
-        return false;
+    if (!isset($forum->maxattachments)) {  // TODO - delete this once we add a field to the forum table
+        $forum->maxattachments = 3;
+    }
+    
+    for ($i=0; $i<$forum->maxattachments; $i++) {
+        $elementname = 'attachment'.$i;
+        if (empty($values->$elementname)) {         // Nothing defined
+            continue;
+        }
+        if (!is_numeric($values->$elementname)) {   // Pre-existing file reference, skip it
+            continue;
+        }
+        if ($mform->save_stored_file($elementname, $context->id, $filearea, $post->id, '/', null, true, $USER->id)) {
+            $post->attachment = '1';
+            $DB->set_field('forum_posts', 'attachment', 1, array('id'=>$post->id));  // Update it now in case we fail later on
+        } else {
+            return false;
+        }
     }
 }
 
 /**
  * Relinks urls linked to draftfile.php in $post->message.
  */
-function forum_relink_inline_attachments($post, $cm){
+function forum_relink_inline_attachments($post, $forum, $cm){
     global $DB;
     
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
@@ -4133,8 +4146,8 @@ function forum_add_new_post($post, $mform, &$message) {
         return false;
     }
 
-    forum_relink_inline_attachments($post, $cm);   
-    forum_add_attachment($post, $cm, $mform, $message, false);
+    forum_relink_inline_attachments($post, $forum, $cm);   
+    forum_add_attachment($post, $forum, $cm, $mform, $message, false);
 
     // Update discussion modified date
     $DB->set_field("forum_discussions", "timemodified", $post->modified, array("id" => $post->discussion));
@@ -4176,8 +4189,8 @@ function forum_update_post($post, $mform, &$message) {
         return false;
     }
 
-    forum_relink_inline_attachments($post, $cm);
-    forum_add_attachment($post, $cm, $mform, $message, true);
+    forum_relink_inline_attachments($post, $forum, $cm);
+    forum_add_attachment($post, $forum, $cm, $mform, $message, false);
 
     if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
         forum_tp_mark_post_read($post->userid, $post, $post->forum);
@@ -4210,7 +4223,7 @@ function forum_add_discussion($discussion, $mform=null, &$message=null) {
     $post->mailed      = 0;
     $post->subject     = $discussion->name;
     $post->message     = $discussion->intro;
-    $post->attachment  = "";
+    $post->attachment  = 0;
     $post->forum       = $forum->id;     // speedup
     $post->course      = $forum->course; // speedup
     $post->format      = $discussion->format;
@@ -4239,8 +4252,8 @@ function forum_add_discussion($discussion, $mform=null, &$message=null) {
         return 0;
     }
 
-    forum_relink_inline_attachments($post, $cm);
-    forum_add_attachment($post, $cm, $mform, $message, false);
+    forum_relink_inline_attachments($post, $forum, $cm);
+    forum_add_attachment($post, $forum, $cm, $mform, $message, false);
 
     if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
         forum_tp_mark_post_read($post->userid, $post, $post->forum);
index ef67c03b0ff4832adfcbc959328265edac5b2688..fa5f6f4f745e8aa9e382488e976803aa4a11c8c4 100644 (file)
@@ -54,6 +54,11 @@ class mod_forum_mod_form extends moodleform_mod {
         $mform->setHelpButton('maxbytes', array('maxattachmentsize', get_string('maxattachmentsize', 'forum'), 'forum'));
         $mform->setDefault('maxbytes', $CFG->forum_maxbytes);
 
+        $choices = array(0,1,2,3,4,5,6,7,8,9);
+        $mform->addElement('select', 'maxattachments', get_string('maxattachments', 'forum'), $choices);
+        $mform->setHelpButton('maxattachments', array('maxattachments', get_string('maxattachments', 'forum'), 'forum'));
+        $mform->setDefault('maxattachments', $CFG->forum_maxattachments);
+
         if ($CFG->enablerssfeeds && isset($CFG->forum_enablerssfeeds) && $CFG->forum_enablerssfeeds) {
 //-------------------------------------------------------------------------------
             $mform->addElement('header', '', get_string('rss'));
index 053b33a494f7ff4ff1c39947461200d081b77138..459f58c513b2a42f7d293f3c525ca53e85a13210 100644 (file)
     }
     $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
 
+    if (!isset($forum->maxattachments)) {  // TODO - delete this once we add a field to the forum table
+        $forum->maxattachments = 3;
+    }
+
     $mform_post = new mod_forum_post_form('post.php', array('course'=>$course, 'cm'=>$cm, 'coursecontext'=>$coursecontext, 'modcontext'=>$modcontext, 'forum'=>$forum, 'post'=>$post));
 
     if ($fromform = $mform_post->get_data()) {
 
-
         require_login($course, false, $cm);
 
         if (empty($SESSION->fromurl)) {
         $subscribe = !empty($USER->autosubscribe);
     }
 
+    $defaultattachments = array();
+
+    if ($forum->maxattachments) {
+        for ($i=0; $i<$forum->maxattachments; $i++) {
+            $defaultattachments['attachment'.$i] = '';
+        }
+    
+        if (!empty($post->attachment)) {   // We already have some attachments, so show them
+            $fs = get_file_storage();
+    
+            $i = 0;
+            if ($files = $fs->get_area_files($modcontext->id, 'forum_attachment', $post->id, "timemodified ASC", false)) {
+                foreach ($files as $file) {
+                    $defaultattachments['attachment'.$i] = $file->get_filename();
+                    $i++;
+                }
+            }
+        }
+    }
+
     // HACK ALERT: this is very wrong, the defaults should be always initialized before calling $mform->get_data() !!!
     $mform_post->set_data(array(    'general'=>$heading,
                                         'subject'=>$post->subject,
                                         'userid'=>$post->userid,
                                         'parent'=>$post->parent,
                                         'discussion'=>$post->discussion,
-                                        'course'=>$course->id)+
+                                        'course'=>$course->id) +
+
+                                        $defaultattachments +
 
-                                        $page_params+
+                                        $page_params +
 
                                 (isset($post->format)?array(
                                         'format'=>$post->format):
index c9b05eb2decb0e749f6790d9f9fe5fb064b1807d..d6ee1d58d01f8253df1b435f2fd1497d1c815dd9 100644 (file)
@@ -52,9 +52,11 @@ class mod_forum_post_form extends moodleform {
             $mform->setHelpButton('subscribemessage', array('subscription', get_string('subscription', 'forum'), 'forum'));
         }
 
-        if ($forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext))  {  //  1 = No attachments at all
-            $mform->addElement('filepicker', 'attachment', get_string('attachment', 'forum'));
-            $mform->setHelpButton('attachment', array('attachment', get_string('attachment', 'forum'), 'forum'));
+        if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext))  {  //  1 = No attachments at all
+            for ($i=0; $i<$forum->maxattachments; $i++) {
+                $mform->addElement('filepicker', 'attachment'.$i, get_string('attachment', 'forum'));
+                $mform->setHelpButton('attachment'.$i, array('attachment2', get_string('attachment', 'forum'), 'forum'));
+            }
         }
 
         if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) { // hack alert
index 323b7872f7be5983579ee7d19940c683d4ae2aec..6c5b1b2561f3e655144664dde3e6212e99c5403a 100644 (file)
@@ -23,6 +23,10 @@ $settings->add(new admin_setting_configtext('forum_manydiscussions', get_string(
 $settings->add(new admin_setting_configselect('forum_maxbytes', get_string('maxattachmentsize', 'forum'),
                    get_string('configmaxbytes', 'forum'), 512000, get_max_upload_sizes($CFG->maxbytes)));
 
+// Default number of attachments allowed per post in all forums
+$settings->add(new admin_setting_configtext('forum_maxattachments', get_string('maxattachments', 'forum'),
+                   get_string('configmaxattachments', 'forum'), 1, PARAM_INT));
+
 // Default whether user needs to mark a post as read
 $settings->add(new admin_setting_configcheckbox('forum_trackreadposts', get_string('trackforum', 'forum'),
                    get_string('configtrackreadposts', 'forum'), 1));
index 0503f290e7121a329c1adeeed62ad9a91fb60711..773e8aa8e2c79c8542f9bfbbdac2002518fb8f2d 100644 (file)
@@ -5,8 +5,8 @@
 //  This fragment is called by /admin/index.php
 ////////////////////////////////////////////////////////////////////////////////
 
-$module->version  = 2008081900;
-$module->requires = 2008081600;  // Requires this Moodle version
+$module->version  = 2008090800;
+$module->requires = 2008090800;  // Requires this Moodle version
 $module->cron     = 60;
 
 ?>