From b6b850b1d3ccf19406a5ac3069cfedc23e2d27f2 Mon Sep 17 00:00:00 2001
From: Adam Olley <adam.olley@netspot.com.au>
Date: Fri, 9 Mar 2012 11:08:26 +1030
Subject: [PATCH] MDL-30869: fix file picker for legacy course files

---
 course/editsection_form.php |    9 +++++++++
 lib/form/editor.php         |   10 ++++++++++
 lib/form/filemanager.php    |    8 +++++++-
 lib/form/filepicker.php     |    9 ++++++++-
 4 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/course/editsection_form.php b/course/editsection_form.php
index ab26049..a9db410 100644
--- a/course/editsection_form.php
+++ b/course/editsection_form.php
@@ -23,6 +23,15 @@ class editsection_form extends moodleform {
 
         /// Prepare course and the editor
 
+        // MDL-30869
+        $context = new stdClass();
+        $context->id = $this->_customdata['editoroptions']['context']->id;
+        $context->contextlevel = $this->_customdata['editoroptions']['context']->contextlevel;
+        $context->protected = $this->_customdata['editoroptions']['context']->instanceid;
+        $context->path = $this->_customdata['editoroptions']['context']->path;
+        $context->depth = $this->_customdata['editoroptions']['context']->depth;
+        $this->_customdata['editoroptions']['context'] = $context;
+
         $mform->addElement('editor', 'summary_editor', get_string('summary'), null, $this->_customdata['editoroptions']);
         $mform->addHelpButton('summary_editor', 'summary');
         $mform->setType('summary_editor', PARAM_RAW);
diff --git a/lib/form/editor.php b/lib/form/editor.php
index 0f592a9..36cf460 100644
--- a/lib/form/editor.php
+++ b/lib/form/editor.php
@@ -34,6 +34,16 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element {
         if (!$this->_options['context']) {
             $this->_options['context'] = get_context_instance(CONTEXT_SYSTEM);
         }
+
+        // MDL-30869
+        $context = new stdClass();
+        $context->id = $this->_options['context']->id;
+        $context->contextlevel = $this->_options['context']->contextlevel;
+        $context->protected = $this->_options['context']->instanceid;
+        $context->path = $this->_options['context']->path;
+        $context->depth = $this->_options['context']->depth;
+        $this->_options['context'] = $context;
+
         $this->_options['trusted'] = trusttext_trusted($this->_options['context']);
         parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
 
diff --git a/lib/form/filemanager.php b/lib/form/filemanager.php
index 3670fef..11da8df 100644
--- a/lib/form/filemanager.php
+++ b/lib/form/filemanager.php
@@ -146,7 +146,13 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
         $options->target    = $id;
         $options->accepted_types = $accepted_types;
         $options->return_types = FILE_INTERNAL;
-        $options->context = $PAGE->context;
+        // MDL-30869
+        $options->context = new stdClass();
+        $options->context->id = $PAGE->context->id;
+        $options->context->contextlevel = $PAGE->context->contextlevel;
+        $options->context->instanceid = $PAGE->context->instanceid;
+        $options->context->path = $PAGE->context->path;
+        $options->context->depth = $PAGE->context->depth;
 
         $html = $this->_getTabs();
         $html .= form_filemanager_render($options);
diff --git a/lib/form/filepicker.php b/lib/form/filepicker.php
index c37da6d..0b83c9e 100644
--- a/lib/form/filepicker.php
+++ b/lib/form/filepicker.php
@@ -84,7 +84,14 @@ class MoodleQuickForm_filepicker extends HTML_QuickForm_input {
         $html = $this->_getTabs();
         $fp = new file_picker($args);
         $options = $fp->options;
-        $options->context = $PAGE->context;
+        //MDL-30869
+        $options->context = new stdClass();
+        $options->context->id = $PAGE->context->id;
+        $options->context->contextlevel = $PAGE->context->contextlevel;
+        $options->context->instanceid = $PAGE->context->instanceid;
+        $options->context->path = $PAGE->context->path;
+        $options->context->depth = $PAGE->context->depth;
+
         $html .= $OUTPUT->render($fp);
         $html .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" value="'.$draftitemid.'" class="filepickerhidden"/>';
 
-- 
1.7.7.6

