-
Notifications
You must be signed in to change notification settings - Fork 16
/
lib.php
executable file
·215 lines (175 loc) · 6.05 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
<?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 global lib
*
* @package block_ranking
* @copyright 2017 Willian Mano http://conecti.me
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
define ('DEFAULT_POINTS', 2);
/**
* Get the groups total points
*
* @return array
*/
function block_ranking_get_total_points_by_group() {
global $COURSE, $DB;
$sql = "SELECT
g.name as groupname, SUM(rp.points) as points
FROM {ranking_points} rp
INNER JOIN {user} u ON u.id = rp.userid
INNER JOIN {groups_members} gm ON gm.userid = rp.userid
INNER JOIN {groups} g ON g.id = gm.groupid
WHERE rp.courseid = :courseid
GROUP BY g.name";
$params['courseid'] = $COURSE->id;
return array_values($DB->get_records_sql($sql, $params));
}
/**
* Return the total of students of a group
*
* @return array
*/
function block_ranking_get_total_students_by_group() {
global $COURSE, $DB;
$sql = "SELECT
g.name as groupname, count(g.name) as qtd
FROM mdl_ranking_points rp
INNER JOIN mdl_user u ON u.id = rp.userid
INNER JOIN mdl_groups_members gm ON gm.userid = rp.userid
INNER JOIN mdl_groups g ON g.id = gm.groupid
WHERE rp.courseid = :courseid
GROUP BY g.name";
$params['courseid'] = $COURSE->id;
return array_values($DB->get_records_sql($sql, $params));
}
/**
* Get the groups total points
*
* @return mixed
*/
function block_ranking_get_average_points_by_group() {
global $COURSE, $DB;
$groups = block_ranking_get_total_points_by_group();
$groupsmembersnumber = block_ranking_get_total_students_by_group();
foreach ($groups as $key => $value) {
foreach ($groupsmembersnumber as $group) {
if ($value->groupname == $group->groupname) {
$groups[$key]->points = $value->points / $group->qtd;
continue 2;
}
}
}
return $groups;
}
/**
* Return the group points evolution
*
* @param int $groupid
* @return array
*/
function block_ranking_get_points_evolution_by_group($groupid) {
global $DB;
$sql = "SELECT
rl.id, rl.points, rl.timecreated
FROM {ranking_logs} rl
INNER JOIN {ranking_points} rp ON rp.id = rl.rankingid
INNER JOIN {groups_members} gm ON gm.userid = rp.userid
INNER JOIN {groups} g ON g.id = gm.groupid
WHERE g.id = :groupid AND rl.timecreated > :lastweek";
$lastweek = time() - (7 * 24 * 60 * 60);
$params['groupid'] = $groupid;
$params['lastweek'] = $lastweek;
return array_values($DB->get_records_sql($sql, $params));
}
/**
* Return a graph of groups points evolution
*
* @param int $groupid
* @return \core\chart_bar
*/
function block_ranking_create_group_points_evolution_chart($groupid) {
$records = block_ranking_get_points_evolution_by_group($groupid);
$pointsbydate = [];
// Pega o primeiro registro, tira do array e soma os pontos na data.
if (count($records)) {
$firstrecord = array_shift($records);
$lastdate = date('d-m-Y', $firstrecord->timecreated);
$pointsbydate[$lastdate] = $firstrecord->points;
}
// Percorre os demais registros.
if (count($records)) {
foreach ($records as $points) {
$currentdate = date('d-m-Y', $points->timecreated);
if ($lastdate != $currentdate && !array_key_exists($currentdate, $pointsbydate)) {
$lastdate = $currentdate;
// Cria novo indice de novo data com valor zero.
$pointsbydate[$lastdate] = 0;
}
$pointsbydate[$lastdate] += $points->points;
}
}
if (empty($pointsbydate)) {
return '';
}
$chart = new \core\chart_line(); // Create a bar chart instance.
$chart->set_smooth(true);
$series = new \core\chart_series(get_string('graph_group_evolution_title', 'block_ranking'), array_values($pointsbydate));
$series->set_type(\core\chart_series::TYPE_LINE);
$chart->add_series($series);
$chart->set_labels(array_keys($pointsbydate));
return $chart;
}
/**
* Return a graph of groups points
*
* @return \core\chart_bar
*/
function block_ranking_create_groups_points_chart() {
$groups = block_ranking_get_total_points_by_group();
$labels = [];
$values = [];
foreach ($groups as $key => $value) {
$labels[] = $value->groupname;
$values[] = $value->points;
}
$chart = new \core\chart_bar(); // Create a bar chart instance.
$series = new \core\chart_series(get_string('graph_groups', 'block_ranking'), $values);
$chart->add_series($series);
$chart->set_labels($labels);
return $chart;
}
/**
* Return a graph of groups points average
*
* @return \core\chart_bar
*/
function block_ranking_create_groups_points_average_chart() {
$groups = block_ranking_get_average_points_by_group();
$labels = [];
$values = [];
foreach ($groups as $key => $value) {
$labels[] = $value->groupname;
$values[] = $value->points;
}
$chart = new \core\chart_bar(); // Create a bar chart instance.
$series = new \core\chart_series(get_string('graph_groups_avg', 'block_ranking'), $values);
$chart->add_series($series);
$chart->set_labels($labels);
return $chart;
}