Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_join_user_group_for_session.inc
1 <?php
18 require_once SQ_SYSTEM_ROOT.'/core/attributes/parameter_map/parameter_map.inc';
19 
32 {
39  function __construct($assetid=0)
40  {
41  parent::__construct($assetid);
42  }//end constructor
43 
44 
55  public static function getInterface($settings, $prefix, $write_access=FALSE)
56  {
57  // we only allow user group type
58  $type_codes = Array(
59  'user_group' => 'D',
60  );
61 
62  // buffer the user group asset chooser
63  ob_start();
64  $user_group_to_join = isset($settings['user_group_to_join']) ? $settings['user_group_to_join'] : '';
65  if ($write_access) {
66  asset_finder($prefix.'[user_group_to_join]', $user_group_to_join, $type_codes);
67  } else if (!empty($user_group_to_join)) {
68  $user_group_asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo($user_group_to_join);
69  if (!empty($user_group_asset_info[$user_group_to_join])) {
70  echo '<b>'.$user_group_asset_info[$user_group_to_join]['name'].' (#'.$user_group_to_join.')</b>';
71  }
72  }
73  $user_group_input = ob_get_clean();
74 
75  // buffer the text box for entering keywords that will be replaced with a user group (hopefully)
76  ob_start();
77  $keyword_target_groupid = isset($settings['keyword_target_groupid']) ? $settings['keyword_target_groupid'] : '';
78  if ($write_access) {
79  text_box($prefix.'[keyword_target_groupid]', $keyword_target_groupid, 30);
80  } else {
81  echo htmlspecialchars($keyword_target_groupid);
82  }
83  $keyword_target_groupid_input = ob_get_clean();
84 
85  // display both the asset chooser and the text box
86  $output = translate('trigger_join_user_for_session', $user_group_input, $keyword_target_groupid_input);
87 
88  return $output;
89 
90  }//end getInterface()
91 
92 
104  public static function processInterface(&$settings, $request_data)
105  {
106  $settings['user_group_to_join'] = isset($request_data['user_group_to_join']['assetid']) ? $request_data['user_group_to_join']['assetid'] : NULL;
107  $settings['keyword_target_groupid'] = isset($request_data['keyword_target_groupid']) ? $request_data['keyword_target_groupid'] : '';
108 
109  return FALSE;
110 
111  }//end processInterface()
112 
113 
124  public static function execute($settings, &$state)
125  {
126  // the group id set in asset finder has the precedence
127  $value = @$settings['user_group_to_join'];
128 
129  // if we don't have a group id in asset finder, see if we have something in the text box
130  if (empty($value)) {
131  $value = @$settings['keyword_target_groupid'];
132  if (!empty($value)) {
133  // replace global keywords
134  replace_global_keywords($value);
135 
136  // replace asset keywords
137  $keywords = retrieve_keywords_replacements($value);
138  $replacements = Array();
139  foreach ($keywords as $keyword) {
140  $replacements[$keyword] = $state['asset']->getKeywordReplacement($keyword);
141  }
142  replace_keywords($value, $replacements);
143 
144  // we only allow an id of a User Group asset
145  if (!is_numeric($value)) return FALSE;
146  $user_group = $GLOBALS['SQ_SYSTEM']->am->getAsset($value);
147  if (!$user_group || !$user_group instanceof User_Group) return FALSE;
148  } else {
149  // if we don't have any group set, there's nothing we need to do
150  return FALSE;
151  }
152  }
153 
154  // get the current user id and double check it's a User asset
155  $current_user = $GLOBALS['SQ_SYSTEM']->am->getAsset($GLOBALS['SQ_SYSTEM']->user->id);
156  if (!$current_user || !$current_user instanceof User) {
157  return FALSE;
158  }
159 
160  // get user groups the current user is in
161  $current_groups = $current_user->getUserGroups();
162 
163  // if user is not physically linked to the specifed group
164  // add the current user to the specified user group that lasts only while the current session last
165  if (!in_array($value, $_SESSION['sq_effective_user_groups'][$GLOBALS['SQ_SYSTEM']->user->id])) {
166  $_SESSION['sq_effective_user_groups'][$GLOBALS['SQ_SYSTEM']->user->id][] = $value;
167  }
168 
169  return Array(
170  'group_joined' => $value,
171  );
172 
173  }//end execute()
174 
175 
176 }//end class
177 
178 ?>