説明なし

CriteriaController.php 45KB

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