/** * Init Progression * * @param int $roomId * @param int $taskId * @param int $userId * @param int $partnerId */ public static function init($roomId, $taskId, $userId, $partnerId) { // Create Progression for the user Progression::create(['room_id' => $roomId, 'task_id' => $taskId, 'user_id' => $userId]); // Create Progression for the partner Progression::create(['room_id' => $roomId, 'task_id' => $taskId, 'user_id' => $partnerId]); }
public function delete($userId, $accepted, Request $request) { $user = User::where('id', '=', $userId)->first(); // Elle accepte donc on reset les tâches if ($accepted == 1) { Progression::where('room_id', '=', $user->room_id)->delete(); } Sorry::where('room_id', '=', $user->room_id)->delete(); // Success return Response::json(['status_code' => 200, 'success' => 'Done ....']); }
/** * GET /api/dashboard * * @param $userId * @param $partnerId * @param Request $request * * @return */ public function get($userId, $partnerId, Request $request) { // Get current room $room = $request->room; $progressions = Progression::where('room_id', '=', $room->id)->get(); $tasksIds = []; $tasks = []; // Get all tasks use in the room foreach ($progressions as $value) { if (!in_array($value->task_id, $tasksIds)) { $tasksIds[] = $value->task_id; $task = Task::with('category')->where('id', '=', $value->task_id)->first(); $userProgression = Progression::where('room_id', '=', $room->id)->where('user_id', '=', $userId)->where('task_id', '=', $task->id)->first(['count']); $partnerProgression = Progression::where('room_id', '=', $room->id)->where('user_id', '=', $partnerId)->where('task_id', '=', $task->id)->first(['count']); $tasks[] = ['id' => $task->id, 'category' => $task->category, 'name' => $task->name, 'difference' => $userProgression->count - $partnerProgression->count]; } } // Return response return Response::json(['status_code' => 200, 'tasks' => $tasks]); }
/** * DELETE /rooms/{roomId} * * @param $roomId * */ public function deleteRoom($roomId) { Room::where('id', '=', $roomId)->delete(); Progression::where('id', '=', $roomId)->delete(); }
/** * Get tasks history for profiles page * * @param int $userId * @param int $partnerId * @param Request $request */ public function tasksProfiles($userId, $partnerId, Request $request) { $user = User::where('id', '=', $userId)->with('room')->first(); $partner = User::where('id', '=', $partnerId)->first(); $roomId = $user->room->id; $tasks = []; foreach (Category::all() as $category) { $tasks[] = ['category' => $category->category, 'name' => $category->name, 'me' => 0, 'partner' => 0, 'me_name' => $user->name, 'partner_name' => $partner->name]; } $progressions = Progression::with('task')->where('room_id', '=', $roomId)->get(); foreach ($progressions as $pro) { $task = Task::where('id', '=', $pro->task_id)->first(); $category = Category::where('id', '=', $task->category_id)->first(); if ($pro->user_id == $userId) { for ($i = 0; $i < count($tasks); $i++) { if ($tasks[$i]['category'] == $category->category) { $tasks[$i]['me'] += $pro->count; } } } if ($pro->user_id == $partnerId) { for ($i = 0; $i < count($tasks); $i++) { if ($tasks[$i]['category'] == $category->category) { $tasks[$i]['partner'] += $pro->count; } } } } // Success return Response::json(['status_code' => 200, 'data' => $tasks]); }