<?php
namespace App\Repository;
use App\Entity\CourseQuizQuestion;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method CourseQuizQuestion|null find($id, $lockMode = null, $lockVersion = null)
* @method CourseQuizQuestion|null findOneBy(array $criteria, array $orderBy = null)
* @method CourseQuizQuestion[] findAll()
* @method CourseQuizQuestion[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class CourseQuizQuestionRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, CourseQuizQuestion::class);
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function remove(CourseQuizQuestion $entity, bool $flush = true): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->flush();
}
}
public function findByPage($page, $resultsPerPage, $order = 'ASC', $orderBy = "", $keyword = "", $course)
{
$keyword = filter_var($keyword, FILTER_SANITIZE_STRING);
$order = filter_var($order, FILTER_SANITIZE_STRING);
$orderBy = filter_var($orderBy, FILTER_SANITIZE_STRING);
try {
$query = $this->createQueryBuilder('c')
->andWhere('c.course = :cos')
->setParameter('cos', $course)
->setMaxResults($resultsPerPage)
->setFirstResult(($page - 1) * $resultsPerPage);
if ($keyword !== "") {
$query->andWhere('c.title LIKE :val')->setParameter('val', '%' . $keyword . '%');
}
if ($orderBy === "") {
$query->orderBy('c.id', 'ASC');
} else {
$query->orderBy("c." . $orderBy, $order);
}
return $query->getQuery()->getResult();
} catch (\Exception $exception) {
return [];
}
}
public function countByPage($keyword = "", $course)
{
$keyword = filter_var($keyword, FILTER_SANITIZE_STRING);
try {
$query = $this->createQueryBuilder('c')
->andWhere('c.course = :cos')
->setParameter('cos', $course)
->select('count(distinct(c.id))');
if ($keyword !== "") {
$query->andWhere('c.title LIKE :val ')->setParameter('val', '%' . $keyword . '%');
}
return $query->getQuery()->getSingleScalarResult();
} catch (\Exception $exception) {
return [];
}
}
// /**
// * @return CourseQuizQuestion[] Returns an array of CourseQuizQuestion objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->orderBy('c.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?CourseQuizQuestion
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}