Ei kuvausta

Course.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. }
  30. public function publishedActivities()
  31. {
  32. return $this->hasMany('Activity')->whereNotNull('activities.outcomes_attempted')->where('activities.draft', 0)->orderBy('date', 'asc');
  33. }
  34. public function outcomes_attempted()
  35. {
  36. return DB::table('courses')
  37. ->join('activities', 'activities.course_id', '=', 'courses.id')
  38. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  39. ->join('criteria', 'activity_criterion.criterion_id', '=', 'criteria.id')
  40. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  41. ->where('courses.id', '=', $this->id)
  42. ->select('criterion_objective_outcome.outcome_id')
  43. ->distinct()
  44. ->orderBy('criterion_objective_outcome.outcome_id')
  45. ->get();
  46. // 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
  47. // return $this->id;
  48. }
  49. public function outcomes_achieved()
  50. {
  51. $criteria = DB::table('criteria')
  52. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  53. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  54. ->join('courses', 'activities.course_id', '=', 'courses.id')
  55. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  56. ->where('courses.id', '=', $this->id)
  57. ->select('criteria.id', 'expected_percentage_students_achieving', 'activity_criterion.activity_id')
  58. ->distinct()
  59. ->get();
  60. $conteo = 0;
  61. foreach ($criteria as $criterion) {
  62. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  63. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  64. if ($students_attempted) {
  65. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  66. } else {
  67. $percentage_students_who_achieved = 0;
  68. }
  69. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  70. $conteo++;
  71. }
  72. }
  73. return $conteo;
  74. }
  75. public function semester()
  76. {
  77. return $this->belongsTo('Semester');
  78. }
  79. public function sectionsWithOutcome($outcome_id)
  80. {
  81. return Course::has('activities')->with(array('activities' => function ($query) use (&$outcome_id) {
  82. $query->where('outcomes_attempted', 'not like', '\'%"' . $outcome_id . '":0%\'');
  83. }))->where('code', $this->code)->where('number', $this->number)->get();
  84. }
  85. }