/**
  * Return array of rows where each row is an array of tab objects
  * taking into account permissions of current user
  */
 public function get_tabs()
 {
     $toprow = array();
     $context = $this->att->context;
     $capabilities = array('mod/attendance:manageattendances', 'mod/attendance:takeattendances', 'mod/attendance:changeattendances');
     if (has_any_capability($capabilities, $context)) {
         $toprow[] = new tabobject(self::TAB_SESSIONS, $this->att->url_manage()->out(), get_string('sessions', 'attendance'));
     }
     if (has_capability('mod/attendance:manageattendances', $context)) {
         $toprow[] = new tabobject(self::TAB_ADD, $this->att->url_sessions()->out(true, array('action' => att_sessions_page_params::ACTION_ADD)), get_string('add', 'attendance'));
     }
     if (has_capability('mod/attendance:viewreports', $context)) {
         $toprow[] = new tabobject(self::TAB_REPORT, $this->att->url_report()->out(), get_string('report', 'attendance'));
     }
     if (has_capability('mod/attendance:export', $context)) {
         $toprow[] = new tabobject(self::TAB_EXPORT, $this->att->url_export()->out(), get_string('export', 'attendance'));
     }
     if (has_capability('mod/attendance:changepreferences', $context)) {
         $toprow[] = new tabobject(self::TAB_PREFERENCES, $this->att->url_preferences()->out(), get_string('settings', 'attendance'));
     }
     if (has_capability('mod/attendance:managetemporaryusers', $context)) {
         $toprow[] = new tabobject(self::TAB_TEMPORARYUSERS, $this->att->url_managetemp()->out(), get_string('tempusers', 'attendance'));
     }
     return array($toprow);
 }
 /**
  * Return array of rows where each row is an array of tab objects
  * taking into account permissions of current user
  */
 public function get_tabs()
 {
     $toprow = array();
     if ($this->att->perm->can_manage() or $this->att->perm->can_take() or $this->att->perm->can_change()) {
         $toprow[] = new tabobject(self::TAB_SESSIONS, $this->att->url_manage()->out(), get_string('sessions', 'attendance'));
     }
     if ($this->att->perm->can_manage()) {
         $toprow[] = new tabobject(self::TAB_ADD, $this->att->url_sessions()->out(true, array('action' => att_sessions_page_params::ACTION_ADD)), get_string('add', 'attendance'));
     }
     if ($this->att->perm->can_view_reports()) {
         $toprow[] = new tabobject(self::TAB_REPORT, $this->att->url_report()->out(), get_string('report', 'attendance'));
     }
     if ($this->att->perm->can_export()) {
         $toprow[] = new tabobject(self::TAB_EXPORT, $this->att->url_export()->out(), get_string('export', 'attendance'));
     }
     if ($this->att->perm->can_change_preferences()) {
         $toprow[] = new tabobject(self::TAB_PREFERENCES, $this->att->url_preferences()->out(), get_string('settings', 'attendance'));
     }
     if ($this->att->perm->can_managetemp()) {
         $toprow[] = new tabobject(self::TAB_TEMPORARYUSERS, $this->att->url_managetemp()->out(), get_string('tempusers', 'attendance'));
     }
     return array($toprow);
 }
require_login($course, true, $cm);
$att->perm->require_managetemp_capability();
$PAGE->set_title($course->shortname . ": " . $att->name . ' - ' . get_string('tempusersedit', 'attendance'));
$PAGE->set_heading($course->fullname);
$PAGE->set_cacheable(true);
$PAGE->navbar->add(get_string('tempusersedit', 'attendance'));
/** @var mod_attendance_renderer $output */
$output = $PAGE->get_renderer('mod_attendance');
if ($action == 'delete') {
    if (optional_param('confirm', false, PARAM_BOOL)) {
        require_sesskey();
        // Remove the user from the grades table, the attendance log and the tempusers table.
        $DB->delete_records('grade_grades', array('userid' => $tempuser->studentid));
        $DB->delete_records('attendance_log', array('studentid' => $tempuser->studentid));
        $DB->delete_records('attendance_tempusers', array('id' => $tempuser->id));
        redirect($att->url_managetemp());
    } else {
        $info = (object) array('fullname' => $tempuser->fullname, 'email' => $tempuser->email);
        $msg = get_string('confirmdeleteuser', 'attendance', $info);
        $continue = new moodle_url($PAGE->url, array('confirm' => 1, 'sesskey' => sesskey()));
        echo $output->header();
        echo $output->confirm($msg, $continue, $att->url_managetemp());
        echo $output->footer();
        die;
    }
}
$formdata = new stdClass();
$formdata->id = $cm->id;
$formdata->tname = $tempuser->fullname;
$formdata->userid = $tempuser->id;
$formdata->temail = $tempuser->email;
$tempuser = $DB->get_record('attendance_tempusers', array('id' => $userid), '*', MUST_EXIST);
$att = new attendance($att, $cm, $course);
$params = array('userid' => $tempuser->id);
$PAGE->set_url($att->url_tempmerge($params));
require_login($course, true, $cm);
$PAGE->set_title($course->shortname . ": " . $att->name . ' - ' . get_string('tempusermerge', 'attendance'));
$PAGE->set_heading($course->fullname);
$PAGE->set_cacheable(true);
$PAGE->set_button($OUTPUT->update_module_button($cm->id, 'attendance'));
$PAGE->navbar->add(get_string('tempusermerge', 'attendance'));
$formdata = (object) array('id' => $cm->id, 'userid' => $tempuser->id);
$custom = array('description' => format_string($tempuser->fullname) . ' (' . format_string($tempuser->email) . ')');
$mform = new tempmerge_form(null, $custom);
$mform->set_data($formdata);
if ($mform->is_cancelled()) {
    redirect($att->url_managetemp());
} else {
    if ($data = $mform->get_data()) {
        $sql = "SELECT s.id, lr.id AS reallogid, lt.id AS templogid\n              FROM {attendance_sessions} s\n              LEFT JOIN {attendance_log} lr ON lr.sessionid = s.id AND lr.studentid = :realuserid\n              LEFT JOIN {attendance_log} lt ON lt.sessionid = s.id AND lt.studentid = :tempuserid\n             WHERE s.attendanceid = :attendanceid AND lt.id IS NOT NULL\n             ORDER BY s.id";
        $params = array('realuserid' => $data->participant, 'tempuserid' => $tempuser->studentid, 'attendanceid' => $att->id);
        $logs = $DB->get_recordset_sql($sql, $params);
        foreach ($logs as $log) {
            if (!is_null($log->reallogid)) {
                // Remove the existing attendance for the real user for this session.
                $DB->delete_records('attendance_log', array('id' => $log->reallogid));
            }
            // Adjust the 'temp user' attendance record to point at the real user.
            $DB->set_field('attendance_log', 'studentid', $data->participant, array('id' => $log->templogid));
        }
        // Delete the temp user.
        $DB->delete_records('attendance_tempusers', array('id' => $tempuser->id));
 * Temporary user management.
 *
 * @package    mod_attendance
 * @copyright  2013 Davo Smith, Synergy Learning
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require_once dirname(__FILE__) . '/../../config.php';
global $CFG, $DB, $OUTPUT, $PAGE, $COURSE;
require_once $CFG->dirroot . '/mod/attendance/locallib.php';
require_once $CFG->dirroot . '/mod/attendance/temp_form.php';
$id = required_param('id', PARAM_INT);
$cm = get_coursemodule_from_id('attendance', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$att = $DB->get_record('attendance', array('id' => $cm->instance), '*', MUST_EXIST);
$att = new attendance($att, $cm, $course);
$PAGE->set_url($att->url_managetemp());
require_login($course, true, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/attendance:managetemporaryusers', $context);
$PAGE->set_title($course->shortname . ": " . $att->name . ' - ' . get_string('tempusers', 'attendance'));
$PAGE->set_heading($course->fullname);
$PAGE->set_cacheable(true);
$PAGE->navbar->add(get_string('tempusers', 'attendance'));
$output = $PAGE->get_renderer('mod_attendance');
$tabs = new attendance_tabs($att, attendance_tabs::TAB_TEMPORARYUSERS);
$formdata = (object) array('id' => $cm->id);
$mform = new temp_form();
$mform->set_data($formdata);
if ($data = $mform->get_data()) {
    // Create temp user in main user table.
    $user = new stdClass();