説明なし

CriteriaController.php 43KB

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