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