-
Notifications
You must be signed in to change notification settings - Fork 0
/
gbif_publishers.module
352 lines (302 loc) · 10.8 KB
/
gbif_publishers.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
define('JSON_DIRECTORY', drupal_get_path('module', 'gbif_publishers') . '/json/');
/**
* 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 gbif_publishers_help($path,$arg){
$msg = "Creates a page with the list of datapublishers endorsed by your node and";
$msg .= " creates a page for each datapublishers embedding charts and metrics relative to their contribution to GBIF.";
switch ($path) {
case "admin/help#gbif_publishers":
return '<p>' . t($msg) . '</p>';
break;
}
}
function gbif_publishers_install (){
$menu = array(
'menu_name' => 'gbif-publishers-menu',
'title' => 'GBIF publishers',
'description' => 'Provides the list of gbif publishers'
);
menu_save($menu);
cache_clear_all('*', 'cache_menu', TRUE);
drupal_set_message($message = t('The module was successfully installed. '), $type = 'status');
}
// Comments are for dev. version
function gbif_publishers_uninstall()
{
$menu = array(
'menu_name' => 'gbif-publishers-menu',
'title' => 'GBIF publishers',
'description' => 'Provides the list of gbif publishers'
);
menu_delete($menu);
cache_clear_all('*', 'cache_menu', TRUE);
drupal_set_message($message = t('The module was successfully disabled '), $type = 'status');
}
function gbif_publishers_disable()
{
cache_clear_all('*', 'cache_menu', TRUE);
}
/**
* Implements hook_menu().
*/
function gbif_publishers_menu() {
//TODO: Add test to check if file exists
$json = file_get_contents( JSON_DIRECTORY . 'org.json' );
$org = json_decode($json);
/* $items['gbif_publishers'] = array(
'title' => 'Data publishers list',
'description' => '',
'page callback' => '_gbif_publishers_page',
'access callback' => TRUE,
'expanded' => TRUE,
'menu_name'=> 'gbif-publishers-menu',
);
*/
// Enables module configuration over Drupal admin interface
$items['admin/config/content/gbif_publishers'] = array(
'title' => 'GBIF Publishers',
'description' => 'Configuration for GBIF publishers module',
'page callback' => 'drupal_get_form',
'page arguments' => array('gbif_publisher_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
foreach($org->results as $k=>$v)
{
$routerUrl = 'gbif_publishers/datapublisher/' . $v->key;
$items[$routerUrl] = array(
'title' => $v->title,
'page callback' => '_gbif_publishers_display',
'page arguments' => array(2),
'access callback' => TRUE,
'menu_name'=> 'gbif-publishers-menu',
);
}
return $items;
}
/**
* Page callback: Module settings
*
* @see gbif_publishers_menu()
*/
function gbif_publisher_form($form, &$form_state) {
$uuid = "da44cd31-5901-4687-a106-6d1c7734ee3a"; // default is french GBIF node
$form['gbif_publisher_node'] = array(
'#type' => 'textfield',
'#title' => t('Enter your node UUID'),
'#default_value' => variable_get('gbif_publisher_node', $uuid),
'#description' => t('The GBIF node uuid. (default uuid value is french GBIF node)'),
'#required' => TRUE,
);
return system_settings_form($form);
}
/*
* Implementation of hook_library()
*/
function gbif_publishers_library()
{
$libraries['jquery'] = array(
'title' => 'jquery',
'website' => 'http://www.jquery.com',
'version' => '1.11.0',
'js' => array( drupal_get_path('module', 'gbif_publishers') . '/js/jquery-1.11.0.min.js' => array(
'type' => 'file',
'scope' => 'footer',
),
),
);
$libraries['highcharts'] = array(
'title' => 'highcharts',
'website' => 'http://www.highcharts.com',
'version' => '4.0.4',
'js' => array( drupal_get_path('module', 'gbif_publishers') . '/js/highcharts.js' => array(
'type' => 'file',
'scope' => 'footer',
),
),
);
$libraries['kingdom_charts'] = array(
'title' => 'kingdom_charts',
'website' => 'http://www.gbif.fr',
'version' => '0.1',
'js' => array( drupal_get_path('module', 'gbif_publishers') . '/js/chart.js' => array(
'type' => 'file',
'scope'=> 'footer',
),
),
'dependencies' => array(
array('gbif_publishers', 'highcharts'),
array('gbif_publishers', 'jquery'),
),
);
$libraries['custom_css'] = array(
'css' => array(
drupal_get_path('module', 'gbif_publishers') . '/css/custom.css' => array(
'type' => 'file',
'media' => 'all',
),
));
$libraries['leaflet'] = array(
'title' => 'leaflet',
'website' => 'http://leafletjs.com',
'version' => '0.7',
'js' => array(
drupal_get_path('module', 'gbif_publishers') . '/js/leaflet.js' => array(
'type' => 'file',
'scope' => 'footer',
),
drupal_get_path('module', 'gbif_publishers') . '/js/Leaflet.fullscreen.min.js' => array(
'type' => 'file',
'scope' => 'footer',
),
),
'css' => array(
drupal_get_path('module', 'gbif_publishers') . '/css/leaflet.css' => array(
'type' => 'file',
'media' => 'all',
),
drupal_get_path('module', 'gbif_publishers') . '/css/leaflet.fullscreen.css' => array(
'type' => 'file',
'media' => 'all',
),
),
);
$libraries['gbif_map'] = array(
'title' => 'gbif_map',
'website' => 'http://www.gbif.fr',
'version' => '0.1',
'js' => array(
drupal_get_path('module', 'gbif_publishers') . '/js/map_publisher.js' => array(
'type' => 'file',
'scope' => 'footer',
),
),
'dependencies' => array(
array('gbif_publishers', 'leaflet'),
),
);
return $libraries;
}
/**
* Page callback for gbif_publishers menu entry.
*
*/
function _gbif_publishers_page() {
$base_content = t('GBIF Publishers module provides a list of your node datapublishers and a page for each datapublisher contribution');
return '<div>' . $base_content . '</div>';
}
function _gbif_publishers_display($uuid)
{
$stat = _gbif_publishers_get_publisher_stat($uuid);
$table = _gbif_publishers_html_table($stat["institution"],$stat["script_exec_date"],$uuid);
return _gbif_publishers_provider_info($table);
}
function _gbif_publishers_get_publisher_stat($uuid)
{
$json = file_get_contents(JSON_DIRECTORY . 'stats.json' );
$stats = json_decode($json);
$exec_date = $stats->script_exec_date;
foreach($stats->datasets_list as $val){
if ($val->institution_uuid==$uuid)
{
return array("institution"=>$val,"script_exec_date"=>$exec_date);
}
}
}
function _gbif_publishers_html_table($stat,$exec_date,$uuid)
{
drupal_add_library('gbif_publishers', 'jquery', FALSE);
drupal_add_library('gbif_publishers', 'highcharts', FALSE);
drupal_add_library('gbif_publishers', 'kingdom_charts', FALSE);
drupal_add_library('gbif_publishers', 'custom_css', FALSE);
drupal_add_library('gbif_publishers', 'leaflet', FALSE);
drupal_add_library('gbif_publishers', 'gbif_map', FALSE);
$cpt_Geo = 0;
$cpt_Occ = 0;
$institution_name = $stat->institution_name;
$html = '<span class="hidden provider_uuid">'. $uuid .'</span>';
$html .= "<span class='small'>(Last update : ".$exec_date.") </span><br /> ";
$html .= '<table class="table stat-provider table-striped table-bordered">
<thead>
<tr>
<th>Dataset</th>
<th class="data-number">Metrics</th>
<th>Downloads</th>
</tr>
</thead>
<tbody>';
$stat_provider = array("kingdom"=>array(),"basisOfRecord"=>array());
foreach($stat->datasets_list as $ds)
{
$ds_url = "http://www.gbif.org/dataset/".$ds->key;
$ds_search_url ="http://www.gbif.org/occurrence/search?datasetKey=".$ds->key;
$html .= "<tr>";
$html .= "<td><b><a target='_blank' class='ds-title' href='".$ds_url."'>".$ds->title."</a></b><br />";
$html .= "<span>". ucfirst(strtolower($ds->type)) ."</span><br /><br />";
if ( (isset($ds->dwc_url) && !(empty($ds->dwc_url))) ) $html .= "<a class='label' href='". $ds->dwc_url ."'>Darwin Core Archive <i class='icon-download-alt'></i></a></td>";
$html .= (isset($ds->total) && !(empty($ds->total))) ? "<td><b>". $ds->total ."</b>": "<td>";
$html .= (isset($ds->percent_geo) && !(empty($ds->percent_geo))) ? "<br /><span>Georeferenced ".$ds->percent_geo." % </span>": "";
$html .= "<br /><span><a target='_blank' href=".$ds_search_url.">View <i class='icon-th-list'></i></a></span>";
// Computes stats (Kingdom / BasisOfRecord) for Publisher
if ($ds->type == "OCCURRENCE")
{
foreach($ds->basisOfRecord as $val)
{
foreach($val as $k=>$v)
{
$stat_provider["basisOfRecord"][$k] = (array_key_exists($k,$stat_provider["basisOfRecord"])) ? $stat_provider["basisOfRecord"][$k] + $v : $v;
}
}
if (isset($ds->kingdom) && count($ds->kingdom)!=0)
{
foreach($ds->kingdom as $val)
{
foreach($val as $k=>$v)
{
$stat_provider["kingdom"][$k] = (array_key_exists($k,$stat_provider["kingdom"])) ? $stat_provider["kingdom"][$k] + $v : $v;
}
}
}
}
$cpt_Geo = (isset($ds->georef) && !(empty($ds->georef))) ? $cpt_Geo + $ds->georef : $cpt_Geo;
$cpt_Occ = (isset($ds->total) && !(empty($ds->total))) ? $cpt_Occ + $ds->total : $cpt_Occ;
$html .= (isset($ds->download) && !(empty($ds->download))) ? '<td><b>'.$ds->download.'</b><br><a target="_blank" href="'.$ds_url.'/'.'activity"><span class="small">(See download details)</span></a></td>' : '<td>-</td>' ;
$html .= "</tr>";
}
$html .= '</tbody></table><br />';
$html .= '<span class="stat_provider hidden">'.json_encode($stat_provider).'</span>';
return array("stats_html"=>$html,"total_geo"=>$cpt_Geo,"total_occ"=>$cpt_Occ,"institution_name"=>$institution_name);
}
function _gbif_publishers_provider_info($table)
{
$cpt_geo = $table["total_geo"] ;
$cpt_occ = $table["total_occ"];
$percent_geo = 0;
$html = '';
if ($cpt_occ != 0)
{
$percent_geo = round((($cpt_geo / $cpt_occ) * 100),2) ;
$html .= $table["stats_html"];
$html .= '<div id="kingdom-chart" style="min-width: 500px; height: 400px; max-width: 300px; margin: 0 auto"></div>';
$html .= '<div id="bor-chart"><b>Basis of records : </b></div></div></div> ';
if ($percent_geo != 0)
{
$html .= ' <p>This map shows georeferenced data provided by '.$table["institution_name"].'.</p><b>'.$percent_geo.' %</b> of these data are georeferenced. <br /><br />';
$html .= '<div id="map" style="width: 748px; height: 400px"></div>';
}
}
else
{
$html .= 'This provider has no dataset indexed on GBIF portal.';
}
return $html;
}