説明なし

Objective2Controller.php 12KB

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