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
 * Discussion vault class.
19
 *
20
 * @package    mod_forum
21
 * @copyright  2019 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\container;
30
use mod_forum\local\entities\forum as forum_entity;
31
use mod_forum\local\entities\discussion as discussion_entity;
32
 
33
/**
34
 * Discussion 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  2019 Ryan Wyllie <ryan@moodle.com>
42
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43
 */
44
class discussion extends db_table_vault {
45
    /** The table for this vault */
46
    private const TABLE = 'forum_discussions';
47
 
48
    /**
49
     * Get the table alias.
50
     *
51
     * @return string
52
     */
53
    protected function get_table_alias(): string {
54
        return 'd';
55
    }
56
 
57
    /**
58
     * Build the SQL to be used in get_records_sql.
59
     *
60
     * @param string|null $wheresql Where conditions for the SQL
61
     * @param string|null $sortsql Order by conditions for the SQL
62
     * @param int|null $userid The user ID
63
     * @return string
64
     */
65
    protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null, ?int $userid = null): string {
66
        $selectsql = 'SELECT * FROM {' . self::TABLE . '} ' . $this->get_table_alias();
67
        $selectsql .= $wheresql ? ' WHERE ' . $wheresql : '';
68
        $selectsql .= $sortsql ? ' ORDER BY ' . $sortsql : '';
69
 
70
        return $selectsql;
71
    }
72
 
73
    /**
74
     * Convert the DB records into discussion entities.
75
     *
76
     * @param array $results The DB records
77
     * @return discussion_entity[]
78
     */
79
    protected function from_db_records(array $results) {
80
        $entityfactory = $this->get_entity_factory();
81
 
82
        return array_map(function(array $result) use ($entityfactory) {
83
            [
84
                'record' => $record,
85
            ] = $result;
86
            return $entityfactory->get_discussion_from_stdclass($record);
87
        }, $results);
88
    }
89
 
90
    /**
91
     * Get all discussions in the specified forum.
92
     *
93
     * @param   forum_entity $forum
94
     * @return  array
95
     */
96
    public function get_all_discussions_in_forum(forum_entity $forum, string $sort = null): ?array {
97
        global $USER;
98
        $options = ['forum' => $forum->get_id()];
99
 
100
        $managerfactory = container::get_manager_factory();
101
        $capabilitymanager = $managerfactory->get_capability_manager($forum);
102
 
103
        $select = "forum = :forum";
104
 
105
        if ($forum->is_in_group_mode() && !$capabilitymanager->can_access_all_groups($USER)) {
106
            $allowedgroups = groups_get_activity_allowed_groups($forum->get_course_module_record());
107
            $allowedgroups = implode(",", array_keys($allowedgroups));
108
            if (!$allowedgroups) {
109
                return [];
110
            }
111
            $select .= " AND groupid IN ($allowedgroups)";
112
        }
113
        $records = $this->get_db()->get_records_select(self::TABLE, $select, $options, $sort ?? '');
114
 
115
        return $this->transform_db_records_to_entities($records);
116
    }
117
 
118
    /**
119
     * Get the first discussion in the specified forum.
120
     *
121
     * @param   forum_entity $forum
122
     * @return  discussion_entity|null
123
     */
124
    public function get_first_discussion_in_forum(forum_entity $forum): ?discussion_entity {
125
        $records = $this->get_db()->get_records(self::TABLE, [
126
            'forum' => $forum->get_id(),
127
        ], 'timemodified ASC', '*', 0, 1);
128
 
129
        $records = $this->transform_db_records_to_entities($records);
130
        return count($records) ? array_shift($records) : null;
131
    }
132
 
133
    /**
134
     * Get the last discussion in the specified forum.
135
     *
136
     * @param   forum_entity $forum
137
     * @return  discussion_entity|null
138
     */
139
    public function get_last_discussion_in_forum(forum_entity $forum): ?discussion_entity {
140
        $records = $this->get_db()->get_records(self::TABLE, [
141
            'forum' => $forum->get_id(),
142
        ], 'timemodified DESC', '*', 0, 1);
143
 
144
        $records = $this->transform_db_records_to_entities($records);
145
        return count($records) ? array_shift($records) : null;
146
    }
147
 
148
    /**
149
     * Get the count of the discussions in the specified forum.
150
     *
151
     * @param   forum_entity $forum
152
     * @return  int
153
     */
154
    public function get_count_discussions_in_forum(forum_entity $forum): ?int {
155
        return $this->get_db()->count_records(self::TABLE, [
156
            'forum' => $forum->get_id()]);
157
    }
158
 
159
    /**
160
     * Update the discussion
161
     *
162
     * @param discussion_entity $discussion
163
     * @return discussion_entity|null
164
     */
165
    public function update_discussion(discussion_entity $discussion): ?discussion_entity {
166
        $discussionrecord = $this->get_legacy_factory()->to_legacy_object($discussion);
167
        if ($this->get_db()->update_record('forum_discussions', $discussionrecord)) {
168
            $records = $this->transform_db_records_to_entities([$discussionrecord]);
169
 
170
            return count($records) ? array_shift($records) : null;
171
        }
172
 
173
        return null;
174
    }
175
}