[system] / trunk / webwork-modperl / lib / WeBWorK / ContentGenerator / Instructor / SetMaker.pm Repository:
ViewVC logotype

View of /trunk/webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3519 - (download) (as text) (annotate)
Sat Aug 13 21:35:56 2005 UTC (7 years, 10 months ago) by jj
File size: 45737 byte(s)
Print contents of COMMENT() commands in the Library Browser.

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm,v 1.51 2005/08/11 21:40:55 sh002i Exp $
    5 #
    6 # This program is free software; you can redistribute it and/or modify it under
    7 # the terms of either: (a) the GNU General Public License as published by the
    8 # Free Software Foundation; either version 2, or (at your option) any later
    9 # version, or (b) the "Artistic License" which comes with this package.
   10 #
   11 # This program is distributed in the hope that it will be useful, but WITHOUT
   12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   14 # Artistic License for more details.
   15 ################################################################################
   16 
   17 
   18 package WeBWorK::ContentGenerator::Instructor::SetMaker;
   19 use base qw(WeBWorK::ContentGenerator::Instructor);
   20 
   21 =head1 NAME
   22 
   23 WeBWorK::ContentGenerator::Instructor::SetMaker - Make problem sets.
   24 
   25 =cut
   26 
   27 use strict;
   28 use warnings;
   29 
   30 use CGI::Pretty qw();
   31 use WeBWorK::Debug;
   32 use WeBWorK::Form;
   33 use WeBWorK::Utils qw(readDirectory max sortByName);
   34 use WeBWorK::Utils::Tasks qw(renderProblems);
   35 use File::Find;
   36 
   37 require WeBWorK::Utils::ListingDB;
   38 
   39 use constant MAX_SHOW_DEFAULT => 20;
   40 use constant NO_LOCAL_SET_STRING => 'No sets in this course yet';
   41 use constant SELECT_SET_STRING => 'Select a Set from this Course';
   42 use constant SELECT_LOCAL_STRING => 'Select a Problem Collection';
   43 use constant MY_PROBLEMS => '  My Problems  ';
   44 use constant MAIN_PROBLEMS => '  Main Problems  ';
   45 use constant CREATE_SET_BUTTON => 'Create New Set';
   46 use constant ALL_CHAPTERS => 'All Chapters';
   47 use constant ALL_SUBJECTS => 'All Subjects';
   48 use constant ALL_SECTIONS => 'All Sections';
   49 use constant ALL_TEXTBOOKS => 'All Textbooks';
   50 
   51 use constant LIB2_DATA => {
   52   'dbchapter' => {name => 'library_chapters', all => 'All Chapters'},
   53   'dbsection' =>  {name => 'library_sections', all =>'All Sections' },
   54   'dbsubject' =>  {name => 'library_subjects', all => 'All Subjects' },
   55   'textbook' =>  {name => 'library_textbook', all =>  'All Textbooks'},
   56   'textchapter' => {name => 'library_textchapter', all => 'All Chapters'},
   57   'textsection' => {name => 'library_textsection', all => 'All Sections'},
   58   'keywords' =>  {name => 'library_keywords', all => '' },
   59   };
   60 
   61 ## Flags for operations on files
   62 
   63 use constant ADDED => 1;
   64 use constant HIDDEN => (1 << 1);
   65 use constant SUCCESS => (1 << 2);
   66 
   67 ##  for additional problib buttons
   68 my %problib;  ## filled in in global.conf
   69 my %ignoredir = (
   70   '.' => 1, '..' => 1, 'Library' => 1,
   71   'headers' => 1, 'macros' => 1, 'email' => 1,
   72 );
   73 
   74 ## This is for searching the disk for directories containing pg files.
   75 ## to make the recursion work, this returns an array where the first
   76 ## item is the number of pg files in the directory.  The second is a
   77 ## list of directories which contain pg files.
   78 ##
   79 ## If a directory contains only one pg file and at least one other
   80 ## file, the directory is considered to be part of the parent
   81 ## directory (it is probably in a separate directory only because
   82 ## it has auxiliarly files that want to be kept together with the
   83 ## pg file).
   84 ##
   85 ## If a directory has a file named "=library-ignore", it is never
   86 ## included in the directory menu.  If a directory contains a file
   87 ## called "=library-combine-up", then its pg are included with those
   88 ## in the parent directory (and the directory does not appear in the
   89 ## menu).  If it has a file called "=library-no-combine" then it is
   90 ## always listed as a separate directory even if it contains only one
   91 ## pg file.
   92 
   93 sub get_library_sets {
   94   my $top = shift; my $dir =  shift;
   95   # ignore directories that give us an error
   96   my @lis = eval { readDirectory($dir) };
   97   if ($@) {
   98     warn $@;
   99     return (0);
  100   }
  101   return (0) if grep /^=library-ignore$/, @lis;
  102 
  103   my @pgdirs;
  104 
  105   my $pgcount = scalar(grep { m/\.pg$/ and (not m/(Header|-text)\.pg$/) and -f "$dir/$_"} @lis);
  106   my $others = scalar(grep { (!m/\.pg$/ || m/(Header|-text)\.pg$/) &&
  107                               !m/(\.(tmp|bak)|~)$/ && -f "$dir/$_" } @lis);
  108 
  109   my @dirs = grep {!$ignoredir{$_} and -d "$dir/$_"} @lis;
  110   if ($top == 1) {@dirs = grep {!$problib{$_}} @dirs}
  111   foreach my $subdir (@dirs) {
  112     my @results = get_library_sets(0, "$dir/$subdir");
  113     $pgcount += shift @results; push(@pgdirs,@results);
  114   }
  115 
  116   return ($pgcount, @pgdirs) if $top || $pgcount == 0 || grep /^=library-combine-up$/, @lis;
  117   return (0,@pgdirs,$dir) if $pgcount > 1 || $others == 0 || grep /^=library-no-combine$/, @lis;
  118   return ($pgcount, @pgdirs);
  119 }
  120 
  121 sub get_library_pgs {
  122   my $top = shift; my $base = shift; my $dir =  shift;
  123   my @lis = readDirectory("$base/$dir");
  124   return () if grep /^=library-ignore$/, @lis;
  125   return () if !$top && grep /^=library-no-combine$/, @lis;
  126 
  127   my @pgs = grep { m/\.pg$/ and (not m/(Header|-text)\.pg$/) and -f "$base/$dir/$_"} @lis;
  128   my $others = scalar(grep { (!m/\.pg$/ || m/(Header|-text)\.pg$/) &&
  129                               !m/(\.(tmp|bak)|~)$/ && -f "$base/$dir/$_" } @lis);
  130 
  131   my @dirs = grep {!$ignoredir{$_} and -d "$base/$dir/$_"} @lis;
  132   if ($top == 1) {@dirs = grep {!$problib{$_}} @dirs}
  133   foreach my $subdir (@dirs) {push(@pgs, get_library_pgs(0,"$base/$dir",$subdir))}
  134 
  135   return () unless $top || (scalar(@pgs) == 1 && $others) || grep /^=library-combine-up$/, @lis;
  136   return (map {"$dir/$_"} @pgs);
  137 }
  138 
  139 sub list_pg_files {
  140   my ($templates,$dir) = @_;
  141   my $top = ($dir eq '.')? 1 : 2;
  142   my @pgs = get_library_pgs($top,$templates,$dir);
  143   return sortByName(undef,@pgs);
  144 }
  145 
  146 ## Search for set definition files
  147 
  148 sub get_set_defs {
  149   my $topdir = shift;
  150   my @found_set_defs;
  151   # get_set_defs_wanted is a closure over @found_set_defs
  152   my $get_set_defs_wanted = sub {
  153     my $fn = $_;
  154     my $fdir = $File::Find::dir;
  155     return() if($fn !~ /^set.*\.def$/);
  156     #return() if(not -T $fn);
  157     push @found_set_defs, "$fdir/$fn";
  158   };
  159   find({ wanted => $get_set_defs_wanted, follow_fast=>1}, $topdir);
  160   map { $_ =~ s|^$topdir/?|| } @found_set_defs;
  161   return @found_set_defs;
  162 }
  163 
  164 ## Try to make reading of set defs more flexible.  Additional strategies
  165 ## for fixing a path can be added here.
  166 
  167 sub munge_pg_file_path {
  168   my $self = shift;
  169   my $pg_path = shift;
  170   my $path_to_set_def = shift;
  171   my $end_path = $pg_path;
  172   # if the path is ok, don't fix it
  173   return($pg_path) if(-e $self->r->ce->{courseDirs}{templates}."/$pg_path");
  174   # if we have followed a link into a self contained course to get
  175   # to the set.def file, we need to insert the start of the path to
  176   # the set.def file
  177   $end_path = "$path_to_set_def/$pg_path";
  178   return($end_path) if(-e $self->r->ce->{courseDirs}{templates}."/$end_path");
  179   # if we got this far, this path is bad, but we let it produce
  180   # an error so the user knows there is a troublesome path in the
  181   # set.def file.
  182   return($pg_path);
  183 }
  184 
  185 ## Read a set definition file.  This could be abstracted since it happens
  186 ## elsewhere.  Here we don't have to process so much of the file.
  187 
  188 sub read_set_def {
  189   my $self = shift;
  190   my $r = $self->r;
  191   my $filePathOrig = shift;
  192   my $filePath = $r->ce->{courseDirs}{templates}."/$filePathOrig";
  193   $filePathOrig =~ s/set.*\.def$//;
  194   $filePathOrig =~ s|/$||;
  195   $filePathOrig = "." if ($filePathOrig !~ /\S/);
  196   my @pg_files = ();
  197   my ($line, $got_to_pgs, $name, @rest) = ("", 0, "");
  198   if ( open (SETFILENAME, "$filePath") )    {
  199     while($line = <SETFILENAME>) {
  200       chomp($line);
  201       $line =~ s|(#.*)||; # don't read past comments
  202       if($got_to_pgs) {
  203         unless ($line =~ /\S/) {next;} # skip blank lines
  204         ($name,@rest) = split (/\s*,\s*/,$line);
  205         $name =~ s/\s*//g;
  206         push @pg_files, $name;
  207       } else {
  208         $got_to_pgs = 1 if ($line =~ /problemList\s*=/);
  209       }
  210     }
  211   } else {
  212     $self->addbadmessage("Cannot open $filePath");
  213   }
  214   # This is where we would potentially munge the pg file paths
  215   # One possibility
  216   @pg_files = map { $self->munge_pg_file_path($_, $filePathOrig) } @pg_files;
  217   return(@pg_files);
  218 }
  219 
  220 ## go through past page getting a list of identifiers for the problems
  221 ## and whether or not they are selected, and whether or not they should
  222 ## be hidden
  223 
  224 sub get_past_problem_files {
  225   my $r = shift;
  226   my @found=();
  227   my $count =1;
  228   while (defined($r->param("filetrial$count"))) {
  229     my $val = 0;
  230     $val |= ADDED if($r->param("trial$count"));
  231     $val |= HIDDEN if($r->param("hideme$count"));
  232     push @found, [$r->param("filetrial$count"), $val];
  233     $count++;
  234   }
  235   return(\@found);
  236 }
  237 
  238 #### For adding new problems
  239 
  240 sub add_selected {
  241   my $self = shift;
  242   my $db = shift;
  243   my $setName = shift;
  244   my @past_problems = @{$self->{past_problems}};
  245   my @selected = @past_problems;
  246   my (@path, $file, $selected, $freeProblemID);
  247   $freeProblemID = max($db->listGlobalProblems($setName)) + 1;
  248   my $addedcount=0;
  249 
  250   for $selected (@selected) {
  251     if($selected->[1] & ADDED) {
  252       $file = $selected->[0];
  253       my $problemRecord = $self->addProblemToSet(setName => $setName,
  254         sourceFile => $file, problemID => $freeProblemID);
  255       $freeProblemID++;
  256       $self->assignProblemToAllSetUsers($problemRecord);
  257       $selected->[1] |= SUCCESS;
  258       $addedcount++;
  259     }
  260   }
  261   return($addedcount);
  262 }
  263 
  264 
  265 ############# List of sets of problems in templates directory
  266 
  267 sub get_problem_directories {
  268   my $ce = shift;
  269   my $lib = shift;
  270   my $source = $ce->{courseDirs}{templates};
  271   my $main = MY_PROBLEMS; my $isTop = 1;
  272   if ($lib) {$source .= "/$lib"; $main = MAIN_PROBLEMS; $isTop = 2}
  273   my @all_problem_directories = get_library_sets($isTop, $source);
  274   my $includetop = shift @all_problem_directories;
  275   my $j;
  276   for ($j=0; $j<scalar(@all_problem_directories); $j++) {
  277     $all_problem_directories[$j] =~ s|^$ce->{courseDirs}->{templates}/?||;
  278   }
  279   @all_problem_directories = sortByName(undef, @all_problem_directories);
  280   unshift @all_problem_directories, $main if($includetop);
  281   return (\@all_problem_directories);
  282 }
  283 
  284 ############# Everyone has a view problems line.  Abstract it
  285 sub view_problems_line {
  286   my $internal_name = shift;
  287   my $label = shift;
  288   my $r = shift; # so we can get parameter values
  289   my $result = CGI::submit(-name=>"$internal_name", -value=>$label);
  290 
  291   my %display_modes = %{WeBWorK::PG::DISPLAY_MODES()};
  292   my @active_modes = grep { exists $display_modes{$_} }
  293     @{$r->ce->{pg}->{displayModes}};
  294   push @active_modes, 'None';
  295   # We have our own displayMode since its value may be None, which is illegal
  296   # in other modules.
  297   my $mydisplayMode = $r->param('mydisplayMode') || $r->ce->{pg}->{options}->{displayMode};
  298   $result .= '&nbsp;Display&nbsp;Mode:&nbsp;'.CGI::popup_menu(-name=> 'mydisplayMode',
  299                                                               -values=>\@active_modes,
  300                                                               -default=> $mydisplayMode);
  301   # Now we give a choice of the number of problems to show
  302   my $defaultMax = $r->param('max_shown') || MAX_SHOW_DEFAULT;
  303   $result .= '&nbsp;Max. Shown:&nbsp'.
  304     CGI::popup_menu(-name=> 'max_shown',
  305                     -values=>[5,10,15,20,25,30,50,'All'],
  306                     -default=> $defaultMax);
  307 
  308   return($result);
  309 }
  310 
  311 
  312 ### The browsing panel has three versions
  313 #####  Version 1 is local problems
  314 sub browse_local_panel {
  315   my $self = shift;
  316   my $library_selected = shift;
  317   my $lib = shift || ''; $lib =~ s/^browse_//;
  318   my $name = ($lib eq '')? 'Local' : $problib{$lib};
  319 
  320   my $list_of_prob_dirs= get_problem_directories($self->r->ce,$lib);
  321   if(scalar(@$list_of_prob_dirs) == 0) {
  322     $library_selected = "Found no directories containing problems";
  323     unshift @{$list_of_prob_dirs}, $library_selected;
  324   } else {
  325     my $default_value = SELECT_LOCAL_STRING;
  326     if (not $library_selected or $library_selected eq $default_value) {
  327       unshift @{$list_of_prob_dirs},  $default_value;
  328       $library_selected = $default_value;
  329     }
  330   }
  331   my $view_problem_line = view_problems_line('view_local_set', 'View Problems', $self->r);
  332   print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "$name Problems: ",
  333     CGI::popup_menu(-name=> 'library_sets',
  334                     -values=>$list_of_prob_dirs,
  335                     -default=> $library_selected),
  336     CGI::br(),
  337     $view_problem_line,
  338   ));
  339 }
  340 
  341 #####  Version 2 is local problem sets
  342 sub browse_mysets_panel {
  343   my $self = shift;
  344   my $library_selected = shift;
  345   my $list_of_local_sets = shift;
  346   my $default_value = "Select a Homework Set";
  347 
  348   if(scalar(@$list_of_local_sets) == 0) {
  349     $list_of_local_sets = [NO_LOCAL_SET_STRING];
  350   } elsif (not $library_selected or $library_selected eq $default_value) {
  351     unshift @{$list_of_local_sets},  $default_value;
  352     $library_selected = $default_value;
  353   }
  354 
  355   my $view_problem_line = view_problems_line('view_mysets_set', 'View Problems', $self->r);
  356   print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "Browse from: ",
  357     CGI::popup_menu(-name=> 'library_sets',
  358                     -values=>$list_of_local_sets,
  359                     -default=> $library_selected),
  360     CGI::br(),
  361     $view_problem_line
  362   ));
  363 }
  364 
  365 #####  Version 3 is the problem library
  366 #
  367 # This comes in 3 forms, problem library version 1, and for version 2 there
  368 # is the basic, and the advanced interfaces.  This function checks what we are
  369 # supposed to do, or aborts if the problem library has not been installed.
  370 
  371 sub browse_library_panel {
  372   my $self=shift;
  373   my $r = $self->r;
  374   my $ce = $r->ce;
  375 
  376   # See if the problem library is installed
  377   my $libraryRoot = $r->{ce}->{problemLibrary}->{root};
  378 
  379   unless($libraryRoot) {
  380     print CGI::Tr(CGI::td(CGI::div({class=>'ResultsWithError', align=>"center"},
  381       "The problem library has not been installed.")));
  382     return;
  383   }
  384   # Test if the Library directory link exists.  If not, try to make it
  385   unless(-d "$ce->{courseDirs}->{templates}/Library") {
  386     unless(symlink($libraryRoot, "$ce->{courseDirs}->{templates}/Library")) {
  387       my $msg =  <<"HERE";
  388 You are missing the directory <code>templates/Library</code>, which is needed
  389 for the Problem Library to function.  It should be a link pointing to
  390 <code>$libraryRoot</code>, which you set in <code>conf/global.conf</code>.
  391 I tried to make the link for you, but that failed.  Check the permissions
  392 in your <code>templates</code> directory.
  393 HERE
  394       $self->addbadmessage($msg);
  395     }
  396   }
  397 
  398   # Now check what version we are supposed to use
  399   my $libraryVersion = $r->{ce}->{problemLibrary}->{version} || 1;
  400   if($libraryVersion == 1) {
  401     return $self->browse_library_panel1;
  402   } elsif($libraryVersion == 2) {
  403     return $self->browse_library_panel2 if($self->{library_basic}==1);
  404     return $self->browse_library_panel2adv;
  405   } else {
  406     print CGI::Tr(CGI::td(CGI::div({class=>'ResultsWithError', align=>"center"},
  407       "The problem library version is set to an illegal value.")));
  408     return;
  409   }
  410 }
  411 
  412 sub browse_library_panel1 {
  413   my $self = shift;
  414   my $r = $self->r;
  415   my $ce = $r->ce;
  416 
  417   my @chaps = WeBWorK::Utils::ListingDB::getAllChapters($r->{ce});
  418   unshift @chaps, LIB2_DATA->{dbchapter}{all};
  419   my $chapter_selected = $r->param('library_chapters') || LIB2_DATA->{dbchapter}->{all};
  420 
  421   my @sects=();
  422   if ($chapter_selected ne LIB2_DATA->{dbchapter}{all}) {
  423     @sects = WeBWorK::Utils::ListingDB::getAllSections($r->{ce}, $chapter_selected);
  424   }
  425 
  426   unshift @sects, ALL_SECTIONS;
  427   my $section_selected =  $r->param('library_sections') || LIB2_DATA->{dbsection}{all};
  428 
  429   my $view_problem_line = view_problems_line('lib_view', 'View Problems', $self->r);
  430 
  431   print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"},
  432     CGI::start_table(),
  433       CGI::Tr(
  434         CGI::td(["Chapter:",
  435           CGI::popup_menu(-name=> 'library_chapters',
  436                           -values=>\@chaps,
  437                           -default=> $chapter_selected,
  438                           -onchange=>"submit();return true"
  439           ),
  440           CGI::submit(-name=>"lib_select_chapter", -value=>"Update Section List")])),
  441       CGI::Tr(
  442         CGI::td("Section:"),
  443         CGI::td({-colspan=>2},
  444           CGI::popup_menu(-name=> 'library_sections',
  445                           -values=>\@sects,
  446                           -default=> $section_selected
  447       ))),
  448 
  449       CGI::Tr(CGI::td({-colspan=>3}, $view_problem_line)),
  450       CGI::end_table(),
  451     ));
  452 }
  453 
  454 sub browse_library_panel2 {
  455   my $self = shift;
  456   my $r = $self->r;
  457   my $ce = $r->ce;
  458 
  459   my @subjs = WeBWorK::Utils::ListingDB::getAllDBsubjects($r);
  460   unshift @subjs, LIB2_DATA->{dbsubject}{all};
  461 
  462   my @chaps = WeBWorK::Utils::ListingDB::getAllDBchapters($r);
  463   unshift @chaps, LIB2_DATA->{dbchapter}{all};
  464 
  465   my @sects=();
  466   @sects = WeBWorK::Utils::ListingDB::getAllDBsections($r);
  467   unshift @sects, LIB2_DATA->{dbsection}{all};
  468 
  469   my $subject_selected = $r->param('library_subjects') || LIB2_DATA->{dbsubject}{all};
  470   my $chapter_selected = $r->param('library_chapters') || LIB2_DATA->{dbchapter}{all};
  471   my $section_selected =  $r->param('library_sections') || LIB2_DATA->{dbsection}{all};
  472 
  473   my $view_problem_line = view_problems_line('lib_view', 'View Problems', $self->r);
  474 
  475   my $count_line = WeBWorK::Utils::ListingDB::countDBListings($r);
  476   if($count_line==0) {
  477     $count_line = "There are no matching pg files";
  478   } else {
  479     $count_line = "There are $count_line matching WeBWorK problem files";
  480   }
  481 
  482   print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"},
  483     CGI::hidden(-name=>"library_is_basic", -default=>[1]),
  484     CGI::start_table({-width=>"100%"}),
  485     CGI::Tr(
  486       CGI::td(["Subject:",
  487         CGI::popup_menu(-name=> 'library_subjects',
  488                       -values=>\@subjs,
  489                       -default=> $subject_selected,
  490                        -onchange=>"submit();return true"
  491         )]),
  492       CGI::td({-colspan=>2, -align=>"right"},
  493         CGI::submit(-name=>"lib_select_subject", -value=>"Update Chapter/Section Lists"))
  494     ),
  495     CGI::Tr(
  496       CGI::td(["Chapter:",
  497         CGI::popup_menu(-name=> 'library_chapters',
  498                       -values=>\@chaps,
  499                       -default=> $chapter_selected,
  500                        -onchange=>"submit();return true"
  501         )]),
  502       CGI::td({-colspan=>2, -align=>"right"},
  503           CGI::submit(-name=>"library_advanced", -value=>"Advanced Search"))
  504     ),
  505     CGI::Tr(
  506       CGI::td(["Section:",
  507       CGI::popup_menu(-name=> 'library_sections',
  508                   -values=>\@sects,
  509                   -default=> $section_selected,
  510               -onchange=>"submit();return true"
  511         )]),
  512      ),
  513      CGI::Tr(CGI::td({-colspan=>3}, $view_problem_line)),
  514      CGI::Tr(CGI::td({-colspan=>3, -align=>"center"}, $count_line)),
  515      CGI::end_table(),
  516    ));
  517 
  518 }
  519 
  520 sub browse_library_panel2adv {
  521   my $self = shift;
  522   my $r = $self->r;
  523   my $ce = $r->ce;
  524   my $right_button_style = "width: 18ex";
  525 
  526   my @subjs = WeBWorK::Utils::ListingDB::getAllDBsubjects($r);
  527   if(! grep { $_ eq $r->param('library_subjects') } @subjs) {
  528     $r->param('library_subjects', '');
  529   }
  530   unshift @subjs, LIB2_DATA->{dbsubject}{all};
  531 
  532   my @chaps = WeBWorK::Utils::ListingDB::getAllDBchapters($r);
  533   if(! grep { $_ eq $r->param('library_chapters') } @chaps) {
  534     $r->param('library_chapters', '');
  535   }
  536   unshift @chaps, LIB2_DATA->{dbchapter}{all};
  537 
  538   my @sects = WeBWorK::Utils::ListingDB::getAllDBsections($r);
  539   if(! grep { $_ eq $r->param('library_sections') } @sects) {
  540     $r->param('library_sections', '');
  541   }
  542   unshift @sects, LIB2_DATA->{dbsection}{all};
  543 
  544   my $texts = WeBWorK::Utils::ListingDB::getDBTextbooks($r);
  545   my @textarray = map { $_->[0] }  @{$texts};
  546   my %textlabels = ();
  547   for my $ta (@{$texts}) {
  548     $textlabels{$ta->[0]} = $ta->[1]." by ".$ta->[2]." (edition ".$ta->[3].")";
  549   }
  550   if(! grep { $_ eq $r->param('library_textbook') } @textarray) {
  551     $r->param('library_textbook', '');
  552   }
  553   unshift @textarray, LIB2_DATA->{textbook}{all};
  554   my $atb = LIB2_DATA->{textbook}{all}; $textlabels{$atb} = LIB2_DATA->{textbook}{all};
  555 
  556   my $textchap_ref = WeBWorK::Utils::ListingDB::getDBTextbooks($r, 'textchapter');
  557   my @textchaps = map { $_->[0] } @{$textchap_ref};
  558   if(! grep { $_ eq $r->param('library_textchapter') } @textchaps) {
  559     $r->param('library_textchapter', '');
  560   }
  561   unshift @textchaps, LIB2_DATA->{textchapter}{all};
  562 
  563   my $textsec_ref = WeBWorK::Utils::ListingDB::getDBTextbooks($r, 'textsection');
  564   my @textsecs = map { $_->[0] } @{$textsec_ref};
  565   if(! grep { $_ eq $r->param('library_textsection') } @textsecs) {
  566     $r->param('library_textsection', '');
  567   }
  568   unshift @textsecs, LIB2_DATA->{textsection}{all};
  569 
  570   my %selected = ();
  571   for my $j (qw( dbsection dbchapter dbsubject textbook textchapter textsection )) {
  572     $selected{$j} = $r->param(LIB2_DATA->{$j}{name}) || LIB2_DATA->{$j}{all};
  573   }
  574 
  575   my $text_popup = CGI::popup_menu(-name => 'library_textbook',
  576                    -values =>\@textarray,
  577                    -labels => \%textlabels,
  578                    -default=>$selected{textbook},
  579                    -onchange=>"submit();return true");
  580 
  581 
  582   my $library_keywords = $r->param('library_keywords') || '';
  583 
  584   my $view_problem_line = view_problems_line('lib_view', 'View Problems', $self->r);
  585 
  586   my $count_line = WeBWorK::Utils::ListingDB::countDBListings($r);
  587   if($count_line==0) {
  588     $count_line = "There are no matching pg files";
  589   } else {
  590     $count_line = "There are $count_line matching WeBWorK problem files";
  591   }
  592 
  593   print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"},
  594     CGI::hidden(-name=>"library_is_basic", -default=>[2]),
  595     CGI::start_table({-width=>"100%"}),
  596     # Html done by hand since it is temporary
  597     CGI::Tr(CGI::td({-colspan=>4, -align=>"center"}, 'All Selected Constraints Joined by "And"')),
  598     CGI::Tr(
  599       CGI::td(["Subject:",
  600         CGI::popup_menu(-name=> 'library_subjects',
  601                       -values=>\@subjs,
  602                       -default=> $selected{dbsubject},
  603                        -onchange=>"submit();return true"
  604         )]),
  605       CGI::td({-colspan=>2, -align=>"right"},
  606         CGI::submit(-name=>"lib_select_subject", -value=>"Update Menus",
  607           -style=> $right_button_style))),
  608     CGI::Tr(
  609       CGI::td(["Chapter:",
  610         CGI::popup_menu(-name=> 'library_chapters',
  611                       -values=>\@chaps,
  612                       -default=> $selected{dbchapter},
  613                        -onchange=>"submit();return true"
  614         )]),
  615       CGI::td({-colspan=>2, -align=>"right"},
  616           CGI::submit(-name=>"library_reset", -value=>"Reset",
  617           -style=>$right_button_style))
  618     ),
  619     CGI::Tr(
  620       CGI::td(["Section:",
  621       CGI::popup_menu(-name=> 'library_sections',
  622                   -values=>\@sects,
  623                   -default=> $selected{dbsection},
  624               -onchange=>"submit();return true"
  625         )]),
  626       CGI::td({-colspan=>2, -align=>"right"},
  627           CGI::submit(-name=>"library_basic", -value=>"Basic Search",
  628           -style=>$right_button_style))
  629      ),
  630      CGI::Tr(
  631       CGI::td(["Textbook:", $text_popup]),
  632      ),
  633      CGI::Tr(
  634       CGI::td(["Text chapter:",
  635       CGI::popup_menu(-name=> 'library_textchapter',
  636                   -values=>\@textchaps,
  637                   -default=> $selected{textchapter},
  638               -onchange=>"submit();return true"
  639         )]),
  640      ),
  641      CGI::Tr(
  642       CGI::td(["Text section:",
  643       CGI::popup_menu(-name=> 'library_textsection',
  644                   -values=>\@textsecs,
  645                   -default=> $selected{textsection},
  646               -onchange=>"submit();return true"
  647         )]),
  648      ),
  649      CGI::Tr(CGI::td("Keywords:"),CGI::td({-colspan=>2},
  650        CGI::textfield(-name=>"library_keywords",
  651               -default=>$library_keywords,
  652               -override=>1,
  653               -size=>40))),
  654      CGI::Tr(CGI::td({-colspan=>3}, $view_problem_line)),
  655      CGI::Tr(CGI::td({-colspan=>3, -align=>"center"}, $count_line)),
  656      CGI::end_table(),
  657    ));
  658 
  659 }
  660 
  661 
  662 #####  Version 4 is the set definition file panel
  663 
  664 sub browse_setdef_panel {
  665   my $self = shift;
  666   my $r = $self->r;
  667   my $ce = $r->ce;
  668   my $library_selected = shift;
  669   my $default_value = "Select a Set Definition File";
  670   my @list_of_set_defs = get_set_defs($ce->{courseDirs}{templates});
  671   if(scalar(@list_of_set_defs) == 0) {
  672     @list_of_set_defs = (NO_LOCAL_SET_STRING);
  673   } elsif (not $library_selected or $library_selected eq $default_value) {
  674     unshift @list_of_set_defs, $default_value;
  675     $library_selected = $default_value;
  676   }
  677   my $view_problem_line = view_problems_line('view_setdef_set', 'View Problems', $self->r);
  678   my $popupetc = CGI::popup_menu(-name=> 'library_sets',
  679                                 -values=>\@list_of_set_defs,
  680                                 -default=> $library_selected).
  681     CGI::br().  $view_problem_line;
  682   if($list_of_set_defs[0] eq NO_LOCAL_SET_STRING) {
  683     $popupetc = "there are no set definition files in this course to look at."
  684   }
  685   print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "Browse from: ",
  686     $popupetc
  687   ));
  688 }
  689 
  690 sub make_top_row {
  691   my $self = shift;
  692   my $r = $self->r;
  693   my $ce = $r->ce;
  694   my %data = @_;
  695 
  696   my $list_of_local_sets = $data{all_db_sets};
  697   my $have_local_sets = scalar(@$list_of_local_sets);
  698   my $browse_which = $data{browse_which};
  699   my $library_selected = $r->param('library_sets');
  700   my $set_selected = $r->param('local_sets');
  701 
  702   my ($dis1, $dis2, $dis3, $dis4) = ("","","", "");
  703   $dis1 =  '-disabled' if($browse_which eq 'browse_library');
  704   $dis2 =  '-disabled' if($browse_which eq 'browse_local');
  705   $dis3 =  '-disabled' if($browse_which eq 'browse_mysets');
  706   $dis4 =  '-disabled' if($browse_which eq 'browse_setdefs');
  707 
  708   ##  Make buttons for additional problem libraries
  709   my $libs = '';
  710   foreach my $lib (sort(keys(%problib))) {
  711     $libs .= ' '. CGI::submit(-name=>"browse_$lib", -value=>$problib{$lib},
  712                                  ($browse_which eq "browse_$lib")? '-disabled': '')
  713       if (-d "$ce->{courseDirs}{templates}/$lib");
  714   }
  715   $libs = CGI::br()."or Problems from".$libs if $libs ne '';
  716 
  717   my $these_widths = "width: 23ex";
  718 
  719   if($have_local_sets ==0) {
  720     $list_of_local_sets = [NO_LOCAL_SET_STRING];
  721   } elsif (not $set_selected or $set_selected eq SELECT_SET_STRING) {
  722     unshift @{$list_of_local_sets}, SELECT_SET_STRING;
  723     $set_selected = SELECT_SET_STRING;
  724   }
  725   my $myjs = 'document.mainform.selfassign.value=confirm("Should I assign the new set to you now?\nUse OK for yes and Cancel for no.");true;';
  726 
  727   print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "Add problems to ",
  728     CGI::b("Target Set: "),
  729     CGI::popup_menu(-name=> 'local_sets',
  730             -values=>$list_of_local_sets,
  731             -default=> $set_selected),
  732     CGI::submit(-name=>"edit_local", -value=>"Edit Target Set"),
  733     CGI::hidden(-name=>"selfassign", -default=>[0]).
  734     CGI::br(),
  735     CGI::br(),
  736     CGI::submit(-name=>"new_local_set", -value=>"Create a New Set in This Course:",
  737     -onclick=>$myjs
  738     ),
  739     "  ",
  740     CGI::textfield(-name=>"new_set_name",
  741              -default=>"Name for new set here",
  742              -override=>1, -size=>30),
  743   ));
  744 
  745   print CGI::Tr(CGI::td({-bgcolor=>"black"}));
  746 
  747   # Tidy this list up since it is used in two different places
  748   if ($list_of_local_sets->[0] eq SELECT_SET_STRING) {
  749     shift @{$list_of_local_sets};
  750   }
  751 
  752   print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"center"},
  753     "Browse ",
  754     CGI::submit(-name=>"browse_library", -value=>"Problem Library", -style=>$these_widths, $dis1),
  755     CGI::submit(-name=>"browse_local", -value=>"Local Problems", -style=>$these_widths, $dis2),
  756     CGI::submit(-name=>"browse_mysets", -value=>"From This Course", -style=>$these_widths, $dis3),
  757     CGI::submit(-name=>"browse_setdefs", -value=>"Set Definition Files", -style=>$these_widths, $dis4),
  758     $libs,
  759   ));
  760 
  761   #print CGI::Tr(CGI::td({-bgcolor=>"black"}));
  762   print CGI::hr();
  763 
  764   if ($browse_which eq 'browse_local') {
  765     $self->browse_local_panel($library_selected);
  766   } elsif ($browse_which eq 'browse_mysets') {
  767     $self->browse_mysets_panel($library_selected, $list_of_local_sets);
  768   } elsif ($browse_which eq 'browse_library') {
  769     $self->browse_library_panel();
  770   } elsif ($browse_which eq 'browse_setdefs') {
  771     $self->browse_setdef_panel($library_selected);
  772   } else { ## handle other problem libraries
  773     $self->browse_local_panel($library_selected,$browse_which);
  774   }
  775 
  776   print CGI::Tr(CGI::td({-bgcolor=>"black"}));
  777 
  778   print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"center"},
  779     CGI::start_table({-border=>"0"}),
  780     CGI::Tr( CGI::td({ -align=>"center"},
  781       CGI::submit(-name=>"select_all", -style=>$these_widths,
  782                   -value=>"Mark All For Adding"),
  783       CGI::submit(-name=>"select_none", -style=>$these_widths,
  784                   -value=>"Clear All Marks"),
  785     )),
  786     CGI::Tr(CGI::td(
  787       CGI::submit(-name=>"update", -style=>$these_widths. "; font-weight:bold",
  788                   -value=>"Update Set"),
  789     CGI::submit(-name=>"rerandomize",
  790                 -style=>$these_widths,
  791                 -value=>"Rerandomize"),
  792     CGI::submit(-name=>"cleardisplay",
  793                 -style=>$these_widths,
  794                 -value=>"Clear Problem Display")
  795   )),
  796   CGI::end_table()));
  797 }
  798 
  799 sub make_data_row {
  800   my $self = shift;
  801   my $sourceFileName = shift;
  802   my $pg = shift;
  803   my $cnt = shift;
  804   my $mark = shift || 0;
  805 
  806   $sourceFileName =~ s|^./||; # clean up top ugliness
  807 
  808   my $urlpath = $self->r->urlpath;
  809   my $problem_output = $pg->{flags}->{error_flag} ?
  810     CGI::div({class=>"ResultsWithError"}, CGI::em("This problem produced an error"))
  811     : CGI::div({class=>"RenderSolo"}, $pg->{body_text});
  812   $problem_output .= $pg->{flags}->{comment} if($pg->{flags}->{comment});
  813 
  814 
  815   #if($self->{r}->param('browse_which') ne 'browse_library') {
  816   my $problem_seed = $self->{r}->param('problem_seed') || 0;
  817   my $edit_link = CGI::a({href=>$self->systemLink(
  818      $urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::PGProblemEditor",
  819         courseID =>$urlpath->arg("courseID"),
  820         setID=>"Undefined_Set",
  821         problemID=>"1"),
  822       params=>{sourceFilePath => "$sourceFileName", problemSeed=> $problem_seed}
  823       )}, "Edit it" );
  824 
  825   my $displayMode = $self->r->param("mydisplayMode");
  826   $displayMode = $self->r->ce->{pg}->{options}->{displayMode}
  827     if not defined $displayMode or $displayMode eq "None";
  828   my $try_link = CGI::a({href=>$self->systemLink(
  829     $urlpath->newFromModule("WeBWorK::ContentGenerator::Problem",
  830       courseID =>$urlpath->arg("courseID"),
  831       setID=>"Undefined_Set",
  832       problemID=>"1"),
  833     params =>{
  834       effectiveUser => scalar($self->r->param('user')),
  835       editMode => "SetMaker",
  836       problemSeed=> $problem_seed,
  837       sourceFilePath => "$sourceFileName",
  838       displayMode => $displayMode,
  839     }
  840   )}, "Try it");
  841 
  842   my %add_box_data = ( -name=>"trial$cnt",-value=>1,-label=>"Add this problem to the current set on the next update");
  843   if($mark & SUCCESS) {
  844     $add_box_data{ -label } .= " (just added this problem)";
  845   } elsif($mark & ADDED) {
  846     $add_box_data{ -checked } = 1;
  847   }
  848 
  849   print CGI::Tr({-align=>"left"}, CGI::td(
  850     CGI::div({-style=>"background-color: #DDDDDD; margin: 0px auto"},
  851       CGI::span({-style=>"float:left ; text-align: left"},"File name: $sourceFileName "),
  852       CGI::span({-style=>"float:right ; text-align: right"}, $edit_link, " ", $try_link)
  853     ), CGI::br(),
  854     CGI::checkbox(-name=>"hideme$cnt",-value=>1,-label=>"Don't show this problem on the next update"),
  855     CGI::br(),
  856     CGI::checkbox((%add_box_data)),
  857     CGI::hidden(-name=>"filetrial$cnt", -default=>[$sourceFileName]).
  858     CGI::p($problem_output),
  859   ));
  860 }
  861 
  862 sub clear_default {
  863   my $r = shift;
  864   my $param = shift;
  865   my $default = shift;
  866   my $newvalue = $r->param($param) || '';
  867   $newvalue = '' if($newvalue eq $default);
  868   $r->param($param, $newvalue);
  869 }
  870 
  871 sub pre_header_initialize {
  872   my ($self) = @_;
  873   my $r = $self->r;
  874   ## For all cases, lets set some things
  875   $self->{error}=0;
  876   my $ce = $r->ce;
  877   my $db = $r->db;
  878   my $maxShown = $r->param('max_shown') || MAX_SHOW_DEFAULT;
  879   $maxShown = 10000000 if($maxShown eq 'All'); # let's hope there aren't more
  880   my $library_basic = $r->param('library_is_basic') || 1;
  881 
  882   ## Fix some parameters
  883   clear_default($r,'library_subjects', ALL_SUBJECTS);
  884   clear_default($r,'library_chapters', ALL_CHAPTERS);
  885   clear_default($r,'library_sections', ALL_SECTIONS);
  886   clear_default($r,'library_textbook', ALL_TEXTBOOKS);
  887 
  888   ##  These directories will have individual buttons
  889   %problib = %{$ce->{courseFiles}{problibs}} if $ce->{courseFiles}{problibs};
  890 
  891   my $userName = $r->param('user');
  892   my $user = $db->getUser($userName); # checked
  893   die "record for user $userName (real user) does not exist."
  894     unless defined $user;
  895   my $authz = $r->authz;
  896   unless ($authz->hasPermissions($userName, "modify_problem_sets")) {
  897     return(""); # Error message already produced in the body
  898   }
  899 
  900   ## Now one action we have to deal with here
  901   if ($r->param('edit_local')) {
  902     my $urlpath = $r->urlpath;
  903     my $db = $r->db;
  904     my $checkset = $db->getGlobalSet($r->param('local_sets'));
  905     if (not defined($checkset)) {
  906       $self->{error} = 1;
  907       $self->addbadmessage('You need to select a "Target Set" before you can edit it.');
  908     } else {
  909       my $page = $urlpath->newFromModule('WeBWorK::ContentGenerator::Instructor::ProblemSetDetail', setID=>$r->param('local_sets'), courseID=>$urlpath->arg("courseID"));
  910       my $url = $self->systemLink($page);
  911       $self->reply_with_redirect($url);
  912     }
  913   }
  914 
  915   ## Next, lots of set up so that errors can be reported with message()
  916 
  917   ############# List of problems we have already printed
  918 
  919   $self->{past_problems} = get_past_problem_files($r);
  920   # if we don't end up reusing problems, this will be wiped out
  921   # if we do redisplay the same problems, we must adjust this accordingly
  922   my @past_marks = map {$_->[1]} @{$self->{past_problems}};
  923   my $none_shown = scalar(@{$self->{past_problems}})==0;
  924   my @pg_files=();
  925   my $use_previous_problems = 1;
  926   my $first_shown = $r->param('first_shown') || 0;
  927   my $last_shown = $r->param('last_shown');
  928   if (not defined($last_shown)) {
  929     $last_shown = -1;
  930   }
  931   my @all_past_list = (); # these are include requested, but not shown
  932   my $j = 0;
  933   while (defined($r->param("all_past_list$j"))) {
  934     push @all_past_list, $r->param("all_past_list$j");
  935     $j++;
  936   }
  937 
  938   ############# Default of which problem selector to display
  939 
  940   my $browse_which = $r->param('browse_which') || 'browse_local';
  941 
  942   my $problem_seed = $r->param('problem_seed') || 0;
  943   $r->param('problem_seed', $problem_seed); # if it wasn't defined before
  944 
  945   ## check for problem lib buttons
  946   my $browse_lib = '';
  947   foreach my $lib (keys %problib) {
  948     if ($r->param("browse_$lib")) {
  949       $browse_lib = "browse_$lib";
  950       last;
  951     }
  952   }
  953 
  954   ########### Start the logic through if elsif elsif ...
  955 
  956   ##### Asked to browse certain problems
  957   if ($browse_lib ne '') {
  958     $browse_which = $browse_lib;
  959     $r->param('library_sets', "");
  960     $use_previous_problems = 0; @pg_files = (); ## clear old problems
  961   } elsif ($r->param('browse_library')) {
  962     $browse_which = 'browse_library';
  963     $r->param('library_sets', "");
  964     $use_previous_problems = 0; @pg_files = (); ## clear old problems
  965   } elsif ($r->param('browse_local')) {
  966     $browse_which = 'browse_local';
  967     $r->param('library_sets', "");
  968     $use_previous_problems = 0; @pg_files = (); ## clear old problems
  969   } elsif ($r->param('browse_mysets')) {
  970     $browse_which = 'browse_mysets';
  971     $r->param('library_sets', "");
  972     $use_previous_problems = 0; @pg_files = (); ## clear old problems
  973   } elsif ($r->param('browse_setdefs')) {
  974     $browse_which = 'browse_setdefs';
  975     $r->param('library_sets', "");
  976     $use_previous_problems = 0; @pg_files = (); ## clear old problems
  977 
  978     ##### Change the seed value
  979 
  980   } elsif ($r->param('rerandomize')) {
  981     $problem_seed++;
  982     $r->param('problem_seed', $problem_seed);
  983     $self->addbadmessage('Changing the problem seed for display, but there are no problems showing.') if $none_shown;
  984 
  985     ##### Clear the display
  986 
  987   } elsif ($r->param('cleardisplay')) {
  988     @pg_files = ();
  989     $use_previous_problems=0;
  990     $self->addbadmessage('The display was already cleared.') if $none_shown;
  991 
  992     ##### View problems selected from the local list
  993 
  994   } elsif ($r->param('view_local_set')) {
  995 
  996     my $set_to_display = $r->param('library_sets');
  997     if (not defined($set_to_display) or $set_to_display eq SELECT_LOCAL_STRING or $set_to_display eq "Found no directories containing problems") {
  998       $self->addbadmessage('You need to select a set to view.');
  999     } else {
 1000       $set_to_display = '.' if $set_to_display eq MY_PROBLEMS;
 1001       $set_to_display = substr($browse_which,7) if $set_to_display eq MAIN_PROBLEMS;
 1002       @pg_files = list_pg_files($ce->{courseDirs}->{templates},
 1003         "$set_to_display");
 1004       $use_previous_problems=0;
 1005     }
 1006 
 1007     ##### View problems selected from the a set in this course
 1008 
 1009   } elsif ($r->param('view_mysets_set')) {
 1010 
 1011     my $set_to_display = $r->param('library_sets');
 1012     if (not defined($set_to_display)
 1013         or $set_to_display eq "Select a Homework Set"
 1014         or $set_to_display eq NO_LOCAL_SET_STRING) {
 1015       $self->addbadmessage("You need to select a set from this course to view.");
 1016     } else {
 1017       my @problemList = $db->listGlobalProblems($set_to_display);
 1018       my $problem;
 1019       @pg_files=();
 1020       for $problem (@problemList) {
 1021         my $problemRecord = $db->getGlobalProblem($set_to_display, $problem); # checked
 1022         die "global $problem for set $set_to_display not found." unless
 1023           $problemRecord;
 1024         push @pg_files, $problemRecord->source_file;
 1025 
 1026       }
 1027       $use_previous_problems=0;
 1028     }
 1029 
 1030     ##### View from the library database
 1031 
 1032   } elsif ($r->param('lib_view')) {
 1033 
 1034     @pg_files=();
 1035     my @dbsearch = WeBWorK::Utils::ListingDB::getSectionListings($r);
 1036     my ($result, $tolibpath);
 1037     for $result (@dbsearch) {
 1038       $tolibpath = "Library/$result->{path}/$result->{filename}";
 1039 
 1040       ## Too clunky!!!!
 1041       push @pg_files, $tolibpath;
 1042     }
 1043     $use_previous_problems=0;
 1044 
 1045     ##### View a set from a set*.def
 1046 
 1047   } elsif ($r->param('view_setdef_set')) {
 1048 
 1049     my $set_to_display = $r->param('library_sets');
 1050     if (not defined($set_to_display)
 1051         or $set_to_display eq "Select a Set Definition File"
 1052         or $set_to_display eq NO_LOCAL_SET_STRING) {
 1053       $self->addbadmessage("You need to select a set from this course to view.");
 1054     } else {
 1055       @pg_files= $self->read_set_def($set_to_display);
 1056     }
 1057     $use_previous_problems=0;
 1058 
 1059     ##### Edit the current local problem set
 1060 
 1061   } elsif ($r->param('edit_local')) { ## Jump to set edit page
 1062 
 1063     ; # already handled
 1064 
 1065 
 1066     ##### Make a new local problem set
 1067 
 1068   } elsif ($r->param('new_local_set')) {
 1069     if ($r->param('new_set_name') !~ /^[\w .-]*$/) {
 1070       $self->addbadmessage("The name ".$r->param('new_set_name')." is not a valid set name.  Use only letters, digits, -, _, and .");
 1071     } else {
 1072       my $newSetName = $r->param('new_set_name');
 1073       # if we want to munge the input set name, do it here
 1074       $newSetName =~ s/\s/_/g;
 1075       $r->param('local_sets',$newSetName);
 1076       my $newSetRecord   = $db->getGlobalSet($newSetName);
 1077       if (defined($newSetRecord)) {
 1078   $self->addbadmessage("The set name $newSetName is already in use.  Pick a different name if you would like to start a new set.");
 1079       } else {      # Do it!
 1080         $newSetRecord = $db->{set}->{record}->new();
 1081         $newSetRecord->set_id($newSetName);
 1082         $newSetRecord->set_header("");
 1083         $newSetRecord->hardcopy_header("");
 1084         $newSetRecord->open_date(time()+60*60*24*7); # in one week
 1085         $newSetRecord->due_date(time()+60*60*24*7*2); # in two weeks
 1086         $newSetRecord->answer_date(time()+60*60*24*7*3); # in three weeks
 1087         eval {$db->addGlobalSet($newSetRecord)};
 1088         if ($@) {
 1089           $self->addbadmessage("Problem creating set $newSetName<br> $@");
 1090         } else {
 1091           $self->addgoodmessage("Set $newSetName has been created.");
 1092           my $selfassign = $r->param('selfassign') || "";
 1093           $selfassign = "" if($selfassign =~ /false/i); # deal with javascript false
 1094           if($selfassign) {
 1095             $self->assignSetToUser($userName, $newSetRecord);
 1096             $self->addgoodmessage("Set $newSetName was assigned to $userName.");
 1097           }
 1098         }
 1099       }
 1100     }
 1101 
 1102     ##### Add selected problems to the current local set
 1103 
 1104   } elsif ($r->param('update')) {
 1105     ## first handle problems to be added before we hide them
 1106     my($localSet, @selected);
 1107 
 1108     @pg_files = grep {($_->[1] & ADDED) != 0 } @{$self->{past_problems}};
 1109     @selected = map {$_->[0]} @pg_files;
 1110 
 1111     my @action_files = grep {$_->[1] > 0 } @{$self->{past_problems}};
 1112     # There are now good reasons to do an update without selecting anything.
 1113     #if(scalar(@action_files) == 0) {
 1114     #  $self->addbadmessage('Update requested, but no problems were marked.');
 1115     #}
 1116 
 1117     if (scalar(@selected)>0) {  # if some are to be added, they need a place to go
 1118       $localSet = $r->param('local_sets');
 1119       if (not defined($localSet) or
 1120           $localSet eq SELECT_SET_STRING or
 1121     $localSet eq NO_LOCAL_SET_STRING) {
 1122   $self->addbadmessage('You are trying to add problems to something, but you did not select a "Target Set" name as a target.');
 1123       } else {
 1124   my $newSetRecord  = $db->getGlobalSet($localSet);
 1125   if (not defined($newSetRecord)) {
 1126     $self->addbadmessage("You are trying to add problems to $localSet, but that set does not seem to exist!  I bet you used your \"Back\" button.");
 1127   } else {
 1128     my $addcount = add_selected($self, $db, $localSet);
 1129     if($addcount > 0) {
 1130       $self->addgoodmessage("Added $addcount problem".(($addcount>1)?'s':'').
 1131         " to $localSet.");
 1132     }
 1133   }
 1134       }
 1135     }
 1136     ## now handle problems to be hidden
 1137 
 1138     ## only keep the ones which are not hidden
 1139     @pg_files = grep {($_->[1] & HIDDEN) ==0 } @{$self->{past_problems}};
 1140     @past_marks = map {$_->[1]} @pg_files;
 1141     @pg_files = map {$_->[0]} @pg_files;
 1142     @all_past_list = (@all_past_list[0..($first_shown-1)],
 1143           @pg_files,
 1144           @all_past_list[($last_shown+1)..(scalar(@all_past_list)-1)]);
 1145     $last_shown = $first_shown+$maxShown -1;
 1146     $last_shown = (scalar(@all_past_list)-1) if($last_shown>=scalar(@all_past_list));
 1147 
 1148   } elsif ($r->param('next_page')) {
 1149     $first_shown = $last_shown+1;
 1150     $last_shown = $first_shown+$maxShown-1;
 1151     $last_shown = (scalar(@all_past_list)-1) if($last_shown>=scalar(@all_past_list));
 1152     @past_marks = ();
 1153   } elsif ($r->param('prev_page')) {
 1154     $last_shown = $first_shown-1;
 1155     $first_shown = $last_shown - $maxShown+1;
 1156 
 1157     $first_shown = 0 if($first_shown<0);
 1158     @past_marks = ();
 1159 
 1160   } elsif ($r->param('select_all')) {
 1161     @past_marks = map {1} @past_marks;
 1162   } elsif ($r->param('library_basic')) {
 1163     $library_basic = 1;
 1164     for my $jj (qw(textchapter textsection textbook)) {
 1165       $r->param('library_'.$jj,'');
 1166     }
 1167   } elsif ($r->param('library_advanced')) {
 1168     $library_basic = 2;
 1169   } elsif ($r->param('library_reset')) {
 1170     for my $jj (qw(chapters sections subjects textbook keywords)) {
 1171       $r->param('library_'.$jj,'');
 1172     }
 1173   } elsif ($r->param('select_none')) {
 1174     @past_marks = ();
 1175 
 1176     ##### No action requested, probably our first time here
 1177 
 1178   } else {
 1179     #my $c = $r->connection;
 1180     #print "Debug info: ". $r->get_remote_host ."<p>".  $c->remote_ip ;
 1181     ;
 1182   }       ##### end of the if elsif ...
 1183 
 1184 
 1185   ############# List of local sets
 1186 
 1187   my @all_db_sets = $db->listGlobalSets;
 1188   @all_db_sets = sortByName(undef, @all_db_sets);
 1189 
 1190   if ($use_previous_problems) {
 1191     @pg_files = @all_past_list;
 1192   } else {
 1193     $first_shown = 0;
 1194     $last_shown = scalar(@pg_files)<$maxShown ? scalar(@pg_files) : $maxShown;
 1195     $last_shown--;    # to make it an array index
 1196     @past_marks = ();
 1197   }
 1198   ############# Now store data in self for retreival by body
 1199   $self->{first_shown} = $first_shown;
 1200   $self->{last_shown} = $last_shown;
 1201   $self->{browse_which} = $browse_which;
 1202   $self->{problem_seed} = $problem_seed;
 1203   $self->{pg_files} = \@pg_files;
 1204   $self->{past_marks} = \@past_marks;
 1205   $self->{all_db_sets} = \@all_db_sets;
 1206   $self->{library_basic} = $library_basic;
 1207 }
 1208 
 1209 
 1210 sub title {
 1211   return "Library Browser";
 1212 }
 1213 
 1214 # hide view options panel since it distracts from SetMaker's built-in view options
 1215 sub options {
 1216   return "";
 1217 }
 1218 
 1219 sub body {
 1220   my ($self) = @_;
 1221 
 1222   my $r = $self->r;
 1223   my $ce = $r->ce;    # course environment
 1224   my $db = $r->db;    # database
 1225   my $j;      # garden variety counter
 1226 
 1227   my $userName = $r->param('user');
 1228 
 1229   my $user = $db->getUser($userName); # checked
 1230   die "record for user $userName (real user) does not exist."
 1231     unless defined $user;
 1232 
 1233   ### Check that this is a professor
 1234   my $authz = $r->authz;
 1235   unless ($authz->hasPermissions($userName, "modify_problem_sets")) {
 1236     print "User $userName returned " .
 1237       $authz->hasPermissions($user, "modify_problem_sets") .
 1238   " for permission";
 1239     return(CGI::div({class=>'ResultsWithError'},
 1240     CGI::em("You are not authorized to access the Instructor tools.")));
 1241   }
 1242 
 1243   ##########  Extract information computed in pre_header_initialize
 1244 
 1245   my $first_shown = $self->{first_shown};
 1246   my $last_shown = $self->{last_shown};
 1247   my $browse_which = $self->{browse_which};
 1248   my $problem_seed = $self->{problem_seed};
 1249   my @pg_files = @{$self->{pg_files}};
 1250   my @all_db_sets = @{$self->{all_db_sets}};
 1251 
 1252   my @pg_html=($last_shown>=$first_shown) ?
 1253     renderProblems(r=> $r,
 1254                    user => $user,
 1255                    problem_list => [@pg_files[$first_shown..$last_shown]],
 1256                    displayMode => $r->param('mydisplayMode')) : ();
 1257 
 1258   ##########  Top part
 1259   print CGI::startform({-method=>"POST", -action=>$r->uri, -name=>'mainform'}),
 1260     $self->hidden_authen_fields,
 1261       '<div align="center">',
 1262   CGI::start_table({-border=>2});
 1263   $self->make_top_row('all_db_sets'=>\@all_db_sets,
 1264          'browse_which'=> $browse_which);
 1265   print CGI::hidden(-name=>'browse_which', -default=>[$browse_which]),
 1266     CGI::hidden(-name=>'problem_seed', -default=>[$problem_seed]);
 1267   for ($j = 0 ; $j < scalar(@pg_files) ; $j++) {
 1268     print CGI::hidden(-name=>"all_past_list$j", -default=>$pg_files[$j]);
 1269   }
 1270 
 1271   print CGI::hidden(-name=>'first_shown', -default=>[$first_shown]);
 1272   print CGI::hidden(-name=>'last_shown', -default=>[$last_shown]);
 1273 
 1274 
 1275   ########## Now print problems
 1276   my $jj;
 1277   for ($jj=0; $jj<scalar(@pg_html); $jj++) {
 1278     $pg_files[$jj] =~ s|^$ce->{courseDirs}->{templates}/?||;
 1279     $self->make_data_row($pg_files[$jj+$first_shown], $pg_html[$jj], $jj+1, $self->{past_marks}->[$jj]);
 1280   }
 1281 
 1282   ########## Finish things off
 1283   print CGI::end_table();
 1284   print '</div>';
 1285   #  if($first_shown>0 or (1+$last_shown)<scalar(@pg_files)) {
 1286   my ($next_button, $prev_button) = ("", "");
 1287   if ($first_shown > 0) {
 1288     $prev_button = CGI::submit(-name=>"prev_page", -style=>"width:15ex",
 1289              -value=>"Previous page");
 1290   }
 1291   if ((1+$last_shown)<scalar(@pg_files)) {
 1292     $next_button = CGI::submit(-name=>"next_page", -style=>"width:15ex",
 1293              -value=>"Next page");
 1294   }
 1295   if (scalar(@pg_files)>0) {
 1296     print CGI::p(($first_shown+1)."-".($last_shown+1)." of ".scalar(@pg_files).
 1297      " shown.", $prev_button, " ", $next_button);
 1298   }
 1299   #  }
 1300   print CGI::endform(), "\n";
 1301 
 1302   return "";
 1303 }
 1304 
 1305 =head1 AUTHOR
 1306 
 1307 Written by John Jones, jj (at) asu.edu.
 1308 
 1309 =cut
 1310 
 1311 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9