diff --git a/classes/task/grade_participation_task.php b/classes/task/grade_participation_task.php index 6dae565..f859ff8 100644 --- a/classes/task/grade_participation_task.php +++ b/classes/task/grade_participation_task.php @@ -44,17 +44,17 @@ class grade_participation_task extends \core\task\scheduled_task { $palestras = $DB->get_records_sql(" select p.id, p.course, p.name, p.startdate, p.duration, p.checkinterval from {palestra} p - inner join {palestra_presence} pp on pp.palestraid = p.id inner join {course} c on c.id = p.course where (p.needsupdate = 1) and (c.visible = 1) and - ((c.startdate <= ?) and (c.enddate = 0 or c.enddate >= ?)) - and ((p.startdate + (p.duration*60)) < ?) - group by p.id, p.course, p.name, p.startdate, p.duration, p.checkinterval - having max(pp.lastcheck) < ? - p.checkinterval*60 - - ", [$now, $now, $now, $now]); + ((c.startdate <= ?) and (c.enddate = 0 or c.enddate >= ?)) and + (select count(*) + from {palestra_presence} pp + where pp.palestraid = p.id and + (pp.lastcheck+(p.duration*60)) < ? + ) > 0 + ", [$now, $now, $now]); mtrace(count($palestras)." palestras to grade..."); diff --git a/grade.php b/grade.php index 63920e1..a9073c0 100644 --- a/grade.php +++ b/grade.php @@ -26,6 +26,7 @@ require_once(__DIR__ . '/../../config.php'); require_once("lib.php"); +require_once($CFG->libdir."/gradelib.php"); $id = required_param('id', PARAM_INT); $userid = optional_param('userid', 0, PARAM_INT); diff --git a/lib.php b/lib.php index 9f061aa..e0b3e40 100644 --- a/lib.php +++ b/lib.php @@ -207,10 +207,12 @@ function palestra_update_grades($palestra, $userid=0, $nullifnone=true) { global $CFG, $DB; require_once($CFG->libdir.'/gradelib.php'); + $now = time(); + if ($userid) { - $presences = $DB->get_records("palestra_presence", array("palestraid"=>$palestra->id, "userid"=>$userid), 'userid, starttime, lastcheck'); + $presences = $DB->get_records("palestra_presence", array("palestraid"=>$palestra->id, "userid"=>$userid), 'id, userid, starttime, lastcheck'); } else { - $presences = $DB->get_records("palestra_presence", array("palestraid"=>$palestra->id), 'userid, starttime, lastcheck'); + $presences = $DB->get_records_select("palestra_presence", "palestraid=? and lastcheck+? < ?", [$palestra->id, $palestra->duration*60, $now], 'userid, starttime', 'id, userid, starttime, lastcheck'); } $audiences = array(); @@ -231,7 +233,9 @@ function palestra_update_grades($palestra, $userid=0, $nullifnone=true) { continue; } if ($presence->starttime < ($start + $minimal)) { - $end = $presence->lastcheck; + if ($end < $presence->lastcheck) { + $end = $presence->lastcheck; + } } else { $audiences[$user] = (isset($audiences[$user])?$audiences[$user]:0) + (max($end - $start, $minimal)/60); $start = $presence->starttime;