From: jj v. a. <we...@ma...> - 2005-07-28 21:11:38
|
Log Message: ----------- Added advanced search panel for database library. Still more features to come. Modified Files: -------------- webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor: SetMaker.pm webwork-modperl/lib/WeBWorK/Utils: ListingDB.pm Revision Data ------------- Index: SetMaker.pm =================================================================== RCS file: /webwork/cvs/system/webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm,v retrieving revision 1.44 retrieving revision 1.45 diff -Llib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm -Llib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm -u -r1.44 -r1.45 --- lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm +++ lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm @@ -42,6 +42,10 @@ use constant MY_PROBLEMS => ' My Problems '; use constant MAIN_PROBLEMS => ' Main Problems '; use constant CREATE_SET_BUTTON => 'Create New Set'; +use constant ALL_CHAPTERS => 'All Chapters'; +use constant ALL_SUBJECTS => 'All Subjects'; +use constant ALL_SECTIONS => 'All Sections'; +use constant ALL_TEXTBOOKS => 'All Textbooks'; ## Flags for operations on files @@ -351,17 +355,16 @@ } ##### Version 3 is the problem library +# +# This comes in 3 forms, problem library version 1, and for version 2 there +# is the basic, and the advanced interfaces. This function checks what we are +# supposed to do, or aborts if the problem library has not been installed. - -# There a different levels, and you can pick a new chapter, -# pick a new section, pick all from chapter, pick all from section -# -# Incoming data - current chapter, current section sub browse_library_panel { my $self=shift; my $r = $self->r; my $ce = $r->ce; - + # See if the problem library is installed my $libraryRoot = $r->{ce}->{problemLibrary}->{root}; @@ -384,13 +387,13 @@ } } - # Now check what version we are supposed to use my $libraryVersion = $r->{ce}->{problemLibrary}->{version} || 1; if($libraryVersion == 1) { return $self->browse_library_panel1; } elsif($libraryVersion == 2) { - return $self->browse_library_panel2; + return $self->browse_library_panel2 if($self->{library_basic}==1); + return $self->browse_library_panel2adv; } else { print CGI::Tr(CGI::td(CGI::div({class=>'ResultsWithError', align=>"center"}, "The problem library version is set to an illegal value."))); @@ -403,22 +406,17 @@ my $r = $self->r; my $ce = $r->ce; - my $default_chap = "All Chapters"; - my $default_sect = "All Sections"; - my @chaps = WeBWorK::Utils::ListingDB::getAllChapters($r->{ce}); - unshift @chaps, $default_chap; - my $chapter_selected = $r->param('library_chapters') || $default_chap; + unshift @chaps, ALL_CHAPTERS; + my $chapter_selected = $r->param('library_chapters') || ALL_CHAPTERS; my @sects=(); - if ($chapter_selected ne $default_chap) { + if ($chapter_selected ne ALL_CHAPTERS) { @sects = WeBWorK::Utils::ListingDB::getAllSections($r->{ce}, $chapter_selected); } - my @textbooks = ('Textbook info not ready'); - - unshift @sects, $default_sect; - my $section_selected = $r->param('library_sections') || $default_sect; + unshift @sects, ALL_SECTIONS; + my $section_selected = $r->param('library_sections') || ALL_SECTIONS; my $view_problem_line = view_problems_line('lib_view', 'View Problems', $self->r); print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, @@ -439,21 +437,6 @@ -default=> $section_selected ))), - #CGI::Tr( - # CGI::td("Textbook:"), - # CGI::td({-colspan=>2}, - # CGI::popup_menu(-name=> 'library_textbooks', - # -values=>\@textbooks, - # #-default=> $section_selected - #))), - - #CGI::Tr( - # CGI::td("Keywords:"), - # CGI::td({-colspan=>2}, - # CGI::textfield(-name=>"keywords", - # -default=>"Keywords not implemented yet", - # -override=>1, -size=>60 - #))), CGI::Tr(CGI::td({-colspan=>3}, $view_problem_line)), CGI::end_table(), )); @@ -464,54 +447,146 @@ my $r = $self->r; my $ce = $r->ce; - my $default_subj = "All Subjects"; - my $default_chap = "All Chapters"; - my $default_sect = "All Sections"; - - my @subjs = WeBWorK::Utils::ListingDB::getAllDBsubjects($ce); - unshift @subjs, $default_subj; - my $subject_selected = $r->param('library_subjects') || $default_subj; - - my @chaps = WeBWorK::Utils::ListingDB::getAllDBchapters($ce, $subject_selected); - unshift @chaps, $default_chap; - my $chapter_selected = $r->param('library_chapters') || $default_chap; + my @subjs = WeBWorK::Utils::ListingDB::getAllDBsubjects($r); + unshift @subjs, ALL_SUBJECTS; + + my @chaps = WeBWorK::Utils::ListingDB::getAllDBchapters($r); + unshift @chaps, ALL_CHAPTERS; my @sects=(); - if ($chapter_selected ne $default_chap) { - @sects = WeBWorK::Utils::ListingDB::getAllDBsections($ce, $chapter_selected); - } - unshift @sects, $default_sect; - my $section_selected = $r->param('library_sections') || $default_sect; + @sects = WeBWorK::Utils::ListingDB::getAllDBsections($r); + unshift @sects, ALL_SECTIONS; + + my $subject_selected = $r->param('library_subjects') || ALL_SUBJECTS; + my $chapter_selected = $r->param('library_chapters') || ALL_CHAPTERS; + my $section_selected = $r->param('library_sections') || ALL_SECTIONS; + my $view_problem_line = view_problems_line('lib_view', 'View Problems', $self->r); print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, - CGI::start_table(), + CGI::hidden(-name=>"library_is_basic", -default=>[1]), + CGI::start_table({-width=>"100%"}), CGI::Tr( CGI::td(["Subject:", CGI::popup_menu(-name=> 'library_subjects', -values=>\@subjs, -default=> $subject_selected, -onchange=>"submit();return true" - ), - CGI::submit(-name=>"lib_select_subject", -value=>"Update Chapter/Section Lists")])), + )]), + CGI::td({-colspan=>2, -align=>"right"}, + CGI::submit(-name=>"lib_select_subject", -value=>"Update Chapter/Section Lists")) + ), CGI::Tr( CGI::td(["Chapter:", CGI::popup_menu(-name=> 'library_chapters', -values=>\@chaps, -default=> $chapter_selected, -onchange=>"submit();return true" - )])), + )]), + CGI::td({-colspan=>2, -align=>"right"}, + CGI::submit(-name=>"library_advanced", -value=>"Advanced Search")) + ), CGI::Tr( - CGI::td("Section:"), - CGI::td({-colspan=>2}, + CGI::td(["Section:", CGI::popup_menu(-name=> 'library_sections', -values=>\@sects, -default=> $section_selected - ))), + )]), + ), CGI::Tr(CGI::td({-colspan=>3}, $view_problem_line)), CGI::end_table(), )); + +} + +sub browse_library_panel2adv { + my $self = shift; + my $r = $self->r; + my $ce = $r->ce; + my @subjs = WeBWorK::Utils::ListingDB::getAllDBsubjects($r); + unshift @subjs, ALL_SUBJECTS; + + my @chaps = WeBWorK::Utils::ListingDB::getAllDBchapters($r); + unshift @chaps, ALL_CHAPTERS; + + my @sects = WeBWorK::Utils::ListingDB::getAllDBsections($r); + unshift @sects, ALL_SECTIONS; + + my $texts = WeBWorK::Utils::ListingDB::getDBTextbooks($r); + #my @textarray = map { $_->[1]." by ".$_->[2] } @{$texts}; + my @textarray = map { $_->[0] } @{$texts}; + my %textlabels = (); + for my $ta (@{$texts}) { + $textlabels{$ta->[0]} = $ta->[1]." by ".$ta->[2]." (edition ".$ta->[3].")"; + } + unshift @textarray, ALL_TEXTBOOKS; + my $atb = ALL_TEXTBOOKS; $textlabels{$atb} = ALL_TEXTBOOKS; + + my $section_selected = $r->param('library_sections') || ALL_SECTIONS; + my $chapter_selected = $r->param('library_chapters') || ALL_CHAPTERS; + my $subject_selected = $r->param('library_subjects') || ALL_SUBJECTS; + my $textbook_selected = $r->param('library_textbook') || ALL_TEXTBOOKS; + + my $text_popup = CGI::popup_menu(-name => 'library_textbook', + -values =>\@textarray, + -labels => \%textlabels, + -default=>$textbook_selected); + my $view_problem_line = view_problems_line('lib_view', 'View Problems', $self->r); + + print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, + CGI::hidden(-name=>"library_is_basic", -default=>[2]), + CGI::start_table({-width=>"100%"}), + # Html done by hand since it is temporary + CGI::Tr(CGI::td({-colspan=>4, -align=>"center"}, 'All Selected Constraints Joined by "And"')), + CGI::Tr( + CGI::td(["Subject:", + CGI::popup_menu(-name=> 'library_subjects', + -values=>\@subjs, + -default=> $subject_selected, + -onchange=>"submit();return true" + )]), + CGI::td({-colspan=>2, -align=>"right"}, + CGI::submit(-name=>"lib_select_subject", -value=>"Update Lists"))), + CGI::Tr( + CGI::td(["Chapter:", + CGI::popup_menu(-name=> 'library_chapters', + -values=>\@chaps, + -default=> $chapter_selected, + -onchange=>"submit();return true" + )]), + CGI::td({-colspan=>2, -align=>"right"}, + CGI::submit(-name=>"library_basic", -value=>"Basic Search")) + ), + CGI::Tr( + CGI::td(["Section:", + CGI::popup_menu(-name=> 'library_sections', + -values=>\@sects, + -default=> $section_selected, + -onchange=>"submit();return true" + )]), + ), + CGI::Tr( + CGI::td(["Textbook:", $text_popup]), + ), + CGI::Tr(CGI::td({-colspan=>3}, $view_problem_line)), + CGI::end_table(), + )); + #CGI::Tr( + # CGI::td("Textbook:"), + # CGI::td({-colspan=>2}, + # CGI::popup_menu(-name=> 'library_textbooks', + # -values=>\@textbooks, + # #-default=> $section_selected + #))), + + #CGI::Tr( + # CGI::td("Keywords:"), + # CGI::td({-colspan=>2}, + # CGI::textfield(-name=>"keywords", + # -default=>"Keywords not implemented yet", + # -override=>1, -size=>60 + #))), } @@ -721,6 +796,13 @@ my $db = $r->db; my $maxShown = $r->param('max_shown') || MAX_SHOW_DEFAULT; $maxShown = 10000000 if($maxShown eq 'All'); # let's hope there aren't more + my $library_basic = $r->param('library_is_basic') || 1; + + ## Fix some parameters + $r->param('library_subjects', '') if($r->param('library_subjects') eq ALL_SUBJECTS); + $r->param('library_chapters', '') if($r->param('library_chapters') eq ALL_CHAPTERS); + $r->param('library_sections', '') if($r->param('library_sections') eq ALL_SECTIONS); + $r->param('library_textbook', '') if($r->param('library_textbook') eq ALL_TEXTBOOKS); ## These directories will have individual buttons %problib = %{$ce->{courseFiles}{problibs}} if $ce->{courseFiles}{problibs}; @@ -864,13 +946,12 @@ $use_previous_problems=0; } - ##### View whole chapter from the library - ## This will change somewhat later + ##### View from the library database } elsif ($r->param('lib_view')) { @pg_files=(); - my @dbsearch = WeBWorK::Utils::ListingDB::getSectionListings($r, subject_default => 'All Subjects', chapter_default => 'All Chapters', section_default=>'All Sections'); + my @dbsearch = WeBWorK::Utils::ListingDB::getSectionListings($r); my ($result, $tolibpath); for $result (@dbsearch) { $tolibpath = "Library/$result->{path}/$result->{filename}"; @@ -997,6 +1078,10 @@ } elsif ($r->param('select_all')) { @past_marks = map {1} @past_marks; + } elsif ($r->param('library_basic')) { + $library_basic = 1; + } elsif ($r->param('library_advanced')) { + $library_basic = 2; } elsif ($r->param('select_none')) { @past_marks = (); @@ -1030,7 +1115,7 @@ $self->{pg_files} = \@pg_files; $self->{past_marks} = \@past_marks; $self->{all_db_sets} = \@all_db_sets; - + $self->{library_basic} = $library_basic; } Index: ListingDB.pm =================================================================== RCS file: /webwork/cvs/system/webwork-modperl/lib/WeBWorK/Utils/ListingDB.pm,v retrieving revision 1.8 retrieving revision 1.9 diff -Llib/WeBWorK/Utils/ListingDB.pm -Llib/WeBWorK/Utils/ListingDB.pm -u -r1.8 -r1.9 --- lib/WeBWorK/Utils/ListingDB.pm +++ lib/WeBWorK/Utils/ListingDB.pm @@ -28,8 +28,8 @@ @EXPORT =qw( &createListing &updateListing &deleteListing &getAllChapters &getAllSections &searchListings &getAllListings &getSectionListings - &getAllDBsubjects &getAllDBchapters &getAllDBsections - &getDBsectionListings + &getAllDBsubjects &getAllDBchapters &getAllDBsections &getDBTextbooks + &getDBListings ); %EXPORT_TAGS =(); @EXPORT_OK =qw(); @@ -46,151 +46,172 @@ return($dbh); } -=item getAllDBsubjects($ce) +=item getDBTextbooks($r) +Returns an array of Textbook names consistent with the subject, chapter, +section selected + +$r is a Apache request object so we can extract whatever parameters we want + +=cut + +sub getDBTextbooks { + my $r = shift; + my $dbh = getDB($r->ce); + # there aren't many, so first get a list of all texts, and then remove + # the ones which aren't ok + my ($sectstring, $chapstring, $subjstring) = ('','',''); + my $subj = $r->param('library_subjects') || ""; + my $chap = $r->param('library_chapters') || ""; + my $sec = $r->param('library_sections') || ""; + if($subj) { + $subj =~ s/'/\\'/g; + $subjstring = " AND t.name = \'$subj\'\n"; + } + if($chap) { + $chap =~ s/'/\\'/g; + $chapstring = " AND c.name = \'$chap\' AND c.DBsubject_id=t.DBsubject_id\n"; + } + if($sec) { + $sec =~ s/'/\\'/g; + $sectstring = " AND s.name = \'$sec\' AND s.DBchapter_id = c.DBchapter_id AND s.DBsection_id=pgf.DBsection_id"; + } + my $query = "SELECT DISTINCT tbk.textbook_id,tbk.title,tbk.author, + tbk.edition + FROM textbook tbk, problem p, pgfile_problem pg, pgfile pgf, + DBsection s, DBchapter c, DBsubject t + WHERE tbk.textbook_id=p.textbook_id AND p.problem_id=pg.problem_id AND + s.DBchapter_id=c.DBchapter_id AND c.DBsubject_id=t.DBsubject_id + AND pgf.DBsection_id=s.DBsection_id AND pgf.pgfile_id=pg.pgfile_id + $chapstring $subjstring $sectstring "; + my $text_ref = $dbh->selectall_arrayref($query); + my @texts = @{$text_ref}; + #@texts = grep { $_->[1] =~ /\S/ } @texts; + return(\@texts); +} + +=item getAllDBsubjects($r) Returns an array of DBsubject names -$ce is a WeBWorK::CourseEnvironment object that describes the problem library. +$r is the Apache request object =cut sub getAllDBsubjects { - my $ce = shift; + my $r = shift; my @results=(); - my ($row,$listing); + my $row; my $query = "SELECT DISTINCT name FROM DBsubject"; - my $dbh = getDB($ce); + my $dbh = getDB($r->ce); my $sth = $dbh->prepare($query); $sth->execute(); - while (1) { - $row = $sth->fetchrow_array; - last if (!defined($row)); - my $listing = $row; - push @results, $listing; + while ($row = $sth->fetchrow_array()) { + push @results, $row; } return @results; } -=item getAllDBchapters($ce) +=item getAllDBchapters($r) Returns an array of DBchapter names -$ce is a WeBWorK::CourseEnvironment object that describes the problem library. +$r is the Apache request object =cut sub getAllDBchapters { - my $ce = shift; - my $subject = shift; - my @results=(); - my ($row,$listing); - my $where = ""; - my $dbh = getDB($ce); - if($subject) { - my $subject_id = ""; - my $query = "SELECT DBsubject_id FROM DBsubject WHERE name = \"$subject\""; - my $subject_id = $dbh->selectrow_array($query); - $where = " WHERE DBsubject_id=\"$subject_id\" "; - } - my $query = "SELECT DISTINCT name FROM DBchapter $where "; - my $sth = $dbh->prepare($query); - $sth->execute(); - while (1) { - $row = $sth->fetchrow_array; - last if (!defined($row)); - my $listing = $row; - push @results, $listing; - } + my $r = shift; + my $subject = $r->param('library_subjects'); + return () unless($subject); + my $dbh = getDB($r->ce); + my $query = "SELECT DISTINCT c.name FROM DBchapter c, DBsubject t + WHERE c.DBsubject_id = t.DBsubject_id AND + t.name = \"$subject\""; + my $all_chaps_ref = $dbh->selectall_arrayref($query); + my @results = map { $_->[0] } @{$all_chaps_ref}; return @results; } -=item getAllDBsections($ce,$chapter) +=item getAllDBsections($r) Returns an array of DBsection names -$ce is a WeBWorK::CourseEnvironment object that describes the problem library. -$chapter is an DBchapter name - +$r is the Apache request object + =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; - } + my $r = shift; + my $subject = $r->param('library_subjects'); + return () unless($subject); + my $chapter = $r->param('library_chapters'); + return () unless($chapter); + my $dbh = getDB($r->ce); + my $query = "SELECT DISTINCT s.name FROM DBsection, DBsection s, + DBchapter c, DBsubject t + WHERE s.DBchapter_id = c.DBchapter_id AND + c.DBsubject_id = t.DBsubject_id AND + t.name = \"$subject\" AND c.name = \"$chapter\""; + my $all_sections_ref = $dbh->selectall_arrayref($query); + my @results = map { $_->[0] } @{$all_sections_ref}; return @results; } -=item getDBSectionListings($ce, $chapter, $section) +=item getDBSectionListings($r) 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 +$r is an Apache request object that has all needed data inside of it + +Here, we search on all known fields out of r =cut -sub getDBsectionListings { - - my $ce = shift; - my $subj = shift; - my $chap = shift; - my $sec = shift; +sub getDBListings { + my $r = shift; + my $ce = $r->ce; + my $subj = $r->param('library_subjects') || ""; + my $chap = $r->param('library_chapters') || ""; + my $sec = $r->param('library_sections') || ""; + my $text = $r->param('library_textbook') || ""; + my $textchap = $r->param('library_textbook_chapter') || ""; + my $textsec = $r->param('library_textbook_section') || ""; my $dbh = getDB($ce); - my $subjstring = ''; + my $extrawhere = ''; if($subj) { $subj =~ s/'/\\'/g; - $subjstring = " AND t.name=\"$subj\" "; + $extrawhere .= " AND dbsj.name=\"$subj\" "; } - my $chapstring = ''; if($chap) { $chap =~ s/'/\\'/g; - $chapstring = " AND c.name=\"$chap\" "; + $extrawhere .= " AND dbc.name=\"$chap\" "; } - my $secstring = ''; if($sec) { $sec =~ s/'/\\'/g; - $secstring = " AND s.name=\"$sec\" "; + $extrawhere .= " AND dbsc.name=\"$sec\" "; } - - my $query = "SELECT DBsection_id - FROM DBsection s, DBchapter c, DBsubject t - WHERE t.DBsubject_id = c.DBsubject_id - and s.DBchapter_id = c.DBchapter_id - $subjstring $chapstring $secstring"; - my $section_id_ref = $dbh->selectall_arrayref($query); - die "getDBSectionListings - no such section: $chap $sec\n" unless(defined $section_id_ref); - my @section_ids = @{$section_id_ref}; - @section_ids = map { "DBsection_id = ". $_->[0] } @section_ids; - my @results; #returned - $query = "SELECT path_id, filename - FROM pgfile WHERE ". join(" OR ", @section_ids); - my $sth = $dbh->prepare($query); - - $sth->execute(); - while (1){ - my ($path_id, $pgfile) = $sth->fetchrow_array(); - last if (!defined($pgfile)); - my $path = $dbh->selectrow_array("SELECT path FROM path - WHERE path_id = $path_id"); - push @results, {"path" => $path, "filename" => $pgfile}; + if($text) { + $text =~ s/'/\\'/g; + $extrawhere .= " AND t.textbook_id=\"$text\" "; + } + + my $query = "SELECT DISTINCT pgf.pgfile_id from pgfile pgf, + DBsection dbsc, DBchapter dbc, DBsubject dbsj, pgfile_problem pgp, + problem p, textbook t + WHERE dbsj.DBsubject_id = dbc.DBsubject_id AND + dbc.DBchapter_id = dbsc.DBchapter_id AND + dbsc.DBsection_id = pgf.DBsection_id AND + pgf.pgfile_id = pgp.pgfile_id AND + pgp.problem_id = p.problem_id AND + p.textbook_id = t.textbook_id \n $extrawhere"; + my $pg_id_ref = $dbh->selectall_arrayref($query); + my @pg_ids = map { $_->[0] } @{$pg_id_ref}; + my @results=(); + for my $pgid (@pg_ids) { + $query = "SELECT path, filename FROM pgfile pgf, path p + WHERE p.path_id = pgf.path_id AND pgf.pgfile_id=\"$pgid\""; + my $row = $dbh->selectrow_arrayref($query); + push @results, {'path' => $row->[0], 'filename' => $row->[1] }; + } return @results; } @@ -368,16 +389,12 @@ sub getSectionListings { #print STDERR "ListingDB::getSectionListings(chapter,section)\n"; my $r = shift; - my %defaults = @_; my $ce = $r->ce; + my $version = $ce->{problemLibrary}->{version} || 1; + if($version == 2) { return(getDBListings($r))} my $subj = $r->param('library_subjects') || ""; my $chap = $r->param('library_chapters') || ""; my $sec = $r->param('library_sections') || ""; - $subj = '' if ($subj eq $defaults{subject_default}); - $chap = '' if ($chap eq $defaults{chapter_default}); - $sec = '' if ($sec eq $defaults{section_default}); - my $version = $ce->{problemLibrary}->{version} || 1; - if($version == 2) { return(getDBsectionListings($ce, $subj, $chap, $sec))} my $chapstring = ''; if($chap) { |