/**
  * @param FormValidator $form
  * @param array $sendTo array('everyone' => false, 'users' => [1, 2], 'groups' => [3, 4])
  * @param array $attributes
  * @param bool $addOnlyItemsInSendTo
  * @param bool $required
  * @return bool
  */
 public function showToForm($form, $sendTo = array(), $attributes = array(), $addOnlyItemsInSendTo = false, $required = false)
 {
     if ($this->type != 'course') {
         return false;
     }
     $order = 'lastname';
     if (api_is_western_name_order()) {
         $order = 'firstname';
     }
     $userList = CourseManager::get_user_list_from_course_code(api_get_course_id(), $this->sessionId, null, $order);
     $groupList = CourseManager::get_group_list_of_course(api_get_course_id(), $this->sessionId);
     $this->setSendToSelect($form, $groupList, $userList, $sendTo, $attributes, $addOnlyItemsInSendTo, $required);
     return true;
 }
 /**
  * this function gets all the groups of the course,
  * not including linked courses
  */
 public static function get_course_groups()
 {
     $session_id = api_get_session_id();
     if ($session_id != 0) {
         $new_group_list = CourseManager::get_group_list_of_course(api_get_course_id(), $session_id, 1);
     } else {
         $new_group_list = CourseManager::get_group_list_of_course(api_get_course_id(), 0, 1);
     }
     return $new_group_list;
 }
 /**
  * @param array $form
  * @param $to_already_selected
  */
 public static function show_to_form($form, $to_already_selected)
 {
     $order = 'lastname';
     if (api_is_western_name_order()) {
         $order = 'firstname';
     }
     $user_list = CourseManager::get_user_list_from_course_code(api_get_course_id(), api_get_session_id(), null, $order);
     $group_list = CourseManager::get_group_list_of_course(api_get_course_id(), api_get_session_id());
     self::construct_not_selected_select_form_validator($form, $group_list, $user_list, $to_already_selected);
 }
}
//Building the form for Users
$formUsers = new \FormValidator('lp_edit', 'post', $url);
$formUsers->addElement('hidden', 'user_form', 1);
$userMultiSelect = $formUsers->addElement('advmultiselect', 'users', get_lang('Users'), $choices);
$formUsers->addButtonSave(get_lang('Save'));
$defaults = array();
if (!empty($selectedChoices)) {
    $defaults['users'] = $selectedChoices;
}
$formUsers->setDefaults($defaults);
//Building the form for Groups
$form = new \FormValidator('lp_edit', 'post', $url);
$form->addElement('hidden', 'group_form', 1);
// Group list
$groupList = \CourseManager::get_group_list_of_course(api_get_course_id(), api_get_session_id(), 1);
$groupChoices = array_column($groupList, 'name', 'id');
// Subscribed groups to a LP
$subscribedGroupsInLp = $em->getRepository('ChamiloCourseBundle:CItemProperty')->getGroupsSubscribedToItem('learnpath', $lpId, $course, $session);
$selectedGroupChoices = array();
/** @var CItemProperty $itemProperty */
foreach ($subscribedGroupsInLp as $itemProperty) {
    $selectedGroupChoices[] = $itemProperty->getGroup()->getId();
}
$groupMultiSelect = $form->addElement('advmultiselect', 'groups', get_lang('Groups'), $groupChoices);
// submit button
$form->addButtonSave(get_lang('Save'));
$defaults = array();
if (!empty($selectedGroupChoices)) {
    $defaults['groups'] = $selectedGroupChoices;
}
function show_to($filter = 0, $id = null)
{
    $order = 'lastname';
    if (api_is_western_name_order()) {
        $order = 'firstname';
    }
    $user_list = CourseManager::get_user_list_from_course_code(api_get_course_id(), api_get_session_id(), null, $order);
    $group_list = CourseManager::get_group_list_of_course(api_get_course_id(), api_get_session_id());
    return construct_to_select_form($group_list, $user_list, $filter, $id);
}
 static function generate_user_group_array($course_code, $session_id = 0)
 {
     $order = api_is_western_name_order() ? 'firstname' : 'lastname';
     $user_list = CourseManager::get_real_and_linked_user_list($course_code, true, $session_id, $order);
     $group_list = CourseManager::get_group_list_of_course($course_code, $session_id, 1);
     $items = self::transform_user_group_array($user_list, $group_list);
     return $items;
 }
 /**
  * Index
  *
  * @param   \Silex\Application $app
  * @param   int $lpId
  *
  * @todo move calls in repositories
  *
  * @return Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
  */
 public function indexAction(Request $request)
 {
     ///$lpId
     $courseId = api_get_course_int_id();
     //@todo use the before filter to aborts this course calls
     if (empty($courseId)) {
         $app->abort(403, 'Course not available');
     }
     $courseCode = api_get_course_id();
     $lp = new \learnpath($courseCode, $lpId, api_get_user_id());
     $url = $app['url_generator']->generate('subscribe_users', array('lpId' => $lpId));
     //Setting breadcrumb @todo move this in the template lib
     $breadcrumb = array(array('url' => api_get_path(WEB_CODE_PATH) . 'newscorm/lp_controller.php?action=list', 'name' => get_lang('LearningPaths')), array('url' => api_get_path(WEB_CODE_PATH) . "newscorm/lp_controller.php?action=build&lp_id=" . $lp->get_id(), 'name' => $lp->get_name()), array('url' => '#', 'name' => get_lang('SubscribeUsers')));
     $app['breadcrumb'] = $breadcrumb;
     // Find session.
     $sessionId = api_get_session_id();
     $session = null;
     if (!empty($sessionId)) {
         $session = $app['orm.em']->getRepository('Chamilo\\CoreBundle\\Entity\\Session')->find($sessionId);
     }
     // Find course.
     $course = $app['orm.em']->getRepository('Chamilo\\CoreBundle\\Entity\\Course')->find($courseId);
     // Getting subscribe users to the course.
     $subscribedUsers = $app['orm.em']->getRepository('Chamilo\\CoreBundle\\Entity\\Course')->getSubscribedStudents($course);
     $subscribedUsers = $subscribedUsers->getQuery();
     $subscribedUsers = $subscribedUsers->execute();
     // Getting all users in a nice format.
     $choices = array();
     foreach ($subscribedUsers as $user) {
         $choices[$user->getUserId()] = $user->getCompleteNameWithClasses();
     }
     // Getting subscribed users to a LP.
     $subscribedUsersInLp = $app['orm.em']->getRepository('Chamilo\\CoreBundle\\Entity\\CItemProperty')->getUsersSubscribedToItem('learnpath', $lpId, $course, $session);
     $selectedChoices = array();
     foreach ($subscribedUsersInLp as $itemProperty) {
         $selectedChoices[] = $itemProperty->getToUserId();
     }
     //Building the form for Users
     $formUsers = new \FormValidator('lp_edit', 'post', $url);
     $formUsers->addElement('hidden', 'user_form', 1);
     $formUsers->addElement('header', get_lang('SubscribeUsersToLp'));
     $userMultiSelect = $formUsers->addElement('advmultiselect', 'users', get_lang('Users'), $choices);
     $userMultiSelect->setButtonAttributes('add');
     $userMultiSelect->setButtonAttributes('remove');
     $formUsers->addElement('style_submit_button', 'submit', get_lang('Save'), 'class="save"');
     $defaults = array();
     if (!empty($selectedChoices)) {
         $defaults['users'] = $selectedChoices;
     }
     $formUsers->setDefaults($defaults);
     //Building the form for Groups
     $form = new \FormValidator('lp_edit', 'post', $url);
     $form->addElement('header', get_lang('SubscribeGroupsToLp'));
     $form->addElement('hidden', 'group_form', 1);
     //Group list
     $groupList = \CourseManager::get_group_list_of_course(api_get_course_id(), api_get_session_id(), 1);
     $groupChoices = array();
     if (!empty($groupList)) {
         foreach ($groupList as $group) {
             $groupChoices[$group['id']] = $group['name'];
         }
     }
     //Subscribed groups to a LP
     $subscribedGroupsInLp = $app['orm.em']->getRepository('Chamilo\\CoreBundle\\Entity\\CItemProperty')->getGroupsSubscribedToItem('learnpath', $lpId, $course, $session);
     $selectedGroupChoices = array();
     foreach ($subscribedGroupsInLp as $itemProperty) {
         $selectedGroupChoices[] = $itemProperty->getToGroupId();
     }
     $groupMultiSelect = $form->addElement('advmultiselect', 'groups', get_lang('Groups'), $groupChoices);
     $groupMultiSelect->setButtonAttributes('add');
     $groupMultiSelect->setButtonAttributes('remove');
     // submit button
     $form->addElement('style_submit_button', 'submit', get_lang('Save'), 'class="save"');
     /*$form = $app['form.factory']->createBuilder('form')
            ->add('origin', 'choice', array(
               'label' => get_lang('Origin'),
               'multiple' => true,
               'required' => false,
               'expanded' => false,
               //'class' => 'Entity\Course',
               //'property' => 'complete_name',
               //'query_builder' => function(Chamilo\CoreBundle\Entity\Repository\CourseRepository $repo) use ($course) {
                   $repo =  $repo->getSubscribedStudents($course);
                   return $repo;
               },
               'choices' => $choices
           ))
           ->add('destination', 'choice', array(
               'label' => get_lang('Destination'),
               'multiple' => true,
               'expanded' => false,
               'required' => false,
               //'class' => 'Entity\Course',
               //'property' => 'complete_name',
               //'query_builder' => function(Chamilo\CoreBundle\Entity\Repository\CourseRepository $repo) use ($course) {
                 //  return $repo->getSubscribedStudents($course);
               //},
               'choices' => $selectedChoices
           ))
           ->getForm();
       */
     $defaults = array();
     if (!empty($selectedGroupChoices)) {
         $defaults['groups'] = $selectedGroupChoices;
     }
     $form->setDefaults($defaults);
     if ($request->getMethod() == 'POST') {
         //Subscribing users
         $users = $request->get('users');
         $userForm = $request->get('user_form');
         if (!empty($userForm)) {
             $app['orm.em']->getRepository('Chamilo\\CoreBundle\\Entity\\CItemProperty')->subscribeUsersToItem('learnpath', $course, $session, $lpId, $users);
         }
         //Subscribing groups
         $groups = $request->get('groups');
         $groupForm = $request->get('group_form');
         if (!empty($groupForm)) {
             $app['orm.em']->getRepository('Chamilo\\CoreBundle\\Entity\\CItemProperty')->subscribeGroupsToItem('learnpath', $course, $session, $lpId, $groups);
         }
         return $app->redirect($url);
     } else {
         $app['template']->assign('formUsers', $formUsers->toHtml());
         $app['template']->assign('formGroups', $form->toHtml());
     }
     $response = $app['template']->render_template('learnpath/subscribe_users.tpl');
     return new Response($response, 200, array());
 }
/**
 * this function gets all the groups of the course
 * @author Patrick Cool <*****@*****.**>, Ghent University
 * @return array
 */
function get_course_groups()
{
    $group_list = CourseManager::get_group_list_of_course(api_get_course_id(), api_get_session_id());
    return $group_list;
}