forked from ramcoelho/moodle-block_ranking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
executable file
·323 lines (275 loc) · 10.3 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
<?php
// This file is part of Ranking block for 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/>.
/**
* Ranking block definition
*
* @package contrib
* @subpackage block_ranking
* @copyright 2015 Willian Mano http://willianmano.net
* @authors Willian Mano
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define ('DEFAULT_POINTS', 2);
// Store the courses contexts.
$coursescontexts = array();
/**
* Return the list of students in the course ranking
*
* @param int
* @return mixed
*/
function block_ranking_get_students($limit = null) {
global $COURSE, $DB, $PAGE;
// Get block ranking configuration.
$cfgranking = get_config('block_ranking');
// Get limit from default configuration or instance configuration.
if (!$limit) {
if (isset($cfgranking->rankingsize) && trim($cfgranking->rankingsize) != '') {
$limit = $cfgranking->rankingsize;
} else {
$limit = 10;
}
}
$context = $PAGE->context;
$userfields = user_picture::fields('u', array('username'));
$sql = "SELECT
DISTINCT $userfields, r.points
FROM
{user} u
INNER JOIN {role_assignments} a ON a.userid = u.id
INNER JOIN {ranking_points} r ON r.userid = u.id AND r.courseid = :r_courseid
INNER JOIN {context} c ON c.id = a.contextid
WHERE a.contextid = :contextid
AND a.userid = u.id
AND a.roleid = :roleid
AND c.instanceid = :courseid
AND r.courseid = :crsid
ORDER BY r.points DESC, u.firstname ASC";
$params['contextid'] = $context->id;
$params['roleid'] = 5;
$params['courseid'] = $COURSE->id;
$params['crsid'] = $COURSE->id;
$params['r_courseid'] = $COURSE->id;
$limitfrom = 0;
$users = array_values($DB->get_records_sql($sql, $params, $limitfrom, $limit));
return $users;
}
/**
* Get the students points based on a time interval
*
* @param int
* @param int
* @param int
* @return mixed
*/
function block_ranking_get_students_by_date($limit = null, $datestart, $dateend) {
global $COURSE, $DB, $PAGE;
// Get block ranking configuration.
$cfgranking = get_config('block_ranking');
// Get limit from default configuration or instance configuration.
if (!$limit) {
if (isset($cfgranking->rankingsize) && trim($cfgranking->rankingsize) != '') {
$limit = $cfgranking->rankingsize;
} else {
$limit = 10;
}
}
$context = $PAGE->context;
$userfields = user_picture::fields('u', array('username'));
$sql = "SELECT
DISTINCT $userfields,
sum(rl.points) as points
FROM
{user} u
INNER JOIN {role_assignments} a ON a.userid = u.id
INNER JOIN {ranking_points} r ON r.userid = u.id AND r.courseid = :r_courseid
INNER JOIN {ranking_logs} rl ON rl.rankingid = r.id
INNER JOIN {context} c ON c.id = a.contextid
WHERE a.contextid = :contextid
AND a.userid = u.id
AND a.roleid = :roleid
AND c.instanceid = :courseid
AND r.courseid = :crsid
AND rl.timecreated BETWEEN :weekstart AND :weekend
GROUP BY u.id
ORDER BY points DESC, u.firstname ASC
LIMIT " . $limit;
$params['contextid'] = $context->id;
$params['roleid'] = 5;
$params['courseid'] = $COURSE->id;
$params['r_courseid'] = $COURSE->id;
$params['crsid'] = $COURSE->id;
$params['weekstart'] = $datestart;
$params['weekend'] = $dateend;
$users = array_values($DB->get_records_sql($sql, $params));
return $users;
}
/**
* Build the ranking table to be viewd in the course
* @param array $rankinglastmonth List of students of the last month ranking
* @param array $rankinglastweek List of students of the last week ranking
* @param array $rankinggeral List of students to be print in ranking block
* @return string
*/
function block_ranking_print_students($rankinglastmonth, $rankinglastweek, $rankinggeral) {
global $PAGE;
$tablelastweek = generate_table($rankinglastweek);
$tablelastmonth = generate_table($rankinglastmonth);
$tablegeral = generate_table($rankinggeral);
$PAGE->requires->js_init_call('M.block_ranking.init_tabview');
return '<div id="ranking-tabs">
<ul>
<li><a href="#semanal">'.get_string('weekly', 'block_ranking').'</a></li>
<li><a href="#mensal">'.get_string('monthly', 'block_ranking').'</a></li>
<li><a href="#geral">'.get_string('general', 'block_ranking').'</a></li>
</ul>
<div>
<div id="semanal">'.$tablelastweek.'</div>
<div id="mensal">'.$tablelastmonth.'</div>
<div id="geral">'.$tablegeral.'</div>
</div>
</div>';
}
/**
* Print the student individual ranking points
*
* @return string
*/
function block_ranking_print_individual_ranking() {
global $USER, $COURSE;
if (!is_student($USER->id)) {
return '';
}
$weekstart = strtotime(date('d-m-Y', strtotime('-'.date('w').' days')));
$lastweekpoints = block_ranking_get_student_points_by_date($USER->id, $weekstart, time());
$lastweekpoints = $lastweekpoints->points != null ? $lastweekpoints->points : '0';
$lastweekpoints = $lastweekpoints . " " . strtolower(get_string('table_points', 'block_ranking'));
$monthstart = strtotime(date('Y-m-01'));
$lastmonthpoints = block_ranking_get_student_points_by_date($USER->id, $monthstart, time());
$lastmonthpoints = $lastmonthpoints->points != null ? $lastmonthpoints->points : '0';
$lastmonthpoints = $lastmonthpoints . " " . strtolower(get_string('table_points', 'block_ranking'));
$totalpoints = block_ranking_get_student_points($USER->id);
$totalpoints = $totalpoints->points != null ? $totalpoints->points : '0';
$totalpoints = $totalpoints . " " . strtolower(get_string('table_points', 'block_ranking'));
$table = new html_table();
$table->attributes = array("class" => "rankingTable table table-striped generaltable");
$table->head = array(
get_string('weekly', 'block_ranking'),
get_string('monthly', 'block_ranking'),
get_string('general', 'block_ranking')
);
$row = new html_table_row();
$row->cells = array($lastweekpoints, $lastmonthpoints, $totalpoints);
$table->data[] = $row;
$individualranking = html_writer::table($table);
return "<h4>".get_string('your_score', 'block_ranking').":</h4>" . $individualranking;
}
/**
* Get the student points
*
* @param int
* @return mixed
*/
function block_ranking_get_student_points($userid) {
global $COURSE, $DB;
$sql = "SELECT
sum(rl.points) as points
FROM
{user} u
INNER JOIN {ranking_points} r ON r.userid = u.id AND r.courseid = :courseid
INNER JOIN {ranking_logs} rl ON rl.rankingid = r.id
WHERE u.id = :userid
AND r.courseid = :crsid";
$params['userid'] = $userid;
$params['courseid'] = $COURSE->id;
$params['crsid'] = $COURSE->id;
return $DB->get_record_sql($sql, $params);
}
/**
* Get the student points based on a time interval
*
* @param int
* @param int
* @param int
* @return mixed
*/
function block_ranking_get_student_points_by_date($userid, $datestart, $dateend) {
global $COURSE, $DB;
$sql = "SELECT
sum(rl.points) as points
FROM
{user} u
INNER JOIN {ranking_points} r ON r.userid = u.id AND r.courseid = :courseid
INNER JOIN {ranking_logs} rl ON rl.rankingid = r.id
WHERE u.id = :userid
AND r.courseid = :crsid
AND rl.timecreated BETWEEN :weekstart AND :weekend";
$params['userid'] = $userid;
$params['courseid'] = $COURSE->id;
$params['crsid'] = $COURSE->id;
$params['weekstart'] = $datestart;
$params['weekend'] = $dateend;
return $DB->get_record_sql($sql, $params);
}
/**
* Return a table of ranking based on data passed
*
* @param mixed
* @return mixed
*/
function generate_table($data) {
global $USER, $OUTPUT;
if (empty($data)) {
return get_string('nostudents', 'block_ranking');
}
$table = new html_table();
$table->attributes = array("class" => "rankingTable table table-striped generaltable");
$table->head = array(
get_string('table_position', 'block_ranking'),
get_string('table_name', 'block_ranking'),
get_string('table_points', 'block_ranking')
);
$lastpos = 1;
$lastpoints = current($data)->points;
for ($i = 0; $i < count($data); $i++) {
$row = new html_table_row();
// Verify if the logged user is one user in ranking.
if ($data[$i]->id == $USER->id) {
$row->attributes = array('class' => 'itsme');
}
if ($lastpoints > $data[$i]->points) {
$lastpos++;
$lastpoints = $data[$i]->points;
}
$row->cells = array(
$lastpos,
$OUTPUT->user_picture($data[$i], array('size' => 24, 'alttext' => false)) . ' '.$data[$i]->firstname,
$data[$i]->points ?: '-'
);
$table->data[] = $row;
}
return html_writer::table($table);
}
/**
* Verify if the user is a student
*
* @param int
* @param int
* @return bool
*/
function is_student($userid) {
return user_has_role_assignment($userid, 5);
}