-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathugent_bib.module
291 lines (259 loc) · 9.15 KB
/
ugent_bib.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* @file
* A module that connects to the UGent Bibliography API and
* can show all of a group's publications on a page, or publications
* of a single author in a block.
*/
define("UGENT_BIB_BASE_URL", "https://biblio.ugent.be");
define("UGENT_BIB_AUTHOR_QUERY", UGENT_BIB_BASE_URL."/person/%ugent_id%/publication/export?format=json");
define("UGENT_BIB_PROJECT_QUERY", UGENT_BIB_BASE_URL."/project/%ugent_id%/publication/export?format=json");
define("UGENT_BIB_PUBLICATION", UGENT_BIB_BASE_URL."/publication/");
define("UGENT_BIB_DOI", "http://dx.doi.org/");
/**
* Implements hook_help().
*
* Displays help and module information.
*
* @param path
* Which path of the site we're using to display help
* @param arg
* Array that holds the current path as returned from arg() function
*/
function ugent_bib_help($path, $arg){
switch ($path) {
case "admin/help#ugent_bib":
return t("This module uses the UGent Bibliography API to display papers by group or by author.");
break;
}
}
/**
* Implements hook_block_info().
*/
function ugent_bib_block_info() {
$blocks['ugent_bib'] = array(
'info' => t('UGent Bibliography'),
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
/**
* Implements hook_menu().
*/
function ugent_bib_menu() {
$items = array();
$items['admin/config/content/ugent_bib'] = array(
'title' => 'UGent Bibliography',
'description' => 'Configuration for UGent Bibliography module',
'page callback' => 'drupal_get_form',
'page arguments' => array('ugent_bib_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
$items['publications'] = array(
'title' => 'Publications',
'page callback' => '_ugent_bib_page',
'access arguments' => array('access ugent_bib content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Page callback: Current posts settings
*
* @see current_posts_menu()
*/
function ugent_bib_form($form, &$form_state) {
$citation_styles = array('vancouver' => 'vancouver', //produces two divs, one for numbering?
'apa' => 'apa', //default
'mla' => 'mla',
'chicago-author-date' => 'chicago-author-date',
'fwo' => 'fwo',
);
$form['ugent_bib_query_type'] = array(
'#type' => 'radios',
'#title' => t('Query type'),
'#default_value' => variable_get('ugent_bib_query_type', 'person'),
'#options' => array('person' => 'Person', 'project' => 'Project'),
'#description' => t('Is the default UGent ID referring to an author or to a project?'),
'#required' => TRUE,
);
$form['ugent_bib_default_ugent_id'] = array(
'#type' => 'textfield',
'#title' => t('Default UGent ID'),
'#default_value' => variable_get('ugent_bib_default_ugent_id', ''),
'#size' => 15,
'#maxlength' => 15,
'#description' => t('The UGent ID to be used on a full page publication display.'),
'#required' => TRUE,
);
$form['ugent_bib_citation_style'] = array(
'#type' => 'select',
'#title' => t('Citation style'),
'#default_value' => variable_get('ugent_bib_citation_style', 'apa'),
'#options' => $citation_styles,
'#description' => t('Pre-formatted citation style as stored in the UGent database.'),
'#required' => TRUE,
);
return system_settings_form($form);
}
/**
* @param $ugent_id Student/Personnel ID at UGent
*
* @return
* An array of JSON strings containing the publications of given author
*/
function ugent_bib_get_papers($ugent_id){
switch(variable_get('ugent_bib_query_type', 'person')){
default:
case "person":
$query_string = UGENT_BIB_AUTHOR_QUERY;
break;
case "project":
$query_string = UGENT_BIB_PROJECT_QUERY;
break;
}
$url = str_replace('%ugent_id%', $ugent_id, $query_string);
$handle = fopen($url, 'r');
$entries = array();
while (($line = fgets($handle)) !== false) {
$entries[] = $line;
}
fclose($handle);
return $entries;
}
/**
* Formats a JSON publication string as reference
*
* @param $json_pub a one line json entry representing a publication
*
* @return A reference in HTML format
*/
function ugent_bib_parse_reference($json_pub){
$json = json_decode($json_pub, TRUE);
$citation_style = variable_get('ugent_bib_citation_style', 'apa');
$ref = $json['cite'][$citation_style];
if (array_key_exists('doi', $json)){
$doi = $json['doi'][0];
}
$biblio = $json['_id'];
if (array_key_exists('abstract', $json)){
$abstract = $json['abstract'][0];
}
$html = '<div class="ugent-bib">';
$ugent_bib_ref_clickable = empty($abstract)? "":"ugent-bib-clickable";
$html.= "<div class=\"ugent-bib-ref $ugent_bib_ref_clickable\">";
$html.= $ref;
if (!empty($abstract)) {
$html.= "<div class=\"ugent-bib-abstract\">$abstract</div>";
}
$html.= '</div>';
$html.= '<div class="ugent-bib-links">';
if (!empty($abstract)){
$html.= l("<i>A</i>", '', array("attributes" => array("title" => "Show abstract", "class" => "ugent-bib-abstract-link"), "html" => TRUE));
}
$html.= l("<i class=\"fa fa-bank\"></i>", UGENT_BIB_PUBLICATION.$biblio, array("attributes" => array("title" => "UGent Biblio", "target" => '_blank'), "html" => TRUE));
if (!empty($doi)){
$html.= l("<i class=\"fa fa-external-link\"></i>", UGENT_BIB_DOI.$doi, array("attributes" => array("title" => "DOI", "target" => '_blank'), "html" => TRUE));
}
$html.= '</div>';
$html.= '</div>';
return $html;
}
/**
* Implements hook_block_view().
*
* Prepares the contents of the block.
*
*/
function ugent_bib_block_view($delta = '') {
drupal_add_js('http://use.fontawesome.com/f479fb1064.js', array('type' => 'external'));
switch ($delta) {
case 'ugent_bib':
$block['subject'] = t('Publications');
if (user_access('access ugent_bib content')) {
$result = array();
//check if we're on a user or node page
//and if the UGent ID is defined
$user = menu_get_object('user');
$node = menu_get_object();
if ($user != null && property_exists($user, "field_ugent_id")) {
if (array_key_exists('und', $user->field_ugent_id)){
$user_id = $user->field_ugent_id['und'][0]['value'];
if (!empty($user_id)){
$result = ugent_bib_get_papers($user_id);
}
}
} else if ($node != null && property_exists($node, "field_ugent_id")) {
if (array_key_exists('und', $node->field_ugent_id)){
$user_id = $node->field_ugent_id['und'][0]['value'];
if (!empty($user_id)){
$result = ugent_bib_get_papers($user_id);
}
}
} else {
$user_id = variable_get('ugent_bib_default_ugent_id', '');
$result = ugent_bib_get_papers($user_id);
}
$items = array();
foreach ($result as $pub) {
$items[] = array(
'data' => ugent_bib_parse_reference($pub),
);
}
// No papers found
if (empty($items)) {
$block['content'] = t('No publications to display.');
}
else {
// Pass data through theme function.
$block['content'] = theme('item_list', array(
'items' => $items,
'type' => 'ol',
'attributes' => array('reversed'=>'reversed'),
));
}
}
return $block;
}
}
/**
* Implements hook_permission().
*/
function ugent_bib_permission() {
return array(
'access ugent_bib content' => array(
'title' => t('Access content for the UGent Bibliography module'),
)
);
}
/**
* Custom page callback function, declared in ugent_bib_menu().
*/
function _ugent_bib_page() {
drupal_add_js('http://use.fontawesome.com/f479fb1064.js', array('type' => 'external'));
$user_id = variable_get('ugent_bib_default_ugent_id', '');
$result = ugent_bib_get_papers($user_id);
$items = array();
foreach ($result as $pub) {
$items[] = array(
'data' => ugent_bib_parse_reference($pub),
);
}
if (empty($items)) {
$page_array['ugent_bib_arguments'] = array(
'#title' => t('Publications'),
'#markup' => t('No publications to display.'),
);
return $page_array;
} else {
$page_array['ugent_bib_arguments'] = array(
'#title' => t('Publications'),
'#items' => $items,
'#theme' => 'item_list__ugent_bib',
'#type' => 'ol',
'#attributes' => array('reversed' => 'reversed'),
);
return $page_array;
}
}