bump version.
[moodle-mod_attendance.git] / locallib.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17 /**
18 * local functions and constants for module attendance
19 *
20 * @package mod_attendance
21 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25 defined('MOODLE_INTERNAL') || die();
26
27 require_once($CFG->libdir . '/gradelib.php');
28 require_once(dirname(__FILE__).'/renderhelpers.php');
29
30 define('ATT_VIEW_DAYS', 1);
31 define('ATT_VIEW_WEEKS', 2);
32 define('ATT_VIEW_MONTHS', 3);
33 define('ATT_VIEW_ALLPAST', 4);
34 define('ATT_VIEW_ALL', 5);
35 define('ATT_VIEW_NOTPRESENT', 6);
36 define('ATT_VIEW_SUMMARY', 7);
37
38 define('ATT_SORT_DEFAULT', 0);
39 define('ATT_SORT_LASTNAME', 1);
40 define('ATT_SORT_FIRSTNAME', 2);
41
42 function attendance_get_statuses($attid, $onlyvisible=true, $statusset = -1) {
43 global $DB;
44
45 // Set selector.
46 $params = array('aid' => $attid);
47 $setsql = '';
48 if ($statusset >= 0) {
49 $params['statusset'] = $statusset;
50 $setsql = ' AND setnumber = :statusset ';
51 }
52
53 if ($onlyvisible) {
54 $statuses = $DB->get_records_select('attendance_statuses', "attendanceid = :aid AND visible = 1 AND deleted = 0 $setsql",
55 $params, 'setnumber ASC, grade DESC');
56 } else {
57 $statuses = $DB->get_records_select('attendance_statuses', "attendanceid = :aid AND deleted = 0 $setsql",
58 $params, 'setnumber ASC, grade DESC');
59 }
60
61 return $statuses;
62 }
63
64 /**
65 * Get the name of the status set.
66 *
67 * @param int $attid
68 * @param int $statusset
69 * @param bool $includevalues
70 * @return string
71 */
72 function attendance_get_setname($attid, $statusset, $includevalues = true) {
73 $statusname = get_string('statusset', 'mod_attendance', $statusset + 1);
74 if ($includevalues) {
75 $statuses = attendance_get_statuses($attid, true, $statusset);
76 $statusesout = array();
77 foreach ($statuses as $status) {
78 $statusesout[] = $status->acronym;
79 }
80 if ($statusesout) {
81 if (count($statusesout) > 6) {
82 $statusesout = array_slice($statusesout, 0, 6);
83 $statusesout[] = '&helip;';
84 }
85 $statusesout = implode(' ', $statusesout);
86 $statusname .= ' ('.$statusesout.')';
87 }
88 }
89
90 return $statusname;
91 }
92
93 function attendance_get_user_courses_attendances($userid) {
94 global $DB;
95
96 $usercourses = enrol_get_users_courses($userid);
97
98 list($usql, $uparams) = $DB->get_in_or_equal(array_keys($usercourses), SQL_PARAMS_NAMED, 'cid0');
99
100 $sql = "SELECT att.id as attid, att.course as courseid, course.fullname as coursefullname,
101 course.startdate as coursestartdate, att.name as attname, att.grade as attgrade
102 FROM {attendance} att
103 JOIN {course} course
104 ON att.course = course.id
105 WHERE att.course $usql
106 ORDER BY coursefullname ASC, attname ASC";
107
108 $params = array_merge($uparams, array('uid' => $userid));
109
110 return $DB->get_records_sql($sql, $params);
111 }
112
113 /**
114 * Used to calculate a fraction based on the part and total values
115 *
116 * @param float $part - part of the total value
117 * @param float $total - total value.
118 * @return float the calculated fraction.
119 */
120 function attendance_calc_fraction($part, $total) {
121 if ($total == 0) {
122 return 0;
123 } else {
124 return $part / $total;
125 }
126 }
127
128 /**
129 * Check to see if statusid in use to help prevent deletion etc.
130 *
131 * @param integer $statusid
132 */
133 function attendance_has_logs_for_status($statusid) {
134 global $DB;
135 return $DB->record_exists('attendance_log', array('statusid' => $statusid));
136 }
137
138 /**
139 * Helper function to add sessiondate_selector to add/update forms.
140 *
141 * @param MoodleQuickForm $mform
142 */
143 function attendance_form_sessiondate_selector (MoodleQuickForm $mform) {
144
145 $mform->addElement('date_selector', 'sessiondate', get_string('sessiondate', 'attendance'));
146
147 for ($i = 0; $i <= 23; $i++) {
148 $hours[$i] = sprintf("%02d", $i);
149 }
150 for ($i = 0; $i < 60; $i += 5) {
151 $minutes[$i] = sprintf("%02d", $i);
152 }
153
154 $sesendtime = array();
155 $sesendtime[] =& $mform->createElement('static', 'from', '', get_string('from', 'attendance'));
156 $sesendtime[] =& $mform->createElement('select', 'starthour', get_string('hour', 'form'), $hours, false, true);
157 $sesendtime[] =& $mform->createElement('select', 'startminute', get_string('minute', 'form'), $minutes, false, true);
158 $sesendtime[] =& $mform->createElement('static', 'to', '', get_string('to', 'attendance'));
159 $sesendtime[] =& $mform->createElement('select', 'endhour', get_string('hour', 'form'), $hours, false, true);
160 $sesendtime[] =& $mform->createElement('select', 'endminute', get_string('minute', 'form'), $minutes, false, true);
161 $mform->addGroup($sesendtime, 'sestime', get_string('time', 'attendance'), array(' '), true);
162 }
163
164 /**
165 * Count the number of status sets that exist for this instance.
166 *
167 * @param int $attendanceid
168 * @return int
169 */
170 function attendance_get_max_statusset($attendanceid) {
171 global $DB;
172
173 $max = $DB->get_field_sql('SELECT MAX(setnumber) FROM {attendance_statuses} WHERE attendanceid = ? AND deleted = 0',
174 array($attendanceid));
175 if ($max) {
176 return $max;
177 }
178 return 0;
179 }
180
181 /**
182 * Returns the maxpoints for each statusset
183 *
184 * @param array statuses
185 * @return array
186 */
187 function attendance_get_statusset_maxpoints($statuses) {
188 $statussetmaxpoints = array();
189 foreach ($statuses as $st) {
190 if (!isset($statussetmaxpoints[$st->setnumber])) {
191 $statussetmaxpoints[$st->setnumber] = $st->grade;
192 }
193 }
194 return $statussetmaxpoints;
195 }
196
197 /**
198 * Update user grades
199 *
200 * @param mixed mod_attendance_structure|stdClass $attendance
201 * @param array $userids
202 */
203 function attendance_update_users_grade($attendance, $userids=array()) {
204 global $DB;
205
206 if (empty($attendance->grade)) {
207 return false;
208 }
209
210 list($course, $cm) = get_course_and_cm_from_instance($attendance->id, 'attendance');
211
212 $summary = new mod_attendance_summary($attendance->id, $userids);
213
214 if (empty($userids)) {
215 $context = context_module::instance($cm->id);
216 $userids = array_keys(get_enrolled_users($context, 'mod/attendance:canbelisted', 0, 'u.id'));
217 }
218
219 if ($attendance->grade < 0) {
220 $dbparams = array('id' => -($attendance->grade));
221 $scale = $DB->get_record('scale', $dbparams);
222 $scalearray = explode(',', $scale->scale);
223 $attendancegrade = count($scalearray);
224 } else {
225 $attendancegrade = $attendance->grade;
226 }
227
228 $grades = array();
229 foreach ($userids as $userid) {
230 $grades[$userid] = new stdClass();
231 $grades[$userid]->userid = $userid;
232
233 if ($summary->has_taken_sessions($userid)) {
234 $usersummary = $summary->get_taken_sessions_summary_for($userid);
235 $grades[$userid]->rawgrade = $usersummary->takensessionspercentage * $attendancegrade;
236 } else {
237 $grades[$userid]->rawgrade = null;
238 }
239 }
240
241 return grade_update('mod/attendance', $course->id, 'mod', 'attendance', $attendance->id, 0, $grades);
242 }