Nessuna descrizione

Course.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. ->where('activities.diagnostic', 0)
  48. ->select('activities.id')
  49. ->distinct()
  50. ->lists('activities.id');
  51. //$this->hasMany('Activity')->whereNotNull('activities.outcomes_attempted')->where('activities.draft', 0)->orderBy('date', 'asc');
  52. return $this->hasMany('Activity')->whereIn('activities.id', $activitiesAssessed)->orderBy('date', 'asc');
  53. }
  54. public function outcomes_attempted()
  55. {
  56. return DB::table('courses')
  57. ->join('activities', 'activities.course_id', '=', 'courses.id')
  58. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  59. ->join('criteria', 'activity_criterion.criterion_id', '=', 'criteria.id')
  60. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  61. ->where('courses.id', '=', $this->id)
  62. ->select('criterion_objective_outcome.outcome_id')
  63. ->distinct()
  64. ->orderBy('criterion_objective_outcome.outcome_id')
  65. ->get();
  66. // 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
  67. // return $this->id;
  68. }
  69. public function outcomes_att()
  70. {
  71. $criteria_attempted = DB::table('courses')
  72. ->join('activities', 'activities.course_id', '=', 'courses.id')
  73. ->join('activity_criterion', 'activities.id', '=', 'activity_criterion.activity_id')
  74. ->select('criterion_id', 'activity_criterion.id as activity_criterion_id')
  75. ->where('courses.id', $this->id)
  76. ->where('draft', 0)
  77. ->where('activities.diagnostic', 0)
  78. ->get();
  79. // Old data was formed like { "outcome1": cuantity_of_criteria_associated, "outcome2":cuantity_of_criteria}
  80. $outcomes_attempted = [];
  81. foreach ($criteria_attempted as $criterion) {
  82. $outcomes = DB::table('criterion_objective_outcome')
  83. ->where('criterion_id', $criterion->criterion_id)
  84. ->select('outcome_id')
  85. ->distinct()
  86. ->lists('outcome_id');
  87. foreach ($outcomes as $outcome_id) {
  88. if (array_key_exists($outcome_id, $outcomes_attempted)) $outcomes_attempted[$outcome_id] += 1;
  89. else $outcomes_attempted[$outcome_id] = 1;
  90. }
  91. }
  92. Log::info("outcomes_attempted");
  93. Log::info($outcomes_attempted);
  94. return $outcomes_attempted;
  95. }
  96. public static function getCriteriaPlanReport($course_code = null)
  97. {
  98. if ($course_code) {
  99. $criteria_id = DB::table('annual_plan_objective')
  100. ->where('typ_semester_course_id', $course_code->typ_semester_course_id)
  101. ->lists('criteria_id');
  102. Log::info($course_code->typ_semester_course_id);
  103. Log::info("dame ese id");
  104. $program = Program::where('id', $course_code->program_id)->first();
  105. // Log::info($program->code);
  106. if ($program->code == "") {
  107. $program_ids = DB::table('programs')
  108. ->where('school_id', $program->school->id)
  109. ->lists('programs.id');
  110. //Log::info("estamos");
  111. } else $program_ids = array($program->id);
  112. /*tabla es asi por curso codigo
  113. *
  114. *criterioDelPlan EstudiantesDelProgramaQuePasaron EstudiantesqueIntentaron
  115. *
  116. * criterio1 3 5
  117. * criterio2 2 2
  118. *
  119. *
  120. */
  121. $table_per_criterion = DB::table('program_student_semester')
  122. ->join('course_student', 'program_student_semester.student_id', '=', 'course_student.student_id')
  123. ->join('courses', 'courses.id', '=', 'course_student.course_id')
  124. ->join('activities', 'activities.course_id', '=', 'courses.id')
  125. ->join('activity_criterion', 'activities.id', '=', 'activity_criterion.activity_id')
  126. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  127. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  128. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  129. ->join('assessments', function ($join) {
  130. $join->on("assessments.student_id", '=', 'program_student_semester.student_id')
  131. ->on('assessments.activity_criterion_id', '=', 'activity_criterion.id');
  132. })
  133. ->where('courses.semester_id', $course_code->semester_id)
  134. ->whereIn('courses.program_id', $program_ids)
  135. ->where('courses.code', $course_code->code)
  136. ->where('courses.number', $course_code->number)
  137. ->whereIn('program_student_semester.program_id', $program_ids)
  138. ->whereIn('activity_criterion.criterion_id', $criteria_id)
  139. ->whereIn('criteria.id', $criteria_id)
  140. ->where('program_student_semester.semester_id', $course_code->semester_id)
  141. ->select('criteria.*')
  142. ->addSelect(DB::raw('count(case when assessments.score>= rubrics.expected_points then 1 else null end) as criteria_achieved'))
  143. ->addSelect(DB::raw('count(assessments.activity_criterion_id) as criteria_attempted'))
  144. ->groupBy('criteria.id');
  145. /* Log::info($table_per_criterion->toSql());
  146. Log::info(array($course_code));
  147. Log::info($program_ids);
  148. Log::info($criteria_id);*/
  149. Log::info('mera 1');
  150. Log::info(array($course_code));
  151. Log::info($table_per_criterion->get());
  152. return $table_per_criterion->get();
  153. }
  154. }
  155. public static function getStudentReportForOutcome($course_code = null)
  156. {
  157. if ($course_code) {
  158. $criteria_id = DB::table('annual_plan_objective')
  159. ->where('typ_semester_course_id', $course_code->typ_semester_course_id)
  160. ->lists('criteria_id');
  161. $program = Program::where('id', $course_code->program_id)->first();
  162. // // Log::info($program->code);
  163. if ($program->code == "") {
  164. $program_ids = DB::table('programs')
  165. ->where('school_id', $program->school->id)
  166. ->lists('programs.id');
  167. // Log::info("estamos");
  168. } else $program_ids = array($program->id);
  169. // Log::info(array($course_code));
  170. //Table
  171. /* Student criterion_attempted criterion passed
  172. * 1 2 1
  173. * 2 7 4
  174. *
  175. *
  176. */
  177. $table_per_student = DB::table('program_student_semester')
  178. ->join('course_student', 'program_student_semester.student_id', '=', 'course_student.student_id')
  179. ->join('courses', 'courses.id', '=', 'course_student.course_id')
  180. ->join('activities', 'activities.course_id', '=', 'courses.id')
  181. ->join('activity_criterion', 'activities.id', '=', 'activity_criterion.activity_id')
  182. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  183. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  184. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  185. ->join('assessments', function ($join) {
  186. $join->on("assessments.student_id", '=', 'program_student_semester.student_id')
  187. ->on('assessments.activity_criterion_id', '=', 'activity_criterion.id');
  188. })
  189. ->where('courses.semester_id', $course_code->semester_id)
  190. ->whereIn('courses.program_id', $program_ids)
  191. ->where('courses.code', $course_code->code)
  192. ->where('courses.number', $course_code->number)
  193. ->whereIn('program_student_semester.program_id', $program_ids)
  194. ->whereIn('activity_criterion.criterion_id', $criteria_id)
  195. ->where('program_student_semester.semester_id', $course_code->semester_id)
  196. ->select('course_student.student_id')
  197. ->addSelect(DB::raw('count(assessments.activity_criterion_id) as criteria_attempted'))
  198. ->addSelect(DB::raw('count(case when assessments.score>=rubrics.expected_points then 1 else NULL end) as criteria_achieved'))
  199. ->groupBy('course_student.student_id');
  200. //Log::info($criteria_id);
  201. //Log::info($program_ids);
  202. //Log::info($table_per_student->toSql());
  203. $students = $table_per_student->get();
  204. return $students;
  205. }
  206. }
  207. public function getReportObject()
  208. {
  209. $reportObject = new stdClass();
  210. $reportObject->criteriaReport = DB::table('courses')
  211. ->join('activities', 'activities.course_id', '=', 'courses.id')
  212. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  213. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  214. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  215. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  216. ->join('assessments', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  217. ->select(DB::raw(
  218. "count(assessments.score) as assessed,
  219. count(case when assessments.score >= rubrics.expected_points then 1 else NULL end) as achieved,
  220. criteria.*"
  221. ))
  222. ->where('courses.id', $this->id)
  223. ->groupBy('criteria.id')
  224. ->get();
  225. //Log::info($this->id);
  226. $reportObject->outcomesReport = DB::table('courses')
  227. ->join('activities', 'activities.course_id', '=', 'courses.id')
  228. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  229. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  230. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  231. ->join('assessments', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  232. ->join('criterion_objective_outcome', 'activity_criterion.criterion_id', '=', 'criterion_objective_outcome.criterion_id')
  233. ->join('outcomes', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id')
  234. ->select(
  235. DB::raw(
  236. "student_id, outcomes.name, outcomes.id,
  237. count(activity_criterion.criterion_id) as attempted,
  238. count(case when assessments.score >= rubrics.expected_points then 1 else null end) as achieved"
  239. )
  240. )
  241. ->where('courses.id', $this->id)
  242. ->groupBy(array('outcome_id', 'student_id'))
  243. ->get();
  244. return $reportObject;
  245. }
  246. public function outcomes_ach()
  247. {
  248. $criteria = DB::table('criteria')
  249. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  250. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  251. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  252. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  253. ->join('courses', 'activities.course_id', '=', 'courses.id')
  254. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  255. ->where('courses.id', '=', $this->id)
  256. ->where('activities.draft', 0)
  257. ->where('activities.diagnostic', 0)
  258. ->select(
  259. 'criteria.id',
  260. 'rubrics.expected_percentage as expected_percentage_students_achieving'/*'expected_percentage_students_achieving',*/,
  261. 'activity_criterion.activity_id'
  262. )
  263. ->distinct()
  264. ->get();
  265. // Old data was formed like { "outcome1": cuantity_of_criteria_associated_passed, "outcome2":cuantity_of_criteria}
  266. $outcomes_achieved = [];
  267. foreach ($criteria as $criterion) {
  268. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  269. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  270. Log::info('estamos aqui');
  271. Log::info($students_attempted);
  272. if ($students_attempted) {
  273. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  274. } else {
  275. $percentage_students_who_achieved = 0;
  276. }
  277. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  278. $outcomes = DB::table('criterion_objective_outcome')
  279. ->where('criterion_id', $criterion->id)
  280. ->select('outcome_id')
  281. ->distinct()
  282. ->lists('outcome_id');
  283. foreach ($outcomes as $outcome_id) {
  284. if (array_key_exists($outcome_id, $outcomes_achieved)) $outcomes_achieved[$outcome_id] += 1;
  285. else $outcomes_achieved[$outcome_id] = 1;
  286. }
  287. }
  288. }
  289. Log::info(
  290. "OUTCOMES_ACHIEVED: "
  291. );
  292. Log::info(
  293. $outcomes_achieved
  294. );
  295. return $outcomes_achieved;
  296. }
  297. public function someSectionAssessed()
  298. {
  299. $all_sections = DB::table('courses')
  300. ->join('activities', 'activities.course_id', '=', 'courses.id')
  301. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  302. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  303. ->where('courses.name', $this->name)
  304. ->where('code', $this->code)
  305. ->where('number', $this->number)
  306. ->where('semester_id', $this->semester_id)
  307. ->where(
  308. 'program_id',
  309. $this->program_id
  310. )
  311. ->where('activities.draft', 0)
  312. ->where('activities.diagnostic', 0)
  313. ->groupBy('courses.id')
  314. ->count();
  315. return $all_sections;
  316. }
  317. public function isAssessed()
  318. {
  319. $assessed = DB::table('assessments')
  320. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  321. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  322. ->where('activities.course_id', $this->id)
  323. ->count();
  324. // Log::info("aqui".$assessed);
  325. if ($assessed) return true;
  326. else return false;
  327. }
  328. public function outcomes_achieved()
  329. {
  330. $criteria = DB::table('criteria')
  331. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  332. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  333. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  334. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  335. ->join('courses', 'activities.course_id', '=', 'courses.id')
  336. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  337. ->where('courses.id', '=', $this->id)
  338. ->select(
  339. 'criteria.id',
  340. 'rubrics.expected_percentage as expected_percentage_students_achieving'/*'expected_percentage_students_achieving',*/,
  341. 'activity_criterion.activity_id'
  342. )
  343. ->distinct()
  344. ->get();
  345. $conteo = 0;
  346. foreach ($criteria as $criterion) {
  347. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  348. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  349. if ($students_attempted) {
  350. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  351. } else {
  352. $percentage_students_who_achieved = 0;
  353. }
  354. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  355. $conteo++;
  356. }
  357. }
  358. return $conteo;
  359. }
  360. public function semester()
  361. {
  362. return $this->belongsTo('Semester');
  363. }
  364. public function sectionsWithOutcome($outcome_id)
  365. {
  366. return Course::has('activities')->with(array('activities' => function ($query) use (&$outcome_id) {
  367. $query->where('outcomes_attempted', 'not like', '\'%"' . $outcome_id . '":0%\'');
  368. }))->where('code', $this->code)->where('number', $this->number)->get();
  369. }
  370. }