Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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   * Utility class for browsing of content bank files in the course category context.
  19   *
  20   * @package    repository_contentbank
  21   * @copyright  2020 Mihail Geshoski <mihail@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace repository_contentbank\browser;
  26  
  27  /**
  28   * Represents the content bank browser in the course category context.
  29   *
  30   * @package    repository_contentbank
  31   * @copyright  2020 Mihail Geshoski <mihail@moodle.com>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class contentbank_browser_context_coursecat extends contentbank_browser {
  35  
  36      /**
  37       * Constructor.
  38       *
  39       * @param \context_coursecat $context The current context
  40       */
  41      public function __construct(\context_coursecat $context) {
  42          $this->context = $context;
  43      }
  44  
  45      /**
  46       * Define the allowed child context levels.
  47       *
  48       * @return int[] The array containing the relevant child context levels
  49       */
  50      protected function allowed_child_context_levels(): array {
  51          // The expected child contexts in the course category context level are the course category context
  52          // (ex. subcategories) and the course context.
  53          return [\CONTEXT_COURSECAT, \CONTEXT_COURSE];
  54      }
  55  
  56      /**
  57       * The required condition to enable the user to view/access the content bank content in this context.
  58       *
  59       * @return bool Whether the user can view/access the content bank content in the context
  60       */
  61      public function can_access_content(): bool {
  62          // When the following conditions are met, the user would be able to share the content created in the course
  63          // category context level all over the site.
  64          // The content from the course category context level should be available to either:
  65          // * Every user which has a capability to access the 'general' content and has capability to access the
  66          // content of any child course of the given course category.
  67          // * Users that have capability to access content at a course category context level.
  68  
  69          if (has_capability('repository/contentbank:accesscoursecategorycontent', $this->context)) {
  70              return true;
  71          }
  72  
  73          $canaccesschildcontent = false;
  74          foreach ($this->get_child_contexts() as $childcontext) {
  75              $browser = \repository_contentbank\helper::get_contentbank_browser($childcontext);
  76              if ($canaccesschildcontent = $browser->can_access_content()) {
  77                  break;
  78              }
  79          }
  80  
  81          return $canaccesschildcontent && has_capability('repository/contentbank:accessgeneralcontent',
  82              $this->context);
  83      }
  84  }