Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * File browsing support.
  19   *
  20   * @package    mod_lesson
  21   * @copyright  2013 Frédéric Massart
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * File browsing support class.
  29   *
  30   * @package    mod_lesson
  31   * @copyright  2013 Frédéric Massart
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class mod_lesson_file_info extends file_info {
  35  
  36      /** @var stdClass Course object */
  37      protected $course;
  38      /** @var stdClass Course module object */
  39      protected $cm;
  40      /** @var array Available file areas */
  41      protected $areas;
  42      /** @var string File area to browse */
  43      protected $filearea;
  44  
  45      /**
  46       * Constructor
  47       *
  48       * @param file_browser $browser file_browser instance
  49       * @param stdClass $course course object
  50       * @param stdClass $cm course module object
  51       * @param stdClass $context module context
  52       * @param array $areas available file areas
  53       * @param string $filearea file area to browse
  54       */
  55      public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
  56          parent::__construct($browser, $context);
  57          $this->course   = $course;
  58          $this->cm       = $cm;
  59          $this->areas    = $areas;
  60          $this->filearea = $filearea;
  61      }
  62  
  63      /**
  64       * Returns list of standard virtual file/directory identification.
  65       * The difference from stored_file parameters is that null values
  66       * are allowed in all fields
  67       * @return array with keys contextid, filearea, itemid, filepath and filename
  68       */
  69      public function get_params() {
  70          return array('contextid' => $this->context->id,
  71                       'component' => 'mod_lesson',
  72                       'filearea'  => $this->filearea,
  73                       'itemid'    => null,
  74                       'filepath'  => null,
  75                       'filename'  => null);
  76      }
  77  
  78      /**
  79       * Returns localised visible name.
  80       * @return string
  81       */
  82      public function get_visible_name() {
  83          return $this->areas[$this->filearea];
  84      }
  85  
  86      /**
  87       * Can I add new files or directories?
  88       * @return bool
  89       */
  90      public function is_writable() {
  91          return false;
  92      }
  93  
  94      /**
  95       * Is directory?
  96       * @return bool
  97       */
  98      public function is_directory() {
  99          return true;
 100      }
 101  
 102      /**
 103       * Returns list of children.
 104       * @return array of file_info instances
 105       */
 106      public function get_children() {
 107          return $this->get_filtered_children('*', false, true);
 108      }
 109  
 110      /**
 111       * Help function to return files matching extensions or their count
 112       *
 113       * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
 114       * @param bool|int $countonly if false returns the children, if an int returns just the
 115       *    count of children but stops counting when $countonly number of children is reached
 116       * @param bool $returnemptyfolders if true returns items that don't have matching files inside
 117       * @return array|int array of file_info instances or the count
 118       */
 119      private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
 120          global $DB;
 121  
 122          $params = array(
 123              'contextid' => $this->context->id,
 124              'component' => 'mod_lesson',
 125              'filearea' => $this->filearea
 126          );
 127          $sql = 'SELECT DISTINCT itemid
 128                    FROM {files}
 129                   WHERE contextid = :contextid
 130                     AND component = :component
 131                     AND filearea = :filearea';
 132  
 133          if (!$returnemptyfolders) {
 134              $sql .= ' AND filename <> :emptyfilename';
 135              $params['emptyfilename'] = '.';
 136          }
 137  
 138          list($sql2, $params2) = $this->build_search_files_sql($extensions);
 139          $sql .= ' ' . $sql2;
 140          $params = array_merge($params, $params2);
 141  
 142          if ($countonly !== false) {
 143              $sql .= ' ORDER BY itemid DESC';
 144          }
 145  
 146          $rs = $DB->get_recordset_sql($sql, $params);
 147          $children = array();
 148          foreach ($rs as $record) {
 149              if (($child = $this->browser->get_file_info($this->context, 'mod_lesson', $this->filearea, $record->itemid))
 150                      && ($returnemptyfolders || $child->count_non_empty_children($extensions))) {
 151                  $children[] = $child;
 152              }
 153              if ($countonly !== false && count($children) >= $countonly) {
 154                  break;
 155              }
 156          }
 157          $rs->close();
 158          if ($countonly !== false) {
 159              return count($children);
 160          }
 161          return $children;
 162      }
 163  
 164      /**
 165       * Returns list of children which are either files matching the specified extensions
 166       * or folders that contain at least one such file.
 167       *
 168       * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
 169       * @return array of file_info instances
 170       */
 171      public function get_non_empty_children($extensions = '*') {
 172          return $this->get_filtered_children($extensions, false);
 173      }
 174  
 175      /**
 176       * Returns the number of children which are either files matching the specified extensions
 177       * or folders containing at least one such file.
 178       *
 179       * @param string|array $extensions, for example '*' or array('.gif','.jpg')
 180       * @param int $limit stop counting after at least $limit non-empty children are found
 181       * @return int
 182       */
 183      public function count_non_empty_children($extensions = '*', $limit = 1) {
 184          return $this->get_filtered_children($extensions, $limit);
 185      }
 186  
 187      /**
 188       * Returns parent file_info instance
 189       * @return file_info or null for root
 190       */
 191      public function get_parent() {
 192          return $this->browser->get_file_info($this->context);
 193      }
 194  }