-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlocallib.php
executable file
·154 lines (135 loc) · 4.96 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
143
144
145
146
147
148
149
150
151
152
153
154
<?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/>.
/**
* Lib File
*
* @package block_enrolmenttimer
* @copyright LearningWorks Ltd 2016
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Checks the timeleft on enrolment in a given course
*
* @param String $unitstoshow
* @return array from the XML file
*/
function block_enrolmenttimer_get_remaining_enrolment_period($unitstoshow) {
global $COURSE, $USER, $DB;
$context = context_course::instance($COURSE->id);
if (has_capability('moodle/site:config', $context)) {
$record = 0;
} else {
$records = block_enrolmenttimer_get_enrolment_records($USER->id, $COURSE->id);
if (isset($records[$USER->id])) {
$record = $records[$USER->id];
} else {
$record = 0;
}
}
if (!is_object($record)) {
return false;
} else if ($record->timeend == 0 ) {
$timeend = $DB->get_record('enrol', array('enrol' => 'self', 'id' => $record->enrolid), 'enrolenddate');
if (isset($timeend->enrolenddate) && (int) $timeend->enrolenddate > 0) {
$timedifference = (int) $timeend->enrolenddate - time();
$result = array();
if (empty($unitstoshow)) {
// They have not selected any, so show all.
$unitstoshow = block_enrolmenttimer_get_units();
} else {
// Have the selected units, but we only have id's for their values.
$unitstoshow = block_enrolmenttimer_sort_units_to_show($unitstoshow);
}
foreach ($unitstoshow as $text => $unit) {
if ($timedifference > $unit) {
$count = floor($timedifference / $unit);
$result[$text] = $count;
$timedifference = $timedifference - ($count * $unit);
}
}
return $result;
} else {
return false;
}
} else {
$timedifference = (int)$record->timeend - time();
$result = array();
if (empty($unitstoshow)) {
// They have not selected any, so show all.
$unitstoshow = block_enrolmenttimer_get_units();
} else {
// Have the selected units, but we only have id's for their values.
$unitstoshow = block_enrolmenttimer_sort_units_to_show($unitstoshow);
}
foreach ($unitstoshow as $text => $unit) {
if ($timedifference > $unit) {
$count = floor($timedifference / $unit);
$result[$text] = $count;
$timedifference = $timedifference - ($count * $unit);
}
}
return $result;
}
}
/**
* Return enrolment records.
* @param int $userid
* @param int $courseid
* @return array
*/
function block_enrolmenttimer_get_enrolment_records($userid, $courseid) {
global $DB;
$sql = '
SELECT ue.userid, ue.id, ue.timestart, ue.timeend, ue.enrolid
FROM {user_enrolments} ue
JOIN {enrol} e on ue.enrolid = e.id
WHERE ue.userid = ? AND e.courseid = ?
';
return $DB->get_records_sql($sql, array($userid, $courseid));
}
/**
* Return the values for different periods.
* @return array
*/
function block_enrolmenttimer_get_units() {
return array (
get_string('key_years', 'block_enrolmenttimer') => 31536000,
get_string('key_months', 'block_enrolmenttimer') => 2592000,
get_string('key_weeks', 'block_enrolmenttimer') => 604800,
get_string('key_days', 'block_enrolmenttimer') => 86400,
get_string('key_hours', 'block_enrolmenttimer') => 3600,
get_string('key_minutes', 'block_enrolmenttimer') => 60,
get_string('key_seconds', 'block_enrolmenttimer') => 1
);
}
/**
* Sorting the units we will show the user.
* @param array $idstring
* @return array
*/
function block_enrolmenttimer_sort_units_to_show($idstring) {
$idarray = explode(',', $idstring);
// Array of array positions eg 1,2,3 tha where selected on the settings menu.
$units = block_enrolmenttimer_get_units();
$unitkeys = array_keys($units);
$output = array();
foreach ($idarray as $key => $value) {
$unitkey = $unitkeys[$value];
$output[$unitkey] = $units[$unitkey];
}
return $output;
}