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