PageRenderTime 48ms CodeModel.GetById 28ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/functions.php

https://bitbucket.org/nfredricks/wp-employee-time
PHP | 357 lines | 287 code | 62 blank | 8 comment | 31 complexity | 357a8c9eeca109c01d232f2aa5d224f3 MD5 | raw file
  1<?php
  2function start_tracking(){
  3  
  4  $gofs = get_option( 'gmt_offset' ); // get WordPress offset in hours
  5  $tz = date_default_timezone_get(); // get current PHP timezone
  6  date_default_timezone_set('Etc/GMT'.(($gofs < 0)?'+':'').-$gofs); // set the PHP timezone to match WordPress
  7  global $wpdb;
  8  $table_name = $wpdb->prefix ."employee_time";
  9
 10  $date = date('Y-m-d');
 11  $cur_time = date('H:i:s');
 12
 13  $track_check = track_check();
 14
 15  if($track_check['track']){
 16    if(!is_tracking($table_name, $wpdb)){
 17      $wpdb->insert( $table_name, array( 'user' => $track_check['id'], 'day' => $date, 'online' => 1, 'start' => $cur_time, 'end' => $cur_time) );
 18    }
 19  }
 20
 21  date_default_timezone_set($tz); // set the PHP timezone back the way it was
 22
 23}
 24
 25function end_tracking($wpdb){
 26  $gofs = get_option( 'gmt_offset' ); // get WordPress offset in hours
 27  $tz = date_default_timezone_get(); // get current PHP timezone
 28  date_default_timezone_set('Etc/GMT'.(($gofs < 0)?'+':'').-$gofs); // set the PHP timezone to match WordPress
 29  $table_name = $wpdb->prefix ."employee_time";
 30
 31  $date = date('Y-m-d');
 32  $cur_time = date('H:i:s');
 33
 34  $track_check = track_check();
 35
 36  if($track_check['track']){
 37    //update end time to right now
 38    $update = array('end' => $cur_time);
 39    $where = array('user' => $track_check['id'], 'day' => $date, 'online' => 1 );
 40    $wpdb->update($table_name, $update, $where);
 41
 42    //set user to offline
 43    remove_online($track_check['id'], $date, $table_name, $wpdb);
 44  }
 45
 46  date_default_timezone_set($tz); // set the PHP timezone back the way it was
 47}
 48
 49function remove_online($id, $date, $table_name, $wpdb){
 50  $update = array('online' => 0);
 51  $where = array('user' => $id, 'day' => $date, 'online'=> 1 );
 52  $wpdb->update($table_name, $update, $where);
 53}
 54
 55function track_check(){
 56  $user = wp_get_current_user();
 57  $roles = $user->roles;
 58  $id = $user->id;
 59  $types = get_option('tracking_types');
 60  $types = explode(',',$types);
 61  foreach($roles as $role){
 62    foreach($types as $type){
 63      if($role == trim($type)){
 64        return array('track' => true, 'id' => $id, 'roles' => $roles);
 65      }
 66    }
 67  }
 68  return array('track' => false, 'id' => $id, 'roles' => $roles);
 69}
 70function check_admin(){
 71  $user = wp_get_current_user();
 72  $roles = $user->roles;
 73  $id = $user->id;
 74  $type = 'administrator';
 75  foreach($roles as $role){
 76      if($role == trim($type)){
 77        return array('admin' => true, 'id' => $id, 'roles' => $roles);
 78      }
 79  }
 80  return array('admin' => false, 'id' => $id, 'roles' => $roles);
 81}
 82
 83function is_tracking($table_name, $wpdb, $id){
 84  if($id == ""){
 85    $user = wp_get_current_user();
 86    $id = $user->id;
 87  }
 88  $result = $wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE user = %s AND online = 1 ", $id));
 89  if($result[0] == NULL){
 90    return false;
 91  }
 92  return true;
 93}
 94
 95function time_since_last_up($cur_time, $table_name, $today, $wpdb){
 96
 97  $user = wp_get_current_user();
 98  $id = $user->id;
 99
100  $result = $wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE user = %s AND day = %s AND online = 1 ", $id, $today));
101  $end = $result[0]->end;
102
103  list($year, $month, $day) = explode("-", $today);
104  list($hour, $min, $sec) = explode(":",$end);
105  $end_utime = mktime($hour, $min, $sec, $month, $day, $year);
106
107  list($hour, $min, $sec) = explode(":",$cur_time);
108  $cur_utime = mktime($hour, $min, $sec, $month, $day, $year);
109  return $cur_utime - $end_utime;
110}
111
112function utime_from_datetime($date, $time){
113  list($year, $month, $day) = explode("-", $date);
114  list($hour, $min, $sec) = explode(":",$time);
115  return(mktime($hour, $min, $sec, $month, $day, $year));
116}
117
118
119
120function get_week($offset) {
121  $gofs = get_option( 'gmt_offset' ); // get WordPress offset in hours
122  $tz = date_default_timezone_get(); // get current PHP timezone
123  date_default_timezone_set('Etc/GMT'.(($gofs < 0)?'+':'').-$gofs); // set the PHP timezone to match WordPress
124    $date = date("Y-m-d");
125
126    if($offset != ""){
127      $date = date("Y-m-d", strtotime($offset." week"));
128    }
129
130    list($year, $month, $day) = explode("-", $date);
131
132    // Get the weekday of the given date
133    $wkday = date('l',mktime('0','0','0',$month,$day,$year));
134//echo $wkday;
135    switch($wkday) {
136        case 'Monday': $numDaysToMon = 0; break;
137        case 'Tuesday': $numDaysToMon = 1; break;
138        case 'Wednesday': $numDaysToMon = 2; break;
139        case 'Thursday': $numDaysToMon = 3; break;
140        case 'Friday': $numDaysToMon = 4; break;
141        case 'Saturday': $numDaysToMon = 5; break;
142        case 'Sunday': $numDaysToMon = 6; break;   
143    }
144
145
146    // Timestamp of the monday for that week
147    $monday = mktime('0','0','0', $month, $day-$numDaysToMon, $year);
148
149    $seconds_in_a_day = 86400;
150
151    // Get date for opt days from Monday (inclusive)
152    for($i=0; $i<7; $i++)
153    {
154        $dates[$i] = date('Y-m-d',$monday+($seconds_in_a_day*$i));
155    }
156      date_default_timezone_set($tz); // set the PHP timezone back the way it was
157    return $dates;
158}
159
160function time_for_day($date, $id, $wpdb, $table_name){
161  $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE user = %s AND day = %s", $id, $date));
162  $sum = 0;
163  foreach($results as $result){
164    $start_utime = utime_from_datetime($date, $result->start);
165    $end_utime = utime_from_datetime($date, $result->end);
166    $sum += $end_utime-$start_utime;
167  }
168  $hours = $sum/3600;
169  $round_up = number_format(round($hours*4)/4, 2);
170  $round_down = number_format(floor($hours*4)/4, 2);
171  $up_diff = abs($round_up-$hours);
172  $dn_diff = abs($round_down-$hours);
173  if($up_diff <= $dn_diff){
174    $num = $round_up;
175  }
176  else{
177    $num = $round_down;
178  }
179
180  return array('raw' => round($hours, 2), 'rounded' => $num);
181}
182
183function convert_number_to_words($number) {
184    
185    $hyphen      = '-';
186    $conjunction = ' and ';
187    $separator   = ', ';
188    $negative    = 'negative ';
189    $decimal     = ' point ';
190    $dictionary  = array(
191        0                   => 'zero',
192        1                   => 'one',
193        2                   => 'two',
194        3                   => 'three',
195        4                   => 'four',
196        5                   => 'five',
197        6                   => 'six',
198        7                   => 'seven',
199        8                   => 'eight',
200        9                   => 'nine',
201        10                  => 'ten',
202        11                  => 'eleven',
203        12                  => 'twelve',
204        13                  => 'thirteen',
205        14                  => 'fourteen',
206        15                  => 'fifteen',
207        16                  => 'sixteen',
208        17                  => 'seventeen',
209        18                  => 'eighteen',
210        19                  => 'nineteen',
211        20                  => 'twenty',
212        30                  => 'thirty',
213        40                  => 'fourty',
214        50                  => 'fifty',
215        60                  => 'sixty',
216        70                  => 'seventy',
217        80                  => 'eighty',
218        90                  => 'ninety',
219        100                 => 'hundred',
220        1000                => 'thousand',
221        1000000             => 'million',
222        1000000000          => 'billion',
223        1000000000000       => 'trillion',
224        1000000000000000    => 'quadrillion',
225        1000000000000000000 => 'quintillion'
226    );
227    
228    if (!is_numeric($number)) {
229        return false;
230    }
231    
232    if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
233        // overflow
234        trigger_error(
235            'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
236            E_USER_WARNING
237        );
238        return false;
239    }
240
241    if ($number < 0) {
242        return $negative . convert_number_to_words(abs($number));
243    }
244    
245    $string = $fraction = null;
246    
247    if (strpos($number, '.') !== false) {
248        list($number, $fraction) = explode('.', $number);
249    }
250    
251    switch (true) {
252        case $number < 21:
253            $string = $dictionary[$number];
254            break;
255        case $number < 100:
256            $tens   = ((int) ($number / 10)) * 10;
257            $units  = $number % 10;
258            $string = $dictionary[$tens];
259            if ($units) {
260                $string .= $hyphen . $dictionary[$units];
261            }
262            break;
263        case $number < 1000:
264            $hundreds  = $number / 100;
265            $remainder = $number % 100;
266            $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
267            if ($remainder) {
268                $string .= $conjunction . convert_number_to_words($remainder);
269            }
270            break;
271        default:
272            $baseUnit = pow(1000, floor(log($number, 1000)));
273            $numBaseUnits = (int) ($number / $baseUnit);
274            $remainder = $number % $baseUnit;
275            $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
276            if ($remainder) {
277                $string .= $remainder < 100 ? $conjunction : $separator;
278                $string .= convert_number_to_words($remainder);
279            }
280            break;
281    }
282    
283    if (null !== $fraction && is_numeric($fraction)) {
284        $string .= $decimal;
285        $words = array();
286        foreach (str_split((string) $fraction) as $number) {
287            $words[] = $dictionary[$number];
288        }
289        $string .= implode(' ', $words);
290    }
291    
292    return $string;
293}
294
295function update_timesheet_file() {
296    global $wpdb;
297
298    $opt_name = 'tracking_types';
299    $opt_val = get_option( $opt_name );
300    $types = explode(',', $opt_val);
301
302    $table_name = $wpdb->prefix . "usermeta"; 
303    $result = $wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE meta_key = 'wp_capabilities' "));
304    $user_id_array = array();
305    foreach($result as $user){
306        if(is_trackable($types, $user)){
307            array_push($user_id_array, $user->user_id);
308        }
309    }
310
311    $employ_table_name = $wpdb->prefix ."employee_time";
312    $objPHPExcel = new PHPExcel();
313
314    $objPHPExcel->getProperties()->setCreator("Timesheet Bot")
315                               ->setLastModifiedBy("Timesheet Bot")
316                               ->setTitle("Employee Timesheets")
317                               ->setSubject("Employee Timesheets")
318                               ->setDescription("Employee Timesheets");
319
320    for ($i = 0; $i < count($user_id_array); $i++) {
321        $result = $wpdb->get_results($wpdb->prepare("SELECT * FROM $employ_table_name WHERE user = %s ", $user_id_array[$i] ));
322        $user_handle = get_user_by('id', $user_id_array[$i]);
323
324        $objPHPExcel->createSheet();
325        $objPHPExcel->setActiveSheetIndex($i);
326        $objPHPExcel->getActiveSheet()->setTitle($user_handle->user_login);
327        foreach ($result as $user) {
328            //write contents
329
330        }
331        
332        
333
334    }
335    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
336    $objWriter->save(str_replace('functions.php', 'master_timesheet.xlsx', __FILE__));
337
338
339}
340
341function is_trackable($types, $user){
342
343$cap = key(unserialize($user->meta_value));  
344foreach($types as $type){
345    if($cap == trim($type)){
346        return true;
347    }
348}
349return false;
350
351}
352
353require_once('PHPExcel.php');
354require_once('PHPExcel/IOFactory.php');
355
356
357?>