Bez popisu

CriteriaController.php 34KB

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