Nenhuma descrição

CriteriaController.php 33KB

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