Aucune description

TransformativeActionsController.php 41KB

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