Delete course using doctrine, fix Course entity relations

Add CourseTrait, to avoid rewriting course set and get
pull/2650/head^2
Julio Montoya 6 years ago
parent ac420ade4e
commit 45daf5b9d0
  1. 8
      app/Migrations/Schema/V200/Version20.php
  2. 32
      main/inc/lib/course.lib.php
  3. 4
      main/inc/lib/urlmanager.lib.php
  4. 23
      src/CoreBundle/Entity/AccessUrl.php
  5. 19
      src/CoreBundle/Entity/AccessUrlRelCourse.php
  6. 29
      src/CoreBundle/Entity/Course.php
  7. 27
      src/CoreBundle/Entity/GradebookEvaluation.php
  8. 27
      src/CoreBundle/Entity/GradebookLink.php
  9. 28
      src/CoreBundle/Entity/TrackEAttempt.php
  10. 34
      src/CoreBundle/Entity/TrackEHotspot.php
  11. 33
      src/CoreBundle/Traits/CourseTrait.php
  12. 10
      src/CourseBundle/Entity/CGroupInfo.php

@ -668,9 +668,17 @@ class Version20 extends AbstractMigrationChamilo
$this->addSql('ALTER TABLE gradebook_result_log CHANGE id_result result_id INT NOT NULL');
$this->addSql('ALTER TABLE c_group_info CHANGE category_id category_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE c_quiz_question_category ADD session_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE c_quiz_question_category ADD CONSTRAINT FK_1414369D613FECDF FOREIGN KEY (session_id) REFERENCES session (id)');
$this->addSql('ALTER TABLE track_e_attempt CHANGE c_id c_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE track_e_attempt ADD CONSTRAINT FK_F8C342C391D79BD3 FOREIGN KEY (c_id) REFERENCES course (id)');
$this->addSql('ALTER TABLE track_e_hotspot ADD CONSTRAINT FK_A89CC3B691D79BD3 FOREIGN KEY (c_id) REFERENCES course (id)');
$this->addSql('CREATE INDEX IDX_A89CC3B691D79BD3 ON track_e_hotspot (c_id)');
// Drop unused columns
$dropColumnsAndIndex = [
'track_e_uploads' => ['columns' => ['upload_cours_id'], 'index' => ['upload_cours_id']],

@ -2274,17 +2274,14 @@ class CourseManager
return false;
}
$codeFiltered = Database::escape_string($code);
$sql = "SELECT * FROM $table_course
WHERE code = '$codeFiltered'";
$res = Database::query($sql);
$course = api_get_course_info($code);
if (Database::num_rows($res) == 0) {
return;
if (empty($course)) {
return false;
}
$course = Database::fetch_array($res);
$courseId = $course['id']; // int
$codeFiltered = $course['code'];
$courseId = $course['real_id']; // int
$count = 0;
if (api_is_multiple_url_enabled()) {
@ -2296,7 +2293,7 @@ class CourseManager
$count = UrlManager::getCountUrlRelCourse($courseId);
}
if ($count == 0) {
if ($count === 0) {
self::create_database_dump($code);
$course_tables = AddCourse::get_course_tables();
@ -2404,23 +2401,28 @@ class CourseManager
WHERE course_id = $courseId";
Database::query($sql);
if (api_get_configuration_value('allow_skill_rel_items')) {
$sql = "DELETE FROM skill_rel_course WHERE c_id = $courseId";
Database::query($sql);
}
$sql = "DELETE FROM skill_rel_course WHERE c_id = $courseId";
Database::query($sql);
// Deletes all groups, group-users, group-tutors information
// To prevent fK mix up on some tables
GroupManager::deleteAllGroupsFromCourse($courseId);
// Delete the course from the database
$sql = "DELETE FROM $table_course WHERE id = $courseId";
Database::query($sql);
/*$sql = "DELETE FROM $table_course WHERE id = $courseId";
Database::query($sql);*/
$courseEntity = api_get_course_entity($courseId);
Database::getManager()->remove($courseEntity);
Database::getManager()->flush();
// delete extra course fields
$extraFieldValues = new ExtraFieldValue('course');
$extraFieldValues->deleteValuesByItem($courseId);
// Add event to system log
Event::addEvent(
LOG_COURSE_DELETE,

@ -280,14 +280,14 @@ class UrlManager
*/
public static function getCountUrlRelCourse($courseId)
{
$courseId = intval($courseId);
$courseId = (int) $courseId;
$tableUrlRelCourse = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
$sql = "SELECT *
FROM $tableUrlRelCourse
WHERE c_id = '$courseId'";
$res = Database::query($sql);
return Database::num_rows($res);
return (int) Database::num_rows($res);
}
/**

@ -3,6 +3,7 @@
namespace Chamilo\CoreBundle\Entity;
use Chamilo\CoreBundle\Traits\CourseTrait;
use Doctrine\ORM\Mapping as ORM;
/**
@ -13,6 +14,8 @@ use Doctrine\ORM\Mapping as ORM;
*/
class AccessUrl
{
use CourseTrait;
/**
* @var int
*
@ -429,26 +432,6 @@ class AccessUrl
return $this;
}
/**
* @return mixed
*/
public function getCourse()
{
return $this->course;
}
/**
* @param mixed $course
*
* @return AccessUrl
*/
public function setCourse($course)
{
$this->course = $course;
return $this;
}
/**
* @return mixed
*/

@ -3,6 +3,7 @@
namespace Chamilo\CoreBundle\Entity;
use Chamilo\CoreBundle\Traits\CourseTrait;
use Doctrine\ORM\Mapping as ORM;
/**
@ -13,6 +14,8 @@ use Doctrine\ORM\Mapping as ORM;
*/
class AccessUrlRelCourse
{
use CourseTrait;
/**
* @var int
*
@ -73,20 +76,4 @@ class AccessUrlRelCourse
{
return $this->url;
}
/**
* @param Course $course
*/
public function setCourse(Course $course)
{
$this->course = $course;
}
/**
* @return Course
*/
public function getCourse()
{
return $this->course;
}
}

@ -27,6 +27,7 @@ use Symfony\Component\Validator\Constraints as Assert;
* @UniqueEntity("code")
* @UniqueEntity("visualCode")
* @UniqueEntity("directory")
*
* @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Repository\CourseRepository")
* @ORM\EntityListeners({"Chamilo\CoreBundle\Entity\Listener\CourseListener"})
*/
@ -66,22 +67,22 @@ class Course
protected $users;
/**
* @ORM\OneToMany(targetEntity="AccessUrlRelCourse", mappedBy="course", cascade={"persist"}, orphanRemoval=true)
* @ORM\OneToMany(targetEntity="AccessUrlRelCourse", mappedBy="course", cascade={"persist", "remove"}, orphanRemoval=true)
*/
protected $urls;
/**
* @ORM\OneToMany(targetEntity="SessionRelCourse", mappedBy="course", cascade={"persist"})
* @ORM\OneToMany(targetEntity="SessionRelCourse", mappedBy="course", cascade={"persist", "remove"})
*/
protected $sessions;
/**
* @ORM\OneToMany(targetEntity="SessionRelCourseRelUser", mappedBy="course", cascade={"persist"})
* @ORM\OneToMany(targetEntity="SessionRelCourseRelUser", mappedBy="course", cascade={"persist", "remove"})
*/
protected $sessionUserSubscriptions;
/**
* @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CGroupInfo", mappedBy="course", cascade={"persist"}, orphanRemoval=true)
* @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CGroupInfo", mappedBy="course", cascade={"persist", "remove"}, orphanRemoval=true)
*/
protected $groups;
@ -91,7 +92,7 @@ class Course
//protected $items;
/**
* @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CTool", mappedBy="course", cascade={"persist"})
* @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CTool", mappedBy="course", cascade={"persist", "remove"})
*/
protected $tools;
@ -106,42 +107,42 @@ class Course
protected $currentUrl;
/**
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\SkillRelUser", mappedBy="course", cascade={"persist"})
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\SkillRelUser", mappedBy="course", cascade={"persist", "remove"})
*/
protected $issuedSkills;
/**
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\GradebookCategory", mappedBy="course")
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\GradebookCategory", mappedBy="course", cascade={"persist", "remove"})
*/
protected $gradebookCategories;
/**
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\GradebookEvaluation", mappedBy="course")
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\GradebookEvaluation", mappedBy="course", cascade={"persist", "remove"})
*/
protected $gradebookEvaluations;
/**
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\GradebookLink", mappedBy="course")
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\GradebookLink", mappedBy="course", cascade={"persist", "remove"})
*/
protected $gradebookLinks;
/**
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\TrackEHotspot", mappedBy="course")
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\TrackEHotspot", mappedBy="course", cascade={"persist", "remove"})
*/
protected $trackEHotspots;
/**
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\TrackEAttempt", mappedBy="course")
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\TrackEAttempt", mappedBy="course", cascade={"persist", "remove"})
*/
protected $trackEAttempts;
/**
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\SearchEngineRef", mappedBy="course")
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\SearchEngineRef", mappedBy="course", cascade={"persist", "remove"})
*/
protected $searchEngineRefs;
/**
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\Templates", mappedBy="course")
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\Templates", mappedBy="course", cascade={"persist", "remove"})
*/
protected $templates;
@ -234,7 +235,9 @@ class Course
/**
* @var string
*
* @Assert\Url()
*
* @ORM\Column(name="department_url", type="string", length=180, nullable=true, unique=false)
*/
protected $departmentUrl;

@ -3,6 +3,7 @@
namespace Chamilo\CoreBundle\Entity;
use Chamilo\CoreBundle\Traits\CourseTrait;
use Doctrine\ORM\Mapping as ORM;
/**
@ -13,6 +14,8 @@ use Doctrine\ORM\Mapping as ORM;
*/
class GradebookEvaluation
{
use CourseTrait;
/**
* @var int
*
@ -170,30 +173,6 @@ class GradebookEvaluation
return $this->userId;
}
/**
* Set course.
*
* @param \Chamilo\CoreBundle\Entity\Course|null $course
*
* @return \Chamilo\CoreBundle\Entity\GradebookEvaluation
*/
public function setCourse($course)
{
$this->course = $course;
return $this;
}
/**
* Get course.
*
* @return \Chamilo\CoreBundle\Entity\Course
*/
public function getCourse()
{
return $this->course;
}
/**
* Set categoryId.
*

@ -3,6 +3,7 @@
namespace Chamilo\CoreBundle\Entity;
use Chamilo\CoreBundle\Traits\CourseTrait;
use Doctrine\ORM\Mapping as ORM;
/**
@ -13,6 +14,8 @@ use Doctrine\ORM\Mapping as ORM;
*/
class GradebookLink
{
use CourseTrait;
/**
* @var int
*
@ -156,30 +159,6 @@ class GradebookLink
return $this->userId;
}
/**
* Set course.
*
* @param \Chamilo\CoreBundle\Entity\Course $course
*
* @return \Chamilo\CoreBundle\Entity\GradebookLink
*/
public function setCourse(Course $course)
{
$this->course = $course;
return $this;
}
/**
* Get course.
*
* @return \Chamilo\CoreBundle\Entity\Course
*/
public function getCourse()
{
return $this->course;
}
/**
* Set categoryId.
*

@ -3,6 +3,7 @@
namespace Chamilo\CoreBundle\Entity;
use Chamilo\CoreBundle\Traits\CourseTrait;
use Doctrine\ORM\Mapping as ORM;
/**
@ -22,6 +23,17 @@ use Doctrine\ORM\Mapping as ORM;
*/
class TrackEAttempt
{
use CourseTrait;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var int
*
@ -64,13 +76,6 @@ class TrackEAttempt
*/
protected $marks;
/**
* @var int
*
* @ORM\Column(name="c_id", type="integer", nullable=false)
*/
protected $cId;
/**
* @var int
*
@ -100,13 +105,10 @@ class TrackEAttempt
protected $filename;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course")
* @ORM\JoinColumn(name="c_id", referencedColumnName="id")
*/
protected $id;
protected $course;
/**
* Set exeId.

@ -3,6 +3,7 @@
namespace Chamilo\CoreBundle\Entity;
use Chamilo\CoreBundle\Traits\CourseTrait;
use Doctrine\ORM\Mapping as ORM;
/**
@ -17,6 +18,8 @@ use Doctrine\ORM\Mapping as ORM;
*/
class TrackEHotspot
{
use CourseTrait;
/**
* @var int
*
@ -34,11 +37,10 @@ class TrackEHotspot
protected $hotspotUserId;
/**
* @var int
*
* @ORM\Column(name="c_id", type="integer", nullable=true)
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course")
* @ORM\JoinColumn(name="c_id", referencedColumnName="id")
*/
protected $cId;
protected $course;
/**
* @var int
@ -99,30 +101,6 @@ class TrackEHotspot
return $this->hotspotUserId;
}
/**
* Set cId.
*
* @param int $cId
*
* @return TrackEHotspot
*/
public function setCId($cId)
{
$this->cId = $cId;
return $this;
}
/**
* Get cId.
*
* @return int
*/
public function getCId()
{
return $this->cId;
}
/**
* Set hotspotExeId.
*

@ -0,0 +1,33 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Traits;
use Chamilo\CoreBundle\Entity\Course;
/**
* Trait CourseTrait
*/
trait CourseTrait
{
/**
* @return Course
*/
public function getCourse()
{
return $this->course;
}
/**
* @param Course $course
*
* @return $this
*/
public function setCourse(Course $course): self
{
$this->course = $course;
return $this;
}
}

@ -36,13 +36,6 @@ class CGroupInfo
*/
protected $id;
/**
* @var int
*
* @ORM\Column(name="c_id", type="integer")
*/
protected $cId;
/**
* @var string
*
@ -60,7 +53,7 @@ class CGroupInfo
/**
* @var int
*
* @ORM\Column(name="category_id", type="integer", nullable=false)
* @ORM\Column(name="category_id", type="integer", nullable=true)
*/
protected $categoryId;
@ -171,6 +164,7 @@ class CGroupInfo
/**
* @var Course
*
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", inversedBy="courses", cascade={"persist"})
* @ORM\JoinColumn(name="c_id", referencedColumnName="id", nullable=false)
*/

Loading…
Cancel
Save