[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/filebrowser/ -> file_info_context_course.php (source)

   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  /**
  19   * Utility class for browsing of course files.
  20   *
  21   * @package    core_files
  22   * @copyright  2008 Petr Skoda (http://skodak.org)
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Represents a course context in the tree navigated by {@link file_browser}.
  30   *
  31   * @package    core_files
  32   * @copyright  2008 Petr Skoda (http://skodak.org)
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class file_info_context_course extends file_info {
  36      /** @var stdClass course object */
  37      protected $course;
  38  
  39      /**
  40       * Constructor
  41       *
  42       * @param file_browser $browser file browser instance
  43       * @param stdClass $context context object
  44       * @param stdClass $course course object
  45       */
  46      public function __construct($browser, $context, $course) {
  47          parent::__construct($browser, $context);
  48          $this->course   = $course;
  49      }
  50  
  51      /**
  52       * Return information about this specific context level
  53       *
  54       * @param string $component component
  55       * @param string $filearea file area
  56       * @param int $itemid item ID
  57       * @param string $filepath file path
  58       * @param string $filename file name
  59       * @return file_info|null file_info instance or null if not found or access not allowed
  60       */
  61      public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
  62          // try to emulate require_login() tests here
  63          if (!isloggedin()) {
  64              return null;
  65          }
  66  
  67          if (!$this->course->visible and !has_capability('moodle/course:viewhiddencourses', $this->context)) {
  68              return null;
  69          }
  70  
  71          if (!is_viewing($this->context) and !is_enrolled($this->context)) {
  72              // no peaking here if not enrolled or inspector
  73              return null;
  74          }
  75  
  76          if (empty($component)) {
  77              return $this;
  78          }
  79  
  80          $methodname = "get_area_{$component}_{$filearea}";
  81  
  82          if (method_exists($this, $methodname)) {
  83              return $this->$methodname($itemid, $filepath, $filename);
  84          }
  85  
  86          return null;
  87      }
  88  
  89      /**
  90       * Gets a stored file for the course summary filearea directory
  91       *
  92       * @param int $itemid item ID
  93       * @param string $filepath file path
  94       * @param string $filename file name
  95       * @return file_info|null file_info instance or null if not found or access not allowed
  96       */
  97      protected function get_area_course_summary($itemid, $filepath, $filename) {
  98          global $CFG;
  99  
 100          if (!has_capability('moodle/course:update', $this->context)) {
 101              return null;
 102          }
 103          if (is_null($itemid)) {
 104              return $this;
 105          }
 106  
 107          $fs = get_file_storage();
 108  
 109          $filepath = is_null($filepath) ? '/' : $filepath;
 110          $filename = is_null($filename) ? '.' : $filename;
 111          if (!$storedfile = $fs->get_file($this->context->id, 'course', 'summary', 0, $filepath, $filename)) {
 112              if ($filepath === '/' and $filename === '.') {
 113                  $storedfile = new virtual_root_file($this->context->id, 'course', 'summary', 0);
 114              } else {
 115                  // not found
 116                  return null;
 117              }
 118          }
 119          $urlbase = $CFG->wwwroot.'/pluginfile.php';
 120          return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false);
 121      }
 122  
 123      /**
 124       * Gets a stored file for the course images filearea directory
 125       *
 126       * @param int $itemid item ID
 127       * @param string $filepath file path
 128       * @param string $filename file name
 129       * @return file_info|null file_info instance or null if not found or access not allowed
 130       */
 131      protected function get_area_course_overviewfiles($itemid, $filepath, $filename) {
 132          global $CFG;
 133  
 134          if (!has_capability('moodle/course:update', $this->context)) {
 135              return null;
 136          }
 137          if (is_null($itemid)) {
 138              return $this;
 139          }
 140  
 141          $fs = get_file_storage();
 142  
 143          $filepath = is_null($filepath) ? '/' : $filepath;
 144          $filename = is_null($filename) ? '.' : $filename;
 145          if (!$storedfile = $fs->get_file($this->context->id, 'course', 'overviewfiles', 0, $filepath, $filename)) {
 146              if ($filepath === '/' and $filename === '.') {
 147                  $storedfile = new virtual_root_file($this->context->id, 'course', 'overviewfiles', 0);
 148              } else {
 149                  // not found
 150                  return null;
 151              }
 152          }
 153          $urlbase = $CFG->wwwroot.'/pluginfile.php';
 154          return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areacourseoverviewfiles', 'repository'), false, true, true, false);
 155      }
 156  
 157      /**
 158       * Gets a stored file for the course section filearea directory
 159       *
 160       * @param int $itemid item ID
 161       * @param string $filepath file path
 162       * @param string $filename file name
 163       * @return file_info|null file_info instance or null if not found or access not allowed
 164       */
 165      protected function get_area_course_section($itemid, $filepath, $filename) {
 166          global $CFG, $DB;
 167  
 168          if (!has_capability('moodle/course:update', $this->context)) {
 169              return null;
 170          }
 171  
 172          if (empty($itemid)) {
 173              // list all sections
 174              return new file_info_area_course_section($this->browser, $this->context, $this->course, $this);
 175          }
 176  
 177          if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
 178              return null; // does not exist
 179          }
 180  
 181          $fs = get_file_storage();
 182  
 183          $filepath = is_null($filepath) ? '/' : $filepath;
 184          $filename = is_null($filename) ? '.' : $filename;
 185          if (!$storedfile = $fs->get_file($this->context->id, 'course', 'section', $itemid, $filepath, $filename)) {
 186              if ($filepath === '/' and $filename === '.') {
 187                  $storedfile = new virtual_root_file($this->context->id, 'course', 'section', $itemid);
 188              } else {
 189                  // not found
 190                  return null;
 191              }
 192          }
 193          $urlbase = $CFG->wwwroot.'/pluginfile.php';
 194          return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->section, true, true, true, false);
 195      }
 196  
 197      /**
 198       * Gets a stored file for the course legacy filearea directory
 199       *
 200       * @param int $itemid item ID
 201       * @param string $filepath file path
 202       * @param string $filename file name
 203       * @return file_info|null file_info instance or null if not found or access not allowed
 204       */
 205      protected function get_area_course_legacy($itemid, $filepath, $filename) {
 206          if (!has_capability('moodle/course:managefiles', $this->context)) {
 207              return null;
 208          }
 209  
 210          if ($this->course->id != SITEID and $this->course->legacyfiles != 2) {
 211              // bad luck, legacy course files not used any more
 212          }
 213  
 214          if (is_null($itemid)) {
 215              return $this;
 216          }
 217  
 218          $fs = get_file_storage();
 219  
 220          $filepath = is_null($filepath) ? '/' : $filepath;
 221          $filename = is_null($filename) ? '.' : $filename;
 222          if (!$storedfile = $fs->get_file($this->context->id, 'course', 'legacy', 0, $filepath, $filename)) {
 223              if ($filepath === '/' and $filename === '.') {
 224                  $storedfile = new virtual_root_file($this->context->id, 'course', 'legacy', 0);
 225              } else {
 226                  // not found
 227                  return null;
 228              }
 229          }
 230  
 231          return new file_info_area_course_legacy($this->browser, $this->context, $storedfile);
 232      }
 233  
 234      /**
 235       * Gets a stored file for the backup course filearea directory
 236       *
 237       * @param int $itemid item ID
 238       * @param string $filepath file path
 239       * @param string $filename file name
 240       * @return file_info|null file_info instance or null if not found or access not allowed
 241       */
 242      protected function get_area_backup_course($itemid, $filepath, $filename) {
 243          global $CFG;
 244  
 245          if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
 246              return null;
 247          }
 248          if (is_null($itemid)) {
 249              return $this;
 250          }
 251  
 252          $fs = get_file_storage();
 253  
 254          $filepath = is_null($filepath) ? '/' : $filepath;
 255          $filename = is_null($filename) ? '.' : $filename;
 256          if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'course', 0, $filepath, $filename)) {
 257              if ($filepath === '/' and $filename === '.') {
 258                  $storedfile = new virtual_root_file($this->context->id, 'backup', 'course', 0);
 259              } else {
 260                  // not found
 261                  return null;
 262              }
 263          }
 264  
 265          $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
 266          $uploadable   = has_capability('moodle/restore:uploadfile', $this->context);
 267  
 268          $urlbase = $CFG->wwwroot.'/pluginfile.php';
 269          return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('coursebackup', 'repository'), false, $downloadable, $uploadable, false);
 270      }
 271  
 272      /**
 273       * Gets a stored file for the automated backup filearea directory
 274       *
 275       * @param int $itemid item ID
 276       * @param string $filepath file path
 277       * @param string $filename file name
 278       * @return file_info|null
 279       */
 280      protected function get_area_backup_automated($itemid, $filepath, $filename) {
 281          global $CFG;
 282  
 283          if (!has_capability('moodle/restore:viewautomatedfilearea', $this->context)) {
 284              return null;
 285          }
 286          if (is_null($itemid)) {
 287              return $this;
 288          }
 289  
 290          $fs = get_file_storage();
 291  
 292          $filepath = is_null($filepath) ? '/' : $filepath;
 293          $filename = is_null($filename) ? '.' : $filename;
 294          if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'automated', 0, $filepath, $filename)) {
 295              if ($filepath === '/' and $filename === '.') {
 296                  $storedfile = new virtual_root_file($this->context->id, 'backup', 'automated', 0);
 297              } else {
 298                  // not found
 299                  return null;
 300              }
 301          }
 302  
 303          $downloadable = has_capability('moodle/site:config', $this->context);
 304          $uploadable   = false;
 305  
 306          $urlbase = $CFG->wwwroot.'/pluginfile.php';
 307          return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('automatedbackup', 'repository'), true, $downloadable, $uploadable, false);
 308      }
 309  
 310      /**
 311       * Gets a stored file for the backup section filearea directory
 312       *
 313       * @param int $itemid item ID
 314       * @param string $filepath file path
 315       * @param string $filename file name
 316       * @return file_info|null file_info instance or null if not found or access not allowed
 317       */
 318      protected function get_area_backup_section($itemid, $filepath, $filename) {
 319          global $CFG, $DB;
 320  
 321          if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
 322              return null;
 323          }
 324  
 325          if (empty($itemid)) {
 326              // list all sections
 327              return new file_info_area_backup_section($this->browser, $this->context, $this->course, $this);
 328          }
 329  
 330          if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
 331              return null; // does not exist
 332          }
 333  
 334          $fs = get_file_storage();
 335  
 336          $filepath = is_null($filepath) ? '/' : $filepath;
 337          $filename = is_null($filename) ? '.' : $filename;
 338          if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'section', $itemid, $filepath, $filename)) {
 339              if ($filepath === '/' and $filename === '.') {
 340                  $storedfile = new virtual_root_file($this->context->id, 'backup', 'section', $itemid);
 341              } else {
 342                  // not found
 343                  return null;
 344              }
 345          }
 346  
 347          $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
 348          $uploadable   = has_capability('moodle/restore:uploadfile', $this->context);
 349  
 350          $urlbase = $CFG->wwwroot.'/pluginfile.php';
 351          return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->id, true, $downloadable, $uploadable, false);
 352      }
 353  
 354      /**
 355       * Returns localised visible name.
 356       *
 357       * @return string
 358       */
 359      public function get_visible_name() {
 360          return ($this->course->id == SITEID) ? get_string('frontpage', 'admin') : format_string(get_course_display_name_for_list($this->course), true, array('context'=>$this->context));
 361      }
 362  
 363      /**
 364       * Whether or not new files or directories can be added
 365       *
 366       * @return bool
 367       */
 368      public function is_writable() {
 369          return false;
 370      }
 371  
 372      /**
 373       * Whether or not this is a directory
 374       *
 375       * @return bool
 376       */
 377      public function is_directory() {
 378          return true;
 379      }
 380  
 381      /**
 382       * Returns list of children.
 383       *
 384       * @return array of file_info instances
 385       */
 386      public function get_children() {
 387          return $this->get_filtered_children('*', false, true);
 388      }
 389  
 390      /**
 391       * Help function to return files matching extensions or their count
 392       *
 393       * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
 394       * @param bool|int $countonly if false returns the children, if an int returns just the
 395       *    count of children but stops counting when $countonly number of children is reached
 396       * @param bool $returnemptyfolders if true returns items that don't have matching files inside
 397       * @return array|int array of file_info instances or the count
 398       */
 399      private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
 400          $areas = array(
 401              array('course', 'summary'),
 402              array('course', 'overviewfiles'),
 403              array('course', 'section'),
 404              array('backup', 'section'),
 405              array('backup', 'course'),
 406              array('backup', 'automated'),
 407              array('course', 'legacy')
 408          );
 409          $children = array();
 410          foreach ($areas as $area) {
 411              if ($child = $this->get_file_info($area[0], $area[1], 0, '/', '.')) {
 412                  if ($returnemptyfolders || $child->count_non_empty_children($extensions)) {
 413                      $children[] = $child;
 414                      if (($countonly !== false) && count($children) >= $countonly) {
 415                          return $countonly;
 416                      }
 417                  }
 418              }
 419          }
 420  
 421          if (!has_capability('moodle/course:managefiles', $this->context)) {
 422              // 'managefiles' capability is checked in every activity module callback.
 423              // Don't even waste time on retrieving the modules if we can't browse the files anyway
 424          } else {
 425              // now list all modules
 426              $modinfo = get_fast_modinfo($this->course);
 427              foreach ($modinfo->cms as $cminfo) {
 428                  if (empty($cminfo->uservisible)) {
 429                      continue;
 430                  }
 431                  $modcontext = context_module::instance($cminfo->id, IGNORE_MISSING);
 432                  if ($child = $this->browser->get_file_info($modcontext)) {
 433                      if ($returnemptyfolders || $child->count_non_empty_children($extensions)) {
 434                          $children[] = $child;
 435                          if (($countonly !== false) && count($children) >= $countonly) {
 436                              return $countonly;
 437                          }
 438                      }
 439                  }
 440              }
 441          }
 442  
 443          if ($countonly !== false) {
 444              return count($children);
 445          }
 446          return $children;
 447      }
 448  
 449      /**
 450       * Returns list of children which are either files matching the specified extensions
 451       * or folders that contain at least one such file.
 452       *
 453       * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
 454       * @return array of file_info instances
 455       */
 456      public function get_non_empty_children($extensions = '*') {
 457          return $this->get_filtered_children($extensions, false);
 458      }
 459  
 460      /**
 461       * Returns the number of children which are either files matching the specified extensions
 462       * or folders containing at least one such file.
 463       *
 464       * @param string|array $extensions, for example '*' or array('.gif','.jpg')
 465       * @param int $limit stop counting after at least $limit non-empty children are found
 466       * @return int
 467       */
 468      public function count_non_empty_children($extensions = '*', $limit = 1) {
 469          return $this->get_filtered_children($extensions, $limit);
 470      }
 471  
 472      /**
 473       * Returns parent file_info instance
 474       *
 475       * @return file_info or null for root
 476       */
 477      public function get_parent() {
 478          $parent = $this->context->get_parent_context();
 479          return $this->browser->get_file_info($parent);
 480      }
 481  }
 482  
 483  
 484  /**
 485   * Subclass of file_info_stored for files in the course files area.
 486   *
 487   * @package   core_files
 488   * @copyright 2008 Petr Skoda (http://skodak.org)
 489   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 490   */
 491  class file_info_area_course_legacy extends file_info_stored {
 492      /**
 493       * Constructor
 494       *
 495       * @param file_browser $browser file browser instance
 496       * @param stdClass $context context object
 497       * @param stored_file $storedfile stored_file instance
 498       */
 499      public function __construct($browser, $context, $storedfile) {
 500          global $CFG;
 501          $urlbase = $CFG->wwwroot.'/file.php';
 502          parent::__construct($browser, $context, $storedfile, $urlbase, get_string('coursefiles'), false, true, true, false);
 503      }
 504  
 505      /**
 506       * Returns file download url
 507       *
 508       * @param bool $forcedownload whether or not force download
 509       * @param bool $https whether or not force https
 510       * @return string url
 511       */
 512      public function get_url($forcedownload=false, $https=false) {
 513          if (!$this->is_readable()) {
 514              return null;
 515          }
 516  
 517          if ($this->lf->is_directory()) {
 518              return null;
 519          }
 520  
 521          $filepath = $this->lf->get_filepath();
 522          $filename = $this->lf->get_filename();
 523          $courseid = $this->context->instanceid;
 524  
 525          $path = '/'.$courseid.$filepath.$filename;
 526  
 527          return file_encode_url($this->urlbase, $path, $forcedownload, $https);
 528      }
 529  
 530      /**
 531       * Returns list of children.
 532       *
 533       * @return array of file_info instances
 534       */
 535      public function get_children() {
 536          if (!$this->lf->is_directory()) {
 537              return array();
 538          }
 539  
 540          $result = array();
 541          $fs = get_file_storage();
 542  
 543          $storedfiles = $fs->get_directory_files($this->context->id, 'course', 'legacy', 0, $this->lf->get_filepath(), false, true, "filepath ASC, filename ASC");
 544          foreach ($storedfiles as $file) {
 545              $result[] = new file_info_area_course_legacy($this->browser, $this->context, $file);
 546          }
 547  
 548          return $result;
 549      }
 550  
 551      /**
 552       * Returns list of children which are either files matching the specified extensions
 553       * or folders that contain at least one such file.
 554       *
 555       * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
 556       * @return array of file_info instances
 557       */
 558      public function get_non_empty_children($extensions = '*') {
 559          if (!$this->lf->is_directory()) {
 560              return array();
 561          }
 562  
 563          $result = array();
 564          $fs = get_file_storage();
 565  
 566          $storedfiles = $fs->get_directory_files($this->context->id, 'course', 'legacy', 0,
 567                                                  $this->lf->get_filepath(), false, true, "filepath, filename");
 568          foreach ($storedfiles as $file) {
 569              $extension = core_text::strtolower(pathinfo($file->get_filename(), PATHINFO_EXTENSION));
 570              if ($file->is_directory() || $extensions === '*' || (!empty($extension) && in_array('.'.$extension, $extensions))) {
 571                  $fileinfo = new file_info_area_course_legacy($this->browser, $this->context, $file, $this->urlbase, $this->topvisiblename,
 572                                                   $this->itemidused, $this->readaccess, $this->writeaccess, false);
 573                  if (!$file->is_directory() || $fileinfo->count_non_empty_children($extensions)) {
 574                      $result[] = $fileinfo;
 575                  }
 576              }
 577          }
 578  
 579          return $result;
 580      }
 581  }
 582  
 583  /**
 584   * Represents a course category context in the tree navigated by {@link file_browser}.
 585   *
 586   * @package    core_files
 587   * @copyright  2008 Petr Skoda (http://skodak.org)
 588   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 589   */
 590  class file_info_area_course_section extends file_info {
 591      /** @var stdClass course object */
 592      protected $course;
 593      /** @var file_info_context_course course file info object */
 594      protected $courseinfo;
 595  
 596      /**
 597       * Constructor
 598       *
 599       * @param file_browser $browser file browser instance
 600       * @param stdClass $context context object
 601       * @param stdClass $course course object
 602       * @param file_info_context_course $courseinfo file info instance
 603       */
 604      public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
 605          parent::__construct($browser, $context);
 606          $this->course     = $course;
 607          $this->courseinfo = $courseinfo;
 608      }
 609  
 610      /**
 611       * Returns list of standard virtual file/directory identification.
 612       * The difference from stored_file parameters is that null values
 613       * are allowed in all fields
 614       *
 615       * @return array with keys contextid, filearea, itemid, filepath and filename
 616       */
 617      public function get_params() {
 618          return array('contextid' => $this->context->id,
 619                       'component' => 'course',
 620                       'filearea'  => 'section',
 621                       'itemid'    => null,
 622                       'filepath'  => null,
 623                       'filename'  => null);
 624      }
 625  
 626      /**
 627       * Returns localised visible name.
 628       *
 629       * @return string
 630       */
 631      public function get_visible_name() {
 632          //$format = $this->course->format;
 633          $sectionsname = get_string("coursesectionsummaries");
 634  
 635          return $sectionsname;
 636      }
 637  
 638      /**
 639       * Return whether or not new files or directories can be added
 640       *
 641       * @return bool
 642       */
 643      public function is_writable() {
 644          return false;
 645      }
 646  
 647      /**
 648       * Return whether or not this is a empty area
 649       *
 650       * @return bool
 651       */
 652      public function is_empty_area() {
 653          $fs = get_file_storage();
 654          return $fs->is_area_empty($this->context->id, 'course', 'section');
 655      }
 656  
 657      /**
 658       * Return whether or not this is a empty area
 659       *
 660       * @return bool
 661       */
 662      public function is_directory() {
 663          return true;
 664      }
 665  
 666      /**
 667       * Returns list of children.
 668       *
 669       * @return array of file_info instances
 670       */
 671      public function get_children() {
 672          global $DB;
 673  
 674          $children = array();
 675  
 676          $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
 677          foreach ($course_sections as $section) {
 678              if ($child = $this->courseinfo->get_file_info('course', 'section', $section->id, '/', '.')) {
 679                  $children[] = $child;
 680              }
 681          }
 682  
 683          return $children;
 684      }
 685  
 686      /**
 687       * Returns the number of children which are either files matching the specified extensions
 688       * or folders containing at least one such file.
 689       *
 690       * @param string|array $extensions, for example '*' or array('.gif','.jpg')
 691       * @param int $limit stop counting after at least $limit non-empty children are found
 692       * @return int
 693       */
 694      public function count_non_empty_children($extensions = '*', $limit = 1) {
 695          global $DB;
 696          $params1 = array(
 697              'courseid' => $this->course->id,
 698              'contextid' => $this->context->id,
 699              'component' => 'course',
 700              'filearea' => 'section',
 701              'emptyfilename' => '.');
 702          $sql1 = "SELECT DISTINCT cs.id FROM {files} f, {course_sections} cs
 703              WHERE cs.course = :courseid
 704              AND f.contextid = :contextid
 705              AND f.component = :component
 706              AND f.filearea = :filearea
 707              AND f.itemid = cs.id
 708              AND f.filename <> :emptyfilename";
 709          list($sql2, $params2) = $this->build_search_files_sql($extensions);
 710          $rs = $DB->get_recordset_sql($sql1. ' '. $sql2, array_merge($params1, $params2));
 711          $cnt = 0;
 712          foreach ($rs as $record) {
 713              if ((++$cnt) >= $limit) {
 714                  break;
 715              }
 716          }
 717          $rs->close();
 718          return $cnt;
 719      }
 720  
 721      /**
 722       * Returns parent file_info instance
 723       *
 724       * @return file_info|null file_info or null for root
 725       */
 726      public function get_parent() {
 727          return $this->courseinfo;
 728      }
 729  }
 730  
 731  
 732  /**
 733   * Implementation of course section backup area
 734   *
 735   * @package    core_files
 736   * @copyright  2008 Petr Skoda (http://skodak.org)
 737   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 738   */
 739  class file_info_area_backup_section extends file_info {
 740      /** @var stdClass course object */
 741      protected $course;
 742      /** @var file_info_context_course course file info object */
 743      protected $courseinfo;
 744  
 745      /**
 746       * Constructor
 747       *
 748       * @param file_browser $browser file browser instance
 749       * @param stdClass $context context object
 750       * @param stdClass $course course object
 751       * @param file_info_context_course $courseinfo file info instance
 752       */
 753      public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
 754          parent::__construct($browser, $context);
 755          $this->course     = $course;
 756          $this->courseinfo = $courseinfo;
 757      }
 758  
 759      /**
 760       * Returns list of standard virtual file/directory identification.
 761       * The difference from stored_file parameters is that null values
 762       * are allowed in all fields
 763       *
 764       * @return array with keys contextid, component, filearea, itemid, filepath and filename
 765       */
 766      public function get_params() {
 767          return array('contextid' => $this->context->id,
 768                       'component' => 'backup',
 769                       'filearea'  => 'section',
 770                       'itemid'    => null,
 771                       'filepath'  => null,
 772                       'filename'  => null);
 773      }
 774  
 775      /**
 776       * Returns localised visible name.
 777       *
 778       * @return string
 779       */
 780      public function get_visible_name() {
 781          return get_string('sectionbackup', 'repository');
 782      }
 783  
 784      /**
 785       * Return whether or not new files and directories can be added
 786       *
 787       * @return bool
 788       */
 789      public function is_writable() {
 790          return false;
 791      }
 792  
 793      /**
 794       * Whether or not this is an empty area
 795       *
 796       * @return bool
 797       */
 798      public function is_empty_area() {
 799          $fs = get_file_storage();
 800          return $fs->is_area_empty($this->context->id, 'backup', 'section');
 801      }
 802  
 803      /**
 804       * Return whether or not this is a directory
 805       *
 806       * @return bool
 807       */
 808      public function is_directory() {
 809          return true;
 810      }
 811  
 812      /**
 813       * Returns list of children.
 814       *
 815       * @return array of file_info instances
 816       */
 817      public function get_children() {
 818          global $DB;
 819  
 820          $children = array();
 821  
 822          $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
 823          foreach ($course_sections as $section) {
 824              if ($child = $this->courseinfo->get_file_info('backup', 'section', $section->id, '/', '.')) {
 825                  $children[] = $child;
 826              }
 827          }
 828  
 829          return $children;
 830      }
 831  
 832      /**
 833       * Returns the number of children which are either files matching the specified extensions
 834       * or folders containing at least one such file.
 835       *
 836       * @param string|array $extensions, for example '*' or array('.gif','.jpg')
 837       * @param int $limit stop counting after at least $limit non-empty children are found
 838       * @return int
 839       */
 840      public function count_non_empty_children($extensions = '*', $limit = 1) {
 841          global $DB;
 842          $params1 = array(
 843              'courseid' => $this->course->id,
 844              'contextid' => $this->context->id,
 845              'component' => 'backup',
 846              'filearea' => 'section',
 847              'emptyfilename' => '.');
 848          $sql1 = "SELECT DISTINCT cs.id AS sectionid FROM {files} f, {course_sections} cs
 849              WHERE cs.course = :courseid
 850              AND f.contextid = :contextid
 851              AND f.component = :component
 852              AND f.filearea = :filearea
 853              AND f.itemid = cs.id
 854              AND f.filename <> :emptyfilename";
 855          list($sql2, $params2) = $this->build_search_files_sql($extensions);
 856          $rs = $DB->get_recordset_sql($sql1. ' '. $sql2, array_merge($params1, $params2));
 857          $cnt = 0;
 858          foreach ($rs as $record) {
 859              if ((++$cnt) >= $limit) {
 860                  break;
 861              }
 862          }
 863          $rs->close();
 864          return $cnt;
 865      }
 866  
 867      /**
 868       * Returns parent file_info instance
 869       *
 870       * @return file_info or null for root
 871       */
 872      public function get_parent() {
 873          return $this->browser->get_file_info($this->context);
 874      }
 875  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1