Implement data privacy provider.
[moodle-mod_attendance.git] / attendance.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 * Prints attendance info for particular user
19 *
20 * @package mod_attendance
21 * @copyright 2014 Dan Marsden
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25 require_once(dirname(__FILE__).'/../../config.php');
26 require_once(dirname(__FILE__).'/locallib.php');
27 require_once(dirname(__FILE__).'/student_attendance_form.php');
28
29 $pageparams = new mod_attendance_sessions_page_params();
30
31 // Check that the required parameters are present.
32 $id = required_param('sessid', PARAM_INT);
33
34 $attforsession = $DB->get_record('attendance_sessions', array('id' => $id), '*', MUST_EXIST);
35 $attendance = $DB->get_record('attendance', array('id' => $attforsession->attendanceid), '*', MUST_EXIST);
36 $cm = get_coursemodule_from_instance('attendance', $attendance->id, 0, false, MUST_EXIST);
37 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
38
39 // Require the user is logged in.
40 require_login($course, true, $cm);
41
42 if (!attendance_can_student_mark($attforsession)) {
43 // TODO: should we add a log message here? - student has got to submit page but cannot save attendance (time ran out?)
44 redirect(new moodle_url('/mod/attendance/view.php', array('id' => $cm->id)));
45 exit;
46 }
47
48 // Check if subnet is set and if the user is in the allowed range.
49 if (!empty($attforsession->subnet) && !address_in_subnet(getremoteaddr(), $attforsession->subnet)) {
50 $url = new moodle_url('/mod/attendance/view.php', array('id' => $cm->id));
51 notice(get_string('subnetwrong', 'attendance'), $url);
52 exit; // Notice calls this anyway.
53 }
54
55 $pageparams->sessionid = $id;
56 $att = new mod_attendance_structure($attendance, $cm, $course, $PAGE->context, $pageparams);
57
58 // Require that a session key is passed to this page.
59 require_sesskey();
60
61 // Check to see if autoassignstatus is in use and no password required.
62 if ($attforsession->autoassignstatus && empty($attforsession->studentpassword)) {
63 $statusid = attendance_session_get_highest_status($att, $attforsession);
64 $url = new moodle_url('/mod/attendance/view.php', array('id' => $cm->id));
65 if (empty($statusid)) {
66 print_error('attendance_no_status', 'mod_attendance', $url);
67 }
68 $take = new stdClass();
69 $take->status = $statusid;
70 $take->sessid = $attforsession->id;
71 $success = $att->take_from_student($take);
72
73 if ($success) {
74 // Redirect back to the view page.
75 redirect($url, get_string('studentmarked', 'attendance'));
76 } else {
77 print_error('attendance_already_submitted', 'mod_attendance', $url);
78 }
79 }
80
81 // Create the form.
82 $mform = new mod_attendance_student_attendance_form(null,
83 array('course' => $course, 'cm' => $cm, 'modcontext' => $PAGE->context, 'session' => $attforsession, 'attendance' => $att));
84
85 $PAGE->set_url($att->url_sessions());
86
87 if ($mform->is_cancelled()) {
88 // The user cancelled the form, so redirect them to the view page.
89 $url = new moodle_url('/mod/attendance/view.php', array('id' => $cm->id));
90 redirect($url);
91 } else if ($fromform = $mform->get_data()) {
92 // Check if password required and if set correctly.
93 if (!empty($attforsession->studentpassword) &&
94 $attforsession->studentpassword !== $fromform->studentpassword) {
95
96 $url = new moodle_url('/mod/attendance/attendance.php', array('sessid' => $id, 'sesskey' => sesskey()));
97 redirect($url, get_string('incorrectpassword', 'mod_attendance'), null, \core\output\notification::NOTIFY_ERROR);
98 }
99 if ($attforsession->autoassignstatus) {
100 $fromform->status = attendance_session_get_highest_status($att, $attforsession);
101 if (empty($fromform->status)) {
102 $url = new moodle_url('/mod/attendance/view.php', array('id' => $cm->id));
103 print_error('attendance_no_status', 'mod_attendance', $url);
104 }
105 }
106
107 if (!empty($fromform->status)) {
108 $success = $att->take_from_student($fromform);
109
110 $url = new moodle_url('/mod/attendance/view.php', array('id' => $cm->id));
111 if ($success) {
112 // Redirect back to the view page.
113 redirect($url, get_string('studentmarked', 'attendance'));
114 } else {
115 print_error('attendance_already_submitted', 'mod_attendance', $url);
116 }
117 }
118
119 // The form did not validate correctly so we will set it to display the data they submitted.
120 $mform->set_data($fromform);
121 }
122
123 $PAGE->set_title($course->shortname. ": ".$att->name);
124 $PAGE->set_heading($course->fullname);
125 $PAGE->set_cacheable(true);
126 $PAGE->navbar->add($att->name);
127
128 $output = $PAGE->get_renderer('mod_attendance');
129 echo $output->header();
130 $mform->display();
131 echo $output->footer();
132