Bez popisu

CriteriaController.php 42KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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 where outcome_id ={$outcome_id}) and id in(select criterion_id from program_criterion where 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. }
  95. $outcome_id = Input::get('outcomeID');
  96. Log::info($outcome_id);
  97. $json = DB::table('objectives')
  98. ->join('objective_outcome', 'objective_outcome.objective_id', '=', 'objectives.id')
  99. ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
  100. ->where('outcome_id', '=', $outcome_id)
  101. ->where('objectives.id', '<>', 0)
  102. ->whereIn('program_id', $program_ids)
  103. ->select('objectives.*', 'objective_outcome.*')
  104. ->distinct()
  105. ->get();
  106. foreach ($json as $objective) {
  107. $objective->program_ids = json_encode(DB::table('objective_program')
  108. ->where('objective_id', $objective->objective_id)
  109. ->lists('program_id'), JSON_NUMERIC_CHECK);
  110. }
  111. return json_encode($json);
  112. }
  113. public function fetchCriterionWithTrashed()
  114. {
  115. /* $json = array();
  116. $json['criteria'] = DB::table('criteria')->where('id', Input::get('id'))->get();
  117. $assoc_outcomes = DB::table('criterion_objective_outcome')->where('criterion_id', Input::get('id'))->lists('outcome_id');
  118. $json['outcomes'] = DB::table('outcomes')->whereIn('id', $assoc_outcomes)->get();
  119. $criteria_id = Input::get('id');
  120. foreach ($json['outcomes'] as $outcome) {
  121. $id = $outcome->id;
  122. $json['objectives'][$id] = DB::table('objectives')
  123. ->join('criterion_objective_outcome', 'objectives.id', '=', 'criterion_objective_outcome.objective_id')
  124. ->where('criterion_id', '=', $criteria_id)
  125. ->where('outcome_id', '=', $id)
  126. ->get();
  127. $json['objectives_assoc'][$id] = DB::table('objectives')
  128. ->join('objective_outcome', 'objective_outcome.objective_id', '=', 'objectives.id')
  129. ->where('outcome_id', $id)
  130. ->get();
  131. foreach ($json['objectives_assoc'][$id] as $objective) {
  132. $objective->program_ids = json_encode(DB::table('objective_program')
  133. ->where('objective_id', $objective->objective_id)
  134. ->lists('program_id'));
  135. }
  136. }
  137. $json['program'] = DB::table('program_criterion')->where('criterion_id', $criteria_id)->get();
  138. $json['activity_criterion'] = DB::table('assessments')
  139. ->join('activity_criterion', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  140. ->where('activity_criterion.criterion_id', $criteria_id)
  141. ->get();
  142. $json['scales'] = DB::table('criterion_scale')
  143. ->join('scales', 'criterion_scale.scale_id', '=', 'scales.id')
  144. ->where('criterion_id', $criteria_id)
  145. ->orderBy('position')
  146. ->get();
  147. /*
  148. $json['criteria'] = DB::select(" select * from criteria where id = ?", array(Input::get('id')));
  149. $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')));
  150. $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')));
  151. $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')));
  152. $json['program'] = DB::select("select criterion_id, program_id from program_criterion where criterion_id =?", array(Input::get('id')));
  153. $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')));
  154. foreach ($json['outcomes'] as $id) {
  155. $outId = $id->id;
  156. $json['outcomes_assoc'][$outId] = DB::select("select name from outcomes where id = {$outId}");
  157. $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");
  158. }*/
  159. $criteria_id = Input::get('id');
  160. $criterion = DB::table('criteria')->where('id', Input::get('id'))->first();
  161. if (!$criterion) return $criterion;
  162. $criterion->outcomes = DB::table('criterion_objective_outcome')
  163. ->join('outcomes', 'outcomes.id', '=', 'criterion_objective_outcome.outcome_id')
  164. ->where('criterion_id', $criterion->id)
  165. ->select('outcomes.*')
  166. ->distinct()
  167. ->get();
  168. $userRole = Auth::user()['role'];
  169. foreach ($criterion->outcomes as $outcome) {
  170. switch ($userRole) {
  171. case 1:
  172. $program_ids = DB::table('programs')->lists('id');
  173. break;
  174. case 2:
  175. $program_ids = DB::table('programs')
  176. ->where('school_id', Auth::user()->school_id)
  177. ->lists('id');
  178. break;
  179. case 3:
  180. $program_ids = DB::table('program_user')
  181. ->where('user_id', Auth::user()->id)
  182. ->lists('program_id');
  183. break;
  184. }
  185. $outcome->assoc_objectives = DB::table('objective_outcome')
  186. ->join('objectives', 'objectives.id', '=', 'objective_outcome.objective_id')
  187. ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
  188. ->whereIn('program_id', $program_ids)
  189. ->where('outcome_id', $outcome->id)
  190. ->select('objectives.*', 'objectives.id as objective_id')
  191. ->distinct()
  192. ->get();
  193. $outcome->objectives_criteria = DB::table('criterion_objective_outcome')
  194. ->join('objectives', 'objectives.id', '=', 'criterion_objective_outcome.objective_id')
  195. ->where('criterion_id', $criterion->id)
  196. ->where('outcome_id', $outcome->id)
  197. ->get();
  198. foreach ($outcome->assoc_objectives as $objective) {
  199. $objective->program_ids = json_encode(DB::table('objective_program')
  200. ->where('objective_id', $objective->id)
  201. ->lists('program_id'), JSON_NUMERIC_CHECK);
  202. }
  203. }
  204. $criterion->scales = DB::table('criterion_scale')
  205. ->join('scales', 'criterion_scale.scale_id', '=', 'scales.id')
  206. ->where('criterion_id', $criteria_id)
  207. ->orderBy('position')
  208. ->get();
  209. $criterion->program = DB::table('program_criterion')->where('criterion_id', $criteria_id)->get();
  210. $criterion->activity_criterion = DB::table('assessments')
  211. ->join('activity_criterion', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  212. ->where('activity_criterion.criterion_id', $criteria_id)
  213. ->get();
  214. return array($criterion);
  215. }
  216. public function delete()
  217. {
  218. DB::delete("delete from criteria where id = ?", array(Input::get('criterion_delete')));
  219. $role = Auth::user()['role'];
  220. switch ($role) {
  221. case 1:
  222. return Redirect::to('criteria')->withInput();
  223. case 2:
  224. return Redirect::to('school-criteria')->withInput();
  225. case 3:
  226. return Redirect::to('program-criteria')->withInput();
  227. }
  228. }
  229. public function isCriterionUnique($input, $existing_criterion = NULL)
  230. {
  231. // dd($input);
  232. Log::info('isCriterionUnique');
  233. if (Input::get('program_id') != 0)
  234. $program_id = $input['program_id'];
  235. else
  236. $program_id = NULL;
  237. $outcomes = [];
  238. $objectives = [];
  239. foreach ($input['objective_id'] as $objective) {
  240. $parentesis = array('(', ')');
  241. $outcome_objective = str_replace($parentesis, '', $objective);
  242. $outcomes[] = $outcome_objective[0];
  243. $objectives[] = $outcome_objective[1];
  244. }
  245. $scales = [];
  246. foreach ($input['scale_description'] as $scale) {
  247. $existing_scale = DB::table('scales')->where('description', $scale)
  248. ->first();
  249. if (!$existing_scale) return true;
  250. $scales[] = $existing_scale->id;
  251. }
  252. $saved_criterion = DB::table('criteria')
  253. ->join('program_criterion', 'program_criterion.criterion_id', '=', 'criteria.id')
  254. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  255. ->join('criterion_scale', 'criterion_scale.criterion_id', '=', 'criteria.id')
  256. ->whereIn('program_id', $input['program_id'])
  257. ->whereIn('objective_id', $objectives)
  258. ->whereIn('outcome_id', $outcomes)
  259. ->where('name', '=', $input['name'])
  260. ->where('subcriteria', '=', $input['subcriteria'])
  261. ->whereIn('scale_id', $scales)
  262. ->where('num_scales', $input['number_of_scales'])
  263. ->where('max_score', $input['maximum_score'])
  264. ->first();
  265. if ($saved_criterion)
  266. return false;
  267. else
  268. return true;
  269. }
  270. private function cleanInput()
  271. {
  272. $clean_input = array();
  273. $clean_input['name'] = trim(preg_replace('/\t+/', '', Input::get('name')));
  274. $trimmed = trim(preg_replace('/\t+/', '', Input::get('subcriteria')));
  275. Log::info('trimmed 1 -->' . $trimmed . '<--');
  276. if ($trimmed == '') {
  277. $trimmed = NULL;
  278. } else {
  279. $trimmed = json_encode(preg_split('/\r\n/', $trimmed));
  280. }
  281. Log::info('trimmed 2 -->' . $trimmed . '<--');
  282. $clean_input['subcriteria'] = $trimmed;
  283. $clean_input['outcome_id'] = Input::get('outcome');
  284. $clean_input['objective_id'] = Input::get('objective');
  285. $clean_input['program_id'] = Input::get('program_id');
  286. $clean_input['copyright'] = trim(preg_replace('/\t+/', '', Input::get('copyright')));
  287. $clean_input['notes'] = trim(preg_replace('/\t+/', '', Input::get('notes')));
  288. $clean_input['maximum_score'] = (int) Input::get('maximum_score');
  289. $clean_input['scale_description'] = Input::get('Scales');
  290. $clean_input['number_of_scales'] = sizeof($clean_input['scale_description']);
  291. return $clean_input;
  292. }
  293. private function makeValidator($clean_input)
  294. {
  295. if (isset($clean_input['scale_description'][0])) {
  296. $scale = $clean_input['scale_description'][0];
  297. } else {
  298. $scale = 0;
  299. }
  300. if (isset($clean_input['program_id'][0])) {
  301. $program_id = $clean_input['program_id'][0];
  302. } else {
  303. $program_id = NULL;
  304. }
  305. if (isset($clean_input['objective_id'][0]) && $clean_input['objective_id'][0] != '0') {
  306. $objective_id = $clean_input['objective_id'][0];
  307. Log::info('Estamos');
  308. } else {
  309. $objective_id = NULL;
  310. }
  311. return Validator::make(
  312. array(
  313. 'name' => $clean_input['name'],
  314. 'subcriteria' => $clean_input['subcriteria'],
  315. // 'outcome_id' => $clean_input['outcome_id'],
  316. 'notes' => $clean_input['notes'],
  317. 'copyright' => $clean_input['copyright'],
  318. 'maximum_score' => $clean_input['maximum_score'],
  319. 'scales' => $scale,
  320. 'objective_id' => $objective_id,
  321. 'program_id' => $program_id,
  322. ),
  323. array(
  324. 'name' => 'required|string',
  325. 'subcriteria' => 'string',
  326. // 'outcome_id' => 'required|array',
  327. 'scales' => 'required|string',
  328. 'notes' => 'string',
  329. 'copyright' => 'string',
  330. 'maximum_score' => 'required|integer',
  331. 'objective_id' => 'required|string',
  332. 'program_id' => 'required|integer'
  333. )
  334. );
  335. }
  336. /**
  337. * Create a new criterion.
  338. *
  339. * @return Redirect Redirect back to form page
  340. */
  341. public function create()
  342. {
  343. $clean_input = $this->cleanInput();
  344. /** Validation rules */
  345. $validator = $this->makeValidator($clean_input);
  346. /** If validation fails */
  347. if ($validator->fails()) {
  348. /** Prepare error message */
  349. $message = '<p>Error(s) creating a new Criterion:</p><ul>';
  350. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  351. $message .= $validationError;
  352. }
  353. $message .= '</ul>';
  354. /** Send error message and old data */
  355. Session::flash('status', 'danger');
  356. Session::flash('message', $message);
  357. $role = Auth::user()['role'];
  358. switch ($role) {
  359. case 1:
  360. return Redirect::to('criteria')->withInput();
  361. case 2:
  362. Log::info(Input::all());
  363. return Redirect::to('school-criteria')->withInput();
  364. case 3:
  365. return Redirect::to('program-criteria')->withInput();
  366. }
  367. } else {
  368. // Check criterion uniqueness
  369. if (!$this->isCriterionUnique($clean_input)) {
  370. /** Send error message and old data*/
  371. Session::flash('status', 'danger');
  372. Session::flash('message', 'This criterion is a duplicate of an already saved criterion because its name and associated program are the same.');
  373. $role = Auth::user()['role'];
  374. switch ($role) {
  375. case 1:
  376. return Redirect::to('criteria')->withInput();
  377. case 2:
  378. return Redirect::to('school-criteria')->withInput();
  379. case 3:
  380. return Redirect::to('program-criteria')->withInput();
  381. }
  382. }
  383. /** Instantiate new criterion */
  384. $criterion = new Criterion;
  385. $criterion->name = $clean_input['name'];
  386. $criterion->subcriteria = $clean_input['subcriteria'];
  387. //gabriel añadió aqui
  388. $criterion->num_scales = sizeof($clean_input['scale_description']);
  389. $criterion->max_score = $clean_input['maximum_score'];
  390. if (Input::get('copyright'))
  391. $criterion->copyright = $clean_input['copyright'];
  392. if (Input::get('notes'))
  393. $criterion->notes = $clean_input['notes'];
  394. /** If criterion is saved, send success message */
  395. if ($criterion->save()) {
  396. $criterionId = $criterion->id;
  397. $parentesis = array('(', ')');
  398. foreach ($clean_input['objective_id'] as $objective_id) {
  399. $outcome_objective = str_replace($parentesis, '', $objective_id);
  400. $outcome_objective = explode(',', $outcome_objective);
  401. DB::insert("insert into `criterion_objective_outcome` (`objective_id`, `outcome_id`, `criterion_id`) values ({$outcome_objective[1]},{$outcome_objective[0]}, {$criterionId})");
  402. }
  403. for ($i = 0; $i < sizeof($clean_input['scale_description']); $i++) {
  404. $existing_scale = DB::table('scales')->where('description', $clean_input['scale_description'][$i])
  405. ->first();
  406. if ($existing_scale) {
  407. DB::insert("insert into `criterion_scale` (`criterion_id`, `scale_id`, `position`) values({$criterionId},{$existing_scale->id},{$i})");
  408. } else {
  409. $scale = new Scale;
  410. $position = $i;
  411. $scale->description = $clean_input['scale_description'][$i];
  412. if ($scale->save()) {
  413. DB::insert("insert into `criterion_scale` (`criterion_id`, `scale_id`, `position`) values({$criterionId},{$scale->id}, {$position})");
  414. } else {
  415. Session::flash('status', 'danger');
  416. Session::flash('message', '<p>Error creating the Scales</p>');
  417. $role = Auth::user()['role'];
  418. switch ($role) {
  419. case 1:
  420. return Redirect::to('criteria')->withInput();
  421. case 2:
  422. return Redirect::to('school-criteria')->withInput();
  423. case 3:
  424. return Redirect::to('program-criteria')->withInput();
  425. }
  426. }
  427. }
  428. }
  429. /* $role = Auth::user()->role;
  430. switch ($role) {
  431. case 1:
  432. $program_ids = DB::table('criterion_objective_outcome')
  433. ->join('objective_program', 'criterion_objective_outcome.objective_id', '=', 'objective_program.program_id')
  434. ->where('criterion_id', $criterionId)
  435. ->select('program_id')
  436. ->distinct()
  437. ->lists('program_id');
  438. break;
  439. case 2:
  440. $program_ids = DB::table('criterion_objective_outcome')
  441. ->join('objective_program', 'criterion_objective_outcome.objective_id', '=', 'objective_program.program_id')
  442. ->join('programs', 'programs.id', '=', 'objective_program.program_id')
  443. ->where('criterion_id', $criterionId)
  444. ->where('programs.school_id', Auth::user()->school_id)
  445. ->select('program_id')
  446. ->distinct()
  447. ->lists('program_id');
  448. break;
  449. case 3:
  450. $program_ids = DB::table('program_user')
  451. ->where('user_id', Auth::user()->id)
  452. ->lists('program_id');
  453. break;
  454. }*/
  455. foreach ($clean_input['program_id'] as $program_id) {
  456. DB::insert("insert into `program_criterion` (`criterion_id`, `program_id`) values({$criterionId},{$program_id})");
  457. }
  458. Session::flash('status', 'success');
  459. Session::flash('message', 'Criterion created: "' . $criterion->name . '".');
  460. $role = Auth::user()['role'];
  461. switch ($role) {
  462. case 1:
  463. return Redirect::to('criteria')->withInput();
  464. case 2:
  465. return Redirect::to('school-criteria')->withInput();
  466. case 3:
  467. return Redirect::to('program-criteria')->withInput();
  468. }
  469. }
  470. /** If saving fails, send error message and old data */
  471. else {
  472. Session::flash('status', 'danger');
  473. Session::flash('message', '<p>Error creating Criterion. Please try again later.</p>');
  474. $role = Auth::user()['role'];
  475. switch ($role) {
  476. case 1:
  477. return Redirect::to('criteria')->withInput();
  478. case 2:
  479. return Redirect::to('school-criteria')->withInput();
  480. case 3:
  481. return Redirect::to('program-criteria')->withInput();
  482. }
  483. }
  484. }
  485. }
  486. public function edit()
  487. {
  488. $title = "Criteria";
  489. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->lists('name', 'id');
  490. $schools = School::orderBy('name', 'ASC')->get();
  491. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  492. $programs = Program::orderBy('name', 'ASC')->get();
  493. $objectives = DB::table('objectives')->orderBy('text', 'ASC')->lists('text', 'id');
  494. return View::make('local.managers.admins.criteria', compact('title', 'outcomes', 'schools', 'criteria', 'programs', 'objectives'));
  495. }
  496. private function cleanInputEdit()
  497. {
  498. $clean_input = array();
  499. $clean_input['name'] = trim(preg_replace('/\t+/', '', Input::get('name')));
  500. $trimmed = trim(preg_replace('/\t+/', '', Input::get('subcriteria')));
  501. Log::info('trimmed 1 -->' . $trimmed . '<--');
  502. if ($trimmed == '') {
  503. $trimmed = NULL;
  504. } else {
  505. $trimmed = json_encode(preg_split('/\r\n/', $trimmed));
  506. }
  507. Log::info('trimmed 2 -->' . $trimmed . '<--');
  508. $clean_input['subcriteria'] = $trimmed;
  509. $clean_input['outcome_id'] = Input::get('outcome');
  510. $clean_input['objective_id'] = Input::get('objective');
  511. $clean_input['program_id'] = Input::get('program_id');
  512. $clean_input['copyright'] = trim(preg_replace('/\t+/', '', Input::get('copyright')));
  513. $clean_input['notes'] = trim(preg_replace('/\t+/', '', Input::get('notes')));
  514. $clean_input['maximum_score'] = (int) Input::get('maximum_score');
  515. //$clean_input['scale_title'] = Input::get('assoc_title');
  516. $clean_input['scale_description'] = Input::get('Scales');
  517. $clean_input['number_of_scales'] = sizeof($clean_input['scale_description']);
  518. return $clean_input;
  519. }
  520. public function changeStatus()
  521. {
  522. $criterion_id = Input::get('criterion_id');
  523. $criterion = Criterion::find($criterion_id);
  524. if (!$criterion->deleted_at) {
  525. $criterion->deleted_at = date('Y-m-d H:i:s');
  526. if ($criterion->save())
  527. return "deleted";
  528. else return 'error';
  529. } else {
  530. $criterion->deleted_at = NULL;
  531. if ($criterion->save())
  532. return "activated";
  533. else return 'error';
  534. }
  535. }
  536. public function update()
  537. {
  538. $criterion = Criterion::withTrashed()->find(Input::get('id'));
  539. $clean_input = $this->cleanInputEdit();
  540. /** Validation rules */
  541. $validator = $this->makeValidator($clean_input);
  542. /** If validation fails */
  543. if ($validator->fails()) {
  544. /** Prepare error message */
  545. $message = 'Error(s) updating the Criterion: <ul>';
  546. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  547. $message .= $validationError;
  548. }
  549. $message .= '</ul>';
  550. /** Send error message and old data */
  551. Session::flash('status', 'danger');
  552. Session::flash('message', $message);
  553. $role = Auth::user()['role'];
  554. switch ($role) {
  555. case 1:
  556. return Redirect::to('criteria')->withInput();
  557. case 2:
  558. return Redirect::to('school-criteria')->withInput();
  559. case 3:
  560. return Redirect::to('program-criteria')->withInput();
  561. }
  562. } else {
  563. // // Check criterion uniqueness
  564. /*if (!$this->isCriterionUnique($clean_input, $criterion)) {
  565. /** Send error message and old data */
  566. /*Session::flash('status', 'danger');
  567. 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.');
  568. $role = Auth::user()['role'];
  569. switch ($role) {
  570. case 1:
  571. return Redirect::to('criteria')->withInput();
  572. case 2:
  573. return Redirect::to('school-criteria')->withInput();
  574. case 3:
  575. return Redirect::to('program-criteria')->withInput();
  576. }
  577. }*/
  578. /** Set info */
  579. $criterion->name = $clean_input['name'];
  580. $criterion->subcriteria = $clean_input['subcriteria'];
  581. $criterion->num_scales = $clean_input['number_of_scales'];
  582. $criterion->max_score = $clean_input['maximum_score'];
  583. // Set program
  584. /*
  585. if (Input::get('program_id') != 0)
  586. $criterion->program_id = Input::get('program_id');
  587. else
  588. $criterion->program_id = NULL;*/
  589. // Set status
  590. if (Input::get('status') == 0)
  591. $criterion->deleted_at = date('Y-m-d H:i:s');
  592. else
  593. $criterion->deleted_at = NULL;
  594. if (Input::get('copyright'))
  595. $criterion->copyright = $clean_input['copyright'];
  596. else
  597. $criterion->copyright = NULL;
  598. if (Input::get('notes'))
  599. $criterion->notes = $clean_input['notes'];
  600. else
  601. $criterion->notes = NULL;
  602. /** If criterion is updated, send success message */
  603. $parentesis = array('(', ')');
  604. if ($criterion->save()) {
  605. $criterionId = $criterion->id;
  606. DB::delete("delete from `criterion_objective_outcome` where `criterion_id` ={$criterionId}");
  607. DB::delete("delete from `program_criterion` where `criterion_id` ={$criterionId}");
  608. foreach ($clean_input['objective_id'] as $objective_id) {
  609. $outcome_objective = str_replace($parentesis, '', $objective_id);
  610. $outcome_objective = explode(',', $outcome_objective);
  611. DB::insert("insert into `criterion_objective_outcome` (`objective_id`, `outcome_id`, `criterion_id`) values ({$outcome_objective[1]},{$outcome_objective[0]}, {$criterionId})");
  612. }
  613. /*DB::delete("delete from `scales` where id in (select scale_id id from criterion_scale where criterion_id = {$criterionId})");
  614. */
  615. DB::delete("delete from criterion_scale where criterion_id = {$criterionId}");
  616. for ($i = 0; $i < sizeof($clean_input['scale_description']); $i++) {
  617. $existing_scale = DB::table('scales')->where('description', $clean_input['scale_description'][$i])
  618. ->first();
  619. if ($existing_scale) {
  620. DB::insert("insert into `criterion_scale` (`criterion_id`, `scale_id`, `position`) values({$criterionId},{$existing_scale->id},{$i})");
  621. } else {
  622. $scale = new Scale;
  623. $position = $i;
  624. $scale->description = $clean_input['scale_description'][$i];
  625. if ($scale->save()) {
  626. DB::insert("insert into `criterion_scale` (`criterion_id`, `scale_id`, `position`) values({$criterionId},{$scale->id}, {$position})");
  627. } else {
  628. Session::flash('status', 'danger');
  629. Session::flash('message', '<p>Error creating the Scales</p>');
  630. $role = Auth::user()['role'];
  631. switch ($role) {
  632. case 1:
  633. return Redirect::to('criteria')->withInput();
  634. case 2:
  635. return Redirect::to('school-criteria')->withInput();
  636. case 3:
  637. return Redirect::to('program-criteria')->withInput();
  638. }
  639. }
  640. }
  641. }/*
  642. switch ($role) {
  643. case 1:
  644. $program_ids = DB::table('criterion_objective_outcome')
  645. ->join('objective_program', 'criterion_objective_outcome.objective_id', '=', 'objective_program.program_id')
  646. ->where('criterion_id', $criterionId)
  647. ->select('program_id')
  648. ->distinct()
  649. ->lists('program_id');
  650. break;
  651. case 2:
  652. $program_ids = DB::table('criterion_objective_outcome')
  653. ->join('objective_program', 'criterion_objective_outcome.objective_id', '=', 'objective_program.program_id')
  654. ->join('programs', 'programs.id', '=', 'objective_program.program_id')
  655. ->where('criterion_id', $criterionId)
  656. ->where('programs.school_id', Auth::user()->school_id)
  657. ->select('program_id')
  658. ->distinct()
  659. ->lists('program_id');
  660. break;
  661. case 3:
  662. $program_ids = DB::table('program_user')
  663. ->where('user_id', Auth::user()->id)
  664. ->lists('program_id');
  665. break;
  666. }*/
  667. foreach ($clean_input['program_id'] as $program_id) {
  668. DB::insert("insert into `program_criterion` (`criterion_id`, `program_id`) values({$criterionId},{$program_id})");
  669. }
  670. Session::flash('status', 'success');
  671. Session::flash('message', 'Updated criterion: "' . $criterion->name . '"');
  672. $role = Auth::user()['role'];
  673. switch ($role) {
  674. case 1:
  675. return Redirect::to('criteria')->withInput();
  676. case 2:
  677. return Redirect::to('school-criteria')->withInput();
  678. case 3:
  679. return Redirect::to('program-criteria')->withInput();
  680. }
  681. }
  682. /** If saving fails, send error message and old data */
  683. else {
  684. Session::flash('status', 'danger');
  685. Session::flash('message', 'Error updating the Criterion. Please try again later.');
  686. $role = Auth::user()['role'];
  687. switch ($role) {
  688. case 1:
  689. return Redirect::to('criteria')->withInput();
  690. case 2:
  691. return Redirect::to('school-criteria')->withInput();
  692. case 3:
  693. return Redirect::to('program-criteria')->withInput();
  694. }
  695. }
  696. }
  697. }
  698. public function index()
  699. {
  700. // el ID de los semestres que el usuario tiene seleccionado.
  701. $semesters_ids = Session::get('semesters_ids');
  702. // buscar informacion de los semestres seleccionados
  703. $semesters = Semester::whereIn('id', $semesters_ids)->get();
  704. $title = "Learning Outcomes and Criteria";
  705. $outcomes = Outcome::orderBy('name', 'ASC')->get();
  706. // $outcomes = DB::table('outcomes')
  707. // ->orderBy('name', 'asc')
  708. // ->get();
  709. $schools = School::orderBy('name', 'ASC')->get();
  710. // $schools = DB::table('schools')
  711. // ->orderBy('name', 'asc')
  712. // ->get();
  713. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  714. // $criteria = DB::table('criteria')
  715. // ->orderBy('name', 'asc')
  716. // ->get();
  717. // se annadio la nueva variable
  718. return View::make('global.view-learning-outcomes-criteria', compact('title', 'outcomes', 'schools', 'criteria', 'semesters'));
  719. }
  720. // copie index() y lo edite
  721. public function objectivesIndex()
  722. {
  723. if (Auth::user()->role == 1) {
  724. //buscar todos los objetivos
  725. /*$objectives = DB::table('program_user')
  726. ->join('objective_program', 'objective_program.program_id', '=', 'program_user.program_id')
  727. ->join('objectives', 'objectives.id', '=', 'objective_program.objective_id')
  728. ->join('programs', 'programs.id', '=', 'program_user.program_id')
  729. ->select('objectives.id', 'objectives.text', 'programs.name')
  730. ->orderBy('objectives.text', 'asc')
  731. ->get();*/
  732. $objectives = DB::table('objectives')
  733. ->where('objectives.active', 1)
  734. ->orderBy('objectives.text', 'asc')
  735. ->get();
  736. } elseif (Auth::user()->role == 2) {
  737. //buscar los objetivos de la departamento (school)
  738. /*$objectives = DB::table('program_user')
  739. ->join('objective_program', 'objective_program.program_id', '=', 'program_user.program_id')
  740. ->join('objectives', 'objectives.id', '=', 'objective_program.objective_id')
  741. ->join('programs', 'programs.id', '=', 'program_user.program_id')
  742. ->where('programs.school_id', Auth::user()->school_id)
  743. ->select('objectives.id', 'objectives.text', 'programs.name')
  744. ->orderBy('objectives.text', 'asc')
  745. ->get();
  746. */
  747. $objectives = DB::table('objectives')
  748. ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
  749. ->join('programs', 'programs.id', '=', 'objective_program.program_id')
  750. ->where('programs.school_id', Auth::user()->school_id)
  751. ->where('objectives.active', 1)
  752. ->orderBy('objectives.text', 'asc')
  753. ->select('objectives.id', 'objectives.text')
  754. ->distinct()
  755. ->get();
  756. } elseif ((Auth::user()->role == 3) || (Auth::user()->role == 4)) {
  757. //buscar los objetivos de los programas cuales el profesor esta
  758. $objectives = DB::table('program_user')
  759. ->join('objective_program', 'objective_program.program_id', '=', 'program_user.program_id')
  760. ->join('objectives', 'objectives.id', '=', 'objective_program.objective_id')
  761. ->join('programs', 'programs.id', '=', 'program_user.program_id')
  762. ->where('program_user.user_id', Auth::user()->id)
  763. ->where('objectives.active', 1)
  764. ->select('objectives.id', 'objectives.text')
  765. ->distinct()
  766. ->orderBy('objectives.text', 'asc')
  767. ->get();
  768. }
  769. $title = "Learning Objectives and Criteria";
  770. return View::make('global.view-objectives-criteria', compact('title', 'objectives'));
  771. }
  772. public function destroy()
  773. {
  774. $criterion = Criterion::withTrashed()->find(Input::get('id'));
  775. if (!$criterion->trashed()) {
  776. try {
  777. $criterion->delete();
  778. Session::flash('status', 'success');
  779. Session::flash('message', 'Deactivated criterion: "' . $criterion->name . '"');
  780. } catch (Exception $e) {
  781. Session::flash('status', 'danger');
  782. Session::flash('message', 'Error deactivating criterion."' . $criterion->name . '"');
  783. }
  784. $role = Auth::user()['role'];
  785. switch ($role) {
  786. case 1:
  787. return Redirect::to('objective')->withInput();
  788. case 2:
  789. return Redirect::to('school-objective')->withInput();
  790. case 3:
  791. return Redirect::to('program-objective')->withInput();
  792. }
  793. } else {
  794. try {
  795. $criterion->restore();
  796. Session::flash('status', 'success');
  797. Session::flash('message', 'Reactivated criterion: "' . $criterion->name . '"');
  798. } catch (Exception $e) {
  799. Session::flash('status', 'danger');
  800. Session::flash('message', 'Error reactivating criterion: "' . $criterion->name . '".');
  801. }
  802. $role = Auth::user()['role'];
  803. switch ($role) {
  804. case 1:
  805. return Redirect::to('objective')->withInput();
  806. case 2:
  807. return Redirect::to('school-objective')->withInput();
  808. case 3:
  809. return Redirect::to('program-objective')->withInput();
  810. }
  811. }
  812. }
  813. public function filterCriteria()
  814. {
  815. switch (Input::get('filter')) {
  816. case 'all':
  817. return Criteria::all();
  818. break;
  819. case 'school':
  820. // If scoord
  821. if (Auth::user()->role == '2') {
  822. // Fetch all the programs whose school is the user's
  823. $program_ids = DB::table('programs')->where('school_id', Auth::user()->school_id)->lists('id');
  824. // Return all criteria belonging to any of those programs
  825. return Criterion::whereIn('program_id', $program_ids)
  826. ->orderBy('name', 'ASC')
  827. ->get();
  828. }
  829. // If pcoord
  830. else {
  831. // Fetch all the programs from the user's school;
  832. $program_ids = DB::table('programs')->where('school_id', Auth::user()->programs[0]->school->id)->lists('id');
  833. return Criterion::whereIn('program_id', $program_ids)
  834. ->orderBy('name', 'ASC')
  835. ->get();
  836. }
  837. break;
  838. case 'program':
  839. return Criterion::whereIn('program_id', Auth::user()->programs->lists('id'))
  840. ->orderBy('name', 'ASC')
  841. ->get();
  842. break;
  843. default:
  844. return Criteria::all();
  845. break;
  846. }
  847. }
  848. }