Skip to content
Snippets Groups Projects
StoredProcedures.sql 51.2 KiB
Newer Older
PavelBegunkov's avatar
PavelBegunkov committed
DELIMITER //
PavelBegunkov's avatar
PavelBegunkov committed

PavelBegunkov's avatar
PavelBegunkov committed
DROP PROCEDURE IF EXISTS GetCurSemesterInfo//
DROP PROCEDURE IF EXISTS GetCurSemesterID//
DROP PROCEDURE IF EXISTS GetHashKey//
PavelBegunkov's avatar
PavelBegunkov committed
DROP PROCEDURE IF EXISTS GetStudyGroupsForDisciplineFull//
PavelBegunkov's avatar
PavelBegunkov committed

PavelBegunkov's avatar
PavelBegunkov committed
DROP PROCEDURE IF EXISTS GetStudyGroupsForDiscipline//
DROP PROCEDURE IF EXISTS GetStudyGroups//
PavelBegunkov's avatar
PavelBegunkov committed

DROP PROCEDURE IF EXISTS GetDisciplineInfoByID//
DROP PROCEDURE IF EXISTS GetMapForDisciplineExam//
DROP PROCEDURE IF EXISTS GetMapForDiscipline//
Silence's avatar
Silence committed

PavelBegunkov's avatar
PavelBegunkov committed
DROP PROCEDURE IF EXISTS GetMapForStudent//
DROP PROCEDURE IF EXISTS GetMapForStudentExam//
DROP PROCEDURE IF EXISTS GetRatesForStudentsGroup//
Silence's avatar
Silence committed

PavelBegunkov's avatar
PavelBegunkov committed
DROP PROCEDURE IF EXISTS CreateFaculty //
DROP PROCEDURE IF EXISTS GetReports//
DROP PROCEDURE IF EXISTS GetSettings//
PavelBegunkov's avatar
PavelBegunkov committed

Silence's avatar
Silence committed

PavelBegunkov's avatar
PavelBegunkov committed
# -------------------------------------------------------------------------------------------
# Label: abbreviations
# -------------------------------------------------------------------------------------------
Silence's avatar
Silence committed

PavelBegunkov's avatar
PavelBegunkov committed
# abbreviation: abbr
# specialization: spec
# department: dep
PavelBegunkov's avatar
PavelBegunkov committed

PavelBegunkov's avatar
PavelBegunkov committed
# -------------------------------------------------------------------------------------------
# Label: preferences
# Label: magic
# -------------------------------------------------------------------------------------------
PavelBegunkov's avatar
PavelBegunkov committed

PavelBegunkov's avatar
PavelBegunkov committed
# -------------------------------------------------------------------------------------------
# Label: semesters
# -------------------------------------------------------------------------------------------
PavelBegunkov's avatar
PavelBegunkov committed

DROP PROCEDURE IF EXISTS GetSemesterInfo//
CREATE PROCEDURE `GetSemesterInfo` (
    IN `pSemesterID` INT
) NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
BEGIN
xamgore's avatar
xamgore committed
    SELECT  semesters.ID    AS 'ID',
            semesters.Num   As 'Num',
PavelBegunkov's avatar
PavelBegunkov committed
            semesters.Year  As 'Year'
        FROM `semesters`
        WHERE semesters.ID = pSemesterID
        LIMIT 1;
PavelBegunkov's avatar
PavelBegunkov committed
END //

PavelBegunkov's avatar
PavelBegunkov committed
DROP PROCEDURE IF EXISTS GetSemesters//
CREATE PROCEDURE `GetSemesters` (
) NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
BEGIN
    SELECT  semesters.ID,
            semesters.Year,
            semesters.Num
PavelBegunkov's avatar
PavelBegunkov committed
        FROM `semesters`
PavelBegunkov's avatar
PavelBegunkov committed
        ORDER BY semesters.ID DESC;
END //

PavelBegunkov's avatar
PavelBegunkov committed


PavelBegunkov's avatar
PavelBegunkov committed
# -------------------------------------------------------------------------------------------
# Label: faculties
# -------------------------------------------------------------------------------------------
PavelBegunkov's avatar
PavelBegunkov committed

DROP PROCEDURE IF EXISTS GetFaculties//
CREATE PROCEDURE `GetFaculties` (
) NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
    SELECT  faculties.ID,
            faculties.Name,
            faculties.Abbr
PavelBegunkov's avatar
PavelBegunkov committed
        FROM `faculties`
        ORDER BY faculties.Name ASC;
PavelBegunkov's avatar
PavelBegunkov committed
# -------------------------------------------------------------------------------------------
# Label: departments
# -------------------------------------------------------------------------------------------
PavelBegunkov's avatar
PavelBegunkov committed

DROP PROCEDURE IF EXISTS GetDepartments//
CREATE PROCEDURE `GetDepartments` (
    IN `pFacultyID` INT
) NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
    SELECT  departments.ID,
            departments.Name
PavelBegunkov's avatar
PavelBegunkov committed
        FROM `departments`
        WHERE departments.FacultyID = pFacultyID
        ORDER BY departments.Name ASC;
PavelBegunkov's avatar
PavelBegunkov committed
END //


PavelBegunkov's avatar
PavelBegunkov committed
# -------------------------------------------------------------------------------------------
# Label: specializations
# -------------------------------------------------------------------------------------------
DROP PROCEDURE IF EXISTS GetSpecializations//
CREATE PROCEDURE `GetSpecializations` (
    IN `pFacultyID` INT
) NO SQL
BEGIN
PavelBegunkov's avatar
PavelBegunkov committed
    SELECT  specializations.ID,
            specializations.Name,
            specializations.Abbr
PavelBegunkov's avatar
PavelBegunkov committed
        FROM    `specializations`
        WHERE   specializations.FacultyID = pFacultyID
        ORDER BY subjects.Name ASC;
PavelBegunkov's avatar
PavelBegunkov committed
END //


PavelBegunkov's avatar
PavelBegunkov committed
# -------------------------------------------------------------------------------------------
# Label: job positions
# -------------------------------------------------------------------------------------------
PavelBegunkov's avatar
PavelBegunkov committed

DROP PROCEDURE IF EXISTS GetJobPositions//
CREATE PROCEDURE `GetJobPositions` (
) NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
BEGIN
PavelBegunkov's avatar
PavelBegunkov committed
    SELECT  job_positions.ID,
            job_positions.Name
PavelBegunkov's avatar
PavelBegunkov committed
        FROM    `job_positions`
        ORDER BY job_positions.Name;
PavelBegunkov's avatar
PavelBegunkov committed
# -------------------------------------------------------------------------------------------
# Label: grades
# -------------------------------------------------------------------------------------------
PavelBegunkov's avatar
PavelBegunkov committed

DROP PROCEDURE IF EXISTS GetGrades//
CREATE PROCEDURE `GetGrades` (
) NO SQL
Silence's avatar
Silence committed
BEGIN
    SELECT  grades.ID,
            grades.Num,
            grades.Degree
        FROM `grades`
PavelBegunkov's avatar
PavelBegunkov committed
        ORDER BY grades.ID;
END //
PavelBegunkov's avatar
PavelBegunkov committed


PavelBegunkov's avatar
PavelBegunkov committed
# -------------------------------------------------------------------------------------------
# Label: study groups
# -------------------------------------------------------------------------------------------
PavelBegunkov's avatar
PavelBegunkov committed

PavelBegunkov's avatar
PavelBegunkov committed
DROP PROCEDURE IF EXISTS GetGroups//
CREATE PROCEDURE `GetGroups` (
    IN `pGradeID` INT,
    IN `pFacultyID` INT
) NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
    SELECT  view_groups.GroupID AS 'ID',
            view_groups.GroupNum,
PavelBegunkov's avatar
PavelBegunkov committed
            view_groups.SpecID,
            view_groups.SpecName,
PavelBegunkov's avatar
PavelBegunkov committed
            view_groups.SpecAbbr
        FROM `view_groups`
        WHERE   view_groups.GradeID = pGradeID AND
                view_groups.FacultyID = pFacultyID
        ORDER BY view_groups.GroupNum ASC;
PavelBegunkov's avatar
PavelBegunkov committed
END //
PavelBegunkov's avatar
PavelBegunkov committed


PavelBegunkov's avatar
PavelBegunkov committed
# get all general study groups, that takes this course
PavelBegunkov's avatar
PavelBegunkov committed
DROP PROCEDURE IF EXISTS GetGroupsForDiscipline//
CREATE PROCEDURE `GetGroupsForDiscipline` (
    IN `pDisciplineID` INT
) NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
BEGIN
PavelBegunkov's avatar
PavelBegunkov committed
    SELECT  view_groups.GroupID AS 'ID',
            view_groups.GroupNum,
            view_groups.GradeID,
            view_groups.GradeNum,
            view_groups.Degree,
            view_groups.SpecID,
            view_groups.SpecName,
            view_groups.SpecAbbr
PavelBegunkov's avatar
PavelBegunkov committed
        FROM `disciplines_groups`
        INNER JOIN `view_groups` ON disciplines_groups.GroupID = view_groups.GroupID
PavelBegunkov's avatar
PavelBegunkov committed
        WHERE disciplines_groups.DisciplineID = pDisciplineID
        ORDER BY view_groups.GradeID ASC, view_groups.GroupID ASC;
PavelBegunkov's avatar
PavelBegunkov committed
END //


PavelBegunkov's avatar
PavelBegunkov committed
# -------------------------------------------------------------------------------------------
# Label: subjects
# -------------------------------------------------------------------------------------------
DROP PROCEDURE IF EXISTS GetSubjects//
CREATE PROCEDURE `GetSubjects` (
    IN `pFacultyID` INT
) NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
BEGIN
PavelBegunkov's avatar
PavelBegunkov committed
    SELECT  subjects.ID,
            subjects.Name AS 'Title',
PavelBegunkov's avatar
PavelBegunkov committed
            subjects.Abbr
PavelBegunkov's avatar
PavelBegunkov committed
        FROM `subjects_faculties`
        INNER JOIN `subjects` ON subjects_faculties.SubjectID = subjects.ID
        WHERE subjects_faculties.FacultyID = pFacultyID
PavelBegunkov's avatar
PavelBegunkov committed
        ORDER BY subjects.Name ASC;
PavelBegunkov's avatar
PavelBegunkov committed
END //


PavelBegunkov's avatar
PavelBegunkov committed
# -------------------------------------------------------------------------------------------
# Label: accounts
# -------------------------------------------------------------------------------------------
# TODO: deprecated (see GetFullInfo)
DROP PROCEDURE IF EXISTS GetAccountInfo//
PavelBegunkov's avatar
PavelBegunkov committed
CREATE PROCEDURE `GetAccountInfo` ( IN `pUserID` INT )
PavelBegunkov's avatar
PavelBegunkov committed
BEGIN
    SELECT  accounts.ID,
            accounts.Login,
            accounts.EMail,
PavelBegunkov's avatar
PavelBegunkov committed
            user_roles.Type,
PavelBegunkov's avatar
PavelBegunkov committed
            user_roles.RoleName AS 'Role',
            user_roles.Mark     AS 'RoleMark',
PavelBegunkov's avatar
PavelBegunkov committed
            accounts.IsEnabled,
PavelBegunkov's avatar
PavelBegunkov committed
            accounts.ActivationCode AS 'Code',
PavelBegunkov's avatar
PavelBegunkov committed
            accounts.UserAgent
PavelBegunkov's avatar
PavelBegunkov committed
        FROM `accounts`
        INNER JOIN `user_roles` ON accounts.UserRoleID = user_roles.ID
        WHERE accounts.ID = pUserID
        LIMIT 1;
PavelBegunkov's avatar
PavelBegunkov committed
END //

# TODO: deprecated (see GetFullInfo)
DROP PROCEDURE IF EXISTS GetPersonalInfo//
CREATE PROCEDURE `GetPersonalInfo` (
    IN `pUserID` INT,
    IN `pSemesterID` INT
) NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
BEGIN
PavelBegunkov's avatar
PavelBegunkov committed
    DECLARE vAccountType INT DEFAULT -1;
PavelBegunkov's avatar
PavelBegunkov committed
    SELECT user_roles.Type INTO vAccountType
PavelBegunkov's avatar
PavelBegunkov committed
        FROM `accounts`
PavelBegunkov's avatar
PavelBegunkov committed
        INNER JOIN `user_roles` ON accounts.UserRoleID = user_roles.ID
        WHERE accounts.ID = pUserID
PavelBegunkov's avatar
PavelBegunkov committed
        LIMIT 1;
PavelBegunkov's avatar
PavelBegunkov committed

PavelBegunkov's avatar
PavelBegunkov committed
    # type 1: student
    #      2: teacher
PavelBegunkov's avatar
PavelBegunkov committed
    IF vAccountType = 1 THEN
PavelBegunkov's avatar
PavelBegunkov committed
        SELECT  view_students.LastName,
                view_students.FirstName,
                view_students.SecondName,
PavelBegunkov's avatar
PavelBegunkov committed
                view_students.StudentID,
PavelBegunkov's avatar
PavelBegunkov committed
                view_students.GradeID,
                view_students.GradeNum,
PavelBegunkov's avatar
PavelBegunkov committed
                view_students.GroupID,
                view_students.GroupNum,
PavelBegunkov's avatar
PavelBegunkov committed
                view_students.GroupName,
PavelBegunkov's avatar
PavelBegunkov committed
                view_students.Degree,
                view_students.SpecID,
                view_students.SpecName,
PavelBegunkov's avatar
PavelBegunkov committed
                view_students.SpecAbbr,
PavelBegunkov's avatar
PavelBegunkov committed
                view_students.FacultyID,
                view_students.FacultyName,
                view_students.FacultyAbbr
PavelBegunkov's avatar
PavelBegunkov committed
            FROM `view_students`
PavelBegunkov's avatar
PavelBegunkov committed
            WHERE view_students.AccountID = pUserID AND
                  view_students.SemesterID = pSemesterID
PavelBegunkov's avatar
PavelBegunkov committed
            LIMIT 1;
PavelBegunkov's avatar
PavelBegunkov committed
    ELSE
PavelBegunkov's avatar
PavelBegunkov committed
        SELECT  view_teachers.LastName,
                view_teachers.FirstName,
                view_teachers.SecondName,
PavelBegunkov's avatar
PavelBegunkov committed
                view_teachers.TeacherID,
PavelBegunkov's avatar
PavelBegunkov committed
                view_teachers.DepID,
                view_teachers.DepName,
PavelBegunkov's avatar
PavelBegunkov committed
                view_teachers.JobPositionName,
                view_teachers.FacultyID,
                view_teachers.FacultyName,
                view_teachers.FacultyAbbr
PavelBegunkov's avatar
PavelBegunkov committed
            FROM `view_teachers`
            WHERE view_teachers.AccountID = pUserID
            LIMIT 1;
    END IF;
PavelBegunkov's avatar
PavelBegunkov committed
END //

DROP PROCEDURE IF EXISTS GetFullInfo//
CREATE PROCEDURE `GetFullInfo` (
PavelBegunkov's avatar
PavelBegunkov committed
    IN `pUserID` INT,
    IN `pSemesterID` INT
PavelBegunkov's avatar
PavelBegunkov committed
    DECLARE vAccountType enum('student', 'teacher');

    SELECT user_roles.Type INTO vAccountType
        FROM `accounts`
        INNER JOIN `user_roles` ON accounts.UserRoleID = user_roles.ID
        WHERE accounts.ID = pUserID
        LIMIT 1;

# type 1: student
#      2: teacher
    IF vAccountType = 'student' THEN
        SELECT  # student info
                view_students.LastName,
                view_students.FirstName,
                view_students.SecondName,
                view_students.StudentID,
                view_students.GradeID,
                view_students.GradeNum,
                view_students.GroupID,
                view_students.GroupNum,
                view_students.GroupName,
                view_students.Degree,
                view_students.SpecID,
                view_students.SpecName,
                view_students.SpecAbbr,
                view_students.FacultyID,
                view_students.FacultyName,
                view_students.FacultyAbbr,
                # account info
                accounts.ID,
                accounts.Login,
                accounts.EMail,
                user_roles.Type,
                user_roles.RoleName AS 'Role',
                user_roles.Mark     AS 'RoleMark',
                accounts.IsEnabled,
                accounts.ActivationCode AS 'Code',
                accounts.UserAgent
            FROM `view_students`
            INNER JOIN `accounts` ON accounts.ID = view_students.AccountID
            INNER JOIN `user_roles` ON user_roles.ID = accounts.UserRoleID
            WHERE view_students.AccountID = pUserID AND
PavelBegunkov's avatar
PavelBegunkov committed
                  view_students.SemesterID = pSemesterID
            LIMIT 1;
    ELSE
        SELECT  # teacher info
                view_teachers.LastName,
                view_teachers.FirstName,
                view_teachers.SecondName,
                view_teachers.TeacherID,
                view_teachers.DepID,
                view_teachers.DepName,
                view_teachers.JobPositionName,
                view_teachers.FacultyID,
                view_teachers.FacultyName,
                view_teachers.FacultyAbbr,
                # account info
                accounts.ID,
                accounts.Login,
                accounts.EMail,
                user_roles.Type,
                user_roles.RoleName AS 'Role',
                user_roles.Mark     AS 'RoleMark',
                accounts.IsEnabled,
                accounts.ActivationCode AS 'Code',
                accounts.UserAgent
            FROM `view_teachers`
            INNER JOIN `accounts` ON accounts.ID = view_teachers.AccountID
            INNER JOIN `user_roles` ON user_roles.ID = accounts.UserRoleID
            WHERE view_teachers.AccountID = pUserID
            LIMIT 1;
    END IF;
END //
PavelBegunkov's avatar
PavelBegunkov committed
# -------------------------------------------------------------------------------------------
# Label: teachers
# -------------------------------------------------------------------------------------------
PavelBegunkov's avatar
PavelBegunkov committed

DROP PROCEDURE IF EXISTS GetTeachersByFaculty//
PavelBegunkov's avatar
PavelBegunkov committed
# CREATE PROCEDURE `GetTeachersByFaculty` (IN `pFacultyID` INT)
#     NO SQL
# BEGIN
#     SELECT  view_teachers.TeacherID       AS 'ID',
#             view_teachers.LastName      AS 'Last',
#             view_teachers.FirstName     AS 'First',
#             view_teachers.SecondName    AS 'Second',
#             view_teachers.AccountID,
#             view_teachers.JobPositionName,
#             view_teachers.DepID,
#             view_teachers.DepName
#         FROM `view_teachers`
#         WHERE view_teachers.FacultyID = pFacultyID
#         ORDER BY view_teachers.LastName ASC, view_teachers.FirstName ASC;
# END //
PavelBegunkov's avatar
PavelBegunkov committed

PavelBegunkov's avatar
PavelBegunkov committed

PavelBegunkov's avatar
PavelBegunkov committed
DROP PROCEDURE IF EXISTS GetTeachersByDepartment//
CREATE PROCEDURE `GetTeachersByDepartment` (IN `pDepartmentID` INT)
    NO SQL
BEGIN
PavelBegunkov's avatar
PavelBegunkov committed
    SELECT  view_teachers.TeacherID AS 'ID',
            view_teachers.LastName,
            view_teachers.FirstName,
            view_teachers.SecondName,
PavelBegunkov's avatar
PavelBegunkov committed
            view_teachers.AccountID,
            view_teachers.JobPositionName,
            view_teachers.DepID,
            view_teachers.DepName
        FROM `view_teachers`
PavelBegunkov's avatar
PavelBegunkov committed
        WHERE view_teachers.DepID = pDepartmentID
PavelBegunkov's avatar
PavelBegunkov committed
        ORDER BY view_teachers.LastName ASC, view_teachers.FirstName ASC;
PavelBegunkov's avatar
PavelBegunkov committed
# get teachers, that teach course
DROP PROCEDURE IF EXISTS GetTeachersForDiscipline//
CREATE PROCEDURE `GetTeachersForDiscipline`(IN `pDisciplineID` INT)
    NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
BEGIN
    DECLARE vAuthorID INT DEFAULT -1;
    SET vAuthorID = GetDisciplineProperty(pDisciplineID, 'author');

    SELECT  view_teachers.TeacherID AS 'ID',
            view_teachers.LastName,
            view_teachers.FirstName,
            view_teachers.SecondName,
            view_teachers.JobPositionID,
            view_teachers.JobPositionName,
            view_teachers.DepID,
            view_teachers.DepName,
            view_teachers.FacultyID,
            view_teachers.FacultyAbbr,
            ( view_teachers.TeacherID = vAuthorID ) AS 'IsAuthor'
        FROM `disciplines_teachers`
        INNER JOIN `view_teachers` ON view_teachers.TeacherID = disciplines_teachers.TeacherID
        WHERE disciplines_teachers.DisciplineID = pDisciplineID
        ORDER BY    view_teachers.TeacherID = vAuthorID DESC,
                    view_teachers.LastName ASC,
                    view_teachers.FirstName ASC;
PavelBegunkov's avatar
PavelBegunkov committed

PavelBegunkov's avatar
PavelBegunkov committed
# get teachers, that don't teach course
DROP PROCEDURE IF EXISTS SearchTeachers//
CREATE PROCEDURE `SearchTeachers` (
    IN `pFacultyID` INT,
    IN `pDepartmentID` INT,
    IN `pFullName` VARCHAR(100) CHARSET utf8,
    # order: LastName + FirstName + SecondName
    IN `pDisciplineID` INT
) NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
BEGIN
PavelBegunkov's avatar
PavelBegunkov committed
    DECLARE vAuthorID INT DEFAULT -1;
    DECLARE vFullNameReg VARCHAR(102) CHARSET utf8;
    SET vAuthorID = GetDisciplineProperty(pDisciplineID, 'author');
    SET vFullNameReg = CONCAT('%', pFullName, '%');


    CREATE TEMPORARY TABLE IF NOT EXISTS tDiscTeachers AS (
        SELECT disciplines_teachers.TeacherID
            FROM disciplines_teachers
            WHERE disciplines_teachers.DisciplineID = pDisciplineID
    );
PavelBegunkov's avatar
PavelBegunkov committed

PavelBegunkov's avatar
PavelBegunkov committed
    SELECT  view_teachers.TeacherID AS 'ID',
            view_teachers.LastName,
            view_teachers.FirstName,
            view_teachers.SecondName,
PavelBegunkov's avatar
PavelBegunkov committed
            view_teachers.JobPositionName,
            view_teachers.DepID,
PavelBegunkov's avatar
PavelBegunkov committed
            view_teachers.DepName,
            ( view_teachers.TeacherID = vAuthorID ) AS 'IsAuthor'
PavelBegunkov's avatar
PavelBegunkov committed
        FROM `view_teachers`
        WHERE   view_teachers.FacultyID = pFacultyID AND
                CASE WHEN pDepartmentID != 0 THEN view_teachers.DepID = pDepartmentID ELSE TRUE END AND
                NOT EXISTS (
                    SELECT * FROM tDiscTeachers
                    WHERE tDiscTeachers.TeacherID = view_teachers.TeacherID
                    LIMIT 1
                ) AND
                CASE
                    WHEN pFullName != '' THEN
                        CONCAT( view_teachers.LastName, ' ', view_teachers.FirstName, ' ', view_teachers.SecondName)
                            LIKE vFullNameReg
                    ELSE
                        TRUE
                END
        ORDER BY    view_teachers.FacultyID ASC,
                    view_teachers.DepName ASC,
                    view_teachers.LastName ASC,
                    view_teachers.FirstName ASC;
PavelBegunkov's avatar
PavelBegunkov committed
# -------------------------------------------------------------------------------------------
# Label: students
# -------------------------------------------------------------------------------------------

DROP PROCEDURE IF EXISTS GetStudentsByStudyGroups//
DROP PROCEDURE IF EXISTS GetStudents//
CREATE PROCEDURE `GetStudents`  (
    IN `pGroupID` INT,
    IN `pSemesterID` INT
) NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
BEGIN
PavelBegunkov's avatar
PavelBegunkov committed
    SELECT  view_students.StudentID AS 'ID',
PavelBegunkov's avatar
PavelBegunkov committed
            view_students.LastName,
            view_students.FirstName,
            view_students.SecondName,
PavelBegunkov's avatar
PavelBegunkov committed
            view_students.AccountID,
            view_students.GradeID,
            view_students.GradeNum,
            view_students.Degree,
            view_students.GroupID,
            view_students.GroupNum
        FROM `view_students`
PavelBegunkov's avatar
PavelBegunkov committed
        WHERE view_students.GroupID = pGroupID AND
              view_students.SemesterID = pSemesterID
PavelBegunkov's avatar
PavelBegunkov committed
        ORDER BY view_students.LastName ASC, view_students.FirstName ASC;
Andrew Rudenets's avatar
Andrew Rudenets committed
DROP PROCEDURE IF EXISTS GetStudentsByFaculty//
CREATE PROCEDURE `GetStudentsByFaculty` (
    IN `pFacultyID` INT,
    IN `pGradeID` INT,
    IN `pGroupID` INT,
    IN `pSemesterID` INT
) NO SQL
Andrew Rudenets's avatar
Andrew Rudenets committed
BEGIN
PavelBegunkov's avatar
PavelBegunkov committed
    SELECT  view_students.StudentID AS 'ID',
PavelBegunkov's avatar
PavelBegunkov committed
            view_students.LastName,
            view_students.FirstName,
            view_students.SecondName,
PavelBegunkov's avatar
PavelBegunkov committed
            view_students.AccountID,
            view_students.GradeID,
            view_students.GradeNum,
            view_students.Degree,
            view_students.GroupID,
            view_students.GroupNum
    FROM `view_students`
    WHERE   view_students.FacultyID = pFacultyID AND
            view_students.SemesterID = pSemesterID AND
            CASE WHEN pGradeID != 0 THEN view_students.GradeID = pGradeID ELSE TRUE END AND
            CASE WHEN pGroupID != 0 THEN view_students.GroupID = pGroupID ELSE TRUE END
PavelBegunkov's avatar
PavelBegunkov committed
    ORDER BY view_students.LastName ASC, view_students.FirstName ASC;
END //


DROP PROCEDURE IF EXISTS SearchStudentsByName//
CREATE PROCEDURE `SearchStudentsByName`(
        IN `pFullName` VARCHAR(100) CHARSET utf8,
        IN `pFacultyID` INT,
        IN `pGradeID` INT,
        IN `pGroupID` INT,
        IN `pSemesterID` INT
    )
    NO SQL
BEGIN
    SELECT  view_students.StudentID AS 'ID',
            view_students.LastName,
            view_students.FirstName,
            view_students.SecondName,
            view_students.AccountID,
            view_students.GradeID,
            view_students.GradeNum,
            view_students.Degree,
            view_students.GroupID,
            view_students.GroupNum
    FROM `view_students`
    WHERE view_students.SemesterID = pSemesterID AND
          (pFacultyID = 0 OR view_students.FacultyID = pFacultyID) AND
          (pGradeID = 0 OR view_students.GradeID = pGradeID) AND
          (pGroupID = 0 OR view_students.GroupID = pGroupID) AND
          CONCAT(view_students.LastName, ' ', view_students.FirstName, ' ', view_students.SecondName)
              LIKE CONCAT('%', pFullName, '%')


    ORDER BY view_students.LastName ASC, view_students.FirstName ASC;
END //

# not in general groups, not attached
PavelBegunkov's avatar
PavelBegunkov committed
DROP PROCEDURE IF EXISTS SearchStudents//
CREATE PROCEDURE `SearchStudents` (
    IN `pGradeID` INT,
    IN `pGroupID` INT,
    IN `pFacultyID` INT,
    IN `pFullName` VARCHAR(100) CHARSET utf8,
    IN `pDisciplineID` INT
) NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
BEGIN
    DECLARE vSemesterID INT DEFAULT -1;
    DECLARE vFullNameReg VARCHAR(102) CHARSET utf8;
    DECLARE vIsGroupAttached BOOLEAN DEFAULT FALSE;
    SET vSemesterID = GetDisciplineProperty(pDisciplineID, 'semester');
    SET vFullNameReg = CONCAT('%', pFullName, '%');

    CREATE TEMPORARY TABLE IF NOT EXISTS tDiscGroups AS (
        SELECT disciplines_groups.GroupID
        FROM `disciplines_groups`
        WHERE disciplines_groups.DisciplineID = pDisciplineID
    );
    CREATE TEMPORARY TABLE IF NOT EXISTS tAttStud AS (
        SELECT disciplines_students.StudentID
        FROM `disciplines_students`
        WHERE   disciplines_students.DisciplineID = pDisciplineID AND
                disciplines_students.Type = 'attach'
    );

    IF pGroupID != 0 THEN
        SET vIsGroupAttached = EXISTS(
            SELECT * FROM tDiscGroups WHERE tDiscGroups.GroupID = pGroupID LIMIT 1
        );
    END IF;

PavelBegunkov's avatar
PavelBegunkov committed
    SELECT  view_students.StudentID AS 'ID',
            view_students.LastName,
            view_students.FirstName,
            view_students.SecondName,
            view_students.GradeID,
            view_students.GradeNum,
            view_students.Degree,
            view_students.GroupID,
            view_students.GroupNum
PavelBegunkov's avatar
PavelBegunkov committed
        FROM `view_students`
        WHERE   view_students.SemesterID = vSemesterID AND
PavelBegunkov's avatar
PavelBegunkov committed
                view_students.FacultyID = pFacultyID AND
PavelBegunkov's avatar
PavelBegunkov committed
                view_students.GradeID = pGradeID AND

                CASE
                    WHEN pGroupID != 0 THEN
                        NOT vIsGroupAttached AND
                        view_students.GroupID = pGroupID
                    ELSE
                        NOT EXISTS( SELECT * FROM tDiscGroups WHERE tDiscGroups.GroupID = view_students.GroupID )
                END AND

                NOT EXISTS( SELECT * FROM tAttStud WHERE tAttStud.StudentID = view_students.StudentID LIMIT 1 ) AND

                CASE
                    WHEN pFullName != '' THEN
                        CONCAT(view_students.LastName, ' ', view_students.FirstName, ' ', view_students.SecondName) LIKE vFullNameReg
PavelBegunkov's avatar
PavelBegunkov committed
        ORDER BY    view_students.GradeID ASC,
                    view_students.GroupID ASC;
PavelBegunkov's avatar
PavelBegunkov committed
END //
PavelBegunkov's avatar
PavelBegunkov committed

# in general groups, attached or detached
PavelBegunkov's avatar
PavelBegunkov committed
DROP PROCEDURE IF EXISTS GetStudentsForDiscipline//
CREATE PROCEDURE `GetStudentsForDiscipline` (
    IN `pDisciplineID` INT
) NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
BEGIN
    DECLARE vSemesterID INT DEFAULT -1;
PavelBegunkov's avatar
PavelBegunkov committed
    SET vSemesterID = GetDisciplineProperty(pDisciplineID, 'semester');
    SELECT  view_students.StudentID AS 'ID',
            view_students.LastName,
            view_students.FirstName,
            view_students.SecondName,
            view_students.GradeID,
            view_students.GradeNum,
            view_students.Degree,
            view_students.GroupID,
            view_students.GroupNum,
            tDiscStudents.Type AS 'AttachType'
        FROM (
                SELECT  st.StudentID,
                        COALESCE(st.Type) AS 'Type'
                FROM (
                    SELECT  disciplines_students.StudentID,
                            disciplines_students.Type
                        FROM `disciplines_students`
                        WHERE disciplines_students.DisciplineID = pDisciplineID
                    UNION
                    SELECT  students_groups.StudentID,
                            NULL AS 'Type'
                        FROM `disciplines_groups`
                        LEFT JOIN `students_groups` ON students_groups.SemesterID = vSemesterID AND
                                                       students_groups.GroupID = disciplines_groups.GroupID
                        WHERE disciplines_groups.DisciplineID = pDisciplineID
                ) as st
                GROUP BY st.StudentID
        ) tDiscStudents
        INNER JOIN `view_students` ON   view_students.StudentID = tDiscStudents.StudentID AND
                                        view_students.SemesterID = vSemesterID
        ORDER BY    ( tDiscStudents.Type <=> 'attach' ) ASC,
                    view_students.GradeID ASC,
                    view_students.GroupNum ASC,
                    view_students.LastName ASC,
                    view_students.FirstName ASC;
PavelBegunkov's avatar
PavelBegunkov committed
END //
# in general groups + attached
PavelBegunkov's avatar
PavelBegunkov committed
DROP PROCEDURE IF EXISTS GetStudentsForRating//
CREATE PROCEDURE `GetStudentsForRating` (
    IN `pDisciplineID` INT
) NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
BEGIN
    DECLARE vSemesterID INT DEFAULT -1;
    SET vSemesterID = GetDisciplineProperty(pDisciplineID, 'semester');


    SELECT  view_students.StudentID AS 'ID',
            view_students.LastName,
            view_students.FirstName,
            view_students.SecondName,
            view_students.GradeID,
            view_students.GradeNum,
            view_students.Degree,
            view_students.GroupID,
            view_students.GroupNum,
            ( tDiscStudents.Type IS NOT NULL ) AS 'IsAttached'
        FROM (
                SELECT  st.StudentID,
                        COALESCE(st.Type) AS 'Type'
                FROM (
                    SELECT  disciplines_students.StudentID,
                            disciplines_students.Type
                        FROM `disciplines_students`
                        WHERE disciplines_students.DisciplineID = pDisciplineID
                    UNION
                        SELECT  students_groups.StudentID,
                                NULL AS 'Type'
                        FROM `disciplines_groups`
                        LEFT JOIN `students_groups` ON  students_groups.SemesterID = vSemesterID AND
                                                        students_groups.GroupID = disciplines_groups.GroupID
                        WHERE disciplines_groups.DisciplineID = pDisciplineID
                ) as st
                GROUP BY st.StudentID
        ) tDiscStudents
        INNER JOIN `view_students` ON   view_students.StudentID = tDiscStudents.StudentID AND
                                        view_students.SemesterID = vSemesterID
        WHERE NOT tDiscStudents.Type <=> 'detach'
        ORDER BY    view_students.GradeID ASC,
                    view_students.GroupNum ASC,
                    view_students.LastName ASC,
                    view_students.FirstName ASC;
PavelBegunkov's avatar
PavelBegunkov committed

PavelBegunkov's avatar
PavelBegunkov committed
# -------------------------------------------------------------------------------------------
# Label: disciplines
# -------------------------------------------------------------------------------------------
PavelBegunkov's avatar
PavelBegunkov committed

DROP PROCEDURE IF EXISTS GetDisciplineInfo//
DROP PROCEDURE IF EXISTS Discipline_GetInfo//
CREATE PROCEDURE `Discipline_GetInfo` (
    IN `pDisciplineID` INT
) NO SQL
BEGIN
    DECLARE vIsBonus BOOLEAN;
    SET vIsBonus = EXISTS(
        SELECT * FROM `modules`
            WHERE   modules.DisciplineID = pDisciplineID AND
                    modules.Type = 'bonus'
            LIMIT 1
    );

    SELECT
xamgore's avatar
xamgore committed
        view_disciplines.DisciplineID AS 'ID',
        view_disciplines.AuthorID,
        view_disciplines.GradeID,
        view_disciplines.GradeNum,
        view_disciplines.Degree,
        view_disciplines.ExamType       AS 'Type',
        view_disciplines.LectureCount   AS 'Lectures',
        view_disciplines.PracticeCount  AS 'Practice',
        view_disciplines.LabCount       AS 'Labs',
        view_disciplines.SemesterID,
        view_disciplines.SubjectID,
        view_disciplines.SubjectName,
        view_disciplines.SubjectAbbr,
        view_disciplines.FacultyID,
        view_disciplines.FacultyName,
        view_disciplines.IsLocked,
        view_disciplines.Milestone,
        view_disciplines.Subtype,
        vIsBonus AS 'IsBonus',
        semesters.Num AS 'semesterNum', # TODO: Camelize
        semesters.Year AS 'semesterYear'
    FROM `view_disciplines`
    INNER JOIN `semesters` ON semesters.ID = view_disciplines.SemesterID
    WHERE view_disciplines.DisciplineID = pDisciplineID
    LIMIT 1;
END //
PavelBegunkov's avatar
PavelBegunkov committed
# TODO: haven't reference on it
# all disciplines for faculty in current semester
DROP PROCEDURE IF EXISTS GetDisciplines//
CREATE PROCEDURE `GetDisciplines` (
    IN `pFacultyID` INT,
    IN `pSemesterID` INT
) NO SQL
xamgore's avatar
xamgore committed
    SELECT  view_disciplines.DisciplineID AS 'ID',
            view_disciplines.SubjectID,
            view_disciplines.SubjectName,
            view_disciplines.ExamType AS 'Type',
xamgore's avatar
xamgore committed
            (view_disciplines_results.DisciplineRateMax = 100) AS 'isMapCreated'
PavelBegunkov's avatar
PavelBegunkov committed
        FROM `view_disciplines`
PavelBegunkov's avatar
PavelBegunkov committed
        INNER JOIN `view_disciplines_results`
PavelBegunkov's avatar
PavelBegunkov committed
            ON view_disciplines_results.DisciplineID = view_disciplines.DisciplineID
        WHERE view_disciplines.SemesterID = pSemesterID AND
PavelBegunkov's avatar
PavelBegunkov committed
            view_disciplines.FacultyID = pFacultyID
        ORDER BY view_disciplines.SubjectName ASC;
PavelBegunkov's avatar
PavelBegunkov committed
# processed format of output (after desequentialization)
PavelBegunkov's avatar
PavelBegunkov committed
# { discipline1 {group1, group2, ...}, discipline2 {groupN, ...}, ... }
DROP PROCEDURE IF EXISTS GetDisciplinesForTeacher//
CREATE PROCEDURE `GetDisciplinesForTeacher` (
        IN `pTeacherID` INT,
        IN `pSemesterID` INT
    )
    NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
BEGIN
PavelBegunkov's avatar
PavelBegunkov committed
    SELECT DISTINCT view_disciplines.DisciplineID AS 'ID',
xamgore's avatar
xamgore committed
                    view_disciplines.ExamType AS 'Type',
                    view_disciplines.Subtype,
                    view_disciplines.GradeID,
                    view_disciplines.GradeNum,
                    view_disciplines.Degree,
                    view_groups.GroupID,
                    view_groups.GroupNum,
                    view_groups.GroupName,
                    view_disciplines.SubjectID,
                    view_disciplines.SubjectName,
                    view_disciplines.AuthorID,
                    view_disciplines.IsLocked AS 'IsLocked', # TODO: change column name
xamgore's avatar
xamgore committed
                    (view_disciplines_results.DisciplineRateMax = 100)    AS 'IsMapCreated'
PavelBegunkov's avatar
PavelBegunkov committed
    FROM `disciplines_teachers`
    LEFT JOIN `disciplines_groups` ON disciplines_groups.DisciplineID = disciplines_teachers.DisciplineID
PavelBegunkov's avatar
PavelBegunkov committed
        # left -> inner join with maybe NULL ?
    LEFT JOIN `view_groups` ON view_groups.GroupID = disciplines_groups.GroupID
    LEFT JOIN `view_disciplines` ON disciplines_teachers.DisciplineID = view_disciplines.DisciplineID
    LEFT JOIN `view_disciplines_results` ON view_disciplines_results.DisciplineID = view_disciplines.DisciplineID
PavelBegunkov's avatar
PavelBegunkov committed
    WHERE disciplines_teachers.TeacherID = pTeacherID AND
            view_disciplines.SemesterID = pSemesterID
PavelBegunkov's avatar
PavelBegunkov committed
    ORDER BY    view_disciplines.GradeID ASC,
PavelBegunkov's avatar
PavelBegunkov committed
                view_disciplines.SubjectName ASC,
                view_disciplines.DisciplineID ASC,
                view_groups.GroupNum ASC;
PavelBegunkov's avatar
PavelBegunkov committed
# get all disciplines, that student take.
PavelBegunkov's avatar
PavelBegunkov committed
DROP PROCEDURE IF EXISTS GetDisciplinesForStudent//
CREATE PROCEDURE `GetDisciplinesForStudent` (
PavelBegunkov's avatar
PavelBegunkov committed
    IN `pStudentID` INT,
    IN `pSemesterID` INT
) NO SQL
PavelBegunkov's avatar
PavelBegunkov committed
BEGIN
    DECLARE vStudentGroup INT DEFAULT -1;
    SET vStudentGroup = GetStudentGroup(pStudentID, pSemesterID);

    # select all disciplines for user
    CREATE TEMPORARY TABLE IF NOT EXISTS tDisciplines AS (
        SELECT disc2.DisciplineID
            FROM (
                SELECT  disc1.DisciplineID,
                        COALESCE(disc1.Type) AS 'Type'
                FROM (
                        SELECT  disciplines_students.DisciplineID,
                                disciplines_students.Type
                            FROM `disciplines_students`
                            WHERE   disciplines_students.StudentID = pStudentID
                        UNION
                        SELECT  disciplines_groups.DisciplineID,
                                NULL AS 'Type'
                            FROM `disciplines_groups`
                            WHERE   disciplines_groups.GroupID = vStudentGroup
                    ) AS disc1
                INNER JOIN `disciplines` ON disciplines.ID = disc1.DisciplineID
                WHERE disciplines.SemesterID = pSemesterID
                GROUP BY disc1.DisciplineID
            ) AS disc2
            WHERE NOT disc2.Type <=> 'detach'
    );

    # only Cthulhu knows, what happened here
xamgore's avatar
xamgore committed
    SELECT  view_disciplines.DisciplineID AS 'ID',
            view_disciplines.SubjectID,
            view_disciplines.SubjectName,
            view_disciplines.ExamType AS 'Type',
            view_disciplines.Subtype,
            teachers.LastName,
            teachers.FirstName,
            teachers.SecondName,
            ( tDR.RateExam + tDR.RateMExam ) AS 'Rate',
            ( tDR.MaxRegularRate + tDR.MaxExamRate ) AS 'MaxCurrentRate'
        FROM (
            SELECT  tRating.DisciplineID,
                    SUM(
                        IF( tRating.SubmoduleIsUsed AND tRating.ModuleType <=> 'regular',
                            tRating.SubmoduleRate, 0)
                    ) AS 'MaxRegularRate',
                    MAX(
                        IF( tRating.SubmoduleIsUsed AND tRating.ModuleType <=> 'exam',
                            tRating.SubmoduleRate, 0)
                    ) AS 'MaxExamRate',
                    SUM(IF(NOT tRating.ModuleType <=> 'exam', tRating.Rate, 0)) AS 'RateMExam',
                    MAX(IF(tRating.ModuleType <=> 'exam' AND tRating.Rate IS NOT NULL, tRating.Rate, 0)) AS 'RateExam'
                FROM (
                    SELECT  tDisciplines.DisciplineID,
                            vr.SubmoduleRate,
                            vr.ModuleType,
                            rt.Rate,
                            vr.SubmoduleIsUsed
                        FROM `tDisciplines`
                        LEFT JOIN `view_roadmap` AS vr ON   vr.DisciplineID = tDisciplines.DisciplineID
                        LEFT JOIN `rating_table` AS rt  ON  rt.StudentID = pStudentID AND
                                                            rt.SubmoduleID = vr.SubmoduleID
                ) AS tRating
                GROUP BY tRating.DisciplineID
        ) AS tDR
        INNER JOIN `view_disciplines` ON view_disciplines.DisciplineID = tDR.DisciplineID
        INNER JOIN `teachers` ON teachers.ID = view_disciplines.AuthorID
        ORDER BY view_disciplines.ExamType ASC, view_disciplines.SubjectName ASC;
PavelBegunkov's avatar
PavelBegunkov committed
# get all disciplines for group, including disciplines, where students have attached status
DROP PROCEDURE IF EXISTS GetDisciplinesForGroup//
CREATE PROCEDURE `GetDisciplinesForGroup` (
        IN `pGroupID` INT,
        IN `pSemesterID` INT
    )
    NO SQL
BEGIN
xamgore's avatar
xamgore committed
    (SELECT view_disciplines.DisciplineID  AS 'ID',
xamgore's avatar
xamgore committed
            view_disciplines.SubjectName,
            view_disciplines.Subtype,
            view_disciplines.ExamType  AS 'Type'
PavelBegunkov's avatar
PavelBegunkov committed
        FROM `disciplines_groups`
        INNER JOIN `view_disciplines` ON    view_disciplines.DisciplineID = disciplines_groups.DisciplineID AND
                                            view_disciplines.SemesterID = pSemesterID
PavelBegunkov's avatar
PavelBegunkov committed
        WHERE disciplines_groups.GroupID = pGroupID
    ) UNION DISTINCT
xamgore's avatar
xamgore committed
    (SELECT view_disciplines.DisciplineID  AS 'ID',
xamgore's avatar
xamgore committed
            view_disciplines.SubjectName,
            view_disciplines.Subtype,
            view_disciplines.ExamType AS 'Type'
PavelBegunkov's avatar
PavelBegunkov committed
        FROM `disciplines_students`
        INNER JOIN `students` ON disciplines_students.StudentID = students.ID
        INNER JOIN `view_disciplines` ON view_disciplines.DisciplineID = disciplines_students.DisciplineID AND
                                         view_disciplines.SemesterID = pSemesterID
PavelBegunkov's avatar
PavelBegunkov committed
        INNER JOIN `students_groups` ON students_groups.StudentID = students.ID AND
                                        students_groups.SemesterID = pSemesterID
PavelBegunkov's avatar
PavelBegunkov committed
        WHERE students_groups.GroupID = pGroupID
PavelBegunkov's avatar
PavelBegunkov committed
    );
PavelBegunkov's avatar
PavelBegunkov committed
# -------------------------------------------------------------------------------------------
# Label: rating
# -------------------------------------------------------------------------------------------
DROP PROCEDURE IF EXISTS GetRatesForGroup//
CREATE PROCEDURE `GetRatesForGroup` (
    IN `pDisciplineID` INT,
    IN `pGroupID` INT)
PavelBegunkov's avatar
PavelBegunkov committed
    NO SQL
BEGIN
PavelBegunkov's avatar
PavelBegunkov committed
    DECLARE vChecker BOOLEAN DEFAULT FALSE;
    DECLARE vSemesterID INT DEFAULT -1;
    SET vSemesterID = GetDisciplineProperty(pDisciplineID, 'semester');
PavelBegunkov's avatar
PavelBegunkov committed
    SELECT disciplines_groups.ID IS NOT NULL INTO vChecker
        FROM `disciplines_groups`
        WHERE disciplines_groups.DisciplineID = pDisciplineID AND
              disciplines_groups.GroupID = pGroupID
        LIMIT 1;
PavelBegunkov's avatar
PavelBegunkov committed
    IF !vChecker THEN