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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. ->whereNotNull('score')
  146. ->where('courses.semester_id', $this->semester_id)
  147. ->whereIn('courses.program_id', $program_ids)
  148. ->where('courses.code', $this->code)
  149. ->where('courses.number', $this->number)
  150. ->whereIn('program_student_semester.program_id', $program_ids)
  151. ->whereIn('activity_criterion.criterion_id', $criteria_id)
  152. ->whereIn('criteria.id', $criteria_id)
  153. ->where('program_student_semester.semester_id', $this->semester_id)
  154. ->select('criteria.*')
  155. ->addSelect(DB::raw("{$this->typ_semester_course_id} as typ_semester_course_id"))
  156. ->addSelect(DB::raw('count(case when assessments.score>= rubrics.expected_points then 1 else null end) as criteria_achieved'))
  157. ->addSelect(DB::raw('count(assessments.activity_criterion_id) as criteria_attempted'))
  158. ->groupBy('criteria.id')
  159. ->get();
  160. }
  161. //paired_criteria solo funciona para plan ahora_mismo, y esta puerquito
  162. public function getPairedCriteriaAttribute()
  163. {
  164. if (isset($this->typ_semester_course_id)) {
  165. return Criterion::join("annual_plan_objective", 'annual_plan_objective.criteria_id', '=', 'criteria.id')
  166. ->where('typ_semester_course_id', $this->typ_semester_course_id)
  167. ->select(
  168. 'criteria.*',
  169. 'typ_semester_course_id'
  170. )
  171. ->get();
  172. /*.
  173. $criteria_info = $this->getCriteriaPlanReport($this);
  174. $criteria_list = [];
  175. foreach ($criteria_info as $index => $criterion) {
  176. $criteria_list[$index] = Criterion::where("id", $criterion->id)->first();
  177. $criteria_list[$index]->setAttribute("criteria_attempted", $criterion->criteria_attempted);
  178. $criteria_list[$index]->setAttribute("criteria_achieved", $criterion->criteria_achieved);
  179. }
  180. return $criteria_list;
  181. }*/
  182. }
  183. return null;
  184. }
  185. public function assessedActivities()
  186. {
  187. // return $this->hasMany('Activity')->whereNotNull('activities.outcomes_attempted')->orderBy('date', 'asc');
  188. $activitiesAssessed = DB::table('activities')
  189. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  190. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  191. ->whereNotNull('score')
  192. ->where('activities.course_id', $this->id)
  193. ->select('activities.id')
  194. ->distinct()
  195. ->lists('activities.id');
  196. //Log::info($activitiesAssessed);
  197. return $this->hasMany('Activity')->whereIn('activities.id', $activitiesAssessed)->orderBy('date', 'asc');
  198. // return $this->hasMany('Activity')->where('activities.draft', 0)->orderBy('date', 'asc');
  199. }
  200. public static function get_associated_objective_outcome($typ_semester_course_id)
  201. {
  202. return DB::table('typ_semester_outcome')->join('typ_semester_objectives', 'typ_semester_outcome.id', '=', 'typ_semester_objectives.typ_semester_outcome_id')
  203. ->join('typ_semester_courses', 'typ_semester_courses.typ_semester_objective_id', '=', 'typ_semester_objectives.id')
  204. ->where('typ_semester_courses.id', $typ_semester_course_id);
  205. }
  206. public static function getFutureTypSemesterCourses($typ_semester_course_id, $program_id)
  207. {
  208. $typ_info = new stdClass();
  209. $typ_info->future_semesters = DB::table('typ_semester_outcome')
  210. ->join('typ_semester_objectives', 'typ_semester_outcome.id', '=', 'typ_semester_objectives.typ_semester_outcome_id')
  211. ->join('typ_semester_courses', 'typ_semester_courses.typ_semester_objective_id', '=', 'typ_semester_objectives.id')
  212. ->join('typ_semester_outcome as typ_o_2', 'typ_o_2.outcome_id', '=', 'typ_semester_outcome.outcome_id')
  213. ->join('semesters as sem1', 'sem1.id', '=', 'typ_semester_outcome.semester_id')
  214. ->join('typ_semester_objectives as typ_ob_2', function ($query) {
  215. $query->on('typ_ob_2.typ_semester_outcome_id', '=', 'typ_o_2.id')
  216. ->on('typ_semester_objectives.objective_id', '=', 'typ_ob_2.objective_id');
  217. })
  218. ->join('typ_semester_courses as typ_course_2', function ($join) {
  219. $join->on('typ_course_2.typ_semester_objective_id', '=', 'typ_ob_2.id')
  220. ->on('typ_semester_courses.course_id', '=', 'typ_course_2.course_id');
  221. })
  222. ->join('typ_program', 'typ_o_2.typ_program_id', '=', 'typ_program.id')
  223. ->join('semesters', 'semesters.id', '=', 'typ_o_2.semester_id')
  224. ->leftJoin('annual_plan_transformative', 'annual_plan_transformative.typ_semester_course_id', '=', 'typ_course_2.id')
  225. ->leftJoin('transformative_actions', 'transformative_actions.id', '=', 'annual_plan_transformative.trans_id')
  226. ->where('typ_semester_courses.id', $typ_semester_course_id)
  227. ->where('typ_program.program_id', $program_id)
  228. ->whereRaw('sem1.code < semesters.code')
  229. ->groupBy('typ_o_2.semester_id')
  230. ->select(
  231. 'typ_o_2.semester_id',
  232. 'typ_o_2.outcome_id',
  233. 'typ_o_2.id',
  234. 'typ_semester_courses.course_id',
  235. 'typ_semester_objectives.objective_id',
  236. 'typ_ob_2.id as typ_future_objective_id',
  237. 'typ_course_2.id as typ_future_course_id',
  238. 'semesters.name as semester_name',
  239. 'semesters.code as semester_code',
  240. 'transformative_actions.id as trans_id',
  241. 'transformative_actions.type_of_TA',
  242. 'transformative_actions.at_text',
  243. 'transformative_actions.description'
  244. ); //->get();
  245. //Log::info('El sql');
  246. $typ_info->future_transformative_actions = DB::table('typ_semester_outcome')
  247. ->join('typ_semester_objectives', 'typ_semester_outcome.id', '=', 'typ_semester_objectives.typ_semester_outcome_id')
  248. ->join('typ_semester_courses', 'typ_semester_courses.typ_semester_objective_id', '=', 'typ_semester_objectives.id')
  249. ->join('typ_semester_outcome as typ_o_2', 'typ_o_2.outcome_id', '=', 'typ_semester_outcome.outcome_id')
  250. ->join('semesters as sem1', 'sem1.id', '=', 'typ_semester_outcome.semester_id')
  251. ->join('typ_semester_objectives as typ_ob_2', function ($query) {
  252. $query->on('typ_ob_2.typ_semester_outcome_id', '=', 'typ_o_2.id')
  253. ->on('typ_semester_objectives.objective_id', '=', 'typ_ob_2.objective_id');
  254. })
  255. ->join('typ_semester_courses as typ_course_2', function ($join) {
  256. $join->on('typ_course_2.typ_semester_objective_id', '=', 'typ_ob_2.id')
  257. ->on('typ_semester_courses.course_id', '=', 'typ_course_2.course_id');
  258. })
  259. ->join('typ_program', 'typ_o_2.typ_program_id', '=', 'typ_program.id')
  260. ->join('semesters', 'semesters.id', '=', 'typ_o_2.semester_id')
  261. ->leftJoin('annual_plan_transformative', 'annual_plan_transformative.typ_semester_course_id', '=', 'typ_course_2.id')
  262. ->leftJoin('transformative_actions', 'transformative_actions.id', '=', 'annual_plan_transformative.trans_id')
  263. ->where('typ_semester_courses.id', $typ_semester_course_id)
  264. ->where('typ_program.program_id', $program_id)
  265. ->whereRaw('sem1.code < semesters.code')
  266. //->groupBy('typ_o_2.semester_id')
  267. ->select(
  268. 'typ_o_2.semester_id',
  269. 'typ_o_2.outcome_id',
  270. 'typ_o_2.id',
  271. 'typ_semester_courses.course_id',
  272. 'typ_semester_objectives.objective_id',
  273. 'typ_ob_2.id as typ_future_objective_id',
  274. 'typ_course_2.id as typ_future_course_id',
  275. 'semesters.name as semester_name',
  276. 'semesters.code as semester_code',
  277. 'transformative_actions.id as trans_id',
  278. 'transformative_actions.type_of_TA',
  279. 'transformative_actions.at_text',
  280. 'transformative_actions.description'
  281. );
  282. //Log::info("typ_info");
  283. //Log::info($typ_info->future_semesters->toSql());
  284. $typ_info->future_semesters = $typ_info->future_semesters->get();
  285. //Log::info($typ_info->future_transformative_actions->toSql());
  286. $typ_info->future_transformative_actions = $typ_info->future_transformative_actions->get();
  287. //Log::info($typ_info->toSql());
  288. Log::info($typ_semester_course_id);
  289. Log::info($program_id);
  290. return $typ_info;
  291. }
  292. public function publishedActivities()
  293. {
  294. $activitiesAssessed = DB::table('activities')
  295. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  296. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  297. ->whereNotNull('score')
  298. ->where('activities.course_id', $this->id)
  299. ->where('activities.draft', 0)
  300. ->where('activities.diagnostic', 0)
  301. ->select('activities.id')
  302. ->distinct()
  303. ->lists('activities.id');
  304. //$this->hasMany('Activity')->whereNotNull('activities.outcomes_attempted')->where('activities.draft', 0)->orderBy('date', 'asc');
  305. return $this->hasMany('Activity')->whereIn('activities.id', $activitiesAssessed)->orderBy('date', 'asc');
  306. }
  307. public function diagnosticActivities()
  308. {
  309. $activitiesAssessed = DB::table('activities')
  310. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  311. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  312. ->whereNotNull('score')
  313. ->where('activities.course_id', $this->id)
  314. ->where('activities.draft', 0)
  315. ->where('activities.diagnostic', 1)
  316. ->select('activities.id')
  317. ->distinct()
  318. ->lists('activities.id');
  319. //$this->hasMany('Activity')
  320. return $this->hasMany('Activity')->whereIn('activities.id', $activitiesAssessed)->orderBy('date', 'asc');
  321. }
  322. public function outcomes_attempted()
  323. {
  324. return DB::table('courses')
  325. ->join('activities', 'activities.course_id', '=', 'courses.id')
  326. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  327. ->join('criteria', 'activity_criterion.criterion_id', '=', 'criteria.id')
  328. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  329. ->where('courses.id', '=', $this->id)
  330. ->select('criterion_objective_outcome.outcome_id')
  331. ->distinct()
  332. ->orderBy('criterion_objective_outcome.outcome_id')
  333. ->get();
  334. // 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
  335. // return $this->id;
  336. }
  337. public function outcomes_att()
  338. {
  339. $criteria_attempted = DB::table('courses')
  340. ->join('activities', 'activities.course_id', '=', 'courses.id')
  341. ->join('activity_criterion', 'activities.id', '=', 'activity_criterion.activity_id')
  342. ->select('criterion_id', 'activity_criterion.id as activity_criterion_id')
  343. ->where('courses.id', $this->id)
  344. ->where('draft', 0)
  345. ->where('activities.diagnostic', 0)
  346. ->get();
  347. // Old data was formed like { "outcome1": cuantity_of_criteria_associated, "outcome2":cuantity_of_criteria}
  348. $outcomes_attempted = [];
  349. foreach ($criteria_attempted as $criterion) {
  350. $outcomes = DB::table('criterion_objective_outcome')
  351. ->where('criterion_id', $criterion->criterion_id)
  352. ->select('outcome_id')
  353. ->distinct()
  354. ->lists('outcome_id');
  355. foreach ($outcomes as $outcome_id) {
  356. if (array_key_exists($outcome_id, $outcomes_attempted)) $outcomes_attempted[$outcome_id] += 1;
  357. else $outcomes_attempted[$outcome_id] = 1;
  358. }
  359. }
  360. Log::info("outcomes_attempted");
  361. Log::info($outcomes_attempted);
  362. return $outcomes_attempted;
  363. }
  364. public static function getCriteriaPlanReport($course_code = null)
  365. {
  366. if ($course_code) {
  367. $criteria_id = DB::table('annual_plan_objective')
  368. ->where('typ_semester_course_id', $course_code->typ_semester_course_id)
  369. ->lists('criteria_id');
  370. //Log::info($course_code->typ_semester_course_id);
  371. //Log::info("dame ese id");
  372. $program = Program::where('id', $course_code->program_id)->first();
  373. // Log::info($program->code);
  374. //Si es un programa como core, enseña todos los estudiantes
  375. if ($program->code == "") {
  376. $program_ids = DB::table('programs')
  377. ->where('school_id', $program->school->id)
  378. ->lists('programs.id');
  379. //Log::info("estamos");
  380. } else $program_ids = array($program->id);
  381. /*tabla es asi por curso codigo
  382. *
  383. *criterioDelPlan EstudiantesDelProgramaQuePasaron EstudiantesqueIntentaron
  384. *
  385. * criterio1 3 5
  386. * criterio2 2 2
  387. *
  388. *
  389. */
  390. $table_per_criterion = DB::table('program_student_semester')
  391. ->join('course_student', 'program_student_semester.student_id', '=', 'course_student.student_id')
  392. ->join('courses', 'courses.id', '=', 'course_student.course_id')
  393. ->join('activities', 'activities.course_id', '=', 'courses.id')
  394. ->join('activity_criterion', 'activities.id', '=', 'activity_criterion.activity_id')
  395. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  396. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  397. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  398. ->join('assessments', function ($join) {
  399. $join->on("assessments.student_id", '=', 'program_student_semester.student_id')
  400. ->on('assessments.activity_criterion_id', '=', 'activity_criterion.id');
  401. })
  402. ->whereNotNull('score')
  403. ->where('courses.semester_id', $course_code->semester_id)
  404. ->whereIn('courses.program_id', $program_ids)
  405. ->where('courses.code', $course_code->code)
  406. ->where('courses.number', $course_code->number)
  407. ->whereIn('program_student_semester.program_id', $program_ids)
  408. ->whereIn('activity_criterion.criterion_id', $criteria_id)
  409. ->whereIn('criteria.id', $criteria_id)
  410. ->where('program_student_semester.semester_id', $course_code->semester_id)
  411. ->select('criteria.*')
  412. ->addSelect(DB::raw('count(case when assessments.score>= rubrics.expected_points then 1 else null end) as criteria_achieved'))
  413. ->addSelect(DB::raw('count(assessments.activity_criterion_id) as criteria_attempted'))
  414. ->groupBy('criteria.id');
  415. /*Log::info($table_per_criterion->toSql());
  416. Log::info(array($course_code));
  417. Log::info($program_ids);
  418. Log::info($criteria_id);
  419. Log::info('mera 1');
  420. Log::info(array($course_code));
  421. Log::info($table_per_criterion->get()); */
  422. return $table_per_criterion->get();
  423. }
  424. }
  425. //dirty code repeated code
  426. public function getStudentReportForOutcomeAttribute()
  427. {
  428. return $this->getStudentReportForOutcome($this);
  429. }
  430. //cuantos estudiantes tiene este cursos que son del programa
  431. public function getQuantityAttribute()
  432. {
  433. $query = DB::table('courses')
  434. ->join('course_student', 'course_student.course_id', '=', 'courses.id')
  435. ->join('program_student_semester as pcs', function ($j) {
  436. $j->on('courses.program_id', '=', 'pcs.program_id')
  437. ->on('course_student.student_id', '=', 'pcs.student_id')
  438. ->on('courses.semester_id', '=', 'pcs.semester_id');
  439. });
  440. if ($this->grouped) {
  441. $query = $query->where('courses.name', $this->name)
  442. ->where('courses.code', $this->code)
  443. ->where('courses.number', $this->number)
  444. ->where('courses.semester_id', $this->semester_id);
  445. } else {
  446. $query = $query->where("courses.id", $this->id);
  447. }
  448. $query = $query->select(DB::raw("count( distinct pcs.student_id) as quantity"));
  449. $query = $query->first();
  450. if (isset($query)) {
  451. return $query->quantity;
  452. }
  453. return 0;
  454. }
  455. //user id is for groupped professor sections. so el query es code, number, semester y user_id
  456. public static function getStudentReportForOutcome($course_code = null, $user_id = null)
  457. {
  458. if ($course_code) {
  459. //Log::info('entré');
  460. //Si entra aqui, significa que son los criterios dentro del annual plan, bajo un dominio en particular.
  461. if (isset($course_code->typ_semester_course_id)) {
  462. $criteria_id = DB::table('annual_plan_objective')
  463. ->where('typ_semester_course_id', $course_code->typ_semester_course_id)
  464. ->lists('criteria_id');
  465. }
  466. //si entra aqui, estamos buscando del curso (o grupo de cursos independientes
  467. else {
  468. $criteria_id = DB::table('courses')
  469. ->join('activities', 'courses.id', '=', 'activities.course_id')
  470. ->join('activity_criterion', 'activities.id', '=', 'activity_criterion.activity_id');
  471. //if this is 1, significa que son todos los cursos de ese semestre con nombre.
  472. if (isset($course_code->grouped)) {
  473. $criteria_id = $criteria_id->where('courses.name', $course_code->name)
  474. ->where('courses.code', $course_code->code)
  475. ->where('courses.number', $course_code->number)
  476. ->where('courses.semester_id', $course_code->semester_id);
  477. //if user_id is set, this means its goruped courses of said professor.
  478. if ($user_id) {
  479. $criteria_id = $criteria_id->where('courses.user_id', $user_id);
  480. }
  481. $criteria_id = $criteria_id
  482. ->groupBy('activity_criterion.criterion_id')
  483. ->lists('activity_criterion.criterion_id');
  484. }
  485. //si es un solo curso.
  486. else {
  487. $criteria_id = $criteria_id->where('courses.id', $course_code->id)
  488. ->groupBy('activity_criterion.criterion_id')
  489. ->lists('activity_criterion.criterion_id');
  490. }
  491. }
  492. //Log::info($criteria_id);
  493. $program = Program::where('id', $course_code->program_id)->first();
  494. // // Log::info($program->code);
  495. if ($program->code == "") {
  496. $program_ids = DB::table('programs')
  497. ->where('school_id', $program->school->id)
  498. ->lists('programs.id');
  499. // Log::info("estamos");
  500. } else $program_ids = array($program->id);
  501. // Log::info(array($course_code));
  502. //ESTA ES LA TABLA QUERY PPOR STUDENT
  503. $table_per_student = DB::table('assessments')
  504. ->join('activity_criterion', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  505. ->join('activities', 'activities.id', '=', 'activity_criterion.activity_id')
  506. ->join('courses', 'courses.id', '=', 'activities.course_id')
  507. ->join('students', 'students.id', '=', 'assessments.student_id')
  508. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'activity_criterion.criterion_id')
  509. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  510. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  511. ->join('semesters', 'semesters.id', '=', 'courses.semester_id')
  512. ->join('outcomes', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id')
  513. ->whereNotNull('score')
  514. ->whereIn('students.program_id', $program_ids)
  515. ->where('semester_id', $course_code->semester_id)
  516. ->where('semesters.is_visible', '=', 1)
  517. ->where('activities.draft', 0)
  518. ->where('activities.diagnostic', 0)
  519. ->select('student_id', 'students.program_id', 'semesters.code', 'outcome_id', 'outcomes.expected_outcome', 'criterion_objective_outcome.criterion_id', 'score', 'expected_points')
  520. ->distinct()
  521. ->get();
  522. //Table
  523. /* Student criterion_attempted criterion passed
  524. * 1 2 1
  525. * 2 7 4
  526. *
  527. *
  528. */
  529. $table_per_student = DB::table('course_student')
  530. //->join('program_student_semester', 'program_student_semester.student_id', '=', 'course_student.student_id')
  531. ->join("students", 'course_student.student_id', '=', 'students.id')
  532. ->join('courses', 'courses.id', '=', 'course_student.course_id')
  533. ->join('activities', 'activities.course_id', '=', 'courses.id')
  534. ->join('activity_criterion', 'activities.id', '=', 'activity_criterion.activity_id')
  535. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  536. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  537. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  538. ->join('assessments', function ($join) {
  539. $join->on("assessments.student_id", '=', 'course_student.student_id')
  540. ->on('assessments.activity_criterion_id', '=', 'activity_criterion.id');
  541. })
  542. ->whereNotNull('score')
  543. ->where('activities.draft', 0)
  544. ->where('activities.diagnostic', 0);
  545. // si typ_semester_course esta prendido
  546. // o, si with_program_report está prendido
  547. //, then significa que es de los estudiantes del programa
  548. //
  549. if (isset($course_code->typ_semester_course_id)) {
  550. $table_per_student = $table_per_student->join('program_student_semester', 'program_student_semester.student_id', '=', 'course_student.student_id')
  551. ->whereIn('program_student_semester.program_id', $program_ids)
  552. ->where('program_student_semester.semester_id', $course_code->semester_id);
  553. }
  554. if (isset($course_code->with_program_report)) {
  555. $table_per_student = $table_per_student->join('program_student_semester', 'program_student_semester.student_id', '=', 'course_student.student_id')
  556. ->whereIn('program_student_semester.program_id', $program_ids)
  557. ->where('program_student_semester.semester_id', $course_code->semester_id);
  558. }
  559. //si entra aqui, no especifica dominio, y hay que join todos los dominios
  560. if (!isset($course_code->typ_semester_course_id)) {
  561. $table_per_student = $table_per_student->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'activity_criterion.criterion_id')
  562. ->join('outcomes', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id');
  563. }
  564. //si estan grouped
  565. if (isset($course_code->grouped)) {
  566. $table_per_student = $table_per_student->where('courses.semester_id', $course_code->semester_id)
  567. ->where('courses.code', $course_code->code)
  568. ->where('courses.number', $course_code->number);
  569. }
  570. //si estan solo
  571. else {
  572. $table_per_student = $table_per_student->where('courses.id', $course_code->id);
  573. }
  574. ///// significa que depende
  575. /////->where('courses.semester_id', $course_code->semester_id)
  576. //->whereIn('courses.program_id', $program_ids)
  577. /////->where('courses.code', $course_code->code)
  578. /////->where('courses.number', $course_code->number)
  579. //->whereIn('program_student_semester.program_id', $program_ids)
  580. $table_per_student = $table_per_student->whereIn('activity_criterion.criterion_id', $criteria_id)
  581. //->where('program_student_semester.semester_id', $course_code->semester_id)
  582. ->select('students.number as student_id')
  583. ->addSelect(DB::raw('count(assessments.activity_criterion_id) as criteria_attempted'))
  584. ->addSelect(DB::raw('count(case when assessments.score>=rubrics.expected_points then 1 else NULL end) as criteria_achieved'));
  585. //si entra aqui, significa que va agrupar los estudiantes para un solo dominio.
  586. if (isset($course_code->typ_semester_course_id)) {
  587. $table_per_student = $table_per_student->groupBy('students.id');
  588. $students = $table_per_student->get();
  589. } else {
  590. $table_per_student = $table_per_student->addSelect('outcome_id', 'outcomes.expected_outcome')
  591. ->groupBy(array('students.id', 'outcome_id'));
  592. Log::info($table_per_student->toSql());
  593. Log::info($criteria_id);
  594. Log::info($course_code->id);
  595. Log::info($program_ids);
  596. Log::info($course_code->semester_id);
  597. Log::info("Amiga ponle a br");
  598. $query = $table_per_student->get();
  599. //Aqui queremos crear un arreglo que sea
  600. /*
  601. students = {
  602. outcome_id: {
  603. 'students':{
  604. {student x, crit_atttempted, crit_achieved},
  605. {student x2, crit_attempted, crit_achieved},
  606. ...
  607. }
  608. 'calculation':{
  609. student_achieved, student_attempted
  610. }
  611. }
  612. outcome_id2:
  613. {
  614. 'students':{
  615. {student x, crit_atttempted, crit_achieved},
  616. {student x2, crit_attempted, crit_achieved},
  617. ...
  618. }
  619. 'calculations':{
  620. student_achieved, student_attempted
  621. }
  622. },
  623. }
  624. */
  625. $students = [];
  626. foreach ($query as $q) {
  627. if (array_key_exists($q->outcome_id, $students)) {
  628. $students[$q->outcome_id]['students'][] = $q;
  629. } else {
  630. $students[$q->outcome_id]['students'] = [];
  631. $students[$q->outcome_id]['students'][] = $q;
  632. }
  633. $criteria_attempted = $q->criteria_attempted;
  634. $criteria_achieved = $q->criteria_achieved;
  635. $expected_outcome = $q->expected_outcome;
  636. //PREGUNTA??? ES 66.66??????
  637. $achieved = 0;
  638. if ($criteria_attempted != 0 && $criteria_achieved / $criteria_attempted * 100 >= $expected_outcome) {
  639. $achieved = 1;
  640. }
  641. if (isset($students[$q->outcome_id]['calculations']) && $criteria_attempted) {
  642. $students[$q->outcome_id]['calculations']['student_attempted'] += 1;
  643. $students[$q->outcome_id]['calculations']['student_achieved'] += $achieved;
  644. } else if ($criteria_attempted) {
  645. $students[$q->outcome_id]['calculations']['student_attempted'] = 1;
  646. $students[$q->outcome_id]['calculations']['student_achieved'] = $achieved;
  647. }
  648. }
  649. }
  650. //Log::info($criteria_id);
  651. // Log::info('los courseCode');
  652. // Log::info(array($course_code));
  653. //Log::info('los programas');
  654. //Log::info($program_ids);
  655. //Log::info("crit");
  656. //Log::info($criteria_id);
  657. //Log::info($table_per_student->toSql());
  658. //Log::info($students);
  659. return $students;
  660. }
  661. }
  662. public function getReportObject()
  663. {
  664. $reportObject = new stdClass();
  665. $reportObject->criteriaReport = DB::table('courses')
  666. ->join('activities', 'activities.course_id', '=', 'courses.id')
  667. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  668. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  669. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  670. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  671. ->join('assessments', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  672. ->select(DB::raw(
  673. "count(assessments.score) as assessed,
  674. count(case when assessments.score >= rubrics.expected_points then 1 else NULL end) as achieved,
  675. criteria.*"
  676. ))
  677. ->whereNotNull('score')
  678. ->where('courses.id', $this->id)
  679. ->groupBy('criteria.id')
  680. ->get();
  681. //Log::info($this->id);
  682. $reportObject->outcomesReport = DB::table('courses')
  683. ->join('activities', 'activities.course_id', '=', 'courses.id')
  684. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  685. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  686. ->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
  687. ->join('assessments', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  688. ->join('criterion_objective_outcome', 'activity_criterion.criterion_id', '=', 'criterion_objective_outcome.criterion_id')
  689. ->join('outcomes', 'criterion_objective_outcome.outcome_id', '=', 'outcomes.id')
  690. ->select(
  691. DB::raw(
  692. "student_id, outcomes.name, outcomes.id,
  693. count(activity_criterion.criterion_id) as attempted,
  694. count(case when assessments.score >= rubrics.expected_points then 1 else null end) as achieved"
  695. )
  696. )
  697. ->whereNotNull('score')
  698. ->where('courses.id', $this->id)
  699. ->groupBy(array('outcome_id', 'student_id'))
  700. ->get();
  701. return $reportObject;
  702. }
  703. public function outcomes_ach()
  704. {
  705. $criteria = DB::table('criteria')
  706. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  707. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  708. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  709. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  710. ->join('courses', 'activities.course_id', '=', 'courses.id')
  711. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  712. ->where('courses.id', '=', $this->id)
  713. ->where('activities.draft', 0)
  714. ->where('activities.diagnostic', 0)
  715. ->select(
  716. 'criteria.id',
  717. 'rubrics.expected_percentage as expected_percentage_students_achieving'/*'expected_percentage_students_achieving',*/,
  718. 'activity_criterion.activity_id'
  719. )
  720. ->distinct()
  721. ->get();
  722. // Old data was formed like { "outcome1": cuantity_of_criteria_associated_passed, "outcome2":cuantity_of_criteria}
  723. $outcomes_achieved = [];
  724. foreach ($criteria as $criterion) {
  725. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  726. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  727. //Log::info('estamos aqui');
  728. Log::info($students_attempted);
  729. if ($students_attempted) {
  730. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  731. } else {
  732. $percentage_students_who_achieved = 0;
  733. }
  734. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  735. $outcomes = DB::table('criterion_objective_outcome')
  736. ->where('criterion_id', $criterion->id)
  737. ->select('outcome_id')
  738. ->distinct()
  739. ->lists('outcome_id');
  740. foreach ($outcomes as $outcome_id) {
  741. if (array_key_exists($outcome_id, $outcomes_achieved)) $outcomes_achieved[$outcome_id] += 1;
  742. else $outcomes_achieved[$outcome_id] = 1;
  743. }
  744. }
  745. }
  746. Log::info(
  747. "OUTCOMES_ACHIEVED: "
  748. );
  749. Log::info(
  750. $outcomes_achieved
  751. );
  752. return $outcomes_achieved;
  753. }
  754. public function someSectionAssessed()
  755. {
  756. $all_sections = DB::table('courses')
  757. ->join('activities', 'activities.course_id', '=', 'courses.id')
  758. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  759. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  760. ->where('courses.name', $this->name)
  761. ->where('code', $this->code)
  762. ->where('number', $this->number)
  763. ->where('semester_id', $this->semester_id)
  764. ->where(
  765. 'program_id',
  766. $this->program_id
  767. )
  768. ->whereNotNull('score')
  769. ->where('activities.draft', 0)
  770. ->where('activities.diagnostic', 0)
  771. ->groupBy('courses.id')
  772. ->count();
  773. return $all_sections;
  774. }
  775. public function isAssessed()
  776. {
  777. $assessed = DB::table('assessments')
  778. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  779. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  780. ->whereNotNull('score')
  781. ->where('activities.course_id', $this->id)
  782. ->count();
  783. // Log::info("aqui".$assessed);
  784. if ($assessed) return true;
  785. else return false;
  786. }
  787. public function outcomes_achieved()
  788. {
  789. $criteria = DB::table('criteria')
  790. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  791. ->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
  792. ->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
  793. ->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
  794. ->join('courses', 'activities.course_id', '=', 'courses.id')
  795. ->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
  796. ->where('courses.id', '=', $this->id)
  797. ->select(
  798. 'criteria.id',
  799. 'rubrics.expected_percentage as expected_percentage_students_achieving'/*'expected_percentage_students_achieving',*/,
  800. 'activity_criterion.activity_id'
  801. )
  802. ->distinct()
  803. ->get();
  804. $conteo = 0;
  805. foreach ($criteria as $criterion) {
  806. $students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
  807. $students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);
  808. if ($students_attempted) {
  809. $percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
  810. } else {
  811. $percentage_students_who_achieved = 0;
  812. }
  813. if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
  814. $conteo++;
  815. }
  816. }
  817. return $conteo;
  818. }
  819. public function semester()
  820. {
  821. return $this->belongsTo('Semester');
  822. }
  823. public function sectionsWithOutcome($outcome_id)
  824. {
  825. return Course::has('activities')->with(array('activities' => function ($query) use (&$outcome_id) {
  826. $query->where('outcomes_attempted', 'not like', '\'%"' . $outcome_id . '":0%\'');
  827. }))->where('code', $this->code)->where('number', $this->number)->get();
  828. }
  829. }