No Description

OutcomesController.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. <?php
  2. class OutcomesController extends \BaseController {
  3. /**
  4. * Show all Learning Outcomes and expected values
  5. *
  6. */
  7. public function index()
  8. {
  9. $title = "Learning Outcomes";
  10. $outcomes = Outcome::withTrashed()->orderBy('name', 'ASC')->get();
  11. $schools = School::orderBy('name', 'ASC')->get();
  12. return View::make('local.managers.admins.learning-outcomes', compact('title', 'outcomes', 'schools'));
  13. }
  14. // TODO: Change to home page
  15. public function newIndex()
  16. {
  17. $title = "Learning Outcomes";
  18. $outcomes = Outcome::withTrashed()->orderBy('name', 'ASC')->get();
  19. $schools = School::orderBy('name', 'ASC')->get();
  20. return View::make('local.managers.admins.new-learning-outcomes', compact('title', 'outcomes', 'schools'));
  21. // return View::make('local.managers.admins.learning-outcomes', compact('title', 'outcomes', 'schools'));
  22. }
  23. public function show($id)
  24. {
  25. DB::disableQueryLog();
  26. $outcome = Outcome::find($id);
  27. $undergradResults=array("names"=>array(), "schools"=>array(), "achieved"=>array(), "attempted"=>array(), "successRate"=>array());
  28. $gradResults = array("names"=>array(), "schools"=>array(), "achieved"=>array(), "attempted"=>array(), "successRate"=>array());
  29. //Calculate performance for this outcome for each undergrad program
  30. $undergradPrograms = Program::where('is_graduate','=', 0)
  31. ->where(function($query)
  32. {
  33. if(Auth::user()->school_id)
  34. {
  35. $query->where('school_id', Auth::user()->school_id);
  36. }
  37. })
  38. ->with('courses')
  39. ->orderBy('name', 'asc')->get();
  40. foreach($undergradPrograms as $program)
  41. {
  42. $undergradResults["names"][$program->id]=$program->name;
  43. $undergradResults["schools"][$program->id]=$program->school->name;
  44. $programAssessed=false;
  45. $undergradResults["attempted"][$program->id]=0;
  46. $undergradResults["achieved"][$program->id]=0;
  47. foreach($program->courses as $course)
  48. {
  49. $course_outcomes_achieved = json_decode($course->outcomes_achieved, true);
  50. $course_outcomes_attempted = json_decode($course->outcomes_attempted, true);
  51. $attemptedCriteriaCount=0;
  52. $achievedCriteriaCount=0;
  53. // If this outcome was evaluated
  54. if(
  55. $course_outcomes_attempted
  56. && array_key_exists($outcome->id, $course_outcomes_attempted)
  57. && $course_outcomes_attempted[$outcome->id]!=0)
  58. {
  59. // Count +1 for attempted and achieved in the program
  60. $attemptedCriteriaCount+=$course_outcomes_attempted[$outcome->id];
  61. $achievedCriteriaCount+=$course_outcomes_achieved[$outcome->id];
  62. $programAssessed=true;
  63. if($attemptedCriteriaCount>0 &&(float)$achievedCriteriaCount/$attemptedCriteriaCount*100 > $outcome->expected_outcome)
  64. {
  65. $undergradResults["achieved"][$program->id]+=1;
  66. }
  67. $undergradResults["attempted"][$program->id]+=1;
  68. }
  69. }
  70. // Calculate success rate for this program
  71. if($programAssessed && $undergradResults["attempted"][$program->id]>0)
  72. $undergradResults["successRate"][$program->id]= round((float)$undergradResults["achieved"][$program->id]/$undergradResults["attempted"][$program->id]*100, 2).'%';
  73. else
  74. $undergradResults["successRate"][$program->id]= 'N/M';
  75. }
  76. //Calculate performance for this outcome for each grad program
  77. $gradPrograms = Program::where('is_graduate','=', 1)
  78. ->where(function($query)
  79. {
  80. if(Auth::user()->school_id)
  81. {
  82. $query->where('school_id', Auth::user()->school_id);
  83. }
  84. })
  85. ->with(array('courses'=>function($query)
  86. {
  87. $query->whereNotNull('outcomes_attempted');
  88. }))
  89. ->orderBy('name', 'asc')->get();
  90. foreach($gradPrograms as $program)
  91. {
  92. $gradResults["names"][$program->id]=$program->name;
  93. $gradResults["schools"][$program->id]=$program->school->name;
  94. $programAssessed=false;
  95. $gradResults["attempted"][$program->id]=0;
  96. $gradResults["achieved"][$program->id]=0;
  97. foreach($program->courses as $course)
  98. {
  99. $course_outcomes_achieved = json_decode($course->outcomes_achieved, true);
  100. $course_outcomes_attempted = json_decode($course->outcomes_attempted, true);
  101. $attemptedCriteriaCount=0;
  102. $achievedCriteriaCount=0;
  103. // If this outcome was evaluated
  104. if(
  105. $course_outcomes_attempted
  106. && array_key_exists($outcome->id, $course_outcomes_attempted)
  107. && $course_outcomes_attempted[$outcome->id]!=0)
  108. {
  109. // Count +1 for attempted and achieved in the program
  110. $attemptedCriteriaCount+=$course_outcomes_attempted[$outcome->id];
  111. $achievedCriteriaCount+=$course_outcomes_achieved[$outcome->id];
  112. $programAssessed=true;
  113. if($attemptedCriteriaCount>0 &&(float)$achievedCriteriaCount/$attemptedCriteriaCount*100 > $outcome->expected_outcome)
  114. {
  115. $gradResults["achieved"][$program->id]+=1;
  116. }
  117. $gradResults["attempted"][$program->id]+=1;
  118. }
  119. }
  120. // Calculate success rate for this program
  121. if($programAssessed && $gradResults["attempted"][$program->id]>0)
  122. $gradResults["successRate"][$program->id]= round((float)$gradResults["achieved"][$program->id]/$gradResults["attempted"][$program->id]*100, 2).'%';
  123. else
  124. $gradResults["successRate"][$program->id]= 'N/M';
  125. }
  126. $title = "Outcome Results: ".$outcome->name;
  127. return View::make('local.managers.admins.learning-outcome', compact('title', 'outcome', 'undergradResults', 'gradResults'));
  128. }
  129. // TODO: Clean up and verify relationships are correct
  130. public function newShow($id)
  131. {
  132. // DB::disableQueryLog();
  133. $outcome = Outcome::find($id);
  134. $title = $outcome->name;
  135. $objectives = $outcome->objectives;
  136. $criteria = $outcome->criteria;
  137. $programs = $objectives->map(function ($objective) { return $objective->program; })
  138. ->merge($criteria->map(function ($criteria) { return $criteria->program; }))
  139. ->filter(function ($program) { return $program->users->contains(Auth::user()); });
  140. $courses = $programs->reduce(function ($carry, $program) {
  141. return $carry->merge($program->courses);
  142. }, new \Illuminate\Database\Eloquent\Collection);
  143. $activities = $programs->reduce(function ($carry, $course) {
  144. return $carry->merge(Activity::where('course_id', '=', $course->id)->get());
  145. }, new \Illuminate\Database\Eloquent\Collection);
  146. return View::make('local.managers.admins.new-learning-outcome', compact('title', 'outcome', 'courses', 'activities'));
  147. }
  148. public function newShowAll()
  149. {
  150. // DB::disableQueryLog();
  151. $title = 'All Domains';
  152. $outcome = Outcome::with('objectives', 'criteria')->get();
  153. $objectives = $outcome->reduce(function ($carry, $outcome) {
  154. return $carry->merge($outcome->objectives);
  155. }, new \Illuminate\Database\Eloquent\Collection);
  156. $criteria = $outcome->reduce(function ($carry, $outcome) {
  157. return $carry->merge($outcome->criteria);
  158. }, new \Illuminate\Database\Eloquent\Collection);
  159. $programs = $objectives->map(function ($objective) { return $objective->program; })
  160. ->merge($criteria->map(function ($criteria) { return $criteria->program; }))
  161. ->filter(function ($program) { return $program->users->contains(Auth::user()); });
  162. $courses = $programs->reduce(function ($carry, $program) {
  163. return $carry->merge($program->courses);
  164. }, new \Illuminate\Database\Eloquent\Collection);
  165. $activities = $programs->reduce(function ($carry, $course) {
  166. return $carry->merge(Activity::where('course_id', '=', $course->id)->get());
  167. }, new \Illuminate\Database\Eloquent\Collection);
  168. return View::make('local.managers.admins.new-learning-outcome', compact('title', 'outcome', 'courses', 'activities'));
  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;
  179. $courses = $programs->reduce(function ($carry, $program) {
  180. return $carry->merge($program->courses);
  181. }, new \Illuminate\Database\Eloquent\Collection);
  182. $activities = $programs->reduce(function ($carry, $course) {
  183. return $carry->merge(Activity::where('course_id', '=', $course->id)->get());
  184. }, new \Illuminate\Database\Eloquent\Collection);
  185. return View::make('local.managers.admins.new-learning-outcome', compact('title', 'outcome', 'courses', 'activities'));
  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. }