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 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 266
VideoRepositoryService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 14
3540.00
0.00% covered (danger)
0.00%
0 / 266
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 __clone
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 getInstance
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 5
 addVideoLink
0.00% covered (danger)
0.00%
0 / 1
210.00
0.00% covered (danger)
0.00%
0 / 44
 deleteVideoMaterial
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 18
 deleteActualSubjectPlanVideoMaterials
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 deleteProposedSubjectPlanVideoMaterials
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 deletePseudoSubjectActualPlan
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 deletePseudoSubjectProposedPlan
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 deleteVideoFromBatchRepository
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 12
 updateVideoMaterialDetails
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 24
 isStaffCourseVideoMaterial
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 17
 getAllVideoCourseMaterials
0.00% covered (danger)
0.00%
0 / 1
156.00
0.00% covered (danger)
0.00%
0 / 89
 getProposedPlanCourseVideoMaterial
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 16
<?php
namespace com\linways\core\ams\professional\service;
use com\linways\base\util\SecurityUtils;
use com\linways\core\ams\professional\constant\ResourceContext;
use com\linways\core\ams\professional\dto\VideoMaterial;
use com\linways\core\ams\professional\exception\ProfessionalException;
use com\linways\core\ams\professional\mapper\VideoMaterialServiceMapper;
use com\linways\core\ams\professional\request\AddResourceRequest;
use com\linways\core\ams\professional\request\AddVideoLinkRequest;
use com\linways\core\ams\professional\request\DeleteVideoFromRepositoryRequest;
use com\linways\core\ams\professional\request\GetCourseVideoMaterialRequest;
use com\linways\core\ams\professional\request\UpdateVideoMaterialRequest;
use com\linways\core\ams\professional\response\GetCourseVideoMaterialResponse;
/**
 *
 * @author jithinvijayan
 * @Date 30/03/20
 */
class VideoRepositoryService extends BaseService
{
    /**
     * @var null
     */
    private static $_instance = null;
    private $mapper = [];
    /**
     * Locked down the constructor|Prevent any outside instantiation of this class
     *
     * CourseMaterialService constructor.
     */
    private function __construct()
    {
        $this->mapper = VideoMaterialServiceMapper::getInstance()->getMapper();
    }
    /**
     * Prevent any object or instance of that class to be cloned | Prevent any copy of this object
     */
    private function __clone()
    {
    }
    /**
     * Have a single globally accessible static method
     *
     * @return VideoRepositoryService|null
     */
    public static function getInstance()
    {
        if (!is_object(self::$_instance))
            self::$_instance = new self ();
        return self::$_instance;
    }
    /**
     * @param AddVideoLinkRequest $request
     * @return Object|integer
     * @throws ProfessionalException
     */
    public function addVideoLink(AddVideoLinkRequest $request)
    {
        $request = $this->realEscapeObject($request);
        if (empty($request->url) &&  $request->backEndType != 'VIEWWAY') {
            throw new ProfessionalException(ProfessionalException::INVALID_URL, "Invalid url given");
        }
        if (empty($request->staffId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_STAFF_ID, "Invalid staff details given");
        }
        if (empty($request->title)) {
            throw new ProfessionalException(ProfessionalException::INVALID_TITLE, "Invalid url title given");
        }
        if (empty($request->pseudoSubjectId) && empty($request->batchId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $addResourceRequest = new AddResourceRequest();
        $addResourceRequest->backendType = $request->backEndType;
        if ($request->storageObject) {
            $addResourceRequest->storageObject = json_encode($request->storageObject);
        } else {
            $addResourceRequest->storageObject = "{}";
        }
        $addResourceRequest->path = $request->url;
        $addResourceRequest->createdBy = $request->createdBy;
        $addResourceRequest->updatedBy = $request->createdBy;
        $addResourceRequest->context = ResourceContext::VIDEO_MATERIALS;
        $resourceId = ResourceService::getInstance()->addResources($addResourceRequest);
        $request->batchId = empty($request->batchId) ? "NULL" : $request->batchId;
        $request->pseudoSubjectId = empty($request->pseudoSubjectId) ? "NULL" : $request->pseudoSubjectId;
        $request->subjectId = empty($request->subjectId) ? "NULL" : $request->subjectId;
        $request->semesterId = empty($request->semesterId) ? "NULL" : $request->semesterId;
        $request->topicId = $request->topicId?$request->topicId:"NULL";
        $id = SecurityUtils::getRandomString();
        $sql = "INSERT INTO course_video_materials (id, title, description, resource_id, keywords, thumbnail_url, staff_id, 
                batch_id, subject_id, pseudo_subject_id, semester_id,topic_id,created_by, updated_by, created_date, updated_date) 
                VALUES ('$id','$request->title','$request->description','$resourceId','$request->keywords',
                        '$request->thumbnail',$request->staffId,$request->batchId,$request->subjectId,$request->pseudoSubjectId,
                        $request->semesterId,$request->topicId ,$request->createdBy,$request->updatedBy,UTC_TIMESTAMP(),UTC_TIMESTAMP())";
        try {
            $this->executeQueryForObject($sql);
            return $id;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $materialId
     * @throws ProfessionalException
     */
    public function deleteVideoMaterial($materialId)
    {
        $materialId = $this->realEscapeString($materialId);
        if (empty($materialId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_BATCH_VIDEO_REPOSITORY_ID, "Invalid video repository details given");
        }
        try {
            $this->beginTransaction();
            # Deleting subject actual plan and video material relation
            $this->deleteActualSubjectPlanVideoMaterials($materialId);
            # Deleting subject proposed plan and video material relation
            $this->deleteProposedSubjectPlanVideoMaterials($materialId);
            # Deleting pseudo subject actual plan and video material relation
            $this->deletePseudoSubjectActualPlan($materialId);
            # Deleting pseudo subject proposed plan and video material relation
            $this->deletePseudoSubjectProposedPlan($materialId);
            # Deleting video material permanently
            $this->deleteVideoFromBatchRepository($materialId);
            $this->commit();
        } catch (\Exception $e) {
            $this->rollback();
            throw new ProfessionalExceptior($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $materialId
     * @throws ProfessionalException
     */
    private function deleteActualSubjectPlanVideoMaterials($materialId)
    {
        $sql = "DELETE FROM subject_actual_plan_video_materials 
                WHERE video_repository_id = '$materialId'";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $materialId
     * @throws ProfessionalException
     */
    private function deleteProposedSubjectPlanVideoMaterials($materialId)
    {
        $sql = "DELETE FROM subject_proposed_plan_video_materials 
                WHERE video_repository_id = '$materialId'";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $materialId
     * @throws ProfessionalException
     */
    private function deletePseudoSubjectActualPlan($materialId)
    {
        $sql = "DELETE FROM ps_actual_plan_video_materials 
                WHERE video_repository_id = '$materialId'";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $materialId
     * @throws ProfessionalException
     */
    private function deletePseudoSubjectProposedPlan($materialId)
    {
        $sql = "DELETE FROM ps_proposed_plan_video_materials 
                WHERE video_repository_id = '$materialId'";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $id
     * @throws ProfessionalException
     */
    private function deleteVideoFromBatchRepository($id)
    {
        $id = $this->realEscapeString($id);
        if (empty($id)) {
            throw new ProfessionalException(ProfessionalException::INVALID_BATCH_VIDEO_REPOSITORY_ID, "Invalid batch video details given");
        }
        $sql = "DELETE FROM course_video_materials WHERE id ='$id'";
        try {
            $this->ExecuteQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param UpdateVideoMaterialRequest $request
     * @throws ProfessionalException
     */
    public function updateVideoMaterialDetails(UpdateVideoMaterialRequest $request)
    {
        $request = $this->realEscapeObject($request);
        if (empty($request->materialId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_DOCUMENT_ID, "Invalid video material id");
        }
        if (empty($request->title)) {
            throw new ProfessionalException(ProfessionalException::INVALID_TITLE, "Invalid title given");
        }
        if (empty($request->description)) {
            throw new ProfessionalException(ProfessionalException::INVALID_DESCRIPTION, "Invalid description given");
        }
        if (empty($request->keywords)) {
            throw new ProfessionalException(ProfessionalException::INVALID_KEYWORDS, "Invalid keywords given");
        }
        $sql = "UPDATE course_video_materials 
                SET title ='$request->title',description = '$request->description',keywords='$request->keywords',
                    updated_by =$request->updatedBy,updated_date = UTC_TIMESTAMP(),semester_id = '$request->semesterId',subject_id = '$request->subjectId
                WHERE id = '$request->materialId'";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * Checking video ownership
     *
     * @param $materialId
     * @param $staffId
     * @return bool
     * @throws ProfessionalException
     */
    public function isStaffCourseVideoMaterial($materialId, $staffId)
    {
        $staffId = (int)$this->realEscapeString($staffId);
        $materialId = $this->realEscapeString($materialId);
        if (empty($materialId) || empty($staffId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "SELECT id FROM course_video_materials 
                WHERE staff_id = $staffId AND id = '$materialId'";
        try {
            $id = $this->executeQueryForObject($sql)->id;
            if (empty($id))
                return true;
            return false;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * Searching course video materials
     *
     * @param GetCourseVideoMaterialRequest $request
     * @return GetCourseVideoMaterialResponse|Object|VideoMaterial
     * @throws ProfessionalException
     */
    public function getAllVideoCourseMaterials(GetCourseVideoMaterialRequest $request)
    {
        $request = $this->realEscapeObject($request);
        $conditions = $conditions1 = "";
        $limitConditions = $limitConditions1 = "";
        $limitSql = $limitSql1 = "";
        if (!empty($request->materialId)) {
            $conditions .= " AND cvm.id ='$request->materialId";
            $conditions1 .= " AND cvm.id ='$request->materialId";
        }
        if (!empty($request->staffId)) {
            $conditions .= " AND cvm.staff_id = $request->staffId ";
            $limitConditions .= " AND staff_id = $request->staffId ";
            $conditions1 .= " AND cvm.staff_id = $request->staffId ";
            $limitConditions1 .= " AND staff_id = $request->staffId ";
        }
        if (!empty($request->pseudoSubjectId)) {
            $conditions .= " AND cvm.pseudo_subject_id = $request->pseudoSubjectId ";
            $limitConditions .= " AND pseudo_subject_id = $request->pseudoSubjectId ";
            $conditions1 .= " AND cvm.pseudo_subject_id = $request->pseudoSubjectId ";
            $limitConditions1 .= " AND pseudo_subject_id = $request->pseudoSubjectId ";
        }
        //if (!empty($request->batchId)) 
        else 
        {
            $conditions .= " AND cvm.batch_id = $request->batchId ";
            $limitConditions .= " AND batch_id = $request->batchId ";
            $conditions1 .= " AND cvm.batch_id = $request->batchId ";
            $limitConditions1 .= " AND batch_id = $request->batchId ";
        }
        if (!empty($request->subjectId)) {
            $conditions .= " AND cvm.subject_id = $request->subjectId ";
            $limitConditions .= " AND subject_id = $request->subjectId ";
            $conditions1 .= " AND cvm.subject_id IS NULL ";
            $limitConditions1 .= " AND subject_id IS NULL ";
        }
        if (!empty($request->semesterId) && empty($request->pseudoSubjectId)) {
            $conditions .= " AND cvm.semester_id = $request->semesterId ";
            $limitConditions .= " AND semester_id = $request->semesterId ";
            $conditions1 .= " AND cvm.semester_id IS NULL ";
            $limitConditions1 .= " AND semester_id IS NULL ";
        }
        if (!empty($request->keyword)) {
            $conditions .= " AND cvm.keywords LIKE '%$request->keyword%' ";
            $limitConditions .= " AND keywords LIKE '%$request->keyword%' ";
            $conditions1 .= " AND cvm.keywords LIKE '%$request->keyword%' ";
            $limitConditions1 .= " AND keywords LIKE '%$request->keyword%' ";
        }
        if (!empty($request->topicId)) {
            $conditions .= " AND cvm.topic_id = $request->topicId ";
            $limitConditions .= " AND topic_id = $request->topicId ";
            $conditions1 .= " AND cvm.topic_id = $request->topicId ";
            $limitConditions1 .= " AND topic_id = $request->topicId ";
        }
        if (empty($request->materialId)) {
            $limitSql = " INNER JOIN (SELECT id FROM course_video_materials WHERE 1=1 $limitConditions 
                        ORDER BY created_date DESC) as temp ON temp.id =cvm.id ";
            $limitSql1 = " INNER JOIN (SELECT id FROM course_video_materials WHERE 1=1 $limitConditions1 
            ORDER BY created_date DESC) as temp ON temp.id =cvm.id ";
                        
        }
        
        $sql = "SELECT cvm.id,cvm.title,cvm.description,cvm.keywords,
                DATE_FORMAT(CONVERT_TZ(cvm.created_date,'+00:00',@@global.time_zone),'%d-%m-%Y %h:%i:%s %p') as created_date,
                sa.staffID as staff_id,sa.staffName as staff_name,
                b.batchID as batch_id,b.batchName as batch_name,
                s2.subjectID as subject_id,s2.subjectName as subject_name,
                ps.pseudosubjectID as pseudo_subject_id,ps.subjectName as pseudo_subject_name,
                s.semID as semester_id,s.semName as semester_name,
                lr.id as resource_id,lr.path,lr.backend_type
                FROM course_video_materials cvm
                INNER JOIN lin_resource lr on cvm.resource_id = lr.id 
                INNER JOIN staffaccounts sa on cvm.staff_id = sa.staffID
                LEFT JOIN batches b on cvm.batch_id = b.batchID 
                LEFT JOIN subjects s2 on cvm.subject_id = s2.subjectID 
                LEFT JOIN pseudosubjects ps ON ps.pseudosubjectID =cvm.pseudo_subject_id 
                LEFT JOIN semesters s ON s.semID= cvm.semester_id
                WHERE 1=1 $conditions ";
        // $sql1 = "SELECT cvm.id,cvm.title,cvm.description,cvm.keywords,
        //         DATE_FORMAT(CONVERT_TZ(cvm.created_date,'+00:00',@@global.time_zone),'%d-%m-%Y %h:%i:%s %p') as created_date,
        //         sa.staffID as staff_id,sa.staffName as staff_name,
        //         b.batchID as batch_id,b.batchName as batch_name,
        //         s2.subjectID as subject_id,s2.subjectName as subject_name,
        //         ps.pseudosubjectID as pseudo_subject_id,ps.subjectName as pseudo_subject_name,
        //         s.semID as semester_id,s.semName as semester_name,
        //         lr.id as resource_id,lr.path,lr.backend_type
        //         FROM course_video_materials cvm
        //         $limitSql1
        //         INNER JOIN lin_resource lr on cvm.resource_id = lr.id 
        //         INNER JOIN staffaccounts sa on cvm.staff_id = sa.staffID
        //         LEFT JOIN batches b on cvm.batch_id = b.batchID 
        //         LEFT JOIN subjects s2 on cvm.subject_id = s2.subjectID 
        //         LEFT JOIN pseudosubjects ps ON ps.pseudosubjectID =cvm.pseudo_subject_id 
        //         LEFT JOIN semesters s ON s.semID= cvm.semester_id
        //         WHERE 1=1 $conditions1 ";
        $sql .= " ORDER BY cvm.created_date DESC "; 
        try {
            if (!empty($request->materialId)) {
                return $this->executeQueryForObject($sql, false, $this->mapper[VideoMaterialServiceMapper::SEARCH_COURSE_VIDEO_MATERIALS]);
            } else {
                $response = new GetCourseVideoMaterialResponse();
                $totalSql = "SELECT COUNT(id) as totalRecords FROM course_video_materials WHERE 1=1  $limitConditions";
                $response->totalRecords = (int)$this->executeQueryForObject($totalSql)->totalRecords;
                $response->materials = $this->executeQueryForList($sql, $this->mapper[VideoMaterialServiceMapper::SEARCH_COURSE_VIDEO_MATERIALS]);
                return $response;
            }
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    public function getProposedPlanCourseVideoMaterial($topicId)
    {
        $topicId = $this->realEscapeString($topicId);
        if (empty($topicId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "SELECT id as vidematId,title,resource_id,topic_id FROM course_video_materials 
                WHERE topic_id = '$topicId";
        try {
            $id = $this->executeQueryForObject($sql)->id;
            if (empty($id))
                return true;
            return false;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
}