No Description

CriteriaController.php 28KB

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