| 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 | declare(strict_types=1);
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | namespace core_blog\reportbuilder\datasource;
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | use lang_string;
 | 
        
           |  |  | 22 | use core_reportbuilder\datasource;
 | 
        
           |  |  | 23 | use core_reportbuilder\local\entities\{course, user};
 | 
        
           |  |  | 24 | use core_blog\reportbuilder\local\entities\blog;
 | 
        
           |  |  | 25 | use core_files\reportbuilder\local\entities\file;
 | 
        
           |  |  | 26 | use core_comment\reportbuilder\local\entities\comment;
 | 
        
           |  |  | 27 | use core_tag\reportbuilder\local\entities\tag;
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | /**
 | 
        
           |  |  | 30 |  * Blogs datasource
 | 
        
           |  |  | 31 |  *
 | 
        
           |  |  | 32 |  * @package     core_blog
 | 
        
           |  |  | 33 |  * @copyright   2022 Paul Holden <paulh@moodle.com>
 | 
        
           |  |  | 34 |  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 35 |  */
 | 
        
           |  |  | 36 | class blogs extends datasource {
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 |     /**
 | 
        
           |  |  | 39 |      * Return user friendly name of the report source
 | 
        
           |  |  | 40 |      *
 | 
        
           |  |  | 41 |      * @return string
 | 
        
           |  |  | 42 |      */
 | 
        
           |  |  | 43 |     public static function get_name(): string {
 | 
        
           |  |  | 44 |         return get_string('blogs', 'core_blog');
 | 
        
           |  |  | 45 |     }
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 |     /**
 | 
        
           |  |  | 48 |      * Initialise report
 | 
        
           |  |  | 49 |      */
 | 
        
           |  |  | 50 |     protected function initialise(): void {
 | 
        
           |  |  | 51 |         $blogentity = new blog();
 | 
        
           |  |  | 52 |   | 
        
           |  |  | 53 |         $postalias = $blogentity->get_table_alias('post');
 | 
        
           |  |  | 54 |         $this->set_main_table('post', $postalias);
 | 
        
           |  |  | 55 |         $this->add_base_condition_simple("{$postalias}.module", 'blog');
 | 
        
           |  |  | 56 |   | 
        
           |  |  | 57 |         $this->add_entity($blogentity);
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 |         // Join the files entity.
 | 
        
           |  |  | 60 |         $fileentity = (new file())
 | 
        
           |  |  | 61 |             ->set_entity_title(new lang_string('blogattachment', 'core_blog'));
 | 
        
           |  |  | 62 |         $filesalias = $fileentity->get_table_alias('files');
 | 
        
           |  |  | 63 |         $this->add_entity($fileentity
 | 
        
           |  |  | 64 |             ->add_join("LEFT JOIN {files} {$filesalias}
 | 
        
           |  |  | 65 |                 ON {$filesalias}.contextid = " . SYSCONTEXTID . "
 | 
        
           |  |  | 66 |                AND {$filesalias}.component = 'blog'
 | 
        
           |  |  | 67 |                AND {$filesalias}.filearea = 'attachment'
 | 
        
           |  |  | 68 |                AND {$filesalias}.itemid = {$postalias}.id
 | 
        
           |  |  | 69 |                AND {$filesalias}.filename != '.'"));
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 71 |         // Join the tag entity.
 | 
        
           |  |  | 72 |         $tagentity = (new tag())
 | 
        
           |  |  | 73 |             ->set_entity_title(new lang_string('blogtags', 'core_blog'))
 | 
        
           |  |  | 74 |             ->set_table_alias('tag', $blogentity->get_table_alias('tag'));
 | 
        
           |  |  | 75 |         $this->add_entity($tagentity
 | 
        
           |  |  | 76 |             ->add_joins($blogentity->get_tag_joins()));
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |         // Join the user entity to represent the blog author.
 | 
        
           |  |  | 79 |         $authorentity = (new user())
 | 
        
           |  |  | 80 |             ->set_entity_title(new lang_string('author', 'core_blog'));
 | 
        
           |  |  | 81 |         $authoralias = $authorentity->get_table_alias('user');
 | 
        
           |  |  | 82 |         $this->add_entity($authorentity
 | 
        
           |  |  | 83 |             ->add_join("LEFT JOIN {user} {$authoralias} ON {$authoralias}.id = {$postalias}.userid"));
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 |         // Join the course entity for course blogs.
 | 
        
           |  |  | 86 |         $courseentity = new course();
 | 
        
           |  |  | 87 |         $coursealias = $courseentity->get_table_alias('course');
 | 
        
           |  |  | 88 |         $this->add_entity($courseentity
 | 
        
           |  |  | 89 |             ->add_join("LEFT JOIN {course} {$coursealias} ON {$coursealias}.id = {$postalias}.courseid"));
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |         // Join the comment entity.
 | 
        
           |  |  | 92 |         $commententity = new comment();
 | 
        
           |  |  | 93 |         $commentalias = $commententity->get_table_alias('comments');
 | 
        
           |  |  | 94 |         $this->add_entity($commententity
 | 
        
           |  |  | 95 |             ->add_join("LEFT JOIN {comments} {$commentalias} ON {$commentalias}.component = 'blog'
 | 
        
           |  |  | 96 |                 AND {$commentalias}.itemid = {$postalias}.id"));
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 |         // Join the user entity to represent the comment author.
 | 
        
           |  |  | 99 |         $commenterentity = (new user())
 | 
        
           |  |  | 100 |             ->set_entity_name('commenter')
 | 
        
           |  |  | 101 |             ->set_entity_title(new lang_string('commenter', 'core_comment'));
 | 
        
           |  |  | 102 |         $commenteralias = $commenterentity->get_table_alias('user');
 | 
        
           |  |  | 103 |         $this->add_entity($commenterentity
 | 
        
           |  |  | 104 |             ->add_joins($commententity->get_joins())
 | 
        
           |  |  | 105 |             ->add_join("LEFT JOIN {user} {$commenteralias} ON {$commenteralias}.id = {$commentalias}.userid"));
 | 
        
           |  |  | 106 |   | 
        
           |  |  | 107 |         // Add report elements from each of the entities we added to the report.
 | 
        
           |  |  | 108 |         $this->add_all_from_entity($blogentity->get_entity_name());
 | 
        
           |  |  | 109 |   | 
        
           |  |  | 110 |         // Add specific file/tag entity elements.
 | 
        
           |  |  | 111 |         $this->add_columns_from_entity($fileentity->get_entity_name(), ['name', 'size', 'type', 'timecreated']);
 | 
        
           |  |  | 112 |         $this->add_filters_from_entity($fileentity->get_entity_name(), ['name', 'size', 'timecreated']);
 | 
        
           |  |  | 113 |         $this->add_conditions_from_entity($fileentity->get_entity_name(), ['name', 'size', 'timecreated']);
 | 
        
           |  |  | 114 |   | 
        
           |  |  | 115 |         $this->add_columns_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink']);
 | 
        
           |  |  | 116 |         $this->add_filter($tagentity->get_filter('name'));
 | 
        
           |  |  | 117 |         $this->add_condition($tagentity->get_condition('name'));
 | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 |         $this->add_all_from_entity($authorentity->get_entity_name());
 | 
        
           |  |  | 120 |         $this->add_all_from_entity($courseentity->get_entity_name());
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 |         // Add specific comment entity elements.
 | 
        
           |  |  | 123 |         $this->add_columns_from_entity($commententity->get_entity_name(), ['content', 'timecreated']);
 | 
        
           |  |  | 124 |         $this->add_filter($commententity->get_filter('timecreated'));
 | 
        
           |  |  | 125 |         $this->add_condition($commententity->get_filter('timecreated'));
 | 
        
           |  |  | 126 |   | 
        
           |  |  | 127 |         $this->add_all_from_entity($commenterentity->get_entity_name());
 | 
        
           |  |  | 128 |     }
 | 
        
           |  |  | 129 |   | 
        
           |  |  | 130 |     /**
 | 
        
           |  |  | 131 |      * Return the columns that will be added to the report upon creation
 | 
        
           |  |  | 132 |      *
 | 
        
           |  |  | 133 |      * @return string[]
 | 
        
           |  |  | 134 |      */
 | 
        
           |  |  | 135 |     public function get_default_columns(): array {
 | 
        
           |  |  | 136 |         return [
 | 
        
           |  |  | 137 |             'user:fullname',
 | 
        
           |  |  | 138 |             'course:fullname',
 | 
        
           |  |  | 139 |             'blog:title',
 | 
        
           |  |  | 140 |             'blog:timecreated',
 | 
        
           |  |  | 141 |         ];
 | 
        
           |  |  | 142 |     }
 | 
        
           |  |  | 143 |   | 
        
           |  |  | 144 |     /**
 | 
        
           |  |  | 145 |      * Return the column sorting that will be added to the report upon creation
 | 
        
           |  |  | 146 |      *
 | 
        
           |  |  | 147 |      * @return int[]
 | 
        
           |  |  | 148 |      */
 | 
        
           |  |  | 149 |     public function get_default_column_sorting(): array {
 | 
        
           |  |  | 150 |         return [
 | 
        
           |  |  | 151 |             'user:fullname' => SORT_ASC,
 | 
        
           |  |  | 152 |             'blog:timecreated' => SORT_ASC,
 | 
        
           |  |  | 153 |         ];
 | 
        
           |  |  | 154 |     }
 | 
        
           |  |  | 155 |   | 
        
           |  |  | 156 |     /**
 | 
        
           |  |  | 157 |      * Return the filters that will be added to the report upon creation
 | 
        
           |  |  | 158 |      *
 | 
        
           |  |  | 159 |      * @return string[]
 | 
        
           |  |  | 160 |      */
 | 
        
           |  |  | 161 |     public function get_default_filters(): array {
 | 
        
           |  |  | 162 |         return [
 | 
        
           |  |  | 163 |             'user:fullname',
 | 
        
           |  |  | 164 |             'blog:title',
 | 
        
           |  |  | 165 |             'blog:timecreated',
 | 
        
           |  |  | 166 |         ];
 | 
        
           |  |  | 167 |     }
 | 
        
           |  |  | 168 |   | 
        
           |  |  | 169 |     /**
 | 
        
           |  |  | 170 |      * Return the conditions that will be added to the report upon creation
 | 
        
           |  |  | 171 |      *
 | 
        
           |  |  | 172 |      * @return string[]
 | 
        
           |  |  | 173 |      */
 | 
        
           |  |  | 174 |     public function get_default_conditions(): array {
 | 
        
           |  |  | 175 |         return [
 | 
        
           |  |  | 176 |             'blog:publishstate',
 | 
        
           |  |  | 177 |         ];
 | 
        
           |  |  | 178 |     }
 | 
        
           |  |  | 179 | }
 |