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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1198 - (download) (as text) (annotate)
Wed Jun 18 16:35:53 2003 UTC (10 years ago) by sh002i
Original Path: trunk/webwork2/lib/WeBWorK/ContentGenerator/Instructor/PGProblemEditor.pm
File size: 10889 byte(s)
now sends editMode=savedFile if the "Save" button is pressed, and
doesn't send the "submit_button" parameter.

    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->{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     if ($submit_button eq 'Save') {
   54       $viewURL       .= "&editMode=savedFile";
   55     } else {
   56       $viewURL       .= "&editMode=temporaryFile";
   57     }
   58     $viewURL       .= '&sourceFilePath='. $self->{currentSourceFilePath}; # path to pg text for viewing
   59     #$viewURL      .= "&submit_button=$submit_button";                   # allows Problem.pg to recognize state
   60 #   $viewURL       .=   '&editErrors='.$self->{editErrors};                                        # of problem being viewed.
   61     $r->header_out(Location => $viewURL );
   62     return REDIRECT;
   63   } else {
   64     # initialize and
   65     # display the editing window
   66 
   67     $self->SUPER::go(@_);
   68   }
   69 
   70 }
   71 
   72 sub initialize {
   73 
   74   my ($self, $setName, $problemNumber) = @_;
   75   my $ce            =   $self->{ce};
   76   my $r           = $self->{r};
   77   my $path_info         =   $r->path_info || "";
   78   my $db            = $self->{db};
   79   my $user          = $r->param('user');
   80   my $effectiveUserName   = $r->param('effectiveUser');
   81   my $courseName        = $ce->{courseName};
   82 
   83 #   FIXME -- sometimes this doesn't find a set
   84 # my $set                 =   $db->getMergedSet($effectiveUserName, $setName);
   85 # my $setID         = $set->set_id;
   86 
   87   # Find URL for viewing problem
   88 
   89   # find path to pg file for the problem
   90   # FIXME  there is a discrepancy in the way that the problems are found.
   91   # FIXME  more error checking is needed in case the problem doesn't exist.
   92   # my $problem_record    = $db->getUserProblem($user,$setID,1);
   93   my $problem_record      = $db->getMergedProblem($effectiveUserName, $setName, $problemNumber);
   94   # If there is no global_user defined problem, (i.e. the sets haven't been assigned yet), then look for a global version of the problem.
   95   $problem_record         = $db->getGlobalProblem($setName, $problemNumber) unless defined($problem_record);
   96   die "Cannot find a problem record for set $setName / problem $problemNumber"
   97     unless defined($problem_record);
   98   my $templateDirectory   = $ce->{courseDirs}->{templates};
   99   my $problemPath       = $templateDirectory."/".$problem_record->source_file;
  100   my $editFileSuffix      = 'tmp';
  101   my $submit_button     =   $r->param('submit');
  102 
  103   my $displayMode         =   ( defined($r->param('displayMode'))   ) ? $r->param('displayMode') : $ce->{pg}->{options}->{displayMode};
  104   # try to get problem seed from the input parameter, or from the problem record
  105   my $problemSeed;
  106   if ( defined($r->param('problemSeed'))  ) {
  107     $problemSeed            =   $r->param('problemSeed');
  108   } elsif ($problem_record->can('problem_seed')) {
  109     $problemSeed            =   $problem_record->problem_seed;
  110   }
  111   # make absolutely sure that the problem seed is defined, if it hasn't been.
  112   $problemSeed        = '123456' unless defined($problemSeed) and $problemSeed =~/\S/;
  113 
  114   my $problemContents = '';
  115   my $currentSourceFilePath = '';
  116   my $editErrors = '';
  117   # update the .pg and .pg.tmp files in the directory
  118   if (not defined($submit_button) ) {
  119     # this is a fresh editing job
  120     # copy the pg file to a new file with the same name with .tmp added
  121     # store this name in the $self->currentSourceFilePath for use in body
  122 
  123     eval { $problemContents     = WeBWorK::Utils::readFile($problemPath)
  124     };  # try to read file
  125     $problemContents = $@ if $@;
  126     $editErrors .= $problemContents;
  127     $currentSourceFilePath      = "$problemPath.$editFileSuffix";
  128     $self->{currentSourceFilePath}  = $currentSourceFilePath;
  129   } elsif ($submit_button eq 'Refresh' ) {
  130     # grab the problemContents from the form in order to save it to the tmp file
  131     # store tmp file name in the $self->currentSourceFilePath for use in body
  132 
  133     $problemContents        = $r->param('problemContents');
  134     $currentSourceFilePath      = "$problemPath.$editFileSuffix";
  135     $self->{currentSourceFilePath}  = $currentSourceFilePath;
  136   } elsif ($submit_button eq 'Save') {
  137     # grab the problemContents from the form in order to save it to the permanent file
  138     # later we will unlink (delete) the temporary file
  139     # store permanent file name in the $self->currentSourceFilePath for use in body
  140 
  141     $problemContents        = $r->param('problemContents');
  142     $currentSourceFilePath      = "$problemPath";
  143     $self->{currentSourceFilePath}  = $currentSourceFilePath;
  144   } else {
  145     # give a warning
  146     die "Unrecognized submit command $submit_button";
  147   }
  148 
  149   # Handle the problem of line endings.  Make sure that all of the line endings.  Convert \r\n to \n
  150   $problemContents    =~    s/\r\n/\n/g;
  151   $problemContents    =~    s/\r/\n/g;
  152 
  153   # print changed pg files
  154   # FIXME  make sure that the permissions are set correctly!!!
  155   # Make sure that the warning is being transmitted properly.
  156 
  157   eval {
  158     local *OUTPUTFILE;
  159     open OUTPUTFILE, ">", $currentSourceFilePath
  160         or die "Failed to write to $currentSourceFilePath: $!";
  161     print OUTPUTFILE $problemContents;
  162     close OUTPUTFILE;
  163   };
  164   # record an error string for later use if there was a difficulty in writing to the file
  165   # FIXME is this string every inspected?
  166 
  167   my $openTempFileErrors = $@ if $@;
  168 
  169   if (  $openTempFileErrors)   {
  170 
  171     $self->{openTempFileErrors} = "Unable to write to $currentSourceFilePath: $openTempFileErrors";
  172     #diagnose errors:
  173     warn "Editing errors: $openTempFileErrors\n";
  174     warn "The file $currentSourceFilePath exists. \n " if -e $currentSourceFilePath; #FIXME
  175     warn "The file $currentSourceFilePath cannot be found. \n " unless -e $currentSourceFilePath;
  176     warn "The file $currentSourceFilePath does not have write permissions. \n"
  177                      if -e $currentSourceFilePath and not -w $currentSourceFilePath;
  178 
  179 
  180 
  181   } else {
  182     # unlink the temporary file if there are no errors and the save button has been pushed
  183 
  184     $self->{openTempFileErrors} = '';
  185     unlink("$problemPath.$editFileSuffix") if defined($submit_button) and $submit_button eq 'Save';
  186   };
  187 
  188 
  189   # return values for use in the body subroutine
  190   $self->{problemPath}    =   $problemPath;
  191   $self->{displayMode}    =   $displayMode;
  192   $self->{problemSeed}    =   $problemSeed;
  193 
  194   # FIXME  there is no way to edit in a temporary file -- all editing takes place on disk!!!
  195 
  196 
  197 
  198 }
  199 
  200 sub body {
  201   my $self = shift;
  202 
  203   # test area
  204   my $r       =   $self->{r};
  205   my $db      =   $self->{db};
  206   my $ce      =   $self->{ce};
  207   my $user    =   $r->param('user');
  208   my $key     =   $db->getKey($user)->key();
  209 
  210 
  211   ################
  212   # Gathering info
  213   # What is needed
  214   #     $problemPath  --
  215   #     $formURL -- given by $r->uri
  216   #     $tmpProblemPath
  217   my $problemPath   =   $self->{problemPath};
  218 
  219 
  220 
  221 
  222 
  223 
  224 
  225   my $header = "Problem Editor:  $problemPath";
  226 
  227   #########################################################################
  228   # Find the text for the problem, either in the tmp file, if it exists
  229   # or in the original file in the template directory
  230   #########################################################################
  231   my $problemContents = '';
  232 
  233   eval { $problemContents = WeBWorK::Utils::readFile($problemPath)  };  # try to read file
  234   $problemContents = $@ if $@;
  235 
  236 
  237 
  238   #########################################################################
  239   # Format the page
  240   #########################################################################
  241   # Define parameters for textarea
  242   # FIXME
  243   # Should the seed be set from some particular user instance??
  244   # The mode list should be obtained from global.conf ultimately
  245   my $rows    =   20;
  246   my $columns   =   80;
  247   my $mode_list   =   ['plainText','formattedText','images'];
  248   my $displayMode =   $self->{displayMode};
  249   my $problemSeed = $self->{problemSeed};
  250   my $uri     = $r->uri;
  251   ########################################################################
  252   # Define a link to view the problem
  253   #FIXME
  254 
  255   #########################################################################
  256 
  257 
  258   warn "Errors in the problem ".CGI::br().$self->{editErrors} if $self->{editErrors};
  259 
  260 
  261   return CGI::p($header),
  262     #CGI::start_form("POST",$r->uri,-target=>'_problem'),  doesn't pass on the target parameter???
  263     qq!<form method="POST" action="$uri" enctype="application/x-www-form-urlencoded", target="_problem">!,
  264     $self->hidden_authen_fields,
  265     CGI::div(
  266     CGI::textfield(-name=>'problemSeed',-value=>$problemSeed),
  267     'Mode: ',
  268     CGI::popup_menu(-name=>'displayMode', -'values'=>$mode_list,
  269                            -default=>$displayMode),
  270     CGI::a(
  271       {-href=>'http://webwork.math.rochester.edu/docs/docs/pglanguage/manpages/',-target=>"manpage_window"},
  272       'Manpages',
  273       )
  274     ),
  275     CGI::p(
  276       CGI::textarea(-name => 'problemContents', -default => $problemContents,
  277               -rows => $rows, -columns => $columns, -override => 1,
  278       ),
  279     ),
  280     CGI::p(
  281       CGI::submit(-value=>'Refresh',-name=>'submit'),
  282       CGI::submit(-value=>'Save',-name=>'submit'),
  283 #     $actionString
  284     ),
  285 
  286     #CGI::a({-href=>$ce->{viewProblemURL},-target=>'_viewProblem'},'view problem'),
  287     CGI::end_form(),
  288 #   "<p> the parameters passed are "  #FIXME -- debugging code
  289 #   . join("<BR>", %{$r->param()}) .
  290 #   "</p> and the gatheredInfo is ",
  291 #   "problemPath=$problemPath<br> formURL=".$r->uri . "<br>"   ,
  292 #   "viewProblemURL ".$ce->{viewProblemURL}."<br>",
  293 #   "problem_obj =". $ce->{problem_obj}."<br>",
  294 #   "path_components ". $ce->{path_components}.'<br>',
  295 #     "hostname =$hostname<br>",
  296 #     "port =$port <br>",
  297 #     "uri = $uri <br>",
  298 #     "viewURL =".$ce->{viewURL}."<br>",
  299 
  300   ;
  301 
  302 
  303 }
  304 
  305 
  306 
  307 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9