]> git.imager.perl.org - bse.git/blob - site/cgi-bin/modules/BSE/TB/SeminarSession.pm
add support for storing custom metadata to cart/order items and options
[bse.git] / site / cgi-bin / modules / BSE / TB / SeminarSession.pm
1 package BSE::TB::SeminarSession;
2 use strict;
3 use base qw(Squirrel::Row);
4 use BSE::Util::SQL qw(now_sqldatetime);
5
6 our $VERSION = "1.001";
7
8 sub columns {
9   return qw/id seminar_id location_id when_at roll_taken/;
10 }
11
12 sub booked_users {
13   my ($self) = @_;
14
15   require BSE::TB::SiteUsers;
16   return BSE::TB::SiteUsers->getSpecial(sessionBookings => $self->{id});
17 }
18
19 # perhaps this should allow removing old sessions with no bookings
20 sub is_removable {
21   my ($self) = @_;
22
23   return $self->{when_at} gt now_sqldatetime();
24 }
25
26 sub seminar {
27   my ($self) = @_;
28
29   require BSE::TB::Seminars;
30
31   return BSE::TB::Seminars->getByPkey($self->{seminar_id});
32 }
33
34 sub location {
35   my ($self) = @_;
36   
37   require BSE::TB::Locations;
38   return BSE::TB::Locations->getByPkey($self->{location_id});
39 }
40
41 sub replace_with {
42   my ($self, $other) = @_;
43
44   # ideally we could just update the column, but that has 2 problems:
45   #  - the user might be booked in both the original and new session
46   #  - this would be changing the primary key of a record, which is bad
47   my %conflicts = map { $_->{id} => 1 }
48     BSE::DB->query(conflictSeminarSessions => $self->{id}, $other->{id});
49   my @users_booked = map $_->{siteuser_id},
50     BSE::DB->query(seminarSessionBookedIds => $self->{id});
51   for my $userid (@users_booked) {
52     unless ($conflicts{$userid}) {
53       BSE::DB->run(seminarSessionBookUser => $other->{id}, $userid, 0);
54     }
55   }
56   BSE::DB->run(cancelSeminarSessionBookings => $self->{id});
57   
58   $self->remove;
59 }
60
61 sub cancel {
62   my ($self) = @_;
63
64   BSE::DB->run(cancelSeminarSessionBookings => $self->{id});
65   $self->remove;
66 }
67
68 sub roll_call_entries {
69   my ($self) = @_;
70
71   BSE::DB->query(seminarSessionRollCallEntries => $self->{id});
72 }
73
74 sub set_roll_present {
75   my ($self, $userid, $present) = @_;
76
77   $present = $present ? 1 : 0;
78
79   BSE::DB->run(updateSessionRollPresent => $present, $self->{id}, $userid);
80 }
81
82 my @attendee_attributes = 
83   qw/roll_present options customer_instructions support_notes/;
84 my %attendee_defaults =
85   (
86    roll_present => 0,
87    options => '',
88    customer_instructions => '',
89    support_notes => '',
90   );
91
92 sub add_attendee {
93   my ($self, $user, %attr) = @_;
94
95   my %work_attr = %attendee_defaults;
96   for my $key (keys %attr) {
97     exists $work_attr{$key} or 
98       Carp::confess("Unknown attendee attribute '$key'");
99     $work_attr{$key} = $attr{$key};
100   }
101
102   my $user_id = ref $user ? $user->{id} : $user;
103
104   require BSE::TB::SeminarBookings;
105   BSE::TB::SeminarBookings->add($self->{id}, $user_id, 
106                @work_attr{@attendee_attributes});
107 }
108
109 sub get_booking {
110   my ($self, $user) = @_;
111
112   my $siteuser_id = ref $user ? $user->{id} : $user;
113
114   require BSE::TB::SeminarBookings;
115   my @result = BSE::TB::SeminarBookings->
116     getBy(session_id => $self->{id}, siteuser_id => $siteuser_id);
117   @result or return;
118
119   return $result[0];
120 }
121
122 sub remove_booking {
123   my ($self, $user) = @_;
124
125   my $siteuser_id = ref $user ? $user->{id} : $user;
126
127   my $result = BSE::DB->run
128     (bse_cancelSessionBookingForUser => $self->{id}, $siteuser_id);
129   $result
130     or die "No such booking\n";
131 }
132
133 sub update_booking {
134   my ($self, $user, %attr) = @_;
135
136   my $have_all = !grep !exists$attr{$_}, @attendee_attributes;
137   unless ($have_all) {
138     my $old_booking = $self->get_booking($user)
139       or die "No such booking\n";
140     %attr = ( %$old_booking, %attr );
141   }
142   
143   my $siteuser_id = ref $user ? $user->{id} : $user;
144
145   BSE::DB->run(bse_updateSessionBookingForUser =>
146                @attr{@attendee_attributes},
147                $self->{id}, $siteuser_id) 
148       or die "No such booking\n";
149
150   return 1;
151 }
152
153 1;
154