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
|
<?php
include "bootstrap.inc";
function solr_search_permission()
{
return array(
"administer solr-search" => array(
"title" => t("administer solr-search"),
"description" => t("administer solr-search."),
));
}
function solr_search_menu()
{
$items = array();
// solr search admin settings
$items['admin/settings/solr_search'] = array(
'title' => 'TBC Solr-Search admin settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('tbc_solr_search_settings_form'),
'access arguments' => array('administer solr-search'),
'type' => MENU_NORMAL_ITEM,
'file' => 'settings.inc',
);
$items['tbc_solr_search'] = array(
'title' => 'Search within Textbook Companion Examples',
'page callback' => 'drupal_get_form',
'page arguments' => array('tbc_solr_search_form'),
'access callback' => true,
'type' => MENU_NORMAL_ITEM,
);
$items['add_all_books_to_solr'] = array(
'title' => 'Add books to Solr',
'page callback' => 'drupal_get_form',
'page arguments' => array('add_all_books_to_solr_form'),
'access arguments' => array('administer solr-search'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function tbc_solr_search_form($form, &$form_state)
{
$form['tbc_search_key'] = array(
'#type' => 'textfield',
'#title' => t('Enter your keyword (Note: special characters ^ ( ) { } \" \' [ ] are not allowed)'),
'#required' => true,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
'#ajax' => array(
'callback' => 'ajax_solr_search_result_callback',
),
);
$form['tbc_search_result'] = array(
'#type' => 'item',
'#markup' => '<div id="ajax-search-result-replace"></div>',
);
return $form;
}
function tbc_solr_search_form_validate($form, &$form_state)
{
if (preg_match('/[^a-zA-Z0-9]/', $form_state['values']['tbc_search_key'])) {
form_set_error('tbc_search_key', t('Special characters not allowed'));
}
return;
}
function tbc_solr_search_form_submit($form, &$form_state)
{
//drupal_goto('tbc_solr_search', array('key' => $_POST['tbc_search_key']));
// ajax_solr_search_result_callback($form,$form_state);
}
function add_all_books_to_solr_form($form, &$form_state)
{
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add to Solr'),
// '#suffix' => $output,
);
return $form;
}
function add_all_books_to_solr_form_submit($form, &$form_state)
{
global $user;
ini_set('max_execution_time', 500000);
$root_path = variable_get('tbc_example_file_path', '');
//var_dump($root_path);die;
$options = array(
'hostname' => SOLR_SERVER_HOSTNAME,
'port' => SOLR_SERVER_PORT,
'login' => SOLR_SERVER_USERNAME,
'password' => SOLR_SERVER_PASSWORD,
'path' => SOLR_SERVER_COLLECTION_PATH,
);
$client = new SolrClient($options);
$book_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE approval_status=1 AND proposal_id IN (SELECT id from textbook_companion_proposal where proposal_status=3)");
while ($book_data = $book_q->fetchObject()) {
$chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE preference_id = :pref_id", array(":pref_id" => $book_data->id));
while ($chapter_row = $chapter_q->fetchObject()) {
//$example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d AND approval_status = 1", $chapter_row->id);
$query = db_select('textbook_companion_example');
$query->fields('textbook_companion_example');
$query->condition('chapter_id', $chapter_row->id);
$query->condition('approval_status', 1);
$example_q = $query->execute();
while ($example_row = $example_q->fetchObject()) {
//$example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $example_row->id);
$example_files_q = db_query("select * from textbook_companion_preference tcp join textbook_companion_chapter tcc on tcp.id=tcc.preference_id join textbook_companion_example tce ON tcc.id=tce.chapter_id join textbook_companion_example_files tcef on tce.id=tcef.example_id where tcef.example_id= :example_id", array(
':example_id' => $example_row->id,
));
$extensions = array('R', 'r');
$example_data = '';
while ($example_files_row = $example_files_q->fetchObject()) {
if (in_array(end(explode('.', $root_path . $example_files_row->directory_name . '/' . $example_files_row->filepath)), $extensions)) {
// echo $example_files_row->filepath."<br />";
$my_file = $root_path . $example_files_row->directory_name . '/' . $example_files_row->filepath;
$handle = fopen($my_file, 'r');
$example_data = fread($handle, filesize($my_file));
fclose($handle);
}
}
$doc = new SolrInputDocument();
$doc->addField('id', $example_row->id);
$doc->addField('chapter_id', $chapter_row->id);
$doc->addField('book_id', $book_data->id);
$doc->addField('title', mb_convert_encoding($book_data->book, "UTF-8"));
$doc->addField('author', mb_convert_encoding($book_data->author, "UTF-8"));
$doc->addField('chapter', mb_convert_encoding(($chapter_row->number . '. ' . $chapter_row->name), "UTF-8"));
$doc->addField('example', mb_convert_encoding(($example_row->number . '. ' . $example_row->caption), "UTF-8"));
$doc->addField('content', mb_convert_encoding($example_data, "UTF-8"));
$doc->addField('added_by', $user->name);
$doc->addField('timestamp', date('Y-m-d H:i:s'));
$updateResponse = $client->addDocument($doc, false);
$client->commit();
}
}
}
drupal_set_message('Books under are updated to solr');
}
function ajax_solr_search_result_callback($form, $form_state)
{
$cloud_url = variable_get('tbc_r_cloud_url', '');
$output = '';
$path = drupal_get_path('module', 'solr_search');
drupal_add_css($path . '/solr_search.css', 'module');
drupal_add_js($path . '/solr_search.js', 'module');
if (isset($form_state['values']['tbc_search_key'])) {
$key = $form_state['values']['tbc_search_key'];
$output = '';
$numfound = '';
if ($key != '') {
$options = array(
'hostname' => SOLR_SERVER_HOSTNAME,
'login' => SOLR_SERVER_USERNAME,
'password' => SOLR_SERVER_PASSWORD,
'port' => SOLR_SERVER_PORT,
'path' => SOLR_SERVER_COLLECTION_PATH,
);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setQuery('content:' . $key);
$query->setStart(0);
$query->setRows(variable_get('tbc_solr_max_results', ''));
$query->addField('id')->addField('book_id')->addField('title')->addField('author')->addField('chapter')->addField('example')->addField('content')->addSortField('title', 0);
$query_response = $client->query($query);
$response = $query_response->getResponse();
$sno = 0;
$output = '<p><b>' . $response['response']->numFound . ' result(s) found</b></p>';
if ($response['response']->numFound > 0) {
$output .= '<div class="search-result"><div id="topcontrol" title="Scroll Back to Top"><a href="#top"></a></div><ol>';
$download_link = '';
foreach ($response['response']->docs as $doc) {
$sno++;
$output .= '<li>';
$output .= '<p><b>Book: </b>' . $doc->title . ' (' . $doc->author[0] . ')</p>';
$output .= '<p><b>Chapter: </b>' . $doc->chapter[0] . '</p>';
$output .= '<p><b>Example: </b>' . $doc->example[0] . '</p>';
$output .= '<p><b>Links: </b> <a href="' . base_path() . 'textbook-companion/download/book/' . $doc->book_id[0]. '"><span style="font-weight:bold;font-size:16px;">⤵</span> Download entire book</a> <a href="'. $cloud_url.'?eid=' . $doc->id . '" target="_blank"><span style="font-weight:bold;font-size:20px;">»</span> View this example</a></p>';
$output .= '</li>';
}
$output .= '<ol></div>';
}
} else {
$output .= '<p><b>0 result(s) found</b></p>';
}
}
$commands[] = ajax_command_html("#ajax-search-result-replace", $output);
return array('#type' => 'ajax', '#commands' => $commands);
}
|