Geen omschrijving

OutcomesController.php 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. <?php
  2. use Illuminate\Database\Eloquent\Collection;
  3. class OutcomesController extends \BaseController {
  4. /**
  5. * Show all Learning Outcomes and expected values
  6. *
  7. */
  8. public function index()
  9. {
  10. $title = "Learning Outcomes";
  11. $outcomes = Outcome::withTrashed()->orderBy('name', 'ASC')->get();
  12. $schools = School::orderBy('name', 'ASC')->get();
  13. // $semesters_ids = Session::get('semesters_ids');
  14. // $semesters = Semester::whereIn('id',$semesters_ids)->get();
  15. return View::make('local.managers.admins.learning-outcomes', compact('title', 'outcomes', 'schools'));
  16. //return View::make('local.managers.admins.learning-outcomes', compact('title', 'outcomes', 'schools','semesters'));
  17. }
  18. // TODO: Change to home page
  19. public function newIndex()
  20. {
  21. $title = "Learning Outcomes";
  22. // TODO: Check when semester doesnt exist or session is empty
  23. $selected_semester = Semester::find(Session::get('semesters_ids')[0]);
  24. $outcomes = Outcome::withTrashed()->where('deactivation_date', '>=', $selected_semester->start)->orWhere('deactivation_date', null)->orderBy('name', 'ASC')->get();
  25. $schools = School::orderBy('name', 'ASC')->get();
  26. return View::make('local.managers.admins.new-learning-outcomes', compact('title', 'outcomes', 'schools'));
  27. }
  28. public function show($id)
  29. {
  30. DB::disableQueryLog();
  31. $outcome = Outcome::find($id);
  32. $undergradResults=array("names"=>array(), "schools"=>array(), "achieved"=>array(), "attempted"=>array(), "successRate"=>array());
  33. $gradResults = array("names"=>array(), "schools"=>array(), "achieved"=>array(), "attempted"=>array(), "successRate"=>array());
  34. //Calculate performance for this outcome for each undergrad program
  35. $undergradPrograms = Program::where('is_graduate','=', 0)
  36. ->where(function($query)
  37. {
  38. if(Auth::user()->school_id)
  39. {
  40. $query->where('school_id', Auth::user()->school_id);
  41. }
  42. })
  43. ->with('courses')
  44. ->orderBy('name', 'asc')->get();
  45. foreach($undergradPrograms as $program)
  46. {
  47. $undergradResults["names"][$program->id]=$program->name;
  48. $undergradResults["schools"][$program->id]=$program->school->name;
  49. $programAssessed=false;
  50. $undergradResults["attempted"][$program->id]=0;
  51. $undergradResults["achieved"][$program->id]=0;
  52. foreach($program->courses as $course)
  53. {
  54. $course_outcomes_achieved = json_decode($course->outcomes_achieved, true);
  55. $course_outcomes_attempted = json_decode($course->outcomes_attempted, true);
  56. $attemptedCriteriaCount=0;
  57. $achievedCriteriaCount=0;
  58. // If this outcome was evaluated
  59. if(
  60. $course_outcomes_attempted
  61. && array_key_exists($outcome->id, $course_outcomes_attempted)
  62. && $course_outcomes_attempted[$outcome->id]!=0)
  63. {
  64. // Count +1 for attempted and achieved in the program
  65. $attemptedCriteriaCount+=$course_outcomes_attempted[$outcome->id];
  66. $achievedCriteriaCount+=$course_outcomes_achieved[$outcome->id];
  67. $programAssessed=true;
  68. if($attemptedCriteriaCount>0 &&(float)$achievedCriteriaCount/$attemptedCriteriaCount*100 > $outcome->expected_outcome)
  69. {
  70. $undergradResults["achieved"][$program->id]+=1;
  71. }
  72. $undergradResults["attempted"][$program->id]+=1;
  73. }
  74. }
  75. // Calculate success rate for this program
  76. if($programAssessed && $undergradResults["attempted"][$program->id]>0)
  77. $undergradResults["successRate"][$program->id]= round((float)$undergradResults["achieved"][$program->id]/$undergradResults["attempted"][$program->id]*100, 2).'%';
  78. else
  79. $undergradResults["successRate"][$program->id]= 'N/M';
  80. }
  81. //Calculate performance for this outcome for each grad program
  82. $gradPrograms = Program::where('is_graduate','=', 1)
  83. ->where(function($query)
  84. {
  85. if(Auth::user()->school_id)
  86. {
  87. $query->where('school_id', Auth::user()->school_id);
  88. }
  89. })
  90. ->with(array('courses'=>function($query)
  91. {
  92. $query->whereNotNull('outcomes_attempted');
  93. }))
  94. ->orderBy('name', 'asc')->get();
  95. foreach($gradPrograms as $program)
  96. {
  97. $gradResults["names"][$program->id]=$program->name;
  98. $gradResults["schools"][$program->id]=$program->school->name;
  99. $programAssessed=false;
  100. $gradResults["attempted"][$program->id]=0;
  101. $gradResults["achieved"][$program->id]=0;
  102. foreach($program->courses as $course)
  103. {
  104. $course_outcomes_achieved = json_decode($course->outcomes_achieved, true);
  105. $course_outcomes_attempted = json_decode($course->outcomes_attempted, true);
  106. $attemptedCriteriaCount=0;
  107. $achievedCriteriaCount=0;
  108. // If this outcome was evaluated
  109. if(
  110. $course_outcomes_attempted
  111. && array_key_exists($outcome->id, $course_outcomes_attempted)
  112. && $course_outcomes_attempted[$outcome->id]!=0)
  113. {
  114. // Count +1 for attempted and achieved in the program
  115. $attemptedCriteriaCount+=$course_outcomes_attempted[$outcome->id];
  116. $achievedCriteriaCount+=$course_outcomes_achieved[$outcome->id];
  117. $programAssessed=true;
  118. if($attemptedCriteriaCount>0 &&(float)$achievedCriteriaCount/$attemptedCriteriaCount*100 > $outcome->expected_outcome)
  119. {
  120. $gradResults["achieved"][$program->id]+=1;
  121. }
  122. $gradResults["attempted"][$program->id]+=1;
  123. }
  124. }
  125. // Calculate success rate for this program
  126. if($programAssessed && $gradResults["attempted"][$program->id]>0)
  127. $gradResults["successRate"][$program->id]= round((float)$gradResults["achieved"][$program->id]/$gradResults["attempted"][$program->id]*100, 2).'%';
  128. else
  129. $gradResults["successRate"][$program->id]= 'N/M';
  130. }
  131. $title = "Outcome Results: ".$outcome->name;
  132. return View::make('local.managers.admins.learning-outcome', compact('title', 'outcome', 'undergradResults', 'gradResults'));
  133. }
  134. // TODO: Clean up and verify relationships are correct
  135. public function newShow($id)
  136. {
  137. // DB::disableQueryLog();
  138. // $outcome = null;
  139. if ($id === 'all') {
  140. $outcome = Outcome::with('objectives.criteria')->get();
  141. $title = 'All Outcomes';
  142. $criteria = $outcome->reduce(function($carry, $outcome) {
  143. return $carry->merge($outcome->criteria);
  144. }, Collection::make([]));
  145. $report_link = URL::action('OutcomesController@newReportAll');
  146. } else {
  147. $outcome = Outcome::with(['objectives.criteria'])->find($id);
  148. $title = $outcome->name;
  149. $criteria = $outcome->criteria->load('rubrics');
  150. $report_link = URL::action('OutcomesController@newReport', ['id' => $outcome->id]);
  151. }
  152. // $objectives = $outcome->objectives;
  153. // var_dump(get_class_methods($criteria));
  154. // var_dump($criteria);
  155. $rubrics = $criteria->reduce(function($carry, $crit) {
  156. return $carry->merge($crit->rubrics);
  157. }, Collection::make([]))->load('activities');
  158. $activities = $rubrics->reduce(function($carry, $rubric) {
  159. return $carry->merge($rubric->activities);
  160. }, Collection::make([]));
  161. $courses = $activities->reduce(function($carry, $activity) {
  162. if ($activity->course !== null) {
  163. $carry->push($activity->course);
  164. }
  165. return $carry;
  166. }, Collection::make([]));
  167. $activities = $activities->filter(function($activity) {
  168. return ($activity->course === null);
  169. });
  170. // var_dump(DB::getQueryLog());
  171. return View::make('local.managers.admins.new-learning-outcome', compact('title', 'outcome', 'courses', 'activities', 'report_link'));
  172. }
  173. public function newReport($id)
  174. {
  175. $outcome = Outcome::find($id);
  176. $objectives = $outcome->objectives;
  177. $criteria = $outcome->criteria;
  178. $programs = $objectives->map(function ($objective) { return $objective->program; })
  179. ->merge($criteria->map(function ($criteria) { return $criteria->program; }))
  180. ->filter(function ($program) { return $program->users->contains(Auth::user()); });
  181. $title = $outcome->name . ' Report';
  182. return View::make('local.managers.admins.new-report', compact('title', 'outcome', 'objectives'));
  183. }
  184. public function newReportAll()
  185. {
  186. $outcomes = Outcome::with('objectives')->get();
  187. $title = 'All Outcomes Report';
  188. return View::make('local.managers.admins.new-report-all', compact('title', 'outcomes'));
  189. }
  190. public function update()
  191. {
  192. $outcomeArray = json_decode(Input::get('outcomeArray'), true);
  193. Session::flash('status', 'success');
  194. Session::flash('message', 'Learning Outcomes updated.');
  195. foreach ($outcomeArray as $outcomeObject)
  196. {
  197. $validator = Validator::make(
  198. array(
  199. 'name' => $outcomeObject['name'],
  200. 'definition' => $outcomeObject['definition'],
  201. 'expected_outcome' => $outcomeObject['expected_outcome']
  202. ),
  203. array(
  204. 'name' => 'required',
  205. 'definition' => 'required',
  206. 'expected_outcome' => 'required|numeric'
  207. )
  208. );
  209. if(!$validator->fails())
  210. {
  211. try
  212. {
  213. $outcome = Outcome::withTrashed()
  214. ->where('id','=', $outcomeObject['id'])
  215. ->firstOrFail();
  216. $outcome->name = $outcomeObject['name'];
  217. $outcome->definition = $outcomeObject['definition'];
  218. $outcome->expected_outcome = $outcomeObject['expected_outcome'];
  219. $outcome->save();
  220. // If delete is 1, and outcome isn't already trashed, delete
  221. if($outcomeObject['delete']==1 && !$outcome->trashed())
  222. $outcome->delete();
  223. // If delete is 0, and outcome is already trashed, restore
  224. elseif($outcomeObject['delete']==0 && $outcome->trashed())
  225. $outcome->restore();
  226. }
  227. catch(Exception $e)
  228. {
  229. Session::flash('message', $e->getMessage());
  230. }
  231. }
  232. else
  233. {
  234. /** Prepare error message */
  235. $message = 'Error(s) updating the Learning Outcomes: <ul>';
  236. foreach ($validator->messages()->all('<li>:message</li>') as $validationError)
  237. {
  238. $message.=$validationError;
  239. }
  240. $message.='</ul>';
  241. /** Send error message and old data */
  242. Session::flash('status', 'danger');
  243. Session::flash('message', $message);
  244. return;
  245. }
  246. }
  247. return;
  248. }
  249. /**
  250. *Copy of update(), but also updates activation_date, deactivation_date and level
  251. */
  252. public function updateMore()
  253. {
  254. $outcomeArray = json_decode(Input::get('outcomeArray'), true);
  255. Session::flash('status', 'success');
  256. Session::flash('message', 'Learning Outcomes updated.');
  257. foreach ($outcomeArray as $outcomeObject)
  258. {
  259. $validator = Validator::make(
  260. array(
  261. 'name' => $outcomeObject['name'],
  262. 'definition' => $outcomeObject['definition'],
  263. 'expected_outcome' => $outcomeObject['expected_outcome']
  264. // TODO- validar los otros 3 valores
  265. ),
  266. array(
  267. 'name' => 'required',
  268. 'definition' => 'required',
  269. 'expected_outcome' => 'required|numeric'
  270. // TODO- los requisitos de los otros 3 valores
  271. )
  272. );
  273. if(!$validator->fails())
  274. {
  275. try
  276. {
  277. $outcome = Outcome::withTrashed()
  278. ->where('id','=', $outcomeObject['id'])
  279. ->firstOrFail();
  280. $outcome->name = $outcomeObject['name'];
  281. $outcome->definition = $outcomeObject['definition'];
  282. $outcome->expected_outcome = $outcomeObject['expected_outcome'];
  283. $outcome->activation_date = $outcomeObject['activation_date'];
  284. $outcome->deactivation_date = $outcomeObject['deactivation_date'];
  285. $outcome->level = $outcomeObject['level'];
  286. $outcome->save();
  287. // If delete is 1, and outcome isn't already trashed, delete
  288. if($outcomeObject['delete']==1 && !$outcome->trashed())
  289. $outcome->delete();
  290. // If delete is 0, and outcome is already trashed, restore
  291. elseif($outcomeObject['delete']==0 && $outcome->trashed())
  292. $outcome->restore();
  293. }
  294. catch(Exception $e)
  295. {
  296. Session::flash('message', $e->getMessage());
  297. }
  298. }
  299. else
  300. {
  301. /** Prepare error message */
  302. $message = 'Error(s) updating the Learning Outcomes: <ul>';
  303. foreach ($validator->messages()->all('<li>:message</li>') as $validationError)
  304. {
  305. $message.=$validationError;
  306. }
  307. $message.='</ul>';
  308. /** Send error message and old data */
  309. Session::flash('status', 'danger');
  310. Session::flash('message', $message);
  311. return;
  312. }
  313. }
  314. return;
  315. }
  316. public function fetchCriteria()
  317. {
  318. if(Input::get('filter'))
  319. {
  320. switch (Input::get('filter'))
  321. {
  322. case 'all':
  323. return Outcome::find(Input::get('id'))->criteria;
  324. break;
  325. case 'school':
  326. // If scoord
  327. if(Auth::user()->role == '2')
  328. {
  329. // Fetch all the programs whose school is the user's
  330. $program_ids = DB::table('programs')->where('school_id', Auth::user()->school_id)->lists('id');
  331. // Return all criteria belonging to any of those programs
  332. return Criterion::
  333. where('outcome_id', Input::get('id'))
  334. ->whereIn('program_id', $program_ids)
  335. ->orderBy('name', 'ASC')
  336. ->get();
  337. }
  338. // If pcoord
  339. else
  340. {
  341. // Fetch all the programs from the user's school;
  342. $program_ids = DB::table('programs')->where('school_id', Auth::user()->programs[0]->school->id)->lists('id');
  343. return Criterion::
  344. where('outcome_id', Input::get('id'))
  345. ->whereIn('program_id', $program_ids)
  346. ->orderBy('name', 'ASC')
  347. ->get();
  348. }
  349. break;
  350. case 'program':
  351. return Criterion::
  352. where('outcome_id', Input::get('id'))
  353. ->whereIn('program_id', Auth::user()->programs->lists('id'))
  354. ->orderBy('name', 'ASC')
  355. ->get();
  356. break;
  357. default:
  358. return Outcome::find(Input::get('id'))->criteria;
  359. break;
  360. }
  361. }
  362. else
  363. {
  364. return Outcome::find(Input::get('id'))->criteria;
  365. }
  366. }
  367. /**
  368. * Create a new learning outcome.
  369. */
  370. public function create()
  371. {
  372. /** Validation rules */
  373. $validator = Validator::make(
  374. array(
  375. 'name' => Input::get('name'),
  376. 'definition' => Input::get('definition')
  377. ),
  378. array(
  379. 'name' => 'required|unique:outcomes',
  380. 'definition' => 'required|min:10'
  381. )
  382. );
  383. /** If validation fails */
  384. if ($validator->fails())
  385. {
  386. /** Prepare error message */
  387. $message = '<p>Error(s) creating a new Learning Outcome</p><ul>';
  388. foreach ($validator->messages()->all('<li>:message</li>') as $validationError)
  389. {
  390. $message.=$validationError;
  391. }
  392. $message.='</ul>';
  393. /** Send error message and old data */
  394. Session::flash('status', 'warning');
  395. Session::flash('message', $message);
  396. return Redirect::to('learning-outcomes')->withInput();
  397. }
  398. else
  399. {
  400. /** Instantiate new outcome */
  401. $outcome = new Outcome;
  402. $outcome->name= Input::get('name');
  403. $outcome->definition = Input::get('definition');
  404. /** If outcome is saved, send success message */
  405. if($outcome->save())
  406. {
  407. Session::flash('status', 'success');
  408. Session::flash('message', '<p>Learning Outcome added.</p>');
  409. return Redirect::to('learning-outcomes');
  410. }
  411. /** If saving fails, send error message and old data */
  412. else
  413. {
  414. Session::flash('status', 'warning');
  415. Session::flash('message', '<p>Error adding Learning Outcome. Please try again later.</p>');
  416. return Redirect::to('learning-outcomes')->withInput();
  417. }
  418. }
  419. }
  420. public function fetchOutcome()
  421. {
  422. $id = Input::get('id');
  423. $outcome = Outcome::find($id);
  424. $outcome->criteria;
  425. return array
  426. (
  427. 'outcome' => $outcome
  428. );
  429. }
  430. public function managerAssessmentReports()
  431. {
  432. $outcomes = Outcome::select(array('id', 'name', 'expected_outcome'))->orderBy('name', 'ASC')->get();
  433. switch (Auth::user()->role) {
  434. case 1:
  435. $title = "Campus Assessment Reports";
  436. return View::make('local.managers.admins.assessment_reports', compact('title', 'outcomes'));
  437. break;
  438. case 2:
  439. $title = "School Assessment Reports";
  440. return View::make('local.managers.sCoords.assessment_reports', compact('title', 'outcomes'));
  441. break;
  442. case 3:
  443. $title = "Program Assessment Reports";
  444. $programs = Auth::user()->programs;
  445. return View::make('local.managers.pCoords.assessment_reports', compact('title', 'outcomes', 'programs'));
  446. break;
  447. default:
  448. App::abort('404');
  449. break;
  450. }
  451. }
  452. /**
  453. * Campus Assessment Reports
  454. */
  455. public function assessmentReport($outcome_id)
  456. {
  457. $outcome = Outcome::find($outcome_id);
  458. if(!$outcome)
  459. App::abort('404');
  460. $title = "Assessment Report: ".$outcome->name;
  461. $schools = School::
  462. has('courses')
  463. ->with(array('programs'=>function($query) use($outcome_id){
  464. $query
  465. ->has('courses')
  466. ->with(array('courses'=>function($query2) use($outcome_id){
  467. $query2
  468. ->has('activities')
  469. ->whereNotNull('outcomes_attempted')
  470. // ->where('outcomes_attempted', 'NOT LIKE', '%"'.$outcome_id.'":0%')
  471. ->whereIn('semester_id', Session::get('semesters_ids'))
  472. ->groupBy(array('code', 'number'));
  473. }));
  474. }))
  475. ->get();
  476. return View::make('local.managers.admins.assessment_report', compact('title', 'outcome', 'schools'));
  477. }
  478. // TODO: Change later
  479. public function newAssessmentReport($outcome_id)
  480. {
  481. $outcome = Outcome::find($outcome_id);
  482. if(!$outcome)
  483. App::abort('404');
  484. $title = "Assessment Report: ".$outcome->name;
  485. $schools = School::
  486. has('courses')
  487. ->with(array('programs'=>function($query) use($outcome_id){
  488. $query
  489. ->has('courses')
  490. ->with(array('courses'=>function($query2) use($outcome_id){
  491. $query2
  492. ->has('activities')
  493. ->whereNotNull('outcomes_attempted')
  494. // ->where('outcomes_attempted', 'NOT LIKE', '%"'.$outcome_id.'":0%')
  495. ->whereIn('semester_id', Session::get('semesters_ids'))
  496. ->groupBy(array('code', 'number'));
  497. }));
  498. }))
  499. ->get();
  500. return View::make('local.managers.admins.assessment_report', compact('title', 'outcome', 'schools'));
  501. }
  502. /**
  503. * School Assessment Reports
  504. */
  505. public function schoolAssessmentReport($outcome_id)
  506. {
  507. $outcome = Outcome::find($outcome_id);
  508. if(!$outcome)
  509. App::abort('404');
  510. $title = "Assessment Report: ".$outcome->name;
  511. $school = School::
  512. where('id', Auth::user()->school_id)
  513. ->has('courses')
  514. ->with(array('programs'=>function($query){
  515. $query
  516. ->has('courses')
  517. ->with(array('courses'=>function($query2){
  518. $query2
  519. ->has('activities')
  520. ->whereNotNull('outcomes_attempted')
  521. ->whereIn('semester_id', Session::get('semesters_ids'))
  522. ->groupBy(array('code', 'number'));
  523. }));
  524. }))
  525. ->first();
  526. return View::make('local.managers.sCoords.assessment_report', compact('title', 'outcome', 'school'));
  527. }
  528. /**
  529. * Program Assessment Reports
  530. */
  531. public function programAssessmentReport($outcome_id, $program_id)
  532. {
  533. $outcome = Outcome::find($outcome_id);
  534. if(!$outcome)
  535. App::abort('404');
  536. $title = "Assessment Report: ".$outcome->name;
  537. $program = Program::
  538. where('id', $program_id)
  539. ->has('courses')
  540. ->with(array('courses'=>function($query){
  541. $query
  542. ->has('activities')
  543. ->whereNotNull('outcomes_attempted')
  544. ->whereIn('semester_id', Session::get('semesters_ids'))
  545. ->groupBy(array('code', 'number'));
  546. }))
  547. ->first();
  548. return View::make('local.managers.pCoords.assessment_report', compact('title', 'outcome', 'program'));
  549. }
  550. public function professorAssessmentReports()
  551. {
  552. $outcomes = Outcome::select(array('id', 'name', 'expected_outcome'))->orderBy('name', 'ASC')->get();
  553. $title = "My Courses' Assessment Reports";
  554. return View::make('local.professors.assessment_reports', compact('title', 'outcomes'));
  555. }
  556. // Report for a single professor with a single learning outcome
  557. public function professorAssessmentReport($outcome_id)
  558. {
  559. $outcome = Outcome::find($outcome_id);
  560. if(!$outcome)
  561. App::abort('404');
  562. $title = "My Courses' Assessment Report: ".$outcome->name;
  563. $courses = Course::
  564. where('user_id', Auth::user()->id)
  565. ->has('activities')
  566. ->whereNotNull('outcomes_attempted')
  567. ->whereIn('semester_id', Session::get('semesters_ids'))
  568. ->groupBy(array('code', 'number'))
  569. ->get();
  570. return View::make('local.professors.assessment_report', compact('title', 'outcome', 'courses'));
  571. }
  572. }