暂无描述

Objective2Controller.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. //edit == true or edit == false
  34. private function makeValidator($clean_input, $edit)
  35. {
  36. /** Validation rules */
  37. if ($edit) {
  38. return Validator::make(
  39. array(
  40. //'text' => $clean_input['text'],
  41. 'outcome_id' => $clean_input['outcome_id'],
  42. 'program_id' => $clean_input['program_id']
  43. ),
  44. array(
  45. 'outcome_id' => 'required|array',
  46. 'program_id' => 'required|array'
  47. )
  48. );
  49. } else {
  50. return Validator::make(
  51. array(
  52. 'text' => $clean_input['text'],
  53. 'outcome_id' => $clean_input['outcome_id'],
  54. 'program_id' => $clean_input['program_id']
  55. ),
  56. array(
  57. 'text' => 'required|string|unique:objectives,text',
  58. 'outcome_id' => 'required|array',
  59. 'program_id' => 'required|array'
  60. )
  61. );
  62. }
  63. }
  64. private function cleanInput()
  65. {
  66. $clean_input = array();
  67. $clean_input['text'] = trim(preg_replace('/\t+/', '', Input::get('text')));
  68. $clean_input['outcome_id'] = Input::get('outcome');
  69. $counter = Input::get('counter') + 0;
  70. // Log::info($clean_input);
  71. foreach ($clean_input['outcome_id'] as $index => $outcome_id) {
  72. $clean_input['outcome_id'][$index] = trim(preg_replace('/\t+/', '', $clean_input['outcome_id'][$index]));
  73. }
  74. $clean_input['program_id'] = Input::get('program_id');
  75. // Log::info(Input::get('program_id'));
  76. return $clean_input;
  77. }
  78. private function cleanAssocInput()
  79. {
  80. $clean_input = array();
  81. $clean_input['text'] = trim(preg_replace('/\t+/', '', Input::get('text')));
  82. $clean_input['outcome_id'] = Input::get('assoc_outcome');
  83. Log::info(Input::all());
  84. $clean_input['program_id'] = Input::get('program_id');
  85. $clean_input['pair_criteria'] = Input::get('pair_every_criteria_with_objective');
  86. return $clean_input;
  87. }
  88. public function fetchObjective()
  89. {
  90. return Objective::find(Input::get('id'));
  91. }
  92. public function fetchObjectiveWithTrashed()
  93. {
  94. $json = array();
  95. $json['program'] = DB::select("select program_id from objective_program where objective_id = ?", array(Input::get('id')));
  96. //$json['outcome'] = DB::select("select outcome_id from objective_outcome outc where outc.objective_id = ?", array(Input::get('id')));
  97. $json['objective'] = DB::select("select text, id from objectives where id =?", array(Input::get('id')));
  98. $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')));
  99. /*
  100. select objective_outcome.*, typ_semester_outcome.id as typ_semester_outcome_id,count(criterion_id) from objectives join objective_outcome on objective_outcome.objective_id = objectives.id left join criterion_objective_outcome on criterion_objective_outcome.objective_id = objectives.id and objective_outcome.outcome_id = criterion_objective_outcome.outcome_id
  101. left join typ_semester_objectives on typ_semester_objectives.objective_id = objectives.id left join typ_semester_outcome on typ_semester_outcome.outcome_id = objective_outcome.outcome_id and typ_semester_outcome.id = typ_semester_objectives.typ_semester_outcome_id group by objectives.id, objective_outcome.outcome_id
  102. */
  103. $json['outcome'] = DB::table('objective_outcome')
  104. ->leftJoin('criterion_objective_outcome', function ($j) {
  105. $j->on('criterion_objective_outcome.objective_id', '=', 'objective_outcome.objective_id')
  106. ->on('criterion_objective_outcome.outcome_id', '=', 'objective_outcome.outcome_id');
  107. })
  108. ->leftJoin('typ_semester_objectives', 'typ_semester_objectives.objective_id', '=', 'objective_outcome.objective_id')
  109. ->leftJoin('typ_semester_outcome', function ($j) {
  110. $j->on('typ_semester_outcome.outcome_id', '=', 'objective_outcome.outcome_id')
  111. ->on('typ_semester_outcome.id', '=', 'typ_semester_objectives.typ_semester_outcome_id');
  112. })
  113. ->groupBy("objective_outcome.objective_id", 'objective_outcome.outcome_id')
  114. ->where('objective_outcome.objective_id', Input::get('id'))
  115. ->select(
  116. 'objective_outcome.*',
  117. DB::raw("count(criterion_id) as count_criterion_id"),
  118. 'typ_semester_outcome.id as typ_semester_outcome_id'
  119. )
  120. ->get();
  121. $json['typ_semester_objectives'] = DB::table('typ_semester_objectives')
  122. ->where('objective_id', Input::get('id'))
  123. ->get();
  124. $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')));
  125. Log::info('is here');
  126. // Log::info(json_encode($json));
  127. return json_encode($json);
  128. }
  129. public function fetchAllobjectives()
  130. {
  131. $program_id = Input::get('program_fetch');
  132. $outcome_id = Input::get('outcome_fetch');
  133. $json = array();
  134. $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} and objective_id <> 0)");
  135. Log::info('En mi sueño');
  136. Log::info($json);
  137. return json_encode($json);
  138. }
  139. public function delete()
  140. {
  141. DB::delete("delete from objectives where id = ?", array(Input::get('deleteObj')));
  142. return Redirect::to('objectives')->withInput();
  143. /*$role = Auth::user()['role'];
  144. switch ($role) {
  145. case 1:
  146. return Redirect::to('objectives')->withInput();
  147. case 2:
  148. return Redirect::to('school-objective')->withInput();
  149. case 3:
  150. return Redirect::to('program-objective')->withInput();
  151. }*/
  152. }
  153. /**
  154. * Create a new Objective.
  155. *
  156. * @return Redirect Redirect back to form page
  157. */
  158. public function create()
  159. {
  160. $clean_input = $this->cleanInput();
  161. Log::info($clean_input);
  162. /** Validation rules */
  163. $validator = $this->makeValidator($clean_input, false);
  164. /** If validation fails */
  165. if ($validator->fails()) {
  166. /** Prepare error message */
  167. $message = '<p>Error(s) creating a new Objective:</p><ul>';
  168. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  169. $message .= $validationError;
  170. }
  171. $message .= '</ul>';
  172. /** Send error message and old data */
  173. Session::flash('status', 'danger');
  174. Session::flash('message', $message);
  175. return Redirect::to('objectives')->withInput();
  176. /*$role = Auth::user()['role'];
  177. switch ($role) {
  178. case 1:
  179. return Redirect::to('objective')->withInput();
  180. case 2:
  181. return Redirect::to('school-objective')->withInput();
  182. case 3:
  183. return Redirect::to('program-objective')->withInput();
  184. }*/
  185. } else {
  186. // Check Objective uniqueness
  187. if (!$this->isObjectiveUnique($clean_input)) {
  188. /** Send error message and old data */
  189. Session::flash('status', 'danger');
  190. Session::flash('message', "This objective is a duplicate of an already saved Objective because it's and associated program are the same.");
  191. return Redirect::to('objectives')->withInput();
  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. /** Instantiate new Objective */
  203. $objective = new Objective;
  204. $objective->text = $clean_input['text'];
  205. /** If Objective is saved, send success message */
  206. if ($objective->save()) {
  207. $objectiveId = $objective->id;
  208. Log::info($clean_input['outcome_id']);
  209. foreach ($clean_input['program_id'] as $program_id) {
  210. DB::insert("insert into objective_program (objective_id, program_id) values({$objectiveId},{$program_id})");
  211. }
  212. foreach ($clean_input['outcome_id'] as $outcome_id) {
  213. DB::insert("insert into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  214. //DB::raw("insert ignore into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  215. /*if (!($objectiveOutcome->save())) {
  216. Session::flash('status', 'danger');
  217. Session::flash('message', '<p>Error creating objective. Please try again later.</p>');
  218. return Redirect::to('objective')->withInput();
  219. }*/
  220. }
  221. DB::table('criterion_objective_outcome')
  222. ->join('program_criterion', 'criterion_objective_outcome.criterion_id', "=", 'program_criterion.criterion_id')
  223. ->whereIn('program_id', $clean_input['program_id'])
  224. ->whereIn('outcome_id', $clean_input['outcome_id'])
  225. ->where('objective_id', '=', 0)
  226. ->update(array('objective_id' => $objectiveId));
  227. // update("update criterion_objective_outcome coo join program_criterion pc on coo.criterion_id=pc.criterion_id set
  228. // objective_id= {$objectiveId} where program_id in (".$clean_input['program_id'].") and objective_id=0 and outcome_id in (".$clean_input['outcome_id'].")");
  229. Session::flash('status', 'success');
  230. Session::flash('message', 'Objective created: "' . $objective->text . '".');
  231. return Redirect::to('objectives')->withInput(Input::only('outcome_id'));
  232. /*$role = Auth::user()['role'];
  233. switch ($role) {
  234. case 1:
  235. return Redirect::to('objective')->withInput(Input::only('outcome_id'));
  236. case 2:
  237. return Redirect::to('school-objective')->withInput(Input::only('outcome_id'));
  238. case 3:
  239. return Redirect::to('program-objective')->withInput(Input::only('outcome_id'));
  240. }*/
  241. }
  242. /** If saving fails, send error message and old data */
  243. else {
  244. Session::flash('status', 'danger');
  245. Session::flash('message', '<p>Error creating objective. Please try again later.</p>');
  246. return Redirect::to('objectives')->withInput();
  247. /*$role = Auth::user()['role'];
  248. switch ($role) {
  249. case 1:
  250. return Redirect::to('objective')->withInput();
  251. case 2:
  252. return Redirect::to('school-objective')->withInput();
  253. case 3:
  254. return Redirect::to('program-objective')->withInput();
  255. }*/
  256. }
  257. }
  258. }
  259. /**
  260. * Store a newly created resource in storage.
  261. *
  262. * @return Response
  263. */
  264. public function store()
  265. {
  266. //
  267. }
  268. /**
  269. * Display the specified resource.
  270. *
  271. * @param int $id
  272. * @return Response
  273. */
  274. public function show($id)
  275. {
  276. //
  277. }
  278. /**
  279. * Show the form for editing the specified resource.
  280. *
  281. * @param int $id
  282. * @return Response
  283. */
  284. public function edit()
  285. {
  286. $title = "Objective";
  287. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  288. $role = Auth::user()->role;
  289. switch ($role) {
  290. case 1:
  291. $program_ids = DB::table("programs")
  292. ->lists('id');
  293. break;
  294. case 2:
  295. $program_ids = DB::table('programs')
  296. ->where('school_id', Auth::user()->school_id)
  297. ->lists('id');
  298. break;
  299. case 3:
  300. $program_ids = DB::table('program_user')
  301. ->where('user_id', Auth::user()->id)
  302. ->lists('program_id');
  303. break;
  304. }
  305. $objectives_from_program = DB::table('objective_program')
  306. ->where('program_id', $program_ids)
  307. ->where('objective_program.objective_id', "<>", 0)
  308. ->lists('objective_id');
  309. $objectives = Objective::withTrashed()->whereIn('id', $objectives_from_program)->orderBy('text', 'ASC')->get();
  310. $programs = Program::whereIn('id', $program_ids)->orderBy('name', 'ASC')->get();
  311. return View::make('local.managers.shared.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  312. }
  313. public function editProgram()
  314. {
  315. $userProgram = Auth::user()['id'];
  316. Log::info(Auth::user());
  317. $userProgram = DB::select("select program_user.program_id from program_user where user_id = {$userProgram}");
  318. $title = "Objective";
  319. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  320. $objectives_from_program = DB::table('objective_program')
  321. ->where('program_id', $userProgram[0]->program_id)
  322. ->where('objective_program.objective_id', "<>", 0)
  323. ->lists('objective_id');
  324. $objectives = Objective::withTrashed()->orderBy('text', 'ASC')->whereIn('id', $objectives_from_program)->get();
  325. $programs = Program::where("id", '=', $userProgram[0]->program_id)->get();
  326. return View::make('local.managers.pCoords.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  327. }
  328. public function editSchool()
  329. {
  330. $userSchool = Auth::user()['school_id'];
  331. Log::info($userSchool);
  332. $title = "Objective";
  333. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  334. $objectives_from_school = DB::table('programs')
  335. ->join('objective_program', 'objective_program.program_id', '=', 'programs.id')
  336. ->where('programs.school_id', $userSchool)
  337. ->where('objective_program.objective_id', "<>", 0)
  338. ->lists('objective_id');
  339. $objectives = Objective::withTrashed()->orderBy('text', 'ASC')->whereIn('id', $objectives_from_school)->get();
  340. $programs = Program::where("school_id", "=", $userSchool)->orderBy('name', 'ASC')->get();
  341. return View::make('local.managers.sCoords.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  342. }
  343. /**
  344. * Update the specified resource in storage.
  345. *
  346. * @param int $id
  347. * @return Response
  348. */
  349. public function update()
  350. {
  351. $Objective = Objective::withTrashed()->find(Input::get('id'));
  352. $clean_input = $this->cleanAssocInput();
  353. //Log::info(print_r($clean_input, true));
  354. /** Validation rules */
  355. if ($clean_input['text'] == $Objective->text) {
  356. $validator = $this->makeValidator($clean_input, true);
  357. } else
  358. $validator = $this->makeValidator($clean_input, false);
  359. /** If validation fails */
  360. if ($validator->fails()) {
  361. /** Prepare error message */
  362. $message = 'Error(s) updating the Objective: <ul>';
  363. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  364. $message .= $validationError;
  365. }
  366. $message .= '</ul>';
  367. /** Send error message and old data */
  368. //Session::flash('status', 'danger');
  369. //Session::flash('message', $message);
  370. $MessageArray = array('status' => 'danger', 'message' => $message);
  371. return $MessageArray;
  372. /*$role = Auth::user()['role'];
  373. switch ($role) {
  374. case 1:
  375. return $MessageArray;
  376. return Redirect::to('objective')->withInput();
  377. case 2:
  378. return $MessageArray;
  379. return Redirect::to('school-objective')->withInput();
  380. case 3:
  381. return $MessageArray;
  382. return Redirect::to('program-objective')->withInput();
  383. }*/
  384. } else {
  385. /** Set info */
  386. Log::info($clean_input);
  387. $Objective->text = $clean_input['text'];
  388. // Set status
  389. /** If Objective is updated, send success message */
  390. if ($Objective->save()) {
  391. //TODO
  392. $objectiveId = $Objective->id;
  393. //DB::delete("delete from `objective_outcome` where objective_id ={$objectiveId}");
  394. DB::delete("delete from objective_program where objective_id = {$objectiveId}");
  395. foreach ($clean_input['program_id'] as $program_id) {
  396. DB::insert("insert into `objective_program`(objective_id, program_id) values ({$objectiveId},{$program_id})");
  397. }
  398. $criteria_assoc = DB::table('criterion_objective_outcome')
  399. ->join('program_criterion', 'program_criterion.criterion_id', '=', 'criterion_objective_outcome.criterion_id')
  400. ->whereIn('program_id', $clean_input['program_id'])
  401. ->where('objective_id', $objectiveId)
  402. ->groupBy('program_criterion.criterion_id')
  403. ->select('program_criterion.criterion_id')
  404. ->lists('program_criterion.criterion_id');
  405. $criterion_array = [];
  406. foreach ($clean_input['outcome_id'] as $outcome_id) {
  407. $check_if_already_inserted = DB::table('objective_outcome')
  408. ->where('objective_id', $objectiveId)
  409. ->where('outcome_id', $outcome_id)
  410. ->first();
  411. if (!isset($check_if_already_inserted)) {
  412. DB::insert("insert into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  413. if ($clean_input['pair_criteria'] == '1') {
  414. foreach ($criteria_assoc as $criterion_id) {
  415. DB::table('criterion_objective_outcome')
  416. ->insert(array(
  417. "criterion_id" => $criterion_id,
  418. "objective_id" => $objectiveId,
  419. "outcome_id" => $outcome_id
  420. ));
  421. }
  422. }
  423. }
  424. }
  425. DB::table('objective_outcome')
  426. ->whereNotIn('outcome_id', $clean_input['outcome_id'])
  427. ->where('objective_id', $objectiveId)
  428. ->delete();
  429. //Session::flash('status', 'success');
  430. //Session::flash('message', 'Updated Objective: "' . $Objective->text . '"');
  431. $MessageArray = array('status' => 'success', 'message' => 'Updated Objective: "' . $Objective->text . '"');
  432. return $MessageArray;
  433. /*$role = Auth::user()['role'];
  434. switch ($role) {
  435. case 1:
  436. return $MessageArray;
  437. return Redirect::to('objective')->withInput();
  438. case 2:
  439. return $MessageArray;
  440. return Redirect::to('school-objective')->withInput();
  441. case 3:
  442. return $MessageArray;
  443. return Redirect::to('program-objective')->withInput();
  444. }*/
  445. }
  446. /** If saving fails, send error message and old data */
  447. else {
  448. //Session::flash('status', 'danger');
  449. ////Session::flash('message', 'Error updating the Objective. Please try again later.');
  450. $MessageArray = array('status' => 'danger', 'message' => 'Error updating the Objective. Please try again later.');
  451. return $MessageArray;
  452. $role = Auth::user()['role'];
  453. switch ($role) {
  454. case 1:
  455. return $MessageArray;
  456. return Redirect::to('objectives')->withInput();
  457. case 2:
  458. return $MessageArray;
  459. return Redirect::to('school-objective')->withInput();
  460. case 3:
  461. return $MessageArray;
  462. return Redirect::to('program-objective')->withInput();
  463. }
  464. }
  465. }
  466. }
  467. /**
  468. * Remove the specified resource from storage.
  469. *
  470. * @param int $id
  471. * @return Response
  472. */
  473. public function destroy($id)
  474. {
  475. //
  476. }
  477. }