暂无描述

Course.php 18KB

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