Exemplo n.º 1
0
 /**
  * Set can enroll property
  * 
  * @access public
  * @param array $courses
  * @return array courses with canRoll property added
  */
 public function setCanEnroll($courses)
 {
     $auth = new AuthenticationService();
     $storage = $auth->getIdentity();
     $currentUser = NULL;
     if ($auth->hasIdentity()) {
         $currentUser = $this->query->find('Users\\Entity\\User', $storage['id']);
     }
     foreach ($courses as $course) {
         $nonAuthorizedEnroll = false;
         $canEnroll = true;
         $users = $course->getUsers();
         $canLeave = false;
         if ($auth->hasIdentity()) {
             $courseAiId = $this->objectUtilities->getId($course->getAi());
             if (in_array(Role::INSTRUCTOR_ROLE, $storage['roles']) && $storage['id'] == $courseAiId) {
                 $nonAuthorizedEnroll = true;
             }
         }
         if (!is_null($currentUser)) {
             $canLeave = $users->contains($currentUser);
         }
         if ($canLeave === true || $nonAuthorizedEnroll === true || $course->getStudentsNo() >= $course->getCapacity()) {
             $canEnroll = false;
         }
         $course->canEnroll = $canEnroll;
         $course->canLeave = $canLeave;
     }
     return $courses;
 }
Exemplo n.º 2
0
 /**
  * prepare object for display
  * 
  * 
  * @access public
  * @param array $objectsArray
  * @param mixed $sampleObject object used to get expected properties when $objectsArray is array of arrays not array of objects ,default is null
  * @param int $depthLevel ,default is 0
  * @param int $maxDepthLevel depth level including first object level ,default is 3
  * @return array objects prepared for display
  */
 public function prepareForDisplay($objectsArray, $sampleObject = null, $depthLevel = 0, $maxDepthLevel = 3)
 {
     $depthLevel++;
     foreach ($objectsArray as &$object) {
         $notObject = false;
         // support array of arrays instead of array of objects
         if (is_array($object)) {
             $object = (object) $object;
             $notObject = true;
         }
         $objectProperties = $this->prepareForStatusDisplay($object);
         if (($notObject === false || $notObject === true && !is_null($sampleObject)) && $depthLevel == 1) {
             if (is_null($sampleObject)) {
                 $sampleObjectForWrapper = $object;
             } else {
                 $sampleObjectForWrapper = $sampleObject;
             }
             $wrapped = AbstractWrapper::wrap($sampleObjectForWrapper, $this->query->entityManager);
             $meta = $wrapped->getMetadata();
         }
         foreach ($objectProperties as $objectPropertyName => $objectPropertyValue) {
             if (is_string($objectPropertyValue) && strlen($objectPropertyValue) <= 5) {
                 $textObjectPropertyName = $objectPropertyName . "Text";
                 if (array_key_exists($objectPropertyValue, $this->languages)) {
                     $object->{$textObjectPropertyName} = $this->languages[$objectPropertyValue];
                 } elseif (strlen($objectPropertyValue) == 2 && array_key_exists($objectPropertyValue, $this->countries)) {
                     $object->{$textObjectPropertyName} = $this->countries[$objectPropertyValue];
                 }
             } elseif ($objectPropertyValue instanceof \DateTime) {
                 $formattedString = $objectPropertyValue->format("D, d M Y");
                 if ($formattedString == Time::UNIX_DATE_STRING) {
                     $formattedString = $objectPropertyValue->format("H:i");
                 }
                 $object->{$objectPropertyName} = $formattedString;
             } elseif (is_object($objectPropertyValue) && $depthLevel != $maxDepthLevel) {
                 $objectsPropertyValue = $this->prepareForDisplay(array($objectPropertyValue), $sampleObject, $depthLevel, $maxDepthLevel);
                 $object->{$objectPropertyName} = reset($objectsPropertyValue);
             } elseif (is_array($objectPropertyValue) && array_key_exists("id", $objectPropertyValue) && isset($meta) && $meta->isSingleValuedAssociation($objectPropertyName)) {
                 $object->{$objectPropertyName} = $this->query->find($meta->getAssociationMapping($objectPropertyName)["targetEntity"], $objectPropertyValue["id"]);
             }
         }
     }
     return $objectsArray;
 }