diff --git a/mod/glossary/lang/en/glossary.php b/mod/glossary/lang/en/glossary.php index 597866436c9..2b7445d5016 100644 --- a/mod/glossary/lang/en/glossary.php +++ b/mod/glossary/lang/en/glossary.php @@ -44,6 +44,8 @@ $string['answer'] = 'Answer'; $string['approve'] = 'Approve'; $string['approvaldisplayformat'] = 'Approval display format'; $string['approvaldisplayformat_help'] = 'When approving glossary items you may wish to use a different display format'; +$string['areaattachment'] = 'Attachments'; +$string['areaentry'] = 'Definitions'; $string['areyousuredelete'] = 'Are you sure you want to delete this entry?'; $string['areyousuredeletecomment'] = 'Are you sure you want to delete this comment?'; $string['areyousureexport'] = 'Are you sure you want to export this entry to'; diff --git a/mod/glossary/lib.php b/mod/glossary/lib.php index 3f19d2081cd..e042f263534 100644 --- a/mod/glossary/lib.php +++ b/mod/glossary/lib.php @@ -1453,6 +1453,10 @@ function glossary_print_attachments($entry, $cm, $type=NULL, $align="left") { } } +//////////////////////////////////////////////////////////////////////////////// +// File API // +//////////////////////////////////////////////////////////////////////////////// + /** * Lists all browsable file areas * @@ -1464,8 +1468,10 @@ function glossary_print_attachments($entry, $cm, $type=NULL, $align="left") { * @return array */ function glossary_get_file_areas($course, $cm, $context) { - $areas = array(); - return $areas; + return array( + 'attachment' => get_string('areaattachment', 'mod_glossary'), + 'entry' => get_string('areaentry', 'mod_glossary'), + ); } /** @@ -1489,42 +1495,52 @@ function mod_glossary_get_file_info($browser, $areas, $course, $cm, $context, $f 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); + if (!isset($areas[$filearea])) { + return null; } - return null; + if (!has_capability('moodle/course:managefiles', $context)) { + return null; + } + + if (is_null($itemid)) { + require_once($CFG->dirroot.'/mod/glossary/locallib.php'); + return new glossary_file_info_container($browser, $course, $cm, $context, $areas, $filearea); + } + + 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, s($entry->concept), true, true, false, false); } /** diff --git a/mod/glossary/locallib.php b/mod/glossary/locallib.php index 5dbe303810d..9e247065a44 100644 --- a/mod/glossary/locallib.php +++ b/mod/glossary/locallib.php @@ -447,3 +447,128 @@ class glossary_entry_portfolio_caller extends portfolio_module_caller_base { } } + +/** + * Class representing the virtual node with all itemids in the file browser + * + * @category files + * @copyright 2012 David Mudrak + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class glossary_file_info_container extends file_info { + /** @var file_browser */ + protected $browser; + /** @var stdClass */ + protected $course; + /** @var stdClass */ + protected $cm; + /** @var string */ + protected $component; + /** @var stdClass */ + protected $context; + /** @var array */ + protected $areas; + /** @var string */ + protected $filearea; + + /** + * Constructor (in case you did not realize it ;-) + * + * @param file_browser $browser + * @param stdClass $course + * @param stdClass $cm + * @param stdClass $context + * @param array $areas + * @param string $filearea + */ + public function __construct($browser, $course, $cm, $context, $areas, $filearea) { + parent::__construct($browser, $context); + $this->browser = $browser; + $this->course = $course; + $this->cm = $cm; + $this->component = 'mod_glossary'; + $this->context = $context; + $this->areas = $areas; + $this->filearea = $filearea; + } + + /** + * @return array with keys contextid, filearea, itemid, filepath and filename + */ + public function get_params() { + return array( + 'contextid' => $this->context->id, + 'component' => $this->component, + 'filearea' => $this->filearea, + 'itemid' => null, + 'filepath' => null, + 'filename' => null, + ); + } + + /** + * Can new files or directories be added via the file browser + * + * @return bool + */ + public function is_writable() { + return false; + } + + /** + * Should this node be considered as a folder in the file browser + * + * @return bool + */ + public function is_directory() { + return true; + } + + /** + * Returns localised visible name of this node + * + * @return string + */ + public function get_visible_name() { + return $this->areas[$this->filearea]; + } + + /** + * Returns list of children nodes + * + * @return array of file_info instances + */ + public function get_children() { + global $DB; + + $sql = "SELECT DISTINCT f.itemid, ge.concept + FROM {files} f + JOIN {modules} m ON (m.name = 'glossary' AND m.visible = 1) + JOIN {course_modules} cm ON (cm.module = m.id AND cm.id = ?) + JOIN {glossary} g ON g.id = cm.instance + JOIN {glossary_entries} ge ON (ge.glossaryid = g.id AND ge.id = f.itemid) + WHERE f.contextid = ? AND f.component = ? AND f.filearea = ? + ORDER BY ge.concept, f.itemid"; + $params = array($this->context->instanceid, $this->context->id, $this->component, $this->filearea); + + $rs = $DB->get_recordset_sql($sql, $params); + $children = array(); + foreach ($rs as $file) { + if ($child = $this->browser->get_file_info($this->context, 'mod_glossary', $this->filearea, $file->itemid)) { + $children[] = $child; + } + } + $rs->close(); + + return $children; + } + + /** + * Returns parent file_info instance + * + * @return file_info or null for root + */ + public function get_parent() { + return $this->browser->get_file_info($this->context); + } +}