From ad1462b9d39f6e264a01c523579467d2c71f0f33 Mon Sep 17 00:00:00 2001 From: Dan Marsden Date: Tue, 15 Mar 2016 12:19:47 +1300 Subject: [PATCH] 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 --- locallib.php | 59 +++++++++++++++++++++++++++---------------------- preferences.php | 4 ++-- renderables.php | 2 +- sessions.php | 4 ++-- 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/locallib.php b/locallib.php index e2310f3..d057aa8 100644 --- a/locallib.php +++ b/locallib.php @@ -1244,8 +1244,8 @@ class attendance { foreach ($userids as $userid) { $grades[$userid] = new stdClass(); $grades[$userid]->userid = $userid; - $grades[$userid]->rawgrade = att_calc_user_grade_fraction($this->get_user_grade($userid), - $this->get_user_max_grade($userid)) * $this->grade; + $grades[$userid]->rawgrade = attendance_calc_user_grade_fraction($this->get_user_grade($userid), + $this->get_user_max_grade($userid)) * $this->grade; } return grade_update('mod/attendance', $this->course->id, 'mod', 'attendance', @@ -1692,7 +1692,13 @@ function att_get_user_courses_attendances($userid) { 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) { return 0; } 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; - - return $DB->get_field('attendance', 'grade', array('id' => $attid)); -} - -function att_update_all_users_grades($attid, $course, $context, $coursemodule) { $grades = array(); $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; } $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) { if (is_null($existinggrade->grade)) { // 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); $usergrade = att_get_user_grade($userstatusesstat, $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; } @@ -1743,24 +1753,21 @@ function att_update_all_users_grades($attid, $course, $context, $coursemodule) { 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; - - 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)); + return $DB->record_exists('attendance_log', array('statusid' => $statusid)); } -// 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) { $mform->addElement('date_selector', 'sessiondate', get_string('sessiondate', 'attendance')); diff --git a/preferences.php b/preferences.php index 5e2f99c..03bbd3c 100644 --- a/preferences.php +++ b/preferences.php @@ -76,7 +76,7 @@ switch ($att->pageparams->action) { } break; 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"); } @@ -123,7 +123,7 @@ switch ($att->pageparams->action) { $errors[$id] = $att->update_status($status, $acronym[$id], $description[$id], $grade[$id], null); } 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; } diff --git a/renderables.php b/renderables.php index 64329f9..925393d 100644 --- a/renderables.php +++ b/renderables.php @@ -554,7 +554,7 @@ class attendance_preferences_data implements renderable { $this->errors = $errors; 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; diff --git a/sessions.php b/sessions.php index 18ffee4..e42768f 100644 --- a/sessions.php +++ b/sessions.php @@ -114,7 +114,7 @@ switch ($att->pageparams->action) { if (isset($confirm) && confirm_sesskey()) { $att->delete_sessions(array($sessionid)); 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')); } @@ -143,7 +143,7 @@ switch ($att->pageparams->action) { $att->delete_sessions($sessionsids); 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')); }