Без опису

Objective2Controller.php 21KB

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