Open Journal Systems  3.3.0
CounterReport.inc.php
1 <?php
2 
14 require_once(dirname(dirname(__FILE__)).'/classes/COUNTER/COUNTER.php');
15 
16 define('COUNTER_EXCEPTION_WARNING', 0);
17 define('COUNTER_EXCEPTION_ERROR', 1);
18 define('COUNTER_EXCEPTION_PARTIAL_DATA', 4);
19 define('COUNTER_EXCEPTION_NO_DATA', 8);
20 define('COUNTER_EXCEPTION_BAD_COLUMNS', 16);
21 define('COUNTER_EXCEPTION_BAD_FILTERS', 32);
22 define('COUNTER_EXCEPTION_BAD_ORDERBY', 64);
23 define('COUNTER_EXCEPTION_BAD_RANGE', 128);
24 define('COUNTER_EXCEPTION_INTERNAL', 256);
25 
26 define('COUNTER_CLASS_PREFIX', 'CounterReport');
27 
28 // COUNTER as of yet is not internationalized and requires English constants
29 define('COUNTER_LITERAL_ARTICLE', 'Article');
30 define('COUNTER_LITERAL_JOURNAL', 'Journal');
31 define('COUNTER_LITERAL_PROPRIETARY', 'Proprietary');
32 
34 
38  var $_release;
39 
43  var $_errors;
44 
49  function __construct($release) {
50  $this->_release = $release;
51  }
52 
53 
58  function getRelease() {
59  return $this->_release;
60  }
61 
66  function getCode() {
67  return substr(get_class($this), strlen(COUNTER_CLASS_PREFIX));
68  }
69 
75  function getKeyForFiletype($filetype) {
76  switch ($filetype) {
77  case STATISTICS_FILE_TYPE_HTML:
78  $metricTypeKey = 'ft_html';
79  break;
80  case STATISTICS_FILE_TYPE_PDF:
81  $metricTypeKey = 'ft_pdf';
82  break;
83  case STATISTICS_FILE_TYPE_OTHER:
84  default:
85  $metricTypeKey = 'other';
86  }
87  return $metricTypeKey;
88  }
89 
95  function getTitle() {
96  assert(false);
97  }
98 
99  /*
100  * Convert an OJS metrics request to COUNTER ReportItems
101  * Abstract method must be implemented by subclass
102  * @param $columns string|array column (aggregation level) selection
103  * @param $filters array report-level filter selection
104  * @param $orderBy array order criteria
105  * @param $range null|DBResultRange paging specification
106  * @see ReportPlugin::getMetrics for more details on parameters
107  * @return array COUNTER\ReportItem array
108  */
109  function getReportItems($columns = array(), $filters = array(), $orderBy = array(), $range = null) {
110  assert(false);
111  }
112 
117  function getErrors() {
118  return $this->_errors ? $this->_errors : array();
119  }
120 
125  function setError($error) {
126  if (!$this->_errors) {
127  $this->_errors = array();
128  }
129  array_push($this->_errors, $error);
130  }
131 
137  protected function filterForContext($filters) {
138  $request = Application::get()->getRequest();
139  $journal = $request->getContext();
140  $journalId = $journal ? $journal->getId() : '';
141  // If the request context is at the journal level, the dimension context id must be that same journal id
142  if ($journalId) {
143  if (isset($filters[STATISTICS_DIMENSION_CONTEXT_ID]) && $filters[STATISTICS_DIMENSION_CONTEXT_ID] != $journalId) {
144  $this->setError(new Exception(__('plugins.reports.counter.generic.exception.filter'), COUNTER_EXCEPTION_WARNING | COUNTER_EXCEPTION_BAD_FILTERS));
145  }
146  $filters[STATISTICS_DIMENSION_CONTEXT_ID] = $journalId;
147  }
148  return $filters;
149  }
150 
157  protected function createMetricByMonth($period, $counters) {
158  $metric = array();
159  try {
160  $metric = new COUNTER\Metric(
161  // Date range for JR1 is beginning of the month to end of the month
162  new COUNTER\DateRange(
163  DateTime::createFromFormat('Ymd His', $period.'01 000000'),
164  DateTime::createFromFormat('Ymd His', $period.date('t', strtotime(substr($period, 0, 4).'-'.substr($period, 4).'-01')).' 235959')
165  ),
166  'Requests',
167  $counters
168  );
169  } catch (Exception $e) {
170  $this->setError($e, COUNTER_EXCEPTION_ERROR | COUNTER_EXCEPTION_INTERNAL);
171  }
172  return $metric;
173  }
174 
180  function createXML($reportItems) {
181  $errors = $this->getErrors();
182  $fatal = false;
183  foreach ($errors as $error) {
184  if ($error->getCode() & COUNTER_EXCEPTION_ERROR) {
185  $fatal = true;
186  }
187  }
188  if (!$fatal) {
189  try {
190  $report = new COUNTER\Reports(
191  new COUNTER\Report(
193  $this->getRelease(),
194  $this->getCode(),
195  $this->getTitle(),
196  new COUNTER\Customer(
197  '0', // customer id is unused
198  $reportItems,
199  __('plugins.reports.counter.allCustomers')
200  ),
201  new COUNTER\Vendor(
202  $this->getVendorID(),
203  $this->getVendorName(),
204  $this->getVendorContacts(),
205  $this->getVendorWebsiteUrl(),
206  $this->getVendorLogoUrl()
207  )
208  )
209  );
210  } catch (Exception $e) {
211  $this->setError($e, COUNTER_EXCEPTION_ERROR | COUNTER_EXCEPTION_INTERNAL);
212  }
213  if (isset($report)) {
214  return (string) $report;
215  }
216  }
217  return;
218  }
219 
224  function getVendorId() {
225  return $this->_getVendorComponent('id');
226  }
227 
232  function getVendorName() {
233  return (string) $this->_getVendorComponent('name');
234  }
235 
240  function getVendorContacts() {
241  return $this->_getVendorComponent('contacts');
242  }
243 
248  function getVendorWebsiteUrl() {
249  return $this->_getVendorComponent('website');
250  }
251 
256  function getVendorLogoUrl() {
257  return $this->_getVendorComponent('logo');
258  }
259 
265  function _getVendorComponent($key) {
266  $request = Application::get()->getRequest();
267  $site = $request->getSite();
268  $context = $request->getContext();
269  $contextDao = Application::getContextDAO();
270  $availableContexts = $contextDao->getAvailable();
271  switch ($key) {
272  case 'name':
273  if ($availableContexts->getCount() > 1) {
274  $name = $site->getLocalizedTitle();
275  } else {
276  $name = $context->getData('publisherInstitution');
277  if (empty($name)) {
278  $name = $context->getLocalizedName();
279  }
280  }
281  return $name;
282  case 'id':
283  return $request->getBaseUrl();
284  case 'contacts':
285  try {
286  if ($availableContexts->getCount() > 1) {
287  $contactName = $site->getLocalizedContactName();
288  $contactEmail = $site->getLocalizedContactEmail();
289  } else {
290  $contactName = $context->getContactName();
291  $contactEmail = $context->getContactEmail();
292  }
293  $contact = new COUNTER\Contact($contactName, $contactEmail);
294  } catch (Exception $e) {
295  $this->setError($e);
296  $contact = array();
297  }
298  return $contact;
299  case 'website':
300  return $request->getBaseUrl();
301  case 'logo':
302  return '';
303  default:
304  return;
305  }
306  }
307 
308 }
309 
310 
Application\getContextDAO
static getContextDAO()
Definition: Application.inc.php:137
CounterReport\getVendorLogoUrl
getVendorLogoUrl()
Definition: CounterReport.inc.php:262
CounterReport\getVendorWebsiteUrl
getVendorWebsiteUrl()
Definition: CounterReport.inc.php:254
CounterReport\getTitle
getTitle()
Definition: CounterReport.inc.php:101
COUNTER\Reports
Definition: COUNTER.php:334
CounterReport\createXML
createXML($reportItems)
Definition: CounterReport.inc.php:186
CounterReport
A COUNTER report, base class.
Definition: CounterReport.inc.php:33
CounterReport\_getVendorComponent
_getVendorComponent($key)
Definition: CounterReport.inc.php:271
COUNTER\Metric
Definition: COUNTER.php:1711
CounterReport\getErrors
getErrors()
Definition: CounterReport.inc.php:123
CounterReport\filterForContext
filterForContext($filters)
Definition: CounterReport.inc.php:143
CounterReport\setError
setError($error)
Definition: CounterReport.inc.php:131
CounterReport\getReportItems
getReportItems($columns=array(), $filters=array(), $orderBy=array(), $range=null)
Definition: CounterReport.inc.php:115
CounterReport\getVendorName
getVendorName()
Definition: CounterReport.inc.php:238
CounterReport\$_errors
$_errors
Definition: CounterReport.inc.php:49
CounterReport\$_release
$_release
Definition: CounterReport.inc.php:41
CounterReport\__construct
__construct($release)
Definition: CounterReport.inc.php:55
COUNTER
Definition: COUNTER.php:20
PKPString\generateUUID
static generateUUID()
Definition: PKPString.inc.php:481
CounterReport\getRelease
getRelease()
Definition: CounterReport.inc.php:64
CounterReport\getKeyForFiletype
getKeyForFiletype($filetype)
Definition: CounterReport.inc.php:81
COUNTER\Contact
Definition: COUNTER.php:654
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
CounterReport\createMetricByMonth
createMetricByMonth($period, $counters)
Definition: CounterReport.inc.php:163
CounterReport\getCode
getCode()
Definition: CounterReport.inc.php:72
CounterReport\getVendorId
getVendorId()
Definition: CounterReport.inc.php:230
CounterReport\getVendorContacts
getVendorContacts()
Definition: CounterReport.inc.php:246