Ei kuvausta

OutcomesController.php 24KB

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