Ingen beskrivning

Course.php 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 outcomes_ach()
  93. {
  94. $criteria = DB::table('criteria')
  95. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  96. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  97. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  98. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  99. ->join('courses', 'activities.course_id', '=', 'courses.id')
  100. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  101. ->where('courses.id', '=', $this->id)
  102. ->where('activities.draft', 0)
  103. ->select(
  104. 'criteria.id',
  105. 'rubrics.expected_percentage as expected_percentage_students_achieving'/*'expected_percentage_students_achieving',*/,
  106. 'activity_criterion.activity_id'
  107. )
  108. ->distinct()
  109. ->get();
  110. // Old data was formed like { "outcome1": cuantity_of_criteria_associated_passed, "outcome2":cuantity_of_criteria}
  111. $outcomes_achieved = [];
  112. foreach ($criteria as $criterion) {
  113. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  114. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  115. if ($students_attempted) {
  116. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  117. } else {
  118. $percentage_students_who_achieved = 0;
  119. }
  120. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  121. $outcomes = DB::table('criterion_objective_outcome')
  122. ->where('criterion_id', $criterion->id)
  123. ->select('outcome_id')
  124. ->distinct()
  125. ->lists('outcome_id');
  126. foreach ($outcomes as $outcome_id) {
  127. if (array_key_exists($outcome_id, $outcomes_achieved)) $outcomes_achieved[$outcome_id] += 1;
  128. else $outcomes_achieved[$outcome_id] = 1;
  129. }
  130. }
  131. }
  132. return $outcomes_achieved;
  133. }
  134. public function someSectionAssessed()
  135. {
  136. $all_sections = DB::table('courses')
  137. ->join('activities', 'activities.course_id', '=', 'courses.id')
  138. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  139. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  140. ->where('courses.name', $this->name)
  141. ->where('code', $this->code)
  142. ->where('number', $this->number)
  143. ->where('semester_id', $this->semester_id)
  144. ->where(
  145. 'program_id',
  146. $this->program_id
  147. )
  148. ->where('activities.draft', 0)
  149. ->groupBy('courses.id')
  150. ->count();
  151. return $all_sections;
  152. }
  153. public function outcomes_achieved()
  154. {
  155. $criteria = DB::table('criteria')
  156. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  157. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  158. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  159. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  160. ->join('courses', 'activities.course_id', '=', 'courses.id')
  161. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  162. ->where('courses.id', '=', $this->id)
  163. ->select(
  164. 'criteria.id',
  165. 'rubrics.expected_percentage as expected_percentage_students_achieving'/*'expected_percentage_students_achieving',*/,
  166. 'activity_criterion.activity_id'
  167. )
  168. ->distinct()
  169. ->get();
  170. $conteo = 0;
  171. foreach ($criteria as $criterion) {
  172. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  173. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  174. if ($students_attempted) {
  175. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  176. } else {
  177. $percentage_students_who_achieved = 0;
  178. }
  179. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  180. $conteo++;
  181. }
  182. }
  183. return $conteo;
  184. }
  185. public function semester()
  186. {
  187. return $this->belongsTo('Semester');
  188. }
  189. public function sectionsWithOutcome($outcome_id)
  190. {
  191. return Course::has('activities')->with(array('activities' => function ($query) use (&$outcome_id) {
  192. $query->where('outcomes_attempted', 'not like', '\'%"' . $outcome_id . '":0%\'');
  193. }))->where('code', $this->code)->where('number', $this->number)->get();
  194. }
  195. }