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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2269 - (download) (as text) (annotate)
Mon Jun 7 02:50:52 2004 UTC (9 years ago) by jj
Original Path: trunk/webwork2/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm
File size: 31444 byte(s)
Added success message when creating a new set.  Also,
added requested feature - when creating a new set, allow the user to assign
it to themselves.  This is done through javascript.  Only users with js
turned on get it, but it is kind of a frill.  Non-js users see just the old
behavior.

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9