From 4db9d482531be2f8334ff51b73c4d5079900d5a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Sun, 25 Aug 2013 15:09:39 +0200 Subject: [PATCH 1/3] MDL-28094 add function that detects if subdirs used and fix subdirs option in form editor --- lib/filelib.php | 25 +++++++++++++++++++++++++ lib/form/editor.php | 5 ++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/filelib.php b/lib/filelib.php index efe335a060a..2260e4af3cd 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -80,6 +80,31 @@ function file_encode_url($urlbase, $path, $forcedownload=false, $https=false) { return $return; } +/** + * Detects if area contains subdirs, + * this is intended for file areas that are attached to content + * migrated from 1.x where subdirs were allowed everywhere. + * + * @param context $context + * @param string $component + * @param string $filearea + * @param string $itemid + * @return bool + */ +function file_area_contains_subdirs(context $context, $component, $filearea, $itemid) { + global $DB; + + if (!isset($itemid)) { + // Not initialised yet. + return false; + } + + // Detect if any directories are already present, this is necessary for content upgraded from 1.x. + $select = "contextid = :contextid AND component = :component AND filearea = :filearea AND itemid = :itemid AND filepath <> '/' AND filename = '.'"; + $params = array('contextid'=>$context->id, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid); + return $DB->record_exists_select('files', $select, $params); +} + /** * Prepares 'editor' formslib element from data in database * diff --git a/lib/form/editor.php b/lib/form/editor.php index 22fbbf3e0e5..87d888dbbbd 100644 --- a/lib/form/editor.php +++ b/lib/form/editor.php @@ -91,6 +91,9 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { $this->_options['trusted'] = trusttext_trusted($this->_options['context']); parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes); + // Note: for some reason the code using this setting does not like bools. + $this->_options['subdirs'] = (int)($this->_options['subdirs'] == 1); + editors_head_setup(); } @@ -205,7 +208,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { * @param bool $allow true if sub directory can be created. */ function setSubdirs($allow) { - $this->_options['subdirs'] = $allow; + $this->_options['subdirs'] = (int)($allow == 1); } /** From 5e95223e86f907281f9e21060b766afd690e7409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Sun, 25 Aug 2013 15:10:27 +0200 Subject: [PATCH 2/3] MDL-28094 keep subdirs in content upgraded from 1.x --- blog/edit.php | 3 ++- course/edit.php | 2 ++ course/editcategory.php | 3 ++- grade/edit/outcome/edit.php | 2 ++ grade/edit/scale/edit.php | 2 ++ group/group.php | 2 ++ mod/forum/classes/post_form.php | 9 ++++++--- mod/forum/lib.php | 6 +++--- mod/forum/post.php | 3 ++- mod/glossary/edit.php | 3 ++- tag/edit.php | 3 ++- 11 files changed, 27 insertions(+), 11 deletions(-) diff --git a/blog/edit.php b/blog/edit.php index 2c367630988..5c06993b957 100644 --- a/blog/edit.php +++ b/blog/edit.php @@ -162,7 +162,8 @@ if (!empty($entry->id)) { } require_once('edit_form.php'); -$summaryoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'context'=>$sitecontext); +$summaryoptions = array('maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'context'=>$sitecontext, + 'subdirs'=>file_area_contains_subdirs($sitecontext, 'blog', 'post', $entry->id)); $attachmentoptions = array('subdirs'=>false, 'maxfiles'=> 99, 'maxbytes'=>$CFG->maxbytes); $blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', 'attachmentoptions', 'sitecontext', 'courseid', 'modid')); diff --git a/course/edit.php b/course/edit.php index c549cc13929..1021c234001 100644 --- a/course/edit.php +++ b/course/edit.php @@ -70,6 +70,7 @@ $overviewfilesoptions = course_overviewfiles_options($course); if (!empty($course)) { //add context for editor $editoroptions['context'] = $coursecontext; + $editoroptions['subdirs'] = file_area_contains_subdirs($coursecontext, 'course', 'summary', 0); $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0); if ($overviewfilesoptions) { file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, $coursecontext, 'course', 'overviewfiles', 0); @@ -84,6 +85,7 @@ if (!empty($course)) { } else { //editor should respect category context if course context is not set. $editoroptions['context'] = $catcontext; + $editoroptions['subdirs'] = 0; $course = file_prepare_standard_editor($course, 'summary', $editoroptions, null, 'course', 'summary', null); if ($overviewfilesoptions) { file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, null, 'course', 'overviewfiles', 0); diff --git a/course/editcategory.php b/course/editcategory.php index a2de1f17331..05a5777f542 100644 --- a/course/editcategory.php +++ b/course/editcategory.php @@ -77,7 +77,8 @@ $editoroptions = array( 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $CFG->maxbytes, 'trusttext' => true, - 'context' => $editorcontext + 'context' => $editorcontext, + 'subdirs' => file_area_contains_subdirs($editorcontext, 'coursecat', 'description', $itemid), ); $category = file_prepare_standard_editor($category, 'description', $editoroptions, $editorcontext, 'coursecat', 'description', $itemid); diff --git a/grade/edit/outcome/edit.php b/grade/edit/outcome/edit.php index 2d2c670c685..48164e2dfc1 100644 --- a/grade/edit/outcome/edit.php +++ b/grade/edit/outcome/edit.php @@ -111,8 +111,10 @@ $editoroptions = array( ); if (!empty($outcome_rec->id)) { + $editoroptions['subdirs'] = file_area_contains_subdirs($systemcontext, 'grade', 'outcome', $outcome_rec->id); $outcome_rec = file_prepare_standard_editor($outcome_rec, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', $outcome_rec->id); } else { + $editoroptions['subdirs'] = false; $outcome_rec = file_prepare_standard_editor($outcome_rec, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', null); } diff --git a/grade/edit/scale/edit.php b/grade/edit/scale/edit.php index 9374a9ff447..04373b19912 100644 --- a/grade/edit/scale/edit.php +++ b/grade/edit/scale/edit.php @@ -104,8 +104,10 @@ $editoroptions = array( ); if (!empty($scale_rec->id)) { + $editoroptions['subdirs'] = file_area_contains_subdirs($systemcontext, 'grade', 'scale', $scale_rec->id); $scale_rec = file_prepare_standard_editor($scale_rec, 'description', $editoroptions, $systemcontext, 'grade', 'scale', $scale_rec->id); } else { + $editoroptions['subdirs'] = false; $scale_rec = file_prepare_standard_editor($scale_rec, 'description', $editoroptions, $systemcontext, 'grade', 'scale', null); } $mform = new edit_scale_form(null, compact('gpr', 'editoroptions')); diff --git a/group/group.php b/group/group.php index 4e0d8b0ca04..b6aa7539d64 100644 --- a/group/group.php +++ b/group/group.php @@ -79,8 +79,10 @@ $returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id.'&group='.$id; // Prepare the description editor: We do support files for group descriptions $editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$course->maxbytes, 'trust'=>false, 'context'=>$context, 'noclean'=>true); if (!empty($group->id)) { + $editoroptions['subdirs'] = file_area_contains_subdirs($context, 'group', 'description', $group->id); $group = file_prepare_standard_editor($group, 'description', $editoroptions, $context, 'group', 'description', $group->id); } else { + $editoroptions['subdirs'] = false; $group = file_prepare_standard_editor($group, 'description', $editoroptions, $context, 'group', 'description', null); } diff --git a/mod/forum/classes/post_form.php b/mod/forum/classes/post_form.php index 68404469835..d768647de94 100644 --- a/mod/forum/classes/post_form.php +++ b/mod/forum/classes/post_form.php @@ -57,9 +57,11 @@ class mod_forum_post_form extends moodleform { /** * Returns the options array to use in forum text editor * + * @param context_module $context + * @param int $postid post id, use null when adding new post * @return array */ - public static function editor_options() { + public static function editor_options(context_module $context, $postid) { 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); @@ -67,7 +69,8 @@ class mod_forum_post_form extends moodleform { 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $maxbytes, 'trusttext'=> true, - 'return_types'=> FILE_INTERNAL | FILE_EXTERNAL + 'return_types'=> FILE_INTERNAL | FILE_EXTERNAL, + 'subdirs' => file_area_contains_subdirs($context, 'mod_forum', 'post', $postid) ); } @@ -106,7 +109,7 @@ class mod_forum_post_form extends moodleform { $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, self::editor_options()); + $mform->addElement('editor', 'message', get_string('message', 'forum'), null, self::editor_options($modcontext, (empty($post->id) ? null : $post->id))); $mform->setType('message', PARAM_RAW); $mform->addRule('message', get_string('required'), 'required', null, 'client'); diff --git a/mod/forum/lib.php b/mod/forum/lib.php index be0a2eae793..41e9be70c93 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -4370,7 +4370,7 @@ function forum_add_new_post($post, $mform, &$message) { $post->id = $DB->insert_record("forum_posts", $post); $post->message = file_save_draft_area_files($post->itemid, $context->id, 'mod_forum', 'post', $post->id, - mod_forum_post_form::editor_options(), $post->message); + mod_forum_post_form::editor_options($context, null), $post->message); $DB->set_field('forum_posts', 'message', $post->message, array('id'=>$post->id)); forum_add_attachment($post, $forum, $cm, $mform, $message); @@ -4420,7 +4420,7 @@ function forum_update_post($post, $mform, &$message) { $discussion->timeend = $post->timeend; } $post->message = file_save_draft_area_files($post->itemid, $context->id, 'mod_forum', 'post', $post->id, - mod_forum_post_form::editor_options(), $post->message); + mod_forum_post_form::editor_options($context, $post->id), $post->message); $DB->set_field('forum_posts', 'message', $post->message, array('id'=>$post->id)); $DB->update_record('forum_discussions', $discussion); @@ -4484,7 +4484,7 @@ function forum_add_discussion($discussion, $mform=null, $unused=null, $userid=nu if (!empty($cm->id) && !empty($discussion->itemid)) { // In "single simple discussions" this may not exist yet $context = context_module::instance($cm->id); $text = file_save_draft_area_files($discussion->itemid, $context->id, 'mod_forum', 'post', $post->id, - mod_forum_post_form::editor_options(), $post->message); + mod_forum_post_form::editor_options($context, null), $post->message); $DB->set_field('forum_posts', 'message', $text, array('id'=>$post->id)); } diff --git a/mod/forum/post.php b/mod/forum/post.php index 3a7bf1bc957..63342eee002 100644 --- a/mod/forum/post.php +++ b/mod/forum/post.php @@ -557,8 +557,9 @@ if (forum_is_subscribed($USER->id, $forum->id)) { $subscribe = !empty($USER->autosubscribe); } +$postid = empty($post->id) ? null : $post->id; $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, mod_forum_post_form::editor_options(), $post->message); +$currenttext = file_prepare_draft_area($draftid_editor, $modcontext->id, 'mod_forum', 'post', $postid, mod_forum_post_form::editor_options($modcontext, $postid), $post->message); $mform_post->set_data(array( 'attachments'=>$draftitemid, 'general'=>$heading, 'subject'=>$post->subject, diff --git a/mod/glossary/edit.php b/mod/glossary/edit.php index 00e9343c80f..e77cb44c443 100644 --- a/mod/glossary/edit.php +++ b/mod/glossary/edit.php @@ -66,7 +66,8 @@ if ($id) { // if entry is specified $maxfiles = 99; // TODO: add some setting $maxbytes = $course->maxbytes; // TODO: add some setting -$definitionoptions = array('trusttext'=>true, 'subdirs'=>false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes, 'context'=>$context); +$definitionoptions = array('trusttext'=>true, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes, 'context'=>$context, + 'subdirs'=>file_area_contains_subdirs($context, 'mod_glossary', 'entry', $entry->id)); $attachmentoptions = array('subdirs'=>false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes); $entry = file_prepare_standard_editor($entry, 'definition', $definitionoptions, $context, 'mod_glossary', 'entry', $entry->id); diff --git a/tag/edit.php b/tag/edit.php index e0c80f807d0..2e2b74aa2c0 100644 --- a/tag/edit.php +++ b/tag/edit.php @@ -76,7 +76,8 @@ $editoroptions = array( 'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $CFG->maxbytes, 'trusttext' => false, - 'context' => $systemcontext + 'context' => $systemcontext, + 'subdirs' => file_area_contains_subdirs($systemcontext, 'tag', 'description', $tag->id), ); $tag = file_prepare_standard_editor($tag, 'description', $editoroptions, $systemcontext, 'tag', 'description', $tag->id); From f9beaf4472ca024c5192b3502480d2c0495a552e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Sun, 25 Aug 2013 15:24:19 +0200 Subject: [PATCH 3/3] MDL-28094 fix subdirs support in module intro editor --- course/modedit.php | 2 +- course/moodleform_mod.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/course/modedit.php b/course/modedit.php index 8cfe0a0afd7..db70a9fcedd 100644 --- a/course/modedit.php +++ b/course/modedit.php @@ -78,7 +78,7 @@ if (!empty($add)) { if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) { $draftid_editor = file_get_submitted_draft_itemid('introeditor'); - file_prepare_draft_area($draftid_editor, null, null, null, null); + file_prepare_draft_area($draftid_editor, null, null, null, null, array('subdirs'=>true)); $data->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default } diff --git a/course/moodleform_mod.php b/course/moodleform_mod.php index 97724aa7f47..8a7aa14b6bc 100644 --- a/course/moodleform_mod.php +++ b/course/moodleform_mod.php @@ -821,7 +821,7 @@ abstract class moodleform_mod extends moodleform { $label = is_null($customlabel) ? get_string('moduleintro') : $customlabel; $mform->addElement('editor', 'introeditor', $label, array('rows' => 10), array('maxfiles' => EDITOR_UNLIMITED_FILES, - 'noclean' => true, 'context' => $this->context)); + 'noclean' => true, 'context' => $this->context, 'subdirs' => true)); $mform->setType('introeditor', PARAM_RAW); // no XSS prevention here, users must be trusted if ($required) { $mform->addRule('introeditor', get_string('required'), 'required', null, 'client');