Без опису

TransformativeActionsController.php 44KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. <?php
  2. use Illuminate\Support\Facades\Input;
  3. class TransformativeActionsController extends \BaseController
  4. {
  5. public function deleteTA()
  6. {
  7. $ta_id = array(Input::get('ta_id'));
  8. //creo que este proximo if no funciona
  9. // aunque el usuario actualmente no tiene manera de enviar id 0.
  10. // si envia id 0, el backend se queja en la linea: $ta = $ta[0];
  11. if($ta_id == 0){
  12. $message = 'Please select a Transformative Action</p>';
  13. Session::flash('status', 'danger');
  14. Session::flash('message', $message);
  15. $role = Auth::user()['role'];
  16. return Redirect::to('transformativeAction')->withInput();
  17. }
  18. dd($ta_id);
  19. $ta = DB::table('transformative_actions')
  20. ->where('transformative_actions.is_custom', 1)
  21. ->where('id',$ta_id)
  22. ->get();
  23. $ta = $ta[0];
  24. $used = DB::table('annual_plan_transformative')
  25. ->where('id',$ta_id)
  26. ->get();
  27. $recommended = $ta->by_professor;
  28. // the TA can only be deleted if error if the TA is not currently as "Recommended"
  29. // and isnt used in an anual plan
  30. if ($recommended == 1 && count($used) == 0) {
  31. // delete the TA if it qualifies
  32. DB::delete("delete from transformative_actions where id = ?", $ta_id);
  33. $name = $ta->at_text;
  34. $message = 'The Transformative Action has been deleted:</p><ul>';
  35. $message .= '<li> '.$name.' </li>';
  36. $message .= '</ul>';
  37. Session::flash('status', 'success');
  38. Session::flash('message', $message);
  39. $role = Auth::user()['role'];
  40. return Redirect::to('transformativeAction')->withInput();
  41. }
  42. $message = 'Transformative Actions can only be deleted if:</p><ul>';
  43. $message .= '<li> It has a status of "Recommended"</li>';
  44. $message .= '<li> It is not currently used in a plan</li>';
  45. $message .= '</ul>';
  46. Session::flash('status', 'danger');
  47. Session::flash('message', $message);
  48. $role = Auth::user()['role'];
  49. return Redirect::to('transformativeAction')->withInput();
  50. }
  51. private function cleanInput()
  52. {
  53. $clean_input = array();
  54. $clean_input['name'] = trim(preg_replace('/\t+/', '', Input::get('name')));
  55. $trimmed = trim(preg_replace('/\t+/', '', Input::get('subcriteria')));
  56. Log::info('trimmed 1 -->' . $trimmed . '<--');
  57. if ($trimmed == '') {
  58. $trimmed = NULL;
  59. } else {
  60. $trimmed = json_encode(preg_split('/\r\n/', $trimmed));
  61. }
  62. Log::info('trimmed 2 -->' . $trimmed . '<--');
  63. $clean_input['subcriteria'] = $trimmed;
  64. $clean_input['outcome_id'] = Input::get('outcome');
  65. $clean_input['objective_id'] = Input::get('objective');
  66. $clean_input['program_id'] = Input::get('program_id');
  67. $clean_input['copyright'] = trim(preg_replace('/\t+/', '', Input::get('copyright')));
  68. $clean_input['notes'] = trim(preg_replace('/\t+/', '', Input::get('notes')));
  69. $clean_input['maximum_score'] = (int) Input::get('maximum_score');
  70. $clean_input['scale_title'] = Input::get('title');
  71. $clean_input['scale_description'] = Input::get('Scales');
  72. $clean_input['min_score'] = Input::get('min');
  73. $clean_input['max_score'] = Input::get('max');
  74. $clean_input['number_of_scales'] = sizeof($clean_input['scale_title']);
  75. return $clean_input;
  76. }
  77. private function makeValidator($clean_input)
  78. {
  79. /** Validation rules */
  80. return Validator::make(
  81. array(
  82. 'name' => $clean_input['name'],
  83. 'subcriteria' => $clean_input['subcriteria'],
  84. 'outcome_id' => $clean_input['outcome_id'],
  85. 'notes' => $clean_input['notes'],
  86. 'copyright' => $clean_input['copyright'],
  87. 'maximum_score' => $clean_input['maximum_score']
  88. ),
  89. array(
  90. 'name' => 'required|string',
  91. 'subcriteria' => 'string',
  92. 'outcome_id' => 'required|array',
  93. 'notes' => 'string',
  94. 'copyright' => 'string',
  95. 'maximum_score' => 'required|integer'
  96. )
  97. );
  98. }
  99. /**
  100. * Create a new transformative action.
  101. *
  102. * @return Redirect Redirect back to form page
  103. */
  104. public function createTA()
  105. {
  106. $clean_input = array();
  107. $clean_input['text'] = Input::get('createText');
  108. $clean_input['description'] = Input::get('createDescription');
  109. $clean_input['objectiveid'] = Input::get('objectiveid');
  110. $clean_input['courseid'] = Input::get('courseid');
  111. // $clean_input['approval'] = Input::get('approval');
  112. /** Validation rules */
  113. $validator = Validator::make(
  114. array(
  115. 'text' => $clean_input['text'],
  116. 'description' => $clean_input['description'],
  117. 'objectiveid' => $clean_input['objectiveid'],
  118. 'courseid' => $clean_input['courseid'],
  119. // 'approval' => $clean_input['approval'],
  120. ),
  121. array(
  122. 'text' => 'required|string',
  123. 'description' => 'required|string',
  124. // 'subcriteria' => 'string',
  125. 'objectiveid' => 'required|array',
  126. 'courseid' => 'required|array',
  127. // 'approval' => 'required|integer',
  128. // 'notes' => 'string',
  129. // 'copyright' => 'string',
  130. // 'maximum_score' => 'required|integer'
  131. )
  132. );
  133. /** If validation fails */
  134. if ($validator->fails()) {
  135. /** Prepare error message */
  136. $message = '<p>Error(s) creating a new Transformative Action:</p><ul>';
  137. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  138. $message .= $validationError;
  139. }
  140. $message .= '</ul>';
  141. /** Send error message and old data */
  142. Session::flash('status', 'danger');
  143. Session::flash('message', $message);
  144. $role = Auth::user()['role'];
  145. return Redirect::to('transformativeAction')->withInput();
  146. } else {
  147. $user_id = auth::user()->id;
  148. $program_id = DB::table('program_user')
  149. ->where('user_id',$user_id)
  150. // ->where('user_id',8) //voy a usar 8 porque user 60 no tiene
  151. ->select('program_id')
  152. ->get();
  153. $program_id = $program_id[0]->program_id;
  154. $role = Auth::user()['role'];
  155. $by_professor = 1;
  156. if($role == 3){
  157. $by_professor = 0;
  158. }
  159. // $by_professor = $clean_input['approval'];
  160. $current_timestamp = date('Y/m/d H:i:s', time());
  161. // insert the TA
  162. $ta_id = DB::table('transformative_actions')->insertGetId(
  163. array('at_text' => $clean_input['text'],
  164. 'description' => $clean_input['description'],
  165. 'is_custom' => 1,
  166. 'user_id' => $user_id,
  167. 'program_id' => $program_id,
  168. 'created_at' => $current_timestamp,
  169. 'by_professor' => $by_professor,
  170. )
  171. );
  172. //
  173. // // insert the multiple TA_objective_program
  174. foreach ($clean_input['objectiveid'] as $objective_id) {
  175. DB::table('transformative_objective_program')->insert(
  176. array(
  177. 'ta_id' => $ta_id,
  178. 'objective_id' => $objective_id,
  179. 'program_id' => $program_id,
  180. 'created_at' => $current_timestamp,
  181. )
  182. );
  183. }
  184. //
  185. // // insert the multiple TA_course
  186. foreach ($clean_input['courseid'] as $course_id) {
  187. DB::table('TA_course')->insert(
  188. array(
  189. 'ta_id' => $ta_id,
  190. 'course_id' => $course_id,
  191. )
  192. );
  193. }
  194. Session::flash('status', 'success');
  195. Session::flash('message', 'Transformative Action created: "' . $clean_input['text'] . '".');
  196. $role = Auth::user()['role'];
  197. return Redirect::to('transformativeAction')->withInput();
  198. }
  199. }
  200. public function selectTA()
  201. {
  202. $ta_id = Input::get("ta_id");
  203. $user_id = Auth::user()->id;
  204. $program_id = DB::table('program_user')
  205. ->where('user_id',$user_id)
  206. ->select('program_id')
  207. ->get();
  208. $program_id = $program_id[0]->program_id;
  209. $objectives = DB::table('transformative_actions')
  210. ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
  211. ->join('objectives','objectives.id','=','transformative_objective_program.objective_id')
  212. ->where('transformative_actions.id',$ta_id)
  213. ->select('objectives.text as text', 'objectives.id as id')
  214. ->orderBy('objectives.text', 'ASC')
  215. ->get();
  216. $an_objective = $objectives[0]->id;
  217. $outcome_id = DB::table('criterion_objective_outcome')
  218. ->where('criterion_objective_outcome.objective_id',$an_objective)
  219. ->select('criterion_objective_outcome.outcome_id')
  220. ->get();
  221. $outcome_id = $outcome_id[0]->outcome_id;
  222. $objectives_from_outcome = DB::table('objectives')
  223. ->join('objective_program','objective_program.objective_id','=','objectives.id')
  224. ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','objectives.id')
  225. ->where('criterion_objective_outcome.outcome_id',$outcome_id)
  226. ->where('objective_program.program_id',$program_id)
  227. ->orderBy('objectives.text', 'ASC')
  228. ->select('objectives.text as text', 'objectives.id as id')
  229. ->get();
  230. $selected_courses = DB::table('ta_course')
  231. ->join('courses','courses.id','=','ta_course.course_id')
  232. ->where('ta_id',$ta_id)
  233. ->orderBy('courses.name', 'ASC')
  234. ->orderBy('courses.code', 'ASC')
  235. ->select('courses.*')
  236. ->get();
  237. $name = DB::table('transformative_actions')
  238. ->where('id',$ta_id)
  239. ->select('at_text as name')
  240. ->lists('name');
  241. $description = DB::table('transformative_actions')
  242. ->where('id',$ta_id)
  243. ->select('description')
  244. ->lists('description');
  245. $status = DB::table('transformative_actions')
  246. ->where('id',$ta_id)
  247. ->select('by_professor as status')
  248. ->lists('status');
  249. $status = $status[0];
  250. $plans_count = DB::table('annual_plan_transformative')
  251. ->where('id',$ta_id)
  252. ->get();
  253. $plans_count = count($plans_count);
  254. $can_be_deleted = false;
  255. if($plans_count == 0 && $status == 1){
  256. $can_be_deleted = true;
  257. }
  258. return array(
  259. 'objectives' => $objectives,
  260. 'objectives_from_outcome' => $objectives_from_outcome,
  261. 'selected_courses' => $selected_courses,
  262. 'name' => $name,
  263. 'description' => $description,
  264. 'status' => $status,
  265. 'can_be_deleted' => $can_be_deleted,
  266. 'plans_count' => $plans_count,
  267. );
  268. }
  269. public function objectivesFromOutcome()
  270. {
  271. $user_id = Auth::user()->id;
  272. $program_id = DB::table('program_user')
  273. ->where('user_id',$user_id)
  274. ->select('program_id')
  275. ->get();
  276. $program_id = $program_id[0]->program_id;
  277. $ta_id = Input::get("ta_id");
  278. $outcome_id = Input::get("outcome_id");
  279. $objectives = DB::table('objectives')
  280. ->join('objective_program','objective_program.objective_id','=','objectives.id')
  281. ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','objectives.id')
  282. ->where('criterion_objective_outcome.outcome_id',$outcome_id)
  283. ->where('objective_program.program_id',$program_id)
  284. ->orderBy('objectives.text', 'ASC')
  285. ->select('objectives.text as text', 'objectives.id as id')
  286. ->get();
  287. return array(
  288. 'objectives' => $objectives,
  289. );
  290. }
  291. public function filterTA()
  292. {
  293. $user_id = Auth::user()->id;
  294. $role = Auth::user()['role'];
  295. $professor_id = Input::get('professor_id');
  296. $course_id = Input::get('course_id');
  297. $outcome_id = Input::get('outcome_id');
  298. $panel_id = Input::get('panel_id');
  299. $program_id = DB::table('program_user')
  300. ->where('user_id',$user_id)
  301. ->select('program_id')
  302. ->get();
  303. $program_id = $program_id[0]->program_id;
  304. // if the user is a coordinator filtering the approvePanel
  305. if ($role == '3' && $panel_id == 'approvePanel'){
  306. // if professor isnt a desired filter, search all professors
  307. if ($professor_id == 0){
  308. $all_ta_users = DB::table('transformative_actions')
  309. ->where('transformative_actions.is_custom', 1)
  310. ->where('transformative_actions.program_id', $program_id)
  311. ->where('transformative_actions.by_professor',1)
  312. ->select('transformative_actions.user_id')
  313. ->get();
  314. $professor_id = array();
  315. foreach ($all_ta_users as $key => $user) {
  316. array_push($professor_id, $user->user_id);
  317. }
  318. }else{
  319. $professor_id = array($professor_id);
  320. }
  321. // if course isnt a desired filter, search all courses
  322. if ($course_id == 0){
  323. $courses = DB::table('ta_course')
  324. ->join('transformative_actions','transformative_actions.id','=','ta_course.ta_id')
  325. ->join('courses','courses.id','=','ta_course.course_id')
  326. ->where('transformative_actions.is_custom', 1)
  327. ->where('transformative_actions.program_id', $program_id)
  328. ->where('transformative_actions.by_professor', 1)
  329. ->select('courses.id')
  330. ->get();
  331. $course_id = array();
  332. foreach ($courses as $key => $course) {
  333. array_push($course_id, $course->id);
  334. }
  335. }else{
  336. $course_id = array($course_id);
  337. }
  338. // if outcome isnt a desired filter, search all outcomes
  339. if ($outcome_id == 0){
  340. $outcomes = DB::table('transformative_actions')
  341. ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
  342. ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
  343. ->join('outcomes','outcomes.id','=','criterion_objective_outcome.outcome_id')
  344. ->where('transformative_actions.is_custom', 1)
  345. ->where('transformative_actions.program_id', $program_id)
  346. ->where('transformative_actions.by_professor', 1)
  347. ->select('outcomes.id')
  348. ->groupBy('outcomes.id')
  349. ->get();
  350. $outcome_id = array();
  351. foreach ($outcomes as $key => $outcome) {
  352. array_push($outcome_id, $outcome->id);
  353. }
  354. }else{
  355. $outcome_id = array($outcome_id);
  356. }
  357. // search TA with filters
  358. $filtered_at = DB::table('transformative_actions')
  359. ->join('ta_course','ta_course.ta_id','=','transformative_actions.id')
  360. ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
  361. ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
  362. ->where('transformative_actions.is_custom', 1)
  363. ->where('transformative_actions.program_id',$program_id)
  364. ->where('transformative_actions.by_professor',1)
  365. ->whereIn('transformative_actions.user_id',$professor_id)
  366. ->whereIn('criterion_objective_outcome.outcome_id',$outcome_id)
  367. ->whereIn('ta_course.course_id',$course_id)
  368. ->select('transformative_actions.*')
  369. ->groupBy('transformative_actions.id')
  370. ->orderBy('transformative_actions.at_text', 'ASC')
  371. ->get();
  372. return $filtered_at;
  373. }
  374. // if the user is a coordinator filtering the editPanel
  375. elseif($role == '3' && $panel_id == 'editPanel'){
  376. // if professor isnt a desired filter, search all professors
  377. if ($professor_id == 0){
  378. $all_ta_users = DB::table('transformative_actions')
  379. ->where('transformative_actions.is_custom', 1)
  380. ->where('transformative_actions.program_id', $program_id)
  381. ->where('transformative_actions.by_professor',0)
  382. ->select('transformative_actions.user_id')
  383. ->get();
  384. $professor_id = array();
  385. foreach ($all_ta_users as $key => $user) {
  386. array_push($professor_id, $user->user_id);
  387. }
  388. }else{
  389. $professor_id = array($professor_id);
  390. }
  391. // if course isnt a desired filter, search all courses
  392. if ($course_id == 0){
  393. $courses = DB::table('ta_course')
  394. ->join('transformative_actions','transformative_actions.id','=','ta_course.ta_id')
  395. ->join('courses','courses.id','=','ta_course.course_id')
  396. ->where('transformative_actions.is_custom', 1)
  397. ->where('transformative_actions.program_id', $program_id)
  398. ->where('transformative_actions.by_professor', 0)
  399. ->select('courses.id')
  400. ->get();
  401. $course_id = array();
  402. foreach ($courses as $key => $course) {
  403. array_push($course_id, $course->id);
  404. }
  405. }else{
  406. $course_id = array($course_id);
  407. }
  408. // if outcome isnt a desired filter, search all outcomes
  409. if ($outcome_id == 0){
  410. $outcomes = DB::table('transformative_actions')
  411. ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
  412. ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
  413. ->join('outcomes','outcomes.id','=','criterion_objective_outcome.outcome_id')
  414. ->where('transformative_actions.is_custom', 1)
  415. ->where('transformative_actions.program_id', $program_id)
  416. ->where('transformative_actions.by_professor', 0)
  417. ->select('outcomes.id')
  418. ->groupBy('outcomes.id')
  419. ->get();
  420. $outcome_id = array();
  421. foreach ($outcomes as $key => $outcome) {
  422. array_push($outcome_id, $outcome->id);
  423. }
  424. }else{
  425. $outcome_id = array($outcome_id);
  426. }
  427. // search TA with filters
  428. $filtered_at = DB::table('transformative_actions')
  429. ->join('ta_course','ta_course.ta_id','=','transformative_actions.id')
  430. ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
  431. ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
  432. ->where('transformative_actions.is_custom', 1)
  433. ->where('transformative_actions.program_id',$program_id)
  434. ->where('transformative_actions.by_professor',0)
  435. ->whereIn('transformative_actions.user_id',$professor_id)
  436. ->whereIn('criterion_objective_outcome.outcome_id',$outcome_id)
  437. ->whereIn('ta_course.course_id',$course_id)
  438. ->select('transformative_actions.*')
  439. ->groupBy('transformative_actions.id')
  440. ->orderBy('transformative_actions.at_text', 'ASC')
  441. ->get();
  442. return $filtered_at;
  443. }
  444. // if the user is a professor filtering the editPanel
  445. elseif($role == '4' && $panel_id == 'editPanel'){
  446. // if course isnt a desired filter, search all courses
  447. if ($course_id == 0){
  448. $courses = DB::table('ta_course')
  449. ->join('transformative_actions','transformative_actions.id','=','ta_course.ta_id')
  450. ->join('courses','courses.id','=','ta_course.course_id')
  451. ->where('transformative_actions.user_id', Auth::user()->id)
  452. ->where('transformative_actions.is_custom', 1)
  453. ->where('transformative_actions.program_id', $program_id)
  454. ->where('transformative_actions.by_professor', 1)
  455. ->select('courses.id')
  456. ->get();
  457. $course_id = array();
  458. foreach ($courses as $key => $course) {
  459. array_push($course_id, $course->id);
  460. }
  461. }else{
  462. $course_id = array($course_id);
  463. }
  464. // if outcome isnt a desired filter, search all outcomes
  465. if ($outcome_id == 0){
  466. $outcomes = DB::table('transformative_actions')
  467. ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
  468. ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
  469. ->join('outcomes','outcomes.id','=','criterion_objective_outcome.outcome_id')
  470. ->where('transformative_actions.user_id', Auth::user()->id)
  471. ->where('transformative_actions.is_custom', 1)
  472. ->where('transformative_actions.program_id', $program_id)
  473. ->where('transformative_actions.by_professor', 1)
  474. ->select('outcomes.id')
  475. ->groupBy('outcomes.id')
  476. ->get();
  477. $outcome_id = array();
  478. foreach ($outcomes as $key => $outcome) {
  479. array_push($outcome_id, $outcome->id);
  480. }
  481. }else{
  482. $outcome_id = array($outcome_id);
  483. }
  484. // search TA with filters
  485. $filtered_at = DB::table('transformative_actions')
  486. ->join('ta_course','ta_course.ta_id','=','transformative_actions.id')
  487. ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
  488. ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
  489. ->where('transformative_actions.user_id', Auth::user()->id)
  490. ->where('transformative_actions.is_custom', 1)
  491. ->where('transformative_actions.program_id',$program_id)
  492. ->where('transformative_actions.by_professor',1)
  493. ->whereIn('criterion_objective_outcome.outcome_id',$outcome_id)
  494. ->whereIn('ta_course.course_id',$course_id)
  495. ->select('transformative_actions.*')
  496. ->groupBy('transformative_actions.id')
  497. ->orderBy('transformative_actions.at_text', 'ASC')
  498. ->get();
  499. return $filtered_at;
  500. }
  501. return 'ilegal';
  502. }
  503. public function editTA()
  504. {
  505. $title = "Transformative Action";
  506. $role = Auth::user()['role'];
  507. $outcomes = Outcome::orderBy('name', 'ASC')->lists('name', 'id');
  508. $schools = School::orderBy('name', 'ASC')->get();
  509. $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
  510. $programs = Program::orderBy('name', 'ASC')->get();
  511. $user_id = auth::user()->id; //ya, tester es 60
  512. $program_id = DB::table('program_user')
  513. ->where('user_id',$user_id)
  514. ->select('program_id')
  515. ->get();
  516. $program_id = $program_id[0]->program_id; //program id 15 debido al user 8
  517. $outcomes = Outcome::orderBy('name', 'ASC')
  518. ->where('deactivation_date','=','0000-00-00')
  519. ->orWhereNull('deactivation_date')
  520. ->get();
  521. $objectives = array();
  522. // if user is program coordinator
  523. if ($role == 3){
  524. //1 edit panel: load the TA that
  525. // are custom ('transformative_actions.by_professor' == 0)
  526. // were approved in past ('transformative_actions.is_custom' == 1)
  527. $ta_edit_panel = DB::table('transformative_actions')
  528. ->where('transformative_actions.is_custom', 1)
  529. ->where('transformative_actions.program_id', $program_id)
  530. ->where('transformative_actions.by_professor', 0)
  531. ->orderBy('at_text', 'ASC')
  532. ->get();
  533. //2 approve panel: load TAs that
  534. // can be approved ('transformative_actions.by_professor' == 1)
  535. $ta_approval_panel = DB::table('transformative_actions')
  536. ->where('transformative_actions.is_custom', 1)
  537. ->where('transformative_actions.program_id', $program_id)
  538. ->where('transformative_actions.by_professor',1)
  539. ->orderBy('at_text', 'ASC')
  540. ->get();
  541. //2.1 approve panel: load the filter options.
  542. // the "->where()" should be the same from $ta_approval_panel,
  543. // but with aditional joins and different select
  544. //
  545. // get the names of the professors
  546. $professor_filter_approvePanel = DB::table('transformative_actions')
  547. ->join('users','users.id','=','transformative_actions.user_id')
  548. ->where('transformative_actions.is_custom', 1)
  549. ->where('transformative_actions.program_id', $program_id)
  550. ->where('transformative_actions.by_professor', 1)
  551. ->select('users.*')
  552. ->groupby('transformative_actions.user_id')
  553. ->orderBy('users.first_name', 'ASC')
  554. ->get();
  555. // get the courses from asociated with a TA
  556. $course_filter_approvePanel = DB::table('ta_course')
  557. ->join('transformative_actions','transformative_actions.id','=','ta_course.ta_id')
  558. ->join('courses','courses.id','=','ta_course.course_id')
  559. ->where('transformative_actions.is_custom', 1)
  560. ->where('transformative_actions.program_id', $program_id)
  561. ->where('transformative_actions.by_professor', 1)
  562. ->select('courses.*')
  563. ->groupBy('courses.name','courses.code')
  564. ->orderBy('courses.name', 'ASC')
  565. ->orderBy('courses.code', 'ASC')
  566. ->get();
  567. // get the outcome asociated with a TA
  568. $outcome_filter_approvePanel = DB::table('transformative_actions')
  569. ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
  570. ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
  571. ->join('outcomes','outcomes.id','=','criterion_objective_outcome.outcome_id')
  572. ->where('transformative_actions.is_custom', 1)
  573. ->where('transformative_actions.program_id', $program_id)
  574. ->where('transformative_actions.by_professor', 1)
  575. ->select('outcomes.*')
  576. ->groupBy('outcomes.id')
  577. ->orderBy('outcomes.name', 'ASC')
  578. ->get();
  579. //3 edit panel: load the filter options.
  580. // the "->where()" should be the same from $ta_edit_panel,
  581. // but with aditional joins and different select
  582. //
  583. $professor_filter_editPanel = DB::table('transformative_actions')
  584. ->join('users','users.id','=','transformative_actions.user_id')
  585. ->where('transformative_actions.is_custom', 1)
  586. ->where('transformative_actions.program_id', $program_id)
  587. ->where('transformative_actions.by_professor', 0)
  588. ->select('users.*')
  589. ->groupby('transformative_actions.user_id')
  590. ->orderBy('users.first_name', 'ASC')
  591. ->get();
  592. $course_filter_editPanel = DB::table('ta_course')
  593. ->join('transformative_actions','transformative_actions.id','=','ta_course.ta_id')
  594. ->join('courses','courses.id','=','ta_course.course_id')
  595. ->where('transformative_actions.is_custom', 1)
  596. ->where('transformative_actions.program_id', $program_id)
  597. ->where('transformative_actions.by_professor', 0)
  598. ->select('courses.*')
  599. ->groupBy('courses.name','courses.code')
  600. ->orderBy('courses.name', 'ASC')
  601. ->orderBy('courses.code', 'ASC')
  602. ->get();
  603. $outcome_filter_editPanel = DB::table('transformative_actions')
  604. ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
  605. ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
  606. ->join('outcomes','outcomes.id','=','criterion_objective_outcome.outcome_id')
  607. ->where('transformative_actions.is_custom', 1)
  608. ->where('transformative_actions.program_id', $program_id)
  609. ->where('transformative_actions.by_professor', 0)
  610. ->select('outcomes.*')
  611. ->groupBy('outcomes.id')
  612. ->orderBy('outcomes.name', 'ASC')
  613. ->get();
  614. // 4 create panel: search all courses
  615. $courses_create = DB::table('courses')
  616. ->where('courses.program_id',$program_id)
  617. ->select('courses.*')
  618. ->groupBy('courses.name','courses.code')
  619. ->orderBy('courses.name', 'ASC')
  620. ->orderBy('courses.code', 'ASC')
  621. ->get();
  622. }
  623. // if user is profesor
  624. elseif ($role == 4){
  625. // 1 the user can only edit TA that need approval and has been submited by the same user
  626. $ta_edit_panel = DB::table('transformative_actions')
  627. ->where('transformative_actions.is_custom', 1)
  628. ->where('transformative_actions.program_id',$program_id)
  629. ->where('transformative_actions.user_id', Auth::user()->id)
  630. ->where('transformative_actions.by_professor',1)
  631. ->select('transformative_actions.*')
  632. ->orderBy('at_text', 'ASC')
  633. ->get();
  634. // 2 approve panel: dont load TA since professors cant approve them
  635. $ta_approval_panel = array();
  636. // 3 edit panel: load professor filter for his courses
  637. $professor_filter_editPanel = array();
  638. $course_filter_editPanel = DB::table('ta_course')
  639. ->join('transformative_actions','transformative_actions.id','=','ta_course.ta_id')
  640. ->join('courses','courses.id','=','ta_course.course_id')
  641. ->where('transformative_actions.is_custom', 1)
  642. ->where('transformative_actions.user_id', Auth::user()->id)
  643. ->where('transformative_actions.program_id', $program_id)
  644. ->where('transformative_actions.by_professor', 1)
  645. ->select('courses.*')
  646. ->groupBy('courses.name','courses.code')
  647. ->orderBy('courses.name', 'ASC')
  648. ->orderBy('courses.code', 'ASC')
  649. ->get();
  650. $outcome_filter_editPanel = DB::table('transformative_actions')
  651. ->join('transformative_objective_program','transformative_objective_program.ta_id','=','transformative_actions.id')
  652. ->join('criterion_objective_outcome','criterion_objective_outcome.objective_id','=','transformative_objective_program.objective_id')
  653. ->join('outcomes','outcomes.id','=','criterion_objective_outcome.outcome_id')
  654. ->where('transformative_actions.is_custom', 1)
  655. ->where('transformative_actions.user_id', Auth::user()->id)
  656. ->where('transformative_actions.program_id',$program_id)
  657. ->where('transformative_actions.by_professor', 1)
  658. ->select('outcomes.*')
  659. ->groupBy('outcomes.id')
  660. ->orderBy('outcomes.name', 'ASC')
  661. ->get();
  662. // these arent used by professors
  663. $professor_filter_approvePanel = array();
  664. $course_filter_approvePanel = array();
  665. $outcome_filter_approvePanel = array();
  666. // 4 create panel: search courses given by the professor
  667. $courses_create = DB::table('courses')
  668. ->where('courses.program_id',$program_id)
  669. ->where('courses.user_id', Auth::user()->id)
  670. ->select('courses.*')
  671. ->groupBy('courses.name','courses.code')
  672. ->orderBy('courses.name', 'ASC')
  673. ->orderBy('courses.code', 'ASC')
  674. ->get();
  675. }
  676. return View::make('local.managers.admins.transformativeAction', compact('title','role', 'outcomes', 'schools', 'criteria', 'programs', 'outcomes', 'objectives',
  677. 'ta_edit_panel','ta_approval_panel','courses_create',
  678. 'professor_filter_approvePanel', 'course_filter_approvePanel', 'outcome_filter_approvePanel',
  679. 'professor_filter_editPanel', 'outcome_filter_editPanel', 'course_filter_editPanel'
  680. ));
  681. }
  682. private function cleanInputEdit()
  683. {
  684. $clean_input = array();
  685. $clean_input['name'] = trim(preg_replace('/\t+/', '', Input::get('name')));
  686. $trimmed = trim(preg_replace('/\t+/', '', Input::get('subcriteria')));
  687. Log::info('trimmed 1 -->' . $trimmed . '<--');
  688. if ($trimmed == '') {
  689. $trimmed = NULL;
  690. } else {
  691. $trimmed = json_encode(preg_split('/\r\n/', $trimmed));
  692. }
  693. Log::info('trimmed 2 -->' . $trimmed . '<--');
  694. $clean_input['subcriteria'] = $trimmed;
  695. $clean_input['outcome_id'] = Input::get('assoc_outcome');
  696. $clean_input['objective_id'] = Input::get('assoc_objective');
  697. $clean_input['program_id'] = Input::get('program_id');
  698. $clean_input['copyright'] = trim(preg_replace('/\t+/', '', Input::get('copyright')));
  699. $clean_input['notes'] = trim(preg_replace('/\t+/', '', Input::get('notes')));
  700. $clean_input['maximum_score'] = (int) Input::get('assoc_maximum_score');
  701. $clean_input['scale_title'] = Input::get('assoc_title');
  702. $clean_input['scale_description'] = Input::get('assoc_scales');
  703. $clean_input['number_of_scales'] = sizeof($clean_input['scale_title']);
  704. return $clean_input;
  705. }
  706. public function updateTA()
  707. {
  708. $clean_input = array();
  709. $clean_input['ta_id'] = Input::get('ta_id');
  710. $clean_input['text'] = Input::get('editText');
  711. $clean_input['description'] = Input::get('editDescription');
  712. $clean_input['objectiveid'] = Input::get('objectiveid');
  713. $clean_input['courseid'] = Input::get('courseid');
  714. $clean_input['approval'] = Input::get('approval');
  715. /** Validation rules */
  716. $validator = Validator::make(
  717. array(
  718. 'text' => $clean_input['text'],
  719. 'description' => $clean_input['description'],
  720. 'objectiveid' => $clean_input['objectiveid'],
  721. 'courseid' => $clean_input['courseid'],
  722. 'approval' => $clean_input['approval'],
  723. ),
  724. array(
  725. 'text' => 'required|string',
  726. 'description' => 'required|string',
  727. // 'subcriteria' => 'string',
  728. 'objectiveid' => 'required|array',
  729. 'courseid' => 'required|array',
  730. 'approval' => 'required|integer',
  731. // 'notes' => 'string',
  732. // 'copyright' => 'string',
  733. // 'maximum_score' => 'required|integer'
  734. )
  735. );
  736. /** If validation fails */
  737. if ($validator->fails()) {
  738. /** Prepare error message */
  739. $message = 'Error(s) updating the Transformative Action: <ul>';
  740. foreach ($validator->messages()->all('<li>:message</li>') as $validationError) {
  741. $message .= $validationError;
  742. }
  743. $message .= '</ul>';
  744. /** Send error message and old data */
  745. Session::flash('status', 'danger');
  746. Session::flash('message', $message);
  747. $role = Auth::user()['role'];
  748. return Redirect::to('transformativeAction')->withInput();
  749. } else {
  750. $user_id = auth::user()->id;
  751. $program_id = DB::table('program_user')
  752. ->where('user_id',$user_id)
  753. // ->where('user_id',8) //voy a usar 8 porque user 60 no tiene
  754. ->select('program_id')
  755. ->get();
  756. $program_id = $program_id[0]->program_id;
  757. $role = Auth::user()['role'];
  758. $by_professor = $clean_input['approval'];
  759. if($role == 4){
  760. $by_professor = 1;
  761. }
  762. // $by_professor = $clean_input['approval'];
  763. $current_timestamp = date('Y/m/d H:i:s', time());
  764. // edit the TA
  765. DB::table('transformative_actions')
  766. ->where('id', $clean_input['ta_id'])
  767. ->update([
  768. 'by_professor' => $by_professor,
  769. 'at_text' => $clean_input['text'],
  770. 'description' => $clean_input['description'],
  771. 'updated_at' => $current_timestamp,
  772. ]);
  773. $ta_id = $clean_input['ta_id'];
  774. $new_objective_id = $clean_input['objectiveid'];
  775. $old_objective_id = DB::table('transformative_objective_program')
  776. ->where('ta_id',$ta_id)
  777. ->select('objective_id')
  778. ->lists('objective_id');
  779. //delete existing objective_id if it isnt in new_ids array
  780. foreach ($old_objective_id as $old_id) {
  781. if(in_array($old_id,$new_objective_id)){
  782. //do nothing if a new id is already in atble
  783. }
  784. else {
  785. //if old id not in new id, delete
  786. DB::table('transformative_objective_program')
  787. ->where('ta_id',$ta_id)
  788. ->where('objective_id', $old_id)
  789. ->delete();
  790. }
  791. }
  792. //
  793. foreach ($new_objective_id as $new_id) {
  794. $result = DB::table('transformative_objective_program')
  795. ->where('objective_id',$new_id)
  796. ->select('objective_id')
  797. ->lists('objective_id');
  798. if(count($result) == 0){
  799. //if the new_id does not exists, do nothing
  800. DB::table('transformative_objective_program')->insert(
  801. array(
  802. 'ta_id' => $ta_id,
  803. 'objective_id' => $new_id,
  804. 'program_id' => $program_id,
  805. 'created_at' => $current_timestamp,
  806. )
  807. );
  808. }
  809. else {
  810. //if the new_id already exists, do nothing
  811. }
  812. }
  813. $new_course_id = $clean_input['courseid'];
  814. $old_course_id = DB::table('ta_course')
  815. ->where('ta_id',$ta_id)
  816. ->select('course_id')
  817. ->lists('course_id');
  818. //delete existing course_id if it isnt in new_ids array
  819. foreach ($old_course_id as $old_id) {
  820. if(in_array($old_id,$new_course_id)){
  821. //do nothing if a new id is already in atble
  822. }
  823. else {
  824. //if old id not in new id, delete
  825. DB::table('ta_course')
  826. ->where('ta_id',$ta_id)
  827. ->where('course_id', $old_id)
  828. ->delete();
  829. }
  830. }
  831. //add course_id if it isnt already inserted
  832. foreach ($new_course_id as $new_id) {
  833. $result = DB::table('ta_course')
  834. ->where('ta_id',$ta_id)
  835. ->where('course_id',$new_id)
  836. ->select('course_id')
  837. ->lists('course_id');
  838. if(count($result) == 0){
  839. //if the new_id does not exists, do nothing
  840. DB::table('ta_course')->insert(
  841. array(
  842. 'ta_id' => $ta_id,
  843. 'course_id' => $new_id,
  844. )
  845. );
  846. }
  847. else {
  848. //if the new_id already exists, do nothing
  849. }
  850. }
  851. Session::flash('status', 'success');
  852. Session::flash('message', 'Updated Transformative Action: "' . $clean_input['text'] . '".');
  853. $role = Auth::user()['role'];
  854. return Redirect::to('transformativeAction')->withInput();
  855. }
  856. }
  857. public function approveTA()
  858. {
  859. $role = Auth::user()['role'];
  860. if($role != 3){
  861. $message = 'Only Program Coordinators can approve a TA';
  862. Session::flash('status', 'danger');
  863. Session::flash('message', $message);
  864. return Redirect::to('transformativeAction')->withInput();
  865. }
  866. $ta_id = Input::get('ta_id');
  867. $text = Input::get('at_text');
  868. if($ta_id == 0){
  869. $message = 'Please select a Transformative Action</p>';
  870. Session::flash('status', 'danger');
  871. Session::flash('message', $message);
  872. $role = Auth::user()['role'];
  873. return Redirect::to('transformativeAction')->withInput();
  874. }
  875. $current_timestamp = date('Y/m/d H:i:s', time());
  876. // edit the TA
  877. DB::table('transformative_actions')
  878. ->where('id', $ta_id)
  879. ->update([
  880. 'by_professor' => 0,
  881. 'updated_at' => $current_timestamp,
  882. ]);
  883. Session::flash('status', 'success');
  884. Session::flash('message', 'Approved the Transformative Action: "' . $text . '".');
  885. $role = Auth::user()['role'];
  886. return Redirect::to('transformativeAction')->withInput();
  887. }
  888. }