Nessuna descrizione

Activity.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 amount_of_assessed_students()
  22. {
  23. $activity_criterion = DB::table('activity_criterion')->where('activity_id', '=', $this->id)->lists('id');
  24. $amount_of_students = DB::table('assessments')->whereIn('activity_criterion_id', $activity_criterion)->lists('student_id');
  25. return count($amount_of_students);
  26. }
  27. public function criteria_achieved()
  28. {
  29. $activity_criterion = DB::table('activity_criterion')
  30. ->where('activity_id', '=', $this->id)
  31. ->get();
  32. $criteria_achieved = [];
  33. foreach ($activity_criterion as $index => $single_ac) {
  34. $assessments_passed = count(DB::table('assessments')
  35. ->where('score', '>=', $this->rubric[0]->expected_points)
  36. ->where('activity_criterion_id', '=', $single_ac->id)
  37. ->lists('student_id'));
  38. $assessments_attempted = count(DB::table('assessments')
  39. ->where('activity_criterion_id', '=', $single_ac->id)
  40. ->lists('student_id'));
  41. if ($assessments_attempted != 0 && (($assessments_passed / $assessments_attempted) * 100) >= $this->rubric[0]->expected_percentage) {
  42. $criteria_achieved[$single_ac->criterion_id] = 1;
  43. } else {
  44. $criteria_achieved[$single_ac->criterion_id] = 0;
  45. }
  46. }
  47. return $criteria_achieved;
  48. }
  49. public function formativeActionsWithCriteria()
  50. {
  51. return DB::table('activity_criterion')
  52. ->join('transformative_activity_criterion', 'transformative_activity_criterion.activity_criterion_id', '=', 'activity_criterion.id')
  53. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  54. ->join('transformative_actions', 'transformative_actions.id', '=', 'transformative_activity_criterion.trans_action_id')
  55. ->where('activity_id', $this->id)
  56. ->get();
  57. }
  58. // cap_array
  59. public function getCapArrayAttribute()
  60. {
  61. $criteria_achieved_percentage = [];
  62. $criterias = DB::table('criteria')
  63. ->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
  64. ->join('assessments', 'activity_criterion.id', '=', 'assessments.activity_criterion_id')
  65. ->where('activity_criterion.activity_id', '=', $this->id)
  66. ->select(
  67. array(
  68. 'activity_criterion.id as id',
  69. 'activity_criterion.criterion_id as criterion_id',
  70. 'activity_criterion.activity_id as activity_id'
  71. )
  72. )
  73. ->addSelect('criteria.name', 'criteria.subcriteria')
  74. ->get();
  75. foreach ($criterias as $index => $single_crit) {
  76. $single_crit->outcome_id = json_encode(DB::table('criterion_objective_outcome')
  77. ->where('criterion_id', '=', $single_crit->criterion_id)
  78. ->lists('outcome_id'));
  79. $amount_of_students = count(DB::table('assessments')
  80. ->where("activity_criterion_id", '=', $single_crit->id)
  81. ->lists('student_id'));
  82. $student_pass = DB::table('assessments')
  83. ->where("activity_criterion_id", '=', $single_crit->id)
  84. ->where('score', '>=', $this->rubric[0]->expected_points)
  85. ->lists('student_id');
  86. $amount_of_students_passed = count(DB::table('assessments')
  87. ->where("activity_criterion_id", '=', $single_crit->id)
  88. ->where('score', '>=', $this->rubric[0]->expected_points)
  89. ->lists('student_id'));
  90. $single_crit->score_percentage = ($amount_of_students_passed / $amount_of_students) * 100;
  91. //this is for criteria graph
  92. $single_crit->outcome_names_html = "<tr><td style='color:#e70033; padding:0'>Learning Outcomes:</td>";
  93. $outcome_names = DB::table('outcomes')
  94. ->whereIn('id', json_decode($single_crit->outcome_id))
  95. ->lists('name');
  96. foreach ($outcome_names as $index => $name) {
  97. $string_to_add = str_repeat("<td>" . $name . ", </td></tr>", ($index + 1) % 2) . str_repeat("<tr><td style='padding:0'>" . $name . ", </td>", $index % 2);
  98. $single_crit->outcome_names_html .= $string_to_add;
  99. }
  100. $single_crit->outcome_names_html .= str_repeat("<td></td></tr>", $index % 2);
  101. $criteria_achieved_percentage[$single_crit->criterion_id] = $single_crit;
  102. }
  103. return $criteria_achieved_percentage;
  104. }
  105. public function allActivityCriterionInfo()
  106. {
  107. $activity_criterion = DB::table('activity_criterion')
  108. ->join('criteria', 'criteria.id', '=', 'activity_criterion.criterion_id')
  109. ->where('activity_id', $this->id)
  110. ->select('activity_criterion.activity_id', 'activity_criterion.criterion_id', 'criteria.*')
  111. ->distinct()
  112. ->get();
  113. return $activity_criterion;
  114. }
  115. public function getOutcomeReport()
  116. {
  117. $outcomes = DB::table('activity_criterion')
  118. ->join('criterion_objective_outcome', 'activity_criterion.criterion_id', '=', 'criterion_objective_outcome.criterion_id')
  119. ->join('outcomes', 'outcomes.id', '=', 'criterion_objective_outcome.outcome_id')
  120. ->where('activity_criterion.activity_id', $this->id)
  121. ->select('outcomes.*')
  122. ->distinct()
  123. ->get();
  124. Log::info(json_encode($this));
  125. if(isset($this->rubric[0]))$rubric = $this->rubric[0]; else $rubric=new stdClass();
  126. $outcomes_dict = [];
  127. foreach ($outcomes as $outcome) {
  128. $outcomes_dict[$outcome->id] = $outcome;
  129. }
  130. /*
  131. foreach($outcomes as $outcome){
  132. $ac_criteria = DB::table('criterion_objective_outcome as cobo')
  133. ->join('activity_criterion as ac','ac.criterion_id','=','cobo.criterion_id')
  134. ->where("outcome_id",$outcome->id)
  135. ->where("activity_id",$this->id)
  136. ->select('ac.*')
  137. ->distinct()
  138. ->get();
  139. $conteo_de_criterios = 0;
  140. $students_who_achieved =[];
  141. $outcome->students_attempted = DB::table("assessments")
  142. ->join("activity_criterion as ac", 'ac.id','=','assessments.activity_criterion_id')
  143. ->join("criterion_objective_outcome as cobo",'cobo.criterion_id','=','ac.criterion_id')
  144. ->where('outcome_id',$outcome->id)
  145. ->where('ac.activity_id',$this->id)
  146. ->count();
  147. foreach($ac_criteria as $criterion){
  148. $students_attempted = Criterion::students_attempted($criterion->criterion_id, $this->id);
  149. $students_achieved = DB::table('assessments')
  150. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  151. ->where('activity_criterion.id', $criterion->id)
  152. ->where('assessments.score', '>=', $rubric->expected_points)
  153. ->lists("student_id");
  154. if((count($students_achieved)/$students_attempted) * 100 >$rubric->expected_percentage){
  155. foreach($students_achieved as $stu_id){
  156. if(!array_key_exists($stu_id, $students_who_achieved)){
  157. $students_who_achieved[$stu_id] = 1;
  158. }
  159. }
  160. $conteo_de_criterios++;
  161. }
  162. }
  163. $outcome->students_achieved = count($students_who_achieved);
  164. }*/
  165. /* $students_attempted = DB::table('assessments')
  166. ->join('activity_criterion', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  167. ->where('activity_criterion.activity_id', $this->id)
  168. ->get();*/
  169. foreach ($outcomes_dict as $outcome) {
  170. $ac_criteria = DB::table('criterion_objective_outcome as cobo')
  171. ->join('activity_criterion as ac', 'ac.criterion_id', '=', 'cobo.criterion_id')
  172. ->where("outcome_id", $outcome->id)
  173. ->where("activity_id", $this->id)
  174. ->select('ac.*')
  175. ->distinct()
  176. ->lists('id');
  177. /*$student_achieved = DB::table('assessments')
  178. ->whereIn('activity_criterion_id', $ac_criteria)
  179. ->where('score', '>=', $rubric->expected_points)
  180. ->groupBy('student_id')
  181. ->select('student_id', 'count(activity_criterion_id)');
  182. $students_attempted = DB::table('assessments as a')
  183. ->whereIn('a.activity_criterion_id', $ac_criteria)
  184. ->groupBy('a.student_id')
  185. ->leftJoin('assessments as b', function ($join) use ($ac_criteria, $rubric) {
  186. $join->on('b.student_id', '=', 'a.student_id')
  187. ->where('b.activity_criterion_id', '=', 'a.activity_criterion_id')
  188. ->where('b.score', '>=', $rubric->expected_points);
  189. })
  190. ->select('a.student_id', DB::raw('count(`a`.`activity_criterion_id`) attempted'), DB::raw('count(`b`.`activity_criterion_id`) achieved'));
  191. Log::info($students_attempted->get());*/
  192. $students_attempted = DB::table('assessments as a')
  193. ->whereIn('a.activity_criterion_id', $ac_criteria)
  194. ->groupBy('a.student_id')
  195. ->select(
  196. 'a.student_id',
  197. DB::raw('count(`a`.`activity_criterion_id`) attempted'),
  198. DB::raw("count(case when score >= {$rubric->expected_points} then 1 else null end) as achieved")
  199. )->get();
  200. $outcome->attempted = count($students_attempted);
  201. $conteo_outcome_achieved = 0;
  202. foreach ($students_attempted as $student) {
  203. Log::info($student->achieved / $student->attempted * 100);
  204. if ($student->achieved / $student->attempted * 100 >= $outcome->expected_outcome) {
  205. $conteo_outcome_achieved++;
  206. }
  207. }
  208. Log::info(json_encode($outcome));
  209. $outcome->achieved = $conteo_outcome_achieved;
  210. if(isset($outcome->attempted) and $outcome->attempted!=0)$outcome->percentage = round(($outcome->achieved / $outcome->attempted * 100), 2);
  211. /*foreach ($students_attempted as $student) {
  212. $student_criteria[$student];
  213. }
  214. $conteo_de_criterios = 0;
  215. $students_who_achieved = [];
  216. foreach ($ac_criteria as $ac) {
  217. $students_attempted = DB::table("assessments")
  218. ->where('activity_criterion_id', $ac->id)
  219. ->get();
  220. }*/
  221. }
  222. return $outcomes_dict;
  223. }
  224. public function getTransformingActionAttribute()
  225. {
  226. return TransformativeAction::join('transformative_activity_criterion', 'trans_action_id', '=', 'transformative_actions.id')
  227. ->join('activity_criterion', 'activity_criterion.id', '=', 'transformative_activity_criterion.activity_criterion_id')
  228. ->where('activity_id', $this->id)->first();
  229. }
  230. // o_ach_array
  231. public function getOAchArrayAttribute($value)
  232. {
  233. /*
  234. $outcomes_achieved = [];
  235. $all_criterion = DB::table('activity_criterion')
  236. ->where('activity_criterion.activity_id', '=', $this->id)
  237. ->get();
  238. $attempted_criteria = 0;
  239. $passed_criteria = 0;
  240. $all_outcomes = DB::table('outcomes')->get();
  241. foreach ($all_criterion as $index => $activity_crit) {
  242. $amount_of_outcomes_attempted = DB::table('criterion_objective_outcome')
  243. ->join('outcomes', 'outcomes.id', '=', 'criterion_objective_outcome.outcome_id')
  244. ->where('criterion_id', '=', $activity_crit->criterion_id)
  245. ->select('criterion_objective_outcome.outcome_id')
  246. ->distinct()
  247. ->orderBy('criterion_objective_outcome.outcome_id')
  248. ->select('outcomes.id', 'outcomes.expected_outcome')
  249. ->get();
  250. $passed_criteria = count(DB::table('assessments')
  251. ->where('activity_criterion_id', '=', $activity_crit->id)
  252. ->where('score', '>=', $this->rubric[0]->expected_points)
  253. ->lists('student_id'));
  254. $attempted_criteria = count(DB::table('assessments')
  255. ->where('activity_criterion_id', '=', $activity_crit->id)
  256. ->lists('student_id'));
  257. Log::info("For activity_crit " . ($activity_crit->id));
  258. $percent_for_criteria = ($passed_criteria / $attempted_criteria) * 100;
  259. Log::info('Percent calculated' . $percent_for_criteria);
  260. $percent_for_criteria = ($passed_criteria / $attempted_criteria) * 100;
  261. foreach ($amount_of_outcomes_attempted as $index2 => $outcome) {
  262. if ($percent_for_criteria >= $this->rubric[0]->expected_percentage) {
  263. if (array_key_exists($outcome->id, $outcomes_achieved)) $outcomes_achieved[$outcome->id] += 1;
  264. else $outcomes_achieved[$outcome->id] = 1;
  265. }
  266. }
  267. }
  268. return $outcomes_achieved;*/
  269. $outcomes_achieved = [];
  270. $criteria_achieved = $this->criteria_achieved();
  271. $all_outcomes = DB::table('outcomes')->lists('id');
  272. foreach ($all_outcomes as $outcome_id) {
  273. $outcomes_achieved[$outcome_id] = 0;
  274. }
  275. foreach ($criteria_achieved as $criterion_id => $passed) {
  276. if ($passed == 1) {
  277. $outcomes_related = DB::table('criterion_objective_outcome')
  278. ->where("criterion_id", '=', $criterion_id)
  279. ->select('outcome_id')
  280. ->distinct()
  281. ->lists('outcome_id');
  282. foreach ($outcomes_related as $index => $outcome_id) {
  283. $outcomes_achieved[$outcome_id] += 1;
  284. }
  285. }
  286. }
  287. return $outcomes_achieved;
  288. }
  289. public function is_assessed()
  290. {
  291. $all_criterion = DB::table('activity_criterion')
  292. ->where('activity_criterion.activity_id', '=', $this->id)
  293. ->lists('id');
  294. $assessments = DB::table('assessments')
  295. ->whereIn('activity_criterion_id', $all_criterion)
  296. ->lists('id');
  297. // return boolval(count($assessments));
  298. return (bool)(count($assessments));
  299. }
  300. public function isStudentAssessed($student_id)
  301. {
  302. return DB::table('activities')
  303. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  304. ->join('assessments', 'assessments.activity_criterion_id', '=', 'ac.id')
  305. ->where('activity_id', $this->id)
  306. ->where('student_id', $student_id)
  307. ->count();
  308. }
  309. public function transforming_actions()
  310. {
  311. $transformative_action = DB::table('activities')
  312. ->join('activity_criterion as ac', 'ac.activity_id', '=', 'activities.id')
  313. ->join('transformative_activity_criterion as tac', 'tac.activity_criterion_id', '=', 'ac.id')
  314. ->join('transformative_actions as ta', 'ta.id', '=', 'tac.trans_action_id')
  315. ->where('activities.id', $this->id)
  316. ->select('ta.*')
  317. ->first();
  318. if ($transformative_action)
  319. return $transformative_action->description;
  320. else return '';
  321. }
  322. // o_att_array
  323. public function getOAttArrayAttribute()
  324. {
  325. $outcomes_attempted = [];
  326. $all_criterion = DB::table('activity_criterion')
  327. ->join('assessments', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
  328. ->where('activity_criterion.activity_id', '=', $this->id)
  329. ->groupBy('criterion_id')
  330. ->lists('criterion_id');
  331. foreach ($all_criterion as $index => $criterion_id) {
  332. $amount_of_outcomes = DB::table('criterion_objective_outcome')
  333. ->where('criterion_id', '=', $criterion_id)
  334. ->select('criterion_objective_outcome.outcome_id')
  335. ->distinct()
  336. ->orderBy('criterion_objective_outcome.outcome_id')
  337. ->lists('outcome_id');
  338. foreach ($amount_of_outcomes as $index2 => $outcome_id) {
  339. if (array_key_exists($outcome_id, $outcomes_attempted)) $outcomes_attempted[$outcome_id] += 1;
  340. else $outcomes_attempted[$outcome_id] = 1;
  341. }
  342. }
  343. //return json_decode($this->outcomes_attempted, true);
  344. return $outcomes_attempted;
  345. }
  346. }