Skip to content
This repository has been archived by the owner on Dec 6, 2019. It is now read-only.

Commit

Permalink
numerous bug fixes due to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nabeel Shahzad committed Mar 25, 2011
1 parent e8bdd47 commit c1ee072
Show file tree
Hide file tree
Showing 19 changed files with 1,694 additions and 1,338 deletions.
7 changes: 4 additions & 3 deletions core/common/FinanceData.class.php
Expand Up @@ -255,7 +255,7 @@ public static function getFlightPercentExpenses() {
/**
* Add an expense
*/
public static function AddExpense($name, $cost, $type) {
public static function addExpense($name, $cost, $type) {

if ($name == '' || $cost == '') {
self::$lasterror = 'Name and cost must be entered';
Expand All @@ -281,7 +281,8 @@ public static function AddExpense($name, $cost, $type) {
/**
* Edit a certain expense
*/
public static function EditExpense($id, $name, $cost, $type) {
public static function editExpense($id, $name, $cost, $type) {

if ($name == '' || $cost == '') {
self::$lasterror = 'Name and cost must be entered';
return false;
Expand All @@ -306,7 +307,7 @@ public static function EditExpense($id, $name, $cost, $type) {
/**
* Delete an expense
*/
public static function RemoveExpense($id) {
public static function removeExpense($id) {
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'expenses
WHERE `id`=' . $id;

Expand Down
81 changes: 65 additions & 16 deletions core/common/PIREPData.class.php
Expand Up @@ -1047,6 +1047,11 @@ public static function deleteFlightReport($pirepid) {
self::UpdatePIREPFeed();
}

/**
* PIREPData::deleteAllRouteDetails()
*
* @return
*/
public static function deleteAllRouteDetails() {
$sql = "UPDATE " . TABLE_PREFIX . "pireps
SET `route_details` = ''";
Expand All @@ -1060,7 +1065,13 @@ public static function deleteAllRouteDetails() {
return true;
}

public static function UpdatePIREPFeed() {
/**
* PIREPData::updatePIREPFeed()
*
* @return
*/
public static function updatePIREPFeed() {

# Load PIREP into RSS feed
$reports = PIREPData::findPIREPS(array(), 10);
$rss = new RSSFeed('Latest Pilot Reports', SITE_URL, 'The latest pilot reports');
Expand All @@ -1081,7 +1092,15 @@ public static function UpdatePIREPFeed() {
$rss->BuildFeed(LIB_PATH . '/rss/latestpireps.rss');
}


/**
*
* @deprecated Use isPIREPUnderAge()
*
*/
public static function PIREPUnderAge($pirepid, $age_hours) {
return self::isPIREPUnderAge($pirepid, $age_hours);
}

/**
* Return true if a PIREP if under $age_hours old
*
Expand All @@ -1090,7 +1109,8 @@ public static function UpdatePIREPFeed() {
* @return bool True/false
*
*/
public static function PIREPUnderAge($pirepid, $age_hours) {
public static function isPIREPUnderAge($pirepid, $age_hours) {

$pirepid = intval($pirepid);

$sql = "SELECT pirepid
Expand All @@ -1104,7 +1124,7 @@ public static function PIREPUnderAge($pirepid, $age_hours) {
return false;
}

return true;
return true;
}

/**
Expand Down Expand Up @@ -1161,8 +1181,6 @@ public static function changePIREPStatus($pirepid, $status) {

if($status == PIREP_ACCEPTED) {

PilotData::updateFlightData($pirep_details->pilotid, $pirep_details->flighttime, 1);

# Pay per-schedule
if(!empty($pirep_details->payforflight)) {

Expand All @@ -1173,10 +1191,10 @@ public static function changePIREPStatus($pirepid, $status) {
DB::query($sql);

} else { # Pay by hour
PilotData::updatePilotPay($pirep_details->pilotid, $pirep_details->flighttime);
PilotData::updatePilotPay($pirep_details->pilotid, $pirep_details->flighttime, true);
}

SchedulesData::incrementFlownCount($pirep_details->code, $pirep_details->flightnum);
SchedulesData::changeFlownCount($pirep_details->code, $pirep_details->flightnum, '+1');

} elseif($status == PIREP_REJECTED) {
// Do nothing, since nothing in the PIREP was actually counted
Expand All @@ -1185,10 +1203,23 @@ public static function changePIREPStatus($pirepid, $status) {
} elseif($pirep_details->accepted == PIREP_ACCEPTED) { # If already accepted

if($status == PIREP_REJECTED) {
PilotData::updateFlightData($pirep_details->pilotid, -1 * floatval($pirep->flighttime), -1);

# Subtract their pay for the rejected flight
if(!empty($pirep_details->payforflight)) {
$sql = 'UPDATE '.TABLE_PREFIX."pilots
SET totalpay=totalpay-{$pirep_details->payforflight}
WHERE pilotid={$pirep_details->pilotid}";

DB::query($sql);
} else {
PilotData::updatePilotPay($pirep_details->pilotid, $pirep_details->flighttime, false);
}

SchedulesData::changeFlownCount($pirep_details->code, $pirep_details->flightnum, '-1');
}
}

PilotData::updatePilotStats($pirep_details->pilotid);
RanksData::calculateUpdatePilotRank($pirep_details->pilotid);
PilotData::generateSignature($pirep_details->pilotid);
StatsData::updateTotalHours();
Expand All @@ -1213,6 +1244,11 @@ public static function addComment($pirepid, $pilotid, $comment) {
}


/**
* PIREPData::getAllFields()
*
* @return
*/
public static function getAllFields() {
return DB::get_results('SELECT * FROM ' . TABLE_PREFIX . 'pirepfields');
}
Expand Down Expand Up @@ -1247,7 +1283,7 @@ public static function getFieldValue($fieldid, $pirepid) {
/**
* Add a custom field to be used in a PIREP
*/
public static function AddField($title, $type = '', $values = '') {
public static function addField($title, $type = '', $values = '') {
$fieldname = strtoupper(str_replace(' ', '_', $title));
//$values = DB::escape($values);

Expand All @@ -1266,7 +1302,8 @@ public static function AddField($title, $type = '', $values = '') {
/**
* Edit the field
*/
public static function EditField($id, $title, $type, $values = '') {
public static function editField($id, $title, $type, $values = '') {

$fieldname = strtoupper(str_replace(' ', '_', $title));

$sql = "UPDATE " . TABLE_PREFIX . "pirepfields
Expand All @@ -1283,17 +1320,21 @@ public static function EditField($id, $title, $type, $values = '') {
/**
* Save PIREP fields
*/
public static function SaveFields($pirepid, $list) {
public static function saveFields($pirepid, $list) {

if (!is_array($list) || $pirepid == '') return false;

$allfields = self::getAllFields();

if (!$allfields) return true;

foreach ($allfields as $field) {

// See if that value already exists
$sql = 'SELECT id FROM ' . TABLE_PREFIX . 'pirepvalues
WHERE fieldid=' . $field->fieldid . ' AND pirepid=' . $pirepid;
$sql = 'SELECT id FROM '.TABLE_PREFIX.'pirepvalues
WHERE fieldid='.$field->fieldid.
' AND pirepid=' . $pirepid;

$res = DB::get_row($sql);

$fieldname = str_replace(' ', '_', $field->name);
Expand All @@ -1316,7 +1357,14 @@ public static function SaveFields($pirepid, $list) {
return true;
}

public static function DeleteField($id) {
/**
* PIREPData::deleteField()
*
* @param mixed $id
* @return
*/
public static function deleteField($id) {

$sql = 'DELETE FROM ' . TABLE_PREFIX . 'pirepfields WHERE fieldid=' . $id;

$res = DB::query($sql);
Expand All @@ -1336,7 +1384,8 @@ public static function DeleteField($id) {
*
* @deprecated
*/
public static function ShowReportCounts($ret = false) {
public static function showReportCounts($ret = false) {

// Recent PIREP #'s
$max = 0;
$data = array();
Expand Down
56 changes: 37 additions & 19 deletions core/common/PilotData.class.php
Expand Up @@ -461,15 +461,15 @@ public static function saveAvatar($code, $pilotid, $_FILES) {
/**
* Accept the pilot (allow them into the system)
*/
public static function AcceptPilot($pilotid) {
public static function acceptPilot($pilotid) {
return self::updateProfile($pilotid, array('confirmed' => PILOT_ACCEPTED,
'retired' => '0'));
}

/**
* Reject a pilot
*/
public static function RejectPilot($pilotid) {
public static function rejectPilot($pilotid) {
return self::DeletePilot($pilotid);
}

Expand Down Expand Up @@ -573,18 +573,24 @@ public static function updateFlightHours($pilotid) {
* @return bool Success
*
*/
public static function updateFlightData($pilotid, $flighttime, $numflights = 1) {
public static function updateFlightData($pilotid) {

return self::updatePilotStats($pilotid);

/*
# Update the flighttime
$pilotdata = PilotData::getPilotData($pilotid);
$flighttime = Util::AddTime($pilotdata->totalhours, $flighttime);
$flighttime = Util::addTime($pilotdata->totalhours, $flighttime);
if ($numflights == '') $numflights = 1;
$params = array('totalhours' => $flighttime, 'totalflights' => ($pilotdata->
totalflights + $numflights), );
$params = array(
'totalhours' => $flighttime,
'totalflights' => ($pilotdata->totalflights + $numflights),
);
return self::updateProfile($pilotid, $params);
*/
}

/**
Expand All @@ -595,24 +601,26 @@ public static function updateFlightData($pilotid, $flighttime, $numflights = 1)
*/
public static function updatePilotStats($pilotid) {

$pireps = PIREPData::findPIREPS(array('p.pilotid' => $pilotid));
$pireps = PIREPData::findPIREPS(array(
'p.pilotid' => $pilotid,
'p.accepted' => PIREP_ACCEPTED
)
);

$totalpireps = 0;
$totalhours = 0;

if (is_array($pireps)) {
if(is_array($pireps) && count($pireps) > 0) {
foreach ($pireps as $p) {
if ($p->accepted != PIREP_ACCEPTED) {
continue;
}

$totalpireps++;
$totalhours = Util::addTime($p->flighttime, $totalhours);
}
}


$params = array('totalhours' => $totalhours, 'totalflights' => $totalpireps, );
$params = array(
'totalhours' => $totalhours,
'totalflights' => $totalpireps,
);

return self::updateProfile($pilotid, $params);
}
Expand Down Expand Up @@ -713,10 +721,13 @@ public static function resetPilotPay($pilotid) {
*
* @param int $pilotid The pilot ID
* @param int $flighthours Number of hours to pay the pilot for
* @param bool $add If true, add the amount, otherwise, subtract it
* @return bool Success
*
*/
public static function updatePilotPay($pilotid, $flighthours) {
public static function updatePilotPay($pilotid, $flighthours, $add = true) {

$pilotid=intval($pilotid);

$sql = 'SELECT payrate
FROM ' . TABLE_PREFIX . 'ranks r, ' . TABLE_PREFIX . 'pilots p
Expand All @@ -725,10 +736,16 @@ public static function updatePilotPay($pilotid, $flighthours) {
$payrate = DB::get_row($sql);

$payupdate = self::getPilotPay($flighthours, $payrate->payrate);

if($add === false) {
$add = '-';
} else {
$add = '+';
}

$sql = 'UPDATE ' . TABLE_PREFIX . 'pilots
SET totalpay=totalpay+' . $payupdate . '
WHERE pilotid=' . $pilotid;
$sql = 'UPDATE ' . TABLE_PREFIX . "pilots
SET totalpay=totalpay {$add} {$payupdate}
WHERE pilotid=".$pilotid;

DB::query($sql);

Expand Down Expand Up @@ -820,7 +837,8 @@ public static function findRetiredPilots() {
* @return bool Success value
*
*/
public static function SaveFields($pilotid, $list) {
public static function saveFields($pilotid, $list) {

$allfields = RegistrationData::getCustomFields(true);

if (!$allfields) return true;
Expand Down
26 changes: 23 additions & 3 deletions core/common/SchedulesData.class.php
Expand Up @@ -174,21 +174,40 @@ public static function getProperFlightNum($flightnum) {
* @return bool
*
*/
public static function IncrementFlownCount($code, $flightnum) {
public static function incrementFlownCount($code, $flightnum) {
return self::changeFlownCount($code, $flightnum, '+1');
}


/**
* SchedulesData::changeFlownCount()
*
* @param mixed $code
* @param mixed $flightnum
* @param mixed $amount
* @return void
*/
public static function changeFlownCount($code, $flightnum, $amount) {

$schedid = intval($schedid);

$code = strtoupper($code);
$flightnum = strtoupper($flightnum);

if(substr_count($amount, '+') == 0) {
$amount = '+'.$amount;
}

$sql = 'UPDATE ' . TABLE_PREFIX . "schedules
SET timesflown=timesflown+1
WHERE code='{$code}' AND flightnum='{$flightnum}'";
SET timesflown=timesflown {$amount}
WHERE code='{$code}' AND flightnum='{$flightnum}'";

$res = DB::query($sql);

if (DB::errno() != 0) return false;

return true;

}


Expand Down Expand Up @@ -502,6 +521,7 @@ public static function updateScheduleFields($scheduleid, $fields) {
*
*/
public static function editScheduleFields($scheduleid, $fields) {

if (!is_array($fields)) {
return false;
}
Expand Down

0 comments on commit c1ee072

Please sign in to comment.