Skip to content

Commit c2cce45

Browse files
committed
make table function smaller
1 parent 454dada commit c2cce45

File tree

1 file changed

+50
-32
lines changed

1 file changed

+50
-32
lines changed

classes/tables/userstats_table.php

+50-32
Original file line numberDiff line numberDiff line change
@@ -187,45 +187,15 @@ private function quick_usertable_sort($low, $high, $key, $order) {
187187
* @return 2d-array with user statistic
188188
*/
189189
public function get_table_data() {
190-
global $DB;
191190
// Get all userdata from a course.
192191
$context = \context_course::instance($this->courseid);
193192
$users = get_enrolled_users($context , '', 0, 'u.id, u.firstname, u.lastname');
194193

195-
// Step 1.0: Build the datatable with all relevant Informations.
196-
$sqlquery = 'SELECT (ROW_NUMBER() OVER (ORDER BY ratings.id)) AS row_num,
197-
discuss.id AS discussid,
198-
discuss.userid AS discussuserid,
199-
posts.id AS postid,
200-
posts.userid AS postuserid,
201-
ratings.id AS rateid,
202-
ratings.rating AS rating,
203-
ratings.userid AS rateuserid,
204-
ratings.postid AS ratepostid,
205-
moodleoverflow.anonymous AS anonymoussetting,
206-
moodleoverflow.id AS moodleoverflowid
207-
FROM {moodleoverflow_discussions} discuss
208-
LEFT JOIN {moodleoverflow_posts} posts ON discuss.id = posts.discussion
209-
LEFT JOIN {moodleoverflow_ratings} ratings ON posts.id = ratings.postid
210-
LEFT JOIN {moodleoverflow} moodleoverflow ON discuss.moodleoverflow = moodleoverflow.id
211-
WHERE discuss.course = ' . $this->courseid . ';';
212-
$ratingdata = $DB->get_records_sql($sqlquery);
194+
$ratingdata = $this->get_rating_data();
213195

214196
// Step 2.0: Now collect the data for every user in the course.
215197
foreach ($users as $user) {
216-
$student = new \stdClass();
217-
$student->id = $user->id;
218-
$student->name = $user->firstname . ' ' . $user->lastname;
219-
$linktostudent = new \moodle_url('/user/view.php', array('id' => $student->id, 'course' => $this->courseid));
220-
$student->link = \html_writer::link($linktostudent->out(), $student->name);
221-
$student->submittedposts = array(); // Posts written by the student. Key = postid, Value = postid.
222-
$student->ratedposts = array(); // Posts that the student rated. Key = rateid, Value = rateid.
223-
$student->receivedupvotes = 0;
224-
$student->receiveddownvotes = 0;
225-
$student->forumactivity = 0; // Number of written posts and submitted ratings in the current moodleoverflow.
226-
$student->courseactivity = 0; // Number of written posts and submitted ratings in the course.
227-
$student->forumreputation = 0; // Reputation in the current moodleoverflow.
228-
$student->coursereputation = 0; // Reputation in the course.
198+
$student = $this->createstudent($user);
229199

230200
foreach ($ratingdata as $row) {
231201
// Is the rating from or for the current student?
@@ -402,4 +372,52 @@ private function badge_render($number) {
402372
public function other_cols($colname, $attempt) {
403373
return null;
404374
}
375+
/**
376+
* Return a student object.
377+
* @param \stdClass $user
378+
* @return object
379+
*/
380+
private function createstudent($user) {
381+
$student = new \stdClass();
382+
$student->id = $user->id;
383+
$student->name = $user->firstname . ' ' . $user->lastname;
384+
$linktostudent = new \moodle_url('/user/view.php', array('id' => $student->id, 'course' => $this->courseid));
385+
$student->link = \html_writer::link($linktostudent->out(), $student->name);
386+
$student->submittedposts = array(); // Posts written by the student. Key = postid, Value = postid.
387+
$student->ratedposts = array(); // Posts that the student rated. Key = rateid, Value = rateid.
388+
$student->receivedupvotes = 0;
389+
$student->receiveddownvotes = 0;
390+
$student->forumactivity = 0; // Number of written posts and submitted ratings in the current moodleoverflow.
391+
$student->courseactivity = 0; // Number of written posts and submitted ratings in the course.
392+
$student->forumreputation = 0; // Reputation in the current moodleoverflow.
393+
$student->coursereputation = 0; // Reputation in the course.
394+
return $student;
395+
}
396+
397+
/**
398+
* All ratings upvotes downbotes activity etc. from the current course.
399+
* @return array
400+
* @throws \dml_exception
401+
*/
402+
private function get_rating_data() {
403+
global $DB;
404+
// Step 1.0: Build the datatable with all relevant Informations.
405+
$sqlquery = 'SELECT (ROW_NUMBER() OVER (ORDER BY ratings.id)) AS row_num,
406+
discuss.id AS discussid,
407+
discuss.userid AS discussuserid,
408+
posts.id AS postid,
409+
posts.userid AS postuserid,
410+
ratings.id AS rateid,
411+
ratings.rating AS rating,
412+
ratings.userid AS rateuserid,
413+
ratings.postid AS ratepostid,
414+
moodleoverflow.anonymous AS anonymoussetting,
415+
moodleoverflow.id AS moodleoverflowid
416+
FROM {moodleoverflow_discussions} discuss
417+
LEFT JOIN {moodleoverflow_posts} posts ON discuss.id = posts.discussion
418+
LEFT JOIN {moodleoverflow_ratings} ratings ON posts.id = ratings.postid
419+
LEFT JOIN {moodleoverflow} moodleoverflow ON discuss.moodleoverflow = moodleoverflow.id
420+
WHERE discuss.course = ' . $this->courseid . ';';
421+
return $DB->get_records_sql($sqlquery);
422+
}
405423
}

0 commit comments

Comments
 (0)