Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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\exporter;
20
use renderer_base;
21
use tool_courserating\constants;
22
use tool_courserating\local\models\rating;
23
 
24
/**
25
 * Exporter for the list of ratings (used in the rating summary popup)
26
 *
27
 * @package     tool_courserating
28
 * @copyright   2022 Marina Glancy <marina.glancy@gmail.com>
29
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class ratings_list_exporter extends exporter {
32
 
33
    /**
34
     * Constructor.
35
     *
36
     * @param array $related - related objects.
37
     */
38
    public function __construct($related) {
39
        parent::__construct([], $related);
40
    }
41
 
42
    /**
43
     * Return the list of properties.
44
     *
45
     * @return array
46
     */
47
    protected static function define_related() {
48
        return [
49
            'limit' => PARAM_INT . '?',
50
            'offset' => PARAM_INT . '?',
51
            'courseid' => PARAM_INT . '?',
52
            'showempty' => PARAM_BOOL . '?',
53
            'withrating' => PARAM_INT . '?',
54
        ];
55
    }
56
 
57
    /**
58
     * Return the list of additional properties used only for display.
59
     *
60
     * @return array - Keys with their types.
61
     */
62
    protected static function define_other_properties() {
63
        return [
64
            'ratings' => [
65
                'type' => rating_exporter::read_properties_definition(),
66
                'multiple' => true,
67
            ],
68
            'offset' => [
69
                'type' => PARAM_INT,
70
            ],
71
            'hasmore' => [
72
                'type' => PARAM_BOOL,
73
            ],
74
            'nextoffset' => [
75
                'type' => PARAM_INT,
76
            ],
77
            'courseid' => [
78
                'type' => PARAM_INT,
79
            ],
80
            'systemcontextid' => [
81
                'type' => PARAM_INT,
82
            ],
83
            'withrating' => [
84
                'type' => PARAM_INT,
85
            ],
86
        ];
87
    }
88
 
89
    /**
90
     * Get the additional values to inject while exporting.
91
     *
92
     * @param renderer_base $output The renderer.
93
     * @return array Keys are the property names, values are their values.
94
     */
95
    protected function get_other_values(renderer_base $output) {
96
        $courseid = $this->related['courseid'];
97
        $offset = $this->related['offset'] ?: 0;
98
        $limit = $this->related['limit'] ?: constants::REVIEWS_PER_PAGE;
99
        $withrating = $this->related['withrating'] ?: 0;
100
 
101
        $reviews = rating::get_records_select(
102
            'courseid = :courseid'.
103
            (empty($this->related['showempty']) ? ' AND hasreview = 1' : '').
104
            ($withrating ? ' AND rating = :rating' : ''),
105
            ['courseid' => $courseid, 'rating' => $withrating],
106
            'timemodified DESC, id DESC', '*', $offset, $limit + 1);
107
 
108
        $data = [
109
            'ratings' => [],
110
            'offset' => $offset,
111
            'hasmore' => count($reviews) > $limit,
112
            'nextoffset' => $offset + $limit,
113
            'courseid' => $courseid,
114
            'systemcontextid' => \context_system::instance()->id,
115
            'withrating' => $withrating,
116
        ];
117
 
118
        $reviews = array_slice(array_values($reviews), 0, $limit);
119
        foreach ($reviews as $review) {
120
            $data['ratings'][] = (new rating_exporter($review))->export($output);
121
        }
122
 
123
        return $data;
124
    }
125
}