$headingStr = $register->name . ($userId ? ': ' . $userToProcessFullname : '');
echo $OUTPUT->heading(format_string($headingStr));
// ==============================================
// Pepare Offline Session insert form, if needed
// ==============================================
// If a userID is defined, offline sessions are enabled and the user may insert Self.certificatins...
// ...prepare the Form for Self.Cert.
// Process the form (if submitted)
// Note that the User is always the CURRENT User (no UserId param is passed by the form)
$doShowContents = true;
$mform = null;
if ($userId && $doShowOfflineSessionForm && !$doShowPrintableVersion) {
    // Prepare Form
    $customFormData = array('register' => $register, 'courses' => $userSessions->trackedCourses->courses);
    // Also pass userId only if is saving for another user
    if (!attendanceregister__isCurrentUser($userId)) {
        $customFormData['userId'] = $userId;
    }
    $mform = new mod_attendanceregister_selfcertification_edit_form(null, $customFormData);
    // Process Self.Cert Form submission
    if ($mform->is_cancelled()) {
        // Cancel
        redirect($PAGE->url);
    } else {
        if ($doSaveOfflineSession && ($formData = $mform->get_data())) {
            // Save Session
            attendanceregister_save_offline_session($register, $formData);
            // Notification & Continue button
            echo $OUTPUT->notification(get_string('offline_session_saved', 'attendanceregister'), 'notifysuccess');
            echo $OUTPUT->continue_button(attendanceregister_makeUrl($register, $userId));
            $doShowContents = false;
 /**
  * Check if the current USER can add Offline Sessions for a specified User
  * @param int $userId (null means current user's register)
  * @return boolean
  */
 public function canAddThisUserOfflineSession($register, $userId)
 {
     global $DB;
     if (attendanceregister__isCurrentUser($userId)) {
         return $this->canAddOwnOfflineSessions;
     } else {
         if ($this->canAddOtherOfflineSessions) {
             // If adding Session for another user also check it is tracked by the register instance
             $user = attendanceregister__getUser($userId);
             return attendanceregister_is_tracked_user($register, $user);
         }
     }
     return false;
 }
/**
 * Save a new offline session
 * Data should have been validated before saving
 *
 * Updates Aggregates after saving
 *
 * @param object $register
 * @param array $formData
 */
function attendanceregister_save_offline_session($register, $formData)
{
    global $DB, $USER;
    $session = new stdClass();
    $session->register = $register->id;
    // If a userid has not been set in the form (the user is saving in his own Register) use current $USER
    $session->userid = isset($formData->userid) ? $formData->userid : $USER->id;
    $session->onlinesess = 0;
    $session->login = $formData->login;
    $session->logout = $formData->logout;
    $session->duration = $formData->logout - $formData->login;
    $session->refcourse = isset($formData->refcourse) ? $formData->refcourse : null;
    // Hack needed as 0 is passed as refcourse if no refcourse has been selected
    $session->comments = isset($formData->comments) ? $formData->comments : null;
    // If saved for another user, record the current user
    if (!attendanceregister__isCurrentUser($session->userid)) {
        $session->addedbyuserid = $USER->id;
    }
    $DB->insert_record('attendanceregister_session', $session);
    // Update aggregates
    attendanceregister__update_user_aggregates($register, $session->userid);
}