PageRenderTime 123ms CodeModel.GetById 60ms app.highlight 8ms RepoModel.GetById 53ms app.codeStats 0ms

/php/lib/forms/subscribe_group_form.class.php

https://bitbucket.org/chamilo/chamilo-app-survey/
PHP | 140 lines | 109 code | 30 blank | 1 comment | 8 complexity | 9febb542df55690cb81af04f8b597bd3 MD5 | raw file
  1<?php 
  2namespace application\survey;
  3
  4use common\libraries\Utilities;
  5use common\libraries\Path;
  6use common\libraries\Translation;
  7use common\libraries\FormValidator;
  8use group\GroupDataManager;
  9use user\UserDataManager;
 10
 11
 12use rights\RightsUtilities;
 13use rights\RightsDataManager;
 14
 15class SurveySubscribeGroupForm extends FormValidator
 16{
 17    
 18    const APPLICATION_NAME = 'survey';
 19    const PARAM_TARGET = 'target_users_and_groups';
 20    const PARAM_TARGET_OPTION = 'target_users_and_groups_option';
 21    const PARAM_RIGHTS = 'rights';
 22    
 23    private $parent;
 24    private $publication;
 25    private $user;
 26
 27    function __construct($publication, $action, $user)
 28    {
 29        parent :: __construct('subscribe_group', 'post', $action);
 30        
 31        $this->publication = $publication;
 32        $this->user = $user;
 33        $this->build_form();
 34        $this->setDefaults();
 35    }
 36
 37    function build_form()
 38    {
 39        $publication = $this->publication;
 40        
 41        $attributes = array();
 42        $attributes['search_url'] = Path :: get(WEB_PATH) . 'group/php/xml_feeds/xml_group_feed.php';
 43        $locale = array();
 44        $locale['Display'] = Translation :: get('ShareWith');
 45        $locale['Searching'] = Translation :: get('Searching');
 46        $locale['NoResults'] = Translation :: get('NoResults');
 47        $locale['Error'] = Translation :: get('Error');
 48        $attributes['locale'] = $locale;
 49        $attributes['defaults'] = array();
 50        $attributes['options'] = array('load_elements' => false);
 51        
 52        $this->add_receivers(self :: APPLICATION_NAME . '_opt_' . self :: PARAM_TARGET, Translation :: get('SubscribeGroups'), $attributes);
 53        
 54        $rights = SurveyRights :: get_available_rights_for_publications();
 55        foreach ($rights as $right_name => $right)
 56        {
 57            $check_boxes[] = $this->createElement('checkbox', $right, $right_name, $right_name . '  ');
 58        }
 59        $this->addGroup($check_boxes, self :: PARAM_RIGHTS, Translation :: get('Rights'), '&nbsp;', true);
 60        
 61        $buttons[] = $this->createElement('style_submit_button', 'submit', Translation :: get('AddGroups'), array('class' => 'positive update'));
 62        $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset'), array('class' => 'normal empty'));
 63        $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
 64        
 65        $this->addElement('category');
 66        $this->addElement('html', '<br />');
 67        $defaults[self :: APPLICATION_NAME . '_opt_forever'] = 1;
 68        $defaults[self :: APPLICATION_NAME . '_opt_' . self :: PARAM_TARGET_OPTION] = 0;
 69        $this->setDefaults($defaults);
 70    
 71    }
 72
 73    function create_group_rights()
 74    {
 75        
 76        $publication_id = $this->publication->get_id();
 77        
 78        $values = $this->exportValues();
 79        
 80        $succes = false;
 81        
 82        $location_id = SurveyRights :: get_location_id_by_identifier_from_surveys_subtree($publication_id, SurveyRights :: TYPE_PUBLICATION);
 83        
 84        if ($values[self :: APPLICATION_NAME . '_opt_' . self :: PARAM_TARGET_OPTION] == 0)
 85        {
 86            //all users of the system will be subscribed if not allready subscribed
 87            $users = UserDataManager :: get_instance()->retrieve_users();
 88            
 89            while ($user = $users->next_result())
 90            {
 91                $user_id = $user->get_id();
 92                
 93                foreach ($values[self :: PARAM_RIGHTS] as $right => $value)
 94                {
 95                    if ($value == 1)
 96                    {
 97                        $succes = RightsUtilities :: set_user_right_location_value($right, $user_id, $location_id, 1);
 98                    }
 99                }
100            }
101        }
102        else
103        {
104            $group_ids = $values[self :: APPLICATION_NAME . '_opt_' . self :: PARAM_TARGET . '_elements']['group'];
105            
106            if (count($group_ids))
107            {
108                foreach ($group_ids as $group_id)
109                {
110                    $group_user_ids = array();
111                    foreach ($group_ids as $group_id)
112                    {
113                        
114                        $group = GroupDataManager :: get_instance()->retrieve_group($group_id);
115                        $ids = $group->get_users(true, true);
116                        $group_user_ids = array_merge($group_user_ids, $ids);
117                    
118                    }
119                    $user_ids = array_unique($group_user_ids);
120                    
121                    foreach ($user_ids as $user_id)
122                    {
123                        foreach ($values[self :: PARAM_RIGHTS] as $right => $value)
124                        {
125                            if ($value == 1)
126                            {
127                                $succes = RightsUtilities :: set_user_right_location_value($right, $user_id, $location_id, 1);
128                            }
129                        }
130                    }
131                }
132            }
133        }
134        
135        return $succes;
136    }
137
138}
139
140?>