CTaskController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace App\Http\Controllers\Client;
  3. use Illuminate\Http\Request;
  4. use App\Http\Requests;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Task;
  7. use App\Models\Project;
  8. use App\Models\ActivityLog;
  9. use App\Models\Notification;
  10. use App\Models\Comment;
  11. use App\Models\AssignProject;
  12. use Auth;
  13. use App\User;
  14. class CTaskController extends Controller
  15. {
  16. public function add_task(Request $request){
  17. $name = $request->name;
  18. $proj_id = $request->proj_id;
  19. $description = $request->description;
  20. $estimated_time = $request->estimated_time;
  21. $task_type = $request->task_type;
  22. date_default_timezone_set("Asia/Dhaka");
  23. $cdate = date('Y-m-d H:i:s');
  24. $sav = new Task;
  25. $sav->name = $name;
  26. $sav->type = $task_type;
  27. $sav->proj_id = $proj_id;
  28. $sav->description = $description;
  29. $sav->created_time = $cdate;
  30. $sav->created_by = Auth::user()->id;
  31. $sav->status = 1;
  32. $sav->save();
  33. //last insert id
  34. $task_id = $sav->id;
  35. //activity log
  36. $msg = "Task: $name created.";
  37. $this->project_log($msg,$proj_id); //call function
  38. $this->task_log($msg,$task_id,$proj_id); //call function
  39. //notification
  40. $fet_pro = Project::find($proj_id);
  41. $projj_name = $fet_pro->name;
  42. $adminss = User::where('utype',1)->get();
  43. $post_by_name = Auth::user()->name;
  44. $post_by_id = Auth::user()->id;
  45. $msg = "Task:$name created in Proj:$projj_name by ".$post_by_name;
  46. foreach($adminss as $info){
  47. $this->notification($info->id, $msg);
  48. }
  49. $ass_usr = AssignProject::where('proj_id',$proj_id)->get();
  50. if(count($ass_usr) > 0){
  51. foreach($ass_usr as $info){
  52. if($info->user_id != $post_by_id){
  53. $this->notification($info->user_id, $msg);
  54. }
  55. }
  56. }
  57. //--end notification
  58. return redirect()->back()->with('message','Success');
  59. }
  60. public function remove_task($id)
  61. {
  62. $del = Task::find($id);
  63. //activity log
  64. $msg = "Task:".$del->name." removed.";
  65. $this->project_log($msg,$del->proj_id); //call function
  66. //notification
  67. $projj_name = Project::find($del->proj_id)->name;
  68. $name = $del->name;
  69. $adminss = User::where('utype',1)->get();
  70. $post_by_name = Auth::user()->name;
  71. $post_by_id = Auth::user()->id;
  72. $msg = "Task:$name removed from Proj:$projj_name by ".$post_by_name;
  73. foreach($adminss as $info){
  74. $this->notification($info->id, $msg);
  75. }
  76. $ass_usr = AssignProject::where('proj_id',$del->proj_id)->get();
  77. if(count($ass_usr) > 0){
  78. foreach($ass_usr as $info){
  79. if($info->user_id != $post_by_id){
  80. $this->notification($info->user_id, $msg);
  81. }
  82. }
  83. }
  84. //end notification
  85. $del->delete();
  86. return redirect()->back()->with('data','Delete successfully !!');
  87. }
  88. public function user_list_for_task_assign(Request $req){
  89. $proj_id = $req->proj_id;
  90. $task_id = $req->task_id;
  91. $all = AssignProject::with('one_user')->where('proj_id',$proj_id)->get();
  92. $str = "";
  93. $str .="<select>";
  94. foreach($all as $info){
  95. $str .="<option value={$info->user_id}>".$info->one_user->name."</option>";
  96. }
  97. $str .="</select>";
  98. //getting status of this task
  99. $task_sta = Task::find($task_id);
  100. $task_status = $task_sta->status;
  101. if(!empty($task_sta->description))
  102. {
  103. $details = $task_sta->description;
  104. }
  105. else{
  106. $details = "No details...";
  107. }
  108. //tast already assigned user
  109. $str1 = $this->fetch_task_assign_user($task_id); //call function
  110. //activity log fetch
  111. $logss = ActivityLog::with('one_user')->where('task_id',$task_id)->orderBy('id','DESC')->get();
  112. $str_log = "";
  113. foreach($logss as $log){
  114. $str_log .="* $log->msg -".$log->one_user->name."<br>";
  115. }
  116. //comments fetch
  117. $str_comment = $this->fetch_comments($task_id);
  118. $data[0] = $str;
  119. $data[1] = $str1;
  120. $data[2] = $str_log;
  121. $data[3] = $str_comment;
  122. $data[4] = $task_status;
  123. $data[5] = $details;
  124. return $data;
  125. }
  126. public function add_task_comment(Request $req){
  127. $comment = $req->comment;
  128. $task_id = $req->task_id;
  129. date_default_timezone_set("Asia/Dhaka");
  130. $assign_time = date('Y-m-d H:i:s');
  131. if(!empty($comment) && $task_id > 0){
  132. $sav = new Comment;
  133. $sav->comment = $comment;
  134. $sav->task_id = $task_id;
  135. $sav->date_time = $assign_time;
  136. $sav->post_by = Auth::user()->id;
  137. $sav->save();
  138. }
  139. //activity log
  140. $task_info = Task::find($task_id);
  141. $task_name = $task_info->name;
  142. $proj_id = $task_info->proj_id;
  143. $user_id = $task_info->user_id;
  144. $msg = "$comment -added to $task_name.";
  145. $this->task_log($msg,$task_id,$proj_id); //call function
  146. //notification
  147. $fetch_proj = Project::find($proj_id);
  148. $projj_name = $fetch_proj->name;
  149. $adminss = User::where('utype',1)->get();
  150. $notification_post_by = Auth::user()->name;
  151. $noti_post_by_id = Auth::user()->id;
  152. $msg = "A comment added in Task:$task_name of Proj:$projj_name by ".$notification_post_by;
  153. foreach($adminss as $info){
  154. $this->notification($info->id, $msg);
  155. }
  156. $this->notification($user_id, $msg);
  157. //--------end notification
  158. $str = $this->fetch_comments($task_id);
  159. return $str;
  160. }
  161. public function fetch_comments($task_id){
  162. $fetch = Comment::with('user')->where('task_id',$task_id)->orderBy('id','DESC')->get();
  163. $str = "";
  164. if(count($fetch) > 0){
  165. foreach($fetch as $fet)
  166. {
  167. $immmgg = "assets/document/task/$fet->url";
  168. $str .="<input type='hidden' value=".$fet->id.">";
  169. $str .="* $fet->comment -by ".$fet->user->name;
  170. if(!empty($fet->url)){
  171. $str .="<br/><a target='_blank' href=".url("assets/document/task/$fet->url").">
  172. <img height='150' width='120' src='".asset($immmgg)."'></a>";
  173. }
  174. $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>
  175. <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/>";
  176. }
  177. }
  178. return $str;
  179. }
  180. public function change_to_process(Request $req)
  181. {
  182. $task_id = $req->task_id;
  183. $status = $req->status;
  184. date_default_timezone_set("Asia/Dhaka");
  185. $t_time = date('Y-m-d H:i:s');
  186. $sav = Task::find($task_id);
  187. $sav->status = $status;
  188. $task_name = $sav->name;
  189. if($status == 2){
  190. $sav->start_time = $t_time;
  191. }
  192. elseif($status == 6){
  193. $sav->closing_time = $t_time;
  194. }
  195. //fetch proj_id for activity log
  196. $proj_id = $sav->proj_id;
  197. if($sav->save())
  198. {
  199. $text = "";
  200. if($status == 2){ $text = "in-process"; }
  201. elseif($status == 3){ $text = "completed"; }
  202. elseif($status == 4){ $text = "checking"; }
  203. elseif($status == 5){ $text = "bug-fixing"; }
  204. elseif($status == 6){ $text = "done"; }
  205. $msg = "$task_name -Change status to: $text";
  206. $this->task_log($msg,$task_id,$proj_id); //call function
  207. return "Yes";
  208. }
  209. else{
  210. return "No";
  211. }
  212. }
  213. public function task_log($msg,$task_id,$proj_id)
  214. {
  215. date_default_timezone_set("Asia/Dhaka");
  216. $assign_time = date('Y-m-d H:i:s');
  217. $sav = new ActivityLog;
  218. $sav->msg = $msg;
  219. $sav->task_id = $task_id;
  220. $sav->proj_id = $proj_id;
  221. $sav->logged_user_id = Auth::user()->id;
  222. $sav->created_at = $assign_time;
  223. $sav->save();
  224. }
  225. public function project_log($msg,$proj_id)
  226. {
  227. date_default_timezone_set("Asia/Dhaka");
  228. $assign_time = date('Y-m-d H:i:s');
  229. $sav = new ActivityLog;
  230. $sav->msg = $msg;
  231. $sav->proj_id = $proj_id;
  232. $sav->logged_user_id = Auth::user()->id;
  233. $sav->created_at = $assign_time;
  234. $sav->save();
  235. }
  236. public function fetch_task_assign_user($task_id)
  237. {
  238. $fetch = Task::with('user')->find($task_id);
  239. $str = "";
  240. if($fetch->user_id > 0){
  241. $str .="<input type='hidden' value=".$fetch->id.">";
  242. $str .= $fetch->user->name."<br>Estimated time: ".$fetch->estimated_time;
  243. }
  244. else{
  245. $str .= "No user assigned";
  246. }
  247. return $str;
  248. }
  249. public function notification($user_id,$msg){
  250. $sav = new Notification;
  251. $sav->user_id = $user_id;
  252. $sav->msg = $msg;
  253. $sav->status = 1;
  254. $sav->save();
  255. }
  256. }