Aucune description

CriteriaController.php 45KB

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