暫無描述

Course.php 20KB

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