-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathislandora_usage_stats_callbacks.module
187 lines (169 loc) · 5.85 KB
/
islandora_usage_stats_callbacks.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* @file
* Primary hook implementations.
*/
/**
* Implements hook_menu().
*/
function islandora_usage_stats_callbacks_menu() {
$items = array();
$items['islandora_usage_stats_callbacks/object_stats/%'] = array(
'page callback' => 'islandora_usage_stats_callbacks_get_object_stats',
'page arguments' => array(2),
'access arguments' => array('access usage stats callbacks api'),
);
$items['admin/islandora/tools/islandora_usage_stats_callbacks'] = array(
'title' => 'Islandora Usage Stats Callbacks Settings',
'description' => 'Change behavior of Islandora Usage Stats Callbacks.',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_usage_stats_callbacks_admin_form'),
'access arguments' => array('manage usage stats callbacks'),
'file' => 'includes/admin.form.inc',
);
return $items;
}
/**
* Implements hook_permission().
*/
function islandora_usage_stats_callbacks_permission() {
return array(
'manage usage stats callbacks' => array(
'title' => 'Manage usage stats callbacks configuration',
'description' => 'Manage the configuration of the Usage Stats Callbacks module',
),
'access usage stats callbacks api' => array(
'title' => t('Access callbacks API'),
'description' => t('Access Islandora Usage Stats callbacks API'),
),
);
}
/**
* Callback functions.
*/
function islandora_usage_stats_callbacks_get_object_stats($pid) {
$report = array();
$ids = islandora_usage_stats_callbacks_get_object_ids($pid);
$legacy_stats = islandora_usage_stats_callbacks_get_legacy_stats($pid);
$report['pid'] = $ids['pid'];
$report['cmodel'] = $ids['cmodel'];
$report['legacy-views'] = $legacy_stats['views'];
$report['legacy-downloads'] = $legacy_stats['downloads'];
$report['views'] = islandora_usage_stats_callbacks_get_object_views($ids);
$report['downloads'] = isset($ids['dsid']) ? islandora_usage_stats_callbacks_get_object_downloads($ids) : FALSE;
echo json_encode($report);
drupal_exit();
}
/**
* Legacy stats reader.
*/
function islandora_usage_stats_callbacks_get_legacy_stats($pid) {
$legacy_stats = array();
$legacy_stats_file_loc = variable_get('islandora_usage_stats_callbacks_legacy_stats_file_location', '');
if (($legacy_stats_file_loc != '') && file_exists($legacy_stats_file_loc)) {
$legacy_data = array();
$legacy_data_csv = array_map('str_getcsv', file($legacy_stats_file_loc));
foreach ($legacy_data_csv as $row) {
$legacy_pid = $row[0];
$legacy_views = $row[1];
$legacy_downloads = $row[2];
$legacy_data[$legacy_pid] = array("legacy-views" => $legacy_views, "legacy-downloads" => $legacy_downloads);
}
if (array_key_exists($pid, $legacy_data)) {
$legacy_stats['views'] = intval($legacy_data[$pid]['legacy-views']);
$legacy_stats['downloads'] = intval($legacy_data[$pid]['legacy-downloads']);
}
else {
$legacy_stats['views'] = 0;
$legacy_stats['downloads'] = 0;
}
}
else {
$legacy_stats['views'] = 0;
$legacy_stats['downloads'] = 0;
}
return $legacy_stats;
}
/**
* Gets Usage Stats object ID.
*/
function islandora_usage_stats_callbacks_get_object_ids($pid) {
// Check PID string format before proceeding.
module_load_include('inc', 'islandora', 'includes/utilities');
if (!islandora_is_valid_pid($pid)) {
echo json_encode(array('error' => "PID '${pid}' is not a valid PID string"));
drupal_exit();
}
$ids = array();
$ids['pid'] = $pid;
$ids['cmodel'] = islandora_object_load($pid)->models[0];
module_load_include('inc', 'islandora_usage_stats_callbacks', 'includes/utilities');
$default_dsids = islandora_usage_stats_callbacks_set_default_dsids();
$dsids = variable_get('islandora_usage_stats_callbacks_cmodel_dsids', $default_dsids);
$cmodel_dsids = array();
$rows = array();
// Convert configured DSIDs into an array.
$rows = preg_split("/\\r\\n|\\r|\\n/", $dsids);
foreach ($rows as $pair) {
$rows = explode('|', $pair);
$cmodel_dsids[$rows[0]] = $rows[1];
}
if ($ids['cmodel'] == '') {
echo json_encode(array('error' => "PID '${ids['pid']}' does not exist"));
drupal_exit();
}
elseif (!in_array($ids['cmodel'], array_keys($cmodel_dsids))) {
echo json_encode(array('error' => "unsupported content model '${ids['cmodel']}'"));
drupal_exit();
}
$ids['dsid'] = $cmodel_dsids[$ids['cmodel']];
$result = db_select('islandora_usage_stats_objects')
->fields('islandora_usage_stats_objects', array('id'))
->condition('pid', $pid)
->execute();
$record = $result->fetchAssoc();
$ids['usid'] = $record['id'];
if ($ids['usid'] == '') {
echo json_encode(array(
'pid' => $ids['pid'],
'cmodel' => $ids['cmodel'],
'views' => array(),
'downloads' => array(),
));
drupal_exit();
}
return $ids;
}
/**
* Gets Usage Stats object views.
*/
function islandora_usage_stats_callbacks_get_object_views($ids) {
$result = db_select('islandora_usage_stats_object_access_log')
->fields('islandora_usage_stats_object_access_log', array('time'))
->condition('pid_id', $ids['usid'])
->execute();
$record = $result->fetchAll();
return $record;
}
/**
* Gets Usage Stats object downloads.
*/
function islandora_usage_stats_callbacks_get_object_downloads($ids) {
$ds_check_query = db_select('islandora_usage_stats_datastreams')
->fields('islandora_usage_stats_datastreams', array('id'))
->condition('pid_id', $ids['usid'])
->condition('dsid', $ids['dsid'])
->execute();
$ds_check_result = $ds_check_query->fetchAssoc();
if ($ds_check_result['id']) {
$result = db_select('islandora_usage_stats_object_ds_access_log')
->fields('islandora_usage_stats_object_ds_access_log', array('time'))
->condition('ds_id', $ds_check_result['id'])
->execute();
$record = $result->fetchAll();
return $record;
}
else {
return array();
}
}