Geen omschrijving

Course.php 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. <?php
  2. class Course extends Eloquent
  3. {
  4. protected $fillable = array('name', 'code', 'number', 'section', 'program_id', 'user_id', 'semester_id');
  5. protected $appends = array('proposed_transformative_actions');
  6. public function program()
  7. {
  8. return $this->belongsTo('Program');
  9. }
  10. public function students()
  11. {
  12. return $this->belongsToMany('Student')->orderBy('name', 'asc')->withTimestamps();
  13. }
  14. public function user()
  15. {
  16. return $this->belongsTo('User')->orderBy('surnames', 'asc')->orderBy('first_name', 'asc');
  17. }
  18. // TODO: Remove
  19. public function activities()
  20. {
  21. return $this->hasMany('Activity')->orderBy('date', 'asc');
  22. }
  23. public function activity()
  24. {
  25. return $this->belongsTo('Activity');
  26. }
  27. //paired_objective, como el typ_semester_course_id esta atado a un solo typ_semester_objective_id pues te devuelve el primero
  28. public function getPairedObjectiveAttribute()
  29. {
  30. return Objective::join('typ_semester_objectives', 'typ_semester_objectives.objective_id', '=', 'objectives.id')
  31. ->join('typ_semester_courses', 'typ_semester_courses.typ_semester_objective_id', '=', 'typ_semester_objectives.id')
  32. ->where('typ_semester_courses.id', $this->typ_semester_course_id)
  33. ->select('typ_semester_objectives.id as typ_semester_objective_id', 'objectives.*')
  34. ->first();
  35. }
  36. //future_transformative_actions
  37. public function getFutureTransformativeActionsAttribute()
  38. {
  39. if (isset($this->typ_semester_course_id)) {
  40. Log::info("entramos al isset");
  41. //Big object where data is going to turn out like
  42. /*
  43. $this->future_transformative_actions = all future transformative actions proposed in annual plan
  44. and each future_transformative_action has an attribute future_semesters where it will be an
  45. array of all future semesters proposed
  46. */
  47. $results = $this->getFutureTypSemesterCourses($this->typ_semester_course_id, $this->program_id);
  48. if (isset($results->future_transformative_actions))
  49. $results = $results->future_transformative_actions;
  50. $future_transformative_actions_ids = [];
  51. foreach ($results as $row) {
  52. //Log::info($this->typ_semester_course_id);
  53. //Log::info('row');
  54. //Log::info($row);
  55. if (isset($row->trans_id)) {
  56. if (!isset($future_transformative_actions_ids[$row->trans_id])) {
  57. $future_transformative_actions_ids[$row->trans_id] = [
  58. 'future_semesters' => []
  59. ];
  60. }
  61. Log::info('arreglo');
  62. Log::info($future_transformative_actions_ids);
  63. $future_transformative_actions_ids[$row->trans_id]['future_semesters'][] = $row->semester_id;
  64. } else continue;
  65. }
  66. $trans_actions = [];
  67. Log::info($future_transformative_actions_ids);
  68. foreach ($future_transformative_actions_ids as $trans_id => $future_semesters) {
  69. $ta = TransformativeAction::where('id', $trans_id)->first();
  70. //$ta->setAttribute('future_semesters', []);
  71. $temp_future_semesters = [];
  72. foreach ($future_semesters['future_semesters'] as $index => $semester_id) {
  73. //$ta->
  74. $temp_future_semesters[$index] = Semester::where('id', $semester_id)->first();
  75. }
  76. $ta->setAttribute('future_semesters', $temp_future_semesters);
  77. $trans_actions[] = $ta;
  78. }
  79. return $trans_actions;
  80. }
  81. }
  82. //proposed_transformative_actions
  83. public function getProposedTransformativeActionsAttribute()
  84. {
  85. if (isset($this->typ_semester_course_id)) {
  86. //return
  87. Log::info(array($this));
  88. return TransformativeAction::join('annual_plan_transformative', 'transformative_actions.id', '=', 'annual_plan_transformative.trans_id')
  89. ->where('typ_semester_course_id', $this->typ_semester_course_id)
  90. ->select('transformative_actions.*', DB::raw("{$this->semester_id} as semester_id"))
  91. ->get();
  92. $transformative_actions = DB::table('annual_plan_transformative')
  93. ->join('transformative_actions', 'transformative_actions.id', '=', 'annual_plan_transformative.trans_id')
  94. ->where('typ_semester_course_id', $this->typ_semester_course_id)
  95. ->select('transformative_actions.*')
  96. ->get();
  97. $trans_list = [];
  98. foreach ($transformative_actions as $trans) {
  99. $ta = TransformativeAction::where('id', $trans->id)->first();
  100. $ta->setAttribute("semester_id", $this->semester_id);
  101. $trans_list[] = $ta;
  102. }
  103. return $trans_list;
  104. }
  105. Log::info('te la viviste tm');
  106. return null;
  107. }
  108. // assessed_paired_criteria
  109. public function getAssessedPairedCriteriaAttribute()
  110. {
  111. $criteria_id = DB::table('annual_plan_objective')
  112. ->where('typ_semester_course_id', $this->typ_semester_course_id)
  113. ->lists('criteria_id');
  114. //Log::info($this->typ_semester_course_id);
  115. //Log::info("dame ese id");
  116. $program = Program::where('id', $this->program_id)->first();
  117. // Log::info($program->code);
  118. //Si es un programa como core, enseña todos los estudiantes
  119. if ($program->code == "") {
  120. $program_ids = DB::table('programs')
  121. ->where('school_id', $program->school->id)
  122. ->lists('programs.id');
  123. //Log::info("estamos");
  124. } else $program_ids = array($program->id);
  125. /*tabla es asi por curso codigo
  126. *
  127. *criterioDelPlan EstudiantesDelProgramaQuePasaron EstudiantesqueIntentaron
  128. *
  129. * criterio1 3 5
  130. * criterio2 2 2
  131. *
  132. *
  133. */
  134. return Criterion::join('activity_criterion', 'criteria.id', '=', 'activity_criterion.criterion_id')
  135. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  136. ->join('courses', 'courses.id', '=', 'activities.course_id')
  137. ->join('course_student', 'course_student.course_id', '=', 'courses.id')
  138. ->join('program_student_semester', 'program_student_semester.student_id', '=', 'course_student.student_id')
  139. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  140. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  141. ->join('assessments', function ($join) {
  142. $join->on("assessments.student_id", '=', 'program_student_semester.student_id')
  143. ->on('assessments.activity_criterion_id', '=', 'activity_criterion.id');
  144. })
  145. ->where('courses.semester_id', $this->semester_id)
  146. ->whereIn('courses.program_id', $program_ids)
  147. ->where('courses.code', $this->code)
  148. ->where('courses.number', $this->number)
  149. ->whereIn('program_student_semester.program_id', $program_ids)
  150. ->whereIn('activity_criterion.criterion_id', $criteria_id)
  151. ->whereIn('criteria.id', $criteria_id)
  152. ->where('program_student_semester.semester_id', $this->semester_id)
  153. ->select('criteria.*')
  154. ->addSelect(DB::raw("{$this->typ_semester_course_id} as typ_semester_course_id"))
  155. ->addSelect(DB::raw('count(case when assessments.score>= rubrics.expected_points then 1 else null end) as criteria_achieved'))
  156. ->addSelect(DB::raw('count(assessments.activity_criterion_id) as criteria_attempted'))
  157. ->groupBy('criteria.id')
  158. ->get();
  159. }
  160. //paired_criteria solo funciona para plan ahora_mismo, y esta puerquito
  161. public function getPairedCriteriaAttribute()
  162. {
  163. if (isset($this->typ_semester_course_id)) {
  164. return Criterion::join("annual_plan_objective", 'annual_plan_objective.criteria_id', '=', 'criteria.id')
  165. ->where('typ_semester_course_id', $this->typ_semester_course_id)
  166. ->select(
  167. 'criteria.*',
  168. 'typ_semester_course_id'
  169. )
  170. ->get();
  171. /*.
  172. $criteria_info = $this->getCriteriaPlanReport($this);
  173. $criteria_list = [];
  174. foreach ($criteria_info as $index => $criterion) {
  175. $criteria_list[$index] = Criterion::where("id", $criterion->id)->first();
  176. $criteria_list[$index]->setAttribute("criteria_attempted", $criterion->criteria_attempted);
  177. $criteria_list[$index]->setAttribute("criteria_achieved", $criterion->criteria_achieved);
  178. }
  179. return $criteria_list;
  180. }*/
  181. }
  182. return null;
  183. }
  184. public function assessedActivities()
  185. {
  186. // return $this->hasMany('Activity')->whereNotNull('activities.outcomes_attempted')->orderBy('date', 'asc');
  187. $activitiesAssessed = DB::table('activities')
  188. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  189. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  190. ->where('activities.course_id', $this->id)
  191. ->select('activities.id')
  192. ->distinct()
  193. ->lists('activities.id');
  194. //Log::info($activitiesAssessed);
  195. return $this->hasMany('Activity')->whereIn('activities.id', $activitiesAssessed)->orderBy('date', 'asc');
  196. // return $this->hasMany('Activity')->where('activities.draft', 0)->orderBy('date', 'asc');
  197. }
  198. public static function get_associated_objective_outcome($typ_semester_course_id)
  199. {
  200. return DB::table('typ_semester_outcome')->join('typ_semester_objectives', 'typ_semester_outcome.id', '=', 'typ_semester_objectives.typ_semester_outcome_id')
  201. ->join('typ_semester_courses', 'typ_semester_courses.typ_semester_objective_id', '=', 'typ_semester_objectives.id')
  202. ->where('typ_semester_courses.id', $typ_semester_course_id);
  203. }
  204. public static function getFutureTypSemesterCourses($typ_semester_course_id, $program_id)
  205. {
  206. $typ_info = new stdClass();
  207. $typ_info->future_semesters = DB::table('typ_semester_outcome')
  208. ->join('typ_semester_objectives', 'typ_semester_outcome.id', '=', 'typ_semester_objectives.typ_semester_outcome_id')
  209. ->join('typ_semester_courses', 'typ_semester_courses.typ_semester_objective_id', '=', 'typ_semester_objectives.id')
  210. ->join('typ_semester_outcome as typ_o_2', 'typ_o_2.outcome_id', '=', 'typ_semester_outcome.outcome_id')
  211. ->join('semesters as sem1', 'sem1.id', '=', 'typ_semester_outcome.semester_id')
  212. ->join('typ_semester_objectives as typ_ob_2', function ($query) {
  213. $query->on('typ_ob_2.typ_semester_outcome_id', '=', 'typ_o_2.id')
  214. ->on('typ_semester_objectives.objective_id', '=', 'typ_ob_2.objective_id');
  215. })
  216. ->join('typ_semester_courses as typ_course_2', function ($join) {
  217. $join->on('typ_course_2.typ_semester_objective_id', '=', 'typ_ob_2.id')
  218. ->on('typ_semester_courses.course_id', '=', 'typ_course_2.course_id');
  219. })
  220. ->join('typ_program', 'typ_o_2.typ_program_id', '=', 'typ_program.id')
  221. ->join('semesters', 'semesters.id', '=', 'typ_o_2.semester_id')
  222. ->leftJoin('annual_plan_transformative', 'annual_plan_transformative.typ_semester_course_id', '=', 'typ_course_2.id')
  223. ->leftJoin('transformative_actions', 'transformative_actions.id', '=', 'annual_plan_transformative.trans_id')
  224. ->where('typ_semester_courses.id', $typ_semester_course_id)
  225. ->where('typ_program.program_id', $program_id)
  226. ->whereRaw('sem1.code < semesters.code')
  227. ->groupBy('typ_o_2.semester_id')
  228. ->select(
  229. 'typ_o_2.semester_id',
  230. 'typ_o_2.outcome_id',
  231. 'typ_o_2.id',
  232. 'typ_semester_courses.course_id',
  233. 'typ_semester_objectives.objective_id',
  234. 'typ_ob_2.id as typ_future_objective_id',
  235. 'typ_course_2.id as typ_future_course_id',
  236. 'semesters.name as semester_name',
  237. 'semesters.code as semester_code',
  238. 'transformative_actions.id as trans_id',
  239. 'transformative_actions.type_of_TA',
  240. 'transformative_actions.at_text',
  241. 'transformative_actions.description'
  242. ); //->get();
  243. //Log::info('El sql');
  244. $typ_info->future_transformative_actions = DB::table('typ_semester_outcome')
  245. ->join('typ_semester_objectives', 'typ_semester_outcome.id', '=', 'typ_semester_objectives.typ_semester_outcome_id')
  246. ->join('typ_semester_courses', 'typ_semester_courses.typ_semester_objective_id', '=', 'typ_semester_objectives.id')
  247. ->join('typ_semester_outcome as typ_o_2', 'typ_o_2.outcome_id', '=', 'typ_semester_outcome.outcome_id')
  248. ->join('semesters as sem1', 'sem1.id', '=', 'typ_semester_outcome.semester_id')
  249. ->join('typ_semester_objectives as typ_ob_2', function ($query) {
  250. $query->on('typ_ob_2.typ_semester_outcome_id', '=', 'typ_o_2.id')
  251. ->on('typ_semester_objectives.objective_id', '=', 'typ_ob_2.objective_id');
  252. })
  253. ->join('typ_semester_courses as typ_course_2', function ($join) {
  254. $join->on('typ_course_2.typ_semester_objective_id', '=', 'typ_ob_2.id')
  255. ->on('typ_semester_courses.course_id', '=', 'typ_course_2.course_id');
  256. })
  257. ->join('typ_program', 'typ_o_2.typ_program_id', '=', 'typ_program.id')
  258. ->join('semesters', 'semesters.id', '=', 'typ_o_2.semester_id')
  259. ->leftJoin('annual_plan_transformative', 'annual_plan_transformative.typ_semester_course_id', '=', 'typ_course_2.id')
  260. ->leftJoin('transformative_actions', 'transformative_actions.id', '=', 'annual_plan_transformative.trans_id')
  261. ->where('typ_semester_courses.id', $typ_semester_course_id)
  262. ->where('typ_program.program_id', $program_id)
  263. ->whereRaw('sem1.code < semesters.code')
  264. //->groupBy('typ_o_2.semester_id')
  265. ->select(
  266. 'typ_o_2.semester_id',
  267. 'typ_o_2.outcome_id',
  268. 'typ_o_2.id',
  269. 'typ_semester_courses.course_id',
  270. 'typ_semester_objectives.objective_id',
  271. 'typ_ob_2.id as typ_future_objective_id',
  272. 'typ_course_2.id as typ_future_course_id',
  273. 'semesters.name as semester_name',
  274. 'semesters.code as semester_code',
  275. 'transformative_actions.id as trans_id',
  276. 'transformative_actions.type_of_TA',
  277. 'transformative_actions.at_text',
  278. 'transformative_actions.description'
  279. );
  280. //Log::info("typ_info");
  281. //Log::info($typ_info->future_semesters->toSql());
  282. $typ_info->future_semesters = $typ_info->future_semesters->get();
  283. //Log::info($typ_info->future_transformative_actions->toSql());
  284. $typ_info->future_transformative_actions = $typ_info->future_transformative_actions->get();
  285. //Log::info($typ_info->toSql());
  286. Log::info($typ_semester_course_id);
  287. Log::info($program_id);
  288. return $typ_info;
  289. }
  290. public function publishedActivities()
  291. {
  292. $activitiesAssessed = DB::table('activities')
  293. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  294. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  295. ->where('activities.course_id', $this->id)
  296. ->where('activities.draft', 0)
  297. ->where('activities.diagnostic', 0)
  298. ->select('activities.id')
  299. ->distinct()
  300. ->lists('activities.id');
  301. //$this->hasMany('Activity')->whereNotNull('activities.outcomes_attempted')->where('activities.draft', 0)->orderBy('date', 'asc');
  302. return $this->hasMany('Activity')->whereIn('activities.id', $activitiesAssessed)->orderBy('date', 'asc');
  303. }
  304. public function diagnosticActivities()
  305. {
  306. $activitiesAssessed = DB::table('activities')
  307. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  308. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  309. ->where('activities.course_id', $this->id)
  310. ->where('activities.draft', 0)
  311. ->where('activities.diagnostic', 1)
  312. ->select('activities.id')
  313. ->distinct()
  314. ->lists('activities.id');
  315. //$this->hasMany('Activity')
  316. return $this->hasMany('Activity')->whereIn('activities.id', $activitiesAssessed)->orderBy('date', 'asc');
  317. }
  318. public function outcomes_attempted()
  319. {
  320. return DB::table('courses')
  321. ->join('activities', 'activities.course_id', '=', 'courses.id')
  322. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  323. ->join('criteria', 'activity_criterion.criterion_id', '=', 'criteria.id')
  324. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  325. ->where('courses.id', '=', $this->id)
  326. ->select('criterion_objective_outcome.outcome_id')
  327. ->distinct()
  328. ->orderBy('criterion_objective_outcome.outcome_id')
  329. ->get();
  330. // 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
  331. // return $this->id;
  332. }
  333. public function outcomes_att()
  334. {
  335. $criteria_attempted = DB::table('courses')
  336. ->join('activities', 'activities.course_id', '=', 'courses.id')
  337. ->join('activity_criterion', 'activities.id', '=', 'activity_criterion.activity_id')
  338. ->select('criterion_id', 'activity_criterion.id as activity_criterion_id')
  339. ->where('courses.id', $this->id)
  340. ->where('draft', 0)
  341. ->where('activities.diagnostic', 0)
  342. ->get();
  343. // Old data was formed like { "outcome1": cuantity_of_criteria_associated, "outcome2":cuantity_of_criteria}
  344. $outcomes_attempted = [];
  345. foreach ($criteria_attempted as $criterion) {
  346. $outcomes = DB::table('criterion_objective_outcome')
  347. ->where('criterion_id', $criterion->criterion_id)
  348. ->select('outcome_id')
  349. ->distinct()
  350. ->lists('outcome_id');
  351. foreach ($outcomes as $outcome_id) {
  352. if (array_key_exists($outcome_id, $outcomes_attempted)) $outcomes_attempted[$outcome_id] += 1;
  353. else $outcomes_attempted[$outcome_id] = 1;
  354. }
  355. }
  356. Log::info("outcomes_attempted");
  357. Log::info($outcomes_attempted);
  358. return $outcomes_attempted;
  359. }
  360. public static function getCriteriaPlanReport($course_code = null)
  361. {
  362. if ($course_code) {
  363. $criteria_id = DB::table('annual_plan_objective')
  364. ->where('typ_semester_course_id', $course_code->typ_semester_course_id)
  365. ->lists('criteria_id');
  366. //Log::info($course_code->typ_semester_course_id);
  367. //Log::info("dame ese id");
  368. $program = Program::where('id', $course_code->program_id)->first();
  369. // Log::info($program->code);
  370. //Si es un programa como core, enseña todos los estudiantes
  371. if ($program->code == "") {
  372. $program_ids = DB::table('programs')
  373. ->where('school_id', $program->school->id)
  374. ->lists('programs.id');
  375. //Log::info("estamos");
  376. } else $program_ids = array($program->id);
  377. /*tabla es asi por curso codigo
  378. *
  379. *criterioDelPlan EstudiantesDelProgramaQuePasaron EstudiantesqueIntentaron
  380. *
  381. * criterio1 3 5
  382. * criterio2 2 2
  383. *
  384. *
  385. */
  386. $table_per_criterion = DB::table('program_student_semester')
  387. ->join('course_student', 'program_student_semester.student_id', '=', 'course_student.student_id')
  388. ->join('courses', 'courses.id', '=', 'course_student.course_id')
  389. ->join('activities', 'activities.course_id', '=', 'courses.id')
  390. ->join('activity_criterion', 'activities.id', '=', 'activity_criterion.activity_id')
  391. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  392. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  393. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  394. ->join('assessments', function ($join) {
  395. $join->on("assessments.student_id", '=', 'program_student_semester.student_id')
  396. ->on('assessments.activity_criterion_id', '=', 'activity_criterion.id');
  397. })
  398. ->where('courses.semester_id', $course_code->semester_id)
  399. ->whereIn('courses.program_id', $program_ids)
  400. ->where('courses.code', $course_code->code)
  401. ->where('courses.number', $course_code->number)
  402. ->whereIn('program_student_semester.program_id', $program_ids)
  403. ->whereIn('activity_criterion.criterion_id', $criteria_id)
  404. ->whereIn('criteria.id', $criteria_id)
  405. ->where('program_student_semester.semester_id', $course_code->semester_id)
  406. ->select('criteria.*')
  407. ->addSelect(DB::raw('count(case when assessments.score>= rubrics.expected_points then 1 else null end) as criteria_achieved'))
  408. ->addSelect(DB::raw('count(assessments.activity_criterion_id) as criteria_attempted'))
  409. ->groupBy('criteria.id');
  410. /*Log::info($table_per_criterion->toSql());
  411. Log::info(array($course_code));
  412. Log::info($program_ids);
  413. Log::info($criteria_id);
  414. Log::info('mera 1');
  415. Log::info(array($course_code));
  416. Log::info($table_per_criterion->get()); */
  417. return $table_per_criterion->get();
  418. }
  419. }
  420. //dirty code repeated code
  421. public function getStudentReportForOutcomeAttribute()
  422. {
  423. return $this->getStudentReportForOutcome($this);
  424. }
  425. //user id is for groupped professor sections. so el query es code, number, semester y user_id
  426. public static function getStudentReportForOutcome($course_code = null, $user_id = null)
  427. {
  428. if ($course_code) {
  429. //Log::info('entré');
  430. //Si entra aqui, significa que son los criterios dentro del annual plan, bajo un dominio en particular.
  431. if (isset($course_code->typ_semester_course_id)) {
  432. $criteria_id = DB::table('annual_plan_objective')
  433. ->where('typ_semester_course_id', $course_code->typ_semester_course_id)
  434. ->lists('criteria_id');
  435. }
  436. //si entra aqui, estamos buscando del curso (o grupo de cursos independientes
  437. else {
  438. $criteria_id = DB::table('courses')
  439. ->join('activities', 'courses.id', '=', 'activities.course_id')
  440. ->join('activity_criterion', 'activities.id', '=', 'activity_criterion.activity_id');
  441. //if this is 1, significa que son todos los cursos de ese semestre con nombre.
  442. if (isset($course_code->grouped)) {
  443. $criteria_id = $criteria_id->where('courses.name', $course_code->name)
  444. ->where('courses.code', $course_code->code)
  445. ->where('courses.number', $course_code->number)
  446. ->where('courses.semester_id', $course_code->semester_id);
  447. //if user_id is set, this means its goruped courses of said professor.
  448. if ($user_id) {
  449. $criteria_id = $criteria_id->where('courses.user_id', $user_id);
  450. }
  451. $criteria_id = $criteria_id
  452. ->groupBy('activity_criterion.criterion_id')
  453. ->lists('activity_criterion.criterion_id');
  454. }
  455. //si es un solo curso.
  456. else {
  457. $criteria_id = $criteria_id->where('courses.id', $course_code->id)
  458. ->groupBy('activity_criterion.criterion_id')
  459. ->lists('activity_criterion.criterion_id');
  460. }
  461. }
  462. //Log::info($criteria_id);
  463. $program = Program::where('id', $course_code->program_id)->first();
  464. // // Log::info($program->code);
  465. if ($program->code == "") {
  466. $program_ids = DB::table('programs')
  467. ->where('school_id', $program->school->id)
  468. ->lists('programs.id');
  469. // Log::info("estamos");
  470. } else $program_ids = array($program->id);
  471. // Log::info(array($course_code));
  472. //ESTA ES LA TABLA QUERY PPOR STUDENT
  473. $table_per_student = DB::table('assessments')
  474. ->join('activity_criterion', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  475. ->join('activities', 'activities.id', '=', 'activity_criterion.activity_id')
  476. ->join('courses', 'courses.id', '=', 'activities.course_id')
  477. ->join('students', 'students.id', '=', 'assessments.student_id')
  478. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'activity_criterion.criterion_id')
  479. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  480. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  481. ->join('semesters', 'semesters.id', '=', 'courses.semester_id')
  482. ->whereIn('students.program_id', $program_ids)
  483. ->where('semester_id', $course_code->semester_id)
  484. ->where('semesters.is_visible', '=', 1)
  485. ->where('activities.draft', 0)
  486. ->where('activities.diagnostic', 0)
  487. ->select('student_id', 'students.program_id', 'semesters.code', 'outcome_id', 'criterion_objective_outcome.criterion_id', 'score', 'expected_points')
  488. ->distinct()
  489. ->get();
  490. //Table
  491. /* Student criterion_attempted criterion passed
  492. * 1 2 1
  493. * 2 7 4
  494. *
  495. *
  496. */
  497. $table_per_student = DB::table('course_student')
  498. //->join('program_student_semester', 'program_student_semester.student_id', '=', 'course_student.student_id')
  499. ->join("students", 'course_student.student_id', '=', 'students.id')
  500. ->join('courses', 'courses.id', '=', 'course_student.course_id')
  501. ->join('activities', 'activities.course_id', '=', 'courses.id')
  502. ->join('activity_criterion', 'activities.id', '=', 'activity_criterion.activity_id')
  503. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  504. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  505. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  506. ->join('assessments', function ($join) {
  507. $join->on("assessments.student_id", '=', 'course_student.student_id')
  508. ->on('assessments.activity_criterion_id', '=', 'activity_criterion.id');
  509. })
  510. ->where('activities.draft', 0)
  511. ->where('activities.diagnostic', 0);
  512. // si typ_semester_course esta prendido
  513. // o, si with_program_report está prendido
  514. //, then significa que es de los estudiantes del programa
  515. //
  516. if (isset($course_code->typ_semester_course_id)) {
  517. $table_per_student = $table_per_student->join('program_student_semester', 'program_student_semester.student_id', '=', 'course_student.student_id')
  518. ->whereIn('program_student_semester.program_id', $program_ids)
  519. ->where('program_student_semester.semester_id', $course_code->semester_id);
  520. }
  521. if (isset($course_code->with_program_report)) {
  522. $table_per_student = $table_per_student->join('program_student_semester', 'program_student_semester.student_id', '=', 'course_student.student_id')
  523. ->whereIn('program_student_semester.program_id', $program_ids)
  524. ->where('program_student_semester.semester_id', $course_code->semester_id);
  525. }
  526. //si entra aqui, no especifica dominio, y hay que join todos los dominios
  527. if (!isset($course_code->typ_semester_course_id)) {
  528. $table_per_student = $table_per_student->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'activity_criterion.criterion_id');
  529. }
  530. //si estan grouped
  531. if (isset($course_code->grouped)) {
  532. $table_per_student = $table_per_student->where('courses.semester_id', $course_code->semester_id)
  533. ->where('courses.code', $course_code->code)
  534. ->where('courses.number', $course_code->number);
  535. }
  536. //si estan solo
  537. else {
  538. $table_per_student = $table_per_student->where('courses.id', $course_code->id);
  539. }
  540. ///// significa que depende
  541. /////->where('courses.semester_id', $course_code->semester_id)
  542. //->whereIn('courses.program_id', $program_ids)
  543. /////->where('courses.code', $course_code->code)
  544. /////->where('courses.number', $course_code->number)
  545. //->whereIn('program_student_semester.program_id', $program_ids)
  546. $table_per_student = $table_per_student->whereIn('activity_criterion.criterion_id', $criteria_id)
  547. //->where('program_student_semester.semester_id', $course_code->semester_id)
  548. ->select('students.number as student_id')
  549. ->addSelect(DB::raw('count(assessments.activity_criterion_id) as criteria_attempted'))
  550. ->addSelect(DB::raw('count(case when assessments.score>=rubrics.expected_points then 1 else NULL end) as criteria_achieved'));
  551. //si entra aqui, significa que va agrupar los estudiantes para un solo dominio.
  552. if (isset($course_code->typ_semester_course_id)) {
  553. $table_per_student = $table_per_student->groupBy('students.id');
  554. $students = $table_per_student->get();
  555. } else {
  556. $table_per_student = $table_per_student->addSelect('outcome_id')
  557. ->groupBy(array('students.id', 'outcome_id'));
  558. Log::info($table_per_student->toSql());
  559. Log::info($criteria_id);
  560. Log::info($course_code->id);
  561. Log::info($program_ids);
  562. Log::info($course_code->semester_id);
  563. Log::info("Amiga ponle a br");
  564. $query = $table_per_student->get();
  565. //Aqui queremos crear un arreglo que sea
  566. /*
  567. students = {
  568. outcome_id: {
  569. 'students':{
  570. {student x, crit_atttempted, crit_achieved},
  571. {student x2, crit_attempted, crit_achieved},
  572. ...
  573. }
  574. 'calculation':{
  575. student_achieved, student_attempted
  576. }
  577. }
  578. outcome_id2:
  579. {
  580. 'students':{
  581. {student x, crit_atttempted, crit_achieved},
  582. {student x2, crit_attempted, crit_achieved},
  583. ...
  584. }
  585. 'calculations':{
  586. student_achieved, student_attempted
  587. }
  588. },
  589. }
  590. */
  591. $students = [];
  592. foreach ($query as $q) {
  593. if (array_key_exists($q->outcome_id, $students)) {
  594. $students[$q->outcome_id]['students'][] = $q;
  595. } else {
  596. $students[$q->outcome_id]['students'] = [];
  597. $students[$q->outcome_id]['students'][] = $q;
  598. }
  599. $criteria_attempted = $q->criteria_attempted;
  600. $criteria_achieved = $q->criteria_achieved;
  601. //PREGUNTA??? ES 66.67??????
  602. $achieved = 0;
  603. if ($criteria_attempted != 0 && $criteria_achieved / $criteria_attempted * 100 >= 66.67) {
  604. $achieved = 1;
  605. }
  606. if (isset($students[$q->outcome_id]['calculations']) && $criteria_attempted) {
  607. $students[$q->outcome_id]['calculations']['student_attempted'] += 1;
  608. $students[$q->outcome_id]['calculations']['student_achieved'] += $achieved;
  609. } else if ($criteria_attempted) {
  610. $students[$q->outcome_id]['calculations']['student_attempted'] = 1;
  611. $students[$q->outcome_id]['calculations']['student_achieved'] = $achieved;
  612. }
  613. }
  614. }
  615. //Log::info($criteria_id);
  616. // Log::info('los courseCode');
  617. // Log::info(array($course_code));
  618. //Log::info('los programas');
  619. //Log::info($program_ids);
  620. //Log::info("crit");
  621. //Log::info($criteria_id);
  622. //Log::info($table_per_student->toSql());
  623. //Log::info($students);
  624. return $students;
  625. }
  626. }
  627. public function getReportObject()
  628. {
  629. $reportObject = new stdClass();
  630. $reportObject->criteriaReport = DB::table('courses')
  631. ->join('activities', 'activities.course_id', '=', 'courses.id')
  632. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  633. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  634. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  635. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  636. ->join('assessments', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  637. ->select(DB::raw(
  638. "count(assessments.score) as assessed,
  639. count(case when assessments.score >= rubrics.expected_points then 1 else NULL end) as achieved,
  640. criteria.*"
  641. ))
  642. ->where('courses.id', $this->id)
  643. ->groupBy('criteria.id')
  644. ->get();
  645. //Log::info($this->id);
  646. $reportObject->outcomesReport = DB::table('courses')
  647. ->join('activities', 'activities.course_id', '=', 'courses.id')
  648. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  649. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  650. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  651. ->join('assessments', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  652. ->join('criterion_objective_outcome', 'activity_criterion.criterion_id', '=', 'criterion_objective_outcome.criterion_id')
  653. ->join('outcomes', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id')
  654. ->select(
  655. DB::raw(
  656. "student_id, outcomes.name, outcomes.id,
  657. count(activity_criterion.criterion_id) as attempted,
  658. count(case when assessments.score >= rubrics.expected_points then 1 else null end) as achieved"
  659. )
  660. )
  661. ->where('courses.id', $this->id)
  662. ->groupBy(array('outcome_id', 'student_id'))
  663. ->get();
  664. return $reportObject;
  665. }
  666. public function outcomes_ach()
  667. {
  668. $criteria = DB::table('criteria')
  669. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  670. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  671. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  672. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  673. ->join('courses', 'activities.course_id', '=', 'courses.id')
  674. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  675. ->where('courses.id', '=', $this->id)
  676. ->where('activities.draft', 0)
  677. ->where('activities.diagnostic', 0)
  678. ->select(
  679. 'criteria.id',
  680. 'rubrics.expected_percentage as expected_percentage_students_achieving'/*'expected_percentage_students_achieving',*/,
  681. 'activity_criterion.activity_id'
  682. )
  683. ->distinct()
  684. ->get();
  685. // Old data was formed like { "outcome1": cuantity_of_criteria_associated_passed, "outcome2":cuantity_of_criteria}
  686. $outcomes_achieved = [];
  687. foreach ($criteria as $criterion) {
  688. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  689. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  690. //Log::info('estamos aqui');
  691. Log::info($students_attempted);
  692. if ($students_attempted) {
  693. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  694. } else {
  695. $percentage_students_who_achieved = 0;
  696. }
  697. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  698. $outcomes = DB::table('criterion_objective_outcome')
  699. ->where('criterion_id', $criterion->id)
  700. ->select('outcome_id')
  701. ->distinct()
  702. ->lists('outcome_id');
  703. foreach ($outcomes as $outcome_id) {
  704. if (array_key_exists($outcome_id, $outcomes_achieved)) $outcomes_achieved[$outcome_id] += 1;
  705. else $outcomes_achieved[$outcome_id] = 1;
  706. }
  707. }
  708. }
  709. Log::info(
  710. "OUTCOMES_ACHIEVED: "
  711. );
  712. Log::info(
  713. $outcomes_achieved
  714. );
  715. return $outcomes_achieved;
  716. }
  717. public function someSectionAssessed()
  718. {
  719. $all_sections = DB::table('courses')
  720. ->join('activities', 'activities.course_id', '=', 'courses.id')
  721. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  722. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  723. ->where('courses.name', $this->name)
  724. ->where('code', $this->code)
  725. ->where('number', $this->number)
  726. ->where('semester_id', $this->semester_id)
  727. ->where(
  728. 'program_id',
  729. $this->program_id
  730. )
  731. ->where('activities.draft', 0)
  732. ->where('activities.diagnostic', 0)
  733. ->groupBy('courses.id')
  734. ->count();
  735. return $all_sections;
  736. }
  737. public function isAssessed()
  738. {
  739. $assessed = DB::table('assessments')
  740. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  741. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  742. ->where('activities.course_id', $this->id)
  743. ->count();
  744. // Log::info("aqui".$assessed);
  745. if ($assessed) return true;
  746. else return false;
  747. }
  748. public function outcomes_achieved()
  749. {
  750. $criteria = DB::table('criteria')
  751. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  752. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  753. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  754. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  755. ->join('courses', 'activities.course_id', '=', 'courses.id')
  756. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  757. ->where('courses.id', '=', $this->id)
  758. ->select(
  759. 'criteria.id',
  760. 'rubrics.expected_percentage as expected_percentage_students_achieving'/*'expected_percentage_students_achieving',*/,
  761. 'activity_criterion.activity_id'
  762. )
  763. ->distinct()
  764. ->get();
  765. $conteo = 0;
  766. foreach ($criteria as $criterion) {
  767. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  768. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  769. if ($students_attempted) {
  770. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  771. } else {
  772. $percentage_students_who_achieved = 0;
  773. }
  774. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  775. $conteo++;
  776. }
  777. }
  778. return $conteo;
  779. }
  780. public function semester()
  781. {
  782. return $this->belongsTo('Semester');
  783. }
  784. public function sectionsWithOutcome($outcome_id)
  785. {
  786. return Course::has('activities')->with(array('activities' => function ($query) use (&$outcome_id) {
  787. $query->where('outcomes_attempted', 'not like', '\'%"' . $outcome_id . '":0%\'');
  788. }))->where('code', $this->code)->where('number', $this->number)->get();
  789. }
  790. }