Example #1
0
 /**
  * @param string $attribute
  * @param CGroupInfo $group
  * @param User $user
  * @return bool
  */
 protected function isGranted($attribute, $group, $user = null)
 {
     // make sure there is a user object (i.e. that the user is logged in)
     if (!$user instanceof UserInterface) {
         return false;
     }
     if ($group == false) {
         return false;
     }
     $authChecker = $this->container->get('security.authorization_checker');
     // Admins have access to everything
     if ($authChecker->isGranted('ROLE_ADMIN')) {
         return true;
     }
     $groupInfo = ['id' => $group->getId(), 'session_id' => 0, 'status' => $group->getStatus()];
     // Legacy
     return \GroupManager::userHasAccessToBrowse($user->getId(), $groupInfo);
     switch ($attribute) {
         case self::VIEW:
             if (!$group->hasUserInCourse($user, $course)) {
                 $user->addRole('ROLE_CURRENT_SESSION_COURSE_STUDENT');
                 return true;
             }
             break;
         case self::EDIT:
         case self::DELETE:
             if (!$session->hasCoachInCourseWithStatus($user, $course)) {
                 $user->addRole('ROLE_CURRENT_SESSION_COURSE_TEACHER');
                 return true;
             }
             break;
     }
     dump("You don't have access to this group!!");
     return false;
 }
 /**
  * Create a group
  * @param string $name The name for this group
  * @param int $category_id
  * @param int $tutor The user-id of the group's tutor
  * @param int $places How many people can subscribe to the new group
  */
 public static function create_group($name, $category_id, $tutor, $places)
 {
     $courseObj = api_get_user_course_entity();
     $_course = api_get_course_info();
     $session_id = api_get_session_id();
     $currentCourseRepository = $_course['path'];
     $category = self::get_category($category_id);
     $places = intval($places);
     if ($category) {
         if ($places == 0) {
             //if the amount of users per group is not filled in, use the setting from the category
             $places = $category['max_student'];
         } else {
             if ($places > $category['max_student'] && $category['max_student'] != 0) {
                 $places = $category['max_student'];
             }
         }
         $docState = $category['doc_state'];
         $calendarState = $category['calendar_state'];
         $workState = $category['work_state'];
         $anonuncementState = $category['announcements_state'];
         $forumState = $category['forum_state'];
         $wikiState = $category['wiki_state'];
         $chatState = $category['chat_state'];
         $selfRegAllowed = $category['self_reg_allowed'];
         $selfUnregAllowed = $category['self_unreg_allowed'];
     } else {
         $docState = self::TOOL_PRIVATE;
         $calendarState = self::TOOL_PRIVATE;
         $workState = self::TOOL_PRIVATE;
         $anonuncementState = self::TOOL_PRIVATE;
         $forumState = self::TOOL_PRIVATE;
         $wikiState = self::TOOL_PRIVATE;
         $chatState = self::TOOL_PRIVATE;
         $selfRegAllowed = 0;
         $selfUnregAllowed = 0;
     }
     $group = new CGroupInfo();
     $group->setName($name)->setStatus(1)->setDescription('')->setMaxStudent($places)->setAnnouncementsState($anonuncementState)->setDocState($docState)->setCalendarState($calendarState)->setChatState($chatState)->setForumState($forumState)->setWikiState($wikiState)->setWorkState($workState)->setSelfUnregistrationAllowed($selfUnregAllowed)->setSelfRegistrationAllowed($selfRegAllowed)->setSessionId($session_id)->setCourse($courseObj)->setCategoryId($category_id)->setDescription('');
     $em = Database::getManager();
     $em->persist($group);
     $em->flush();
     $lastId = $group->getIid();
     if ($lastId) {
         $desired_dir_name = '/' . api_replace_dangerous_char($name) . '_groupdocs';
         $my_path = api_get_path(SYS_COURSE_PATH) . $currentCourseRepository . '/document';
         $newFolderData = create_unexisting_directory($_course, api_get_user_id(), $session_id, $lastId, null, $my_path, $desired_dir_name, null, 1);
         $unique_name = $newFolderData['path'];
         $group->setId($lastId);
         $group->setSecretDirectory($unique_name);
         $group->setName($name);
         $em->merge($group);
         $em->flush();
         // create a forum if needed
         if ($forumState >= 0) {
             require_once api_get_path(SYS_CODE_PATH) . 'forum/forumconfig.inc.php';
             require_once api_get_path(SYS_CODE_PATH) . 'forum/forumfunction.inc.php';
             $forum_categories = get_forum_categories();
             if (empty($forum_categories)) {
                 $categoryParam = array('forum_category_title' => get_lang('GroupForums'));
                 store_forumcategory($categoryParam);
                 $forum_categories = get_forum_categories();
             }
             $counter = 0;
             foreach ($forum_categories as $key => $value) {
                 if ($counter == 0) {
                     $forum_category_id = $key;
                 }
                 $counter++;
             }
             // A sanity check.
             if (empty($forum_category_id)) {
                 $forum_category_id = 0;
             }
             $values = array();
             $values['forum_title'] = $name;
             $values['group_id'] = $lastId;
             $values['forum_category'] = $forum_category_id;
             $values['allow_anonymous_group']['allow_anonymous'] = 0;
             $values['students_can_edit_group']['students_can_edit'] = 0;
             $values['approval_direct_group']['approval_direct'] = 0;
             $values['allow_attachments_group']['allow_attachments'] = 1;
             $values['allow_new_threads_group']['allow_new_threads'] = 1;
             $values['default_view_type_group']['default_view_type'] = api_get_setting('forum.default_forum_view');
             $values['group_forum'] = $lastId;
             if ($forumState == '1') {
                 $values['public_private_group_forum_group']['public_private_group_forum'] = 'public';
             } elseif ($forumState == '2') {
                 $values['public_private_group_forum_group']['public_private_group_forum'] = 'private';
             } elseif ($forumState == '0') {
                 $values['public_private_group_forum_group']['public_private_group_forum'] = 'unavailable';
             }
             store_forum($values);
         }
     }
     return $lastId;
 }