From a72955caa31829d0ecf3094a426277ae298e08dc Mon Sep 17 00:00:00 2001 From: Paul Pound Date: Fri, 31 Mar 2017 16:14:56 -0300 Subject: [PATCH] first commit --- README.md | 13 ++ includes/admin.form.inc | 56 ++++++ includes/doi.inc | 117 +++++++++++ includes/form.inc | 421 ++++++++++++++++++++++++++++++++++++++++ includes/relais.inc | 80 ++++++++ includes/utilities.inc | 76 ++++++++ upei_roblib_ill.info | 3 + upei_roblib_ill.install | 19 ++ upei_roblib_ill.module | 38 ++++ 9 files changed, 823 insertions(+) create mode 100644 README.md create mode 100644 includes/admin.form.inc create mode 100644 includes/doi.inc create mode 100644 includes/form.inc create mode 100644 includes/relais.inc create mode 100644 includes/utilities.inc create mode 100644 upei_roblib_ill.info create mode 100644 upei_roblib_ill.install create mode 100644 upei_roblib_ill.module diff --git a/README.md b/README.md new file mode 100644 index 0000000..b0575ed --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# Robertson Library ILL Forms module + +## Introduction + +Standalone module to submit ILL requests to Relais. Can read openurl parameters or lookup data based on a DOI + +## Requirements + +* Drupal 7 + +## Maintainers/Sponsors + +* UPEI Robertson Library diff --git a/includes/admin.form.inc b/includes/admin.form.inc new file mode 100644 index 0000000..e6da6f8 --- /dev/null +++ b/includes/admin.form.inc @@ -0,0 +1,56 @@ + TRUE, + '#type' => 'textfield', + '#size' => 200, + '#title' => t('The Relais AddRequest URL'), + '#description' => t('The URL for the Relais ILL server AddRequest service, the default is + https://caul-cbua.relais-host.com/portal-service/request/add'), + '#default_value' => variable_get('upei_roblib_ill_add_url', 'https://caul-cbua.relais-host.com/portal-service/request/add'), + ); + $form['upei_roblib_ill_auth_url'] = array( + '#required' => TRUE, + '#type' => 'textfield', + '#size' => 200, + '#title' => t('The Relais Auth URL'), + '#description' => t('The URL for the Relais ILL server Auth service (retrieve the aid), the default is + https://caul-cbua.relais-host.com/portal-service/user/authentication'), + '#default_value' => variable_get('upei_roblib_ill_auth_url', 'https://caul-cbua.relais-host.com/portal-service/user/authentication'), + ); + $form['upei_roblib_ill_library_symbol'] = array( + '#required' => TRUE, + '#type' => 'textfield', + '#size' => 200, + '#title' => t('Your Relais Library Symbol'), + '#description' => t('Your Relais Library Symbol'), + '#default_value' => variable_get('upei_roblib_ill_library_symbol'), + ); + + $form['upei_roblib_ill_relais_key'] = array( + '#required' => TRUE, + '#type' => 'textfield', + '#size' => 200, + '#title' => t('The Relais API key'), + '#description' => t('The API key used to communicate with the Relais API, Contact Relais International to get your key'), + '#default_value' => variable_get('upei_roblib_ill_relais_key'), + ); + $form['upei_roblib_ill_doi_openurl_pid'] = array( + '#required' => TRUE, + '#type' => 'textfield', + '#size' => 200, + '#title' => t('OpenURL PID'), + '#description' => t('An identifier to call yourself, for the OpenURL endpoint. To use this service you first need to register for an account here: http://www.crossref.org/requestaccount/'), + '#default_value' => variable_get('upei_roblib_ill_doi_openurl_pid'), + ); + + return system_settings_form($form); + +} diff --git a/includes/doi.inc b/includes/doi.inc new file mode 100644 index 0000000..dbf03da --- /dev/null +++ b/includes/doi.inc @@ -0,0 +1,117 @@ +headers); + if ($response->data && + strpos($headers['content-type'], 'text/html') === FALSE && + strpos($response->data, "Malformed DOI") === FALSE + ) { + $crossref_xml = new DOMDocument('1.0'); + if (!$crossref_xml->loadXML($response->data)) { + drupal_set_message(t('Error parseing DOI response, @msg')); + $form['doi']['#value'] = ''; + return $form; + } + else { + return upei_roblib_ill_populate_form_doi($crossref_xml, $form); + } + } + else { + drupal_set_message(t('Error retrieve data using DOI of @doi', array('@doi' => $doi)), 'error'); + $form['doi']['#value'] = ''; + return $form; + } +} + +function upei_roblib_ill_populate_form_doi($crossref_xml, $form) { + $full_title = $crossref_xml->getElementsbyTagName('full_title') + ->item(0)->nodeValue; + $form['Title']['#value'] = $full_title; + $form['ISSN']['#value'] = $crossref_xml->getElementsbyTagName('issn') + ->item(0)->nodeValue; + $form['Date']['#value'] = $crossref_xml->getElementsbyTagName('year') + ->item(0)->nodeValue; + $form['ArticleTitle']['#value'] = $crossref_xml->getElementsbyTagName('title') + ->item(0)->nodeValue; + $form['ImageOrPageNumber']['#value'] = $crossref_xml->getElementsbyTagName('first_page') + ->item(0)->nodeValue; + $form['PagesRequested']['#value'] = $crossref_xml->getElementsbyTagName('last_page') + ->item(0)->nodeValue; + $form['Volume']['#value'] = $crossref_xml->getElementsbyTagName('volume') + ->item(0)->nodeValue; + $form['Issue']['#value'] = $crossref_xml->getElementsbyTagName('issue') + ->item(0)->nodeValue; + foreach ($crossref_xml->getElementsbyTagName('person_name') as $person) { + $role = $person->getAttribute('contributor_role'); + $sequence = $person->getAttribute('sequence'); + if ($person->getAttribute('contributor_role') == 'author' && + $person->getAttribute('sequence') == 'first' + ) { + $author = $person->getElementsbyTagName('surname') + ->item(0)->nodeValue . ',' . $person->getElementsbyTagName('given_name') + ->item(0)->nodeValue; + } + } + $form['ArticleAuthor']['#value'] = $author; + $form['doi']['#value'] = ''; + return $form; +} + +/** + * Get the DOI name for the given url. + * + * @param string $doi_url + * A DOI url, or a DOI name with or without the "doi:" prefix. If the $url + * is a CrossRef ID passed in as doi:10.1016/j.tiv.2011.10.017 or + * 10.1111/eve.12339 it will not get changed by running it through parse_url. + * + * @return string + * A string containing the DOI name. + */ +function upei_roblib_ill_doi_name_from_url($doi_url) { + // Allows for DOI to be entered in the following formats: 10.1111/eva.12339, + // http://dx.doi.org/10.1111/eva.12339, http://doi.org/10.1111/eva.12339, + // or doi:10.1111/eva.12339. Removing whitespace before parsing the url and + // then removing any trailing "/" from the returned path. + $doi_name = trim(parse_url(trim($doi_url), PHP_URL_PATH), '/'); + return $doi_name; +} + + +/** + * Perform a request to CrossRef for the given ID. + * + * @param string $doi + * A DOI to lookop. May be provided with or without the "doi:" prefix. Can + * also pass a full DOI url (i.e, http://doi.org/10.1111/eva.12339 ). + * + * @return object + * An object as provided by drupal_http_request(). + */ +function upei_roblib_ill_doi_load($doi) { + // Allows for $id to pass a DOI url string or the DOI name. + $id = upei_roblib_ill_doi_name_from_url($doi); + $openurl = variable_get('upei_roblib_ill_doi_openurl', 'http://www.crossref.org/openurl'); + $openurl_pid = variable_get('upei_roblib_ill_doi_openurl_pid', 'user@example.com'); + $url = url($openurl, array( + 'query' => array( + 'noredirect' => 'true', + 'pid' => $openurl_pid, + 'format' => 'unixref', + 'id' => ((strpos($id, 'doi:') === 0) ? $id : 'doi:' . $id), + ), + )); + return drupal_http_request($url); +} + diff --git a/includes/form.inc b/includes/form.inc new file mode 100644 index 0000000..b4fea65 --- /dev/null +++ b/includes/form.inc @@ -0,0 +1,421 @@ + 'textfield', + '#title' => t('Your First Name'), + '#size' => 50, + '#required' => TRUE, + ); + $form['Surname'] = array( + '#type' => 'textfield', + '#title' => t('Your Last Name'), + '#size' => 50, + '#required' => TRUE, + ); + $form['campus_id'] = array( + '#type' => 'textfield', + '#title' => t('Your Campus ID'), + '#size' => 50, + '#required' => TRUE, + ); + $patron_types = array( + 'None' => t('None'), + 'UPEI Faculty/Staff' => t('UPEI Faculty/Staff'), + 'UPEI Graduate Student' => t('UPEI Graduate Student'), + 'UPEI Undergraduate Student' => t('UPEI Undergraduate Student'), + 'UPEI Honours Undergraduate Student' => t('UPEI Honours Undergraduate Student'), + 'UPEI Alumni Premiere member' => t('UPEI Alumni Premiere member'), + 'UPEI Alumni member' => t('UPEI Alumni member'), + 'Public Borrower' => t('Public Borrower'), + ); + $departments = array( + 'Applied Human Sciences' => t('Applied Human Sciences'), + 'AVC - Biomedical Sciences' => t('AVC - Biomedical Sciences'), + 'AVC - Companion Animals' => t('AVC - Companion Animals'), + 'AVC - Health Management' => t('AVC - Health Management'), + 'AVC - Pathology/Microbiology' => t('AVC - Pathology/Microbiology'), + 'AVC - Lobster Science' => t('AVC - Lobster Science'), + 'Biology' => t('Biology'), + 'Business' => t('Business'), + 'Chemistry' => t('Chemistry'), + 'Classics' => t('Classics'), + 'Diversity and Social Justice Studies' => t('Diversity and Social Justice Studies'), + 'Economics' => t('Economics'), + 'Education' => t('Education'), + 'English' => t('English'), + 'Engineering' => t('Engineering'), + 'Environmental Studies' => t('Environmental Studies'), + 'Fine Arts' => t('Fine Arts'), + 'History' => t('History'), + 'Island Studies' => t('Island Studies'), + 'Island Studies (Masters)' => t('Island Studies (Masters)'), + 'Robertson Library' => t('Robertson Library'), + 'Robertson Library' => t('Robertson Library'), + 'Mathematical and Computational Sciences' => t('Mathematical and Computational Sciences'), + 'Modern Languages' => t('Modern Languages'), + 'Music' => t('Music'), + 'Nursing' => t('Nursing'), + 'Philosophy' => t('Philosophy'), + 'Physics' => t('Physics'), + 'Political Science' => t('Political Science'), + 'Psychology' => t('Psychology'), + 'Religious Studies' => t('Religious Studies'), + 'Sociology/Anthropology' => t('Sociology/Anthropology'), + 'Other' => t('Other (Please add to the Notes field)'), + ); + $form['patron_type'] = array( + '#type' => 'select', + '#title' => t('Your Patron Type'), + '#options' => $patron_types, + '#required' => TRUE, + ); + $form['Department'] = array( + '#type' => 'select', + '#title' => t('Your Department'), + '#options' => $departments, + '#required' => TRUE, + ); + $form['DeliveryAddress'] = array( + '#type' => 'textfield', + '#title' => t('Your Email'), + '#size' => 50, + + '#required' => TRUE, + ); + $form['InstitutionName'] = array( + '#type' => 'textfield', + '#title' => t('Name of Institution'), + '#size' => 50, + '#default_value' => 'University of Prince Edward Island', + '#required' => TRUE, + ); + $form['LibraryName'] = array( + '#type' => 'textfield', + '#title' => t('Name of Institution'), + '#size' => 50, + '#default_value' => 'Robertson Library', + '#required' => TRUE, + ); + $form['notes'] = array( + '#type' => 'textarea', + '#title' => t('Notes'), + '#size' => 50, + '#required' => FALSE, + ); + $form['certify'] = array( + '#type' => 'checkbox', + '#description' => 'I hereby certify that this item is being sought for a fair dealing purpose and any copying will be in accordance with the Copyright Act. ', + '#size' => 50, + '#required' => TRUE, + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Submit', + ); + + return $form; + +} + + +/** + * The bibliographic portion of the ILL form. + * + * @param array $form + * An array representing a Drupal form. + * @param array $form_state + * An array containing the Drupal form state. + * + * @return array + * An array representing the bibliographic form. + */ +function upei_roblib_ill_request_form($form, &$form_state) { + module_load_include('inc', 'upei_roblib_ill', 'includes/utilities'); + $form['#prefix'] = '
'; + $form['#suffix'] = '
'; + $genre = empty(upei_roblib_ill_get_request_variable('genre')) ? 'article' : upei_roblib_ill_get_request_variable('genre'); +//$values = isset($form_state['multistep_values']['upei_roblib_ill_request']) ? orm_state['multistep_values']['ra$fte_the_room'] : array(); + $form['Genre'] = array( + '#type' => 'select', + '#title' => t('Genre'), + '#default_value' => $genre, + '#options' => array( + 'article' => t('Journal Article'), + 'book' => t('Book'), + 'chapter' => t('Book Chapter'), + ), + '#required' => TRUE, + ); + $form['doi'] = array( + '#type' => 'textfield', + '#title' => t('DOI'), + '#default_value' => upei_roblib_ill_get_request_variable('doi'), + '#description' => 'DOI, if you provide a DOI we will attempt to lookup the record for you', + '#size' => 50, + '#states' => array( + 'visible' => array( + ':input[name="Genre"]' => array('value' => 'article'), + ), + ), + '#required' => FALSE, + ); + $form['doi_button'] = array( + '#type' => 'submit', + '#value' => 'lookup DOI', + '#ajax' => array( + 'callback' => 'upei_roblib_ill_doi_callback', + 'method' => 'replace', + 'wrapper' => 'upei-roblib-ill-request-form', + 'effect' => 'fade', + ), + '#states' => array( + 'visible' => array( + ':input[name="Genre"]' => array('value' => 'article'), + ), + ), + '#required' => FALSE, + ); + $form['Title'] = array( + '#type' => 'textfield', + '#title' => t('Source Title'), + '#default_value' => upei_roblib_ill_get_request_variable('title'), + '#description' => 'Journal or Book Title', + '#size' => 50, + '#required' => FALSE, + ); + $form['ArticleTitle'] = array( + '#type' => 'textfield', + '#title' => t('Title'), + '#default_value' => upei_roblib_ill_get_request_variable('atitle'), + '#description' => 'Article or Chapter Title', + '#size' => 50, + '#states' => array( + 'visible' => array( + ':input[name="Genre"]' => array('value' => 'article'), + ), + ), + '#required' => FALSE, + ); + + $form['ISSN'] = array( + '#type' => 'textfield', + '#title' => t('ISSN'), + '#default_value' => upei_roblib_ill_get_request_variable('issn'), + '#size' => 50, + '#states' => array( + 'visible' => array( + ':input[name="Genre"]' => array('value' => 'article'), + ), + ), + '#required' => FALSE, + ); + $form['ArticleAuthor'] = array( + '#type' => 'textfield', + '#title' => t('Author'), + '#default_value' => upei_roblib_ill_get_request_variable('author'), + '#size' => 50, + '#required' => FALSE, + '#description' => 'Article or Chapter Author', + '#states' => array( + 'invisible' => array( + ':input[name="Genre"]' => array('value' => 'book'), + ), + ), + ); + $form['Author'] = array( + '#type' => 'textfield', + '#title' => t('Book Author'), + '#default_value' => upei_roblib_ill_get_request_variable('author'), + '#size' => 50, + '#required' => FALSE, + '#states' => array( + 'invisible' => array( + ':input[name="Genre"]' => array('value' => t('article')), + ), + ), + ); + $form['ISBN'] = array( + '#type' => 'textfield', + '#title' => t('ISBN'), + '#default_value' => upei_roblib_ill_get_request_variable('isbn'), + '#size' => 50, + '#states' => array( + 'invisible' => array( + ':input[name="Genre"]' => array('value' => 'article'), + ), + ), + '#required' => FALSE, + ); + $form['SubTitle'] = array( + '#type' => 'hidden', + '#title' => t('Sub Title'), + '#default_value' => upei_roblib_ill_get_request_variable('ubtitle'), + '#size' => 50, + '#required' => FALSE, + ); + $form['SeriesTitle'] = array( + '#type' => 'hidden', + '#title' => t('Series Title'), + '#default_value' => upei_roblib_ill_get_request_variable('stitle'), + '#size' => 50, + '#required' => FALSE, + ); + + $form['Volume'] = array( + '#type' => 'textfield', + '#title' => t('Volume'), + '#default_value' => upei_roblib_ill_get_request_variable('volume'), + '#size' => 50, + '#required' => FALSE, + '#states' => array( + 'visible' => array( + ':input[name="Genre"]' => array('value' => 'article'), + ), + ), + ); + $form['Issue'] = array( + '#type' => 'textfield', + '#title' => t('Issue'), + '#default_value' => upei_roblib_ill_get_request_variable('issue'), + '#size' => 50, + '#required' => FALSE, + '#states' => array( + 'visible' => array( + ':input[name="Genre"]' => array('value' => 'article'), + ), + ), + ); + $form['ImageOrPageNumber'] = array( + '#type' => 'textfield', + '#title' => t('Start Page'), + '#default_value' => upei_roblib_ill_get_request_variable('spage'), + '#size' => 50, + '#required' => FALSE, + '#states' => array( + 'invisible' => array( + ':input[name="Genre"]' => array('value' => 'book'), + ), + ), + ); + $form['PagesRequested'] = array( + '#type' => 'textfield', + '#title' => t('End Page'), + '#default_value' => upei_roblib_ill_get_request_variable('epage'), + '#size' => 50, + '#required' => FALSE, + '#states' => array( + 'invisible' => array( + ':input[name="Genre"]' => array('value' => 'book'), + ), + ), + ); + $form['Date'] = array( + '#type' => 'textfield', + '#title' => t('Date'), + '#default_value' => upei_roblib_ill_get_request_variable('date'), + '#size' => 50, + '#required' => FALSE, + ); + $form['next'] = array( + '#type' => 'submit', + '#value' => t('Next') + ); + $form_state['stage'] = 'upei_roblib_ill_auth'; + return $form; + +} diff --git a/includes/relais.inc b/includes/relais.inc new file mode 100644 index 0000000..ab6841c --- /dev/null +++ b/includes/relais.inc @@ -0,0 +1,80 @@ + 'PCU', + ); + $relais_arr['BibliographicInfo'] = upei_roblib_ill_clean_array($form_state['storage']['upei_roblib_ill_request_form']); + //$relais_arr['BibliographicInfo'] + $relais_arr['DeliveryAddress'] = upei_roblib_ill_clean_array($form_state['storage']['upei_roblib_ill_auth_form']); + $request_info = upei_roblib_ill_request_info_array($relais_arr['BibliographicInfo'], $form_state['values']['notes']); + $relais_arr['RequestInfo'] = $request_info; + $relais_json = json_encode($relais_arr); + $options = array( + 'method' => 'POST', + 'data' => $relais_json, + 'timeout' => 15, + 'headers' => array('Content-Type' => 'application/json'), + ); + $result = drupal_http_request($url . '?aid=' . $aid, $options); + if ($result->code == '200') { + $response_json = $result->data; + $response_arr = json_decode($response_json); + //TODO remove or improve the line below after testing + drupal_set_message('submitted request to relais' . $response_json); + } + else { + drupal_set_message(t('Error submitting request to relais, @code, @message', array( + '@code' => $result->code, + '@message' => $result->message + )), 'error'); + } +} + +/** + * Request an aid from Relais. + * @param string $barcode + * A patron_id, campus_id or barcode to identify a user + * @return string/null + * Returns a Relais authentication id (token) or NULL on if authentication fails + */ +function upei_roblib_ill_authenticate($barcode) { + $url = variable_get('upei_roblib_ill_auth_url', 'https://caul-cbua.relais-host.com/portal-service/user/authentication'); + $json_arr = array(); + $json_arr['ApiKey'] = variable_get('upei_roblib_ill_relais_key'); + $json_arr['UserGroup'] = 'patron'; + $json_arr['LibrarySymbol'] = variable_get('upei_roblib_ill_library_symbol'); + $json_arr['PatronId'] = $barcode; + $request_json = json_encode($json_arr); + $options = array( + 'method' => 'POST', + 'data' => $request_json, + 'timeout' => 15, + 'headers' => array('Content-Type' => 'application/json'), + ); + $result = drupal_http_request($url, $options); + if ($result->code == '200') { + $response_data = json_decode($result->data, TRUE); + if (isset($response_data['Problem'])) { + drupal_set_message(t('Error retrieving authentication token, @message', array('@message' => $response_data['Problem']['Message'])), 'error'); + return NULL; + } + $aid = $response_data['AuthorizationId']; + } + return isset($aid) ? $aid : NULL; + +} + diff --git a/includes/utilities.inc b/includes/utilities.inc new file mode 100644 index 0000000..09c524a --- /dev/null +++ b/includes/utilities.inc @@ -0,0 +1,76 @@ + 'UPEI Roblib ILL settings', + 'description' => 'Conifiguration for the UPEI ILL forms', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('upei_roblib_ill_admin_form'), + 'access arguments' => array('access administration pages'), + 'file' => 'includes/admin.form.inc', + 'type' => MENU_NORMAL_ITEM, + ); + $items['upei/roblib/ill'] = array( + 'title' => 'UPEI ILL Form', + 'description' => 'The entry point for the UPEI ILL forms', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('upei_roblib_ill_form'), + 'access arguments' => array('access content'), + 'file' => 'includes/form.inc', + 'type' => MENU_NORMAL_ITEM, + ); + return $items; +} + +function upei_roblib_ill_doi_callback($form, &$form_state) { + module_load_include('inc', 'upei_roblib_ill', 'includes/doi'); + $form = upei_roblib_ill_doi_get_data($form, $form_state['values']['doi']); + return $form; +} + + +