Geen omschrijving

CriteriaController.php 35KB

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