Без опису

Objective2Controller.php 18KB

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