Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
PKPStatisticsHelper.inc.php
1 <?php
2 
17 // Dimensions:
18 // 1) publication object dimension:
19 define('STATISTICS_DIMENSION_CONTEXT_ID', 'context_id');
20 define('STATISTICS_DIMENSION_PKP_SECTION_ID', 'pkp_section_id');
21 define('STATISTICS_DIMENSION_ASSOC_OBJECT_TYPE', 'assoc_object_type');
22 define('STATISTICS_DIMENSION_ASSOC_OBJECT_ID', 'assoc_object_id');
23 define('STATISTICS_DIMENSION_SUBMISSION_ID', 'submission_id');
24 define('STATISTICS_DIMENSION_REPRESENTATION_ID', 'representation_id');
25 define('STATISTICS_DIMENSION_ASSOC_TYPE', 'assoc_type');
26 define('STATISTICS_DIMENSION_ASSOC_ID', 'assoc_id');
27 define('STATISTICS_DIMENSION_FILE_TYPE', 'file_type');
28 // 2) time dimension:
29 define('STATISTICS_DIMENSION_MONTH', 'month');
30 define('STATISTICS_DIMENSION_DAY', 'day');
31 // 3) geography dimension:
32 define('STATISTICS_DIMENSION_COUNTRY', 'country_id');
33 define('STATISTICS_DIMENSION_REGION', 'region');
34 define('STATISTICS_DIMENSION_CITY', 'city');
35 // 4) metric type dimension (non-additive!):
36 define('STATISTICS_DIMENSION_METRIC_TYPE', 'metric_type');
37 
38 // Metrics:
39 define('STATISTICS_METRIC', 'metric');
40 
41 // Odering:
42 define('STATISTICS_ORDER_ASC', 'ASC');
43 define('STATISTICS_ORDER_DESC', 'DESC');
44 
45 // Global report size limit:
46 define('STATISTICS_MAX_ROWS', 5000);
47 
48 // File type to be used in publication object dimension.
49 define('STATISTICS_FILE_TYPE_HTML', 1);
50 define('STATISTICS_FILE_TYPE_PDF', 2);
51 define('STATISTICS_FILE_TYPE_OTHER', 3);
52 define('STATISTICS_FILE_TYPE_DOC', 4);
53 
54 // Geography.
55 define('STATISTICS_UNKNOWN_COUNTRY_ID', 'ZZ');
56 
57 // Constants used to filter time dimension to current time.
58 define('STATISTICS_CURRENT_DAY', 'currentDay');
59 define('STATISTICS_CURRENT_MONTH', 'currentMonth');
60 
61 abstract class PKPStatisticsHelper {
62 
63  function PKPStatisticsHelper() {
64  }
65 
79  function &getContext($filter) {
80  // Check whether the report is on context level.
81  $context = null;
82  if (isset($filter[STATISTICS_DIMENSION_CONTEXT_ID])) {
83  $contextFilter = $filter[STATISTICS_DIMENSION_CONTEXT_ID];
84  if (is_scalar($contextFilter)) {
85  // Retrieve the context object.
86  $contextDao = Application::getContextDAO(); /* @var $contextDao ContextDAO */
87  $context = $contextDao->getById($contextFilter);
88  }
89  }
90  return $context;
91  }
92 
103  function canonicalizeMetricTypes($metricType, $context, $defaultSiteMetricType, $siteMetricTypes) {
104  // Metric type is null: Return the default metric for
105  // the filtered context.
106  if (is_null($metricType)) {
107  if (is_a($context, 'Context')) {
108  $metricType = $context->getDefaultMetricType();
109  } else {
110  $metricType = $defaultSiteMetricType;
111  }
112  }
113 
114  // Canonicalize the metric type to an array of metric types.
115  if (!is_null($metricType)) {
116  if (is_scalar($metricType) && $metricType !== '*') {
117  // Metric type is a scalar value: Select a single metric.
118  $metricType = array($metricType);
119 
120  } elseif ($metricType === '*') {
121  // Metric type is '*': Select all available metrics.
122  if (is_a($context, 'Context')) {
123  $metricType = $context->getMetricTypes();
124  } else {
125  $metricType = $siteMetricTypes;
126  }
127 
128  } else {
129  // Only arrays are otherwise supported as metric type
130  // specification.
131  if (!is_array($metricType)) $metricType = null;
132 
133  // Metric type is an array: Select multiple metrics. This is the
134  // canonical format so no change is required.
135  }
136  }
137 
138  return $metricType;
139  }
140 
147  function &getReportPluginByMetricType($metricType) {
148  $returner = null;
149 
150  // Retrieve site-level report plugins.
151  $reportPlugins = PluginRegistry::loadCategory('reports', true, CONTEXT_SITE);
152  if (!is_array($reportPlugins) || empty($metricType)) {
153  return $returner;
154  }
155 
156  if (is_scalar($metricType)) {
157  $metricType = array($metricType);
158  }
159 
160  foreach ($reportPlugins as $reportPlugin) {
161  /* @var $reportPlugin ReportPlugin */
162  $pluginMetricTypes = $reportPlugin->getMetricTypes();
163  $metricTypeMatches = array_intersect($pluginMetricTypes, $metricType);
164  if (!empty($metricTypeMatches)) {
165  $returner = $reportPlugin;
166  break;
167  }
168  }
169 
170  return $returner;
171  }
172 
180  $allMetricTypes = array();
181  $reportPlugins = PluginRegistry::loadCategory('reports', true, CONTEXT_SITE);
182  if (is_array($reportPlugins)) {
183  foreach ($reportPlugins as $reportPlugin) {
184  /* @var $reportPlugin ReportPlugin */
185  $reportMetricTypes = $reportPlugin->getMetricTypes();
186  foreach ($reportMetricTypes as $metricType) {
187  $allMetricTypes[$metricType] = $reportPlugin->getMetricDisplayType($metricType);
188  }
189  }
190  }
191 
192  return $allMetricTypes;
193  }
194 
200  function getColumnNames($column = null) {
201  $columns = $this->getReportColumnsArray();
202 
203  if ($column) {
204  if (isset($columns[$column])) {
205  return $columns[$column];
206  } else {
207  return null;
208  }
209  } else {
210  return $columns;
211  }
212  }
213 
219  function getObjectTypeString($assocType = null) {
220  $objectTypes = $this->getReportObjectTypesArray();
221 
222  if (is_null($assocType)) {
223  return $objectTypes;
224  } else {
225  if (isset($objectTypes[$assocType])) {
226  return $objectTypes[$assocType];
227  } else {
228  assert(false);
229  }
230  }
231  }
232 
238  function getFileTypeString($fileType = null) {
239  $fileTypeArray = $this->getFileTypesArray();
240 
241  if (is_null($fileType)) {
242  return $fileTypeArray;
243  } else {
244  if (isset($fileTypeArray[$fileType])) {
245  return $fileTypeArray[$fileType];
246  } else {
247  assert(false);
248  }
249  }
250  }
251 
262  function getReportUrl($request, $metricType, $columns, $filter, $orderBy = array()) {
263  $dispatcher = $request->getDispatcher(); /* @var $dispatcher Dispatcher */
264  $args = array(
265  'metricType' => $metricType,
266  'columns' => $columns,
267  'filters' => serialize($filter)
268  );
269 
270  if (!empty($orderBy)) {
271  $args['orderBy'] = serialize($orderBy);
272  }
273 
274  return $dispatcher->url($request, ROUTE_PAGE, null, 'management', 'tools', 'generateReport', $args);
275  }
276 
277 
282  function &getGeoLocationTool() {
283  $geoLocationTool = null;
284  $plugin = PluginRegistry::getPlugin('generic', 'usagestatsplugin'); /* @var $plugin UsageStatsPlugin */
285  if (is_a($plugin, 'UsageStatsPlugin')) {
286  $geoLocationTool = $plugin->getGeoLocationTool();
287  }
288  return $geoLocationTool;
289  }
290 
291 
292  //
293  // Protected methods.
294  //
300  protected function getReportColumnsArray() {
301  return array(
302  STATISTICS_DIMENSION_ASSOC_ID => __('common.id'),
303  STATISTICS_DIMENSION_ASSOC_TYPE => __('common.type'),
304  STATISTICS_DIMENSION_FILE_TYPE => __('common.fileType'),
305  STATISTICS_DIMENSION_SUBMISSION_ID => $this->getAppColumnTitle(STATISTICS_DIMENSION_SUBMISSION_ID),
306  STATISTICS_DIMENSION_CONTEXT_ID => $this->getAppColumnTitle(STATISTICS_DIMENSION_CONTEXT_ID),
307  STATISTICS_DIMENSION_PKP_SECTION_ID => $this->getAppColumnTitle(STATISTICS_DIMENSION_PKP_SECTION_ID),
308  STATISTICS_DIMENSION_CITY => __('manager.statistics.city'),
309  STATISTICS_DIMENSION_REGION => __('manager.statistics.region'),
310  STATISTICS_DIMENSION_COUNTRY => __('common.country'),
311  STATISTICS_DIMENSION_DAY => __('common.day'),
312  STATISTICS_DIMENSION_MONTH => __('common.month'),
313  STATISTICS_DIMENSION_METRIC_TYPE => __('common.metric'),
314  STATISTICS_METRIC => __('common.count')
315  );
316  }
317 
323  protected function getReportObjectTypesArray() {
324  return array(
325  ASSOC_TYPE_SUBMISSION_FILE => __('submission.submit.submissionFiles')
326  );
327  }
328 
334  protected function getFileTypesArray() {
335  return array(
336  STATISTICS_FILE_TYPE_PDF => 'PDF',
337  STATISTICS_FILE_TYPE_HTML => 'HTML',
338  STATISTICS_FILE_TYPE_OTHER => __('common.other'),
339  STATISTICS_FILE_TYPE_DOC => 'DOC',
340  );
341  }
342 
348  abstract protected function getAppColumnTitle($column);
349 }
350 
351 ?>
getReportUrl($request, $metricType, $columns, $filter, $orderBy=array())
static & loadCategory($category, $enabledOnly=false, $mainContextId=null)
getAppColumnTitle($column)
& getReportPluginByMetricType($metricType)
static & getPlugin($category, $name)
getObjectTypeString($assocType=null)
Statistics helper class.
static getContextDAO()
canonicalizeMetricTypes($metricType, $context, $defaultSiteMetricType, $siteMetricTypes)