Implement data privacy provider.
[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->navbar->add(get_string('tempusermerge', 'attendance'));
49
50 $formdata = (object)array(
51 'id' => $cm->id,
52 'userid' => $tempuser->id,
53 );
54
55 $custom = array(
56 'description' => format_string($tempuser->fullname).' ('.format_string($tempuser->email).')',
57 );
58 $mform = new tempmerge_form(null, $custom);
59 $mform->set_data($formdata);
60
61 if ($mform->is_cancelled()) {
62 redirect($att->url_managetemp());
63
64 } else if ($data = $mform->get_data()) {
65
66 $sql = "SELECT s.id, lr.id AS reallogid, lt.id AS templogid
67 FROM {attendance_sessions} s
68 LEFT JOIN {attendance_log} lr ON lr.sessionid = s.id AND lr.studentid = :realuserid
69 LEFT JOIN {attendance_log} lt ON lt.sessionid = s.id AND lt.studentid = :tempuserid
70 WHERE s.attendanceid = :attendanceid AND lt.id IS NOT NULL
71 ORDER BY s.id";
72 $params = array(
73 'realuserid' => $data->participant,
74 'tempuserid' => $tempuser->studentid,
75 'attendanceid' => $att->id,
76 );
77 $logs = $DB->get_recordset_sql($sql, $params);
78
79 foreach ($logs as $log) {
80 if (!is_null($log->reallogid)) {
81 // Remove the existing attendance for the real user for this session.
82 $DB->delete_records('attendance_log', array('id' => $log->reallogid));
83 }
84 // Adjust the 'temp user' attendance record to point at the real user.
85 $DB->set_field('attendance_log', 'studentid', $data->participant, array('id' => $log->templogid));
86 }
87
88 // Delete the temp user.
89 $DB->delete_records('attendance_tempusers', array('id' => $tempuser->id));
90 $att->update_users_grade(array($data->participant)); // Update the gradebook after the merge.
91
92 redirect($att->url_managetemp());
93 }
94
95 /** @var mod_attendance_renderer $output */
96 $output = $PAGE->get_renderer('mod_attendance');
97 $tabs = new attendance_tabs($att, attendance_tabs::TAB_TEMPORARYUSERS);
98
99 echo $output->header();
100 echo $output->heading(get_string('tempusermerge', 'attendance').' : '.format_string($course->fullname));
101 echo $output->render($tabs);
102 $mform->display();
103 echo $output->footer($course);