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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 983 - (download) (as text) (annotate)
Tue Jun 3 17:48:35 2003 UTC (9 years, 11 months ago) by malsyned
File size: 12555 byte(s)
Changed explicit references to "/webwork" to the abstract
$ce->{webworkURLs}->{root}.  If you've been hardcoding "/webwork" into
your URLs, you should take a look at the diff to this update to learn
the prefered method.
-Dennis

    1 package WeBWorK::ContentGenerator::Instructor::PGProblemEditor;
    2 use base qw(WeBWorK::ContentGenerator::Instructor);
    3 
    4 
    5 =head1 NAME
    6 
    7 WeBWorK::ContentGenerator::Instructor::ProblemSetEditor - Edit a set definition list
    8 
    9 =cut
   10 
   11 use strict;
   12 use warnings;
   13 use CGI qw();
   14 use WeBWorK::Utils qw(readFile);
   15 use Apache::Constants qw(:common REDIRECT);
   16 
   17 
   18 our $libraryName;
   19 our $rowheight;
   20 
   21 sub title {
   22   my $self = shift;
   23   #FIXME  don't need the entire path  ??
   24   return "Instructor Tools - PG Problem Editor for ". $self->{ce}->{problemPath};
   25 }
   26 sub go {
   27   my $self      = shift;
   28   my ($setName, $problemNumber) = @_;
   29   my $r         =   $self->{r};
   30   my $ce        = $self->{ce};
   31   my $submit_button   = $r->param('submit');  # obtain submit command from form
   32 
   33   # various actions depending on state.
   34   if (     defined($submit_button) and ($submit_button eq 'Save' or $submit_button eq 'Refresh')    ) {
   35 
   36     $self->initialize($setName,$problemNumber);  # write the necessary files
   37                            # return file path for viewing problem
   38                            # in $self->{currentSourceFilePath}
   39     #redirect to view the problem
   40 
   41     my $hostname    =   $r->hostname();
   42     my $port        =   $r->get_server_port();
   43     my $uri       =   $r->uri;
   44     my $courseName    = $self->{ce}->{courseName};
   45     my $editFileSuffix  = $self->{ce}->{editFileSuffix};
   46     my $problemSeed   =   ($r->param('problemSeed')) ? $r->param('problemSeed') : '';
   47     my $displayMode   = ($r->param('displayMode')) ? $r->param('displayMode') : '';
   48 
   49     my $viewURL     =   "http://$hostname:$port";
   50     $viewURL       .=   $ce->{webworkURLs}->{root}."/$courseName/$setName/$problemNumber/?";
   51     $viewURL       .= $self->url_authen_args;
   52     $viewURL       .=   "&displayMode=$displayMode&problemSeed=$problemSeed";   # optional displayMode and problemSeed overrides
   53     $viewURL       .= "&editMode=temporaryFile";
   54     $viewURL       .= '&sourceFilePath='. $self->{currentSourceFilePath}; # path to pg text for viewing
   55     $viewURL       .= "&submit_button=$submit_button";                   # allows Problem.pg to recognize state
   56     $viewURL       .=   '&editErrors='.$self->{ce}->{editErrors};                                        # of problem being viewed.
   57     $r->header_out(Location => $viewURL );
   58     return REDIRECT;
   59   } else {
   60     # initialize and
   61     # display the editing window
   62 
   63     $self->SUPER::go(@_);
   64   }
   65 
   66 }
   67 
   68 sub initialize {
   69 
   70   my ($self, $setName, $problemNumber) = @_;
   71   my $ce            =   $self->{ce};
   72   my $r           = $self->{r};
   73   my $path_info         =   $r->path_info || "";
   74   my $db            = $self->{db};
   75   my $user          = $r->param('user');
   76   my $effectiveUserName   = $r->param('effectiveUser');
   77   my $courseName        = $ce->{courseName};
   78 
   79 #   FIXME -- sometimes this doesn't find a set
   80 # my $set                 =   $db->getGlobalUserSet($effectiveUserName, $setName);
   81 # my $setID         = $set->set_id;
   82 
   83   # Find URL for viewing problem
   84 
   85   # find path to pg file for the problem
   86   # FIXME  there is a discrepancy in the way that the problems are found.
   87   # FIXME  more error checking is needed in case the problem doesn't exist.
   88   # my $problem_record    = $db->getUserProblem($user,$setID,1);
   89   my $problem_record          =   $db->getGlobalUserProblem($effectiveUserName, $setName, $problemNumber);
   90   my $templateDirectory   = $ce->{courseDirs}->{templates};
   91   my $problemPath       = $templateDirectory."/".$problem_record->source_file;
   92   my $editFileSuffix      = 'tmp';
   93   my $submit_button     =   $r->param('submit');
   94 
   95   my $displayMode         =   ( defined($r->param('displayMode'))   ) ? $r->param('displayMode') : $ce->{pg}->{options}->{displayMode};
   96   my $problemSeed       = ( defined($r->param('problemSeed')) ) ? $r->param('problemSeed') : $problem_record->problem_seed;
   97   $problemSeed        = '1234' unless defined($problemSeed) and $problemSeed =~/\S/;
   98 
   99   my $problemContents = '';
  100   my $currentSourceFilePath = '';
  101   # update the .pg and .pg.tmp files in the directory
  102   if (not defined($submit_button) ) {
  103     # this is a fresh editing job
  104     # copy the pg file to a new file with the same name with .tmp added
  105     # store this name in the $self->currentSourceFilePath for use in body
  106 
  107     eval { $problemContents     = WeBWorK::Utils::readFile($problemPath)
  108     };  # try to read file
  109     $problemContents = $@ if $@;
  110     $currentSourceFilePath      = "$problemPath.$editFileSuffix";
  111     $self->{currentSourceFilePath}  = $currentSourceFilePath;
  112   } elsif ($submit_button eq 'Refresh' ) {
  113     # grab the problemContents from the form in order to save it to the tmp file
  114     # store tmp file name in the $self->currentSourceFilePath for use in body
  115 
  116     $problemContents        = $r->param('problemContents');
  117     $currentSourceFilePath      = "$problemPath.$editFileSuffix";
  118     $self->{currentSourceFilePath}  = $currentSourceFilePath;
  119   } elsif ($submit_button eq 'Save') {
  120     # grab the problemContents from the form in order to save it to the permanent file
  121     # later we will unlink (delete) the temporary file
  122     # store permanent file name in the $self->currentSourceFilePath for use in body
  123 
  124     $problemContents        = $r->param('problemContents');
  125     $currentSourceFilePath      = "$problemPath";
  126     $self->{currentSourceFilePath}  = $currentSourceFilePath;
  127   } else {
  128     # give a warning
  129     die "Unrecognized submit command $submit_button";
  130   }
  131   # print changed pg files
  132   # FIXME  make sure that the permissions are set correctly!!!
  133   # Make sure that the warning is being transmitted properly.
  134   eval {
  135     local *OUTPUTFILE;
  136     open OUTPUTFILE, ">", $currentSourceFilePath
  137         or die "Failed to write to $currentSourceFilePath: $!";
  138     print OUTPUTFILE $problemContents;
  139     close OUTPUTFILE;
  140   };
  141   # record an error string for later use if there was a difficulty in writing to the file
  142   # FIXME is this string every inspected?
  143   my $errors = $@ if $@;
  144   if (  $errors)   {
  145 
  146     $self->{editErrors} = "Unable to write to $currentSourceFilePath: $errors";
  147 
  148 
  149   } else {
  150     # unlink the temporary file if there are no errors and the save button has been pushed
  151 
  152     $self->{editErrors} = '';
  153     unlink("$problemPath.$editFileSuffix") if defined($submit_button) and $submit_button eq 'Save';
  154   };
  155 
  156 
  157   # return values for use in the body subroutine
  158   $self->{problemPath}    =   $problemPath;
  159   $self->{displayMode}    =   $displayMode;
  160   $self->{problemSeed}    =   $problemSeed;
  161 
  162   # FIXME  there is no way to edit in a temporary file -- all editing takes place on disk!!!
  163 
  164 
  165 
  166 }
  167 
  168 sub body {
  169   my $self = shift;
  170 
  171   # test area
  172   my $r       =   $self->{r};
  173   my $db      =   $self->{db};
  174   my $ce      =   $self->{ce};
  175   my $user    =   $r->param('user');
  176   my $key     =   $db->getKey($user)->key();
  177 
  178 
  179   ################
  180   # Gathering info
  181   # What is needed
  182   #     $problemPath  --
  183   #     $formURL -- given by $r->uri
  184   #     $tmpProblemPath
  185   my $problemPath   =   $self->{problemPath};
  186 
  187 
  188 
  189 
  190 
  191 
  192 
  193   my $header = "Problem Editor:  $problemPath";
  194 
  195   #########################################################################
  196   # Find the text for the problem, either in the tmp file, if it exists
  197   # or in the original file in the template directory
  198   #########################################################################
  199   my $problemContents = '';
  200 #   my $editMode    = (defined($r->param('problemContents')))?
  201 #                 'tmpMode':'startMode';
  202 #
  203 #   if ( $editMode eq 'tmpMode') {
  204 #     $problemContents  = $r->param('problemContents');
  205 #
  206 #   } else{
  207   eval { $problemContents = WeBWorK::Utils::readFile($problemPath)  };  # try to read file
  208   $problemContents = $@ if $@;
  209 # }
  210 
  211   #  save Action  FIXME  -- is this the write place for this?
  212 #   my $actionString = '';
  213 #   if ($r->param('submit') eq 'Save') {
  214 #     $actionString = "File saved to $problemPath";
  215 #     #FIXME  it would be MUCH better to work with temporary files
  216 #     open(FILE,">$problemPath") or die "Can't open $problemPath";
  217 #     print FILE $problemContents;
  218 #     close(FILE);
  219 #
  220 # }
  221 
  222 
  223 
  224 
  225   #########################################################################
  226   # Format the page
  227   #########################################################################
  228   # Define parameters for textarea
  229   # FIXME
  230   # Should the seed be set from some particular user instance??
  231   # The mode list should be obtained from global.conf ultimately
  232   my $rows    =   20;
  233   my $columns   =   80;
  234   my $mode_list   =   ['plainText','formattedText','images'];
  235   my $displayMode =   $self->{displayMode};
  236   my $problemSeed = $self->{problemSeed};
  237   my $uri     = $r->uri;
  238   ########################################################################
  239   # Define a link to view the problem
  240   #FIXME
  241 
  242   #########################################################################
  243 
  244 
  245 
  246 
  247 
  248   return CGI::p($header),
  249     #CGI::start_form("POST",$r->uri,-target=>'_problem'),  doesn't pass on the target parameter???
  250     qq!<form method="POST" action="$uri" enctype="application/x-www-form-urlencoded", target="_problem">!,
  251     $self->hidden_authen_fields,
  252     CGI::div(
  253     CGI::textfield(-name=>'problemSeed',-value=>$problemSeed),
  254     'Mode: ',
  255     CGI::popup_menu(-name=>'displayMode', -'values'=>$mode_list,
  256                            -default=>$displayMode),
  257     CGI::a(
  258       {-href=>'http://webwork.math.rochester.edu/docs/docs/pglanguage/manpages/',-target=>"manpage_window"},
  259       'Manpages',
  260       )
  261     ),
  262     CGI::p(
  263       CGI::textarea(-name => 'problemContents', -default => $problemContents,
  264               -rows => $rows, -columns => $columns, -override => 1,
  265       ),
  266     ),
  267     CGI::p(
  268       CGI::submit(-value=>'Refresh',-name=>'submit'),
  269       CGI::submit(-value=>'Save',-name=>'submit'),
  270 #     $actionString
  271     ),
  272 
  273     #CGI::a({-href=>$ce->{viewProblemURL},-target=>'_viewProblem'},'view problem'),
  274     CGI::end_form(),
  275 #   "<p> the parameters passed are "  #FIXME -- debugging code
  276 #   . join("<BR>", %{$r->param()}) .
  277 #   "</p> and the gatheredInfo is ",
  278 #   "problemPath=$problemPath<br> formURL=".$r->uri . "<br>"   ,
  279 #   "viewProblemURL ".$ce->{viewProblemURL}."<br>",
  280 #   "problem_obj =". $ce->{problem_obj}."<br>",
  281 #   "path_components ". $ce->{path_components}.'<br>',
  282 #     "hostname =$hostname<br>",
  283 #     "port =$port <br>",
  284 #     "uri = $uri <br>",
  285 #     "viewURL =".$ce->{viewURL}."<br>",
  286 
  287   ;
  288 
  289 
  290 }
  291 
  292 
  293 # sub gatherProblemList {   #workaround for obtaining the definition of a problem set (awaiting implementation of db function)
  294 #   my $self = shift;
  295 #   my $setName = shift;
  296 #   my $output = "";
  297 #   if ( defined($setName) and $setName ne "" ) {
  298 #     my $templateDirectory = $self->{ce}->{courseDirs}->{templates};
  299 #     my $fileName = "$templateDirectory/$setName.def";
  300 #     my @output =  split("\n",WeBWorK::Utils::readFile($fileName) );
  301 #     @output = grep  /\.pg/,   @output;     # only get the .pg files
  302 #     @output = grep  !/Header/, @output;   # eliminate header files
  303 #     $output = join("\n",@output);
  304 #   } else {
  305 #     $output = "No set name |$setName| is defined";
  306 #   }
  307 #
  308 #
  309 #   return  $output
  310 #
  311 #
  312 #
  313 #
  314 # }
  315 # sub fetchSetDirectories {
  316 #
  317 #   my $self = shift;
  318 #   my $defaultChoice = shift;
  319 #   my $templateDirectory = $self->{ce}->{courseDirs}->{templates};
  320 #   opendir SETDEFDIR, $templateDirectory
  321 #     or return "Can't open directory $templateDirectory";
  322 #
  323 #   my @allFiles = grep !/^\./, readdir SETDEFDIR;
  324 #   closedir  SETDEFDIR;
  325 #
  326 #   ## filter to find only the set directories
  327 #   ## -- it is assumed that these directories don't contain a period in their names
  328 #   ## and that all other files do.  Directories names must also begin with "set".
  329 #   ## A better plan would be to read only the names of directories, not files.
  330 #
  331 #   ## sort the directories
  332 #   my @setDefFiles = grep /^set[^\.]*$/, @allFiles;
  333 #   my @sortedNames = sort @setDefFiles;
  334 #
  335 #   return "$libraryName/" . CGI::br(). CGI::popup_menu(-name=>'setDirectory', -size=>$rowheight,
  336 #    -values=>\@sortedNames, -default=>$defaultChoice ) .CGI::br() ;
  337 # }
  338 #
  339 # sub fetchPGproblems {
  340 #
  341 #   my $self = shift;
  342 #   my $setDirectory = shift;
  343 #
  344 #   # Handle default for setDirectory
  345 #   # fix me -- this is not bullet proof
  346 #   $setDirectory = "set0" unless defined($setDirectory);
  347 #   my $templateDirectory = $self->{ce}->{courseDirs}->{templates};
  348 #
  349 #   ##
  350 #   opendir SETDEFDIR, "$templateDirectory/$setDirectory"
  351 #     or return "Can't open directory $templateDirectory/$setDirectory";
  352 #
  353 #   my @allFiles = grep !/^\./, readdir SETDEFDIR;
  354 #   closedir  SETDEFDIR;
  355 #
  356 #   ## filter to find only pg problems
  357 #   ## Some problems are themselves in directories (if they have auxiliary
  358 #   ## .png's for example.  This eventuallity needs to be handled.
  359 #
  360 #   ## sort the directories
  361 #   my @pgFiles = grep /\.pg$/, @allFiles;
  362 #   my @sortedNames = sort @pgFiles;
  363 #
  364 #   return "$setDirectory ". CGI::br() .
  365 #   CGI::popup_menu(-name=>'pgProblem', -size=>$rowheight, -multiple=>undef, -values=>\@sortedNames,  ) .
  366 #   CGI::br() ;
  367 # }
  368 
  369 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9