-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
395 lines (310 loc) · 11 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') || die();
function block_course_rating_templates($page = 0, $perpage = 25)
{
global $DB;
$fields = "SELECT t.id, t.name, t.data";
$fields .= ", (SELECT count(*) FROM {course_rating_answers} a WHERE a.course_rating_templates_id = t.id) is_used";
$countfields = "SELECT COUNT(*)";
$sql = " FROM {course_rating_templates} t";
$total = $DB->count_records_sql($countfields . $sql);
$order = " ORDER BY t.timecreated";
$data = $DB->get_records_sql($fields . $sql . $order, [], $page * $perpage, $perpage);
return array('data' => $data, 'total' => $total);
}
function block_course_rating_all_templates()
{
return block_course_rating_templates(0, 0)['data'];
}
function block_course_rating_is_used($id)
{
global $DB;
$select = "SELECT COUNT(*)";
$sql = " FROM {course_rating_answers} t";
$where = " WHERE t.course_rating_templates_id = :id";
return $DB->count_records_sql($select . $sql . $where, ['id' => $id]);
}
function block_course_rating_is_student()
{
global $USER, $PAGE;
list($context, $course, $cm) = get_context_info_array($PAGE->context->id);
foreach (get_user_roles($context, $USER->id) as $role) {
if ($role->shortname == 'student') {
return true;
}
}
return false;
}
function block_course_rating_is_teacher()
{
global $USER, $PAGE;
list($context, $course, $cm) = get_context_info_array($PAGE->context->id);
foreach (get_user_roles($context, $USER->id) as $role) {
if ($role->shortname == 'student') {
return true;
}
}
return false;
}
function block_course_rating_get_questions($id)
{
global $DB;
$template = $DB->get_record('course_rating_templates', array('id' => $id), '*', MUST_EXIST);
return preg_split('@#@', $template->data, -1, PREG_SPLIT_NO_EMPTY);
}
function block_course_rating_get_answers($courseid, $templateid)
{
global $DB;
$sql = <<<SQL
select min(a.id) as id,
a.timecreated as timecreated,
a.usercreated as userid,
concat(u.lastname, ' ', u.firstname) as fio,
group_concat(a.user_answer order by a.order_question separator '#') answers,
f.feedback
from {course_rating_templates} t
join {course_rating_answers} a on a.course_rating_templates_id = t.id
left join {user} u on u.id = a.usercreated
left join {course_rating_feedback} f
on f.courseid = a.courseid and f.usercreated = a.usercreated
where t.id = :templateid
and a.courseid = :courseid
group by a.timecreated, a.usercreated, f.feedback
order by 2 desc
SQL;
$records = $DB->get_records_sql($sql, ['courseid' => $courseid, 'templateid' => $templateid]);
$data = [];
foreach ($records as $record) {
$line = [];
$line[] = date_format_string($record->timecreated, '%d.%m.%Y');
$line[] = date_format_string($record->timecreated, '%H:%M');
$line[] = $record->fio;
foreach (preg_split('@#@', $record->answers, -1, PREG_SPLIT_NO_EMPTY) as $answer) {
$line[] = $answer;
}
$line[] = $record->feedback;
$data[] = $line;
}
return $data;
}
function block_course_rating_get_answers_paging($courseid, $templateid, $page = 0, $offset = 25)
{
global $DB;
$sql = <<<SQL
select min(a.id) as id,
a.timecreated as timecreated,
a.usercreated as userid,
concat(u.lastname, ' ', u.firstname) as fio,
group_concat(a.user_answer order by a.order_question separator '#') answers,
f.feedback
from {course_rating_templates} t
join {course_rating_answers} a on a.course_rating_templates_id = t.id
left join {user} u on u.id = a.usercreated
left join {course_rating_feedback} f
on f.courseid = a.courseid and f.usercreated = a.usercreated
where t.id = :templateid
and a.courseid = :courseid
group by a.timecreated, a.usercreated, f.feedback
SQL;
$sqlCount = <<<SQL
select count(*) from (
$sql
) z
SQL;
$orderby = ' order by 2 desc';
$records = $DB->get_records_sql($sql . $orderby, ['courseid' => $courseid, 'templateid' => $templateid], $page, $offset);
return array(
'total' => $DB->get_record_sql($sqlCount, ['courseid' => $courseid, 'templateid' => $templateid]),
'data' => $records
);
}
function block_course_rating_get_rating($templateid, $id)
{
global $DB;
$sql = <<<SQL
select coalesce(avg(z.avg), 0) as avg, count(*) as cnt
from (
select t.usercreated, sum(t.user_answer) / count(*) as avg
from {course_rating_answers} t
where t.courseid = :id
and t.course_rating_templates_id = :templateid
group by t.usercreated) z
SQL;
$record = $DB->get_record_sql($sql, ['id' => $id, 'templateid' => $templateid]);
$result = new stdClass();
$result->value = round($record->avg, 2);
$result->percent = round($record->avg / 5 * 100, 2);
$result->count = $record->cnt;
return $result;
}
function block_course_rating_render($courseid, $showText = true)
{
$instance = block_course_rating_get_instance_block($courseid);
$template = block_course_rating_get_template_block($instance);
$rating = block_course_rating_get_rating($template, $courseid);
$title = get_string('rating', 'block_course_rating', $rating);
if ($showText) {
$margin = 15;
} else {
$margin = 0;
}
$content = <<<HTML
<style>
.showblockicons .block_course_rating.block .header .title h2:before {
content: '★';
font-size: 22px;
}
.course-ratings-css {
unicode-bidi: bidi-override;
color: #c5c5c5;
min-height: 32px;
height: auto;
width: 100%;
max-width: 220px;
margin: {$margin}px auto;
position: relative;
text-shadow: 0 1px 0 #a2a2a2;
}
.course-ratings-css::before {
content: '★★★★★';
opacity: .3;
}
.course-ratings-css::after {
color: gold;
content: '★★★★★';
text-shadow: 0 1px 0 #ab5414;
position: absolute;
z-index: 1;
display: block;
left: 0;
top:0;
overflow: hidden;
width: {$rating->percent}%;
}
.course-ratings-container {
width: 100%;
text-align: center;
}
</style>
<div class="course-ratings-container">
<div class="course-ratings-css"></div>
HTML;
if ($showText) {
$content .= <<<HTML
<p>$title</p>
HTML;
}
$content .= <<<HTML
</div>
<script>
const myObserver = new ResizeObserver(entries => {
// this will get called whenever div dimension changes
entries.forEach(entry => {
console.log('width', entry.contentRect.width);
console.log('height', entry.contentRect.height);
console.log(entry);
entry.target.style.fontSize = entry.contentRect.width / 4.2 + 'px';
entry.target.style.lineHeight = entry.contentRect.width / 4.2 + 'px';
});
});
const someEl = document.querySelector('.course-ratings-css');
// start listening to changes
myObserver.observe(someEl);
</script>
HTML;
return $content;
}
function block_course_rating_render_btn_vote($userid, $courseid, $returnurl)
{
global $OUTPUT;
$is_student = block_course_rating_is_student();
$is_voted = block_course_rating_is_voted($userid, $courseid);
if (!$is_student || $is_voted) {
return '';
}
$instance = block_course_rating_get_instance_block($courseid);
$template = block_course_rating_get_template_block($instance);
return html_writer::tag(
'div',
$OUTPUT->single_button(new moodle_url('/blocks/course_rating/vote.php', [
'templateid' => $template,
'courseid' => $courseid,
'returnurl' => $returnurl,
]), 'Голосовать'),
['style' => 'text-align: center']
);
}
function block_course_rating_render_btn_download($courseid, $returnurl)
{
global $OUTPUT;
$context = context_course::instance($courseid);
if (!has_capability('block/course_rating:download', $context)) {
return '';
}
$instance = block_course_rating_get_instance_block($courseid);
$template = block_course_rating_get_template_block($instance);
return html_writer::tag(
'div',
$OUTPUT->single_button(new moodle_url('/blocks/course_rating/export.php', [
'templateid' => $template,
'courseid' => $courseid,
'returnurl' => $returnurl,
]), 'Скачать результаты'),
['style' => 'text-align: center']
);
}
function block_course_rating_render_btn_view_votes($courseid)
{
global $OUTPUT;
$instance = block_course_rating_get_instance_block($courseid);
$template = block_course_rating_get_template_block($instance);
return html_writer::tag(
'div',
html_writer::link(new moodle_url('/blocks/course_rating/votes.php', [
'templateid' => $template,
'courseid' => $courseid,
]), 'Просмотр ответов', ['class' => 'btn btn-secondary']),
['style' => 'text-align: center']
);
}
function block_course_rating_is_voted($userid, $courseid, $template = null)
{
global $DB;
if ($template == null) {
$instance = block_course_rating_get_instance_block($courseid);
$template = block_course_rating_get_template_block($instance);
}
$select = "SELECT COUNT(*)";
$sql = " FROM {course_rating_answers} t";
$where = " WHERE t.usercreated = :userid and t.courseid = :courseid and t.course_rating_templates_id = :template";
return $DB->count_records_sql($select . $sql . $where, ['template' => $template, 'userid' => $userid, 'courseid' => $courseid]);
}
function block_course_rating_get_instance_block($courseid)
{
global $DB;
$context = context_course::instance($courseid);
return $DB->get_record('block_instances', ['parentcontextid' => $context->id, 'blockname' => 'course_rating'], '*', IGNORE_MISSING);
}
function block_course_rating_get_template_block($instance)
{
try {
$config = \unserialize(\base64_decode($instance->configdata));
} catch (Throwable $e) {
$config = new stdClass();
}
return $config->template ?? 1;
}