Geen omschrijving

Activity.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <?php
  2. class Activity extends Eloquent
  3. {
  4. public function section()
  5. {
  6. return $this->belongsTo('Section');
  7. }
  8. public function rubric()
  9. {
  10. // return $this->belongsTo('Rubric');
  11. return $this->belongsToMany('Rubric', 'rubric_activity');
  12. }
  13. public function course()
  14. {
  15. return $this->belongsTo('Course');
  16. }
  17. // public function assessed_students()
  18. // {
  19. // return $this->belongsToMany('Student', 'assessments')->withPivot('scores', 'percentage')->withTimestamps();
  20. // }
  21. public function assessed_students()
  22. {
  23. return DB::table('students')
  24. -> join('assessments','assessments.student_id','=','students.id')
  25. -> join('activity_criterion','assessments.activity_criterion_id','=','activity_criterion.id')
  26. ->where('activity_criterion.activity_id', '=', $this->id)
  27. ->select('students.*')
  28. ->distinct()
  29. ->get();;
  30. }
  31. public function student_scores($student_id)
  32. {
  33. // $assessments=DB::table('activity_criterion')
  34. // -> leftJoin('assessments','assessments.activity_criterion_id','=','activity_criterion.id')
  35. // ->where('activity_criterion.activity_id', '=', $this->id)
  36. // ->where('assessments.student_id', '=', $student_id)
  37. // ->select('activity_criterion.id','score')
  38. // ->orderBy('criterion_ids')
  39. // ->get();
  40. // $assessments=DB::table('assessments')
  41. // -> join('activity_criterion','assessments.activity_criterion_id','=','activity_criterion.id')
  42. // ->where('activity_criterion.activity_id', '=', $this->id)
  43. // ->where('assessments.student_id', '=', $student_id)
  44. // ->select('activity_criterion.id','score')
  45. // ->orderBy('criterion_id')
  46. // ->get();
  47. $activity_id=$this->id;
  48. $assessments=DB::select(DB::raw("select id, COALESCE(score,0) score from
  49. (select activity_criterion_id, score from`assessments` where student_id= $student_id) a right OUTER join
  50. (select id from `activity_criterion` where `activity_criterion`.`activity_id` = $activity_id) b on a.activity_criterion_id = b.id"));
  51. $scores=array();
  52. foreach($assessments as $assess)
  53. {
  54. $scores[$assess->id]=$assess->score;
  55. }
  56. return $scores;
  57. }
  58. public function student_comments($student_id)
  59. {
  60. $comments=DB::table('activity_student')
  61. ->where('activity_id', '=', $this->id)
  62. ->where('student_id', '=', $student_id)
  63. ->select('comments')
  64. ->first();
  65. return $comments;
  66. }
  67. public function student_percentage($student_id)
  68. {
  69. // $scores=DB::table('assessments')
  70. // -> join('activity_criterion','assessments.activity_criterion_id','=','activity_criterion.id')
  71. // ->where('activity_criterion.activity_id', '=', $this->id)
  72. // ->where('assessments.student_id', '=', $student_id)
  73. // ->select('criterion_id','score')
  74. // ->get();
  75. //rubrics->expected_points
  76. $activity_id=$this->id;
  77. $percentage=DB::select(DB::raw("select COALESCE(ROUND(100*sum(score*weight)/sum(max_score*weight),2),0) percentage from (select activity_criterion_id, score from`assessments` where
  78. student_id= $student_id) a right OUTER join
  79. (select id, weight, activity_id from `activity_criterion` where `activity_criterion`.`activity_id` = $activity_id) b on a.activity_criterion_id = b.id join
  80. rubric_activity on rubric_activity.activity_id=b.activity_id join rubrics on rubrics.id=rubric_activity.rubric_id"));
  81. return $percentage[0]->percentage;
  82. }
  83. public function amount_of_assessed_students()
  84. {
  85. $activity_criterion = DB::table('activity_criterion')->where('activity_id', '=', $this->id)->lists('id');
  86. $amount_of_students = DB::table('assessments')->whereIn('activity_criterion_id', $activity_criterion)->lists('student_id');
  87. return count($amount_of_students);
  88. }
  89. public function criteria_achieved()
  90. {
  91. $activity_criterion = DB::table('activity_criterion')
  92. ->where('activity_id', '=', $this->id)
  93. ->get();
  94. $criteria_achieved = [];
  95. foreach ($activity_criterion as $index => $single_ac) {
  96. $assessments_passed = count(DB::table('assessments')
  97. ->where('score', '>=', $this->rubric[0]->expected_points)
  98. ->where('activity_criterion_id', '=', $single_ac->id)
  99. ->lists('student_id'));
  100. $assessments_attempted = count(DB::table('assessments')
  101. ->where('activity_criterion_id', '=', $single_ac->id)
  102. ->lists('student_id'));
  103. if ($assessments_attempted != 0 && (($assessments_passed / $assessments_attempted) * 100) >= $this->rubric[0]->expected_percentage) {
  104. $criteria_achieved[$single_ac->criterion_id] = 1;
  105. } else {
  106. $criteria_achieved[$single_ac->criterion_id] = 0;
  107. }
  108. }
  109. return $criteria_achieved;
  110. }
  111. public function formativeActionsWithCriteria()
  112. {
  113. return DB::table('activity_criterion')
  114. ->join('transformative_activity_criterion', 'transformative_activity_criterion.activity_criterion_id', '=', 'activity_criterion.id')
  115. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  116. ->join('transformative_actions', 'transformative_actions.id', '=', 'transformative_activity_criterion.trans_action_id')
  117. ->where('activity_id', $this->id)
  118. ->get();
  119. }
  120. // cap_array
  121. public function getCapArrayAttribute()
  122. {
  123. $criteria_achieved_percentage = [];
  124. $criterias = DB::table('criteria')
  125. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  126. ->join('assessments', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  127. ->where('activity_criterion.activity_id', '=', $this->id)
  128. ->select(
  129. array(
  130. 'activity_criterion.id as id',
  131. 'activity_criterion.criterion_id as criterion_id',
  132. 'activity_criterion.activity_id as activity_id'
  133. )
  134. )
  135. ->addSelect('criteria.name', 'criteria.subcriteria')
  136. ->get();
  137. foreach ($criterias as $index => $single_crit) {
  138. $single_crit->outcome_id = json_encode(DB::table('criterion_objective_outcome')
  139. ->where('criterion_id', '=', $single_crit->criterion_id)
  140. ->lists('outcome_id'));
  141. $amount_of_students = count(DB::table('assessments')
  142. ->where("activity_criterion_id", '=', $single_crit->id)
  143. ->lists('student_id'));
  144. $student_pass = DB::table('assessments')
  145. ->where("activity_criterion_id", '=', $single_crit->id)
  146. ->where('score', '>=', $this->rubric[0]->expected_points)
  147. ->lists('student_id');
  148. $amount_of_students_passed = count(DB::table('assessments')
  149. ->where("activity_criterion_id", '=', $single_crit->id)
  150. ->where('score', '>=', $this->rubric[0]->expected_points)
  151. ->lists('student_id'));
  152. $single_crit->score_percentage = ($amount_of_students_passed / $amount_of_students) * 100;
  153. //this is for criteria graph
  154. $single_crit->outcome_names_html = "<tr><td style='color:#e70033; padding:0'>Learning Outcomes:</td>";
  155. $outcome_names = DB::table('outcomes')
  156. ->whereIn('id', json_decode($single_crit->outcome_id))
  157. ->lists('name');
  158. foreach ($outcome_names as $index => $name) {
  159. $string_to_add = str_repeat("<td>" . $name . ", </td></tr>", ($index + 1) % 2) . str_repeat("<tr><td style='padding:0'>" . $name . ", </td>", $index % 2);
  160. $single_crit->outcome_names_html .= $string_to_add;
  161. }
  162. $single_crit->outcome_names_html .= str_repeat("<td></td></tr>", $index % 2);
  163. $criteria_achieved_percentage[$single_crit->criterion_id] = $single_crit;
  164. }
  165. return $criteria_achieved_percentage;
  166. }
  167. public function allActivityCriterionInfo()
  168. {
  169. $activity_criterion = DB::table('activity_criterion')
  170. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  171. ->where('activity_id', $this->id)
  172. ->select('activity_criterion.activity_id', 'activity_criterion.criterion_id', 'criteria.*')
  173. ->distinct()
  174. ->orderBy('criteria.id')
  175. ->get();
  176. return $activity_criterion;
  177. }
  178. public function getOutcomeReport()
  179. {
  180. $outcomes = DB::table('activity_criterion')
  181. ->join('criterion_objective_outcome', 'activity_criterion.criterion_id', '=', 'criterion_objective_outcome.criterion_id')
  182. ->join('outcomes', 'outcomes.id', '=', 'criterion_objective_outcome.outcome_id')
  183. ->where('activity_criterion.activity_id', $this->id)
  184. ->select('outcomes.*')
  185. ->distinct()
  186. ->get();
  187. Log::info(json_encode($this));
  188. if (isset($this->rubric[0])) $rubric = $this->rubric[0];
  189. else $rubric = new stdClass();
  190. $outcomes_dict = [];
  191. foreach ($outcomes as $outcome) {
  192. $outcomes_dict[$outcome->id] = $outcome;
  193. }
  194. /*
  195. foreach($outcomes as $outcome){
  196. $ac_criteria = DB::table('criterion_objective_outcome as cobo')
  197. ->join('activity_criterion as ac','ac.criterion_id','=','cobo.criterion_id')
  198. ->where("outcome_id",$outcome->id)
  199. ->where("activity_id",$this->id)
  200. ->select('ac.*')
  201. ->distinct()
  202. ->get();
  203. $conteo_de_criterios = 0;
  204. $students_who_achieved =[];
  205. $outcome->students_attempted = DB::table("assessments")
  206. ->join("activity_criterion as ac", 'ac.id','=','assessments.activity_criterion_id')
  207. ->join("criterion_objective_outcome as cobo",'cobo.criterion_id','=','ac.criterion_id')
  208. ->where('outcome_id',$outcome->id)
  209. ->where('ac.activity_id',$this->id)
  210. ->count();
  211. foreach($ac_criteria as $criterion){
  212. $students_attempted = Criterion::students_attempted($criterion->criterion_id, $this->id);
  213. $students_achieved = DB::table('assessments')
  214. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  215. ->where('activity_criterion.id', $criterion->id)
  216. ->where('assessments.score', '>=', $rubric->expected_points)
  217. ->lists("student_id");
  218. if((count($students_achieved)/$students_attempted) * 100 >$rubric->expected_percentage){
  219. foreach($students_achieved as $stu_id){
  220. if(!array_key_exists($stu_id, $students_who_achieved)){
  221. $students_who_achieved[$stu_id] = 1;
  222. }
  223. }
  224. $conteo_de_criterios++;
  225. }
  226. }
  227. $outcome->students_achieved = count($students_who_achieved);
  228. }*/
  229. /* $students_attempted = DB::table('assessments')
  230. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  231. ->where('activity_criterion.activity_id', $this->id)
  232. ->get();*/
  233. foreach ($outcomes_dict as $outcome) {
  234. // dame los criterios que son de este dominio, y de esta actividad.
  235. $ac_criteria = DB::table('criterion_objective_outcome as cobo')
  236. ->join('activity_criterion as ac', 'ac.criterion_id', '=', 'cobo.criterion_id')
  237. ->where("outcome_id", $outcome->id)
  238. ->where("activity_id", $this->id)
  239. ->select('ac.*')
  240. ->distinct()
  241. ->lists('id');
  242. /*$student_achieved = DB::table('assessments')
  243. ->whereIn('activity_criterion_id', $ac_criteria)
  244. ->where('score', '>=', $rubric->expected_points)
  245. ->groupBy('student_id')
  246. ->select('student_id', 'count(activity_criterion_id)');
  247. $students_attempted = DB::table('assessments as a')
  248. ->whereIn('a.activity_criterion_id', $ac_criteria)
  249. ->groupBy('a.student_id')
  250. ->leftJoin('assessments as b', function ($join) use ($ac_criteria, $rubric) {
  251. $join->on('b.student_id', '=', 'a.student_id')
  252. ->where('b.activity_criterion_id', '=', 'a.activity_criterion_id')
  253. ->where('b.score', '>=', $rubric->expected_points);
  254. })
  255. ->select('a.student_id', DB::raw('count(`a`.`activity_criterion_id`) attempted'), DB::raw('count(`b`.`activity_criterion_id`) achieved'));
  256. Log::info($students_attempted->get());*/
  257. $students_attempted = DB::table('assessments as a')
  258. ->whereIn('a.activity_criterion_id', $ac_criteria)
  259. ->groupBy('a.student_id')
  260. ->select(
  261. 'a.student_id',
  262. DB::raw('count(`a`.`activity_criterion_id`) attempted'),
  263. DB::raw("count(case when score >= {$rubric->expected_points} then 1 else null end) as achieved")
  264. )->get();
  265. $outcome->attempted = count($students_attempted);
  266. $conteo_outcome_achieved = 0;
  267. foreach ($students_attempted as $student) {
  268. Log::info($student->achieved / $student->attempted * 100);
  269. if ($student->achieved / $student->attempted * 100 >= $outcome->expected_outcome) {
  270. $conteo_outcome_achieved++;
  271. }
  272. }
  273. Log::info(json_encode($outcome));
  274. $outcome->achieved = $conteo_outcome_achieved;
  275. if (isset($outcome->attempted) && $outcome->attempted != 0) $outcome->percentage = round(($outcome->achieved / $outcome->attempted * 100), 2);
  276. /*foreach ($students_attempted as $student) {
  277. $student_criteria[$student];
  278. }
  279. $conteo_de_criterios = 0;
  280. $students_who_achieved = [];
  281. foreach ($ac_criteria as $ac) {
  282. $students_attempted = DB::table("assessments")
  283. ->where('activity_criterion_id', $ac->id)
  284. ->get();
  285. }*/
  286. }
  287. return $outcomes_dict;
  288. }
  289. public function getTransformingActionAttribute()
  290. {
  291. return TransformativeAction::join('transformative_activity_criterion', 'trans_action_id', '=', 'transformative_actions.id')
  292. ->join('activity_criterion', 'activity_criterion.id', '=', 'transformative_activity_criterion.activity_criterion_id')
  293. ->where('activity_id', $this->id)->first();
  294. }
  295. // o_ach_array
  296. public function getOAchArrayAttribute($value)
  297. {
  298. /*
  299. $outcomes_achieved = [];
  300. $all_criterion = DB::table('activity_criterion')
  301. ->where('activity_criterion.activity_id', '=', $this->id)
  302. ->get();
  303. $attempted_criteria = 0;
  304. $passed_criteria = 0;
  305. $all_outcomes = DB::table('outcomes')->get();
  306. foreach ($all_criterion as $index => $activity_crit) {
  307. $amount_of_outcomes_attempted = DB::table('criterion_objective_outcome')
  308. ->join('outcomes', 'outcomes.id', '=', 'criterion_objective_outcome.outcome_id')
  309. ->where('criterion_id', '=', $activity_crit->criterion_id)
  310. ->select('criterion_objective_outcome.outcome_id')
  311. ->distinct()
  312. ->orderBy('criterion_objective_outcome.outcome_id')
  313. ->select('outcomes.id', 'outcomes.expected_outcome')
  314. ->get();
  315. $passed_criteria = count(DB::table('assessments')
  316. ->where('activity_criterion_id', '=', $activity_crit->id)
  317. ->where('score', '>=', $this->rubric[0]->expected_points)
  318. ->lists('student_id'));
  319. $attempted_criteria = count(DB::table('assessments')
  320. ->where('activity_criterion_id', '=', $activity_crit->id)
  321. ->lists('student_id'));
  322. Log::info("For activity_crit " . ($activity_crit->id));
  323. $percent_for_criteria = ($passed_criteria / $attempted_criteria) * 100;
  324. Log::info('Percent calculated' . $percent_for_criteria);
  325. $percent_for_criteria = ($passed_criteria / $attempted_criteria) * 100;
  326. foreach ($amount_of_outcomes_attempted as $index2 => $outcome) {
  327. if ($percent_for_criteria >= $this->rubric[0]->expected_percentage) {
  328. if (array_key_exists($outcome->id, $outcomes_achieved)) $outcomes_achieved[$outcome->id] += 1;
  329. else $outcomes_achieved[$outcome->id] = 1;
  330. }
  331. }
  332. }
  333. return $outcomes_achieved;*/
  334. $outcomes_achieved = [];
  335. $criteria_achieved = $this->criteria_achieved();
  336. $all_outcomes = DB::table('outcomes')->lists('id');
  337. foreach ($all_outcomes as $outcome_id) {
  338. $outcomes_achieved[$outcome_id] = 0;
  339. }
  340. foreach ($criteria_achieved as $criterion_id => $passed) {
  341. if ($passed == 1) {
  342. $outcomes_related = DB::table('criterion_objective_outcome')
  343. ->where("criterion_id", '=', $criterion_id)
  344. ->select('outcome_id')
  345. ->distinct()
  346. ->lists('outcome_id');
  347. foreach ($outcomes_related as $index => $outcome_id) {
  348. $outcomes_achieved[$outcome_id] += 1;
  349. }
  350. }
  351. }
  352. return $outcomes_achieved;
  353. }
  354. public function is_assessed()
  355. {
  356. $all_criterion = DB::table('activity_criterion')
  357. ->where('activity_criterion.activity_id', '=', $this->id)
  358. ->lists('id');
  359. $assessments = DB::table('assessments')
  360. ->whereIn('activity_criterion_id', $all_criterion)
  361. ->lists('id');
  362. // return boolval(count($assessments));
  363. return (bool)(count($assessments));
  364. }
  365. public function isStudentAssessed($student_id)
  366. {
  367. return DB::table('activities')
  368. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  369. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  370. ->where('activity_id', $this->id)
  371. ->where('student_id', $student_id)
  372. ->count();
  373. }
  374. public function transforming_actions()
  375. {
  376. $transformative_action = DB::table('activities')
  377. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  378. ->join('transformative_activity_criterion as tac', 'tac.activity_criterion_id', '=', 'ac.id')
  379. ->join('transformative_actions as ta', 'ta.id', '=', 'tac.trans_action_id')
  380. ->where('activities.id', $this->id)
  381. ->select('ta.*')
  382. ->first();
  383. if ($transformative_action)
  384. return $transformative_action->description;
  385. else return '';
  386. }
  387. // o_att_array
  388. public function getOAttArrayAttribute()
  389. {
  390. $outcomes_attempted = [];
  391. $all_criterion = DB::table('activity_criterion')
  392. ->join('assessments', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  393. ->where('activity_criterion.activity_id', '=', $this->id)
  394. ->groupBy('criterion_id')
  395. ->lists('criterion_id');
  396. foreach ($all_criterion as $index => $criterion_id) {
  397. $amount_of_outcomes = DB::table('criterion_objective_outcome')
  398. ->where('criterion_id', '=', $criterion_id)
  399. ->select('criterion_objective_outcome.outcome_id')
  400. ->distinct()
  401. ->orderBy('criterion_objective_outcome.outcome_id')
  402. ->lists('outcome_id');
  403. foreach ($amount_of_outcomes as $index2 => $outcome_id) {
  404. if (array_key_exists($outcome_id, $outcomes_attempted)) $outcomes_attempted[$outcome_id] += 1;
  405. else $outcomes_attempted[$outcome_id] = 1;
  406. }
  407. }
  408. //return json_decode($this->outcomes_attempted, true);
  409. return $outcomes_attempted;
  410. }
  411. }