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 read receipt collection 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 stdClass; 30 31 /** 32 * Post read receipt collection class. 33 * 34 * This should be the only place that accessed the database. 35 * 36 * This uses the repository pattern. See: 37 * https://designpatternsphp.readthedocs.io/en/latest/More/Repository/README.html 38 * 39 * @copyright 2018 Ryan Wyllie <ryan@moodle.com> 40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 41 */ 42 class post_read_receipt_collection extends db_table_vault { 43 /** The table for this vault */ 44 private const TABLE = 'forum_read'; 45 46 /** 47 * Get the table alias. 48 * 49 * @return string 50 */ 51 protected function get_table_alias() : string { 52 return 'fr'; 53 } 54 55 /** 56 * Build the SQL to be used in get_records_sql. 57 * 58 * @param string|null $wheresql Where conditions for the SQL 59 * @param string|null $sortsql Order by conditions for the SQL 60 * @param int|null $userid The user ID 61 * @return string 62 */ 63 protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null, ?int $userid = null) : string { 64 $selectsql = 'SELECT * FROM {' . self::TABLE . '} ' . $this->get_table_alias(); 65 $selectsql .= $wheresql ? ' WHERE ' . $wheresql : ''; 66 $selectsql .= $sortsql ? ' ORDER BY ' . $sortsql : ''; 67 68 return $selectsql; 69 } 70 71 /** 72 * Convert the DB records into post_read_receipt_collection entities. 73 * 74 * @param array $results The DB records 75 * @return post_read_receipt_collection 76 */ 77 protected function from_db_records(array $results) { 78 $entityfactory = $this->get_entity_factory(); 79 $records = array_map(function($result) { 80 return $result['record']; 81 }, $results); 82 83 return $entityfactory->get_post_read_receipt_collection_from_stdclasses($records); 84 } 85 86 /** 87 * Load the post_read_receipt_collection for the given user and set 88 * of posts. 89 * 90 * @param int $userid Id of the user to load receipts for 91 * @param int[] $postids List of post ids to load receipts for 92 * @return post_read_receipt_collection 93 */ 94 public function get_from_user_id_and_post_ids(int $userid, array $postids) { 95 $alias = $this->get_table_alias(); 96 [$postidinsql, $params] = $this->get_db()->get_in_or_equal($postids); 97 $params[] = $userid; 98 99 $wheresql = "{$alias}.postid {$postidinsql}"; 100 $wheresql .= " AND {$alias}.userid = ?"; 101 $sql = $this->generate_get_records_sql($wheresql); 102 $records = $this->get_db()->get_records_sql($sql, $params); 103 104 return $this->transform_db_records_to_entities($records); 105 } 106 107 /** 108 * Load the post_read_receipt_collection for the given user and set 109 * of posts. 110 * 111 * @param stdClass $user The user to load receipts for 112 * @param post_entity[] $posts List of posts to load receipts for 113 * @return post_read_receipt_collection 114 */ 115 public function get_from_user_and_posts(stdClass $user, array $posts) { 116 $postids = array_map(function($post) { 117 return $post->get_id(); 118 }, $posts); 119 return $this->get_from_user_id_and_post_ids($user->id, $postids); 120 } 121 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body