96 |
efrain |
1 |
<?php
|
|
|
2 |
|
117 |
ariadna |
3 |
class block_cesa_course_rating extends block_base
|
|
|
4 |
{
|
96 |
efrain |
5 |
|
|
|
6 |
|
117 |
ariadna |
7 |
public function init()
|
|
|
8 |
{
|
|
|
9 |
$this->title = get_string('pluginname', 'block_cesa_course_rating');
|
|
|
10 |
}
|
96 |
efrain |
11 |
|
117 |
ariadna |
12 |
public function applicable_formats()
|
|
|
13 |
{
|
|
|
14 |
|
|
|
15 |
return [
|
501 |
ariadna |
16 |
'all' => true
|
117 |
ariadna |
17 |
];
|
|
|
18 |
}
|
|
|
19 |
|
|
|
20 |
function instance_allow_multiple()
|
|
|
21 |
{
|
|
|
22 |
return false;
|
|
|
23 |
}
|
96 |
efrain |
24 |
|
117 |
ariadna |
25 |
public function html_attributes()
|
|
|
26 |
{
|
|
|
27 |
global $CFG;
|
|
|
28 |
$attributes = parent::html_attributes();
|
|
|
29 |
|
|
|
30 |
/*
|
96 |
efrain |
31 |
$ccn_mt = '';
|
|
|
32 |
$ccn_mb = '';
|
|
|
33 |
$ccn_pt = '';
|
|
|
34 |
$ccn_pb = '';
|
|
|
35 |
$ccn_css_class = '';
|
|
|
36 |
|
|
|
37 |
if(!empty($this->config->ccn_margin_top)){
|
|
|
38 |
if($this->config->ccn_margin_top == '0') {
|
|
|
39 |
$ccn_mt = '';
|
|
|
40 |
} elseif($this->config->ccn_margin_top == 'zero') {
|
|
|
41 |
$ccn_mt = 'margin-top:0px;';
|
|
|
42 |
} else {
|
|
|
43 |
$ccn_mt = 'margin-top:'.$this->config->ccn_margin_top.'px;';
|
|
|
44 |
}
|
|
|
45 |
}
|
|
|
46 |
if(!empty($this->config->ccn_margin_bottom)){
|
|
|
47 |
$ccn_mb = 'margin-bottom:'.$this->config->ccn_margin_bottom.'px;';
|
|
|
48 |
} else {
|
|
|
49 |
$ccn_mb = '';
|
|
|
50 |
}
|
|
|
51 |
if(!empty($this->config->ccn_padding_top)){
|
|
|
52 |
$ccn_pt = 'padding-top:'.$this->config->ccn_padding_top.'px;';
|
|
|
53 |
} else {
|
|
|
54 |
$ccn_pt = '';
|
|
|
55 |
}
|
|
|
56 |
if(!empty($this->config->ccn_padding_bottom)){
|
|
|
57 |
$ccn_pb = 'padding-bottom:'.$this->config->ccn_padding_bottom.'px;';
|
|
|
58 |
} else {
|
|
|
59 |
$ccn_pb = '';
|
|
|
60 |
}
|
|
|
61 |
if(!empty($this->config->ccn_css_class)){
|
|
|
62 |
$ccn_css_class = $this->config->ccn_css_class;
|
|
|
63 |
} else {
|
|
|
64 |
$ccn_css_class = '';
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
$ccn_stylize = $ccn_mt . $ccn_mb . $ccn_pt . $ccn_pb;
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
$attributes['ccn_style'] = $ccn_stylize;
|
|
|
75 |
// $attributes['class'] .= ' block_'. $this->name();
|
|
|
76 |
$attributes['class'] .= ' '. $ccn_css_class;
|
|
|
77 |
*/
|
|
|
78 |
|
|
|
79 |
|
117 |
ariadna |
80 |
return $attributes;
|
|
|
81 |
}
|
96 |
efrain |
82 |
|
117 |
ariadna |
83 |
public function has_config()
|
|
|
84 |
{
|
|
|
85 |
return true;
|
|
|
86 |
}
|
96 |
efrain |
87 |
|
117 |
ariadna |
88 |
public function get_content()
|
|
|
89 |
{
|
|
|
90 |
global $CFG, $COURSE;
|
96 |
efrain |
91 |
|
117 |
ariadna |
92 |
if ($this->content !== null) {
|
|
|
93 |
// return $this->content;
|
|
|
94 |
}
|
96 |
efrain |
95 |
|
117 |
ariadna |
96 |
$this->content = new stdClass;
|
96 |
efrain |
97 |
|
117 |
ariadna |
98 |
if (!empty($this->config->title)) {
|
|
|
99 |
$this->content->title = format_text($this->config->title, FORMAT_HTML, array('filter' => true));
|
|
|
100 |
} else {
|
|
|
101 |
$this->content->title = get_string('pluginname', 'block_cesa_course_rating');
|
|
|
102 |
}
|
96 |
efrain |
103 |
|
117 |
ariadna |
104 |
$courseid = $COURSE->id;
|
|
|
105 |
$context = get_context_instance(CONTEXT_COURSE, $courseid);
|
96 |
efrain |
106 |
|
117 |
ariadna |
107 |
$canRate = has_capability('block/cesa_course_rating:rate', $context);
|
|
|
108 |
if ($canRate == 1) {
|
|
|
109 |
$canRateClass = 'ccn-can-rate';
|
|
|
110 |
} else {
|
|
|
111 |
$canRateClass = 'ccn-cannot-rate';
|
|
|
112 |
}
|
96 |
efrain |
113 |
|
117 |
ariadna |
114 |
$ccnSubmitRating = $this->submit_rating();
|
|
|
115 |
$this->content->text = '';
|
96 |
efrain |
116 |
|
117 |
ariadna |
117 |
$ccnRating = number_format($this->overall_rating($COURSE->id), 1);
|
96 |
efrain |
118 |
|
117 |
ariadna |
119 |
$ccnStar = '<li class="list-inline-item"><i class="fa fa-star"></i></li>';
|
|
|
120 |
$ccnStarHalf = '<li class="list-inline-item"><i class="fa fa-star-half-o"></i></li>';
|
|
|
121 |
$ccnStarVoid = '<li class="list-inline-item"><i class="fa fa-star-o"></i></li>';
|
96 |
efrain |
122 |
|
117 |
ariadna |
123 |
if ($ccnRating == 5) {
|
|
|
124 |
$ccnStars = str_repeat($ccnStar, 5);
|
|
|
125 |
} elseif ($ccnRating == 4.5) {
|
|
|
126 |
$ccnStars = str_repeat($ccnStar, 4) . str_repeat($ccnStarHalf, 1);
|
|
|
127 |
} elseif ($ccnRating == 4) {
|
|
|
128 |
$ccnStars = str_repeat($ccnStar, 4) . str_repeat($ccnStarVoid, 1);
|
|
|
129 |
} elseif ($ccnRating == 3.5) {
|
|
|
130 |
$ccnStars = str_repeat($ccnStar, 3) . str_repeat($ccnStarHalf, 1) . str_repeat($ccnStarVoid, 1);
|
|
|
131 |
} elseif ($ccnRating == 3) {
|
|
|
132 |
$ccnStars = str_repeat($ccnStar, 3) . str_repeat($ccnStarVoid, 2);
|
|
|
133 |
} elseif ($ccnRating == 2.5) {
|
|
|
134 |
$ccnStars = str_repeat($ccnStar, 2) . str_repeat($ccnStarHalf, 1) . str_repeat($ccnStarVoid, 2);
|
|
|
135 |
} elseif ($ccnRating == 2) {
|
|
|
136 |
$ccnStars = str_repeat($ccnStar, 2) . str_repeat($ccnStarVoid, 3);
|
|
|
137 |
} elseif ($ccnRating == 1.5) {
|
|
|
138 |
$ccnStars = str_repeat($ccnStar, 1) . str_repeat($ccnStarHalf, 1) . str_repeat($ccnStarVoid, 3);
|
|
|
139 |
} elseif ($ccnRating == 0.5) {
|
|
|
140 |
$ccnStars = str_repeat($ccnStarHalf, 1) . str_repeat($ccnStarVoid, 4);
|
|
|
141 |
} else {
|
|
|
142 |
$ccnStars = str_repeat($ccnStarVoid, 5);
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
$ccnFive = $this->get_specific_average($COURSE->id, 5);
|
|
|
146 |
$ccnFour = $this->get_specific_average($COURSE->id, 4);
|
|
|
147 |
$ccnThree = $this->get_specific_average($COURSE->id, 3);
|
|
|
148 |
$ccnTwo = $this->get_specific_average($COURSE->id, 2);
|
|
|
149 |
$ccnOne = $this->get_specific_average($COURSE->id, 1);
|
|
|
150 |
|
|
|
151 |
$this->content->text .= '
|
96 |
efrain |
152 |
<div class="cs_row_five">
|
|
|
153 |
<div class="student_feedback_container">
|
117 |
ariadna |
154 |
<h4 data-ccn="title" class="aii_title">' . $this->content->title . '</h4>
|
96 |
efrain |
155 |
<div class="s_feeback_content">
|
|
|
156 |
<ul class="skills">
|
117 |
ariadna |
157 |
<li class="list-inline-item">' . get_string('stars_5', 'block_cesa_course_rating') . '</li>
|
|
|
158 |
<li class="list-inline-item progressbar1" data-width="' . $ccnFive . '" data-target="100">' . $ccnFive . '%</li>
|
96 |
efrain |
159 |
</ul>
|
|
|
160 |
<ul class="skills">
|
117 |
ariadna |
161 |
<li class="list-inline-item">' . get_string('stars_4', 'block_cesa_course_rating') . '</li>
|
|
|
162 |
<li class="list-inline-item progressbar2" data-width="' . $ccnFour . '" data-target="100">' . $ccnFour . '%</li>
|
96 |
efrain |
163 |
</ul>
|
|
|
164 |
<ul class="skills">
|
117 |
ariadna |
165 |
<li class="list-inline-item">' . get_string('stars_3', 'block_cesa_course_rating') . '</li>
|
|
|
166 |
<li class="list-inline-item progressbar3" data-width="' . $ccnThree . '" data-target="100">' . $ccnThree . '%</li>
|
96 |
efrain |
167 |
</ul>
|
|
|
168 |
<ul class="skills">
|
117 |
ariadna |
169 |
<li class="list-inline-item">' . get_string('stars_2', 'block_cesa_course_rating') . '</li>
|
|
|
170 |
<li class="list-inline-item progressbar4" data-width="' . $ccnTwo . '" data-target="100">' . $ccnTwo . '%</li>
|
96 |
efrain |
171 |
</ul>
|
|
|
172 |
<ul class="skills">
|
117 |
ariadna |
173 |
<li class="list-inline-item">' . get_string('stars_1', 'block_cesa_course_rating') . '</li>
|
|
|
174 |
<li class="list-inline-item progressbar5" data-width="' . $ccnOne . '" data-target="100">' . $ccnOne . '%</li>
|
96 |
efrain |
175 |
</ul>
|
|
|
176 |
</div>
|
117 |
ariadna |
177 |
<div class="aii_average_review text-center ' . $canRateClass . '">
|
96 |
efrain |
178 |
<div class="av_content">
|
117 |
ariadna |
179 |
<h2>' . $ccnRating . '</h2>
|
96 |
efrain |
180 |
<ul class="aii_rive_list mb0">
|
117 |
ariadna |
181 |
' . $ccnStars . '
|
96 |
efrain |
182 |
</ul>
|
117 |
ariadna |
183 |
<p>' . $this->count_ratings($COURSE->id) . '</p>';
|
|
|
184 |
if ($canRate == 1) {
|
|
|
185 |
$this->content->text .= $ccnSubmitRating;
|
|
|
186 |
}
|
|
|
187 |
$this->content->text .= '
|
96 |
efrain |
188 |
</div>
|
|
|
189 |
</div>
|
|
|
190 |
</div>
|
|
|
191 |
</div>';
|
117 |
ariadna |
192 |
return $this->content;
|
|
|
193 |
}
|
96 |
efrain |
194 |
|
117 |
ariadna |
195 |
public function overall_rating($courseID)
|
|
|
196 |
{
|
|
|
197 |
global $CFG, $DB;
|
|
|
198 |
$sql = " SELECT AVG(rating) AS average
|
96 |
efrain |
199 |
FROM {block_cesa_course_rating}
|
|
|
200 |
WHERE course = $courseID
|
|
|
201 |
";
|
117 |
ariadna |
202 |
$totalAverage = -1;
|
|
|
203 |
if ($getAverage = $DB->get_record_sql($sql)) {
|
|
|
204 |
$totalAverage = round($getAverage->average * 2) / 2;
|
96 |
efrain |
205 |
}
|
117 |
ariadna |
206 |
return $totalAverage;
|
|
|
207 |
}
|
96 |
efrain |
208 |
|
117 |
ariadna |
209 |
public function count_ratings($courseID)
|
|
|
210 |
{
|
|
|
211 |
global $CFG, $DB;
|
|
|
212 |
$countRecords = $DB->count_records('block_cesa_course_rating', array('course' => $courseID));
|
|
|
213 |
$countRatings = '';
|
|
|
214 |
if ($countRecords > 0) {
|
|
|
215 |
$countRatings = get_string('rated_by', 'block_cesa_course_rating', $countRecords);
|
|
|
216 |
} else {
|
|
|
217 |
$countRatings = get_string('rated_by_none', 'block_cesa_course_rating');
|
96 |
efrain |
218 |
}
|
117 |
ariadna |
219 |
return $countRatings;
|
|
|
220 |
}
|
|
|
221 |
public function count_ratings_external($courseID)
|
|
|
222 |
{
|
|
|
223 |
global $CFG, $DB;
|
|
|
224 |
$countRecords = $DB->count_records('block_cesa_course_rating', array('course' => $courseID));
|
|
|
225 |
return $countRecords;
|
|
|
226 |
}
|
96 |
efrain |
227 |
|
117 |
ariadna |
228 |
public function get_specific_average($courseID, $rating)
|
|
|
229 |
{
|
|
|
230 |
global $CFG, $DB;
|
|
|
231 |
$countOnlyRating = $DB->count_records('block_cesa_course_rating', array('course' => $courseID, 'rating' => $rating));
|
|
|
232 |
$countExcludingRating = $DB->count_records_sql(
|
|
|
233 |
" SELECT COUNT(*)
|
96 |
efrain |
234 |
FROM {block_cesa_course_rating}
|
|
|
235 |
WHERE course = $courseID
|
|
|
236 |
AND rating <> $rating
|
117 |
ariadna |
237 |
"
|
|
|
238 |
);
|
96 |
efrain |
239 |
|
117 |
ariadna |
240 |
$countTotal = $DB->count_records('block_cesa_course_rating', array('course' => $courseID));
|
96 |
efrain |
241 |
|
117 |
ariadna |
242 |
if ($countTotal == 0) {
|
|
|
243 |
$result = '0';
|
|
|
244 |
} else {
|
|
|
245 |
$result = $countOnlyRating / $countTotal * 100;
|
96 |
efrain |
246 |
}
|
117 |
ariadna |
247 |
return $result;
|
|
|
248 |
}
|
96 |
efrain |
249 |
|
117 |
ariadna |
250 |
public function submit_rating()
|
|
|
251 |
{
|
96 |
efrain |
252 |
|
117 |
ariadna |
253 |
global $CFG, $COURSE;
|
96 |
efrain |
254 |
|
117 |
ariadna |
255 |
$courseid = $COURSE->id;
|
|
|
256 |
$context = get_context_instance(CONTEXT_COURSE, $courseid);
|
96 |
efrain |
257 |
|
117 |
ariadna |
258 |
$ccnStar = '<span class="fa fa-star"></span>';
|
|
|
259 |
$return = '<form id="ccn-star-rate" method="post" action="' . $CFG->wwwroot . '/blocks/cesa_course_rating/rate_course.php">
|
|
|
260 |
<input name="id" type="hidden" value="' . $courseid . '" />
|
96 |
efrain |
261 |
<div class="ccn-star-rate-inner">';
|
117 |
ariadna |
262 |
for ($i = 5; $i >= 1; $i--) {
|
|
|
263 |
$printCcnStar = str_repeat($ccnStar, $i);
|
|
|
264 |
$return .= ' <input required type="radio" id="stars-' . $i . '" name="rating" value="' . $i . '" /><label for="stars-' . $i . '"></label>';
|
|
|
265 |
}
|
|
|
266 |
$return .= ' </div>
|
|
|
267 |
<button class="btn btn-primary" type="submit">' . get_string('rate_course', 'block_cesa_course_rating') . '</button>
|
96 |
efrain |
268 |
</form>';
|
117 |
ariadna |
269 |
return $return;
|
|
|
270 |
}
|
96 |
efrain |
271 |
|
117 |
ariadna |
272 |
public function external_star_rating($courseID)
|
|
|
273 |
{
|
96 |
efrain |
274 |
|
117 |
ariadna |
275 |
$ccnStar = '<li class="list-inline-item"><i class="fa fa-star"></i></li>';
|
|
|
276 |
$ccnStarHalf = '<li class="list-inline-item"><i class="fa fa-star-half-o"></i></li>';
|
|
|
277 |
$ccnStarVoid = '<li class="list-inline-item"><i class="fa fa-star-o"></i></li>';
|
|
|
278 |
$ccnRating = $this->overall_rating($courseID);
|
96 |
efrain |
279 |
|
117 |
ariadna |
280 |
if ($ccnRating == 5) {
|
|
|
281 |
$ccnStars = str_repeat($ccnStar, 5);
|
|
|
282 |
} elseif ($ccnRating == 4.5) {
|
|
|
283 |
$ccnStars = str_repeat($ccnStar, 4) . str_repeat($ccnStarHalf, 1);
|
|
|
284 |
} elseif ($ccnRating == 4) {
|
|
|
285 |
$ccnStars = str_repeat($ccnStar, 4) . str_repeat($ccnStarVoid, 1);
|
|
|
286 |
} elseif ($ccnRating == 3.5) {
|
|
|
287 |
$ccnStars = str_repeat($ccnStar, 3) . str_repeat($ccnStarHalf, 1) . str_repeat($ccnStarVoid, 1);
|
|
|
288 |
} elseif ($ccnRating == 3) {
|
|
|
289 |
$ccnStars = str_repeat($ccnStar, 3) . str_repeat($ccnStarVoid, 2);
|
|
|
290 |
} elseif ($ccnRating == 2.5) {
|
|
|
291 |
$ccnStars = str_repeat($ccnStar, 2) . str_repeat($ccnStarHalf, 1) . str_repeat($ccnStarVoid, 2);
|
|
|
292 |
} elseif ($ccnRating == 2) {
|
|
|
293 |
$ccnStars = str_repeat($ccnStar, 2) . str_repeat($ccnStarVoid, 3);
|
|
|
294 |
} elseif ($ccnRating == 1.5) {
|
|
|
295 |
$ccnStars = str_repeat($ccnStar, 1) . str_repeat($ccnStarHalf, 1) . str_repeat($ccnStarVoid, 3);
|
|
|
296 |
} elseif ($ccnRating == 0.5) {
|
|
|
297 |
$ccnStars = str_repeat($ccnStarHalf, 1) . str_repeat($ccnStarVoid, 4);
|
|
|
298 |
} else {
|
|
|
299 |
$ccnStars = str_repeat($ccnStarVoid, 5);
|
96 |
efrain |
300 |
}
|
|
|
301 |
|
117 |
ariadna |
302 |
$return = '<div class="ccn-external-stars">' . $ccnStars . '<li class="list-inline-item"><span>(' . $this->count_ratings_external($courseID) . ')</span></li></div>';
|
|
|
303 |
return $return;
|
|
|
304 |
}
|
96 |
efrain |
305 |
}
|