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);
 }
 private static function authCheck()
 {
     $app = \Slim\Slim::getInstance();
     $token = $app->request->headers->get('X_Authorization');
     $auth = Auth_Token::where('token', '=', $token)->first();
     if (!$auth) {
         $app->halt(401, json_encode('Unauthorized'));
     }
     $idUser = $auth->user_id;
     if (!User::find($idUser)) {
         $app->halt(401, json_encode('user not exist'));
     }
     return $idUser;
 }
 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;
 }
 public static function launchCourse($idCourse, $token, $page)
 {
     $app = \Slim\Slim::getInstance();
     if (!Course::find($idCourse)) {
         $app->halt("404");
     }
     $auth = Auth_Token::where('token', '=', $token)->first();
     if (!$auth || Enrollment::where('user_id', '=', $auth->user_id)->where('course_id', '=', $idCourse)->count() == 0) {
         $app->response->headers->set('Content-Type', 'text/html');
         $app->render('course_launch_401.php');
         $app->stop();
     }
     FileController::readCourse($idCourse, $page);
 }
 /**
  * @api {get} /admin/idUserLogout/logout Logout a user
  * @apiName Let Ignitor Labs Super Admin force logout a user
  * @apiDescription  - Destroys the tocken
  * @apiGroup Ignitor Super Admin
  * @apiHeader (Header) {String} X_Authorization Token
  * @apiParam  (url Parameter) {Number} idUserLogout Users unique ID.
  *
  * @apiError 401 Not authorized. This will happen if the header value is not attached.
  * @apiError 404 User not found.
  * @apiError 403 Permission denied. This will happen if the user is not a ignitor super admin.
  * @apiSuccessExample {boolean} Success-Response:
  *     HTTP/1.1 200 OK
  *     1
  */
 public static function adminLogout($idUserLogout)
 {
     $app = \Slim\Slim::getInstance();
     $user = User::find($idUserLogout);
     if (!$user) {
         $app->response->setStatus(400);
         return "User does not exist";
     } else {
         $app->response->setStatus(200);
         $auth = Auth_Token::where('user_id', '=', $idUserLogout)->first();
         self::removeToken($auth->id);
         return 1;
     }
 }