-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathuserstats_table.php
392 lines (350 loc) · 14.2 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
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
<?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/>.
/**
* Class needed in userstats.php
*
* @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();
use mod_moodleoverflow\ratings;
global $CFG;
require_once($CFG->dirroot . '/mod/moodleoverflow/lib.php');
require_once($CFG->dirroot . '/mod/moodleoverflow/locallib.php');
require_once($CFG->libdir . '/tablelib.php');
use mod_moodleoverflow\output\helpicon;
/**
* 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 {
/** @var int the Course ID*/
private $courseid;
/** @var int Moodleoverflow that started the printing of statistics*/
private $moodleoverflowid;
/** @var array table that will have objects with every user and his statistics. */
private $userstatsdata = [];
/** @var \stdClass Help icon for amountofactivity-column.*/
private $helpactivity;
/**
* Constructor for workflow_table.
*
* @param int $uniqueid Unique id of this table.
* @param int $courseid
* @param int $moodleoverflow ID if the moodleoverflow
* @param string $url The url of the table
*/
public function __construct($uniqueid, $courseid, $moodleoverflow, $url) {
global $PAGE;
parent::__construct($uniqueid);
$PAGE->requires->js_call_amd('mod_moodleoverflow/activityhelp', 'init');
$this->courseid = $courseid;
$this->moodleoverflowid = $moodleoverflow;
$this->set_helpactivity();
$this->set_attribute('class', 'moodleoverflow-statistics-table');
$this->set_attribute('id', $uniqueid);
$this->define_columns(['username', 'receivedupvotes', 'receiveddownvotes', 'forumactivity', 'courseactivity',
'forumreputation', 'coursereputation', ]);
$this->define_baseurl($url);
$this->define_headers([get_string('fullnameuser'),
get_string('userstatsupvotes', 'moodleoverflow'),
get_string('userstatsdownvotes', 'moodleoverflow'),
(get_string('userstatsforumactivity', 'moodleoverflow') . $this->helpactivity->object),
(get_string('userstatscourseactivity', 'moodleoverflow') . $this->helpactivity->object),
get_string('userstatsforumreputation', 'moodleoverflow'),
get_string('userstatscoursereputation', 'moodleoverflow'), ]);
$this->get_table_data();
$this->sortable(true, 'coursereputation', SORT_DESC);
$this->no_sorting('username');
$this->setup();
}
/**
* Method to display the table.
* @return void
*/
public function out() {
$this->start_output();
$this->sort_table_data($this->get_sort_order());
$this->format_and_add_array_of_rows($this->userstatsdata, true);
$this->text_sorting('coursereputation');
$this->finish_output();
}
/**
* Method to collect all the data.
* Method will collect all users from the given course and will determine the user statistics
* Builds an 2d-array with user statistic
*/
public function get_table_data() {
// Get all userdata from a course.
$context = \context_course::instance($this->courseid);
$users = get_enrolled_users($context , '', 0, 'u.id, u.firstname, u.lastname');
// Step 1.0: Build the datatable with all relevant information.
$ratingdata = $this->get_rating_data();
// Step 2.0: Now collect the data for every user in the course.
foreach ($users as $user) {
$student = $this->createstudent($user);
foreach ($ratingdata as $row) {
// Did the student receive an up- or downvote?
if ($row->postuserid == $student->id) {
$this->process_received_votes($student, $row);
}
// Did a student submit a rating?
if ($row->rateuserid == $student->id ) {
$this->process_submitted_ratings($student, $row);
}
// Did the student write a post?
if ($row->postuserid == $student->id ) {
$this->process_written_posts($student, $row);
}
}
// Get the user reputation from the course.
$student->forumreputation = ratings::moodleoverflow_get_reputation_instance($this->moodleoverflowid, $student->id);
$student->coursereputation = ratings::moodleoverflow_get_reputation_course($this->courseid, $student->id);
$this->userstatsdata[] = $student;
}
}
/**
* Return the userstatsdata-table.
*/
public function get_usertable() {
return $this->userstatsdata;
}
/**
* Setup the help icon for amount of activity
*/
public function set_helpactivity() {
$htmlclass = 'helpactivityclass btn btn-link';
$content = get_string('helpamountofactivity', 'moodleoverflow');
$helpobject = new helpicon($htmlclass, $content);
$this->helpactivity = new \stdClass();
$this->helpactivity->object = $helpobject->get_helpicon();
}
// Functions that show the data.
/**
* username column
* @param object $row
* @return string
*/
public function col_username($row) {
return $row->link;
}
/**
* upvotes column
* @param object $row
* @return string
*/
public function col_receivedupvotes($row) {
return $this->badge_render($row->receivedupvotes);
}
/**
* downvotes column
* @param object $row
* @return string
*/
public function col_receiveddownvotes($row) {
return $this->badge_render($row->receiveddownvotes);
}
/**
* Forum activity column
* @param object $row
* @return string
*/
public function col_forumactivity($row) {
return $this->badge_render($row->forumactivity);
}
/**
* Forum reputation column
* @param object $row
* @return string
*/
public function col_forumreputation($row) {
return $this->badge_render($row->forumreputation);
}
/**
* Course activity column
* @param object $row
* @return string
*/
public function col_courseactivity($row) {
return $this->badge_render($row->courseactivity);
}
/**
* Course reputation column
* @param object $row
* @return string
*/
public function col_coursereputation($row) {
return $this->badge_render($row->coursereputation);
}
/**
* Depending on the value display success or warning badge.
* @param int $number
* @return string
*/
private function badge_render($number) {
if ($number > 0) {
return \html_writer::tag('h5', \html_writer::start_span('badge bg-success') .
$number . \html_writer::end_span());
} else {
return \html_writer::tag('h5', \html_writer::start_span('badge bg-warning') .
$number . \html_writer::end_span());
}
}
/**
* error handling
* @param object $colname
* @param int $attempt
* @return null
*/
public function other_cols($colname, $attempt) {
return null;
}
// Helper functions.
/**
* Return a student object.
* @param \stdClass $user
* @return object
*/
private function createstudent($user) {
$student = new \stdClass();
$student->id = $user->id;
$student->name = $user->firstname . ' ' . $user->lastname;
$linktostudent = new \moodle_url('/user/view.php', ['id' => $student->id, 'course' => $this->courseid]);
$student->link = \html_writer::link($linktostudent->out(), $student->name);
$student->submittedposts = []; // Posts written by the student. Key = postid, Value = postid.
$student->ratedposts = []; // Posts that the student rated. Key = rateid, Value = rateid.
$student->receivedupvotes = 0;
$student->receiveddownvotes = 0;
$student->forumactivity = 0; // Number of written posts and submitted ratings in the current moodleoverflow.
$student->courseactivity = 0; // Number of written posts and submitted ratings in the course.
$student->forumreputation = 0; // Reputation in the current moodleoverflow.
$student->coursereputation = 0; // Reputation in the course.
return $student;
}
/**
* All ratings upvotes downbotes activity etc. from the current course.
* @return array
* @throws \dml_exception
*/
private function get_rating_data() {
global $DB;
$sqlquery = 'SELECT (ROW_NUMBER() OVER (ORDER BY ratings.id)) AS row_num,
discuss.id AS discussid,
discuss.userid AS discussuserid,
posts.id AS postid,
posts.userid AS postuserid,
ratings.id AS rateid,
ratings.rating AS rating,
ratings.userid AS rateuserid,
ratings.postid AS ratepostid,
moodleoverflow.anonymous AS anonymoussetting,
moodleoverflow.id AS moodleoverflowid
FROM {moodleoverflow_discussions} discuss
LEFT JOIN {moodleoverflow_posts} posts ON discuss.id = posts.discussion
LEFT JOIN {moodleoverflow_ratings} ratings ON posts.id = ratings.postid
LEFT JOIN {moodleoverflow} moodleoverflow ON discuss.moodleoverflow = moodleoverflow.id
WHERE discuss.course = ' . $this->courseid . ';';
return $DB->get_records_sql($sqlquery);
}
/**
* Process the received votes for a student.
* @param $student
* @param $row
* @return void
*/
private function process_received_votes(&$student, $row) {
// Only count received votes if the post is not anonymous (no anonymous setting or only questioner anonymous discussion).
if ((($row->anonymoussetting == 0) || ($row->anonymoussetting == 1 && $row->postuserid != $row->discussuserid))) {
if ($row->rating == RATING_UPVOTE) {
$student->receivedupvotes += 1;
} else if ($row->rating == RATING_DOWNVOTE) {
$student->receiveddownvotes += 1;
}
}
}
/**
* Process the submitted ratings from a student.
* @param $student
* @param $row
* @return void
*/
private function process_submitted_ratings(&$student, $row) {
// For solution marks: only count a solution if the discussion is not completely anonymous.
// For helpful marks: only count helpful marks if the discussion is not any kind of anonymous.
// Up and downvotes are always counted.
$solvedcheck = ($row->rating == RATING_SOLVED && $row->anonymoussetting != 2);
$helpfulcheck = ($row->rating == RATING_HELPFUL && $row->anonymoussetting == 0);
$isvote = ($row->rating == RATING_UPVOTE || $row->rating == RATING_DOWNVOTE);
if (!array_key_exists($row->rateid, $student->ratedposts)) {
if ($solvedcheck || $helpfulcheck || $isvote) {
$this->increment_forumactivity($student, $row);
$student->courseactivity++;
$student->ratedposts[$row->rateid] = $row->rateid;
}
}
}
/**
* Process the written posts from a student for the activity.
* @param $student
* @param $row
* @return void
*/
private function process_written_posts(&$student, $row) {
// Only count a written post if: the post is not in an anonymous discussion:
// or the post is in a partial anonymous discussion and the user is not the starter of the discussion.
if (!array_key_exists($row->postid, $student->submittedposts) &&
($row->anonymoussetting == 0 || ($row->anonymoussetting == 1 && $row->postuserid != $row->discussuserid))) {
$this->increment_forumactivity($student, $row);
$student->courseactivity += 1;
$student->submittedposts[$row->postid] = $row->postid;
}
}
/**
* Increments the forum activity of a student.
* @param $row
* @param $moodleoverflowid
* @param $forumactivity
* @return void
*/
private function increment_forumactivity(&$student, $row) {
if ($row->moodleoverflowid == $this->moodleoverflowid) {
$student->forumactivity++;
}
}
// Sort function.
/**
* Method to sort the userstatsdata-table.
* @param array $sortorder The sort order array.
* @return void
*/
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->userstatsdata);
if ($sortorder['sortorder'] == 4) {
// 4 means sort in ascending order.
moodleoverflow_quick_array_sort($this->userstatsdata, 0, $length - 1, $key, 'asc');
} else if ($sortorder['sortorder'] == 3) {
// 3 means sort in descending order.
moodleoverflow_quick_array_sort($this->userstatsdata, 0, $length - 1, $key, 'desc');
}
}
}