Няма описание

Objective2Controller.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <?php
  2. use Illuminate\Support\Facades\Auth;
  3. class Objective2Controller extends \BaseController
  4. {
  5. /**
  6. * Display a listing of the resource.
  7. *
  8. * @return Response
  9. */
  10. /**
  11. * Show the form for creating a new resource.
  12. *
  13. * @return Response
  14. */
  15. public function isObjectiveUnique($input, $existing_Objective = NULL)
  16. {
  17. Log::info('isObjectiveUnique');
  18. if (Input::get('program_id') != 0)
  19. $program_id = $input['program_id'];
  20. else
  21. $program_id = NULL;
  22. $saved_Objective = DB::table('objectives')->join('objective_outcome', 'objective_outcome.objective_id', '=', 'objectives.id')
  23. ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
  24. ->where('objectives.text', '=', $input['text'])
  25. ->whereIn('objective_outcome.outcome_id', $input['outcome_id'])
  26. ->whereIn('objective_program.program_id', $input['program_id'])
  27. ->get();
  28. if (count($saved_Objective))
  29. return false;
  30. else
  31. return true;
  32. }
  33. private function makeValidator($clean_input)
  34. {
  35. /** Validation rules */
  36. return Validator::make(
  37. array(
  38. 'text' => $clean_input['text'],
  39. 'outcome_id' => $clean_input['outcome_id'],
  40. 'program_id' => $clean_input['program_id']
  41. ),
  42. array(
  43. 'text' => 'required|string',
  44. 'outcome_id' => 'required|array',
  45. 'program_id' => 'required|array'
  46. )
  47. );
  48. }
  49. private function cleanInput()
  50. {
  51. $clean_input = array();
  52. $clean_input['text'] = trim(preg_replace('/\t+/', '', Input::get('text')));
  53. $clean_input['outcome_id'] = Input::get('outcome');
  54. $counter = Input::get('counter') + 0;
  55. Log::info($clean_input);
  56. foreach ($clean_input['outcome_id'] as $index => $outcome_id) {
  57. $clean_input['outcome_id'][$index] = trim(preg_replace('/\t+/', '', $clean_input['outcome_id'][$index]));
  58. }
  59. $clean_input['program_id'] = Input::get('program_id');
  60. Log::info(Input::get('program_id'));
  61. return $clean_input;
  62. }
  63. private function cleanAssocInput()
  64. {
  65. $clean_input = array();
  66. $clean_input['text'] = trim(preg_replace('/\t+/', '', Input::get('text')));
  67. $clean_input['outcome_id'] = Input::get('assoc_outcome');
  68. Log::info(Input::all());
  69. $clean_input['program_id'] = Input::get('program_id');
  70. return $clean_input;
  71. }
  72. public function fetchObjective()
  73. {
  74. return Objective::find(Input::get('id'));
  75. }
  76. public function fetchObjectiveWithTrashed()
  77. {
  78. $json = array();
  79. $json['program'] = DB::select("select program_id from objective_program where objective_id = ?", array(Input::get('id')));
  80. $json['outcome'] = DB::select("select outcome_id from objective_outcome outc where outc.objective_id = ?", array(Input::get('id')));
  81. $json['objective'] = DB::select("select text, id from objectives where id =?", array(Input::get('id')));
  82. $json['assessment'] = DB::select("select * from assessments where activity_criterion_id in (select id from activity_criterion where criterion_id in (select criterion_id from criterion_objective_outcome where objective_id = ?))", array(Input::get('id')));
  83. $json['assoc_criteria'] = DB::select("select name from criteria where id in(select criterion_id from criterion_objective_outcome where objective_id =?)", array(Input::get('id')));
  84. Log::info('is here');
  85. return json_encode($json);
  86. }
  87. public function fetchAllobjectives()
  88. {
  89. $program_id = Input::get('program_fetch');
  90. $outcome_id = Input::get('outcome_fetch');
  91. $json = array();
  92. $json['objective'] = DB::select("SELECT * FROM `objectives` where id in (select objective_id from objective_outcome where outcome_id ={$outcome_id}) and id in (select objective_id from objective_program where program_id = {$program_id})");
  93. return json_encode($json);
  94. }
  95. public function delete()
  96. {
  97. DB::delete("delete from objectives where id = ?", array(Input::get('deleteObj')));
  98. $role = Auth::user()['role'];
  99. switch ($role) {
  100. case 1:
  101. return Redirect::to('objectives')->withInput();
  102. case 2:
  103. return Redirect::to('school-objectives')->withInput();
  104. case 3:
  105. return Redirect::to('program-objectives')->withInput();
  106. }
  107. }
  108. /**
  109. * Create a new Objective.
  110. *
  111. * @return Redirect Redirect back to form page
  112. */
  113. public function create()
  114. {
  115. $clean_input = $this->cleanInput();
  116. /** Validation rules */
  117. $validator = $this->makeValidator($clean_input);
  118. /** If validation fails */
  119. if ($validator->fails()) {
  120. /** Prepare error message */
  121. $message = '<p>Error(s) creating a new Objective:</p><ul>';
  122. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  123. $message .= $validationError;
  124. }
  125. $message .= '</ul>';
  126. /** Send error message and old data */
  127. Session::flash('status', 'danger');
  128. Session::flash('message', $message);
  129. $role = Auth::user()['role'];
  130. switch ($role) {
  131. case 1:
  132. return Redirect::to('objective')->withInput();
  133. case 2:
  134. return Redirect::to('school-objective')->withInput();
  135. case 3:
  136. return Redirect::to('program-objective')->withInput();
  137. }
  138. } else {
  139. // Check Objective uniqueness
  140. if (!$this->isObjectiveUnique($clean_input)) {
  141. /** Send error message and old data */
  142. Session::flash('status', 'danger');
  143. Session::flash('message', "This objective is a duplicate of an already saved Objective because it's and associated program are the same.");
  144. $role = Auth::user()['role'];
  145. switch ($role) {
  146. case 1:
  147. return Redirect::to('objective')->withInput();
  148. case 2:
  149. return Redirect::to('school-objective')->withInput();
  150. case 3:
  151. return Redirect::to('program-objective')->withInput();
  152. }
  153. }
  154. /** Instantiate new Objective */
  155. $objective = new Objective;
  156. $objective->text = $clean_input['text'];
  157. /** If Objective is saved, send success message */
  158. if ($objective->save()) {
  159. $objectiveId = $objective->id;
  160. foreach ($clean_input['program_id'] as $program_id) {
  161. DB::insert("insert into objective_program (objective_id, program_id) values({$objectiveId},{$program_id})");
  162. }
  163. foreach ($clean_input['outcome_id'] as $outcome_id) {
  164. DB::insert("insert into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  165. /*if (!($objectiveOutcome->save())) {
  166. Session::flash('status', 'danger');
  167. Session::flash('message', '<p>Error creating objective. Please try again later.</p>');
  168. return Redirect::to('objective')->withInput();
  169. }*/
  170. }
  171. Session::flash('status', 'success');
  172. Session::flash('message', 'Objective created: "' . $objective->text . '".');
  173. $role = Auth::user()['role'];
  174. switch ($role) {
  175. case 1:
  176. return Redirect::to('objective')->withInput(Input::only('outcome_id'));
  177. case 2:
  178. return Redirect::to('school-objective')->withInput(Input::only('outcome_id'));
  179. case 3:
  180. return Redirect::to('program-objective')->withInput(Input::only('outcome_id'));
  181. }
  182. }
  183. /** If saving fails, send error message and old data */
  184. else {
  185. Session::flash('status', 'danger');
  186. Session::flash('message', '<p>Error creating objective. Please try again later.</p>');
  187. $role = Auth::user()['role'];
  188. switch ($role) {
  189. case 1:
  190. return Redirect::to('objective')->withInput();
  191. case 2:
  192. return Redirect::to('school-objective')->withInput();
  193. case 3:
  194. return Redirect::to('program-objective')->withInput();
  195. }
  196. }
  197. }
  198. }
  199. /**
  200. * Store a newly created resource in storage.
  201. *
  202. * @return Response
  203. */
  204. public function store()
  205. {
  206. //
  207. }
  208. /**
  209. * Display the specified resource.
  210. *
  211. * @param int $id
  212. * @return Response
  213. */
  214. public function show($id)
  215. {
  216. //
  217. }
  218. /**
  219. * Show the form for editing the specified resource.
  220. *
  221. * @param int $id
  222. * @return Response
  223. */
  224. public function edit()
  225. {
  226. $title = "Objective";
  227. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  228. $objectives = Objective::withTrashed()->orderBy('text', 'ASC')->get();
  229. $programs = Program::orderBy('name', 'ASC')->get();
  230. return View::make('local.managers.admins.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  231. }
  232. public function editProgram()
  233. {
  234. $userProgram = Auth::user()['id'];
  235. Log::info(Auth::user());
  236. $userProgram = DB::select("select program_user.program_id from program_user where user_id = {$userProgram}");
  237. $title = "Objective";
  238. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  239. $objectives = Objective::withTrashed()->orderBy('text', 'ASC')->get();
  240. $programs = Program::where("id", '=', $userProgram[0]->program_id)->get();
  241. return View::make('local.managers.pCoords.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  242. }
  243. public function editSchool()
  244. {
  245. $userSchool = Auth::user()['school_id'];
  246. Log::info($userSchool);
  247. $title = "Objective";
  248. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  249. $objectives = Objective::withTrashed()->orderBy('text', 'ASC')->get();
  250. $programs = Program::where("school_id", "=", $userSchool)->orderBy('name', 'ASC')->get();
  251. return View::make('local.managers.sCoords.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  252. }
  253. /**
  254. * Update the specified resource in storage.
  255. *
  256. * @param int $id
  257. * @return Response
  258. */
  259. public function update()
  260. {
  261. $Objective = Objective::withTrashed()->find(Input::get('id'));
  262. $clean_input = $this->cleanAssocInput();
  263. Log::info(print_r($clean_input, true));
  264. /** Validation rules */
  265. $validator = $this->makeValidator($clean_input);
  266. /** If validation fails */
  267. if ($validator->fails()) {
  268. /** Prepare error message */
  269. $message = 'Error(s) updating the Objective: <ul>';
  270. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  271. $message .= $validationError;
  272. }
  273. $message .= '</ul>';
  274. /** Send error message and old data */
  275. //Session::flash('status', 'danger');
  276. //Session::flash('message', $message);
  277. $MessageArray = array('status' => 'danger', 'message' => $message);
  278. $role = Auth::user()['role'];
  279. switch ($role) {
  280. case 1:
  281. return $MessageArray;
  282. return Redirect::to('objective')->withInput();
  283. case 2:
  284. return $MessageArray;
  285. return Redirect::to('school-objective')->withInput();
  286. case 3:
  287. return $MessageArray;
  288. return Redirect::to('program-objective')->withInput();
  289. }
  290. } else {
  291. /** Set info */
  292. Log::info($clean_input);
  293. $Objective->text = $clean_input['text'];
  294. // Set status
  295. /** If Objective is updated, send success message */
  296. if ($Objective->save()) {
  297. $objectiveId = $Objective->id;
  298. DB::delete("delete from `objective_outcome` where objective_id ={$objectiveId}");
  299. DB::delete("delete from objective_program where objective_id = {$objectiveId}");
  300. foreach ($clean_input['program_id'] as $program_id) {
  301. DB::insert("insert into `objective_program`(objective_id, program_id) values ({$objectiveId},{$program_id})");
  302. }
  303. foreach ($clean_input['outcome_id'] as $outcome_id) {
  304. DB::insert("insert into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  305. }
  306. //Session::flash('status', 'success');
  307. //Session::flash('message', 'Updated Objective: "' . $Objective->text . '"');
  308. $MessageArray = array('status' => 'success', 'message' => 'Updated Objective: "' . $Objective->text . '"');
  309. $role = Auth::user()['role'];
  310. switch ($role) {
  311. case 1:
  312. return $MessageArray;
  313. return Redirect::to('objective')->withInput();
  314. case 2:
  315. return $MessageArray;
  316. return Redirect::to('school-objective')->withInput();
  317. case 3:
  318. return $MessageArray;
  319. return Redirect::to('program-objective')->withInput();
  320. }
  321. }
  322. /** If saving fails, send error message and old data */
  323. else {
  324. //Session::flash('status', 'danger');
  325. ////Session::flash('message', 'Error updating the Objective. Please try again later.');
  326. $MessageArray = array('status' => 'danger', 'message' => 'Error updating the Objective. Please try again later.');
  327. $role = Auth::user()['role'];
  328. switch ($role) {
  329. case 1:
  330. return $MessageArray;
  331. return Redirect::to('objective')->withInput();
  332. case 2:
  333. return $MessageArray;
  334. return Redirect::to('school-objective')->withInput();
  335. case 3:
  336. return $MessageArray;
  337. return Redirect::to('program-objective')->withInput();
  338. }
  339. }
  340. }
  341. }
  342. /**
  343. * Remove the specified resource from storage.
  344. *
  345. * @param int $id
  346. * @return Response
  347. */
  348. public function destroy($id)
  349. {
  350. //
  351. }
  352. }