Açıklama Yok

Objective2Controller.php 12KB

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