Keine Beschreibung

Objective2Controller.php 32KB

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