forked from odensecentralbibliotek/ting_recommendation_panes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathding_adhl_frontend.module
More file actions
215 lines (193 loc) · 6.75 KB
/
ding_adhl_frontend.module
File metadata and controls
215 lines (193 loc) · 6.75 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* @REVIEW_COMMENT: Dette modul ville gavne af at blive omskrevet til adhl 3.0.
* Til denne version af servicen skal man sende en pid og man får en række pid'er retur
* som kan hentes med object get.
* Det vil give en simplere kode, og et stort performance boost
*
* Der er et par "@todo" kommentarer i koden, bør de ikke fixes/fjernes inden det kommer i
* core
*/
/**
* @file
* Enables the ADHL services to ding2 sites.
*/
/**
* Implements hook_menu().
*/
function ding_adhl_frontend_menu() {
$items = array();
$items['admin/config/ding/adhl'] = array(
'title' => 'Ding ADHL settings',
'description' => 'Configure ADHL caching duration and number of elements to fetch.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ding_adhl_frontend_settings_form'),
'access arguments' => array('administer adhl recommendations'),
'file' => 'ding_adhl_frontend.admin.inc',
);
return $items;
}
/**
* Implements hook_permission().
*/
function ding_adhl_frontend_permission() {
return array(
'administer adhl recommendations' => array(
'title' => t('Administer ADHL'),
'description' => t('Administer ADHL recommedation settings'),
),
);
}
/**
* Implements hook_theme().
*/
function ding_adhl_frontend_theme() {
return array(
'ding_adhl_frontend_recommendation_list' => array(
'variables' => array('type' => 'ul', 'items' => array()),
'template' => 'templates/ding_adhl_frontend_recommendation_list',
),
'ding_adhl_frontend_recommendation_list_entry' => array(
'variables' => array('item' => NULL),
'template' => 'templates/ding_adhl_frontend_recommendation_list_entry',
),
);
}
/**
* Implements hook_preprocess_HOOK().
*
* Default implementation of preprocess function for ding_adhl_frontend_recommendation_list_entry
* theme function.
*/
function template_preprocess_ding_adhl_frontend_recommendation_list_entry(&$vars) {
$item = $vars['item'];
// Build string with the creators.
$creators = FALSE;
if (!empty($item->creators)) {
$creators = implode(", ", $item->creators);
}
$vars['creators'] = $creators;
// Create link and title back to the ting object.
$uri = entity_uri("ting_object", $item);
$vars['link'] = array(
'uri' => url($uri['path']),
'title' => $item->title . ($creators ? ': ' . $creators : ''),
);
}
/**
* Implements hook_flush_caches().
*/
function ding_adhl_frontend_flush_caches() {
return array('cache_ding_adhl_frontend');
}
/**
* Implements hook_ctools_plugin_directory().
*
* Tells CTools (and thus Panels) where to look for plugin code.
*/
function ding_adhl_frontend_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' || $module == 'panels') {
return 'plugins/' . $plugin;
}
}
/**
* Load TingClientObjects for each recommendation.
*
* @param ting $objects
* Ting objects (ting entities) that repesent a datawell object. (This may be different stuff array, object, collection ?)
* @param bool $reset
* Reset the cache. Defaults to FALSE.
* @return ting_entities
* An array of recommendations as ting entities.
*/
function ding_adhl_frontend_get_recommended_objects($objects, $reset = FALSE) {
// Check static cache for recommendations.
$recommended_objects = &drupal_static(__FUNCTION__);
if (!isset($recommended_objects) || $reset) {
// Build cache id to try persistent cache.
$identifiers = ding_adhl_frontend_extract_identifers($objects);
$cid = md5(implode('', $identifiers));
$cache = cache_get($cid, 'cache_ding_adhl_frontend');
if (!$reset && $cache && $cache->expire > REQUEST_TIME) {
$recommended_objects = $cache->data;
}
else {
// No cached information was found, so try getting recommendation form the
// service.
$number_of_recommendations = variable_get('ding_adhl_frontend_number_to_fetch', 24);
$local_ids = array();
foreach ($identifiers as $identifier) {
// Don't fetch more recommendations that the configured number.
if (count($local_ids) >= $number_of_recommendations) {
break;
}
// Get the recommendations from the ting client (ADHL service).
$recommendations = ting_get_object_recommendations($identifier, $number_of_recommendations);
if (isset($recommendations) && is_array($recommendations)) {
foreach ($recommendations as $recommendation) {
$local_ids[$recommendation->localId] = $recommendation;
}
}
}
// Fetch objects/entities representing the recommendations.
$recommended_objects = array();
// Hvorfor hente et andet antal objecter end man skal bruge $number_of_recommendations vs. $number_of_objects
$number_of_objects = variable_get('ding_adhl_frontend_number_to_display', 10);
$agency = variable_get('ting_agency', -1);
foreach (array_keys($local_ids) as $local_id) {
$ting_object = ding_entity_load(ding_provider_build_entity_id($local_id));
// Only show objects belonging to our own library.
if ($ting_object && $ting_object->ownerId == $agency) {
$recommended_objects[] = $ting_object;
// Don't get more than we want to display.
if (count($recommended_objects) >= $number_of_objects) {
break;
}
}
}
// Cache the result.
cache_set($cid, $recommended_objects, 'cache_ding_adhl_frontend', REQUEST_TIME + variable_get('ding_adhl_frontend_cache_duration', 604800));
}
}
return $recommended_objects;
}
/**
* Helper function that finds ting objects/entites id's based on the type of
* input given.
*
* @todo: There most be a more clean way to get these ids -- cableman.
*
* @param mixed $objects
* The parameter can be anything from an array to ting object collections.
*/
function ding_adhl_frontend_extract_identifers($objects) {
if (is_array($objects)) {
// If passing an array then extract ids from each of the entries.
$ids = array();
foreach ($objects as $o) {
$ids = array_merge($ids, ding_adhl_frontend_extract_identifers($o));
}
return array_unique($ids);
}
else {
// If passing a single value then extract ids depending on type.
switch (get_class($objects)) {
case 'TingClientObject':
if (!empty($objects->record['dc:identifier']['dkdcplus:ISBN'])) {
return $objects->record['dc:identifier']['dkdcplus:ISBN'];
}
else {
return array();
}
break;
case 'TingClientObjectCollection':
// Get ids from each of the objects in the collection.
return ding_adhl_frontend_extract_identifers($objects->objects);
break;
default:
// Assume a string or integer has been passed and return it.
return array($objects);
break;
}
}
}