MDL-31738: added functions mod_xxx_get_file_info() to glossary and data modules

This commit is contained in:
Marina Glancy 2012-02-23 12:04:10 +08:00
parent c4a12afaf9
commit 00ecac7295
2 changed files with 131 additions and 0 deletions

View File

@ -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 ;-)
*

View File

@ -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 ;-)
*