暫無描述

CriteriaController.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 fetchCriterion()
  12. {
  13. return Criterion::find(Input::get('id'));
  14. }
  15. public function fetchObjectivesForSelect()
  16. {
  17. //$alloutcome = Input::get('outcome');
  18. //$array = array();
  19. //foreach ($alloutcome as $outcome) {
  20. $outcome = DB::select("select objectives.id, objectives.text from objectives, objective_outcome where objective_outcome.outcome_id =? and objective_outcome.objective_id = objectives.id and objectives.active=1", array(Input::get('id')));
  21. //
  22. return json_encode($outcome);
  23. }
  24. public function fetchCriterionWithTrashed()
  25. {
  26. $json = array();
  27. $json['criteria'] = DB::select(" select * from new_criteria where id = ?", array(Input::get('id')));
  28. $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", array(Input::get('id')));
  29. $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')));
  30. $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 = ?
  31. and outcomes.id = criterion_objective_outcome.outcome_id) and objectives.id = objective_outcome.objective_id ORDER BY outcome_id", array(Input::get('id')));
  32. //foreach ($objectivesAll as $objective) {
  33. // $objectivesArray = (array) $objective;
  34. /// Log::info("Logging an array: " . print_r($objectivesArray, true));
  35. /// $json['objectives_outcome'][strval($objectivesArray['outcome_id'])] = array('id' => $objectivesArray['id'], 'text' => $objectivesArray['text'], 'outcome_id');
  36. //}
  37. return json_encode($json);
  38. }
  39. public function isCriterionUnique($input, $existing_criterion = NULL)
  40. {
  41. // dd($input);
  42. Log::info('isCriterionUnique');
  43. if (Input::get('program_id') != 0)
  44. $program_id = $input['program_id'];
  45. else
  46. $program_id = NULL;
  47. $saved_criterion = Criterion::withTrashed()
  48. ->where('name', '=', $input['name'])
  49. ->where('outcome_id', '=', $input['outcome_id'])
  50. ->where('program_id', '=', $program_id)
  51. // ->where('description12', '=', $input['description12'])
  52. // ->where('description34', '=', $input['description34'])
  53. // ->where('description56', '=', $input['description56'])
  54. // ->where('description78', '=', $input['description78'])
  55. ->first();
  56. if ($saved_criterion)
  57. return false;
  58. else
  59. return true;
  60. }
  61. private function cleanInput()
  62. {
  63. $clean_input = array();
  64. $clean_input['name'] = trim(preg_replace('/\t+/', '', Input::get('name')));
  65. $trimmed = trim(preg_replace('/\t+/', '', Input::get('subcriteria')));
  66. Log::info('trimmed 1 -->' . $trimmed . '<--');
  67. if ($trimmed == '') {
  68. $trimmed = NULL;
  69. } else {
  70. $trimmed = json_encode(preg_split('/\r\n/', $trimmed));
  71. }
  72. Log::info('trimmed 2 -->' . $trimmed . '<--');
  73. $clean_input['subcriteria'] = $trimmed;
  74. $clean_input['outcome_id'] = Input::get('outcome');
  75. $clean_input['objective_id'] = Input::get('objective');
  76. $clean_input['program_id'] = trim(preg_replace('/\t+/', '', Input::get('program_id')));
  77. $clean_input['description12'] = trim(preg_replace('/\t+/', '', Input::get('description12')));
  78. $clean_input['description34'] = trim(preg_replace('/\t+/', '', Input::get('description34')));
  79. $clean_input['description56'] = trim(preg_replace('/\t+/', '', Input::get('description56')));
  80. $clean_input['description78'] = trim(preg_replace('/\t+/', '', Input::get('description78')));
  81. $clean_input['copyright'] = trim(preg_replace('/\t+/', '', Input::get('copyright')));
  82. $clean_input['notes'] = trim(preg_replace('/\t+/', '', Input::get('notes')));
  83. $clean_input['maximum_score'] = 8;
  84. $clean_input['number_of_scales'] = 4;
  85. return $clean_input;
  86. }
  87. private function makeValidator($clean_input)
  88. {
  89. /** Validation rules */
  90. return Validator::make(
  91. array(
  92. 'name' => $clean_input['name'],
  93. 'subcriteria' => $clean_input['subcriteria'],
  94. 'outcome_id' => $clean_input['outcome_id'],
  95. 'description12' => $clean_input['description12'],
  96. 'description34' => $clean_input['description34'],
  97. 'description56' => $clean_input['description56'],
  98. 'description78' => $clean_input['description78'],
  99. 'notes' => $clean_input['notes'],
  100. 'copyright' => $clean_input['copyright'],
  101. ),
  102. array(
  103. 'name' => 'required|string',
  104. 'subcriteria' => 'string',
  105. 'outcome_id' => 'required|array',
  106. 'description12' => 'required|string',
  107. 'description34' => 'required|string',
  108. 'description56' => 'required|string',
  109. 'description78' => 'required|string',
  110. 'notes' => 'string',
  111. 'copyright' => 'string',
  112. ),
  113. array(
  114. 'description12.required' => 'The Beginning (1-2) field is required.',
  115. 'description34.required' => 'The In Progress (3-4) field is required.',
  116. 'description56.required' => 'The Satisfactory (5-6) field is required.',
  117. 'description78.required' => 'The Excellent (7-8) field is required.',
  118. )
  119. );
  120. }
  121. /**
  122. * Create a new criterion.
  123. *
  124. * @return Redirect Redirect back to form page
  125. */
  126. public function create()
  127. {
  128. $clean_input = $this->cleanInput();
  129. /** Validation rules */
  130. $validator = $this->makeValidator($clean_input);
  131. /** If validation fails */
  132. if ($validator->fails()) {
  133. /** Prepare error message */
  134. $message = '<p>Error(s) creating a new Criterion:</p><ul>';
  135. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  136. $message .= $validationError;
  137. }
  138. $message .= '</ul>';
  139. /** Send error message and old data */
  140. Session::flash('status', 'danger');
  141. Session::flash('message', $message);
  142. return Redirect::to('criteria')->withInput();
  143. } else {
  144. // Check criterion uniqueness
  145. /*if (!$this->isCriterionUnique($clean_input)) {
  146. /** Send error message and old data
  147. Session::flash('status', 'danger');
  148. Session::flash('message', 'This criterion is a duplicate of an already saved criterion because its name and associated program are the same.');
  149. return Redirect::to('criteria')->withInput();
  150. }*/
  151. /** Instantiate new criterion */
  152. $criterion = new Criterion;
  153. $criterion->name = $clean_input['name'];
  154. $criterion->subcriteria = $clean_input['subcriteria'];
  155. $criterion->description12 = $clean_input['description12'];
  156. $criterion->description34 = $clean_input['description34'];
  157. $criterion->description56 = $clean_input['description56'];
  158. $criterion->description78 = $clean_input['description78'];
  159. //gabriel añadió aqui
  160. $criterion->objective_id = 1; //$clean_input['objective_id'];
  161. if (Input::get('copyright'))
  162. $criterion->copyright = $clean_input['copyright'];
  163. if (Input::get('notes'))
  164. $criterion->notes = $clean_input['notes'];
  165. // Set program
  166. if (Input::get('program_id') != 0)
  167. $criterion->program_id = $clean_input['program_id'];
  168. else
  169. $criterion->program_id = NULL;
  170. /** If criterion is saved, send success message */
  171. if ($criterion->save()) {
  172. $criterionId = $criterion->id;
  173. foreach ($clean_input['outcome_id'] as $outcome_id) {
  174. foreach ($clean_input['objective_id'] as $objective_id) {
  175. //try{
  176. DB::insert("insert into `criterion_objective_outcome` (`objective_id`, `outcome_id`, `criterion_id`, `objective_outcome_id`) values ({$objective_id},{$outcome_id}, {$criterionId}, 0)");
  177. // }
  178. //catch{
  179. //Session::flash('status', 'danger');
  180. //Session::flash('message', '<p>Error creating objective. Please try again later.</p>');
  181. //return Redirect::to('objective')->withInput();
  182. //}
  183. }
  184. }
  185. Session::flash('status', 'success');
  186. Session::flash('message', 'Criterion created: "' . $criterion->name . '".');
  187. return Redirect::to('criteria')->withInput(Input::only('outcome_id'));
  188. }
  189. /** If saving fails, send error message and old data */
  190. else {
  191. Session::flash('status', 'danger');
  192. Session::flash('message', '<p>Error creating Criterion. Please try again later.</p>');
  193. return Redirect::to('learning-outcomes-criteria')->withInput();
  194. }
  195. }
  196. }
  197. public function edit()
  198. {
  199. $title = "Criteria";
  200. $outcomes = Outcome::orderBy('name', 'ASC')->lists('name', 'id');
  201. $schools = School::orderBy('name', 'ASC')->get();
  202. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  203. $programs = Program::orderBy('name', 'ASC')->get();
  204. $objectives = DB::table('objectives')->orderBy('text', 'ASC')->lists('text', 'id');
  205. return View::make('local.managers.admins.criteria', compact('title', 'outcomes', 'schools', 'criteria', 'programs', 'objectives'));
  206. }
  207. private function cleanInputEdit()
  208. {
  209. $clean_input = array();
  210. $clean_input['name'] = trim(preg_replace('/\t+/', '', Input::get('name')));
  211. $trimmed = trim(preg_replace('/\t+/', '', Input::get('subcriteria')));
  212. Log::info('trimmed 1 -->' . $trimmed . '<--');
  213. if ($trimmed == '') {
  214. $trimmed = NULL;
  215. } else {
  216. $trimmed = json_encode(preg_split('/\r\n/', $trimmed));
  217. }
  218. Log::info('trimmed 2 -->' . $trimmed . '<--');
  219. $clean_input['subcriteria'] = $trimmed;
  220. $clean_input['outcome_id'] = Input::get('assoc_outcome');
  221. $clean_input['objective_id'] = Input::get('assoc_objective');
  222. $clean_input['program_id'] = trim(preg_replace('/\t+/', '', Input::get('program_id')));
  223. $clean_input['description12'] = trim(preg_replace('/\t+/', '', Input::get('description12')));
  224. $clean_input['description34'] = trim(preg_replace('/\t+/', '', Input::get('description34')));
  225. $clean_input['description56'] = trim(preg_replace('/\t+/', '', Input::get('description56')));
  226. $clean_input['description78'] = trim(preg_replace('/\t+/', '', Input::get('description78')));
  227. $clean_input['copyright'] = trim(preg_replace('/\t+/', '', Input::get('copyright')));
  228. $clean_input['notes'] = trim(preg_replace('/\t+/', '', Input::get('notes')));
  229. $clean_input['maximum_score'] = 8;
  230. $clean_input['number_of_scales'] = 4;
  231. return $clean_input;
  232. }
  233. public function update()
  234. {
  235. $criterion = Criterion::withTrashed()->find(Input::get('id'));
  236. $clean_input = $this->cleanInputEdit();
  237. /** Validation rules */
  238. $validator = $this->makeValidator($clean_input);
  239. /** If validation fails */
  240. if ($validator->fails()) {
  241. /** Prepare error message */
  242. $message = 'Error(s) updating the Criterion: <ul>';
  243. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  244. $message .= $validationError;
  245. }
  246. $message .= '</ul>';
  247. /** Send error message and old data */
  248. Session::flash('status', 'danger');
  249. Session::flash('message', $message);
  250. return Redirect::back()->withInput();
  251. } else {
  252. // // Check criterion uniqueness
  253. // if(!$this->isCriterionUnique($clean_input, $criterion))
  254. // {
  255. // /** Send error message and old data */
  256. // Session::flash('status', 'danger');
  257. // 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.');
  258. // return Redirect::to('criteria')->withInput();
  259. // }
  260. /** Set info */
  261. $criterion->name = $clean_input['name'];
  262. $criterion->subcriteria = $clean_input['subcriteria'];
  263. //$criterion->outcome_id = $clean_input['outcome_id'];
  264. $criterion->description12 = $clean_input['description12'];
  265. $criterion->description34 = $clean_input['description34'];
  266. $criterion->description56 = $clean_input['description56'];
  267. $criterion->description78 = $clean_input['description78'];
  268. // Set program
  269. if (Input::get('program_id') != 0)
  270. $criterion->program_id = Input::get('program_id');
  271. else
  272. $criterion->program_id = NULL;
  273. // Set status
  274. if (Input::get('status') == 0)
  275. $criterion->deleted_at = date('Y-m-d H:i:s');
  276. else
  277. $criterion->deleted_at = NULL;
  278. if (Input::get('copyright'))
  279. $criterion->copyright = $clean_input['copyright'];
  280. else
  281. $criterion->copyright = NULL;
  282. if (Input::get('notes'))
  283. $criterion->notes = $clean_input['notes'];
  284. else
  285. $criterion->notes = NULL;
  286. /** If criterion is updated, send success message */
  287. if ($criterion->save()) {
  288. $criterionId = $criterion->id;
  289. DB::delete("delete from `criterion_objective_outcome` where `criterion_id` ={$criterionId}");
  290. foreach ($clean_input['outcome_id'] as $outcome_id) {
  291. foreach ($clean_input['objective_id'] as $objective_id) {
  292. DB::insert("insert into `criterion_objective_outcome` (`objective_id`, `outcome_id`, `criterion_id`,`objective_outcome_id`) values ({$objective_id},{$outcome_id}, {$criterionId}, 0)");
  293. }
  294. }
  295. Session::flash('status', 'success');
  296. Session::flash('message', 'Updated criterion: "' . $criterion->name . '"');
  297. return Redirect::back();
  298. }
  299. /** If saving fails, send error message and old data */
  300. else {
  301. Session::flash('status', 'danger');
  302. Session::flash('message', 'Error updating the Criterion. Please try again later.');
  303. return Redirect::back()->withInput();
  304. }
  305. }
  306. }
  307. public function index()
  308. {
  309. $title = "Learning Outcomes and Criteria";
  310. $outcomes = Outcome::orderBy('name', 'ASC')->get();
  311. $schools = School::orderBy('name', 'ASC')->get();
  312. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  313. return View::make('global.view-learning-outcomes-criteria', compact('title', 'outcomes', 'schools', 'criteria'));
  314. }
  315. public function destroy()
  316. {
  317. $criterion = Criterion::withTrashed()->find(Input::get('id'));
  318. if (!$criterion->trashed()) {
  319. try {
  320. $criterion->delete();
  321. Session::flash('status', 'success');
  322. Session::flash('message', 'Deactivated criterion: "' . $criterion->name . '"');
  323. } catch (Exception $e) {
  324. Session::flash('status', 'danger');
  325. Session::flash('message', 'Error deactivating criterion."' . $criterion->name . '"');
  326. }
  327. return Redirect::back();
  328. } else {
  329. try {
  330. $criterion->restore();
  331. Session::flash('status', 'success');
  332. Session::flash('message', 'Reactivated criterion: "' . $criterion->name . '"');
  333. } catch (Exception $e) {
  334. Session::flash('status', 'danger');
  335. Session::flash('message', 'Error reactivating criterion: "' . $criterion->name . '".');
  336. }
  337. return Redirect::back();
  338. }
  339. }
  340. public function filterCriteria()
  341. {
  342. switch (Input::get('filter')) {
  343. case 'all':
  344. return Criteria::all();
  345. break;
  346. case 'school':
  347. // If scoord
  348. if (Auth::user()->role == '2') {
  349. // Fetch all the programs whose school is the user's
  350. $program_ids = DB::table('programs')->where('school_id', Auth::user()->school_id)->lists('id');
  351. // Return all criteria belonging to any of those programs
  352. return Criterion::whereIn('program_id', $program_ids)
  353. ->orderBy('name', 'ASC')
  354. ->get();
  355. }
  356. // If pcoord
  357. else {
  358. // Fetch all the programs from the user's school;
  359. $program_ids = DB::table('programs')->where('school_id', Auth::user()->programs[0]->school->id)->lists('id');
  360. return Criterion::whereIn('program_id', $program_ids)
  361. ->orderBy('name', 'ASC')
  362. ->get();
  363. }
  364. break;
  365. case 'program':
  366. return Criterion::whereIn('program_id', Auth::user()->programs->lists('id'))
  367. ->orderBy('name', 'ASC')
  368. ->get();
  369. break;
  370. default:
  371. return Criteria::all();
  372. break;
  373. }
  374. }
  375. }