From: jj v. a. <we...@ma...> - 2005-08-03 22:18:14
|
Log Message: ----------- Added problem count to basic library panel, and text chapters/sections to advanced library panel. Modified Files: -------------- webwork-modperl/lib/WeBWorK/Utils: ListingDB.pm webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor: SetMaker.pm Revision Data ------------- Index: ListingDB.pm =================================================================== RCS file: /webwork/cvs/system/webwork-modperl/lib/WeBWorK/Utils/ListingDB.pm,v retrieving revision 1.12 retrieving revision 1.13 diff -Llib/WeBWorK/Utils/ListingDB.pm -Llib/WeBWorK/Utils/ListingDB.pm -u -r1.12 -r1.13 --- lib/WeBWorK/Utils/ListingDB.pm +++ lib/WeBWorK/Utils/ListingDB.pm @@ -18,6 +18,16 @@ use strict; use DBI; +use constant LIBRARY_STRUCTURE => { + textbook => { select => 'tbk.textbook_id,tbk.title,tbk.author,tbk.edition', + name => 'library_textbook', where => 'tbk.textbook_id'}, + textchapter => { select => 'tc.name', name=>'library_textchapter', + where => 'tc.name'}, + textsection => { select => 'ts.name', name=>'library_textsection', + where => 'ts.name'}, + problem => { select => 'prob.name' }, + }; + BEGIN { require Exporter; @@ -46,48 +56,110 @@ return($dbh); } -=item getDBTextbooks($r) -Returns an array of Textbook names consistent with the subject, chapter, -section selected +=item kwtidy($s) and keywordcleaner($s) +Both take a string and perform utility functions related to keywords. +keywordcleaner splits a string, and uses kwtidy to regularize punctuation +and case for an individual entry. -$r is a Apache request object so we can extract whatever parameters we want +=cut + +sub kwtidy { + my $s = shift; + $s =~ s/\'//g; + $s =~ s/\"//g; + $s =~ s/\#//g; + $s = lc($s); + return($s); +} + +sub keywordCleaner { + my $string = shift; + my @spl1 = split /\s*,\s*/, $string; + my @spl2 = map(kwtidy($_), @spl1); + return(@spl2); +} + +sub makeKeywordWhere { + my $kwstring = shift; + my @kwlist = keywordCleaner($kwstring); + @kwlist = map { "kw.keyword = \"$_\"" } @kwlist; + my $where = join(" OR ", @kwlist); + return "AND ( $where )"; +} + + +=item getDBTextbooks($r) +Returns textbook dependent entries. +$r is a Apache request object so we can extract whatever parameters we want + +$thing is a string of either 'textbook', 'textchapter', or 'textsection' to +specify what to return. + +If we are to return textbooks, then return an array of textbook names +consistent with the DB subject, chapter, section selected. + =cut sub getDBTextbooks { my $r = shift; + my $thing = shift || 'textbook'; 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 $extrawhere = ''; + # Handle DB* restrictions 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"; + $extrawhere .= " AND t.name = \'$subj\'\n"; } if($chap) { $chap =~ s/'/\\'/g; - $chapstring = " AND c.name = \'$chap\' AND c.DBsubject_id=t.DBsubject_id\n"; + $extrawhere .= " 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"; + $extrawhere .= " AND s.name = \'$sec\' AND s.DBchapter_id = c.DBchapter_id AND s.DBsection_id=pgf.DBsection_id"; + } + my $textextrawhere = ''; + my $textid = $r->param('library_textbook') || ''; + if($textid and $thing ne 'textbook') { + $textextrawhere .= " AND tbk.textbook_id=\"$textid\" "; + } else { + return([]) if($thing ne 'textbook'); + } + + my $textchap = $r->param('library_textchapter') || ''; + if($textchap and $thing eq 'textsection') { + $textextrawhere .= " AND tc.name=\"$textchap\" "; + } else { + return([]) if($thing eq 'textsection'); } - 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, chapter cc, section ss - WHERE ss.section_id=p.section_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 - AND ss.chapter_id=cc.chapter_id AND cc.textbook_id=tbk.textbook_id - $chapstring $subjstring $sectstring "; + + my $selectwhat = LIBRARY_STRUCTURE->{$thing}{select}; + + my $query = "SELECT DISTINCT $selectwhat + FROM textbook tbk, problem prob, pgfile_problem pg, pgfile pgf, + DBsection s, DBchapter c, DBsubject t, chapter tc, section ts + WHERE ts.section_id=prob.section_id AND + prob.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 AND + ts.chapter_id=tc.chapter_id AND + tc.textbook_id=tbk.textbook_id + $extrawhere $textextrawhere "; my $text_ref = $dbh->selectall_arrayref($query); my @texts = @{$text_ref}; - @texts = grep { $_->[1] =~ /\S/ } @texts; - return(\@texts); + if( $thing eq 'textbook') { + @texts = grep { $_->[1] =~ /\S/ } @texts; + return(\@texts); + } else { + @texts = grep { $_->[0] =~ /\S/ } @texts; + return(\@texts); + } } =item getAllDBsubjects($r) @@ -172,9 +244,15 @@ 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 $keywords = $r->param('library_keywords') || ""; + my ($kw1, $kw2) = ('',''); + if($keywords) { + $kw1 = ", keyword kw, pgfile_keyword pgkey"; + $kw2 = " AND kw.keyword_id=pgkey.keyword_id AND + pgkey.pgfile_id=pgf.pgfile_id ". + makeKeywordWhere($keywords) + ; + } my $dbh = getDB($ce); @@ -191,9 +269,13 @@ $sec =~ s/'/\\'/g; $extrawhere .= " AND dbsc.name=\"$sec\" "; } - if($text) { - $text =~ s/'/\\'/g; - $extrawhere .= " AND t.textbook_id=\"$text\" "; + my $textextrawhere = ''; + for my $j (qw( textbook textchapter textsection )) { + my $foo = $r->param(LIBRARY_STRUCTURE->{$j}{name}) || ''; + if($foo) { + $foo =~ s/'/\\'/g; + $textextrawhere .= " AND ".LIBRARY_STRUCTURE->{$j}{where}."=\"$foo\" "; + } } my $selectwhat = 'DISTINCT pgf.pgfile_id'; @@ -201,15 +283,18 @@ my $query = "SELECT $selectwhat from pgfile pgf, DBsection dbsc, DBchapter dbc, DBsubject dbsj, pgfile_problem pgp, - problem p, textbook t , chapter cc, section ss + problem prob, textbook tbk , chapter tc, section ts $kw1 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 - cc.textbook_id = t.textbook_id AND - ss.chapter_id = cc.chapter_id AND - p.section_id = ss.section_id \n $extrawhere"; + pgp.problem_id = prob.problem_id AND + tc.textbook_id = tbk.textbook_id AND + ts.chapter_id = tc.chapter_id AND + prob.section_id = ts.section_id \n $extrawhere \n $textextrawhere + $kw2"; +#$query =~ s/\n/ /g; +#warn $query; my $pg_id_ref = $dbh->selectall_arrayref($query); my @pg_ids = map { $_->[0] } @{$pg_id_ref}; if($amcounter) { Index: SetMaker.pm =================================================================== RCS file: /webwork/cvs/system/webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm,v retrieving revision 1.47 retrieving revision 1.48 diff -Llib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm -Llib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm -u -r1.47 -r1.48 --- lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm +++ lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm @@ -47,12 +47,15 @@ use constant ALL_SECTIONS => 'All Sections'; use constant ALL_TEXTBOOKS => 'All Textbooks'; -#use constant LIB2_DATA => [ - #[qw(dbchapter library_chapters), ALL_CHAPTERS], - #[qw(dbsection library_sections), ALL_SECTIONS], - #[qw(dbsubject library_subjects), ALL_SUBJECT], - #[qw(textbook library_textbook), ALL_TEXTBOOKS], - #]; +use constant LIB2_DATA => { + 'dbchapter' => {name => 'library_chapters', all => 'All Chapters'}, + 'dbsection' => {name => 'library_sections', all =>'All Sections' }, + 'dbsubject' => {name => 'library_subjects', all => 'All Subjects' }, + 'textbook' => {name => 'library_textbook', all => 'All Textbooks'}, + 'textchapter' => {name => 'library_textchapter', all => 'All Chapters'}, + 'textsection' => {name => 'library_textsection', all => 'All Sections'}, + 'keywords' => {name => 'library_keywords', all => '' }, + }; ## Flags for operations on files @@ -414,16 +417,17 @@ my $ce = $r->ce; my @chaps = WeBWorK::Utils::ListingDB::getAllChapters($r->{ce}); - unshift @chaps, ALL_CHAPTERS; - my $chapter_selected = $r->param('library_chapters') || ALL_CHAPTERS; + unshift @chaps, LIB2_DATA->{dbchapter}{all}; + my $chapter_selected = $r->param('library_chapters') || LIB2_DATA->{dbchapter}->{all}; my @sects=(); - if ($chapter_selected ne ALL_CHAPTERS) { + if ($chapter_selected ne LIB2_DATA->{dbchapter}{all}) { @sects = WeBWorK::Utils::ListingDB::getAllSections($r->{ce}, $chapter_selected); } unshift @sects, ALL_SECTIONS; - my $section_selected = $r->param('library_sections') || ALL_SECTIONS; + my $section_selected = $r->param('library_sections') || LIB2_DATA->{dbsection}{all}; + my $view_problem_line = view_problems_line('lib_view', 'View Problems', $self->r); print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, @@ -455,21 +459,28 @@ my $ce = $r->ce; my @subjs = WeBWorK::Utils::ListingDB::getAllDBsubjects($r); - unshift @subjs, ALL_SUBJECTS; + unshift @subjs, LIB2_DATA->{dbsubject}{all}; my @chaps = WeBWorK::Utils::ListingDB::getAllDBchapters($r); - unshift @chaps, ALL_CHAPTERS; + unshift @chaps, LIB2_DATA->{dbchapter}{all}; my @sects=(); @sects = WeBWorK::Utils::ListingDB::getAllDBsections($r); - unshift @sects, ALL_SECTIONS; + unshift @sects, LIB2_DATA->{dbsection}{all}; - 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 $subject_selected = $r->param('library_subjects') || LIB2_DATA->{dbsubject}{all}; + my $chapter_selected = $r->param('library_chapters') || LIB2_DATA->{dbchapter}{all}; + my $section_selected = $r->param('library_sections') || LIB2_DATA->{dbsection}{all}; my $view_problem_line = view_problems_line('lib_view', 'View Problems', $self->r); + my $count_line = WeBWorK::Utils::ListingDB::countDBListings($r); + if($count_line==0) { + $count_line = "There are no matching pg files"; + } else { + $count_line = "There are $count_line matching WeBWorK problem files"; + } + print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, CGI::hidden(-name=>"library_is_basic", -default=>[1]), CGI::start_table({-width=>"100%"}), @@ -497,10 +508,12 @@ CGI::td(["Section:", CGI::popup_menu(-name=> 'library_sections', -values=>\@sects, - -default=> $section_selected + -default=> $section_selected, + -onchange=>"submit();return true" )]), ), CGI::Tr(CGI::td({-colspan=>3}, $view_problem_line)), + CGI::Tr(CGI::td({-colspan=>3, -align=>"center"}, $count_line)), CGI::end_table(), )); @@ -516,19 +529,19 @@ if(! grep { $_ eq $r->param('library_subjects') } @subjs) { $r->param('library_subjects', ''); } - unshift @subjs, ALL_SUBJECTS; + unshift @subjs, LIB2_DATA->{dbsubject}{all}; my @chaps = WeBWorK::Utils::ListingDB::getAllDBchapters($r); if(! grep { $_ eq $r->param('library_chapters') } @chaps) { $r->param('library_chapters', ''); } - unshift @chaps, ALL_CHAPTERS; + unshift @chaps, LIB2_DATA->{dbchapter}{all}; my @sects = WeBWorK::Utils::ListingDB::getAllDBsections($r); if(! grep { $_ eq $r->param('library_sections') } @sects) { $r->param('library_sections', ''); } - unshift @sects, ALL_SECTIONS; + unshift @sects, LIB2_DATA->{dbsection}{all}; my $texts = WeBWorK::Utils::ListingDB::getDBTextbooks($r); my @textarray = map { $_->[0] } @{$texts}; @@ -539,19 +552,34 @@ if(! grep { $_ eq $r->param('library_textbook') } @textarray) { $r->param('library_textbook', ''); } - unshift @textarray, ALL_TEXTBOOKS; - my $atb = ALL_TEXTBOOKS; $textlabels{$atb} = ALL_TEXTBOOKS; + unshift @textarray, LIB2_DATA->{textbook}{all}; + my $atb = LIB2_DATA->{textbook}{all}; $textlabels{$atb} = LIB2_DATA->{textbook}{all}; - 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 $textchap_ref = WeBWorK::Utils::ListingDB::getDBTextbooks($r, 'textchapter'); + my @textchaps = map { $_->[0] } @{$textchap_ref}; + if(! grep { $_ eq $r->param('library_textchapter') } @textchaps) { + $r->param('library_textchapter', ''); + } + unshift @textchaps, LIB2_DATA->{textchapter}{all}; + + my $textsec_ref = WeBWorK::Utils::ListingDB::getDBTextbooks($r, 'textsection'); + my @textsecs = map { $_->[0] } @{$textsec_ref}; + if(! grep { $_ eq $r->param('library_textsection') } @textsecs) { + $r->param('library_textsection', ''); + } + unshift @textsecs, LIB2_DATA->{textsection}{all}; + + my %selected = (); + for my $j (qw( dbsection dbchapter dbsubject textbook textchapter textsection )) { + $selected{$j} = $r->param(LIB2_DATA->{$j}{name}) || LIB2_DATA->{$j}{all}; + } my $text_popup = CGI::popup_menu(-name => 'library_textbook', -values =>\@textarray, -labels => \%textlabels, - -default=>$textbook_selected, + -default=>$selected{textbook}, -onchange=>"submit();return true"); + my $library_keywords = $r->param('library_keywords') || ''; @@ -573,7 +601,7 @@ CGI::td(["Subject:", CGI::popup_menu(-name=> 'library_subjects', -values=>\@subjs, - -default=> $subject_selected, + -default=> $selected{dbsubject}, -onchange=>"submit();return true" )]), CGI::td({-colspan=>2, -align=>"right"}, @@ -583,7 +611,7 @@ CGI::td(["Chapter:", CGI::popup_menu(-name=> 'library_chapters', -values=>\@chaps, - -default=> $chapter_selected, + -default=> $selected{dbchapter}, -onchange=>"submit();return true" )]), CGI::td({-colspan=>2, -align=>"right"}, @@ -594,7 +622,7 @@ CGI::td(["Section:", CGI::popup_menu(-name=> 'library_sections', -values=>\@sects, - -default=> $section_selected, + -default=> $selected{dbsection}, -onchange=>"submit();return true" )]), CGI::td({-colspan=>2, -align=>"right"}, @@ -604,6 +632,22 @@ CGI::Tr( CGI::td(["Textbook:", $text_popup]), ), + CGI::Tr( + CGI::td(["Text chapter:", + CGI::popup_menu(-name=> 'library_textchapter', + -values=>\@textchaps, + -default=> $selected{textchapter}, + -onchange=>"submit();return true" + )]), + ), + CGI::Tr( + CGI::td(["Text section:", + CGI::popup_menu(-name=> 'library_textsection', + -values=>\@textsecs, + -default=> $selected{textsection}, + -onchange=>"submit();return true" + )]), + ), CGI::Tr(CGI::td("Keywords:"),CGI::td({-colspan=>2}, CGI::textfield(-name=>"library_keywords", -default=>$library_keywords, @@ -1114,8 +1158,15 @@ @past_marks = map {1} @past_marks; } elsif ($r->param('library_basic')) { $library_basic = 1; + for my $jj (qw(textchapter textsection textbook)) { + $r->param('library_'.$jj,''); + } } elsif ($r->param('library_advanced')) { $library_basic = 2; + } elsif ($r->param('library_reset')) { + for my $jj (qw(chapters sections subjects textbook keywords)) { + $r->param('library_'.$jj,''); + } } elsif ($r->param('select_none')) { @past_marks = (); |