Ei kuvausta

CriteriaController.php 29KB

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