Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_calculate_average_comment_rating.inc
1 <?php
17 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
18 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
19 
20 
34 {
35 
36 
58  public static function execute($settings, &$state)
59  {
60  // check required settings
61  if (empty($settings['fieldid']['assetid'])) {
62  return FALSE;
63  } else {
64  $fieldid = $settings['fieldid']['assetid'];
65  }
66  if (empty($settings['totalid']['assetid'])) {
67  return FALSE;
68  } else {
69  $totalid = $settings['totalid']['assetid'];
70  }
71 
72  // Define $success here so we know we have success, if the trigger fails it should report to us further down
73  $success = 1;
74 
75  // set the metadata value on all immediate parents if they have the schema applied
76  if (!empty($state['immediate_parents'])) {
77  foreach ($state['immediate_parents'] as $parentid => $type_code) {
78  // get all comment ratings and calculate average
79  $comment_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($parentid, SQ_SC_LINK_ALL, 'comment');
80  $numofratings = 0;
81  $sumofratings = 0;
82  $assetdeleted = 0;
83  // check if we are deleting an asset
84  if ($state['event']['name'] === 'trigger_event_before_asset_deleted') {
85  // remove this asset from our list of links to process
86  $assetdeleted = $state['assetid'];
87  }
88 
89  foreach ($comment_links as $link_info) {
90  // skip asset if we are deleting it
91  if ($link_info['minorid'] === $assetdeleted) continue;
92  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($link_info['minorid']);
93  $rating = $asset->attr('rating');
94  // means this rating hasn't been set, i.e. default value
95  if ($rating === '-1') continue;
96  // include the asset to total assets only if we are getting the rating from it
97  // unrated comment should be kept at distance
98  $numofratings++;
99  $sumofratings += $rating;
100  }
101 
102  // check first $numofratings is not zero because you cannot divide by zero (ie. no items found)
103  if ($numofratings > 0) {
104  // set the average for this parent (percentage)
105  $avgofratings = (int) ($sumofratings/$numofratings);
106  // returned array data
107  $parentdata[$parentid] = $avgofratings;
108  } else {
109  $avgofratings = 0;
110  $numofratings = 0;
111  $parentdata[$parentid] = 0;
112  }
113 
114  // Update the metadata fields
115  $mm =& $GLOBALS['SQ_SYSTEM']->getMetadataManager();
116  $metadata = Array(
117  $fieldid => Array(
118  0 => Array(
119  'value' => $avgofratings,
120  ),
121  ),
122  $totalid => Array(
123  0 => Array(
124  'value' => $numofratings,
125  ),
126  ),
127  );
128 
129  // get the parent schema of this metadata field
130  $schemaid = $GLOBALS['SQ_SYSTEM']->am->getParents($fieldid, 'metadata_schema');
131  // get the schemas that apply to this asset
132  $schemas = $mm->getSchemas($parentid, TRUE);
133 
134  if (array_intersect($schemas, array_keys($schemaid)) !== Array()) {
135  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
136  $success = $mm->setMetadata($parentid, $metadata);
137  if ($success) {
138  $fieldid_field = $GLOBALS['SQ_SYSTEM']->am->getAsset($fieldid);
139  $totalid_field = $GLOBALS['SQ_SYSTEM']->am->getAsset($totalid);
140  // If both of these two fields are contextable, regenerate the metadata file in the current context only;
141  // otherwise, regenerate all metadata files
142  if ($fieldid_field->attr('is_contextable') && $totalid_field->attr('is_contextable')) {
143  $mm->generateContentFile($parentid);
144  } else {
145  $all_contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
146  foreach ($all_contexts as $contextid => $context_data) {
147  $mm->generateContentFile($parentid, FALSE, $contextid);
148  }
149  }
150  }
151  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
152  }
153 
154  $success_results = Array(
155  'fieldid' => $fieldid,
156  'totalid' => $totalid,
157  'parentdata' => $parentdata,
158  );
159  }//end foreach
160  }//end if
161 
162  if ($success) {
163  return $success_results;
164  } else {
165  return FALSE;
166  }
167 
168  }//end execute()
169 
170 
181  public static function getInterface($settings, $prefix, $write_access=FALSE)
182  {
183  $fieldid = array_get_index($settings, 'fieldid', '0');
184  $totalid = array_get_index($settings, 'totalid', '0');
185  ob_start();
186 
187  // The average rating field store
188  echo translate('trigger_avg_comment_rating_metadata_field');
189  if ($write_access) {
190  $type_code = Array(
191  'metadata_field_text' => 'I',
192  );
193  if (isset($fieldid['assetid'])) {
194  $field_selected_asset = $fieldid['assetid'];
195  } else {
196  $field_selected_asset = 0;
197  }
198  echo asset_finder($prefix.'[fieldid]', $field_selected_asset, $type_code);
199  } else {
200  if (empty($fieldid['assetid'])) {
201  echo '<b>['.translate('no_asset_selected').']</b>';
202  } else {
203  echo get_asset_tag_line($fieldid['assetid']);
204  }
205  }
206  echo '<br />';
207 
208  // The total comments field store
209  echo translate('trigger_total_comment_rating_metadata_field');
210  if ($write_access) {
211  $type_code = Array(
212  'metadata_field_text' => 'I',
213  );
214  if (isset($totalid['assetid'])) {
215  $total_selected_asset = $totalid['assetid'];
216  } else {
217  $total_selected_asset = 0;
218  }
219  echo asset_finder($prefix.'[totalid]', $total_selected_asset, $type_code);
220  } else {
221  if (empty($totalid['assetid'])) {
222  echo '<b>['.translate('no_asset_selected').']</b>';
223  } else {
224  echo get_asset_tag_line($totalid['assetid']);
225  }
226  }
227  echo '<br />';
228 
229  return ob_get_clean();
230 
231  }//end getInterface()
232 
233 
245  public static function processInterface(&$settings, $request_data)
246  {
247  // Store the average field
248  $fieldid = array_get_index($request_data, 'fieldid', '');
249  if (empty($fieldid)) return FALSE;
250  $settings['fieldid'] = $fieldid;
251 
252  // Store the total field
253  $totalid = array_get_index($request_data, 'totalid', '');
254  if (empty($totalid)) return FALSE;
255  $settings['totalid'] = $totalid;
256 
257  return FALSE;
258 
259  }//end processInterface()
260 
261 
271  public static function getLocks($settings, &$state)
272  {
273  return Array();
274 
275  }//end getLocks()
276 
277 
278 }//end class