update README
[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
21 * @subpackage attendance
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_attenance_form.php');
28
29 $pageparams = new att_sessions_page_params();
30
31 // Check that the required parameters are present.
32 $id = required_param('sessid', PARAM_INT);
33 $attendance_session_id = required_param('sessid', PARAM_INT);
34
35
36 $attforsession = $DB->get_record('attendance_sessions', array('id' => $id), '*', MUST_EXIST);
37 $attendance = $DB->get_record('attendance', array('id' => $attforsession->attendanceid), '*', MUST_EXIST);
38 $cm = get_coursemodule_from_instance('attendance', $attendance->id, 0, false, MUST_EXIST);
39 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
40
41 // Require the user is logged in.
42 require_login($course, true, $cm);
43
44 $pageparams->sessionid = $id;
45 $att = new attendance($attendance, $cm, $course, $PAGE->context, $pageparams);
46
47 // Require that a session key is passed to this page.
48 require_sesskey();
49
50 // Create the form.
51 $mform = new mod_attendance_student_attendance_form(null,
52 array('course' => $course, 'cm' => $cm, 'modcontext' => $PAGE->context, 'session' => $attforsession, 'attendance' => $att));
53
54 if ($mform->is_cancelled()) {
55 // The user cancelled the form, so redirect them to the view page.
56 $url = new moodle_url('/mod/attendance/view.php', array('id' => $cm->id));
57 redirect($url);
58 } else if ($fromform = $mform->get_data()) {
59 if (!empty($fromform->status)) {
60 $success = $att->take_from_student($fromform);
61
62 $url = new moodle_url('/mod/attendance/view.php', array('id' => $cm->id));
63 if ($success) {
64 // Redirect back to the view page for the block.
65 redirect($url);
66 } else {
67 print_error ('attendance_already_submitted', 'mod_attendance', $url);
68 }
69 }
70
71 // The form did not validate correctly so we will set it to display the data they submitted.
72 $mform->set_data($fromform);
73 }
74
75 $PAGE->set_url($att->url_sessions());
76 $PAGE->set_title($course->shortname. ": ".$att->name);
77 $PAGE->set_heading($course->fullname);
78 $PAGE->set_cacheable(true);
79 $PAGE->navbar->add($att->name);
80
81 $output = $PAGE->get_renderer('mod_attendance');
82 echo $output->header();
83 $mform->display();
84 echo $output->footer();