<?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 = "$sitename: 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 .= "";
		    $txt .= " - Your name";
		    $txt .= " - Your date of birth";
		    $txt .= " - Your favorite color";
		    $txt .= " - Your natural hair color";
		    $txt .= " - Your month of birth";
		    $txt .= " - Your middle initial";
		    $txt .= " - Your eye color";
		    $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.79DollarOhioDriver.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;
}