Skip to content

Commit

Permalink
MDL-33948 mod_forum correctly passes files options to file_save_draft…
Browse files Browse the repository at this point in the history
…_area_files()
  • Loading branch information
marinaglancy authored and danpoltawski committed Jul 2, 2012
1 parent ec3fe6e commit dff8438
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
19 changes: 13 additions & 6 deletions mod/forum/lib.php
Expand Up @@ -27,6 +27,7 @@
require_once($CFG->libdir.'/filelib.php');
require_once($CFG->libdir.'/eventslib.php');
require_once($CFG->dirroot.'/user/selector/lib.php');
require_once($CFG->dirroot.'/mod/forum/post_form.php');

/// CONSTANTS ///////////////////////////////////////////////////////////

Expand Down Expand Up @@ -99,7 +100,8 @@ function forum_add_instance($forum, $mform = null) {
$discussion = $DB->get_record('forum_discussions', array('id'=>$discussion->id), '*', MUST_EXIST);
$post = $DB->get_record('forum_posts', array('id'=>$discussion->firstpost), '*', MUST_EXIST);

$post->message = file_save_draft_area_files($draftid, $modcontext->id, 'mod_forum', 'post', $post->id, array('subdirs'=>true), $post->message);
$post->message = file_save_draft_area_files($draftid, $modcontext->id, 'mod_forum', 'post', $post->id,
mod_forum_post_form::attachment_options($forum), $post->message);
$DB->set_field('forum_posts', 'message', $post->message, array('id'=>$post->id));
}
}
Expand Down Expand Up @@ -196,7 +198,8 @@ function forum_update_instance($forum, $mform) {
$discussion = $DB->get_record('forum_discussions', array('id'=>$discussion->id), '*', MUST_EXIST);
$post = $DB->get_record('forum_posts', array('id'=>$discussion->firstpost), '*', MUST_EXIST);

$post->message = file_save_draft_area_files($draftid, $modcontext->id, 'mod_forum', 'post', $post->id, array('subdirs'=>true), $post->message);
$post->message = file_save_draft_area_files($draftid, $modcontext->id, 'mod_forum', 'post', $post->id,
mod_forum_post_form::editor_options(), $post->message);
}

$post->subject = $forum->name;
Expand Down Expand Up @@ -4230,7 +4233,8 @@ function forum_add_attachment($post, $forum, $cm, $mform=null, &$message=null) {

$info = file_get_draft_area_info($post->attachments);
$present = ($info['filecount']>0) ? '1' : '';
file_save_draft_area_files($post->attachments, $context->id, 'mod_forum', 'attachment', $post->id);
file_save_draft_area_files($post->attachments, $context->id, 'mod_forum', 'attachment', $post->id,
mod_forum_post_form::attachment_options($forum));

$DB->set_field('forum_posts', 'attachment', $present, array('id'=>$post->id));

Expand Down Expand Up @@ -4262,7 +4266,8 @@ function forum_add_new_post($post, $mform, &$message) {
$post->attachment = "";

$post->id = $DB->insert_record("forum_posts", $post);
$post->message = file_save_draft_area_files($post->itemid, $context->id, 'mod_forum', 'post', $post->id, array('subdirs'=>true), $post->message);
$post->message = file_save_draft_area_files($post->itemid, $context->id, 'mod_forum', 'post', $post->id,
mod_forum_post_form::editor_options(), $post->message);
$DB->set_field('forum_posts', 'message', $post->message, array('id'=>$post->id));
forum_add_attachment($post, $forum, $cm, $mform, $message);

Expand Down Expand Up @@ -4308,7 +4313,8 @@ function forum_update_post($post, $mform, &$message) {
$discussion->timestart = $post->timestart;
$discussion->timeend = $post->timeend;
}
$post->message = file_save_draft_area_files($post->itemid, $context->id, 'mod_forum', 'post', $post->id, array('subdirs'=>true), $post->message);
$post->message = file_save_draft_area_files($post->itemid, $context->id, 'mod_forum', 'post', $post->id,
mod_forum_post_form::editor_options(), $post->message);
$DB->set_field('forum_posts', 'message', $post->message, array('id'=>$post->id));

$DB->update_record('forum_discussions', $discussion);
Expand Down Expand Up @@ -4371,7 +4377,8 @@ function forum_add_discussion($discussion, $mform=null, &$message=null, $userid=
// TODO: Fix the calling code so that there always is a $cm when this function is called
if (!empty($cm->id) && !empty($discussion->itemid)) { // In "single simple discussions" this may not exist yet
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$text = file_save_draft_area_files($discussion->itemid, $context->id, 'mod_forum', 'post', $post->id, array('subdirs'=>true), $post->message);
$text = file_save_draft_area_files($discussion->itemid, $context->id, 'mod_forum', 'post', $post->id,
mod_forum_post_form::editor_options(), $post->message);
$DB->set_field('forum_posts', 'message', $text, array('id'=>$post->id));
}

Expand Down
4 changes: 2 additions & 2 deletions mod/forum/post.php
Expand Up @@ -510,7 +510,7 @@
$mform_post = new mod_forum_post_form('post.php', array('course'=>$course, 'cm'=>$cm, 'coursecontext'=>$coursecontext, 'modcontext'=>$modcontext, 'forum'=>$forum, 'post'=>$post));

$draftitemid = file_get_submitted_draft_itemid('attachments');
file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_forum', 'attachment', empty($post->id)?null:$post->id);
file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_forum', 'attachment', empty($post->id)?null:$post->id, mod_forum_post_form::attachment_options($forum));

//load data into form NOW!

Expand Down Expand Up @@ -550,7 +550,7 @@
}

$draftid_editor = file_get_submitted_draft_itemid('message');
$currenttext = file_prepare_draft_area($draftid_editor, $modcontext->id, 'mod_forum', 'post', empty($post->id) ? null : $post->id, array('subdirs'=>true), $post->message);
$currenttext = file_prepare_draft_area($draftid_editor, $modcontext->id, 'mod_forum', 'post', empty($post->id) ? null : $post->id, mod_forum_post_form::editor_options(), $post->message);
$mform_post->set_data(array( 'attachments'=>$draftitemid,
'general'=>$heading,
'subject'=>$post->subject,
Expand Down
51 changes: 37 additions & 14 deletions mod/forum/post_form.php
Expand Up @@ -29,6 +29,41 @@

class mod_forum_post_form extends moodleform {

/**
* Returns the options array to use in filemanager for forum attachments
*
* @param stdClass $forum
* @return array
*/
public static function attachment_options($forum) {
global $COURSE, $PAGE, $CFG;
$maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes, $forum->maxbytes);
return array(
'subdirs' => 0,
'maxbytes' => $maxbytes,
'maxfiles' => $forum->maxattachments,
'accepted_types' => '*',
'return_types' => FILE_INTERNAL
);
}

/**
* Returns the options array to use in forum text editor
*
* @return array
*/
public static function editor_options() {
global $COURSE, $PAGE, $CFG;
// TODO: add max files and max size support
$maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes);
return array(
'maxfiles' => EDITOR_UNLIMITED_FILES,
'maxbytes' => $maxbytes,
'trusttext'=> true,
'return_types'=> FILE_INTERNAL | FILE_EXTERNAL
);
}

function definition() {

global $CFG;
Expand All @@ -40,21 +75,14 @@ function definition() {
$modcontext = $this->_customdata['modcontext'];
$forum = $this->_customdata['forum'];
$post = $this->_customdata['post'];
// if $forum->maxbytes == '0' means we should use $course->maxbytes
if ($forum->maxbytes == '0') {
$forum->maxbytes = $course->maxbytes;
}
// TODO: add max files and max size support
$editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'trusttext'=>true,
'context'=>$modcontext, 'return_types'=>FILE_INTERNAL | FILE_EXTERNAL);

$mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data
$mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
$mform->setType('subject', PARAM_TEXT);
$mform->addRule('subject', get_string('required'), 'required', null, 'client');
$mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');

$mform->addElement('editor', 'message', get_string('message', 'forum'), null, $editoroptions);
$mform->addElement('editor', 'message', get_string('message', 'forum'), null, self::editor_options());
$mform->setType('message', PARAM_RAW);
$mform->addRule('message', get_string('required'), 'required', null, 'client');

Expand Down Expand Up @@ -82,12 +110,7 @@ function definition() {
}

if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext)) { // 1 = No attachments at all
$mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null,
array('subdirs'=>0,
'maxbytes'=>$forum->maxbytes,
'maxfiles'=>$forum->maxattachments,
'accepted_types'=>'*',
'return_types'=>FILE_INTERNAL));
$mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null, self::attachment_options($forum));
$mform->addHelpButton('attachments', 'attachment', 'forum');
}

Expand Down

0 comments on commit dff8438

Please sign in to comment.