Geen omschrijving

CriteriaController.php 42KB

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