Açıklama Yok

CriteriaController.php 14KB

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