-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
142 lines (119 loc) · 4.21 KB
/
locallib.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
<?php
// This file is part of the my modules block
//
// 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/>.
/**
*
* @package block_mystats
* @copyright 2014 Bas Brands, www.basbrands.nl
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Bas Brands, basbrands.nl
*/
defined('MOODLE_INTERNAL') || die();
function block_mystats_get_dashboard() {
$dashboard = new Object;
$dashboard->courses = block_mystats_mycourses();
$dashboard->statistics = block_mystats_get_statistics($dashboard->courses);
return $dashboard;
}
function block_mystats_mycourses() {
global $USER;
$mycourses = array();
$count = 0;
$enrolledcourses = enrol_get_users_courses($USER->id);
foreach ($enrolledcourses as $course) {
if (isset($USER->lastcourseaccess[$course->id])) {
$course->lastaccess = $USER->lastcourseaccess[$course->id];
} else {
$course->lastaccess = 0;
}
$mycourses[$course->id] = $course;
$count++;
}
if (is_enabled_auth('mnet')) {
$remotecourses = get_my_remotecourses();
foreach ($remotecourses as $course) {
$mycourses[$course->id] = $course;
$count++;
}
}
return $mycourses;
}
function block_mystats_get_statistics($courses) {
global $DB, $USER, $OUTPUT, $CFG;
$statistics = new Object;
$statistics->totalvideos = 0;
$statistics->totaltime = 0;
$statistics->viewcount = 0;
$statistics->timeviewed = 0;
$statistics->totaldiems = 0;
$statistics->dmearned = 0;
$statistics->totalquizzes = 0;
$statistics->quizzes = 0;
if (count($courses) == 0) {
return $statistics;
}
// Get DB viewcount and totaltime
$courseids = array();
foreach ($courses as $course) {
$courseids[] = $course->id;
}
$courseidlist = implode(",",$courseids);
$query = "SELECT w.id, w.course, s.duration
FROM {webvideo} w
LEFT JOIN {webvideo_store} s
ON w.providervideoid = s.providervideoid
WHERE w.course in ($courseidlist)";
$results = $DB->get_records_sql($query);
$statistics->totalvideos = count($results);
$totaltime = 0;
$totaldiems = 0;
foreach ($results as $result) {
$totaltime+= $result->duration;
$totaldiems+= 100;
}
$statistics->totaltime = $totaltime;
$query = "SELECT w.id, w.course, v.viewcount, s.duration, w.providervideoid
FROM {webvideo} w
LEFT JOIN {webvideo_views} v
ON w.id = v.webvideo
LEFT JOIN {webvideo_store} s
ON w.providervideoid = s.providervideoid
WHERE v.userid = ?";
$results = $DB->get_records_sql($query, array($USER->id));
$statistics->viewcount = count($results);
$timeviewed = 0;
foreach ($results as $result) {
$timeviewed+= $result->duration;
}
$statistics->timeviewed = $timeviewed;
// Get diems
$statistics->totaldiems = $totaldiems;
$statistics->dmearned = $OUTPUT->completion_points();
// Get Quiz attempts
$query = "SELECT id, course
FROM {quiz}
WHERE course in ($courseidlist)";
$results = $DB->get_records_sql($query);
foreach ($results as $result) {
$totaldiems += 100;
}
$statistics->totalquizzes = count($results);
$query = "SELECT DISTINCT quiz
FROM {quiz_attempts}
WHERE userid = ?";
$results = $DB->get_records_sql($query, array($USER->id));
$statistics->quizzes = count($results);
return $statistics;
}