Skip to content
Snippets Groups Projects
Commit 0762696d authored by PavelBegunkov's avatar PavelBegunkov
Browse files

DB namesake checking + php stubs (see #61)

parent 77f657cc
No related merge requests found
......@@ -447,6 +447,39 @@ END //
# -------------------------------------------------------------------------------------------
DROP FUNCTION IF EXISTS CheckNamesake//
CREATE FUNCTION `CheckNamesake` (
`pLastName` VARCHAR(30) CHARSET utf8,
`pFirstName` VARCHAR(30) CHARSET utf8,
`pSecondName` VARCHAR(30) CHARSET utf8,
`pType` enum ('student', 'teacher')
) RETURNS BOOLEAN # TRUE - exist, FALSE - doesn't
NO SQL
BEGIN
-- default - pessimistic case
declare vRes boolean default false;
if pType = 'student' then
set vRes = exists(
select *
from `students`
where students.LastName = pLastName
AND students.FirstName = pFirstName
AND students.SecondName = pSecondName
limit 1
);
else
set vRes = exists(
select *
from `teachers`
where teachers.LastName = pLastName
AND teachers.FirstName = pFirstName
AND teachers.SecondName = pSecondName
limit 1
);
end if;
RETURN vRes;
END //
DROP FUNCTION IF EXISTS CheckAccountExistence//
......@@ -614,13 +647,19 @@ CREATE FUNCTION `Teacher_Create` (
`pSecondName` VARCHAR(30) CHARSET utf8,
`pJobPositionID` INT,
`pDepartmentID` INT,
`pActivationCode` VARCHAR(40) CHARSET utf8
`pActivationCode` VARCHAR(40) CHARSET utf8,
`pCheckNameSake` bool
) RETURNS int(11) # 0 - success, <0 failed
NO SQL
BEGIN
DECLARE vAccountID INT DEFAULT -1;
DECLARE EXIT HANDLER FOR SQLEXCEPTION RETURN -1;
if pCheckNameSake && CheckNamesake(pLastName, pFirstName, pSecondName, 'teacher')
then
return -2;
end if;
# TODO: kill magic constants (user role 2 - common teacher)
# create account
INSERT INTO `accounts`
......@@ -680,13 +719,19 @@ CREATE FUNCTION `CreateStudent` (
`pGroupNum` INT,
`pFacultyID` INT,
`pActivationCode` VARCHAR(40) CHARSET utf8,
`pSemesterID` INT
`pSemesterID` INT,
`pCheckNameSake` bool
) RETURNS int(11)
NO SQL
BEGIN
DECLARE vAccountID, vGroupID, vStudentID, vSemesterID INT DEFAULT -1;
DECLARE EXIT HANDLER FOR SQLEXCEPTION RETURN -1;
if pCheckNameSake && CheckNamesake(pLastName, pFirstName, pSecondName, 'student')
then
return -2;
end if;
SET vGroupID = GetGroup(pGradeID, pGroupNum, pFacultyID);
IF vGroupID <= 0 THEN
RETURN -1;
......@@ -2338,4 +2383,4 @@ BEGIN
RETURN LAST_INSERT_ID();
END //
DELIMITER ;
\ No newline at end of file
DELIMITER ;
......@@ -28,12 +28,12 @@ class Model_Account extends Model
return UTF8::strtoupper($activationCode);
}
public static function createStudentEx($lastName, $firstName, $secondName, $gradeNum, $groupNum, $degree, $specialization, $facultyID, $semesterID = null) {
public static function createStudentEx($lastName, $firstName, $secondName, $gradeNum, $groupNum, $degree, $specialization, $facultyID, $semesterID = null, $namesake = true) {
$code = self::generateActivationCode();
if (!$semesterID) $semesterID = Model_System::$SemesterID;
$sql = "SELECT `CreateStudentEx`(:last, :first, :second, :grade, :group, :degree, :spec, :faculty, :code, :semester) AS `UserID`;";
$sql = "SELECT `CreateStudentEx`(:last, :first, :second, :grade, :group, :degree, :spec, :faculty, :code, :semester, :namesake) AS `UserID`;";
$response = DB::query(Database::SELECT, $sql)
->parameters([
':last' => trim($lastName),
......@@ -45,7 +45,8 @@ class Model_Account extends Model
':spec' => $specialization,
':faculty' => $facultyID,
':code' => $code,
':semester' => $semesterID
':semester' => $semesterID,
':namesake' => $namesake
])->execute()->get('UserID');
return $response == -1 ? -1 : $code;
......
......@@ -40,11 +40,12 @@ class Model_Student extends Model_Container
return new Model_Helper_StudentBuilder();
}
protected function create() {
$sql = 'SELECT `CreateStudent`(LastName, FirstName, SecondName, GradeID, GroupNum, FacultyID, ActivationCode, SemesterID) AS `ID`';
protected function create($checkNameSake = true) {
$sql = 'SELECT `CreateStudent`(LastName, FirstName, SecondName, GradeID, GroupNum, FacultyID, ActivationCode, SemesterID, namesake) AS `ID`';
$this->data[self::$ID_FIELD] = DB::query(Database::SELECT, $sql)
->parameters($this->data)
->param('namesake', $checkNameSake)
->execute()->get('ID');
if ($this->ID <= 0)
......
......@@ -45,11 +45,12 @@ class Model_Teacher extends Model_Container
return self::load($id);
}
protected function create() {
$sql = 'SELECT `Teacher_Create`(LastName, FirstName, SecondName, JobPositionID, DepID, ActivationCode) AS `ID`';
protected function create($checkNameSake = true) {
$sql = 'SELECT `Teacher_Create`(LastName, FirstName, SecondName, JobPositionID, DepID, ActivationCode, namesake) AS `ID`';
$this->data[self::$ID_FIELD] = DB::query(Database::SELECT, $sql)
->parameters($this->data)
->param('namesake', $checkNameSake)
->execute()->get('ID');
if ($this->ID <= 0)
......
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