Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 34
AdditionalModulesService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
56.00
0.00% covered (danger)
0.00%
0 / 34
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 createHostelStudentEntry
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 15
 addUserAccountRoles
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 17
<?php
namespace com\linways\core\ams\professional\service\additionalModules;
use com\linways\base\util\MakeSingletonTrait;
use com\linways\core\ams\professional\service\BaseService;
use com\linways\core\ams\professional\exception\ProfessionalException;
use com\linways\base\constant\UserType;
class AdditionalModulesService extends BaseService
{
    use MakeSingletonTrait;
    private function __construct()
    {
    }
    /**
     * Function to Create hostel applicant entry and assign `HOSTEL_APPLICANT` role.
     *
     * @param studentId StudentAccountId 
     * @return hostelApplicantId It Return hostel Applicant Id Of the given student
     * @throws ProfessionalException
     * @author Akshay K P <akshay.kp@linways.com>
     */
    public function createHostelStudentEntry($studentAccountId){
        $studentAccountId = $this->realEscapeString($studentAccountId);
        try {
            $year = date("Y");
            $sql_check = "SELECT id FROM hostel_v4_applicant hva WHERE student_account_id='$studentAccountId'";
            $applicantId = $this->executeQueryForObject($sql_check)->id;
            if (!$applicantId) {
                $sql = "INSERT INTO hostel_v4_applicant (student_name, email, user_name, password, created_by, created_date,updated_by, updated_date, mobile, `year`, gender, student_account_id) SELECT s.studentName, s.studentEmail, s.studentAccount, md5(s.studentAccount), 1, now(), 1, now(), s.studentPhone, '$year', s.studentGender, s.studentID from studentaccount s where studentID = '$studentAccountId'";
                $applicantId = $this->executeQuery($sql, true)->id;
            }
            // Assign given role to the students
            $this->addUserAccountRoles($applicantId, UserType::HOSTEL_APPLICANT, 'HOSTEL_APPLICANT');
            return $applicantId;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * The function `addUserAccountRoles` inserts a new user account role into the database based on
     * the provided user ID, user type, and role.
     * 
     * @param userId The userId parameter is the ID of the user for whom you want to add account roles.
     * @param userType The `userType` parameter represents the type of user account being added. It
     * could be a string value such as "admin", "customer", or "employee".
     * @param role The Assigning role Code.
     * 
     * @return userAccountRoleId
     * @throws ProfessionalException
     */
    public function addUserAccountRoles($userId, $userType, $role)
    {
        $userId = (int)$this->realEscapeString($userId);
        $userType = $this->realEscapeString($userType);
        $role = $this->realEscapeString($role);
        
        $userAccountRoleId = NULL;
        try {
            
            // Check Role exists Or Not, if exist return user account role Id Else Create new entry and return the id
            $sql_check = "SELECT id FROM `user_account_roles` WHERE user_id = '$userId' AND user_type = '$userType' AND role_id = (SELECT id FROM roles WHERE code = '$role');";
            $userAccountRoleId = $this->executeQueryForObject($sql_check)->id;
            if(empty($userAccountRoleId)) {
                $sql = "INSERT INTO `user_account_roles` (`user_id`, `user_type`, `role_id`, `created_by`, `created_date`, `updated_by`, `updated_date`) SELECT $userId,'$userType', id, 1, utc_timestamp(), 1, utc_timestamp() FROM roles WHERE code = '$role' AND is_active=1";
                $userAccountRoleId = $this->executeQuery($sql, true)->id;
            }
            return $userAccountRoleId;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
}