-
Notifications
You must be signed in to change notification settings - Fork 2
/
locallib.php
1809 lines (1510 loc) · 64.8 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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* local functions and constants for module attendance
*
* @package mod_attendance
* @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/gradelib.php');
require_once(dirname(__FILE__).'/renderhelpers.php');
define('ATT_VIEW_DAYS', 1);
define('ATT_VIEW_WEEKS', 2);
define('ATT_VIEW_MONTHS', 3);
define('ATT_VIEW_ALLPAST', 4);
define('ATT_VIEW_ALL', 5);
define('ATT_VIEW_NOTPRESENT', 6);
define('ATT_SORT_LASTNAME', 1);
define('ATT_SORT_FIRSTNAME', 2);
class attendance_permissions {
private $canview;
private $canviewreports;
private $cantake;
private $canchange;
private $canmanage;
private $canchangepreferences;
private $canexport;
private $canbelisted;
private $canaccessallgroups;
private $cm;
private $context;
public function __construct($cm, $context) {
$this->cm = $cm;
$this->context = $context;
}
public function can_view() {
if (is_null($this->canview)) {
$this->canview = has_capability('mod/attendance:view', $this->context);
}
return $this->canview;
}
public function require_view_capability() {
require_capability('mod/attendance:view', $this->context);
}
public function can_view_reports() {
if (is_null($this->canviewreports)) {
$this->canviewreports = has_capability('mod/attendance:viewreports', $this->context);
}
return $this->canviewreports;
}
public function require_view_reports_capability() {
require_capability('mod/attendance:viewreports', $this->context);
}
public function can_take() {
if (is_null($this->cantake)) {
$this->cantake = has_capability('mod/attendance:takeattendances', $this->context);
}
return $this->cantake;
}
public function can_take_session($groupid) {
if (!$this->can_take()) {
return false;
}
if ($groupid == attendance::SESSION_COMMON
|| $this->can_access_all_groups()
|| array_key_exists($groupid, groups_get_activity_allowed_groups($this->cm))) {
return true;
}
return false;
}
public function can_change() {
if (is_null($this->canchange)) {
$this->canchange = has_capability('mod/attendance:changeattendances', $this->context);
}
return $this->canchange;
}
public function can_manage() {
if (is_null($this->canmanage)) {
$this->canmanage = has_capability('mod/attendance:manageattendances', $this->context);
}
return $this->canmanage;
}
public function require_manage_capability() {
require_capability('mod/attendance:manageattendances', $this->context);
}
public function can_change_preferences() {
if (is_null($this->canchangepreferences)) {
$this->canchangepreferences = has_capability('mod/attendance:changepreferences', $this->context);
}
return $this->canchangepreferences;
}
public function require_change_preferences_capability() {
require_capability('mod/attendance:changepreferences', $this->context);
}
public function can_export() {
if (is_null($this->canexport)) {
$this->canexport = has_capability('mod/attendance:export', $this->context);
}
return $this->canexport;
}
public function require_export_capability() {
require_capability('mod/attendance:export', $this->context);
}
public function can_be_listed() {
if (is_null($this->canbelisted)) {
$this->canbelisted = has_capability('mod/attendance:canbelisted', $this->context, null, false);
}
return $this->canbelisted;
}
public function can_access_all_groups() {
if (is_null($this->canaccessallgroups)) {
$this->canaccessallgroups = has_capability('moodle/site:accessallgroups', $this->context);
}
return $this->canaccessallgroups;
}
}
class att_page_with_filter_controls {
const SELECTOR_NONE = 1;
const SELECTOR_GROUP = 2;
const SELECTOR_SESS_TYPE = 3;
const SESSTYPE_COMMON = 0;
const SESSTYPE_ALL = -1;
const SESSTYPE_NO_VALUE = -2;
/** @var int current view mode */
public $view;
/** @var int $view and $curdate specify displaed date range */
public $curdate;
/** @var int start date of displayed date range */
public $startdate;
/** @var int end date of displayed date range */
public $enddate;
public $selectortype = self::SELECTOR_NONE;
protected $defaultview = ATT_VIEW_WEEKS;
private $cm;
private $sessgroupslist;
private $sesstype;
public function init($cm) {
$this->cm = $cm;
$this->init_view();
$this->init_curdate();
$this->init_start_end_date();
}
private function init_view() {
global $SESSION;
if (isset($this->view)) {
$SESSION->attcurrentattview[$this->cm->course] = $this->view;
} else if (isset($SESSION->attcurrentattview[$this->cm->course])) {
$this->view = $SESSION->attcurrentattview[$this->cm->course];
} else {
$this->view = $this->defaultview;
}
}
private function init_curdate() {
global $SESSION;
if (isset($this->curdate)) {
$SESSION->attcurrentattdate[$this->cm->course] = $this->curdate;
} else if (isset($SESSION->attcurrentattdate[$this->cm->course])) {
$this->curdate = $SESSION->attcurrentattdate[$this->cm->course];
} else {
$this->curdate = time();
}
}
public function init_start_end_date() {
global $CFG;
// HOURSECS solves issue for weeks view with Daylight saving time and clocks adjusting by one hour backward.
$date = usergetdate($this->curdate + HOURSECS);
$mday = $date['mday'];
$wday = $date['wday'] - $CFG->calendar_startwday;
if ($wday < 0) {
$wday += 7;
}
$mon = $date['mon'];
$year = $date['year'];
switch ($this->view) {
case ATT_VIEW_DAYS:
$this->startdate = make_timestamp($year, $mon, $mday);
$this->enddate = make_timestamp($year, $mon, $mday + 1);
break;
case ATT_VIEW_WEEKS:
$this->startdate = make_timestamp($year, $mon, $mday - $wday);
$this->enddate = make_timestamp($year, $mon, $mday + 7 - $wday) - 1;
break;
case ATT_VIEW_MONTHS:
$this->startdate = make_timestamp($year, $mon);
$this->enddate = make_timestamp($year, $mon + 1);
break;
case ATT_VIEW_ALLPAST:
$this->startdate = 1;
$this->enddate = time();
break;
case ATT_VIEW_ALL:
$this->startdate = 0;
$this->enddate = 0;
break;
}
}
private function calc_sessgroupslist_sesstype() {
global $SESSION;
if (!array_key_exists('attsessiontype', $SESSION)) {
$SESSION->attsessiontype = array($this->cm->course => self::SESSTYPE_ALL);
} else if (!array_key_exists($this->cm->course, $SESSION->attsessiontype)) {
$SESSION->attsessiontype[$this->cm->course] = self::SESSTYPE_ALL;
}
$group = optional_param('group', self::SESSTYPE_NO_VALUE, PARAM_INT);
if ($this->selectortype == self::SELECTOR_SESS_TYPE) {
if ($group > self::SESSTYPE_NO_VALUE) {
$SESSION->attsessiontype[$this->cm->course] = $group;
if ($group > self::SESSTYPE_ALL) {
// Set activegroup in $SESSION.
groups_get_activity_group($this->cm, true);
} else {
// Reset activegroup in $SESSION.
unset($SESSION->activegroup[$this->cm->course][VISIBLEGROUPS][$this->cm->groupingid]);
unset($SESSION->activegroup[$this->cm->course]['aag'][$this->cm->groupingid]);
unset($SESSION->activegroup[$this->cm->course][SEPARATEGROUPS][$this->cm->groupingid]);
}
$this->sesstype = $group;
} else {
$this->sesstype = $SESSION->attsessiontype[$this->cm->course];
}
} else if ($this->selectortype == self::SELECTOR_GROUP) {
if ($group == 0) {
$SESSION->attsessiontype[$this->cm->course] = self::SESSTYPE_ALL;
$this->sesstype = self::SESSTYPE_ALL;
} else if ($group > 0) {
$SESSION->attsessiontype[$this->cm->course] = $group;
$this->sesstype = $group;
} else {
$this->sesstype = $SESSION->attsessiontype[$this->cm->course];
}
}
if (is_null($this->sessgroupslist)) {
$this->calc_sessgroupslist();
}
// For example, we set SESSTYPE_ALL but user can access only to limited set of groups.
if (!array_key_exists($this->sesstype, $this->sessgroupslist)) {
reset($this->sessgroupslist);
$this->sesstype = key($this->sessgroupslist);
}
}
private function calc_sessgroupslist() {
global $USER, $PAGE;
$this->sessgroupslist = array();
$groupmode = groups_get_activity_groupmode($this->cm);
if ($groupmode == NOGROUPS) {
return;
}
if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $PAGE->context)) {
$allowedgroups = groups_get_all_groups($this->cm->course, 0, $this->cm->groupingid);
} else {
$allowedgroups = groups_get_all_groups($this->cm->course, $USER->id, $this->cm->groupingid);
}
if ($allowedgroups) {
if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $PAGE->context)) {
$this->sessgroupslist[self::SESSTYPE_ALL] = get_string('all', 'attendance');
}
// Show Common groups always
$this->sessgroupslist[self::SESSTYPE_COMMON] = get_string('commonsessions', 'attendance');
foreach ($allowedgroups as $group) {
$this->sessgroupslist[$group->id] = format_string($group->name);
}
}
}
public function get_sess_groups_list() {
if (is_null($this->sessgroupslist)) {
$this->calc_sessgroupslist_sesstype();
}
return $this->sessgroupslist;
}
public function get_current_sesstype() {
if (is_null($this->sesstype)) {
$this->calc_sessgroupslist_sesstype();
}
return $this->sesstype;
}
public function set_current_sesstype($sesstype) {
$this->sesstype = $sesstype;
}
}
class att_view_page_params extends att_page_with_filter_controls {
const MODE_THIS_COURSE = 0;
const MODE_ALL_COURSES = 1;
public $studentid;
public $mode;
public function __construct() {
$this->defaultview = ATT_VIEW_MONTHS;
}
public function get_significant_params() {
$params = array();
if (isset($this->studentid)) {
$params['studentid'] = $this->studentid;
}
if ($this->mode != self::MODE_THIS_COURSE) {
$params['mode'] = $this->mode;
}
return $params;
}
}
class att_manage_page_params extends att_page_with_filter_controls {
public function __construct() {
$this->selectortype = att_page_with_filter_controls::SELECTOR_SESS_TYPE;
}
public function get_significant_params() {
return array();
}
}
class att_sessions_page_params {
const ACTION_ADD = 1;
const ACTION_UPDATE = 2;
const ACTION_DELETE = 3;
const ACTION_DELETE_SELECTED = 4;
const ACTION_CHANGE_DURATION = 5;
/** @var int view mode of taking attendance page*/
public $action;
}
class att_take_page_params {
const SORTED_LIST = 1;
const SORTED_GRID = 2;
const DEFAULT_VIEW_MODE = self::SORTED_LIST;
public $sessionid;
public $grouptype;
public $group;
public $sort;
public $copyfrom;
/** @var int view mode of taking attendance page*/
public $viewmode;
public $gridcols;
public function init() {
if (!isset($this->group)) {
$this->group = 0;
}
if (!isset($this->sort)) {
$this->sort = ATT_SORT_LASTNAME;
}
$this->init_view_mode();
$this->init_gridcols();
}
private function init_view_mode() {
if (isset($this->viewmode)) {
set_user_preference("attendance_take_view_mode", $this->viewmode);
} else {
$this->viewmode = get_user_preferences("attendance_take_view_mode", self::DEFAULT_VIEW_MODE);
}
}
private function init_gridcols() {
if (isset($this->gridcols)) {
set_user_preference("attendance_gridcolumns", $this->gridcols);
} else {
$this->gridcols = get_user_preferences("attendance_gridcolumns", 5);
}
}
public function get_significant_params() {
$params = array();
$params['sessionid'] = $this->sessionid;
$params['grouptype'] = $this->grouptype;
if ($this->group) {
$params['group'] = $this->group;
}
if ($this->sort != ATT_SORT_LASTNAME) {
$params['sort'] = $this->sort;
}
if (isset($this->copyfrom)) {
$params['copyfrom'] = $this->copyfrom;
}
return $params;
}
}
class att_report_page_params extends att_page_with_filter_controls {
public $group;
public $sort;
public function __construct() {
$this->selectortype = self::SELECTOR_GROUP;
}
public function init($cm) {
parent::init($cm);
if (!isset($this->group)) {
$this->group = $this->get_current_sesstype() > 0 ? $this->get_current_sesstype() : 0;
}
if (!isset($this->sort)) {
$this->sort = ATT_SORT_LASTNAME;
}
}
public function get_significant_params() {
$params = array();
if ($this->sort != ATT_SORT_LASTNAME) {
$params['sort'] = $this->sort;
}
return $params;
}
}
class att_preferences_page_params {
const ACTION_ADD = 1;
const ACTION_DELETE = 2;
const ACTION_HIDE = 3;
const ACTION_SHOW = 4;
const ACTION_SAVE = 5;
/** @var int view mode of taking attendance page*/
public $action;
public $statusid;
public function get_significant_params() {
$params = array();
if (isset($this->action)) {
$params['action'] = $this->action;
}
if (isset($this->statusid)) {
$params['statusid'] = $this->statusid;
}
return $params;
}
}
class att_train_page_params extends att_page_with_filter_controls {
public $group;
public $sort;
public function __construct() {
$this->selectortype = self::SELECTOR_GROUP;
}
public function init($cm) {
parent::init($cm);
if (!isset($this->group)) {
$this->group = $this->get_current_sesstype() > 0 ? $this->get_current_sesstype() : 0;
}
if (!isset($this->sort)) {
$this->sort = ATT_SORT_LASTNAME;
}
}
public function get_significant_params() {
$params = array();
if ($this->sort != ATT_SORT_LASTNAME) {
$params['sort'] = $this->sort;
}
return $params;
}
}
class att_approve_page_params extends att_page_with_filter_controls {
public $group;
public $sort;
public function __construct() {
$this->selectortype = self::SELECTOR_GROUP;
}
public function init($cm) {
parent::init($cm);
if (!isset($this->group)) {
$this->group = $this->get_current_sesstype() > 0 ? $this->get_current_sesstype() : 0;
}
if (!isset($this->sort)) {
$this->sort = ATT_SORT_LASTNAME;
}
}
public function get_significant_params() {
$params = array();
if ($this->sort != ATT_SORT_LASTNAME) {
$params['sort'] = $this->sort;
}
return $params;
}
}
class attendance {
const SESSION_COMMON = 0;
const SESSION_GROUP = 1;
/** @var stdclass course module record */
public $cm;
/** @var stdclass course record */
public $course;
/** @var stdclass context object */
public $context;
/** @var int attendance instance identifier */
public $id;
/** @var string attendance activity name */
public $name;
/** @var float number (10, 5) unsigned, the maximum grade for attendance */
public $grade;
/** current page parameters */
public $pageparams;
/** @var attendance_permissions permission of current user for attendance instance*/
public $perm;
private $groupmode;
private $statuses;
// Array by sessionid.
private $sessioninfo = array();
// Arrays by userid.
private $usertakensesscount = array();
private $userstatusesstat = array();
/**
* Initializes the attendance API instance using the data from DB
*
* Makes deep copy of all passed records properties. Replaces integer $course attribute
* with a full database record (course should not be stored in instances table anyway).
*
* @param stdClass $dbrecord Attandance instance data from {attendance} table
* @param stdClass $cm Course module record as returned by {@link get_coursemodule_from_id()}
* @param stdClass $course Course record from {course} table
* @param stdClass $context The context of the workshop instance
*/
public function __construct(stdclass $dbrecord, stdclass $cm, stdclass $course, stdclass $context=null, $pageparams=null) {
foreach ($dbrecord as $field => $value) {
if (property_exists('attendance', $field)) {
$this->{$field} = $value;
} else {
throw new coding_exception('The attendance table has a field with no property in the attendance class');
}
}
$this->cm = $cm;
$this->course = $course;
if (is_null($context)) {
$this->context = context_module::instance_by_id($this->cm->id);
} else {
$this->context = $context;
}
$this->pageparams = $pageparams;
$this->perm = new attendance_permissions($this->cm, $this->context);
}
public function get_group_mode() {
if (is_null($this->groupmode)) {
$this->groupmode = groups_get_activity_groupmode($this->cm, $this->course);
}
return $this->groupmode;
}
/**
* Returns current sessions for this attendance
*
* Fetches data from {attendance_sessions}
*
* @return array of records or an empty array
*/
public function get_current_sessions() {
global $DB;
$today = time(); // Because we compare with database, we don't need to use usertime().
$sql = "SELECT *
FROM {attendance_sessions}
WHERE :time BETWEEN sessdate AND (sessdate + duration)
AND attendanceid = :aid";
$params = array(
'time' => $today,
'aid' => $this->id);
return $DB->get_records_sql($sql, $params);
}
/**
* Returns today sessions for this attendance
*
* Fetches data from {attendance_sessions}
*
* @return array of records or an empty array
*/
public function get_today_sessions() {
global $DB;
$start = usergetmidnight(time());
$end = $start + DAYSECS;
$sql = "SELECT *
FROM {attendance_sessions}
WHERE sessdate >= :start AND sessdate < :end
AND attendanceid = :aid";
$params = array(
'start' => $start,
'end' => $end,
'aid' => $this->id);
return $DB->get_records_sql($sql, $params);
}
/**
* Returns today sessions suitable for copying attendance log
*
* Fetches data from {attendance_sessions}
*
* @return array of records or an empty array
*/
public function get_today_sessions_for_copy($sess) {
global $DB;
$start = usergetmidnight($sess->sessdate);
$sql = "SELECT *
FROM {attendance_sessions}
WHERE sessdate >= :start AND sessdate <= :end AND
(groupid = 0 OR groupid = :groupid) AND
lasttaken > 0 AND attendanceid = :aid";
$params = array(
'start' => $start,
'end' => $sess->sessdate,
'groupid' => $sess->groupid,
'aid' => $this->id);
return $DB->get_records_sql($sql, $params);
}
/**
* Returns count of hidden sessions for this attendance
*
* Fetches data from {attendance_sessions}
*
* @return count of hidden sessions
*/
public function get_hidden_sessions_count() {
global $DB;
$where = "attendanceid = :aid AND sessdate < :csdate";
$params = array(
'aid' => $this->id,
'csdate'=> $this->course->startdate);
return $DB->count_records_select('attendance_sessions', $where, $params);
}
public function get_filtered_sessions($filtergroups = true) {
global $DB;
if ($this->pageparams->startdate && $this->pageparams->enddate) {
$where = "attendanceid = :aid AND sessdate >= :csdate AND sessdate >= :sdate AND sessdate < :edate";
} else if ($this->pageparams->enddate) {
$where = "attendanceid = :aid AND sessdate >= :csdate AND sessdate < :edate";
} else {
$where = "attendanceid = :aid AND sessdate >= :csdate";
}
if ($filtergroups && $this->pageparams->get_current_sesstype() > att_page_with_filter_controls::SESSTYPE_ALL) {
$where .= " AND groupid=:cgroup";
}
$params = array(
'aid' => $this->id,
'csdate' => $this->course->startdate,
'sdate' => $this->pageparams->startdate,
'edate' => $this->pageparams->enddate,
'cgroup' => $this->pageparams->get_current_sesstype());
$sessions = $DB->get_records_select('attendance_sessions', $where, $params, 'sessdate asc');
foreach ($sessions as $sess) {
if (empty($sess->description)) {
$sess->description = get_string('nodescription', 'attendance');
} else {
$sess->description = file_rewrite_pluginfile_urls($sess->description,
'pluginfile.php', $this->context->id, 'mod_attendance', 'session', $sess->id);
}
}
return $sessions;
}
/**
* @return moodle_url of manage.php for attendance instance
*/
public function url_manage($params=array()) {
$params = array_merge(array('id' => $this->cm->id), $params);
return new moodle_url('/mod/attendance/manage.php', $params);
}
/**
* @return moodle_url of sessions.php for attendance instance
*/
public function url_sessions($params=array()) {
$params = array_merge(array('id' => $this->cm->id), $params);
return new moodle_url('/mod/attendance/sessions.php', $params);
}
/**
* @return moodle_url of report.php for attendance instance
*/
public function url_report($params=array()) {
$params = array_merge(array('id' => $this->cm->id), $params);
return new moodle_url('/mod/attendance/report.php', $params);
}
/**
* @return moodle_url of export.php for attendance instance
*/
public function url_export() {
$params = array('id' => $this->cm->id);
return new moodle_url('/mod/attendance/export.php', $params);
}
/**
* @return moodle_url of attsettings.php for attendance instance
*/
public function url_preferences($params=array()) {
$params = array_merge(array('id' => $this->cm->id), $params);
return new moodle_url('/mod/attendance/preferences.php', $params);
}
/**
* @return moodle_url of attsettings.php for attendance instance
*/
public function url_train($params=array()) {
$params = array_merge(array('id' => $this->cm->id), $params);
return new moodle_url('/mod/attendance/train.php', $params);
}
/**
* @return moodle_url of attsettings.php for attendance instance
*/
public function url_approve($params=array()) {
$params = array_merge(array('id' => $this->cm->id), $params);
return new moodle_url('/mod/attendance/approve.php', $params);
}
/**
* @return moodle_url of attendances.php for attendance instance
*/
public function url_take($params=array()) {
$params = array_merge(array('id' => $this->cm->id), $params);
return new moodle_url('/mod/attendance/take.php', $params);
}
public function url_view($params=array()) {
$params = array_merge(array('id' => $this->cm->id), $params);
return new moodle_url('/mod/attendance/view.php', $params);
}
public function add_sessions($sessions) {
global $DB;
foreach ($sessions as $sess) {
$sess->attendanceid = $this->id;
$sess->id = $DB->insert_record('attendance_sessions', $sess);
$description = file_save_draft_area_files($sess->descriptionitemid,
$this->context->id, 'mod_attendance', 'session', $sess->id,
array('subdirs' => false, 'maxfiles' => -1, 'maxbytes' => 0),
$sess->description);
$DB->set_field('attendance_sessions', 'description', $description, array('id' => $sess->id));
$info_array = array();
$info_array[] = construct_session_full_date_time($sess->sessdate, $sess->duration);
// Trigger a session added event.
$event = \mod_attendance\event\session_added::create(array(
'objectid' => $this->id,
'context' => $this->context,
'other' => array('info' => implode(',', $info_array))
));
$event->add_record_snapshot('course_modules', $this->cm);
$sess->description = $description;
$sess->lasttaken = 0;
$sess->lasttakenby = 0;
$sess->studentscanmark = 0;
$event->add_record_snapshot('attendance_sessions', $sess);
$event->trigger();
}
}
public function update_session_from_form_data($formdata, $sessionid) {
global $DB;
if (!$sess = $DB->get_record('attendance_sessions', array('id' => $sessionid) )) {
print_error('No such session in this course');
}
$sess->sessdate = $formdata->sessiondate;
$sess->duration = $formdata->durtime['hours']*HOURSECS + $formdata->durtime['minutes']*MINSECS;
$description = file_save_draft_area_files($formdata->sdescription['itemid'],
$this->context->id, 'mod_attendance', 'session', $sessionid,
array('subdirs' => false, 'maxfiles' => -1, 'maxbytes' => 0), $formdata->sdescription['text']);
$sess->description = $description;
$sess->descriptionformat = $formdata->sdescription['format'];
$sess->timemodified = time();
$DB->update_record('attendance_sessions', $sess);
$info = construct_session_full_date_time($sess->sessdate, $sess->duration);
$event = \mod_attendance\event\session_updated::create(array(
'objectid' => $this->id,
'context' => $this->context,
'other' => array('info' => $info, 'sessionid' => $sessionid, 'action' => att_sessions_page_params::ACTION_UPDATE)));
$event->add_record_snapshot('course_modules', $this->cm);
$event->add_record_snapshot('attendance_sessions', $sess);
$event->trigger();
}
public function remove_from_student($sessid, $studentid) {
global $DB;
$now = time();
$dbsesslog = $DB->delete_records('attendance_log', array('sessionid' => $sessid, 'studentid' => $studentid));
// Update the session to show that a register has been taken, or staff may overwrite records.
$session = $this->get_session_info($sessid);
$session->lasttaken = $now;
$session->lasttakenby = $studentid;
$DB->update_record('attendance_sessions', $session);
// Update the users grade.
$this->update_users_grade(array($studentid));
/* create url for link in log screen
* need to set grouptype to 0 to allow take attendance page to be called
* from report/log page */
$params = array(
'sessionid' => $sessid,
'grouptype' => 0);
// Log the change.
$event = \mod_attendance\event\attendance_taken_by_student::create(array(
'objectid' => $this->id,
'context' => $this->context,
'other' => $params));
$event->add_record_snapshot('course_modules', $this->cm);
$event->add_record_snapshot('attendance_sessions', $session);
// $event->add_record_snapshot('attendance_log', $record);
$event->trigger();
return true;
}
/**
* Used to record attendance submitted by the student.
*
* @global type $DB
* @param type $studentid
* @param type $sessid
* @return boolean
*/
public function take_from_student($sessid, $studentid) {
global $DB;
$statuses = implode(',', array_keys( (array)$this->get_statuses() ));
$now = time();
$record = new stdClass();
$record->studentid = $studentid;
// Marca como presente!
$record->statusid = 5;
$record->statusset = $statuses;
$record->remarks = get_string('set_by_student', 'mod_attendance');
$record->sessionid = $sessid;
$record->timetaken = $now;
$record->takenby = $studentid;
$dbsesslog = $this->get_session_log($sessid);
if (array_key_exists($record->studentid, $dbsesslog)) {
// Already recorded do not save.
return false;
}
else {
$DB->insert_record('attendance_log', $record, false);
}
// Update the session to show that a register has been taken, or staff may overwrite records.
$session = $this->get_session_info($sessid);
$session->lasttaken = $now;
$session->lasttakenby = $studentid;
$DB->update_record('attendance_sessions', $session);
// Update the users grade.
$this->update_users_grade(array($studentid));
/* create url for link in log screen
* need to set grouptype to 0 to allow take attendance page to be called
* from report/log page */