Example #1
0
function makeBinFromRaw($binRaw, $lang = "en")
{
    include_once "bintypes.php";
    include_once "databasefunctions.php";
    $newBin = new Collection();
    $newBin->BinId = (int) $binRaw[0];
    $newBin->OwnerId = (int) $binRaw[1];
    $newBin->Name = html_entity_decode($binRaw[2], ENT_QUOTES);
    $newBin->Type = new BinTypeAndIDPair((int) $binRaw[3], binTypes($binRaw[3], $lang));
    $newBin->BatteryLevel = (double) $binRaw[4];
    $newBin->CurrentWeight = 0;
    $history = GetBinHistory((int) $binRaw[0]);
    $highest = 0;
    for ($i = 0; $i < count($history); $i++) {
        if ($history[$i][3] > $highest) {
            $highest = $history[$i][3];
            $newBin->CurrentWeight = (int) $history[$i][2];
        }
    }
    return $newBin;
}
function EditBinWeight($id, $weight)
{
    include_once "exchangeRates.php";
    include_once "pointObject.php";
    include_once "bin.php";
    $binOld = makeBinFromRaw(GetBin($id), "en");
    $oldWeight = $binOld->CurrentWeight;
    $weightDiff = max(0, $weight - $oldWeight);
    $link = Connect();
    $time = time();
    $sql = "INSERT INTO `history` (binId, weight, unixStamp) VALUES ('{$id}', '{$weight}', '{$time}')";
    mysqli_query($link, $sql);
    $bin = GetBin($id);
    $typeId = (int) $bin[3];
    $typeName = binTypes($bin[3], "en");
    // award points
    $toAward = new PointObject();
    $points = array();
    switch ($typeName) {
        case "Waste":
            $toAward->Waste = $weightDiff * getExchangeRates()->waste;
            break;
        case "Plastic":
            $toAward->Plastic = $weightDiff * getExchangeRates()->plastic;
            break;
        case "Glass":
            $toAward->Glass = $weightDiff * getExchangeRates()->glass;
            break;
        case "Organic":
            $toAward->Organic = $weightDiff * getExchangeRates()->organic;
            break;
        case "Tin":
            $toAward->Tin = $weightDiff * getExchangeRates()->tin;
            break;
        case "Paper":
            $toAward->Paper = $weightDiff * getExchangeRates()->paper;
            break;
        case "Chemical":
            $toAward->Chemical = $weightDiff * getExchangeRates()->chemical;
            break;
    }
    $userId = (int) $bin[1];
    $user = makeUserFromRaw(GetUser($userId), "full");
    $newPoints = new PointObject();
    $newPoints->Add($user->Points);
    $newPoints->Add($toAward);
    $user->Points = $newPoints;
    $user->Points->RemoveNegative();
    EditUserPoints($user->UserId, json_encode($user->Points));
    //
    return $toAward;
}