-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.php
More file actions
471 lines (376 loc) · 17.6 KB
/
lib.php
File metadata and controls
471 lines (376 loc) · 17.6 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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<?php
/*
This file is part of Moodle - http://moodle.org/
Released under GNU General Public License version three (3), for further details,
review the web page "https://www.gnu.org/licenses/gpl.html" or "license.txt".
A tool for Date rollover and a tool for Individual date adjustment. Jesus lives!
@package local
@subpackage dateadjustmenttools
@copyright 2014 Tsedey Terefe <snort.test123@gmail.com>
@copyright 2015 Anton Thelander <anton.thelander@outlook.com>
@license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
global $CFG;
require_once($CFG->dirroot.'/lib/navigationlib.php');
require_once($CFG->dirroot.'/local/dateadjustmenttools/db/access.php');
//Function for getting the link "Date adjustment tools" under "Course administration" to show.
function local_dateadjustmenttools_extends_settings_navigation ($settingsnav, $context) {
global $CFG, $PAGE;
//If the node 'courseadmin' does not exist, then exit this function via return.
if (null == ($courseadmin_node = $settingsnav->get('courseadmin'))) {
return;
}
$context_temp = $context;
$context = $PAGE->context;
$coursecontext = $context->get_course_context();
if (!has_capability('local/dateadjustmenttools:writepermission', $coursecontext)) {
return;
}
$context = $context_temp;
//If not in a course context, then leave (return), does not work for the login screen?
if ($context == null || $context->contextlevel != CONTEXT_COURSE) {
return;
}
//Make a new node called '$branch' out of the node called 'courseadmin'.
$branch = $settingsnav -> get('courseadmin');
//Extract the plugin name to the variable '$pn'.
$pn = get_string('pluginname', 'local_dateadjustmenttools');
//Make an url to 'dateadjustmenttools.php' with the course id appended.
$url = new moodle_url('/local/dateadjustmenttools/index.php', array('courseid' => $context->instanceid));
//Add a node under '$branch' called '$extrabranch' with the name '$pn' and the url '$url'.
$extrabranch = $branch->add($pn,$url);/**/
}
//Function for Date rollover.
function local_dateadjustmenttools_rollover_dates ($data, $courseid) {
global $DB;
$temp=$DB->get_fieldset_select('course', 'startdate', 'id=?', array ($courseid));
$offset = 0;
if ( !empty ($temp) ) {
$newcoursestartdate = $data->rolloverdate;
$oldcoursestartdate = $temp[0];
$offset = $newcoursestartdate - $oldcoursestartdate;
local_dateadjustmenttools_int_validation ($newcoursestartdate);
}
$assignments = $DB->get_records('assign', array ('course' => $courseid));
if ( !empty ($assignments) ) {
foreach ($assignments as $assignment) {
$assignmentrecord = new stdClass();
$assignmentrecord->id=$assignment->id;
if ( $assignment->allowsubmissionsfromdate != 0 ) {
$assignmentrecord->allowsubmissionsfromdate = ( ($assignment->allowsubmissionsfromdate) + ($offset) );
}
if ( $assignment->duedate != 0 ) {
$assignmentrecord->duedate = ( ($assignment->duedate) + ($offset) );
}
if ( $assignment->cutoffdate != 0 ) {
$assignmentrecord->cutoffdate = ( ($assignment->cutoffdate) + ($offset) );
}
$currenttime = time();
$assignmentrecord->timemodified=$currenttime;
$DB->update_record('assign',$assignmentrecord);
}
}
$quizzes = $DB->get_records('quiz', array ('course' => $courseid));
if ( !empty ($quizzes) ) {
foreach ($quizzes as $quiz) {
$quizrecord = new stdClass();
$quizrecord->id=$quiz->id;
if ( $quiz->timeopen != 0 ) {
$quizrecord->timeopen = ( ($quiz->timeopen) + ($offset) );
}
if ( $quiz->timeclose != 0 ) {
$quizrecord->timeclose = ( ($quiz->timeclose) + ($offset) );
}
$quizrecord->timemodified = $currenttime;
$DB->update_record('quiz', $quizrecord);
}
}
$coursedates=$DB->get_records('course', array('id' => $courseid));
if ( !empty ($coursedates) ) {
foreach ($coursedates as $coursedate) {
$courserecord = new stdClass();
$courserecord->id = $coursedate->id;
$courserecord->startdate = $newcoursestartdate;
$courserecord->timemodified = $currenttime;
$DB->update_record('course', $courserecord);
}
}
$calendarevents=$DB->get_records('event',array ('courseid' => $courseid));
if ( !empty ($calendarevents) ) {
foreach ($calendarevents as $calendarevent) {
$eventrecord = new stdClass();
$eventrecord->id = $calendarevent->id;
$eventrecord->timestart = ( ($calendarevent->timestart) + ($offset) );
$eventrecord->timemodified = $currenttime;
$DB->update_record('event', $eventrecord);
}
}
}
//Function for changing the course start date.
function local_dateadjustmenttools_change_course_start_date($data, $courseid) {
global $DB;
$newcoursestartdate = $data->coursestartdate;
local_dateadjustmenttools_int_validation ($newcoursestartdate);
$courserecord = new stdClass();
$courserecord->id = $courseid;
$courserecord->startdate = $newcoursestartdate;
$currenttime = time();
$courserecord->timemodified = $currenttime;
$DB->update_record('course', $courserecord);
}
//Function for changing the assignment dates with relating calendar events.
function local_dateadjustmenttools_change_assignment_dates($data, $courseid, $localindex) {
global $DB, $USER;
$assignmentnamestr = 'assignmentname'.$localindex;
$assignmentname = $data->$assignmentnamestr;
$assignmentidstr = 'assignmentid'.$localindex;
$assignmentid = $data->$assignmentidstr;
$asfdstr = 'assignmentasfd'.$localindex;
$asfd = $data->$asfdstr;
$ddstr = 'assignmentdd'.$localindex;
$dd = $data->$ddstr;
$codstr = 'assignmentcod'.$localindex;
$cod = $data->$codstr;
local_dateadjustmenttools_int_validation ($asfd);
local_dateadjustmenttools_int_validation ($dd);
local_dateadjustmenttools_int_validation ($cod);
$assignmentrecord = new stdClass();
$assignmentrecord->id = $assignmentid;
//31622400 is the Unix time for 1971-01-02 00:00.
if ( $asfd > 31622400 ) {
$assignmentrecord->allowsubmissionsfromdate = $asfd;
} else {
$assignmentrecord->allowsubmissionsfromdate = 0;
}
//31622400 is the Unix time for 1971-01-02 00:00.
if ( $dd > 31622400 ) {
$assignmentrecord->duedate = $dd;
//Calendar event query.
$query_ass = 'courseid=? AND instance=? AND modulename=? AND eventtype=?';
$conditions = array($courseid, $assignmentid, 'assign', 'due');
$get_fieldset_eve = $DB->get_fieldset_select('event', 'id', $query_ass, $conditions);
//Update calendar.
if ( !empty ($get_fieldset_eve) ) { //If an event exists, update the event.
$eventrecord = new stdClass();
$eventrecord->id = $get_fieldset_eve[0];
$eventrecord->timestart = $dd;
$currenttime = time();
$eventrecord->timemodified = $currenttime;
$DB->update_record('event',$eventrecord);
} else { //If an event doesn't exist, create a new calendar event.
$query_ass = 'course=? AND id=?';
$conditions = array($courseid, $assignmentid);
$get_record_ass = $DB->get_fieldset_select('assign', 'intro', $query_ass, $conditions);
$assignmentdescription = $get_record_ass[0];
$event = new stdClass;
$event->name = $assignmentname;
$event->description = $assignmentdescription;
$event->courseid = $courseid;
$event->groupid = 0;
$event->userid = $USER->id;
$event->modulename = 'assign';
$event->instance = $assignmentid;
$event->eventtype = 'due';
$event->timestart = $dd;
$event->visible = 1;
$event->timeduration = 0;
$currenttime = time();
$event->timemodified = $currenttime;
calendar_event::create($event);
}
} else {
//If a date in 1970 is selected then change the duedate to 0.
$assignmentrecord->duedate = 0;
//Calendar event query.
$query_ass = 'courseid=? AND instance=? AND modulename=? AND eventtype=?';
$conditions = array($courseid, $assignmentid, 'assign', 'due');
$get_fieldset_eve = $DB->get_fieldset_select('event', 'id', $query_ass, $conditions);
//Update calendar.
if( !empty ($get_fieldset_eve) ) { //If there is an event, load it and delete it.
$calendarevent = calendar_event::load($get_fieldset_eve[0]);
$calendarevent->delete();
} //Else do nothing.
}
//31622400 is the Unix time for 1971-01-02 00:00.
if ( $cod > 31622400 ) {
$assignmentrecord->cutoffdate = $cod;
} else {
$assignmentrecord->cutoffdate = 0;
}
$currenttime = time();
$assignmentrecord->timemodified = $currenttime;
$DB->update_record('assign', $assignmentrecord);
}
//Function for changing the quiz dates with relating calendar events.
function local_dateadjustmenttools_change_quiz_dates($data, $courseid, $localindex) {
global $DB,$USER;
$quiznamestr = 'quizname'.$localindex;
$quizname = $data->$quiznamestr;
$quizidstr = 'quizid'.$localindex;
$quizid = $data->$quizidstr;
$otqstr = 'quizotq'.$localindex;
$otq = $data->$otqstr;
$ctqstr = 'quizctq'.$localindex;
$ctq = $data->$ctqstr;
local_dateadjustmenttools_int_validation ($otq);
local_dateadjustmenttools_int_validation ($ctq);
$quizrecord = new stdClass();
$quizrecord->id = $quizid;
//31622400 is the Unix time for 1971-01-02 00:00.
if ( $otq > 31622400 ) {
$quizrecord->timeopen = $otq;
//Calendar event query.
$query_qui = 'courseid=? AND instance=? AND modulename=? AND eventtype=?';
$conditions = array($courseid, $quizid, 'quiz', 'open');
$get_fieldset_eve = $DB->get_fieldset_select('event', 'id', $query_qui, $conditions);
//Update calendar.
if ( !empty ($get_fieldset_eve) ) { //If an event exists, update the event.
$eventrecord = new stdClass();
$eventrecord->id = $get_fieldset_eve[0];
$eventrecord->timestart = $otq;
$currenttime = time();
$eventrecord->timemodified = $currenttime;
$DB->update_record('event',$eventrecord);
} else { //If an event doesn't exist, create a new calendar event.
$query_qui = 'course=? AND id=?';
$conditions = array($courseid, $quizid);
$get_record_qui = $DB->get_fieldset_select('quiz', 'intro', $query_qui, $conditions);
$quizdescription = $get_record_qui[0];
$event = new stdClass;
$quizopens_str = get_string('quizopenspar' ,'local_dateadjustmenttools');
$event->name = $quizname.' '.$quizopens_str;
$event->description = $quizdescription;
$event->courseid = $courseid;
$event->groupid = 0;
$event->userid = $USER->id;
$event->modulename = 'quiz';
$event->instance = $quizid;
$event->eventtype = 'open';
$event->timestart = $otq;
$event->visible = 1;
$event->timeduration = 0;
$currenttime = time();
$event->timemodified = $currenttime;
calendar_event::create($event);
}
} else {
$quizrecord->timeopen = 0;
//Calendar event query.
$query_eve = 'courseid=? AND instance=? AND modulename=? AND eventtype=?';
$conditions = array($courseid, $quizid, 'quiz', 'open');
$get_fieldset_eve = $DB->get_fieldset_select('event', 'id', $query_eve, $conditions);
//Update calendar.
if( !empty ($get_fieldset_eve) ) { //If there is an event, load it and delete it.
$calendarevent = calendar_event::load($get_fieldset_eve[0]);
$calendarevent->delete();
} //Else do nothing.
}
//31622400 is the Unix time for 1971-01-02 00:00.
if ( $ctq > 31622400 ) {
$quizrecord->timeclose = $ctq;
//Calendar event query.
$query_qui = 'courseid=? AND instance=? AND modulename=? AND eventtype=?';
$conditions = array($courseid, $quizid, 'quiz', 'close');
$get_fieldset_eve = $DB->get_fieldset_select('event', 'id', $query_qui, $conditions);
//Update calendar.
if ( !empty ($get_fieldset_eve) ) { //If an event exists, update the event.
$eventrecord = new stdClass();
$eventrecord->id = $get_fieldset_eve[0];
$eventrecord->timestart = $ctq;
$currenttime = time();
$eventrecord->timemodified = $currenttime;
$DB->update_record('event',$eventrecord);
} else { //If an event doesn't exist, create a new calendar event.
$query_qui = 'course=? AND id=?';
$conditions = array($courseid, $quizid);
$get_record_qui = $DB->get_fieldset_select('quiz', 'intro', $query_qui, $conditions);
$quizdescription = $get_record_qui[0];
$event = new stdClass;
$quizcloses_str = get_string('quizclosespar', 'local_dateadjustmenttools');
$event->name = $quizname.' '.$quizcloses_str;
$event->description = $quizdescription;
$event->courseid = $courseid;
$event->groupid = 0;
$event->userid = $USER->id;
$event->modulename = 'quiz';
$event->instance = $quizid;
$event->eventtype = 'close';
$event->timestart = $ctq;
$event->visible = 1;
$event->timeduration = 0;
$currenttime = time();
$event->timemodified = $currenttime;
calendar_event::create($event);
}
} else {
$quizrecord->timeclose = 0;
//Calendar event query.
$query_eve = 'courseid=? AND instance=? AND modulename=? AND eventtype=?';
$conditions = array ($courseid, $quizid, 'quiz', 'close');
$get_fieldset_eve = $DB->get_fieldset_select('event', 'id', $query_eve, $conditions);
//Update calendar.
if( !empty ($get_fieldset_eve) ) { //If there is an event, load it and delete it.
$calendarevent = calendar_event::load($get_fieldset_eve[0]);
$calendarevent->delete();
} //Else do nothing.
}
$currenttime = time();
$quizrecord->timemodified = $currenttime;
$DB->update_record('quiz', $quizrecord);
}
//Function for changing the plugin expand/collapse settings.
function local_dateadjustmenttools_change_settings ($data) {
global $DB, $USER;
$userid = $USER->id;
$table = 'local_dateadjustmenttools';
$query_lcl = 'userid=?';
$conditions = array ($userid);
$recordarr = $DB->get_fieldset_select($table, 'id', $query_lcl, $conditions);
$settingsrecord = new stdClass();
$settingsrecord->id = $recordarr[0];
if ( !empty ( $data->dateoverviewheaderexpcheckbox ) ) {
$settingsrecord->dateoverviewheaderexp = 1;
} else {
$settingsrecord->dateoverviewheaderexp = 0;
}
if ( !empty ( $data->selecttoolheaderexpcheckbox ) ) {
$settingsrecord->selecttoolheaderexp = 1;
} else {
$settingsrecord->selecttoolheaderexp = 0;
}
$settingsrecord->selecttool = $data->selecttool;
if ( !empty ( $data->daterolloverheaderexpcheckbox ) ) {
$settingsrecord->daterolloverheaderexp = 1;
} else {
$settingsrecord->daterolloverheaderexp = 0;
}
if ( !empty ( $data->individualdateadjheaderexpcheckbox ) ) {
$settingsrecord->individualdateadjheaderexp = 1;
} else {
$settingsrecord->individualdateadjheaderexp = 0;
}
if ( !empty ( $data->settingsheaderexpcheckbox ) ) {
$settingsrecord->settingsheaderexp = 1;
} else {
$settingsrecord->settingsheaderexp = 0;
}
$currenttime = time();
$settingsrecord->timemodified = $currenttime;
$DB->update_record ($table, $settingsrecord);
}
//Function for custom integer validation.
function local_dateadjustmenttools_int_validation ($var) {
if ( empty ($var) ) { //Checks if $var is empty.
$error = get_string('emptydate','local_dateadjustmenttools');
} else if ( $var < 0 ) { //Checks if $var is below zero.
$error = get_string('invaliddate','local_dateadjustmenttools');
} else if ( !is_int ($var) ) { //Checks if $var isn't an integer.
$error = get_string('notaninteger','local_dateadjustmenttools');
}
if ( !empty ($error) ) { //If any earlier checks prompt true, show the error and exit.
echo $error;
echo '<br /><a href="" onclick="history.go(-1)"><button>Go Back</button></a>';
die;
}
}