-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathuserstats_table.php
274 lines (254 loc) · 10.8 KB
/
userstats_table.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
<?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/>.
/**
* Prints a particular instance of moodleoverflow
*
* You can have a rather longer description of the file as well,
* if you like, and it can span multiple lines.
*
* @package mod_moodleoverflow
* @copyright 2023 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_moodleoverflow\tables;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/moodleoverflow/lib.php');
require_once($CFG->libdir . '/tablelib.php');
/**
* Table listing all user statistics of a course
*
* @package mod_moodleoverflow
* @copyright 2023 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class userstats_table extends \flexible_table {
private $courseid; // Course ID.
private $moodleoverflowid; // Moodleoverflow that started the printing of statistics.
private $usertable = array(); // Usertable will have objects with every user and his statistics.
/**
* Constructor for workflow_table.
* @param int $uniqueid Unique id of this table.
*/
public function __construct($uniqueid, $courseid, $moodleoverflow, $url) {
parent::__construct($uniqueid);
global $PAGE;
$this->courseid = $courseid;
$this->moodleoverflowid = $moodleoverflow;
$this->set_attribute('class', 'statistics-table');
$this->set_attribute('id', $uniqueid);
$this->define_columns(['id', 'username', 'receivedupvotes', 'receiveddownvotes', 'activity', 'reputation']);
$this->define_baseurl($url);
$this->define_headers(['User ID', 'User', 'Received upvotes',
'Received downvotes', 'Amount of activity', 'User reputation']);
$this->get_table_data();
$this->sortable(true, 'reputation', SORT_DESC);
$this->no_sorting('id');
$this->no_sorting('username');
$this->setup();
}
/**
* Method to display the table.
* @return void
*/
public function out() {
global $DB;
$this->start_output();
$this->sort_table_data($this->get_sort_order());
$this->format_and_add_array_of_rows($this->usertable, true);
$this->text_sorting('username');
$this->finish_output();
}
/**
* Method to sort the usertable.
*/
private function sort_table_data($sortorder) {
$key = $sortorder['sortby'];
// The index of each object in usertable is it's value of $key.
$length = count($this->usertable);
if ($sortorder['sortorder'] == 4) {
// 4 means sort in ascending order.
$this->quick_usertable_sort(0, $length - 1, $key, 'asc');
} else if ($sortorder['sortorder'] == 3) {
// 3 means sort in descending order.
$this->quick_usertable_sort(0, $length - 1, $key, 'desc');
}
}
/**
* Sorts usertable with quicksort algorithm.
*/
private function quick_usertable_sort($low, $high, $key, $order) {
if ($low >= $high) {
return;
}
$left = $low;
$right = $high;
$pivot = $this->usertable[intval(($low + $high) / 2)];
$pivot = $pivot->$key;
do {
if ($order == 'asc') {
while ($this->usertable[$left]->$key < $pivot) {
$left++;
}
while ($this->usertable[$right]->$key > $pivot) {
$right--;
}
} else if ($order == 'desc') {
while ($this->usertable[$left]->$key > $pivot) {
$left++;
}
while ($this->usertable[$right]->$key < $pivot) {
$right--;
}
}
if ($left <= $right) {
$temp = $this->usertable[$right];
$this->usertable[$right] = $this->usertable[$left];
$this->usertable[$left] = $temp;
$right--;
$left++;
}
} while ($left <= $right);
if ($low < $right) {
if ($order == 'asc') {
$this->quick_usertable_sort($low, $right, $key, 'asc');
} else if ($order == 'desc') {
$this->quick_usertable_sort($low, $right, $key, 'desc');
}
}
if ($high > $left) {
if ($order == 'asc') {
$this->quick_usertable_sort($left, $high, $key, 'desc');
} else if ($order == 'desc') {
$this->quick_usertable_sort($left, $high, $key, 'desc');
}
}
}
/**
* Method to collect all the data.
* Method will collect all users from the given course and will determine the user statistics
*
* @return 2d-array with user statistic
*/
public function get_table_data() {
global $DB;
// Get all userdata from a course.
$context = \context_course::instance($this->courseid);
$users = get_enrolled_users($context , '', 0, $userfields = 'u.id, u.firstname, u.lastname');
// Step 1.0: Build the datatable with all relevant Informations.
$sqlquery = 'SELECT (ROW_NUMBER() OVER (ORDER BY ratings.id)) AS row_num,
ratings.id AS rateid,
discuss.userid AS discussuserid,
posts.id AS postid,
posts.userid AS postuserid,
ratings.rating AS rating,
ratings.userid AS rateuserid,
ratings.postid AS ratepostid,
discuss.id AS discussid,
posts.discussion AS postdiscussid,
ratings.discussionid AS ratediscussid
FROM {moodleoverflow_discussions} discuss
LEFT JOIN {moodleoverflow_posts} posts ON discuss.id = posts.discussion
LEFT JOIN {moodleoverflow_ratings} ratings ON posts.id = ratings.postid
WHERE discuss.course = ' . $this->courseid . ';';
$ratingdata = $DB->get_records_sql($sqlquery);
// Step 2.0: Now collect the data for every user in the course.
foreach ($users as $user) {
$student = new \stdClass();
$student->id = $user->id;
$student->name = $user->firstname . ' ' . $user->lastname;
$linktostudent = new \moodle_url('/user/view.php', array('id' => $student->id, 'course' => $this->courseid));
$student->link = \html_writer::link($linktostudent->out(), $student->name);
$student->submittedposts = array(); // Key = postid, Value = postid.
$student->ratedposts = array(); // Key = rateid, Value = rateid.
$student->receivedupvotes = 0;
$student->receiveddownvotes = 0;
$student->activity = 0;
$student->reputation = 0;
foreach ($ratingdata as $row) {
if ($row->postuserid == $student->id && $row->rating == RATING_UPVOTE) {
$student->receivedupvotes += 1;
}
if ($row->postuserid == $student->id && $row->rating == RATING_DOWNVOTE) {
$student->receiveddownvotes += 1;
}
if ($row->rateuserid == $student->id && !array_key_exists($row->rateid, $student->ratedposts)) {
$student->activity += 1;
$student->ratedposts[$row->rateid] = $row->rateid;
}
if ($row->postuserid == $student->id && !array_key_exists($row->postid, $student->submittedposts)) {
$student->activity += 1;
$student->submittedposts[$row->postid] = $row->postid;
}
}
// Get the user reputation from the course.
$student->reputation = \mod_moodleoverflow\ratings::moodleoverflow_get_reputation($this->moodleoverflowid,
$student->id);
array_push($this->usertable, $student);
}
}
/**
* Return the usertable.
*/
public function get_usertable() {
return $this->usertable;
}
// Functions that show the data.
public function col_userid($row) {
return $row->id;
}
public function col_username($row) {
return $row->link;
}
public function col_receivedupvotes($row) {
if ($row->receivedupvotes > 0) {
return \html_writer::tag('h5', \html_writer::start_span('badge badge-success') .
$row->receivedupvotes . \html_writer::end_span());
} else {
return \html_writer::tag('h5', \html_writer::start_span('badge badge-warning') .
$row->receivedupvotes . \html_writer::end_span());
}
}
public function col_receiveddownvotes($row) {
if ($row->receiveddownvotes > 0) {
return \html_writer::tag('h5', \html_writer::start_span('badge badge-success') .
$row->receiveddownvotes . \html_writer::end_span());
} else {
return \html_writer::tag('h5', \html_writer::start_span('badge badge-warning') .
$row->receiveddownvotes . \html_writer::end_span());
}
}
public function col_activity($row) {
if ($row->activity > 0) {
return \html_writer::tag('h5', \html_writer::start_span('badge badge-success') .
$row->activity . \html_writer::end_span());
} else {
return \html_writer::tag('h5', \html_writer::start_span('badge badge-warning') .
$row->activity . \html_writer::end_span());
}
}
public function col_reputation($row) {
if ($row->reputation > 0) {
return \html_writer::tag('h5', \html_writer::start_span('badge badge-success') .
$row->reputation . \html_writer::end_span());
} else {
return \html_writer::tag('h5', \html_writer::start_span('badge badge-warning') .
$row->reputation . \html_writer::end_span());
}
}
public function other_cols($colname, $attempt) {
return null;
}
}