| 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 |
|
|
|
22 |
/**
|
|
|
23 |
* Exporter for 5-star rating
|
|
|
24 |
*
|
|
|
25 |
* @package tool_courserating
|
|
|
26 |
* @copyright 2022 Marina Glancy <marina.glancy@gmail.com>
|
|
|
27 |
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
class stars_exporter extends exporter {
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Constructor.
|
|
|
33 |
*
|
|
|
34 |
* @param float|null $rating
|
|
|
35 |
*/
|
|
|
36 |
public function __construct(?float $rating) {
|
|
|
37 |
parent::__construct([], ['rating' => $rating]);
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Return the list of properties.
|
|
|
42 |
*
|
|
|
43 |
* @return array
|
|
|
44 |
*/
|
|
|
45 |
protected static function define_related() {
|
|
|
46 |
return [
|
|
|
47 |
'rating' => PARAM_FLOAT,
|
|
|
48 |
];
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Return the list of additional properties used only for display.
|
|
|
53 |
*
|
|
|
54 |
* @return array - Keys with their types.
|
|
|
55 |
*/
|
|
|
56 |
protected static function define_other_properties() {
|
|
|
57 |
return [
|
|
|
58 |
'staricons' => [
|
|
|
59 |
'type' => [
|
|
|
60 |
'key' => ['type' => PARAM_RAW],
|
|
|
61 |
'component' => ['type' => PARAM_RAW],
|
|
|
62 |
'title' => ['type' => PARAM_RAW],
|
|
|
63 |
],
|
|
|
64 |
'multiple' => true,
|
|
|
65 |
],
|
|
|
66 |
];
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* One star
|
|
|
71 |
*
|
|
|
72 |
* @param float $rating
|
|
|
73 |
* @return array
|
|
|
74 |
*/
|
|
|
75 |
protected function star(float $rating) {
|
|
|
76 |
if ($rating < 0.25) {
|
|
|
77 |
$icon = 'star-o';
|
|
|
78 |
} else if ($rating >= 0.75) {
|
|
|
79 |
$icon = 'star';
|
|
|
80 |
} else {
|
|
|
81 |
$icon = 'star-half';
|
|
|
82 |
}
|
|
|
83 |
return (new \pix_icon($icon, '', 'tool_courserating', []))
|
|
|
84 |
->export_for_pix();
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Get the additional values to inject while exporting.
|
|
|
89 |
*
|
|
|
90 |
* @param renderer_base $output The renderer.
|
|
|
91 |
* @return array Keys are the property names, values are their values.
|
|
|
92 |
*/
|
|
|
93 |
protected function get_other_values(renderer_base $output) {
|
|
|
94 |
$rating = $this->related['rating'];
|
|
|
95 |
$rating = round($rating * 20) / 20;
|
|
|
96 |
$icons = array_fill(0, floor($rating), $this->star(1));
|
|
|
97 |
$icons[] = $this->star($rating - floor($rating));
|
|
|
98 |
$icons = array_slice($icons, 0, 5);
|
|
|
99 |
$icons = array_merge($icons, array_fill(0, 5 - count($icons), $this->star(0)));
|
|
|
100 |
return [
|
|
|
101 |
'staricons' => $icons,
|
|
|
102 |
];
|
|
|
103 |
}
|
|
|
104 |
}
|