Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is 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  namespace core_course\cache;
  18  
  19  use cache_data_source;
  20  use cache_definition;
  21  use moodle_url;
  22  use core_course_list_element;
  23  
  24  /**
  25   * Class to describe cache data source for course image.
  26   *
  27   * @package    core
  28   * @subpackage course
  29   * @author     Dmitrii Metelkin <dmitriim@catalyst-au.net>
  30   * @copyright  2021 Catalyst IT
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class course_image implements cache_data_source {
  34  
  35      /** @var course_image */
  36      protected static $instance = null;
  37  
  38      /**
  39       * Returns an instance of the data source class that the cache can use for loading data using the other methods
  40       * specified by this interface.
  41       *
  42       * @param cache_definition $definition
  43       * @return \core_course\cache\course_image
  44       */
  45      public static function get_instance_for_cache(cache_definition $definition): course_image {
  46          if (is_null(self::$instance)) {
  47              self::$instance = new course_image();
  48          }
  49          return self::$instance;
  50      }
  51  
  52      /**
  53       * Loads the data for the key provided ready formatted for caching.
  54       *
  55       * @param string|int $key The key to load.
  56       * @return string|bool Returns course image url as a string or false if the image is not exist
  57       */
  58      public function load_for_cache($key) {
  59          // We should use get_course() instead of get_fast_modinfo() for better performance.
  60          $course = get_course($key);
  61          return $this->get_image_url_from_overview_files($course);
  62      }
  63  
  64      /**
  65       * Returns image URL from course overview files.
  66       *
  67       * @param \stdClass $course Course object.
  68       * @return null|string Image URL or null if it's not exists.
  69       */
  70      protected function get_image_url_from_overview_files(\stdClass $course): ?string {
  71          $courseinlist = new core_course_list_element($course);
  72          foreach ($courseinlist->get_course_overviewfiles() as $file) {
  73              if ($file->is_valid_image()) {
  74                  return moodle_url::make_pluginfile_url(
  75                      $file->get_contextid(),
  76                      $file->get_component(),
  77                      $file->get_filearea(),
  78                      null,
  79                      $file->get_filepath(),
  80                      $file->get_filename()
  81                  )->out();
  82              }
  83          }
  84  
  85          // Returning null if no image found to let it be cached
  86          // as false is what cache API returns then a data is not found in cache.
  87          return null;
  88      }
  89  
  90      /**
  91       * Loads several keys for the cache.
  92       *
  93       * @param array $keys An array of keys each of which will be string|int.
  94       * @return array An array of matching data items.
  95       */
  96      public function load_many_for_cache(array $keys): array {
  97          $records = [];
  98          foreach ($keys as $key) {
  99              $records[$key] = $this->load_for_cache($key);
 100          }
 101          return $records;
 102      }
 103  }