Skip to content

Commit

Permalink
Add Crossref citations badge (#3)
Browse files Browse the repository at this point in the history
* Add Crossref citations badge

* Fix whitespace in admin form

* Fixing Diego's suggestions

* Add error handling to drupal_http_request

* Force plaintext in block_text variable

* Add translatable alt text to badge image
  • Loading branch information
bondjimbond authored and DiegoPino committed Aug 10, 2017
1 parent 32073f0 commit a271f86
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 0 deletions.
48 changes: 48 additions & 0 deletions modules/islandora_crossref_citations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Islandora Crossref Citations

## Introduction

Islandora Crossref Citations generates a block which displays citation counts for any object with a DOI, via the Crossref Cited-by service, for use with Islandora Badges.

The badge will only display on objects with a MODS datastream and a DOI (Digital Object Identifier).

## Requirements

This module requires the following modules/libraries:

* [Islandora](https://github.com/islandora/islandora)
* [Islandora Badges](https://github.com/Islandora/islandora_badges)

## Installation

Install as usual, see [this](https://drupal.org/documentation/install/modules-themes/modules-7) for further information.

## Configuration

Configuration path is admin/islandora/tools/badges/crossref (Administration > Islandora > Islandora Utility Modules > Islandora Badges Configuration > Crossref citations).

Block text: Defines the text used in the citation count block. Defaults to "Citations via Crossref".

Badge type: You can choose between plain text or an automatically-generated image from shields.io. Plain text is offered for custom styling.


## Troubleshooting/Issues

Having problems or solved a problem? Check out the Islandora google groups for a solution.

* [Islandora Group](https://groups.google.com/forum/?hl=en&fromgroups#!forum/islandora)
* [Islandora Dev Group](https://groups.google.com/forum/?hl=en&fromgroups#!forum/islandora-dev)

## Maintainers/Sponsors

Current maintainers:

* [Brandon Weigel](https://github.com/bondjimbond)

## Development

If you would like to contribute to this module, please check out [CONTRIBUTING.md](CONTRIBUTING.md). In addition, we have helpful [Documentation for Developers](https://github.com/Islandora/islandora/wiki#wiki-documentation-for-developers) info, as well as our [Developers](http://islandora.ca/developers) section on the [Islandora.ca](http://islandora.ca) site.

## License

[GPLv3](http://www.gnu.org/licenses/gpl-3.0.txt)
27 changes: 27 additions & 0 deletions modules/islandora_crossref_citations/includes/admin.form.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* @file
* Crossref Citations badge admin options
*/

/**
* Admin form: Nothing here yet.
*/
function islandora_crossref_citations_admin_form($form, $form_settings) {
$form['islandora_crossref_citations_text'] = array(
'#type' => 'textfield',
'#title' => t('Link text'),
'#description' => t('Text to use when generating external download link.'),
'#default_value' => variable_get('islandora_crossref_citations_text', 'Citations via Crossref'),
);

$form['islandora_crossref_citations_badgetype'] = array(
'#type' => 'radios',
'#title' => t('Type of badge'),
'#description' => t('You may choose an image generated by shields.io, or a text block you can style yourself with CSS.'),
'#options' => array('image' => t('image'), 'text' => t('text')),
'#default_value' => variable_get('islandora_crossref_citations_badgetype', 'image'),
);

return system_settings_form($form);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = Islandora Crossref Citations
core = 7.x
description = Islandora Crossref citation counts.
dependencies[] = islandora_badges
package = Islandora Tools
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* @file
* Installation hooks for Crossref Citations badges.
*/

/**
* Implements hook_uninstall().
*/
function islandora_crossref_citations_uninstall() {
$vars = array(
islandora_crossref_citations_text,
islandora_crossref_citations_badgetype,
);
array_walk($vars, 'variable_del');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* @file
* Crossref Citation Badges
*/

/**
* Implements hook_menu().
*/
function islandora_crossref_citations_menu() {
return array(
'admin/islandora/tools/badges/crossref' => array(
'title' => 'Crossref citations',
'description' => 'Configure Crossref Citations settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_crossref_citations_admin_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_TASK,
'file' => 'includes/admin.form.inc',
),
);
}

/**
* Implements hook_block_info().
*/
function islandora_crossref_citations_block_info() {
return array(
'islandora_crossref_badge' => array(
'visibility' => BLOCK_VISIBILITY_LISTED,
'pages' => 'islandora/object/*',
'cache' => DRUPAL_CACHE_PER_PAGE,
'info' => t('Crossref citations badge'),
),
);
}

/**
* Implements hook_block_view().
*/
function islandora_crossref_citations_block_view($delta = '') {
module_load_include('inc', 'islandora_badges', 'includes/utilities');
$to_render = array();
if ($delta == 'islandora_crossref_badge') {
$object = menu_get_object('islandora_object', 2);
if ($object) {
// Check CModel against Badges configuration.
if (islandora_badges_show_for_cmodel($object)) {
$doi = islandora_badges_get_doi($object);
if ($doi) {
// Set API endpoint URL
// @TODO: Add to admin form.
$url = "https://api.crossref.org/works/";
$request_url = $url . $doi;
// Make the request and get results!
$result_json = drupal_http_request($request_url);
if (!isset($result_json->error)) {
$result_array = json_decode($result_json->data, TRUE);
$result_citation_count = $result_array['message']['is-referenced-by-count'];
}
}
}
}
}

if (isset($result_citation_count) && ($result_citation_count > 0)) {
// Sanitize block text input.
$block_text = check_plain(variable_get('islandora_crossref_citations_text', 'Citations via Crossref'));
$badge_type = variable_get('islandora_crossref_citations_badgetype', 'image');
if ($badge_type == 'text') {
$badge = $block_text . ': ' . $result_citation_count;
}
else {
$badge = '<img src="https://img.shields.io/badge/' . $block_text . '-' . $result_citation_count . '-blue.svg?style=flat" alt="' . t('Number of citations via Crossref: ' . $result_citation_count) . '">';
}
$to_render['content'] = $badge;

}

return $to_render;

}

0 comments on commit a271f86

Please sign in to comment.