Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
    /** Sort the attachments by filename */
48
    private const SORT = 'filename';
49
    /** Don't include directories */
50
    private const INCLUDE_DIRECTORIES = false;
51
    /** @var file_storage $filestorage File storage */
52
    private $filestorage;
53
 
54
    /**
55
     * Construct.
56
     *
57
     * @param file_storage $filestorage File storage
58
     */
59
    public function __construct(file_storage $filestorage) {
60
        $this->filestorage = $filestorage;
61
    }
62
 
63
    /**
64
     * Get the attachments for the given posts. The results are indexed by
65
     * post id.
66
     *
67
     * @param context $context The (forum) context that the posts are in
68
     * @param post_entity[] $posts The list of posts to load attachments for
69
     * @param string $area The file storage area, can be 'attachment' or 'post' for inline attachments.
70
     * @return array Post attachments indexed by post id
71
     */
72
    private function get_area_attachments_for_posts(context $context, array $posts, string $area) {
73
        $itemids = array_map(function($post) {
74
            return $post->get_id();
75
        }, $posts);
76
 
77
        $files = $this->filestorage->get_area_files(
78
            $context->id,
79
            self::COMPONENT,
80
            $area,
81
            $itemids,
82
            self::SORT,
83
            self::INCLUDE_DIRECTORIES
84
        );
85
 
86
        $filesbyid = array_reduce($posts, function($carry, $post) {
87
            $carry[$post->get_id()] = [];
88
            return $carry;
89
        }, []);
90
 
91
        return array_reduce($files, function($carry, $file) {
92
            $itemid = $file->get_itemid();
93
            $carry[$itemid] = array_merge($carry[$itemid], [$file]);
94
            return $carry;
95
        }, $filesbyid);
96
    }
97
 
98
    /**
99
     * Get attachment for posts.
100
     *
101
     * @param context $context The (forum) context that the posts are in
102
     * @param post_entity[] $posts The list of posts to load attachments for
103
     * @return array Post attachments indexed by post id
104
     */
105
    public function get_attachments_for_posts(context $context, array $posts) {
106
        return $this->get_area_attachments_for_posts($context, $posts, 'attachment');
107
    }
108
 
109
    /**
110
     * Get inline attachments for posts.
111
     *
112
     * @param context $context The (forum) context that the posts are in
113
     * @param post_entity[] $posts The list of posts to load attachments for
114
     * @return array Post attachments indexed by post id
115
     */
116
    public function get_inline_attachments_for_posts(context $context, array $posts) {
117
        return $this->get_area_attachments_for_posts($context, $posts, 'post');
118
    }
119
 
120
}