Няма описание

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