Browse Source

Implementations and fixes of working with gradebook

MOODLE_23_STABLE
Artem Andreev 14 years ago
parent
commit
0d26cb07f0
  1. 15
      lib.php
  2. 66
      locallib.php

15
lib.php

@ -14,8 +14,10 @@ function attforblock_supports($feature) {
case FEATURE_GRADE_HAS_GRADE: return true; case FEATURE_GRADE_HAS_GRADE: return true;
case FEATURE_GROUPS: return true; case FEATURE_GROUPS: return true;
// Artem Andreev: AFAIK it's not tested // Artem Andreev: AFAIK it's not tested
// we need implement filtration of groups list by grouping
case FEATURE_GROUPINGS: return false; case FEATURE_GROUPINGS: return false;
// Artem Andreev: AFAIK it's not tested // Artem Andreev: AFAIK it's not tested
// harder "All courses" report
case FEATURE_GROUPMEMBERSONLY: return false; case FEATURE_GROUPMEMBERSONLY: return false;
case FEATURE_MOD_INTRO: return false; case FEATURE_MOD_INTRO: return false;
case FEATURE_BACKUP_MOODLE2: return true; case FEATURE_BACKUP_MOODLE2: return true;
@ -45,7 +47,7 @@ function attforblock_add_instance($attforblock) {
$DB->insert_record('attendance_statuses', $rec); $DB->insert_record('attendance_statuses', $rec);
} }
// attforblock_grade_item_update($attforblock); attforblock_grade_item_update($attforblock);
// attforblock_update_grades($attforblock); // attforblock_update_grades($attforblock);
return $attforblock->id; return $attforblock->id;
} }
@ -217,10 +219,10 @@ function attforblock_cron () {
* @param int $userid optional user id, 0 means all users * @param int $userid optional user id, 0 means all users
* @return array array of grades, false if none * @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; global $CFG, $DB;
require_once('locallib.php'); require_once('_locallib.php');
if (! $course = $DB->get_record('course', array('id'=> $attforblock->course))) { if (! $course = $DB->get_record('course', array('id'=> $attforblock->course))) {
error("Course is misconfigured"); error("Course is misconfigured");
@ -242,7 +244,7 @@ function attforblock_get_user_grades($attforblock, $userid=0) {
} }
return $result; return $result;
} }*/
/** /**
* Update grades by firing grade_updated event * 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 object $attforblock null means all attforblocks
* @param int $userid specific user only, 0 mean all * @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; global $CFG, $DB;
if (!function_exists('grade_update')) { //workaround for buggy PHP versions if (!function_exists('grade_update')) { //workaround for buggy PHP versions
require_once($CFG->libdir.'/gradelib.php'); require_once($CFG->libdir.'/gradelib.php');
@ -283,7 +285,7 @@ function attforblock_update_grades($attforblock=null, $userid=0, $nullifnone=tru
$rs->close($rs); $rs->close($rs);
} }
} }
} }*/
/** /**
* Create grade item for given attforblock * Create grade item for given attforblock
@ -321,7 +323,6 @@ function attforblock_grade_item_update($attforblock, $grades=NULL) {
$params['gradetype'] = GRADE_TYPE_VALUE; $params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $attforblock->grade; $params['grademax'] = $attforblock->grade;
$params['grademin'] = 0; $params['grademin'] = 0;
} }
else if ($attforblock->grade < 0) { else if ($attforblock->grade < 0) {
$params['gradetype'] = GRADE_TYPE_SCALE; $params['gradetype'] = GRADE_TYPE_SCALE;

66
locallib.php

@ -332,8 +332,9 @@ class attforblock {
private $sessioninfo; private $sessioninfo;
private $statuses; private $statuses;
private $usertakensesscount; // arrays by userid
private $userstatusesstat; private $usertakensesscount = array();
private $userstatusesstat = array();
/** /**
* Initializes the attendance API instance using the data from DB * Initializes the attendance API instance using the data from DB
@ -700,7 +701,7 @@ class attforblock {
$sesslog = array(); $sesslog = array();
$formdata = (array)$formdata; $formdata = (array)$formdata;
foreach($formdata as $key => $value) { foreach($formdata as $key => $value) {
if(substr($key, 0, 4) == 'user' && $value !== '') { if(substr($key, 0, 4) == 'user') {
$sid = substr($key, 4); $sid = substr($key, 4);
$sesslog[$sid] = new Object(); $sesslog[$sid] = new Object();
$sesslog[$sid]->studentid = $sid; $sesslog[$sid]->studentid = $sid;
@ -715,12 +716,14 @@ class attforblock {
$dbsesslog = $this->get_session_log($this->pageparams->sessionid); $dbsesslog = $this->get_session_log($this->pageparams->sessionid);
foreach ($sesslog as $log) { foreach ($sesslog as $log) {
if (array_key_exists($log->studentid, $dbsesslog)) { if ($log->statusid) {
$log->id = $dbsesslog[$log->studentid]->id; if (array_key_exists($log->studentid, $dbsesslog)) {
$DB->update_record('attendance_log', $log); $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(); $rec = new object();
@ -729,7 +732,7 @@ class attforblock {
$rec->lasttakenby = $USER->id; $rec->lasttakenby = $USER->id;
$DB->update_record('attendance_sessions', $rec); $DB->update_record('attendance_sessions', $rec);
// TODO: update_grades $this->update_users_grade(array_keys($sesslog));
// TODO: log // TODO: log
redirect($this->url_manage(), get_string('attendancesuccess','attforblock')); redirect($this->url_manage(), get_string('attendancesuccess','attforblock'));
} }
@ -838,16 +841,16 @@ class attforblock {
public function get_user_taken_sessions_count($userid) { public function get_user_taken_sessions_count($userid) {
global $DB; global $DB;
if (!isset($this->usertakensesscount)) if (!array_key_exists($userid, $this->usertakensesscount))
$this->usertakensesscount = get_user_taken_sessions_count($this->id, $this->course->startdate, $userid); $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) { public function get_user_statuses_stat($userid) {
global $DB; global $DB;
if (!isset($this->userstatusesstat)) { if (!array_key_exists($userid, $this->userstatusesstat)) {
$qry = "SELECT al.statusid, count(al.statusid) AS stcnt $qry = "SELECT al.statusid, count(al.statusid) AS stcnt
FROM {attendance_log} al FROM {attendance_log} al
JOIN {attendance_sessions} ats JOIN {attendance_sessions} ats
@ -861,10 +864,10 @@ class attforblock {
'cstartdate' => $this->course->startdate, 'cstartdate' => $this->course->startdate,
'uid' => $userid); '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) { public function get_user_grade($userid) {
@ -876,10 +879,24 @@ class attforblock {
// In the future we can implement another methods: // In the future we can implement another methods:
// * all sessions between user start enrolment date and now; // * all sessions between user start enrolment date and now;
// * all sessions between user start and end enrolment date. // * 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) { public function get_user_max_grade($userid) {
return get_user_max_grade($this->get_user_taken_sessions_count($userid), $this->get_statuses()); 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) { public function get_user_filtered_sessions_log($userid) {
global $DB; global $DB;
@ -996,4 +1013,23 @@ function get_user_courses_attendances($userid) {
return $DB->get_records_sql($sql, $params); 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);
}
?> ?>

Loading…
Cancel
Save