[system] / branches / gage_dev / webwork2 / lib / WeBWorK / ContentGenerator / Instructor / GetLibrarySetProblems.pm Repository:
ViewVC logotype

View of /branches/gage_dev/webwork2/lib/WeBWorK/ContentGenerator/Instructor/GetLibrarySetProblems.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6727 - (download) (as text) (annotate)
Thu Mar 10 01:32:38 2011 UTC (2 years, 2 months ago) by gage
File size: 37717 byte(s)
merge from dev_dg


    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright © 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: webwork2/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm,v 1.85 2008/07/01 13:18:52 glarose 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::GetLibrarySetProblems;
   19 use base qw(WeBWorK::ContentGenerator::Instructor);
   20 
   21 =head1 NAME
   22 
   23 WeBWorK::ContentGenerator::Instructor::GetLibrarySetProblems - Make homework sets.
   24 
   25 =cut
   26 
   27 use strict;
   28 use warnings;
   29 
   30 
   31 #use CGI qw(-nosticky);
   32 use WeBWorK::CGI;
   33 use WeBWorK::Debug;
   34 use WeBWorK::Form;
   35 use WeBWorK::Utils qw(readDirectory max sortByName);
   36 use WeBWorK::Utils::Tasks qw(renderProblems);
   37 use File::Find;
   38 
   39 require WeBWorK::Utils::ListingDB;
   40 
   41 use constant SHOW_HINTS_DEFAULT => 0;
   42 use constant SHOW_SOLUTIONS_DEFAULT => 0;
   43 use constant MAX_SHOW_DEFAULT => 20;
   44 use constant NO_LOCAL_SET_STRING => 'No sets in this course yet';
   45 use constant SELECT_SET_STRING => 'Select a Set from this Course';
   46 use constant SELECT_LOCAL_STRING => 'Select a Problem Collection';
   47 use constant MY_PROBLEMS => '  My Problems  ';
   48 use constant MAIN_PROBLEMS => '  Unclassified Problems  ';
   49 use constant CREATE_SET_BUTTON => 'Create New Set';
   50 use constant ALL_CHAPTERS => 'All Chapters';
   51 use constant ALL_SUBJECTS => 'All Subjects';
   52 use constant ALL_SECTIONS => 'All Sections';
   53 use constant ALL_TEXTBOOKS => 'All Textbooks';
   54 
   55 use constant LIB2_DATA => {
   56   'dbchapter' => {name => 'library_chapters', all => 'All Chapters'},
   57   'dbsection' =>  {name => 'library_sections', all =>'All Sections' },
   58   'dbsubject' =>  {name => 'library_subjects', all => 'All Subjects' },
   59   'textbook' =>  {name => 'library_textbook', all =>  'All Textbooks'},
   60   'textchapter' => {name => 'library_textchapter', all => 'All Chapters'},
   61   'textsection' => {name => 'library_textsection', all => 'All Sections'},
   62   'keywords' =>  {name => 'library_keywords', all => '' },
   63   };
   64 
   65 ## Flags for operations on files
   66 
   67 use constant ADDED => 1;
   68 use constant HIDDEN => (1 << 1);
   69 use constant SUCCESS => (1 << 2);
   70 use constant DELETED => (1 << 3);
   71 use constant MOVED => (1 << 4);
   72 
   73 ##  for additional problib buttons
   74 my %problib;  ## filled in in global.conf
   75 my %ignoredir = (
   76   '.' => 1, '..' => 1, 'Library' => 1, 'CVS' => 1, 'tmpEdit' => 1,
   77   'headers' => 1, 'macros' => 1, 'email' => 1, '.svn' => 1,
   78 );
   79 
   80 sub prepare_activity_entry {
   81   my $self=shift;
   82   my $r = $self->r;
   83   my $user = $self->r->param('user') || 'NO_USER';
   84   return("In SetMaker2 as user $user");
   85 }
   86 
   87 ## This is for searching the disk for directories containing pg files.
   88 ## to make the recursion work, this returns an array where the first
   89 ## item is the number of pg files in the directory.  The second is a
   90 ## list of directories which contain pg files.
   91 ##
   92 ## If a directory contains only one pg file and the directory name
   93 ## is the same as the file name, then the directory is considered
   94 ## to be part of the parent directory (it is probably in a separate
   95 ## directory only because it has auxiliary files that want to be
   96 ## kept together with the pg file).
   97 ##
   98 ## If a directory has a file named "=library-ignore", it is never
   99 ## included in the directory menu.  If a directory contains a file
  100 ## called "=library-combine-up", then its pg are included with those
  101 ## in the parent directory (and the directory does not appear in the
  102 ## menu).  If it has a file called "=library-no-combine" then it is
  103 ## always listed as a separate directory even if it contains only one
  104 ## pg file.
  105 
  106 sub get_library_sets {
  107   my $top = shift; my $dir = shift;
  108   # ignore directories that give us an error
  109   my @lis = eval { readDirectory($dir) };
  110   if ($@) {
  111     warn $@;
  112     return (0);
  113   }
  114   return (0) if grep /^=library-ignore$/, @lis;
  115 
  116   my @pgfiles = grep { m/\.pg$/ and (not m/(Header|-text)\.pg$/) and -f "$dir/$_"} @lis;
  117   my $pgcount = scalar(@pgfiles);
  118   my $pgname = $dir; $pgname =~ s!.*/!!; $pgname .= '.pg';
  119   my $combineUp = ($pgcount == 1 && $pgname eq $pgfiles[0] && !(grep /^=library-no-combine$/, @lis));
  120 
  121   my @pgdirs;
  122   my @dirs = grep {!$ignoredir{$_} and -d "$dir/$_"} @lis;
  123   if ($top == 1) {@dirs = grep {!$problib{$_}} @dirs}
  124   foreach my $subdir (@dirs) {
  125     my @results = get_library_sets(0, "$dir/$subdir");
  126     $pgcount += shift @results; push(@pgdirs,@results);
  127   }
  128 
  129   return ($pgcount, @pgdirs) if $top || $combineUp || grep /^=library-combine-up$/, @lis;
  130   return (0,@pgdirs,$dir);
  131 }
  132 
  133 sub get_library_pgs {
  134   my $top = shift; my $base = shift; my $dir = shift;
  135   my @lis = readDirectory("$base/$dir");
  136   return () if grep /^=library-ignore$/, @lis;
  137   return () if !$top && grep /^=library-no-combine$/, @lis;
  138 
  139   my @pgs = grep { m/\.pg$/ and (not m/(Header|-text)\.pg$/) and -f "$base/$dir/$_"} @lis;
  140   my $others = scalar(grep { (!m/\.pg$/ || m/(Header|-text)\.pg$/) &&
  141                               !m/(\.(tmp|bak)|~)$/ && -f "$base/$dir/$_" } @lis);
  142 
  143   my @dirs = grep {!$ignoredir{$_} and -d "$base/$dir/$_"} @lis;
  144   if ($top == 1) {@dirs = grep {!$problib{$_}} @dirs}
  145   foreach my $subdir (@dirs) {push(@pgs, get_library_pgs(0,"$base/$dir",$subdir))}
  146 
  147   return () unless $top || (scalar(@pgs) == 1 && $others) || grep /^=library-combine-up$/, @lis;
  148   return (map {"$dir/$_"} @pgs);
  149 }
  150 
  151 sub list_pg_files {
  152   my ($templates,$dir) = @_;
  153   my $top = ($dir eq '.')? 1 : 2;
  154   my @pgs = get_library_pgs($top,$templates,$dir);
  155   return sortByName(undef,@pgs);
  156 }
  157 
  158 ## Search for set definition files
  159 
  160 sub get_set_defs {
  161   my $topdir = shift;
  162   my @found_set_defs;
  163   # get_set_defs_wanted is a closure over @found_set_defs
  164   my $get_set_defs_wanted = sub {
  165     #my $fn = $_;
  166     #my $fdir = $File::Find::dir;
  167     #return() if($fn !~ /^set.*\.def$/);
  168     ##return() if(not -T $fn);
  169     #push @found_set_defs, "$fdir/$fn";
  170     push @found_set_defs, $_ if m|/set[^/]*\.def$|;
  171   };
  172   find({ wanted => $get_set_defs_wanted, follow_fast=>1, no_chdir=>1}, $topdir);
  173   map { $_ =~ s|^$topdir/?|| } @found_set_defs;
  174   return @found_set_defs;
  175 }
  176 
  177 ## Try to make reading of set defs more flexible.  Additional strategies
  178 ## for fixing a path can be added here.
  179 
  180 sub munge_pg_file_path {
  181   my $self = shift;
  182   my $pg_path = shift;
  183   my $path_to_set_def = shift;
  184   my $end_path = $pg_path;
  185   # if the path is ok, don't fix it
  186   return($pg_path) if(-e $self->r->ce->{courseDirs}{templates}."/$pg_path");
  187   # if we have followed a link into a self contained course to get
  188   # to the set.def file, we need to insert the start of the path to
  189   # the set.def file
  190   $end_path = "$path_to_set_def/$pg_path";
  191   return($end_path) if(-e $self->r->ce->{courseDirs}{templates}."/$end_path");
  192   # if we got this far, this path is bad, but we let it produce
  193   # an error so the user knows there is a troublesome path in the
  194   # set.def file.
  195   return($pg_path);
  196 }
  197 
  198 ## Read a set definition file.  This could be abstracted since it happens
  199 ## elsewhere.  Here we don't have to process so much of the file.
  200 
  201 sub read_set_def {
  202   my $self = shift;
  203   my $r = $self->r;
  204   my $filePathOrig = shift;
  205   my $filePath = $r->ce->{courseDirs}{templates}."/$filePathOrig";
  206   $filePathOrig =~ s/set.*\.def$//;
  207   $filePathOrig =~ s|/$||;
  208   $filePathOrig = "." if ($filePathOrig !~ /\S/);
  209   my @pg_files = ();
  210   my ($line, $got_to_pgs, $name, @rest) = ("", 0, "");
  211   if ( open (SETFILENAME, "$filePath") )    {
  212     while($line = <SETFILENAME>) {
  213       chomp($line);
  214       $line =~ s|(#.*)||; # don't read past comments
  215       if($got_to_pgs) {
  216         unless ($line =~ /\S/) {next;} # skip blank lines
  217         ($name,@rest) = split (/\s*,\s*/,$line);
  218         $name =~ s/\s*//g;
  219         push @pg_files, $name;
  220       } else {
  221         $got_to_pgs = 1 if ($line =~ /problemList\s*=/);
  222       }
  223     }
  224   } else {
  225     $self->addbadmessage("Cannot open $filePath");
  226   }
  227   # This is where we would potentially munge the pg file paths
  228   # One possibility
  229   @pg_files = map { $self->munge_pg_file_path($_, $filePathOrig) } @pg_files;
  230   return(@pg_files);
  231 }
  232 
  233 ## go through past page getting a list of identifiers for the problems
  234 ## and whether or not they are selected, and whether or not they should
  235 ## be hidden
  236 
  237 sub get_past_problem_files {
  238   my $r = shift;
  239   my @found=();
  240   my $count =1;
  241   while (defined($r->param("filetrial$count"))) {
  242     my $val = 0;
  243     $val |= ADDED if($r->param("trial$count"));
  244     $val |= HIDDEN if($r->param("hideme$count"));
  245     $val |= MOVED if($r->param("moved$count"));
  246     push @found, [$r->param("filetrial$count"), $val];
  247     $count++;
  248   }
  249   $count = 1;
  250   while (defined($r->param("mysetfiletrial$count"))) {
  251     my $val = 0;
  252     $val |= DELETED if($r->param("deleted$count"));
  253     push @found, [$r->param("mysetfiletrial$count"), $val];
  254     $count++;
  255   }
  256   return(\@found);
  257 }
  258 
  259 
  260 sub make_data_row {
  261   my $self = shift;
  262   my $sourceFileName = shift;
  263   my $pg = shift;
  264   my $cnt = shift;
  265   my $mark = shift || 0;
  266 
  267   $sourceFileName =~ s|^./||; # clean up top ugliness
  268 
  269   my $urlpath = $self->r->urlpath;
  270   my $db = $self->r->db;
  271 
  272   ## to set up edit and try links elegantly we want to know if
  273   ##    any target set is a gateway assignment or not
  274   my $localSet = $self->r->param('local_sets');
  275   my $setRecord;
  276   if ( defined($localSet) && $localSet ne SELECT_SET_STRING &&
  277        $localSet ne NO_LOCAL_SET_STRING ) {
  278     $setRecord = $db->getGlobalSet( $localSet );
  279   }
  280   my $isGatewaySet = ( defined($setRecord) &&
  281            $setRecord->assignment_type =~ /gateway/ );
  282 
  283   my $problem_output = $pg->{flags}->{error_flag} ?
  284     CGI::div({class=>"ResultsWithError"}, CGI::em("This problem produced an error"))
  285     : CGI::div({class=>"RenderSolo"}, $pg->{body_text});
  286   $problem_output .= $pg->{flags}->{comment} if($pg->{flags}->{comment});
  287 
  288 
  289   #if($self->{r}->param('browse_which') ne 'browse_npl_library') {
  290   my $problem_seed = $self->{'problem_seed'} || 1234;
  291   my $edit_link = CGI::a({href=>$self->systemLink(
  292      $urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::PGProblemEditor",
  293         courseID =>$urlpath->arg("courseID"),
  294         setID=>"Undefined_Set",
  295         problemID=>"1"),
  296       params=>{sourceFilePath => "$sourceFileName", problemSeed=> $problem_seed}
  297       ), target=>"WW_Editor"}, "Edit it" );
  298 
  299   my $displayMode = $self->r->param("mydisplayMode");
  300   $displayMode = $self->r->ce->{pg}->{options}->{displayMode}
  301     if not defined $displayMode or $displayMode eq "None";
  302   my $module = ( $isGatewaySet ) ? "GatewayQuiz" : "Problem";
  303   my %pathArgs = ( courseID =>$urlpath->arg("courseID"),
  304       setID=>"Undefined_Set" );
  305   $pathArgs{problemID} = "1" if ( ! $isGatewaySet );
  306 
  307   my $try_link = CGI::a({href=>$self->systemLink(
  308     $urlpath->newFromModule("WeBWorK::ContentGenerator::$module",
  309       %pathArgs ),
  310       params =>{
  311         effectiveUser => scalar($self->r->param('user')),
  312         editMode => "SetMaker",
  313         problemSeed=> $problem_seed,
  314         sourceFilePath => "$sourceFileName",
  315         displayMode => $displayMode,
  316       }
  317     ), target=>"WW_View"}, "Try it");
  318 
  319   my %add_box_data = ( -id=>"trial$cnt", -class=>"add_problem_checkbox" ,-name=>"trial$cnt",-value=>1,-label=>"Add this problem to the target set on the next update");
  320   #allow for a move command from one problem set to anoter
  321   my $move_box_data;
  322   if($self->r->param('edit_mysets_set')){
  323     $move_box_data = CGI::checkbox( -id=>"moved$cnt" ,-name=>"moved$cnt",-value=>1,,-override=>1,-label=>"Move this problem from this set to the target set on the next update");
  324   }
  325   else{
  326     $move_box_data = "";
  327   }
  328   if($mark & SUCCESS) {
  329     $add_box_data{ -label } .= " (just added this problem)";
  330   } elsif($mark & ADDED) {
  331     $add_box_data{ -checked } = 1;
  332   }
  333 
  334   if(!($self->{isInSet}{$sourceFileName})){
  335 
  336     print CGI::div({-class=>"problem libraryProblem", -align=>"left", -draggable=>"true", -href=>"#", -id=>"$cnt"},
  337       CGI::p({},"File name: $sourceFileName "),
  338       CGI::p({}, $edit_link, " ", $try_link),
  339       CGI::p(CGI::checkbox(-id=>"hideme$cnt", -name=>"hideme$cnt",-value=>1,-label=>"Don't show this problem on the next update",-override=>1)),
  340       CGI::p(CGI::checkbox((%add_box_data),-override=>1)),
  341       CGI::p($move_box_data),
  342       CGI::hidden(-id=>"filetrial$cnt", -name=>"filetrial$cnt", -default=>$sourceFileName,-override=>1).
  343       CGI::p($problem_output)
  344     );
  345   }
  346   else{
  347     print CGI::div({-class=>"problem libraryProblem used", -align=>"left", -draggable=>"true", -href=>"#", -id=>"$cnt"},
  348       CGI::p({},"File name: $sourceFileName "),
  349       CGI::p({}, $edit_link, " ", $try_link),
  350       CGI::p(CGI::checkbox(-id=>"hideme$cnt", -name=>"hideme$cnt",-value=>1,-label=>"Don't show this problem on the next update",-override=>1)),
  351       CGI::p(CGI::checkbox((%add_box_data),-override=>1)),
  352       CGI::p($move_box_data),
  353       CGI::hidden(-id=>"filetrial$cnt", -name=>"filetrial$cnt", -default=>$sourceFileName,-override=>1).
  354       CGI::p($problem_output),
  355       CGI::b("(This problem is in the target set)")
  356     );
  357   }
  358 }
  359 
  360 
  361 sub clear_default {
  362   my $r = shift;
  363   my $param = shift;
  364   my $default = shift;
  365   my $newvalue = $r->param($param) || '';
  366   $newvalue = '' if($newvalue eq $default);
  367   $r->param($param, $newvalue);
  368 }
  369 
  370 sub pre_header_initialize {
  371   my ($self) = @_;
  372   my $r = $self->r;
  373   ## For all cases, lets set some things
  374   $self->{error}=0;
  375   my $ce = $r->ce;
  376   my $db = $r->db;
  377   my $maxShown = $r->param('max_shown') || MAX_SHOW_DEFAULT;
  378   $maxShown = 10000000 if($maxShown eq 'All'); # let's hope there aren't more
  379   my $library_basic = $r->param('library_is_basic') || 1;
  380   $self->{problem_seed} = $r->param('problem_seed') || 1234;
  381   ## Fix some parameters
  382   for my $key (keys(%{ LIB2_DATA() })) {
  383     clear_default($r, LIB2_DATA->{$key}->{name}, LIB2_DATA->{$key}->{all} );
  384   }
  385     ##  Grab library sets to display from parameters list.  We will modify this
  386     ##  as we go through the if/else tree
  387     $self->{current_library_set} =  $r->param('library_sets');
  388     $self->{current_myset_set} = $r->param('myset_sets');
  389     if (not defined($self->{current_myset_set})
  390     or $self->{current_myset_set} eq "Select a Homework Set"
  391     or $self->{current_myset_set} eq NO_LOCAL_SET_STRING) {
  392       my @all_db_sets = $db->listGlobalSets;
  393       @all_db_sets = sortByName(undef, @all_db_sets);
  394       $self->{current_myset_set} = shift(@all_db_sets);
  395     }
  396 
  397   ##  These directories will have individual buttons
  398   %problib = %{$ce->{courseFiles}{problibs}} if $ce->{courseFiles}{problibs};
  399 
  400   my $userName = $r->param('user');
  401   my $user = $db->getUser($userName); # checked
  402   die "record for user $userName (real user) does not exist."
  403     unless defined $user;
  404   my $authz = $r->authz;
  405   unless ($authz->hasPermissions($userName, "modify_problem_sets")) {
  406     return(""); # Error message already produced in the body
  407   }
  408 
  409   ## Now one action we have to deal with here
  410   if ($r->param('edit_local')) {
  411     my $urlpath = $r->urlpath;
  412     my $db = $r->db;
  413     my $checkset = $db->getGlobalSet($r->param('local_sets'));
  414     if (not defined($checkset)) {
  415       $self->{error} = 1;
  416       $self->addbadmessage('You need to select a "Target Set" before you can edit it.');
  417     } else {
  418       my $page = $urlpath->newFromModule('WeBWorK::ContentGenerator::Instructor::ProblemSetDetail', setID=>$r->param('local_sets'), courseID=>$urlpath->arg("courseID"));
  419       my $url = $self->systemLink($page);
  420       $self->reply_with_redirect($url);
  421     }
  422   }
  423 
  424   ## Next, lots of set up so that errors can be reported with message()
  425 
  426   ############# List of problems we have already printed
  427 
  428   $self->{past_problems} = get_past_problem_files($r);
  429   # if we don't end up reusing problems, this will be wiped out
  430   # if we do redisplay the same problems, we must adjust this accordingly
  431   my @past_marks = map {$_->[1]} @{$self->{past_problems}};
  432   my $none_shown = scalar(@{$self->{past_problems}})==0;
  433   my @pg_files=();
  434   my @myset_files=();
  435   my $use_previous_problems = 1;
  436   my $first_shown = $r->param('first_shown') || 0;
  437   my $last_shown = $r->param('last_shown');
  438   if (not defined($last_shown)) {
  439     $last_shown = -1;
  440   }
  441   my @all_past_list = (); # these are include requested, but not shown
  442   my $j = 0;
  443   while (defined($r->param("all_past_list$j"))) {
  444     push @all_past_list, $r->param("all_past_list$j");
  445     $j++;
  446   }
  447 
  448   ############# Default of which problem selector to display
  449 
  450   my $browse_which = $r->param('browse_which') || 'browse_npl_library';
  451   my $gridded = $r->param('gridify') || 'false';
  452 
  453 
  454 
  455   ## check for problem lib buttons
  456   my $browse_lib = '';
  457   foreach my $lib (keys %problib) {
  458     if ($r->param("browse_$lib")) {
  459       $browse_lib = "browse_$lib";
  460       last;
  461     }
  462   }
  463 
  464 
  465   ########### Start the logic through if elsif elsif ...
  466     debug("browse_lib", $r->param("$browse_lib"));
  467     debug("browse_npl_library", $r->param("browse_npl_library"));
  468     debug("edit_mysets", $r->param("edit_mysets"));
  469     debug("browse_setdefs", $r->param("browse_setdefs"));
  470   ##### Asked to browse certain problems
  471   if ($browse_lib ne '') {
  472     $browse_which = $browse_lib;
  473     $self->{current_library_set} = "";
  474     $use_previous_problems = 0; @pg_files = (); ## clear old problems
  475   } elsif ($r->param('browse_npl_library')) {
  476     $browse_which = 'browse_npl_library';
  477     $self->{current_library_set} = "";
  478     $use_previous_problems = 0; @pg_files = (); ## clear old problems
  479   } elsif ($r->param('browse_local')) {
  480     $browse_which = 'browse_local';
  481     #$self->{current_library_set} = "";
  482     $use_previous_problems = 0; @pg_files = (); ## clear old problems
  483   } elsif ($r->param('edit_mysets')) {
  484     $browse_which = 'edit_mysets';
  485     $self->{current_library_set} = "";
  486     $use_previous_problems = 0; @pg_files = (); ## clear old problems
  487   } elsif ($r->param('browse_setdefs')) {
  488     $browse_which = 'browse_setdefs';
  489     $self->{current_library_set} = "";
  490     $use_previous_problems = 0; @pg_files = (); ## clear old problems
  491 
  492     ##### Change the seed value
  493 
  494   } elsif ($r->param('rerandomize')) {
  495     $self->{problem_seed}= 1+$self->{problem_seed};
  496     #$r->param('problem_seed', $problem_seed);
  497     $self->addbadmessage('Changing the problem seed for display, but there are no problems showing.') if $none_shown;
  498 
  499     ##### Clear the display
  500 
  501   } elsif ($r->param('cleardisplay')) {
  502     @pg_files = ();
  503     $use_previous_problems=0;
  504     $self->addbadmessage('The display was already cleared.') if $none_shown;
  505 
  506     ##### View problems selected from the local list
  507 
  508   } elsif ($r->param('view_local_set')) {
  509 
  510     my $set_to_display = $self->{current_library_set};
  511     if (not defined($set_to_display) or $set_to_display eq SELECT_LOCAL_STRING or $set_to_display eq "Found no directories containing problems") {
  512       $self->addbadmessage('You need to select a set to view.');
  513     } else {
  514       $set_to_display = '.' if $set_to_display eq MY_PROBLEMS;
  515       $set_to_display = substr($browse_which,7) if $set_to_display eq MAIN_PROBLEMS;
  516       @pg_files = list_pg_files($ce->{courseDirs}->{templates},
  517         "$set_to_display");
  518       $use_previous_problems=0;
  519     }
  520 
  521     ##### View problems selected from the a set in this course
  522   } elsif ($r->param('edit_mysets_set')){
  523 
  524     my $set_to_display = $self->{current_library_set};
  525       debug("set_to_display is $set_to_display");
  526       if (not defined($set_to_display)
  527           or $set_to_display eq "Select a Homework Set"
  528           or $set_to_display eq NO_LOCAL_SET_STRING) {
  529         $self->addbadmessage("You need to select a set from this course to view.");
  530       } else {
  531         # DBFIXME don't use ID list, use an iterator
  532         my @problemList = $db->listGlobalProblems($set_to_display);
  533         my $problem;
  534         @pg_files=();
  535         for $problem (@problemList) {
  536           my $problemRecord = $db->getGlobalProblem($set_to_display, $problem); # checked
  537           die "global $problem for set $set_to_display not found." unless
  538             $problemRecord;
  539           push @pg_files, $problemRecord->source_file;
  540 
  541         }
  542         @pg_files = sortByName(undef,@pg_files);
  543         $use_previous_problems=0;
  544       }
  545 
  546     ##### View from the library database
  547 
  548   } elsif ($r->param('lib_view')) {
  549     @pg_files=();
  550     my @dbsearch = WeBWorK::Utils::ListingDB::getSectionListings($r);
  551     my ($result, $tolibpath);
  552     for $result (@dbsearch) {
  553       $tolibpath = "Library/$result->{path}/$result->{filename}";
  554 
  555       ## Too clunky!!!!
  556       push @pg_files, $tolibpath;
  557     }
  558     $use_previous_problems=0;
  559 
  560     ##### View a set from a set*.def
  561 
  562   } elsif ($r->param('view_setdef_set')) {
  563 
  564     my $set_to_display = $self->{current_library_set};
  565     debug("set_to_display is $set_to_display");
  566     if (not defined($set_to_display)
  567         or $set_to_display eq "Select a Set Definition File"
  568         or $set_to_display eq NO_LOCAL_SET_STRING) {
  569       $self->addbadmessage("You need to select a set definition file to view.");
  570     } else {
  571       @pg_files= $self->read_set_def($set_to_display);
  572     }
  573     $use_previous_problems=0;
  574 
  575     ##### Edit the current local homework set
  576 
  577   } elsif ($r->param('edit_local')) { ## Jump to set edit page
  578 
  579     ; # already handled
  580 
  581 
  582     ##### Make a new local homework set
  583 
  584   } elsif ($r->param('new_local_set')) {
  585     if ($r->param('new_set_name') !~ /^[\w .-]*$/) {
  586       $self->addbadmessage("The name ".$r->param('new_set_name')." is not a valid set name.  Use only letters, digits, -, _, and .");
  587     } else {
  588       my $newSetName = $r->param('new_set_name');
  589       # if we want to munge the input set name, do it here
  590       $newSetName =~ s/\s/_/g;
  591       debug("local_sets was ", $r->param('local_sets'));
  592       $r->param('local_sets',$newSetName);  ## use of two parameter param
  593       debug("new value of local_sets is ", $r->param('local_sets'));
  594       my $newSetRecord   = $db->getGlobalSet($newSetName);
  595       if (defined($newSetRecord)) {
  596               $self->addbadmessage("The set name $newSetName is already in use.
  597               Pick a different name if you would like to start a new set.");
  598       } else {      # Do it!
  599         # DBFIXME use $db->newGlobalSet
  600         $newSetRecord = $db->{set}->{record}->new();
  601         $newSetRecord->set_id($newSetName);
  602         $newSetRecord->set_header("");
  603         $newSetRecord->hardcopy_header("");
  604         $newSetRecord->open_date(time()+60*60*24*7); # in one week
  605         $newSetRecord->due_date(time()+60*60*24*7*2); # in two weeks
  606         $newSetRecord->answer_date(time()+60*60*24*7*3); # in three weeks
  607         eval {$db->addGlobalSet($newSetRecord)};
  608         if ($@) {
  609           $self->addbadmessage("Problem creating set $newSetName<br> $@");
  610         } else {
  611           $self->addgoodmessage("Set $newSetName has been created.");
  612           my $selfassign = $r->param('selfassign') || "";
  613           $selfassign = "" if($selfassign =~ /false/i); # deal with javascript false
  614           if($selfassign) {
  615             $self->assignSetToUser($userName, $newSetRecord);
  616             $self->addgoodmessage("Set $newSetName was assigned to $userName.");
  617           }
  618         }
  619       }
  620     }
  621 
  622     ##### Add selected problems to the current local set
  623 
  624   } elsif ($r->param('update')) {
  625     $self->addgoodmessage("the update param exists");
  626     ## first handle problems to be added before we hide them
  627     my($localSet, @selected);
  628 
  629     my @add_pg_files = grep {(($_->[1] & ADDED)) != 0 } @{$self->{past_problems}};
  630     my @add_selected = map {$_->[0]} @add_pg_files;
  631 
  632     my @delete_pg_files = grep {(($_->[1] & DELETED)) != 0} @{$self->{past_problems}};
  633     my @delete_selected = map {$_->[0]} @delete_pg_files;
  634 
  635     my @move_pg_files = grep {(($_->[1] & MOVED)) != 0} @{$self->{past_problems}};
  636     my @move_selected = map {$_->[0]} @move_pg_files;
  637 
  638     my @action_files = grep {$_->[1] > 0 } @{$self->{past_problems}};
  639     # There are now good reasons to do an update without selecting anything.
  640     #if(scalar(@action_files) == 0) {
  641     #  $self->addbadmessage('Update requested, but no problems were marked.');
  642     #}
  643 
  644     if (scalar(@add_selected)>0) {  # if some are to be added, they need a place to go
  645       $localSet = $r->param('myset_sets');
  646       if (not defined($localSet) or
  647           $localSet eq SELECT_SET_STRING or
  648                 $localSet eq NO_LOCAL_SET_STRING) {
  649         $self->addbadmessage('You are trying to add problems to something,
  650         but you did not select a "Target Set" name as a target.');
  651       } else {
  652         my $newSetRecord  = $db->getGlobalSet($localSet);
  653         if (not defined($newSetRecord)) {
  654           $self->addbadmessage("You are trying to add problems to $localSet,
  655           but that set does not seem to exist!  I bet you used your \"Back\" button.");
  656         } else {
  657           my $addcount = add_selected($self, $db, $localSet);
  658           if($addcount > 0) {
  659             $self->addgoodmessage("Added $addcount problem".(($addcount>1)?'s':'').
  660               " to $localSet.");
  661           }
  662         }
  663       }
  664     }
  665     if (scalar(@delete_selected)>0) { # if some are to be added, they need a place to go
  666       $localSet = $r->param('myset_sets');
  667       if (not defined($localSet) or
  668           $localSet eq SELECT_SET_STRING or
  669                 $localSet eq NO_LOCAL_SET_STRING) {
  670         $self->addbadmessage('You are trying to add problems to something,
  671         but you did not select a "Target Set" name as a target.');
  672       } else {
  673         my $newSetRecord  = $db->getGlobalSet($localSet);
  674         if (not defined($newSetRecord)) {
  675           $self->addbadmessage("You are trying to delete problems to $localSet,
  676           but that set does not seem to exist!  I bet you used your \"Back\" button.");
  677         } else {
  678           my $deletecount = delete_selected($self, $db, $localSet);
  679           if($deletecount > 0) {
  680             $self->addgoodmessage("Deleted $deletecount problem".(($deletecount>1)?'s':'').
  681               " to $localSet.");
  682           }
  683         }
  684       }
  685     }
  686     if (scalar(@move_selected)>0) { # if some are to be added, they need a place to go
  687       $localSet = $r->param('myset_sets');
  688       my $otherSet = $self->{current_library_set};
  689       if (not defined($localSet) or not defined($otherSet) or
  690           $localSet eq SELECT_SET_STRING or
  691                 $localSet eq NO_LOCAL_SET_STRING) {
  692         $self->addbadmessage('You are trying to add problems to something,
  693         but you did not select a "Target Set" name as a target.');
  694       } else {
  695         my $newSetRecord  = $db->getGlobalSet($localSet);
  696         my $otherNewSetRecord = $db->getGlobalSet($otherSet);
  697         if (not defined($newSetRecord) or not defined($otherNewSetRecord)) {
  698           $self->addbadmessage("You are trying to move problems from $otherSet to $localSet,
  699           but that set does not seem to exist!  I bet you used your \"Back\" button.");
  700         } else {
  701           my $addcount = add_selected($self, $db, $localSet);
  702           my $deletecount = delete_selected($self, $db, $otherSet);
  703           if($addcount > 0 && $deletecount > 0) {
  704             $self->addgoodmessage("Moved $addcount problem".(($addcount>1)?'s':'').
  705               " from $otherSet to $localSet.");
  706           }
  707         }
  708       }
  709     }
  710     ## now handle problems to be hidden
  711 
  712     ## only keep the ones which are not hidden
  713     @pg_files = grep {($_->[1] & HIDDEN) ==0 } @{$self->{past_problems}};
  714     @pg_files = grep {(($_->[1] & DELETED)) != 0 } @pg_files;
  715     @past_marks = map {$_->[1]} @pg_files;
  716     @pg_files = map {$_->[0]} @pg_files;
  717     @all_past_list = (@all_past_list[0..($first_shown-1)],
  718           @pg_files,
  719           @all_past_list[($last_shown+1)..(scalar(@all_past_list)-1)]);
  720     $last_shown = $first_shown+$maxShown -1; debug("last_shown 3: ", $last_shown);
  721     $last_shown = (scalar(@all_past_list)-1) if($last_shown>=scalar(@all_past_list)); debug("last_shown 4: ", $last_shown);
  722 
  723     #want to reorder in addition to anything else
  724     if ($r->param('isReordered')){
  725     #this might work, i'm not sure if this is the right ID
  726       reorderProblems($self, $db, $r->param('myset_sets'), $r);
  727     }
  728 
  729   } elsif ($r->param('next_page')) {
  730     $first_shown = $last_shown+1;
  731     $last_shown = $first_shown+$maxShown-1; debug("last_shown 5: ", $last_shown);
  732     $last_shown = (scalar(@all_past_list)-1) if($last_shown>=scalar(@all_past_list)); debug("last_shown 6: ", $last_shown);
  733     $self->addgoodmessage("It sees the next_page parameter, last shown is: $last_shown and first shown is: $first_shown");
  734     @past_marks = ();
  735   } elsif ($r->param('prev_page')) {
  736     $last_shown = $first_shown-1;
  737     $first_shown = $last_shown - $maxShown+1;
  738 
  739     $first_shown = 0 if($first_shown<0);
  740     @past_marks = ();
  741 
  742   } elsif ($r->param('select_all')) {
  743     @past_marks = map {1} @past_marks;
  744   } elsif ($r->param('library_basic')) {
  745     $library_basic = 1;
  746     for my $jj (qw(textchapter textsection textbook)) {
  747       $r->param('library_'.$jj,'');
  748     }
  749   } elsif ($r->param('library_advanced')) {
  750     $library_basic = 2;
  751   } elsif ($r->param('library_reset')) {
  752     for my $jj (qw(chapters sections subjects textbook keywords)) {
  753       $r->param('library_'.$jj,'');
  754     }
  755   } elsif ($r->param('select_none')) {
  756     @past_marks = ();
  757   } else {
  758     #nothing
  759   }       ##### end of the if elsif ...
  760 
  761   #I'm worried this will break something
  762   my $default_set = $self->{current_myset_set};
  763     #debug("set_to_display is $default_set");
  764     if (not defined($default_set)
  765       or $default_set eq "Select a Homework Set"
  766       or $default_set eq NO_LOCAL_SET_STRING) {
  767       $self->addbadmessage("You need to select a set from this course to view.");
  768     } else {
  769       # DBFIXME don't use ID list, use an iterator
  770       my @problemList = $db->listGlobalProblems($default_set);
  771       my $problem;
  772       @myset_files=();
  773       for $problem (@problemList) {
  774         my $problemRecord = $db->getGlobalProblem($default_set, $problem); # checked
  775         die "global $problem for set $default_set not found." unless
  776           $problemRecord;
  777         push @myset_files, $problemRecord->source_file;
  778 
  779       }
  780       #@myset_files = sortByName(undef,@myset_files);
  781     }
  782 
  783   ############# List of local sets
  784 
  785   # DBFIXME sorting in database, please!
  786   my @all_db_sets = $db->listGlobalSets;
  787   @all_db_sets = sortByName(undef, @all_db_sets);
  788 
  789   if ($use_previous_problems) {
  790     @pg_files = @all_past_list;
  791   } else {
  792     $first_shown = 0;
  793     $last_shown = scalar(@pg_files)<$maxShown ? scalar(@pg_files) : $maxShown;
  794     $last_shown--;    # to make it an array index
  795     @past_marks = ();
  796   }
  797   ############# Now store data in self for retreival by body
  798   $self->{first_shown} = $first_shown;
  799   $self->{last_shown} = $last_shown;
  800   $self->{browse_which} = $browse_which;
  801   $self->{gridded} = $gridded;
  802   #$self->{problem_seed} = $problem_seed;
  803   $self->{pg_files} = \@pg_files;
  804   $self->{myset_files} = \@myset_files;
  805   $self->{past_marks} = \@past_marks;
  806   $self->{all_db_sets} = \@all_db_sets;
  807   $self->{library_basic} = $library_basic;
  808   debug("past_marks is ", join(" ", @{$self->{past_marks}}));
  809 }
  810 
  811 
  812 sub title {
  813   return "Library Browser v2";
  814 }
  815 
  816 # hide view options panel since it distracts from SetMaker's built-in view options
  817 sub options {
  818   return "";
  819 }
  820 
  821 sub head {
  822   return "";
  823 }
  824 
  825 sub body {
  826   my ($self) = @_;
  827 
  828   my $r = $self->r;
  829   my $ce = $r->ce;    # course environment
  830   my $db = $r->db;    # database
  831   my $j;      # garden variety counter
  832 
  833   my $userName = $r->param('user');
  834 
  835   my $user = $db->getUser($userName); # checked
  836   die "record for user $userName (real user) does not exist."
  837     unless defined $user;
  838 
  839   ### Check that this is a professor
  840   my $authz = $r->authz;
  841   unless ($authz->hasPermissions($userName, "modify_problem_sets")) {
  842     print "User $userName returned " .
  843       $authz->hasPermissions($user, "modify_problem_sets") .
  844   " for permission";
  845     return(CGI::div({class=>'ResultsWithError'},
  846     CGI::em("You are not authorized to access the Instructor tools.")));
  847   }
  848 
  849   my $showHints = $r->param('showHints');
  850   my $showSolutions = $r->param('showSolutions');
  851 
  852   ##########  Extract information computed in pre_header_initialize
  853 
  854   my $first_shown = $self->{first_shown};
  855   my $last_shown = $self->{last_shown};
  856   my $browse_which = $self->{browse_which};
  857   my $gridded = $self->{gridded};
  858   my $problem_seed = $self->{problem_seed}||1234;
  859   my @pg_files = @{$self->{pg_files}};
  860   my @myset_files =@{$self->{myset_files}};
  861   my @all_db_sets = @{$self->{all_db_sets}};
  862 
  863   my $displayModePlaceholder;
  864   if (not defined($r->param('mydisplayMode'))){
  865     $displayModePlaceholder = "images";
  866   }
  867   else{
  868     $displayModePlaceholder = $r->param('mydisplayMode');
  869   }
  870 
  871   my @pg_html;
  872   if ($last_shown >= $first_shown) {
  873     @pg_html = renderProblems(
  874       r=> $r,
  875       user => $user,
  876       problem_list => [@pg_files[$first_shown..$last_shown]],
  877       displayMode => $displayModePlaceholder,
  878       showHints => $showHints,
  879       showSolutions => $showSolutions,
  880     );
  881   }
  882   my @myset_html;
  883 
  884   @myset_html = renderProblems(
  885     r=> $r,
  886     user => $user,
  887     problem_list => [@myset_files],
  888     displayMode => $displayModePlaceholder,
  889     showHints => $showHints,
  890     showSolutions => $showSolutions,
  891   );
  892 
  893   my %isInSet;
  894   my $setName = $r->param("myset_sets");
  895   if ($setName) {
  896     # DBFIXME where clause, iterator
  897     # DBFIXME maybe instead of hashing here, query when checking source files?
  898     # DBFIXME definitely don't need to be making full record objects
  899     # DBFIXME SELECT source_file FROM whatever_problem WHERE set_id=? GROUP BY source_file ORDER BY NULL;
  900     # DBFIXME (and stick result directly into hash)
  901     foreach my $problem ($db->listGlobalProblems($setName)) {
  902       my $problemRecord = $db->getGlobalProblem($setName, $problem);
  903       $isInSet{$problemRecord->source_file} = 1;
  904     }
  905   }
  906   $self->{isInSet} = \%isInSet;
  907   my $jj;
  908 
  909 
  910         #some work for the moved controls
  911         my $list_of_local_sets = $self->{all_db_sets};
  912         my $have_local_sets = scalar(@$list_of_local_sets);
  913         my $library_selected = $self->{current_library_set};
  914         my $set_selected = $r->param('local_sets');
  915 
  916         if($have_local_sets ==0) {
  917           $list_of_local_sets = [NO_LOCAL_SET_STRING];
  918         } elsif (not defined($set_selected) or $set_selected eq ""
  919           or $set_selected eq SELECT_SET_STRING) {
  920           unshift @{$list_of_local_sets}, SELECT_SET_STRING;
  921           $set_selected = SELECT_SET_STRING;
  922         }
  923 
  924         # Tidy this list up since it is used in two different places
  925         if ($list_of_local_sets->[0] eq SELECT_SET_STRING) {
  926           shift @{$list_of_local_sets};
  927         }
  928 
  929         print '<div id="setmaker_library_box" class="setmaker_library">';
  930           print '<h1>Library</h1>';
  931           print '<div class="setSelector">';
  932           print '</div>';
  933           print '<div id="setmaker_library_data">';
  934           #hidden inputs for memeory
  935           #remember gridify
  936           print CGI::hidden(-id=>'pastGridded', -name=>'gridify', -value=>$gridded, -override=>1);
  937           print CGI::hidden(-id=>'isReordered', -name=>'isReordered', -vale=>0 ,-override=>0);
  938           print CGI::hidden(-name=>'browse_which', -value=>$browse_which,-override=>1),
  939           CGI::hidden(-name=>'problem_seed', -value=>$problem_seed, -override=>1);
  940           for ($j = 0 ; $j < scalar(@pg_files) ; $j++) {
  941             print CGI::hidden(-name=>"all_past_list$j", -value=>$pg_files[$j],-override=>1);
  942           }
  943           print CGI::hidden(-name=>'first_shown', -value=>$first_shown,-override=>1);
  944           print CGI::hidden(-name=>'last_shown', -value=>$last_shown, -override=>1);
  945           print '<div id="setmaker_library_problems" class="problemList">';
  946           ########## Now print problems
  947             for ($jj=0; $jj<scalar(@pg_html); $jj++) {
  948               $pg_files[$jj] =~ s|^$ce->{courseDirs}->{templates}/?||;
  949               $self->make_data_row($pg_files[$jj+$first_shown], $pg_html[$jj], $jj+1,         $self->{past_marks}->[$jj]);
  950             }
  951           print '</div>';
  952           print '<p><button type="button" onclick="increaseLibAcross();">+</button><span id="libAcross">4</span><button type="button" onclick="decreaseLibAcross();">-</button><span> problems across</span></p>';
  953           ########## Finish things off
  954           my ($next_button, $prev_button) = ("", "");
  955           if ($first_shown > 0) {
  956             #$prev_button = CGI::submit(-name=>"prev_page", -style=>"width:15ex", -value=>"Previous page");
  957             my $urlpath = $r->urlpath;
  958             my $getProblemPath = $self->systemLink($urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::GetLibrarySetProblems", courseID =>$urlpath->arg("courseID")));
  959             $prev_button = "<span><button type='button' onclick='viewProblems(\"prev_page\" ,\"mainform\", \"".$getProblemPath."\", \"prev_spinner\");'>Previous page</button><img id='prev_spinner' style='display:none;' src='/webwork2_files/images/ajax-loader-small.gif'></img></span>";
  960           }
  961           if ((1+$last_shown)<scalar(@pg_files)) {
  962             #$next_button = CGI::submit(-name=>"next_page", -style=>"width:15ex", -value=>"Next page");
  963             my $urlpath = $r->urlpath;
  964             my $getProblemPath = $self->systemLink($urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::GetLibrarySetProblems", courseID =>$urlpath->arg("courseID")));
  965             $next_button = "<span><button type='button' onclick='viewProblems(\"next_page\" ,\"mainform\", \"".$getProblemPath."\", \"next_spinner\");'>Next page</button><img id='next_spinner' style='display:none;' src='/webwork2_files/images/ajax-loader-small.gif'></img></span>";
  966           }
  967           if (scalar(@pg_files)>0) {
  968             my $urlpath = $r->urlpath;
  969             my $getProblemPath = $self->systemLink($urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::SetMaker2", courseID =>$urlpath->arg("courseID")));
  970             print CGI::p(($first_shown+1)."-".($last_shown+1)." of ".scalar(@pg_files)." shown.", $prev_button, " ", $next_button,
  971             '<span><button type="button" style="float:right;" onclick="saveChanges(\'mainform\', \''.$getProblemPath.'\', \'save_changes_spinner2\');">save changes</button><img id="save_changes_spinner2" style="display:none;float:right;" src="/webwork2_files/images/ajax-loader-small.gif"></img></span>');
  972           }
  973           #close setmaker_library_data
  974           print '</div>';
  975           #close setmaker_library
  976         print '</div>';
  977 
  978 
  979   return "";
  980 }
  981 
  982 =head1 AUTHOR
  983 
  984 Written by John Jones, jj (at) asu.edu.
  985 Edited by David Gage
  986 
  987 =cut
  988 
  989 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9