function ws_isResourceAvailable($itemId, $qty)
{
    session_start();
    $userId = $_SESSION["userid"];
    $item = db_getItem($itemId);
    $result = true;
    if ($item->type == "PACKAGE") {
        $packageItems = db_getPackageItem($item->id);
        foreach ($packageItems as $packageItem) {
            $item = db_getItem($id);
            if (!ws_isResourceAvailable($item->id, $packageItem->quantity * $qty)) {
                return false;
            }
        }
    } else {
        $timeZoneId = db_getUserTimeZone($userId)->data;
        $item = db_getItem($itemId);
        $creditType = ws_getCreditTypeById($item->referenceid);
        $course = db_getCourseById($creditType->courseId);
        $policy = ws_getPolicyById($creditType->policyId, $timeZoneId);
        $dates = ws_getItemStartAndEndDates($policy, $timeZoneId);
        $startDate = $dates['startDate'];
        $endDate = $dates['endDate'];
        $quota = $policy->quotaInPeriod * $qty;
        try {
            $params = array('course' => $course->fullname, 'resourceType' => $creditType->resource, 'start' => $startDate, 'end' => $endDate, 'quota' => $quota);
            $client = new SoapClient(WSDL_VL, array('location' => LOCATION_VL));
            $response = $client->isResourceAvailable($params);
            $result = $response->success;
        } catch (Exception $e) {
            $result = false;
        } catch (SoapFault $soapfault) {
            $result = false;
        }
    }
    return $result;
}
function sto_getPolicyDescription($policyId, $timeZoneId)
{
    $policy = ws_getPolicyById($policyId, $timeZoneId);
    $policyType = $policy->policyType;
    $absolute = $policy->absolute;
    $compatibleTimezone = substr($timeZoneId, 10);
    date_default_timezone_set($compatibleTimezone);
    if ($policyType == "NOEXPIRATION") {
        $description .= $policy->quotaInPeriod . " minutes. ";
        $description .= "This item will not expire. ";
    } else {
        if ($policyType == "FIXED") {
            $description .= $policy->quotaInPeriod . " minutes. ";
            $daysFromStart = $policy->numberOfPeriods * $policy->daysInPeriod;
            if ($absolute) {
                $expDate = strtotime('+ ' . $daysFromStart . ' day', strtotime($policy->startDate));
                $description .= "This item can be used from " . date('F j, Y, g:i a', strtotime($policy->startDate)) . " until " . date('F j, Y, g:i a', $expDate) . ". ";
            } else {
                $description .= "This item will expire " . $daysFromStart . " days after purchase. ";
            }
        } else {
            if ($policyType == "GRADUAL") {
                $description .= $policy->quotaInPeriod . " minutes per period (" . $policy->numberOfPeriods . " periods of " . $policy->daysInPeriod . " days each). ";
                $description .= "A maximum of " . $policy->maximum . " minutes can be used each period. ";
                $daysFromStart = $policy->numberOfPeriods * $policy->daysInPeriod;
                if ($absolute) {
                    $expDate = strtotime('+ ' . $daysFromStart . ' day', strtotime($policy->startDate));
                    $description .= "This item can be used from " . date('F j, Y, g:i a', strtotime($policy->startDate)) . " until " . date('F j, Y, g:i a', $expDate) . ". ";
                } else {
                    $description .= "This item will expire " . $daysFromStart . " days after purchase. ";
                }
            } else {
                if ($policyType == "MINMAX") {
                    $description .= $policy->quotaInPeriod . " minutes per period (" . $policy->numberOfPeriods . " periods of " . $policy->daysInPeriod . " days each). ";
                    $description .= "A maximum of " . $policy->maximum . " minutes can be used each period. ";
                    $description .= "A minimum of " . $policy->minimum . " minutes must be used each period. Otherwise, they remaining minutes will expire. ";
                    $daysFromStart = $policy->numberOfPeriods * $policy->daysInPeriod;
                    if ($absolute) {
                        $expDate = strtotime('+ ' . $daysFromStart . ' day', strtotime($policy->startDate));
                        $description .= "This item can be used from " . date('F j, Y, g:i a', strtotime($policy->startDate)) . " until " . date('F j, Y, g:i a', $expDate) . ". ";
                    } else {
                        $description .= "This item will expire " . $daysFromStart . " days after purchase. ";
                    }
                }
            }
        }
    }
    return $description;
}