No Description

CriteriaController.php 28KB

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