Nav apraksta

OutcomesController.php 21KB

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