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

Objective2Controller.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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',
  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. /** Validation rules */
  162. $validator = $this->makeValidator($clean_input, false);
  163. /** If validation fails */
  164. if ($validator->fails()) {
  165. /** Prepare error message */
  166. $message = '<p>Error(s) creating a new Objective:</p><ul>';
  167. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  168. $message .= $validationError;
  169. }
  170. $message .= '</ul>';
  171. /** Send error message and old data */
  172. Session::flash('status', 'danger');
  173. Session::flash('message', $message);
  174. return Redirect::to('objectives')->withInput();
  175. /*$role = Auth::user()['role'];
  176. switch ($role) {
  177. case 1:
  178. return Redirect::to('objective')->withInput();
  179. case 2:
  180. return Redirect::to('school-objective')->withInput();
  181. case 3:
  182. return Redirect::to('program-objective')->withInput();
  183. }*/
  184. } else {
  185. // Check Objective uniqueness
  186. if (!$this->isObjectiveUnique($clean_input)) {
  187. /** Send error message and old data */
  188. Session::flash('status', 'danger');
  189. Session::flash('message', "This objective is a duplicate of an already saved Objective because it's and associated program are the same.");
  190. return Redirect::to('objectives')->withInput();
  191. /*$role = Auth::user()['role'];
  192. switch ($role) {
  193. case 1:
  194. return Redirect::to('objective')->withInput();
  195. case 2:
  196. return Redirect::to('school-objective')->withInput();
  197. case 3:
  198. return Redirect::to('program-objective')->withInput();
  199. }*/
  200. }
  201. /** Instantiate new Objective */
  202. $objective = new Objective;
  203. $objective->text = $clean_input['text'];
  204. /** If Objective is saved, send success message */
  205. if ($objective->save()) {
  206. $objectiveId = $objective->id;
  207. Log::info($clean_input['outcome_id']);
  208. foreach ($clean_input['program_id'] as $program_id) {
  209. DB::insert("insert into objective_program (objective_id, program_id) values({$objectiveId},{$program_id})");
  210. }
  211. foreach ($clean_input['outcome_id'] as $outcome_id) {
  212. DB::insert("insert into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  213. /*if (!($objectiveOutcome->save())) {
  214. Session::flash('status', 'danger');
  215. Session::flash('message', '<p>Error creating objective. Please try again later.</p>');
  216. return Redirect::to('objective')->withInput();
  217. }*/
  218. }
  219. DB::table('criterion_objective_outcome')
  220. ->join('program_criterion', 'criterion_objective_outcome.criterion_id', "=", 'program_criterion.criterion_id')
  221. ->whereIn('program_id', $clean_input['program_id'])
  222. ->whereIn('outcome_id', $clean_input['outcome_id'])
  223. ->where('objective_id', '=', 0)
  224. ->update(array('objective_id' => $objectiveId));
  225. // update("update criterion_objective_outcome coo join program_criterion pc on coo.criterion_id=pc.criterion_id set
  226. // objective_id= {$objectiveId} where program_id in (".$clean_input['program_id'].") and objective_id=0 and outcome_id in (".$clean_input['outcome_id'].")");
  227. Session::flash('status', 'success');
  228. Session::flash('message', 'Objective created: "' . $objective->text . '".');
  229. return Redirect::to('objectives')->withInput(Input::only('outcome_id'));
  230. /*$role = Auth::user()['role'];
  231. switch ($role) {
  232. case 1:
  233. return Redirect::to('objective')->withInput(Input::only('outcome_id'));
  234. case 2:
  235. return Redirect::to('school-objective')->withInput(Input::only('outcome_id'));
  236. case 3:
  237. return Redirect::to('program-objective')->withInput(Input::only('outcome_id'));
  238. }*/
  239. }
  240. /** If saving fails, send error message and old data */
  241. else {
  242. Session::flash('status', 'danger');
  243. Session::flash('message', '<p>Error creating objective. Please try again later.</p>');
  244. return Redirect::to('objectives')->withInput();
  245. /*$role = Auth::user()['role'];
  246. switch ($role) {
  247. case 1:
  248. return Redirect::to('objective')->withInput();
  249. case 2:
  250. return Redirect::to('school-objective')->withInput();
  251. case 3:
  252. return Redirect::to('program-objective')->withInput();
  253. }*/
  254. }
  255. }
  256. }
  257. /**
  258. * Store a newly created resource in storage.
  259. *
  260. * @return Response
  261. */
  262. public function store()
  263. {
  264. //
  265. }
  266. /**
  267. * Display the specified resource.
  268. *
  269. * @param int $id
  270. * @return Response
  271. */
  272. public function show($id)
  273. {
  274. //
  275. }
  276. /**
  277. * Show the form for editing the specified resource.
  278. *
  279. * @param int $id
  280. * @return Response
  281. */
  282. public function edit()
  283. {
  284. $title = "Objective";
  285. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  286. $role = Auth::user()->role;
  287. switch ($role) {
  288. case 1:
  289. $program_ids = DB::table("programs")
  290. ->lists('id');
  291. break;
  292. case 2:
  293. $program_ids = DB::table('programs')
  294. ->where('school_id', Auth::user()->school_id)
  295. ->lists('id');
  296. break;
  297. case 3:
  298. $program_ids = DB::table('program_user')
  299. ->where('user_id', Auth::user()->id)
  300. ->lists('program_id');
  301. break;
  302. }
  303. $objectives_from_program = DB::table('objective_program')
  304. ->where('program_id', $program_ids)
  305. ->where('objective_program.objective_id', "<>", 0)
  306. ->lists('objective_id');
  307. $objectives = Objective::withTrashed()->whereIn('id', $objectives_from_program)->orderBy('text', 'ASC')->get();
  308. $programs = Program::whereIn('id', $program_ids)->orderBy('name', 'ASC')->get();
  309. return View::make('local.managers.shared.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  310. }
  311. public function editProgram()
  312. {
  313. $userProgram = Auth::user()['id'];
  314. Log::info(Auth::user());
  315. $userProgram = DB::select("select program_user.program_id from program_user where user_id = {$userProgram}");
  316. $title = "Objective";
  317. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  318. $objectives_from_program = DB::table('objective_program')
  319. ->where('program_id', $userProgram[0]->program_id)
  320. ->where('objective_program.objective_id', "<>", 0)
  321. ->lists('objective_id');
  322. $objectives = Objective::withTrashed()->orderBy('text', 'ASC')->whereIn('id', $objectives_from_program)->get();
  323. $programs = Program::where("id", '=', $userProgram[0]->program_id)->get();
  324. return View::make('local.managers.pCoords.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  325. }
  326. public function editSchool()
  327. {
  328. $userSchool = Auth::user()['school_id'];
  329. Log::info($userSchool);
  330. $title = "Objective";
  331. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  332. $objectives_from_school = DB::table('programs')
  333. ->join('objective_program', 'objective_program.program_id', '=', 'programs.id')
  334. ->where('programs.school_id', $userSchool)
  335. ->where('objective_program.objective_id', "<>", 0)
  336. ->lists('objective_id');
  337. $objectives = Objective::withTrashed()->orderBy('text', 'ASC')->whereIn('id', $objectives_from_school)->get();
  338. $programs = Program::where("school_id", "=", $userSchool)->orderBy('name', 'ASC')->get();
  339. return View::make('local.managers.sCoords.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  340. }
  341. /**
  342. * Update the specified resource in storage.
  343. *
  344. * @param int $id
  345. * @return Response
  346. */
  347. public function update()
  348. {
  349. $Objective = Objective::withTrashed()->find(Input::get('id'));
  350. $clean_input = $this->cleanAssocInput();
  351. //Log::info(print_r($clean_input, true));
  352. /** Validation rules */
  353. if ($clean_input['text'] == $Objective->text) {
  354. $validator = $this->makeValidator($clean_input, true);
  355. } else
  356. $validator = $this->makeValidator($clean_input, false);
  357. /** If validation fails */
  358. if ($validator->fails()) {
  359. /** Prepare error message */
  360. $message = 'Error(s) updating the Objective: <ul>';
  361. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  362. $message .= $validationError;
  363. }
  364. $message .= '</ul>';
  365. /** Send error message and old data */
  366. //Session::flash('status', 'danger');
  367. //Session::flash('message', $message);
  368. $MessageArray = array('status' => 'danger', 'message' => $message);
  369. return $MessageArray;
  370. /*$role = Auth::user()['role'];
  371. switch ($role) {
  372. case 1:
  373. return $MessageArray;
  374. return Redirect::to('objective')->withInput();
  375. case 2:
  376. return $MessageArray;
  377. return Redirect::to('school-objective')->withInput();
  378. case 3:
  379. return $MessageArray;
  380. return Redirect::to('program-objective')->withInput();
  381. }*/
  382. } else {
  383. /** Set info */
  384. Log::info($clean_input);
  385. $Objective->text = $clean_input['text'];
  386. // Set status
  387. /** If Objective is updated, send success message */
  388. if ($Objective->save()) {
  389. //TODO
  390. $objectiveId = $Objective->id;
  391. //DB::delete("delete from `objective_outcome` where objective_id ={$objectiveId}");
  392. DB::delete("delete from objective_program where objective_id = {$objectiveId}");
  393. foreach ($clean_input['program_id'] as $program_id) {
  394. DB::insert("insert into `objective_program`(objective_id, program_id) values ({$objectiveId},{$program_id})");
  395. }
  396. $criteria_assoc = DB::table('criterion_objective_outcome')
  397. ->join('program_criterion', 'program_criterion.criterion_id', '=', 'criterion_objective_outcome.criterion_id')
  398. ->whereIn('program_id', $clean_input['program_id'])
  399. ->where('objective_id', $objectiveId)
  400. ->groupBy('program_criterion.criterion_id')
  401. ->select('program_criterion.criterion_id')
  402. ->lists('program_criterion.criterion_id');
  403. $criterion_array = [];
  404. foreach ($clean_input['outcome_id'] as $outcome_id) {
  405. $check_if_already_inserted = DB::table('objective_outcome')
  406. ->where('objective_id', $objectiveId)
  407. ->where('outcome_id', $outcome_id)
  408. ->first();
  409. if (!isset($check_if_already_inserted)) {
  410. DB::insert("insert into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  411. if ($clean_input['pair_criteria'] == '1') {
  412. foreach ($criteria_assoc as $criterion_id) {
  413. DB::table('criterion_objective_outcome')
  414. ->insert(array(
  415. "criterion_id" => $criterion_id,
  416. "objective_id" => $objectiveId,
  417. "outcome_id" => $outcome_id
  418. ));
  419. }
  420. }
  421. }
  422. }
  423. DB::table('objective_outcome')
  424. ->whereNotIn('outcome_id', $clean_input['outcome_id'])
  425. ->where('objective_id', $objectiveId)
  426. ->delete();
  427. //Session::flash('status', 'success');
  428. //Session::flash('message', 'Updated Objective: "' . $Objective->text . '"');
  429. $MessageArray = array('status' => 'success', 'message' => 'Updated Objective: "' . $Objective->text . '"');
  430. return $MessageArray;
  431. /*$role = Auth::user()['role'];
  432. switch ($role) {
  433. case 1:
  434. return $MessageArray;
  435. return Redirect::to('objective')->withInput();
  436. case 2:
  437. return $MessageArray;
  438. return Redirect::to('school-objective')->withInput();
  439. case 3:
  440. return $MessageArray;
  441. return Redirect::to('program-objective')->withInput();
  442. }*/
  443. }
  444. /** If saving fails, send error message and old data */
  445. else {
  446. //Session::flash('status', 'danger');
  447. ////Session::flash('message', 'Error updating the Objective. Please try again later.');
  448. $MessageArray = array('status' => 'danger', 'message' => 'Error updating the Objective. Please try again later.');
  449. return $MessageArray;
  450. $role = Auth::user()['role'];
  451. switch ($role) {
  452. case 1:
  453. return $MessageArray;
  454. return Redirect::to('objectives')->withInput();
  455. case 2:
  456. return $MessageArray;
  457. return Redirect::to('school-objective')->withInput();
  458. case 3:
  459. return $MessageArray;
  460. return Redirect::to('program-objective')->withInput();
  461. }
  462. }
  463. }
  464. }
  465. /**
  466. * Remove the specified resource from storage.
  467. *
  468. * @param int $id
  469. * @return Response
  470. */
  471. public function destroy($id)
  472. {
  473. //
  474. }
  475. }