public static function createUser($first_name, $last_name, $email, $user_type, $majorId, $coursesIds, $termId) { self::validateName($first_name); self::validateName($last_name); self::validateNewEmail($email, self::DB_TABLE); self::validateUserType($user_type); //$this->validate_teaching_course($teaching_courses); try { $queryInsertUser = "******" . App::getDbName() . "`.`" . User::DB_TABLE . "` (`" . User::DB_COLUMN_EMAIL . "`,\n\t\t\t`" . User::DB_COLUMN_FIRST_NAME . "`, `" . User::DB_COLUMN_LAST_NAME . "`, `" . User::DB_COLUMN_USER_TYPES_ID . "`)\n\t\t\t\tVALUES(\n\t\t\t\t\t:email,\n\t\t\t\t\t:first_name,\n\t\t\t\t\t:last_name,\n\t\t\t\t\t(SELECT `" . UserTypesFetcher::DB_COLUMN_ID . "` FROM `" . UserTypesFetcher::DB_TABLE . "` WHERE `" . UserTypesFetcher::DB_COLUMN_TYPE . "`=:user_type )\n\t\t\t\t)"; $dbConnection = DatabaseManager::getConnection(); $dbConnection->beginTransaction(); $queryInsertUser = $dbConnection->prepare($queryInsertUser); $queryInsertUser->bindParam(':email', $email, PDO::PARAM_STR); $queryInsertUser->bindParam(':first_name', $first_name, PDO::PARAM_STR); $queryInsertUser->bindParam(':last_name', $last_name, PDO::PARAM_STR); $queryInsertUser->bindParam(':user_type', $user_type, PDO::PARAM_STR); $queryInsertUser->execute(); // last inserted if of THIS connection $userId = $dbConnection->lastInsertId(); if (strcmp($user_type, User::TUTOR) === 0) { Major::validateId($majorId); Tutor::insertMajor($userId, $majorId); if ($coursesIds !== NULL) { Tutor_has_course_has_schedule::addCourses($userId, $coursesIds, $termId); } } $dbConnection->commit(); return $userId; } catch (Exception $e) { $dbConnection->rollback(); throw new Exception("Could not insert user into database."); } }
public static function updateMajorId($id, $newMajorId, $oldMajorId) { if (!isset($newMajorId) || empty($newMajorId)) { throw new Exception("Data tempering detected. Aborting."); } if (strcmp($newMajorId, $oldMajorId) === 0) { return; } Major::validateId($newMajorId); StudentFetcher::updateMajorId($id, $newMajorId); }
public static function replaceMajorId($id, $newMajorId, $oldMajorId) { // no changes made. no need to do any work. if (strcmp($newMajorId, $oldMajorId) === 0) { return false; } Tutor::validateId($id); Major::validateId($newMajorId); Major::validateId($oldMajorId); TutorFetcher::replaceMajorId($id, $newMajorId); return true; }