Nessuna descrizione

CriteriaController.php 47KB

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