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\entities\forum as forum_entity; 30 use mod_forum\local\entities\discussion as discussion_entity; 31 32 /** 33 * Discussion vault class. 34 * 35 * This should be the only place that accessed the database. 36 * 37 * This uses the repository pattern. See: 38 * https://designpatternsphp.readthedocs.io/en/latest/More/Repository/README.html 39 * 40 * @copyright 2019 Ryan Wyllie <ryan@moodle.com> 41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 42 */ 43 class discussion extends db_table_vault { 44 /** The table for this vault */ 45 private const TABLE = 'forum_discussions'; 46 47 /** 48 * Get the table alias. 49 * 50 * @return string 51 */ 52 protected function get_table_alias() : string { 53 return 'd'; 54 } 55 56 /** 57 * Build the SQL to be used in get_records_sql. 58 * 59 * @param string|null $wheresql Where conditions for the SQL 60 * @param string|null $sortsql Order by conditions for the SQL 61 * @param int|null $userid The user ID 62 * @return string 63 */ 64 protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null, ?int $userid = null) : string { 65 $selectsql = 'SELECT * FROM {' . self::TABLE . '} ' . $this->get_table_alias(); 66 $selectsql .= $wheresql ? ' WHERE ' . $wheresql : ''; 67 $selectsql .= $sortsql ? ' ORDER BY ' . $sortsql : ''; 68 69 return $selectsql; 70 } 71 72 /** 73 * Convert the DB records into discussion entities. 74 * 75 * @param array $results The DB records 76 * @return discussion_entity[] 77 */ 78 protected function from_db_records(array $results) { 79 $entityfactory = $this->get_entity_factory(); 80 81 return array_map(function(array $result) use ($entityfactory) { 82 [ 83 'record' => $record, 84 ] = $result; 85 return $entityfactory->get_discussion_from_stdclass($record); 86 }, $results); 87 } 88 89 /** 90 * Get all discussions in the specified forum. 91 * 92 * @param forum_entity $forum 93 * @return array 94 */ 95 public function get_all_discussions_in_forum(forum_entity $forum, string $sort = null): ?array { 96 $records = $this->get_db()->get_records(self::TABLE, [ 97 'forum' => $forum->get_id(), 98 ], $sort ?? ''); 99 100 return $this->transform_db_records_to_entities($records); 101 } 102 103 /** 104 * Get the first discussion in the specified forum. 105 * 106 * @param forum_entity $forum 107 * @return discussion_entity|null 108 */ 109 public function get_first_discussion_in_forum(forum_entity $forum) : ?discussion_entity { 110 $records = $this->get_db()->get_records(self::TABLE, [ 111 'forum' => $forum->get_id(), 112 ], 'timemodified ASC', '*', 0, 1); 113 114 $records = $this->transform_db_records_to_entities($records); 115 return count($records) ? array_shift($records) : null; 116 } 117 118 /** 119 * Get the last discussion in the specified forum. 120 * 121 * @param forum_entity $forum 122 * @return discussion_entity|null 123 */ 124 public function get_last_discussion_in_forum(forum_entity $forum) : ?discussion_entity { 125 $records = $this->get_db()->get_records(self::TABLE, [ 126 'forum' => $forum->get_id(), 127 ], 'timemodified DESC', '*', 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 count of the discussions in the specified forum. 135 * 136 * @param forum_entity $forum 137 * @return int 138 */ 139 public function get_count_discussions_in_forum(forum_entity $forum) : ?int { 140 return $this->get_db()->count_records(self::TABLE, [ 141 'forum' => $forum->get_id()]); 142 } 143 144 /** 145 * Update the discussion 146 * 147 * @param discussion_entity $discussion 148 * @return discussion_entity|null 149 */ 150 public function update_discussion(discussion_entity $discussion) : ?discussion_entity { 151 $discussionrecord = $this->get_legacy_factory()->to_legacy_object($discussion); 152 if ($this->get_db()->update_record('forum_discussions', $discussionrecord)) { 153 $records = $this->transform_db_records_to_entities([$discussionrecord]); 154 155 return count($records) ? array_shift($records) : null; 156 } 157 158 return null; 159 } 160 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body