Sin descripción

CriteriaController.php 44KB

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