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