forked from learningworks/moodle-block_enrolmenttimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_enrolmenttimer.php
More file actions
294 lines (245 loc) · 10.5 KB
/
block_enrolmenttimer.php
File metadata and controls
294 lines (245 loc) · 10.5 KB
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
<?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/>.
/**
* Main file to display the block
*
* @package block_enrolmenttimer
* @copyright LearningWorks Ltd 2016
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_enrolmenttimer extends block_base {
public function init() {
$this->title = get_string('enrolmenttimer', 'block_enrolmenttimer');
}//closing init()
public function has_config() {
return true;
}//closing has_config()
public function applicable_formats() {
return array(
'site-index' => false,
'course-view' => true,
'course-view-social' => true,
'mod' => true
);
}//closing //applicable_formats()
public function specialization() {
/*
*
* Use this code to check instance settings before
* using the admin settings
*
*/
// if (!empty($this->config->title)) {
// $this->title = $this->config->title;
// } else {
$this->title = get_string('enrolmenttimer', 'block_enrolmenttimer');
//}
// if(!empty($this->config->instance_completionpercentage)) {
// $this->completionpercentage = $this->config->instance_completionpercentage;
// } else {
$this->completionpercentage = get_config('enrolmenttimer', 'completionpercentage');
//}
// if(!empty($this->config->instance_activecountdown)) {
// $this->activecountdown = $this->config->instance_activecountdown;
// } else {
$this->activecountdown = get_config('enrolmenttimer', 'activecountdown');
//}
// if(!empty($this->config->instance_viewoptions)) {
// $this->viewoptions = $this->config->instance_viewoptions;
// } else {
$this->viewoptions = get_config('enrolmenttimer', 'viewoptions');
//}
}//closing specialization
public function cron() {
global $CFG;
mtrace( "block/enrolmenttimer - Cron is beginning" );
require_once($CFG->dirroot . '/blocks/enrolmenttimer/locallib.php');
global $DB; // Global database object
if(get_config('enrolmenttimer', 'timeleftmessagechk') == 0 && get_config('enrolmenttimer', 'completionsmessagechk') == 0){
//neither of the cron emails are set to run so exit
mtrace('- no mail sent');
return true;
}
// Get the instances of the block
$instances = $DB->get_records( 'block_instances', array('blockname'=>'enrolmenttimer') );
//get the cron run time
$crontime = $DB->get_record('block', array('name'=>'enrolmenttimer'));
//will never return null, otherwise we wouldnt be in the cron method
$crontime = $crontime->cron;
// Iterate over the instances
foreach ($instances as $instance) {
// Recreate block object
$block = block_instance('enrolmenttimer', $instance);
//get courseid of the course this instance is on
$contextid = $block->instance->parentcontextid;
$blockcontext = context::instance_by_id($contextid);
$courseid = $blockcontext->instanceid;
//get enrolled users from the course
$course = $DB->get_record('course', array('id'=>$courseid));
$coursecontext = context_course::instance($course->id);
$users = get_role_users(5, $coursecontext);
//loop through - check days left alert and completion
foreach($users as $user){
//Send Notification Emails
echo get_config('enrolmenttimer', 'timeleftmessagechk');
if(get_config('enrolmenttimer', 'timeleftmessagechk') == 1){
$records = block_enrolmenttimer_get_enrolment_records($user->id, $course->id);
if(isset($records[$user->id])){
$record = $records[$user->id];
if(is_object($record) || $record->timeend != 0 ){
//calculate timestamp at which to alert the user
$enrolmentEnd = (int)$record->timeend;
$enrolmentAlertPeriod = (int)get_config('enrolmenttimer', 'daystoalertenrolmentend')*$crontime; // daystoalert ment to be hours?
$enrolmentAlertTime = $enrolmentEnd - $enrolmentAlertPeriod;
//calculate timestamp at which to stop alerting user
$enrolmentStopAlertPeriod = (int)$enrolmentAlertTime + $crontime;
echo "User id".$user->id;
echo "enrolmentAlertTime ".$enrolmentAlertTime;
echo "enrolmentStopAlertTime ".$enrolmentStopAlertPeriod;
echo "Current Time ".time();
if($enrolmentAlertTime < time() && $enrolmentStopAlertPeriod > time()){
// Send the email to the user
echo "We Got In".time()."<br/>";
$from = core_user::get_support_user();
$subject = get_config('enrolmenttimer', 'enrolmentemailsubject');
$body = get_config('enrolmenttimer', 'timeleftmessage');
//personalise subject words
$body = str_replace("[[user_name]]", $user->firstname, $body);
$body = str_replace("[[course_name]]", $course->fullname, $body);
$body = str_replace("[[days_to_alert]]", get_config('enrolmenttimer', 'daystoalertenrolmentend'), $body);
$textOnlyBody = strip_tags($body);
email_to_user($user, $from, $subject, $textOnlyBody, $body);
}
}
}
}
//Send Completion Emails
if(get_config('enrolmenttimer', 'completionsmessagechk') == 1){
$completion = $DB->get_record('course_completions', array('userid'=>$user->id, 'course'=>$course->id));
if($completion != false && ($completion->timecompleted != NULL || $completion->reaggregate != NULL)){
if($completion->timecompleted != NULL){
//set by user completing course
$completion = $completion->timecompleted;
}elseif($completion->reaggregate != NULL){
// set by admin override
$completion = $completion->reaggregate;
}else{
//moves to the next loop in the foreach
continue;
}
if($completion > (time()-$crontime)){
// Send the email to the user
$from = core_user::get_support_user();
$subject = get_config('enrolmenttimer', 'completionemailsubject');
$body = get_config('enrolmenttimer', 'completionsmessage');
//personalise subject words
$body = str_replace("[[user_name]]", $user->firstname, $body);
$body = str_replace("[[course_name]]", $course->fullname, $body);
$textOnlyBody = strip_tags($body);
email_to_user($user, $from, $subject, $textOnlyBody, $body);
}
}
}
}
}
return true;
}//closing cron()
public function get_content() {
global $COURSE, $USER, $DB, $CFG;
require_once($CFG->dirroot . '/blocks/enrolmenttimer/locallib.php');
if ($this->content !== null) {
return $this->content;
}
$this->content = new stdClass;
$this->content->text = '';
$this->page->requires->js('/blocks/enrolmenttimer/scripts/jquery-1.10.2.min.js');
$this->page->requires->js('/blocks/enrolmenttimer/scripts/scripts.js');
$timeLeft = block_enrolmenttimer_get_remaining_enrolment_period($this->viewoptions);
$this->content->text .= '<div';
if($this->activecountdown == 1){
$this->content->text .= ' class="active"';
}
$this->content->text .= '>';
if(!$timeLeft){
$displayNothingNoDateSet = get_config('enrolmenttimer', 'displayNothingNoDateSet');
if($displayNothingNoDateSet == 1){
$this->content->text = '';
return $this->content;
}else{
$this->content->text .= '<p class="noDateSet">'.get_string('noDateSet','block_enrolmenttimer').'</p></div>';
return $this->content;
}
}else{
//$this->content->text .= 'You have ';
$counter = 1;
$text = '';
$force2digits = get_config('enrolmenttimer', 'forceTwoDigits');
$displayLabels = get_config('enrolmenttimer', 'displayUnitLabels');
$displayTextCounter = get_config('enrolmenttimer', 'displayTextCounter');
$this->content->text .= '<hr>';
$this->content->text .= '<div class="visual-counter">';
$this->content->text .= '<div class="timer-wrapper"';
if($force2digits == 1){
$this->content->text .= ' data-id="force2" ';
}
$this->content->text .= '>';
foreach($timeLeft as $unit => $count){
$stringCount = (string)$count;
$countLength = strlen($stringCount);
if($displayLabels == 1){
$this->content->text .= '<div class="numberTypeWrapper">';
}
$this->content->text .= '<div class="timerNum" data-id="'.$unit.'">';
if($countLength == 1 && $force2digits == 1){
$this->content->text .= '<span class="timerNumChar" data-id="0">0</span>';
$this->content->text .= '<span class="timerNumChar" data-id="1">'.$stringCount.'</span>';
}else{
for ($i=0; $i < $countLength; $i++) {
$this->content->text .= '<span class="timerNumChar" data-id="'.$i.'">'.$stringCount[$i].'</span>';
}
}
$this->content->text .= '</div>';
if($displayLabels == 1){
$this->content->text .= '<p>'.$unit.'</p></div>';
}
if($counter != count($timeLeft)){
$this->content->text .= '<div class="seperator">:</div>';
}
$text .= '<span class="'.$unit.'">'.$count.'</span> ';
if($count > 1){
$text .= $unit.' ';
}else{
$text .= rtrim($unit, "s").' ';
}
$counter++;
}
$this->content->text .= '</div>';
$this->content->text .= '</div>';
$this->content->text .= '<hr>';
$this->content->text .= '<div class="text-wrapper">';
$this->content->text .= '<p class="text-desc"';
if($displayTextCounter == 0){
$this->content->text .= ' style="display: none;"';
}
$this->content->text .= '>'.$text.'</p>';
$this->content->text .= '<p class="sub-text">'.get_string('expirytext', 'block_enrolmenttimer').'</p>';
$this->content->text .= '</div>';
}
$this->content->text .= '</div>';
$this->content->footer = '';
return $this->content;
}//closing get_content()
}//closing class definition