From 621847333427f422d6df1a50901323d3a6aeaa5f Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Mon, 11 May 2015 16:42:46 +0800 Subject: [PATCH] MDL-28954 cohorts: support files in cohort descriptions --- cohort/edit.php | 31 ++++++++++++++++++++++++++----- cohort/index.php | 2 ++ lib/filelib.php | 29 +++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/cohort/edit.php b/cohort/edit.php index b8c73ba8bb64c..8cae07a29582e 100644 --- a/cohort/edit.php +++ b/cohort/edit.php @@ -116,15 +116,18 @@ redirect($returnurl); } -$editoroptions = array('maxfiles'=>0, 'context'=>$context); +$editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, + 'maxbytes' => $SITE->maxbytes, 'context' => $context); if ($cohort->id) { // Edit existing. - $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context); + $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, + $context, 'cohort', 'description', $cohort->id); $strheading = get_string('editcohort', 'cohort'); } else { // Add new. - $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, $context); + $cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions, + $context, 'cohort', 'description', null); $strheading = get_string('addcohort', 'cohort'); } @@ -138,12 +141,30 @@ redirect($returnurl); } else if ($data = $editform->get_data()) { - $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context); + $oldcontextid = $context->id; + $editoroptions['context'] = $context = context::instance_by_id($data->contextid); if ($data->id) { + if ($data->contextid != $oldcontextid) { + // Cohort was moved to another context. + get_file_storage()->move_area_files_to_new_context($oldcontextid, $context->id, + 'cohort', 'description', $data->id); + } + $data = file_postupdate_standard_editor($data, 'description', $editoroptions, + $context, 'cohort', 'description', $data->id); cohort_update_cohort($data); } else { - cohort_add_cohort($data); + $data->descriptionformat = $data->description_editor['format']; + $data->description = $description = $data->description_editor['text']; + $data->id = cohort_add_cohort($data); + $editoroptions['context'] = $context = context::instance_by_id($data->contextid); + $data = file_postupdate_standard_editor($data, 'description', $editoroptions, + $context, 'cohort', 'description', $data->id); + if ($description != $data->description) { + $updatedata = (object)array('id' => $data->id, + 'description' => $data->description, 'contextid' => $context->id); + cohort_update_cohort($updatedata); + } } if ($returnurl->get_param('showall') || $returnurl->get_param('contextid') == $data->contextid) { diff --git a/cohort/index.php b/cohort/index.php index f1cb480639d54..923e464f2f6ac 100644 --- a/cohort/index.php +++ b/cohort/index.php @@ -122,6 +122,8 @@ foreach($cohorts['cohorts'] as $cohort) { $line = array(); $cohortcontext = context::instance_by_id($cohort->contextid); + $cohort->description = file_rewrite_pluginfile_urls($cohort->description, 'pluginfile.php', $cohortcontext->id, + 'cohort', 'description', $cohort->id); if ($showall) { if ($cohortcontext->contextlevel == CONTEXT_COURSECAT) { $line[] = html_writer::link(new moodle_url('/cohort/index.php' , diff --git a/lib/filelib.php b/lib/filelib.php index b893cf22f9861..185bcd6119d5c 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -4199,6 +4199,35 @@ function file_pluginfile($relativepath, $forcedownload, $preview = null) { send_file_not_found(); } + } else if ($component === 'cohort') { + + $cohortid = (int)array_shift($args); + $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST); + $cohortcontext = context::instance_by_id($cohort->contextid); + + // The context in the file URL must be either cohort context or context of the course underneath the cohort's context. + if ($context->id != $cohort->contextid && + ($context->contextlevel != CONTEXT_COURSE || !in_array($cohort->contextid, $context->get_parent_context_ids()))) { + send_file_not_found(); + } + + // User is able to access cohort if they have view cap on cohort level or + // the cohort is visible and they have view cap on course level. + $canview = has_capability('moodle/cohort:view', $cohortcontext) || + ($cohort->visible && has_capability('moodle/cohort:view', $context)); + + if ($filearea === 'description' && $canview) { + $filename = array_pop($args); + $filepath = $args ? '/'.implode('/', $args).'/' : '/'; + if (($file = $fs->get_file($cohortcontext->id, 'cohort', 'description', $cohort->id, $filepath, $filename)) + && !$file->is_directory()) { + \core\session\manager::write_close(); // Unlock session during file serving. + send_stored_file($file, 60 * 60, 0, $forcedownload, array('preview' => $preview)); + } + } + + send_file_not_found(); + } else if ($component === 'group') { if ($context->contextlevel != CONTEXT_COURSE) { send_file_not_found();