-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeclock.php
122 lines (101 loc) · 4.09 KB
/
timeclock.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
<?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 page will allow the user to clock in and clock out.
*
* @package Block
* @subpackage TimeTracker
* @copyright 2011 Marty Gilbert & Brad Hughes
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
*/
require_once('../../config.php');
require_once ('lib.php');
global $CFG, $COURSE, $USER;
require_login();
$courseid = required_param('id', PARAM_INTEGER);
$ttuserid = required_param('userid',PARAM_INTEGER);
$clockin = optional_param('clockin', 0,PARAM_INTEGER);
$clockout = optional_param('clockout',0, PARAM_INTEGER);
$urlparams['id'] = $courseid;
$urlparams['userid'] = $ttuserid;
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$PAGE->set_course($course);
$context = $PAGE->context;
$timeclockurl = new moodle_url($CFG->wwwroot.'/blocks/timetracker/timeclock.php',$urlparams);
$index = new moodle_url($CFG->wwwroot.'/blocks/timetracker/index.php',$urlparams);
if(isset($_SERVER['HTTP_REFERER'])){
$nextpage = $_SERVER['HTTP_REFERER'];
} else {
$nextpage = $index;
}
$workerrecord = $DB->get_record('block_timetracker_workerinfo',
array('id'=>$ttuserid,'courseid'=>$courseid));
if(!$workerrecord){
print_error("NO WORKER FOUND!");
die;
}
if($workerrecord->active == 0){
$status = get_string('notactiveerror','block_timetracker');
} else if($clockin == 1){
$status = 'Clock in successful';
//protect against refreshing a 'clockin' screen
$pendingrecord= $DB->record_exists('block_timetracker_pending',
array('userid'=>$ttuserid,'courseid'=>$courseid));
if(!$pendingrecord){
$cin = new stdClass();
$cin->userid = $ttuserid;
$cin->timein = time();
$cin->courseid = $courseid;
$cisuccess = $DB->insert_record('block_timetracker_pending', $cin);
if($cisuccess){
add_to_log($COURSE->id, '', 'add clock-in',
"blocks/timetracker/index.php?id=$course->id&userid=$USER->id",
'TimeTracker clock-in.');
} else {
print_error('You tried to clock-in, but something went wrong.
We have logged the error. Please contact your supervisor.');
add_to_log($COURSE->id, '', 'error adding clock-in', ''.$COURSE->id,
'ERROR: TimeTracker clock-in failed.');
}
}
} else if ($clockout == 1){
$status = 'Clock out successful';
$cin = $DB->get_record('block_timetracker_pending',
array('userid'=>$ttuserid,'courseid'=>$courseid));
if($cin){
$nowtime = time();
//timein && timeout are same day
$cin->lastedited = $nowtime;
$cin->timeout = $nowtime;
$cin->lasteditedby = $USER->id;
$cin->payrate = $workerrecord->currpayrate;
unset($cin->id);
$worked = add_unit($cin);
if($worked){
$DB->delete_records('block_timetracker_pending',
array('userid'=>$ttuserid,'courseid'=>$courseid));
} else {
print_error(
'You tried to clock-out, but something went wrong. We have logged the
error. Please contact your supervisor.');
}
} else {
$status = 'No matching clock-in. Work unit not recorded';
add_to_log($COURSE->id, '', 'error finding clock-in', ''.$COURSE->id,
'ERROR: No Matching clock-in.');
}
}
redirect($nextpage, $status,1);