説明なし

CriteriaController.php 36KB

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