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.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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   * Post attachment vault class.
  19   *
  20   * @package    mod_forum
  21   * @copyright  2018 Ryan Wyllie <ryan@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_forum\local\vaults;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use mod_forum\local\entities\post as post_entity;
  30  use context;
  31  use file_storage;
  32  
  33  /**
  34   * Post attachment vault class.
  35   *
  36   * This should be the only place that accessed the database.
  37   *
  38   * This uses the repository pattern. See:
  39   * https://designpatternsphp.readthedocs.io/en/latest/More/Repository/README.html
  40   *
  41   * @copyright  2018 Ryan Wyllie <ryan@moodle.com>
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class post_attachment {
  45      /** The component for attachments */
  46      private const COMPONENT = 'mod_forum';
  47      /** File area for attachments */
  48      private const FILE_AREA = 'attachment';
  49      /** Sort the attachments by filename */
  50      private const SORT = 'filename';
  51      /** Don't include directories */
  52      private const INCLUDE_DIRECTORIES = false;
  53      /** @var file_storage $filestorage File storage */
  54      private $filestorage;
  55  
  56      /**
  57       * Construct.
  58       *
  59       * @param file_storage $filestorage File storage
  60       */
  61      public function __construct(file_storage $filestorage) {
  62          $this->filestorage = $filestorage;
  63      }
  64  
  65      /**
  66       * Get the attachments for the given posts. The results are indexed by
  67       * post id.
  68       *
  69       * @param context $context The (forum) context that the posts are in
  70       * @param post_entity[] $posts The list of posts to load attachments for
  71       * @return array Post attachments indexed by post id
  72       */
  73      public function get_attachments_for_posts(context $context, array $posts) {
  74          $itemids = array_map(function($post) {
  75              return $post->get_id();
  76          }, $posts);
  77  
  78          $files = $this->filestorage->get_area_files(
  79              $context->id,
  80              self::COMPONENT,
  81              self::FILE_AREA,
  82              $itemids,
  83              self::SORT,
  84              self::INCLUDE_DIRECTORIES
  85          );
  86  
  87          $filesbyid = array_reduce($posts, function($carry, $post) {
  88              $carry[$post->get_id()] = [];
  89              return $carry;
  90          }, []);
  91  
  92          return array_reduce($files, function($carry, $file) {
  93              $itemid = $file->get_itemid();
  94              $carry[$itemid] = array_merge($carry[$itemid], [$file]);
  95              return $carry;
  96          }, $filesbyid);
  97      }
  98  }