| 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 | namespace tool_courserating\external;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | use core\external\persistent_exporter;
 | 
        
           |  |  | 20 | use core_user\external\user_summary_exporter;
 | 
        
           |  |  | 21 | use tool_courserating\api;
 | 
        
           |  |  | 22 | use tool_courserating\constants;
 | 
        
           |  |  | 23 | use tool_courserating\helper;
 | 
        
           |  |  | 24 | use tool_courserating\local\models\flag;
 | 
        
           |  |  | 25 | use tool_courserating\local\models\rating;
 | 
        
           |  |  | 26 | use tool_courserating\output\renderer;
 | 
        
           |  |  | 27 | use tool_courserating\permission;
 | 
        
           |  |  | 28 | use tool_dataprivacy\category;
 | 
        
           |  |  | 29 | use tool_dataprivacy\context_instance;
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | /**
 | 
        
           |  |  | 32 |  * Class for exporting field data.
 | 
        
           |  |  | 33 |  *
 | 
        
           |  |  | 34 |  * @package    tool_courserating
 | 
        
           |  |  | 35 |  * @copyright  2022 Marina Glancy <marina.glancy@gmail.com>
 | 
        
           |  |  | 36 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 37 |  */
 | 
        
           |  |  | 38 | class rating_exporter extends persistent_exporter {
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 |     /**
 | 
        
           |  |  | 41 |      * Defines the persistent class.
 | 
        
           |  |  | 42 |      *
 | 
        
           |  |  | 43 |      * @return string
 | 
        
           |  |  | 44 |      */
 | 
        
           |  |  | 45 |     protected static function define_class() {
 | 
        
           |  |  | 46 |         return rating::class;
 | 
        
           |  |  | 47 |     }
 | 
        
           |  |  | 48 |   | 
        
           |  |  | 49 |     /**
 | 
        
           |  |  | 50 |      * Returns a list of objects that are related.
 | 
        
           |  |  | 51 |      *
 | 
        
           |  |  | 52 |      * @return array
 | 
        
           |  |  | 53 |      */
 | 
        
           |  |  | 54 |     protected static function define_related() {
 | 
        
           |  |  | 55 |         return [
 | 
        
           |  |  | 56 |             'context' => 'context?',
 | 
        
           |  |  | 57 |         ];
 | 
        
           |  |  | 58 |     }
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |     /**
 | 
        
           |  |  | 61 |      * Get the formatting parameters for the review field.
 | 
        
           |  |  | 62 |      *
 | 
        
           |  |  | 63 |      * @return array
 | 
        
           |  |  | 64 |      */
 | 
        
           |  |  | 65 |     protected function get_format_parameters_for_review() {
 | 
        
           |  |  | 66 |         return [
 | 
        
           |  |  | 67 |             'component' => 'tool_courserating',
 | 
        
           |  |  | 68 |             'filearea' => 'review',
 | 
        
           |  |  | 69 |             'itemid' => $this->data->id,
 | 
        
           |  |  | 70 |             'context' => \context_course::instance($this->data->courseid),
 | 
        
           |  |  | 71 |         ];
 | 
        
           |  |  | 72 |     }
 | 
        
           |  |  | 73 |   | 
        
           |  |  | 74 |     /**
 | 
        
           |  |  | 75 |      * Return a list of additional properties used only for display
 | 
        
           |  |  | 76 |      *
 | 
        
           |  |  | 77 |      * @return array
 | 
        
           |  |  | 78 |      */
 | 
        
           |  |  | 79 |     protected static function define_other_properties(): array {
 | 
        
           |  |  | 80 |         return [
 | 
        
           |  |  | 81 |             'user' => ['type' => user_summary_exporter::read_properties_definition(), 'optional' => true],
 | 
        
           |  |  | 82 |             'reviewtext' => ['type' => PARAM_RAW],
 | 
        
           |  |  | 83 |             'reviewstars' => ['type' => stars_exporter::read_properties_definition()],
 | 
        
           |  |  | 84 |             'reviewdate' => ['type' => PARAM_RAW],
 | 
        
           |  |  | 85 |             'ratingflag' => [
 | 
        
           |  |  | 86 |                 'type' => 'array',
 | 
        
           |  |  | 87 |                 'optional' => true,
 | 
        
           |  |  | 88 |             ],
 | 
        
           |  |  | 89 |         ];
 | 
        
           |  |  | 90 |     }
 | 
        
           |  |  | 91 |   | 
        
           |  |  | 92 |     /**
 | 
        
           |  |  | 93 |      * Get additional values to inject while exporting
 | 
        
           |  |  | 94 |      *
 | 
        
           |  |  | 95 |      * @param \renderer_base $output
 | 
        
           |  |  | 96 |      * @return array
 | 
        
           |  |  | 97 |      */
 | 
        
           |  |  | 98 |     protected function get_other_values(\renderer_base $output): array {
 | 
        
           |  |  | 99 |         global $USER;
 | 
        
           |  |  | 100 |         $result = [];
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 |         if ($user = \core_user::get_user($this->data->userid)) {
 | 
        
           |  |  | 103 |             $userexporter = new user_summary_exporter($user);
 | 
        
           |  |  | 104 |             $result['user'] = $userexporter->export($output);
 | 
        
           |  |  | 105 |         } else {
 | 
        
           |  |  | 106 |             $result['user'] = [];
 | 
        
           |  |  | 107 |         }
 | 
        
           |  |  | 108 |   | 
        
           |  |  | 109 |         $result['reviewtext'] = helper::format_review($this->data->review, $this->data);
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 |         $result['reviewstars'] = (new stars_exporter($this->data->rating))->export($output);
 | 
        
           |  |  | 112 |   | 
        
           |  |  | 113 |         $result['reviewdate'] = helper::format_date($this->data->timemodified);
 | 
        
           |  |  | 114 |   | 
        
           |  |  | 115 |         if (permission::can_delete_rating($this->data->id, $this->data->courseid)) {
 | 
        
           |  |  | 116 |             $flags = flag::count_records(['ratingid' => $this->data->id]);
 | 
        
           |  |  | 117 |         } else {
 | 
        
           |  |  | 118 |             $flags = [];
 | 
        
           |  |  | 119 |         }
 | 
        
           |  |  | 120 |         $flagged = flag::get_records(['ratingid' => $this->data->id, 'userid' => $USER->id]) ? true : false;
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 |         $result['ratingflag'] = (object)[
 | 
        
           |  |  | 123 |             'flagged' => $flagged,
 | 
        
           |  |  | 124 |             'toggleflag' => api::get_flag_inplace_editable($this->data->id, $flagged)->export_for_template($output),
 | 
        
           |  |  | 125 |             'flags' => $flags,
 | 
        
           |  |  | 126 |             'candelete' => $flags > 0,
 | 
        
           |  |  | 127 |             'ratingid' => $this->data->id,
 | 
        
           |  |  | 128 |         ];
 | 
        
           |  |  | 129 |   | 
        
           |  |  | 130 |         return $result;
 | 
        
           |  |  | 131 |     }
 | 
        
           |  |  | 132 | }
 |