[system] / trunk / webwork2 / lib / WeBWorK / DB / Classlist.pm Repository:
ViewVC logotype

Diff of /trunk/webwork2/lib/WeBWorK/DB/Classlist.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 359 Revision 360
10use WeBWorK::User; 10use WeBWorK::User;
11 11
12# there should be a `use' line for each database type 12# there should be a `use' line for each database type
13use WeBWorK::DB::GDBM; 13use WeBWorK::DB::GDBM;
14 14
15# new($invocant, $courseEnv) 15# new($courseEnv)
16# $invocant implicitly set by caller
17# $courseEnv an instance of CourseEnvironment 16# $courseEnv - an instance of CourseEnvironment
18sub new($$) { 17sub new($$) {
19 my $invocant = shift; 18 my $invocant = shift;
20 my $class = ref($invocant) || $invocant; 19 my $class = ref($invocant) || $invocant;
21 my $courseEnv = shift; 20 my $courseEnv = shift;
22 my $dbModule = fullyQualifiedPackageName($courseEnv->{dbInfo}->{cldb_type}); 21 my $dbModule = fullyQualifiedPackageName($courseEnv->{dbInfo}->{cldb_type});
35 return $package; 34 return $package;
36} 35}
37 36
38# ----- 37# -----
39 38
39# getUsers() - returns a list of user IDs present in the database
40sub getUsers($) { 40sub getUsers($) {
41 my $self = shift; 41 my $self = shift;
42 return unless $self->{classlist_db}->connect("ro"); 42 return unless $self->{classlist_db}->connect("ro");
43 my @result = keys %{$self->{classlist_db}->hashRef}; 43 my @result = keys %{$self->{classlist_db}->hashRef};
44 $self->{classlist_db}->disconnect; 44 $self->{classlist_db}->disconnect;
46 return @result; 46 return @result;
47} 47}
48 48
49# ----- 49# -----
50 50
51# getUser($userID) - returns a WeBWorK::User object if $userID exists
52# or an undefined value if not.
53# $userID - the ID of the user requested
51sub getUser($$) { 54sub getUser($$) {
52 my $self = shift; 55 my $self = shift;
53 my $userID = shift; 56 my $userID = shift;
54 if ($userID =~ /^>>/) { 57 if ($userID =~ /^>>/) {
55 warn "Attempt to use the special key $userID as a user!"; 58 warn "Attempt to use the special key $userID as a user!";
60 $self->{classlist_db}->disconnect; 63 $self->{classlist_db}->disconnect;
61 return unless defined $result; 64 return unless defined $result;
62 return hash2user($userID, decode($result)); 65 return hash2user($userID, decode($result));
63} 66}
64 67
68# setUser($user) - if a user with the same user ID as $user exists, that user
69# is replaced. if not, a new user is added. A true value is
70# returned in success, an undefined value on failure.
71# $user - an instance of WeBWorK::User containing user data
65sub setUser($$) { 72sub setUser($$) {
66 my $self = shift; 73 my $self = shift;
67 my $user = shift; 74 my $user = shift;
68 if ($user->id =~ /^>>/) { 75 if ($user->id =~ /^>>/) {
69 warn "Attempt to use the special key \"", $user->id, "\" as a user ID!"; 76 warn "Attempt to use the special key \"", $user->id, "\" as a user ID!";
70 return; 77 return;
71 } 78 }
72 die "Can't add/modify user ", $user->id, ": classlist database locked" if $self->locked; 79 die "Can't add/modify user ", $user->id, ": classlist database locked" if $self->locked;
73 $self->{classlist_db}->connect("rw"); 80 return unless $self->{classlist_db}->connect("rw");
74 $self->{classlist_db}->hashRef->{$user->id} = encode(user2hash($user)); 81 $self->{classlist_db}->hashRef->{$user->id} = encode(user2hash($user));
75 $self->{classlist_db}->disconnect; 82 $self->{classlist_db}->disconnect;
83 return 1;
76} 84}
77 85
86# deleteUser($userID) - removed a user with the specified user ID. Returns
87# a true value on success, an undefined one on failure.
88# $userID - the ID of the user to delete
78sub deleteUser($$) { 89sub deleteUser($$) {
79 my $self = shift; 90 my $self = shift;
80 my $userID = shift; 91 my $userID = shift;
81 if ($userID =~ /^>>/) { 92 if ($userID =~ /^>>/) {
82 warn "Attempt to use the special key \"$userID\" as a user ID!"; 93 warn "Attempt to use the special key \"$userID\" as a user ID!";
84 } 95 }
85 die "Can't delete user $userID: classlist database locked" if $self->locked; 96 die "Can't delete user $userID: classlist database locked" if $self->locked;
86 return unless $self->{classlist_db}->connect("rw"); 97 return unless $self->{classlist_db}->connect("rw");
87 delete $self->{classlist_db}->hashRef->{$userID}; 98 delete $self->{classlist_db}->hashRef->{$userID};
88 $self->{classlist_db}->disconnect; 99 $self->{classlist_db}->disconnect;
100 return 1;
89} 101}
90 102
91# ----- 103# -----
92 104
105# lock() - locks the database associated with this classlist object. when
106# a database is locked, it cannot be modified except to unlock it.
93sub lock($) { 107sub lock($) {
94 my $self = shift; 108 my $self = shift;
95 return unless $self->{classlist_db}->connect("rw"); 109 return unless $self->{classlist_db}->connect("rw");
96 $self->{classlist_db}->hashRef->{">>lock_status"} = "locked"; 110 $self->{classlist_db}->hashRef->{">>lock_status"} = "locked";
97 $self->{classlist_db}->disconnect; 111 $self->{classlist_db}->disconnect;
112 return 1;
98} 113}
99 114
115# unlock() - unlocks the database associated with this classlist object.
100sub unlock($) { 116sub unlock($) {
101 my $self = shift; 117 my $self = shift;
102 return unless $self->{classlist_db}->connect("rw"); 118 return unless $self->{classlist_db}->connect("rw");
103 # the old code sets this to "unlocked", but I'm going to remove it. 119 # the old code sets this to "unlocked", but I going to delete it instead
104 delete $self->{classlist_db}->hashRef->{">>lock_status"}; 120 delete $self->{classlist_db}->hashRef->{">>lock_status"};
105 $self->{classlist_db}->disconnect; 121 $self->{classlist_db}->disconnect;
122 return 1;
106} 123}
107 124
125# locked() - returns true if the database is locked, false if it is not.
108sub locked($) { 126sub locked($) {
109 my $self = shift; 127 my $self = shift;
110 return unless $self->{classlist_db}->connect("ro"); 128 return unless $self->{classlist_db}->connect("ro");
111 my $result = $self->{classlist_db}->hashRef->{">>lock_status"}; 129 my $result = $self->{classlist_db}->hashRef->{">>lock_status"};
112 $self->{classlist_db}->disconnect; 130 $self->{classlist_db}->disconnect;

Legend:
Removed from v.359  
changed lines
  Added in v.360

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9