Нема описа

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