Ingen beskrivning

CriteriaController.php 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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_title']);
  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_title']); $i++) {
  239. $scale = new Scale;
  240. $scale->title = $clean_input['scale_title'][$i];
  241. $scale->position = $i + 1;
  242. $scale->description = $clean_input['scale_description'][$i];
  243. $scale->min_score = $clean_input['min_score'][$i];
  244. $scale->max_score = $clean_input['max_score'][$i];
  245. if ($scale->save()) {
  246. DB::insert("insert into `criterion_scale` (`criterion_id`, `scale_id`) values({$criterionId},{$scale->id})");
  247. } else {
  248. Session::flash('status', 'danger');
  249. Session::flash('message', '<p>Error creating the Scales</p>');
  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. }*/
  261. foreach ($clean_input['program_id'] as $program_id) {
  262. DB::insert("insert into `program_criterion` (`criterion_id`, `program_id`) values({$criterionId},{$program_id})");
  263. }
  264. Session::flash('status', 'success');
  265. Session::flash('message', 'Criterion created: "' . $criterion->name . '".');
  266. $role = Auth::user()['role'];
  267. switch ($role) {
  268. case 1:
  269. return Redirect::to('criteria')->withInput();
  270. case 2:
  271. return Redirect::to('school-criteria')->withInput();
  272. case 3:
  273. return Redirect::to('program-criteria')->withInput();
  274. }
  275. }
  276. /** If saving fails, send error message and old data */
  277. else {
  278. Session::flash('status', 'danger');
  279. Session::flash('message', '<p>Error creating Criterion. Please try again later.</p>');
  280. $role = Auth::user()['role'];
  281. switch ($role) {
  282. case 1:
  283. return Redirect::to('criteria')->withInput();
  284. case 2:
  285. return Redirect::to('school-criteria')->withInput();
  286. case 3:
  287. return Redirect::to('program-criteria')->withInput();
  288. }
  289. }
  290. }
  291. }
  292. public function edit()
  293. {
  294. $title = "Criteria";
  295. $outcomes = Outcome::where("deactivation_date", '=', null)->orderBy('name', 'ASC')->lists('name', 'id');
  296. $schools = School::orderBy('name', 'ASC')->get();
  297. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  298. $programs = Program::orderBy('name', 'ASC')->get();
  299. $objectives = DB::table('objectives')->orderBy('text', 'ASC')->lists('text', 'id');
  300. return View::make('local.managers.admins.criteria', compact('title', 'outcomes', 'schools', 'criteria', 'programs', 'objectives'));
  301. }
  302. private function cleanInputEdit()
  303. {
  304. $clean_input = array();
  305. $clean_input['name'] = trim(preg_replace('/\t+/', '', Input::get('name')));
  306. $trimmed = trim(preg_replace('/\t+/', '', Input::get('subcriteria')));
  307. Log::info('trimmed 1 -->' . $trimmed . '<--');
  308. if ($trimmed == '') {
  309. $trimmed = NULL;
  310. } else {
  311. $trimmed = json_encode(preg_split('/\r\n/', $trimmed));
  312. }
  313. Log::info('trimmed 2 -->' . $trimmed . '<--');
  314. $clean_input['subcriteria'] = $trimmed;
  315. $clean_input['outcome_id'] = Input::get('assoc_outcome');
  316. $clean_input['objective_id'] = Input::get('assoc_objective');
  317. $clean_input['program_id'] = Input::get('program_id');
  318. $clean_input['copyright'] = trim(preg_replace('/\t+/', '', Input::get('copyright')));
  319. $clean_input['notes'] = trim(preg_replace('/\t+/', '', Input::get('notes')));
  320. /* $clean_input['maximum_score'] = (int) Input::get('assoc_maximum_score');
  321. $clean_input['scale_title'] = Input::get('assoc_title');
  322. $clean_input['scale_description'] = Input::get('assoc_scales');
  323. $clean_input['number_of_scales'] = sizeof($clean_input['scale_title']);*/
  324. return $clean_input;
  325. }
  326. public function update()
  327. {
  328. $criterion = Criterion::withTrashed()->find(Input::get('id'));
  329. $clean_input = $this->cleanInputEdit();
  330. /** Validation rules */
  331. $validator = $this->makeValidator($clean_input);
  332. /** If validation fails */
  333. if ($validator->fails()) {
  334. /** Prepare error message */
  335. $message = 'Error(s) updating the Criterion: <ul>';
  336. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  337. $message .= $validationError;
  338. }
  339. $message .= '</ul>';
  340. /** Send error message and old data */
  341. Session::flash('status', 'danger');
  342. Session::flash('message', $message);
  343. $role = Auth::user()['role'];
  344. switch ($role) {
  345. case 1:
  346. return Redirect::to('objective')->withInput();
  347. case 2:
  348. return Redirect::to('school-objective')->withInput();
  349. case 3:
  350. return Redirect::to('program-objective')->withInput();
  351. }
  352. } else {
  353. // // Check criterion uniqueness
  354. // if(!$this->isCriterionUnique($clean_input, $criterion))
  355. // {
  356. // /** Send error message and old data */
  357. // Session::flash('status', 'danger');
  358. // 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.');
  359. // return Redirect::to('criteria')->withInput();
  360. // }
  361. /** Set info */
  362. $criterion->name = $clean_input['name'];
  363. $criterion->subcriteria = $clean_input['subcriteria'];
  364. // Set program
  365. /*
  366. if (Input::get('program_id') != 0)
  367. $criterion->program_id = Input::get('program_id');
  368. else
  369. $criterion->program_id = NULL;*/
  370. // Set status
  371. if (Input::get('status') == 0)
  372. $criterion->deleted_at = date('Y-m-d H:i:s');
  373. else
  374. $criterion->deleted_at = NULL;
  375. if (Input::get('copyright'))
  376. $criterion->copyright = $clean_input['copyright'];
  377. else
  378. $criterion->copyright = NULL;
  379. if (Input::get('notes'))
  380. $criterion->notes = $clean_input['notes'];
  381. else
  382. $criterion->notes = NULL;
  383. /** If criterion is updated, send success message */
  384. if ($criterion->save()) {
  385. $criterionId = $criterion->id;
  386. DB::delete("delete from `criterion_objective_outcome` where `criterion_id` ={$criterionId}");
  387. DB::delete("delete from `program_criterion` where `criterion_id` ={$criterionId}");
  388. foreach ($clean_input['outcome_id'] as $outcome_id) {
  389. foreach ($clean_input['objective_id'] as $objective_id) {
  390. DB::insert("insert into `criterion_objective_outcome` (`objective_id`, `outcome_id`, `criterion_id`) values ({$objective_id},{$outcome_id}, {$criterionId})");
  391. }
  392. }
  393. /*DB::delete("delete from `scales` where id in (select scale_id id from criterion_scale where criterion_id = {$criterionId})");
  394. for ($i = 0; $i < sizeof($clean_input['scale_title']); $i++) {
  395. $scale = new Scale;
  396. $scale->title = $clean_input['scale_title'][$i];
  397. $scale->position = $i + 1;
  398. $scale->description = $clean_input['scale_description'][$i];
  399. if ($scale->save()) {
  400. DB::insert("insert into `criterion_scale` (`criterion_id`, `scale_id`) values({$criterionId},{$scale->id})");
  401. } else {
  402. Session::flash('status', 'danger');
  403. Session::flash('message', '<p>Error creating the Scales</p>');
  404. $role = Auth::user()['role'];
  405. switch ($role) {
  406. case 1:
  407. return Redirect::to('objective')->withInput();
  408. case 2:
  409. return Redirect::to('school-objective')->withInput();
  410. case 3:
  411. return Redirect::to('program-objective')->withInput();
  412. }
  413. }
  414. }*/
  415. foreach ($clean_input['program_id'] as $program_id) {
  416. DB::insert("insert into `program_criterion` (`criterion_id`, `program_id`) values({$criterionId},{$program_id})");
  417. }
  418. Session::flash('status', 'success');
  419. Session::flash('message', 'Updated criterion: "' . $criterion->name . '"');
  420. $role = Auth::user()['role'];
  421. switch ($role) {
  422. case 1:
  423. return Redirect::to('criteria')->withInput();
  424. case 2:
  425. return Redirect::to('school-criteria')->withInput();
  426. case 3:
  427. return Redirect::to('program-criteria')->withInput();
  428. }
  429. }
  430. /** If saving fails, send error message and old data */
  431. else {
  432. Session::flash('status', 'danger');
  433. Session::flash('message', 'Error updating the Criterion. Please try again later.');
  434. $role = Auth::user()['role'];
  435. switch ($role) {
  436. case 1:
  437. return Redirect::to('criteria')->withInput();
  438. case 2:
  439. return Redirect::to('school-criteria')->withInput();
  440. case 3:
  441. return Redirect::to('program-criteria')->withInput();
  442. }
  443. }
  444. }
  445. }
  446. public function index()
  447. {
  448. // el ID de los semestres que el usuario tiene seleccionado.
  449. $semesters_ids = Session::get('semesters_ids');
  450. // buscar informacion de los semestres seleccionados
  451. $semesters = Semester::whereIn('id', $semesters_ids)->get();
  452. $title = "Learning Outcomes and Criteria";
  453. $outcomes = Outcome::orderBy('name', 'ASC')->get();
  454. // $outcomes = DB::table('outcomes')
  455. // ->orderBy('name', 'asc')
  456. // ->get();
  457. $schools = School::orderBy('name', 'ASC')->get();
  458. // $schools = DB::table('schools')
  459. // ->orderBy('name', 'asc')
  460. // ->get();
  461. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  462. // $criteria = DB::table('criteria')
  463. // ->orderBy('name', 'asc')
  464. // ->get();
  465. // se annadio la nueva variable
  466. return View::make('global.view-learning-outcomes-criteria', compact('title', 'outcomes', 'schools', 'criteria', 'semesters'));
  467. }
  468. // copie index() y lo edite
  469. public function objectivesIndex()
  470. {
  471. if (Auth::user()->role == 1) {
  472. //buscar todos los objetivos
  473. $objectives = DB::table('program_user')
  474. ->join('objective_program', 'objective_program.program_id', '=', 'program_user.program_id')
  475. ->join('objectives', 'objectives.id', '=', 'objective_program.objective_id')
  476. ->join('programs', 'programs.id', '=', 'program_user.program_id')
  477. ->select('objectives.id', 'objectives.text', 'programs.name')
  478. ->orderBy('objectives.text', 'asc')
  479. ->get();
  480. } elseif (Auth::user()->role == 2) {
  481. //buscar los objetivos de la departamento (school)
  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('programs.school_id', Auth::user()->school_id)
  486. ->select('objectives.id', 'objectives.text', 'programs.name')
  487. ->orderBy('objectives.text', 'asc')
  488. ->get();
  489. } elseif ((Auth::user()->role == 3) || (Auth::user()->role == 4)) {
  490. //buscar los objetivos de los programas cuales el profesor esta
  491. $objectives = DB::table('program_user')
  492. ->join('objectives', 'objectives.program_id', '=', 'program_user.program_id')
  493. ->join('programs', 'programs.id', '=', 'program_user.program_id')
  494. ->where('program_user.user_id', Auth::user()->id)
  495. ->select('objectives.id', 'objectives.text', 'programs.name')
  496. ->orderBy('objectives.text', 'asc')
  497. ->get();
  498. }
  499. $title = "Learning Objectives and Criteria";
  500. return View::make('global.view-objectives-criteria', compact('title', 'objectives'));
  501. }
  502. public function destroy()
  503. {
  504. $criterion = Criterion::withTrashed()->find(Input::get('id'));
  505. if (!$criterion->trashed()) {
  506. try {
  507. $criterion->delete();
  508. Session::flash('status', 'success');
  509. Session::flash('message', 'Deactivated criterion: "' . $criterion->name . '"');
  510. } catch (Exception $e) {
  511. Session::flash('status', 'danger');
  512. Session::flash('message', 'Error deactivating criterion."' . $criterion->name . '"');
  513. }
  514. $role = Auth::user()['role'];
  515. switch ($role) {
  516. case 1:
  517. return Redirect::to('objective')->withInput();
  518. case 2:
  519. return Redirect::to('school-objective')->withInput();
  520. case 3:
  521. return Redirect::to('program-objective')->withInput();
  522. }
  523. } else {
  524. try {
  525. $criterion->restore();
  526. Session::flash('status', 'success');
  527. Session::flash('message', 'Reactivated criterion: "' . $criterion->name . '"');
  528. } catch (Exception $e) {
  529. Session::flash('status', 'danger');
  530. Session::flash('message', 'Error reactivating criterion: "' . $criterion->name . '".');
  531. }
  532. $role = Auth::user()['role'];
  533. switch ($role) {
  534. case 1:
  535. return Redirect::to('objective')->withInput();
  536. case 2:
  537. return Redirect::to('school-objective')->withInput();
  538. case 3:
  539. return Redirect::to('program-objective')->withInput();
  540. }
  541. }
  542. }
  543. public function filterCriteria()
  544. {
  545. switch (Input::get('filter')) {
  546. case 'all':
  547. return Criteria::all();
  548. break;
  549. case 'school':
  550. // If scoord
  551. if (Auth::user()->role == '2') {
  552. // Fetch all the programs whose school is the user's
  553. $program_ids = DB::table('programs')->where('school_id', Auth::user()->school_id)->lists('id');
  554. // Return all criteria belonging to any of those programs
  555. return Criterion::whereIn('program_id', $program_ids)
  556. ->orderBy('name', 'ASC')
  557. ->get();
  558. }
  559. // If pcoord
  560. else {
  561. // Fetch all the programs from the user's school;
  562. $program_ids = DB::table('programs')->where('school_id', Auth::user()->programs[0]->school->id)->lists('id');
  563. return Criterion::whereIn('program_id', $program_ids)
  564. ->orderBy('name', 'ASC')
  565. ->get();
  566. }
  567. break;
  568. case 'program':
  569. return Criterion::whereIn('program_id', Auth::user()->programs->lists('id'))
  570. ->orderBy('name', 'ASC')
  571. ->get();
  572. break;
  573. default:
  574. return Criteria::all();
  575. break;
  576. }
  577. }
  578. }