Refactor - exercise/question category to use resources.

Add trait SessionCourseResourcesTrait in order to know
if the resource needs to load base courses when loading a session.
pull/3064/head
Julio Montoya 5 years ago
parent 3c1abb8b8e
commit f4fc63d477
  1. 4
      main/exercise/ReadingComprehension.php
  2. 232
      main/exercise/TestCategory.php
  3. 4
      main/exercise/exercise.class.php
  4. 47
      main/exercise/question.class.php
  5. 2
      main/exercise/question_pool.php
  6. 40
      main/inc/lib/ExerciseCategoryManager.php
  7. 10
      main/inc/lib/document.lib.php
  8. 2
      src/CoreBundle/Controller/ResourceController.php
  9. 4
      src/CoreBundle/Entity/Resource/ResourceNode.php
  10. 5
      src/CoreBundle/Migrations/Schema/V200/Version20.php
  11. 4
      src/CoreBundle/Repository/IllustrationRepository.php
  12. 185
      src/CoreBundle/Repository/ResourceRepository.php
  13. 3
      src/CourseBundle/Component/CourseCopy/CourseBuilder.php
  14. 51
      src/CourseBundle/Entity/CQuizQuestionCategory.php
  15. 20
      src/CourseBundle/Traits/SessionCourseResourcesTrait.php

@ -132,8 +132,8 @@ class ReadingComprehension extends UniqueAnswer
public function createForm(&$form, $exercise)
{
// Categories
$tabCat = TestCategory::getCategoriesIdAndName();
$form->addSelect('questionCategory', get_lang('Category'), $tabCat);
$categories = TestCategory::getCategoriesForSelect();
$form->addSelect('questionCategory', get_lang('Category'), $categories);
// Advanced parameters
$levels = self::get_default_levels();
$form->addSelect('questionLevel', get_lang('Difficulty'), $levels);

@ -1,6 +1,9 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\Resource\ResourceLink;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CourseBundle\Entity\CQuizQuestionCategory;
use ChamiloSession as Session;
/**
@ -81,30 +84,42 @@ class TestCategory
$row = Database::fetch_array($result);
// lets add in BDD if not the same name
if ($row['nb'] <= 0) {
$params = [
'c_id' => $courseId,
'title' => $this->name,
'description' => $this->description,
];
$newId = Database::insert($table, $params);
if ($newId) {
$repo = Container::getQuestionCategoryRepository();
$category = new CQuizQuestionCategory();
$category
->setTitle($this->name)
->setCourse($courseInfo['entity'])
->setDescription($this->description);
$em = $repo->getEntityManager();
$em->persist($category);
$em->flush();
if ($category) {
$newId = $category->getIid();
$sql = "UPDATE $table SET id = iid WHERE iid = $newId";
Database::query($sql);
$repo->addResourceToCourse(
$category,
ResourceLink::VISIBILITY_PUBLISHED,
api_get_user_entity(api_get_user_id()),
$courseInfo['entity'],
api_get_session_entity(),
api_get_group_entity()
);
api_item_property_update(
/*api_item_property_update(
$courseInfo,
TOOL_TEST_CATEGORY,
$newId,
'TestCategoryAdded',
api_get_user_id()
);
}
);*/
return $newId;
} else {
return false;
return $newId;
}
}
return false;
}
/**
@ -117,30 +132,35 @@ class TestCategory
*/
public function removeCategory($id)
{
$table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$tbl_question_rel_cat = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
$id = (int) $id;
$course_id = api_get_course_int_id();
$category = $this->getCategory($id);
$category = $this->getCategory($id, $course_id);
if ($category) {
$sql = "DELETE FROM $table
/*$sql = "DELETE FROM $table
WHERE id= $id AND c_id=".$course_id;
Database::query($sql);
Database::query($sql);*/
// remove link between question and category
$sql = "DELETE FROM $tbl_question_rel_cat
WHERE category_id = $id AND c_id=".$course_id;
Database::query($sql);
$repo = Container::getQuestionCategoryRepository();
$category = $repo->find($id);
$repo->hardDelete($category);
//Database::query($sql);
// item_property update
$courseInfo = api_get_course_info_by_id($course_id);
/*$courseInfo = api_get_course_info_by_id($course_id);
api_item_property_update(
$courseInfo,
TOOL_TEST_CATEGORY,
$this->id,
'TestCategoryDeleted',
api_get_user_id()
);
);*/
return true;
}
@ -157,33 +177,31 @@ class TestCategory
*/
public function modifyCategory($courseId = 0)
{
$table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$id = intval($this->id);
$name = Database::escape_string($this->name);
$description = Database::escape_string($this->description);
$cat = $this->getCategory($id, $courseId);
$courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId;
$courseInfo = api_get_course_info_by_id($courseId);
if (empty($courseInfo)) {
return false;
}
if ($cat) {
$sql = "UPDATE $table SET
title = '$name',
description = '$description'
WHERE id = $id AND c_id = ".$courseId;
Database::query($sql);
$repo = Container::getQuestionCategoryRepository();
/** @var CQuizQuestionCategory $category */
$category = $repo->find($this->id);
if ($category) {
$category
->setTitle($this->name)
->setDescription($this->description)
;
$repo->getEntityManager()->persist($category);
$repo->getEntityManager()->flush();
// item_property update
api_item_property_update(
/*api_item_property_update(
$courseInfo,
TOOL_TEST_CATEGORY,
$this->id,
'TestCategoryModified',
api_get_user_id()
);
);*/
return true;
}
@ -192,58 +210,23 @@ class TestCategory
/**
* Gets the number of question of category id=in_id.
*
* @param int $id
*
* @return int
*/
public function getCategoryQuestionsNumber()
public function getCategoryQuestionsNumber($id): int
{
$table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
$id = (int) $this->id;
$id = (int) $id;
$sql = "SELECT count(*) AS nb
FROM $table
WHERE category_id = $id AND c_id=".api_get_course_int_id();
$res = Database::query($sql);
$row = Database::fetch_array($res);
return $row['nb'];
}
/**
* Return an array of all Category objects in the database
* If $field=="" Return an array of all category objects in the database
* Otherwise, return an array of all in_field value
* in the database (in_field = id or name or description).
*
* @param string $field
* @param int $courseId
*
* @return array
*/
public static function getCategoryListInfo($field = '', $courseId = 0)
{
$courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId;
$table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$categories = [];
if (empty($field)) {
$sql = "SELECT id FROM $table
WHERE c_id = $courseId
ORDER BY title ASC";
$res = Database::query($sql);
while ($row = Database::fetch_array($res)) {
$category = new TestCategory();
$categories[] = $category->getCategory($row['id'], $courseId);
}
} else {
$field = Database::escape_string($field);
$sql = "SELECT $field FROM $table
WHERE c_id = $courseId
ORDER BY $field ASC";
$res = Database::query($sql);
while ($row = Database::fetch_array($res)) {
$categories[] = $row[$field];
}
}
return $categories;
return (int) $row['nb'];
}
/**
@ -483,22 +466,17 @@ class TestCategory
}
/**
* Return an array (id=>name)
* array[0] = get_lang('NoCategory');.
*
* @param int $courseId
*
* @return array
*/
public static function getCategoriesIdAndName($courseId = 0)
public static function getCategoriesForSelect()
{
if (empty($courseId)) {
$courseId = api_get_course_int_id();
}
$categories = self::getCategoryListInfo('', $courseId);
$courseId = api_get_course_int_id();
$categories = self::getCategories($courseId);
$result = ['0' => get_lang('GeneralSelected')];
for ($i = 0; $i < count($categories); $i++) {
$result[$categories[$i]->id] = $categories[$i]->name;
/** @var CQuizQuestionCategory $category */
foreach ($categories as $category) {
$result[$category->getId()] = $category->getTitle();
}
return $result;
@ -1022,16 +1000,15 @@ class TestCategory
*
* @return bool
*/
public static function categoryTitleExists($name, $courseId = 0)
public static function categoryTitleExists($name, $courseId)
{
$categories = self::getCategoryListInfo('title', $courseId);
foreach ($categories as $title) {
if ($title == $name) {
return true;
}
}
$repo = Container::getQuestionCategoryRepository();
$criteria = [
'title' => $name,
'course' => $courseId,
];
return false;
return $repo->getRepository()->count($criteria) > 0;
}
/**
@ -1096,13 +1073,18 @@ class TestCategory
*
* @return array
*/
public function getCategories($courseId, $sessionId = 0)
public static function getCategories($courseId, $sessionId = 0)
{
$table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
$sessionId = intval($sessionId);
$courseId = intval($courseId);
if (empty($courseId)) {
return [];
}
$sessionId = (int) $sessionId;
$courseId = (int) $courseId;
$sessionEntity = null;
if (!empty($sessionId)) {
$sessionEntity = api_get_session_entity($sessionId);
}
if (empty($sessionId)) {
$sessionCondition = api_get_session_condition(
$sessionId,
@ -1119,21 +1101,11 @@ class TestCategory
);
}
if (empty($courseId)) {
return [];
}
$courseEntity = api_get_course_entity($courseId);
$repo = Container::getQuestionCategoryRepository();
$resources = $repo->getResourcesByCourse($courseEntity, $sessionEntity);
$sql = "SELECT c.* FROM $table c
INNER JOIN $itemProperty i
ON c.c_id = i.c_id AND i.ref = c.id
WHERE
c.c_id = $courseId AND
i.tool = '".TOOL_TEST_CATEGORY."'
$sessionCondition
ORDER BY title";
$result = Database::query($sql);
return Database::store_result($result, 'ASSOC');
return $resources;
}
/**
@ -1147,27 +1119,27 @@ class TestCategory
$sessionId = (int) $sessionId;
$categories = $this->getCategories($courseId, $sessionId);
$html = '';
$deleteIcon = Display::return_icon('delete.png', get_lang('Delete'), [], ICON_SIZE_SMALL);
/** @var CQuizQuestionCategory $category */
foreach ($categories as $category) {
$tmpobj = new TestCategory();
$tmpobj = $tmpobj->getCategory($category['id']);
$nb_question = $tmpobj->getCategoryQuestionsNumber();
$rowname = self::protectJSDialogQuote($category['title']);
$nb_question_label = $nb_question == 1 ? $nb_question.' '.get_lang('Question') : $nb_question.' '.get_lang('Questions');
$content = "<span style='float:right'>".$nb_question_label."</span>";
$id = $category->getId();
$count = $this->getCategoryQuestionsNumber($id);
$rowname = self::protectJSDialogQuote($category->getTitle());
$label = $count == 1 ? $count.' '.get_lang('Question') : $count.' '.get_lang('Questions');
$content = "<span style='float:right'>".$label."</span>";
$content .= '<div class="sectioncomment">';
$content .= $category['description'];
$content .= $category->getTitle();
$content .= '</div>';
$links = '';
$links = '';
if (!$sessionId) {
$links .= '<a href="'.api_get_self().'?action=editcategory&category_id='.$category['id'].'&'.api_get_cidreq().'">'.
$links .= '<a href="'.api_get_self().'?action=editcategory&category_id='.$id.'&'.api_get_cidreq().'">'.
Display::return_icon('edit.png', get_lang('Edit'), [], ICON_SIZE_SMALL).'</a>';
$links .= ' <a href="'.api_get_self().'?'.api_get_cidreq().'&action=deletecategory&category_id='.$category['id'].'" ';
$links .= 'onclick="return confirmDelete(\''.self::protectJSDialogQuote(get_lang('Are you sure you want to delete this category?').'['.$rowname).'] ?\', \'id_cat'.$category['id'].'\');">';
$links .= Display::return_icon('delete.png', get_lang('Delete'), [], ICON_SIZE_SMALL).'</a>';
$links .= ' <a href="'.api_get_self().'?'.api_get_cidreq().'&action=deletecategory&category_id='.$id.'" ';
$links .= 'onclick="return confirmDelete(\''.self::protectJSDialogQuote(get_lang('Are you sure you want to delete this category?').'['.$rowname).'] ?\', \'id_cat'.$id.'\');">';
$links .= $deleteIcon.'</a>';
}
$html .= Display::panel($content, $category['title'].$links);
$html .= Display::panel($content, $category->getTitle().$links);
}
return $html;

@ -1691,10 +1691,10 @@ class Exercise
Database::query($sql);
$exercise = $repo->find($this->id);
$node = $repo->addResourceNode($exercise, api_get_user_entity(api_get_user_id()), $courseEntity);
$repo->addResourceToCourse(
$node,
$exercise,
ResourceLink::VISIBILITY_PUBLISHED,
api_get_user_entity(api_get_user_id()),
api_get_course_entity(),
api_get_session_entity(),
api_get_group_entity()

@ -1,6 +1,7 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\Resource\ResourceLink;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CourseBundle\Entity\CDocument;
use Chamilo\CourseBundle\Entity\CQuizAnswer;
@ -541,45 +542,6 @@ abstract class Question
$this->uncheckedMayScore = $value;
}
/**
* Save category of a question.
*
* A question can have n categories if category is empty,
* then question has no category then delete the category entry
*
* @param array $category_list
*
* @author Julio Montoya - Adding multiple cat support
*/
public function saveCategories($category_list)
{
if (!empty($category_list)) {
$this->deleteCategory();
$table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
// update or add category for a question
foreach ($category_list as $category_id) {
$category_id = (int) $category_id;
$question_id = (int) $this->id;
$sql = "SELECT count(*) AS nb
FROM $table
WHERE
category_id = $category_id
AND question_id = $question_id
AND c_id=".api_get_course_int_id();
$res = Database::query($sql);
$row = Database::fetch_array($res);
if ($row['nb'] > 0) {
// DO nothing
} else {
$sql = "INSERT INTO $table (c_id, question_id, category_id)
VALUES (".api_get_course_int_id().", $question_id, $category_id)";
Database::query($sql);
}
}
}
}
/**
* in this version, a question can only have 1 category
* if category is 0, then question has no category then delete the category entry.
@ -1042,9 +1004,9 @@ abstract class Question
$questionEntity = $questionRepo->find($this->id);
$exerciseEntity = $exerciseRepo->find($exerciseId);
$node = $questionRepo->addResourceNode($questionEntity, api_get_user_entity(api_get_user_id()), $exerciseEntity);
$questionRepo->addResourceToCourse(
$questionRepo->addResourceNodeToCourse(
$node,
\Chamilo\CoreBundle\Entity\Resource\ResourceLink::VISIBILITY_PUBLISHED,
ResourceLink::VISIBILITY_PUBLISHED,
api_get_course_entity(),
api_get_session_entity(),
api_get_group_entity()
@ -1674,7 +1636,8 @@ abstract class Question
);
// Categories
$tabCat = TestCategory::getCategoriesIdAndName();
$tabCat = TestCategory::getCategoriesForSelect();
$form->addElement(
'select',
'questionCategory',

@ -394,7 +394,7 @@ if ($course_id_changed) {
}
// Get category list for the course $selected_course
$categoryList = TestCategory::getCategoriesIdAndName($selected_course);
$categoryList = TestCategory::getCategoriesForSelect();
$selectCourseCategory = Display::select(
'courseCategoryId',
$categoryList,

@ -1,6 +1,7 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\Resource\ResourceLink;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CourseBundle\Entity\CExerciseCategory;
@ -76,6 +77,35 @@ class ExerciseCategoryManager extends Model
$repo->hardDelete($category);
}
/**
* @param array $params
* @param bool $showQuery
*
* @return bool
*/
public function update($params, $showQuery = false)
{
$id = $params['id'];
$repo = Container::getExerciseCategoryRepository();
/** @var CExerciseCategory $category */
$category = $repo->find($id);
if ($category) {
$category
->setName($params['name'])
->setDescription($params['description'])
;
$repo->getEntityManager()->persist($category);
$repo->getEntityManager()->flush();
return true;
}
return false;
}
/**
* Save values in the *_field_values table.
*
@ -109,7 +139,15 @@ class ExerciseCategoryManager extends Model
$category->setPosition($position);
*/
$em->persist($category);
$repo->addResourceNode($category, api_get_user_entity(api_get_user_id()), api_get_course_entity($courseId));
$repo->addResourceToCourse(
$category,
ResourceLink::VISIBILITY_PUBLISHED,
api_get_user_entity(api_get_user_id()),
api_get_course_entity($courseId),
api_get_session_entity(),
api_get_group_entity()
);
$em->flush();
return $category;

@ -1168,9 +1168,13 @@ class DocumentManager
$repo = Container::getDocumentRepository();
$document = $repo->find($docInfo['iid']);
$repo->hardDelete($document);
if ($document) {
$repo->hardDelete($document);
return true;
return true;
}
return false;
}
/**
@ -6276,7 +6280,7 @@ This folder contains all sessions that have been opened in the chat. Although th
$visibility = ResourceLink::VISIBILITY_PUBLISHED;
}
$repo->addResourceToCourse($resourceNode, $visibility, $document->getCourse(), $document->getSession(), $group);
$repo->addResourceNodeToCourse($resourceNode, $visibility, $document->getCourse(), $document->getSession(), $group);
$em->flush();
$documentId = $document->getIid();

@ -71,7 +71,7 @@ class ResourceController extends BaseController implements CourseControllerInter
if (!empty($parentId)) {
$parent = $repository->find($parentId);
}
$resources = $repository->getResourceByCourse($course, $tool, $parent);
$resources = $repository->getResourcesByCourse($course, $tool, $parent);
$source->setData($resources);
$grid->setSource($source);

@ -236,7 +236,7 @@ class ResourceNode
* Returns the path cleaned from its ids.
* Eg.: "Root/subdir/file.txt".
*
* @return
* @return string
*/
public function getPathForDisplay()
{
@ -300,8 +300,6 @@ class ResourceNode
/**
* This is required for logging the resource path at the creation.
* Do not use this function otherwise.
*
* @return type
*/
public function setPathForCreationLog($path)
{

@ -951,8 +951,9 @@ class Version20 extends AbstractMigrationChamilo
/*ALTER TABLE c_quiz CHANGE exercise_category_id exercise_category_id BIGINT DEFAULT NULL;
ALTER TABLE c_quiz ADD CONSTRAINT FK_B7A1C35FB48D66 FOREIGN KEY (exercise_category_id) REFERENCES c_exercise_category (id);
CREATE INDEX IDX_B7A1C35FB48D66 ON c_quiz (exercise_category_id);*/
CREATE INDEX IDX_B7A1C35FB48D66 ON c_quiz (exercise_category_id);
ALTER TABLE c_quiz_question_category ADD CONSTRAINT FK_1414369D91D79BD3 FOREIGN KEY (c_id) REFERENCES course (id);
*/
}
/**

@ -44,9 +44,7 @@ class IllustrationRepository extends ResourceRepository
public function getIllustrationNodeFromResource(AbstractResource $resource)
{
$nodeRepo = $this->getResourceNodeRepository();
$resourceType = $this->getRepositoryResourceType();
//var_dump($resource->getResourceNode()->getId());exit;
$resourceType = $this->getResourceType();
/** @var ResourceNode $node */
$node = $nodeRepo->findOneBy(

@ -29,8 +29,6 @@ use Symfony\Component\Routing\RouterInterface;
*/
class ResourceRepository extends EntityRepository
{
/** @var string */
public $shortClassName;
/**
* @var EntityRepository
*/
@ -73,7 +71,6 @@ class ResourceRepository extends EntityRepository
$this->fs = $mountManager->getFilesystem('resources_fs');
$this->router = $router;
$this->resourceNodeRepository = $entityManager->getRepository('ChamiloCoreBundle:Resource\ResourceNode');
$this->shortClassName = (new \ReflectionClass($className))->getShortName();
}
/**
@ -84,14 +81,6 @@ class ResourceRepository extends EntityRepository
return new $this->className();
}
/**
* @return string
*/
public function getShortClassName(): string
{
return $this->shortClassName;
}
/**
* @return EntityRepository
*/
@ -188,7 +177,7 @@ class ResourceRepository extends EntityRepository
): ResourceNode {
$em = $this->getEntityManager();
$resourceType = $this->getRepositoryResourceType();
$resourceType = $this->getResourceType();
$resourceNode = new ResourceNode();
$resourceNode
@ -210,54 +199,10 @@ class ResourceRepository extends EntityRepository
return $resourceNode;
}
/**
* @return ResourceType
*/
public function getRepositoryResourceType()
{
$em = $this->getEntityManager();
return $em->getRepository('ChamiloCoreBundle:Resource\ResourceType')->findOneBy(
['name' => $this->getShortClassName()]
);
}
/**
* @param ResourceNode $resourceNode
*
* @return ResourceLink
*/
public function addResourceToMe(ResourceNode $resourceNode): ResourceLink
{
$resourceLink = new ResourceLink();
$resourceLink
->setResourceNode($resourceNode)
->setPrivate(true);
$this->getEntityManager()->persist($resourceLink);
$this->getEntityManager()->flush();
return $resourceLink;
}
/**
* @param ResourceNode $resourceNode
* @param ResourceRight $right
*
* @return ResourceLink
*/
public function addResourceToEveryone(ResourceNode $resourceNode, ResourceRight $right): ResourceLink
public function addResourceToCourse(AbstractResource $resource, $visibility, User $creator, Course $course, $session = null, $group = null)
{
$resourceLink = new ResourceLink();
$resourceLink
->setResourceNode($resourceNode)
->addResourceRight($right)
;
$this->getEntityManager()->persist($resourceLink);
$this->getEntityManager()->flush();
return $resourceLink;
$node = $this->addResourceNode($resource, $creator, $course);
$this->addResourceNodeToCourse($node, $visibility, $course, $session, $group);
}
/**
@ -267,7 +212,7 @@ class ResourceRepository extends EntityRepository
* @param Session $session
* @param CGroupInfo $group
*/
public function addResourceToCourse(ResourceNode $resourceNode, $visibility, $course, $session, $group): void
public function addResourceNodeToCourse(ResourceNode $resourceNode, $visibility, $course, $session, $group): void
{
$visibility = (int) $visibility;
if (empty($visibility)) {
@ -309,6 +254,59 @@ class ResourceRepository extends EntityRepository
$em->flush();
}
/**
* @return ResourceType
*/
public function getResourceType()
{
$em = $this->getEntityManager();
$entityName = $this->getRepository()->getClassMetadata()->getReflectionClass()->getShortName();
return $em->getRepository('ChamiloCoreBundle:Resource\ResourceType')->findOneBy(
['name' => $entityName]
);
}
/**
* @param ResourceNode $resourceNode
*
* @return ResourceLink
*/
public function addResourceToMe(ResourceNode $resourceNode): ResourceLink
{
$resourceLink = new ResourceLink();
$resourceLink
->setResourceNode($resourceNode)
->setPrivate(true);
$this->getEntityManager()->persist($resourceLink);
$this->getEntityManager()->flush();
return $resourceLink;
}
/**
* @param ResourceNode $resourceNode
* @param ResourceRight $right
*
* @return ResourceLink
*/
public function addResourceToEveryone(ResourceNode $resourceNode, ResourceRight $right): ResourceLink
{
$resourceLink = new ResourceLink();
$resourceLink
->setResourceNode($resourceNode)
->addResourceRight($right)
;
$this->getEntityManager()->persist($resourceLink);
$this->getEntityManager()->flush();
return $resourceLink;
}
/**
* @param ResourceNode $resourceNode
* @param Course $course
@ -365,7 +363,6 @@ class ResourceRepository extends EntityRepository
* @param Session $session
* @param ResourceRight $right
*
* @return ResourceLink
*/
public function addResourceToSession(
ResourceNode $resourceNode,
@ -449,44 +446,75 @@ class ResourceRepository extends EntityRepository
}
/**
* @param Course $course
* @param Tool $tool
* @param AbstractResource $parent
* @param Course $course
* @param Session|null $session
* @param CGroupInfo|null $group
*
* @return ResourceLink
* @return array
*/
public function getResourceByCourse(Course $course, Tool $tool, AbstractResource $parent = null)
public function getResourcesByCourse(Course $course, Session $session = null, CGroupInfo $group = null)
{
$repo = $this->getRepository();
$className = $repo->getClassName();
// Check if this resource type requires to load the base course resources when using a session
$loadBaseSessionContent = $repo->getClassMetadata()->getReflectionClass()->hasProperty(
'loadBaseCourseResourcesFromSession'
);
$type = $this->getResourceType();
$query = $this->getEntityManager()->createQueryBuilder()
->select('resource')
->from('Chamilo\CoreBundle\Entity\Resource\ResourceNode', 'node')
->innerJoin('node.links', 'links')
->from(ResourceNode::class, 'node')
->innerJoin('node.resourceLinks', 'links')
->innerJoin(
$this->getClassName(),
$className,
'resource',
Join::WITH,
'resource.course = links.course AND resource.resourceNode = node.id'
)
->where('node.tool = :tool')
->where('node.resourceType = :type')
->andWhere('links.course = :course')
//->where('link.cId = ?', $course->getId())
//->where('node.cId = 0')
//->orderBy('node');
->setParameters(
[
'tool' => $tool,
'type' => $type,
'course' => $course,
]
);
if ($parent !== null) {
if ($session === null) {
$query->andWhere('links.session IS NULL');
} else {
if ($loadBaseSessionContent) {
$query->andWhere('links.session = :session OR links.session IS NULL');
$query->setParameter('session', $session);
} else {
$query->andWhere('links.session = :session');
$query->setParameter('session', $session);
}
}
if ($group === null) {
$query->andWhere('links.group IS NULL');
}
/*if ($parent !== null) {
$query->andWhere('node.parent = :parentId');
$query->setParameter('parentId', $parent->getResourceNode()->getId());
} else {
$query->andWhere('node.parent IS NULL');
}
$query->andWhere('node.parent = :parentId');
$query->setParameter('parentId', $course->getResourceNode());
}*/
/*$query->setFirstResult();
$query->setMaxResults();
$query->orderBy();*/
$query = $query->getQuery();
//var_dump($query->getSQL());
/*$query = $this->getEntityManager()->createQueryBuilder()
->select('notebook')
@ -503,7 +531,6 @@ class ResourceRepository extends EntityRepository
)
->getQuery()
;*/
return $query->getResult();
}
@ -512,7 +539,7 @@ class ResourceRepository extends EntityRepository
*
* @return Tool|null
*/
public function getTool($tool)
private function getTool($tool)
{
return $this
->getEntityManager()
@ -550,7 +577,6 @@ class ResourceRepository extends EntityRepository
/** @var ResourceLink $link */
foreach ($links as $link) {
$link->setVisibility($visibility);
if ($visibility === ResourceLink::VISIBILITY_DRAFT) {
$editorMask = ResourceNodeVoter::getEditorMask();
$rights = [];
@ -575,11 +601,10 @@ class ResourceRepository extends EntityRepository
}
/**
* Deletes the AbstractResource (CDocument, CQuiz), ResourceNode,
* ResourceLinks and ResourceFile as well as deletes the files phisically via Flysystem.
* Deletes several entities: AbstractResource (Ex: CDocument, CQuiz), ResourceNode,
* ResourceLinks and ResourceFile (including files via Flysystem)
*
* @param AbstractResource $resource
*
*/
public function hardDelete(AbstractResource $resource)
{

@ -1079,7 +1079,8 @@ class CourseBuilder
$idList = []
) {
// get all test category in course
$categories = TestCategory::getCategoryListInfo('', $courseId);
$category = new TestCategory();
$categories = $category->getCategories();
foreach ($categories as $category) {
$this->findAndSetDocumentsInText($category->description);

@ -5,6 +5,8 @@ namespace Chamilo\CourseBundle\Entity;
use Chamilo\CoreBundle\Entity\Resource\AbstractResource;
use Chamilo\CoreBundle\Entity\Resource\ResourceInterface;
use Chamilo\CourseBundle\Traits\SessionCourseResources;
use Chamilo\CourseBundle\Traits\SessionCourseResourcesTrait;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM;
@ -21,6 +23,8 @@ use Doctrine\ORM\Mapping as ORM;
*/
class CQuizQuestionCategory extends AbstractResource implements ResourceInterface
{
use SessionCourseResourcesTrait;
/**
* @var int
*
@ -30,13 +34,6 @@ class CQuizQuestionCategory extends AbstractResource implements ResourceInterfac
*/
protected $iid;
/**
* @var int
*
* @ORM\Column(name="c_id", type="integer")
*/
protected $cId;
/**
* @var int
*
@ -58,6 +55,12 @@ class CQuizQuestionCategory extends AbstractResource implements ResourceInterfac
*/
protected $description;
/**
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course")
* @ORM\JoinColumn(name="c_id", referencedColumnName="id", nullable=false)
*/
protected $course;
/**
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session", cascade={"persist"})
* @ORM\JoinColumn(name="session_id", referencedColumnName="id", nullable=true)
@ -145,27 +148,43 @@ class CQuizQuestionCategory extends AbstractResource implements ResourceInterfac
}
/**
* Set cId.
*
* @param int $cId
* @return mixed
*/
public function getCourse()
{
return $this->course;
}
/**
* @param mixed $course
*
* @return CQuizQuestionCategory
*/
public function setCId($cId)
public function setCourse($course)
{
$this->cId = $cId;
$this->course = $course;
return $this;
}
/**
* Get cId.
* @return mixed
*/
public function getSession()
{
return $this->session;
}
/**
* @param mixed $session
*
* @return int
* @return CQuizQuestionCategory
*/
public function getCId()
public function setSession($session)
{
return $this->cId;
$this->session = $session;
return $this;
}
/**

@ -0,0 +1,20 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\CourseBundle\Traits;
/**
* Trait SessionCourseResourcesTrait.
*/
trait SessionCourseResourcesTrait
{
protected $loadBaseCourseResourcesFromSession = true;
/**
* @return bool
*/
public function isLoadBaseCourseResourcesFromSession(): bool
{
return $this->loadBaseCourseResourcesFromSession;
}
}
Loading…
Cancel
Save