Exemplo n.º 1
0
 /**
  * @api {post} /managers/:idUser/assign Assign Content by Manager
  * @apiName Assign Content by Manager
  * @apiGroup Manager
  * @apiHeader (Header) {String} X_Authorization Authorization value.
  * @apiParam  (url Parameter) {Number} idUser User unique ID.
  * @apiParam  {Number} idBin Bin's unique ID.
  * @apiParam  {Number} idUser User's unique ID. The change will apply to this user.
  * 
  * @apiError 400 Input Invalid. This will happen if the param is missing or not the valid format.
  * @apiError 404 Not found. This will happen if the bin id/user id/course id/sale id is not in our system.
  * @apiError 401 Not authorized. This will happen if the header value is not attached.
  * @apiError 403 The user is not a manager yet. 
  * @apiError 409 User already enrolled.
  * @apiErrorExample {json} Error-Response:
  *     HTTP/1.1 412 Precondition Failed. User doesn't complete the prerequisite course.
  *      [
  *       {
  *           "id": 45951,
  *           "code": "Test",
  *           "name": "TurboChef: High h conveyor C",
  *           "manufacturer": "TurboChef",
  *           "shortDescription": null,
  *           "description": null,
  *           "note": null,
  *          "isPublished": 1,
  *           "time": 90,
  *           "type": "course",
  *           "safety": 1,
  *           "html5": 1,
  *           "video": null,
  *           "thumbnail": "http://localhost:9090/ignitor-api/courses/thumbnail/45951"
  *       }
  *   ]    
  *
  *
  */
 public static function assignContent($idUser)
 {
     $app = \Slim\Slim::getInstance();
     $request = $app->request->post();
     $validata = $app->validata;
     $validator = $validata::key('idBin', $validata::digit()->notEmpty())->key('idUser', $validata::digit()->notEmpty());
     if (!$validator->validate($request)) {
         $app->halt("400", json_encode("Input Invalid"));
     }
     if (!GroupController::isMemberOfAdmin($request['idUser'], $idUser)) {
         $app->halt("403", json_encode("Permission denied."));
     }
     if ($idUser != $request['idUser'] && GroupController::isManagerOfAdmin($request['idUser'], $idUser)) {
         $app->halt("403", json_encode("Permission denied. You can only transfer content to manager user."));
     }
     $bin_id = $request['idBin'];
     $bin = Manager_Bin::where('id', '=', $bin_id)->lockForUpdate()->first();
     if (!$bin) {
         $app->halt("404", json_encode("manager content record does not exist"));
     }
     if ($bin->user_id != $idUser) {
         $app->halt("401");
     }
     if ($bin->quantity < 1) {
         $app->halt("404", json_encode("No available seat found."));
     }
     $sale = Price::find($bin->course_sale_id);
     if (!$sale) {
         $app->halt("404", json_encode("sale record does not exist"));
     }
     $isEnroll = EnrollmentController::isEnroll($sale->course_id, $request['idUser']);
     if ($isEnroll) {
         $app->halt("409", json_encode("This User already has this content."));
     }
     EnrollmentController::meetPrerequisite($sale->course_id, $request['idUser']);
     $enrollment = EnrollmentController::enroll($sale->course_id, $request['idUser'], $bin->expiration_dt);
     if ($enrollment) {
         $bin->seats()->attach($enrollment, array('sender_id' => $idUser, 'receiver_id' => $request['idUser'], 'course_sale_id' => $bin->course_sale_id));
         $bin = Manager_Bin::where('id', '=', $bin_id)->lockForUpdate()->first();
         if ($bin->quantity < 1) {
             $app->halt("404", json_encode("No available seat found."));
         }
         $bin->quantity = $bin->quantity - 1;
         $bin->save();
     }
 }