diff --git a/public/main/inc/lib/add_course.lib.inc.php b/public/main/inc/lib/add_course.lib.inc.php index 18a46d7b70..0144243577 100644 --- a/public/main/inc/lib/add_course.lib.inc.php +++ b/public/main/inc/lib/add_course.lib.inc.php @@ -1,6 +1,8 @@ setCode($code) ->setDirectory($directory) @@ -781,44 +791,46 @@ class AddCourse // Default true $addTeacher = isset($params['add_user_as_teacher']) ? $params['add_user_as_teacher'] : true; if ($addTeacher) { - $i_course_sort = CourseManager::userCourseSort( - $user_id, - $code - ); - if (!empty($user_id)) { - $sql = "INSERT INTO ".$TABLECOURSUSER." SET - c_id = '".$course_id."', - user_id = '".intval($user_id)."', - status = '1', - is_tutor = '0', - sort = '".($i_course_sort)."', - relation_type = 0, - user_course_cat = '0'"; - Database::query($sql); - } + $iCourseSort = CourseManager::userCourseSort($userId, $code); + $courseRelTutor = (new CourseRelUser()) + ->setCourse($course) + ->setUser($user) + ->setStatus(true) + ->setTutor(true) + ->setSort($iCourseSort) + ->setRelationType(0) + ->setUserCourseCat(0) + ; + Database::getManager()->persist($courseRelTutor); } if (!empty($teachers)) { + $sort = $user->getMaxSortValue(); if (!is_array($teachers)) { $teachers = [$teachers]; } foreach ($teachers as $key) { - //just in case - if ($key == $user_id) { + // Just in case. + if ($key == $userId) { continue; } if (empty($key)) { continue; } - $sql = "INSERT INTO " . $TABLECOURSUSER . " SET - c_id = '" . Database::escape_string($course_id) . "', - user_id = '" . Database::escape_string($key) . "', - status = '1', - is_tutor = '0', - sort = '" . ($sort + 1) . "', - relation_type = 0, - user_course_cat = '0'"; - Database::query($sql); + $teacher = api_get_user_entity($key); + if (is_null($teacher)) { + continue; + } + $courseRelTeacher = (new CourseRelUser()) + ->setCourse($course) + ->setUser($teacher) + ->setStatus(true) + ->setTutor(false) + ->setSort($sort + 1) + ->setRelationType(0) + ->setUserCourseCat(0) + ; + Database::getManager()->persist($courseRelTeacher); } } @@ -857,10 +869,11 @@ class AddCourse ).' '.$siteName.' - '.$iname."\n"; $message .= get_lang('Course name').' '.$title."\n"; - if ($courseCategory) { - $message .= get_lang( - 'Category' - ).' '.$courseCategory->getCode()."\n"; + if ($course->getCategories()->count() > 0) { + foreach ($course->getCategories() as $category) { + $message .= get_lang('Category').': '.$category->getCode()."\n"; + } + } $message .= get_lang('Coach').' '.$tutor_name."\n"; $message .= get_lang('Language').' '.$course_language; diff --git a/src/CoreBundle/Entity/Course.php b/src/CoreBundle/Entity/Course.php index 71597297b2..7e62ef021f 100644 --- a/src/CoreBundle/Entity/Course.php +++ b/src/CoreBundle/Entity/Course.php @@ -761,8 +761,6 @@ class Course extends AbstractResource implements ResourceInterface, ResourceWith /** * Set category. * - * @param ArrayCollection $categories - * * @return Course */ public function setCategories(ArrayCollection $categories): self diff --git a/src/CoreBundle/Entity/CourseRelUser.php b/src/CoreBundle/Entity/CourseRelUser.php index 29698c70e5..03a0821f75 100644 --- a/src/CoreBundle/Entity/CourseRelUser.php +++ b/src/CoreBundle/Entity/CourseRelUser.php @@ -261,6 +261,8 @@ class CourseRelUser public function setTutor($tutor) { $this->tutor = $tutor; + + return $this; } /** diff --git a/src/CoreBundle/Entity/User.php b/src/CoreBundle/Entity/User.php index bcf5456edf..28e4cfa8a4 100644 --- a/src/CoreBundle/Entity/User.php +++ b/src/CoreBundle/Entity/User.php @@ -2275,4 +2275,36 @@ class User implements UserInterface, EquatableInterface return $sessions; } + + /** + * Find the largest sort value in a given UserCourseCategory + * This method is used when we are moving a course to a different category + * and also when a user subscribes to courses (the new course is added at the end of the main category). + * + * Used to be implemented in global function \api_max_sort_value. + * Reimplemented using the ORM cache. + * + * @param UserCourseCategory|null $userCourseCategory the user_course_category + * + * @return int|mixed + */ + public function getMaxSortValue($userCourseCategory = null) + { + $categoryCourses = $this->courses->matching( + Criteria::create() + ->where(Criteria::expr()->neq('relationType', COURSE_RELATION_TYPE_RRHH)) + ->andWhere(Criteria::expr()->eq('userCourseCat', $userCourseCategory)) + ); + + return $categoryCourses->isEmpty() + ? 0 + : max( + $categoryCourses->map( + /** @var CourseRelUser $courseRelUser */ + function ($courseRelUser) { + return $courseRelUser->getSort(); + } + )->toArray() + ); + } } diff --git a/src/Migrations/Version20200821224242.php b/src/CoreBundle/Migrations/Schema/V200/Version20200821224242.php similarity index 70% rename from src/Migrations/Version20200821224242.php rename to src/CoreBundle/Migrations/Schema/V200/Version20200821224242.php index 0482e33d23..28ec525c55 100644 --- a/src/Migrations/Version20200821224242.php +++ b/src/CoreBundle/Migrations/Schema/V200/Version20200821224242.php @@ -1,27 +1,22 @@ abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); - $this->addSql('CREATE TABLE course_rel_category (course_id INT NOT NULL, course_category_id INT NOT NULL, INDEX IDX_8EB34CC5591CC992 (course_id), INDEX IDX_8EB34CC56628AD36 (course_category_id), PRIMARY KEY(course_id, course_category_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC'); $this->addSql('ALTER TABLE course_rel_category ADD CONSTRAINT FK_8EB34CC5591CC992 FOREIGN KEY (course_id) REFERENCES course (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE course_rel_category ADD CONSTRAINT FK_8EB34CC56628AD36 FOREIGN KEY (course_category_id) REFERENCES course_category (id) ON DELETE CASCADE'); @@ -31,11 +26,8 @@ final class Version20200821224242 extends AbstractMigration $this->addSql('ALTER TABLE course DROP category_id'); } - public function down(Schema $schema) : void + public function down(Schema $schema): void { - // this down() migration is auto-generated, please modify it to your needs - $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); - $this->addSql('CREATE TABLE version (id INT UNSIGNED AUTO_INCREMENT NOT NULL, version VARCHAR(20) CHARACTER SET utf8mb4 DEFAULT NULL COLLATE `utf8mb4_general_ci`, UNIQUE INDEX version (version), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB COMMENT = \'\' '); $this->addSql('DROP TABLE course_rel_category'); $this->addSql('ALTER TABLE course ADD category_id INT DEFAULT NULL'); diff --git a/src/CoreBundle/Repository/CourseCategoryRepository.php b/src/CoreBundle/Repository/CourseCategoryRepository.php index 355b07c994..f3e5e0cff3 100644 --- a/src/CoreBundle/Repository/CourseCategoryRepository.php +++ b/src/CoreBundle/Repository/CourseCategoryRepository.php @@ -7,9 +7,9 @@ namespace Chamilo\CoreBundle\Repository; use Chamilo\CoreBundle\Entity\Course; use Chamilo\CoreBundle\Entity\CourseCategory; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\ORM\Query\Expr\Join; -use Doctrine\Common\Collections\ArrayCollection; /** * Class CCourseCategoryRepository. @@ -59,7 +59,7 @@ class CourseCategoryRepository extends ServiceEntityRepository * Get all categories in an access url and course id. * * @param int $accessUrl - * @param int $courseId + * @param int $courseId * @param bool $allowBaseCategories * * @return array @@ -125,7 +125,6 @@ class CourseCategoryRepository extends ServiceEntityRepository $em->persist($course); $em->flush(); - // Add new categories $courseCategories = new ArrayCollection();