No Description

CriteriaController.php 29KB

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