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\helper;
23
use tool_courserating\local\models\summary;
24
use tool_courserating\permission;
25
 
26
/**
27
 * Exporter for rating summary (how many people gave 5 stars, etc)
28
 *
29
 * @package     tool_courserating
30
 * @copyright   2022 Marina Glancy <marina.glancy@gmail.com>
31
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class summary_exporter extends exporter {
34
 
35
    /** @var summary */
36
    protected $summary;
37
 
38
    /**
39
     * Constructor.
40
     *
41
     * @param int $courseid
42
     * @param summary|null $summary
43
     * @param bool $overridedisplayempty
44
     */
45
    public function __construct(int $courseid, ?summary $summary = null, bool $overridedisplayempty = false) {
46
        if (!$summary) {
47
            $records = summary::get_records(['courseid' => $courseid]);
48
            if (count($records)) {
49
                $summary = reset($records);
50
            } else {
51
                $summary = new summary(0, (object)['courseid' => $courseid]);
52
            }
53
        }
54
        $this->summary = $summary;
55
        parent::__construct([], ['overridedisplayempty' => $overridedisplayempty]);
56
    }
57
 
58
    /**
59
     * Return the list of properties.
60
     *
61
     * @return array
62
     */
63
    protected static function define_related() {
64
        return [
65
            'overridedisplayempty' => 'bool',
66
        ];
67
    }
68
 
69
    /**
70
     * Return the list of additional properties used only for display.
71
     *
72
     * @return array - Keys with their types.
73
     */
74
    protected static function define_other_properties() {
75
        return [
76
            'avgrating' => ['type' => PARAM_RAW],
77
            'stars' => ['type' => stars_exporter::read_properties_definition()],
78
            'lines' => [
79
                'type' => [
80
                    'rating' => ['type' => PARAM_INT],
81
                    'star' => ['type' => stars_exporter::read_properties_definition()],
82
                    'percent' => ['type' => PARAM_RAW],
83
                ],
84
                'multiple' => true,
85
            ],
86
            'courseid' => ['type' => PARAM_INT],
87
            'cntall' => ['type' => PARAM_INT],
88
            'displayempty' => ['type' => PARAM_INT],
89
        ];
90
    }
91
 
92
    /**
93
     * Get the additional values to inject while exporting.
94
     *
95
     * @param renderer_base $output The renderer.
96
     * @return array Keys are the property names, values are their values.
97
     */
98
    protected function get_other_values(renderer_base $output) {
99
        $summary = $this->summary;
100
        $courseid = $summary->get('courseid');
101
        $avgrating = $summary->get('cntall') ? $summary->get('avgrating') : 0;
102
        $data = [
103
            'avgrating' => $summary->get('cntall') ? sprintf("%.1f", $summary->get('avgrating')) : '-',
104
            'cntall' => $summary->get('cntall'),
105
            'stars' => (new stars_exporter($avgrating))->export($output),
106
            'lines' => [],
107
            'courseid' => $courseid,
108
            'displayempty' => !empty($this->related['overridedisplayempty'])
109
                || helper::get_setting(constants::SETTING_DISPLAYEMPTY),
110
        ];
111
        foreach ([5, 4, 3, 2, 1] as $line) {
112
            $percent = $summary->get('cntall') ? round(100 * $summary->get('cnt0' . $line) / $summary->get('cntall')) : 0;
113
            $data['lines'][] = [
114
                'rating' => $line,
115
                'star' => (new stars_exporter($line))->export($output),
116
                'percent' => $percent . '%',
117
            ];
118
        }
119
 
120
        return $data;
121
    }
122
}