-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathting_recommendation_panes.module
161 lines (142 loc) · 4.63 KB
/
ting_recommendation_panes.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
<?php
/**
* Implements hook_menu().
*/
function ting_recommendation_panes_menu() {
$path = drupal_get_path('module', 'ting_recommendation_panes');
$items = array();
$items['admin/config/ting_recommendation_panes'] = array(
'title' => 'Ting recommendation panes',
'page callback' => 'drupal_get_form',
'page arguments' => array('ting_recommendation_panes_settings_form'),
'access arguments' => array('administer recommendation panes'),
'file' => 'ting_recommendation_panes.admin.inc',
'file path' => $path,
);
return $items;
}
/**
* Implements hook_permission().
*/
function ting_recommendation_panes_permission() {
return array(
'administer recommendation panes' => array(
'title' => t('administer recommendation panes'),
'description' => t('TODO Add a description for \'administer recommendation panes\''),
),
);
}
/**
* Implements hook_theme().
*
* This lets us tell Drupal about our theme functions and their arguments.
*/
function ting_recommendation_panes_theme() {
return array(
'ting_recommendation_panes_recommendation_list' => array(
'variables' => array('objects' => NULL),
'template' => 'ting_recommendation_panes_recommendation_list',
),
'ting_recommendation_panes_recommendation_list_entry' => array(
'variables' => array('object' => NULL),
'template' => 'ting_recommendation_panes_recommendation_list_entry',
),
);
}
/**
* Implements hook_flush_caches().
*/
function ting_recommendation_panes_flush_caches() {
return array('cache_ting_recommendation_panes');
}
/**
* Implements hook_ctools_plugin_directory().
*
* Tells CTools (and thus Panels) where to look for plugin code.
*/
function ting_recommendation_panes_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' || $module == 'panels') {
return 'plugins/' . $plugin;
}
}
/**
* Load TingClientObjects for each recommendation.
*/
function ting_recommendation_panes_get_recommended_objects($objects) {
$identifiers = ting_recommendation_panes_extract_identifers($objects);
$cacheId = md5(implode('|', $identifiers));
$cache = cache_get($cacheId, 'cache_ting_recommendation_panes');
if ($cache) {
$recommended_objects = $cache->data;
}
else {
$numResults = 24;
$localIds = array();
foreach ($identifiers as $id) {
// Don't get more than 24 local ids..
if (count($localIds) >= 24) {
break;
}
try {
$recommendations = ting_get_object_recommendations($id, $numResults);
foreach ($recommendations as $recommendation) {
$localIds[$recommendation->localId] = $recommendation;
}
}
catch (TingClientException $e) {
watchdog('ting client', 'Error retrieving recommendations: ' . $e->getMessage(), array($id, $e), WATCHDOG_WARNING, $_SERVER["REQUEST_URI"]);
}
}
arsort($localIds);
$recommended_objects = array();
$agency = variable_get('ting_agency', -1);
foreach (array_keys($localIds) as $localId) {
$ting_object = ding_entity_load("$agency:$localId");
// Only show objects belonging to our own library.
if ($ting_object && $ting_object->ownerId == $agency) {
$recommended_objects[] = $ting_object;
// Don't get more than 10 valid objects.
if (count($recommended_objects) >= 10) {
break;
}
}
}
cache_set($cacheId, $recommended_objects, 'cache_ting_recommendation_panes', REQUEST_TIME + variable_get('ting_recommendation_panes_cache_duration', 604800));
}
return $recommended_objects;
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function ting_recommendation_panes_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, ting_recommendation_panes_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 ting_recommendation_panes_extract_identifers($objects->objects);
break;
default:
//assume a string or integer has been passed and return it
return array($objects);
break;
}
}
}