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
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\CourseRelUser;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CourseBundle\Entity\CGroupCategory;
use Chamilo\CourseBundle\Entity\CToolIntro;
@ -733,11 +735,19 @@ class AddCourse
}
$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) {
$repo = Container::getCourseRepository();
$categoryRepo = Container::getCourseCategoryRepository();
$course = new \Chamilo\CoreBundle\Entity\Course();
$course = new Course();
$course
->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;

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

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

@ -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()
);
}
}

@ -1,27 +1,22 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* 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 '';
}
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('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');

@ -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();

Loading…
Cancel
Save