No Description

CriteriaController.php 28KB

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