bump version.
[moodle-mod_attendance.git] / tempmerge.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 * Merge temporary user with real user.
19 *
20 * @package mod_attendance
21 * @copyright 2013 Davo Smith, Synergy Learning
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25 require_once(dirname(__FILE__).'/../../config.php');
26
27 global $CFG, $DB, $PAGE, $OUTPUT;
28 require_once($CFG->dirroot.'/mod/attendance/locallib.php');
29 require_once($CFG->dirroot.'/mod/attendance/tempmerge_form.php');
30
31 $id = required_param('id', PARAM_INT);
32 $userid = required_param('userid', PARAM_INT);
33
34 $cm = get_coursemodule_from_id('attendance', $id, 0, false, MUST_EXIST);
35 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
36 $att = $DB->get_record('attendance', array('id' => $cm->instance), '*', MUST_EXIST);
37 $tempuser = $DB->get_record('attendance_tempusers', array('id' => $userid), '*', MUST_EXIST);
38
39 $att = new mod_attendance_structure($att, $cm, $course);
40 $params = array('userid' => $tempuser->id);
41 $PAGE->set_url($att->url_tempmerge($params));
42
43 require_login($course, true, $cm);
44
45 $PAGE->set_title($course->shortname.": ".$att->name.' - '.get_string('tempusermerge', 'attendance'));
46 $PAGE->set_heading($course->fullname);
47 $PAGE->set_cacheable(true);
48 $PAGE->set_button($OUTPUT->update_module_button($cm->id, 'attendance'));
49 $PAGE->navbar->add(get_string('tempusermerge', 'attendance'));
50
51 $formdata = (object)array(
52 'id' => $cm->id,
53 'userid' => $tempuser->id,
54 );
55
56 $custom = array(
57 'description' => format_string($tempuser->fullname).' ('.format_string($tempuser->email).')',
58 );
59 $mform = new tempmerge_form(null, $custom);
60 $mform->set_data($formdata);
61
62 if ($mform->is_cancelled()) {
63 redirect($att->url_managetemp());
64
65 } else if ($data = $mform->get_data()) {
66
67 $sql = "SELECT s.id, lr.id AS reallogid, lt.id AS templogid
68 FROM {attendance_sessions} s
69 LEFT JOIN {attendance_log} lr ON lr.sessionid = s.id AND lr.studentid = :realuserid
70 LEFT JOIN {attendance_log} lt ON lt.sessionid = s.id AND lt.studentid = :tempuserid
71 WHERE s.attendanceid = :attendanceid AND lt.id IS NOT NULL
72 ORDER BY s.id";
73 $params = array(
74 'realuserid' => $data->participant,
75 'tempuserid' => $tempuser->studentid,
76 'attendanceid' => $att->id,
77 );
78 $logs = $DB->get_recordset_sql($sql, $params);
79
80 foreach ($logs as $log) {
81 if (!is_null($log->reallogid)) {
82 // Remove the existing attendance for the real user for this session.
83 $DB->delete_records('attendance_log', array('id' => $log->reallogid));
84 }
85 // Adjust the 'temp user' attendance record to point at the real user.
86 $DB->set_field('attendance_log', 'studentid', $data->participant, array('id' => $log->templogid));
87 }
88
89 // Delete the temp user.
90 $DB->delete_records('attendance_tempusers', array('id' => $tempuser->id));
91 $att->update_users_grade(array($data->participant)); // Update the gradebook after the merge.
92
93 redirect($att->url_managetemp());
94 }
95
96 /** @var mod_attendance_renderer $output */
97 $output = $PAGE->get_renderer('mod_attendance');
98 $tabs = new attendance_tabs($att, attendance_tabs::TAB_TEMPORARYUSERS);
99
100 echo $output->header();
101 echo $output->heading(get_string('tempusermerge', 'attendance').' : '.format_string($course->fullname));
102 echo $output->render($tabs);
103 $mform->display();
104 echo $output->footer($course);