暫無描述

Course.php 39KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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. //cuantos estudiantes tiene este cursos que son del programa
  426. public function getQuantityAttribute()
  427. {
  428. $query = DB::table('courses')
  429. ->join('course_student', 'course_student.course_id', '=', 'courses.id')
  430. ->join('program_student_semester as pcs', function ($j) {
  431. $j->on('courses.program_id', '=', 'pcs.program_id')
  432. ->on('course_student.student_id', '=', 'pcs.student_id')
  433. ->on('courses.semester_id', '=', 'pcs.semester_id');
  434. });
  435. if ($this->grouped) {
  436. $query = $query->where('courses.name', $this->name)
  437. ->where('courses.code', $this->code)
  438. ->where('courses.number', $this->number)
  439. ->where('courses.semester_id', $this->semester_id);
  440. } else {
  441. $query = $query->where("courses.id", $this->id);
  442. }
  443. $query = $query->select(DB::raw("count( distinct pcs.student_id) as quantity"));
  444. $query = $query->first();
  445. if (isset($query)) {
  446. return $query->quantity;
  447. }
  448. return 0;
  449. }
  450. //user id is for groupped professor sections. so el query es code, number, semester y user_id
  451. public static function getStudentReportForOutcome($course_code = null, $user_id = null)
  452. {
  453. if ($course_code) {
  454. //Log::info('entré');
  455. //Si entra aqui, significa que son los criterios dentro del annual plan, bajo un dominio en particular.
  456. if (isset($course_code->typ_semester_course_id)) {
  457. $criteria_id = DB::table('annual_plan_objective')
  458. ->where('typ_semester_course_id', $course_code->typ_semester_course_id)
  459. ->lists('criteria_id');
  460. }
  461. //si entra aqui, estamos buscando del curso (o grupo de cursos independientes
  462. else {
  463. $criteria_id = DB::table('courses')
  464. ->join('activities', 'courses.id', '=', 'activities.course_id')
  465. ->join('activity_criterion', 'activities.id', '=', 'activity_criterion.activity_id');
  466. //if this is 1, significa que son todos los cursos de ese semestre con nombre.
  467. if (isset($course_code->grouped)) {
  468. $criteria_id = $criteria_id->where('courses.name', $course_code->name)
  469. ->where('courses.code', $course_code->code)
  470. ->where('courses.number', $course_code->number)
  471. ->where('courses.semester_id', $course_code->semester_id);
  472. //if user_id is set, this means its goruped courses of said professor.
  473. if ($user_id) {
  474. $criteria_id = $criteria_id->where('courses.user_id', $user_id);
  475. }
  476. $criteria_id = $criteria_id
  477. ->groupBy('activity_criterion.criterion_id')
  478. ->lists('activity_criterion.criterion_id');
  479. }
  480. //si es un solo curso.
  481. else {
  482. $criteria_id = $criteria_id->where('courses.id', $course_code->id)
  483. ->groupBy('activity_criterion.criterion_id')
  484. ->lists('activity_criterion.criterion_id');
  485. }
  486. }
  487. //Log::info($criteria_id);
  488. $program = Program::where('id', $course_code->program_id)->first();
  489. // // Log::info($program->code);
  490. if ($program->code == "") {
  491. $program_ids = DB::table('programs')
  492. ->where('school_id', $program->school->id)
  493. ->lists('programs.id');
  494. // Log::info("estamos");
  495. } else $program_ids = array($program->id);
  496. // Log::info(array($course_code));
  497. //ESTA ES LA TABLA QUERY PPOR STUDENT
  498. $table_per_student = DB::table('assessments')
  499. ->join('activity_criterion', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  500. ->join('activities', 'activities.id', '=', 'activity_criterion.activity_id')
  501. ->join('courses', 'courses.id', '=', 'activities.course_id')
  502. ->join('students', 'students.id', '=', 'assessments.student_id')
  503. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_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('semesters', 'semesters.id', '=', 'courses.semester_id')
  507. ->join('outcomes', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id')
  508. ->whereIn('students.program_id', $program_ids)
  509. ->where('semester_id', $course_code->semester_id)
  510. ->where('semesters.is_visible', '=', 1)
  511. ->where('activities.draft', 0)
  512. ->where('activities.diagnostic', 0)
  513. ->select('student_id', 'students.program_id', 'semesters.code', 'outcome_id', 'outcomes.expected_outcome', 'criterion_objective_outcome.criterion_id', 'score', 'expected_points')
  514. ->distinct()
  515. ->get();
  516. //Table
  517. /* Student criterion_attempted criterion passed
  518. * 1 2 1
  519. * 2 7 4
  520. *
  521. *
  522. */
  523. $table_per_student = DB::table('course_student')
  524. //->join('program_student_semester', 'program_student_semester.student_id', '=', 'course_student.student_id')
  525. ->join("students", 'course_student.student_id', '=', 'students.id')
  526. ->join('courses', 'courses.id', '=', 'course_student.course_id')
  527. ->join('activities', 'activities.course_id', '=', 'courses.id')
  528. ->join('activity_criterion', 'activities.id', '=', 'activity_criterion.activity_id')
  529. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  530. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  531. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  532. ->join('assessments', function ($join) {
  533. $join->on("assessments.student_id", '=', 'course_student.student_id')
  534. ->on('assessments.activity_criterion_id', '=', 'activity_criterion.id');
  535. })
  536. ->where('activities.draft', 0)
  537. ->where('activities.diagnostic', 0);
  538. // si typ_semester_course esta prendido
  539. // o, si with_program_report está prendido
  540. //, then significa que es de los estudiantes del programa
  541. //
  542. if (isset($course_code->typ_semester_course_id)) {
  543. $table_per_student = $table_per_student->join('program_student_semester', 'program_student_semester.student_id', '=', 'course_student.student_id')
  544. ->whereIn('program_student_semester.program_id', $program_ids)
  545. ->where('program_student_semester.semester_id', $course_code->semester_id);
  546. }
  547. if (isset($course_code->with_program_report)) {
  548. $table_per_student = $table_per_student->join('program_student_semester', 'program_student_semester.student_id', '=', 'course_student.student_id')
  549. ->whereIn('program_student_semester.program_id', $program_ids)
  550. ->where('program_student_semester.semester_id', $course_code->semester_id);
  551. }
  552. //si entra aqui, no especifica dominio, y hay que join todos los dominios
  553. if (!isset($course_code->typ_semester_course_id)) {
  554. $table_per_student = $table_per_student->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'activity_criterion.criterion_id')
  555. ->join('outcomes', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id');
  556. }
  557. //si estan grouped
  558. if (isset($course_code->grouped)) {
  559. $table_per_student = $table_per_student->where('courses.semester_id', $course_code->semester_id)
  560. ->where('courses.code', $course_code->code)
  561. ->where('courses.number', $course_code->number);
  562. }
  563. //si estan solo
  564. else {
  565. $table_per_student = $table_per_student->where('courses.id', $course_code->id);
  566. }
  567. ///// significa que depende
  568. /////->where('courses.semester_id', $course_code->semester_id)
  569. //->whereIn('courses.program_id', $program_ids)
  570. /////->where('courses.code', $course_code->code)
  571. /////->where('courses.number', $course_code->number)
  572. //->whereIn('program_student_semester.program_id', $program_ids)
  573. $table_per_student = $table_per_student->whereIn('activity_criterion.criterion_id', $criteria_id)
  574. //->where('program_student_semester.semester_id', $course_code->semester_id)
  575. ->select('students.number as student_id')
  576. ->addSelect(DB::raw('count(assessments.activity_criterion_id) as criteria_attempted'))
  577. ->addSelect(DB::raw('count(case when assessments.score>=rubrics.expected_points then 1 else NULL end) as criteria_achieved'));
  578. //si entra aqui, significa que va agrupar los estudiantes para un solo dominio.
  579. if (isset($course_code->typ_semester_course_id)) {
  580. $table_per_student = $table_per_student->groupBy('students.id');
  581. $students = $table_per_student->get();
  582. } else {
  583. $table_per_student = $table_per_student->addSelect('outcome_id', 'outcomes.expected_outcome')
  584. ->groupBy(array('students.id', 'outcome_id'));
  585. Log::info($table_per_student->toSql());
  586. Log::info($criteria_id);
  587. Log::info($course_code->id);
  588. Log::info($program_ids);
  589. Log::info($course_code->semester_id);
  590. Log::info("Amiga ponle a br");
  591. $query = $table_per_student->get();
  592. //Aqui queremos crear un arreglo que sea
  593. /*
  594. students = {
  595. outcome_id: {
  596. 'students':{
  597. {student x, crit_atttempted, crit_achieved},
  598. {student x2, crit_attempted, crit_achieved},
  599. ...
  600. }
  601. 'calculation':{
  602. student_achieved, student_attempted
  603. }
  604. }
  605. outcome_id2:
  606. {
  607. 'students':{
  608. {student x, crit_atttempted, crit_achieved},
  609. {student x2, crit_attempted, crit_achieved},
  610. ...
  611. }
  612. 'calculations':{
  613. student_achieved, student_attempted
  614. }
  615. },
  616. }
  617. */
  618. $students = [];
  619. foreach ($query as $q) {
  620. if (array_key_exists($q->outcome_id, $students)) {
  621. $students[$q->outcome_id]['students'][] = $q;
  622. } else {
  623. $students[$q->outcome_id]['students'] = [];
  624. $students[$q->outcome_id]['students'][] = $q;
  625. }
  626. $criteria_attempted = $q->criteria_attempted;
  627. $criteria_achieved = $q->criteria_achieved;
  628. $expected_outcome = $q->expected_outcome;
  629. //PREGUNTA??? ES 66.66??????
  630. $achieved = 0;
  631. if ($criteria_attempted != 0 && $criteria_achieved / $criteria_attempted * 100 >= $expected_outcome) {
  632. $achieved = 1;
  633. }
  634. if (isset($students[$q->outcome_id]['calculations']) && $criteria_attempted) {
  635. $students[$q->outcome_id]['calculations']['student_attempted'] += 1;
  636. $students[$q->outcome_id]['calculations']['student_achieved'] += $achieved;
  637. } else if ($criteria_attempted) {
  638. $students[$q->outcome_id]['calculations']['student_attempted'] = 1;
  639. $students[$q->outcome_id]['calculations']['student_achieved'] = $achieved;
  640. }
  641. }
  642. }
  643. //Log::info($criteria_id);
  644. // Log::info('los courseCode');
  645. // Log::info(array($course_code));
  646. //Log::info('los programas');
  647. //Log::info($program_ids);
  648. //Log::info("crit");
  649. //Log::info($criteria_id);
  650. //Log::info($table_per_student->toSql());
  651. //Log::info($students);
  652. return $students;
  653. }
  654. }
  655. public function getReportObject()
  656. {
  657. $reportObject = new stdClass();
  658. $reportObject->criteriaReport = DB::table('courses')
  659. ->join('activities', 'activities.course_id', '=', 'courses.id')
  660. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  661. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  662. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  663. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  664. ->join('assessments', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  665. ->select(DB::raw(
  666. "count(assessments.score) as assessed,
  667. count(case when assessments.score >= rubrics.expected_points then 1 else NULL end) as achieved,
  668. criteria.*"
  669. ))
  670. ->where('courses.id', $this->id)
  671. ->groupBy('criteria.id')
  672. ->get();
  673. //Log::info($this->id);
  674. $reportObject->outcomesReport = DB::table('courses')
  675. ->join('activities', 'activities.course_id', '=', 'courses.id')
  676. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  677. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  678. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  679. ->join('assessments', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  680. ->join('criterion_objective_outcome', 'activity_criterion.criterion_id', '=', 'criterion_objective_outcome.criterion_id')
  681. ->join('outcomes', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id')
  682. ->select(
  683. DB::raw(
  684. "student_id, outcomes.name, outcomes.id,
  685. count(activity_criterion.criterion_id) as attempted,
  686. count(case when assessments.score >= rubrics.expected_points then 1 else null end) as achieved"
  687. )
  688. )
  689. ->where('courses.id', $this->id)
  690. ->groupBy(array('outcome_id', 'student_id'))
  691. ->get();
  692. return $reportObject;
  693. }
  694. public function outcomes_ach()
  695. {
  696. $criteria = DB::table('criteria')
  697. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  698. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  699. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  700. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  701. ->join('courses', 'activities.course_id', '=', 'courses.id')
  702. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  703. ->where('courses.id', '=', $this->id)
  704. ->where('activities.draft', 0)
  705. ->where('activities.diagnostic', 0)
  706. ->select(
  707. 'criteria.id',
  708. 'rubrics.expected_percentage as expected_percentage_students_achieving'/*'expected_percentage_students_achieving',*/,
  709. 'activity_criterion.activity_id'
  710. )
  711. ->distinct()
  712. ->get();
  713. // Old data was formed like { "outcome1": cuantity_of_criteria_associated_passed, "outcome2":cuantity_of_criteria}
  714. $outcomes_achieved = [];
  715. foreach ($criteria as $criterion) {
  716. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  717. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  718. //Log::info('estamos aqui');
  719. Log::info($students_attempted);
  720. if ($students_attempted) {
  721. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  722. } else {
  723. $percentage_students_who_achieved = 0;
  724. }
  725. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  726. $outcomes = DB::table('criterion_objective_outcome')
  727. ->where('criterion_id', $criterion->id)
  728. ->select('outcome_id')
  729. ->distinct()
  730. ->lists('outcome_id');
  731. foreach ($outcomes as $outcome_id) {
  732. if (array_key_exists($outcome_id, $outcomes_achieved)) $outcomes_achieved[$outcome_id] += 1;
  733. else $outcomes_achieved[$outcome_id] = 1;
  734. }
  735. }
  736. }
  737. Log::info(
  738. "OUTCOMES_ACHIEVED: "
  739. );
  740. Log::info(
  741. $outcomes_achieved
  742. );
  743. return $outcomes_achieved;
  744. }
  745. public function someSectionAssessed()
  746. {
  747. $all_sections = DB::table('courses')
  748. ->join('activities', 'activities.course_id', '=', 'courses.id')
  749. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  750. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  751. ->where('courses.name', $this->name)
  752. ->where('code', $this->code)
  753. ->where('number', $this->number)
  754. ->where('semester_id', $this->semester_id)
  755. ->where(
  756. 'program_id',
  757. $this->program_id
  758. )
  759. ->where('activities.draft', 0)
  760. ->where('activities.diagnostic', 0)
  761. ->groupBy('courses.id')
  762. ->count();
  763. return $all_sections;
  764. }
  765. public function isAssessed()
  766. {
  767. $assessed = DB::table('assessments')
  768. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  769. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  770. ->where('activities.course_id', $this->id)
  771. ->count();
  772. // Log::info("aqui".$assessed);
  773. if ($assessed) return true;
  774. else return false;
  775. }
  776. public function outcomes_achieved()
  777. {
  778. $criteria = DB::table('criteria')
  779. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  780. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  781. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  782. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  783. ->join('courses', 'activities.course_id', '=', 'courses.id')
  784. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  785. ->where('courses.id', '=', $this->id)
  786. ->select(
  787. 'criteria.id',
  788. 'rubrics.expected_percentage as expected_percentage_students_achieving'/*'expected_percentage_students_achieving',*/,
  789. 'activity_criterion.activity_id'
  790. )
  791. ->distinct()
  792. ->get();
  793. $conteo = 0;
  794. foreach ($criteria as $criterion) {
  795. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  796. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  797. if ($students_attempted) {
  798. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  799. } else {
  800. $percentage_students_who_achieved = 0;
  801. }
  802. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  803. $conteo++;
  804. }
  805. }
  806. return $conteo;
  807. }
  808. public function semester()
  809. {
  810. return $this->belongsTo('Semester');
  811. }
  812. public function sectionsWithOutcome($outcome_id)
  813. {
  814. return Course::has('activities')->with(array('activities' => function ($query) use (&$outcome_id) {
  815. $query->where('outcomes_attempted', 'not like', '\'%"' . $outcome_id . '":0%\'');
  816. }))->where('code', $this->code)->where('number', $this->number)->get();
  817. }
  818. }