Sin descripción

Activity.php 13KB

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