Без опису

Objective2Controller.php 30KB

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