From 00ecac7295a45ccdbe357eff7c7fa86b507c38d7 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Thu, 23 Feb 2012 12:04:10 +0800 Subject: [PATCH] MDL-31738: added functions mod_xxx_get_file_info() to glossary and data modules --- mod/data/lib.php | 72 ++++++++++++++++++++++++++++++++++++++++++++ mod/glossary/lib.php | 59 ++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) diff --git a/mod/data/lib.php b/mod/data/lib.php index c8b52affdb2..9eef55730b5 100644 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -2863,6 +2863,78 @@ function data_get_file_areas($course, $cm, $context) { return $areas; } +/** + * File browsing support for data module. + * + * @param file_browser $browser + * @param array $areas + * @param stdClass $course + * @param cm_info $cm + * @param context $context + * @param string $filearea + * @param int $itemid + * @param string $filepath + * @param string $filename + * @return file_info_stored file_info_stored instance or null if not found + */ +function mod_data_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) { + global $CFG, $DB; + + if ($context->contextlevel != CONTEXT_MODULE) { + return null; + } + + if ($filearea === 'content') { + if (!$content = $DB->get_record('data_content', array('id'=>$itemid))) { + return null; + } + + if (!$field = $DB->get_record('data_fields', array('id'=>$content->fieldid))) { + return null; + } + + if (!$record = $DB->get_record('data_records', array('id'=>$content->recordid))) { + return null; + } + + if (!$data = $DB->get_record('data', array('id'=>$field->dataid))) { + return null; + } + + //check if approved + if ($data->approval and !$record->approved and !data_isowner($record) and !has_capability('mod/data:approve', $context)) { + return null; + } + + // group access + if ($record->groupid) { + $groupmode = groups_get_activity_groupmode($cm, $course); + if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) { + if (!groups_is_member($record->groupid)) { + return null; + } + } + } + + $fieldobj = data_get_field($field, $data, $cm); + + $filepath = is_null($filepath) ? '/' : $filepath; + $filename = is_null($filename) ? '.' : $filename; + if (!$fieldobj->file_ok($filepath.$filename)) { + return null; + } + + $fs = get_file_storage(); + if (!($storedfile = $fs->get_file($context->id, 'mod_data', $filearea, $itemid, $filepath, $filename))) { + return null; + } + $urlbase = $CFG->wwwroot.'/pluginfile.php'; + return new file_info_stored($browser, $context, $storedfile, $urlbase, $filearea, $itemid, true, true, false); + } + + return null; +} + /** * Serves the data attachments. Implements needed access control ;-) * diff --git a/mod/glossary/lib.php b/mod/glossary/lib.php index 4a24a76ac89..53bb1921d42 100644 --- a/mod/glossary/lib.php +++ b/mod/glossary/lib.php @@ -1460,6 +1460,65 @@ function glossary_get_file_areas($course, $cm, $context) { return $areas; } +/** + * File browsing support for glossary module. + * + * @param file_browser $browser + * @param array $areas + * @param stdClass $course + * @param cm_info $cm + * @param context $context + * @param string $filearea + * @param int $itemid + * @param string $filepath + * @param string $filename + * @return file_info_stored file_info_stored instance or null if not found + */ +function mod_glossary_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) { + global $CFG, $DB; + + if ($context->contextlevel != CONTEXT_MODULE) { + return null; + } + + if ($filearea === 'attachment' or $filearea === 'entry') { + if (!$entry = $DB->get_record('glossary_entries', array('id' => $itemid))) { + return null; + } + + if (!$glossary = $DB->get_record('glossary', array('id' => $cm->instance))) { + return null; + } + + if ($glossary->defaultapproval and !$entry->approved and !has_capability('mod/glossary:approve', $context)) { + return null; + } + + // this trickery here is because we need to support source glossary access + if ($entry->glossaryid == $cm->instance) { + $filecontext = $context; + } else if ($entry->sourceglossaryid == $cm->instance) { + if (!$maincm = get_coursemodule_from_instance('glossary', $entry->glossaryid)) { + return null; + } + $filecontext = get_context_instance(CONTEXT_MODULE, $maincm->id); + } else { + return null; + } + + $fs = get_file_storage(); + $filepath = is_null($filepath) ? '/' : $filepath; + $filename = is_null($filename) ? '.' : $filename; + if (!($storedfile = $fs->get_file($filecontext->id, 'mod_glossary', $filearea, $itemid, $filepath, $filename))) { + return null; + } + $urlbase = $CFG->wwwroot.'/pluginfile.php'; + return new file_info_stored($browser, $filecontext, $storedfile, $urlbase, $filearea, $itemid, true, true, false); + } + + return null; +} + /** * Serves the glossary attachments. Implements needed access control ;-) *