Parent Directory
|
Revision Log
Revision 981 -
(view)
(download)
(as text)
Original Path: trunk/webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/PGProblemEditor.pm
| 1 : | gage | 889 | 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 : | gage | 925 | use Apache::Constants qw(:common REDIRECT); |
| 16 : | gage | 889 | |
| 17 : | gage | 925 | |
| 18 : | gage | 889 | our $libraryName; |
| 19 : | our $rowheight; | ||
| 20 : | |||
| 21 : | sub title { | ||
| 22 : | my $self = shift; | ||
| 23 : | gage | 892 | #FIXME don't need the entire path ?? |
| 24 : | return "Instructor Tools - PG Problem Editor for ". $self->{ce}->{problemPath}; | ||
| 25 : | gage | 889 | } |
| 26 : | gage | 925 | 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 : | gage | 889 | |
| 33 : | gage | 925 | # 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 : | gage | 981 | # in $self->{currentSourceFilePath} |
| 39 : | gage | 925 | #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 : | gage | 928 | my $problemSeed = ($r->param('problemSeed')) ? $r->param('problemSeed') : ''; |
| 47 : | gage | 925 | 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 : | gage | 928 | $viewURL .= "&displayMode=$displayMode&problemSeed=$problemSeed"; # optional displayMode and problemSeed overrides |
| 53 : | gage | 925 | $viewURL .= "&editMode=temporaryFile"; |
| 54 : | gage | 981 | $viewURL .= '&sourceFilePath='. $self->{currentSourceFilePath}; # path to pg text for viewing |
| 55 : | $viewURL .= "&submit_button=$submit_button"; # allows Problem.pg to recognize state | ||
| 56 : | gage | 925 | $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 : | malsyned | 980 | 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 : | gage | 925 | |
| 79 : | malsyned | 980 | # 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 : | gage | 981 | # store this name in the $self->currentSourceFilePath for use in body |
| 106 : | malsyned | 980 | |
| 107 : | eval { $problemContents = WeBWorK::Utils::readFile($problemPath) | ||
| 108 : | }; # try to read file | ||
| 109 : | $problemContents = $@ if $@; | ||
| 110 : | $currentSourceFilePath = "$problemPath.$editFileSuffix"; | ||
| 111 : | gage | 981 | $self->{currentSourceFilePath} = $currentSourceFilePath; |
| 112 : | malsyned | 980 | } elsif ($submit_button eq 'Refresh' ) { |
| 113 : | gage | 981 | # 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 : | malsyned | 980 | |
| 116 : | $problemContents = $r->param('problemContents'); | ||
| 117 : | $currentSourceFilePath = "$problemPath.$editFileSuffix"; | ||
| 118 : | gage | 981 | $self->{currentSourceFilePath} = $currentSourceFilePath; |
| 119 : | malsyned | 980 | } elsif ($submit_button eq 'Save') { |
| 120 : | gage | 981 | # 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 : | malsyned | 980 | |
| 124 : | $problemContents = $r->param('problemContents'); | ||
| 125 : | $currentSourceFilePath = "$problemPath"; | ||
| 126 : | gage | 981 | $self->{currentSourceFilePath} = $currentSourceFilePath; |
| 127 : | malsyned | 980 | } 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 : | gage | 981 | # 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 : | malsyned | 980 | my $errors = $@ if $@; |
| 144 : | if ( $errors) { | ||
| 145 : | |||
| 146 : | gage | 981 | $self->{editErrors} = "Unable to write to $currentSourceFilePath: $errors"; |
| 147 : | malsyned | 980 | |
| 148 : | |||
| 149 : | gage | 981 | } 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 : | malsyned | 980 | }; |
| 155 : | |||
| 156 : | |||
| 157 : | gage | 981 | # return values for use in the body subroutine |
| 158 : | $self->{problemPath} = $problemPath; | ||
| 159 : | $self->{displayMode} = $displayMode; | ||
| 160 : | $self->{problemSeed} = $problemSeed; | ||
| 161 : | malsyned | 980 | |
| 162 : | # FIXME there is no way to edit in a temporary file -- all editing takes place on disk!!! | ||
| 163 : | |||
| 164 : | |||
| 165 : | |||
| 166 : | } | ||
| 167 : | |||
| 168 : | gage | 889 | sub body { |
| 169 : | my $self = shift; | ||
| 170 : | |||
| 171 : | # test area | ||
| 172 : | gage | 981 | 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 : | gage | 889 | |
| 178 : | |||
| 179 : | ################ | ||
| 180 : | # Gathering info | ||
| 181 : | # What is needed | ||
| 182 : | # $problemPath -- | ||
| 183 : | gage | 892 | # $formURL -- given by $r->uri |
| 184 : | gage | 889 | # $tmpProblemPath |
| 185 : | gage | 981 | my $problemPath = $self->{problemPath}; |
| 186 : | gage | 892 | |
| 187 : | gage | 889 | |
| 188 : | gage | 892 | |
| 189 : | gage | 889 | |
| 190 : | |||
| 191 : | |||
| 192 : | |||
| 193 : | gage | 925 | my $header = "Problem Editor: $problemPath"; |
| 194 : | gage | 889 | |
| 195 : | ######################################################################### | ||
| 196 : | gage | 892 | # 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 : | gage | 925 | # 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 : | gage | 902 | |
| 211 : | # save Action FIXME -- is this the write place for this? | ||
| 212 : | gage | 925 | # 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 : | gage | 889 | |
| 223 : | gage | 925 | |
| 224 : | |||
| 225 : | ######################################################################### | ||
| 226 : | # Format the page | ||
| 227 : | ######################################################################### | ||
| 228 : | gage | 892 | # Define parameters for textarea |
| 229 : | gage | 928 | # FIXME |
| 230 : | # Should the seed be set from some particular user instance?? | ||
| 231 : | # The mode list should be obtained from global.conf ultimately | ||
| 232 : | gage | 902 | my $rows = 20; |
| 233 : | my $columns = 80; | ||
| 234 : | gage | 928 | my $mode_list = ['plainText','formattedText','images']; |
| 235 : | my $displayMode = $self->{displayMode}; | ||
| 236 : | my $problemSeed = $self->{problemSeed}; | ||
| 237 : | gage | 925 | my $uri = $r->uri; |
| 238 : | gage | 902 | ######################################################################## |
| 239 : | # Define a link to view the problem | ||
| 240 : | #FIXME | ||
| 241 : | gage | 892 | |
| 242 : | gage | 889 | ######################################################################### |
| 243 : | |||
| 244 : | |||
| 245 : | |||
| 246 : | gage | 925 | |
| 247 : | |||
| 248 : | gage | 889 | return CGI::p($header), |
| 249 : | gage | 925 | #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 : | gage | 892 | $self->hidden_authen_fields, |
| 252 : | gage | 902 | CGI::div( |
| 253 : | gage | 928 | CGI::textfield(-name=>'problemSeed',-value=>$problemSeed), |
| 254 : | gage | 902 | 'Mode: ', |
| 255 : | gage | 928 | CGI::popup_menu(-name=>'displayMode', -'values'=>$mode_list, |
| 256 : | -default=>$displayMode), | ||
| 257 : | gage | 902 | CGI::a( |
| 258 : | {-href=>'http://webwork.math.rochester.edu/docs/docs/pglanguage/manpages/',-target=>"manpage_window"}, | ||
| 259 : | 'Manpages', | ||
| 260 : | ) | ||
| 261 : | gage | 892 | ), |
| 262 : | gage | 902 | CGI::p( |
| 263 : | CGI::textarea(-name => 'problemContents', -default => $problemContents, | ||
| 264 : | -rows => $rows, -columns => $columns, -override => 1, | ||
| 265 : | ), | ||
| 266 : | ), | ||
| 267 : | CGI::p( | ||
| 268 : | gage | 925 | CGI::submit(-value=>'Refresh',-name=>'submit'), |
| 269 : | gage | 902 | CGI::submit(-value=>'Save',-name=>'submit'), |
| 270 : | gage | 925 | # $actionString |
| 271 : | gage | 902 | ), |
| 272 : | gage | 925 | |
| 273 : | #CGI::a({-href=>$ce->{viewProblemURL},-target=>'_viewProblem'},'view problem'), | ||
| 274 : | gage | 902 | CGI::end_form(), |
| 275 : | gage | 929 | # "<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 : | gage | 925 | # "viewProblemURL ".$ce->{viewProblemURL}."<br>", |
| 280 : | # "problem_obj =". $ce->{problem_obj}."<br>", | ||
| 281 : | gage | 929 | # "path_components ". $ce->{path_components}.'<br>', |
| 282 : | gage | 925 | # "hostname =$hostname<br>", |
| 283 : | # "port =$port <br>", | ||
| 284 : | gage | 929 | # "uri = $uri <br>", |
| 285 : | gage | 925 | # "viewURL =".$ce->{viewURL}."<br>", |
| 286 : | gage | 892 | |
| 287 : | gage | 889 | ; |
| 288 : | |||
| 289 : | gage | 925 | |
| 290 : | gage | 889 | } |
| 291 : | |||
| 292 : | gage | 928 | |
| 293 : | gage | 889 | # 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 |