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