<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function send_account_suspend_email_to_user() {
    global $USER, $CFG, $DB;
    $admin = $DB->get_record('user', array('id' => 2));
    $to = "$USER->email";
    $sitename = $DB->get_field('course', 'fullname', array('id' => 1));
    $subject = "$49 DUI School: your account has been blocked";
    $body = "Oh dear...your account was blocked because you did not answer your security questions correctly. Please confirm the following information, and we will get you back on track!";
			$txt .= "\n\n";
		    $txt .= " - Your name\n";
		    $txt .= " - Your date of birth\n";
		    $txt .= " - Your favorite color\n";
		    $txt .= " - Your middle initial\n";	
		    $txt .= " - Your eye color\n";
		    $txt .= " - Your natural hair color\n";
		    $txt .= "";
		    $txt .= "We will respond to your email to let you know your account is reopened.";
    $return = email_to_user($admin,$to, $subject, $body);
    return $return;
}

function send_account_unsuspend_email_to_user($touser) {
    global $USER, $CFG, $DB;
    $admin = $DB->get_record('user', array('id' => 2));
    $to = "$USER->email";
    $sitename = $DB->get_field('course', 'fullname', array('id' => 1));
    $subject = "$sitename: your account has been unlocked";
    $body = "Your account has been unlocked on $sitename. Go to www.NV-DUI.com and log in as usual.";
    $return = email_to_user($admin, $touser, $subject, $body);
    return $return;
}

function return_to_course($thecourseurl) {
	global $thecourse;
	//if the previous URL contains the word "course" we want to redirect there
			$thepreviousurl = $_POST['previousurl'];
			$currentreferer = $_SERVER['HTTP_REFERER'];
			if (!empty($thepreviousurl)) {
			    $previouspageurlprefix = substr($thepreviousurl,0,53);
			    $courseurlprefix = $CFG->wwwroot . '/course/view.php';
			    if ($previouspageurlprefix==$courseurlprefix) {
				    $thecourse = $thepreviousurl;
					$return = "We came from a course with url " . $thecourse;
					//$return = redirect($thecourse);
				} else {
				    $return = redirect($url);	
				}
			}
	return $return;
}