Нет описания

CriteriaController.php 42KB

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