Bez popisu

Course.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. //Log::info('entré');
  159. $criteria_id = DB::table('annual_plan_objective')
  160. ->where('typ_semester_course_id', $course_code->typ_semester_course_id)
  161. ->lists('criteria_id');
  162. Log::info($criteria_id);
  163. $program = Program::where('id', $course_code->program_id)->first();
  164. // // Log::info($program->code);
  165. if ($program->code == "") {
  166. $program_ids = DB::table('programs')
  167. ->where('school_id', $program->school->id)
  168. ->lists('programs.id');
  169. // Log::info("estamos");
  170. } else $program_ids = array($program->id);
  171. // Log::info(array($course_code));
  172. //Table
  173. /* Student criterion_attempted criterion passed
  174. * 1 2 1
  175. * 2 7 4
  176. *
  177. *
  178. */
  179. $table_per_student = DB::table('program_student_semester')
  180. ->join('course_student', 'program_student_semester.student_id', '=', 'course_student.student_id')
  181. ->join('courses', 'courses.id', '=', 'course_student.course_id')
  182. ->join('activities', 'activities.course_id', '=', 'courses.id')
  183. ->join('activity_criterion', 'activities.id', '=', 'activity_criterion.activity_id')
  184. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  185. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  186. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  187. ->join('assessments', function ($join) {
  188. $join->on("assessments.student_id", '=', 'program_student_semester.student_id')
  189. ->on('assessments.activity_criterion_id', '=', 'activity_criterion.id');
  190. })
  191. ->where('courses.semester_id', $course_code->semester_id)
  192. ->whereIn('courses.program_id', $program_ids)
  193. ->where('courses.code', $course_code->code)
  194. ->where('courses.number', $course_code->number)
  195. ->whereIn('program_student_semester.program_id', $program_ids)
  196. ->whereIn('activity_criterion.criterion_id', $criteria_id)
  197. ->where('program_student_semester.semester_id', $course_code->semester_id)
  198. ->select('course_student.student_id')
  199. ->addSelect(DB::raw('count(assessments.activity_criterion_id) as criteria_attempted'))
  200. ->addSelect(DB::raw('count(case when assessments.score>=rubrics.expected_points then 1 else NULL end) as criteria_achieved'))
  201. ->groupBy('course_student.student_id');
  202. //Log::info($criteria_id);
  203. Log::info('los courseCode');
  204. Log::info(array($course_code));
  205. Log::info('los programas');
  206. Log::info($program_ids);
  207. Log::info($table_per_student->toSql());
  208. $students = $table_per_student->get();
  209. return $students;
  210. }
  211. }
  212. public function getReportObject()
  213. {
  214. $reportObject = new stdClass();
  215. $reportObject->criteriaReport = DB::table('courses')
  216. ->join('activities', 'activities.course_id', '=', 'courses.id')
  217. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  218. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  219. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  220. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  221. ->join('assessments', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  222. ->select(DB::raw(
  223. "count(assessments.score) as assessed,
  224. count(case when assessments.score >= rubrics.expected_points then 1 else NULL end) as achieved,
  225. criteria.*"
  226. ))
  227. ->where('courses.id', $this->id)
  228. ->groupBy('criteria.id')
  229. ->get();
  230. //Log::info($this->id);
  231. $reportObject->outcomesReport = DB::table('courses')
  232. ->join('activities', 'activities.course_id', '=', 'courses.id')
  233. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  234. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  235. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  236. ->join('assessments', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  237. ->join('criterion_objective_outcome', 'activity_criterion.criterion_id', '=', 'criterion_objective_outcome.criterion_id')
  238. ->join('outcomes', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id')
  239. ->select(
  240. DB::raw(
  241. "student_id, outcomes.name, outcomes.id,
  242. count(activity_criterion.criterion_id) as attempted,
  243. count(case when assessments.score >= rubrics.expected_points then 1 else null end) as achieved"
  244. )
  245. )
  246. ->where('courses.id', $this->id)
  247. ->groupBy(array('outcome_id', 'student_id'))
  248. ->get();
  249. return $reportObject;
  250. }
  251. public function outcomes_ach()
  252. {
  253. $criteria = DB::table('criteria')
  254. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  255. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  256. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  257. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  258. ->join('courses', 'activities.course_id', '=', 'courses.id')
  259. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  260. ->where('courses.id', '=', $this->id)
  261. ->where('activities.draft', 0)
  262. ->where('activities.diagnostic', 0)
  263. ->select(
  264. 'criteria.id',
  265. 'rubrics.expected_percentage as expected_percentage_students_achieving'/*'expected_percentage_students_achieving',*/,
  266. 'activity_criterion.activity_id'
  267. )
  268. ->distinct()
  269. ->get();
  270. // Old data was formed like { "outcome1": cuantity_of_criteria_associated_passed, "outcome2":cuantity_of_criteria}
  271. $outcomes_achieved = [];
  272. foreach ($criteria as $criterion) {
  273. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  274. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  275. Log::info('estamos aqui');
  276. Log::info($students_attempted);
  277. if ($students_attempted) {
  278. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  279. } else {
  280. $percentage_students_who_achieved = 0;
  281. }
  282. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  283. $outcomes = DB::table('criterion_objective_outcome')
  284. ->where('criterion_id', $criterion->id)
  285. ->select('outcome_id')
  286. ->distinct()
  287. ->lists('outcome_id');
  288. foreach ($outcomes as $outcome_id) {
  289. if (array_key_exists($outcome_id, $outcomes_achieved)) $outcomes_achieved[$outcome_id] += 1;
  290. else $outcomes_achieved[$outcome_id] = 1;
  291. }
  292. }
  293. }
  294. Log::info(
  295. "OUTCOMES_ACHIEVED: "
  296. );
  297. Log::info(
  298. $outcomes_achieved
  299. );
  300. return $outcomes_achieved;
  301. }
  302. public function someSectionAssessed()
  303. {
  304. $all_sections = DB::table('courses')
  305. ->join('activities', 'activities.course_id', '=', 'courses.id')
  306. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  307. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  308. ->where('courses.name', $this->name)
  309. ->where('code', $this->code)
  310. ->where('number', $this->number)
  311. ->where('semester_id', $this->semester_id)
  312. ->where(
  313. 'program_id',
  314. $this->program_id
  315. )
  316. ->where('activities.draft', 0)
  317. ->where('activities.diagnostic', 0)
  318. ->groupBy('courses.id')
  319. ->count();
  320. return $all_sections;
  321. }
  322. public function isAssessed()
  323. {
  324. $assessed = DB::table('assessments')
  325. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  326. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  327. ->where('activities.course_id', $this->id)
  328. ->count();
  329. // Log::info("aqui".$assessed);
  330. if ($assessed) return true;
  331. else return false;
  332. }
  333. public function outcomes_achieved()
  334. {
  335. $criteria = DB::table('criteria')
  336. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  337. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  338. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  339. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  340. ->join('courses', 'activities.course_id', '=', 'courses.id')
  341. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  342. ->where('courses.id', '=', $this->id)
  343. ->select(
  344. 'criteria.id',
  345. 'rubrics.expected_percentage as expected_percentage_students_achieving'/*'expected_percentage_students_achieving',*/,
  346. 'activity_criterion.activity_id'
  347. )
  348. ->distinct()
  349. ->get();
  350. $conteo = 0;
  351. foreach ($criteria as $criterion) {
  352. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  353. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  354. if ($students_attempted) {
  355. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  356. } else {
  357. $percentage_students_who_achieved = 0;
  358. }
  359. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  360. $conteo++;
  361. }
  362. }
  363. return $conteo;
  364. }
  365. public function semester()
  366. {
  367. return $this->belongsTo('Semester');
  368. }
  369. public function sectionsWithOutcome($outcome_id)
  370. {
  371. return Course::has('activities')->with(array('activities' => function ($query) use (&$outcome_id) {
  372. $query->where('outcomes_attempted', 'not like', '\'%"' . $outcome_id . '":0%\'');
  373. }))->where('code', $this->code)->where('number', $this->number)->get();
  374. }
  375. }