Aucune description

Objective2Controller.php 37KB

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