diff --git a/lib.php b/lib.php index 8daf024..cda89ba 100644 --- a/lib.php +++ b/lib.php @@ -14,8 +14,10 @@ function attforblock_supports($feature) { case FEATURE_GRADE_HAS_GRADE: return true; case FEATURE_GROUPS: return true; // Artem Andreev: AFAIK it's not tested + // we need implement filtration of groups list by grouping case FEATURE_GROUPINGS: return false; // Artem Andreev: AFAIK it's not tested + // harder "All courses" report case FEATURE_GROUPMEMBERSONLY: return false; case FEATURE_MOD_INTRO: return false; case FEATURE_BACKUP_MOODLE2: return true; @@ -45,7 +47,7 @@ function attforblock_add_instance($attforblock) { $DB->insert_record('attendance_statuses', $rec); } -// attforblock_grade_item_update($attforblock); + attforblock_grade_item_update($attforblock); // attforblock_update_grades($attforblock); return $attforblock->id; } @@ -217,10 +219,10 @@ function attforblock_cron () { * @param int $userid optional user id, 0 means all users * @return array array of grades, false if none */ -function attforblock_get_user_grades($attforblock, $userid=0) { +/*function attforblock_get_user_grades($attforblock, $userid=0) { global $CFG, $DB; - require_once('locallib.php'); + require_once('_locallib.php'); if (! $course = $DB->get_record('course', array('id'=> $attforblock->course))) { error("Course is misconfigured"); @@ -242,7 +244,7 @@ function attforblock_get_user_grades($attforblock, $userid=0) { } return $result; -} +}*/ /** * Update grades by firing grade_updated event @@ -250,7 +252,7 @@ function attforblock_get_user_grades($attforblock, $userid=0) { * @param object $attforblock null means all attforblocks * @param int $userid specific user only, 0 mean all */ -function attforblock_update_grades($attforblock=null, $userid=0, $nullifnone=true) { +/*function attforblock_update_grades($attforblock=null, $userid=0, $nullifnone=true) { global $CFG, $DB; if (!function_exists('grade_update')) { //workaround for buggy PHP versions require_once($CFG->libdir.'/gradelib.php'); @@ -283,7 +285,7 @@ function attforblock_update_grades($attforblock=null, $userid=0, $nullifnone=tru $rs->close($rs); } } -} +}*/ /** * Create grade item for given attforblock @@ -321,7 +323,6 @@ function attforblock_grade_item_update($attforblock, $grades=NULL) { $params['gradetype'] = GRADE_TYPE_VALUE; $params['grademax'] = $attforblock->grade; $params['grademin'] = 0; - } else if ($attforblock->grade < 0) { $params['gradetype'] = GRADE_TYPE_SCALE; diff --git a/locallib.php b/locallib.php index ed99fb3..2f5721b 100644 --- a/locallib.php +++ b/locallib.php @@ -332,8 +332,9 @@ class attforblock { private $sessioninfo; private $statuses; - private $usertakensesscount; - private $userstatusesstat; + // arrays by userid + private $usertakensesscount = array(); + private $userstatusesstat = array(); /** * Initializes the attendance API instance using the data from DB @@ -700,7 +701,7 @@ class attforblock { $sesslog = array(); $formdata = (array)$formdata; foreach($formdata as $key => $value) { - if(substr($key, 0, 4) == 'user' && $value !== '') { + if(substr($key, 0, 4) == 'user') { $sid = substr($key, 4); $sesslog[$sid] = new Object(); $sesslog[$sid]->studentid = $sid; @@ -715,12 +716,14 @@ class attforblock { $dbsesslog = $this->get_session_log($this->pageparams->sessionid); foreach ($sesslog as $log) { - if (array_key_exists($log->studentid, $dbsesslog)) { - $log->id = $dbsesslog[$log->studentid]->id; - $DB->update_record('attendance_log', $log); + if ($log->statusid) { + if (array_key_exists($log->studentid, $dbsesslog)) { + $log->id = $dbsesslog[$log->studentid]->id; + $DB->update_record('attendance_log', $log); + } + else + $DB->insert_record('attendance_log', $log, false); } - else - $DB->insert_record('attendance_log', $log, false); } $rec = new object(); @@ -729,7 +732,7 @@ class attforblock { $rec->lasttakenby = $USER->id; $DB->update_record('attendance_sessions', $rec); - // TODO: update_grades + $this->update_users_grade(array_keys($sesslog)); // TODO: log redirect($this->url_manage(), get_string('attendancesuccess','attforblock')); } @@ -838,16 +841,16 @@ class attforblock { public function get_user_taken_sessions_count($userid) { global $DB; - if (!isset($this->usertakensesscount)) - $this->usertakensesscount = get_user_taken_sessions_count($this->id, $this->course->startdate, $userid); + if (!array_key_exists($userid, $this->usertakensesscount)) + $this->usertakensesscount[$userid] = get_user_taken_sessions_count($this->id, $this->course->startdate, $userid); - return $this->usertakensesscount; + return $this->usertakensesscount[$userid]; } public function get_user_statuses_stat($userid) { global $DB; - if (!isset($this->userstatusesstat)) { + if (!array_key_exists($userid, $this->userstatusesstat)) { $qry = "SELECT al.statusid, count(al.statusid) AS stcnt FROM {attendance_log} al JOIN {attendance_sessions} ats @@ -861,10 +864,10 @@ class attforblock { 'cstartdate' => $this->course->startdate, 'uid' => $userid); - $this->userstatusesstat = $DB->get_records_sql($qry, $params); + $this->userstatusesstat[$userid] = $DB->get_records_sql($qry, $params); } - return $this->userstatusesstat; + return $this->userstatusesstat[$userid]; } public function get_user_grade($userid) { @@ -876,10 +879,24 @@ class attforblock { // In the future we can implement another methods: // * all sessions between user start enrolment date and now; // * all sessions between user start and end enrolment date. + // While implementing those methods we need recalculate grades of all users + // on session adding public function get_user_max_grade($userid) { return get_user_max_grade($this->get_user_taken_sessions_count($userid), $this->get_statuses()); } + public function update_users_grade($userids) { + $grades = array(); + + foreach ($userids as $userid) { + $grades[$userid]->userid = $userid; + $grades[$userid]->rawgrade = $this->get_user_grade($userid) / $this->get_user_max_grade($userid) * 100; + } + + return grade_update('mod/attforblock', $this->course->id, 'mod', 'attforblock', + $this->id, 0, $grades); + } + public function get_user_filtered_sessions_log($userid) { global $DB; @@ -996,4 +1013,23 @@ function get_user_courses_attendances($userid) { return $DB->get_records_sql($sql, $params); } +function update_all_users_grades($attid, $course, $context) { + global $COURSE; + + $grades = array(); + + $userids = get_enrolled_users($context, 'mod/attforblock:canbelisted', 0, 'u.id'); + + $statuses = get_statuses($attid); + foreach ($userids as $userid) { + $grades[$userid]->userid = $userid; + $userstatusesstat = get_user_statuses_stat($attid, $course->startdate, $userid); + $usertakensesscount = get_user_taken_sessions_count($attid, $course->startdate, $userid); + $grades[$userid]->rawgrade = get_user_grade($userstatusesstat, $statuses) / get_user_max_grade($usertakensesscount, $statuses) * 100; + } + + return grade_update('mod/attforblock', $course->id, 'mod', 'attforblock', + $attid, 0, $grades); +} + ?>