Nav apraksta

CriteriaController.php 41KB

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