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 ///////////////////////////////////////////////////////////
$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));
}
}
$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;
$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));
$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);
$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);
// 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));
}
$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!
}
$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,
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;
$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->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');
}
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');
}