暫無描述

CriteriaController.php 46KB

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