Nessuna descrizione

CriteriaController.php 43KB

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