Nenhuma descrição

Objective2Controller.php 30KB

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