No Description

Course.php 39KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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. ->whereIn('students.program_id', $program_ids)
  508. ->where('semester_id', $course_code->semester_id)
  509. ->where('semesters.is_visible', '=', 1)
  510. ->where('activities.draft', 0)
  511. ->where('activities.diagnostic', 0)
  512. ->select('student_id', 'students.program_id', 'semesters.code', 'outcome_id', 'criterion_objective_outcome.criterion_id', 'score', 'expected_points')
  513. ->distinct()
  514. ->get();
  515. //Table
  516. /* Student criterion_attempted criterion passed
  517. * 1 2 1
  518. * 2 7 4
  519. *
  520. *
  521. */
  522. $table_per_student = DB::table('course_student')
  523. //->join('program_student_semester', 'program_student_semester.student_id', '=', 'course_student.student_id')
  524. ->join("students", 'course_student.student_id', '=', 'students.id')
  525. ->join('courses', 'courses.id', '=', 'course_student.course_id')
  526. ->join('activities', 'activities.course_id', '=', 'courses.id')
  527. ->join('activity_criterion', 'activities.id', '=', 'activity_criterion.activity_id')
  528. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  529. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  530. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  531. ->join('assessments', function ($join) {
  532. $join->on("assessments.student_id", '=', 'course_student.student_id')
  533. ->on('assessments.activity_criterion_id', '=', 'activity_criterion.id');
  534. })
  535. ->where('activities.draft', 0)
  536. ->where('activities.diagnostic', 0);
  537. // si typ_semester_course esta prendido
  538. // o, si with_program_report está prendido
  539. //, then significa que es de los estudiantes del programa
  540. //
  541. if (isset($course_code->typ_semester_course_id)) {
  542. $table_per_student = $table_per_student->join('program_student_semester', 'program_student_semester.student_id', '=', 'course_student.student_id')
  543. ->whereIn('program_student_semester.program_id', $program_ids)
  544. ->where('program_student_semester.semester_id', $course_code->semester_id);
  545. }
  546. if (isset($course_code->with_program_report)) {
  547. $table_per_student = $table_per_student->join('program_student_semester', 'program_student_semester.student_id', '=', 'course_student.student_id')
  548. ->whereIn('program_student_semester.program_id', $program_ids)
  549. ->where('program_student_semester.semester_id', $course_code->semester_id);
  550. }
  551. //si entra aqui, no especifica dominio, y hay que join todos los dominios
  552. if (!isset($course_code->typ_semester_course_id)) {
  553. $table_per_student = $table_per_student->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'activity_criterion.criterion_id');
  554. }
  555. //si estan grouped
  556. if (isset($course_code->grouped)) {
  557. $table_per_student = $table_per_student->where('courses.semester_id', $course_code->semester_id)
  558. ->where('courses.code', $course_code->code)
  559. ->where('courses.number', $course_code->number);
  560. }
  561. //si estan solo
  562. else {
  563. $table_per_student = $table_per_student->where('courses.id', $course_code->id);
  564. }
  565. ///// significa que depende
  566. /////->where('courses.semester_id', $course_code->semester_id)
  567. //->whereIn('courses.program_id', $program_ids)
  568. /////->where('courses.code', $course_code->code)
  569. /////->where('courses.number', $course_code->number)
  570. //->whereIn('program_student_semester.program_id', $program_ids)
  571. $table_per_student = $table_per_student->whereIn('activity_criterion.criterion_id', $criteria_id)
  572. //->where('program_student_semester.semester_id', $course_code->semester_id)
  573. ->select('students.number as student_id')
  574. ->addSelect(DB::raw('count(assessments.activity_criterion_id) as criteria_attempted'))
  575. ->addSelect(DB::raw('count(case when assessments.score>=rubrics.expected_points then 1 else NULL end) as criteria_achieved'));
  576. //si entra aqui, significa que va agrupar los estudiantes para un solo dominio.
  577. if (isset($course_code->typ_semester_course_id)) {
  578. $table_per_student = $table_per_student->groupBy('students.id');
  579. $students = $table_per_student->get();
  580. } else {
  581. $table_per_student = $table_per_student->addSelect('outcome_id')
  582. ->groupBy(array('students.id', 'outcome_id'));
  583. Log::info($table_per_student->toSql());
  584. Log::info($criteria_id);
  585. Log::info($course_code->id);
  586. Log::info($program_ids);
  587. Log::info($course_code->semester_id);
  588. Log::info("Amiga ponle a br");
  589. $query = $table_per_student->get();
  590. //Aqui queremos crear un arreglo que sea
  591. /*
  592. students = {
  593. outcome_id: {
  594. 'students':{
  595. {student x, crit_atttempted, crit_achieved},
  596. {student x2, crit_attempted, crit_achieved},
  597. ...
  598. }
  599. 'calculation':{
  600. student_achieved, student_attempted
  601. }
  602. }
  603. outcome_id2:
  604. {
  605. 'students':{
  606. {student x, crit_atttempted, crit_achieved},
  607. {student x2, crit_attempted, crit_achieved},
  608. ...
  609. }
  610. 'calculations':{
  611. student_achieved, student_attempted
  612. }
  613. },
  614. }
  615. */
  616. $students = [];
  617. foreach ($query as $q) {
  618. if (array_key_exists($q->outcome_id, $students)) {
  619. $students[$q->outcome_id]['students'][] = $q;
  620. } else {
  621. $students[$q->outcome_id]['students'] = [];
  622. $students[$q->outcome_id]['students'][] = $q;
  623. }
  624. $criteria_attempted = $q->criteria_attempted;
  625. $criteria_achieved = $q->criteria_achieved;
  626. //PREGUNTA??? ES 66.67??????
  627. $achieved = 0;
  628. if ($criteria_attempted != 0 && $criteria_achieved / $criteria_attempted * 100 >= 66.67) {
  629. $achieved = 1;
  630. }
  631. if (isset($students[$q->outcome_id]['calculations']) && $criteria_attempted) {
  632. $students[$q->outcome_id]['calculations']['student_attempted'] += 1;
  633. $students[$q->outcome_id]['calculations']['student_achieved'] += $achieved;
  634. } else if ($criteria_attempted) {
  635. $students[$q->outcome_id]['calculations']['student_attempted'] = 1;
  636. $students[$q->outcome_id]['calculations']['student_achieved'] = $achieved;
  637. }
  638. }
  639. }
  640. //Log::info($criteria_id);
  641. // Log::info('los courseCode');
  642. // Log::info(array($course_code));
  643. //Log::info('los programas');
  644. //Log::info($program_ids);
  645. //Log::info("crit");
  646. //Log::info($criteria_id);
  647. //Log::info($table_per_student->toSql());
  648. //Log::info($students);
  649. return $students;
  650. }
  651. }
  652. public function getReportObject()
  653. {
  654. $reportObject = new stdClass();
  655. $reportObject->criteriaReport = DB::table('courses')
  656. ->join('activities', 'activities.course_id', '=', 'courses.id')
  657. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  658. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  659. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  660. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  661. ->join('assessments', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  662. ->select(DB::raw(
  663. "count(assessments.score) as assessed,
  664. count(case when assessments.score >= rubrics.expected_points then 1 else NULL end) as achieved,
  665. criteria.*"
  666. ))
  667. ->where('courses.id', $this->id)
  668. ->groupBy('criteria.id')
  669. ->get();
  670. //Log::info($this->id);
  671. $reportObject->outcomesReport = DB::table('courses')
  672. ->join('activities', 'activities.course_id', '=', 'courses.id')
  673. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  674. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  675. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  676. ->join('assessments', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  677. ->join('criterion_objective_outcome', 'activity_criterion.criterion_id', '=', 'criterion_objective_outcome.criterion_id')
  678. ->join('outcomes', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id')
  679. ->select(
  680. DB::raw(
  681. "student_id, outcomes.name, outcomes.id,
  682. count(activity_criterion.criterion_id) as attempted,
  683. count(case when assessments.score >= rubrics.expected_points then 1 else null end) as achieved"
  684. )
  685. )
  686. ->where('courses.id', $this->id)
  687. ->groupBy(array('outcome_id', 'student_id'))
  688. ->get();
  689. return $reportObject;
  690. }
  691. public function outcomes_ach()
  692. {
  693. $criteria = DB::table('criteria')
  694. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  695. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  696. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  697. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  698. ->join('courses', 'activities.course_id', '=', 'courses.id')
  699. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  700. ->where('courses.id', '=', $this->id)
  701. ->where('activities.draft', 0)
  702. ->where('activities.diagnostic', 0)
  703. ->select(
  704. 'criteria.id',
  705. 'rubrics.expected_percentage as expected_percentage_students_achieving'/*'expected_percentage_students_achieving',*/,
  706. 'activity_criterion.activity_id'
  707. )
  708. ->distinct()
  709. ->get();
  710. // Old data was formed like { "outcome1": cuantity_of_criteria_associated_passed, "outcome2":cuantity_of_criteria}
  711. $outcomes_achieved = [];
  712. foreach ($criteria as $criterion) {
  713. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  714. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  715. //Log::info('estamos aqui');
  716. Log::info($students_attempted);
  717. if ($students_attempted) {
  718. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  719. } else {
  720. $percentage_students_who_achieved = 0;
  721. }
  722. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  723. $outcomes = DB::table('criterion_objective_outcome')
  724. ->where('criterion_id', $criterion->id)
  725. ->select('outcome_id')
  726. ->distinct()
  727. ->lists('outcome_id');
  728. foreach ($outcomes as $outcome_id) {
  729. if (array_key_exists($outcome_id, $outcomes_achieved)) $outcomes_achieved[$outcome_id] += 1;
  730. else $outcomes_achieved[$outcome_id] = 1;
  731. }
  732. }
  733. }
  734. Log::info(
  735. "OUTCOMES_ACHIEVED: "
  736. );
  737. Log::info(
  738. $outcomes_achieved
  739. );
  740. return $outcomes_achieved;
  741. }
  742. public function someSectionAssessed()
  743. {
  744. $all_sections = DB::table('courses')
  745. ->join('activities', 'activities.course_id', '=', 'courses.id')
  746. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  747. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  748. ->where('courses.name', $this->name)
  749. ->where('code', $this->code)
  750. ->where('number', $this->number)
  751. ->where('semester_id', $this->semester_id)
  752. ->where(
  753. 'program_id',
  754. $this->program_id
  755. )
  756. ->where('activities.draft', 0)
  757. ->where('activities.diagnostic', 0)
  758. ->groupBy('courses.id')
  759. ->count();
  760. return $all_sections;
  761. }
  762. public function isAssessed()
  763. {
  764. $assessed = DB::table('assessments')
  765. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  766. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  767. ->where('activities.course_id', $this->id)
  768. ->count();
  769. // Log::info("aqui".$assessed);
  770. if ($assessed) return true;
  771. else return false;
  772. }
  773. public function outcomes_achieved()
  774. {
  775. $criteria = DB::table('criteria')
  776. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  777. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  778. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  779. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  780. ->join('courses', 'activities.course_id', '=', 'courses.id')
  781. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  782. ->where('courses.id', '=', $this->id)
  783. ->select(
  784. 'criteria.id',
  785. 'rubrics.expected_percentage as expected_percentage_students_achieving'/*'expected_percentage_students_achieving',*/,
  786. 'activity_criterion.activity_id'
  787. )
  788. ->distinct()
  789. ->get();
  790. $conteo = 0;
  791. foreach ($criteria as $criterion) {
  792. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  793. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  794. if ($students_attempted) {
  795. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  796. } else {
  797. $percentage_students_who_achieved = 0;
  798. }
  799. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  800. $conteo++;
  801. }
  802. }
  803. return $conteo;
  804. }
  805. public function semester()
  806. {
  807. return $this->belongsTo('Semester');
  808. }
  809. public function sectionsWithOutcome($outcome_id)
  810. {
  811. return Course::has('activities')->with(array('activities' => function ($query) use (&$outcome_id) {
  812. $query->where('outcomes_attempted', 'not like', '\'%"' . $outcome_id . '":0%\'');
  813. }))->where('code', $this->code)->where('number', $this->number)->get();
  814. }
  815. }