The administration panel and web client for ParEdu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.6 KiB

<?php
class Connection {
private $charset= 'utf8mb4';
protected function connect (){
include "../../creds.php";
$dsn= "mysql:host=$sqlserver;charset=$this->charset";
try {
$pdo= new pdo ($dsn,$sqluser,$sqlpass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false );
$pdo->setattribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$pdo->exec('USE '.$dbname.'_chat');
return $pdo;
}
catch (Exception $e) {
echo 'Error: '.$e->getMessage();
}
}
}
class Model extends Connection {
public function getUsers($id){
include "../../creds.php";
//$sql="SELECT * FROM users WHERE user_id != ?";
$pdo = $this->connect();
$pdo->exec('USE '.$dbname);
$sql = "SELECT * FROM `auth` WHERE `id` != ?";
$stmt=$pdo->prepare($sql);
$stmt->execute([$id]);
//$result=$stmt->fetchAll();
$result = array();
$fetch = $stmt->fetchAll();
for($i = 0; $i<count($fetch); $i++){
$sndarray = array();
$sndarray["id"] = $fetch[$i]["id"];
if($fetch[$i]["fullname"] != ""){
$sndarray["name"] = $fetch[$i]["fullname"];
}else{
$sndarray["name"] = $fetch[$i]["username"];
}
$sndarray["userimg"] = $siteurl."/API/request.php?type=image&subtype=user&id=".$fetch[$i]["id"];
$pdo->exec('USE '.$dbname."_chat");
$sql = "SELECT * FROM notifications WHERE userid = ? AND fromuserid = ?; ";
$stmtcheck=$pdo->prepare($sql);
$stmtcheck->execute([$id, $fetch[$i]["id"]]);
$resultcheck = $stmtcheck->fetchAll();
if($stmtcheck && $stmtcheck->rowCount() > 0){
$sndarray["unread"] = $resultcheck[0]["unread"];
}else{
$sndarray["unread"] = 0;
}
$result[$i] = $sndarray;
}
return $result;
}
public function insertChat($id,$user2_id, $message, $time){
$conn = $this->connect();
$sql = "SELECT * FROM notifications WHERE userid = ? AND fromuserid = ?; ";
$stmtcheck=$conn->prepare($sql);
$stmtcheck->execute([$user2_id, $id]);
$result = $stmtcheck->fetchAll();
if($stmtcheck && $stmtcheck->rowCount() > 0){
$unread = $result[0]["unread"];
}
if(isset($unread)){
$sql = "UPDATE notifications SET unread = ? WHERE userid = ? AND fromuserid = ?;";
$stmtinsert=$conn->prepare($sql);
$stmtinsert->execute([($unread+1), $user2_id, $id]);
}else{
$sql="INSERT INTO notifications (userid, fromuserid, unread) VALUES (?, ?, 1);";
$stmtinsert=$conn->prepare($sql);
$stmtinsert->execute([$user2_id, $id]);
}
$sql="INSERT INTO chats (chat_user_id, chat_user2_id, chat_message, chat_sent_by, chat_date) VALUES(?,?,?,?,?)";
$stmt=$conn->prepare($sql);
$stmt->execute([$id,$user2_id, $message, $id, $time]);
$result= $stmt->fetchAll();
return $result;
}
public function thisUser($user_name, $pass){
$sql="SELECT * From users WHERE user_name =? AND users_pass= ?";
$stmt= $this->connect()->prepare($sql);
$stmt->execute([$user_name, $pass]);
$result=$stmt->fetchAll();
return $result;
}
public function getChats($id1, $id2){
$conn = $this->connect();
$sql = "SELECT * FROM notifications WHERE userid = ? AND fromuserid = ?";
$stmtcheck=$conn->prepare($sql);
$stmtcheck->execute([$id1, $id2]);
$result = $stmtcheck->fetchAll();
if($stmtcheck->rowCount() > 0){
$sql = "DELETE FROM notifications WHERE userid = ? AND fromuserid = ?";
$stmtinsert=$conn->prepare($sql);
$stmtinsert->execute([$id1, $id2]);
}
$sql="SELECT * FROM chats WHERE chat_user_id = ? and chat_user2_id=? OR chat_user_id=? and chat_user2_id=?";
$stmt=$conn->prepare($sql);
$stmt->execute([$id1, $id2, $id2, $id1]);
$result=$stmt->fetchAll();
return $result;
}
}
?>