暫無描述

Course.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. class Course extends Eloquent
  3. {
  4. protected $fillable = array('name', 'code', 'number', 'section', 'program_id', 'user_id', 'semester_id');
  5. public function program()
  6. {
  7. return $this->belongsTo('Program');
  8. }
  9. public function students()
  10. {
  11. return $this->belongsToMany('Student')->orderBy('name', 'asc')->withTimestamps();
  12. }
  13. public function user()
  14. {
  15. return $this->belongsTo('User')->orderBy('surnames', 'asc')->orderBy('first_name', 'asc');
  16. }
  17. // TODO: Remove
  18. public function activities()
  19. {
  20. return $this->hasMany('Activity')->orderBy('date', 'asc');
  21. }
  22. public function activity()
  23. {
  24. return $this->belongsTo('Activity');
  25. }
  26. public function assessedActivities()
  27. {
  28. // return $this->hasMany('Activity')->whereNotNull('activities.outcomes_attempted')->orderBy('date', 'asc');
  29. $activitiesAssessed = DB::table('activities')
  30. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  31. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  32. ->where('activities.course_id', $this->id)
  33. ->select('activities.id')
  34. ->distinct()
  35. ->lists('activities.id');
  36. Log::info($activitiesAssessed);
  37. return $this->hasMany('Activity')->whereIn('activities.id', $activitiesAssessed)->orderBy('date', 'asc');
  38. // return $this->hasMany('Activity')->where('activities.draft', 0)->orderBy('date', 'asc');
  39. }
  40. public function publishedActivities()
  41. {
  42. $activitiesAssessed = DB::table('activities')
  43. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  44. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  45. ->where('activities.course_id', $this->id)
  46. ->where('activities.draft', 0)
  47. ->select('activities.id')
  48. ->distinct()
  49. ->lists('activities.id');
  50. //return $this->hasMany('Activity')->whereNotNull('activities.outcomes_attempted')->where('activities.draft', 0)->orderBy('date', 'asc');
  51. return $this->hasMany('Activity')->whereIn('activities.id', $activitiesAssessed)->orderBy('date', 'asc');
  52. }
  53. public function outcomes_attempted()
  54. {
  55. return DB::table('courses')
  56. ->join('activities', 'activities.course_id', '=', 'courses.id')
  57. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  58. ->join('criteria', 'activity_criterion.criterion_id', '=', 'criteria.id')
  59. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  60. ->where('courses.id', '=', $this->id)
  61. ->select('criterion_objective_outcome.outcome_id')
  62. ->distinct()
  63. ->orderBy('criterion_objective_outcome.outcome_id')
  64. ->get();
  65. // SELECT distinct cr.outcome_id FROM courses c, activities a, activity_criterion ac, criteria cr WHERE c.id=a.course_id and a.id=ac.activity_id and ac.criterion_id=cr.id and c.id=1
  66. // return $this->id;
  67. }
  68. public function outcomes_att()
  69. {
  70. $criteria_attempted = DB::table('courses')
  71. ->join('activities', 'activities.course_id', '=', 'courses.id')
  72. ->join('activity_criterion', 'activities.id', '=', 'activity_criterion.activity_id')
  73. ->select('criterion_id', 'activity_criterion.id as activity_criterion_id')
  74. ->where('courses.id', $this->id)
  75. ->where('draft', 0)
  76. ->get();
  77. // Old data was formed like { "outcome1": cuantity_of_criteria_associated, "outcome2":cuantity_of_criteria}
  78. $outcomes_attempted = [];
  79. foreach ($criteria_attempted as $criterion) {
  80. $outcomes = DB::table('criterion_objective_outcome')
  81. ->where('criterion_id', $criterion->criterion_id)
  82. ->select('outcome_id')
  83. ->distinct()
  84. ->lists('outcome_id');
  85. foreach ($outcomes as $outcome_id) {
  86. if (array_key_exists($outcome_id, $outcomes_attempted)) $outcomes_attempted[$outcome_id] += 1;
  87. else $outcomes_attempted[$outcome_id] = 1;
  88. }
  89. }
  90. return $outcomes_attempted;
  91. }
  92. public function getReportObject()
  93. {
  94. $reportObject = new stdClass();
  95. $reportObject->criteriaReport = DB::table('courses')
  96. ->join('activities', 'activities.course_id', '=', 'courses.id')
  97. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  98. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  99. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  100. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  101. ->join('assessments', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  102. ->select(DB::raw(
  103. "count(assessments.score) as assessed,
  104. count(case when assessments.score >= rubrics.expected_points then 1 else NULL end) as achieved,
  105. criteria.*"
  106. ))
  107. ->where('courses.id', $this->id)
  108. ->groupBy('criteria.id')
  109. ->get();
  110. Log::info($this->id);
  111. $reportObject->outcomesReport = DB::table('courses')
  112. ->join('activities', 'activities.course_id', '=', 'courses.id')
  113. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  114. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  115. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  116. ->join('assessments', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  117. ->join('criterion_objective_outcome', 'activity_criterion.criterion_id', '=', 'criterion_objective_outcome.criterion_id')
  118. ->join('outcomes', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id')
  119. ->select(
  120. DB::raw(
  121. "student_id, outcomes.name, outcomes.id,
  122. count(activity_criterion.criterion_id) as attempted,
  123. count(case when assessments.score >= rubrics.expected_points then 1 else null end) as achieved"
  124. )
  125. )
  126. ->where('courses.id', $this->id)
  127. ->groupBy(array('outcome_id', 'student_id'))
  128. ->get();
  129. return $reportObject;
  130. }
  131. public function outcomes_ach()
  132. {
  133. $criteria = DB::table('criteria')
  134. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  135. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  136. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  137. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  138. ->join('courses', 'activities.course_id', '=', 'courses.id')
  139. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  140. ->where('courses.id', '=', $this->id)
  141. ->where('activities.draft', 0)
  142. ->select(
  143. 'criteria.id',
  144. 'rubrics.expected_percentage as expected_percentage_students_achieving'/*'expected_percentage_students_achieving',*/,
  145. 'activity_criterion.activity_id'
  146. )
  147. ->distinct()
  148. ->get();
  149. // Old data was formed like { "outcome1": cuantity_of_criteria_associated_passed, "outcome2":cuantity_of_criteria}
  150. $outcomes_achieved = [];
  151. foreach ($criteria as $criterion) {
  152. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  153. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  154. Log::info('estamos aqui');
  155. Log::info($students_attempted);
  156. if ($students_attempted) {
  157. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  158. } else {
  159. $percentage_students_who_achieved = 0;
  160. }
  161. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  162. $outcomes = DB::table('criterion_objective_outcome')
  163. ->where('criterion_id', $criterion->id)
  164. ->select('outcome_id')
  165. ->distinct()
  166. ->lists('outcome_id');
  167. foreach ($outcomes as $outcome_id) {
  168. if (array_key_exists($outcome_id, $outcomes_achieved)) $outcomes_achieved[$outcome_id] += 1;
  169. else $outcomes_achieved[$outcome_id] = 1;
  170. }
  171. }
  172. }
  173. Log::info(
  174. "OUTCOMES_ACHIEVED: "
  175. );
  176. Log::info(
  177. $outcomes_achieved
  178. );
  179. return $outcomes_achieved;
  180. }
  181. public function someSectionAssessed()
  182. {
  183. $all_sections = DB::table('courses')
  184. ->join('activities', 'activities.course_id', '=', 'courses.id')
  185. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  186. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  187. ->where('courses.name', $this->name)
  188. ->where('code', $this->code)
  189. ->where('number', $this->number)
  190. ->where('semester_id', $this->semester_id)
  191. ->where(
  192. 'program_id',
  193. $this->program_id
  194. )
  195. ->where('activities.draft', 0)
  196. ->groupBy('courses.id')
  197. ->count();
  198. return $all_sections;
  199. }
  200. public function outcomes_achieved()
  201. {
  202. $criteria = DB::table('criteria')
  203. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  204. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  205. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  206. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  207. ->join('courses', 'activities.course_id', '=', 'courses.id')
  208. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  209. ->where('courses.id', '=', $this->id)
  210. ->select(
  211. 'criteria.id',
  212. 'rubrics.expected_percentage as expected_percentage_students_achieving'/*'expected_percentage_students_achieving',*/,
  213. 'activity_criterion.activity_id'
  214. )
  215. ->distinct()
  216. ->get();
  217. $conteo = 0;
  218. foreach ($criteria as $criterion) {
  219. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  220. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  221. if ($students_attempted) {
  222. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  223. } else {
  224. $percentage_students_who_achieved = 0;
  225. }
  226. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  227. $conteo++;
  228. }
  229. }
  230. return $conteo;
  231. }
  232. public function semester()
  233. {
  234. return $this->belongsTo('Semester');
  235. }
  236. public function sectionsWithOutcome($outcome_id)
  237. {
  238. return Course::has('activities')->with(array('activities' => function ($query) use (&$outcome_id) {
  239. $query->where('outcomes_attempted', 'not like', '\'%"' . $outcome_id . '":0%\'');
  240. }))->where('code', $this->code)->where('number', $this->number)->get();
  241. }
  242. }