public static function price() { $app = \Slim\Slim::getInstance(); $data = $app->request->post(); $course_sales = isset($data['course_sales']) ? $data['course_sales'] : null; $coupons = isset($data['coupons']) ? $data['coupons'] : null; $token = AuthController::getToken($app->request->headers); $auth = Auth_Token::where('token', '=', $token)->first(); if (!$auth) { $app->response->setStatus(401); return 0; } $price_array = explode(",", $course_sales); $coupon_array = explode(",", $coupons); $result = array(); //go get price and course details and put in memory so that we minimize hits to DB $details_array = array(); foreach ($price_array as $price_id) { try { $price = Price::find($price_id); $course = $price->course; array_push($details_array, array("course_id" => $course->id, "price_id" => $price->id, "price" => $course->price)); } catch (Exception $ex) { } } foreach ($coupon_array as $code) { $code = trim($code); try { $valid_coupon = Coupon::valid()->where("code", "=", $code)->first(); } catch (Exception $e) { $valid_coupon = false; } if ($valid_coupon) { //check to see if the course exists $course_sale_id = $valid_coupon->course_sale_id; for ($i = 0; $i < sizeof($details_array); $i++) { if ($course_sale_id == $details_array[$i]["price_id"]) { $price = $details_array[$i]["price"]; $value = $valid_coupon->value; $type = $valid_coupon->type; $price_change = 0; if ($type == "percent-discount") { $price_change = $price * $value; } elseif ($type == "flat-discount") { $price_change = $value; } array_push($result, array("course_sale_id" => $course_sale_id, "code" => $code, "comments" => $valid_coupon->comments, "price" => $price, "price_change" => $price_change, "new_price" => $price - $price_change)); array_splice($details_array, $i, 1); break; } } } } $app->response->setStatus(200); return json_encode($result); }
public static function getEnrollmentByCourseId($idCourse) { $app = \Slim\Slim::getInstance(); if (!Course::find($idCourse)) { $app->halt("404", "course not found"); } $token = AuthController::getToken($app->request->headers); $auth = Auth_Token::where('token', '=', $token)->first(); if (!$auth) { $app->halt("401"); } $idUser = $auth->user_id; $enrollment = Enrollment::where('user_id', '=', $idUser)->where('course_id', '=', $idCourse)->first(); if (!$enrollment) { $app->halt("401"); } return $enrollment; }