[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 3357 - (download) (as text) (annotate)
Tue Jul 5 18:56:12 2005 UTC (7 years, 10 months ago) by sh002i
Original Path: trunk/webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm
File size: 33863 byte(s)
renamed "problem set" to "homework set" in page content.
closes bug #797.

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: webwork2/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm,v 1.34 2004/10/25 22:40:19 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 
   35 require WeBWorK::Utils::ListingDB;
   36 
   37 use constant MAX_SHOW_DEFAULT => 20;
   38 use constant NO_LOCAL_SET_STRING => 'There are no local sets yet';
   39 use constant SELECT_SET_STRING => 'Select a Set for This Course';
   40 use constant SELECT_LOCAL_STRING => 'Select a Problem Collection';
   41 use constant MY_PROBLEMS => '  My Problems  ';
   42 use constant MAIN_PROBLEMS => '  Main Problems  ';
   43 
   44 ## Flags for operations on files
   45 
   46 use constant ADDED => 1;
   47 use constant HIDDEN => (1 << 1);
   48 use constant SUCCESS => (1 << 2);
   49 
   50 ##  for additional problib buttons
   51 my %problib;  ## filled in in global.conf
   52 my %ignoredir = (
   53   '.' => 1, '..' => 1, 'Library' => 1,
   54   'headers' => 1, 'macros' => 1, 'email' => 1,
   55 );
   56 
   57 ##
   58 ## This is for searching the disk for directories containing pg files.
   59 ## to make the recursion work, this returns an array where the first
   60 ## item is the number of pg files in the directory.  The second is a
   61 ## list of directories which contain pg files.
   62 ##
   63 ## If a directory contains only one pg file and at least one other
   64 ## file, the directory is considered to be part of the parent
   65 ## directory (it is probably in a separate directory only because
   66 ## it has auxiliarly files that want to be kept together with the
   67 ## pg file).
   68 ##
   69 ## If a directory has a file named "=library-ignore", it is never
   70 ## included in the directory menu.  If a directory contains a file
   71 ## called "=library-combine-up", then its pg are included with those
   72 ## in the parent directory (and the directory does not appear in the
   73 ## menu).  If it has a file called "=library-no-combine" then it is
   74 ## always listed as a separate directory even if it contains only one
   75 ## pg file.
   76 ##
   77 
   78 sub get_library_sets {
   79   my $top = shift; my $dir =  shift;
   80   # ignore directories that give us an error
   81   my @lis = eval { readDirectory($dir) };
   82   if ($@) {
   83     warn $@;
   84     return (0);
   85   }
   86   return (0) if grep /^=library-ignore$/, @lis;
   87 
   88   my @pgdirs;
   89 
   90   my $pgcount = scalar(grep { m/\.pg$/ and (not m/(Header|-text)\.pg$/) and -f "$dir/$_"} @lis);
   91   my $others = scalar(grep { (!m/\.pg$/ || m/(Header|-text)\.pg$/) &&
   92                               !m/(\.(tmp|bak)|~)$/ && -f "$dir/$_" } @lis);
   93 
   94   my @dirs = grep {!$ignoredir{$_} and -d "$dir/$_"} @lis;
   95   if ($top == 1) {@dirs = grep {!$problib{$_}} @dirs}
   96   foreach my $subdir (@dirs) {
   97     my @results = get_library_sets(0, "$dir/$subdir");
   98     $pgcount += shift @results; push(@pgdirs,@results);
   99   }
  100 
  101   return ($pgcount, @pgdirs) if $top || $pgcount == 0 || grep /^=library-combine-up$/, @lis;
  102   return (0,@pgdirs,$dir) if $pgcount > 1 || $others == 0 || grep /^=library-no-combine$/, @lis;
  103   return ($pgcount, @pgdirs);
  104 }
  105 
  106 sub get_library_pgs {
  107   my $top = shift; my $base = shift; my $dir =  shift;
  108   my @lis = readDirectory("$base/$dir");
  109   return () if grep /^=library-ignore$/, @lis;
  110   return () if !$top && grep /^=library-no-combine$/, @lis;
  111 
  112   my @pgs = grep { m/\.pg$/ and (not m/(Header|-text)\.pg$/) and -f "$base/$dir/$_"} @lis;
  113   my $others = scalar(grep { (!m/\.pg$/ || m/(Header|-text)\.pg$/) &&
  114                               !m/(\.(tmp|bak)|~)$/ && -f "$base/$dir/$_" } @lis);
  115 
  116   my @dirs = grep {!$ignoredir{$_} and -d "$base/$dir/$_"} @lis;
  117   if ($top == 1) {@dirs = grep {!$problib{$_}} @dirs}
  118   foreach my $subdir (@dirs) {push(@pgs, get_library_pgs(0,"$base/$dir",$subdir))}
  119 
  120   return () unless $top || (scalar(@pgs) == 1 && $others) || grep /^=library-combine-up$/, @lis;
  121   return (map {"$dir/$_"} @pgs);
  122 }
  123 
  124 sub list_pg_files {
  125   my ($templates,$dir) = @_;
  126   my $top = ($dir eq '.')? 1 : 2;
  127   my @pgs = get_library_pgs($top,$templates,$dir);
  128   return sortByName(undef,@pgs);
  129 }
  130 
  131 ## go through past page getting a list of identifiers for the problems
  132 ## and whether or not they are selected, and whether or not they should
  133 ## be hidden
  134 
  135 sub get_past_problem_files {
  136   my $r = shift;
  137   my @found=();
  138   my $count =1;
  139   while (defined($r->param("filetrial$count"))) {
  140     my $val = 0;
  141     $val |= ADDED if($r->param("trial$count"));
  142     $val |= HIDDEN if($r->param("hideme$count"));
  143     push @found, [$r->param("filetrial$count"), $val];
  144     $count++;
  145   }
  146   return(\@found);
  147 }
  148 
  149 #### For adding new problems
  150 
  151 sub add_selected {
  152   my $self = shift;
  153   my $db = shift;
  154   my $setName = shift;
  155   my @past_problems = @{$self->{past_problems}};
  156   my @selected = @past_problems;
  157   my (@path, $file, $selected, $freeProblemID);
  158   $freeProblemID = max($db->listGlobalProblems($setName)) + 1;
  159   my $addedcount=0;
  160 
  161   for $selected (@selected) {
  162     if($selected->[1] & ADDED) {
  163       $file = $selected->[0];
  164       my $problemRecord = $self->addProblemToSet(setName => $setName,
  165         sourceFile => $file, problemID => $freeProblemID);
  166       $freeProblemID++;
  167       $self->assignProblemToAllSetUsers($problemRecord);
  168       $selected->[1] |= SUCCESS;
  169       $addedcount++;
  170     }
  171   }
  172   return($addedcount);
  173 }
  174 
  175 
  176 ############# List of sets of problems in templates directory
  177 
  178 sub get_problem_directories {
  179   my $ce = shift;
  180   my $lib = shift;
  181   my $source = $ce->{courseDirs}{templates};
  182   my $main = MY_PROBLEMS; my $isTop = 1;
  183   if ($lib) {$source .= "/$lib"; $main = MAIN_PROBLEMS; $isTop = 2}
  184   my @all_problem_directories = get_library_sets($isTop, $source);
  185   my $includetop = shift @all_problem_directories;
  186   my $j;
  187   for ($j=0; $j<scalar(@all_problem_directories); $j++) {
  188     $all_problem_directories[$j] =~ s|^$ce->{courseDirs}->{templates}/?||;
  189   }
  190   @all_problem_directories = sortByName(undef, @all_problem_directories);
  191   unshift @all_problem_directories, $main if($includetop);
  192   return (\@all_problem_directories);
  193 }
  194 
  195 ############# Everyone has a view problems line.  Abstract it
  196 sub view_problems_line {
  197   my $internal_name = shift;
  198   my $label = shift;
  199   my $r = shift; # so we can get parameter values
  200   my $result = CGI::submit(-name=>"$internal_name", -value=>$label);
  201 
  202   my %display_modes = %{WeBWorK::PG::DISPLAY_MODES()};
  203   my @active_modes = grep { exists $display_modes{$_} }
  204     @{$r->ce->{pg}->{displayModes}};
  205   push @active_modes, 'None';
  206   # We have our own displayMode since its value may be None, which is illegal
  207   # in other modules.
  208   my $mydisplayMode = $r->param('mydisplayMode') || $r->ce->{pg}->{options}->{displayMode};
  209   $result .= '&nbsp;Display&nbsp;Mode:&nbsp;'.CGI::popup_menu(-name=> 'mydisplayMode',
  210                                                               -values=>\@active_modes,
  211                                                               -default=> $mydisplayMode);
  212   # Now we give a choice of the number of problems to show
  213   my $defaultMax = $r->param('max_shown') || MAX_SHOW_DEFAULT;
  214   $result .= '&nbsp;Max. Shown:&nbsp'.
  215     CGI::popup_menu(-name=> 'max_shown',
  216                     -values=>[5,10,15,20,25,30,50,'All'],
  217                     -default=> $defaultMax);
  218 
  219   return($result);
  220 }
  221 
  222 
  223 ### The browsing panel has three versions
  224 #####  Version 1 is local problems
  225 sub browse_local_panel {
  226   my $self = shift;
  227   my $library_selected = shift;
  228   my $lib = shift || ''; $lib =~ s/^browse_//;
  229   my $name = ($lib eq '')? 'Local' : $problib{$lib};
  230 
  231   my $list_of_prob_dirs= get_problem_directories($self->r->ce,$lib);
  232   if(scalar(@$list_of_prob_dirs) == 0) {
  233     $library_selected = "Found no directories containing problems";
  234     unshift @{$list_of_prob_dirs}, $library_selected;
  235   } else {
  236     my $default_value = SELECT_LOCAL_STRING;
  237     if (not $library_selected or $library_selected eq $default_value) {
  238       unshift @{$list_of_prob_dirs},  $default_value;
  239       $library_selected = $default_value;
  240     }
  241   }
  242   my $view_problem_line = view_problems_line('view_local_set', 'View Problems', $self->r);
  243   print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "$name Problems: ",
  244     CGI::popup_menu(-name=> 'library_sets',
  245                     -values=>$list_of_prob_dirs,
  246                     -default=> $library_selected),
  247     CGI::br(),
  248     $view_problem_line,
  249   ));
  250 }
  251 
  252 #####  Version 2 is local problem sets
  253 sub browse_mysets_panel {
  254   my $self = shift;
  255   my $library_selected = shift;
  256   my $list_of_local_sets = shift;
  257   my $default_value = "Select a Homework Set";
  258 
  259   if(scalar(@$list_of_local_sets) == 0) {
  260     $list_of_local_sets = [NO_LOCAL_SET_STRING];
  261   } elsif (not $library_selected or $library_selected eq $default_value) {
  262     unshift @{$list_of_local_sets},  $default_value;
  263     $library_selected = $default_value;
  264   }
  265 
  266   my $view_problem_line = view_problems_line('view_mysets_set', 'View Problems', $self->r);
  267   print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "Browse from: ",
  268     CGI::popup_menu(-name=> 'library_sets',
  269                     -values=>$list_of_local_sets,
  270                     -default=> $library_selected),
  271     CGI::br(),
  272     $view_problem_line
  273   ));
  274 }
  275 
  276 #####  Version 3 is the problem library
  277 
  278 
  279 # There a different levels, and you can pick a new chapter,
  280 # pick a new section, pick all from chapter, pick all from section
  281 #
  282 # Incoming data - current chapter, current section
  283 sub browse_library_panel {
  284   my $self = shift;
  285   my $r = $self->r;
  286   my $ce = $r->ce;
  287 
  288   my $libraryRoot = $r->{ce}->{problemLibrary}->{root};
  289 
  290   unless($libraryRoot) {
  291     print CGI::Tr(CGI::td(CGI::div({class=>'ResultsWithError', align=>"center"},
  292       "The problem library has not been installed.")));
  293     return;
  294   }
  295   # Test if the Library directory exists.  If not, try to make it
  296   unless(-d "$ce->{courseDirs}->{templates}/Library") {
  297     unless(symlink($libraryRoot, "$ce->{courseDirs}->{templates}/Library")) {
  298       my $msg =  <<"HERE";
  299 You are missing the directory <code>templates/Library</code>, which is needed
  300 for the Problem Library to function.  It should be a link pointing to
  301 <code>$libraryRoot</code>, which you set in <code>conf/global.conf</code>.
  302 I tried to make the link for you, but that failed.  Check the permissions
  303 in your <code>templates</code> directory.
  304 HERE
  305       $self->addbadmessage($msg);
  306     }
  307   }
  308 
  309   my $default_chap = "All Chapters";
  310   my $default_sect = "All Sections";
  311 
  312   my @chaps = WeBWorK::Utils::ListingDB::getAllChapters($r->{ce});
  313   unshift @chaps, $default_chap;
  314   my $chapter_selected = $r->param('library_chapters') || $default_chap;
  315 
  316   my @sects=();
  317   if ($chapter_selected ne $default_chap) {
  318     @sects = WeBWorK::Utils::ListingDB::getAllSections($r->{ce}, $chapter_selected);
  319   }
  320 
  321   my @textbooks = ('Textbook info not ready');
  322 
  323   unshift @sects, $default_sect;
  324   my $section_selected =  $r->param('library_sections') || $default_sect;
  325   my $view_problem_line = view_problems_line('lib_view', 'View Problems', $self->r);
  326 
  327   print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"},
  328     CGI::start_table(),
  329       CGI::Tr(
  330         CGI::td(["Chapter:",
  331           CGI::popup_menu(-name=> 'library_chapters',
  332                           -values=>\@chaps,
  333                           -default=> $chapter_selected,
  334                           -onchange=>"submit();return true"
  335           ),
  336           CGI::submit(-name=>"lib_select_chapter", -value=>"Update Section List")])),
  337       CGI::Tr(
  338         CGI::td("Section:"),
  339         CGI::td({-colspan=>2},
  340           CGI::popup_menu(-name=> 'library_sections',
  341                           -values=>\@sects,
  342                           -default=> $section_selected
  343       ))),
  344 
  345       #CGI::Tr(
  346       # CGI::td("Textbook:"),
  347       # CGI::td({-colspan=>2},
  348       #   CGI::popup_menu(-name=> 'library_textbooks',
  349       #                   -values=>\@textbooks,
  350       #                   #-default=> $section_selected
  351       #))),
  352 
  353       #CGI::Tr(
  354       # CGI::td("Keywords:"),
  355       #   CGI::td({-colspan=>2},
  356       #     CGI::textfield(-name=>"keywords",
  357       #                      -default=>"Keywords not implemented yet",
  358       #                    -override=>1, -size=>60
  359       #))),
  360       CGI::Tr(CGI::td({-colspan=>3}, $view_problem_line)),
  361       CGI::end_table(),
  362     ));
  363 }
  364 
  365 sub make_top_row {
  366   my $self = shift;
  367   my $r = $self->r;
  368   my $ce = $r->ce;
  369   my %data = @_;
  370 
  371   my $list_of_local_sets = $data{all_set_defs};
  372   my $have_local_sets = scalar(@$list_of_local_sets);
  373   my $browse_which = $data{browse_which};
  374   my $library_selected = $r->param('library_sets');
  375   my $set_selected = $r->param('local_sets');
  376 
  377   my ($dis1, $dis2, $dis3) = ("","","");
  378   $dis1 =  '-disabled' if($browse_which eq 'browse_library');
  379   $dis2 =  '-disabled' if($browse_which eq 'browse_local');
  380   $dis3 =  '-disabled' if($browse_which eq 'browse_mysets');
  381 
  382   ##  Make buttons for additional problem libraries
  383   my $libs = '';
  384   foreach my $lib (sort(keys(%problib))) {
  385     $libs .= ' '. CGI::submit(-name=>"browse_$lib", -value=>$problib{$lib},
  386                                  ($browse_which eq "browse_$lib")? '-disabled': '')
  387       if (-d "$ce->{courseDirs}{templates}/$lib");
  388   }
  389   $libs = CGI::br()."or Problems from".$libs if $libs ne '';
  390 
  391   my $these_widths = "width: 20ex";
  392   print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"center"},
  393     "Browse ",
  394     CGI::submit(-name=>"browse_library", -value=>"Problem Library", -style=>$these_widths, $dis1),
  395     CGI::submit(-name=>"browse_local", -value=>"Local Problems", -style=>$these_widths, $dis2),
  396     CGI::submit(-name=>"browse_mysets", -value=>"From This Course", -style=>$these_widths, $dis3),
  397     $libs,
  398   ));
  399 
  400   print CGI::Tr(CGI::td({-bgcolor=>"black"}));
  401 
  402   if ($browse_which eq 'browse_local') {
  403     $self->browse_local_panel($library_selected);
  404   } elsif ($browse_which eq 'browse_mysets') {
  405     $self->browse_mysets_panel($library_selected, $list_of_local_sets);
  406   } elsif ($browse_which eq 'browse_library') {
  407     $self->browse_library_panel();
  408   } else { ## handle other problem libraries
  409     $self->browse_local_panel($library_selected,$browse_which);
  410   }
  411 
  412   print CGI::Tr(CGI::td({-bgcolor=>"black"}));
  413 
  414   if($have_local_sets ==0) {
  415     $list_of_local_sets = [NO_LOCAL_SET_STRING];
  416   } elsif (not $set_selected or $set_selected eq SELECT_SET_STRING) {
  417     if ($list_of_local_sets->[0] eq "Select a Homework Set") {
  418       shift @{$list_of_local_sets};
  419     }
  420     unshift @{$list_of_local_sets}, SELECT_SET_STRING;
  421     $set_selected = SELECT_SET_STRING;
  422   }
  423   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;';
  424 
  425   print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "Adding Problems to ",
  426     CGI::b("Target Set: "),
  427     CGI::popup_menu(-name=> 'local_sets',
  428             -values=>$list_of_local_sets,
  429             -default=> $set_selected),
  430     CGI::submit(-name=>"edit_local", -value=>"Edit Target Set"),
  431     CGI::hidden(-name=>"selfassign", -default=>[0]).
  432     CGI::br(),
  433     CGI::br(),
  434     CGI::submit(-name=>"new_local_set", -value=>"Create a New Set in This Course:",
  435     -onclick=>$myjs
  436     ),
  437     "  ",
  438     CGI::textfield(-name=>"new_set_name",
  439              -default=>"Name for new set here",
  440              -override=>1, -size=>30),
  441     CGI::br(),
  442   ));
  443 
  444   print CGI::Tr(CGI::td({-bgcolor=>"black"}));
  445 
  446   print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"center"},
  447     CGI::start_table({-border=>"0"}),
  448     CGI::Tr( CGI::td({ -align=>"center"},
  449       CGI::submit(-name=>"select_all", -style=>$these_widths,
  450                   -value=>"Mark All For Adding"),
  451       CGI::submit(-name=>"select_none", -style=>$these_widths,
  452                   -value=>"Clear All Marks"),
  453     )),
  454     CGI::Tr(CGI::td(
  455       CGI::submit(-name=>"update", -style=>$these_widths. "; font-weight:bold",
  456                   -value=>"Update"),
  457     CGI::submit(-name=>"rerandomize",
  458                 -style=>$these_widths,
  459                 -value=>"Rerandomize"),
  460     CGI::submit(-name=>"cleardisplay",
  461                 -style=>$these_widths,
  462                 -value=>"Clear Problem Display")
  463   )),
  464   CGI::end_table()));
  465 
  466 }
  467 
  468 sub make_data_row {
  469   my $self = shift;
  470   my $sourceFileName = shift;
  471   my $pg = shift;
  472   my $cnt = shift;
  473   my $mark = shift || 0;
  474 
  475   $sourceFileName =~ s|^./||; # clean up top ugliness
  476 
  477   my $urlpath = $self->r->urlpath;
  478   my $problem_output = $pg->{flags}->{error_flag} ?
  479     CGI::div({class=>"ResultsWithError"}, CGI::em("This problem produced an error"))
  480     : CGI::div({class=>"RenderSolo"}, $pg->{body_text});
  481 
  482 
  483   my $edit_link =  '';
  484   #if($self->{r}->param('browse_which') ne 'browse_library') {
  485   my $problem_seed = $self->{r}->param('problem_seed') || 0;
  486   if($sourceFileName !~ /^Library\//) {
  487     $edit_link = CGI::a({href=>$self->systemLink(
  488       $urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::PGProblemEditor",
  489         courseID =>$urlpath->arg("courseID"),
  490         setID=>"Undefined_Set",
  491         problemID=>"1"),
  492         params=>{sourceFilePath => "$sourceFileName", problemSeed=> $problem_seed}
  493       )}, "Edit it" );
  494   }
  495 
  496   my $try_link = CGI::a({href=>$self->systemLink(
  497     $urlpath->newFromModule("WeBWorK::ContentGenerator::Problem",
  498       courseID =>$urlpath->arg("courseID"),
  499       setID=>"Undefined_Set",
  500       problemID=>"1"),
  501       params =>{
  502         effectiveUser => scalar($self->r->param('user')),
  503         editMode => "SetMaker",
  504         problemSeed=> $problem_seed,
  505         sourceFilePath => "$sourceFileName"
  506       }
  507     )}, "Try it");
  508 
  509   my %add_box_data = ( -name=>"trial$cnt",-value=>1,-label=>"Add this problem to the current set on the next update");
  510   if($mark & SUCCESS) {
  511     $add_box_data{ -label } .= " (just added this problem)";
  512   } elsif($mark & ADDED) {
  513     $add_box_data{ -checked } = 1;
  514   }
  515 
  516   print CGI::Tr({-align=>"left"}, CGI::td(
  517     CGI::div({-style=>"background-color: #DDDDDD; margin: 0px auto"},
  518       CGI::span({-style=>"float:left ; text-align: left"},"File name: $sourceFileName "),
  519       CGI::span({-style=>"float:right ; text-align: right"}, $edit_link, " ", $try_link)
  520     ), CGI::br(),
  521     CGI::checkbox(-name=>"hideme$cnt",-value=>1,-label=>"Don't show this problem on the next update"),
  522     CGI::br(),
  523     CGI::checkbox((%add_box_data)),
  524     CGI::hidden(-name=>"filetrial$cnt", -default=>[$sourceFileName]).
  525     CGI::p($problem_output),
  526   ));
  527 }
  528 
  529 
  530 sub pre_header_initialize {
  531   my ($self) = @_;
  532   my $r = $self->r;
  533   ## For all cases, lets set some things
  534   $self->{error}=0;
  535   my $ce = $r->ce;
  536   my $db = $r->db;
  537   my $maxShown = $r->param('max_shown') || MAX_SHOW_DEFAULT;
  538   $maxShown = 10000000 if($maxShown eq 'All'); # let's hope there aren't more
  539 
  540   ##  These directories will have individual buttons
  541   %problib = %{$ce->{courseFiles}{problibs}} if $ce->{courseFiles}{problibs};
  542 
  543   my $userName = $r->param('user');
  544   my $user = $db->getUser($userName); # checked
  545   die "record for user $userName (real user) does not exist."
  546     unless defined $user;
  547   my $authz = $r->authz;
  548   unless ($authz->hasPermissions($userName, "modify_problem_sets")) {
  549     return(""); # Error message already produced in the body
  550   }
  551 
  552   ## Now one action we have to deal with here
  553   if ($r->param('edit_local')) {
  554     my $urlpath = $r->urlpath;
  555     my $db = $r->db;
  556     my $checkset = $db->getGlobalSet($r->param('local_sets'));
  557     if (not defined($checkset)) {
  558       $self->{error} = 1;
  559       $self->addbadmessage('You need to select a "Target Set" before you can edit it.');
  560     } else {
  561       my $page = $urlpath->newFromModule('WeBWorK::ContentGenerator::Instructor::ProblemSetDetail', setID=>$r->param('local_sets'), courseID=>$urlpath->arg("courseID"));
  562       my $url = $self->systemLink($page);
  563       $self->reply_with_redirect($url);
  564     }
  565   }
  566 
  567   ## Next, lots of set up so that errors can be reported with message()
  568 
  569   ############# List of problems we have already printed
  570 
  571   $self->{past_problems} = get_past_problem_files($r);
  572   # if we don't end up reusing problems, this will be wiped out
  573   # if we do redisplay the same problems, we must adjust this accordingly
  574   my @past_marks = map {$_->[1]} @{$self->{past_problems}};
  575   my $none_shown = scalar(@{$self->{past_problems}})==0;
  576   my @pg_files=();
  577   my $use_previous_problems = 1;
  578   my $first_shown = $r->param('first_shown') || 0;
  579   my $last_shown = $r->param('last_shown');
  580   if (not defined($last_shown)) {
  581     $last_shown = -1;
  582   }
  583   my @all_past_list = (); # these are include requested, but not shown
  584   my $j = 0;
  585   while (defined($r->param("all_past_list$j"))) {
  586     push @all_past_list, $r->param("all_past_list$j");
  587     $j++;
  588   }
  589 
  590   ############# Default of which problem selector to display
  591 
  592   my $browse_which = $r->param('browse_which') || 'browse_local';
  593 
  594   my $problem_seed = $r->param('problem_seed') || 0;
  595   $r->param('problem_seed', $problem_seed); # if it wasn't defined before
  596 
  597   ## check for problem lib buttons
  598   my $browse_lib = '';
  599   foreach my $lib (keys %problib) {
  600     if ($r->param("browse_$lib")) {
  601       $browse_lib = "browse_$lib";
  602       last;
  603     }
  604   }
  605 
  606   ########### Start the logic through if elsif elsif ...
  607 
  608   ##### Asked to browse certain problems
  609   if ($browse_lib ne '') {
  610     $browse_which = $browse_lib;
  611     $r->param('library_sets', "");
  612     $use_previous_problems = 0; @pg_files = (); ## clear old problems
  613   } elsif ($r->param('browse_library')) {
  614     $browse_which = 'browse_library';
  615     $r->param('library_sets', "");
  616     $use_previous_problems = 0; @pg_files = (); ## clear old problems
  617   } elsif ($r->param('browse_local')) {
  618     $browse_which = 'browse_local';
  619     $r->param('library_sets', "");
  620     $use_previous_problems = 0; @pg_files = (); ## clear old problems
  621   } elsif ($r->param('browse_mysets')) {
  622     $browse_which = 'browse_mysets';
  623     $r->param('library_sets', "");
  624     $use_previous_problems = 0; @pg_files = (); ## clear old problems
  625 
  626     ##### Change the seed value
  627 
  628   } elsif ($r->param('rerandomize')) {
  629     $problem_seed++;
  630     $r->param('problem_seed', $problem_seed);
  631     $self->addbadmessage('Changing the problem seed for display, but there are no problems showing.') if $none_shown;
  632 
  633     ##### Clear the display
  634 
  635   } elsif ($r->param('cleardisplay')) {
  636     @pg_files = ();
  637     $use_previous_problems=0;
  638     $self->addbadmessage('The display was already cleared.') if $none_shown;
  639 
  640     ##### View problems selected from the local list
  641 
  642   } elsif ($r->param('view_local_set')) {
  643 
  644     my $set_to_display = $r->param('library_sets');
  645     if (not defined($set_to_display) or $set_to_display eq SELECT_LOCAL_STRING or $set_to_display eq "Found no directories containing problems") {
  646       $self->addbadmessage('You need to select a set to view.');
  647     } else {
  648       $set_to_display = '.' if $set_to_display eq MY_PROBLEMS;
  649       $set_to_display = substr($browse_which,7) if $set_to_display eq MAIN_PROBLEMS;
  650       @pg_files = list_pg_files($ce->{courseDirs}->{templates},
  651         "$set_to_display");
  652       $use_previous_problems=0;
  653     }
  654 
  655     ##### View problems selected from the a set in this course
  656 
  657   } elsif ($r->param('view_mysets_set')) {
  658 
  659     my $set_to_display = $r->param('library_sets');
  660     if (not defined($set_to_display)
  661         or $set_to_display eq "Select a Homework Set"
  662         or $set_to_display eq NO_LOCAL_SET_STRING) {
  663       $self->addbadmessage("You need to select a set from this course to view.");
  664     } else {
  665       my @problemList = $db->listGlobalProblems($set_to_display);
  666       my $problem;
  667       @pg_files=();
  668       for $problem (@problemList) {
  669   my $problemRecord = $db->getGlobalProblem($set_to_display, $problem); # checked
  670   die "global $problem for set $set_to_display not found." unless
  671     $problemRecord;
  672   push @pg_files, $problemRecord->source_file;
  673 
  674       }
  675       $use_previous_problems=0;
  676     }
  677 
  678     ##### View whole chapter from the library
  679     ## This will change somewhat later
  680 
  681   } elsif ($r->param('lib_view')) {
  682 
  683     @pg_files=();
  684     my $chap = $r->param('library_chapters') || "";
  685     $chap = "" if($chap eq "All Chapters");
  686     my $sect = $r->param('library_sections') || "";
  687     $sect = "" if($sect eq "All Sections");
  688     my @dbsearch = WeBWorK::Utils::ListingDB::getSectionListings($r->{ce}, "$chap", "$sect");
  689     my ($result, $tolibpath);
  690     for $result (@dbsearch) {
  691       $tolibpath = "Library/$result->{path}/$result->{filename}";
  692 
  693       ## Too clunky!!!!
  694       push @pg_files, $tolibpath;
  695     }
  696     $use_previous_problems=0;
  697 
  698     ##### Edit the current local problem set
  699 
  700   } elsif ($r->param('edit_local')) { ## Jump to set edit page
  701 
  702     ; # already handled
  703 
  704 
  705     ##### Make a new local problem set
  706 
  707   } elsif ($r->param('new_local_set')) {
  708     if ($r->param('new_set_name') !~ /^[\w.-]*$/) {
  709       $self->addbadmessage("The name ".$r->param('new_set_name')." is not a valid set name.  Use only letters, digits, -, _, and .");
  710     } else {
  711       my $newSetName = $r->param('new_set_name');
  712       # if we want to munge the input set name, do it here
  713       $r->param('local_sets',$newSetName);
  714       my $newSetRecord   = $db->getGlobalSet($newSetName);
  715       if (defined($newSetRecord)) {
  716   $self->addbadmessage("The set name $newSetName is already in use.  Pick a different name if you would like to start a new set.");
  717       } else {      # Do it!
  718   $newSetRecord = $db->{set}->{record}->new();
  719   $newSetRecord->set_id($newSetName);
  720   $newSetRecord->set_header("");
  721   $newSetRecord->hardcopy_header("");
  722   $newSetRecord->open_date(time()+60*60*24*7); # in one week
  723   $newSetRecord->due_date(time()+60*60*24*7*2); # in two weeks
  724   $newSetRecord->answer_date(time()+60*60*24*7*3); # in three weeks
  725   eval {$db->addGlobalSet($newSetRecord)};
  726   $self->addgoodmessage("Set $newSetName has been created.");
  727   my $selfassign = $r->param('selfassign') || "";
  728   $selfassign = "" if($selfassign =~ /false/i); # deal with javascript false
  729   if($selfassign) {
  730     $self->assignSetToUser($userName, $newSetRecord);
  731     $self->addgoodmessage("Set $newSetName was assigned to $userName.");
  732         }
  733       }
  734     }
  735 
  736     ##### Add selected problems to the current local set
  737 
  738   } elsif ($r->param('update')) {
  739     ## first handle problems to be added before we hide them
  740     my($localSet, @selected);
  741 
  742     @pg_files = grep {($_->[1] & ADDED) != 0 } @{$self->{past_problems}};
  743     @selected = map {$_->[0]} @pg_files;
  744 
  745     my @action_files = grep {$_->[1] > 0 } @{$self->{past_problems}};
  746     # There are now good reasons to do an update without selecting anything.
  747     #if(scalar(@action_files) == 0) {
  748     #  $self->addbadmessage('Update requested, but no problems were marked.');
  749     #}
  750 
  751     if (scalar(@selected)>0) {  # if some are to be added, they need a place to go
  752       $localSet = $r->param('local_sets');
  753       if (not defined($localSet) or
  754           $localSet eq SELECT_SET_STRING or
  755     $localSet eq NO_LOCAL_SET_STRING) {
  756   $self->addbadmessage('You are trying to add problems to something, but you did not select a "Target Set" name as a target.');
  757       } else {
  758   my $newSetRecord  = $db->getGlobalSet($localSet);
  759   if (not defined($newSetRecord)) {
  760     $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.");
  761   } else {
  762     my $addcount = add_selected($self, $db, $localSet);
  763     if($addcount > 0) {
  764       $self->addgoodmessage("Added $addcount problem".(($addcount>1)?'s':'').
  765         " to $localSet.");
  766     }
  767   }
  768       }
  769     }
  770     ## now handle problems to be hidden
  771 
  772     ## only keep the ones which are not hidden
  773     @pg_files = grep {($_->[1] & HIDDEN) ==0 } @{$self->{past_problems}};
  774     @past_marks = map {$_->[1]} @pg_files;
  775     @pg_files = map {$_->[0]} @pg_files;
  776     @all_past_list = (@all_past_list[0..($first_shown-1)],
  777           @pg_files,
  778           @all_past_list[($last_shown+1)..(scalar(@all_past_list)-1)]);
  779     $last_shown = $first_shown+$maxShown -1;
  780     $last_shown = (scalar(@all_past_list)-1) if($last_shown>=scalar(@all_past_list));
  781 
  782   } elsif ($r->param('next_page')) {
  783     $first_shown = $last_shown+1;
  784     $last_shown = $first_shown+$maxShown-1;
  785     $last_shown = (scalar(@all_past_list)-1) if($last_shown>=scalar(@all_past_list));
  786     @past_marks = ();
  787   } elsif ($r->param('prev_page')) {
  788     $last_shown = $first_shown-1;
  789     $first_shown = $last_shown - $maxShown+1;
  790 
  791     $first_shown = 0 if($first_shown<0);
  792     @past_marks = ();
  793 
  794   } elsif ($r->param('select_all')) {
  795     @past_marks = map {1} @past_marks;
  796   } elsif ($r->param('select_none')) {
  797     @past_marks = ();
  798 
  799     ##### No action requested, probably our first time here
  800 
  801   } else {
  802     #my $c = $r->connection;
  803     #print "Debug info: ". $r->get_remote_host ."<p>".  $c->remote_ip ;
  804     ;
  805   }       ##### end of the if elsif ...
  806 
  807 
  808   ############# List of local sets
  809 
  810   my @all_set_defs = $db->listGlobalSets;
  811   @all_set_defs = sortByName(undef, @all_set_defs);
  812 
  813   if ($use_previous_problems) {
  814     @pg_files = @all_past_list;
  815   } else {
  816     $first_shown = 0;
  817     $last_shown = scalar(@pg_files)<$maxShown ? scalar(@pg_files) : $maxShown;
  818     $last_shown--;    # to make it an array index
  819     @past_marks = ();
  820   }
  821   ############# Now store data in self for retreival by body
  822   $self->{first_shown} = $first_shown;
  823   $self->{last_shown} = $last_shown;
  824   $self->{browse_which} = $browse_which;
  825   $self->{problem_seed} = $problem_seed;
  826   $self->{pg_files} = \@pg_files;
  827   $self->{past_marks} = \@past_marks;
  828   $self->{all_set_defs} = \@all_set_defs;
  829 
  830 }
  831 
  832 
  833 sub title {
  834   return "Homework Set Maker";
  835 }
  836 
  837 sub body {
  838   my ($self) = @_;
  839 
  840   my $r = $self->r;
  841   my $ce = $r->ce;    # course environment
  842   my $db = $r->db;    # database
  843   my $j;      # garden variety counter
  844 
  845   my $userName = $r->param('user');
  846 
  847   my $user = $db->getUser($userName); # checked
  848   die "record for user $userName (real user) does not exist."
  849     unless defined $user;
  850 
  851   ### Check that this is a professor
  852   my $authz = $r->authz;
  853   unless ($authz->hasPermissions($userName, "modify_problem_sets")) {
  854     print "User $userName returned " .
  855       $authz->hasPermissions($user, "modify_problem_sets") .
  856   " for permission";
  857     return(CGI::div({class=>'ResultsWithError'},
  858     CGI::em("You are not authorized to access the Instructor tools.")));
  859   }
  860 
  861   ##########  Extract information computed in pre_header_initialize
  862 
  863   my $first_shown = $self->{first_shown};
  864   my $last_shown = $self->{last_shown};
  865   my $browse_which = $self->{browse_which};
  866   my $problem_seed = $self->{problem_seed};
  867   my @pg_files = @{$self->{pg_files}};
  868   my @all_set_defs = @{$self->{all_set_defs}};
  869 
  870   my @pg_html=($last_shown>=$first_shown) ?
  871     renderProblems(r=> $r,
  872                    user => $user,
  873                    problem_list => [@pg_files[$first_shown..$last_shown]],
  874                    displayMode => $r->param('mydisplayMode')) : ();
  875 
  876   ##########  Top part
  877   print CGI::startform({-method=>"POST", -action=>$r->uri, -name=>'mainform'}),
  878     $self->hidden_authen_fields,
  879       '<div align="center">',
  880   CGI::start_table({-border=>2});
  881   $self->make_top_row('all_set_defs'=>\@all_set_defs,
  882          'browse_which'=> $browse_which);
  883   print CGI::hidden(-name=>'browse_which', -default=>[$browse_which]),
  884     CGI::hidden(-name=>'problem_seed', -default=>[$problem_seed]);
  885   for ($j = 0 ; $j < scalar(@pg_files) ; $j++) {
  886     print CGI::hidden(-name=>"all_past_list$j", -default=>$pg_files[$j]);
  887   }
  888 
  889   print CGI::hidden(-name=>'first_shown', -default=>[$first_shown]);
  890   print CGI::hidden(-name=>'last_shown', -default=>[$last_shown]);
  891 
  892 
  893   ########## Now print problems
  894   my $jj;
  895   for ($jj=0; $jj<scalar(@pg_html); $jj++) {
  896     $pg_files[$jj] =~ s|^$ce->{courseDirs}->{templates}/?||;
  897     $self->make_data_row($pg_files[$jj+$first_shown], $pg_html[$jj], $jj+1, $self->{past_marks}->[$jj]);
  898   }
  899 
  900   ########## Finish things off
  901   print CGI::end_table();
  902   print '</div>';
  903   #  if($first_shown>0 or (1+$last_shown)<scalar(@pg_files)) {
  904   my ($next_button, $prev_button) = ("", "");
  905   if ($first_shown > 0) {
  906     $prev_button = CGI::submit(-name=>"prev_page", -style=>"width:15ex",
  907              -value=>"Previous page");
  908   }
  909   if ((1+$last_shown)<scalar(@pg_files)) {
  910     $next_button = CGI::submit(-name=>"next_page", -style=>"width:15ex",
  911              -value=>"Next page");
  912   }
  913   if (scalar(@pg_files)>0) {
  914     print CGI::p(($first_shown+1)."-".($last_shown+1)." of ".scalar(@pg_files).
  915      " shown.", $prev_button, " ", $next_button);
  916   }
  917   #  }
  918   print CGI::endform(), "\n";
  919 
  920   return "";
  921 }
  922 
  923 ############################################## End of Body
  924 
  925 # SKEL: To emit your own HTTP header, uncomment this:
  926 #
  927 #sub header {
  928 # my ($self) = @_;
  929 #
  930 # # Generate your HTTP header here.
  931 #
  932 # # If you return something, it will be used as the HTTP status code for this
  933 # # request. The Apache::Constants module might be useful for gerating status
  934 # # codes. If you don't return anything, the status code "OK" will be used.
  935 # return "";
  936 #}
  937 
  938 # SKEL: If you need to do any processing after the HTTP header is sent, but before
  939 # any template processing occurs, or you need to calculate values that will be
  940 # used in multiple methods, do it in this method:
  941 #
  942 #sub initialize {
  943 #my ($self) = @_;
  944 #}
  945 
  946 # SKEL: If you need to add tags to the document <HEAD>, uncomment this method:
  947 #
  948 #sub head {
  949 # my ($self) = @_;
  950 #
  951 # # You can print head tags here, like <META>, <SCRIPT>, etc.
  952 #
  953 # return "";
  954 #}
  955 
  956 # SKEL: To fill in the "info" box (to the right of the main body), use this
  957 # method:
  958 #
  959 #sub info {
  960 # my ($self) = @_;
  961 #
  962 # # Print HTML here.
  963 #
  964 # return "";
  965 #}
  966 
  967 # SKEL: To provide navigation links, use this method:
  968 #
  969 #sub nav {
  970 # my ($self, $args) = @_;
  971 #
  972 # # See the documentation of path() and pathMacro() in
  973 # # WeBWorK::ContentGenerator for more information.
  974 #
  975 # return "";
  976 #}
  977 
  978 # SKEL: For a little box for display options, etc., use this method:
  979 #
  980 #sub options {
  981 # my ($self) = @_;
  982 #
  983 # # Print HTML here.
  984 #
  985 # return "";
  986 #}
  987 
  988 # SKEL: For a list of sibling objects, use this method:
  989 #
  990 #sub siblings {
  991 # my ($self, $args) = @_;
  992 #
  993 # # See the documentation of siblings() and siblingsMacro() in
  994 # # WeBWorK::ContentGenerator for more information.
  995 # #
  996 # # Refer to implementations in ProblemSet and Problem.
  997 #
  998 # return "";
  999 #}
 1000 
 1001 =head1 AUTHOR
 1002 
 1003 Written by John Jones, jj (at) asu.edu.
 1004 
 1005 =cut
 1006 
 1007 
 1008 
 1009 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9