Browse Source

Minor - Remove api calls in Entities, moved into a lib

- Replace private with protected
Julio Montoya 6 years ago
parent
commit
56ae329065

+ 4 - 6
main/badge/issued.php

@@ -3,6 +3,7 @@
 
 use Chamilo\CoreBundle\Entity\SkillRelUser;
 use Chamilo\CoreBundle\Entity\SkillRelUserComment;
+use \SkillRelUser as SkillRelUserManager;
 
 /**
  * Show information about the issued badge.
@@ -107,7 +108,7 @@ $skillIssueInfo = [
     'skill_short_code' => $skillIssue->getSkill()->getShortCode(),
     'skill_description' => $skillIssue->getSkill()->getDescription(),
     'skill_criteria' => $skillIssue->getSkill()->getCriteria(),
-    'badge_assertion' => $skillIssue->getAssertionUrl(),
+    'badge_assertion' => SkillRelUserManager::getAssertionUrl($skillIssue),
     'comments' => [],
     'feedback_average' => $skillIssue->getAverage(),
 ];
@@ -120,7 +121,6 @@ $skillId = $skillIssueInfo['skill_id'];
 /** @var SkillRelUserComment $comment */
 foreach ($skillIssueComments as $comment) {
     $commentDate = api_get_local_time($comment->getFeedbackDateTime());
-
     $skillIssueInfo['comments'][] = [
         'text' => $comment->getFeedbackText(),
         'value' => $comment->getFeedbackValue(),
@@ -157,7 +157,6 @@ if (!$profile) {
 
 if ($profile) {
     $profileId = $profile->getId();
-
     $levels = $skillLevelRepo->findBy([
         'profile' => $profileId,
     ]);
@@ -168,7 +167,6 @@ if ($profile) {
     }
 
     ksort($profileLevels); // Sort the array by Position.
-
     foreach ($profileLevels as $profileLevel) {
         $profileId = key($profileLevel);
         $acquiredLevel[$profileId] = $profileLevel[$profileId];
@@ -193,7 +191,7 @@ if ($showLevels && $allowToEdit) {
         $entityManager->flush();
         Display::addFlash(Display::return_message(get_lang('Saved')));
 
-        header("Location: ".$skillIssue->getIssueUrl());
+        header('Location: '.SkillRelUserManager::getIssueUrl($skillIssue));
         exit;
     }
 }
@@ -226,7 +224,7 @@ if ($form->validate() && $allowComment && $allowToEdit) {
     $entityManager->flush();
     Display::addFlash(Display::return_message(get_lang('Added')));
 
-    header("Location: ".$skillIssue->getIssueUrl());
+    header('Location: '.SkillRelUserManager::getIssueUrl($skillIssue));
     exit;
 }
 

+ 12 - 9
main/badge/issued_all.php

@@ -4,6 +4,7 @@
 use Chamilo\CoreBundle\Entity\SkillRelUser;
 use Chamilo\CoreBundle\Entity\SkillRelUserComment;
 use Chamilo\UserBundle\Entity\User;
+use \SkillRelUser as SkillRelUserManager;
 
 /**
  * Show information about all issued badges with same skill by user.
@@ -14,8 +15,8 @@ use Chamilo\UserBundle\Entity\User;
  */
 require_once __DIR__.'/../inc/global.inc.php';
 
-$userId = isset($_GET['user']) ? intval($_GET['user']) : 0;
-$skillId = isset($_GET['skill']) ? intval($_GET['skill']) : 0;
+$userId = isset($_GET['user']) ? (int) $_GET['user'] : 0;
+$skillId = isset($_GET['skill']) ? (int) $_GET['skill'] : 0;
 
 if (!$userId || !$skillId) {
     api_not_allowed(true);
@@ -26,7 +27,6 @@ Skill::isAllowed($userId);
 $em = Database::getManager();
 $user = api_get_user_entity($userId);
 $skill = $em->find('ChamiloCoreBundle:Skill', $skillId);
-
 $currentUserId = api_get_user_id();
 
 if (!$user || !$skill) {
@@ -80,7 +80,10 @@ foreach ($userSkills as $index => $skillIssue) {
         'datetime' => api_format_date($skillIssueDate, DATE_TIME_FORMAT_SHORT),
         'acquired_level' => $currentSkillLevel,
         'argumentation_author_id' => $skillIssue->getArgumentationAuthorId(),
-        'argumentation_author_name' => api_get_person_name($argumentationAuthor['firstname'], $argumentationAuthor['lastname']),
+        'argumentation_author_name' => api_get_person_name(
+            $argumentationAuthor['firstname'],
+            $argumentationAuthor['lastname']
+        ),
         'argumentation' => $skillIssue->getArgumentation(),
         'source_name' => $skillIssue->getSourceName(),
         'user_id' => $skillIssue->getUser()->getId(),
@@ -91,7 +94,7 @@ foreach ($userSkills as $index => $skillIssue) {
         'skill_short_code' => $skillIssue->getSkill()->getShortCode(),
         'skill_description' => $skillIssue->getSkill()->getDescription(),
         'skill_criteria' => $skillIssue->getSkill()->getCriteria(),
-        'badge_assertion' => $skillIssue->getAssertionUrl(),
+        'badge_assertion' => SkillRelUserManager::getAssertionUrl($skillIssue),
         'comments' => [],
         'feedback_average' => $skillIssue->getAverage(),
     ];
@@ -156,7 +159,7 @@ foreach ($userSkills as $index => $skillIssue) {
     $formAcquiredLevel = new FormValidator(
         'acquired_level'.$skillIssue->getId(),
         'post',
-        $skillIssue->getIssueUrlAll()
+        SkillRelUserManager::getIssueUrlAll($skillIssue)
     );
     $formAcquiredLevel->addSelect('acquired_level', get_lang('AcquiredLevel'), $acquiredLevel);
     $formAcquiredLevel->addHidden('user', $skillIssue->getUser()->getId());
@@ -172,14 +175,14 @@ foreach ($userSkills as $index => $skillIssue) {
         $em->persist($skillIssue);
         $em->flush();
 
-        header("Location: ".$skillIssue->getIssueUrlAll());
+        header('Location: '.SkillRelUserManager::getIssueUrlAll($skillIssue));
         exit;
     }
 
     $form = new FormValidator(
         'comment'.$skillIssue->getId(),
         'post',
-        $skillIssue->getIssueUrlAll()
+        SkillRelUserManager::getIssueUrlAll($skillIssue)
     );
     $form->addTextarea('comment', get_lang('NewComment'), ['rows' => 4]);
     $form->applyFilter('comment', 'trim');
@@ -207,7 +210,7 @@ foreach ($userSkills as $index => $skillIssue) {
         $em->persist($skillUserComment);
         $em->flush();
 
-        header("Location: ".$skillIssue->getIssueUrlAll());
+        header('Location: '.SkillRelUserManager::getIssueUrlAll($skillIssue));
         exit;
     }
 

+ 48 - 0
main/inc/lib/skill.lib.php

@@ -3,6 +3,7 @@
 
 use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
 use Chamilo\CoreBundle\Entity\Skill as SkillEntity;
+use Chamilo\CoreBundle\Entity\SkillRelUser as SkillRelUserEntity;
 use Chamilo\SkillBundle\Entity\SkillRelCourse;
 use Chamilo\SkillBundle\Entity\SkillRelItem;
 use Chamilo\UserBundle\Entity\User;
@@ -11,6 +12,7 @@ use Fhaculty\Graph\Vertex;
 
 /**
  * Class SkillProfile.
+ * @todo break the file in different classes
  *
  * @package chamilo.library
  */
@@ -614,6 +616,52 @@ class SkillRelUser extends Model
             'where' => $where,
         ], 'first');
     }
+
+    /**
+     * Get the URL for the issue.
+     *
+     * @param SkillRelUserEntity $skillIssue
+     *
+     * @return string
+     *
+     */
+    public static function getIssueUrl(SkillRelUserEntity $skillIssue)
+    {
+        return api_get_path(WEB_PATH)."badge/{$skillIssue->getId()}";
+    }
+
+
+    /**
+     * Get the URL for the All issues page.
+     *
+     * @param SkillRelUserEntity $skillIssue
+     *
+     * @return string
+     */
+    public static function getIssueUrlAll(SkillRelUserEntity $skillIssue)
+    {
+        return api_get_path(WEB_PATH)."skill/{$skillIssue->getSkill()->getId()}/user/{$skillIssue->getUser()->getId()}";
+    }
+
+    /**
+     * Get the URL for the assertion.
+     * @param SkillRelUserEntity $skillIssue
+     *
+     * @return string
+     */
+    public static function getAssertionUrl(SkillRelUserEntity $skillIssue)
+    {
+        $url = api_get_path(WEB_CODE_PATH).'badge/assertion.php?';
+
+        $url .= http_build_query([
+            'user' => $skillIssue->getUser()->getId(),
+            'skill' => $skillIssue->getSkill()->getId(),
+            'course' => $skillIssue->getCourse() ? $skillIssue->getCourse()->getId() : 0,
+            'session' => $skillIssue->getSession() ? $skillIssue->getSession()->getId() : 0,
+        ]);
+
+        return $url;
+    }
 }
 
 /**

+ 15 - 18
src/Chamilo/CoreBundle/Entity/Session.php

@@ -4,6 +4,7 @@
 namespace Chamilo\CoreBundle\Entity;
 
 use Chamilo\CourseBundle\Entity\CStudentPublication;
+use Chamilo\CoreBundle\Entity\Course;
 use Chamilo\UserBundle\Entity\User;
 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\Common\Collections\Criteria;
@@ -411,7 +412,7 @@ class Session
     {
         if ($this->getCourses()->count()) {
             $criteria = Criteria::create()->where(
-                Criteria::expr()->eq("course", $course)
+                Criteria::expr()->eq('course', $course)
             );
             $relation = $this->getCourses()->matching($criteria);
 
@@ -508,14 +509,14 @@ class Session
     public function getUserInCourse(User $user, Course $course, $status = null)
     {
         $criteria = Criteria::create()->where(
-            Criteria::expr()->eq("course", $course)
+            Criteria::expr()->eq('course', $course)
         )->andWhere(
-            Criteria::expr()->eq("user", $user)
+            Criteria::expr()->eq('user', $user)
         );
 
         if (!is_null($status)) {
             $criteria->andWhere(
-                Criteria::expr()->eq("status", $status)
+                Criteria::expr()->eq('status', $status)
             );
         }
 
@@ -527,7 +528,7 @@ class Session
      *
      * @param string $name
      *
-     * @return Session
+     * @return $this
      */
     public function setName($name)
     {
@@ -551,7 +552,7 @@ class Session
      *
      * @param string $description
      *
-     * @return Groups
+     * @return $this
      */
     public function setDescription($description)
     {
@@ -919,11 +920,7 @@ class Session
     {
         $now = new \Datetime('now');
 
-        if ($now > $this->getAccessStartDate()) {
-            return true;
-        }
-
-        return false;
+        return $now > $this->getAccessStartDate();
     }
 
     /**
@@ -1021,11 +1018,11 @@ class Session
     {
         if ($this->getUserCourseSubscriptions()->count()) {
             $criteria = Criteria::create()->where(
-                Criteria::expr()->eq("user", $subscription->getUser())
+                Criteria::expr()->eq('user', $subscription->getUser())
             )->andWhere(
-                Criteria::expr()->eq("course", $subscription->getCourse())
+                Criteria::expr()->eq('course', $subscription->getCourse())
             )->andWhere(
-                Criteria::expr()->eq("session", $subscription->getSession())
+                Criteria::expr()->eq('session', $subscription->getSession())
             );
             $relation = $this->getUserCourseSubscriptions()->matching($criteria);
 
@@ -1085,8 +1082,8 @@ class Session
     /**
      * Get user from course by status.
      *
-     * @param \Chamilo\CoreBundle\Entity\Course $course
-     * @param int                               $status
+     * @param Course $course
+     * @param int    $status
      *
      * @return \Doctrine\Common\Collections\ArrayCollection|\Doctrine\Common\Collections\Collection
      */
@@ -1094,10 +1091,10 @@ class Session
     {
         $criteria = Criteria::create()
             ->where(
-                Criteria::expr()->eq("course", $course)
+                Criteria::expr()->eq('course', $course)
             )
             ->andWhere(
-                Criteria::expr()->eq("status", $status)
+                Criteria::expr()->eq('status', $status)
             );
 
         return $this->userCourseSubscriptions->matching($criteria);

+ 19 - 56
src/Chamilo/CoreBundle/Entity/SkillRelUser.php

@@ -24,10 +24,6 @@ use Doctrine\ORM\Mapping as ORM;
  */
 class SkillRelUser
 {
-    /**
-     * @ORM\OneToMany(targetEntity="SkillRelUserComment", mappedBy="skillRelUser")
-     */
-    protected $comments;
     /**
      * @var int
      *
@@ -35,39 +31,45 @@ class SkillRelUser
      * @ORM\Id
      * @ORM\GeneratedValue
      */
-    private $id;
+    protected $id;
+
+    /**
+     * @ORM\OneToMany(targetEntity="SkillRelUserComment", mappedBy="skillRelUser")
+     */
+    protected $comments;
+
 
     /**
      * @ORM\ManyToOne(targetEntity="Chamilo\UserBundle\Entity\User", inversedBy="achievedSkills", cascade={"persist"})
      * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
      */
-    private $user;
+    protected $user;
 
     /**
      * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Skill", inversedBy="issuedSkills", cascade={"persist"})
      * @ORM\JoinColumn(name="skill_id", referencedColumnName="id", nullable=false)
      */
-    private $skill;
+    protected $skill;
 
     /**
      * @var \DateTime
      *
      * @ORM\Column(name="acquired_skill_at", type="datetime", nullable=false)
      */
-    private $acquiredSkillAt;
+    protected $acquiredSkillAt;
 
     /**
      * @var int
      *
      * @ORM\Column(name="assigned_by", type="integer", nullable=false)
      */
-    private $assignedBy;
+    protected $assignedBy;
 
     /**
      * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", inversedBy="issuedSkills", cascade={"persist"})
      * @ORM\JoinColumn(name="course_id", referencedColumnName="id", nullable=true)
      */
-    private $course;
+    protected $course;
 
     /**
      * @var Session
@@ -75,7 +77,7 @@ class SkillRelUser
      * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session", inversedBy="issuedSkills", cascade={"persist"})
      * @ORM\JoinColumn(name="session_id", referencedColumnName="id", nullable=true)
      */
-    private $session;
+    protected $session;
 
     /**
      * @var Level
@@ -83,21 +85,21 @@ class SkillRelUser
      * @ORM\ManyToOne(targetEntity="Chamilo\SkillBundle\Entity\Level")
      * @ORM\JoinColumn(name="acquired_level", referencedColumnName="id")
      */
-    private $acquiredLevel;
+    protected $acquiredLevel;
 
     /**
      * @var string
      *
      * @ORM\Column(name="argumentation", type="text")
      */
-    private $argumentation;
+    protected $argumentation;
 
     /**
      * @var int
      *
      * @ORM\Column(name="argumentation_author_id", type="integer")
      */
-    private $argumentationAuthorId;
+    protected $argumentationAuthorId;
 
     /**
      * SkillRelUser constructor.
@@ -342,56 +344,17 @@ class SkillRelUser
     {
         $source = '';
 
-        if ($this->session && $this->session->getId() != 0) {
-            $source .= "[{$this->session->getName()}] ";
+        if ($this->session && $this->getSession()->getId() != 0) {
+            $source .= "[{$this->getSession()->getName()}] ";
         }
 
         if ($this->course) {
-            $source .= $this->course->getTitle();
+            $source .= $this->getCourse()->getTitle();
         }
 
         return $source;
     }
 
-    /**
-     * Get the URL for the issue.
-     *
-     * @return string
-     */
-    public function getIssueUrl()
-    {
-        return api_get_path(WEB_PATH)."badge/{$this->id}";
-    }
-
-    /**
-     * Get the URL for the All issues page.
-     *
-     * @return string
-     */
-    public function getIssueUrlAll()
-    {
-        return api_get_path(WEB_PATH)."skill/{$this->skill->getId()}/user/{$this->user->getId()}";
-    }
-
-    /**
-     * Get the URL for the assertion.
-     *
-     * @return string
-     */
-    public function getAssertionUrl()
-    {
-        $url = api_get_path(WEB_CODE_PATH).'badge/assertion.php?';
-
-        $url .= http_build_query([
-            'user' => $this->getUser()->getId(),
-            'skill' => $this->getSkill()->getId(),
-            'course' => $this->course ? $this->getCourse()->getId() : 0,
-            'session' => $this->session ? $this->getSession()->getId() : 0,
-        ]);
-
-        return $url;
-    }
-
     /**
      * Get comments.
      *