mirror of
https://github.com/moodle/moodle.git
synced 2025-08-06 17:36:38 +02:00
MDL-14722 Added new maxattachments setting at admin and forum level to
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.
This commit is contained in:
parent
757f30a20c
commit
30a9aff589
10 changed files with 111 additions and 28 deletions
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue