1 package OpenILS::Application::Circ::Survey;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4 use OpenSRF::EX qw/:try/;
5 use OpenILS::Application::AppUtils;
7 use Time::HiRes qw(time);
9 my $apputils = "OpenILS::Application::AppUtils";
11 #open-ils.storage.direct.action.survey.search.*
16 # - creates a new survey
17 # expects a survey complete with questions and answers
19 __PACKAGE__->register_method(
20 method => "add_survey",
21 api_name => "open-ils.circ.survey.create");
24 my( $self, $client, $user_session, $survey ) = @_;
26 my $user_obj = $apputils->check_user_session($user_session);
27 my $session = $apputils->start_db_session();
28 my $err = undef; my $id;
30 warn "Creating new survey\n" . Dumper($survey) . "\n";
35 $survey = _add_survey($session, $survey);
36 _add_questions($session, $survey);
37 $apputils->commit_db_session($session);
41 $err = "Error creating survey: $e\n";
42 $apputils->rollback_db_session($session);
45 if($err) { throw OpenSRF::EX::ERROR ($err); }
47 # re-retrieve the survey from the db and return it
48 return $self->get_fleshed_survey( $client, $survey->id() );
53 my($session, $survey) = @_;
54 my $req = $session->request(
55 "open-ils.storage.direct.action.survey.create",
58 my $id = $req->gather(1);
61 throw OpenSRF::EX::ERROR
62 ("Unable to create new survey " . $survey->name());
65 warn "Created new survey with id $id\n";
71 my($session, $survey) = @_;
75 my($session, $survey) = @_;
77 # create new questions in the db
78 if( $survey->questions() ) {
79 for my $question (@{$survey->questions()}){
81 $question->survey($survey->id());
82 my $virtual_id = $question->id();
83 $question->clear_id();
86 warn "Creating new question: " . $question->question() . "\n";
87 warn Dumper $question;
89 my $req = $session->request(
90 'open-ils.storage.direct.action.survey_question.create',
92 my $new_id = $req->gather(1);
95 throw OpenSRF::EX::ERROR
96 ("Error creating new survey question " . $question->question() . "\n")
99 warn "added new question with id $new_id\n";
101 # now update the responses to this question
102 if($question->answers()) {
103 for my $answer (@{$question->answers()}) {
104 $answer->question($new_id);
105 _add_answer($session,$answer);
114 my($session, $answer) = @_;
115 warn "Adding answer " . $answer->answer() . "\n";
117 my $req = $session->request(
118 "open-ils.storage.direct.action.survey_answer.create",
120 my $id = $req->gather(1);
122 throw OpenSRF::EX::ERROR
123 ("Error creating survey answer " . $answer->answer() );
126 warn "Added new answer with id $id\n";
131 # retrieve surveys for a specific org subtree.
132 __PACKAGE__->register_method(
133 method => "get_required_surveys",
134 api_name => "open-ils.circ.survey.retrieve.required");
136 sub get_required_surveys {
137 my( $self, $client, $user_session ) = @_;
139 my $user_obj = $apputils->check_user_session($user_session);
140 return $apputils->simple_scalar_request(
142 "open-ils.storage.action.survey.required.atomic",
143 $user_obj->home_ou() );
146 __PACKAGE__->register_method(
147 method => "get_survey_responses",
148 api_name => "open-ils.circ.survey.response.retrieve");
150 sub get_survey_responses {
151 my( $self, $client, $user_session, $survey_id, $user_id ) = @_;
153 warn "retrieing responses $user_session $survey_id $user_id\n";
155 my $user_obj = $apputils->check_user_session($user_session);
156 $user_id = $user_obj->id;
159 my $res = $apputils->simple_scalar_request(
161 "open-ils.storage.direct.action.survey_response.search.atomic",
162 { usr => $user_id, survey => $survey_id } );
164 warn "Surveys: " . Dumper($res);
166 if( $res && ref($res) and $res->[0]) {
167 return [ sort { $a->id() <=> $b->id() } @$res ];
173 __PACKAGE__->register_method(
174 method => "get_all_surveys",
175 api_name => "open-ils.circ.survey.retrieve.all");
177 sub get_all_surveys {
178 my( $self, $client, $user_session ) = @_;
180 my $user_obj = $apputils->check_user_session($user_session);
181 my $surveys = $apputils->simple_scalar_request(
183 "open-ils.storage.action.survey.all.atomic",
184 $user_obj->home_ou() );
187 for my $survey (@$surveys) {
188 push(@fleshed, $self->get_fleshed_survey($client, $survey));
196 __PACKAGE__->register_method(
197 method => "get_fleshed_survey",
198 api_name => "open-ils.circ.survey.fleshed.retrieve");
200 sub get_fleshed_survey {
201 my( $self, $client, $survey_id ) = @_;
203 my $session = OpenSRF::AppSession->create("open-ils.storage");
205 warn "Searching for survey $survey_id\n";
208 if( ref($survey_id) and
209 (ref($survey_id) =~ /^Fieldmapper/)) {
210 $survey = $survey_id;
214 my $sreq = $session->request(
215 "open-ils.storage.direct.action.survey.retrieve",
217 $survey = $sreq->gather(1);
218 if(! $survey) { return undef; }
221 $survey->questions([]);
224 warn "Grabbing survey questions\n";
225 my $qreq = $session->request(
226 "open-ils.storage.direct.action.survey_question.search.survey",
229 my $questions = $qreq->gather(1);
233 for my $question (@$questions) {
235 # add this question to the survey
236 push( @{$survey->questions()}, $question );
238 warn "Grabbing question answers\n";
240 my $ans_req = $session->request(
241 "open-ils.storage.direct.action.survey_answer.search.question",
244 # add this array of answers to this question
245 $question->answers( $ans_req->gather(1) );
250 $session->disconnect();
257 __PACKAGE__->register_method(
258 method => "submit_survey",
259 api_name => "open-ils.circ.survey.submit.session");
261 __PACKAGE__->register_method(
262 method => "submit_survey",
263 api_name => "open-ils.circ.survey.submit.user_id");
267 my( $self, $client, $responses ) = @_;
270 throw OpenSRF::EX::ERROR
271 ("No survey object sent in update");
274 if(!ref($responses)) { $responses = [$responses]; }
276 my $session = $apputils->start_db_session();
279 for my $res (@$responses) {
283 if($self->api_name =~ /session/) {
284 if( ! ($id = $already_seen{$res->usr}) ) {
285 my $user_obj = $apputils->check_user_session($res->usr);
287 $already_seen{$res->usr} = $id;
292 warn "Submitting response with question " . $res->question . "\n";
294 my $req = $session->request(
295 "open-ils.storage.direct.action.survey_response.create",
297 my $newid = $req->gather(1);
298 warn "New response id: $newid\n";
301 throw OpenSRF::EX::ERROR
302 ("Error creating new survey response");
306 $apputils->commit_db_session($session);
307 warn "survey response update completed\n";
313 __PACKAGE__->register_method(
314 method => "get_random_survey",
315 api_name => "open-ils.circ.survey.retrieve.random");
317 sub get_random_survey {
318 my( $self, $client, $user_session ) = @_;
320 my $user_obj = $apputils->check_user_session($user_session);
321 my $surveys = $apputils->simple_scalar_request(
323 "open-ils.storage.action.survey.all.atomic",
324 $user_obj->home_ou() );
326 my $random = int(rand(scalar(@$surveys)));
327 warn "Random survey index for process $$ is $random\n";
328 my $surv = $surveys->[$random];
330 return $self->get_fleshed_survey($client, $surv);
335 __PACKAGE__->register_method(
336 method => "retrieve_survey",
337 api_name => "open-ils.circ.survey.retrieve");