Нема описа

CriteriaController.php 50KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. <?php
  2. use Illuminate\Support\Facades\Input;
  3. class CriteriaController extends \BaseController
  4. {
  5. /**
  6. * Display a listing of the resource.
  7. *
  8. *
  9. * @return Response
  10. */
  11. public function editProgram()
  12. {
  13. $userProgram = Auth::user()['id'];
  14. $userProgram = DB::select("select program_user.program_id from program_user where user_id = {$userProgram}");
  15. Log::info($userProgram);
  16. $title = "Criteria";
  17. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->lists('name', 'id');
  18. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  19. $programs = Program::where("id", "=", $userProgram[0]->program_id)->get();
  20. return View::make('local.managers.pCoords.criteria', compact('title', 'outcomes', 'schools', 'criteria', 'programs', 'objectives'));
  21. }
  22. public function editSchool()
  23. {
  24. $userSchool = Auth::user()['school_id'];
  25. Log::info($userSchool);
  26. $title = "Criteria";
  27. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->lists('name', 'id');
  28. $schools = School::find($userSchool)->get();
  29. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  30. $programs = Program::where("school_id", "=", $userSchool)->orderBy('name', 'ASC')->get();
  31. $listOfId = array();
  32. foreach ($programs as $program) {
  33. $listOfId[] = $program->id;
  34. }
  35. //$objectives = DB::table('objectives')->whereIn('program_id', $listOfId)->orderBy('text', 'ASC')->lists('text', 'id');
  36. return View::make('local.managers.sCoords.criteria', compact('title', 'outcomes', 'schools', 'criteria', 'programs' /* 'objectives'*/));
  37. }
  38. public function fetchCriterionWithTemplate()
  39. {
  40. $json_to_send = array();
  41. $json_to_send['criterion'] = DB::table('criteria')->where('id', '=', Input::get('id'))->first();
  42. $outcomeIDS = DB::table('criterion_objective_outcome')->where('criterion_id', '=', $json_to_send['criterion']->id)->lists('outcome_id');
  43. Log::info($outcomeIDS);
  44. $json_to_send['scales'] = DB::table('criterion_scale')
  45. ->join('scales', 'scales.id', '=', 'criterion_scale.scale_id')
  46. ->where('criterion_id', Input::get('id'))
  47. ->orderBy('position')
  48. ->get();
  49. $json_to_send['outcomes'] = DB::table('outcomes')->whereIn('id', $outcomeIDS)->get();
  50. $outcomeStr = '';
  51. if (count($json_to_send['outcomes']) == 1) {
  52. $outcomeStr .= $json_to_send['outcomes'][0]->name;
  53. } else {
  54. foreach ($json_to_send['outcomes'] as $index => $outcome) {
  55. $outcomeStr .= ($index + 1) . '. ' . $outcome->name . '<br>';
  56. }
  57. $outcomeStr = rtrim($outcomeStr, ',');
  58. }
  59. $json_to_send['outcomes'] = $outcomeStr;
  60. return json_encode($json_to_send);
  61. }
  62. public function fetchCriterion()
  63. {
  64. $json_to_send = array();
  65. $json_to_send['criterion'] = DB::table('criteria')->where('id', '=', Input::get('id'))->first();
  66. return $json_to_send['criterion'];
  67. }
  68. public function fetchAllCriterion()
  69. {
  70. $program_id = Input::get('program_fetch');
  71. $outcome_id = Input::get('outcome_fetch');
  72. $json = array();
  73. $json['criterion'] = DB::select("SELECT * FROM `criteria` where id in (select criterion_id from criterion_objective_outcome join program_criterion_objective_outcome on program_criterion_objective_outcome.cri_obj_out_id = criterion_objective_outcome.id where outcome_id ={$outcome_id} and program_id = {$program_id}) ");
  74. return json_encode($json);
  75. }
  76. public function fetchObjectivesForSelect()
  77. {
  78. $json = array();
  79. $role = Auth::user()->role;
  80. switch ($role) {
  81. case 1:
  82. $program_ids = DB::table('programs')->lists('id');
  83. break;
  84. case 2:
  85. $program_ids = DB::table('programs')
  86. ->where('school_id', Auth::user()->school_id)
  87. ->lists('id');
  88. break;
  89. case 3:
  90. $program_ids = DB::table('program_user')
  91. ->where('user_id', Auth::user()->id)
  92. ->lists('program_id');
  93. break;
  94. case 4:
  95. $program_ids = DB::table('program_user')
  96. ->where('user_id', Auth::user()->id)
  97. ->lists('program_id');
  98. break;
  99. }
  100. $outcome_id = Input::get('outcomeID');
  101. Log::info($outcome_id);
  102. $json = DB::table('objectives')
  103. ->join('objective_outcome', 'objective_outcome.objective_id', '=', 'objectives.id')
  104. ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
  105. ->where('outcome_id', '=', $outcome_id)
  106. ->where('objectives.id', '<>', 0)
  107. ->whereIn('program_id', $program_ids)
  108. ->select('objectives.*', 'objective_outcome.*')
  109. ->distinct()
  110. ->get();
  111. foreach ($json as $objective) {
  112. $objective->program_ids = json_encode(DB::table('objective_program')
  113. ->where('objective_id', $objective->objective_id)
  114. ->lists('program_id'), JSON_NUMERIC_CHECK);
  115. }
  116. return json_encode($json);
  117. }
  118. public function fetchCriterionWithTrashed()
  119. {
  120. /* $json = array();
  121. $json['criteria'] = DB::table('criteria')->where('id', Input::get('id'))->get();
  122. $assoc_outcomes = DB::table('criterion_objective_outcome')->where('criterion_id', Input::get('id'))->lists('outcome_id');
  123. $json['outcomes'] = DB::table('outcomes')->whereIn('id', $assoc_outcomes)->get();
  124. $criteria_id = Input::get('id');
  125. foreach ($json['outcomes'] as $outcome) {
  126. $id = $outcome->id;
  127. $json['objectives'][$id] = DB::table('objectives')
  128. ->join('criterion_objective_outcome', 'objectives.id', '=', 'criterion_objective_outcome.objective_id')
  129. ->where('criterion_id', '=', $criteria_id)
  130. ->where('outcome_id', '=', $id)
  131. ->get();
  132. $json['objectives_assoc'][$id] = DB::table('objectives')
  133. ->join('objective_outcome', 'objective_outcome.objective_id', '=', 'objectives.id')
  134. ->where('outcome_id', $id)
  135. ->get();
  136. foreach ($json['objectives_assoc'][$id] as $objective) {
  137. $objective->program_ids = json_encode(DB::table('objective_program')
  138. ->where('objective_id', $objective->objective_id)
  139. ->lists('program_id'));
  140. }
  141. }
  142. $json['program'] = DB::table('program_criterion')->where('criterion_id', $criteria_id)->get();
  143. $json['activity_criterion'] = DB::table('assessments')
  144. ->join('activity_criterion', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  145. ->where('activity_criterion.criterion_id', $criteria_id)
  146. ->get();
  147. $json['scales'] = DB::table('criterion_scale')
  148. ->join('scales', 'criterion_scale.scale_id', '=', 'scales.id')
  149. ->where('criterion_id', $criteria_id)
  150. ->orderBy('position')
  151. ->get();
  152. /*
  153. $json['criteria'] = DB::select(" select * from criteria where id = ?", array(Input::get('id')));
  154. $json['outcomes'] = DB::select("select DISTINCT outcomes.id, outcomes.name from outcomes , criterion_objective_outcome where criterion_objective_outcome.criterion_id = ? and outcomes.id = criterion_objective_outcome.outcome_id order by outcomes.id", array(Input::get('id')));
  155. $json['objectives'] = DB::select("SELECT DISTINCT objectives.id, objectives.text FROM objectives, criterion_objective_outcome where criterion_objective_outcome.criterion_id = ? and criterion_objective_outcome.objective_id= objectives.id ", array(Input::get('id')));
  156. $json['objectives_outcome'] = DB::select("select objectives.id, objectives.text, objective_outcome.outcome_id from objective_outcome, objectives where objective_outcome.outcome_id in(select DISTINCT outcomes.id from outcomes , criterion_objective_outcome where criterion_objective_outcome.criterion_id = ? and outcomes.id = criterion_objective_outcome.outcome_id) and objectives.id = objective_outcome.objective_id ORDER BY outcome_id", array(Input::get('id')));
  157. $json['program'] = DB::select("select criterion_id, program_id from program_criterion where criterion_id =?", array(Input::get('id')));
  158. $json['activity_criterion'] = DB::select("select * from assessments where activity_criterion_id in (select id from activity_criterion where criterion_id = ?)", array(Input::get('id')));
  159. foreach ($json['outcomes'] as $id) {
  160. $outId = $id->id;
  161. $json['outcomes_assoc'][$outId] = DB::select("select name from outcomes where id = {$outId}");
  162. $json['objectives_assoc'][$outId] = DB::select("select objectives.id, objectives.text, outcomes.name from objectives, objective_outcome, outcomes where objective_outcome.outcome_id ={$outId} and objective_outcome.objective_id = objectives.id and objectives.active=1 and outcomes.id = objective_outcome.outcome_id");
  163. }*/
  164. $userRole = Auth::user()['role'];
  165. switch ($userRole) {
  166. case 1:
  167. $program_ids = DB::table('programs')->lists('id');
  168. break;
  169. case 2:
  170. $program_ids = DB::table('programs')
  171. ->where('school_id', Auth::user()->school_id)
  172. ->lists('id');
  173. break;
  174. case 3:
  175. case 4:
  176. $program_ids = DB::table('program_user')
  177. ->where('user_id', Auth::user()->id)
  178. ->lists('program_id');
  179. break;
  180. }
  181. $criteria_id = Input::get('id');
  182. $criterion = DB::table('criteria')->where('id', Input::get('id'))->first();
  183. if (!$criterion) return $criterion;
  184. $criterion->outcomes = DB::table('criterion_objective_outcome')
  185. ->join('outcomes', 'outcomes.id', '=', 'criterion_objective_outcome.outcome_id')
  186. ->join('program_criterion_objective_outcome as poco', 'poco.cri_obj_out_id', '=', 'criterion_objective_outcome.id')
  187. ->whereIn("program_id", $program_ids)
  188. ->where('criterion_id', $criterion->id)
  189. ->select('outcomes.*')
  190. ->distinct()
  191. ->get();
  192. foreach ($criterion->outcomes as $outcome) {
  193. $outcome->assoc_objectives = DB::table('objective_outcome')
  194. ->join('objectives', 'objectives.id', '=', 'objective_outcome.objective_id')
  195. ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
  196. ->whereIn('program_id', $program_ids)
  197. ->where('outcome_id', $outcome->id)
  198. ->select('objectives.*', 'objectives.id as objective_id')
  199. ->distinct()
  200. ->get();
  201. $outcome->objectives_criteria = DB::table('criterion_objective_outcome')
  202. ->join("program_criterion_objective_outcome as poco", 'poco.cri_obj_out_id', '=', 'criterion_objective_outcome.id')
  203. ->join('objectives', 'objectives.id', '=', 'criterion_objective_outcome.objective_id')
  204. ->where('criterion_id', $criterion->id)
  205. ->whereIn("poco.program_id", $program_ids)
  206. ->where('outcome_id', $outcome->id)
  207. ->select('objectives.*', 'objectives.id as objective_id')
  208. ->distinct()
  209. ->get();
  210. foreach ($outcome->assoc_objectives as $objective) {
  211. $objective->program_ids = json_encode(DB::table('objective_program')
  212. ->where('objective_id', $objective->id)
  213. ->lists('program_id'), JSON_NUMERIC_CHECK);
  214. }
  215. }
  216. $criterion->scales = DB::table('criterion_scale')
  217. ->join('scales', 'criterion_scale.scale_id', '=', 'scales.id')
  218. ->where('criterion_id', $criteria_id)
  219. ->orderBy('position')
  220. ->get();
  221. $criterion->program = DB::table('program_criterion_objective_outcome as poco')
  222. ->join('criterion_objective_outcome as cobo', 'cobo.id', '=', 'poco.cri_obj_out_id')
  223. ->where('criterion_id', $criteria_id)
  224. ->select('poco.program_id')
  225. ->distinct()->get();
  226. $criterion->activity_criterion = DB::table('assessments')
  227. ->join('activity_criterion', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  228. ->where('activity_criterion.criterion_id', $criteria_id)
  229. ->get();
  230. return array($criterion);
  231. }
  232. public function delete()
  233. {
  234. Log::info(Input::all());
  235. Log::info("LLEGAMOS??");
  236. $number_of_rows = DB::delete("delete from criteria where id = ?", array(Input::get('criterion_delete')));
  237. if ($number_of_rows <= 0) {
  238. $message = '<p>Error deleting Criterion:</p><ul>';
  239. $message .= '</ul>';
  240. /** Send error message and old data */
  241. Session::flash('status', 'danger');
  242. Session::flash('message', $message);
  243. } else {
  244. $message = '<p>Criterion deleted!</p><ul>';
  245. $message .= '</ul>';
  246. /** Send error message and old data */
  247. Session::flash('status', 'success');
  248. Session::flash('message', $message);
  249. }
  250. return Redirect::to('criteria')->withInput();
  251. $role = Auth::user()['role'];
  252. switch ($role) {
  253. case 1:
  254. return Redirect::to('criteria')->withInput();
  255. case 2:
  256. return Redirect::to('school-criteria')->withInput();
  257. case 3:
  258. return Redirect::to('program-criteria')->withInput();
  259. }
  260. }
  261. public function isCriterionUnique($input, $existing_criterion = NULL)
  262. {
  263. // dd($input);
  264. Log::info('isCriterionUnique');
  265. if (Input::get('program_id') != 0)
  266. $program_id = $input['program_id'];
  267. else
  268. $program_id = NULL;
  269. $outcomes = [];
  270. $objectives = [];
  271. foreach ($input['objective_id'] as $objective) {
  272. $parentesis = array('(', ')');
  273. $outcome_objective = str_replace($parentesis, '', $objective);
  274. $outcomes[] = $outcome_objective[0];
  275. $objectives[] = $outcome_objective[1];
  276. }
  277. $scales = [];
  278. foreach ($input['scale_description'] as $scale) {
  279. $existing_scale = DB::table('scales')->where('description', $scale)
  280. ->first();
  281. if (!$existing_scale) return true;
  282. $scales[] = $existing_scale->id;
  283. }
  284. $saved_criterion = DB::table('criteria')
  285. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  286. ->join('program_criterion_objective_outcome', 'program_criterion_objective_outcome.cri_obj_out_id', '=', 'criterion_objective_outcome.id')
  287. ->join('criterion_scale', 'criterion_scale.criterion_id', '=', 'criteria.id')
  288. ->whereIn('program_id', $input['program_id'])
  289. ->whereIn('objective_id', $objectives)
  290. ->whereIn('outcome_id', $outcomes)
  291. ->where('name', '=', $input['name'])
  292. ->where('subcriteria', '=', $input['subcriteria'])
  293. ->whereIn('scale_id', $scales)
  294. ->where('num_scales', $input['number_of_scales'])
  295. ->where('max_score', $input['maximum_score'])
  296. ->first();
  297. if ($saved_criterion)
  298. return false;
  299. else
  300. return true;
  301. }
  302. private function cleanInput()
  303. {
  304. $clean_input = array();
  305. $clean_input['name'] = trim(preg_replace('/\t+/', '', Input::get('name')));
  306. $trimmed = trim(preg_replace('/\t+/', '', Input::get('subcriteria')));
  307. Log::info('trimmed 1 -->' . $trimmed . '<--');
  308. if ($trimmed == '') {
  309. $trimmed = NULL;
  310. } else {
  311. $trimmed = json_encode(preg_split('/\r\n/', $trimmed));
  312. }
  313. Log::info('trimmed 2 -->' . $trimmed . '<--');
  314. $clean_input['subcriteria'] = $trimmed;
  315. $clean_input['outcome_id'] = Input::get('outcome');
  316. $clean_input['objective_id'] = Input::get('objective');
  317. $clean_input['program_id'] = Input::get('program_id');
  318. $clean_input['copyright'] = trim(preg_replace('/\t+/', '', Input::get('copyright')));
  319. $clean_input['notes'] = trim(preg_replace('/\t+/', '', Input::get('notes')));
  320. $clean_input['maximum_score'] = (int) Input::get('maximum_score');
  321. $clean_input['scale_description'] = Input::get('Scales');
  322. $clean_input['number_of_scales'] = sizeof($clean_input['scale_description']);
  323. return $clean_input;
  324. }
  325. private function makeValidator($clean_input)
  326. {
  327. if (isset($clean_input['scale_description'][0])) {
  328. $scale = $clean_input['scale_description'][0];
  329. } else {
  330. $scale = 0;
  331. }
  332. if (isset($clean_input['program_id'][0])) {
  333. $program_id = $clean_input['program_id'][0];
  334. } else {
  335. $program_id = NULL;
  336. }
  337. if (isset($clean_input['objective_id'][0]) && $clean_input['objective_id'][0] != '0') {
  338. $objective_id = $clean_input['objective_id'][0];
  339. Log::info('Estamos');
  340. } else {
  341. $objective_id = NULL;
  342. }
  343. return Validator::make(
  344. array(
  345. 'name' => $clean_input['name'],
  346. 'subcriteria' => $clean_input['subcriteria'],
  347. // 'outcome_id' => $clean_input['outcome_id'],
  348. 'notes' => $clean_input['notes'],
  349. 'copyright' => $clean_input['copyright'],
  350. 'maximum_score' => $clean_input['maximum_score'],
  351. 'scales' => $scale,
  352. 'objective_id' => $objective_id,
  353. 'program_id' => $program_id,
  354. ),
  355. array(
  356. 'name' => 'required|string',
  357. 'subcriteria' => 'string',
  358. // 'outcome_id' => 'required|array',
  359. 'scales' => 'required|string',
  360. 'notes' => 'string',
  361. 'copyright' => 'string',
  362. 'maximum_score' => 'required|integer',
  363. 'objective_id' => 'required|string',
  364. 'program_id' => 'required|integer'
  365. )
  366. );
  367. }
  368. /**
  369. * Create a new criterion.
  370. *
  371. * @return Redirect Redirect back to form page
  372. */
  373. public function create()
  374. {
  375. $clean_input = $this->cleanInput();
  376. Log::info(json_encode($clean_input));
  377. /** Validation rules */
  378. $validator = $this->makeValidator($clean_input);
  379. /** If validation fails */
  380. if ($validator->fails()) {
  381. /** Prepare error message */
  382. $message = '<p>Error(s) creating a new Criterion:</p><ul>';
  383. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  384. $message .= $validationError;
  385. }
  386. $message .= '</ul>';
  387. /** Send error message and old data */
  388. Session::flash('status', 'danger');
  389. Session::flash('message', $message);
  390. return Redirect::to('criteria')->withInput();
  391. $role = Auth::user()['role'];
  392. switch ($role) {
  393. case 1:
  394. return Redirect::to('criteria')->withInput();
  395. case 2:
  396. // Log::info(Input::all());
  397. return Redirect::to('school-criteria')->withInput();
  398. case 3:
  399. return Redirect::to('program-criteria')->withInput();
  400. }
  401. } else {
  402. // Check criterion uniqueness
  403. if (!$this->isCriterionUnique($clean_input)) {
  404. /** Send error message and old data*/
  405. Session::flash('status', 'danger');
  406. Session::flash('message', 'This criterion is a duplicate of an already saved criterion because its name and associated program are the same.');
  407. return Redirect::to('criteria')->withInput();
  408. $role = Auth::user()['role'];
  409. switch ($role) {
  410. case 1:
  411. return Redirect::to('criteria')->withInput();
  412. case 2:
  413. return Redirect::to('school-criteria')->withInput();
  414. case 3:
  415. return Redirect::to('program-criteria')->withInput();
  416. }
  417. }
  418. /** Instantiate new criterion */
  419. $criterion = new Criterion;
  420. $criterion->name = $clean_input['name'];
  421. $criterion->subcriteria = $clean_input['subcriteria'];
  422. //gabriel añadió aqui
  423. $criterion->num_scales = sizeof($clean_input['scale_description']);
  424. $criterion->max_score = $clean_input['maximum_score'];
  425. if (Input::get('copyright'))
  426. $criterion->copyright = $clean_input['copyright'];
  427. if (Input::get('notes'))
  428. $criterion->notes = $clean_input['notes'];
  429. /** If criterion is saved, send success message */
  430. Log::info(Input::all());
  431. if ($criterion->save()) {
  432. $criterionId = $criterion->id;
  433. $parentesis = array('(', ')');
  434. foreach ($clean_input['objective_id'] as $objective_id) {
  435. $outcome_objective = str_replace($parentesis, '', $objective_id);
  436. $outcome_objective = explode(',', $outcome_objective);
  437. $cobo_id = DB::table('criterion_objective_outcome')
  438. ->insertGetId(array(
  439. "objective_id" => $outcome_objective[1],
  440. "outcome_id" => $outcome_objective[0],
  441. "criterion_id" => $criterionId
  442. ));
  443. foreach ($clean_input['program_id'] as $program_id) {
  444. DB::table('program_criterion_objective_outcome')
  445. ->insert(array(
  446. 'program_id' => $program_id,
  447. 'cri_obj_out_id' => $cobo_id
  448. ));
  449. }
  450. //DB::insert("insert into `criterion_objective_outcome` (`objective_id`, `outcome_id`, `criterion_id`) values ({$outcome_objective[1]},{$outcome_objective[0]}, {$criterionId})");
  451. }
  452. for ($i = 0; $i < sizeof($clean_input['scale_description']); $i++) {
  453. $existing_scale = DB::table('scales')->where('description', $clean_input['scale_description'][$i])
  454. ->first();
  455. if ($existing_scale) {
  456. DB::insert("insert into `criterion_scale` (`criterion_id`, `scale_id`, `position`) values({$criterionId},{$existing_scale->id},{$i})");
  457. } else {
  458. $scale = new Scale;
  459. $position = $i;
  460. $scale->description = $clean_input['scale_description'][$i];
  461. if ($scale->save()) {
  462. DB::insert("insert into `criterion_scale` (`criterion_id`, `scale_id`, `position`) values({$criterionId},{$scale->id}, {$position})");
  463. } else {
  464. Session::flash('status', 'danger');
  465. Session::flash('message', '<p>Error creating the Scales</p>');
  466. return Redirect::to('criteria')->withInput();
  467. $role = Auth::user()['role'];
  468. switch ($role) {
  469. case 1:
  470. return Redirect::to('criteria')->withInput();
  471. case 2:
  472. return Redirect::to('school-criteria')->withInput();
  473. case 3:
  474. return Redirect::to('program-criteria')->withInput();
  475. }
  476. }
  477. }
  478. }
  479. /* $role = Auth::user()->role;
  480. switch ($role) {
  481. case 1:
  482. $program_ids = DB::table('criterion_objective_outcome')
  483. ->join('objective_program', 'criterion_objective_outcome.objective_id', '=', 'objective_program.program_id')
  484. ->where('criterion_id', $criterionId)
  485. ->select('program_id')
  486. ->distinct()
  487. ->lists('program_id');
  488. break;
  489. case 2:
  490. $program_ids = DB::table('criterion_objective_outcome')
  491. ->join('objective_program', 'criterion_objective_outcome.objective_id', '=', 'objective_program.program_id')
  492. ->join('programs', 'programs.id', '=', 'objective_program.program_id')
  493. ->where('criterion_id', $criterionId)
  494. ->where('programs.school_id', Auth::user()->school_id)
  495. ->select('program_id')
  496. ->distinct()
  497. ->lists('program_id');
  498. break;
  499. case 3:
  500. $program_ids = DB::table('program_user')
  501. ->where('user_id', Auth::user()->id)
  502. ->lists('program_id');
  503. break;
  504. }*/
  505. //foreach ($clean_input['program_id'] as $program_id) {
  506. // DB::insert("insert into `program_criterion` (`criterion_id`, `program_id`) values({$criterionId},{$program_id})");
  507. //}
  508. Session::flash('status', 'success');
  509. Session::flash('message', 'Criterion created: "' . $criterion->name . '".');
  510. return Redirect::to('criteria')->withInput();
  511. $role = Auth::user()['role'];
  512. switch ($role) {
  513. case 1:
  514. return Redirect::to('criteria')->withInput();
  515. case 2:
  516. return Redirect::to('school-criteria')->withInput();
  517. case 3:
  518. return Redirect::to('program-criteria')->withInput();
  519. }
  520. }
  521. /** If saving fails, send error message and old data */
  522. else {
  523. Session::flash('status', 'danger');
  524. Session::flash('message', '<p>Error creating Criterion. Please try again later.</p>');
  525. return Redirect::to('criteria')->withInput();
  526. $role = Auth::user()['role'];
  527. switch ($role) {
  528. case 1:
  529. return Redirect::to('criteria')->withInput();
  530. case 2:
  531. return Redirect::to('school-criteria')->withInput();
  532. case 3:
  533. return Redirect::to('program-criteria')->withInput();
  534. }
  535. }
  536. }
  537. }
  538. public function edit()
  539. {
  540. $title = "Criteria";
  541. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->lists('name', 'id');
  542. //$schools = School::orderBy('name', 'ASC')->get();
  543. // $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  544. // $objectives = DB::table('objectives')->orderBy('text', 'ASC')->lists('text', 'id');
  545. $role = Auth::user()->role;
  546. switch ($role) {
  547. case 1:
  548. $program_ids = DB::table('programs')->lists('id');
  549. break;
  550. case 2:
  551. $program_ids = DB::table('programs')
  552. ->where('school_id', Auth::user()->school_id)
  553. ->lists('id');
  554. break;
  555. case 3:
  556. $program_ids = DB::table('program_user')
  557. ->where('user_id', Auth::user()->id)
  558. ->lists('program_id');
  559. break;
  560. }
  561. //Log::info($userProgram);
  562. $title = "Criteria";
  563. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->lists('name', 'id');
  564. //$criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  565. $programs = Program::whereIn("id", $program_ids)->get();
  566. return View::make('local.managers.shared.criteria', compact('title', 'outcomes', 'programs', 'objectives'));
  567. // return View::make('local.managers.shared.criteria', compact('title', 'outcomes', 'schools', 'criteria', 'programs', 'objectives'));
  568. }
  569. private function cleanInputEdit()
  570. {
  571. $clean_input = array();
  572. $clean_input['name'] = trim(preg_replace('/\t+/', '', Input::get('name')));
  573. $trimmed = trim(preg_replace('/\t+/', '', Input::get('subcriteria')));
  574. Log::info('trimmed 1 -->' . $trimmed . '<--');
  575. if ($trimmed == '') {
  576. $trimmed = NULL;
  577. } else {
  578. $trimmed = json_encode(preg_split('/\r\n/', $trimmed));
  579. }
  580. Log::info('trimmed 2 -->' . $trimmed . '<--');
  581. $clean_input['subcriteria'] = $trimmed;
  582. $clean_input['outcome_id'] = Input::get('outcome');
  583. $clean_input['objective_id'] = Input::get('objective');
  584. $clean_input['program_id'] = Input::get('program_id');
  585. $clean_input['copyright'] = trim(preg_replace('/\t+/', '', Input::get('copyright')));
  586. $clean_input['notes'] = trim(preg_replace('/\t+/', '', Input::get('notes')));
  587. $clean_input['maximum_score'] = (int) Input::get('maximum_score');
  588. //$clean_input['scale_title'] = Input::get('assoc_title');
  589. $clean_input['scale_description'] = Input::get('Scales');
  590. $clean_input['number_of_scales'] = sizeof($clean_input['scale_description']);
  591. return $clean_input;
  592. }
  593. public function changeStatus()
  594. {
  595. $criterion_id = Input::get('criterion_id');
  596. $criterion = Criterion::find($criterion_id);
  597. if (!$criterion->deleted_at) {
  598. $criterion->deleted_at = date('Y-m-d H:i:s');
  599. if ($criterion->save())
  600. return "deleted";
  601. else return 'error';
  602. } else {
  603. $criterion->deleted_at = NULL;
  604. if ($criterion->save())
  605. return "activated";
  606. else return 'error';
  607. }
  608. }
  609. public function update()
  610. {
  611. $criterion = Criterion::withTrashed()->find(Input::get('id'));
  612. $clean_input = $this->cleanInputEdit();
  613. /** Validation rules */
  614. $validator = $this->makeValidator($clean_input);
  615. /** If validation fails */
  616. if ($validator->fails()) {
  617. /** Prepare error message */
  618. $message = 'Error(s) updating the Criterion: <ul>';
  619. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  620. $message .= $validationError;
  621. }
  622. $message .= '</ul>';
  623. /** Send error message and old data */
  624. Session::flash('status', 'danger');
  625. Session::flash('message', $message);
  626. return Redirect::to('criteria')->withInput();
  627. $role = Auth::user()['role'];
  628. switch ($role) {
  629. case 1:
  630. return Redirect::to('criteria')->withInput();
  631. case 2:
  632. return Redirect::to('school-criteria')->withInput();
  633. case 3:
  634. return Redirect::to('program-criteria')->withInput();
  635. }
  636. } else {
  637. // // Check criterion uniqueness
  638. /*if (!$this->isCriterionUnique($clean_input, $criterion)) {
  639. /** Send error message and old data */
  640. /*Session::flash('status', 'danger');
  641. Session::flash('message', 'This criterion is a duplicate of an already saved criterion because its name, assoaciated school or program, and progress indicators are the same.');
  642. $role = Auth::user()['role'];
  643. switch ($role) {
  644. case 1:
  645. return Redirect::to('criteria')->withInput();
  646. case 2:
  647. return Redirect::to('school-criteria')->withInput();
  648. case 3:
  649. return Redirect::to('program-criteria')->withInput();
  650. }
  651. }*/
  652. /** Set info */
  653. $criterion->name = $clean_input['name'];
  654. $criterion->subcriteria = $clean_input['subcriteria'];
  655. $criterion->num_scales = $clean_input['number_of_scales'];
  656. $criterion->max_score = $clean_input['maximum_score'];
  657. // Set program
  658. /*
  659. if (Input::get('program_id') != 0)
  660. $criterion->program_id = Input::get('program_id');
  661. else
  662. $criterion->program_id = NULL;*/
  663. // Set status
  664. if (Input::get('status') == 0)
  665. $criterion->deleted_at = date('Y-m-d H:i:s');
  666. else
  667. $criterion->deleted_at = NULL;
  668. if (Input::get('copyright'))
  669. $criterion->copyright = $clean_input['copyright'];
  670. else
  671. $criterion->copyright = NULL;
  672. if (Input::get('notes'))
  673. $criterion->notes = $clean_input['notes'];
  674. else
  675. $criterion->notes = NULL;
  676. /** If criterion is updated, send success message */
  677. $parentesis = array('(', ')');
  678. if ($criterion->save()) {
  679. $criterionId = $criterion->id;
  680. DB::table('criterion_objective_outcome as cobo')
  681. ->join('program_criterion_objective_outcome as poco', 'poco.cri_obj_out_id', '=', 'cobo.id')
  682. ->where('cobo.criterion_id', $criterionId)
  683. ->delete();
  684. // DB::delete("delete from `criterion_objective_outcome` where `criterion_id` ={$criterionId}");
  685. DB::delete("delete from `program_criterion` where `criterion_id` ={$criterionId}");
  686. foreach ($clean_input['objective_id'] as $objective_id) {
  687. $outcome_objective = str_replace($parentesis, '', $objective_id);
  688. $outcome_objective = explode(',', $outcome_objective);
  689. $cobo_id = DB::table('criterion_objective_outcome')
  690. ->insertGetId(array(
  691. "objective_id" => $outcome_objective[1],
  692. "outcome_id" => $outcome_objective[0],
  693. "criterion_id" => $criterionId
  694. ));
  695. foreach ($clean_input['program_id'] as $program_id) {
  696. DB::table('program_criterion_objective_outcome')
  697. ->insert(array(
  698. 'program_id' => $program_id,
  699. 'cri_obj_out_id' => $cobo_id
  700. ));
  701. }
  702. }
  703. /*DB::delete("delete from `scales` where id in (select scale_id id from criterion_scale where criterion_id = {$criterionId})");
  704. */
  705. DB::delete("delete from criterion_scale where criterion_id = {$criterionId}");
  706. for ($i = 0; $i < sizeof($clean_input['scale_description']); $i++) {
  707. $existing_scale = DB::table('scales')->where('description', $clean_input['scale_description'][$i])
  708. ->first();
  709. if ($existing_scale) {
  710. DB::insert("insert into `criterion_scale` (`criterion_id`, `scale_id`, `position`) values({$criterionId},{$existing_scale->id},{$i})");
  711. } else {
  712. $scale = new Scale;
  713. $position = $i;
  714. $scale->description = $clean_input['scale_description'][$i];
  715. if ($scale->save()) {
  716. DB::insert("insert into `criterion_scale` (`criterion_id`, `scale_id`, `position`) values({$criterionId},{$scale->id}, {$position})");
  717. } else {
  718. Session::flash('status', 'danger');
  719. Session::flash('message', '<p>Error creating the Scales</p>');
  720. return Redirect::to('criteria')->withInput();
  721. /*$role = Auth::user()['role'];
  722. switch ($role) {
  723. case 1:
  724. return Redirect::to('criteria')->withInput();
  725. case 2:
  726. return Redirect::to('school-criteria')->withInput();
  727. case 3:
  728. return Redirect::to('program-criteria')->withInput();
  729. }*/
  730. }
  731. }
  732. }/*
  733. switch ($role) {
  734. case 1:
  735. $program_ids = DB::table('criterion_objective_outcome')
  736. ->join('objective_program', 'criterion_objective_outcome.objective_id', '=', 'objective_program.program_id')
  737. ->where('criterion_id', $criterionId)
  738. ->select('program_id')
  739. ->distinct()
  740. ->lists('program_id');
  741. break;
  742. case 2:
  743. $program_ids = DB::table('criterion_objective_outcome')
  744. ->join('objective_program', 'criterion_objective_outcome.objective_id', '=', 'objective_program.program_id')
  745. ->join('programs', 'programs.id', '=', 'objective_program.program_id')
  746. ->where('criterion_id', $criterionId)
  747. ->where('programs.school_id', Auth::user()->school_id)
  748. ->select('program_id')
  749. ->distinct()
  750. ->lists('program_id');
  751. break;
  752. case 3:
  753. $program_ids = DB::table('program_user')
  754. ->where('user_id', Auth::user()->id)
  755. ->lists('program_id');
  756. break;
  757. }*/
  758. //foreach ($clean_input['program_id'] as $program_id) {
  759. // DB::insert("insert into `program_criterion` (`criterion_id`, `program_id`) values({$criterionId},{$program_id})");
  760. //}
  761. Session::flash('status', 'success');
  762. Session::flash('message', 'Updated criterion: "' . $criterion->name . '"');
  763. return Redirect::to('criteria')->withInput();
  764. /*$role = Auth::user()['role'];
  765. switch ($role) {
  766. case 1:
  767. return Redirect::to('criteria')->withInput();
  768. case 2:
  769. return Redirect::to('school-criteria')->withInput();
  770. case 3:
  771. return Redirect::to('program-criteria')->withInput();
  772. }*/
  773. }
  774. /** If saving fails, send error message and old data */
  775. else {
  776. Session::flash('status', 'danger');
  777. Session::flash('message', 'Error updating the Criterion. Please try again later.');
  778. return Redirect::to('criteria')->withInput();
  779. $role = Auth::user()['role'];
  780. /*switch ($role) {
  781. case 1:
  782. return Redirect::to('criteria')->withInput();
  783. case 2:
  784. return Redirect::to('school-criteria')->withInput();
  785. case 3:
  786. return Redirect::to('program-criteria')->withInput();
  787. }*/
  788. }
  789. }
  790. }
  791. public function index()
  792. {
  793. // el ID de los semestres que el usuario tiene seleccionado.
  794. $semesters_ids = Session::get('semesters_ids');
  795. // buscar informacion de los semestres seleccionados
  796. $semesters = Semester::whereIn('id', $semesters_ids)->get();
  797. $title = "Learning Outcomes and Criteria";
  798. $outcomes = Outcome::orderBy('name', 'ASC')->get();
  799. // $outcomes = DB::table('outcomes')
  800. // ->orderBy('name', 'asc')
  801. // ->get();
  802. $schools = School::orderBy('name', 'ASC')->get();
  803. // $schools = DB::table('schools')
  804. // ->orderBy('name', 'asc')
  805. // ->get();
  806. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  807. // $criteria = DB::table('criteria')
  808. // ->orderBy('name', 'asc')
  809. // ->get();
  810. // se annadio la nueva variable
  811. return View::make('global.view-learning-outcomes-criteria', compact('title', 'outcomes', 'schools', 'criteria', 'semesters'));
  812. }
  813. // copie index() y lo edite
  814. public function objectivesIndex()
  815. {
  816. if (Auth::user()->role == 1) {
  817. //buscar todos los objetivos
  818. /*$objectives = DB::table('program_user')
  819. ->join('objective_program', 'objective_program.program_id', '=', 'program_user.program_id')
  820. ->join('objectives', 'objectives.id', '=', 'objective_program.objective_id')
  821. ->join('programs', 'programs.id', '=', 'program_user.program_id')
  822. ->select('objectives.id', 'objectives.text', 'programs.name')
  823. ->orderBy('objectives.text', 'asc')
  824. ->get();*/
  825. $objectives = DB::table('objectives')
  826. ->where('objectives.active', 1)
  827. ->orderBy('objectives.text', 'asc')
  828. ->get();
  829. } elseif (Auth::user()->role == 2) {
  830. //buscar los objetivos de la departamento (school)
  831. /*$objectives = DB::table('program_user')
  832. ->join('objective_program', 'objective_program.program_id', '=', 'program_user.program_id')
  833. ->join('objectives', 'objectives.id', '=', 'objective_program.objective_id')
  834. ->join('programs', 'programs.id', '=', 'program_user.program_id')
  835. ->where('programs.school_id', Auth::user()->school_id)
  836. ->select('objectives.id', 'objectives.text', 'programs.name')
  837. ->orderBy('objectives.text', 'asc')
  838. ->get();
  839. */
  840. $objectives = DB::table('objectives')
  841. ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
  842. ->join('programs', 'programs.id', '=', 'objective_program.program_id')
  843. ->where('programs.school_id', Auth::user()->school_id)
  844. ->where('objectives.active', 1)
  845. ->orderBy('objectives.text', 'asc')
  846. ->select('objectives.id', 'objectives.text')
  847. ->distinct()
  848. ->get();
  849. } elseif ((Auth::user()->role == 3) || (Auth::user()->role == 4)) {
  850. //buscar los objetivos de los programas cuales el profesor esta
  851. $objectives = DB::table('program_user')
  852. ->join('objective_program', 'objective_program.program_id', '=', 'program_user.program_id')
  853. ->join('objectives', 'objectives.id', '=', 'objective_program.objective_id')
  854. ->join('programs', 'programs.id', '=', 'program_user.program_id')
  855. ->where('program_user.user_id', Auth::user()->id)
  856. ->where('objectives.active', 1)
  857. ->select('objectives.id', 'objectives.text')
  858. ->distinct()
  859. ->orderBy('objectives.text', 'asc')
  860. ->get();
  861. }
  862. $title = "Learning Objectives and Criteria";
  863. return View::make('global.view-objectives-criteria', compact('title', 'objectives'));
  864. }
  865. public function destroy()
  866. {
  867. $criterion = Criterion::withTrashed()->find(Input::get('id'));
  868. if (!$criterion->trashed()) {
  869. try {
  870. $criterion->delete();
  871. Session::flash('status', 'success');
  872. Session::flash('message', 'Deactivated criterion: "' . $criterion->name . '"');
  873. } catch (Exception $e) {
  874. Session::flash('status', 'danger');
  875. Session::flash('message', 'Error deactivating criterion."' . $criterion->name . '"');
  876. }
  877. return Redirect::to('criteria')->withInput();
  878. /*$role = Auth::user()['role'];
  879. switch ($role) {
  880. case 1:
  881. return Redirect::to('objective')->withInput();
  882. case 2:
  883. return Redirect::to('school-objective')->withInput();
  884. case 3:
  885. return Redirect::to('program-objective')->withInput();
  886. }*/
  887. } else {
  888. try {
  889. $criterion->restore();
  890. Session::flash('status', 'success');
  891. Session::flash('message', 'Reactivated criterion: "' . $criterion->name . '"');
  892. } catch (Exception $e) {
  893. Session::flash('status', 'danger');
  894. Session::flash('message', 'Error reactivating criterion: "' . $criterion->name . '".');
  895. }
  896. return Redirect::to('criteria')->withInput();
  897. /*$role = Auth::user()['role'];
  898. switch ($role) {
  899. case 1:
  900. return Redirect::to('objective')->withInput();
  901. case 2:
  902. return Redirect::to('school-objective')->withInput();
  903. case 3:
  904. return Redirect::to('program-objective')->withInput();
  905. }*/
  906. }
  907. }
  908. public function filterCriteria()
  909. {
  910. switch (Input::get('filter')) {
  911. case 'all':
  912. return Criteria::all();
  913. break;
  914. case 'school':
  915. // If scoord
  916. if (Auth::user()->role == '2') {
  917. // Fetch all the programs whose school is the user's
  918. $program_ids = DB::table('programs')->where('school_id', Auth::user()->school_id)->lists('id');
  919. // Return all criteria belonging to any of those programs
  920. return Criterion::whereIn('program_id', $program_ids)
  921. ->orderBy('name', 'ASC')
  922. ->get();
  923. }
  924. // If pcoord
  925. else {
  926. // Fetch all the programs from the user's school;
  927. $program_ids = DB::table('programs')->where('school_id', Auth::user()->programs[0]->school->id)->lists('id');
  928. return Criterion::whereIn('program_id', $program_ids)
  929. ->orderBy('name', 'ASC')
  930. ->get();
  931. }
  932. break;
  933. case 'program':
  934. return Criterion::whereIn('program_id', Auth::user()->programs->lists('id'))
  935. ->orderBy('name', 'ASC')
  936. ->get();
  937. break;
  938. default:
  939. return Criteria::all();
  940. break;
  941. }
  942. }
  943. public function fetchFromTemplate()
  944. {
  945. $role = Auth::user()->role;
  946. $criterion_id = Input::get('criterion_id');
  947. switch ($role) {
  948. case 1:
  949. $program_ids = Program::select('programs.*')->lists('id');
  950. break;
  951. case 2:
  952. $program_ids = Program::where("school_id", Auth::user()->school_id)->lists('id');
  953. break;
  954. case 3:
  955. case 4:
  956. $program_ids = Program::whereIn('id', Auth::user()->programs->lists('id'));
  957. break;
  958. }
  959. $outcomes_paired_ids = DB::table('criterion_objective_outcome as cobo')
  960. ->join('program_criterion_objective_outcome as poco', 'poco.cri_obj_out_id', '=', 'cobo.id')
  961. ->whereIn("program_id", $program_ids)
  962. ->where('criterion_id', $criterion_id)
  963. ->select('cobo.outcome_id')
  964. ->distinct()
  965. ->lists('cobo.outcome_id');
  966. $json = [];
  967. $json['outcomes'] = $outcomes_paired_ids;
  968. $json['objectives_available'] = DB::table('objectives')
  969. ->join('objective_outcome', 'objective_outcome.objective_id', '=', 'objectives.id')
  970. ->join('outcomes', 'outcomes.id', '=', 'outcome_id')
  971. ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
  972. ->whereIn("outcome_id", $outcomes_paired_ids)
  973. ->whereIn("program_id", $program_ids)
  974. ->where('objectives.id', '<>', 0)
  975. ->select('objectives.*')
  976. ->distinct()
  977. ->get();
  978. $json['objectives_selected'] = DB::table('objectives')
  979. ->join('criterion_objective_outcome as cobo', 'cobo.objective_id', '=', 'objectives.id')
  980. ->join('outcomes', 'outcomes.id', '=', 'outcome_id')
  981. ->join('program_criterion_objective_outcome as poco', 'poco.cri_obj_out_id', '=', 'cobo.id')
  982. ->whereIn('outcome_id', $outcomes_paired_ids)
  983. ->whereIn("program_id", $program_ids)
  984. ->where('criterion_id', $criterion_id)
  985. ->where('objective_id', '<>', 0)
  986. ->select('objectives.*')
  987. //->groupBy('objectives.id', 'outcome_id')
  988. ->distinct()
  989. ->get();
  990. return $json;
  991. }
  992. }