暂无描述

CriteriaController.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. // el ID de los semestres que el usuario tiene seleccionado.
  251. $semesters_ids = Session::get('semesters_ids');
  252. // buscar informacion de los semestres seleccionados
  253. $semesters = Semester::whereIn('id',$semesters_ids)->get();
  254. $title = "Learning Outcomes and Criteria";
  255. $outcomes = Outcome::orderBy('name', 'ASC')->get();
  256. $schools = School::orderBy('name', 'ASC')->get();
  257. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  258. // se annadio la nueva variable
  259. return View::make('global.view-learning-outcomes-criteria', compact('title', 'outcomes', 'schools', 'criteria', 'semesters'));
  260. }
  261. // copie index() y lo edite
  262. public function objectivesIndex()
  263. {
  264. if(Auth::user()->role==1)
  265. {
  266. //uscar todos los objetivos
  267. $objectives = DB::table('program_user')
  268. ->join('objectives','objectives.program_id','=','program_user.program_id')
  269. ->join('programs','programs.id','=','program_user.program_id')
  270. ->select('objectives.id','objectives.text','programs.name')
  271. ->orderBy('objectives.text', 'asc')
  272. ->get();
  273. }
  274. elseif(Auth::user()->role==2)
  275. {
  276. //buscar los objetivos de la departamento (school)
  277. $objectives = DB::table('program_user')
  278. ->join('objectives','objectives.program_id','=','program_user.program_id')
  279. ->join('programs','programs.id','=','program_user.program_id')
  280. ->where('programs.school_id',Auth::user()->school_id)
  281. ->select('objectives.id','objectives.text','programs.name')
  282. ->orderBy('objectives.text', 'asc')
  283. ->get();
  284. }
  285. elseif((Auth::user()->role==3) || (Auth::user()->role==4))
  286. {
  287. //buscar los objetivos de los programas cuales el profesor esta
  288. $objectives = DB::table('program_user')
  289. ->join('objectives','objectives.program_id','=','program_user.program_id')
  290. ->join('programs','programs.id','=','program_user.program_id')
  291. ->where('program_user.user_id',Auth::user()->id)
  292. ->select('objectives.id','objectives.text','programs.name')
  293. ->orderBy('objectives.text', 'asc')
  294. ->get();
  295. }
  296. $title = "Learning Objectives and Criteria";
  297. return View::make('global.view-objectives-criteria', compact('title', 'objectives'));
  298. }
  299. public function destroy()
  300. {
  301. $criterion = Criterion::withTrashed()->find(Input::get('id'));
  302. if(!$criterion->trashed())
  303. {
  304. try
  305. {
  306. $criterion->delete();
  307. Session::flash('status', 'success');
  308. Session::flash('message', 'Deactivated criterion: "'.$criterion->name.'"');
  309. }
  310. catch (Exception $e)
  311. {
  312. Session::flash('status', 'danger');
  313. Session::flash('message', 'Error deactivating criterion."'.$criterion->name.'"');
  314. }
  315. return Redirect::back();
  316. }
  317. else
  318. {
  319. try
  320. {
  321. $criterion->restore();
  322. Session::flash('status', 'success');
  323. Session::flash('message', 'Reactivated criterion: "'.$criterion->name.'"');
  324. }
  325. catch (Exception $e)
  326. {
  327. Session::flash('status', 'danger');
  328. Session::flash('message', 'Error reactivating criterion: "'.$criterion->name.'".');
  329. }
  330. return Redirect::back();
  331. }
  332. }
  333. public function filterCriteria()
  334. {
  335. switch (Input::get('filter'))
  336. {
  337. case 'all':
  338. return Criteria::all();
  339. break;
  340. case 'school':
  341. // If scoord
  342. if(Auth::user()->role == '2')
  343. {
  344. // Fetch all the programs whose school is the user's
  345. $program_ids = DB::table('programs')->where('school_id', Auth::user()->school_id)->lists('id');
  346. // Return all criteria belonging to any of those programs
  347. return Criterion::
  348. whereIn('program_id', $program_ids)
  349. ->orderBy('name', 'ASC')
  350. ->get();
  351. }
  352. // If pcoord
  353. else
  354. {
  355. // Fetch all the programs from the user's school;
  356. $program_ids = DB::table('programs')->where('school_id', Auth::user()->programs[0]->school->id)->lists('id');
  357. return Criterion::
  358. whereIn('program_id', $program_ids)
  359. ->orderBy('name', 'ASC')
  360. ->get();
  361. }
  362. break;
  363. case 'program':
  364. return Criterion::
  365. whereIn('program_id', Auth::user()->programs->lists('id'))
  366. ->orderBy('name', 'ASC')
  367. ->get();
  368. break;
  369. default:
  370. return Criteria::all();
  371. break;
  372. }
  373. }
  374. }