Geen omschrijving

TransformativeActionsController.php 43KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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('objective_outcome', 'objective_outcome.objective_id', '=', 'transformative_objective.objective_id')
  120. ->join('outcomes', 'outcomes.id', '=', '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_role = Auth::user()->role;
  562. switch ($user_role) {
  563. case 1:
  564. $program_ids = DB::table('programs')->lists('id');
  565. break;
  566. case 2:
  567. $user_school = Auth::user()->school_id;
  568. $program_ids = DB::table('programs')
  569. ->where('school_id', $user_school)
  570. ->lists('id');
  571. break;
  572. case 3:
  573. $user_id = Auth::user()->id;
  574. $program_ids = DB::table('program_user')
  575. ->where('user_id', $user_id)
  576. ->select('program_id')
  577. ->lists('program_id');
  578. break;
  579. }
  580. //$program_id = DB::table('program_user')
  581. // ->where('user_id', $user_id)
  582. // ->select('program_id')
  583. // ->get();
  584. //$program_id = $program_id[0]->program_id;
  585. $objectives = DB::table('transformative_actions')
  586. ->join('transformative_objective', 'transformative_objective.ta_id', '=', 'transformative_actions.id')
  587. ->join('objectives', 'objectives.id', '=', 'transformative_objective.objective_id')
  588. ->where('transformative_actions.id', $ta_id)
  589. ->select('objectives.text as text', 'objectives.id as id')
  590. ->orderBy('objectives.text', 'ASC')
  591. ->get();
  592. $objective_ids = DB::table('transformative_actions')
  593. ->join('transformative_objective', 'transformative_objective.ta_id', '=', 'transformative_actions.id')
  594. ->join('objectives', 'objectives.id', '=', 'transformative_objective.objective_id')
  595. ->where('transformative_actions.id', $ta_id)
  596. ->select('objectives.text as text', 'objectives.id as id')
  597. ->orderBy('objectives.text', 'ASC')
  598. ->lists('objectives.id');
  599. Log::info($ta_id);
  600. //$an_objective = $objectives[0]->id;
  601. $outcome_ids = DB::table('objective_outcome')
  602. ->whereIn('objective_outcome.objective_id', $objective_ids)
  603. ->select('objective_outcome.outcome_id')
  604. ->lists('outcome_id');
  605. //$outcome_id = $outcome_id[0]->outcome_id;
  606. Log::info("outcomes");
  607. Log::info($outcome_ids);
  608. $objectives_from_outcome = DB::table('objectives')
  609. ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
  610. ->join('objective_outcome', 'objective_outcome.objective_id', '=', 'objectives.id')
  611. ->whereIn('objective_outcome.outcome_id', $outcome_ids)
  612. ->whereIn('objective_program.program_id', $program_ids)
  613. ->orderBy('objectives.text', 'ASC')
  614. ->select('objectives.text as text', 'objectives.id as id')
  615. ->distinct()
  616. ->get();
  617. $selected_courses = DB::table('ta_course')
  618. ->join('courses', function ($join) {
  619. $join->on('courses.number', '=', 'ta_course.course_number');
  620. $join->on('courses.code', '=', 'ta_course.course_code');
  621. })
  622. ->where('ta_id', $ta_id)
  623. ->orderBy('courses.name', 'ASC')
  624. ->orderBy('courses.code', 'ASC')
  625. ->select('courses.*')
  626. ->get();
  627. $name = DB::table('transformative_actions')
  628. ->where('id', $ta_id)
  629. ->select('at_text as name')
  630. ->lists('name');
  631. $description = DB::table('transformative_actions')
  632. ->where('id', $ta_id)
  633. ->select('description')
  634. ->lists('description');
  635. $status = DB::table('transformative_actions')
  636. ->where('id', $ta_id)
  637. ->select('by_professor as status')
  638. ->lists('status');
  639. $status = $status[0];
  640. $plans_count = DB::table('annual_plan_transformative')
  641. ->where('id', $ta_id)
  642. ->get();
  643. $plans_count = count($plans_count);
  644. $can_be_deleted = false;
  645. if ($plans_count == 0 && $status == 1) {
  646. $can_be_deleted = true;
  647. }
  648. return array(
  649. 'objectives' => $objectives,
  650. 'objectives_from_outcome' => $objectives_from_outcome,
  651. 'selected_courses' => $selected_courses,
  652. 'name' => $name,
  653. 'description' => $description,
  654. 'status' => $status,
  655. 'can_be_deleted' => $can_be_deleted,
  656. 'plans_count' => $plans_count,
  657. );
  658. }
  659. // load the available objectieves from an outcome when creating a Tranformative Action
  660. public function objectivesFromOutcome()
  661. {
  662. $user_role = Auth::user()->role;
  663. switch ($user_role) {
  664. case 1:
  665. $program_ids = DB::table('programs')->lists('id');
  666. break;
  667. case 2:
  668. $user_school = Auth::user()->school_id;
  669. $program_ids = DB::table('programs')
  670. ->where('school_id', $user_school)
  671. ->lists('id');
  672. break;
  673. case 3:
  674. $user_id = Auth::user()->id;
  675. $program_ids = DB::table('program_user')
  676. ->where('user_id', $user_id)
  677. ->select('program_id')
  678. ->lists('program_id');
  679. break;
  680. }
  681. //$program_id = DB::table('program_user')
  682. // ->where('user_id', $user_id)
  683. // ->select('program_id')
  684. // ->get();
  685. //$program_id = $program_id[0]->program_id;
  686. $ta_id = Input::get("ta_id");
  687. $outcome_id = Input::get("outcome_id");
  688. $objectives = DB::table('objectives')
  689. ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
  690. ->join('objective_outcome', 'objective_outcome.objective_id', '=', 'objectives.id')
  691. ->where('objective_outcome.outcome_id', $outcome_id)
  692. ->whereIn('objective_program.program_id', $program_ids)
  693. ->orderBy('objectives.text', 'ASC')
  694. ->select('objectives.text as text', 'objectives.id as id')
  695. ->get();
  696. return array(
  697. 'objectives' => $objectives,
  698. );
  699. }
  700. // return Transformative Actions that meet the filter criterias
  701. public function filterTA()
  702. {
  703. $user_id = Auth::user()->id;
  704. $role = Auth::user()['role'];
  705. $professor_id = Input::get('professor_id');
  706. $course_id = Input::get('course_id');
  707. $outcome_id = Input::get('outcome_id');
  708. $panel_id = Input::get('panel_id');
  709. $program_id = DB::table('program_user')
  710. ->where('user_id', $user_id)
  711. ->select('program_id')
  712. ->get();
  713. $program_id = $program_id[0]->program_id;
  714. // if the user is a coordinator filtering the approvePanel
  715. if ($role == '3' && $panel_id == 'approvePanel') {
  716. // if professor isnt a desired filter, search all professors
  717. if ($professor_id == 0) {
  718. $all_ta_users = DB::table('transformative_actions')
  719. ->where('transformative_actions.is_custom', 1)
  720. ->where('transformative_actions.program_id', $program_id)
  721. ->where('transformative_actions.by_professor', 1)
  722. ->select('transformative_actions.user_id')
  723. ->get();
  724. $professor_id = array();
  725. foreach ($all_ta_users as $key => $user) {
  726. array_push($professor_id, $user->user_id);
  727. }
  728. } else {
  729. $professor_id = array($professor_id);
  730. }
  731. // if course isnt a desired filter, search all courses
  732. if ($course_id == 0) {
  733. $courses = DB::table('ta_course')
  734. ->join('transformative_actions', 'transformative_actions.id', '=', 'ta_course.ta_id')
  735. ->join('courses', function ($join) {
  736. $join->on('courses.number', '=', 'ta_course.course_number');
  737. $join->on('courses.code', '=', 'ta_course.course_code');
  738. })
  739. ->where('transformative_actions.is_custom', 1)
  740. ->where('transformative_actions.program_id', $program_id)
  741. ->where('transformative_actions.by_professor', 1)
  742. ->select('ta_course.course_number', 'ta_course.course_code')
  743. ->distinct()
  744. ->get();
  745. $course_id = array();
  746. foreach ($courses as $key => $course) {
  747. array_push($course_id["number"], $course->course_number);
  748. array_push($course_id["code"], $course->code);
  749. }
  750. } else {
  751. $parentesis = array('(', ')');
  752. $course_code_number = str_replace($parentesis, '', $course_id);
  753. $course_code_number = explode(',', $course_code_number);
  754. $course_id = array();
  755. $course_id['number'] = $course_code_number[1];
  756. $course_id['code'] = $course_code_number[0];
  757. }
  758. // if outcome isnt a desired filter, search all outcomes
  759. if ($outcome_id == 0) {
  760. $outcomes = DB::table('transformative_actions')
  761. ->join('transformative_objective', 'transformative_objective.ta_id', '=', 'transformative_actions.id')
  762. ->join('objective_outcome', 'objective_outcome.objective_id', '=', 'transformative_objective.objective_id')
  763. ->join('outcomes', 'outcomes.id', '=', 'objective_outcome.outcome_id')
  764. ->where('transformative_actions.is_custom', 1)
  765. ->where('transformative_actions.program_id', $program_id)
  766. ->where('transformative_actions.by_professor', 1)
  767. ->select('outcomes.id')
  768. ->groupBy('outcomes.id')
  769. ->get();
  770. $outcome_id = array();
  771. foreach ($outcomes as $key => $outcome) {
  772. array_push($outcome_id, $outcome->id);
  773. }
  774. } else {
  775. $outcome_id = array($outcome_id);
  776. }
  777. // search TA with filters
  778. $filtered_at = DB::table('transformative_actions')
  779. ->join('ta_course', 'ta_course.ta_id', '=', 'transformative_actions.id')
  780. ->join('transformative_objective', 'transformative_objective.ta_id', '=', 'transformative_actions.id')
  781. ->join('objective_outcome', 'objective_outcome.objective_id', '=', 'transformative_objective.objective_id')
  782. ->where('transformative_actions.is_custom', 1)
  783. ->where('transformative_actions.program_id', $program_id)
  784. ->where('transformative_actions.by_professor', 1)
  785. ->whereIn('transformative_actions.user_id', $professor_id)
  786. ->whereIn('objective_outcome.outcome_id', $outcome_id)
  787. ->whereIn('ta_course.course_code', $course_id['code'])
  788. ->whereIn('ta_course.course_number', $course_id['number'])
  789. ->select('transformative_actions.*')
  790. ->groupBy('transformative_actions.id')
  791. ->orderBy('transformative_actions.at_text', 'ASC')
  792. ->get();
  793. return $filtered_at;
  794. }
  795. // if the user is a coordinator filtering the editPanel
  796. elseif ($role == '3' && $panel_id == 'editPanel') {
  797. // if professor isnt a desired filter, search all professors
  798. if ($professor_id == 0) {
  799. $all_ta_users = DB::table('transformative_actions')
  800. ->where('transformative_actions.is_custom', 1)
  801. ->where('transformative_actions.program_id', $program_id)
  802. ->where('transformative_actions.by_professor', 0)
  803. ->select('transformative_actions.user_id')
  804. ->get();
  805. $professor_id = array();
  806. foreach ($all_ta_users as $key => $user) {
  807. array_push($professor_id, $user->user_id);
  808. }
  809. } else {
  810. $professor_id = array($professor_id);
  811. }
  812. // if course isnt a desired filter, search all courses
  813. if ($course_id == 0) {
  814. $courses = DB::table('ta_course')
  815. ->join('transformative_actions', 'transformative_actions.id', '=', 'ta_course.ta_id')
  816. ->join('courses', function ($join) {
  817. $join->on('courses.number', '=', 'ta_course.course_number');
  818. $join->on('courses.code', '=', 'ta_course.course_code');
  819. })
  820. ->where('transformative_actions.is_custom', 1)
  821. ->where('transformative_actions.program_id', $program_id)
  822. ->where('transformative_actions.by_professor', 0)
  823. ->select('ta_course.course_number', 'ta_course.course_code')
  824. ->distinct()
  825. ->get();
  826. $course_id = array();
  827. foreach ($courses as $key => $course) {
  828. array_push($course_id["number"], $course->course_number);
  829. array_push($course_id["code"], $course->code);
  830. }
  831. } else {
  832. $parentesis = array('(', ')');
  833. $course_code_number = str_replace($parentesis, '', $course_id);
  834. $course_code_number = explode(',', $course_code_number);
  835. $course_id = array();
  836. $course_id['number'] = $course_code_number[1];
  837. $course_id['code'] = $course_code_number[0];
  838. }
  839. // if outcome isnt a desired filter, search all outcomes
  840. if ($outcome_id == 0) {
  841. $outcomes = DB::table('transformative_actions')
  842. ->join('transformative_objective', 'transformative_objective.ta_id', '=', 'transformative_actions.id')
  843. ->join('objective_outcome', 'objective_outcome.objective_id', '=', 'transformative_objective.objective_id')
  844. ->join('outcomes', 'outcomes.id', '=', 'objective_outcome.outcome_id')
  845. ->where('transformative_actions.is_custom', 1)
  846. ->where('transformative_actions.program_id', $program_id)
  847. ->where('transformative_actions.by_professor', 0)
  848. ->select('outcomes.id')
  849. ->groupBy('outcomes.id')
  850. ->get();
  851. $outcome_id = array();
  852. foreach ($outcomes as $key => $outcome) {
  853. array_push($outcome_id, $outcome->id);
  854. }
  855. } else {
  856. $outcome_id = array($outcome_id);
  857. }
  858. // search TA with filters
  859. $filtered_at = DB::table('transformative_actions')
  860. ->join('ta_course', 'ta_course.ta_id', '=', 'transformative_actions.id')
  861. ->join('transformative_objective', 'transformative_objective.ta_id', '=', 'transformative_actions.id')
  862. ->join('objective_outcome', 'objective_outcome.objective_id', '=', 'transformative_objective.objective_id')
  863. ->where('transformative_actions.is_custom', 1)
  864. ->where('transformative_actions.program_id', $program_id)
  865. ->where('transformative_actions.by_professor', 0)
  866. ->whereIn('transformative_actions.user_id', $professor_id)
  867. ->whereIn('objective_outcome.outcome_id', $outcome_id)
  868. ->whereIn('ta_course.course_code', $course_id['code'])
  869. ->whereIn('ta_course.course_number', $course_id['number'])
  870. ->select('transformative_actions.*')
  871. ->groupBy('transformative_actions.id')
  872. ->orderBy('transformative_actions.at_text', 'ASC')
  873. ->get();
  874. return $filtered_at;
  875. }
  876. // if the user is a professor filtering the editPanel
  877. elseif ($role == '4' && $panel_id == 'editPanel') {
  878. // if course isnt a desired filter, search all courses
  879. if ($course_id == 0) {
  880. $courses = DB::table('ta_course')
  881. ->join('transformative_actions', 'transformative_actions.id', '=', 'ta_course.ta_id')
  882. ->join('courses', 'courses.id', '=', 'ta_course.course_id')
  883. ->where('transformative_actions.user_id', Auth::user()->id)
  884. ->where('transformative_actions.is_custom', 1)
  885. ->where('transformative_actions.program_id', $program_id)
  886. ->where('transformative_actions.by_professor', 1)
  887. ->select('ta_course.course_number', 'ta_course.course_code')
  888. ->distinct()
  889. ->get();
  890. $course_id = array();
  891. foreach ($courses as $key => $course) {
  892. array_push($course_id["number"], $course->course_number);
  893. array_push($course_id["code"], $course->code);
  894. }
  895. } else {
  896. $parentesis = array('(', ')');
  897. $course_code_number = str_replace($parentesis, '', $course_id);
  898. $course_code_number = explode(',', $course_code_number);
  899. $course_id = array();
  900. $course_id['number'] = $course_code_number[1];
  901. $course_id['code'] = $course_code_number[0];
  902. }
  903. // if outcome isnt a desired filter, search all outcomes
  904. if ($outcome_id == 0) {
  905. $outcomes = DB::table('transformative_actions')
  906. ->join('transformative_objective', 'transformative_objective.ta_id', '=', 'transformative_actions.id')
  907. ->join('objective_outcome', 'objective_outcome.objective_id', '=', 'transformative_objective.objective_id')
  908. ->join('outcomes', 'outcomes.id', '=', 'objective_outcome.outcome_id')
  909. ->where('transformative_actions.user_id', Auth::user()->id)
  910. ->where('transformative_actions.is_custom', 1)
  911. ->where('transformative_actions.program_id', $program_id)
  912. ->where('transformative_actions.by_professor', 1)
  913. ->select('outcomes.id')
  914. ->groupBy('outcomes.id')
  915. ->get();
  916. $outcome_id = array();
  917. foreach ($outcomes as $key => $outcome) {
  918. array_push($outcome_id, $outcome->id);
  919. }
  920. } else {
  921. $outcome_id = array($outcome_id);
  922. }
  923. // search TA with filters
  924. $filtered_at = DB::table('transformative_actions')
  925. ->join('ta_course', 'ta_course.ta_id', '=', 'transformative_actions.id')
  926. ->join('transformative_objective', 'transformative_objective.ta_id', '=', 'transformative_actions.id')
  927. ->join('objective_outcome', 'objective_outcome.objective_id', '=', 'transformative_objective.objective_id')
  928. ->where('transformative_actions.user_id', Auth::user()->id)
  929. ->where('transformative_actions.is_custom', 1)
  930. ->where('transformative_actions.program_id', $program_id)
  931. ->where('transformative_actions.by_professor', 1)
  932. ->whereIn('objective_outcome.outcome_id', $outcome_id)
  933. ->whereIn('ta_course.course_code', $course_id['code'])
  934. ->whereIn('ta_course.course_number', $course_id['number'])
  935. ->select('transformative_actions.*')
  936. ->groupBy('transformative_actions.id')
  937. ->orderBy('transformative_actions.at_text', 'ASC')
  938. ->get();
  939. return $filtered_at;
  940. }
  941. return 'ilegal';
  942. }
  943. function postActivityCriterion($activity_id)
  944. {
  945. DB::beginTransaction();
  946. $existing_transformative_action = DB::table('transformative_actions')
  947. ->join('transformative_activity_criterion', 'transformative_activity_criterion.trans_action_id', '=', 'transformative_actions.id')
  948. ->join('activity_criterion', 'activity_criterion.id', '=', 'transformative_activity_criterion.activity_criterion_id')
  949. ->where('activity_id', $activity_id)
  950. ->delete();
  951. $activity_criterion = Input::get('trans_act');
  952. $trans = new TransformativeAction;
  953. $trans->user_id = Auth::user()['id'];
  954. $program_id = DB::table('activities')
  955. ->join('courses', 'activities.course_id', '=', 'courses.id')
  956. ->where('activities.id', $activity_id)
  957. ->lists('program_id');
  958. $trans->program_id = $program_id[0];
  959. $trans->is_custom = 1;
  960. $trans->by_professor = 1;
  961. $trans->at_text = Input::get('name_trans');
  962. $trans->description = Input::get('transforming_actions');
  963. $activity = DB::table('activities')
  964. ->where('id', $activity_id)
  965. ->first();
  966. $course_credentials = DB::table('courses')
  967. ->where('id', $activity->course_id)
  968. ->first();
  969. if ($trans->save()) {
  970. foreach ($activity_criterion as $single_ac) {
  971. $result = DB::insert("insert into `transformative_activity_criterion` (`trans_action_id`, `activity_criterion_id`) values ($trans->id, $single_ac)");
  972. if (!$result) {
  973. DB::rollback();
  974. Session::flash('status', 'danger');
  975. Session::flash('message', 'Error saving Transforming Action. Try again later.');
  976. return Redirect::to("professor/activities/{$activity_id}");
  977. }
  978. }
  979. $criteria = DB::table('activity_criterion')
  980. ->whereIn('id', $activity_criterion)
  981. ->lists('criterion_id');
  982. $allObjectives = DB::table('criterion_objective_outcome')
  983. ->whereIn('criterion_id', $criteria)
  984. ->select('objective_id')
  985. ->distinct()
  986. ->lists('objective_id');
  987. foreach ($allObjectives as $objective) {
  988. $result2 = DB::insert("insert into `transformative_objective` (`ta_id`, `objective_id`) values ({$trans->id},{$objective})");
  989. if (!$result2) {
  990. DB::rollback();
  991. Session::flash('status', 'danger');
  992. Session::flash('message', 'Error saving Transforming Action. Try again later.');
  993. return Redirect::to("professor/activities/{$activity_id}");
  994. }
  995. }
  996. $result3 = DB::insert("insert into `ta_course` (`ta_id`, `course_number`, `course_code`) values ({$trans->id}, '{$course_credentials->number}', '{$course_credentials->code}')");
  997. if (!($result3)) {
  998. DB::rollback();
  999. Session::flash('status', 'danger');
  1000. Session::flash('message', 'Error saving Transforming Action. Try again later.');
  1001. return Redirect::to("professor/activities/{$activity_id}");
  1002. }
  1003. DB::commit();
  1004. Session::flash('status', 'success');
  1005. Session::flash('message', 'Transformative Actions Saved.');
  1006. return Redirect::to("professor/activities/{$activity_id}");
  1007. } else {
  1008. DB::rollback();
  1009. Session::flash('status', 'danger');
  1010. Session::flash('message', 'Error saving Transforming Action. Try again later.');
  1011. return Redirect::to("professor/activities/{$activity_id}");
  1012. }
  1013. }
  1014. }