Line 0
Link Here
|
|
|
1 |
package C4::ClubsAndServices; |
2 |
|
3 |
# $Id: ClubsAndServices.pm,v 0.1 2007/04/10 kylemhall |
4 |
|
5 |
# This package is intended for dealing with clubs and services |
6 |
# and enrollments in such, such as summer reading clubs, and |
7 |
# library newsletters |
8 |
|
9 |
# Copyright 2012 Kyle Hall |
10 |
# |
11 |
# This file is part of Koha. |
12 |
# |
13 |
# Koha is free software; you can redistribute it and/or modify it under the |
14 |
# terms of the GNU General Public License as published by the Free Software |
15 |
# Foundation; either version 2 of the License, or (at your option) any later |
16 |
# version. |
17 |
# |
18 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
19 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
20 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
21 |
# |
22 |
# You should have received a copy of the GNU General Public License along with |
23 |
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
24 |
# Suite 330, Boston, MA 02111-1307 USA |
25 |
|
26 |
use Modern::Perl; |
27 |
|
28 |
require Exporter; |
29 |
|
30 |
use C4::Context; |
31 |
|
32 |
use vars qw($VERSION @ISA @EXPORT); |
33 |
|
34 |
# set the version for version checking |
35 |
$VERSION = 0.01; |
36 |
|
37 |
=head1 NAME |
38 |
|
39 |
C4::ClubsAndServices - Functions for managing clubs and services |
40 |
|
41 |
=head1 FUNCTIONS |
42 |
|
43 |
=over 2 |
44 |
|
45 |
=cut |
46 |
|
47 |
@ISA = qw( Exporter ); |
48 |
@EXPORT = qw( |
49 |
AddClubOrServiceArchetype |
50 |
UpdateClubOrServiceArchetype |
51 |
DeleteClubOrServiceArchetype |
52 |
|
53 |
AddClubOrService |
54 |
UpdateClubOrService |
55 |
DeleteClubOrService |
56 |
|
57 |
EnrollInClubOrService |
58 |
GetEnrollments |
59 |
GetClubsAndServices |
60 |
GetClubOrService |
61 |
GetClubsAndServicesArchetypes |
62 |
GetClubOrServiceArchetype |
63 |
DoesEnrollmentRequireData |
64 |
CancelClubOrServiceEnrollment |
65 |
GetEnrolledClubsAndServices |
66 |
GetPubliclyEnrollableClubsAndServices |
67 |
GetAllEnrollableClubsAndServices |
68 |
GetCasEnrollments |
69 |
|
70 |
ReserveForBestSellersClub |
71 |
|
72 |
getTodayMysqlDateFormat |
73 |
); |
74 |
|
75 |
=head2 AddClubOrServiceArchetype |
76 |
|
77 |
Creates a new archetype for a club or service |
78 |
An archetype is something after which other things a patterned, |
79 |
For example, you could create a 'Summer Reading Club' club archtype |
80 |
which is then used to create an individual 'Summer Reading Club' |
81 |
*for each library* in your system. |
82 |
|
83 |
Input: |
84 |
$type : 'club' or 'service', could be extended to add more types |
85 |
$title: short description of the club or service |
86 |
$description: long description of the club or service |
87 |
$publicEnrollment: If true, any borrower should be able |
88 |
to enroll in club or service from opac. If false, |
89 |
Only a librarian should be able to enroll a borrower |
90 |
in the club or service. |
91 |
$casData1Title: explanation of what is stored in |
92 |
clubsAndServices.casData1Title |
93 |
$casData2Title: same but for casData2Title |
94 |
$casData3Title: same but for casData3Title |
95 |
$caseData1Title: explanation of what is stored in |
96 |
clubsAndServicesEnrollment.data1 |
97 |
$caseData2Title: Same but for data2 |
98 |
$caseData3Title: Same but for data3 |
99 |
$casData1Desc: Long explanation of what is stored in |
100 |
clubsAndServices.casData1Title |
101 |
$casData2Desc: same but for casData2Title |
102 |
$casData3Desc: same but for casData3Title |
103 |
$caseData1Desc: Long explanation of what is stored in |
104 |
clubsAndServicesEnrollment.data1 |
105 |
$caseData2Desc: Same but for data2 |
106 |
$caseData3Desc: Same but for data3 |
107 |
$caseRequireEmail: If 1, enrollment in clubs or services based on this archetype will require a valid e-mail address field in the borrower |
108 |
record as specified in the syspref AutoEmailPrimaryAddress |
109 |
$branchcode: The branchcode for the branch where this Archetype was created |
110 |
|
111 |
Output: |
112 |
$success: 1 if all database operations were successful, 0 otherwise |
113 |
$errorCode: Code for reason of failure, good for translating errors in templates |
114 |
$errorMessage: English description of error |
115 |
|
116 |
=cut |
117 |
|
118 |
sub AddClubOrServiceArchetype { |
119 |
my ($type, $title, $description, $publicEnrollment, $casData1Title, $casData2Title, |
120 |
$casData3Title, $caseData1Title, $caseData2Title, $caseData3Title, $casData1Desc, $casData2Desc, |
121 |
$casData3Desc, $caseData1Desc, $caseData2Desc, $caseData3Desc, $caseRequireEmail, $branchcode |
122 |
) = @_; |
123 |
|
124 |
## Check for all neccessary parameters |
125 |
if ( !$type ) { |
126 |
return ( 0, "NO_TYPE" ); |
127 |
} |
128 |
if ( !$title ) { |
129 |
return ( 0, "NO_TITLE" ); |
130 |
} |
131 |
if ( !$description ) { |
132 |
return ( 0, "NO_DESCRIPTION" ); |
133 |
} |
134 |
|
135 |
my $success = 1; |
136 |
|
137 |
my $dbh = C4::Context->dbh; |
138 |
|
139 |
my $sth; |
140 |
$sth = $dbh->prepare( |
141 |
"INSERT INTO clubsAndServicesArchetypes ( casaId, type, title, description, publicEnrollment, caseRequireEmail, branchcode, last_updated ) |
142 |
VALUES ( NULL, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)" |
143 |
); |
144 |
$sth->execute( $type, $title, $description, $publicEnrollment, $caseRequireEmail, $branchcode ) or $success = 0; |
145 |
my $casaId = $dbh->{'mysql_insertid'}; |
146 |
$sth->finish; |
147 |
|
148 |
if ($casData1Title) { |
149 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET casData1Title = ? WHERE casaId = ?"); |
150 |
$sth->execute( $casData1Title, $casaId ) or $success = 0; |
151 |
$sth->finish; |
152 |
} |
153 |
if ($casData2Title) { |
154 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET casData2Title = ? WHERE casaId = ?"); |
155 |
$sth->execute( $casData2Title, $casaId ) or $success = 0; |
156 |
$sth->finish; |
157 |
} |
158 |
if ($casData3Title) { |
159 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET casData3Title = ? WHERE casaId = ?"); |
160 |
$sth->execute( $casData3Title, $casaId ) or $success = 0; |
161 |
$sth->finish; |
162 |
} |
163 |
|
164 |
if ($caseData1Title) { |
165 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET caseData1Title = ? WHERE casaId = ?"); |
166 |
$sth->execute( $caseData1Title, $casaId ) or $success = 0; |
167 |
$sth->finish; |
168 |
} |
169 |
if ($caseData2Title) { |
170 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET caseData2Title = ? WHERE casaId = ?"); |
171 |
$sth->execute( $caseData2Title, $casaId ) or $success = 0; |
172 |
$sth->finish; |
173 |
} |
174 |
if ($caseData3Title) { |
175 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET caseData3Title = ? WHERE casaId = ?"); |
176 |
$sth->execute( $caseData3Title, $casaId ) or $success = 0; |
177 |
$sth->finish; |
178 |
} |
179 |
|
180 |
if ($casData1Desc) { |
181 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET casData1Desc = ? WHERE casaId = ?"); |
182 |
$sth->execute( $casData1Desc, $casaId ) or $success = 0; |
183 |
$sth->finish; |
184 |
} |
185 |
if ($casData2Desc) { |
186 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET casData2Desc = ? WHERE casaId = ?"); |
187 |
$sth->execute( $casData2Desc, $casaId ) or $success = 0; |
188 |
$sth->finish; |
189 |
} |
190 |
if ($casData3Desc) { |
191 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET casData3Desc = ? WHERE casaId = ?"); |
192 |
$sth->execute( $casData3Desc, $casaId ) or $success = 0; |
193 |
$sth->finish; |
194 |
} |
195 |
|
196 |
if ($caseData1Desc) { |
197 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET caseData1Desc = ? WHERE casaId = ?"); |
198 |
$sth->execute( $caseData1Desc, $casaId ) or $success = 0; |
199 |
$sth->finish; |
200 |
} |
201 |
if ($caseData2Desc) { |
202 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET caseData2Desc = ? WHERE casaId = ?"); |
203 |
$sth->execute( $caseData2Desc, $casaId ) or $success = 0; |
204 |
$sth->finish; |
205 |
} |
206 |
if ($caseData3Desc) { |
207 |
$sth = $dbh->prepare("UPDATE clubsAndServicesArchetypes SET caseData3Desc = ? WHERE casaId = ?"); |
208 |
$sth->execute( $caseData3Desc, $casaId ) or $success = 0; |
209 |
$sth->finish; |
210 |
} |
211 |
|
212 |
my ( $errorCode, $errorMessage ); |
213 |
if ( !$success ) { |
214 |
$errorMessage = "DB_ERROR"; |
215 |
} |
216 |
|
217 |
return ( $success, $errorMessage ); |
218 |
|
219 |
} |
220 |
|
221 |
=head2 UpdateClubOrServiceArchetype |
222 |
|
223 |
Updates an archetype for a club or service |
224 |
|
225 |
Input: |
226 |
$casaId: id of the archetype to be updated |
227 |
$type : 'club' or 'service', could be extended to add more types |
228 |
$title: short description of the club or service |
229 |
$description: long description of the club or service |
230 |
$publicEnrollment: If true, any borrower should be able |
231 |
to enroll in club or service from opac. If false, |
232 |
Only a librarian should be able to enroll a borrower |
233 |
in the club or service. |
234 |
$casData1Title: explanation of what is stored in |
235 |
clubsAndServices.casData1Title |
236 |
$casData2Title: same but for casData2Title |
237 |
$casData3Title: same but for casData3Title |
238 |
$caseData1Title: explanation of what is stored in |
239 |
clubsAndServicesEnrollment.data1 |
240 |
$caseData2Title: Same but for data2 |
241 |
$caseData3Title: Same but for data3 |
242 |
$casData1Desc: Long explanation of what is stored in |
243 |
clubsAndServices.casData1Title |
244 |
$casData2Desc: same but for casData2Title |
245 |
$casData3Desc: same but for casData3Title |
246 |
$caseData1Desc: Long explanation of what is stored in |
247 |
clubsAndServicesEnrollment.data1 |
248 |
$caseData2Desc: Same but for data2 |
249 |
$caseData3Desc: Same but for data3 |
250 |
$caseRequireEmail: If 1, enrollment in clubs or services based on this archetype will require a valid e-mail address field in the borrower |
251 |
record as specified in the syspref AutoEmailPrimaryAddress |
252 |
|
253 |
Output: |
254 |
$success: 1 if all database operations were successful, 0 otherwise |
255 |
$errorCode: Code for reason of failure, good for translating errors in templates |
256 |
$errorMessage: English description of error |
257 |
|
258 |
=cut |
259 |
|
260 |
sub UpdateClubOrServiceArchetype { |
261 |
my ($casaId, $type, $title, $description, $publicEnrollment, $casData1Title, |
262 |
$casData2Title, $casData3Title, $caseData1Title, $caseData2Title, $caseData3Title, $casData1Desc, |
263 |
$casData2Desc, $casData3Desc, $caseData1Desc, $caseData2Desc, $caseData3Desc, $caseRequireEmail, |
264 |
) = @_; |
265 |
|
266 |
## Check for all neccessary parameters |
267 |
if ( !$casaId ) { |
268 |
return ( 0, "NO_ID" ); |
269 |
} |
270 |
if ( !$type ) { |
271 |
return ( 0, "NO_TYPE" ); |
272 |
} |
273 |
if ( !$title ) { |
274 |
return ( 0, "NO_TITLE" ); |
275 |
} |
276 |
if ( !$description ) { |
277 |
return ( 0, "NO_DESCRIPTION" ); |
278 |
} |
279 |
|
280 |
my $success = 1; |
281 |
|
282 |
my $dbh = C4::Context->dbh; |
283 |
|
284 |
my $sth; |
285 |
$sth = $dbh->prepare( |
286 |
"UPDATE clubsAndServicesArchetypes |
287 |
SET |
288 |
type = ?, title = ?, description = ?, publicEnrollment = ?, |
289 |
casData1Title = ?, casData2Title = ?, casData3Title = ?, |
290 |
caseData1Title = ?, caseData2Title = ?, caseData3Title = ?, |
291 |
casData1Desc = ?, casData2Desc = ?, casData3Desc = ?, |
292 |
caseData1Desc = ?, caseData2Desc = ?, caseData3Desc = ?, caseRequireEmail = ?, |
293 |
last_updated = NOW() WHERE casaId = ?" |
294 |
); |
295 |
|
296 |
$sth->execute( |
297 |
$type, $title, $description, $publicEnrollment, $casData1Title, $casData2Title, |
298 |
$casData3Title, $caseData1Title, $caseData2Title, $caseData3Title, $casData1Desc, $casData2Desc, |
299 |
$casData3Desc, $caseData1Desc, $caseData2Desc, $caseData3Desc, $caseRequireEmail, $casaId |
300 |
) or return ( $success = 0, my $errorMessage = "DB_ERROR" ); |
301 |
$sth->finish; |
302 |
|
303 |
return $success; |
304 |
|
305 |
} |
306 |
|
307 |
=head2 DeleteClubOrServiceArchetype |
308 |
|
309 |
Deletes an Archetype of the given id |
310 |
and all Clubs or Services based on it, |
311 |
and all Enrollments based on those clubs |
312 |
or services. |
313 |
|
314 |
Input: |
315 |
$casaId : id of the Archtype to be deleted |
316 |
|
317 |
Output: |
318 |
$success : 1 on successful deletion, 0 otherwise |
319 |
|
320 |
=cut |
321 |
|
322 |
sub DeleteClubOrServiceArchetype { |
323 |
my ($casaId) = @_; |
324 |
|
325 |
## Paramter check |
326 |
if ( !$casaId ) { |
327 |
return 0; |
328 |
} |
329 |
|
330 |
my $success = 1; |
331 |
|
332 |
my $dbh = C4::Context->dbh; |
333 |
|
334 |
my $sth; |
335 |
|
336 |
$sth = $dbh->prepare("DELETE FROM clubsAndServicesEnrollments WHERE casaId = ?"); |
337 |
$sth->execute($casaId) or $success = 0; |
338 |
$sth->finish; |
339 |
|
340 |
$sth = $dbh->prepare("DELETE FROM clubsAndServices WHERE casaId = ?"); |
341 |
$sth->execute($casaId) or $success = 0; |
342 |
$sth->finish; |
343 |
|
344 |
$sth = $dbh->prepare("DELETE FROM clubsAndServicesArchetypes WHERE casaId = ?"); |
345 |
$sth->execute($casaId) or $success = 0; |
346 |
$sth->finish; |
347 |
|
348 |
return 1; |
349 |
} |
350 |
|
351 |
=head2 AddClubOrService |
352 |
|
353 |
Creates a new club or service in the database |
354 |
|
355 |
Input: |
356 |
$type: 'club' or 'service', other types may be added as necessary. |
357 |
$title: Short description of the club or service |
358 |
$description: Long description of the club or service |
359 |
$casData1: The data described in case.casData1Title |
360 |
$casData2: The data described in case.casData2Title |
361 |
$casData3: The data described in case.casData3Title |
362 |
$startDate: The date the club or service begins ( Optional: Defaults to TODAY() ) |
363 |
$endDate: The date the club or service ends ( Optional ) |
364 |
$branchcode: Branch that created this club or service ( Optional: NULL is system-wide ) |
365 |
|
366 |
Output: |
367 |
$success: 1 on successful add, 0 on failure |
368 |
$errorCode: Code for reason of failure, good for translating errors in templates |
369 |
$errorMessage: English description of error |
370 |
|
371 |
=cut |
372 |
|
373 |
sub AddClubOrService { |
374 |
my ( $casaId, $title, $description, $casData1, $casData2, $casData3, $startDate, $endDate, $branchcode ) = @_; |
375 |
|
376 |
## Check for all neccessary parameters |
377 |
if ( !$casaId ) { |
378 |
return ( 0, "NO_ARCHETYPE" ); |
379 |
} |
380 |
if ( !$title ) { |
381 |
return ( 0, "NO_TITLE" ); |
382 |
} |
383 |
if ( !$description ) { |
384 |
return ( 0, "NO_DESCRIPTION" ); |
385 |
} |
386 |
|
387 |
my $success = 1; |
388 |
|
389 |
if ( !$startDate ) { |
390 |
$startDate = getTodayMysqlDateFormat(); |
391 |
} |
392 |
|
393 |
my $dbh = C4::Context->dbh; |
394 |
|
395 |
my $sth; |
396 |
if ($endDate) { |
397 |
$sth = $dbh->prepare( |
398 |
"INSERT INTO clubsAndServices ( casId, casaId, title, description, casData1, casData2, casData3, startDate, endDate, branchcode, last_updated ) |
399 |
VALUES ( NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)" |
400 |
); |
401 |
$sth->execute( $casaId, $title, $description, $casData1, $casData2, $casData3, $startDate, $endDate, $branchcode ) or $success = 0; |
402 |
} else { |
403 |
$sth = $dbh->prepare( |
404 |
"INSERT INTO clubsAndServices ( casId, casaId, title, description, casData1, casData2, casData3, startDate, branchcode, last_updated ) |
405 |
VALUES ( NULL, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)" |
406 |
); |
407 |
$sth->execute( $casaId, $title, $description, $casData1, $casData2, $casData3, $startDate, $branchcode ) or $success = 0; |
408 |
} |
409 |
$sth->finish; |
410 |
|
411 |
my ( $errorCode, $errorMessage ); |
412 |
if ( !$success ) { |
413 |
$errorMessage = "DB_ERROR"; |
414 |
} |
415 |
|
416 |
return ( $success, $errorCode, $errorMessage ); |
417 |
} |
418 |
|
419 |
=head UpdateClubOrService |
420 |
|
421 |
Updates club or service in the database |
422 |
|
423 |
Input: |
424 |
$casId: id of the club or service to be updated |
425 |
$type: 'club' or 'service', other types may be added as necessary. |
426 |
$title: Short description of the club or service |
427 |
$description: Long description of the club or service |
428 |
$casData1: The data described in case.casData1Title |
429 |
$casData2: The data described in case.casData2Title |
430 |
$casData3: The data described in case.casData3Title |
431 |
$startDate: The date the club or service begins ( Optional: Defaults to TODAY() ) |
432 |
$endDate: The date the club or service ends ( Optional ) |
433 |
|
434 |
Output: |
435 |
$success: 1 on successful add, 0 on failure |
436 |
$errorCode: Code for reason of failure, good for translating errors in templates |
437 |
$errorMessage: English description of error |
438 |
|
439 |
=cut |
440 |
|
441 |
sub UpdateClubOrService { |
442 |
my ( $casId, $casaId, $title, $description, $casData1, $casData2, $casData3, $startDate, $endDate ) = @_; |
443 |
|
444 |
## Check for all neccessary parameters |
445 |
if ( !$casId ) { |
446 |
return ( 0, "NO_CAS_ID" ); |
447 |
} |
448 |
if ( !$casaId ) { |
449 |
return ( 0, "NO_CASA_ID" ); |
450 |
} |
451 |
if ( !$title ) { |
452 |
return ( 0, "NO_TITLE" ); |
453 |
} |
454 |
if ( !$description ) { |
455 |
return ( 0, "NO_DESCRIPTION" ); |
456 |
} |
457 |
|
458 |
my $success = 1; |
459 |
|
460 |
if ( !$startDate ) { |
461 |
$startDate = getTodayMysqlDateFormat(); |
462 |
} |
463 |
|
464 |
my $dbh = C4::Context->dbh; |
465 |
|
466 |
my $sth; |
467 |
if ($endDate) { |
468 |
$sth = $dbh->prepare( |
469 |
"UPDATE clubsAndServices SET casaId = ?, title = ?, description = ?, casData1 = ?, casData2 = ?, casData3 = ?, startDate = ?, endDate = ?, last_updated = NOW() WHERE casId = ?" |
470 |
); |
471 |
$sth->execute( $casaId, $title, $description, $casData1, $casData2, $casData3, $startDate, $endDate, $casId ) |
472 |
or return ( my $success = 0, my $errorCode = 5, my $errorMessage = $sth->errstr() ); |
473 |
} else { |
474 |
$sth = $dbh->prepare( |
475 |
"UPDATE clubsAndServices SET casaId = ?, title = ?, description = ?, casData1 = ?, casData2 = ?, casData3 = ?, startDate = ?, endDate = NULL, last_updated = NOW() WHERE casId = ?" |
476 |
); |
477 |
$sth->execute( $casaId, $title, $description, $casData1, $casData2, $casData3, $startDate, $casId ) |
478 |
or return ( my $success = 0, my $errorCode = 5, my $errorMessage = $sth->errstr() ); |
479 |
} |
480 |
$sth->finish; |
481 |
|
482 |
my ( $errorCode, $errorMessage ); |
483 |
if ( !$success ) { |
484 |
$errorMessage = "DB_ERROR"; |
485 |
} |
486 |
|
487 |
return ( $success, $errorMessage ); |
488 |
} |
489 |
|
490 |
=head2 DeleteClubOrService |
491 |
|
492 |
Deletes a club or service of the given id and all enrollments based on it. |
493 |
|
494 |
Input: |
495 |
$casId : id of the club or service to be deleted |
496 |
|
497 |
Output: |
498 |
$success : 1 on successful deletion, 0 otherwise |
499 |
|
500 |
=cut |
501 |
|
502 |
sub DeleteClubOrService { |
503 |
my ($casId) = @_; |
504 |
|
505 |
if ( !$casId ) { |
506 |
return 0; |
507 |
} |
508 |
|
509 |
my $success = 1; |
510 |
|
511 |
my $dbh = C4::Context->dbh; |
512 |
|
513 |
my $sth; |
514 |
$sth = $dbh->prepare("DELETE FROM clubsAndServicesEnrollments WHERE casId = ?"); |
515 |
$sth->execute($casId) or $success = 0; |
516 |
$sth->finish; |
517 |
|
518 |
$sth = $dbh->prepare("DELETE FROM clubsAndServices WHERE casId = ?"); |
519 |
$sth->execute($casId) or $success = 0; |
520 |
$sth->finish; |
521 |
|
522 |
return 1; |
523 |
} |
524 |
|
525 |
=head EnrollInClubOrService |
526 |
|
527 |
Enrolls a borrower in a given club or service |
528 |
|
529 |
Input: |
530 |
$casId: The unique id of the club or service being enrolled in |
531 |
$borrowerCardnumber: The card number of the enrolling borrower |
532 |
$dateEnrolled: Date the enrollment begins ( Optional: Defauls to TODAY() ) |
533 |
$data1: The data described in ClubsAndServicesArchetypes.caseData1Title |
534 |
$data2: The data described in ClubsAndServicesArchetypes.caseData2Title |
535 |
$data3: The data described in ClubsAndServicesArchetypes.caseData3Title |
536 |
$branchcode: The branch where this club or service enrollment is, |
537 |
$borrowernumber: ( Optional: Alternative to using $borrowerCardnumber ) |
538 |
|
539 |
Output: |
540 |
$success: 1 on successful enrollment, 0 on failure |
541 |
$errorCode: Code for reason of failure, good for translating errors in templates |
542 |
$errorMessage: English description of error |
543 |
|
544 |
=cut |
545 |
|
546 |
sub EnrollInClubOrService { |
547 |
my ( $casaId, $casId, $borrowerCardnumber, $dateEnrolled, $data1, $data2, $data3, $branchcode, $borrowernumber ) = @_; |
548 |
|
549 |
## Check for all neccessary parameters |
550 |
unless ($casaId) { |
551 |
return ( 0, "NO_CASA_ID" ); |
552 |
} |
553 |
unless ($casId) { |
554 |
return ( 0, "NO_CAS_ID" ); |
555 |
} |
556 |
unless ( $borrowerCardnumber || $borrowernumber ) { |
557 |
return ( 0, "NO_BORROWER" ); |
558 |
} |
559 |
|
560 |
my $member; |
561 |
if ($borrowerCardnumber) { |
562 |
$member = C4::Members::GetMember( cardnumber => $borrowerCardnumber ); |
563 |
} elsif ($borrowernumber) { |
564 |
$member = C4::Members::GetMember( borrowernumber => $borrowernumber ); |
565 |
} |
566 |
|
567 |
unless ($member) { |
568 |
return ( 0, "NO_BORROWER_FOUND" ); |
569 |
} |
570 |
|
571 |
my $casa = GetClubOrServiceArchetype( $casaId, 1 ); |
572 |
if ( $casa->{'caseRequireEmail'} ) { |
573 |
my $AutoEmailPrimaryAddress = C4::Context->preference('AutoEmailPrimaryAddress'); |
574 |
unless ( $member->{$AutoEmailPrimaryAddress} ) { |
575 |
return ( 0, "NO_EMAIL" ); |
576 |
} |
577 |
} |
578 |
|
579 |
$borrowernumber = $member->{'borrowernumber'}; |
580 |
|
581 |
if ( isEnrolled( $casId, $borrowernumber ) ) { return ( 0, "ENROLLED" ); } |
582 |
|
583 |
if ( !$dateEnrolled ) { |
584 |
$dateEnrolled = getTodayMysqlDateFormat(); |
585 |
} |
586 |
|
587 |
my $dbh = C4::Context->dbh; |
588 |
my $sth = $dbh->prepare( |
589 |
"INSERT INTO clubsAndServicesEnrollments ( caseId, casaId, casId, borrowernumber, data1, data2, data3, dateEnrolled, dateCanceled, last_updated, branchcode) |
590 |
VALUES ( NULL, ?, ?, ?, ?, ?, ?, ?, NULL, NOW(), ? )" |
591 |
); |
592 |
$sth->execute( $casaId, $casId, $borrowernumber, $data1, $data2, $data3, $dateEnrolled, $branchcode ) |
593 |
or return ( my $success = 0, my $errorCode = 4, my $errorMessage = $sth->errstr() ); |
594 |
$sth->finish; |
595 |
|
596 |
return $success = 1; |
597 |
} |
598 |
|
599 |
=head2 GetEnrollments |
600 |
|
601 |
Returns information about the clubs and services the given borrower is enrolled in. |
602 |
|
603 |
Input: |
604 |
$borrowernumber: The borrowernumber of the borrower |
605 |
|
606 |
Output: |
607 |
$results: Reference to an array of associated arrays |
608 |
|
609 |
=cut |
610 |
|
611 |
sub GetEnrollments { |
612 |
my ($borrowernumber) = @_; |
613 |
|
614 |
my $dbh = C4::Context->dbh; |
615 |
|
616 |
my $sth = $dbh->prepare( |
617 |
"SELECT * FROM clubsAndServices, clubsAndServicesEnrollments |
618 |
WHERE clubsAndServices.casId = clubsAndServicesEnrollments.casId |
619 |
AND clubsAndServicesEnrollments.borrowernumber = ?" |
620 |
); |
621 |
$sth->execute($borrowernumber) or return 0; |
622 |
|
623 |
my @results; |
624 |
while ( my $row = $sth->fetchrow_hashref ) { |
625 |
push( @results, $row ); |
626 |
} |
627 |
|
628 |
$sth->finish; |
629 |
|
630 |
return \@results; |
631 |
} |
632 |
|
633 |
=head2 GetCasEnrollments |
634 |
|
635 |
Returns information about the clubs and services borrowers that are enrolled |
636 |
|
637 |
Input: |
638 |
$casId: The id of the club or service to look up enrollments for |
639 |
|
640 |
Output: |
641 |
$results: Reference to an array of associated arrays |
642 |
|
643 |
=cut |
644 |
|
645 |
sub GetCasEnrollments { |
646 |
my ($casId) = @_; |
647 |
|
648 |
my $dbh = C4::Context->dbh; |
649 |
|
650 |
my $sth = $dbh->prepare( |
651 |
"SELECT * FROM clubsAndServicesEnrollments, borrowers |
652 |
WHERE clubsAndServicesEnrollments.borrowernumber = borrowers.borrowernumber |
653 |
AND clubsAndServicesEnrollments.casId = ? AND dateCanceled IS NULL |
654 |
ORDER BY surname, firstname" |
655 |
); |
656 |
$sth->execute($casId) or return 0; |
657 |
|
658 |
my @results; |
659 |
while ( my $row = $sth->fetchrow_hashref ) { |
660 |
push( @results, $row ); |
661 |
} |
662 |
|
663 |
$sth->finish; |
664 |
|
665 |
return \@results; |
666 |
} |
667 |
|
668 |
=head2 GetClubsAndServices |
669 |
|
670 |
Returns information about clubs and services |
671 |
|
672 |
Input: |
673 |
$type: ( Optional: 'club' or 'service' ) |
674 |
$branchcode: ( Optional: Get clubs and services only created by this branch ) |
675 |
$orderby: ( Optional: name of column to sort by ) |
676 |
|
677 |
Output: |
678 |
$results: Reference to an array of associated arrays |
679 |
|
680 |
=cut |
681 |
|
682 |
sub GetClubsAndServices { |
683 |
my ( $type, $branchcode, $orderby ) = @_; |
684 |
$orderby = 'startDate DESC' unless ($orderby); |
685 |
|
686 |
my $dbh = C4::Context->dbh; |
687 |
|
688 |
my ( $sth, @results ); |
689 |
if ( $type && $branchcode ) { |
690 |
$sth = $dbh->prepare( |
691 |
"SELECT clubsAndServices.casId, |
692 |
clubsAndServices.casaId, |
693 |
clubsAndServices.title, |
694 |
clubsAndServices.description, |
695 |
clubsAndServices.casData1, |
696 |
clubsAndServices.casData2, |
697 |
clubsAndServices.casData3, |
698 |
clubsAndServices.startDate, |
699 |
clubsAndServices.endDate, |
700 |
clubsAndServices.last_updated, |
701 |
clubsAndServices.branchcode |
702 |
FROM clubsAndServices, clubsAndServicesArchetypes |
703 |
WHERE |
704 |
clubsAndServices.casaId = clubsAndServicesArchetypes.casaId |
705 |
AND clubsAndServices.branchcode = ? |
706 |
AND clubsAndServicesArchetypes.type = ? |
707 |
ORDER BY $orderby |
708 |
" |
709 |
); |
710 |
$sth->execute( $branchcode, $type ) or return 0; |
711 |
|
712 |
} elsif ($type) { |
713 |
$sth = $dbh->prepare( |
714 |
"SELECT clubsAndServices.casId, |
715 |
clubsAndServices.casaId, |
716 |
clubsAndServices.title, |
717 |
clubsAndServices.description, |
718 |
clubsAndServices.casData1, |
719 |
clubsAndServices.casData2, |
720 |
clubsAndServices.casData3, |
721 |
clubsAndServices.startDate, |
722 |
clubsAndServices.endDate, |
723 |
clubsAndServices.last_updated, |
724 |
clubsAndServices.branchcode |
725 |
FROM clubsAndServices, clubsAndServicesArchetypes |
726 |
WHERE |
727 |
clubsAndServices.casaId = clubsAndServicesArchetypes.casaId |
728 |
AND clubsAndServicesArchetypes.type = ? |
729 |
ORDER BY $orderby |
730 |
" |
731 |
); |
732 |
$sth->execute($type) or return 0; |
733 |
|
734 |
} elsif ($branchcode) { |
735 |
$sth = $dbh->prepare( |
736 |
"SELECT clubsAndServices.casId, |
737 |
clubsAndServices.casaId, |
738 |
clubsAndServices.title, |
739 |
clubsAndServices.description, |
740 |
clubsAndServices.casData1, |
741 |
clubsAndServices.casData2, |
742 |
clubsAndServices.casData3, |
743 |
clubsAndServices.startDate, |
744 |
clubsAndServices.endDate, |
745 |
clubsAndServices.last_updated, |
746 |
clubsAndServices.branchcode |
747 |
FROM clubsAndServices, clubsAndServicesArchetypes |
748 |
WHERE |
749 |
clubsAndServices.casaId = clubsAndServicesArchetypes.casaId |
750 |
AND clubsAndServices.branchcode = ? |
751 |
ORDER BY $orderby |
752 |
" |
753 |
); |
754 |
$sth->execute($branchcode) or return 0; |
755 |
|
756 |
} else { ## Get all clubs and services |
757 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServices ORDER BY $orderby"); |
758 |
$sth->execute() or return 0; |
759 |
} |
760 |
|
761 |
while ( my $row = $sth->fetchrow_hashref ) { |
762 |
push( @results, $row ); |
763 |
} |
764 |
|
765 |
$sth->finish; |
766 |
|
767 |
return \@results; |
768 |
|
769 |
} |
770 |
|
771 |
=head2 GetClubOrService |
772 |
|
773 |
Returns information about a club or service |
774 |
|
775 |
Input: |
776 |
$casId: Id of club or service to get |
777 |
|
778 |
Output: $casId, $casaId, $title, $description, $casData1, $casData2, $casData3, $startDate, $endDate, $last_updated, $branchcode |
779 |
|
780 |
=cut |
781 |
|
782 |
sub GetClubOrService { |
783 |
my ($casId) = @_; |
784 |
|
785 |
my $dbh = C4::Context->dbh; |
786 |
|
787 |
my ( $sth, @results ); |
788 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServices WHERE casId = ?"); |
789 |
$sth->execute($casId) or return 0; |
790 |
|
791 |
my $row = $sth->fetchrow_hashref; |
792 |
|
793 |
$sth->finish; |
794 |
|
795 |
return ( |
796 |
$$row{'casId'}, $$row{'casaId'}, $$row{'title'}, $$row{'description'}, $$row{'casData1'}, $$row{'casData2'}, |
797 |
$$row{'casData3'}, $$row{'startDate'}, $$row{'endDate'}, $$row{'last_updated'}, $$row{'branchcode'} |
798 |
); |
799 |
|
800 |
} |
801 |
|
802 |
=head2 GetClubsAndServicesArchetypes |
803 |
|
804 |
Returns information about clubs and services archetypes |
805 |
|
806 |
Input: |
807 |
$type: 'club' or 'service' ( Optional: Defaults to all types ) |
808 |
$branchcode: Get clubs or services created by this branch ( Optional ) |
809 |
|
810 |
Output: |
811 |
$results: |
812 |
Otherwise: Reference to an array of associated arrays |
813 |
Except: 0 on failure |
814 |
|
815 |
=cut |
816 |
|
817 |
sub GetClubsAndServicesArchetypes { |
818 |
my ( $type, $branchcode ) = @_; |
819 |
my $dbh = C4::Context->dbh; |
820 |
|
821 |
my $sth; |
822 |
if ( $type && $branchcode ) { |
823 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServicesArchetypes WHERE type = ? AND branchcode = ?"); |
824 |
$sth->execute( $type, $branchcode ) or return 0; |
825 |
} elsif ($type) { |
826 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServicesArchetypes WHERE type = ?"); |
827 |
$sth->execute($type) or return 0; |
828 |
} elsif ($branchcode) { |
829 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServicesArchetypes WHERE branchcode = ?"); |
830 |
$sth->execute($branchcode) or return 0; |
831 |
} else { |
832 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServicesArchetypes"); |
833 |
$sth->execute() or return 0; |
834 |
} |
835 |
|
836 |
my @results; |
837 |
while ( my $row = $sth->fetchrow_hashref ) { |
838 |
push( @results, $row ); |
839 |
} |
840 |
|
841 |
$sth->finish; |
842 |
|
843 |
return \@results; |
844 |
} |
845 |
|
846 |
=head2 GetClubOrServiceArchetype |
847 |
|
848 |
Returns information about a club or services archetype |
849 |
|
850 |
Input: |
851 |
$casaId: Id of Archetype to get |
852 |
$asHashref: Optional, if true, will return hashref instead of array |
853 |
|
854 |
Output: |
855 |
( $casaId, $type, $title, $description, $publicEnrollment, |
856 |
$casData1Title, $casData2Title, $casData3Title, |
857 |
$caseData1Title, $caseData2Title, $caseData3Title, |
858 |
$casData1Desc, $casData2Desc, $casData3Desc, |
859 |
$caseData1Desc, $caseData2Desc, $caseData3Desc, |
860 |
$caseRequireEmail, $last_updated, $branchcode ) |
861 |
Except: 0 on failure |
862 |
|
863 |
=cut |
864 |
|
865 |
sub GetClubOrServiceArchetype { |
866 |
my ( $casaId, $asHashref ) = @_; |
867 |
|
868 |
my $dbh = C4::Context->dbh; |
869 |
|
870 |
my $sth; |
871 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServicesArchetypes WHERE casaId = ?"); |
872 |
$sth->execute($casaId) or return 0; |
873 |
|
874 |
my $row = $sth->fetchrow_hashref; |
875 |
|
876 |
$sth->finish; |
877 |
|
878 |
if ($asHashref) { return $row; } |
879 |
|
880 |
return ( |
881 |
$$row{'casaId'}, $$row{'type'}, $$row{'title'}, $$row{'description'}, $$row{'publicEnrollment'}, |
882 |
$$row{'casData1Title'}, $$row{'casData2Title'}, $$row{'casData3Title'}, $$row{'caseData1Title'}, $$row{'caseData2Title'}, |
883 |
$$row{'caseData3Title'}, $$row{'casData1Desc'}, $$row{'casData2Desc'}, $$row{'casData3Desc'}, $$row{'caseData1Desc'}, |
884 |
$$row{'caseData2Desc'}, $$row{'caseData3Desc'}, $$row{'caseRequireEmail'}, $$row{'last_updated'}, $$row{'branchcode'} |
885 |
); |
886 |
} |
887 |
|
888 |
=head2 DoesEnrollmentRequireData |
889 |
|
890 |
Returns 1 if the given Archetype has |
891 |
data fields that need to be filled in |
892 |
at the time of enrollment. |
893 |
|
894 |
Input: |
895 |
$casaId: Id of Archetype to get |
896 |
|
897 |
Output: |
898 |
1: Enrollment will require extra data |
899 |
0: Enrollment will not require extra data |
900 |
|
901 |
=cut |
902 |
|
903 |
sub DoesEnrollmentRequireData { |
904 |
my ($casaId) = @_; |
905 |
|
906 |
my $dbh = C4::Context->dbh; |
907 |
|
908 |
my $sth; |
909 |
$sth = $dbh->prepare("SELECT caseData1Title FROM clubsAndServicesArchetypes WHERE casaId = ?"); |
910 |
$sth->execute($casaId) or return 0; |
911 |
|
912 |
my $row = $sth->fetchrow_hashref; |
913 |
|
914 |
$sth->finish; |
915 |
|
916 |
if ( $$row{'caseData1Title'} ) { |
917 |
return 1; |
918 |
} else { |
919 |
return 0; |
920 |
} |
921 |
} |
922 |
|
923 |
=head2 CancelClubOrServiceEnrollment |
924 |
|
925 |
Cancels the given enrollment in a club or service |
926 |
|
927 |
Input: |
928 |
$caseId: The id of the enrollment to be canceled |
929 |
|
930 |
Output: |
931 |
$success: 1 on successful cancelation, 0 otherwise |
932 |
|
933 |
=cut |
934 |
|
935 |
sub CancelClubOrServiceEnrollment { |
936 |
my ($caseId) = @_; |
937 |
|
938 |
my $success = 1; |
939 |
|
940 |
my $dbh = C4::Context->dbh; |
941 |
|
942 |
my $sth = $dbh->prepare("UPDATE clubsAndServicesEnrollments SET dateCanceled = CURDATE(), last_updated = NOW() WHERE caseId = ?"); |
943 |
$sth->execute($caseId) or $success = 0; |
944 |
$sth->finish; |
945 |
|
946 |
return $success; |
947 |
} |
948 |
|
949 |
=head2 GetEnrolledClubsAndServices |
950 |
|
951 |
Returns information about clubs and services |
952 |
the given borrower is enrolled in. |
953 |
|
954 |
Input: |
955 |
$borrowernumber |
956 |
|
957 |
Output: |
958 |
$results: Reference to an array of associated arrays |
959 |
|
960 |
=cut |
961 |
|
962 |
sub GetEnrolledClubsAndServices { |
963 |
my ($borrowernumber) = @_; |
964 |
my $dbh = C4::Context->dbh; |
965 |
|
966 |
my ( $sth, @results ); |
967 |
$sth = $dbh->prepare( |
968 |
"SELECT |
969 |
clubsAndServicesEnrollments.caseId, |
970 |
clubsAndServices.casId, |
971 |
clubsAndServices.casaId, |
972 |
clubsAndServices.title, |
973 |
clubsAndServices.description, |
974 |
clubsAndServices.branchcode, |
975 |
clubsAndServicesArchetypes.type, |
976 |
clubsAndServicesArchetypes.publicEnrollment |
977 |
FROM clubsAndServices, clubsAndServicesArchetypes, clubsAndServicesEnrollments |
978 |
WHERE ( |
979 |
clubsAndServices.casaId = clubsAndServicesArchetypes.casaId |
980 |
AND clubsAndServices.casId = clubsAndServicesEnrollments.casId |
981 |
AND ( clubsAndServices.endDate >= CURRENT_DATE() OR clubsAndServices.endDate IS NULL ) |
982 |
AND clubsAndServicesEnrollments.dateCanceled IS NULL |
983 |
AND clubsAndServicesEnrollments.borrowernumber = ? |
984 |
) |
985 |
ORDER BY type, title |
986 |
" |
987 |
); |
988 |
$sth->execute($borrowernumber) or return 0; |
989 |
|
990 |
while ( my $row = $sth->fetchrow_hashref ) { |
991 |
push( @results, $row ); |
992 |
} |
993 |
|
994 |
$sth->finish; |
995 |
|
996 |
return \@results; |
997 |
|
998 |
} |
999 |
|
1000 |
=head2 GetPubliclyEnrollableClubsAndServices |
1001 |
|
1002 |
Returns information about clubs and services |
1003 |
the given borrower can enroll in. |
1004 |
|
1005 |
Input: |
1006 |
$borrowernumber |
1007 |
|
1008 |
Output: |
1009 |
$results: Reference to an array of associated arrays |
1010 |
|
1011 |
=cut |
1012 |
|
1013 |
sub GetPubliclyEnrollableClubsAndServices { |
1014 |
my ($borrowernumber) = @_; |
1015 |
|
1016 |
my $dbh = C4::Context->dbh; |
1017 |
|
1018 |
my ( $sth, @results ); |
1019 |
$sth = $dbh->prepare( " |
1020 |
SELECT |
1021 |
DISTINCT ( clubsAndServices.casId ), |
1022 |
clubsAndServices.title, |
1023 |
clubsAndServices.description, |
1024 |
clubsAndServices.branchcode, |
1025 |
clubsAndServicesArchetypes.type, |
1026 |
clubsAndServices.casaId |
1027 |
FROM clubsAndServices, clubsAndServicesArchetypes |
1028 |
WHERE clubsAndServicesArchetypes.casaId = clubsAndServices.casaId |
1029 |
AND clubsAndServicesArchetypes.publicEnrollment =1 |
1030 |
AND clubsAndServices.casId NOT |
1031 |
IN ( |
1032 |
SELECT clubsAndServices.casId |
1033 |
FROM clubsAndServices, clubsAndServicesEnrollments |
1034 |
WHERE clubsAndServicesEnrollments.casId = clubsAndServices.casId |
1035 |
AND clubsAndServicesEnrollments.dateCanceled IS NULL |
1036 |
AND clubsAndServicesEnrollments.borrowernumber = ? |
1037 |
) |
1038 |
ORDER BY type, title" ); |
1039 |
$sth->execute($borrowernumber) or return 0; |
1040 |
|
1041 |
while ( my $row = $sth->fetchrow_hashref ) { |
1042 |
push( @results, $row ); |
1043 |
} |
1044 |
|
1045 |
$sth->finish; |
1046 |
|
1047 |
return \@results; |
1048 |
|
1049 |
} |
1050 |
|
1051 |
=head2 GetAllEnrollableClubsAndServices |
1052 |
|
1053 |
Returns information about clubs and services |
1054 |
the given borrower can enroll in. |
1055 |
|
1056 |
Input: |
1057 |
$borrowernumber |
1058 |
|
1059 |
Output: |
1060 |
$results: Reference to an array of associated arrays |
1061 |
|
1062 |
=cut |
1063 |
|
1064 |
sub GetAllEnrollableClubsAndServices { |
1065 |
my ( $borrowernumber, $branchcode ) = @_; |
1066 |
|
1067 |
if ( $branchcode eq '' ) { |
1068 |
$branchcode = '%'; |
1069 |
} |
1070 |
|
1071 |
my $dbh = C4::Context->dbh; |
1072 |
|
1073 |
my ( $sth, @results ); |
1074 |
$sth = $dbh->prepare( " |
1075 |
SELECT |
1076 |
DISTINCT ( clubsAndServices.casId ), |
1077 |
clubsAndServices.title, |
1078 |
clubsAndServices.description, |
1079 |
clubsAndServices.branchcode, |
1080 |
clubsAndServicesArchetypes.type, |
1081 |
clubsAndServices.casaId |
1082 |
FROM clubsAndServices, clubsAndServicesArchetypes |
1083 |
WHERE clubsAndServicesArchetypes.casaId = clubsAndServices.casaId |
1084 |
AND ( |
1085 |
DATE(clubsAndServices.endDate) >= CURDATE() |
1086 |
OR |
1087 |
clubsAndServices.endDate IS NULL |
1088 |
) |
1089 |
AND clubsAndServices.branchcode LIKE ? |
1090 |
AND clubsAndServices.casId NOT |
1091 |
IN ( |
1092 |
SELECT clubsAndServices.casId |
1093 |
FROM clubsAndServices, clubsAndServicesEnrollments |
1094 |
WHERE clubsAndServicesEnrollments.casId = clubsAndServices.casId |
1095 |
AND clubsAndServicesEnrollments.dateCanceled IS NULL |
1096 |
AND clubsAndServicesEnrollments.borrowernumber = ? |
1097 |
) |
1098 |
ORDER BY type, title" ); |
1099 |
$sth->execute( $branchcode, $borrowernumber ) or return 0; |
1100 |
|
1101 |
while ( my $row = $sth->fetchrow_hashref ) { |
1102 |
push( @results, $row ); |
1103 |
} |
1104 |
|
1105 |
$sth->finish; |
1106 |
|
1107 |
return \@results; |
1108 |
|
1109 |
} |
1110 |
|
1111 |
sub getBorrowernumberByCardnumber { |
1112 |
my $dbh = C4::Context->dbh; |
1113 |
|
1114 |
my $sth = $dbh->prepare("SELECT borrowernumber FROM borrowers WHERE cardnumber = ?"); |
1115 |
$sth->execute(@_) or return (0); |
1116 |
|
1117 |
my $row = $sth->fetchrow_hashref; |
1118 |
|
1119 |
my $borrowernumber = $$row{'borrowernumber'}; |
1120 |
$sth->finish; |
1121 |
|
1122 |
return ($borrowernumber); |
1123 |
} |
1124 |
|
1125 |
sub isEnrolled { |
1126 |
my ( $casId, $borrowernumber ) = @_; |
1127 |
|
1128 |
my $dbh = C4::Context->dbh; |
1129 |
|
1130 |
my $sth = $dbh->prepare("SELECT COUNT(*) as isEnrolled FROM clubsAndServicesEnrollments WHERE casId = ? AND borrowernumber = ? AND dateCanceled IS NULL"); |
1131 |
$sth->execute( $casId, $borrowernumber ) or return (0); |
1132 |
|
1133 |
my $row = $sth->fetchrow_hashref; |
1134 |
|
1135 |
my $isEnrolled = $$row{'isEnrolled'}; |
1136 |
$sth->finish; |
1137 |
|
1138 |
return ($isEnrolled); |
1139 |
} |
1140 |
|
1141 |
sub getTodayMysqlDateFormat { |
1142 |
my ( $day, $month, $year ) = (localtime)[ 3, 4, 5 ]; |
1143 |
my $today = sprintf( "%04d-%02d-%02d", $year + 1900, $month + 1, $day ); |
1144 |
return $today; |
1145 |
} |
1146 |
|
1147 |
sub ReserveForBestSellersClub { |
1148 |
my ($biblionumber) = @_; |
1149 |
|
1150 |
unless ($biblionumber) { return; } |
1151 |
|
1152 |
my $dbh = C4::Context->dbh; |
1153 |
my $sth; |
1154 |
|
1155 |
## Grab the bib for this biblionumber, we will need the author and title to find the relevent clubs |
1156 |
my $biblio_data = C4::Biblio::GetBiblioData($biblionumber); |
1157 |
my $author = $biblio_data->{'author'}; |
1158 |
my $title = $biblio_data->{'title'}; |
1159 |
my $itemtype = $biblio_data->{'itemtype'}; |
1160 |
|
1161 |
## Find the casaId for the Bestsellers Club archetype |
1162 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServicesArchetypes WHERE code LIKE 'BESTSELLERS_CLUB' "); |
1163 |
$sth->execute(); |
1164 |
my $casa = $sth->fetchrow_hashref(); |
1165 |
my $casaId = $casa->{'casaId'}; |
1166 |
$sth->finish(); |
1167 |
|
1168 |
unless ($casaId) { return; } |
1169 |
|
1170 |
## Find all the relevent bestsellers clubs |
1171 |
## casData1 is title, casData2 is author |
1172 |
$sth = $dbh->prepare("SELECT * FROM clubsAndServices WHERE casaId = ?"); |
1173 |
$sth->execute($casaId); |
1174 |
my @clubs; |
1175 |
while ( my $club = $sth->fetchrow_hashref() ) { |
1176 |
|
1177 |
#warn "Author/casData2 : '$author'/ " . $club->{'casData2'} . "'"; |
1178 |
#warn "Title/casData1 : '$title'/" . $club->{'casData1'} . "'"; |
1179 |
|
1180 |
## If the author, title or both match, keep it. |
1181 |
if ( ( $club->{'casData1'} eq $title ) || ( $club->{'casData2'} eq $author ) ) { |
1182 |
push( @clubs, $club ); |
1183 |
|
1184 |
#warn "casId" . $club->{'casId'}; |
1185 |
} elsif ( $club->{'casData1'} =~ m/%/ ) { # Title is using % as a wildcard |
1186 |
my @substrings = split( /%/, $club->{'casData1'} ); |
1187 |
my $all_match = 1; |
1188 |
foreach my $sub (@substrings) { |
1189 |
unless ( $title =~ m/\Q$sub/ ) { |
1190 |
$all_match = 0; |
1191 |
} |
1192 |
} |
1193 |
if ($all_match) { push( @clubs, $club ); } |
1194 |
} elsif ( $club->{'casData2'} =~ m/%/ ) { # Author is using % as a wildcard |
1195 |
my @substrings = split( /%/, $club->{'casData2'} ); |
1196 |
my $all_match = 1; |
1197 |
foreach my $sub (@substrings) { |
1198 |
unless ( $author =~ m/\Q$sub/ ) { |
1199 |
$all_match = 0; |
1200 |
} |
1201 |
} |
1202 |
|
1203 |
## Make sure the bib is in the list of itemtypes to use |
1204 |
my @itemtypes = split( / /, $club->{'casData3'} ); |
1205 |
my $found_itemtype_match = 0; |
1206 |
if (@itemtypes) { ## If no itemtypes are listed, all itemtypes are valid, skip test. |
1207 |
foreach my $it (@itemtypes) { |
1208 |
if ( $it eq $itemtype ) { |
1209 |
$found_itemtype_match = 1; |
1210 |
last; ## Short circuit for speed. |
1211 |
} |
1212 |
} |
1213 |
$all_match = 0 unless ($found_itemtype_match); |
1214 |
} |
1215 |
|
1216 |
if ($all_match) { push( @clubs, $club ); } |
1217 |
} |
1218 |
} |
1219 |
$sth->finish(); |
1220 |
|
1221 |
unless ( scalar(@clubs) ) { return; } |
1222 |
|
1223 |
## Get all the members of the relevant clubs, but only get each borrower once, even if they are in multiple relevant clubs |
1224 |
## Randomize the order of the borrowers |
1225 |
my @casIds; |
1226 |
my $sql = "SELECT DISTINCT(borrowers.borrowernumber) FROM borrowers, clubsAndServicesEnrollments |
1227 |
WHERE clubsAndServicesEnrollments.borrowernumber = borrowers.borrowernumber |
1228 |
AND ("; |
1229 |
my $clubsCount = scalar(@clubs); |
1230 |
foreach my $club (@clubs) { |
1231 |
$sql .= " casId = ?"; |
1232 |
if ( --$clubsCount ) { |
1233 |
$sql .= " OR"; |
1234 |
} |
1235 |
push( @casIds, $club->{'casId'} ); |
1236 |
} |
1237 |
$sql .= " ) ORDER BY RAND()"; |
1238 |
|
1239 |
$sth = $dbh->prepare($sql); |
1240 |
$sth->execute(@casIds); |
1241 |
my @borrowers; |
1242 |
while ( my $borrower = $sth->fetchrow_hashref() ) { |
1243 |
push( @borrowers, $borrower ); |
1244 |
} |
1245 |
|
1246 |
unless ( scalar(@borrowers) ) { return; } |
1247 |
|
1248 |
my $priority = 1; |
1249 |
foreach my $borrower (@borrowers) { |
1250 |
C4::Reserves::AddReserve( |
1251 |
my $branch = $borrower->{'branchcode'}, |
1252 |
my $borrowernumber = $borrower->{'borrowernumber'}, |
1253 |
$biblionumber, my $constraint = 'a', |
1254 |
my $bibitems, $priority, my $notes = $casa->{'title'}, |
1255 |
$title, my $checkitem, |
1256 |
my $found, my $expire_date |
1257 |
); |
1258 |
$priority++; |
1259 |
} |
1260 |
} |
1261 |
|
1262 |
1; |
1263 |
|
1264 |
__END__ |
1265 |
|
1266 |
=back |
1267 |
|
1268 |
=head1 AUTHOR |
1269 |
|
1270 |
Kyle M Hall <kylemhall@gmail.com> |
1271 |
|
1272 |
=cut |