/
lib.php
1266 lines (1033 loc) · 38.3 KB
/
lib.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/>.
/**
* This block will display a summary of hours and earnings for the worker.
*
* @package TimeTracker
* @copyright Marty Gilbert & Brad Hughes
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
*/
defined('MOODLE_INTERNAL') || die();
/**
* Tell whether a unit can be editable, based on the following:
* A unit may be edited until the 5th day of the next month
*
* Question about this is whethere we should look at timein
* or timeout, now that we're not splitting up work units
* before adding them to the db TODO
* @deprecated as of v2011123100
*/
function expired($timein, $now = -1){
if($now == -1) $now = time();
$currdateinfo = usergetdate($now);
$unitdateinfo = usergetdate($timein);
if($now - $timein > (86400 * 35) ||
(($currdateinfo['month'] != $unitdateinfo['month'] ||
$currdateinfo['year'] != $unitdateinfo['year']) &&
$currdateinfo['mday'] > 5)){
return true;
}
return false;
}
/**
* Given a userid, courseid, start time and end time, find all units
* that meet within this time period, and split them up across the day boundary.
* For Example:
* DB work unit 10/01/11 09:00am to 10/02/11 02:00am
* this function would return:
*
* array[0]: 10/01/11 09:00am - 11:59:59
* array[1]: 10/02/11 12:00am - 02:00am
*
* Note: some $unit->id may be the same, since a single unit will be broken
* up across many days.
*
* NOTE: $unsignedonly takes precedence over $timesheetid. If you ask for
* $unsignedonly, it will give you only those units that are NOT part of
* an existing timesheet.
*
* @return an array of objects, each having all the properties of a workunit
*/
function get_split_units($start, $end, $userid=0, $courseid=0, $timesheetid=-1, $sort='ASC',
$unsignedonly=false){
global $CFG, $DB;
$sql = 'SELECT * FROM '.$CFG->prefix.'block_timetracker_workunit WHERE ';
if($timesheetid != -1){
$sql .= ' timesheetid='.$timesheetid;
} else {
$sql.=
'(timein BETWEEN '.
$start. ' AND '.$end.' OR timeout BETWEEN '.
$start. ' AND '.$end.') ';
}
if($userid > 0){
$sql .= ' AND userid='.$userid;
}
if($courseid > 0){
$sql .= ' AND courseid='.$courseid;
}
if($unsignedonly){
$sql .= ' AND timesheetid=0';
}
$sql .= ' ORDER BY timein '.$sort;
$units = $DB->get_records_sql($sql);
if(!$units) {
return;
}
$splitunits = array();
$nowtime = time();
foreach($units as $unit){
$splits = split_unit($unit);
$splitunits = array_merge($splitunits, $splits);
}
return $splitunits;
}
/**
* If any units straddle the $start or $end boundary, split them into multiple units
* Only consider units that have NOT been included in a timesheet already.
*/
function split_boundary_units($start, $end, $userid, $courseid){
global $DB, $CFG;
$sql = 'SELECT * FROM '.$CFG->prefix.'block_timetracker_workunit WHERE '.
'userid = '.$userid.' AND courseid = '.$courseid.' AND timesheetid=0 AND '.
'timein < '.$start.' AND timeout > '.$start;
$startunits = $DB->get_records_sql($sql);
if($startunits){
//if there are some (only should be 1, right?)
//split them up to timein->$start and $start->timeout
foreach($startunits as $unit){
$origid = $unit->id;
$timein = $unit->timein;
$timeout = $unit->timeout;
unset($unit->id);
$unit->timeout = $start-1;
$result = $DB->insert_record('block_timetracker_workunit', $unit);
if(!$result) {
print_error("Error splitting boundary work unit");
return;
}
unset($unit->id);
$unit->timein = $start;
$unit->timeout = $timeout;
$result = $DB->insert_record('block_timetracker_workunit', $unit);
if(!$result) {
print_error("Error splitting boundary work unit");
return;
}
//delete the original
$DB->delete_records('block_timetracker_workunit', array('id'=>$origid));
//TODO update workunit history here?
}
}
$sql = 'SELECT * FROM '.$CFG->prefix.'block_timetracker_workunit WHERE '.
'userid = '.$userid.' AND courseid = '.$courseid.' AND timesheetid=0 AND '.
'timein BETWEEN '.$start.' AND '.$end.' AND timeout > '.$end;
$endunits = $DB->get_records_sql($sql);
if($endunits){
//if there are some (only should be 1, right?)
//split them up to timein->$end and $end->timeout
foreach($endunits as $unit){
$origid = $unit->id;
$timein = $unit->timein;
$timeout = $unit->timeout;
unset($unit->id);
$unit->timeout = $end;
$result = $DB->insert_record('block_timetracker_workunit', $unit);
if(!$result) {
print_error("Error splitting boundary work unit");
return;
}
unset($unit->id);
$unit->timein = $end + 1;
$unit->timeout = $timeout;
$result = $DB->insert_record('block_timetracker_workunit', $unit);
if(!$result) {
print_error("Error splitting boundary work unit");
return;
}
//delete the original
$DB->delete_records('block_timetracker_workunit', array('id'=>$origid));
//TODO update workunit history here?
}
}
}
/**
* Given a $unit (full of workunit table data), return an array of $unit objects
* That are split across the day boundary
* TO NOTE: Units that are split up have a 'partial' property that is set to True.
* @return an array of workunits split up into days
*/
function split_unit($unit){
$splitunits = array();
if(!is_object($unit)) return $splitunits;
$timein = usergetdate($unit->timein);
$timeout = usergetdate($unit->timeout);
//check to see if in and out are on the same day
if($timein['year'] == $timeout['year'] &&
$timein['month'] == $timeout['month'] &&
$timein['mday'] == $timeout['mday']){
$newunit = new stdClass();
$newunit->timein = $unit->timein;
$newunit->timeout = $unit->timeout;
$newunit->payrate = $unit->payrate;
$newunit->lastedited = $unit->lastedited;
$newunit->lasteditedby = $unit->lasteditedby;
$newunit->id = $unit->id;
$newunit->userid = $unit->userid;
$newunit->courseid = $unit->courseid;
$newunit->partial = 0;
$newunit->timesheetid = $unit->timesheetid;
$newunit->canedit = $unit->canedit;
$newunit->submitted = $unit->submitted;
$splitunits[] = $newunit;
} else { //spans multiple days
$origtimein = $unit->timein;
$checkout = $unit->timeout;
$endofday = (86400+(usergetmidnight($unit->timein)-1));
$usersdate = usergetdate($endofday);
if($usersdate['hours'] == 22){
$endofday += 60 * 60;
} else if ($usersdate['hours'] == 0){
$endofday -= 60 * 60;
}
while ($unit->timein < $checkout){
//add to array
$unit->timeout = $endofday;
$newunit = new stdClass();
$newunit->timein = $unit->timein;
$newunit->timeout = $unit->timeout;
$newunit->payrate = $unit->payrate;
$newunit->lastedited = $unit->lastedited;
$newunit->lasteditedby = $unit->lasteditedby;
$newunit->id = $unit->id;
$newunit->userid = $unit->userid;
$newunit->courseid = $unit->courseid;
$newunit->partial = true;
$newunit->timesheetid = $unit->timesheetid;
$newunit->canedit = $unit->canedit;
$newunit->submitted = $unit->submitted;
$splitunits[] = $newunit;
$unit->timein = $endofday + 1;
//find next 23:59:59
$endofday = 86400 + (usergetmidnight($unit->timein)-1);
//because I can't get dst_offset_on to work!
$usersdate = usergetdate($endofday);
if($usersdate['hours'] == 22){
$endofday += 60 * 60;
} else if ($usersdate['hours'] == 0){
$endofday -= 60 * 60;
}
//if not a full day, don't go to 23:59:59
//but rather checkout time
if($endofday > $checkout){
$endofday = $unit->timein + ($checkout - $unit->timein);
}
}
}
return $splitunits;
}
/*
* uses get_split_units() to return an array of unit objects, none of
* which cross the day boundary. Note: id of these units may be shared.
* @return array of unit objects
*
*/
function get_split_month_work_units($userid, $courseid, $month, $year, $timesheetid=-1,
$unsignedonly = false){
$info = get_month_info($month, $year);
return get_split_units($info['firstdaytimestamp'], $info['lastdaytimestamp'],
$userid, $courseid, $timesheetid, 'ASC', $unsignedonly);
}
/**
* Given an object that holds all of the values necessary from block_timetracker_workunit,
* Add it to the workunit table.
* @return true if worked, false if failed
*/
function add_unit($unit, $hourlog=false){
global $DB, $CFG, $OUTPUT;
if(!is_object($unit)) return false;
if(isset($unit->id)) unset($unit->id);
$unitid = $DB->insert_record('block_timetracker_workunit', $unit);
$url = new moodle_url($CFG->wwwroot.'/blocks/timetracker/reports.php');
$url->params(array('id'=>$unit->courseid,
'userid'=>$unit->userid,
'reportstart'=>($unit->timein-1),
'reportend'=>($unit->timeout+1)));
if($unitid){
if($hourlog)
add_to_log($unit->courseid, '', 'add work unit', '', 'TimeTracker add work unit');
else
add_to_log($unit->courseid, '', 'add clock-out', '', 'TimeTracker clock-out');
} else {
if($hourlog){
add_to_log($unit->courseid, '',
'error adding work unit', '', 'ERROR: User hourlog failed.');
} else {
add_to_log($unit->courseid, '',
'error clocking-out', '', 'ERROR: User clock-out failed.');
}
return false;
}
return true;
}
/**
* Given an object that holds all of the values necessary from block_timetracker_workunit,
* add update it in the DB
* @return the true if updated successfully, false if not
*
*/
function update_unit($unit){
global $DB;
$id = $unit->id;
$result = add_unit($unit);
if($result){
$deleteresult = $DB->delete_records('block_timetracker_workunit',
array('id'=>$id));
if(!$deleteresult){
//log error in deleting?
return false;
}
} else {
return false;
}
return true;
}
/**
* Attempts to see if this workunit overlaps with any other workunits already submitted
* for user $userid in $COURSE
* @return T if overlaps
*/
function overlaps($timein, $timeout, $userid, $unitid=-1, $courseid=-1){
global $CFG, $COURSE, $DB;
if($courseid == -1) $courseid = $COURSE->id;
$sql = 'SELECT COUNT(*) FROM '.$CFG->prefix.'block_timetracker_workunit WHERE '.
"$userid = userid AND $courseid = courseid AND (".
"($timein >= timein AND $timein < timeout) OR ".
"($timeout > timein AND $timeout <= timeout) OR ".
"(timein >= $timein AND timein < $timeout))";
if($unitid != -1){
$sql.=" AND id != $unitid";
}
$numexistingunits = $DB->count_records_sql($sql);
$sql = 'SELECT COUNT(*) FROM '.$CFG->prefix.'block_timetracker_pending WHERE '.
"$userid = userid AND $courseid = courseid AND ".
"timein BETWEEN $timein AND $timeout";
$numpending = $DB->count_records_sql($sql);
if($numexistingunits == 0 && $numpending == 0) return false;
return true;
}
/**
* Returns an array of stdobjects that have the following:
* obj->display (how to display the work unit)
* obj->editlink (a url to edit the work unit)
* obj->deletelink (a url to delete the unit)
* obj->alertlink (a url to create an alert)
* obj->timein (a timestamp for this clock-in)
* obj->timeout (a timestamp for this clock-out, if applicable. if
* obj->id (the id of the offending unit)
* it is a pending clock-in, this value will be the same as the clock-in value)
* If the array is empty, there are no overlapping units
*/
function find_conflicts($timein, $timeout, $userid, $unitid=-1, $courseid=-1,
$ispending=false, $onlyunsigned=false){
global $CFG, $COURSE, $DB;
if($courseid == -1) $courseid = $COURSE->id;
//check workunit table first
$sql = 'SELECT * FROM '.$CFG->prefix.'block_timetracker_workunit WHERE '.
"$userid = userid AND $courseid = courseid AND (".
"($timein >= timein AND $timein < timeout) OR ".
"($timeout > timein AND $timeout <= timeout) OR ".
"(timein >= $timein AND timein < $timeout))";
if($unitid != -1 && !$ispending){
$sql.=" AND id != $unitid";
}
if($onlyunsigned){
$sql .= " AND timesheetid=0";
}
$conflictingunits = $DB->get_records_sql($sql);
$conflicts = array();
$baseurl = $CFG->wwwroot.'/blocks/timetracker';
foreach ($conflictingunits as $unit){
$entry = new stdClass();
$disp = userdate($unit->timein,
get_string('datetimeformat', 'block_timetracker')).
' to '.userdate($unit->timeout,
get_string('datetimeformat', 'block_timetracker'),99,false);
$entry->display = $disp;
$entry->deletelink = $baseurl.'/deleteworkunit.php?id='.$unit->courseid.
'&userid='.$unit->userid.'&unitid='.$unit->id.
'&sesskey='.sesskey();
$entry->editlink = $baseurl.'/editunit.php?id='.$unit->courseid.
'&userid='.$unit->userid.'&unitid='.$unit->id;
$entry->alertlink = $baseurl.'/alert.php?id='.$unit->courseid.
'&userid='.$unit->userid.'&unitid='.$unit->id;
$entry->timein = $unit->timein;
$entry->timeout = $unit->timeout;
$entry->id = $unit->id;
$conflicts[] = $entry;
}
//pending units
$sql = 'SELECT * FROM '.$CFG->prefix.'block_timetracker_pending WHERE '.
"$userid = userid AND $courseid = courseid AND ".
"timein BETWEEN $timein AND $timeout";
if($unitid != -1 && $ispending){
$sql.=" AND id != $unitid";
}
$pendingconflicts = $DB->get_records_sql($sql);
foreach ($pendingconflicts as $pending){
$entry = new stdClass();
$disp = 'Pending clock-in time: '.userdate($pending->timein,
get_string('datetimeformat', 'block_timetracker'));
$entry->display = $disp;
$entry->deletelink = $baseurl.'/deleteworkunit.php?id='.$pending->courseid.
'&userid='.$pending->userid.'&unitid='.$pending->id;
$entry->editlink = '#';
$entry->timein = $entry->timeout = $pending->timein;
$entry->alertlink = $baseurl.'/alert.php?id='.$pending->courseid.
'&userid='.$pending->userid.'&unitid='.$pending->id.'&ispending=true';
$entry->id = $pending->id;
$conflicts[] = $entry;
}
return $conflicts;
}
/**
* Used in navigation
* @return array of tabobjects
*/
function get_tabs($urlparams, $canmanage = false, $courseid = -1){
global $CFG;
$basicparams = $urlparams;
unset($basicparams['userid']);
$tabs = array();
$tabs[] = new tabobject('home',
new moodle_url($CFG->wwwroot.'/blocks/timetracker/index.php', $basicparams),
'Main');
$tabs[] = new tabobject('reports',
new moodle_url($CFG->wwwroot.'/blocks/timetracker/reports.php',
$urlparams),'Reports');
$tabs[] = new tabobject('timesheets',
new moodle_url($CFG->wwwroot.'/blocks/timetracker/timesheet.php',
$urlparams), 'Timesheets');
$numalerts = '';
if($canmanage){
$manageurl =
new moodle_url($CFG->wwwroot.'/blocks/timetracker/manageworkers.php',
$basicparams);
$manageurl->remove_params('userid');
$tabs[] = new tabobject('manage', $manageurl, 'Manage Workers');
$tabs[] = new tabobject('terms',
new moodle_url($CFG->wwwroot.'/blocks/timetracker/terms.php', $basicparams),
'Terms');
if($courseid != -1){
//getnumalerts from $courseid
$n = has_course_alerts($courseid);
if($n > 0){
$numalerts = '('.$n.')';
}
}
}
$tabs[] = new tabobject('alerts',
new moodle_url($CFG->wwwroot.'/blocks/timetracker/managealerts.php', $basicparams),
'Alerts '.$numalerts);
return $tabs;
}
function add_enrolled_users($context){
global $COURSE, $DB;
//before displaying anything, add any enrolled users NOT in the WORKERINFO table.
//consider moving this to a 'refresh' link or something so it doesn't do it everytime?
//TODO
$config = get_timetracker_config($COURSE->id);
$students = get_users_by_capability($context, 'mod/assignment:submit');
foreach ($students as $student){
if(!$DB->record_exists('block_timetracker_workerinfo',
array('mdluserid'=>$student->id, 'courseid'=>$COURSE->id))){
$student->mdluserid = $student->id;
unset($student->id);
$student->courseid = $COURSE->id;
$student->idnum = $student->username;
$student->address = '0';
$student->position = $config['position'];
$student->currpayrate = $config['curr_pay_rate'];
$student->timetrackermethod = $config['trackermethod'];
$student->dept = $config['department'];
$student->budget = $config['budget'];
$student->supervisor = $config['supname'];
$student->institution = $config['institution'];
$student->maxtermearnings = $config['default_max_earnings'];
$res = $DB->insert_record('block_timetracker_workerinfo', $student);
if(!$res){
print_error("Error adding $student->firstname $student->lastname ".
"to TimeTracker");
}
}
}
}
/*
* rounds to nearest 15 minutes (900 secs) by default. can be set
* using config_block_timetracker_round
*/
function round_time($totalsecs=0, $round=900){
if($totalsecs <= 0) return 0;
if($round > 0){
$temp = $totalsecs % 3600;
$disttoround = $temp % $round;
if($disttoround >= ($round/2))
$totalsecs = $totalsecs + ($round - $disttoround); //round up
else
$totalsecs = $totalsecs - $disttoround; //round down
}
return $totalsecs;
}
/*
* @return number of hours in decimal format, rounded to the nearest config->round
*/
function get_hours($totalsecs=0, $courseid=-1){
$round = get_rounding_config($courseid);
$totalsecs = round_time($totalsecs, $round);
$hrs = round($totalsecs/3600, 3);
return ($hrs);
}
/**
* returns the $totalsecs as 'xx hour(s) xx minute(s)', rounded to the nearest 15 min
*/
function format_elapsed_time($totalsecs=0, $courseid=-1){
if($totalsecs <= 0){
return '0 hours 0 minutes';
}
$round = get_rounding_config($courseid);
$totalsecs = round_time($totalsecs, $round);
$hours = floor($totalsecs/3600);
$minutes = ($totalsecs % 3600)/60;
return $hours.' hour(s) and '.$minutes. ' minute(s)';
}
function get_rounding_config($courseid = -1){
if($courseid == -1){
$round = 900;
} else {
$config = get_timetracker_config($courseid);
if(array_key_exists('round', $config)){
$round = $config['round'];
} else {
$round = 900;
}
}
return $round;
}
/**
* Calculate Total Earnings
* @param $userid, $courseid
* @return total money earned
*/
function get_total_earnings($userid, $courseid){
$workerunits = get_split_units(0, time(), $userid, $courseid);
if(!$workerunits) return 0;
$round = get_rounding_config($courseid);
$earnings = 0;
foreach($workerunits as $subunit){
$hours = round_time($subunit->timeout - $subunit->timein, $round);
$hours = round($hours/3600, 3);
$earnings += $hours * $subunit->payrate;
}
return round($earnings, 2);
}
/**
* Determine if the course has alerts waiting
* @param $courseid id of the course
* @return 0 if no alerts are pending, # of alerts if they exist.
*/
function has_course_alerts($courseid){
global $CFG, $DB;
//check the alert* tables to see if there are any outstanding alerts:
$sql = 'SELECT COUNT(*) FROM '.
$CFG->prefix.'block_timetracker_alertunits'.
' WHERE courseid='.$courseid.
' ORDER BY alerttime';
$numalerts = $DB->count_records_sql($sql);
return $numalerts;
}
/**
* Generate the alert links for a course
* @param $courseid id of the course
* @return three-dimensional array of links. First index is the TT worker id, the second is
* the alertid, while the third index is either 'approve', 'deny', or 'change' for each of
* the corresponding links
*/
function get_course_alert_links($courseid){
global $CFG, $DB;
//check the alert* tables to see if there are any outstanding alerts:
$sql = 'SELECT '.$CFG->prefix.'block_timetracker_alertunits.* FROM '.
$CFG->prefix.'block_timetracker_alertunits '.
'WHERE courseid='.$courseid.
' ORDER BY alerttime';
$alerts = $DB->get_recordset_sql($sql);
$alertlinks = array();
foreach ($alerts as $alert){
$url = $CFG->wwwroot.'/blocks/timetracker/alertaction.php';
$params = "?alertid=$alert->id";
if($alert->todelete) $params.="&delete=1";
$alertlinks[$alert->userid][$alert->id]['approve'] = $url.$params."&action=approve";
$alertlinks[$alert->userid][$alert->id]['deny'] = $url.$params."&action=deny";
$alertlinks[$alert->userid][$alert->id]['change'] = $url.$params."&action=change";
$alertlinks[$alert->userid][$alert->id]['delete'] = $url.$params."&action=delete";
}
return $alertlinks;
}
/**
* Generate the alert links for a course
* @param $courseid id of the course
* @param $alerticon create the alert icon (using new pix_icon)
* @param $alertaction create the action of the alert (usually $OUTPUT->action_icon($alertsurl,$alertaction)
* @return hyperlink with number of existing alerts
* Example: Manage Alerts (3)
*/
function get_alerts_link($courseid, $alerticon, $alertaction){
global $CFG, $DB;
//getnumalerts from $courseid
$numalerts = '';
$n = has_course_alerts($courseid);
if($n > 0){
$numalerts = '('.$n.')';
}
$urlparams['id'] = $courseid;
$baseurl = $CFG->wwwroot.'/blocks/timetracker';
$url = new moodle_url($baseurl.'/managealerts.php', $urlparams);
$text = $alertaction.' <a href="'.$url. 'style="color: red">Manage Alerts '.$numalerts.'</a><br />';
return $text;
}
/**
* Determine if the course has alerts waiting
* @param $courseid id of the course
* @return 0 if no alerts are pending, # of alerts if they exist.
*/
function has_unsigned_timesheets($courseid){
global $CFG, $DB;
//check the timesheet table to see if there are any unsigned timesheets:
$numtimesheets = $DB->count_records('block_timetracker_timesheet',
array('courseid'=>$courseid,
'supervisorsignature'=>0));
return $numtimesheets;
}
function get_timesheet_link($courseid, $timesheetsicon, $timesheetsaction){
global $CFG, $DB;
//getnumalerts from $courseid
$numts = '';
$n = has_unsigned_timesheets($courseid);
if($n > 0){
$numts = '('.$n.')';
}
$urlparams['id'] = $courseid;
$baseurl = $CFG->wwwroot.'/blocks/timetracker';
$url = new moodle_url($baseurl.'/supervisorsig.php', $urlparams);
$text = $timesheetsaction.' <a href="'.$url.'" style="color: red">
Sign Timesheets '.$numts.'</a><br /><br />';
return $text;
}
/**
* Calculate Total Hours
* @param $workerunits is an array, each $subunit has $subunit->timein and $subunit->timeout
* @return total hous worked in decimal format rounded to the nearest interval.
*/
function get_total_hours($userid, $courseid){
$workerunits = get_split_units(0, time(), $userid, $courseid);
if(!$workerunits) return 0;
$round = get_rounding_config($courseid);
$total = 0;
foreach($workerunits as $subunit){
$total += round_time($subunit->timeout - $subunit->timein, $round);
}
return get_hours($total, $courseid);
}
/**
* Also calculates overtime (Weeks > 40 hours Mon-Sun)
* @return earnings (in dollars) for this time period
*
*/
function get_earnings($userid, $courseid, $start, $end, $processovt=1){
global $DB;
$units = get_split_units($start, $end, $userid, $courseid);
if(!$units) return 0;
$info = break_down_earnings($units, $processovt);
return $info['earnings'];
}
/**
* Break down earnings into reghours, regearnings, ovthours, ovtearnings,hours,earnings
* All $unit in $units should be from the same userid/courseid
* @return array
*/
function break_down_earnings($units, $processovt = 1){
global $DB;
$info = array();
$info['reghours'] = 0;
$info['regearnings'] = 0;
$info['ovthours'] = 0;
$info['ovtearnings'] = 0;
$info['hours'] = 0;
$info['earnings'] = 0;
if(!$units) return $info;
$exampleunit = reset($units);
$round = get_rounding_config($exampleunit->courseid);
$worker = $DB->get_record('block_timetracker_workerinfo',
array('id'=>$exampleunit->userid));
if(!$worker) return $info;
if(!$processovt){
$earnings = 0;
foreach($units as $unit){
$hours = round_time($unit->timeout - $unit->timein, $round);
$hours = round($hours/3600, 3);
$info['hours'] += $hours;
$info['reghours'] += $hours;
$earnings += $hours * $unit->payrate;
}
$info['earnings'] = $info['regearnings'] = round($earnings, 2);
} else {
$earnings = $weekhours = $prevweekday = $prevdate = 0;
foreach($units as $unit){
//weekday will be from 1 (Monday) to 7 (Sunday)
//if weekday is < prevweekday, we're on a new week.
$weekday = userdate($unit->timein, "%u");
$date = userdate($unit->timein, "%Y%m%d");
if($weekday < $prevweekday ||
($weekday == $prevweekday && $date != $prevdate)){
$weekhours = 0;
}
$prevweekday = $weekday;
$prevdate = $date;
$hours = round_time($unit->timeout - $unit->timein, $round);
$hours = round($hours/3600, 3);
if( ($hours + $weekhours) > 40){
$ovthours = $reghours = 0;
if($weekhours > 40){
$ovthours = $hours;
} else {
$reghours = 40 - $weekhours;
$ovthours = $hours - $reghours;
}
$info['reghours'] += $reghours;
$info['ovthours'] += $ovthours;
$amt = $reghours * $unit->payrate;
$info['regearnings'] += $amt;
$ovtamt = $ovthours * ($worker->currpayrate * 1.5);
$info['ovtearnings'] += $ovtamt;
} else {
$amt = $hours * $unit->payrate;
$info['reghours'] += $hours;
$info['regearnings'] += $amt;
}
$weekhours += $hours;
}
$info['regearnings'] = round($info['regearnings'], 2);
$info['ovtearnings'] = round($info['ovtearnings'], 2);
$info['earnings'] = round($info['regearnings']+$info['ovtearnings'], 2);
$info['hours'] = $info['reghours'] + $info['ovthours'];
}
return $info;
}
/**
* @return earnings (in dollars) for this month
*
*/
function get_earnings_this_month($userid, $courseid, $month = -1, $year = -1){
$currtime = usergetdate(time());
if($month == -1 || $year == -1){
$units = get_split_month_work_units($userid, $courseid,
$currtime['mon'], $currtime['year']);
} else {
$units = get_split_month_work_units($userid, $courseid,
$month, $year);
}
if(!$units) return 0;
$round = get_rounding_config($courseid);
$earnings = 0;
foreach($units as $unit){
$hours = round_time($unit->timeout - $unit->timein, $round);
$hours = round($hours/3600, 3);
$earnings += $hours * $unit->payrate;
}
return round($earnings, 2);
}
/**
* @param $month The month (1-12) that you're inspecting
* @param $year The year (yyyy) that you're inspecting
* @return array of values
$monthinfo['firstdaytimestamp'] <= unix time of midnight of first day
$monthinfo['lastdaytimestamp'] <= unix time of 23:59:59 of last day
$monthinfo['dayofweek'] <= the index of day of week of first day
$monthinfo['lastday'] <= the last day of this month
$monthinfo['monthname'] <= the name of this month
*/
function get_month_info($month, $year){
$monthinfo = array();
$timestamp = make_timestamp($year, $month); //timestamp of midnight, first day of $month
$monthinfo['firstdaytimestamp'] = $timestamp;
$monthinfo['lastday'] = date('t', $timestamp);
$thistime = usergetdate($timestamp);
$monthinfo['dayofweek'] = $thistime['wday'];
$monthinfo['monthname'] = $thistime['month'];
$timestamp = make_timestamp($year, $month, $monthinfo['lastday'],23,59,59);
$monthinfo['lastdaytimestamp'] = $timestamp; //23:59:59pm
return $monthinfo;
}
/**
* @return hours (in decimal) for this defined period
*
*/
function get_hours_this_period($userid, $courseid, $start, $end){
$units = get_split_units($start, $end, $userid, $courseid);
$config = get_timetracker_config($courseid);
if(!$units) return 0;
$round = get_rounding_config($courseid);
$total = 0;
foreach($units as $unit){
$total += round_time($unit->timeout - $unit->timein, $round);
}
return get_hours($total, $courseid);
}
/**
* @return hours (in decimal) for this month
*
*/
function get_hours_this_month($userid, $courseid, $month = -1, $year = -1){
$currtime = usergetdate(time());
if($month == -1 || $year == -1){
$units = get_split_month_work_units($userid, $courseid,
$currtime['mon'], $currtime['year']);
} else {
$units = get_split_month_work_units($userid, $courseid,
$month, $year);
}
if(!$units) return 0;
$round = get_rounding_config($courseid);
$total = 0;
foreach($units as $unit){
$total += round_time($unit->timeout - $unit->timein, $round);
}