[system] / branches / rel-2-4-dev / webwork-modperl / lib / WeBWorK / ContentGenerator / Instructor / SetMaker.pm Repository:
ViewVC logotype

View of /branches/rel-2-4-dev/webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3427 - (download) (as text) (annotate)
Fri Jul 29 20:45:41 2005 UTC (7 years, 10 months ago) by jj
Original Path: trunk/webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm
File size: 42564 byte(s)
More improvements on library - advanced panel.  Still a work in progress.

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9