Bez popisu

Objective2Controller.php 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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 deletePCOBO()
  41. {
  42. $pcobo_id = Input::get('pcobo_id');
  43. $criterion_id = Input::get('criterion_id');
  44. //$objective_id = Input::get('objective_id');
  45. $outcome_id = Input::get('outcome_id');
  46. $program_id = Input::get('program_id');
  47. $cri_hol_dom = DB::table('criterion_objective_outcome')
  48. ->where('objective_id', 0)
  49. ->where('criterion_id', $criterion_id)
  50. ->where('outcome_id', $outcome_id)
  51. ->first();
  52. if (!isset($cri_hol_dom)) {
  53. $cri_obj_out_id = DB::table('criterion_objective_outcome')->insertGetId(
  54. array(
  55. 'objective_id' => 0,
  56. 'outcome_id' => $outcome_id,
  57. 'criterion_id' => $criterion_id
  58. )
  59. );
  60. } else {
  61. $cri_obj_out_id = $cri_hol_dom->id;
  62. //no creo que esto pase pero
  63. $exists_pair = DB::table("program_criterion_objective_outcome")
  64. ->where('program_id', $program_id)
  65. ->where('cri_obj_out_id', $cri_obj_out_id)
  66. ->first();
  67. if (isset($exists_pair)) {
  68. //se puede borrar la entrada
  69. //porque el pareo de programa-criterio-dominio-holder ya existe, so borrar esta entrada borra duplicados.
  70. DB::table('program_criterion_objective_outcome')
  71. ->where('id', $pcobo_id)
  72. ->delete();
  73. return 200;
  74. }
  75. }
  76. DB::table('program_criterion_objective_outcome')
  77. ->where('id', $pcobo_id)->update(array(
  78. 'cri_obj_out_id' => $cri_obj_out_id
  79. ));
  80. return 200;
  81. }
  82. public function isObjectiveUnique($input, $existing_Objective = NULL)
  83. {
  84. Log::info('isObjectiveUnique');
  85. if (Input::get('program_id') != 0)
  86. $program_id = $input['program_id'];
  87. else
  88. $program_id = NULL;
  89. $saved_Objective = DB::table('objectives')->join('objective_outcome', 'objective_outcome.objective_id', '=', 'objectives.id')
  90. ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
  91. ->where('objectives.text', '=', $input['text'])
  92. ->whereIn('objective_outcome.outcome_id', $input['outcome_id'])
  93. ->whereIn('objective_program.program_id', $input['program_id'])
  94. ->get();
  95. if (count($saved_Objective))
  96. return false;
  97. else
  98. return true;
  99. }
  100. //edit == true or edit == false
  101. private function makeValidator($clean_input, $edit)
  102. {
  103. /** Validation rules */
  104. if ($edit) {
  105. return Validator::make(
  106. array(
  107. //'text' => $clean_input['text'],
  108. 'outcome_id' => $clean_input['outcome_id'],
  109. 'program_id' => $clean_input['program_id']
  110. ),
  111. array(
  112. 'outcome_id' => 'required|array',
  113. 'program_id' => 'required|array'
  114. )
  115. );
  116. } else {
  117. return Validator::make(
  118. array(
  119. 'text' => $clean_input['text'],
  120. 'outcome_id' => $clean_input['outcome_id'],
  121. 'program_id' => $clean_input['program_id']
  122. ),
  123. array(
  124. 'text' => 'required|string|unique:objectives,text',
  125. 'outcome_id' => 'required|array',
  126. 'program_id' => 'required|array'
  127. )
  128. );
  129. }
  130. }
  131. private function cleanInput()
  132. {
  133. $clean_input = array();
  134. $clean_input['text'] = trim(preg_replace('/\t+/', '', Input::get('text')));
  135. $clean_input['outcome_id'] = Input::get('outcome');
  136. $counter = Input::get('counter') + 0;
  137. // Log::info($clean_input);
  138. foreach ($clean_input['outcome_id'] as $index => $outcome_id) {
  139. $clean_input['outcome_id'][$index] = trim(preg_replace('/\t+/', '', $clean_input['outcome_id'][$index]));
  140. }
  141. $clean_input['program_id'] = Input::get('program_id');
  142. // Log::info(Input::get('program_id'));
  143. return $clean_input;
  144. }
  145. private function cleanAssocInput()
  146. {
  147. $clean_input = array();
  148. $clean_input['text'] = trim(preg_replace('/\t+/', '', Input::get('text')));
  149. $clean_input['outcome_id'] = Input::get('assoc_outcome');
  150. Log::info(Input::all());
  151. $clean_input['program_id'] = Input::get('program_id');
  152. $clean_input['pair_criteria'] = Input::get('pair_every_criteria_with_objective');
  153. return $clean_input;
  154. }
  155. public function fetchObjective()
  156. {
  157. return Objective::find(Input::get('id'));
  158. }
  159. public function fetchProgramCriteria()
  160. {
  161. $outcome_id = Input::get("outcome_id");
  162. $objective = Objective::findOrFail(Input::get('objective_id'));
  163. $user_objective_programs_query = DB::table('objective_program');
  164. //Si es admin o school, puede ver todos los criterios de los programas pareados
  165. //Si es program_coordinator depende.
  166. if (Auth::user()->role == 3) {
  167. $user_objective_programs_query = $user_objective_programs_query
  168. ->join("program_user", 'program_user.program_id', '=', 'objective_program.program_id')
  169. ->where('user_id', Auth::user()->id);
  170. } elseif (Auth::user()->role == 2) {
  171. $user_objective_programs_query = $user_objective_programs_query
  172. ->join('programs', 'programs.id', '=', 'objective_program.program_id')
  173. ->where("programs.school_id", Auth::user()->school->id);
  174. }
  175. $program_ids = $user_objective_programs_query->where('objective_id', $objective->id)
  176. ->select('objective_program.program_id')
  177. ->lists('objective_program.program_id');
  178. Log::info("PRogram");
  179. //Log::info();
  180. //$array_to_send = [];
  181. //$array_to_send["programs"] = Program::whereIn('id', $program_ids)->with('criteria')->get();
  182. //$array_to_send['selected_criteria'] = $objective->pairedCriteria();
  183. //Program
  184. return Program::whereIn('id', $program_ids)
  185. ->select(DB::raw("programs.*, {$outcome_id} as outcome_id"))
  186. ->get();
  187. }
  188. public function updatePCOBO()
  189. {
  190. //aqui entra si cambia el viejo al nuevo
  191. $criterion_id = Input::get('criterion_id');
  192. $pcobo_id = Input::get("pcobo_id");
  193. $cobo_id = Input::get("cobo_id");
  194. $objective_id = Input::get("objective_id");
  195. $outcome_id = Input::get("outcome_id");
  196. $program_id = Input::get("program_id");
  197. $old_crit = Input::get('old_crit');
  198. $old_cobo_id = Input::get('old_cobo');
  199. $old_pcobo_id = Input::get('old_pcobo');
  200. $old_outcome_id = Input::get('old_outcome');
  201. //Existe el pareo de este criterio, este dominio y este objetivo?
  202. $existing_cobo_id = DB::table('criterion_objective_outcome')
  203. ->where('objective_id', $objective_id)
  204. ->where('outcome_id', $outcome_id)
  205. ->where('criterion_id', $criterion_id)
  206. ->first();
  207. if (!isset($existing_cobo_id)) {
  208. $insert_cobo_id = DB::table('criterion_objective_outcome')->insertGetId(array(
  209. 'criterion_id' => $criterion_id,
  210. 'outcome_id' => $outcome_id,
  211. 'objective_id' => $objective_id
  212. ));
  213. } else $insert_cobo_id = $existing_cobo_id->id;
  214. $it_exists = DB::table('program_criterion_objective_outcome')
  215. ->where('program_id', $program_id)
  216. ->where('cri_obj_out_id', $insert_cobo_id)
  217. ->first();
  218. if (isset($it_exists)) {
  219. return "DUPLICATE";
  220. }
  221. DB::table('program_criterion_objective_outcome')
  222. ->where("program_id", $program_id)
  223. ->where('cri_obj_out_id', $old_cobo_id)
  224. ->update(array(
  225. //'program_id' => $program_id,
  226. 'cri_obj_out_id' => $insert_cobo_id
  227. ));
  228. //now check if old criterion_objective_outcome is being used
  229. $other_pcobo_exist = DB::table('program_criterion_objective_outcome')
  230. ->where("cri_obj_out_id", $old_cobo_id)
  231. ->first();
  232. //si ningun otro programa tiene el pareo de este criterio con ese objetivo y ese dominio
  233. //lo puedes borrar
  234. if (!isset($other_pcobo_exist)) {
  235. DB::table('criterion_objective_outcome')
  236. ->where('id', $old_cobo_id)
  237. ->delete();
  238. }
  239. //Ahora hay que ver que la relacion de program, crit y dominio existe.
  240. $relationship_exists = DB::table('criterion_objective_outcome as cobo')
  241. ->join('program_criterion_objective_outcome as pcobo', 'pcobo.cri_obj_out_id', '=', 'cobo.id')
  242. ->where('criterion_id', $old_crit)
  243. ->where('outcome_id', $old_outcome_id)
  244. ->where('program_id', $program_id)
  245. ->first();
  246. if (!isset($relationship_exists)) {
  247. $new_cobo_id = DB::table('criterion_objective_outcome')
  248. ->insertGetId(array(
  249. 'criterion_id' => $old_crit,
  250. 'outcome_id' => $old_outcome_id,
  251. 'objective_id' => 0
  252. ));
  253. DB::table('program_criterion_objective_outcome')
  254. ->insert(array(
  255. 'program_id' => $program_id,
  256. 'cri_obj_out_id' => $new_cobo_id
  257. ));
  258. }
  259. return 200;
  260. //Aqui es insertar
  261. }
  262. public function insertPCOBO()
  263. {
  264. //aqui entra si le da a add criterio, y escoje uno nuevo
  265. $criterion_id = Input::get('criterion_id');
  266. $pcobo_id = Input::get("pcobo_id");
  267. $cobo_id = Input::get("cobo_id");
  268. $objective_id = Input::get("objective_id");
  269. $outcome_id = Input::get("outcome_id");
  270. $program_id = Input::get("program_id");
  271. Log::info(Input::all());
  272. //doc se esta usando? ie, objetivo_id == 0?
  273. //Existe el pareo de este criterio, este dominio y este objetivo?
  274. $old_cobo_id = DB::table('criterion_objective_outcome')
  275. ->where('objective_id', $objective_id)
  276. ->where('outcome_id', $outcome_id)
  277. ->where('criterion_id', $criterion_id)
  278. ->first();
  279. if (!isset($old_cobo_id)) {
  280. $insert_cobo_id = DB::table('criterion_objective_outcome')->insertGetId(array(
  281. 'criterion_id' => $criterion_id,
  282. 'outcome_id' => $outcome_id,
  283. 'objective_id' => $objective_id
  284. ));
  285. } else $insert_cobo_id = $old_cobo_id->id;
  286. $it_exists = DB::table('program_criterion_objective_outcome')
  287. ->where('program_id', $program_id)
  288. ->where('cri_obj_out_id', $insert_cobo_id)
  289. ->first();
  290. if (isset($it_exists)) {
  291. return "DUPLICATE";
  292. }
  293. DB::table('program_criterion_objective_outcome')
  294. ->insert(array(
  295. 'program_id' => $program_id,
  296. 'cri_obj_out_id' => $insert_cobo_id
  297. ));
  298. return 200;
  299. //Aqui es insertar
  300. }
  301. public function fetchObjectiveWithTrashed()
  302. {
  303. $json = array();
  304. $json['program'] = DB::select("select program_id from objective_program where objective_id = ?", array(Input::get('id')));
  305. //$json['outcome'] = DB::select("select outcome_id from objective_outcome outc where outc.objective_id = ?", array(Input::get('id')));
  306. $json['objective'] = DB::select("select text, id from objectives where id =?", array(Input::get('id')));
  307. $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')));
  308. /*
  309. 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
  310. 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
  311. */
  312. $json['outcome'] = DB::table('objective_outcome')
  313. ->leftJoin('criterion_objective_outcome', function ($j) {
  314. $j->on('criterion_objective_outcome.objective_id', '=', 'objective_outcome.objective_id')
  315. ->on('criterion_objective_outcome.outcome_id', '=', 'objective_outcome.outcome_id');
  316. })
  317. ->leftJoin('typ_semester_objectives', 'typ_semester_objectives.objective_id', '=', 'objective_outcome.objective_id')
  318. ->leftJoin('typ_semester_outcome', function ($j) {
  319. $j->on('typ_semester_outcome.outcome_id', '=', 'objective_outcome.outcome_id')
  320. ->on('typ_semester_outcome.id', '=', 'typ_semester_objectives.typ_semester_outcome_id');
  321. })
  322. ->groupBy("objective_outcome.objective_id", 'objective_outcome.outcome_id')
  323. ->where('objective_outcome.objective_id', Input::get('id'))
  324. ->select(
  325. 'objective_outcome.*',
  326. DB::raw("count(criterion_id) as count_criterion_id"),
  327. 'typ_semester_outcome.id as typ_semester_outcome_id'
  328. )
  329. ->get();
  330. $json['typ_semester_objectives'] = DB::table('typ_semester_objectives')
  331. ->where('objective_id', Input::get('id'))
  332. ->get();
  333. $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')));
  334. Log::info('is here');
  335. // Log::info(json_encode($json));
  336. return json_encode($json);
  337. }
  338. public function fetchAllobjectives()
  339. {
  340. $program_id = Input::get('program_fetch');
  341. $outcome_id = Input::get('outcome_fetch');
  342. $json = array();
  343. $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)");
  344. Log::info('En mi sueño');
  345. Log::info($json);
  346. return json_encode($json);
  347. }
  348. public function delete()
  349. {
  350. DB::delete("delete from objectives where id = ?", array(Input::get('deleteObj')));
  351. return Redirect::to('objectives')->withInput();
  352. /*$role = Auth::user()['role'];
  353. switch ($role) {
  354. case 1:
  355. return Redirect::to('objectives')->withInput();
  356. case 2:
  357. return Redirect::to('school-objective')->withInput();
  358. case 3:
  359. return Redirect::to('program-objective')->withInput();
  360. }*/
  361. }
  362. /**
  363. * Create a new Objective.
  364. *
  365. * @return Redirect Redirect back to form page
  366. */
  367. public function create()
  368. {
  369. $clean_input = $this->cleanInput();
  370. Log::info($clean_input);
  371. /** Validation rules */
  372. $validator = $this->makeValidator($clean_input, false);
  373. /** If validation fails */
  374. if ($validator->fails()) {
  375. /** Prepare error message */
  376. $message = '<p>Error(s) creating a new Objective:</p><ul>';
  377. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  378. $message .= $validationError;
  379. }
  380. $message .= '</ul>';
  381. /** Send error message and old data */
  382. Session::flash('status', 'danger');
  383. Session::flash('message', $message);
  384. return Redirect::to('objectives')->withInput();
  385. /*$role = Auth::user()['role'];
  386. switch ($role) {
  387. case 1:
  388. return Redirect::to('objective')->withInput();
  389. case 2:
  390. return Redirect::to('school-objective')->withInput();
  391. case 3:
  392. return Redirect::to('program-objective')->withInput();
  393. }*/
  394. } else {
  395. // Check Objective uniqueness
  396. if (!$this->isObjectiveUnique($clean_input)) {
  397. /** Send error message and old data */
  398. Session::flash('status', 'danger');
  399. Session::flash('message', "This objective is a duplicate of an already saved Objective because it's and associated program are the same.");
  400. return Redirect::to('objectives')->withInput();
  401. /*$role = Auth::user()['role'];
  402. switch ($role) {
  403. case 1:
  404. return Redirect::to('objective')->withInput();
  405. case 2:
  406. return Redirect::to('school-objective')->withInput();
  407. case 3:
  408. return Redirect::to('program-objective')->withInput();
  409. }*/
  410. }
  411. /** Instantiate new Objective */
  412. $objective = new Objective;
  413. $objective->text = $clean_input['text'];
  414. /** If Objective is saved, send success message */
  415. if ($objective->save()) {
  416. $objectiveId = $objective->id;
  417. Log::info($clean_input['outcome_id']);
  418. foreach ($clean_input['program_id'] as $program_id) {
  419. DB::insert("insert into objective_program (objective_id, program_id) values({$objectiveId},{$program_id})");
  420. }
  421. foreach ($clean_input['outcome_id'] as $outcome_id) {
  422. DB::insert("insert into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  423. //DB::raw("insert ignore into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  424. /*if (!($objectiveOutcome->save())) {
  425. Session::flash('status', 'danger');
  426. Session::flash('message', '<p>Error creating objective. Please try again later.</p>');
  427. return Redirect::to('objective')->withInput();
  428. }*/
  429. }
  430. DB::table('criterion_objective_outcome')
  431. ->join('program_criterion', 'criterion_objective_outcome.criterion_id', "=", 'program_criterion.criterion_id')
  432. ->whereIn('program_id', $clean_input['program_id'])
  433. ->whereIn('outcome_id', $clean_input['outcome_id'])
  434. ->where('objective_id', '=', 0)
  435. ->update(array('objective_id' => $objectiveId));
  436. // update("update criterion_objective_outcome coo join program_criterion pc on coo.criterion_id=pc.criterion_id set
  437. // objective_id= {$objectiveId} where program_id in (".$clean_input['program_id'].") and objective_id=0 and outcome_id in (".$clean_input['outcome_id'].")");
  438. Session::flash('status', 'success');
  439. Session::flash('message', 'Objective created: "' . $objective->text . '".');
  440. return Redirect::to('objectives')->withInput(Input::only('outcome_id'));
  441. /*$role = Auth::user()['role'];
  442. switch ($role) {
  443. case 1:
  444. return Redirect::to('objective')->withInput(Input::only('outcome_id'));
  445. case 2:
  446. return Redirect::to('school-objective')->withInput(Input::only('outcome_id'));
  447. case 3:
  448. return Redirect::to('program-objective')->withInput(Input::only('outcome_id'));
  449. }*/
  450. }
  451. /** If saving fails, send error message and old data */
  452. else {
  453. Session::flash('status', 'danger');
  454. Session::flash('message', '<p>Error creating objective. Please try again later.</p>');
  455. return Redirect::to('objectives')->withInput();
  456. /*$role = Auth::user()['role'];
  457. switch ($role) {
  458. case 1:
  459. return Redirect::to('objective')->withInput();
  460. case 2:
  461. return Redirect::to('school-objective')->withInput();
  462. case 3:
  463. return Redirect::to('program-objective')->withInput();
  464. }*/
  465. }
  466. }
  467. }
  468. /**
  469. * Store a newly created resource in storage.
  470. *
  471. * @return Response
  472. */
  473. public function store()
  474. {
  475. //
  476. }
  477. /**
  478. * Display the specified resource.
  479. *
  480. * @param int $id
  481. * @return Response
  482. */
  483. public function show($id)
  484. {
  485. //
  486. }
  487. /**
  488. * Show the form for editing the specified resource.
  489. *
  490. * @param int $id
  491. * @return Response
  492. */
  493. public function edit()
  494. {
  495. $title = "Objective";
  496. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  497. $role = Auth::user()->role;
  498. switch ($role) {
  499. case 1:
  500. $program_ids = DB::table("programs")
  501. ->lists('id');
  502. break;
  503. case 2:
  504. $program_ids = DB::table('programs')
  505. ->where('school_id', Auth::user()->school_id)
  506. ->lists('id');
  507. break;
  508. case 3:
  509. $program_ids = DB::table('program_user')
  510. ->where('user_id', Auth::user()->id)
  511. ->lists('program_id');
  512. break;
  513. }
  514. $objectives_from_program = DB::table('objective_program')
  515. ->where('program_id', $program_ids)
  516. ->where('objective_program.objective_id', "<>", 0)
  517. ->lists('objective_id');
  518. $objectives = Objective::withTrashed()->whereIn('id', $objectives_from_program)->orderBy('text', 'ASC')->get();
  519. $programs = Program::whereIn('id', $program_ids)->orderBy('name', 'ASC')->get();
  520. return View::make('local.managers.shared.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  521. }
  522. public function editProgram()
  523. {
  524. $userProgram = Auth::user()['id'];
  525. Log::info(Auth::user());
  526. $userProgram = DB::select("select program_user.program_id from program_user where user_id = {$userProgram}");
  527. $title = "Objective";
  528. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  529. $objectives_from_program = DB::table('objective_program')
  530. ->where('program_id', $userProgram[0]->program_id)
  531. ->where('objective_program.objective_id', "<>", 0)
  532. ->lists('objective_id');
  533. $objectives = Objective::withTrashed()->orderBy('text', 'ASC')->whereIn('id', $objectives_from_program)->get();
  534. $programs = Program::where("id", '=', $userProgram[0]->program_id)->get();
  535. return View::make('local.managers.pCoords.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  536. }
  537. public function editSchool()
  538. {
  539. $userSchool = Auth::user()['school_id'];
  540. Log::info($userSchool);
  541. $title = "Objective";
  542. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->get();
  543. $objectives_from_school = DB::table('programs')
  544. ->join('objective_program', 'objective_program.program_id', '=', 'programs.id')
  545. ->where('programs.school_id', $userSchool)
  546. ->where('objective_program.objective_id', "<>", 0)
  547. ->lists('objective_id');
  548. $objectives = Objective::withTrashed()->orderBy('text', 'ASC')->whereIn('id', $objectives_from_school)->get();
  549. $programs = Program::where("school_id", "=", $userSchool)->orderBy('name', 'ASC')->get();
  550. return View::make('local.managers.sCoords.objectives', compact('title', 'outcomes', 'objectives', 'programs'));
  551. }
  552. /**
  553. * Update the specified resource in storage.
  554. *
  555. * @param int $id
  556. * @return Response
  557. */
  558. public function update()
  559. {
  560. $Objective = Objective::withTrashed()->find(Input::get('id'));
  561. $clean_input = $this->cleanAssocInput();
  562. //Log::info(print_r($clean_input, true));
  563. /** Validation rules */
  564. if ($clean_input['text'] == $Objective->text) {
  565. $validator = $this->makeValidator($clean_input, true);
  566. } else
  567. $validator = $this->makeValidator($clean_input, false);
  568. /** If validation fails */
  569. if ($validator->fails()) {
  570. /** Prepare error message */
  571. $message = 'Error(s) updating the Objective: <ul>';
  572. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  573. $message .= $validationError;
  574. }
  575. $message .= '</ul>';
  576. /** Send error message and old data */
  577. //Session::flash('status', 'danger');
  578. //Session::flash('message', $message);
  579. $MessageArray = array('status' => 'danger', 'message' => $message);
  580. return $MessageArray;
  581. /*$role = Auth::user()['role'];
  582. switch ($role) {
  583. case 1:
  584. return $MessageArray;
  585. return Redirect::to('objective')->withInput();
  586. case 2:
  587. return $MessageArray;
  588. return Redirect::to('school-objective')->withInput();
  589. case 3:
  590. return $MessageArray;
  591. return Redirect::to('program-objective')->withInput();
  592. }*/
  593. } else {
  594. /** Set info */
  595. Log::info($clean_input);
  596. $Objective->text = $clean_input['text'];
  597. // Set status
  598. /** If Objective is updated, send success message */
  599. if ($Objective->save()) {
  600. //TODO
  601. $objectiveId = $Objective->id;
  602. //criteria_assoc, si le dan a 1, parea todos los criterios que esten pareado a este objetivo,
  603. // a todos los criterios asociados de su programa
  604. $criteria_assoc = DB::table('criterion_objective_outcome')
  605. ->join('program_criterion_objective_outcome as poco', 'poco.cri_obj_out_id', '=', 'criterion_objective_outcome.id')
  606. //->join('program_criterion', 'program_criterion.criterion_id', '=', 'criterion_objective_outcome.criterion_id')
  607. ->whereIn('program_id', $clean_input['program_id'])
  608. ->where('objective_id', $objectiveId)
  609. ->groupBy('criterion_objective_outcome.criterion_id')
  610. ->select('criterion_objective_outcome.criterion_id')
  611. ->lists('criterion_objective_outcome.criterion_id');
  612. //DB::delete("delete from `objective_outcome` where objective_id ={$objectiveId}");
  613. DB::delete("delete from objective_program where objective_id = {$objectiveId}");
  614. foreach ($clean_input['program_id'] as $program_id) {
  615. DB::insert("insert into `objective_program`(objective_id, program_id) values ({$objectiveId},{$program_id})");
  616. }
  617. //borra todos los pareos de este objetivo de programas que no estan vinculados a este objetivo
  618. $cri_obj_out_ids = DB::table('criterion_objective_outcome as cobo')
  619. ->where('objective_id', $objectiveId)
  620. ->lists('cobo.id');
  621. DB::table("program_criterion_objective_outcome")
  622. ->whereIn('cri_obj_out_id', $cri_obj_out_ids)
  623. ->whereNotIn('program_id', $clean_input['program_id'])
  624. ->delete();
  625. //el plan es, convertir todos los pareos de criterios-obj-out asociado a mi programa
  626. //a holder primero
  627. $cri_obj_out = DB::table('criterion_objective_outcome')
  628. ->join('program_criterion_objective_outcome as poco', 'poco.cri_obj_out_id', '=', 'criterion_objective_outcome.id')
  629. ->whereIn('program_id', $clean_input['program_id'])
  630. ->where('objective_id', $objectiveId)
  631. ->update(array(
  632. 'objective_id' => 0
  633. ));
  634. //Luego por cada dominio que haya, chequea si existe ya el pareo
  635. //si no existe insertalo.
  636. //si insertaste, y pair_criteria == 1, parea todos los criterios bajo este programa con
  637. // ese dominio y outcome
  638. // Luego por cada criterio que esta pareado a objetivo 0, en ese programa
  639. //y en ese dominio, cambiale el objetivo a este.
  640. //los que se crearon nue
  641. foreach ($clean_input['outcome_id'] as $outcome_id) {
  642. $check_if_already_inserted = DB::table('objective_outcome')
  643. ->where('objective_id', $objectiveId)
  644. ->where('outcome_id', $outcome_id)
  645. ->first();
  646. if (!isset($check_if_already_inserted)) {
  647. DB::insert("insert into `objective_outcome` (objective_id, outcome_id) values ({$objectiveId}, {$outcome_id})");
  648. if ($clean_input['pair_criteria'] == '1') {
  649. foreach ($criteria_assoc as $criterion_id) {
  650. $cob_id = DB::table('criterion_objective_outcome')
  651. ->insertGetId(array(
  652. "criterion_id" => $criterion_id,
  653. "objective_id" => $objectiveId,
  654. "outcome_id" => $outcome_id
  655. ));
  656. foreach ($clean_input['program_id'] as $program_id)
  657. DB::table('program_criterion_objective_outcome')
  658. ->insert(array(
  659. 'cri_obj_out_id' => $cob_id,
  660. 'program_id' => $program_id
  661. ));
  662. }
  663. }
  664. }
  665. //aqui estoy diciendo, todos los pareos bajo estos programas que tienen objetivo 0, y este dominio,
  666. //unelo a este objetivo.
  667. DB::table('criterion_objective_outcome')
  668. ->join("program_criterion_objective_outcome", 'program_criterion_objective_outcome.cri_obj_out_id', '=', 'criterion_objective_outcome.id')
  669. ->whereIn('program_id', $clean_input['program_id'])
  670. ->where('objective_id', 0)
  671. ->where('outcome_id', $outcome_id)
  672. ->update(array(
  673. 'objective_id' => $objectiveId
  674. ));
  675. $criterion_array = [];
  676. }
  677. DB::table('objective_outcome')
  678. ->whereNotIn('outcome_id', $clean_input['outcome_id'])
  679. ->where('objective_id', $objectiveId)
  680. ->delete();
  681. //Session::flash('status', 'success');
  682. //Session::flash('message', 'Updated Objective: "' . $Objective->text . '"');
  683. $MessageArray = array('status' => 'success', 'message' => 'Updated Objective: "' . $Objective->text . '"');
  684. return $MessageArray;
  685. /*$role = Auth::user()['role'];
  686. switch ($role) {
  687. case 1:
  688. return $MessageArray;
  689. return Redirect::to('objective')->withInput();
  690. case 2:
  691. return $MessageArray;
  692. return Redirect::to('school-objective')->withInput();
  693. case 3:
  694. return $MessageArray;
  695. return Redirect::to('program-objective')->withInput();
  696. }*/
  697. }
  698. /** If saving fails, send error message and old data */
  699. else {
  700. //Session::flash('status', 'danger');
  701. ////Session::flash('message', 'Error updating the Objective. Please try again later.');
  702. $MessageArray = array('status' => 'danger', 'message' => 'Error updating the Objective. Please try again later.');
  703. return $MessageArray;
  704. $role = Auth::user()['role'];
  705. switch ($role) {
  706. case 1:
  707. return $MessageArray;
  708. return Redirect::to('objectives')->withInput();
  709. case 2:
  710. return $MessageArray;
  711. return Redirect::to('school-objective')->withInput();
  712. case 3:
  713. return $MessageArray;
  714. return Redirect::to('program-objective')->withInput();
  715. }
  716. }
  717. }
  718. }
  719. /**
  720. * Remove the specified resource from storage.
  721. *
  722. * @param int $id
  723. * @return Response
  724. */
  725. public function destroy($id)
  726. {
  727. //
  728. }
  729. }