uw_lib_refshelf.module 11.8 KB
Newer Older
Ganesharatnam's avatar
Ganesharatnam committed
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
<?php
/**
 * Purpose of this modules to display the data feeding from Textura in JSON to
 * WCMS Library site.Keyword search functionality for description and name is
 * also supported by this module.
 * Written by Lathangi Ganesh Dec 12, 2017
 */

/**
 * Implements hook_menu().
 */
function uw_lib_refshelf_menu() {
  $items['online-reference-shelf']=array(
    'title' => 'Online Reference Shelf',
    'page callback' => 'uw_lib_refshelf_render_data',
    'access callback' => TRUE,
  );
  return $items;
}

/**
 * Get the element as object
 * @param $uri the URI to get
 * @return object
 */
function uw_lib_refshelf_data($param, $url) {
  $uri = $url . $param;
  $file_headers = @get_headers($uri);
  if (!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
Ganesharatnam's avatar
Ganesharatnam committed
30
    drupal_set_message(t('<h3>URL is not available to collect the data</h3>'));
Ganesharatnam's avatar
Ganesharatnam committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  }
  else{
    $response = file_get_contents($uri);
    if (!empty($response)) {
      return drupal_json_decode($response);
    }
    else{
      drupal_set_message(t('<h3> No Data is available</h3>'));
    }
  }
}

/**
 * Page callback from uw_lib_refshelf_menu()
 */
function uw_lib_refshelf_render_data() {
  $content = "";
  $links = "";
49
50
  //$main_category = '<a href="online-reference-shelf" style="float:right;">Go back to main category</a>';
  $main_category = '<a href="online-reference-shelf">Go back to main categories</a>';
Ganesharatnam's avatar
Ganesharatnam committed
51
52
53
54
55
56

//Check for cat_id and toc_id in GET
  if (array_key_exists("cat_id", $_GET) || array_key_exists("toc_id", $_GET)) {
    if (isset($_GET['cat_id']) && !empty($_GET['cat_id'])) {
      //Displaying Subcategories
      $tit = html_entity_decode($_GET['title']);
Ganesharatnam's avatar
Ganesharatnam committed
57
      $txt = "<h2>" . $tit . "</h2><br />";
Ganesharatnam's avatar
Ganesharatnam committed
58
59
60
61
      $_SESSION['tit'] = $txt;
      $qparam = $_GET['cat_id'];
      $links = uw_lib_refshelf_get_links($qparam, "toc_id");
      if (empty($links)) {
Ganesharatnam's avatar
Ganesharatnam committed
62
        $txt = "<h3>Resources for " . $tit . "</h3><br />";
Ganesharatnam's avatar
Ganesharatnam committed
63
64
65
        $links .= uw_lib_refshelf_get_links($qparam, "resources");
      }
      $_SESSION['toc_links'] = $links;
Ganesharatnam's avatar
Ganesharatnam committed
66
      $content .= $main_category;
Ganesharatnam's avatar
Ganesharatnam committed
67
68
69
70
71
      $content .= $txt;
      $content .= $links;
    }
    elseif (isset($_GET['toc_id']) && !empty($_GET['toc_id'])) {
      //Displaying resources for the subcategories
Ganesharatnam's avatar
Ganesharatnam committed
72
      $content .= $main_category;
Ganesharatnam's avatar
Ganesharatnam committed
73
74
75
76
77
      $content .= $_SESSION['tit'];
      $content .= "<div>" . $_SESSION['toc_links'] . "</div><br />";
      $tit = html_entity_decode($_GET['title']);
      $txt = "<h3>" . $tit . "</h3><br />";
      $content .= $txt;
Ganesharatnam's avatar
Ganesharatnam committed
78
      $qparam = $_GET['toc_id'];
Ganesharatnam's avatar
Ganesharatnam committed
79
80
81
82
83
84
85
86
87
      $content .= uw_lib_refshelf_get_links($qparam, "resources");
    }
  }
  else{
    if (isset($_GET{'pagenum'})) {
      //Pagination View
      $search_res = "";
      $rec_limit = 0;
      $pagenum = $_GET{'pagenum'} + 1;
Ganesharatnam's avatar
Ganesharatnam committed
88
      $offset = $rec_limit * $pagenum;
Ganesharatnam's avatar
Ganesharatnam committed
89
90
91
92
93
94
95
96
97
98
99
      $stored_search_results = $_SESSION['search_results'];
      $word = $_SESSION['search_word'];
      $toc = $_SESSION['top_cat'];
      $title = $_SESSION['txt'];
      $search_res .= uw_lib_refshelf_view_results($stored_search_results, $word, $pagenum, $offset);
      $content = $title . $toc . $search_res;
    }
    else {
      //Displaying the top level links
      $links = "";
      $txt = '<div><p>Find answers to quick factual questions in the online resources below.</p></div>';
Ganesharatnam's avatar
Ganesharatnam committed
100
      //$txt .= '<div><h3><a href="online-reference-shelf">Browse by category</a></h3><br /></div>';
Ganesharatnam's avatar
Ganesharatnam committed
101
      $txt .= '<div><h2>Browse by category</h2><br /></div>';
Ganesharatnam's avatar
Ganesharatnam committed
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
      $_SESSION['txt'] = $txt;
      $param = 0;
      $content .= $txt;
      $links .= uw_lib_refshelf_get_links($param, "cat_id");
      $_SESSION['top_cat'] = $links;
      $content .= $links . '<br \>';

      //Search  at the bottom of links for the firstpage
      $search_form = drupal_get_form('uw_lib_refshelf_form');
      $search_block = drupal_render($search_form );
      $_SESSION['ref_search_blck'] = $search_block;
      $content .= $search_block;
    }
  }
  return $content;
}

/**
 * get the data from json and manipulate the fields
 */
function uw_lib_refshelf_get_links($param, $str) {
  if ($str == 'resources') {
    $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/resources/category/";
  }
  else {
    $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/categories/parent/";
  }
  $param_data = uw_lib_refshelf_data($param, $url);
  $page = "";
  $link = "";
  $desc = "";
  $elements = $param_data['data'];

  foreach ($elements as $element) {
    $link_name = $element['name'];
    $id = $element['id'];

    if (isset($element['url'])) {
      $link_url = $element['url'];
    }
    else {
      $link_url = "?" . $str . "=" . $id . "&title=" . $link_name;
    }
    if (isset($element['description'])) {
Ganesharatnam's avatar
Ganesharatnam committed
146
147
      //$page .= '<div><a href="' . $link_url . '" target=\"_blank\">' . $link_name . '</a></div>';
      $page .= '<div><a href="' . $link_url . '">' . $link_name . '</a></div>';
Ganesharatnam's avatar
Ganesharatnam committed
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
      $page .= "<div>" . $element['description'] . "</div><br />";
    }
    else {
      if (!empty($link_name) && !empty($link_url)) {
        //$page .= '<li><a href="' . $link_url . '">' . $link_name . '</a></li>';
        $page .= '<p><a href="' . $link_url . '">' . $link_name . '</a></p>';
      }
    }
  }
  return $page;
}

/**
 * Implements hook_form().
 */
function uw_lib_refshelf_form($form, &$form_state) {
  $form = array(
    '#action' => '#',
    //We define a simple text field for the "search"
    'name' => array(
      '#type' => 'textfield',
      //The label that will be placed with the field
      '#title' => t('Search'),
      //The description will be placed right below the field
      '#description' => t('Please enter  keyword for search'),
      //If true the system will perform a simple check on submit
      // so that it is never empty
      //'#required' => TRUE,
    ),
    //We define a simple submit button
    'submit' => array(
      '#type' => 'submit',
      '#value' => t('Search'),
    ),
  );

  if (!empty($form_state['results_table'])) {
    $form['results_table'] = array(
      '#markup' => $form_state['results_table']
    );
  }

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Search',
  );
  return $form;
}

/**
 * Implements hook_form_validate().
 */
function uw_lib_refshelf_form_validate($form, &$form_state) {
  if (strlen($form_state['values']['name']) > 50) {
    form_set_error('', t('Word is too long'));
  }
  elseif (strlen($form_state['values']['name']) < 3) {
    form_set_error('', t('Please enter a word more than 2 characters'));
  }
}

/**
 * Implements hook_form_submit().
 */
function uw_lib_refshelf_form_submit($form, &$form_state) {
  $word = $form_state['values']['name'];
  $form_state['results_table'] = uw_lib_refshelf_search($word);
  $form_state['rebuild'] = TRUE;
}

/**
 * To get the appropriate search results for different parameters
 * In this case searching in description and name fields
 */
function uw_lib_refshelf_search($word) {
  //drupal_set_message(t('working'));
  $page = "";
  $param = "";
  $all = array();

  $url = "https://libtextura.uwaterloo.ca/api-auth/v1/refshelf/resources/";
  $param_data = uw_lib_refshelf_data($param, $url);
  $elements = $param_data['data'];

  foreach ($elements as $element) {
    $link_name = ($element['name']);
    $link_url = ($element['url']);
    $desc = $element['description'];

    //keyword search in description
    if (isset($element['description']) && !empty($element['description'])) {
      $desc = $element['description'];
      $status = uw_lib_refshelf_keyword_search($word, $desc);
      $all[] =  uw_lib_refshelf_search_results($status, $desc, $link_name, $link_url, $word);
    }

    //keyword search in name
    if (isset($element['name']) && !empty($element['name'])) {
      $sname = $element['name'];
      $status = uw_lib_refshelf_keyword_search($word, $sname);
      $all[] =  uw_lib_refshelf_search_results($status, $desc, $link_name, $link_url, $word);
    }
  }

  //Eliminate duplicates
  $no_duplicates = array_unique($all);
  $records_cnt = count($no_duplicates);
Ganesharatnam's avatar
Ganesharatnam committed
255
  $actual_records = $records_cnt -1 ;
Ganesharatnam's avatar
Ganesharatnam committed
256

Ganesharatnam's avatar
Ganesharatnam committed
257
  if ($records_cnt < 25) {
Ganesharatnam's avatar
Ganesharatnam committed
258
    $msg = "<br /><br /><div><h3>The following " . $actual_records . " results found for the keyword <b>" . $word . "</b></h3></div><br />";
Ganesharatnam's avatar
Ganesharatnam committed
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
    if (!empty($no_duplicates)) {
      foreach ($no_duplicates as $result) {
        $page .= $result;
      }
    }
    if (!empty($page)) {
      $results = $msg . $page;
      return $results;
    }
    else{
      return "<br /><br /><div><h3>No results found for the keyword <b>" . $word . "</b></h3></div>";
    }
  }
  else{
    $_SESSION['search_results'] =  $no_duplicates;
    $pagenum = 0;
    $offset = 0;
    $ret = uw_lib_refshelf_view_results($no_duplicates, $word, $pagenum, $offset);
    $_SESSION['search_word'] = $word;
    return $ret;
  }
}

/**
 * To highlight the search word in search results
 */
function uw_lib_refshelf_highlight_word($word, $str) {
  $word_style = "<span style='background-color: #ffea30;'>" . $word . "</span>";
  $highlighted = str_ireplace($word, $word_style, $str);
  return $highlighted;
}

/**
 * To display the search results with pagination
 */
function uw_lib_refshelf_view_results($no_duplicates, $word, $pagenum, $offset) {
  $page = "";
  $content = "";
Ganesharatnam's avatar
Ganesharatnam committed
297
298
  $results = "";
  $rec_count = count($no_duplicates);
Ganesharatnam's avatar
Ganesharatnam committed
299
300
301
302
303
304
305
  $rec_limit = 25;
  $left_rec = $rec_count - ($pagenum * $rec_limit) - 2 ;
  $offset = $rec_limit * $pagenum ;
  $limit = $rec_limit * $pagenum;

  if (!empty($no_duplicates)) {
    if ($rec_count > $offset) {
Ganesharatnam's avatar
Ganesharatnam committed
306
      $msg = "<br /><br /><br /><p><h3>The following results found for the keyword <b>" . $word . "</b></h3></p>";
Ganesharatnam's avatar
Ganesharatnam committed
307
308
309
310
311
312
313
314
315
316
317
318
      $cnt = 0;
      $c = 0;
      foreach ($no_duplicates as $result) {
        if ($cnt > $offset) {
          if ($c < $rec_limit) {
        $page .= $result;
            }
          $c++;
        }
        $cnt++;
      }
      if (!empty($page)) {
Ganesharatnam's avatar
Ganesharatnam committed
319
        $results = $msg . $page;
Ganesharatnam's avatar
Ganesharatnam committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
      }
      else {
        $results = "<br /><br /><p>No results found for the keyword <b>" . $word . "</b></p>";
      }
    }
  }
  $pagination = uw_lib_refshelf_pagination($pagenum, $left_rec, $rec_limit);
  $content = $results . $pagination;
  return $content;
}

/**
 * Formatting the search results to display
 */
function uw_lib_refshelf_search_results($status, $desc, $link_name, $link_url, $word) {
  $page = "";
  $desc = uw_lib_refshelf_highlight_word($word, $desc);
  $link_name = uw_lib_refshelf_highlight_word($word, $link_name);
Ganesharatnam's avatar
Ganesharatnam committed
338

Ganesharatnam's avatar
Ganesharatnam committed
339
340
  if ($status) {
    if (!empty($desc)) {
Ganesharatnam's avatar
Ganesharatnam committed
341
      $link = '<span class="ref_links"><a href="' . $link_url . '">' . $link_name . '</a></span>';
Ganesharatnam's avatar
Ganesharatnam committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
      $page .= $link . "<p>" . $desc . "</p>";
    }
    else {
      $page .= "<p>No results found for the word</p>";
    }
  }
  return $page;
}

/**
 * Keyword search in the data array or data string
 */
function uw_lib_refshelf_keyword_search($word, $data) {
  $word = trim($word);
  $word = strtolower($word);

  //if data is array
  if (is_array($data) && !empty($data)) {
    $data = array_map('strtolower', $data);
    if (in_array ($word, $data)) {
      return TRUE;
    }
    else{
      return FALSE;
    }
  }
  else{
  //if data is string
    $str = trim($data);
    $str = strtolower($data);

    if (!empty($word) && !empty($str)) {
      if (strlen($str) > strlen($word)) {
        if (strpos($str, $word) !== FALSE) {
          return TRUE;
        }
        else{
          return FALSE;
        }
      }
    }
  }
}

/**
 * Pagination at bottom for search results - custom
 */
function uw_lib_refshelf_pagination($pagenum, $left_rec, $rec_limit) {
  if ( $left_rec < $rec_limit ) {
Ganesharatnam's avatar
Ganesharatnam committed
391
392
    $last = $pagenum - 2;
    $content = "<a href = \"?pagenum={$last}\">Previous</a>";
Ganesharatnam's avatar
Ganesharatnam committed
393
394
395
396
397
398
399
400
  }
  elseif ($pagenum == 0 ) {
    $content =  "<a href = \"?pagenum={$pagenum}\">Next</a>";
  }
  elseif ($pagenum > 0 ) {
    $last = $pagenum - 2;
    $content .=  "<a href = \"?pagenum={$last}\">Previous</a> |";
    $content .=  "<a href = \"?pagenum={$pagenum}\">Next</a>";
Ganesharatnam's avatar
Ganesharatnam committed
401
402
403
404
  }
  $content = "<table align='center'><tr><td align='center'>" . $content . "</td></tr></table>";
  return $content;
}