Open Journal Systems  3.3.0
LegacyJR1.inc.php
1 <?php
2 
15 class LegacyJR1 {
16 
20  var $_plugin;
21 
26  function LegacyJR1($plugin) {
27  $this->_plugin = $plugin;
28  }
29 
34  function display($request) {
35  $oldStats = (boolean) $request->getUserVar('useOldCounterStats');
36  $year = (string) $request->getUserVar('year');
37  $type = (string) $request->getUserVar('type');
38  switch ($type) {
39  case 'report':
40  $this->_report($request, $year, $oldStats);
41  break;
42  case 'reportxml':
43  $this->_reportXml($request, $year, $oldStats);
44  break;
45  }
46  }
47 
54  function _report($request, $year, $useLegacyStats) {
55  $journal = $request->getContext();
56  list($begin, $end) = $this->_getLimitDates($year);
57 
58  header('content-type: text/comma-separated-values');
59  header('content-disposition: attachment; filename=counter-' . date('Ymd') . '.csv');
60 
61  $fp = fopen('php://output', 'wt');
62  fputcsv($fp, array(__('plugins.reports.counter.1a.title1')));
63  fputcsv($fp, array(__('plugins.reports.counter.1a.title2', array('year' => $year))));
64  fputcsv($fp, array()); // FIXME: Criteria should be here?
65  fputcsv($fp, array(__('plugins.reports.counter.1a.dateRun')));
66  fputcsv($fp, array(strftime("%Y-%m-%d")));
67 
68  $cols = array(
69  '',
70  __('plugins.reports.counter.1a.publisher'),
71  __('plugins.reports.counter.1a.platform'),
72  __('plugins.reports.counter.1a.printIssn'),
73  __('plugins.reports.counter.1a.onlineIssn')
74  );
75  for ($i=1; $i<=12; $i++) {
76  $time = strtotime($year . '-' . $i . '-01');
77  strftime('%b', $time);
78  $cols[] = strftime('%b-%Y', $time);
79  }
80 
81  $cols[] = __('plugins.reports.counter.1a.ytdTotal');
82  $cols[] = __('plugins.reports.counter.1a.ytdHtml');
83  $cols[] = __('plugins.reports.counter.1a.ytdPdf');
84  fputcsv($fp, $cols);
85 
86  // Display the totals first
87  $totals = $this->_getMonthlyTotalRange($begin, $end, $useLegacyStats);
88  $cols = array(
89  __('plugins.reports.counter.1a.totalForAllJournals'),
90  '-', // Publisher
91  '', // Platform
92  '-',
93  '-'
94  );
95  $this->_formColumns($cols, $totals);
96  fputcsv($fp, $cols);
97 
98  // Get statistics from the log.
99  $journalDao = DAORegistry::getDAO('JournalDAO'); /* @var $journalDao JournalDAO */
100  $journalIds = $this->_getJournalIds($useLegacyStats);
101  foreach ($journalIds as $journalId) {
102  $journal = $journalDao->getById($journalId);
103  if (!$journal) continue;
104  $entries = $this->_getMonthlyLogRange($journalId, $begin, $end, $useLegacyStats);
105  $cols = array(
106  $journal->getLocalizedName(),
107  $journal->getData('publisherInstitution'),
108  __('common.software'), // Platform
109  $journal->getData('printIssn'),
110  $journal->getData('onlineIssn')
111  );
112  $this->_formColumns($cols, $entries);
113  fputcsv($fp, $cols);
114  unset($journal, $entry);
115  }
116 
117  fclose($fp);
118  }
119 
126  function _formColumns(&$cols, $entries) {
127  $allMonthsTotal = 0;
128  $currTotal = 0;
129  $htmlTotal = 0;
130  $pdfTotal = 0;
131  for ($i = 1; $i <= 12; $i++) {
132  $currTotal = 0;
133  foreach ($entries as $entry) {
134  $month = (int) substr($entry[STATISTICS_DIMENSION_MONTH], 4, 2);
135  if ($i == $month) {
136  $metric = $entry[STATISTICS_METRIC];
137  $currTotal += $metric;
138  if ($entry[STATISTICS_DIMENSION_FILE_TYPE] == STATISTICS_FILE_TYPE_HTML) {
139  $htmlTotal += $metric;
140  } else if ($entry[STATISTICS_DIMENSION_FILE_TYPE] == STATISTICS_FILE_TYPE_PDF) {
141  $pdfTotal += $metric;
142  }
143  }
144  }
145  $cols[]=$currTotal;
146  $allMonthsTotal += $currTotal;
147  }
148  $cols[] = $allMonthsTotal;
149  $cols[] = $htmlTotal;
150  $cols[] = $pdfTotal;
151  }
152 
161  function _assignTemplateCounterXML($request, $templateManager, $begin, $end='', $useLegacyStats) {
162  $journal = $request->getContext();
163 
164  $journalDao = DAORegistry::getDAO('JournalDAO'); /* @var $journalDao JournalDAO */
165  $journalIds = $this->_getJournalIds($useLegacyStats);
166 
167  $site = $request->getSite();
168  $availableContexts = $journalDao->getAvailable();
169  if ($availableContexts->getCount() > 1) {
170  $vendorName = $site->getLocalizedTitle();
171  } else {
172  $vendorName = $journal->getData('publisherInstitution');
173  if (empty($vendorName)) {
174  $vendorName = $journal->getLocalizedName();
175  }
176  }
177 
178  if ($end == '') $end = $begin;
179 
180  $i=0;
181 
182  foreach ($journalIds as $journalId) {
183  $journal = $journalDao->getById($journalId);
184  if (!$journal) continue;
185  $entries = $this->_getMonthlyLogRange($journalId, $begin, $end, $useLegacyStats);
186 
187  $journalsArray[$i]['entries'] = $this->_arrangeEntries($entries);
188  $journalsArray[$i]['journalTitle'] = $journal->getLocalizedName();
189  $journalsArray[$i]['publisherInstitution'] = $journal->getData('publisherInstitution');
190  $journalsArray[$i]['printIssn'] = $journal->getData('printIssn');
191  $journalsArray[$i]['onlineIssn'] = $journal->getData('onlineIssn');
192  $i++;
193  }
194 
195  $base_url = Config::getVar('general','base_url');
196 
197  $reqUser = $request->getUser();
198  if ($reqUser) {
199  $templateManager->assign('reqUserName', $reqUser->getUsername());
200  $templateManager->assign('reqUserId', $reqUser->getId());
201  } else {
202  $templateManager->assign('reqUserName', __('plugins.reports.counter.1a.anonymous'));
203  $templateManager->assign('reqUserId', '');
204  }
205 
206  $templateManager->assign('journalsArray', $journalsArray);
207 
208  $templateManager->assign('vendorName', $vendorName);
209  $templateManager->assign('base_url', $base_url);
210  }
211 
216  function _arrangeEntries($entries) {
217  $ret = array();
218 
219  $i = 0;
220 
221  foreach ($entries as $entry) {
222  $year = substr($entry['month'], 0, 4);
223  $month = substr($entry['month'], 4, 2);
224  $start = date("Y-m-d", mktime(0, 0, 0, $month, 1, $year));
225  $end = date("Y-m-t", mktime(0, 0, 0, $month, 1, $year));
226 
227  $rangeExists = false;
228  foreach ($ret as $key => $record) {
229  if ($record['start'] == $start && $record['end'] == $end) {
230  $rangeExists = true;
231  break;
232  }
233  }
234 
235  if (!$rangeExists) {
236  $workingKey = $i;
237  $i++;
238 
239  $ret[$workingKey]['start'] = $start;
240  $ret[$workingKey]['end'] = $end;
241  } else {
242  $workingKey = $key;
243  }
244 
245  if (array_key_exists('count_total', $ret[$workingKey])) {
246  $totalCount = $ret[$workingKey]['count_total'];
247  } else {
248  $totalCount = 0;
249  }
250  $ret[$workingKey]['count_total'] = $entry[STATISTICS_METRIC] + $totalCount;
251  if ($entry[STATISTICS_DIMENSION_FILE_TYPE] == STATISTICS_FILE_TYPE_HTML) {
252  $ret[$workingKey]['count_html'] = $entry[STATISTICS_METRIC];
253  } else if ($entry[STATISTICS_DIMENSION_FILE_TYPE] == STATISTICS_FILE_TYPE_PDF) {
254  $ret[$workingKey]['count_pdf'] = $entry[STATISTICS_METRIC];
255  }
256  }
257 
258  return $ret;
259  }
260 
267  function _getLimitDates($year) {
268  $begin = "$year-01-01";
269  $end = "$year-12-01";
270 
271  return array($begin, $end);
272  }
273 
279  function _getJournalIds($useLegacyStats = false) {
280  $metricsDao = DAORegistry::getDAO('MetricsDAO'); /* @var $metricsDao MetricsDAO */
281  if ($useLegacyStats) {
282  $results = $metricsDao->getMetrics(OJS_METRIC_TYPE_LEGACY_COUNTER, array(STATISTICS_DIMENSION_ASSOC_ID));
283  $fieldId = STATISTICS_DIMENSION_ASSOC_ID;
284  } else {
285  $filter = array(STATISTICS_DIMENSION_ASSOC_TYPE => ASSOC_TYPE_SUBMISSION_FILE);
286  $results = $metricsDao->getMetrics(METRIC_TYPE_COUNTER, array(STATISTICS_DIMENSION_CONTEXT_ID), $filter);
287  $fieldId = STATISTICS_DIMENSION_CONTEXT_ID;
288  }
289  $journalIds = array();
290  foreach($results as $record) {
291  $journalIds[] = $record[$fieldId];
292  }
293  return $journalIds;
294  }
295 
304  function _getMonthlyLogRange($journalId = null, $begin, $end, $useLegacyStats = false) {
305  $begin = date('Ym', strtotime($begin));
306  $end = date('Ym', strtotime($end));
307 
308  $metricsDao = DAORegistry::getDAO('MetricsDAO'); /* @var $metricsDao MetricsDAO */
309  $columns = array(STATISTICS_DIMENSION_MONTH, STATISTICS_DIMENSION_FILE_TYPE);
310  $filter = array(
311  STATISTICS_DIMENSION_MONTH => array('from' => $begin, 'to' => $end)
312  );
313 
314  if ($useLegacyStats) {
315  $dimension = STATISTICS_DIMENSION_ASSOC_ID;
316  $metricType = OJS_METRIC_TYPE_LEGACY_COUNTER;
317  } else {
318  $dimension = STATISTICS_DIMENSION_CONTEXT_ID;
319  $metricType = METRIC_TYPE_COUNTER;
320  $filter[STATISTICS_DIMENSION_ASSOC_TYPE] = ASSOC_TYPE_SUBMISSION_FILE;
321  }
322 
323  if ($journalId) {
324  $columns[] = $dimension;
325  $filter[$dimension] = $journalId;
326  }
327 
328  $results = $metricsDao->getMetrics($metricType, $columns, $filter);
329  return $results;
330  }
331 
339  function _getMonthlyTotalRange($begin, $end, $useLegacyStats = false) {
340  return $this->_getMonthlyLogRange(null, $begin, $end, $useLegacyStats);
341  }
342 
350  function _reportXML($request, $year, $useLegacyStats) {
351  $templateManager = TemplateManager::getManager();
352  list($begin, $end) = $this->_getLimitDates($year);
353 
354  $this->_assignTemplateCounterXML($request, $templateManager, $begin, $end, $useLegacyStats);
355  $reportContents = $templateManager->fetch($this->_plugin->getTemplateResource('reportxml.tpl'));
356  header('Content-type: text/xml');
357  echo $reportContents;
358  }
359 
360 }
361 
362 
LegacyJR1\$_plugin
$_plugin
Definition: LegacyJR1.inc.php:23
LegacyJR1\_report
_report($request, $year, $useLegacyStats)
Definition: LegacyJR1.inc.php:57
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
LegacyJR1\_getMonthlyTotalRange
_getMonthlyTotalRange($begin, $end, $useLegacyStats=false)
Definition: LegacyJR1.inc.php:342
LegacyJR1\_getJournalIds
_getJournalIds($useLegacyStats=false)
Definition: LegacyJR1.inc.php:282
LegacyJR1\LegacyJR1
LegacyJR1($plugin)
Definition: LegacyJR1.inc.php:29
LegacyJR1\_getLimitDates
_getLimitDates($year)
Definition: LegacyJR1.inc.php:270
LegacyJR1\display
display($request)
Definition: LegacyJR1.inc.php:37
Config\getVar
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
LegacyJR1\_formColumns
_formColumns(&$cols, $entries)
Definition: LegacyJR1.inc.php:129
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
LegacyJR1\_arrangeEntries
_arrangeEntries($entries)
Definition: LegacyJR1.inc.php:219
LegacyJR1
The Legacy COUNTER JR1 (r3) report.
Definition: LegacyJR1.inc.php:15
LegacyJR1\_reportXML
_reportXML($request, $year, $useLegacyStats)
Definition: LegacyJR1.inc.php:353
LegacyJR1\_getMonthlyLogRange
_getMonthlyLogRange($journalId=null, $begin, $end, $useLegacyStats=false)
Definition: LegacyJR1.inc.php:307
LegacyJR1\_assignTemplateCounterXML
_assignTemplateCounterXML($request, $templateManager, $begin, $end='', $useLegacyStats)
Definition: LegacyJR1.inc.php:164