From e9de1cf49cae005ea2260ae1cc80dd30dcfa3959 Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Tue, 12 Jul 2011 10:45:55 +0800 Subject: [PATCH 1/4] MDL-27896 context - Fixed editor context to observe filter status --- cohort/edit.php | 4 ++-- course/edit.php | 2 ++ course/editcategory.php | 7 ++++++- grade/edit/outcome/edit.php | 8 +++++++- grade/edit/scale/edit.php | 8 +++++++- mod/assignment/lib.php | 1 + mod/assignment/type/online/assignment.class.php | 7 ++++++- mod/feedback/item/label/lib.php | 4 ++++ mod/lesson/editpage.php | 1 + mod/wiki/pagelib.php | 2 ++ mod/workshop/exsubmission.php | 9 ++++++++- mod/workshop/form/accumulative/lib.php | 3 +++ mod/workshop/form/comments/lib.php | 3 +++ mod/workshop/form/numerrors/lib.php | 3 +++ mod/workshop/form/rubric/lib.php | 3 +++ mod/workshop/submission.php | 9 ++++++++- tag/edit.php | 7 ++++++- user/edit.php | 9 ++++++++- user/editadvanced.php | 9 ++++++++- 19 files changed, 88 insertions(+), 11 deletions(-) diff --git a/cohort/edit.php b/cohort/edit.php index 4063e9c54c9..dc1bd08bb04 100644 --- a/cohort/edit.php +++ b/cohort/edit.php @@ -98,12 +98,12 @@ if ($delete and $cohort->id) { $editoroptions = array('maxfiles'=>0, 'context'=>$context); if ($cohort->id) { // edit existing - $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions); + $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context); $strheading = get_string('editcohort', 'cohort'); } else { // add new - $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions); + $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context); $strheading = get_string('addcohort', 'cohort'); } diff --git a/course/edit.php b/course/edit.php index b4871329613..d184328dff5 100644 --- a/course/edit.php +++ b/course/edit.php @@ -77,6 +77,8 @@ if (!empty($course)) { } } $course->allowedmods = $allowedmods; + //add context for editor + $editoroptions['context'] = $coursecontext; $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0); } else { diff --git a/course/editcategory.php b/course/editcategory.php index 3cff561935c..f033b8d8102 100644 --- a/course/editcategory.php +++ b/course/editcategory.php @@ -49,7 +49,12 @@ if ($id) { $PAGE->set_pagelayout('admin'); -$editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true); +$editoroptions = array( + 'maxfiles' => EDITOR_UNLIMITED_FILES, + 'maxbytes' => $CFG->maxbytes, + 'trusttext' => true, + 'context' => $editorcontext +); $category = file_prepare_standard_editor($category, 'description', $editoroptions, $editorcontext, 'coursecat', 'description', 0); $mform = new editcategory_form('editcategory.php', compact('category', 'editoroptions')); diff --git a/grade/edit/outcome/edit.php b/grade/edit/outcome/edit.php index d301cfe6c8c..1f4d9b7f7c6 100644 --- a/grade/edit/outcome/edit.php +++ b/grade/edit/outcome/edit.php @@ -103,7 +103,13 @@ if (!$courseid) { // default return url $gpr = new grade_plugin_return(); $returnurl = $gpr->get_return_url('index.php?id='.$courseid); -$editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true); +$editoroptions = array( + 'maxfiles' => EDITOR_UNLIMITED_FILES, + 'maxbytes' => $CFG->maxbytes, + 'trusttext' => false, + 'noclean' => true, + 'context' => $systemcontext +); if (!empty($outcome_rec->id)) { $outcome_rec = file_prepare_standard_editor($outcome_rec, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', $outcome_rec->id); diff --git a/grade/edit/scale/edit.php b/grade/edit/scale/edit.php index 5787ed10885..f2261396a31 100644 --- a/grade/edit/scale/edit.php +++ b/grade/edit/scale/edit.php @@ -88,7 +88,13 @@ if (!$courseid) { // default return url $gpr = new grade_plugin_return(); $returnurl = $gpr->get_return_url('index.php?id='.$courseid); -$editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true); +$editoroptions = array( + 'maxfiles' => EDITOR_UNLIMITED_FILES, + 'maxbytes' => $CFG->maxbytes, + 'trusttext' => false, + 'noclean' => true, + 'context' => $systemcontext +); if (!empty($scale_rec->id)) { $scale_rec = file_prepare_standard_editor($scale_rec, 'description', $editoroptions, $systemcontext, 'grade', 'scale', $scale_rec->id); diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index 86c583209f3..60917e1cac5 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -2372,6 +2372,7 @@ class mod_assignment_grading_form extends moodleform { $editoroptions['noclean'] = false; $editoroptions['maxfiles'] = 0; //TODO: no files for now, we need to first implement assignment_feedback area, integration with gradebook, files support in quickgrading, etc. (skodak) $editoroptions['maxbytes'] = $this->_customdata->maxbytes; + $editoroptions['context'] = $this->_customdata->context; return $editoroptions; } diff --git a/mod/assignment/type/online/assignment.class.php b/mod/assignment/type/online/assignment.class.php index 54532f71ca5..b705528be3c 100644 --- a/mod/assignment/type/online/assignment.class.php +++ b/mod/assignment/type/online/assignment.class.php @@ -38,7 +38,12 @@ class assignment_online extends assignment_base { if ($editmode) { // prepare form and process submitted data - $editoroptions = array('noclean'=>false, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$this->course->maxbytes); + $editoroptions = array( + 'noclean' => false, + 'maxfiles' => EDITOR_UNLIMITED_FILES, + 'maxbytes' => $this->course->maxbytes, + 'context' => $this->context + ); $data = new stdClass(); $data->id = $this->cm->id; diff --git a/mod/feedback/item/label/lib.php b/mod/feedback/item/label/lib.php index ce9b3999e1f..5e5df4c6478 100644 --- a/mod/feedback/item/label/lib.php +++ b/mod/feedback/item/label/lib.php @@ -49,6 +49,10 @@ class feedback_item_label extends feedback_item_base { //preparing the editor for new file-api $item->presentationformat = FORMAT_HTML; $item->presentationtrust = 1; + + // Append editor context to presentation options, giving preference to existing context. + $this->presentationoptions = array_merge(array('context' => $this->context), $this->presentationoptions); + $item = file_prepare_standard_editor($item, 'presentation', //name of the form element $this->presentationoptions, diff --git a/mod/lesson/editpage.php b/mod/lesson/editpage.php index e9bffb8a2cc..f725cae7ee2 100644 --- a/mod/lesson/editpage.php +++ b/mod/lesson/editpage.php @@ -84,6 +84,7 @@ if ($edit) { $data = $editpage->properties(); $data->pageid = $editpage->id; $data->id = $cm->id; + $editoroptions['context'] = $context; $data = file_prepare_standard_editor($data, 'contents', $editoroptions, $context, 'mod_lesson', 'page_contents', $editpage->id); $mform->set_data($data); $PAGE->navbar->add(get_string('edit'), new moodle_url('/mod/lesson/edit.php', array('id'=>$id))); diff --git a/mod/wiki/pagelib.php b/mod/wiki/pagelib.php index 6266308480a..a81f6854e53 100644 --- a/mod/wiki/pagelib.php +++ b/mod/wiki/pagelib.php @@ -535,6 +535,8 @@ class page_wiki_edit extends page_wiki { switch ($format) { case 'html': $data->newcontentformat = FORMAT_HTML; + // Append editor context to editor options, giving preference to existing context. + page_wiki_edit::$attachmentoptions = array_merge(array('context' => $this->modcontext), page_wiki_edit::$attachmentoptions); $data = file_prepare_standard_editor($data, 'newcontent', page_wiki_edit::$attachmentoptions, $this->modcontext, 'mod_wiki', 'attachments', $this->subwiki->id); break; default: diff --git a/mod/workshop/exsubmission.php b/mod/workshop/exsubmission.php index 70870e8df5d..486c27814ee 100644 --- a/mod/workshop/exsubmission.php +++ b/mod/workshop/exsubmission.php @@ -103,7 +103,14 @@ if ($edit and $canmanage) { $maxfiles = $workshop->nattachments; $maxbytes = $workshop->maxbytes; - $contentopts = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes); + $contentopts = array( + 'trusttext' => true, + 'subdirs' => false, + 'maxfiles' => $maxfiles, + 'maxbytes' => $maxbytes, + 'context' => $workshop->context + ); + $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes); $example = file_prepare_standard_editor($example, 'content', $contentopts, $workshop->context, 'mod_workshop', 'submission_content', $example->id); diff --git a/mod/workshop/form/accumulative/lib.php b/mod/workshop/form/accumulative/lib.php index ec04384e2d5..1c960e20f3c 100644 --- a/mod/workshop/form/accumulative/lib.php +++ b/mod/workshop/form/accumulative/lib.php @@ -116,6 +116,9 @@ class workshop_accumulative_strategy implements workshop_strategy { $norepeats += self::ADDDIMS; } + // Append editor context to editor options, giving preference to existing context. + $this->descriptionopts = array_merge(array('context' => $PAGE->context), $this->descriptionopts); + // prepare the embeded files for ($i = 0; $i < $nodimensions; $i++) { // prepare all editor elements diff --git a/mod/workshop/form/comments/lib.php b/mod/workshop/form/comments/lib.php index 8fa77351d09..156eef8555a 100644 --- a/mod/workshop/form/comments/lib.php +++ b/mod/workshop/form/comments/lib.php @@ -116,6 +116,9 @@ class workshop_comments_strategy implements workshop_strategy { $norepeats += self::ADDDIMS; } + // Append editor context to editor options, giving preference to existing context. + $this->descriptionopts = array_merge(array('context' => $PAGE->context), $this->descriptionopts); + // prepare the embedded files for ($i = 0; $i < $nodimensions; $i++) { // prepare all editor elements diff --git a/mod/workshop/form/numerrors/lib.php b/mod/workshop/form/numerrors/lib.php index 123160b3bf2..9fae313a55b 100644 --- a/mod/workshop/form/numerrors/lib.php +++ b/mod/workshop/form/numerrors/lib.php @@ -120,6 +120,9 @@ class workshop_numerrors_strategy implements workshop_strategy { $norepeats += self::ADDDIMS; } + // Append editor context to editor options, giving preference to existing context. + $this->descriptionopts = array_merge(array('context' => $PAGE->context), $this->descriptionopts); + // prepare the embeded files for ($i = 0; $i < $nodimensions; $i++) { // prepare all editor elements diff --git a/mod/workshop/form/rubric/lib.php b/mod/workshop/form/rubric/lib.php index 995a797132f..17b54c27d0f 100644 --- a/mod/workshop/form/rubric/lib.php +++ b/mod/workshop/form/rubric/lib.php @@ -126,6 +126,9 @@ class workshop_rubric_strategy implements workshop_strategy { $norepeats += self::ADDDIMS; } + // Append editor context to editor options, giving preference to existing context. + $this->descriptionopts = array_merge(array('context' => $this->workshop->context), $this->descriptionopts); + // prepare the embeded files for ($i = 0; $i < $nodimensions; $i++) { // prepare all editor elements diff --git a/mod/workshop/submission.php b/mod/workshop/submission.php index daf76d14143..9e2c2cd0cbd 100644 --- a/mod/workshop/submission.php +++ b/mod/workshop/submission.php @@ -124,7 +124,14 @@ if ($edit) { $maxfiles = $workshop->nattachments; $maxbytes = $workshop->maxbytes; - $contentopts = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes); + $contentopts = array( + 'trusttext' => true, + 'subdirs' => false, + 'maxfiles' => $maxfiles, + 'maxbytes' => $maxbytes, + 'context' => $workshop->context + ); + $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes); $submission = file_prepare_standard_editor($submission, 'content', $contentopts, $workshop->context, 'mod_workshop', 'submission_content', $submission->id); diff --git a/tag/edit.php b/tag/edit.php index 306d5576a59..66fd0fbc366 100644 --- a/tag/edit.php +++ b/tag/edit.php @@ -77,7 +77,12 @@ if (can_use_html_editor()) { $errorstring = ''; -$editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false); +$editoroptions = array( + 'maxfiles' => EDITOR_UNLIMITED_FILES, + 'maxbytes' => $CFG->maxbytes, + 'trusttext' => false, + 'context' => $systemcontext +); $tag = file_prepare_standard_editor($tag, 'description', $editoroptions, $systemcontext, 'tag', 'description', $tag->id); $tagform = new tag_edit_form(null, compact('editoroptions')); diff --git a/user/edit.php b/user/edit.php index 5e8283503db..2e4be7186cb 100644 --- a/user/edit.php +++ b/user/edit.php @@ -143,7 +143,14 @@ profile_load_data($user); // Prepare the editor and create form -$editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'forcehttps'=>false); +$editoroptions = array( + 'maxfiles' => EDITOR_UNLIMITED_FILES, + 'maxbytes' => $CFG->maxbytes, + 'trusttext' => false, + 'forcehttps' => false, + 'context' => $personalcontext +); + $user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0); $userform = new user_edit_form(null, array('editoroptions'=>$editoroptions)); if (empty($user->country)) { diff --git a/user/editadvanced.php b/user/editadvanced.php index b547dbb0fd7..0f7fc0e50a3 100644 --- a/user/editadvanced.php +++ b/user/editadvanced.php @@ -113,7 +113,14 @@ if (!empty($CFG->usetags)) { if ($user->id !== -1) { $usercontext = get_context_instance(CONTEXT_USER, $user->id); - $editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'forcehttps'=>false); + $editoroptions = array( + 'maxfiles' => EDITOR_UNLIMITED_FILES, + 'maxbytes' => $CFG->maxbytes, + 'trusttext' => false, + 'forcehttps' => false, + 'context' => $usercontext + ); + $user = file_prepare_standard_editor($user, 'description', $editoroptions, $usercontext, 'user', 'profile', 0); } else { $usercontext = null; From f9157eb798e37ca9b4f517e585ff4ffb8a876a10 Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Thu, 28 Jul 2011 10:31:26 +0800 Subject: [PATCH 2/4] MDL-27896 editor - added sanity check to pick wrong context passed to editor creation --- lib/filelib.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/filelib.php b/lib/filelib.php index 7d8830bcb7e..c7ce0832c89 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -111,6 +111,20 @@ function file_prepare_standard_editor($data, $field, array $options, $context=nu $options['noclean'] = false; } + //sanity check for passed context. This function doesn't expect $option['context'] to be set + //But this function is called before creating editor hence, this is one of the best places to check + //if context is used properly. This check notify developer that they missed passing context to editor. + if (isset($context) && !isset($options['context'])) { + //if $context is not null then make sure $option['context'] is also set. + debugging('Context for editor is not set in editoroptions. Hence editor will not respect editor filters', DEBUG_DEVELOPER); + } else if (isset($options['context']) && isset($context)) { + //If both are passed then they should be equal. + if ($options['context']->id != $context->id) { + $exceptionmsg = 'Editor context ['.$options['context']->id.'] is not equal to passed context ['.$context->id.']'; + throw new coding_exception($exceptionmsg); + } + } + if (is_null($itemid) or is_null($context)) { $contextid = null; $itemid = null; From dedb69a2a15bf1d9ac397995a4bd233044f3c17c Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Thu, 28 Jul 2011 11:27:07 +0800 Subject: [PATCH 3/4] MDL-27896 editor - parent context will be respected by editor in case context is not created. --- course/edit.php | 2 ++ course/editcategory.php | 2 +- user/editadvanced.php | 8 +++++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/course/edit.php b/course/edit.php index d184328dff5..053d8bb8f03 100644 --- a/course/edit.php +++ b/course/edit.php @@ -82,6 +82,8 @@ if (!empty($course)) { $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0); } else { + //editor should respect category context if course context is not set. + $editoroptions['context'] = $catcontext; $course = file_prepare_standard_editor($course, 'summary', $editoroptions, null, 'course', 'summary', null); } diff --git a/course/editcategory.php b/course/editcategory.php index f033b8d8102..52b9a63d8de 100644 --- a/course/editcategory.php +++ b/course/editcategory.php @@ -42,7 +42,7 @@ if ($id) { $category->parent = $parent; require_capability('moodle/category:manage', $context); $strtitle = get_string("addnewcategory"); - $editorcontext = null; + $editorcontext = $context; $title = "$SITE->shortname: ".get_string('addnewcategory'); $fullname = $SITE->fullname; } diff --git a/user/editadvanced.php b/user/editadvanced.php index 0f7fc0e50a3..e376086fd68 100644 --- a/user/editadvanced.php +++ b/user/editadvanced.php @@ -125,7 +125,13 @@ if ($user->id !== -1) { } else { $usercontext = null; // This is a new user, we don't want to add files here - $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false); + $editoroptions = array( + 'maxfiles'=>0, + 'maxbytes'=>0, + 'trusttext'=>false, + 'forcehttps'=>false, + 'context' => $coursecontext + ); } //create form From cec1d81461cf2e1ab7f31b90c7008217d81b77c4 Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Thu, 11 Aug 2011 14:42:05 +0800 Subject: [PATCH 4/4] MDL-27896 course - Rectified wrong use of itemid for preparing draft file area --- course/editcategory.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/course/editcategory.php b/course/editcategory.php index 52b9a63d8de..ceffc63bb89 100644 --- a/course/editcategory.php +++ b/course/editcategory.php @@ -13,6 +13,8 @@ require_once('editcategory_form.php'); require_login(); $id = optional_param('id', 0, PARAM_INT); +$itemid = 0; //initalise itemid, as all files in category description has item id 0 + if ($id) { if (!$category = $DB->get_record('course_categories', array('id' => $id))) { print_error('unknowcategory'); @@ -43,6 +45,7 @@ if ($id) { require_capability('moodle/category:manage', $context); $strtitle = get_string("addnewcategory"); $editorcontext = $context; + $itemid = null; //set this explicitly, so files for parent category should not get loaded in draft area. $title = "$SITE->shortname: ".get_string('addnewcategory'); $fullname = $SITE->fullname; } @@ -55,7 +58,7 @@ $editoroptions = array( 'trusttext' => true, 'context' => $editorcontext ); -$category = file_prepare_standard_editor($category, 'description', $editoroptions, $editorcontext, 'coursecat', 'description', 0); +$category = file_prepare_standard_editor($category, 'description', $editoroptions, $editorcontext, 'coursecat', 'description', $itemid); $mform = new editcategory_form('editcategory.php', compact('category', 'editoroptions')); $mform->set_data($category);