| 1455 | ariadna | 1 | <?php
 | 
        
           |  |  | 2 | use core_completion\progress;
 | 
        
           |  |  | 3 |   | 
        
           |  |  | 4 | class block_progreso_sinopsis_renderer extends plugin_renderer_base {
 | 
        
           |  |  | 5 |   | 
        
           |  |  | 6 |     public function procesar($instance_id) {
 | 
        
           |  |  | 7 |   | 
        
           |  |  | 8 |         global $USER, $CFG;
 | 
        
           |  |  | 9 |   | 
        
           |  |  | 10 |        // require_once $CFG->libdir . '/grade/grade_item.php';
 | 
        
           |  |  | 11 |   | 
        
           |  |  | 12 |         require_once $CFG->libdir . '/completionlib.php';
 | 
        
           |  |  | 13 |         require_once $CFG->dirroot.'/completion/classes/progress.php';
 | 
        
           |  |  | 14 |   | 
        
           |  |  | 15 |         require_once $CFG->libdir . '/badgeslib.php';
 | 
        
           |  |  | 16 |         require_once $CFG->libdir . '/gradelib.php';
 | 
        
           |  |  | 17 |         require_once $CFG->dirroot . '/grade/querylib.php';
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 |   | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 |         $mycourses = enrol_get_users_courses($USER->id);
 | 
        
           |  |  | 22 |         $max_time   = time();
 | 
        
           |  |  | 23 |         $min_time   = strtotime('-1 YEAR');
 | 
        
           |  |  | 24 |         $limit      = 3600;
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 |         $dedication = 0;
 | 
        
           |  |  | 27 |         $completed  = 0;
 | 
        
           |  |  | 28 |         $inprogress = 0;
 | 
        
           |  |  | 29 |         $grades     = 0;
 | 
        
           |  |  | 30 |         $points     = 0;
 | 
        
           |  |  | 31 |         $badges     = 0;
 | 
        
           |  |  | 32 |   | 
        
           |  |  | 33 |   | 
        
           |  |  | 34 |         foreach($mycourses as $key => $val) {
 | 
        
           |  |  | 35 |             $course = get_course($val->id);
 | 
        
           |  |  | 36 |             $completion = new \completion_info($course);
 | 
        
           |  |  | 37 |             //$completion = new completion_info($course);
 | 
        
           |  |  | 38 |             if ($completion->is_enabled()) {
 | 
        
           |  |  | 39 |                 $percentage = progress::get_course_progress_percentage($course);
 | 
        
           |  |  | 40 |                 $percentage = !$percentage ? 0 : $percentage;
 | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 |                 $points += $percentage;
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 |                 if($percentage >= 100) {
 | 
        
           |  |  | 45 |                     $completed++;
 | 
        
           |  |  | 46 |                 } else {
 | 
        
           |  |  | 47 |                     $inprogress++;
 | 
        
           |  |  | 48 |                 }
 | 
        
           |  |  | 49 |             } else {
 | 
        
           |  |  | 50 |                 $inprogress++;
 | 
        
           |  |  | 51 |             }
 | 
        
           |  |  | 52 |             // OJO: Comentado, se requiere los badges y no los grades
 | 
        
           |  |  | 53 | //             $resultkrb =  grade_get_course_grades($course->id, $USER->id);
 | 
        
           |  |  | 54 | //             $grd = $resultkrb->grades[$USER->id];
 | 
        
           |  |  | 55 | //             if('-' != $grd->str_grade) {
 | 
        
           |  |  | 56 | //                 $grades++;
 | 
        
           |  |  | 57 | //             }
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 | //             $records = badges_get_user_badges($USER->id,$course->id, null, null, null, true);
 | 
        
           |  |  | 60 |             $records = badges_get_user_badges($USER->id,null, null, null, null, true);
 | 
        
           |  |  | 61 |             $badges = count($records);
 | 
        
           |  |  | 62 | //             var_dump($records)."<br>";
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |             $dedication = $dedication + $this->get_students_dedication($USER->id, $course->id, $min_time, $max_time, $limit);
 | 
        
           |  |  | 65 |             //var_dump($dedication);//test
 | 
        
           |  |  | 66 |             //die();
 | 
        
           |  |  | 67 |         }
 | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 |         if(0 != $completed || 0 != $inprogress) {
 | 
        
           |  |  | 70 |             $points = (int) ($points / ($completed + $inprogress));
 | 
        
           |  |  | 71 |         }
 | 
        
           |  |  | 72 |   | 
        
           |  |  | 73 |   | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 |         $data = [
 | 
        
           |  |  | 76 |             'dedication'    => $this->format_dedication($dedication),
 | 
        
           |  |  | 77 |             'completed'     => $completed,
 | 
        
           |  |  | 78 |             'inprogress'    => $inprogress,
 | 
        
           |  |  | 79 |             'grades'        => $badges,
 | 
        
           |  |  | 80 |             'points'    => $points,
 | 
        
           |  |  | 81 |         ];
 | 
        
           |  |  | 82 |         //var_dump($data);//test
 | 
        
           |  |  | 83 |         //die();
 | 
        
           |  |  | 84 |         return $this->render_from_template('block_progreso_sinopsis/full', $data);
 | 
        
           |  |  | 85 |     }
 | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 |     public static function format_dedication($totalsecs) {
 | 
        
           |  |  | 88 |         $totalsecs = abs($totalsecs);
 | 
        
           |  |  | 89 |   | 
        
           |  |  | 90 |         $str = new stdClass();
 | 
        
           |  |  | 91 |         $str->hour = get_string('hour');
 | 
        
           |  |  | 92 |         $str->hours = get_string('hours');
 | 
        
           |  |  | 93 |         $str->min = get_string('min');
 | 
        
           |  |  | 94 |         $str->mins = get_string('mins');
 | 
        
           |  |  | 95 |         $str->sec = get_string('sec');
 | 
        
           |  |  | 96 |         $str->secs = get_string('secs');
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 |         $hours = floor($totalsecs / HOURSECS);
 | 
        
           |  |  | 99 |         $remainder = $totalsecs - ($hours * HOURSECS);
 | 
        
           |  |  | 100 |         $mins = floor($remainder / MINSECS);
 | 
        
           |  |  | 101 |         $secs = round($remainder - ($mins * MINSECS), 2);
 | 
        
           |  |  | 102 |   | 
        
           |  |  | 103 |         $ss = ($secs == 1) ? $str->sec : $str->secs;
 | 
        
           |  |  | 104 |         $sm = ($mins == 1) ? $str->min : $str->mins;
 | 
        
           |  |  | 105 |         $sh = ($hours == 1) ? $str->hour : $str->hours;
 | 
        
           |  |  | 106 |   | 
        
           |  |  | 107 |         $ohours = '';
 | 
        
           |  |  | 108 |         $omins = '';
 | 
        
           |  |  | 109 |         $osecs = '';
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 |   | 
        
           |  |  | 112 |         if ($hours) {
 | 
        
           |  |  | 113 |            // $ohours = $hours . ' ' . $sh;
 | 
        
           |  |  | 114 |             $ohours = $hours . ' ' . 'h';
 | 
        
           |  |  | 115 |         }
 | 
        
           |  |  | 116 |         if ($mins) {
 | 
        
           |  |  | 117 |             //$omins = $mins . ' ' . $sm;
 | 
        
           |  |  | 118 |             $omins = $mins . ' ' . 'm';
 | 
        
           |  |  | 119 |         }
 | 
        
           |  |  | 120 |         if ($secs) {
 | 
        
           |  |  | 121 |             //$osecs = $secs . ' ' . $ss;
 | 
        
           |  |  | 122 |             $osecs = $secs . ' ' . 's';
 | 
        
           |  |  | 123 |         }
 | 
        
           |  |  | 124 |   | 
        
           |  |  | 125 |         if ($hours) {
 | 
        
           |  |  | 126 |             return trim($ohours . ' ' . $omins);
 | 
        
           |  |  | 127 |         }
 | 
        
           |  |  | 128 |         if ($mins) {
 | 
        
           |  |  | 129 |             return trim($omins . ' ' . $osecs);
 | 
        
           |  |  | 130 |         }
 | 
        
           |  |  | 131 |         if ($secs) {
 | 
        
           |  |  | 132 |             return $osecs;
 | 
        
           |  |  | 133 |         }
 | 
        
           |  |  | 134 |   | 
        
           |  |  | 135 |         return get_string('none');
 | 
        
           |  |  | 136 |   | 
        
           |  |  | 137 |   | 
        
           |  |  | 138 |     }
 | 
        
           |  |  | 139 |   | 
        
           |  |  | 140 |     public function get_events_select($selectwhere, array $params)
 | 
        
           |  |  | 141 |     {
 | 
        
           |  |  | 142 |         $logstores  = ['logstore_standard', 'logstore_legacy'];
 | 
        
           |  |  | 143 |         $return     = [];
 | 
        
           |  |  | 144 |   | 
        
           |  |  | 145 |         static $allreaders = null;
 | 
        
           |  |  | 146 |   | 
        
           |  |  | 147 |         if (is_null($allreaders)) {
 | 
        
           |  |  | 148 |             $allreaders = get_log_manager()->get_readers();
 | 
        
           |  |  | 149 |         }
 | 
        
           |  |  | 150 |   | 
        
           |  |  | 151 |         $processedreaders = 0;
 | 
        
           |  |  | 152 |   | 
        
           |  |  | 153 |         foreach ($logstores as $name) {
 | 
        
           |  |  | 154 |             if (isset($allreaders[$name])) {
 | 
        
           |  |  | 155 |                 $reader = $allreaders[$name];
 | 
        
           |  |  | 156 |                 $events = $reader->get_events_select($selectwhere, $params, 'timecreated ASC', 0, 0);
 | 
        
           |  |  | 157 |                 foreach ($events as $event) {
 | 
        
           |  |  | 158 |                     // Note: see \core\event\base to view base class of event.
 | 
        
           |  |  | 159 |                     $obj = new stdClass();
 | 
        
           |  |  | 160 |                     $obj->time = $event->timecreated;
 | 
        
           |  |  | 161 |                     $obj->ip = $event->get_logextra()['ip'];
 | 
        
           |  |  | 162 |                     $return[] = $obj;
 | 
        
           |  |  | 163 |                 }
 | 
        
           |  |  | 164 |                 if (!empty($events)) {
 | 
        
           |  |  | 165 |                     $processedreaders++;
 | 
        
           |  |  | 166 |                 }
 | 
        
           |  |  | 167 |             }
 | 
        
           |  |  | 168 |         }
 | 
        
           |  |  | 169 |   | 
        
           |  |  | 170 |         // Sort mixed array by time ascending again only when more of a reader has added events to return array.
 | 
        
           |  |  | 171 |         if ($processedreaders > 1) {
 | 
        
           |  |  | 172 |             usort($return, function($a, $b) {
 | 
        
           |  |  | 173 |                 return $a->time > $b->time;
 | 
        
           |  |  | 174 |             });
 | 
        
           |  |  | 175 |         }
 | 
        
           |  |  | 176 |   | 
        
           |  |  | 177 |         return $return;
 | 
        
           |  |  | 178 |     }
 | 
        
           |  |  | 179 |   | 
        
           |  |  | 180 |     public function get_students_dedication($user_id, $course_id, $min_time, $max_time, $limit)
 | 
        
           |  |  | 181 |     {
 | 
        
           |  |  | 182 |   | 
        
           |  |  | 183 |         $where = 'courseid = :courseid AND userid = :userid AND timecreated >= :mintime AND timecreated <= :maxtime';
 | 
        
           |  |  | 184 |         $params = array(
 | 
        
           |  |  | 185 |             'courseid'  => $course_id,
 | 
        
           |  |  | 186 |             'userid'    => $user_id,
 | 
        
           |  |  | 187 |             'mintime'   => $min_time,
 | 
        
           |  |  | 188 |             'maxtime'   => $max_time
 | 
        
           |  |  | 189 |         );
 | 
        
           |  |  | 190 |   | 
        
           |  |  | 191 |         //var_dump($params);//test
 | 
        
           |  |  | 192 |         //die();
 | 
        
           |  |  | 193 |   | 
        
           |  |  | 194 |   | 
        
           |  |  | 195 |         $logs = $this->get_events_select($where, $params);
 | 
        
           |  |  | 196 |         if ($logs) {
 | 
        
           |  |  | 197 |             $previouslog = array_shift($logs);
 | 
        
           |  |  | 198 |             $previouslogtime = $previouslog->time;
 | 
        
           |  |  | 199 |             $sessionstart = $previouslog->time;
 | 
        
           |  |  | 200 |             $dedication = 0;
 | 
        
           |  |  | 201 |   | 
        
           |  |  | 202 |             foreach ($logs as $log) {
 | 
        
           |  |  | 203 |                 if (($log->time - $previouslogtime) > $limit) {
 | 
        
           |  |  | 204 |                     $dedication += $previouslogtime - $sessionstart;
 | 
        
           |  |  | 205 |                     $sessionstart = $log->time;
 | 
        
           |  |  | 206 |                 }
 | 
        
           |  |  | 207 |                 $previouslogtime = $log->time;
 | 
        
           |  |  | 208 |             }
 | 
        
           |  |  | 209 |             $dedication += $previouslogtime - $sessionstart;
 | 
        
           |  |  | 210 |         } else {
 | 
        
           |  |  | 211 |             $dedication = 0;
 | 
        
           |  |  | 212 |         }
 | 
        
           |  |  | 213 |   | 
        
           |  |  | 214 |   | 
        
           |  |  | 215 |        // var_dump($dedication);//test
 | 
        
           |  |  | 216 |         //die();
 | 
        
           |  |  | 217 |   | 
        
           |  |  | 218 |         return $dedication;
 | 
        
           |  |  | 219 |   | 
        
           |  |  | 220 |     }
 | 
        
           |  |  | 221 |   | 
        
           |  |  | 222 |   | 
        
           |  |  | 223 |   | 
        
           |  |  | 224 |   | 
        
           |  |  | 225 |   | 
        
           |  |  | 226 |   | 
        
           |  |  | 227 | }
 |