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