Geen omschrijving

CriteriaController.php 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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::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::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 fetchCriterion()
  39. {
  40. return Criterion::find(Input::get('id'));
  41. }
  42. public function fetchAllCriterion()
  43. {
  44. $program_id = Input::get('program_fetch');
  45. $outcome_id = Input::get('outcome_fetch');
  46. $json = array();
  47. $json['criterion'] = DB::select("SELECT * FROM `new_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})");
  48. return json_encode($json);
  49. }
  50. public function fetchObjectivesForSelect()
  51. {
  52. $json = array();
  53. Log::info("GET COCKEd");
  54. Log::info(Input::get('allOutcomes'));
  55. foreach (Input::get('allOutcomes') as $id) {
  56. $json['outcomes'][$id] = DB::select("select name from outcomes where id = {$id}");
  57. $json['objectives'][$id] = DB::select("select objectives.id, objectives.text, outcomes.name from objectives, objective_outcome, outcomes where objective_outcome.outcome_id ={$id} and objective_outcome.objective_id = objectives.id and objectives.active=1 and outcomes.id = objective_outcome.outcome_id");
  58. }
  59. Log::info(print_r($json, true));
  60. return json_encode($json);
  61. }
  62. public function fetchCriterionWithTrashed()
  63. {
  64. $json = array();
  65. $json['criteria'] = DB::select(" select * from new_criteria where id = ?", array(Input::get('id')));
  66. $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')));
  67. $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')));
  68. $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')));
  69. $json['scales'] = DB::select("select title, description, min_score, max_score from scales, criterion_scale where criterion_scale.scale_id = scales.id and criterion_id = ?", array(Input::get('id')));
  70. $json['program'] = DB::select("select criterion_id, program_id from program_criterion where criterion_id =?", array(Input::get('id')));
  71. $json['activity_criterion'] = DB::select("select * from new_assessments where activity_criterion_id in (select id from activity_criterion where criterion_id = ?)", array(Input::get('id')));
  72. foreach ($json['outcomes'] as $id) {
  73. $outId = $id->id;
  74. $json['outcomes_assoc'][$outId] = DB::select("select name from outcomes where id = {$outId}");
  75. $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");
  76. }
  77. return json_encode($json);
  78. }
  79. public function delete()
  80. {
  81. DB::delete("delete from new_criteria where id = ?", array(Input::get('criterion_delete')));
  82. $role = Auth::user()['role'];
  83. switch ($role) {
  84. case 1:
  85. return Redirect::to('criteria')->withInput();
  86. case 2:
  87. return Redirect::to('school-criteria')->withInput();
  88. case 3:
  89. return Redirect::to('program-criteria')->withInput();
  90. }
  91. }
  92. public function isCriterionUnique($input, $existing_criterion = NULL)
  93. {
  94. // dd($input);
  95. Log::info('isCriterionUnique');
  96. if (Input::get('program_id') != 0)
  97. $program_id = $input['program_id'];
  98. else
  99. $program_id = NULL;
  100. $saved_criterion = Criterion::withTrashed()
  101. ->where('name', '=', $input['name'])
  102. ->where('outcome_id', '=', $input['outcome_id'])
  103. ->where('program_id', '=', $program_id)
  104. ->first();
  105. if ($saved_criterion)
  106. return false;
  107. else
  108. return true;
  109. }
  110. private function cleanInput()
  111. {
  112. $clean_input = array();
  113. $clean_input['name'] = trim(preg_replace('/\t+/', '', Input::get('name')));
  114. $trimmed = trim(preg_replace('/\t+/', '', Input::get('subcriteria')));
  115. Log::info('trimmed 1 -->' . $trimmed . '<--');
  116. if ($trimmed == '') {
  117. $trimmed = NULL;
  118. } else {
  119. $trimmed = json_encode(preg_split('/\r\n/', $trimmed));
  120. }
  121. Log::info('trimmed 2 -->' . $trimmed . '<--');
  122. $clean_input['subcriteria'] = $trimmed;
  123. $clean_input['outcome_id'] = Input::get('outcome');
  124. $clean_input['objective_id'] = Input::get('objective');
  125. $clean_input['program_id'] = Input::get('program_id');
  126. $clean_input['copyright'] = trim(preg_replace('/\t+/', '', Input::get('copyright')));
  127. $clean_input['notes'] = trim(preg_replace('/\t+/', '', Input::get('notes')));
  128. $clean_input['maximum_score'] = (int) Input::get('maximum_score');
  129. $clean_input['scale_title'] = Input::get('title');
  130. $clean_input['scale_description'] = Input::get('Scales');
  131. $clean_input['min_score'] = Input::get('min');
  132. $clean_input['max_score'] = Input::get('max');
  133. $clean_input['number_of_scales'] = sizeof($clean_input['scale_title']);
  134. return $clean_input;
  135. }
  136. private function makeValidator($clean_input)
  137. {
  138. /** Validation rules */
  139. return Validator::make(
  140. array(
  141. 'name' => $clean_input['name'],
  142. 'subcriteria' => $clean_input['subcriteria'],
  143. 'outcome_id' => $clean_input['outcome_id'],
  144. 'notes' => $clean_input['notes'],
  145. 'copyright' => $clean_input['copyright'],
  146. 'maximum_score' => $clean_input['maximum_score']
  147. ),
  148. array(
  149. 'name' => 'required|string',
  150. 'subcriteria' => 'string',
  151. 'outcome_id' => 'required|array',
  152. 'notes' => 'string',
  153. 'copyright' => 'string',
  154. 'maximum_score' => 'required|integer'
  155. )
  156. );
  157. }
  158. /**
  159. * Create a new criterion.
  160. *
  161. * @return Redirect Redirect back to form page
  162. */
  163. public function create()
  164. {
  165. $clean_input = $this->cleanInput();
  166. /** Validation rules */
  167. $validator = $this->makeValidator($clean_input);
  168. /** If validation fails */
  169. if ($validator->fails()) {
  170. /** Prepare error message */
  171. $message = '<p>Error(s) creating a new Criterion:</p><ul>';
  172. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  173. $message .= $validationError;
  174. }
  175. $message .= '</ul>';
  176. /** Send error message and old data */
  177. Session::flash('status', 'danger');
  178. Session::flash('message', $message);
  179. $role = Auth::user()['role'];
  180. switch ($role) {
  181. case 1:
  182. return Redirect::to('criteria')->withInput();
  183. case 2:
  184. return Redirect::to('school-criteria')->withInput();
  185. case 3:
  186. return Redirect::to('program-criteria')->withInput();
  187. }
  188. } else {
  189. // Check criterion uniqueness
  190. /*if (!$this->isCriterionUnique($clean_input)) {
  191. /** Send error message and old data
  192. Session::flash('status', 'danger');
  193. Session::flash('message', 'This criterion is a duplicate of an already saved criterion because its name and associated program are the same.');
  194. return Redirect::to('criteria')->withInput();
  195. }*/
  196. /** Instantiate new criterion */
  197. $criterion = new Criterion;
  198. $criterion->name = $clean_input['name'];
  199. $criterion->subcriteria = $clean_input['subcriteria'];
  200. //gabriel añadió aqui
  201. $criterion->number_of_scales = $clean_input['number_of_scales'];
  202. $criterion->maximum_score = $clean_input['maximum_score'];
  203. if (Input::get('copyright'))
  204. $criterion->copyright = $clean_input['copyright'];
  205. if (Input::get('notes'))
  206. $criterion->notes = $clean_input['notes'];
  207. /** If criterion is saved, send success message */
  208. if ($criterion->save()) {
  209. $criterionId = $criterion->id;
  210. foreach ($clean_input['outcome_id'] as $outcome_id) {
  211. foreach ($clean_input['objective_id'] as $objective_id) {
  212. DB::insert("insert into `criterion_objective_outcome` (`objective_id`, `outcome_id`, `criterion_id`, `objective_outcome_id`) values ({$objective_id},{$outcome_id}, {$criterionId}, 0)");
  213. }
  214. }
  215. for ($i = 0; $i < sizeof($clean_input['scale_title']); $i++) {
  216. $scale = new Scale;
  217. $scale->title = $clean_input['scale_title'][$i];
  218. $scale->position = $i + 1;
  219. $scale->description = $clean_input['scale_description'][$i];
  220. $scale->min_score = $clean_input['min_score'][$i];
  221. $scale->max_score = $clean_input['max_score'][$i];
  222. if ($scale->save()) {
  223. DB::insert("insert into `criterion_scale` (`criterion_id`, `scale_id`) values({$criterionId},{$scale->id})");
  224. } else {
  225. Session::flash('status', 'danger');
  226. Session::flash('message', '<p>Error creating the Scales</p>');
  227. $role = Auth::user()['role'];
  228. switch ($role) {
  229. case 1:
  230. return Redirect::to('criteria')->withInput();
  231. case 2:
  232. return Redirect::to('school-criteria')->withInput();
  233. case 3:
  234. return Redirect::to('program-criteria')->withInput();
  235. }
  236. }
  237. }
  238. foreach ($clean_input['program_id'] as $program_id) {
  239. DB::insert("insert into `program_criterion` (`criterion_id`, `program_id`) values({$criterionId},{$program_id})");
  240. }
  241. Session::flash('status', 'success');
  242. Session::flash('message', 'Criterion created: "' . $criterion->name . '".');
  243. $role = Auth::user()['role'];
  244. switch ($role) {
  245. case 1:
  246. return Redirect::to('criteria')->withInput();
  247. case 2:
  248. return Redirect::to('school-criteria')->withInput();
  249. case 3:
  250. return Redirect::to('program-criteria')->withInput();
  251. }
  252. }
  253. /** If saving fails, send error message and old data */
  254. else {
  255. Session::flash('status', 'danger');
  256. Session::flash('message', '<p>Error creating Criterion. Please try again later.</p>');
  257. $role = Auth::user()['role'];
  258. switch ($role) {
  259. case 1:
  260. return Redirect::to('criteria')->withInput();
  261. case 2:
  262. return Redirect::to('school-criteria')->withInput();
  263. case 3:
  264. return Redirect::to('program-criteria')->withInput();
  265. }
  266. }
  267. }
  268. }
  269. public function edit()
  270. {
  271. $title = "Criteria";
  272. $outcomes = Outcome::orderBy('name', 'ASC')->lists('name', 'id');
  273. $schools = School::orderBy('name', 'ASC')->get();
  274. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  275. $programs = Program::orderBy('name', 'ASC')->get();
  276. $objectives = DB::table('objectives')->orderBy('text', 'ASC')->lists('text', 'id');
  277. return View::make('local.managers.admins.criteria', compact('title', 'outcomes', 'schools', 'criteria', 'programs', 'objectives'));
  278. }
  279. private function cleanInputEdit()
  280. {
  281. $clean_input = array();
  282. $clean_input['name'] = trim(preg_replace('/\t+/', '', Input::get('name')));
  283. $trimmed = trim(preg_replace('/\t+/', '', Input::get('subcriteria')));
  284. Log::info('trimmed 1 -->' . $trimmed . '<--');
  285. if ($trimmed == '') {
  286. $trimmed = NULL;
  287. } else {
  288. $trimmed = json_encode(preg_split('/\r\n/', $trimmed));
  289. }
  290. Log::info('trimmed 2 -->' . $trimmed . '<--');
  291. $clean_input['subcriteria'] = $trimmed;
  292. $clean_input['outcome_id'] = Input::get('assoc_outcome');
  293. $clean_input['objective_id'] = Input::get('assoc_objective');
  294. $clean_input['program_id'] = Input::get('program_id');
  295. $clean_input['copyright'] = trim(preg_replace('/\t+/', '', Input::get('copyright')));
  296. $clean_input['notes'] = trim(preg_replace('/\t+/', '', Input::get('notes')));
  297. $clean_input['maximum_score'] = (int) Input::get('assoc_maximum_score');
  298. $clean_input['scale_title'] = Input::get('assoc_title');
  299. $clean_input['scale_description'] = Input::get('assoc_scales');
  300. $clean_input['number_of_scales'] = sizeof($clean_input['scale_title']);
  301. return $clean_input;
  302. }
  303. public function update()
  304. {
  305. $criterion = Criterion::withTrashed()->find(Input::get('id'));
  306. $clean_input = $this->cleanInputEdit();
  307. /** Validation rules */
  308. $validator = $this->makeValidator($clean_input);
  309. /** If validation fails */
  310. if ($validator->fails()) {
  311. /** Prepare error message */
  312. $message = 'Error(s) updating the Criterion: <ul>';
  313. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  314. $message .= $validationError;
  315. }
  316. $message .= '</ul>';
  317. /** Send error message and old data */
  318. Session::flash('status', 'danger');
  319. Session::flash('message', $message);
  320. $role = Auth::user()['role'];
  321. switch ($role) {
  322. case 1:
  323. return Redirect::to('objective')->withInput();
  324. case 2:
  325. return Redirect::to('school-objective')->withInput();
  326. case 3:
  327. return Redirect::to('program-objective')->withInput();
  328. }
  329. } else {
  330. // // Check criterion uniqueness
  331. // if(!$this->isCriterionUnique($clean_input, $criterion))
  332. // {
  333. // /** Send error message and old data */
  334. // Session::flash('status', 'danger');
  335. // 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.');
  336. // return Redirect::to('criteria')->withInput();
  337. // }
  338. /** Set info */
  339. $criterion->name = $clean_input['name'];
  340. $criterion->subcriteria = $clean_input['subcriteria'];
  341. $criterion->number_of_scales = $clean_input['number_of_scales'];
  342. $criterion->maximum_score = $clean_input['maximum_score'];
  343. // Set program
  344. if (Input::get('program_id') != 0)
  345. $criterion->program_id = Input::get('program_id');
  346. else
  347. $criterion->program_id = NULL;
  348. // Set status
  349. if (Input::get('status') == 0)
  350. $criterion->deleted_at = date('Y-m-d H:i:s');
  351. else
  352. $criterion->deleted_at = NULL;
  353. if (Input::get('copyright'))
  354. $criterion->copyright = $clean_input['copyright'];
  355. else
  356. $criterion->copyright = NULL;
  357. if (Input::get('notes'))
  358. $criterion->notes = $clean_input['notes'];
  359. else
  360. $criterion->notes = NULL;
  361. /** If criterion is updated, send success message */
  362. if ($criterion->save()) {
  363. $criterionId = $criterion->id;
  364. DB::delete("delete from `criterion_objective_outcome` where `criterion_id` ={$criterionId}");
  365. foreach ($clean_input['outcome_id'] as $outcome_id) {
  366. foreach ($clean_input['objective_id'] as $objective_id) {
  367. DB::insert("insert into `criterion_objective_outcome` (`objective_id`, `outcome_id`, `criterion_id`,`objective_outcome_id`) values ({$objective_id},{$outcome_id}, {$criterionId}, 0)");
  368. }
  369. }
  370. DB::delete("delete from `scales` where id in (select scale_id id from criterion_scale where criterion_id = {$criterionId})");
  371. for ($i = 0; $i < sizeof($clean_input['scale_title']); $i++) {
  372. $scale = new Scale;
  373. $scale->title = $clean_input['scale_title'][$i];
  374. $scale->position = $i + 1;
  375. $scale->description = $clean_input['scale_description'][$i];
  376. if ($scale->save()) {
  377. DB::insert("insert into `criterion_scale` (`criterion_id`, `scale_id`) values({$criterionId},{$scale->id})");
  378. } else {
  379. Session::flash('status', 'danger');
  380. Session::flash('message', '<p>Error creating the Scales</p>');
  381. $role = Auth::user()['role'];
  382. switch ($role) {
  383. case 1:
  384. return Redirect::to('objective')->withInput();
  385. case 2:
  386. return Redirect::to('school-objective')->withInput();
  387. case 3:
  388. return Redirect::to('program-objective')->withInput();
  389. }
  390. }
  391. }
  392. Session::flash('status', 'success');
  393. Session::flash('message', 'Updated criterion: "' . $criterion->name . '"');
  394. $role = Auth::user()['role'];
  395. switch ($role) {
  396. case 1:
  397. return Redirect::to('objective')->withInput();
  398. case 2:
  399. return Redirect::to('school-objective')->withInput();
  400. case 3:
  401. return Redirect::to('program-objective')->withInput();
  402. }
  403. }
  404. /** If saving fails, send error message and old data */
  405. else {
  406. Session::flash('status', 'danger');
  407. Session::flash('message', 'Error updating the Criterion. Please try again later.');
  408. $role = Auth::user()['role'];
  409. switch ($role) {
  410. case 1:
  411. return Redirect::to('objective')->withInput();
  412. case 2:
  413. return Redirect::to('school-objective')->withInput();
  414. case 3:
  415. return Redirect::to('program-objective')->withInput();
  416. }
  417. }
  418. }
  419. }
  420. public function index()
  421. {
  422. // el ID de los semestres que el usuario tiene seleccionado.
  423. $semesters_ids = Session::get('semesters_ids');
  424. // buscar informacion de los semestres seleccionados
  425. $semesters = Semester::whereIn('id', $semesters_ids)->get();
  426. $title = "Learning Outcomes and Criteria";
  427. $outcomes = Outcome::orderBy('name', 'ASC')->get();
  428. // $outcomes = DB::table('outcomes')
  429. // ->orderBy('name', 'asc')
  430. // ->get();
  431. $schools = School::orderBy('name', 'ASC')->get();
  432. // $schools = DB::table('schools')
  433. // ->orderBy('name', 'asc')
  434. // ->get();
  435. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  436. // $criteria = DB::table('criteria')
  437. // ->orderBy('name', 'asc')
  438. // ->get();
  439. // se annadio la nueva variable
  440. return View::make('global.view-learning-outcomes-criteria', compact('title', 'outcomes', 'schools', 'criteria', 'semesters'));
  441. }
  442. // copie index() y lo edite
  443. public function objectivesIndex()
  444. {
  445. if (Auth::user()->role == 1) {
  446. //uscar todos los objetivos
  447. $objectives = DB::table('program_user')
  448. ->join('objectives', 'objectives.program_id', '=', 'program_user.program_id')
  449. ->join('programs', 'programs.id', '=', 'program_user.program_id')
  450. ->select('objectives.id', 'objectives.text', 'programs.name')
  451. ->orderBy('objectives.text', 'asc')
  452. ->get();
  453. } elseif (Auth::user()->role == 2) {
  454. //buscar los objetivos de la departamento (school)
  455. $objectives = DB::table('program_user')
  456. ->join('objectives', 'objectives.program_id', '=', 'program_user.program_id')
  457. ->join('programs', 'programs.id', '=', 'program_user.program_id')
  458. ->where('programs.school_id', Auth::user()->school_id)
  459. ->select('objectives.id', 'objectives.text', 'programs.name')
  460. ->orderBy('objectives.text', 'asc')
  461. ->get();
  462. } elseif ((Auth::user()->role == 3) || (Auth::user()->role == 4)) {
  463. //buscar los objetivos de los programas cuales el profesor esta
  464. $objectives = DB::table('program_user')
  465. ->join('objectives', 'objectives.program_id', '=', 'program_user.program_id')
  466. ->join('programs', 'programs.id', '=', 'program_user.program_id')
  467. ->where('program_user.user_id', Auth::user()->id)
  468. ->select('objectives.id', 'objectives.text', 'programs.name')
  469. ->orderBy('objectives.text', 'asc')
  470. ->get();
  471. }
  472. $title = "Learning Objectives and Criteria";
  473. return View::make('global.view-objectives-criteria', compact('title', 'objectives'));
  474. }
  475. public function destroy()
  476. {
  477. $criterion = Criterion::withTrashed()->find(Input::get('id'));
  478. if (!$criterion->trashed()) {
  479. try {
  480. $criterion->delete();
  481. Session::flash('status', 'success');
  482. Session::flash('message', 'Deactivated criterion: "' . $criterion->name . '"');
  483. } catch (Exception $e) {
  484. Session::flash('status', 'danger');
  485. Session::flash('message', 'Error deactivating criterion."' . $criterion->name . '"');
  486. }
  487. $role = Auth::user()['role'];
  488. switch ($role) {
  489. case 1:
  490. return Redirect::to('objective')->withInput();
  491. case 2:
  492. return Redirect::to('school-objective')->withInput();
  493. case 3:
  494. return Redirect::to('program-objective')->withInput();
  495. }
  496. } else {
  497. try {
  498. $criterion->restore();
  499. Session::flash('status', 'success');
  500. Session::flash('message', 'Reactivated criterion: "' . $criterion->name . '"');
  501. } catch (Exception $e) {
  502. Session::flash('status', 'danger');
  503. Session::flash('message', 'Error reactivating criterion: "' . $criterion->name . '".');
  504. }
  505. $role = Auth::user()['role'];
  506. switch ($role) {
  507. case 1:
  508. return Redirect::to('objective')->withInput();
  509. case 2:
  510. return Redirect::to('school-objective')->withInput();
  511. case 3:
  512. return Redirect::to('program-objective')->withInput();
  513. }
  514. }
  515. }
  516. public function filterCriteria()
  517. {
  518. switch (Input::get('filter')) {
  519. case 'all':
  520. return Criteria::all();
  521. break;
  522. case 'school':
  523. // If scoord
  524. if (Auth::user()->role == '2') {
  525. // Fetch all the programs whose school is the user's
  526. $program_ids = DB::table('programs')->where('school_id', Auth::user()->school_id)->lists('id');
  527. // Return all criteria belonging to any of those programs
  528. return Criterion::whereIn('program_id', $program_ids)
  529. ->orderBy('name', 'ASC')
  530. ->get();
  531. }
  532. // If pcoord
  533. else {
  534. // Fetch all the programs from the user's school;
  535. $program_ids = DB::table('programs')->where('school_id', Auth::user()->programs[0]->school->id)->lists('id');
  536. return Criterion::whereIn('program_id', $program_ids)
  537. ->orderBy('name', 'ASC')
  538. ->get();
  539. }
  540. break;
  541. case 'program':
  542. return Criterion::whereIn('program_id', Auth::user()->programs->lists('id'))
  543. ->orderBy('name', 'ASC')
  544. ->get();
  545. break;
  546. default:
  547. return Criteria::all();
  548. break;
  549. }
  550. }
  551. }