No Description

CriteriaController.php 32KB

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