Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Tasks:
- Id Student Teacher Mark Completed Content Title Date
- Users
- Id Access E-mail Password Teacher Name
- */
- class CUser
- {
- private $db;
- public $Error;
- public function __construct( $_db )
- {
- $this->db = $_db;
- }
- public function Login( $login, $password )
- {
- $query = $this->db->prepare("SELECT `Id`, `Access`, `E-mail` FROM `Users` WHERE `E-mail` = :login AND `Password` = :pass LIMIT 1");
- $query->bindParam(':login', $login, PDO::PARAM_STR);
- $query->bindParam(':pass', $password, PDO::PARAM_STR);
- $query->execute();
- $result = $query->fetch(PDO::FETCH_ASSOC);
- if( !empty($result) )
- {
- foreach ($result as $key => $value) {
- $_SESSION[$key] = $value;
- }
- return 1;
- }
- else
- {
- $Error = "Неверный логин/пароль.";
- return 2;
- }
- }
- public function RegisterStudent( $params, $TeacherId )
- {
- $UID = $this->GetMaxUserId();
- $Access = 0;
- if( array_key_exists('Password', $params) && $params["Password"] == "" )
- $params["Password"] = chr( rand( 97,122 ) ) . chr( rand( 97,122 ) ) . chr( rand( 97,122 ) ) . chr( rand( 97,122 ) );
- $query = $this->db->prepare( "INSERT INTO `Users` (`Id`, `Name`, `E-mail`, `Password`, `Access`, `Teacher`) VALUES (:Id, :Name, :Email, :Password, :Access, :TeacherId);" );
- $query->bindParam( ':Id', $UID, PDO::PARAM_INT );
- $query->bindParam( ':Name', $params["Name"], PDO::PARAM_STR );
- $query->bindParam( ':Email', $params["E-mail"], PDO::PARAM_STR );
- $query->bindParam( ':Password', $params["Password"], PDO::PARAM_STR );
- $query->bindParam( ':Access', $Access, PDO::PARAM_INT );
- $query->bindParam( ':TeacherId', $TeacherId, PDO::PARAM_INT );
- return $query->execute();
- }
- public function OldRegisterStudent( $params, $Id )
- {
- $params["Teacher"] = $Id;
- $params["Id"] = $this->GetMaxUserId();
- $params["Access"] = 0;
- if( $params["Password"] == "" )
- $params["Password"] = chr( rand( 97,122 ) ) . chr( rand( 97,122 ) ) . chr( rand( 97,122 ) ) . chr( rand( 97,122 ) );
- $tQuery = $this->db->prepare("DESCRIBE `Users`;");
- $tQuery->execute();
- $tResult = $tQuery->fetchAll(PDO::FETCH_ASSOC);
- $arTypes = array();
- foreach ($tResult as $key => $value) {
- if($value['Null'] == 'NO')
- {
- $type = preg_replace('/^(\w+)(\(.*\))?$/', '$1', $value['Type']);
- /* if(strpos($value['Type'], 'varchar') !== false) $arTypes[$value['Field']] = PDO::PARAM_STR;
- elseif(strpos($value['Type'], 'int') !== false) $arTypes[$value['Field']] = PDO::PARAM_INT; */
- switch( $type )
- {
- case 'int':
- case 'decimal':
- $arTypes[ $value['Field'] ] = PDO::PARAM_INT;
- break;
- default:
- $arTypes[ $value['Field'] ] = PDO::PARAM_STR;
- break;
- }
- }
- }
- $tQuery->closeCursor();
- $arFields = "";
- $arFieldsBool = false;
- $arVals = "";
- foreach ($arTypes as $key => $value) {
- if( !array_key_exists( $key, $params ) )
- {
- $this->Error = "Missing key: " . $key;
- }
- else
- {
- if($arFieldsBool === false)
- {
- $arFields = "`" . $key . "`";
- $arVals = ":" . $key;
- $arFieldsBool = true;
- }
- else
- {
- $arFields .= ", `" . $key . "`";
- $arVals .= ", :" . $key;
- }
- }
- }
- $dquery = $this->db->prepare("INSERT INTO `Users` (" . $arFields . ") VALUES (" . $arVals . ");");
- foreach($arTypes as $key => $value)
- {
- $dquery->bindValue(':' . $key, $params[ $key ], $value);
- }
- $dquery->execute();
- return 0;
- }
- public function GetTask( $Id )
- {
- return $this->GetCommonById( '`Id`', '`Tasks`', '`Id`', $Id );
- }
- public function GetTasksForStudent( $Id )
- {
- return $this->GetCommonById( '`Id`', '`Tasks`', '`Student`', $Id );
- }
- public function GetTasksForStudentA( $Id )
- {
- return $this->GetCommonById( '*', '`Tasks`', '`Student`', $Id );
- }
- public function GetTasksForTeacher( $Id )
- {
- return $this->GetCommonById( '`Id`', '`Tasks`', '`Teacher`', $Id );
- }
- public function GetStudentsForTeacher( $Id )
- {
- return $this->GetCommonById( '`Id`, `Name`, `E-mail`', '`Users`', '`Teacher`', $Id );
- }
- public function GetMaxUserId()
- {
- $query = $this->db->prepare("SELECT MAX(Id) FROM `Users`;");
- $query->execute();
- $res = $query->fetch(PDO::FETCH_ASSOC);
- $id = $res['MAX(Id)'];
- $query->closeCursor();
- ++$id;
- return $id;
- }
- public function GetTasksAll()
- {
- $query = $this->db->prepare("SELECT * FROM `Tasks`;");
- $query->execute();
- return $query->fetchAll();
- }
- public function GetCommonById( $SelectKeys, $TableName, $IdName, $Id )
- {
- $query = $this->db->prepare("SELECT " . $SelectKeys . " FROM " . $TableName . " WHERE " . $IdName . " = :id");
- $query->bindParam(':id', $Id, PDO::PARAM_INT);
- $query->execute();
- return $query->fetchAll();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement