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 |
* Author 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 |
/**
|
|
|
30 |
* Author vault class.
|
|
|
31 |
*
|
|
|
32 |
* This should be the only place that accessed the database.
|
|
|
33 |
*
|
|
|
34 |
* This uses the repository pattern. See:
|
|
|
35 |
* https://designpatternsphp.readthedocs.io/en/latest/More/Repository/README.html
|
|
|
36 |
*
|
|
|
37 |
* @copyright 2019 Ryan Wyllie <ryan@moodle.com>
|
|
|
38 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
39 |
*/
|
|
|
40 |
class author extends db_table_vault {
|
|
|
41 |
/** The table for this vault */
|
|
|
42 |
private const TABLE = 'user';
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Get the table alias.
|
|
|
46 |
*
|
|
|
47 |
* @return string
|
|
|
48 |
*/
|
|
|
49 |
protected function get_table_alias(): string {
|
|
|
50 |
return 'a';
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Build the SQL to be used in get_records_sql.
|
|
|
55 |
*
|
|
|
56 |
* @param string|null $wheresql Where conditions for the SQL
|
|
|
57 |
* @param string|null $sortsql Order by conditions for the SQL
|
|
|
58 |
* @param int|null $userid The user ID
|
|
|
59 |
* @return string
|
|
|
60 |
*/
|
|
|
61 |
protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null, ?int $userid = null): string {
|
|
|
62 |
$selectsql = 'SELECT * FROM {' . self::TABLE . '} ' . $this->get_table_alias();
|
|
|
63 |
$selectsql .= $wheresql ? ' WHERE ' . $wheresql : '';
|
|
|
64 |
$selectsql .= $sortsql ? ' ORDER BY ' . $sortsql : '';
|
|
|
65 |
|
|
|
66 |
return $selectsql;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Convert the DB records into author entities.
|
|
|
71 |
*
|
|
|
72 |
* @param array $results The DB records
|
|
|
73 |
* @return author_entity[]
|
|
|
74 |
*/
|
|
|
75 |
protected function from_db_records(array $results) {
|
|
|
76 |
$entityfactory = $this->get_entity_factory();
|
|
|
77 |
|
|
|
78 |
return array_map(function(array $result) use ($entityfactory) {
|
|
|
79 |
[
|
|
|
80 |
'record' => $record,
|
|
|
81 |
] = $result;
|
|
|
82 |
return $entityfactory->get_author_from_stdclass($record);
|
|
|
83 |
}, $results);
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Get the authors for the given posts.
|
|
|
88 |
*
|
|
|
89 |
* Returns a distinct list of authors indexed by author id.
|
|
|
90 |
*
|
|
|
91 |
* @param post_entity[] $posts The list of posts
|
|
|
92 |
* @return author_entity[]
|
|
|
93 |
*/
|
|
|
94 |
public function get_authors_for_posts(array $posts): array {
|
|
|
95 |
$authorids = array_reduce($posts, function($carry, $post) {
|
|
|
96 |
$carry[$post->get_author_id()] = true;
|
|
|
97 |
return $carry;
|
|
|
98 |
}, []);
|
|
|
99 |
$authorids = array_keys($authorids);
|
|
|
100 |
return $this->get_from_ids($authorids);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Get the context ids for a set of author ids. The results are indexed
|
|
|
105 |
* by the author id.
|
|
|
106 |
*
|
|
|
107 |
* @param int[] $authorids The list of author ids to fetch.
|
|
|
108 |
* @return int[] Results indexed by author id.
|
|
|
109 |
*/
|
|
|
110 |
public function get_context_ids_for_author_ids(array $authorids): array {
|
|
|
111 |
$db = $this->get_db();
|
|
|
112 |
[$insql, $params] = $db->get_in_or_equal($authorids);
|
|
|
113 |
$sql = "SELECT instanceid, id FROM {context} WHERE contextlevel = ? AND instanceid {$insql}";
|
|
|
114 |
$records = $db->get_records_sql($sql, array_merge([CONTEXT_USER], $params));
|
|
|
115 |
return array_reduce($authorids, function($carry, $id) use ($records) {
|
|
|
116 |
$carry[$id] = isset($records[$id]) ? (int) $records[$id]->id : null;
|
|
|
117 |
return $carry;
|
|
|
118 |
}, []);
|
|
|
119 |
}
|
|
|
120 |
}
|