Internal: Fix course categories

pull/3464/head
Julio Montoya 4 years ago
parent 0d12296e6a
commit aa32ed97cd
  1. 75
      public/main/inc/lib/add_course.lib.inc.php
  2. 2
      src/CoreBundle/Entity/Course.php
  3. 2
      src/CoreBundle/Entity/CourseRelUser.php
  4. 32
      src/CoreBundle/Entity/User.php
  5. 20
      src/CoreBundle/Migrations/Schema/V200/Version20200821224242.php
  6. 5
      src/CoreBundle/Repository/CourseCategoryRepository.php

@ -1,6 +1,8 @@
<?php <?php
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\CourseRelUser;
use Chamilo\CoreBundle\Framework\Container; use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CourseBundle\Entity\CGroupCategory; use Chamilo\CourseBundle\Entity\CGroupCategory;
use Chamilo\CourseBundle\Entity\CToolIntro; use Chamilo\CourseBundle\Entity\CToolIntro;
@ -733,11 +735,19 @@ class AddCourse
} }
$course_id = 0; $course_id = 0;
$userId = empty($params['user_id']) ? api_get_user_id() : (int) $params['user_id'];
$user = api_get_user_entity($userId);
if (null === $user) {
error_log(sprintf('user_id "%s" is invalid', $userId));
return 0;
}
if ($ok_to_register_course) { if ($ok_to_register_course) {
$repo = Container::getCourseRepository(); $repo = Container::getCourseRepository();
$categoryRepo = Container::getCourseCategoryRepository(); $categoryRepo = Container::getCourseCategoryRepository();
$course = new \Chamilo\CoreBundle\Entity\Course(); $course = new Course();
$course $course
->setCode($code) ->setCode($code)
->setDirectory($directory) ->setDirectory($directory)
@ -781,44 +791,46 @@ class AddCourse
// Default true // Default true
$addTeacher = isset($params['add_user_as_teacher']) ? $params['add_user_as_teacher'] : true; $addTeacher = isset($params['add_user_as_teacher']) ? $params['add_user_as_teacher'] : true;
if ($addTeacher) { if ($addTeacher) {
$i_course_sort = CourseManager::userCourseSort( $iCourseSort = CourseManager::userCourseSort($userId, $code);
$user_id, $courseRelTutor = (new CourseRelUser())
$code ->setCourse($course)
); ->setUser($user)
if (!empty($user_id)) { ->setStatus(true)
$sql = "INSERT INTO ".$TABLECOURSUSER." SET ->setTutor(true)
c_id = '".$course_id."', ->setSort($iCourseSort)
user_id = '".intval($user_id)."', ->setRelationType(0)
status = '1', ->setUserCourseCat(0)
is_tutor = '0', ;
sort = '".($i_course_sort)."', Database::getManager()->persist($courseRelTutor);
relation_type = 0,
user_course_cat = '0'";
Database::query($sql);
}
} }
if (!empty($teachers)) { if (!empty($teachers)) {
$sort = $user->getMaxSortValue();
if (!is_array($teachers)) { if (!is_array($teachers)) {
$teachers = [$teachers]; $teachers = [$teachers];
} }
foreach ($teachers as $key) { foreach ($teachers as $key) {
//just in case // Just in case.
if ($key == $user_id) { if ($key == $userId) {
continue; continue;
} }
if (empty($key)) { if (empty($key)) {
continue; continue;
} }
$sql = "INSERT INTO " . $TABLECOURSUSER . " SET $teacher = api_get_user_entity($key);
c_id = '" . Database::escape_string($course_id) . "', if (is_null($teacher)) {
user_id = '" . Database::escape_string($key) . "', continue;
status = '1', }
is_tutor = '0', $courseRelTeacher = (new CourseRelUser())
sort = '" . ($sort + 1) . "', ->setCourse($course)
relation_type = 0, ->setUser($teacher)
user_course_cat = '0'"; ->setStatus(true)
Database::query($sql); ->setTutor(false)
->setSort($sort + 1)
->setRelationType(0)
->setUserCourseCat(0)
;
Database::getManager()->persist($courseRelTeacher);
} }
} }
@ -857,10 +869,11 @@ class AddCourse
).' '.$siteName.' - '.$iname."\n"; ).' '.$siteName.' - '.$iname."\n";
$message .= get_lang('Course name').' '.$title."\n"; $message .= get_lang('Course name').' '.$title."\n";
if ($courseCategory) { if ($course->getCategories()->count() > 0) {
$message .= get_lang( foreach ($course->getCategories() as $category) {
'Category' $message .= get_lang('Category').': '.$category->getCode()."\n";
).' '.$courseCategory->getCode()."\n"; }
} }
$message .= get_lang('Coach').' '.$tutor_name."\n"; $message .= get_lang('Coach').' '.$tutor_name."\n";
$message .= get_lang('Language').' '.$course_language; $message .= get_lang('Language').' '.$course_language;

@ -761,8 +761,6 @@ class Course extends AbstractResource implements ResourceInterface, ResourceWith
/** /**
* Set category. * Set category.
* *
* @param ArrayCollection $categories
*
* @return Course * @return Course
*/ */
public function setCategories(ArrayCollection $categories): self public function setCategories(ArrayCollection $categories): self

@ -261,6 +261,8 @@ class CourseRelUser
public function setTutor($tutor) public function setTutor($tutor)
{ {
$this->tutor = $tutor; $this->tutor = $tutor;
return $this;
} }
/** /**

@ -2275,4 +2275,36 @@ class User implements UserInterface, EquatableInterface
return $sessions; 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()
);
}
} }

@ -1,27 +1,22 @@
<?php <?php
declare(strict_types=1); namespace Chamilo\CoreBundle\Migrations\Schema\V200;
namespace DoctrineMigrations;
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/** /**
* Auto-generated Migration: Please modify to your needs! * Auto-generated Migration: Please modify to your needs!
*/ */
final class Version20200821224242 extends AbstractMigration final class Version20200821224242 extends AbstractMigrationChamilo
{ {
public function getDescription() : string public function getDescription(): string
{ {
return ''; return '';
} }
public function up(Schema $schema) : void public function up(Schema $schema): void
{ {
// this up() 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 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('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_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'); $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'); $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('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('DROP TABLE course_rel_category');
$this->addSql('ALTER TABLE course ADD category_id INT DEFAULT NULL'); $this->addSql('ALTER TABLE course ADD category_id INT DEFAULT NULL');

@ -7,9 +7,9 @@ namespace Chamilo\CoreBundle\Repository;
use Chamilo\CoreBundle\Entity\Course; use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\CourseCategory; use Chamilo\CoreBundle\Entity\CourseCategory;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Query\Expr\Join; use Doctrine\ORM\Query\Expr\Join;
use Doctrine\Common\Collections\ArrayCollection;
/** /**
* Class CCourseCategoryRepository. * Class CCourseCategoryRepository.
@ -59,7 +59,7 @@ class CourseCategoryRepository extends ServiceEntityRepository
* Get all categories in an access url and course id. * Get all categories in an access url and course id.
* *
* @param int $accessUrl * @param int $accessUrl
* @param int $courseId * @param int $courseId
* @param bool $allowBaseCategories * @param bool $allowBaseCategories
* *
* @return array * @return array
@ -125,7 +125,6 @@ class CourseCategoryRepository extends ServiceEntityRepository
$em->persist($course); $em->persist($course);
$em->flush(); $em->flush();
// Add new categories // Add new categories
$courseCategories = new ArrayCollection(); $courseCategories = new ArrayCollection();

Loading…
Cancel
Save