Open Journal Systems  3.3.0
CounterReportPlugin.inc.php
1 <?php
2 
16 define('OJS_METRIC_TYPE_LEGACY_COUNTER', 'ojs::legacyCounterPlugin');
17 
18 define('COUNTER_CLASS_SUFFIX', '.inc.php');
19 
20 import('lib.pkp.classes.plugins.ReportPlugin');
21 import('plugins.reports.counter.classes.CounterReport');
22 
24 
28  function register($category, $path, $mainContextId = null) {
29  $success = parent::register($category, $path, $mainContextId);
30  if($success) {
31  $this->addLocaleData();
32  }
33  return $success;
34  }
35 
39  function getLocaleFilename($locale) {
40  $localeFilenames = parent::getLocaleFilename($locale);
41  // Add dynamic locale keys.
42  foreach (glob($this->getPluginPath() . DIRECTORY_SEPARATOR . 'locale' . DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . '*.xml') as $file) {
43  if (!in_array($file, $localeFilenames)) {
44  $localeFilenames[] = $file;
45  }
46  }
47  return $localeFilenames;
48  }
49 
53  function getName() {
54  return 'CounterReportPlugin';
55  }
56 
60  function getDisplayName() {
61  return __('plugins.reports.counter');
62  }
63 
67  function getDescription() {
68  return __('plugins.reports.counter.description');
69  }
70 
75  function getCurrentRelease() {
76  return '4.1';
77  }
78 
84  function getValidReports() {
85  $reports = array();
86  $prefix = $this->getReportPath().DIRECTORY_SEPARATOR.COUNTER_CLASS_PREFIX;
87  $suffix = COUNTER_CLASS_SUFFIX;
88  foreach (glob($prefix.'*'.$suffix) as $file) {
89  $report_name = substr($file, strlen($prefix), -strlen($suffix));
90  $report_class_file = substr($file, strlen($prefix), -strlen(COUNTER_CLASS_SUFFIX));
91  $reports[$report_name] = $report_class_file;
92  }
93  return $reports;
94  }
95 
103  function getReporter($report, $release) {
104  $reportClass = COUNTER_CLASS_PREFIX.$report;
105  $reportClasspath = 'plugins.reports.counter.classes.reports.';
106  $reportPath = str_replace('.', DIRECTORY_SEPARATOR, $reportClasspath);
107  if (file_exists($reportPath.$reportClass.COUNTER_CLASS_SUFFIX)) {
108  import($reportPath.$reportClass);
109  $reporter = new $reportClass($release);
110  return $reporter;
111  }
112  return false;
113  }
114 
119  function getClassPath() {
120  return $this->getPluginPath() . DIRECTORY_SEPARATOR . 'classes';
121  }
122 
123 
128  function getReportPath() {
129  return $this->getClassPath().DIRECTORY_SEPARATOR.'reports';
130  }
131 
135  function display($args, $request) {
136  // We need these constants
137  import('classes.statistics.StatisticsHelper');
138 
139  $available = $this->getValidReports();
140  $years = $this->_getYears();
141  if ($request->getUserVar('type')) {
142  $type = (string) $request->getUserVar('type');
143  $errormessage = '';
144  switch ($type) {
145  case 'report':
146  case 'reportxml':
147  // Legacy COUNTER Release 3
148  if (!Validation::isSiteAdmin()) {
149  // Legacy reports are site-wide
151  }
152  import('plugins.reports.counter.classes.LegacyJR1');
153  $r3jr1 = new LegacyJR1($this);
154  $r3jr1->display($request);
155  return;
156  case 'fetch':
157  // Modern COUNTER Releases
158  // must provide a release, report, and year parameter
159  $release = $request->getUserVar('release');
160  $report = $request->getUserVar('report');
161  $year = $request->getUserVar('year');
162  if ($release && $report && $year) {
163  // release, report and year parameters must be sane
164  if ($release == $this->getCurrentRelease() && isset($available[$report]) && in_array($year, $years)) {
165  // try to get the report
166  $reporter = $this->getReporter($report, $release);
167  if ($reporter) {
168  // default report parameters with a yearlong range
169  $reportItems = $reporter->getReportItems(array(), array(STATISTICS_DIMENSION_MONTH => array('from' => $year.'01', 'to' => $year.'12')));
170  if ($reportItems) {
171  $xmlResult = $reporter->createXML($reportItems);
172  if ($xmlResult) {
173  header('content-type: text/xml');
174  header('content-disposition: attachment; filename=counter-'. $release . '-' . $report . '-' . date('Ymd') . '.xml');
175  print $xmlResult;
176  return;
177  } else {
178  $errormessage = __('plugins.reports.counter.error.noXML');
179  }
180  } else {
181  $errormessage = __('plugins.reports.counter.error.noResults');
182  }
183  }
184  }
185  }
186  // fall through to default case with error message
187  if (!$errormessage) {
188  $errormessage = __('plugins.reports.counter.error.badParameters');
189  }
190  default:
191  if (!$errormessage) {
192  $errormessage = __('plugins.reports.counter.error.badRequest');
193  }
194  $user = $request->getUser();
195  import('classes.notification.NotificationManager');
196  $notificationManager = new NotificationManager();
197  $notificationManager->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_ERROR, array('contents' => $errormessage));
198  }
199  }
200  $legacyYears = $this->_getYears(true);
201  $templateManager = TemplateManager::getManager();
202  krsort($available);
203  $templateManager->assign('pluginName', $this->getName());
204  $templateManager->assign('available', $available);
205  $templateManager->assign('release', $this->getCurrentRelease());
206  $templateManager->assign('years', $years);
207  // legacy reports are site-wide, so only site admins have access
208  $templateManager->assign('showLegacy', Validation::isSiteAdmin());
209  if (!empty($legacyYears)) $templateManager->assign('legacyYears', $legacyYears);
210  $templateManager->assign('pageTitle', __('plugins.reports.counter'));
211  $templateManager->display($this->getTemplateResource('index.tpl'));
212  }
213 
219  function _getYears($useLegacyStats = false) {
220  if ($useLegacyStats) {
221  $metricType = OJS_METRIC_TYPE_LEGACY_COUNTER;
222  $filter = array();
223  } else {
224  $metricType = METRIC_TYPE_COUNTER;
225  $filter = array(STATISTICS_DIMENSION_ASSOC_TYPE => ASSOC_TYPE_SUBMISSION_FILE);
226  }
227  $metricsDao = DAORegistry::getDAO('MetricsDAO'); /* @var $metricsDao MetricsDAO */
228  $results = $metricsDao->getMetrics($metricType, array(STATISTICS_DIMENSION_MONTH), $filter);
229  $years = array();
230  foreach($results as $record) {
231  $year = substr($record['month'], 0, 4);
232  if (in_array($year, $years)) continue;
233  $years[] = $year;
234  }
235  return $years;
236  }
237 
238 }
239 
240 
CounterReportPlugin\getDescription
getDescription()
Definition: CounterReportPlugin.inc.php:67
Validation\isSiteAdmin
static isSiteAdmin()
Definition: Validation.inc.php:400
Validation\redirectLogin
static redirectLogin($message=null)
Definition: Validation.inc.php:168
CounterReportPlugin\_getYears
_getYears($useLegacyStats=false)
Definition: CounterReportPlugin.inc.php:219
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
CounterReportPlugin\display
display($args, $request)
Definition: CounterReportPlugin.inc.php:135
CounterReportPlugin\getClassPath
getClassPath()
Definition: CounterReportPlugin.inc.php:119
ReportPlugin
Abstract class for report plugins.
Definition: ReportPlugin.inc.php:18
CounterReportPlugin\getDisplayName
getDisplayName()
Definition: CounterReportPlugin.inc.php:60
CounterReportPlugin\getReporter
getReporter($report, $release)
Definition: CounterReportPlugin.inc.php:103
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
CounterReportPlugin
Counter report plugin.
Definition: CounterReportPlugin.inc.php:23
Plugin\getTemplateResource
getTemplateResource($template=null, $inCore=false)
Definition: Plugin.inc.php:349
Plugin\getPluginPath
getPluginPath()
Definition: Plugin.inc.php:330
Plugin\$request
$request
Definition: Plugin.inc.php:68
Plugin\addLocaleData
addLocaleData($locale=null)
Definition: Plugin.inc.php:454
NotificationManager
Definition: NotificationManager.inc.php:19
CounterReportPlugin\getLocaleFilename
getLocaleFilename($locale)
Definition: CounterReportPlugin.inc.php:39
LegacyJR1
The Legacy COUNTER JR1 (r3) report.
Definition: LegacyJR1.inc.php:15
CounterReportPlugin\getName
getName()
Definition: CounterReportPlugin.inc.php:53
CounterReportPlugin\getCurrentRelease
getCurrentRelease()
Definition: CounterReportPlugin.inc.php:75
CounterReportPlugin\getValidReports
getValidReports()
Definition: CounterReportPlugin.inc.php:84
CounterReportPlugin\getReportPath
getReportPath()
Definition: CounterReportPlugin.inc.php:128