1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
3 use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN HTTP_GONE HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST HTTP_NOT_FOUND);
4 use OpenSRF::Utils::Logger qw/$logger/;
5 use OpenILS::Utils::CStoreEditor qw/:funcs/;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Application::AppUtils;
10 my $U = 'OpenILS::Application::AppUtils';
16 $ctx->{page} = 'course';
17 $ctx->{readonly} = $self->cgi->param('readonly');
19 my $course_id = $ctx->{page_args}->[0];
21 return Apache2::Const::HTTP_BAD_REQUEST
22 unless $course_id and $course_id =~ /^\d+$/;
24 $ctx->{course} = $U->simplereq(
26 'open-ils.circ.courses.retrieve',
30 $ctx->{instructors} = $U->simplereq(
32 'open-ils.circ.course_users.retrieve',
36 $ctx->{course_materials} = $U->simplereq(
38 'open-ils.circ.course_materials.retrieve.fleshed',
39 {course => $course_id}
41 return Apache2::Const::OK;
47 my $internal = $args{internal};
50 my $e = $self->editor;
53 $ctx->{page} = 'cresult' unless $internal;
56 $ctx->{hit_count} = 0;
57 $ctx->{search_ou} = $self->_get_search_lib();
58 my $page = $cgi->param('page') || 0;
59 my $offset = $page * $limit;
61 $ctx->{page_size} = $limit;
62 $ctx->{search_page} = $page;
63 $ctx->{pagable_limit} = 50;
65 # fetch this page plus the first hit from the next page
67 $limit = $offset + $limit + 1;
71 my ($user_query, $query, @queries, $modifiers) = _prepare_course_search($cgi, $ctx);
73 return Apache2::Const::OK unless $query;
75 $ctx->{user_query} = $user_query;
76 $ctx->{processed_search_query} = $query;
78 my $course_numbers = ();
84 # Handle is_archived checkbox and Org Selector
85 my $search_orgs = $U->get_org_descendants($ctx->{search_ou});
86 push @$and_terms, {'owning_lib' => $search_orgs};
87 push @$and_terms, {'-not' => {'+acmc' => 'is_archived'}} unless $query =~ qr\#include_archived\;
89 # Now let's push the actual queries
90 for my $query_obj (@queries) {
91 my $type = $query_obj->{'qtype'};
92 my $query = $query_obj->{'value'};
93 my $bool = $query_obj->{'bool'};
94 my $contains = $query_obj->{'contains'};
95 my $operator = ($contains eq 'nocontains') ? '!~*' : '~*';
97 if ($type eq 'instructor') {
98 my $in = ($contains eq 'nocontains') ? "not in" : "in";
99 $search_query = {'id' => {$in => {
101 'select' => {'acmcu' => ['course']},
102 'where' => {'usr' => {'in' => {
104 'select' => {'au' => ['id']},
107 {'pref_first_given_name' => {'~*' => $query}},
108 {'first_given_name' => {'~*' => $query}},
109 {'pref_second_given_name' => {'~*' => $query}},
110 {'second_given_name' => {'~*' => $query}},
111 {'pref_family_name' => {'~*' => $query}},
112 {'family_name' => {'~*' => $query}}
118 $search_query = ($contains eq 'nocontains') ?
119 {'+acmc' => { $type => {$operator => $query}}} :
120 {$type => {$operator => $query}};
124 push @$or_terms, $search_query;
127 if ($bool eq 'and') {
128 push @$and_terms, $search_query;
132 if ($or_terms and @$or_terms > 0) {
133 if ($and_terms and @$and_terms > 0) {
134 push @$or_terms, $and_terms;
136 $where_clause = {'-or' => $or_terms};
138 $where_clause = {'-and' => $and_terms};
141 my $hits = $e->json_query({
143 "select" => {"acmc" => ['id']},
144 "where" => $where_clause
147 my $results = $e->json_query({
149 "select" => {"acmc" => [
159 "order_by" => {"acmc" => ['id']},
160 "where" => $where_clause
162 for my $result (@$results) {
163 push @{$ctx->{courses}}, {
165 course_number => $result->{course_number},
166 section_number => $result->{section_number},
167 owning_lib => $result->{owning_lib},
168 name => $result->{name},
169 is_archived => $result->{is_archived},
174 #$ctx->{courses} = $@courses;#[{id=>10, name=>"test", course_number=>"LIT"}];
175 $ctx->{hit_count} = @$hits || 0;
176 #$ctx->{hit_count} = 0;
177 return Apache2::Const::OK;
180 sub _prepare_course_search {
181 my ($cgi, $ctx) = @_;
183 my ($user_query, @queries) = _prepare_query($cgi);
187 my $query = $user_query;
188 $query .= ' ' . $ctx->{global_search_filter} if $ctx->{global_search_filter};
190 foreach ($cgi->param('modifier')) {
191 $query = ('#' . $_ . ' ' . $query) unless $query =~ qr/\#\Q$_/;
195 foreach (grep /^fi:/, $cgi->param) {
197 my $term = join(",", $cgi->param($_));
198 $query .= " $1($term)" if length $term;
201 return () unless $query;
203 return ($user_query, $query, @queries);
209 return $cgi->param('query') unless $cgi->param('qtype');
212 my @part_names = qw/qtype contains query bool modifier/;
213 $parts{$_} = [ $cgi->param($_) ] for (@part_names);
217 for (my $i = 0; $i < scalar @{$parts{'qtype'}}; $i++) {
218 my ($qtype, $contains, $query, $bool, $modifier) = map { $parts{$_}->[$i] } @part_names;
219 next unless $query =~ /\S/;
221 $contains = "" unless defined $contains;
224 contains => $contains,
230 $bool = ($bool and $bool eq 'or') ? '||' : '&&';
232 $query = "$qtype:$query";
234 $full_query = $full_query ? "($full_query $bool $query)" : $query;
237 return ($full_query, @queries);