UTaskController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <?php
  2. namespace App\Http\Controllers\User;
  3. use Illuminate\Http\Request;
  4. use App\Http\Requests;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Project;
  7. use App\Models\Group;
  8. use App\Models\Task;
  9. use App\Models\ActivityLog;
  10. use App\Models\Notification;
  11. use App\Models\AssignProject;
  12. use App\Models\Comment;
  13. use App\User;
  14. use Auth;
  15. class UTaskController extends Controller
  16. {
  17. public function task_details(Request $req){
  18. $proj_id = $req->proj_id;
  19. $task_id = $req->task_id;
  20. $all = AssignProject::with('one_user')->where('proj_id',$proj_id)->get();
  21. $str = "";
  22. $str .="<select>";
  23. foreach($all as $info){
  24. $str .="<option value={$info->user_id}>".$info->one_user->name."</option>";
  25. }
  26. $str .="</select>";
  27. //getting status of this task
  28. $task_sta = Task::find($task_id);
  29. $task_status = $task_sta->status;
  30. //tast already assigned user
  31. $str1 = $this->fetch_task_assign_user($task_id); //call function
  32. //activity log fetch
  33. $logss = ActivityLog::with('one_user')->where('task_id',$task_id)->orderBy('id','DESC')->get();
  34. $str_log = "";
  35. foreach($logss as $log){
  36. $str_log .="* $log->msg -".$log->one_user->name."<br>";
  37. }
  38. //comments fetch
  39. $str_comment = $this->fetch_comments($task_id);
  40. $data[0] = $str;
  41. $data[1] = $str1;
  42. $data[2] = $str_log;
  43. $data[3] = $str_comment;
  44. $data[4] = $task_status;
  45. return $data;
  46. }
  47. public function add_task(Request $request){
  48. $name = $request->name;
  49. $proj_id = $request->proj_id;
  50. $p=Project::find($proj_id);
  51. $office_id=$p->office_id;
  52. $description = $request->description;
  53. $dead_line = $request->dead_line;
  54. $task_type = empty($request->task_type)?1:2;
  55. date_default_timezone_set("Asia/Dhaka");
  56. $cdate = date('Y-m-d H:i:s');
  57. $sav = new Task;
  58. $sav->name = $name;
  59. $sav->type = $task_type;
  60. $sav->proj_id = $proj_id;
  61. $sav->user_id = Auth::user()->id;
  62. $sav->assign_by = Auth::user()->id;
  63. $sav->assign_time = $cdate;
  64. $sav->dead_line = $dead_line;
  65. $sav->description = $description;
  66. $sav->created_time = $cdate;
  67. $sav->created_by = Auth::user()->id;
  68. $sav->status = 1;
  69. $sav->office_id = $office_id;
  70. $sav->save();
  71. $task_id = $sav->id;
  72. //assing task
  73. // $sav_assign_task = new AssignTask;
  74. // $sav_assign_task->task_id = $task_id;
  75. // $sav_assign_task->user_id = Auth::user()->id;
  76. // $sav_assign_task->assign_by = Auth::user()->id;
  77. // $sav_assign_task->assign_time = $cdate;
  78. // $sav_assign_task->estimated_time = $estimated_time;
  79. // $sav_assign_task->status = 1;
  80. // $sav_assign_task->save();
  81. $msg = "Task: $name created.";
  82. $this->project_log($msg,$proj_id); //call function
  83. $this->task_log($msg,$task_id,$proj_id); //call function
  84. //notification
  85. $fet_pro = Project::find($proj_id);
  86. $projj_name = $fet_pro->name;
  87. $client_id = $fet_pro->client_id;
  88. $adminss = User::where('utype',1)->get();
  89. $msg = "Task:$name created in Proj:$projj_name by ".Auth::user()->name;
  90. foreach($adminss as $info){
  91. $this->notification($info->id, $msg);
  92. }
  93. $this->notification($client_id, $msg);
  94. //--end notification
  95. return redirect()->back()->with('message','Success');
  96. }
  97. public function remove_task($id)
  98. {
  99. $del = Task::find($id);
  100. //activity log
  101. $msg = "Task:".$del->name." removed.";
  102. $this->project_log($msg,$del->proj_id); //call function
  103. //notification
  104. $projj_name = Project::find($del->proj_id)->name;
  105. $name = $del->name;
  106. $adminss = User::where('utype',1)->get();
  107. $post_by_name = Auth::user()->name;
  108. $post_by_id = Auth::user()->id;
  109. $msg = "Task:$name removed from Proj:$projj_name by ".$post_by_name;
  110. foreach($adminss as $info){
  111. $this->notification($info->id, $msg);
  112. }
  113. $ass_usr = AssignProject::where('proj_id',$del->proj_id)->get();
  114. if(count($ass_usr) > 0){
  115. foreach($ass_usr as $info){
  116. if($info->user_id != $post_by_id){
  117. $this->notification($info->user_id, $msg);
  118. }
  119. }
  120. }
  121. //end notification
  122. $del->delete();
  123. return redirect()->back()->with('data','Delete successfully !!');
  124. }
  125. public function change_tast_by_proj(Request $req)
  126. {
  127. $proj_id = $req->proj_id;
  128. $all = Task::where('proj_id',$proj_id)->get();
  129. $str = "";
  130. $str .="<select>";
  131. $str .="<option value='all'>All Tast</option>";
  132. foreach($all as $info){
  133. $str .="<option value=".$info->id.">".$info->name."</option>";
  134. }
  135. $str .="</select>";
  136. return $str;
  137. }
  138. public function user_list_for_task_assign(Request $req){
  139. $proj_id = $req->proj_id;
  140. $task_id = $req->task_id;
  141. $all = AssignProject::with('one_user')->where('proj_id',$proj_id)->get();
  142. $str = "";
  143. $str .="<select>";
  144. foreach($all as $info){
  145. $str .="<option value={$info->user_id}>".$info->one_user->name."</option>";
  146. }
  147. $str .="</select>";
  148. //getting status of this task
  149. $task_sta = Task::find($task_id);
  150. $task_status = $task_sta->status;
  151. if(!empty($task_sta->description))
  152. {
  153. $details = $task_sta->description;
  154. }
  155. else{
  156. $details = "No details...";
  157. }
  158. //tast already assigned user
  159. $str1 = $this->fetch_task_assign_user($task_id); //call function
  160. //activity log fetch
  161. $logss = ActivityLog::with('one_user')->where('task_id',$task_id)->orderBy('id','DESC')->get();
  162. $str_log = "";
  163. foreach($logss as $log){
  164. $str_log .="* $log->msg -".$log->one_user->name."<br>";
  165. }
  166. //comments fetch
  167. $str_comment = $this->fetch_comments($task_id);
  168. $data[0] = $str;
  169. $data[1] = $str1;
  170. $data[2] = $str_log;
  171. $data[3] = $str_comment;
  172. $data[4] = $task_status;
  173. $data[5] = $details;
  174. return $data;
  175. }
  176. public function task_assign_to_user(Request $req){
  177. $task_id = $req->id_task_id;
  178. $estimated_time = $req->id_estimated_time;
  179. $user_id = $req->id_user_id;
  180. date_default_timezone_set("Asia/Dhaka");
  181. $cdate = date('Y-m-d H:i:s');
  182. $sav = Task::find($task_id);
  183. //fetch proj_id for activity log
  184. $proj_id = $sav->proj_id;
  185. $task_name = $sav->name;
  186. $sav->user_id = $user_id;
  187. $sav->assign_by = Auth::user()->id;
  188. $sav->assign_time = $cdate;
  189. $sav->estimated_time = $estimated_time;
  190. $sav->save();
  191. //activity log
  192. $nam = User::find($user_id);
  193. $msg = "User: $nam->name added";
  194. $this->task_log($msg,$task_id,$proj_id); //call function
  195. //notification
  196. $projj_name = Project::find($proj_id)->name;
  197. $adminss = User::where('utype',1)->get();
  198. $post_by_name = Auth::user()->name;
  199. $post_by_id = Auth::user()->id;
  200. $msg = "$nam->name assigned in Task:$task_name of Proj:$projj_name by ".$post_by_name;
  201. foreach($adminss as $info){
  202. $this->notification($info->id, $msg);
  203. }
  204. if($user_id != $post_by_id){
  205. $this->notification($user_id, $msg);
  206. }
  207. //--------end notification
  208. $str = $this->fetch_task_assign_user($task_id);
  209. return $str;
  210. }
  211. // public function remove_assign_task_user(Request $req){
  212. // $id = $req->id;
  213. // $task_id = $req->task_id;
  214. // $del = AssignTask::find($id);
  215. // //activity log
  216. // $nam = User::find($del->user_id);
  217. // $msg = "User: $nam->name removed";
  218. // $this->task_log($msg,$task_id); //call function
  219. // $del->delete();
  220. // $str = $this->fetch_task_assign_user($task_id); //call function
  221. // return $str;
  222. // }
  223. public function add_task_comment(Request $req){
  224. $comment = $req->comment;
  225. $task_id = $req->task_id;
  226. date_default_timezone_set("Asia/Dhaka");
  227. $assign_time = date('Y-m-d H:i:s');
  228. if(!empty($comment) && $task_id > 0){
  229. $sav = new Comment;
  230. $sav->comment = $comment;
  231. $sav->task_id = $task_id;
  232. $sav->date_time = $assign_time;
  233. $sav->post_by = Auth::user()->id;
  234. $sav->save();
  235. }
  236. //activity log
  237. $task_info = Task::find($task_id);
  238. $task_name = $task_info->name;
  239. $proj_id = $task_info->proj_id;
  240. $user_id = $task_info->user_id;
  241. $msg = "$comment -added to $task_name.";
  242. $this->task_log($msg,$task_id,$proj_id); //call function
  243. //notification
  244. $fetch_proj = Project::find($proj_id);
  245. $projj_name = $fetch_proj->name;
  246. $client_id = $fetch_proj->client_id;
  247. $adminss = User::where('utype',1)->get();
  248. $notification_post_by = Auth::user()->name;
  249. $noti_post_by_id = Auth::user()->id;
  250. $msg = "A comment added in Task:$task_name of Proj:$projj_name by ".$notification_post_by;
  251. foreach($adminss as $info){
  252. $this->notification($info->id, $msg);
  253. }
  254. $this->notification($client_id, $msg);
  255. $str = $this->fetch_comments($task_id);
  256. return $str;
  257. }
  258. public function fetch_task_assign_user($task_id)
  259. {
  260. $fetch = Task::with('user')->find($task_id);
  261. $str = "";
  262. if($fetch->user_id > 0){
  263. $str .="<input type='hidden' value=".$fetch->id.">";
  264. $str .= $fetch->user->name."<br>Estimated time: ".$fetch->estimated_time;
  265. }
  266. else{
  267. $str .= "No user assigned";
  268. }
  269. return $str;
  270. }
  271. public function task_log($msg,$task_id,$proj_id)
  272. {
  273. date_default_timezone_set("Asia/Dhaka");
  274. $assign_time = date('Y-m-d H:i:s');
  275. $sav = new ActivityLog;
  276. $sav->msg = $msg;
  277. $sav->task_id = $task_id;
  278. $sav->proj_id = $proj_id;
  279. $sav->logged_user_id = Auth::user()->id;
  280. $sav->created_at = $assign_time;
  281. $sav->save();
  282. }
  283. public function project_log($msg,$proj_id)
  284. {
  285. date_default_timezone_set("Asia/Dhaka");
  286. $assign_time = date('Y-m-d H:i:s');
  287. $sav = new ActivityLog;
  288. $sav->msg = $msg;
  289. $sav->proj_id = $proj_id;
  290. $sav->logged_user_id = Auth::user()->id;
  291. $sav->created_at = $assign_time;
  292. $sav->save();
  293. }
  294. public function fetch_comments($task_id){
  295. $fetch = Comment::with('user')->where('task_id',$task_id)->orderBy('id','DESC')->get();
  296. $str = "";
  297. $login_user = Auth::user()->id;
  298. if(count($fetch) > 0){
  299. foreach($fetch as $fet)
  300. {
  301. $immmgg = "assets/document/task/$fet->url";
  302. $str .="<input type='hidden' value=".$fet->id.">";
  303. $str .="* $fet->comment -by ".$fet->user->name;
  304. if(!empty($fet->url)){
  305. $str .="<br/><a target='_blank' href=".url("assets/document/task/$fet->url").">
  306. <img height='150' width='120' src='".asset($immmgg)."'></a>";
  307. }
  308. $str .="<span style='color:#FD4B39;cursor:pointer;margin-left:10px' data-id='".$fet->id."' class='edit_comment pull-right'><i class='fa fa-pencil-square-o' aria-hidden='true'></i></span>
  309. <span style='color:#FD4B39;cursor:pointer;' data-id='".$fet->id."' class='remove_comment'><i class='fa fa-times pull-right' aria-hidden='true'></i></span><br/>";
  310. }
  311. }
  312. return $str;
  313. }
  314. public function change_to_process(Request $req)
  315. {
  316. $task_id = $req->task_id;
  317. $user_id = Auth::user()->id;
  318. $status = $req->status;
  319. date_default_timezone_set("Asia/Dhaka");
  320. $t_time = date('Y-m-d H:i:s');
  321. $sav = Task::find($task_id);
  322. $task_name = $sav->name;
  323. if($user_id = $sav->user_id){
  324. $sav->status = $status;
  325. if($status == 2){
  326. $sav->start_time = $t_time;
  327. }
  328. elseif($status == 6){
  329. $sav->closing_time = $t_time;
  330. }
  331. //fetch proj_id for activity log
  332. $proj_id = $sav->proj_id;
  333. $sav->save();
  334. $text = "";
  335. if($status == 2){ $text = "in-process"; }
  336. elseif($status == 3){ $text = "completed"; }
  337. elseif($status == 4){ $text = "checking"; }
  338. elseif($status == 5){ $text = "bug-fixing"; }
  339. elseif($status == 6){ $text = "done"; }
  340. $msg = "$task_name -Change status to: $text";
  341. $this->task_log($msg,$task_id,$proj_id); //call function
  342. return "Yes";
  343. }
  344. return "No";
  345. }
  346. public function change_to_inprocess(Request $req){
  347. $task_id = $req->task_id;
  348. $status = $req->status;
  349. $user_id = Auth::user()->id;
  350. date_default_timezone_set("Asia/Dhaka");
  351. $t_time = date('Y-m-d H:i:s');
  352. $sav = Task::find($task_id);
  353. $task_name = $sav->name;
  354. if($user_id = $sav->user_id){
  355. $sav->status = $status;
  356. if($status == 2){
  357. $sav->start_time = $t_time;
  358. }
  359. elseif($status == 6){
  360. $sav->closing_time = $t_time;
  361. }
  362. //fetch proj_id for activity log
  363. $proj_id = $sav->proj_id;
  364. $sav->save();
  365. $text = "";
  366. if($status == 2){ $text = "in-process"; }
  367. elseif($status == 6){ $text = "done"; }
  368. $msg = "$task_name -Change status to: $text";
  369. $this->task_log($msg,$task_id,$proj_id); //call function
  370. return "Yes";
  371. }
  372. }
  373. public function notification($user_id,$msg){
  374. $sav = new Notification;
  375. $sav->user_id = $user_id;
  376. $sav->msg = $msg;
  377. $sav->status = 1;
  378. $sav->save();
  379. }
  380. }