Nav apraksta

CriteriaController.php 47KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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. $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. Log::info(json_encode($clean_input));
  366. /** Validation rules */
  367. $validator = $this->makeValidator($clean_input);
  368. /** If validation fails */
  369. if ($validator->fails()) {
  370. /** Prepare error message */
  371. $message = '<p>Error(s) creating a new Criterion:</p><ul>';
  372. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  373. $message .= $validationError;
  374. }
  375. $message .= '</ul>';
  376. /** Send error message and old data */
  377. Session::flash('status', 'danger');
  378. Session::flash('message', $message);
  379. return Redirect::to('criteria')->withInput();
  380. $role = Auth::user()['role'];
  381. switch ($role) {
  382. case 1:
  383. return Redirect::to('criteria')->withInput();
  384. case 2:
  385. // Log::info(Input::all());
  386. return Redirect::to('school-criteria')->withInput();
  387. case 3:
  388. return Redirect::to('program-criteria')->withInput();
  389. }
  390. } else {
  391. // Check criterion uniqueness
  392. if (!$this->isCriterionUnique($clean_input)) {
  393. /** Send error message and old data*/
  394. Session::flash('status', 'danger');
  395. Session::flash('message', 'This criterion is a duplicate of an already saved criterion because its name and associated program are the same.');
  396. return Redirect::to('criteria')->withInput();
  397. $role = Auth::user()['role'];
  398. switch ($role) {
  399. case 1:
  400. return Redirect::to('criteria')->withInput();
  401. case 2:
  402. return Redirect::to('school-criteria')->withInput();
  403. case 3:
  404. return Redirect::to('program-criteria')->withInput();
  405. }
  406. }
  407. /** Instantiate new criterion */
  408. $criterion = new Criterion;
  409. $criterion->name = $clean_input['name'];
  410. $criterion->subcriteria = $clean_input['subcriteria'];
  411. //gabriel añadió aqui
  412. $criterion->num_scales = sizeof($clean_input['scale_description']);
  413. $criterion->max_score = $clean_input['maximum_score'];
  414. if (Input::get('copyright'))
  415. $criterion->copyright = $clean_input['copyright'];
  416. if (Input::get('notes'))
  417. $criterion->notes = $clean_input['notes'];
  418. /** If criterion is saved, send success message */
  419. if ($criterion->save()) {
  420. $criterionId = $criterion->id;
  421. $parentesis = array('(', ')');
  422. foreach ($clean_input['objective_id'] as $objective_id) {
  423. $outcome_objective = str_replace($parentesis, '', $objective_id);
  424. $outcome_objective = explode(',', $outcome_objective);
  425. $cobo_id = DB::table('criterion_objective_outcome')
  426. ->insertGetId(array(
  427. "objective_id" => $outcome_objective[1],
  428. "outcome_id" => $outcome_objective[0],
  429. "criterion_id" => $criterionId
  430. ));
  431. foreach ($clean_input['program_id'] as $program_id) {
  432. DB::table('program_criterion_objective_outcome')
  433. ->insert(array(
  434. 'program_id' => $program_id,
  435. 'cri_obj_out_id' => $cobo_id
  436. ));
  437. }
  438. //DB::insert("insert into `criterion_objective_outcome` (`objective_id`, `outcome_id`, `criterion_id`) values ({$outcome_objective[1]},{$outcome_objective[0]}, {$criterionId})");
  439. }
  440. for ($i = 0; $i < sizeof($clean_input['scale_description']); $i++) {
  441. $existing_scale = DB::table('scales')->where('description', $clean_input['scale_description'][$i])
  442. ->first();
  443. if ($existing_scale) {
  444. DB::insert("insert into `criterion_scale` (`criterion_id`, `scale_id`, `position`) values({$criterionId},{$existing_scale->id},{$i})");
  445. } else {
  446. $scale = new Scale;
  447. $position = $i;
  448. $scale->description = $clean_input['scale_description'][$i];
  449. if ($scale->save()) {
  450. DB::insert("insert into `criterion_scale` (`criterion_id`, `scale_id`, `position`) values({$criterionId},{$scale->id}, {$position})");
  451. } else {
  452. Session::flash('status', 'danger');
  453. Session::flash('message', '<p>Error creating the Scales</p>');
  454. return Redirect::to('criteria')->withInput();
  455. $role = Auth::user()['role'];
  456. switch ($role) {
  457. case 1:
  458. return Redirect::to('criteria')->withInput();
  459. case 2:
  460. return Redirect::to('school-criteria')->withInput();
  461. case 3:
  462. return Redirect::to('program-criteria')->withInput();
  463. }
  464. }
  465. }
  466. }
  467. /* $role = Auth::user()->role;
  468. switch ($role) {
  469. case 1:
  470. $program_ids = DB::table('criterion_objective_outcome')
  471. ->join('objective_program', 'criterion_objective_outcome.objective_id', '=', 'objective_program.program_id')
  472. ->where('criterion_id', $criterionId)
  473. ->select('program_id')
  474. ->distinct()
  475. ->lists('program_id');
  476. break;
  477. case 2:
  478. $program_ids = DB::table('criterion_objective_outcome')
  479. ->join('objective_program', 'criterion_objective_outcome.objective_id', '=', 'objective_program.program_id')
  480. ->join('programs', 'programs.id', '=', 'objective_program.program_id')
  481. ->where('criterion_id', $criterionId)
  482. ->where('programs.school_id', Auth::user()->school_id)
  483. ->select('program_id')
  484. ->distinct()
  485. ->lists('program_id');
  486. break;
  487. case 3:
  488. $program_ids = DB::table('program_user')
  489. ->where('user_id', Auth::user()->id)
  490. ->lists('program_id');
  491. break;
  492. }*/
  493. foreach ($clean_input['program_id'] as $program_id) {
  494. DB::insert("insert into `program_criterion` (`criterion_id`, `program_id`) values({$criterionId},{$program_id})");
  495. }
  496. Session::flash('status', 'success');
  497. Session::flash('message', 'Criterion created: "' . $criterion->name . '".');
  498. return Redirect::to('criteria')->withInput();
  499. $role = Auth::user()['role'];
  500. switch ($role) {
  501. case 1:
  502. return Redirect::to('criteria')->withInput();
  503. case 2:
  504. return Redirect::to('school-criteria')->withInput();
  505. case 3:
  506. return Redirect::to('program-criteria')->withInput();
  507. }
  508. }
  509. /** If saving fails, send error message and old data */
  510. else {
  511. Session::flash('status', 'danger');
  512. Session::flash('message', '<p>Error creating Criterion. Please try again later.</p>');
  513. return Redirect::to('criteria')->withInput();
  514. $role = Auth::user()['role'];
  515. switch ($role) {
  516. case 1:
  517. return Redirect::to('criteria')->withInput();
  518. case 2:
  519. return Redirect::to('school-criteria')->withInput();
  520. case 3:
  521. return Redirect::to('program-criteria')->withInput();
  522. }
  523. }
  524. }
  525. }
  526. public function edit()
  527. {
  528. $title = "Criteria";
  529. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->lists('name', 'id');
  530. //$schools = School::orderBy('name', 'ASC')->get();
  531. // $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  532. // $objectives = DB::table('objectives')->orderBy('text', 'ASC')->lists('text', 'id');
  533. $role = Auth::user()->role;
  534. switch ($role) {
  535. case 1:
  536. $program_ids = DB::table('programs')->lists('id');
  537. break;
  538. case 2:
  539. $program_ids = DB::table('programs')
  540. ->where('school_id', Auth::user()->school_id)
  541. ->lists('id');
  542. break;
  543. case 3:
  544. $program_ids = DB::table('program_user')
  545. ->where('user_id', Auth::user()->id)
  546. ->lists('program_id');
  547. break;
  548. }
  549. //Log::info($userProgram);
  550. $title = "Criteria";
  551. $outcomes = Outcome::whereNull("deactivation_date")->orderBy('name', 'ASC')->lists('name', 'id');
  552. //$criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  553. $programs = Program::whereIn("id", $program_ids)->get();
  554. return View::make('local.managers.shared.criteria', compact('title', 'outcomes', 'programs', 'objectives'));
  555. // return View::make('local.managers.shared.criteria', compact('title', 'outcomes', 'schools', 'criteria', 'programs', 'objectives'));
  556. }
  557. private function cleanInputEdit()
  558. {
  559. $clean_input = array();
  560. $clean_input['name'] = trim(preg_replace('/\t+/', '', Input::get('name')));
  561. $trimmed = trim(preg_replace('/\t+/', '', Input::get('subcriteria')));
  562. Log::info('trimmed 1 -->' . $trimmed . '<--');
  563. if ($trimmed == '') {
  564. $trimmed = NULL;
  565. } else {
  566. $trimmed = json_encode(preg_split('/\r\n/', $trimmed));
  567. }
  568. Log::info('trimmed 2 -->' . $trimmed . '<--');
  569. $clean_input['subcriteria'] = $trimmed;
  570. $clean_input['outcome_id'] = Input::get('outcome');
  571. $clean_input['objective_id'] = Input::get('objective');
  572. $clean_input['program_id'] = Input::get('program_id');
  573. $clean_input['copyright'] = trim(preg_replace('/\t+/', '', Input::get('copyright')));
  574. $clean_input['notes'] = trim(preg_replace('/\t+/', '', Input::get('notes')));
  575. $clean_input['maximum_score'] = (int) Input::get('maximum_score');
  576. //$clean_input['scale_title'] = Input::get('assoc_title');
  577. $clean_input['scale_description'] = Input::get('Scales');
  578. $clean_input['number_of_scales'] = sizeof($clean_input['scale_description']);
  579. return $clean_input;
  580. }
  581. public function changeStatus()
  582. {
  583. $criterion_id = Input::get('criterion_id');
  584. $criterion = Criterion::find($criterion_id);
  585. if (!$criterion->deleted_at) {
  586. $criterion->deleted_at = date('Y-m-d H:i:s');
  587. if ($criterion->save())
  588. return "deleted";
  589. else return 'error';
  590. } else {
  591. $criterion->deleted_at = NULL;
  592. if ($criterion->save())
  593. return "activated";
  594. else return 'error';
  595. }
  596. }
  597. public function update()
  598. {
  599. $criterion = Criterion::withTrashed()->find(Input::get('id'));
  600. $clean_input = $this->cleanInputEdit();
  601. /** Validation rules */
  602. $validator = $this->makeValidator($clean_input);
  603. /** If validation fails */
  604. if ($validator->fails()) {
  605. /** Prepare error message */
  606. $message = 'Error(s) updating the Criterion: <ul>';
  607. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  608. $message .= $validationError;
  609. }
  610. $message .= '</ul>';
  611. /** Send error message and old data */
  612. Session::flash('status', 'danger');
  613. Session::flash('message', $message);
  614. return Redirect::to('criteria')->withInput();
  615. $role = Auth::user()['role'];
  616. switch ($role) {
  617. case 1:
  618. return Redirect::to('criteria')->withInput();
  619. case 2:
  620. return Redirect::to('school-criteria')->withInput();
  621. case 3:
  622. return Redirect::to('program-criteria')->withInput();
  623. }
  624. } else {
  625. // // Check criterion uniqueness
  626. /*if (!$this->isCriterionUnique($clean_input, $criterion)) {
  627. /** Send error message and old data */
  628. /*Session::flash('status', 'danger');
  629. 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.');
  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. /** Set info */
  641. $criterion->name = $clean_input['name'];
  642. $criterion->subcriteria = $clean_input['subcriteria'];
  643. $criterion->num_scales = $clean_input['number_of_scales'];
  644. $criterion->max_score = $clean_input['maximum_score'];
  645. // Set program
  646. /*
  647. if (Input::get('program_id') != 0)
  648. $criterion->program_id = Input::get('program_id');
  649. else
  650. $criterion->program_id = NULL;*/
  651. // Set status
  652. if (Input::get('status') == 0)
  653. $criterion->deleted_at = date('Y-m-d H:i:s');
  654. else
  655. $criterion->deleted_at = NULL;
  656. if (Input::get('copyright'))
  657. $criterion->copyright = $clean_input['copyright'];
  658. else
  659. $criterion->copyright = NULL;
  660. if (Input::get('notes'))
  661. $criterion->notes = $clean_input['notes'];
  662. else
  663. $criterion->notes = NULL;
  664. /** If criterion is updated, send success message */
  665. $parentesis = array('(', ')');
  666. if ($criterion->save()) {
  667. $criterionId = $criterion->id;
  668. DB::table('criterion_objective_outcome as cobo')
  669. ->join('program_criterion_objective_outcome as poco', 'poco.cri_obj_out_id', '=', 'cobo.id')
  670. ->where($criterionId)
  671. ->delete();
  672. // DB::delete("delete from `criterion_objective_outcome` where `criterion_id` ={$criterionId}");
  673. DB::delete("delete from `program_criterion` where `criterion_id` ={$criterionId}");
  674. foreach ($clean_input['objective_id'] as $objective_id) {
  675. $outcome_objective = str_replace($parentesis, '', $objective_id);
  676. $outcome_objective = explode(',', $outcome_objective);
  677. $cobo_id = DB::table('criterion_objective_outcome')
  678. ->insertGetId(array(
  679. "objective_id" => $outcome_objective[1],
  680. "outcome_id" => $outcome_objective[0],
  681. "criterion_id" => $criterionId
  682. ));
  683. foreach ($clean_input['program_id'] as $program_id) {
  684. DB::table('program_criterion_objective_outcome')
  685. ->insert(array(
  686. 'program_id' => $program_id,
  687. 'cri_obj_out_id' => $cobo_id
  688. ));
  689. }
  690. }
  691. /*DB::delete("delete from `scales` where id in (select scale_id id from criterion_scale where criterion_id = {$criterionId})");
  692. */
  693. DB::delete("delete from criterion_scale where criterion_id = {$criterionId}");
  694. for ($i = 0; $i < sizeof($clean_input['scale_description']); $i++) {
  695. $existing_scale = DB::table('scales')->where('description', $clean_input['scale_description'][$i])
  696. ->first();
  697. if ($existing_scale) {
  698. DB::insert("insert into `criterion_scale` (`criterion_id`, `scale_id`, `position`) values({$criterionId},{$existing_scale->id},{$i})");
  699. } else {
  700. $scale = new Scale;
  701. $position = $i;
  702. $scale->description = $clean_input['scale_description'][$i];
  703. if ($scale->save()) {
  704. DB::insert("insert into `criterion_scale` (`criterion_id`, `scale_id`, `position`) values({$criterionId},{$scale->id}, {$position})");
  705. } else {
  706. Session::flash('status', 'danger');
  707. Session::flash('message', '<p>Error creating the Scales</p>');
  708. return Redirect::to('criteria')->withInput();
  709. /*$role = Auth::user()['role'];
  710. switch ($role) {
  711. case 1:
  712. return Redirect::to('criteria')->withInput();
  713. case 2:
  714. return Redirect::to('school-criteria')->withInput();
  715. case 3:
  716. return Redirect::to('program-criteria')->withInput();
  717. }*/
  718. }
  719. }
  720. }/*
  721. switch ($role) {
  722. case 1:
  723. $program_ids = DB::table('criterion_objective_outcome')
  724. ->join('objective_program', 'criterion_objective_outcome.objective_id', '=', 'objective_program.program_id')
  725. ->where('criterion_id', $criterionId)
  726. ->select('program_id')
  727. ->distinct()
  728. ->lists('program_id');
  729. break;
  730. case 2:
  731. $program_ids = DB::table('criterion_objective_outcome')
  732. ->join('objective_program', 'criterion_objective_outcome.objective_id', '=', 'objective_program.program_id')
  733. ->join('programs', 'programs.id', '=', 'objective_program.program_id')
  734. ->where('criterion_id', $criterionId)
  735. ->where('programs.school_id', Auth::user()->school_id)
  736. ->select('program_id')
  737. ->distinct()
  738. ->lists('program_id');
  739. break;
  740. case 3:
  741. $program_ids = DB::table('program_user')
  742. ->where('user_id', Auth::user()->id)
  743. ->lists('program_id');
  744. break;
  745. }*/
  746. foreach ($clean_input['program_id'] as $program_id) {
  747. DB::insert("insert into `program_criterion` (`criterion_id`, `program_id`) values({$criterionId},{$program_id})");
  748. }
  749. Session::flash('status', 'success');
  750. Session::flash('message', 'Updated criterion: "' . $criterion->name . '"');
  751. return Redirect::to('criteria')->withInput();
  752. /*$role = Auth::user()['role'];
  753. switch ($role) {
  754. case 1:
  755. return Redirect::to('criteria')->withInput();
  756. case 2:
  757. return Redirect::to('school-criteria')->withInput();
  758. case 3:
  759. return Redirect::to('program-criteria')->withInput();
  760. }*/
  761. }
  762. /** If saving fails, send error message and old data */
  763. else {
  764. Session::flash('status', 'danger');
  765. Session::flash('message', 'Error updating the Criterion. Please try again later.');
  766. return Redirect::to('criteria')->withInput();
  767. $role = Auth::user()['role'];
  768. /*switch ($role) {
  769. case 1:
  770. return Redirect::to('criteria')->withInput();
  771. case 2:
  772. return Redirect::to('school-criteria')->withInput();
  773. case 3:
  774. return Redirect::to('program-criteria')->withInput();
  775. }*/
  776. }
  777. }
  778. }
  779. public function index()
  780. {
  781. // el ID de los semestres que el usuario tiene seleccionado.
  782. $semesters_ids = Session::get('semesters_ids');
  783. // buscar informacion de los semestres seleccionados
  784. $semesters = Semester::whereIn('id', $semesters_ids)->get();
  785. $title = "Learning Outcomes and Criteria";
  786. $outcomes = Outcome::orderBy('name', 'ASC')->get();
  787. // $outcomes = DB::table('outcomes')
  788. // ->orderBy('name', 'asc')
  789. // ->get();
  790. $schools = School::orderBy('name', 'ASC')->get();
  791. // $schools = DB::table('schools')
  792. // ->orderBy('name', 'asc')
  793. // ->get();
  794. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  795. // $criteria = DB::table('criteria')
  796. // ->orderBy('name', 'asc')
  797. // ->get();
  798. // se annadio la nueva variable
  799. return View::make('global.view-learning-outcomes-criteria', compact('title', 'outcomes', 'schools', 'criteria', 'semesters'));
  800. }
  801. // copie index() y lo edite
  802. public function objectivesIndex()
  803. {
  804. if (Auth::user()->role == 1) {
  805. //buscar todos los objetivos
  806. /*$objectives = DB::table('program_user')
  807. ->join('objective_program', 'objective_program.program_id', '=', 'program_user.program_id')
  808. ->join('objectives', 'objectives.id', '=', 'objective_program.objective_id')
  809. ->join('programs', 'programs.id', '=', 'program_user.program_id')
  810. ->select('objectives.id', 'objectives.text', 'programs.name')
  811. ->orderBy('objectives.text', 'asc')
  812. ->get();*/
  813. $objectives = DB::table('objectives')
  814. ->where('objectives.active', 1)
  815. ->orderBy('objectives.text', 'asc')
  816. ->get();
  817. } elseif (Auth::user()->role == 2) {
  818. //buscar los objetivos de la departamento (school)
  819. /*$objectives = DB::table('program_user')
  820. ->join('objective_program', 'objective_program.program_id', '=', 'program_user.program_id')
  821. ->join('objectives', 'objectives.id', '=', 'objective_program.objective_id')
  822. ->join('programs', 'programs.id', '=', 'program_user.program_id')
  823. ->where('programs.school_id', Auth::user()->school_id)
  824. ->select('objectives.id', 'objectives.text', 'programs.name')
  825. ->orderBy('objectives.text', 'asc')
  826. ->get();
  827. */
  828. $objectives = DB::table('objectives')
  829. ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
  830. ->join('programs', 'programs.id', '=', 'objective_program.program_id')
  831. ->where('programs.school_id', Auth::user()->school_id)
  832. ->where('objectives.active', 1)
  833. ->orderBy('objectives.text', 'asc')
  834. ->select('objectives.id', 'objectives.text')
  835. ->distinct()
  836. ->get();
  837. } elseif ((Auth::user()->role == 3) || (Auth::user()->role == 4)) {
  838. //buscar los objetivos de los programas cuales el profesor esta
  839. $objectives = DB::table('program_user')
  840. ->join('objective_program', 'objective_program.program_id', '=', 'program_user.program_id')
  841. ->join('objectives', 'objectives.id', '=', 'objective_program.objective_id')
  842. ->join('programs', 'programs.id', '=', 'program_user.program_id')
  843. ->where('program_user.user_id', Auth::user()->id)
  844. ->where('objectives.active', 1)
  845. ->select('objectives.id', 'objectives.text')
  846. ->distinct()
  847. ->orderBy('objectives.text', 'asc')
  848. ->get();
  849. }
  850. $title = "Learning Objectives and Criteria";
  851. return View::make('global.view-objectives-criteria', compact('title', 'objectives'));
  852. }
  853. public function destroy()
  854. {
  855. $criterion = Criterion::withTrashed()->find(Input::get('id'));
  856. if (!$criterion->trashed()) {
  857. try {
  858. $criterion->delete();
  859. Session::flash('status', 'success');
  860. Session::flash('message', 'Deactivated criterion: "' . $criterion->name . '"');
  861. } catch (Exception $e) {
  862. Session::flash('status', 'danger');
  863. Session::flash('message', 'Error deactivating criterion."' . $criterion->name . '"');
  864. }
  865. return Redirect::to('criteria')->withInput();
  866. /*$role = Auth::user()['role'];
  867. switch ($role) {
  868. case 1:
  869. return Redirect::to('objective')->withInput();
  870. case 2:
  871. return Redirect::to('school-objective')->withInput();
  872. case 3:
  873. return Redirect::to('program-objective')->withInput();
  874. }*/
  875. } else {
  876. try {
  877. $criterion->restore();
  878. Session::flash('status', 'success');
  879. Session::flash('message', 'Reactivated criterion: "' . $criterion->name . '"');
  880. } catch (Exception $e) {
  881. Session::flash('status', 'danger');
  882. Session::flash('message', 'Error reactivating criterion: "' . $criterion->name . '".');
  883. }
  884. return Redirect::to('criteria')->withInput();
  885. /*$role = Auth::user()['role'];
  886. switch ($role) {
  887. case 1:
  888. return Redirect::to('objective')->withInput();
  889. case 2:
  890. return Redirect::to('school-objective')->withInput();
  891. case 3:
  892. return Redirect::to('program-objective')->withInput();
  893. }*/
  894. }
  895. }
  896. public function filterCriteria()
  897. {
  898. switch (Input::get('filter')) {
  899. case 'all':
  900. return Criteria::all();
  901. break;
  902. case 'school':
  903. // If scoord
  904. if (Auth::user()->role == '2') {
  905. // Fetch all the programs whose school is the user's
  906. $program_ids = DB::table('programs')->where('school_id', Auth::user()->school_id)->lists('id');
  907. // Return all criteria belonging to any of those programs
  908. return Criterion::whereIn('program_id', $program_ids)
  909. ->orderBy('name', 'ASC')
  910. ->get();
  911. }
  912. // If pcoord
  913. else {
  914. // Fetch all the programs from the user's school;
  915. $program_ids = DB::table('programs')->where('school_id', Auth::user()->programs[0]->school->id)->lists('id');
  916. return Criterion::whereIn('program_id', $program_ids)
  917. ->orderBy('name', 'ASC')
  918. ->get();
  919. }
  920. break;
  921. case 'program':
  922. return Criterion::whereIn('program_id', Auth::user()->programs->lists('id'))
  923. ->orderBy('name', 'ASC')
  924. ->get();
  925. break;
  926. default:
  927. return Criteria::all();
  928. break;
  929. }
  930. }
  931. }