예제 #1
0
 /**
  * @Given I have a course :arg1
  */
 public function iHaveACourse($arg1)
 {
     $em = $this->getEntityManager();
     $entity = new Course();
     $entity->setTitle($arg1);
     $em->persist($entity);
     $em->flush();
 }
예제 #2
0
 /**
  * @param Course $course
  * @return Course
  */
 public function addToolsInCourse(Course $course)
 {
     $tools = $this->getTools();
     /** @var BaseTool $tool */
     foreach ($tools as $tool) {
         $toolEntity = new CTool();
         $toolEntity->setCId($course->getId())->setImage($tool->getImage())->setName($tool->getName())->setVisibility(1)->setLink($tool->getLink())->setTarget($tool->getTarget())->setCategory($tool->getCategory());
         $course->addTools($toolEntity);
     }
     return $course;
 }
예제 #3
0
 /**
  * Get the last acquired skill by a user on course and/or session
  * @param User $user The user
  * @param Course $course The course
  * @param Session $session The session
  * @return Skill
  */
 public function getLastByUser(User $user, Course $course = null, Session $session = null)
 {
     $qb = $this->createQueryBuilder('s');
     $qb->innerJoin('ChamiloCoreBundle:SkillRelUser', 'su', Join::WITH, $qb->expr()->eq('s', 'su.skill'))->where($qb->expr()->eq('su.user', $user->getId()));
     if ($course) {
         $qb->andWhere($qb->expr()->eq('su.course', $course->getId()));
     }
     if ($session) {
         $qb->andWhere($qb->expr()->eq('su.session', $session->getId()));
     }
     $qb->setMaxResults(1)->orderBy('su.id', 'DESC');
     return $qb->getQuery()->getOneOrNullResult();
 }
예제 #4
0
 /**
  * @param string $attribute
  * @param Course $course
  * @param User $user
  * @return bool
  */
 protected function isGranted($attribute, $course, $user = null)
 {
     // make sure there is a user object (i.e. that the user is logged in)
     if (!$user instanceof UserInterface) {
         return false;
     }
     $authChecker = $this->container->get('security.authorization_checker');
     // Admins have access to everything
     if ($authChecker->isGranted('ROLE_ADMIN')) {
         dump('Im admin');
         // return true;
     }
     // Is an active course
     if (!$course->isActive()) {
         dump('Course is not active');
         return false;
     }
     switch ($attribute) {
         case self::VIEW:
             // "Open to the world" no need to check if user is registered
             if ($course->isPublic()) {
                 dump('Course is public');
                 return true;
             }
             // User is subscribed in the course no matter if is teacher/student
             if ($course->hasUser($user)) {
                 dump('User is subscribed in course');
                 $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_STUDENT);
                 return true;
             }
             break;
         case self::EDIT:
         case self::DELETE:
             // Only teacher can edit stuff
             if ($course->hasTeacher($user)) {
                 $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER);
                 return true;
             }
             break;
     }
     dump("You dont have access to this course!!");
     return false;
 }
예제 #5
0
 /**
  * Get all users that are registered in the course. No matter the status
  *
  * @param Course $course
  *
  * @return \Doctrine\ORM\QueryBuilder
  */
 public function getSubscribedUsers(Course $course)
 {
     $queryBuilder = $this->createQueryBuilder('a');
     // Selecting user info.
     $queryBuilder->select('DISTINCT u');
     // Loading EntityUser.
     $queryBuilder->from('Chamilo\\UserBundle\\Entity\\User', 'u');
     // Selecting courses for users.
     $queryBuilder->innerJoin('u.courses', 'c');
     //@todo check app settings
     $queryBuilder->add('orderBy', 'u.lastname ASC');
     $wherePart = $queryBuilder->expr()->andx();
     // Get only users subscribed to this course
     $wherePart->add($queryBuilder->expr()->eq('c.cId', $course->getId()));
     // $wherePart->add($queryBuilder->expr()->eq('c.status', $status));
     $queryBuilder->where($wherePart);
     //$q = $queryBuilder->getQuery();
     //return $q->execute();
     return $queryBuilder;
 }
예제 #6
0
 /**
  * Get all users that are registered in the course. No matter the status
  *
  * @param Course $course
  *
  * @return \Doctrine\ORM\QueryBuilder
  */
 public function getSubscribedUsers(Course $course)
 {
     // Course builder
     $queryBuilder = $this->createQueryBuilder('c');
     // Selecting user info.
     $queryBuilder->select('DISTINCT user');
     // Selecting courses for users.
     $queryBuilder->innerJoin('c.users', 'subscriptions');
     $queryBuilder->innerJoin('ChamiloUserBundle:User', 'user', Join::WITH, 'subscriptions.user = user.id');
     if (api_is_western_name_order()) {
         $queryBuilder->add('orderBy', 'user.firstname ASC');
     } else {
         $queryBuilder->add('orderBy', 'user.lastname ASC');
     }
     $wherePart = $queryBuilder->expr()->andx();
     // Get only users subscribed to this course
     $wherePart->add($queryBuilder->expr()->eq('c.id', $course->getId()));
     // $wherePart->add($queryBuilder->expr()->eq('c.status', $status));
     $queryBuilder->where($wherePart);
     //var_dump($queryBuilder->getQuery()->getSQL());
     //$q = $queryBuilder->getQuery();
     //return $q->execute();
     return $queryBuilder;
 }
예제 #7
0
 /**
  * @param CourseRepository $repo
  * @param Course $course
  * @param AccessUrl $url
  * @throws \Exception
  */
 private function checkLimit($repo, Course $course, AccessUrl $url)
 {
     $limit = $url->getLimitCourses();
     if (!empty($limit)) {
         $count = $repo->getCountCoursesByUrl($url);
         if ($count >= $limit) {
             api_warn_hosting_contact('hosting_limit_courses', $limit);
             throw new \Exception('PortalCoursesLimitReached');
         }
     }
     if ($course->getVisibility() != COURSE_VISIBILITY_HIDDEN) {
         $limit = $url->getLimitActiveCourses();
         if (!empty($limit)) {
             $count = $repo->getCountActiveCoursesByUrl($url);
             if ($count >= $limit) {
                 api_warn_hosting_contact('hosting_limit_active_courses', $limit);
                 throw new \Exception('PortalActiveCoursesLimitReached');
             }
         }
     }
 }
예제 #8
0
 /**
  * Function register_course to create a record in the course table of the main database
  * @param array Course details (see code for details)
  * @return int  Created course ID
  * @todo use an array called $params instead of lots of params
  * @assert (null) === false
  */
 public static function register_course($params)
 {
     global $error_msg;
     $title = $params['title'];
     $code = $params['code'];
     $visual_code = $params['visual_code'];
     $directory = $params['directory'];
     $tutor_name = isset($params['tutor_name']) ? $params['tutor_name'] : null;
     //$description        = $params['description'];
     $category_code = isset($params['course_category']) ? $params['course_category'] : '';
     $course_language = isset($params['course_language']) && !empty($params['course_language']) ? $params['course_language'] : api_get_setting('language.platform_language');
     $user_id = empty($params['user_id']) ? api_get_user_id() : intval($params['user_id']);
     $department_name = isset($params['department_name']) ? $params['department_name'] : null;
     $department_url = isset($params['department_url']) ? $params['department_url'] : null;
     $disk_quota = isset($params['disk_quota']) ? $params['disk_quota'] : null;
     if (!isset($params['visibility'])) {
         $default_course_visibility = api_get_setting('course.courses_default_creation_visibility');
         if ($default_course_visibility != '') {
             $visibility = $default_course_visibility;
         } else {
             $visibility = COURSE_VISIBILITY_OPEN_PLATFORM;
         }
     } else {
         $visibility = $params['visibility'];
     }
     $subscribe = isset($params['subscribe']) ? intval($params['subscribe']) : ($visibility == COURSE_VISIBILITY_OPEN_PLATFORM ? 1 : 0);
     $unsubscribe = isset($params['unsubscribe']) ? intval($params['unsubscribe']) : 0;
     $expiration_date = isset($params['expiration_date']) ? $params['expiration_date'] : null;
     $teachers = isset($params['teachers']) ? $params['teachers'] : null;
     $status = isset($params['status']) ? $params['status'] : null;
     $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
     $ok_to_register_course = true;
     // Check whether all the needed parameters are present.
     if (empty($code)) {
         $error_msg[] = 'courseSysCode is missing';
         $ok_to_register_course = false;
     }
     if (empty($visual_code)) {
         $error_msg[] = 'courseScreenCode is missing';
         $ok_to_register_course = false;
     }
     if (empty($directory)) {
         $error_msg[] = 'courseRepository is missing';
         $ok_to_register_course = false;
     }
     if (empty($title)) {
         $error_msg[] = 'title is missing';
         $ok_to_register_course = false;
     }
     if (empty($expiration_date)) {
         $expiration_date = api_get_utc_datetime(time() + FIRST_EXPIRATION_DELAY, false, true);
     } else {
         $expiration_date = api_get_utc_datetime($expiration_date, false, true);
     }
     if ($visibility < 0 || $visibility > 4) {
         $error_msg[] = 'visibility is invalid';
         $ok_to_register_course = false;
     }
     if (empty($disk_quota)) {
         $disk_quota = api_get_setting('document.default_document_quotum');
     }
     if (stripos($department_url, 'http://') === false && stripos($department_url, 'https://') === false) {
         $department_url = 'http://' . $department_url;
     }
     //just in case
     if ($department_url == 'http://') {
         $department_url = '';
     }
     $course_id = 0;
     if ($ok_to_register_course) {
         $manager = Database::getManager();
         $url = $manager->getRepository('ChamiloCoreBundle:AccessUrl')->find(api_get_current_access_url_id());
         $accessRelCourse = new AccessUrlRelCourse();
         $accessRelCourse->setUrl($url);
         $course = new Course();
         $course->setCode($code)->setDirectory($directory)->setCourseLanguage($course_language)->setTitle($title)->setDescription(self::lang2db(get_lang('CourseDescription')))->setCategoryCode($category_code)->setVisibility($visibility)->setShowScore(1)->setExpirationDate($expiration_date)->setDiskQuota(intval($disk_quota))->setTutorName($tutor_name)->setDepartmentName($department_name)->setDepartmentUrl($department_url)->setSubscribe(intval($subscribe))->setVisualCode($visual_code)->addUrls($accessRelCourse);
         $manager->persist($course);
         $manager->flush();
         $course_id = $course->getId();
         if ($course_id) {
             $sort = api_max_sort_value('0', api_get_user_id());
             // 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\n                                c_id     = '" . $course_id . "',\n                                user_id         = '" . intval($user_id) . "',\n                                status          = '1',\n                                is_tutor        = '0',\n                                sort            = '" . $i_course_sort . "',\n                                relation_type = 0,\n                                user_course_cat = '0'";
                     Database::query($sql);
                 }
             }
             if (!empty($teachers)) {
                 if (!is_array($teachers)) {
                     $teachers = array($teachers);
                 }
                 foreach ($teachers as $key) {
                     //just in case
                     if ($key == $user_id) {
                         continue;
                     }
                     if (empty($key)) {
                         continue;
                     }
                     $sql = "INSERT INTO " . $TABLECOURSUSER . " SET\n                            c_id     = '" . Database::escape_string($course_id) . "',\n                            user_id         = '" . Database::escape_string($key) . "',\n                            status          = '1',\n                            is_tutor        = '0',\n                            sort            = '" . ($sort + 1) . "',\n                            relation_type = 0,\n                            user_course_cat = '0'";
                     Database::query($sql);
                 }
             }
             // Adding the course to an URL.
             if (api_is_multiple_url_enabled()) {
                 $url_id = 1;
                 if (api_get_current_access_url_id() != -1) {
                     $url_id = api_get_current_access_url_id();
                 }
                 UrlManager::add_course_to_url($course_id, $url_id);
             } else {
                 UrlManager::add_course_to_url($course_id, 1);
             }
             // Add event to the system log.
             $user_id = api_get_user_id();
             Event::addEvent(LOG_COURSE_CREATE, LOG_COURSE_CODE, $code, api_get_utc_datetime(), $user_id, $course_id);
             $send_mail_to_admin = api_get_setting('course.send_email_to_admin_when_create_course');
             // @todo Improve code to send to all current portal administrators.
             if ($send_mail_to_admin == 'true') {
                 $siteName = api_get_setting('platform.site_name');
                 $recipient_email = api_get_setting('admin.administrator_email');
                 $recipient_name = api_get_person_name(api_get_setting('admin.administrator_name'), api_get_setting('admin.administrator_surname'));
                 $iname = api_get_setting('platform.institution');
                 $subject = get_lang('NewCourseCreatedIn') . ' ' . $siteName . ' - ' . $iname;
                 $message = get_lang('Dear') . ' ' . $recipient_name . ",\n\n" . get_lang('MessageOfNewCourseToAdmin') . ' ' . $siteName . ' - ' . $iname . "\n";
                 $message .= get_lang('CourseName') . ' ' . $title . "\n";
                 $message .= get_lang('Category') . ' ' . $category_code . "\n";
                 $message .= get_lang('Tutor') . ' ' . $tutor_name . "\n";
                 $message .= get_lang('Language') . ' ' . $course_language;
                 $userInfo = api_get_user_info($user_id);
                 $additionalParameters = array('smsType' => SmsPlugin::NEW_COURSE_BEEN_CREATED, 'userId' => $user_id, 'courseName' => $title, 'creatorUsername' => $userInfo['username']);
                 api_mail_html($recipient_name, $recipient_email, $subject, $message, $siteName, $recipient_email, null, null, null, $additionalParameters);
             }
         }
     }
     return $course_id;
 }
예제 #9
0
 /**
  * Get the coaches for a course within a session
  * @param Session $session The session
  * @param Course $course The course
  * @return \Doctrine\ORM\QueryBuilder
  */
 public function getCoachesForSessionCourse(Session $session, Course $course)
 {
     $queryBuilder = $this->createQueryBuilder('u');
     $queryBuilder->select('u')->innerJoin('ChamiloCoreBundle:SessionRelCourseRelUser', 'scu', Join::WITH, 'scu.user = u')->where($queryBuilder->expr()->andX($queryBuilder->expr()->eq('scu.session', $session->getId()), $queryBuilder->expr()->eq('scu.course', $course->getId()), $queryBuilder->expr()->eq('scu.status', SessionRelCourseRelUser::STATUS_COURSE_COACH)));
     return $queryBuilder->getQuery()->getResult();
 }
예제 #10
0
 /**
  * @inheritdoc
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     // @todo as a service to load the preferred_choices
     $builder->add('title', 'text')->add('course_language', 'locale', array('preferred_choices' => array('en', 'fr', 'es')))->add('visibility', 'choice', array('choices' => Course::getStatusList()))->add('department_name', 'text', array('required' => false))->add('department_url', 'url', array('required' => false))->add('expiration_date', 'sonata_type_datetime_picker', array('required' => false))->add('save', 'submit', array('label' => 'Update'));
 }
예제 #11
0
 /**
  * @param Course $course
  * @return $this
  */
 public function setCurrentCourse(Course $course)
 {
     // If the session is registered in the course session list.
     if ($this->getCourses()->contains($course->getId())) {
         $this->currentCourse = $course;
     }
     return $this;
 }
예제 #12
0
 /**
  * @param $status
  * @param User $user
  * @param Course $course
  * @param Session $session
  */
 private function addUserInCourse($status, User $user, Course $course, Session $session)
 {
     if ($session->isActive() && $user->getIsActive() && $course->isActive()) {
         if ($session->hasCourse($course)) {
             switch ($status) {
                 case Session::DRH:
                     if ($user->hasRole('ROLE_RRHH')) {
                         $session->addUserInSession(Session::DRH, $user);
                     }
                     break;
                 case Session::STUDENT:
                     $session->addUserInSession(Session::STUDENT, $user);
                     $session->addUserInCourse(Session::STUDENT, $user, $course);
                     break;
                 case Session::COACH:
                     if ($user->hasRole('ROLE_TEACHER')) {
                         $session->addUserInCourse(Session::COACH, $user, $course);
                     }
                     break;
             }
         }
     }
 }
예제 #13
0
 /**
  *  Very important in order to save the related entities while creation.
  * @param Course $course
  * @return mixed|void
  */
 public function prePersist($course)
 {
     $course->setUsers($course->getUsers());
     $course->setUrls($course->getUrls());
     $this->updateTools($course);
 }
 /**
  * Convert the course info to array with necessary course data for save item
  * @param \Chamilo\CoreBundle\Entity\Course $course
  * @param array $defaultCurrency Optional. Currency data
  * @return array
  */
 public function getCourseForConfiguration(\Chamilo\CoreBundle\Entity\Course $course, $defaultCurrency = null)
 {
     $courseItem = ['item_id' => null, 'course_id' => $course->getId(), 'course_visual_code' => $course->getVisualCode(), 'course_code' => $course->getCode(), 'course_title' => $course->getTitle(), 'course_visibility' => $course->getVisibility(), 'visible' => false, 'currency' => empty($defaultCurrency) ? null : $defaultCurrency['iso_code'], 'price' => 0.0];
     $item = $this->getItemByProduct($course->getId(), self::PRODUCT_TYPE_COURSE);
     if ($item !== false) {
         $courseItem['item_id'] = $item['id'];
         $courseItem['visible'] = true;
         $courseItem['currency'] = $item['iso_code'];
         $courseItem['price'] = $item['price'];
     }
     return $courseItem;
 }
 /**
  * Get the course template for a user
  * @param Course $course
  * @param User $user
  * @return ArrayCollection
  */
 public function getCourseTemplates(Course $course, User $user)
 {
     $qb = $this->createQueryBuilder('t');
     $qb->select('t', 'd.path')->innerJoin('ChamiloCoreBundle:Course', 'c', Join::WITH, $qb->expr()->eq('t.courseCode', 'c.code'))->innerJoin('ChamiloCourseBundle:CDocument', 'd', Join::WITH, $qb->expr()->eq('c.id', 'd.cId'))->where($qb->expr()->eq('d.iid', 't.refDoc'))->andWhere($qb->expr()->eq('c.id', $course->getId()))->andWhere($qb->expr()->eq('t.userId', $user->getId()));
     return $qb->getQuery()->getResult();
 }