Browse Source

Tidy up a bit.

remove old att_log_convert_url functoin
use record_exists instead of weird count
add some phpdocs
rename some att functions to correct prefix
MOODLE_29_STABLE
Dan Marsden 9 years ago
parent
commit
ad1462b9d3
  1. 57
      locallib.php
  2. 4
      preferences.php
  3. 2
      renderables.php
  4. 4
      sessions.php

57
locallib.php

@ -1244,7 +1244,7 @@ class attendance {
foreach ($userids as $userid) { foreach ($userids as $userid) {
$grades[$userid] = new stdClass(); $grades[$userid] = new stdClass();
$grades[$userid]->userid = $userid; $grades[$userid]->userid = $userid;
$grades[$userid]->rawgrade = att_calc_user_grade_fraction($this->get_user_grade($userid), $grades[$userid]->rawgrade = attendance_calc_user_grade_fraction($this->get_user_grade($userid),
$this->get_user_max_grade($userid)) * $this->grade; $this->get_user_max_grade($userid)) * $this->grade;
} }
@ -1692,7 +1692,13 @@ function att_get_user_courses_attendances($userid) {
return $DB->get_records_sql($sql, $params); return $DB->get_records_sql($sql, $params);
} }
function att_calc_user_grade_fraction($grade, $maxgrade) { /**
* Used to caclulate usergrade based on rawgrade and max grade.
*
* @param $grade - raw grade for user
* @param $maxgrade - maxgrade for this session.
*/
function attendance_calc_user_grade_fraction($grade, $maxgrade) {
if ($maxgrade == 0) { if ($maxgrade == 0) {
return 0; return 0;
} else { } else {
@ -1700,13 +1706,16 @@ function att_calc_user_grade_fraction($grade, $maxgrade) {
} }
} }
function att_get_gradebook_maxgrade($attid) { /**
* Update all user grades - used when settings have changed.
*
* @param $attid
* @param $course
* @param $context
* @param $coursemodule
*/
function attendance_update_all_users_grades($attid, $course, $context, $coursemodule) {
global $DB; global $DB;
return $DB->get_field('attendance', 'grade', array('id' => $attid));
}
function att_update_all_users_grades($attid, $course, $context, $coursemodule) {
$grades = array(); $grades = array();
$userids = array_keys(get_enrolled_users($context, 'mod/attendance:canbelisted', 0, 'u.id')); $userids = array_keys(get_enrolled_users($context, 'mod/attendance:canbelisted', 0, 'u.id'));
@ -1717,7 +1726,8 @@ function att_update_all_users_grades($attid, $course, $context, $coursemodule) {
$usergrades = $attgrades->items[0]->grades; $usergrades = $attgrades->items[0]->grades;
} }
$statuses = att_get_statuses($attid); $statuses = att_get_statuses($attid);
$gradebookmaxgrade = att_get_gradebook_maxgrade($attid); // TODO: we should be able to pass full $attendance record into this function and avoid this db call.
$gradebookmaxgrade = $DB->get_field('attendance', 'grade', array('id' => $attid));
foreach ($usergrades as $userid => $existinggrade) { foreach ($usergrades as $userid => $existinggrade) {
if (is_null($existinggrade->grade)) { if (is_null($existinggrade->grade)) {
// Don't update grades where one doesn't exist yet. // Don't update grades where one doesn't exist yet.
@ -1729,7 +1739,7 @@ function att_update_all_users_grades($attid, $course, $context, $coursemodule) {
$usertakensesscount = att_get_user_taken_sessions_count($attid, $course->startdate, $userid, $coursemodule); $usertakensesscount = att_get_user_taken_sessions_count($attid, $course->startdate, $userid, $coursemodule);
$usergrade = att_get_user_grade($userstatusesstat, $statuses); $usergrade = att_get_user_grade($userstatusesstat, $statuses);
$usermaxgrade = att_get_user_max_grade($usertakensesscount, $statuses); $usermaxgrade = att_get_user_max_grade($usertakensesscount, $statuses);
$grade->rawgrade = att_calc_user_grade_fraction($usergrade, $usermaxgrade) * $gradebookmaxgrade; $grade->rawgrade = attendance_calc_user_grade_fraction($usergrade, $usermaxgrade) * $gradebookmaxgrade;
$grades[$userid] = $grade; $grades[$userid] = $grade;
} }
@ -1743,24 +1753,21 @@ function att_update_all_users_grades($attid, $course, $context, $coursemodule) {
return $result; return $result;
} }
function att_has_logs_for_status($statusid) { /**
* Check to see if statusid in use to help prevent deletion etc.
*
* @param integer $statusid
*/
function attendance_has_logs_for_status($statusid) {
global $DB; global $DB;
return $DB->record_exists('attendance_log', array('statusid' => $statusid));
return $DB->count_records('attendance_log', array('statusid' => $statusid)) > 0;
}
function att_log_convert_url(moodle_url $fullurl) {
static $baseurl;
if (!isset($baseurl)) {
$baseurl = new moodle_url('/mod/attendance/');
$baseurl = $baseurl->out();
}
return substr($fullurl->out(), strlen($baseurl));
} }
// Helper function to add sessiondate_selector to form. /**
* Helper function to add sessiondate_selector to add/update forms.
*
* @param MoodleQuickForm $mform
*/
function attendance_form_sessiondate_selector (MoodleQuickForm $mform) { function attendance_form_sessiondate_selector (MoodleQuickForm $mform) {
$mform->addElement('date_selector', 'sessiondate', get_string('sessiondate', 'attendance')); $mform->addElement('date_selector', 'sessiondate', get_string('sessiondate', 'attendance'));

4
preferences.php

@ -76,7 +76,7 @@ switch ($att->pageparams->action) {
} }
break; break;
case att_preferences_page_params::ACTION_DELETE: case att_preferences_page_params::ACTION_DELETE:
if (att_has_logs_for_status($att->pageparams->statusid)) { if (attendance_has_logs_for_status($att->pageparams->statusid)) {
print_error('cantdeletestatus', 'attendance', "attsettings.php?id=$id"); print_error('cantdeletestatus', 'attendance', "attsettings.php?id=$id");
} }
@ -123,7 +123,7 @@ switch ($att->pageparams->action) {
$errors[$id] = $att->update_status($status, $acronym[$id], $description[$id], $grade[$id], null); $errors[$id] = $att->update_status($status, $acronym[$id], $description[$id], $grade[$id], null);
} }
if ($att->grade > 0) { if ($att->grade > 0) {
att_update_all_users_grades($att->id, $att->course, $att->context, $cm); attendance_update_all_users_grades($att->id, $att->course, $att->context, $cm);
} }
break; break;
} }

2
renderables.php

@ -554,7 +554,7 @@ class attendance_preferences_data implements renderable {
$this->errors = $errors; $this->errors = $errors;
foreach ($this->statuses as $st) { foreach ($this->statuses as $st) {
$st->haslogs = att_has_logs_for_status ($st->id); $st->haslogs = attendance_has_logs_for_status($st->id);
} }
$this->att = $att; $this->att = $att;

4
sessions.php

@ -114,7 +114,7 @@ switch ($att->pageparams->action) {
if (isset($confirm) && confirm_sesskey()) { if (isset($confirm) && confirm_sesskey()) {
$att->delete_sessions(array($sessionid)); $att->delete_sessions(array($sessionid));
if ($att->grade > 0) { if ($att->grade > 0) {
att_update_all_users_grades($att->id, $att->course, $att->context, $cm); attendance_update_all_users_grades($att->id, $att->course, $att->context, $cm);
} }
redirect($att->url_manage(), get_string('sessiondeleted', 'attendance')); redirect($att->url_manage(), get_string('sessiondeleted', 'attendance'));
} }
@ -143,7 +143,7 @@ switch ($att->pageparams->action) {
$att->delete_sessions($sessionsids); $att->delete_sessions($sessionsids);
if ($att->grade > 0) { if ($att->grade > 0) {
att_update_all_users_grades($att->id, $att->course, $att->context, $cm); attendance_update_all_users_grades($att->id, $att->course, $att->context, $cm);
} }
redirect($att->url_manage(), get_string('sessiondeleted', 'attendance')); redirect($att->url_manage(), get_string('sessiondeleted', 'attendance'));
} }

Loading…
Cancel
Save