Permalink
Browse files

MDL-42212 mod_lesson: Fix callbacks retrieving activity files

  • Loading branch information...
1 parent c9a3d37 commit 4a59b3b5550821374d138cd5d41e027632b54b83 @FMCorz FMCorz committed Dec 13, 2013
Showing with 198 additions and 5 deletions.
  1. +29 −5 mod/lesson/lib.php
  2. +169 −0 mod/lesson/locallib.php
View
@@ -919,20 +919,44 @@ function lesson_get_file_areas() {
* @return file_info_stored
*/
function lesson_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
- global $CFG;
- if (has_capability('moodle/course:managefiles', $context)) {
- // no peaking here for students!!
+ global $CFG, $DB;
+
+ if (!has_capability('moodle/course:managefiles', $context)) {
+ // No peaking here for students!
return null;
}
+ // Mediafile area does not have sub directories, so let's select the default itemid to prevent
+ // the user from selecting a directory to access the mediafile content.
+ if ($filearea == 'mediafile' && is_null($itemid)) {
+ $itemid = 0;
+ }
+
+ if (is_null($itemid)) {
+ require_once(__DIR__ . '/locallib.php');
+ return new mod_lesson_file_info($browser, $course, $cm, $context, $areas, $filearea);
+ }
+
$fs = get_file_storage();
$filepath = is_null($filepath) ? '/' : $filepath;
$filename = is_null($filename) ? '.' : $filename;
- $urlbase = $CFG->wwwroot.'/pluginfile.php';
if (!$storedfile = $fs->get_file($context->id, 'mod_lesson', $filearea, $itemid, $filepath, $filename)) {
return null;
}
- return new file_info_stored($browser, $context, $storedfile, $urlbase, $filearea, $itemid, true, true, false);
+
+ $itemname = $filearea;
+ if ($filearea == 'page_contents') {
+ $itemname = $DB->get_field('lesson_pages', 'title', array('lessonid' => $cm->instance, 'id' => $itemid));
+ $itemname = format_string($itemname, true, array('context' => $context));
+ } else {
+ $areas = lesson_get_file_areas();
+ if (isset($areas[$filearea])) {
+ $itemname = $areas[$filearea];
+ }
+ }
+
+ $urlbase = $CFG->wwwroot . '/pluginfile.php';
+ return new file_info_stored($browser, $context, $storedfile, $urlbase, $itemname, $itemid, true, true, false);
}
View
@@ -2815,3 +2815,172 @@ public function get_add_page_type_links($previd) {
return $links;
}
}
+
+/**
+ * File browsing support class.
+ *
+ * @package mod_lesson
+ * @copyright 2013 Frédéric Massart
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class mod_lesson_file_info extends file_info {
+
+ /** @var stdClass Course object */
+ protected $course;
+ /** @var stdClass Course module object */
+ protected $cm;
+ /** @var array Available file areas */
+ protected $areas;
+ /** @var string File area to browse */
+ protected $filearea;
+
+ /**
+ * Constructor
+ *
+ * @param file_browser $browser file_browser instance
+ * @param stdClass $course course object
+ * @param stdClass $cm course module object
+ * @param stdClass $context module context
+ * @param array $areas available file areas
+ * @param string $filearea file area to browse
+ */
+ public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
+ parent::__construct($browser, $context);
+ $this->course = $course;
+ $this->cm = $cm;
+ $this->areas = $areas;
+ $this->filearea = $filearea;
+ }
+
+ /**
+ * Returns list of standard virtual file/directory identification.
+ * The difference from stored_file parameters is that null values
+ * are allowed in all fields
+ * @return array with keys contextid, filearea, itemid, filepath and filename
+ */
+ public function get_params() {
+ return array('contextid' => $this->context->id,
+ 'component' => 'mod_lesson',
+ 'filearea' => $this->filearea,
+ 'itemid' => null,
+ 'filepath' => null,
+ 'filename' => null);
+ }
+
+ /**
+ * Returns localised visible name.
+ * @return string
+ */
+ public function get_visible_name() {
+ return $this->areas[$this->filearea];
+ }
+
+ /**
+ * Can I add new files or directories?
+ * @return bool
+ */
+ public function is_writable() {
+ return false;
+ }
+
+ /**
+ * Is directory?
+ * @return bool
+ */
+ public function is_directory() {
+ return true;
+ }
+
+ /**
+ * Returns list of children.
+ * @return array of file_info instances
+ */
+ public function get_children() {
+ return $this->get_filtered_children('*', false, true);
+ }
+
+ /**
+ * Help function to return files matching extensions or their count
+ *
+ * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
+ * @param bool|int $countonly if false returns the children, if an int returns just the
+ * count of children but stops counting when $countonly number of children is reached
+ * @param bool $returnemptyfolders if true returns items that don't have matching files inside
+ * @return array|int array of file_info instances or the count
+ */
+ private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
+ global $DB;
+
+ $params = array(
+ 'contextid' => $this->context->id,
+ 'component' => 'mod_lesson',
+ 'filearea' => $this->filearea
+ );
+ $sql = 'SELECT DISTINCT itemid
+ FROM {files}
+ WHERE contextid = :contextid
+ AND component = :component
+ AND filearea = :filearea';
+
+ if (!$returnemptyfolders) {
+ $sql .= ' AND filename <> :emptyfilename';
+ $params['emptyfilename'] = '.';
+ }
+
+ list($sql2, $params2) = $this->build_search_files_sql($extensions);
+ $sql .= ' ' . $sql2;
+ $params = array_merge($params, $params2);
+
+ if ($countonly !== false) {
+ $sql .= ' ORDER BY itemid DESC';
+ }
+
+ $rs = $DB->get_recordset_sql($sql, $params);
+ $children = array();
+ foreach ($rs as $record) {
+ if (($child = $this->browser->get_file_info($this->context, 'mod_lesson', $this->filearea, $record->itemid))
+ && ($returnemptyfolders || $child->count_non_empty_children($extensions))) {
+ $children[] = $child;
+ }
+ if ($countonly !== false && count($children) >= $countonly) {
+ break;
+ }
+ }
+ $rs->close();
+ if ($countonly !== false) {
+ return count($children);
+ }
+ return $children;
+ }
+
+ /**
+ * Returns list of children which are either files matching the specified extensions
+ * or folders that contain at least one such file.
+ *
+ * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
+ * @return array of file_info instances
+ */
+ public function get_non_empty_children($extensions = '*') {
+ return $this->get_filtered_children($extensions, false);
+ }
+
+ /**
+ * Returns the number of children which are either files matching the specified extensions
+ * or folders containing at least one such file.
+ *
+ * @param string|array $extensions, for example '*' or array('.gif','.jpg')
+ * @param int $limit stop counting after at least $limit non-empty children are found
+ * @return int
+ */
+ public function count_non_empty_children($extensions = '*', $limit = 1) {
+ return $this->get_filtered_children($extensions, $limit);
+ }
+
+ /**
+ * Returns parent file_info instance
+ * @return file_info or null for root
+ */
+ public function get_parent() {
+ return $this->browser->get_file_info($this->context);
+ }
+}

0 comments on commit 4a59b3b

Please sign in to comment.