Skip to content

Commit

Permalink
MDL-28954 cohorts: support files in cohort descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Jun 8, 2015
1 parent 17abbfb commit 6218473
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
31 changes: 26 additions & 5 deletions cohort/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions cohort/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ,
Expand Down
29 changes: 29 additions & 0 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 6218473

Please sign in to comment.