No Description

OutcomesController.php 22KB

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