From: jj v. a. <we...@ma...> - 2005-07-26 17:34:22
|
Log Message: ----------- Lots of indentation fixup, and started adding code for new and improved problem library. Still compatible with current "Problem Library". Modified Files: -------------- webwork-modperl/lib/WeBWorK/Utils: ListingDB.pm Revision Data ------------- Index: ListingDB.pm =================================================================== RCS file: /webwork/cvs/system/webwork-modperl/lib/WeBWorK/Utils/ListingDB.pm,v retrieving revision 1.3 retrieving revision 1.4 diff -Llib/WeBWorK/Utils/ListingDB.pm -Llib/WeBWorK/Utils/ListingDB.pm -u -r1.3 -r1.4 --- lib/WeBWorK/Utils/ListingDB.pm +++ lib/WeBWorK/Utils/ListingDB.pm @@ -20,59 +20,177 @@ BEGIN { - require Exporter; - use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); - - $VERSION =1.0; - @ISA =qw(Exporter); - @EXPORT =qw( + require Exporter; + use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); + + $VERSION =1.0; + @ISA =qw(Exporter); + @EXPORT =qw( &createListing &updateListing &deleteListing &getAllChapters &getAllSections - &searchListings &getAllListings &getSectionListings + &searchListings &getAllListings &getSectionListings + &getAllDBchapters &getAllDBsections &getDBsectionListings ); - %EXPORT_TAGS =(); - @EXPORT_OK =qw(); + %EXPORT_TAGS =(); + @EXPORT_OK =qw(); } use vars @EXPORT_OK; sub getDB { - my $ce = shift; - my $dbinfo = $ce->{problemLibrary}; - my $dbh = DBI->connect_cached("dbi:mysql:$dbinfo->{sourceSQL}", - $dbinfo->{userSQL}, $dbinfo->{passwordSQL}); - die "Cannot connect to problem library database" unless $dbh; - return($dbh); + my $ce = shift; + my $dbinfo = $ce->{problemLibrary}; + my $dbh = DBI->connect_cached("dbi:mysql:$dbinfo->{sourceSQL}", + $dbinfo->{userSQL}, $dbinfo->{passwordSQL}); + die "Cannot connect to problem library database" unless $dbh; + return($dbh); +} + +=item getAllDBchapters($ce) +Returns an array of DBchapter names + +$ce is a WeBWorK::CourseEnvironment object that describes the problem library. + +=cut + +sub getAllDBchapters { + my $ce = shift; + my @results=(); + my ($row,$listing); + my $query = "SELECT DISTINCT name FROM DBchapter"; + my $dbh = getDB($ce); + my $sth = $dbh->prepare($query); + $sth->execute(); + while (1) + { + $row = $sth->fetchrow_array; + if (!defined($row)) + { + last; + } + else + { + my $listing = $row; + push @results, $listing; + } + } + return @results; +} + +=item getAllDBsections($ce,$chapter) +Returns an array of DBsection names + +$ce is a WeBWorK::CourseEnvironment object that describes the problem library. +$chapter is an DBchapter name + +=cut + +sub getAllDBsections { + my $ce = shift; + my $chapter = shift; + # $chapter = '"'.$chapter.'"'; # \'$chapter\' or \"$chapter\" does not work in $query anymore! wth? + my @results=(); + my ($row,$listing); + my $dbh = getDB($ce); + my $query = "SELECT DBchapter_id FROM DBchapter + WHERE name = \"$chapter\" "; + my $chapter_id = $dbh->selectrow_array($query); + die "ERROR - no such chapter: $chapter\n" unless(defined $chapter_id); + $query = "SELECT DISTINCT name FROM DBsection + WHERE DBchapter_id = $chapter_id"; + my $sth = $dbh->prepare($query); + $sth->execute(); + while (1) + { + $row = $sth->fetchrow_array; + last if (!defined($row)); + my $listing = $row; + push @results, $listing; + } + return @results; +} + +=item getDBSectionListings($ce, $chapter, $section) +Returns an array of hash references with the keys: path, filename. + +$ce is a WeBWorK::CourseEnvironment object that describes the problem library. +$chapter is an DBchapter name +$section is a DBsection name + +=cut + +sub getDBsectionListings { + + my $ce = shift; + my $chap = shift; + my $sec = shift; + + my $dbh = getDB($ce); + + my $chapstring = ''; + if($chap) { + $chap =~ s/'/\\'/g; + $chap = '"'.$chap.'"'; + } + my $secstring = ''; + if($sec) { + $sec =~ s/'/\\'/g; + $sec = '"'.$sec.'"'; + } + + my $query = "SELECT DBsection_id + FROM DBsection s, DBchapter c + WHERE c.name = $chap AND s.name = $sec"; + my $section_id = $dbh->selectrow_array($query); + die "getDBSectionListings - no such section: $chap $sec\n" unless(defined $section_id); + + my @results; #returned + $query = "SELECT path_id, filename + FROM pgfile + WHERE DBsection_id = $section_id"; + my $sth = $dbh->prepare($query); + + $sth->execute(); + while (1){ + my ($path_id, $pgfile) = $sth->fetchrow_array(); + if (!defined($pgfile)){ + last; + }else{ + my $path = $dbh->selectrow_array("SELECT path FROM path + WHERE path_id = $path_id"); + push @results, {"path" => $path, "filename" => $pgfile}; + } + } + return @results; } ############################################################################## # input expected: keywords,<keywords>,chapter,<chapter>,section,<section>,path,<path>,filename,<filename>,author,<author>,instituition,<instituition>,history,<history> -sub createListing - { - my $ce = shift; - my %listing_data = @_; - my $classify_id; - my $dbh = getDB($ce); - # my $dbh = WeBWorK::ProblemLibrary::DB::getDB(); - my $query = "INSERT INTO classify +sub createListing { + my $ce = shift; + my %listing_data = @_; + my $classify_id; + my $dbh = getDB($ce); + # my $dbh = WeBWorK::ProblemLibrary::DB::getDB(); + my $query = "INSERT INTO classify (filename,chapter,section,keywords) VALUES ($listing_data{filename},$listing_data{chapter},$listing_data{section},$listing_data{keywords})"; - $dbh->do($query); #TODO: watch out for comma delimited keywords, sections, chapters! + $dbh->do($query); #TODO: watch out for comma delimited keywords, sections, chapters! - $query = "SELECT id FROM classify WHERE filename = $listing_data{filename}"; - my $sth = $dbh->prepare($query); - $sth->execute(); - if ($sth->rows()) - { - ($classify_id) = $sth->fetchrow_array; - } - else - { - #print STDERR "ListingDB::createListingPGfiles: $listing_data{filename} failed insert into classify table"; - return 0; - }; + $query = "SELECT id FROM classify WHERE filename = $listing_data{filename}"; + my $sth = $dbh->prepare($query); + $sth->execute(); + if ($sth->rows()) + { + ($classify_id) = $sth->fetchrow_array; + } + else + { + #print STDERR "ListingDB::createListingPGfiles: $listing_data{filename} failed insert into classify table"; + return 0; + }; - $query = "INSERT INTO pgfiles + $query = "INSERT INTO pgfiles ( classify_id, path, @@ -88,186 +206,175 @@ $listing_data{institution}, $listing_data{history} )"; - - $dbh->do($query); - return 1; - } + + $dbh->do($query); + return 1; +} ############################################################################## # input expected any pair of: keywords,<keywords data>,chapter,<chapter data>,section,<section data>,filename,<filename data>,author,<author data>,instituition,<instituition data> # returns an array of hash references -sub searchListings -{ - my $ce = shift; - my %searchterms = @_; - #print STDERR "ListingDB::searchListings input array @_\n"; - my @results; - my ($row,$key); - my $dbh = getDB($ce); - my $query = "SELECT c.filename, p.path - FROM classify c, pgfiles p - WHERE c.id = p.classify_id"; - foreach $key (keys %searchterms) { - $query .= " AND c.$key = $searchterms{$key}"; - }; - my $sth = $dbh->prepare($query); - $sth->execute(); - if ($sth->rows()) - { - while (1) - { - $row = $sth->fetchrow_hashref(); - if (!defined($row)) - { - last; - } - else - { - #print STDERR "ListingDB::searchListings(): found $row->{id}\n"; - my $listing = $row; - push @results, $listing; - } - } - } - return @results; +sub searchListings { + my $ce = shift; + my %searchterms = @_; + #print STDERR "ListingDB::searchListings input array @_\n"; + my @results; + my ($row,$key); + my $dbh = getDB($ce); + my $query = "SELECT c.filename, p.path + FROM classify c, pgfiles p + WHERE c.id = p.classify_id"; + foreach $key (keys %searchterms) { + $query .= " AND c.$key = $searchterms{$key}"; + }; + my $sth = $dbh->prepare($query); + $sth->execute(); + if ($sth->rows()) + { + while (1) + { + $row = $sth->fetchrow_hashref(); + if (!defined($row)) + { + last; + } + else + { + #print STDERR "ListingDB::searchListings(): found $row->{id}\n"; + my $listing = $row; + push @results, $listing; + } + } + } + return @results; } ############################################################################## # returns a list of chapters -sub getAllChapters - { - #print STDERR "ListingDB::getAllChapters\n"; - my $ce = shift; - my @results=(); - my ($row,$listing); - my $query = "SELECT DISTINCT chapter FROM classify"; - my $dbh = getDB($ce); - my $sth = $dbh->prepare($query); - $sth->execute(); - while (1) - { - $row = $sth->fetchrow_array; - if (!defined($row)) - { - last; - } - else - { - my $listing = $row; - push @results, $listing; - #print STDERR "ListingDB::getAllChapters $listing\n"; - } - } - return @results; - } +sub getAllChapters { + #print STDERR "ListingDB::getAllChapters\n"; + my $ce = shift; + my @results=(); + my ($row,$listing); + my $query = "SELECT DISTINCT chapter FROM classify"; + my $dbh = getDB($ce); + my $sth = $dbh->prepare($query); + $sth->execute(); + while (1) + { + $row = $sth->fetchrow_array; + if (!defined($row)) + { + last; + } + else + { + my $listing = $row; + push @results, $listing; + #print STDERR "ListingDB::getAllChapters $listing\n"; + } + } + return @results; +} ############################################################################## # input chapter # returns a list of sections -sub getAllSections - { - #print STDERR "ListingDB::getAllSections\n"; - my $ce = shift; - my $chapter = shift; - my @results=(); - my ($row,$listing); - my $query = "SELECT DISTINCT section FROM classify +sub getAllSections { + #print STDERR "ListingDB::getAllSections\n"; + my $ce = shift; + my $chapter = shift; + my @results=(); + my ($row,$listing); + my $query = "SELECT DISTINCT section FROM classify WHERE chapter = \'$chapter\'"; - my $dbh = getDB($ce); - my $sth = $dbh->prepare($query); - $sth->execute(); - while (1) - { - $row = $sth->fetchrow_array; - if (!defined($row)) - { - last; - } - else - { - my $listing = $row; - push @results, $listing; - #print STDERR "ListingDB::getAllSections $listing\n"; - } - } - return @results; - } + my $dbh = getDB($ce); + my $sth = $dbh->prepare($query); + $sth->execute(); + while (1) + { + $row = $sth->fetchrow_array; + if (!defined($row)) + { + last; + } + else + { + my $listing = $row; + push @results, $listing; + #print STDERR "ListingDB::getAllSections $listing\n"; + } + } + return @results; +} ############################################################################## # returns an array of hash references -sub getAllListings -{ - #print STDERR "ListingDB::getAllListings\n"; - my $ce = shift; - my @results; - my ($row,$key); - my $dbh = getDB($ce); - my $query = "SELECT c.*, p.path - FROM classify c, pgfiles p - WHERE c.pgfiles_id = p.pgfiles_id"; - my $sth = $dbh->prepare($query); - $sth->execute(); - while (1) - { - $row = $sth->fetchrow_hashref(); - if (!defined($row)) - { - last; - } - else - { - my $listing = $row; - push @results, $listing; - #print STDERR "ListingDB::getAllListings $listing\n"; - } - } - return @results; +sub getAllListings { + #print STDERR "ListingDB::getAllListings\n"; + my $ce = shift; + my @results; + my ($row,$key); + my $dbh = getDB($ce); + my $query = "SELECT c.*, p.path + FROM classify c, pgfiles p + WHERE c.pgfiles_id = p.pgfiles_id"; + my $sth = $dbh->prepare($query); + $sth->execute(); + while (1) + { + $row = $sth->fetchrow_hashref(); + last if (!defined($row)); + my $listing = $row; + push @results, $listing; + #print STDERR "ListingDB::getAllListings $listing\n"; + } + return @results; } ############################################################################## # input chapter, section # returns an array of hash references. # if section is omitted, get all from the chapter -sub getSectionListings - { - #print STDERR "ListingDB::getSectionListings(chapter,section)\n"; - - my $ce = shift; - my $chap = shift; - my $sec = shift; - - - my $chapstring = ''; - if($chap) { - $chap =~ s/'/\\'/g; - $chapstring = " c.chapter = \'$chap\' AND "; - } - my $secstring = ''; - if($sec) { - $sec =~ s/'/\\'/g; - $secstring = " c.section = \'$sec\' AND "; - } +sub getSectionListings { + #print STDERR "ListingDB::getSectionListings(chapter,section)\n"; - my @results; #returned - my $query = "SELECT c.*, p.path + my $ce = shift; + my $chap = shift; + my $sec = shift; + + + my $chapstring = ''; + if($chap) { + $chap =~ s/'/\\'/g; + $chapstring = " c.chapter = \'$chap\' AND "; + } + my $secstring = ''; + if($sec) { + $sec =~ s/'/\\'/g; + $secstring = " c.section = \'$sec\' AND "; + } + + my @results; #returned + my $query = "SELECT c.*, p.path FROM classify c, pgfiles p WHERE $chapstring $secstring c.pgfiles_id = p.pgfiles_id"; - my $dbh = getDB($ce); - my $sth = $dbh->prepare($query); - - $sth->execute(); - while (1) - { - my $row = $sth->fetchrow_hashref(); - if (!defined($row)) - { - last; - } - else - { - push @results, $row; - #print STDERR "ListingDB::getSectionListings $row\n"; - } - } - return @results; - } + my $dbh = getDB($ce); + my $sth = $dbh->prepare($query); + + $sth->execute(); + while (1) + { + my $row = $sth->fetchrow_hashref(); + if (!defined($row)) + { + last; + } + else + { + push @results, $row; + #print STDERR "ListingDB::getSectionListings $row\n"; + } + } + return @results; +} ############################################################################### # INPUT: @@ -276,16 +383,14 @@ # 1 = all ok # # not implemented yet -sub deleteListing -{ - my $ce = shift; - my $listing_id = shift; - #print STDERR "ListingDB::deleteListing(): listing == '$listing_id'\n"; - - my $dbh = getDB($ce); +sub deleteListing { + my $ce = shift; + my $listing_id = shift; + #print STDERR "ListingDB::deleteListing(): listing == '$listing_id'\n"; - return undef; + my $dbh = getDB($ce); + return undef; } ############################################################################## |