Commit a3a58d03 authored by S.Holzhauer's avatar S.Holzhauer

Database bericht & notificatie creatie

parent 5cca34e4
<!-- The script that generates emails/messages -->
<?php
require_once('dbconnection.php');
//Get the ID of the camera (?ID in the URL)
$ID = $_REQUEST['ID'];
//get the time & date
date_default_timezone_set("Europe/Amsterdam");
$time = date("H:i:sa");
$date = date("Y-m-d");
$day = date("w");
$movement = 0;
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Lookup what Elder is connected to the camera.
$sql = "SELECT AlleenstaandeOuder_OuderID FROM Camera WHERE CameraID='$ID'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$row = $result->fetch_assoc();
$OuderID=$row["AlleenstaandeOuder_OuderID"];
} else {
echo "0 results";
}
$sql = "SELECT Movement FROM Leefpatroon WHERE StartTime<='$time' AND EndTime>='$time' AND OuderID=$OuderID AND Day=$day;";
$result = $conn->query($sql);
error_log($result);
if($result->num_rows > 0){
$row = $result->fetch_assoc();
$movement = $row['Movement'];
error_log($movement);
}
if($movement == 0){
// Check connection
//Using the find out how the Elder is Called
$sql0 = "SELECT Voorletters,Achternaam FROM AlleenstaandeOuder Where OuderID=$OuderID";
$result0 = $conn->query($sql0);
if ($result0->num_rows > 0) {
// output data of each row
$row = $result0->fetch_assoc();
$name=$row["Voorletters"] . " " . $row["Achternaam"];
} else {
echo "0 results";
}
//set mail features.
$headers = 'From: <irp@stienos.nl>';
//Building the contents of the email. Using previously initialized variables & static strings. \n is an enter.
$Msg = "De camera bij $name heeft een beweging waar genomen op $date om $time.\n Kijk snel op stienos.nl/watchdad/inbox.php";
//Insert message into DB
$sql3 = "INSERT INTO Berichten (Time,Message,Subject,Date) VALUES ('$time','$Msg','Camera Alert','$date')";
$result3 = $conn->query($sql3);
//$sql3 query creates an auto ID, this ID is called here and bound to $last_id
if ($result3 === TRUE) {
$last_id = $conn->insert_id;
} else {
echo "An error occurd when connecting with the Database.";
//Next line was comented out because the $sql gives away db information.
//echo "Error: " . $sql . "<br>" . $conn->error;
}
//Find out what account is connected to the elder
$sql1 = "SELECT Account_AccountID FROM Account_has_AlleenstaandeOuder WHERE AlleenstaandeOuder_OuderID='$OuderID'";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
// output data of each row
//Multiple accounts can be connected to 1 elder
while($result1 && ($row = $result1->fetch_assoc()))
{
$AccountID=$row["Account_AccountID"];
//Get the email adres connected to the account
$sql2 = "SELECT Emailadres FROM Account WHERE AccountID='$AccountID'";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
// output data of each row
$row = $result2->fetch_assoc();
//Binding the return value to $Email
$Email=$row["Emailadres"];
} else {
echo "0 results";
}
//Get the Name connected to the account
$sql5 = "SELECT Voorletters,Achternaam FROM Account WHERE AccountID='$AccountID'";
$result5 = $conn->query($sql5);
if ($result5->num_rows > 0) {
// output data of each row
$row = $result5->fetch_assoc();
//Binding the return to $AcName
$AcName=$row["Voorletters"] . " " . $row["Achternaam"];
} else {
echo "0 results";
}
//sending the mail, $Email is initialized previously, $Msg is created above & $headers changes the sender information to irp@stienos.nl.
mail($Email,"Camera Alert","Beste $AcName $Msg",$headers);
//Connect the message to the account with status 0 "Not Read"
$sql4 = "INSERT INTO Account_has_Berichten (Berichten_BerichtID,Account_AccountID,Status) VALUES ($last_id,$AccountID,0)";
$result4 = $conn->query($sql4);
}
} else {
echo "0 results";
}
}
else{
}
$conn->close();
/*
* test mail. Commented out
* Only for testing of the file.
* mail("stijnholzhauer+hvapad@gmail.com", "cameratest", "2e mail, komt de 1e binnen?");
*/
?>
\ No newline at end of file
......@@ -85,7 +85,7 @@ else if ($ID == "IRPS001"){
$conn->query($sql);
//select the details from accounts connected to and details from the elder.
$sql = "SELECT A.Voorletters,A.Achternaam,A.Emailadres,C.Voorletters,C.Achternaam
$sql = "SELECT A.AccountID,A.Voorletters,A.Achternaam,A.Emailadres,C.Voorletters,C.Achternaam
FROM AlleenstaandeOuder C
INNER JOIN Account_has_AlleenstaandeOuder B
ON B.AlleenstaandeOuder_OuderID = C.OuderID
......@@ -93,19 +93,50 @@ else if ($ID == "IRPS001"){
ON A.AccountID = B.Account_AccountID
WHERE C.OuderID = '$OuderID';";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$AccountID = $row['A.AccountID'];
$Acname = $row['A.Voorletters'] . " " . $row['A.Achternaam'];
$Email= $row['A.Emailadres'];
$Oudername = $row['C.Voorletters'] . " " . $row['C.Achternaam'];
//set mail features.
$headers = 'From: <WatchDad@irp.nl>';
$Msg = "De camera bij $Oudername heeft een beweging waar genomen terwijl dit niet zou moeten volgens het leefpatroon.\n
Dit gebeurde op $date om $time.\n
Kijk snel op stienos.nl/watchdad/";
//Create a notification
$sql = "INSERT INTO Notificatie (`Date`,`Time`,`Title`)
VALUES ('$date','$time','Ongeplande Beweging');";
$result = $conn->query($sql);
if ($result === TRUE) {
$notificatieID = $conn->insert_id;
}
//add thee messaga to berichten in the database
$sql = "INSERT INTO Berichten (`Time`,`Message`,`Subject`,`Date`)
VALUES ('$time','$Msg','Unplanned activity','$date');";
$result = $conn->query($sql);
//get the id of inserted message
if ($result === TRUE) {
$berichtID = $conn->insert_id;
}
//loop trough every account so they all get an email
while ($result && ($row = $result->fetch_assoc())){
$Acname = $row['A.Voorletters'] . " " . $row['A.Achternaam'];
$Email= $row['A.Emailadres'];
$Oudername = $row['C.Voorletters'] . " " . $row['C.Achternaam'];
//set mail features.
$headers = 'From: <WatchDad@irp.nl>';
$Msg = "De camera bij $Oudername heeft een beweging waar genomen terwijl dit niet zou moeten volgens het leefpatroon.\n
Dit gebeurde op $date om $time.\n
Kijk snel op stienos.nl/watchdad/";
//send the mail
mail($Email,"Camera Alert","Beste $AcName $Msg",$headers);
//couple the message to appropriate accounts
$sql = "INSERT INTO Account_has_Berichten (`Berichten_BerichtID`,`Account_AccountID`,`Status`)
VALUES ('$berichtID','$AccountID',0);";
$conn->query($sql);
//couple notification to appropriate accounts
$sql = "INSERT INTO Account_has_Notificatie (`Notificatie_NotificatieID`,`Account_AccountID`)
VALUES ('$notificatieID','$AccountID');";
}
}
else if (($Movement == 0) && ($Detected == 0)){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment