From: Nik Okuntseff Date: Fri, 22 Jul 2016 21:29:12 +0000 (+0000) Subject: Some refactoring and commenting. X-Git-Tag: timetracker_1.19-1~1669 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=f74704271ae5d37aee3fdf941f3462c8b92ad0fd;p=timetracker.git Some refactoring and commenting. --- diff --git a/dbinstall.php b/dbinstall.php index 2b875518..c32695d5 100755 --- a/dbinstall.php +++ b/dbinstall.php @@ -700,7 +700,7 @@ if ($_POST) { setChange("OPTIMIZE TABLE tt_fav_reports"); setChange("OPTIMIZE TABLE tt_invoices"); setChange("OPTIMIZE TABLE tt_log"); - setChange("OPTIMIZE TABLE tt_monthly_quota"); + setChange("OPTIMIZE TABLE tt_monthly_quotas"); setChange("OPTIMIZE TABLE tt_project_task_binds"); setChange("OPTIMIZE TABLE tt_projects"); setChange("OPTIMIZE TABLE tt_tasks"); diff --git a/plugins/CustomFields.class.php b/plugins/CustomFields.class.php index 1c2483a1..e1b42b80 100644 --- a/plugins/CustomFields.class.php +++ b/plugins/CustomFields.class.php @@ -4,6 +4,27 @@ // +----------------------------------------------------------------------+ // | Copyright (c) Anuko International Ltd. (https://www.anuko.com) // +----------------------------------------------------------------------+ +// | LIBERAL FREEWARE LICENSE: This source code document may be used +// | by anyone for any purpose, and freely redistributed alone or in +// | combination with other software, provided that the license is obeyed. +// | +// | There are only two ways to violate the license: +// | +// | 1. To redistribute this code in source form, with the copyright +// | notice or license removed or altered. (Distributing in compiled +// | forms without embedded copyright notices is permitted). +// | +// | 2. To redistribute modified versions of this code in *any* form +// | that bears insufficient indications that the modifications are +// | not the work of the original author(s). +// | +// | This license applies to this document only, not any other software +// | that it may be combined with. +// | +// +----------------------------------------------------------------------+ +// | Contributors: +// | https://www.anuko.com/time_tracker/credits.htm +// +----------------------------------------------------------------------+ class CustomFields { diff --git a/plugins/MonthlyQuota.class.php b/plugins/MonthlyQuota.class.php index 0ad73d8b..3516fe4a 100644 --- a/plugins/MonthlyQuota.class.php +++ b/plugins/MonthlyQuota.class.php @@ -1,21 +1,49 @@ db = getConnection(); $i18n = $GLOBALS['I18N']; $this->holidays = $i18n->holidays; global $user; - $this->usersTeamId = $user->team_id; + $this->team_id = $user->team_id; } - + + // update - deletes a quota, then inserts a new one. public function update($year, $month, $quota) { - $teamId = $this->usersTeamId; + $teamId = $this->team_id; $deleteSql = "DELETE FROM tt_monthly_quotas WHERE year = $year AND month = $month AND team_id = $teamId"; $this->db->exec($deleteSql); if ($quota){ @@ -25,17 +53,18 @@ class MonthlyQuota { } return true; } - + + // get - obains either a single month quota or an array of quotas for an entire year. public function get($year, $month) { if (is_null($month)){ return $this->getMany($year); } - return $this->getSingle($year, $month); } - public function getDailyWorkingHours(){ - $teamId = $this->usersTeamId; + // getWorkdayHours - obtains workday_hours value for a team from the database. + public function getWorkdayHours(){ + $teamId = $this->team_id; $sql = "SELECT workday_hours FROM tt_teams where id = $teamId"; $reader = $this->db->query($sql); if (is_a($reader, 'PEAR_Error')) { @@ -43,11 +72,12 @@ class MonthlyQuota { } $row = $reader->fetchRow(); - return $row["workday_hours"]; + return $row['workday_hours']; } - + + // getSingle - obtains a quota for a single month. private function getSingle($year, $month) { - $teamId = $this->usersTeamId; + $teamId = $this->team_id; $sql = "SELECT quota FROM tt_monthly_quotas WHERE year = $year AND month = $month AND team_id = $teamId"; $reader = $this->db->query($sql); if (is_a($reader, 'PEAR_Error')) { @@ -55,26 +85,24 @@ class MonthlyQuota { } $row = $reader->fetchRow(); + if ($row) + return $row['quota']; - // if we don't find a record, return calculated monthly quota - if (is_null($row)){ - - $holidaysWithYear = array(); - foreach ($this->holidays as $day) { - $parts = explode("/", $day); - $holiday = "$year-$parts[0]-$parts[1]"; - array_push($holidaysWithYear, $holiday); - } - - $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); - return $this->getWorkingDays("$year-$month-01", "$year-$month-$daysInMonth", $holidaysWithYear) * $this->getDailyWorkingHours(); + // If we did not find a record, return a calculated monthly quota. + $holidaysWithYear = array(); + foreach ($this->holidays as $day) { + $parts = explode("/", $day); + $holiday = "$year-$parts[0]-$parts[1]"; + array_push($holidaysWithYear, $holiday); } - - return $row["quota"]; - } + $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); + return $this->getWorkingDays("$year-$month-01", "$year-$month-$daysInMonth", $holidaysWithYear) * $this->getWorkdayHours(); + } + + // getMany - returns an array of quotas for a given year for team. private function getMany($year){ - $teamId = $this->usersTeamId; + $teamId = $this->team_id; $sql = "SELECT month, quota FROM tt_monthly_quotas WHERE year = $year AND team_id = $teamId"; $result = array(); $res = $this->db->query($sql); @@ -83,15 +111,17 @@ class MonthlyQuota { } while ($val = $res->fetchRow()) { - $result[$val["month"]] = $val["quota"]; - // $result[] = $val; + $result[$val['month']] = $val['quota']; } return $result; } - //The function returns the no. of business days between two dates and it skips the holidays + // The function returns the number of business days between two dates skipping holidays. private function getWorkingDays($startDate, $endDate, $holidays) { + // TODO: this function needs a fix, as it assumes Sat + Sun weekends, + // and will not work properly for other week types (ex. Arabic weeks). + // do strtotime calculations just once $endDate = strtotime($endDate); $startDate = strtotime($startDate); @@ -157,4 +187,4 @@ class MonthlyQuota { return $workingDays; } -} \ No newline at end of file +} diff --git a/quotas.php b/quotas.php index 83107ad7..1dfa06fa 100644 --- a/quotas.php +++ b/quotas.php @@ -97,7 +97,7 @@ if ($request->isPost()){ $monthsData = $quota->get($selectedYear); $form = new Form('monthlyQuotasForm'); -$form->addInput(array('type'=>'text', 'name'=>'workdayHours', 'value'=>$quota->getDailyWorkingHours(), 'style'=>'width:50px')); +$form->addInput(array('type'=>'text', 'name'=>'workdayHours', 'value'=>$quota->getWorkdayHours(), 'style'=>'width:50px')); $form->addInput(array('type'=>'combobox','name'=>'year','data'=>$years,'datakeys'=>array('id','name'),'value'=>$selectedYear,'onchange'=>'yearChange(this.value);')); for ($i=0; $i < count($months); $i++) { $value = "";