-
Notifications
You must be signed in to change notification settings - Fork 2
/
ares.module
625 lines (554 loc) · 21.4 KB
/
ares.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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
<?php
require_once(dirname(__FILE__) . '/config.php');
require_once(dirname(__FILE__) . '/common.php');
/**
* Implementation of hook_init().
*/
function ares_init() {
if (drupal_is_front_page()) {
drupal_add_css(drupal_get_path('module', 'ares') . '/table_skins/cu/ares-table-cu-style.css');
drupal_add_js(drupal_get_path('module', 'ares') . '/ares.js'); // added here so it works on home page too
}
}
/**
* Implementation of hook_permission().
*/
function ares_permission() {
return array('Administer Ares' => array('title' => 'Administer Ares'));
}
/**
* Implementation of hook_menu().
*/
function ares_menu() {
$items['admin/structure/ares'] = array(
'title' => 'Ares Locations',
'description' => "Configure which Ares locations to use on this site.",
'page callback' => 'ares_admin_overview',
'access arguments' => array('Administer Ares'),
'type' => MENU_NORMAL_ITEM,
'file' => 'ares.admin.inc',
);
$items['admin/structure/ares/add'] = array(
'title' => 'Add Ares Location',
'page callback' => 'drupal_get_form',
'page arguments' => array('ares_form_location'),
'access arguments' => array('Administer Ares'),
'type' => MENU_NORMAL_ITEM,
'parent' => 'admin/structure/ares',
'file' => 'ares.admin.inc',
);
$items['admin/structure/ares/edit/%ares_location'] = array(
'title' => 'Edit Ares Location',
'page callback' => 'drupal_get_form',
'page arguments' => array('ares_form_location', 4),
'access arguments' => array('Administer Ares'),
'type' => MENU_CALLBACK,
'parent' => 'admin/structure/ares',
'file' => 'ares.admin.inc',
);
# $items['ares/items/%'] = array(
# 'title' => '',
# 'page callback' => 'drupal_get_form',
# 'page arguments' => array('ares_form_location', 2),
# 'access arguments' => array('Administer Ares'),
# 'type' => MENU_CALLBACK,
# 'parent' => 'admin/structure/ares',
# 'file' => 'ares.admin.inc',
# );
$items['ares/get_courses_json/%'] = array(
'title' => 'Get courses for a library as JSON data.',
'page callback' => 'get_courses_json',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['ares/get_course_title_json/%/%'] = array(
'title' => 'Get title and author information for a course as JSON data.',
'page callback' => 'get_course_title_json',
'page arguments' => array(2, 3),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['ares/get_items_json/%'] = array(
'title' => 'Get items for a course as JSON data.',
'page callback' => 'get_items_json',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['ares/library/%'] = array(
'title' => 'Course Reserves',
'page callback' => 'ares_page',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['ares/library/%/course'] = array(
'title' => 'Course Reserves',
'page callback' => 'ares_page',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['ares/search'] = array(
'title' => 'Search Course Reserves',
'page callback' => 'ares_search_list',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
$items['ares/autocomplete'] = array(
'title' => 'Ares Autocomplete',
'page callback' => 'ares_autocomplete',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_help().
*/
function ares_help($path, $arg) {
switch ($path) {
case 'admin/structure/ares':
return '<p>' . t('Current Ares locations are listed below, and <a href="@addares">new Ares locations may be added</a>. For each Ares location, a block may be enabled at the <a href="@block">blocks administration page</a>.', array('@addares' => url('admin/structure/ares/add'), '@block' => url('admin/structure/block'))) . '</p>';
}
}
/**
* Implementation of hook_block_info().
*/
function ares_block_info() {
$blocks = array();
$result = db_query("SELECT alid, name FROM {ares_location} ORDER BY name");
foreach ($result as $location) {
$blocks['location-'. $location->alid . '-separate']['info'] = t('!name Library Ares reserves (results display separately)', array('!name' => ucwords(strtolower($location->name))));
$blocks['location-'. $location->alid . '-separate']['pages'] = 'ares/library/*';
$blocks['location-'. $location->alid . '-inline']['info'] = t('!name Library Ares reserves (results display inline)', array('!name' => ucwords(strtolower($location->name))));
}
$blocks['ares-front-page']['info'] = t('Front-page block for Ares course search');
return $blocks;
}
/**
* Implementation of hook_block_save().
*/
function ares_block_save ($delta = '', $edit=array()) {
//$value = db_query("UPDATE {ares_location} SET block = :block WHERE alid = :alid", array(':block' => $edit['block'], ':alid' => $id))->fetchObject();
$value = db_update('ares_location')
->fields(array('block' => $edit['block']))
->condition('alid', $id, '=')
->execute();
}
/**
* Implementation of hook_block_view().
*/
function ares_block_view ($delta = '') {
if ($delta == 'ares-front-page') {
$block['title'] = 'Courses';
$block['content'] = drupal_get_form('ares_search_form');
return $block;
}
else {
list($type, $id, $style) = explode('-', $delta);
$location = db_query("SELECT alid, name, block FROM {ares_location} WHERE alid = :alid", array(':alid' => $id))->fetchObject();
if (isset($location->name)) {
$block['subject'] = check_plain(ucwords(strtolower($location->name)) . ' Course Reserves');
$block['content'] = ares_block_course_select($location->name, $style);
return $block;
}
}
}
/**
* Load an Ares location.
*
* @param $alid
* The Ares location id.
* @return
* An associative array describing the Ares location.
*/
function ares_location_load($alid) {
static $ares_locations;
if (!isset($ares_locations[$alid])) {
$ares_locations[$alid] = db_query("SELECT * FROM {ares_location} WHERE alid = :alid", array(':alid' => $alid))->fetchObject();
}
return $ares_locations[$alid];
}
/**
* Add/edit/delete Ares locations.
*
* @param $edit
* An associative array describing the location to be added/edited/deleted.
*/
function ares_save_location($edit) {
$link_path = 'admin/structure/ares/edit/location/';
if (!empty($edit['alid'])) {
$link_path .= $edit['alid'];
if (!empty($edit['name'])) {
//db_query("UPDATE {ares_location} SET name = ':name' WHERE alid = :alid", array(':name' => $edit['name'], ':alid' => $edit['alid']));
db_update('ares_location')
->fields(array('name' => $edit['name']))
->condition('alid', $edit['alid'])
->execute();
$op = 'update';
}
else {
//db_query("DELETE FROM {ares_location} WHERE alid = :alid", array(':alid' => $edit['alid']));
db_delete('ares_location')->condition('alid', $edit['alid'])->execute();
// Make sure there is no active block for this location.
//db_query("DELETE FROM {blocks} WHERE module = ':module' AND delta = ':delta'", array(':module' => 'ares', ':delta' => 'location-' . $edit['alid']));
db_delete('block')
->condition(db_and()->condition('module', 'ares')->condition('delta', 'location-' . $edit['alid']))
->execute();
$edit['name'] = '';
$op = 'delete';
}
}
else if (!empty($edit['name'])) {
// A single unique id for bundles and feeds, to use in blocks
$id = db_insert('ares_location')
->fields(array('name' => $edit['name'],
'block' => 5))
->execute();
$link_path .= $id;
$op = 'insert';
}
}
function ares_page($library='ALL', $course=NULL) {
$style='inline';
$form = drupal_get_form('ares_search_form');
$output = drupal_render($form); // pass by reference
$output .= ares_output_course_select($library, $style);
$output .= ares_output_course_items($library, $style);
$output .= ares_output_unobtrusive_js($library, $course, $style);
return $output;
}
function ares_block_course_select($library, $style) {
$output = ares_output_course_select($library, $style);
$output .= ares_output_course_items($library, $style);
$output .= ares_output_unobtrusive_js($library, NULL, $style);
return $output;
}
/**
* show block containing emergency message
* This is a regular block with the text entered by content people
* to be displayed above the search results
* see https://www.drupal.org/project/drupal/issues/957038
*/
function ares_block_emergency_message() {
$block_id = 50;
$block = block_load('block', $block_id);
if (empty($block->bid) === false) {
$block_element = _block_get_renderable_array(_block_render_blocks(array($block)));
$output = drupal_render($block_element);
return $output;
}
return '';
}
# NOTE: two elements in the output here have been hidden — the
# drop-down select and the label — because they shouldn't be
# visible to the user, but JS doesn't seem to work without them
function ares_output_course_select($library, $style) {
$spinner_src = base_path() . drupal_get_path('module', 'ares') . '/loading.gif';
$submit_url = url('ares/get_courses_json/' . $library);
$form_url = url('ares/library');
$output = ares_block_emergency_message();
$output .= <<<HERE
<a href="$submit_url" id="courselist-link-$library-$style"></a>
<form action="$form_url" accept-charset="UTF-8" method="get"
id="courselist-form-$library-$style" class="courselist-form">
<div class="form-item edit-courselist-wrapper" id="edit-courselist-wrapper-$library-$style">
<label for="edit-courselist" class="hidden">Select a course: </label>
<span id="course-spinner-$library-$style"><img src="$spinner_src"> Loading...</span>
<input type="hidden" name="library" value="$library">
<select name="course" class="form-select edit-courselist hidden" id="edit-courselist-$library-$style" style="display: none">
</select>
</div>
</form>
HERE;
return $output;
}
function ares_output_course_items($library, $style) {
$spinner_src = base_path() . drupal_get_path('module', 'ares') . '/loading.gif';
$items_url = url('ares/get_items_json/');
$title_url = url('ares/get_course_title_json/');
$output = <<<HERE
<span id="items-spinner-$library-$style" style="display: none"><img src="$spinner_src"> Loading...</span>
<a href="$items_url" id="itemlist-link-$library-$style-items"></a>
<a href="$title_url" id="itemlist-link-$library-$style-title"></a>
<div id="reserve-items-$library-$style"
class="reserve-items"
style="display: none">
<h3></h3>
<table id="course-reserves-$library-$style" class="tablesorter standard course-reserves sticky-enabled table table-striped table-responsive">
<thead>
<tr>
<th class="item">Title</th>
<th class="author">Author</th>
<th class="callnumber">Location/Call Number</th>
<th class="status">Due Back</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
HERE;
return $output;
}
function ares_output_unobtrusive_js($library, $course=NULL, $style='inline') {
drupal_add_js(drupal_get_path('module', 'ares') . '/ares.js');
drupal_add_js(drupal_get_path('module', 'ares') . '/jquery.tablesorter.min.js');
drupal_add_js(drupal_get_path('module', 'ares') . '/moment.min.js');
#drupal_add_js(array('ares' => array('library' => $library)), 'setting');
$output = <<<HERE
<script>
// document.ready() slows page loading
//$(document).ready(function () {
jQuery(window).load(function () {
populate_course_selector("$library", "$course", "$style");
jQuery("#course-reserves-$library-$style").tablesorter({sortList: [[0,0]] , widgets:["zebra"]});
});
</script>
HERE;
return $output;
}
/**
* Call get_courses_json() via URL, return JSON.
*
*/
function get_courses_json($library) {
global $courses_url;
static $ares_courses_json;
$cid = 'ares_courses_' . $library;
$url = $courses_url . $library;
$ares_courses_json = get_and_cache_json($cid, $url);
output_json_string($ares_courses_json);
}
/**
* Call get_items_json() via URL, return JSON.
*
*/
function get_items_json($course) {
global $reserves_url;
static $ares_json;
$url = $reserves_url . $course;
$ares_json = get_json($url);
output_json_string($ares_json);
}
function get_course_title_json($library, $course) {
global $courses_url;
static $ares_course_title_json;
$cid = 'ares_courses_' . $library;
$url = $courses_url . $library;
$ares_course_title_json = get_and_cache_json($cid, $url);
$json = '';
$course_cid = 'ares_course_title_' . $course;
if (($cached = cache_get($course_cid, 'cache')) && ! empty($cached->data)) {
$json = $cached->data;
} else {
$courses = json_decode($ares_course_title_json)->{"courseList"};
$course_titles = array();
foreach ($courses as $acourse) {
$course_title = '';
if ($acourse->{"id"} == $course) {
$course_title .= $acourse->{"displayCourseNumber"} . ': ' . $acourse->{"courseName"};
if (trim($acourse->{"courseCode"}) != '') {
$course_title .= '<div>Section ' . $acourse->{"courseCode"} . '</div>';
}
if (trim($acourse->{"instructor"}) != '') {
$course_title .= '<div>Instructor: ' . $acourse->{"instructor"} . '</div>';
}
}
if ($course_title != '') {
$course_titles[strtoupper(trim($acourse->{"displayCourseNumber"})) . strtoupper(trim($acourse->{"courseCode"}))] = $course_title;
}
}
ksort($course_titles);
$json = '{"value" : "' . join("<br/>", $course_titles) . '"}';
cache_set($course_cid, $json, 'cache');
}
output_json_string($json);
}
function cache_all_courses() {
global $courses_url;
$library = 'ALL';
$cid = 'ares_courses_' . $library;
$url = $courses_url . $library;
get_and_cache_json($cid, $url);
}
function cache_all_libraries() {
global $libraries_url;
get_and_cache_json('ares_libraries', $libraries_url);
}
/**
* Implementation of hook_cron().
*/
function ares_cron() {
cache_clear_all('ares', 'cache', TRUE);
cache_all_courses();
cache_all_libraries();
}
/****************** Search field functionality ****************/
function ares_search_form($form, &$form_state) {
$form['search_box'] = array(
'#type' => 'textfield',
'#id' => 'search_box',
'#name' => 'search_box',
'#autocomplete_path' => 'ares/autocomplete',
'#attributes' => array('class'=> array('auto_submit')),
//'#default_value' => 'peanut butter',
'#size' => 15,
'#maxlength' => 255,
'#prefix' => '<label class="sr-only" for="search_box">Search course reserves</label>'
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
'#attributes' => array('class' => array('go')),
);
return $form;
}
function ares_search_form_validate($form, &$form_state) {
if ($form_state['values']['search_box'] == '') {
form_set_error('search_box', t('This search field cannot be empty.'));
$form_state['rebuild'] = TRUE;
}
}
function ares_search_form_submit($form, &$form_state) {
$form_state['redirect'] = 'ares/search/' . $form_state['values']['search_box'];
}
function ares_search_list($query) {
$courses = ares_search_reserves(urldecode($query));
//$displayCourseId = '';
$displayCourseId = (empty($courses) ? NULL : $courses[0]->id); # The course to show as the main result
drupal_get_messages(); // Empty the message queue
if (count($courses) == 0) {
drupal_set_message('No current course reserve items were found.
Please check that you have correctly entered the course number, course name, or instructor. If reserve requests have been submitted, they may be in-progress and show up here shortly. If you or your instructor are expecting reserve items to be available now for this course, please <a href="mailto:[email protected]">notify us</a>.', 'error');
}
elseif (count($courses) > 1) {
usort($courses, '_ares_sort_course_names');
$displayCourseId = $courses[0]->id;
# This code is used to create links to alternate sections for the user.
# Since the first course in the list is already displayed as the primary
# result, there's no need to link to it here.
array_shift($courses);
$sectionLinks = array();
foreach ($courses as $course) {
$sectionQuery = $course->displayCourseNumber . '.' . $course->courseCode;
$link = "<a href=\"/ares/search/$sectionQuery\">";
$link .= $course->displayCourseNumber;
if ($course->courseCode && $course->courseCode != ' ') {
$link .= '.' . $course->courseCode;
}
else {
#$sectionLinks .= '.---';
}
if ($course->instructor && strlen($course->instructor) > 1) {
$link .= ' (' . $course->instructor . ')';
}
$link .= '</a>';
$sectionLinks[] = $link;
}
$messageText = 'NOTE: Your search matched multiple courses/sections. The first match appears below. If you are looking for a different section, try one of the following links.<ul>';
foreach ($sectionLinks as $link) {
$messageText .= "<li>$link</li>";
}
$messageText .= '</ul>';
drupal_set_message($messageText, 'warning');
}
return ares_page('all', $displayCourseId);
}
/*
* This function is called both by the main search menu callback (ares_search_list)
* and the autocomplete function (ares_autocomplete). It looks for matches of
* $query with course titles, numbers, departments, or instructors
*/
function ares_search_reserves($query) {
global $courses_url;
$url = $courses_url . 'ALL';
$data = get_and_cache_json('ares_courses_ALL', $url);
// $output = "raw: " . print_r($data,1);
// return $output;
// exit(0);
$data = json_decode($data);
$results = array();
foreach ($data->courseList as $course) {
$fullCourseTitle = $course->displayCourseNumber . ': ' . $course->courseName;
$sectionCourseTitle = $course->displayCourseNumber . '.' . $course->courseCode;
# course numbers are in the form ABCD 1234, but people might type in
# ABCD1234 without the space. Find hits for both
$spacelessCourseNumber = str_replace(' ', '', $course->displayCourseNumber);
$spacelessQuery = str_replace(' ', '', $query);
$extendedCourseInfo = _ares_make_extended_course_name($course);
if ((stripos($course->courseName, $query) !== FALSE) ||
(stripos($spacelessCourseNumber, $spacelessQuery) !== FALSE) ||
# It's very unlikely that anyone will type in a full course number and
# name, but this next line helps when a user selects an autocomplete option
# to search on.
(stripos($fullCourseTitle, $query) !== FALSE) ||
# similarly, we need the extended course name with section and instructor
# info to match the new autocomplete list format
(stripos($extendedCourseInfo, $query) !== FALSE) ||
# and now they want to be able to handle a course name AND section
(stripos($sectionCourseTitle, $query) !== FALSE) ||
(stripos($course->courseNumber, $query) !== FALSE) ||
(stripos($course->instructor, $query) !== FALSE) ||
(stripos($course->department, $query) !== FALSE)) {
$results[] = $course;
}
}
return $results;
}
function ares_autocomplete($query) {
$courses = ares_search_reserves($query);
// echo "courses: " .print_r($courses,1);
// return 0;
$output = array();
foreach ($courses as $course) {
$courseId = _ares_make_extended_course_name($course);
$output[$courseId] = $courseId;
}
asort($output);
drupal_json_output($output);
}
/*
* Helper function that takes in a course hash and returns a long-form
* name for it consisting of course number, course name, section number,
* and instructor name — or as many of those parts as are available.
* Output looks like:
* <DEPT> <NUMBER>: <NAME> [Sec. <NUMBER>: <INSTRUCTOR>]
* e.g.,
* ENGL 1147: Writing About Stuff [Sec. 102: Smythe]
*/
function _ares_make_extended_course_name($course) {
$combinedCourseName = $course->displayCourseNumber . ': ' . $course->courseName;
// Add section number and instructor, if available, to distinguish identical
// course numbers
$additional_id = '';
if ($course->courseCode and $course->courseCode != ' ') {
$additional_id = 'Sec ' . $course->courseCode;
}
if ($course->instructor and $course->instructor != ' ') {
if ($additional_id != '') {
$additional_id .= ': ';
}
list($lastName) = explode(',', $course->instructor);
$additional_id .= $lastName;
}
if ($additional_id != '') {
$additional_id = "[$additional_id]";
}
return $combinedCourseName . ' ' . $additional_id;
}
function _ares_sort_course_names($a, $b) {
$aName = $a->displayCourseNumber;
$bName = $b->displayCourseNumber;
$aSection = $a->courseCode;
$bSection = $b->courseCode;
if ($aName == $bName) {
if ($aSection == $bSection) { return 0; }
return ($aSection < $bSection) ? -1 : 1;
}
return ($aName < $bName) ? -1 : 1;
}