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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3815 - (view) (download) (as text)
Original Path: trunk/webwork2/lib/WeBWorK/ContentGenerator/Instructor/PGProblemEditor.pm

1 : sh002i 1663 ################################################################################
2 :     # WeBWorK Online Homework Delivery System
3 :     # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/
4 : sh002i 3815 # $CVSHeader: webwork2/lib/WeBWorK/ContentGenerator/Instructor/PGProblemEditor.pm,v 1.65 2005/12/14 00:16:50 gage Exp $
5 : sh002i 1663 #
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 : gage 889 package WeBWorK::ContentGenerator::Instructor::PGProblemEditor;
18 :     use base qw(WeBWorK::ContentGenerator::Instructor);
19 :    
20 :    
21 : gage 3718
22 : gage 889 =head1 NAME
23 :    
24 : toenail 2288 WeBWorK::ContentGenerator::Instructor::PGProblemEditor - Edit a pg file
25 : gage 889
26 :     =cut
27 :    
28 :     use strict;
29 :     use warnings;
30 :     use CGI qw();
31 : gage 2081 use WeBWorK::Utils qw(readFile surePathToFile);
32 : gage 925 use Apache::Constants qw(:common REDIRECT);
33 : gage 1980 use HTML::Entities;
34 : gage 3037 use URI::Escape;
35 : gage 3175 use WeBWorK::Utils;
36 : jj 2016 use WeBWorK::Utils::Tasks qw(fake_set fake_problem);
37 : gage 889
38 : sh002i 1924 ###########################################################
39 :     # This editor will edit problem files or set header files or files, such as course_info
40 :     # whose name is defined in the global.conf database
41 :     #
42 :     # Only files under the template directory ( or linked to this location) can be edited.
43 :     #
44 :     # The course information and problems are located in the course templates directory.
45 :     # Course information has the name defined by courseFiles->{course_info}
46 :     #
47 : gage 3175 # Only files under the template directory ( or linked to this location) can be edited.
48 : sh002i 1924 #
49 :     # editMode = temporaryFile (view the temp file defined by course_info.txt.user_name.tmp
50 :     # instead of the file course_info.txt)
51 : gage 3037 # this flag is read by Problem.pm and ProblemSet.pm, perhaps others
52 :     # The TEMPFILESUFFIX is "user_name.tmp" by default. It's definition should be moved to Instructor.pm #FIXME
53 : sh002i 1924 ###########################################################
54 : gage 925
55 : gage 3037 ###########################################################
56 :     # The behavior of this module is essentially defined
57 :     # by the values of $file_type and the submit button which is placed in $action
58 :     #############################################################
59 :     # File types which can be edited
60 :     #
61 :     # file_type eq 'problem'
62 :     # this is the most common type -- this editor can be called by an instructor when viewing any problem.
63 :     # the information for retrieving the source file is found using the problemID in order to look
64 :     # look up the source file path.
65 :     #
66 : gage 3718 # file_type eq 'source_path_for_problem_file'
67 : gage 3037 # This is the same as the 'problem' file type except that the source for the problem is found in
68 : gage 3718 # the parameter $r->param('sourceFilePath'). This path is relative to the templates directory
69 : gage 3037 #
70 :     # file_type eq 'set_header'
71 :     # This is a special case of editing the problem. The set header is often listed as problem 0 in the set's list of problems.
72 :     #
73 :     # file_type eq 'hardcopy_header'
74 :     # This is a special case of editing the problem. The hardcopy_header is often listed as problem 0 in the set's list of problems.
75 :     # But it is used instead of set_header when producing a hardcopy of the problem set in the TeX format, instead of producing HTML
76 :     # formatted version for use on the computer screen.
77 :     #
78 :     # filte_type eq 'course_info
79 :     # This allows editing of the course_info.txt file which gives general information about the course. It is called from the
80 :     # ProblemSets.pm module.
81 :     #
82 :     # file_type eq 'blank_problem'
83 :     # This is a special call which allows one to create and edit a new PG problem. The "stationery" source for this problem is
84 :     # stored in the conf/snippets directory and defined in global.conf by $webworkFiles{screenSnippets}{blankProblem}
85 :     #############################################################
86 : gage 3718 # Requested actions -- these and the file_type determine the state of the module
87 : gage 3037 # Save ---- action = save
88 :     # Save as ---- action = save_as
89 : gage 3718 # View Problem ---- action = view
90 :     # Add this problem to: ---- action = add_problem
91 :     # Make this set header for: ---- action = add_problem
92 : gage 3037 # Revert ---- action = revert
93 :     # no submit button defined ---- action = fresh_edit
94 :     ###################################################
95 :     #
96 :     # Determining which is the correct path to the file is a mess!!! FIXME
97 :     # The path to the file to be edited is eventually put in tempFilePath
98 :     #
99 : gage 3718 # (tempFilePath)(editFilePath)(forcedSourceFile)
100 :     #input parameter is: sourceFilePath
101 : gage 3037 #################################################################
102 :     # params read
103 :     # user
104 :     # effectiveUser
105 :     # submit
106 :     # file_type
107 :     # problemSeed
108 :     # displayMode
109 :     # edit_level
110 :     # make_local_copy
111 :     # sourceFilePath
112 :     # problemContents
113 :     # save_to_new_file
114 :     #
115 :    
116 : gage 3790 use constant ACTION_FORMS => [qw(view add_problem make_local_copy save save_as revert)]; #[qw(view save save_as revert add_problem add_header make_local_copy)];
117 : gage 889
118 : gage 3718 # permissions needed to perform a given action
119 :     use constant FORM_PERMS => {
120 :     view => "modify_student_data",
121 :     add_problem => "modify_student_data",
122 :     make_local_copy => "modify_student_data",
123 :     save => "modify_student_data",
124 :     save_as => "modify_student_data",
125 : gage 3790 # rename => "modify_student_data",
126 : gage 3718 revert => "modify_student_data",
127 :     };
128 :    
129 : gage 3790 our $BLANKPROBLEM = 'blankProblem.pg';
130 :     # use constant BLANKPROBLEM => 'blankProblem.pg'; # doesn't work because this constant needs to be used inside a match.
131 : gage 1747 sub pre_header_initialize {
132 : gage 3037 my ($self) = @_;
133 :     my $r = $self->r;
134 :     my $ce = $r->ce;
135 :     my $urlpath = $r->urlpath;
136 :     my $authz = $r->authz;
137 :     my $user = $r->param('user');
138 : gage 3718 $self->{courseID} = $urlpath->arg("courseID");
139 :     $self->{setID} = $r->urlpath->arg("setID") ; # using $r->urlpath->arg("setID") ||'' causes trouble with set 0!!!
140 :     $self->{problemID} = $r->urlpath->arg("problemID");
141 : sh002i 1924
142 : gage 3037 my $submit_button = $r->param('submit'); # obtain submit command from form
143 : gage 3718 my $actionID = $r->param('action');
144 : gage 3037 my $file_type = $r->param("file_type") || '';
145 : gage 3718 my $setName = $self->{setID};
146 :     my $problemNumber = $self->{problemID};
147 : gage 3037
148 : toenail 2305 # Check permissions
149 :     return unless ($authz->hasPermissions($user, "access_instructor_tools"));
150 :     return unless ($authz->hasPermissions($user, "modify_problem_sets"));
151 : gage 3037
152 : gage 3718 ##############################################################################
153 :     # displayMode and problemSeed
154 :     #
155 :     # Determine the display mode
156 :     # If $self->{problemSeed} was obtained within saveFileChanges from the problem_record
157 :     # then it can be overridden by the value obtained from the form.
158 :     # Insure that $self->{problemSeed} has some non-empty value
159 :     # displayMode and problemSeed
160 :     # will be needed for viewing the problem via redirect.
161 :     # They are also two of the parameters which can be set by the editor
162 :     ##############################################################################
163 :    
164 :     if (defined $r->param('displayMode')) {
165 :     $self->{displayMode} = $r->param('displayMode');
166 :     } else {
167 :     $self->{displayMode} = $ce->{pg}->{options}->{displayMode};
168 :     }
169 :    
170 :     # form version of problemSeed overrides version obtained from the the problem_record
171 :     # inside saveFileChanges
172 :     $self->{problemSeed} = $r->param('problemSeed') if (defined $r->param('problemSeed'));
173 :     # Make sure that the problem seed has some value
174 :     $self->{problemSeed} = '123456' unless defined $self->{problemSeed} and $self->{problemSeed} =~/\S/;
175 :    
176 :     ##############################################################################
177 : gage 3037 #############################################################################
178 :     # Save file to permanent or temporary file, then redirect for viewing
179 :     #############################################################################
180 :     #
181 :     # Any file "saved as" should be assigned to "Undefined_Set" and redirectoed to be viewed again in the editor
182 :     #
183 :     # Problems "saved" or 'refreshed' are to be redirected to the Problem.pm module
184 :     # Set headers which are "saved" are to be redirected to the ProblemSet.pm page
185 :     # Hardcopy headers which are "saved" are aso to be redirected to the ProblemSet.pm page
186 :     # Course_info files are redirected to the ProblemSets.pm page
187 :     ##############################################################################
188 :    
189 : toenail 2305
190 : gage 3037
191 :     ######################################
192 :     # Insure that file_type is defined
193 :     ######################################
194 :     # We have already read in the file_type parameter from the form
195 :     #
196 :     # If this has not been defined we are dealing with a set header
197 :     # or regular problem
198 :     if (defined($file_type) and ($file_type =~/\S/)) { #file_type is defined and is not blank
199 :     # file type is already defined -- do nothing
200 :     } else {
201 : gage 3718 # if "sourceFilePath" is defined in the form, then we are getting the path directly.
202 : gage 3037 # if the problem number is defined and is 0
203 :     # then we are dealing with some kind of
204 :     # header file. The default is 'set_header' which prints properly
205 :     # to the screen.
206 :     # If the problem number is not zero, we are dealing with a real problem
207 :     ######################################
208 :     if ( defined($r->param('sourceFilePath') and $r->param('sourceFilePath') =~/\S/) ) {
209 :     $file_type ='source_path_for_problem_file';
210 :     } elsif ( defined($problemNumber) ) {
211 :     if ( $problemNumber =~/^\d+$/ and $problemNumber == 0 ) { # if problem number is numeric and zero
212 :     $file_type = 'set_header' unless $file_type eq 'set_header'
213 :     or $file_type eq 'hardcopy_header';
214 :     } else {
215 :     $file_type = 'problem';
216 :     }
217 :    
218 :     }
219 :     }
220 :     die "The file_type variable has not been defined or is blank." unless defined($file_type) and $file_type =~/\S/;
221 : gage 3718 # clean up sourceFilePath, just in case
222 :     # double check that sourceFilePath is relative to the templates file
223 :     if ($file_type eq 'source_path_for_problem_file' ) {
224 :     my $templatesDirectory = $ce->{courseDirs}->{templates};
225 :     my $sourceFilePath = $r->param('sourceFilePath');
226 :     $sourceFilePath =~ s/$templatesDirectory//;
227 :     $sourceFilePath =~ s|^/||; # remove intial /
228 :     $self->{sourceFilePath} = $sourceFilePath;
229 :     }
230 : gage 3037 $self->{file_type} = $file_type;
231 :    
232 :     ##########################################
233 : gage 3718 # File type is one of: blank_problem course_info problem set_header hardcopy_header source_path_for_problem_file
234 : gage 3037 ##########################################
235 :     #
236 :     # Determine the path to the file
237 :     #
238 :     ###########################################
239 : gage 3718 $self->getFilePaths($setName, $problemNumber, $file_type);
240 :     #defines $self->{editFilePath} # path to the permanent file to be edited
241 :     # $self->{tempFilePath} # path to the permanent file to be edited has .tmp suffix
242 :     # $self->{inputFilePath} # path to the file for input, (might be a .tmp file)
243 :    
244 :    
245 :    
246 : gage 3037 ##########################################
247 : gage 3718 # Default problem contents
248 :     ##########################################
249 :     $self->{r_problemContents}= undef;
250 :    
251 :     ##########################################
252 : gage 3037 #
253 :     # Determine action
254 :     #
255 :     ###########################################
256 :    
257 : gage 3718 if ($actionID) {
258 :     unless (grep { $_ eq $actionID } @{ ACTION_FORMS() } ) {
259 :     die "Action $actionID not found";
260 :     }
261 :     # Check permissions
262 :     if (not FORM_PERMS()->{$actionID} or $authz->hasPermissions($user, FORM_PERMS()->{$actionID})) {
263 :     my $actionHandler = "${actionID}_handler";
264 :     my %genericParams =();
265 :     # foreach my $param (qw(selected_users)) {
266 :     # $genericParams{$param} = [ $r->param($param) ];
267 :     # }
268 :     my %actionParams = $self->getActionParams($actionID);
269 :     my %tableParams = (); # $self->getTableParams();
270 :     $self->{action}= $actionID;
271 :     $self->$actionHandler(\%genericParams, \%actionParams, \%tableParams);
272 :     } else {
273 :     $self->addbadmessage( "You are not authorized to perform this action.");
274 :     }
275 :     } else {
276 :     $self->{action}='fresh_edit';
277 :     my $actionHandler = "fresh_edit_handler";
278 :     my %genericParams;
279 :     my %actionParams = (); #$self->getActionParams($actionID);
280 :     my %tableParams = (); # $self->getTableParams();
281 :     my $problemContents = '';
282 :     $self->{r_problemContents}=\$problemContents;
283 :     $self->$actionHandler(\%genericParams, \%actionParams, \%tableParams);
284 :     }
285 :    
286 : gage 3037
287 :     ##############################################################################
288 :     # displayMode and problemSeed
289 :     #
290 :     # Determine the display mode
291 :     # If $self->{problemSeed} was obtained within saveFileChanges from the problem_record
292 :     # then it can be overridden by the value obtained from the form.
293 :     # Insure that $self->{problemSeed} has some non-empty value
294 :     # displayMode and problemSeed
295 :     # will be needed for viewing the problem via redirect.
296 :     # They are also two of the parameters which can be set by the editor
297 :     ##############################################################################
298 :    
299 :     if (defined $r->param('displayMode')) {
300 :     $self->{displayMode} = $r->param('displayMode');
301 :     } else {
302 :     $self->{displayMode} = $ce->{pg}->{options}->{displayMode};
303 :     }
304 :    
305 :     # form version of problemSeed overrides version obtained from the the problem_record
306 :     # inside saveFileChanges
307 :     $self->{problemSeed} = $r->param('problemSeed') if (defined $r->param('problemSeed'));
308 :     # Make sure that the problem seed has some value
309 :     $self->{problemSeed} = '123456' unless defined $self->{problemSeed} and $self->{problemSeed} =~/\S/;
310 :    
311 :     ##############################################################################
312 :     # Return
313 :     # If file saving fails or
314 :     # if no redirects are required. No further processing takes place in this subroutine.
315 :     # Redirects are required only for the following submit values
316 :     # 'Save'
317 :     # 'Save as'
318 :     # 'Refresh'
319 :     # add problem to set
320 :     # add set header to set
321 :     #
322 :     #########################################
323 :    
324 :     return if $self->{failure};
325 :     # FIXME: even with an error we still open a new page because of the target specified in the form
326 :    
327 :    
328 : gage 3667 # Some cases do not need a redirect: save, refresh, save_as, add_problem_to_set, add_header_to_set,make_local_copy
329 : gage 3037 my $action = $self->{action};
330 : gage 3718 return ;
331 : gage 3037
332 : gage 925 }
333 :    
334 : gage 3037
335 : gage 1747 sub initialize {
336 : sh002i 1924 my ($self) = @_;
337 :     my $r = $self->r;
338 : toenail 2305 my $authz = $r->authz;
339 :     my $user = $r->param('user');
340 : sh002i 1924
341 : toenail 2305 # Check permissions
342 :     return unless ($authz->hasPermissions($user, "access_instructor_tools"));
343 :     return unless ($authz->hasPermissions($user, "modify_problem_sets"));
344 : gage 3037
345 :     my $tempFilePath = $self->{tempFilePath}; # path to the file currently being worked with (might be a .tmp file)
346 :     my $inputFilePath = $self->{inputFilePath}; # path to the file for input, (might be a .tmp file)
347 : gage 3667
348 :     $self->addmessage($r->param('status_message') ||''); # record status messages carried over if this is a redirect
349 : gage 3037 $self->addbadmessage("Changes in this file have not yet been permanently saved.") if -r $tempFilePath;
350 : gage 3718 if ( not( -e $inputFilePath) ) {
351 :     $self->addbadmessage("This file: $inputFilePath, cannot be found.");
352 :     } elsif (not -w $inputFilePath ) {
353 :     $self->addbadmessage("This file '$inputFilePath' is protected! ".CGI::br()."To edit this text you must either 'Make a local copy' of this problem, or
354 :     use 'Save As' to save it to another file.");
355 :     }
356 : gage 3790 if ($inputFilePath =~/$BLANKPROBLEM$/) {
357 :     $self->addbadmessage("This file '$inputFilePath' is a blank problem! ".CGI::br()."To edit this text you must
358 :     use 'Save As' to save it to another file.");
359 :     }
360 : gage 3037
361 : sh002i 1924 }
362 : gage 1747
363 : gage 2456 sub path {
364 :     my ($self, $args) = @_;
365 :     my $r = $self->r;
366 :     my $urlpath = $r->urlpath;
367 :     my $courseName = $urlpath->arg("courseID");
368 : gage 2474 my $setName = $r->urlpath->arg("setID") || '';
369 :     my $problemNumber = $r->urlpath->arg("problemID") || '';
370 : gage 2456
371 :     # we need to build a path to the problem being edited by hand, since it is not the same as the urlpath
372 :     # For this page the bread crum path leads back to the problem being edited, not to the Instructor tool.
373 :     my @path = ( 'WeBWork', $r->location,
374 :     "$courseName", $r->location."/$courseName",
375 :     "$setName", $r->location."/$courseName/$setName",
376 :     "$problemNumber", $r->location."/$courseName/$setName/$problemNumber",
377 :     "Editor", ""
378 :     );
379 :    
380 :     #print "\n<!-- BEGIN " . __PACKAGE__ . "::path -->\n";
381 :     print $self->pathMacro($args, @path);
382 :     #print "<!-- END " . __PACKAGE__ . "::path -->\n";
383 :    
384 :     return "";
385 :     }
386 : jj 2029 sub title {
387 :     my $self = shift;
388 :     my $r = $self->r;
389 :     my $problemNumber = $r->urlpath->arg("problemID");
390 :     my $file_type = $self->{'file_type'} || '';
391 : toenail 2809 return "Set Header" if ($file_type eq 'set_header');
392 :     return "Hardcopy Header" if ($file_type eq 'hardcopy_header');
393 : jj 2029 return "Course Information" if($file_type eq 'course_info');
394 :     return 'Problem ' . $r->{urlpath}->name;
395 :     }
396 :    
397 : sh002i 1924 sub body {
398 :     my ($self) = @_;
399 :     my $r = $self->r;
400 :     my $db = $r->db;
401 :     my $ce = $r->ce;
402 : toenail 2305 my $authz = $r->authz;
403 :     my $user = $r->param('user');
404 : toenail 2809 my $make_local_copy = $r->param('make_local_copy');
405 : gage 3037
406 : toenail 2305 # Check permissions
407 :     return CGI::div({class=>"ResultsWithError"}, "You are not authorized to access the Instructor tools.")
408 : gage 3040 unless $authz->hasPermissions($user, "access_instructor_tools");
409 : sh002i 1924
410 : toenail 2305 return CGI::div({class=>"ResultsWithError"}, "You are not authorized to modify problems.")
411 : gage 3040 unless $authz->hasPermissions($user, "modify_student_data");
412 : toenail 2305
413 :    
414 : gage 3736
415 :    
416 : sh002i 1924 # Gathering info
417 : gage 3037 my $editFilePath = $self->{editFilePath}; # path to the permanent file to be edited
418 :     my $tempFilePath = $self->{tempFilePath}; # path to the file currently being worked with (might be a .tmp file)
419 :     my $inputFilePath = $self->{inputFilePath}; # path to the file for input, (might be a .tmp file)
420 : gage 3718 my $setName = $self->{setID} ;
421 :     my $problemNumber = $self->{problemID} ;
422 : gage 3062 $setName = defined($setName) ? $setName : ''; # we need this instead of using the || construction
423 :     # to keep set 0 from being set to the
424 :     # empty string.
425 :     $problemNumber = defined($problemNumber) ? $problemNumber : '';
426 :    
427 : gage 3736 #########################################################################
428 :     # Construct url for reporting bugs:
429 : sh002i 1924 #########################################################################
430 : gage 3736
431 : gage 3779 # $editFilePath =~ m|([^/]*)Library|; #find the path to the file
432 :     # my $libraryName = $1; # find the library, if any exists in the path name (first library is picked)
433 :     # $libraryName ='rochester' unless defined($libraryName) and $libraryName =~/\S/; # default library
434 :     my $libraryName = '';
435 :     if ($editFilePath =~ m|([^/]*)Library|) { #find the path to the file
436 :     # find the library, if any exists in the path name (first library is picked)
437 :     my $tempLibraryName = $1;
438 :     $libraryName = ( defined($tempLibraryName) and $tempLibraryName =~/\S/ ) ?
439 :     $tempLibraryName : "Library";
440 :     # things that start /Library/setFoo/probBar are labeled as component "Library"
441 :     # which refers to the SQL based problem library. (is nationalLibrary a better name?)
442 :     } else {
443 :     $libraryName = 'rochester'; # make sure there is some default component defined.
444 :     }
445 :    
446 :     my $BUGZILLA = "http://bugs.webwork.rochester.edu/enter_bug.cgi?product=Problem%20libraries".
447 :     "&component=$libraryName&bug_file_loc=${editFilePath}_with_problemSeed=".$self->{problemSeed};
448 : gage 3736 #FIXME # The construction of this URL is somewhat fragile. A separate module could be devoted to intelligent reporting of bugs.
449 :    
450 :     #########################################################################
451 : sh002i 1924 # Find the text for the problem, either in the tmp file, if it exists
452 :     # or in the original file in the template directory
453 : gage 3037 # or in the problem contents gathered in the initialization phase.
454 : sh002i 1924 #########################################################################
455 :    
456 :     my $problemContents = ${$self->{r_problemContents}};
457 : gage 3718
458 : gage 3037 unless ( $problemContents =~/\S/) { # non-empty contents
459 :     if (-r $tempFilePath and not -d $tempFilePath) {
460 :     eval { $problemContents = WeBWorK::Utils::readFile($tempFilePath) };
461 :     $problemContents = $@ if $@;
462 :     $inputFilePath = $tempFilePath;
463 :     } elsif (-r $editFilePath and not -d $editFilePath) {
464 :     eval { $problemContents = WeBWorK::Utils::readFile($editFilePath) };
465 :     $problemContents = $@ if $@;
466 :     $inputFilePath = $editFilePath;
467 :     } else { # file not existing is not an error
468 :     #warn "No file exists";
469 :     $problemContents = '';
470 :     }
471 :     } else {
472 :     #warn "obtaining input from r_problemContents";
473 :     }
474 : gage 3062
475 : gage 3037 my $protected_file = not -w $inputFilePath;
476 :     my $header = CGI::i("Editing problem".CGI::b("set $setName/ problem $problemNumber</emphasis>").CGI::br()." in file $inputFilePath");
477 : gage 3718 $header = ($self->isTempFilePath($inputFilePath) ) ? CGI::div({class=>'temporaryFile'},$header) : $header; # use colors if temporary file
478 : gage 3037
479 : sh002i 1924 #########################################################################
480 :     # Format the page
481 :     #########################################################################
482 :    
483 :     # Define parameters for textarea
484 :     # FIXME
485 :     # Should the seed be set from some particular user instance??
486 : gage 3037 my $rows = 20;
487 :     my $columns = 80;
488 :     my $mode_list = $ce->{pg}->{displayModes};
489 :     my $displayMode = $self->{displayMode};
490 :     my $problemSeed = $self->{problemSeed};
491 :     my $uri = $r->uri;
492 :     my $edit_level = $r->param('edit_level') || 0;
493 :     my $file_type = $self->{file_type};
494 : jj 2016
495 : gage 3718 my $force_field = (defined($self->{sourceFilePath}) and $self->{sourceFilePath} ne "") ?
496 : jj 2016 CGI::hidden(-name=>'sourceFilePath',
497 : gage 3718 -default=>$self->{sourceFilePath}) : '';
498 : toenail 2305
499 :     my @allSetNames = sort $db->listGlobalSets;
500 : jj 2291 for (my $j=0; $j<scalar(@allSetNames); $j++) {
501 :     $allSetNames[$j] =~ s|^set||;
502 :     $allSetNames[$j] =~ s|\.def||;
503 :     }
504 : gage 3718 my $target = "problem$edit_level"; # increasing edit_level gives you a new window with each edit.
505 :    
506 : gage 3037
507 : gage 3718 print CGI::p($header),
508 : toenail 2305 CGI::start_form({method=>"POST", name=>"editor", action=>"$uri", target=>$target, enctype=>"application/x-www-form-urlencoded"}),
509 : sh002i 1924 $self->hidden_authen_fields,
510 : jj 2016 $force_field,
511 : sh002i 1924 CGI::hidden(-name=>'file_type',-default=>$self->{file_type}),
512 : gage 3736 CGI::div(" | ",
513 : sh002i 1924 CGI::a({-href=>'http://webwork.math.rochester.edu/docs/docs/pglanguage/manpages/',-target=>"manpage_window"},
514 : gage 3433 '&nbsp;Manpages&nbsp;',
515 : gage 3736 )," | ",
516 : gage 3433 CGI::a({-href=>'http://devel.webwork.rochester.edu/twiki/bin/view/Webwork/PGmacrosByFile',-target=>"manpage_window"},
517 :     '&nbsp;macro list&nbsp;',
518 : gage 3736 )," | ",
519 : gage 3433 CGI::a({-href=>'http://devel.webwork.rochester.edu/doc/cvs/pg_HEAD/',-target=>"manpage_window"},
520 :     '&nbsp;pod docs&nbsp;',
521 : gage 3736 )," | ",
522 :     CGI::a({-href=>$BUGZILLA,-target=>"bugs_window"},
523 :     '&nbsp;report problem bugs&nbsp;',
524 :     )," | ",
525 : sh002i 1924 ),
526 :     CGI::p(
527 :     CGI::textarea(
528 :     -name => 'problemContents', -default => $problemContents,
529 :     -rows => $rows, -columns => $columns, -override => 1,
530 : gage 3736 )," | ",
531 : gage 3718 );
532 :    
533 :    
534 :    
535 :     ######### print action forms
536 :    
537 :     print CGI::start_table({});
538 :     #print CGI::Tr({}, CGI::td({-colspan=>2}, "Select an action to perform:"));
539 : gage 3037
540 : gage 3718 my @formsToShow = @{ ACTION_FORMS() };
541 :     my $default_choice = $formsToShow[0];
542 :     my $i = 0;
543 :     foreach my $actionID (@formsToShow) {
544 :     # Check permissions
545 :     #next if FORM_PERMS()->{$actionID} and not $authz->hasPermissions($user, FORM_PERMS()->{$actionID});
546 :     my $actionForm = "${actionID}_form";
547 :     my $onChange = "document.userlist.action[$i].checked=true";
548 :     my %actionParams = $self->getActionParams($actionID);
549 :     my $line_contents = $self->$actionForm($onChange, %actionParams);
550 :     my $radio_params = {-type=>"radio", -name=>"action", -value=>$actionID};
551 :     $radio_params->{checked}=1 if ($actionID eq $default_choice) ;
552 :     print CGI::Tr({-valign=>"top"},
553 :     CGI::td({}, CGI::input($radio_params)),
554 :     CGI::td({}, $line_contents)
555 :     ) if $line_contents;
556 :    
557 :     $i++;
558 :     }
559 :     print CGI::Tr({}, CGI::td({-align=>"right"}, "Select above then:"),
560 :     CGI::td({-align=>"left"}, CGI::submit(-name=>'submit', -value=>"Take Action!")),
561 :     );
562 :     print CGI::end_table();
563 :    
564 :    
565 :     print CGI::end_form();
566 :     return "";
567 : gage 3037
568 : gage 3718
569 : sh002i 1924 }
570 : gage 1747
571 : sh002i 1924 ################################################################################
572 :     # Utilities
573 :     ################################################################################
574 : gage 3667
575 :     # determineLocalFilePath constructs a local file path parallel to a library file path
576 :     # This is a subroutine, not a method
577 :     #
578 :     sub determineLocalFilePath {
579 : gage 3726 my $self= shift; die "determineLocalFilePath is a method" unless ref($self);
580 : gage 3667 my $path = shift;
581 : gage 3726 # my $default_screen_header_path = $self->r->ce->{webworkFiles}->{hardcopySnippets}->{setHeader};
582 :     # my $default_hardcopy_header_path = $self->r->ce->{webworkFiles}->{screenSnippets}->{setHeader};
583 :     my $setID = $self->{setID} || int(rand(1000));
584 : gage 3667 if ($path =~ /Library/) {
585 :     $path =~ s|^.*?Library/||; # truncate the url up to a segment such as ...rochesterLibrary/.......
586 : gage 3726 # } elsif ($path eq $default_screen_header_path) {
587 :     # $path = "set$setID/setHeader.pg";
588 :     # } elsif ($path eq $default_hardcopy_header_path) {
589 :     # $path = "set$setID/hardcopyHeader.tex";
590 : gage 3667 } else { # if its not in a library we'll just save it locally
591 : gage 3719 $path = "new_problem_".int(rand(1000)).".pg"; #l hope there aren't any collisions.
592 : gage 3667 }
593 :     $path;
594 :    
595 :     }
596 : gage 3718
597 :     sub determineTempFilePath { # this does not create the path to the file
598 :     my $self = shift; die "determineTempFilePath is a method" unless ref($self);
599 :     my $path =shift;
600 :     my $user = $self->r->param("user");
601 : gage 3719 $user = int(rand(1000)) unless defined $user;
602 :     my $setID = $self->{setID} || int(rand(1000));
603 : gage 3718 my $courseDirectory = $self->r->ce->{courseDirs};
604 :     ###############
605 :     # Calculate the location of the temporary file
606 :     ###############
607 :     my $templatesDirectory = $courseDirectory->{templates};
608 : gage 3726 my $blank_file_path = $self->r->ce->{webworkFiles}->{screenSnippets}->{blankProblem};
609 :     my $default_screen_header_path = $self->r->ce->{webworkFiles}->{hardcopySnippets}->{setHeader};
610 :     my $default_hardcopy_header_path = $self->r->ce->{webworkFiles}->{screenSnippets}->{setHeader};
611 : gage 3719 my $tmpEditFileDirectory = (defined ($courseDirectory->{tmpEditFileDir}) ) ? $courseDirectory->{tmpEditFileDir} : "$templatesDirectory/tmpEdit";
612 : gage 3718 if ($path =~ /^$templatesDirectory/ ) {
613 :     $path =~ s|^$templatesDirectory||;
614 :     $path =~ s|^/||; # remove the initial slash if any
615 : gage 3719 $path = "$tmpEditFileDirectory/$path.$user.tmp";
616 :     } elsif ($path eq $blank_file_path) {
617 :     $path = "$tmpEditFileDirectory/blank.$setID.$user.tmp"; # handle the case of the blank problem
618 : gage 3726 } elsif ($path eq $default_screen_header_path) {
619 :     $path = "$tmpEditFileDirectory/screenHeader.$setID.$user.tmp"; # handle the case of the screen header in snippets
620 :     } elsif ($path eq $default_hardcopy_header_path) {
621 :     $path = "$tmpEditFileDirectory/hardcopyHeader.$setID.$user.tmp"; # handle the case of the hardcopy header in snippets
622 : gage 3718 } else {
623 :     die "determineTempFilePath should only be used on paths within the templates directory, not on $path";
624 :     }
625 :     $path;
626 :     }
627 :     sub isTempFilePath {
628 :     my $self = shift;
629 :     my $path = shift;
630 :     my $courseDirectory = $self->r->ce->{courseDirs};
631 :     my $templatesDirectory = $courseDirectory->{templates};
632 :     my $tmpEditFileDirectory = (defined ($courseDirectory->{tmpEditFileDir}) ) ? $courseDirectory->{tmpEditFileDir} : "$templatesDirectory/tmpEdit";
633 :     ($path =~/^$tmpEditFileDirectory/) ? 1: 0;
634 :     }
635 : gage 3037 sub getFilePaths {
636 : gage 3718 my ($self, $setName, $problemNumber, $file_type) = @_;
637 : sh002i 1924 my $r = $self->r;
638 :     my $ce = $r->ce;
639 :     my $db = $r->db;
640 :     my $urlpath = $r->urlpath;
641 :     my $courseName = $urlpath->arg("courseID");
642 :     my $user = $r->param('user');
643 :     my $effectiveUserName = $r->param('effectiveUser');
644 : gage 3037
645 : sh002i 1924 $setName = '' unless defined $setName;
646 :     $problemNumber = '' unless defined $problemNumber;
647 : gage 3037 die 'Internal error to PGProblemEditor -- file type is not defined' unless defined $file_type;
648 :    
649 :     ##########################################################
650 :     # Determine path to the input file to be edited.
651 : gage 3718 # The permanent path of the input file == $editFilePath
652 :     # A temporary path to the input file == $tempFilePath
653 : gage 3037 ##########################################################
654 :     # Relevant parameters
655 :     # $r->param("displayMode")
656 :     # $r->param('problemSeed')
657 :     # $r->param('submit')
658 :     # $r->param('make_local_copy')
659 :     # $r->param('sourceFilePath')
660 :     # $r->param('problemContents')
661 :     # $r->param('save_to_new_file')
662 :     ##########################################################################
663 :     # Define the following variables
664 : gage 3718 # path to regular file -- $editFilePath;
665 :     # path to file being read (temporary or permanent)
666 : gage 3037 # contents of the file being read --- $problemContents
667 : gage 3718 # $self->{r_problemContents} = \$problemContents;
668 : gage 3037 ###########################################################################
669 :    
670 : sh002i 1924 my $editFilePath = $ce->{courseDirs}->{templates};
671 : gage 1348
672 : gage 3037 ##########################################################################
673 :     # Determine path to regular file, place it in $editFilePath
674 :     # problemSeed is defined for the file_type = 'problem' and 'source_path_to_problem'
675 :     ##########################################################################
676 :     CASE:
677 :     {
678 :     ($file_type eq 'course_info') and do {
679 :     # we are editing the course_info file
680 :     # value of courseFiles::course_info is relative to templates directory
681 :     $editFilePath .= '/' . $ce->{courseFiles}->{course_info};
682 :     last CASE;
683 :     };
684 : sh002i 1924
685 : gage 3037 ($file_type eq 'blank_problem') and do {
686 :     $editFilePath = $ce->{webworkFiles}->{screenSnippets}->{blankProblem};
687 : gage 3718 $self->addbadmessage("$editFilePath is blank problem template file and can not be edited directly. "
688 : gage 3667 ."First use 'Save as' to make a local copy, then add the file to the current problem set, then edit the file."
689 :     );
690 : gage 3037 last CASE;
691 :     };
692 : sh002i 1924
693 : gage 3037 ($file_type eq 'set_header' or $file_type eq 'hardcopy_header') and do {
694 :     # first try getting the merged set for the effective user
695 :     my $set_record = $db->getMergedSet($effectiveUserName, $setName); # checked
696 :     # if that doesn't work (the set is not yet assigned), get the global record
697 :     $set_record = $db->getGlobalSet($setName); # checked
698 :     # bail if no set is found
699 :     die "Cannot find a set record for set $setName" unless defined($set_record);
700 :    
701 :     my $header_file = "";
702 :     $header_file = $set_record->{$file_type};
703 :     if ($header_file && $header_file ne "") {
704 : toenail 2809 $editFilePath .= '/' . $header_file;
705 : gage 3037 } else {
706 : toenail 2809 # if the set record doesn't specify the filename
707 : gage 3037 # then the set uses the default from snippets
708 : toenail 2809 # so we'll load that file, but change where it will be saved
709 :     # to and grey out the "Save" button
710 : gage 3037 # FIXME why does the make_local_copy variable need to be checked?
711 :     # Isn't it automatic that a local copy has to be made?
712 :     #if ($r->param('make_local_copy')) {
713 :     $editFilePath = $ce->{webworkFiles}->{screenSnippets}->{setHeader} if $file_type eq 'set_header';
714 :     $editFilePath = $ce->{webworkFiles}->{hardcopySnippets}->{setHeader} if $file_type eq 'hardcopy_header';
715 : toenail 2809 $self->addbadmessage("$editFilePath is the default header file and cannot be edited directly.");
716 :     $self->addbadmessage("Any changes you make will have to be saved as another file.");
717 : gage 3037 #}
718 :     }
719 :     last CASE;
720 :     }; #end 'set_header, hardcopy_header' case
721 :    
722 :     ($file_type eq 'problem') and do {
723 :    
724 :     # first try getting the merged problem for the effective user
725 :     my $problem_record = $db->getMergedProblem($effectiveUserName, $setName, $problemNumber); # checked
726 :    
727 :     # if that doesn't work (the problem is not yet assigned), get the global record
728 :     $problem_record = $db->getGlobalProblem($setName, $problemNumber) unless defined($problem_record); # checked
729 :     # bail if no source path for the problem is found ;
730 :     die "Cannot find a problem record for set $setName / problem $problemNumber" unless defined($problem_record);
731 :     $editFilePath .= '/' . $problem_record->source_file;
732 :     # define the problem seed for later use
733 :     $self->{problemSeed}= $problem_record->problem_seed if defined($problem_record) and $problem_record->can('problem_seed') ;
734 :     last CASE;
735 :     }; # end 'problem' case
736 :    
737 :     ($file_type eq 'source_path_for_problem_file') and do {
738 : gage 3718 my $forcedSourceFile = $self->{sourceFilePath};
739 : gage 3037 # bail if no source path for the problem is found ;
740 :     die "Cannot find a file path to save to" unless( defined($forcedSourceFile) and ($forcedSourceFile =~ /\S/) );
741 :     $self->{problemSeed} = 1234;
742 :     $editFilePath .= '/' . $forcedSourceFile;
743 :     last CASE;
744 :     }; # end 'source_path_for_problem_file' case
745 :     } # end CASE: statement
746 : jj 2016
747 : gage 1348
748 : toenail 2809 # if a set record or problem record contains an empty blank for a header or problem source_file
749 :     # we could find ourselves trying to edit /blah/templates/.toenail.tmp or something similar
750 :     # which is almost undoubtedly NOT desirable
751 :    
752 :     if (-d $editFilePath) {
753 : gage 3037 my $msg = "The file $editFilePath is a directory!";
754 :     $self->{failure} = 1;
755 :     $self->addbadmessage($msg);
756 : toenail 2809 }
757 : gage 3037 if (-e $editFilePath and not -r $editFilePath) { #it's ok if the file doesn't exist, perhaps we're going to create it
758 :     # with save as
759 :     my $msg = "The file $editFilePath cannot be read!";
760 :     $self->{failure} = 1;
761 :     $self->addbadmessage($msg);
762 : sh002i 1924 }
763 : gage 3037 #################################################
764 :     # The path to the permanent file is now verified and stored in $editFilePath
765 :     # Whew!!!
766 :     #################################################
767 : sh002i 1924
768 : gage 3718 my $tempFilePath = $self->determineTempFilePath($editFilePath); #"$editFilePath.$TEMPFILESUFFIX";
769 : gage 3037 $self->{editFilePath} = $editFilePath;
770 :     $self->{tempFilePath} = $tempFilePath;
771 : gage 3718 $self->{inputFilePath} = (-r $tempFilePath) ? $tempFilePath : $editFilePath;
772 :     #warn "editfile path is $editFilePath and tempFile is $tempFilePath and inputFilePath is ". $self->{inputFilePath};
773 : gage 3037 }
774 : gage 3718 sub new_saveFileChanges {
775 : gage 3315
776 :     ################################################################################
777 : gage 3718 # new_saveFileChanges does most of the work. it is a separate method so that it can
778 : gage 3315 # be called from either pre_header_initialize() or initilize(), depending on
779 :     # whether a redirect is needed or not.
780 :     #
781 :     # it actually does a lot more than save changes to the file being edited, and
782 :     # sometimes less.
783 :     ################################################################################
784 : gage 3037
785 : gage 3718 my ($self, $outputFilePath, $problemContents ) = @_;
786 :     my $r = $self->r;
787 :     my $ce = $r->ce;
788 : gage 3037
789 : gage 3718 my $action = $self->{action}||'no action';
790 : gage 3037 my $editFilePath = $self->{editFilePath};
791 :     my $tempFilePath = $self->{tempFilePath};
792 : gage 3718
793 :     if (defined($problemContents) and ref($problemContents) ) {
794 :     $problemContents = ${$problemContents};
795 :     } elsif( not defined($problemContents) or $problemContents =~/\S/ ) {
796 :     $problemContents = ${$self->{r_problemContents}};
797 :     }
798 : gage 1747 ##############################################################################
799 :     # read and update the targetFile and targetFile.tmp files in the directory
800 :     # if a .tmp file already exists use that, unless the revert button has been pressed.
801 : gage 3718 # The .tmp files are removed when the file is or when the revert occurs.
802 : gage 1747 ##############################################################################
803 :    
804 : gage 3037
805 : gage 3718 unless (defined($outputFilePath) and $outputFilePath =~/\S/ ) {
806 :     $self->addbadmessage("You must specify an file name in order to save a new file.");
807 :     return "";
808 :     }
809 :     my $do_not_save = 0 ; # flag to prevent saving of file
810 : gage 1591 my $editErrors = '';
811 : gage 3037
812 : gage 1747 ##############################################################################
813 :     # write changes to the approriate files
814 : malsyned 980 # FIXME make sure that the permissions are set correctly!!!
815 :     # Make sure that the warning is being transmitted properly.
816 : gage 1747 ##############################################################################
817 : gage 3718
818 : gage 3037 my $writeFileErrors;
819 : gage 3718 if ( defined($outputFilePath) and $outputFilePath =~/\S/ ) { # save file
820 : gage 3037 # Handle the problem of line endings.
821 :     # Make sure that all of the line endings are of unix type.
822 :     # Convert \r\n to \n
823 : gage 3718 #$problemContents =~ s/\r\n/\n/g;
824 :     #$problemContents =~ s/\r/\n/g;
825 : gage 3037
826 : toenail 2134 # make sure any missing directories are created
827 : gage 3718 WeBWorK::Utils::surePathToFile($ce->{courseDirs}->{templates},
828 : gage 3037 $outputFilePath);
829 :    
830 : gage 1750 eval {
831 :     local *OUTPUTFILE;
832 : gage 3718 open OUTPUTFILE, ">$outputFilePath"
833 : gage 3037 or die "Failed to open $outputFilePath";
834 : gage 1750 print OUTPUTFILE $problemContents;
835 : gage 3718 close OUTPUTFILE;
836 :     # any errors are caught in the next block
837 :     };
838 :    
839 : gage 3037 $writeFileErrors = $@ if $@;
840 :     }
841 : gage 1980
842 :     ###########################################################
843 :     # Catch errors in saving files, clean up temp files
844 :     ###########################################################
845 : gage 3037
846 : gage 3718 $self->{saveError} = $do_not_save; # don't do redirects if the file was not saved.
847 : gage 3037 # don't unlink files or send success messages
848 : gage 1980
849 : gage 3037 if ($writeFileErrors) {
850 :     # get the current directory from the outputFilePath
851 :     $outputFilePath =~ m|^(/.*?/)[^/]+$|;
852 : toenail 2288 my $currentDirectory = $1;
853 :    
854 :     my $errorMessage;
855 :     # check why we failed to give better error messages
856 :     if ( not -w $ce->{courseDirs}->{templates} ) {
857 :     $errorMessage = "Write permissions have not been enabled in the templates directory. No changes can be made.";
858 :     } elsif ( not -w $currentDirectory ) {
859 :     $errorMessage = "Write permissions have not been enabled in $currentDirectory. Changes must be saved to a different directory for viewing.";
860 : gage 3037 } elsif ( -e $outputFilePath and not -w $outputFilePath ) {
861 :     $errorMessage = "Write permissions have not been enabled for $outputFilePath. Changes must be saved to another file for viewing.";
862 : toenail 2288 } else {
863 : gage 3037 $errorMessage = "Unable to write to $outputFilePath: $writeFileErrors";
864 : toenail 2288 }
865 :    
866 : gage 3037 $self->{failure} = 1;
867 : toenail 2288 $self->addbadmessage(CGI::p($errorMessage));
868 :    
869 : gage 3037 }
870 : gage 3718 ###########################################################
871 :     # clean up temp files on revert, save and save_as
872 :     ###########################################################
873 : gage 3037 unless( $writeFileErrors or $do_not_save) { # everything worked! unlink and announce success!
874 : gage 981 # unlink the temporary file if there are no errors and the save button has been pushed
875 : gage 3037 if ($action eq 'save' or $action eq 'save_as' or $action eq 'revert') {
876 :     unlink($self->{tempFilePath}) ;
877 :     }
878 :     if ( defined($outputFilePath) and ! $self->{failure} ) {
879 : gage 3718 my $msg = "Saved to file: |$outputFilePath|";
880 : gage 3037 $self->addgoodmessage($msg);
881 :     }
882 :    
883 : sh002i 1924 }
884 : gage 3718
885 :    
886 :     } # end new_saveFileChanges
887 :    
888 :    
889 :    
890 :    
891 :    
892 :     sub getActionParams {
893 :     my ($self, $actionID) = @_;
894 :     my $r = $self->{r};
895 :    
896 :     my %actionParams=();
897 :     foreach my $param ($r->param) {
898 :     next unless $param =~ m/^action\.$actionID\./;
899 :     $actionParams{$param} = [ $r->param($param) ];
900 :     }
901 :     return %actionParams;
902 :     }
903 :    
904 :     sub fixProblemContents {
905 :     #NOT a method
906 :     my $problemContents = shift;
907 :     # Handle the problem of line endings.
908 :     # Make sure that all of the line endings are of unix type.
909 :     # Convert \r\n to \n
910 :     $problemContents =~ s/\r\n/\n/g;
911 :     $problemContents =~ s/\r/\n/g;
912 :     $problemContents;
913 :     }
914 :    
915 :     sub fresh_edit_handler {
916 :     my ($self, $genericParams, $actionParams, $tableParams) = @_;
917 : gage 3811 #$self->addgoodmessage("fresh_edit_handler called");
918 : gage 3718 }
919 :     sub view_form {
920 :     my ($self, $onChange, %actionParams) = @_;
921 :     my $output_string = "View";
922 :     unless ($self->{file_type} eq 'course_info') {
923 :     $output_string .= join(" ",
924 :     " problem using seed ",
925 :     CGI::textfield(-name=>'action.view.seed',-value=>$self->{problemSeed}),
926 :     "and display mode ",
927 :     CGI::popup_menu(-name=>'action.view.displayMode', -values=>$self->r->ce->{pg}->{displayModes},
928 :     -default=>$self->{displayMode}
929 :     ), ".",
930 :     );
931 :     }
932 :    
933 :     return $output_string; #FIXME add -lables to the pop up menu
934 :     }
935 :    
936 :     sub view_handler {
937 :     my ($self, $genericParams, $actionParams, $tableParams) = @_;
938 :     my $courseName = $self->{courseID};
939 :     my $setName = $self->{setID};
940 :     my $problemNumber = $self->{problemID};
941 :     my $problemSeed = ($actionParams->{'action.view.seed'}) ?
942 :     $actionParams->{'action.view.seed'}->[0]
943 :     : 1234;
944 :     my $displayMode = ($actionParams->{'action.view.displayMode'}) ?
945 :     $actionParams->{'action.view.displayMode'}->[0]
946 :     : $self->r->ce->{pg}->{options}->{displayMode};
947 :    
948 :     my $editFilePath = $self->{editFilePath};
949 :     my $tempFilePath = $self->{tempFilePath};
950 :     ########################################################
951 :     # grab the problemContents from the form in order to save it to the tmp file
952 :     ########################################################
953 :     my $problemContents = fixProblemContents($self->r->param('problemContents'));
954 :     $self->{r_problemContents} = \$problemContents;
955 :    
956 :    
957 :     my $do_not_save = 0;
958 :     my $file_type = $self->{file_type};
959 :     $self->new_saveFileChanges($tempFilePath,);
960 :    
961 :     ########################################################
962 :     # construct redirect URL and redirect
963 :     ########################################################
964 :     my $edit_level = $self->r->param("edit_level") || 0;
965 :     $edit_level++;
966 :     my $viewURL;
967 :    
968 : sh002i 3815 if ($file_type eq 'problem' or $file_type eq 'source_path_for_problem_file') { # redirect to Problem.pm
969 : gage 3718 my $problemPage = $self->r->urlpath->newFromModule("WeBWorK::ContentGenerator::Problem",
970 :     courseID => $courseName, setID => $setName, problemID => $problemNumber
971 :     );
972 : malsyned 980
973 : gage 3718 $viewURL = $self->systemLink($problemPage,
974 :     params => {
975 :     displayMode => $displayMode,
976 :     problemSeed => $problemSeed,
977 :     editMode => "temporaryFile",
978 :     edit_level => $edit_level,
979 :     sourceFilePath => $tempFilePath,
980 :     status_message => uri_escape($self->{status_message})
981 : gage 3037
982 : gage 3718 }
983 :     );
984 :     } elsif ($file_type eq 'set_header' or $file_type eq 'hardcopy_header') { # redirect to ProblemSet
985 :     my $problemPage = $self->r->urlpath->newFromModule("WeBWorK::ContentGenerator::ProblemSet",
986 :     courseID => $courseName, setID => $setName,
987 :     );
988 :    
989 :     $viewURL = $self->systemLink($problemPage,
990 :     params => {
991 :     displayMode => $displayMode,
992 :     problemSeed => $problemSeed,
993 :     editMode => "temporaryFile",
994 :     edit_level => $edit_level,
995 :     status_message => uri_escape($self->{status_message})
996 :    
997 :     }
998 :     );
999 :    
1000 :    
1001 :     } elsif ($file_type eq 'course_info') { # redirecto to ProblemSets.pm
1002 :     my $problemSetsPage = $self->r->urlpath->newFromModule("WeBWorK::ContentGenerator::ProblemSets",
1003 :     courseID => $courseName);
1004 :     $viewURL = $self->systemLink($problemSetsPage,
1005 :     params => {
1006 :     editMode => ("temporaryFile"),
1007 :     edit_level => $edit_level,
1008 :     status_message => uri_escape($self->{status_message})
1009 :     }
1010 :     );
1011 :     } else {
1012 :     die "I don't know how to redirect this file type $file_type ";
1013 :     }
1014 : malsyned 980
1015 : gage 3718 $self->reply_with_redirect($viewURL);
1016 :     }
1017 :    
1018 :     sub add_problem_form {
1019 :     my $self = shift;
1020 :     my ($onChange, %actionParams) = @_;
1021 :     my $r = $self->r;
1022 :     my $setName = $self->{setID} ;
1023 :     my $problemNumber = $self->{problemID} ;
1024 :     $setName = defined($setName) ? $setName : ''; # we need this instead of using the || construction
1025 :     # to keep set 0 from being set to the
1026 :     # empty string.
1027 :     $setName =~ s|^set||;
1028 :     my @allSetNames = sort $r->db->listGlobalSets;
1029 :     for (my $j=0; $j<scalar(@allSetNames); $j++) {
1030 :     $allSetNames[$j] =~ s|^set||;
1031 :     $allSetNames[$j] =~ s|\.def||;
1032 :     }
1033 :     return "" if $self->{file_type} eq 'course_info';
1034 :     return join(" ",
1035 :     "Add problem to set " ,
1036 :     CGI::popup_menu(-name=>'action.add_problem.target_set', -values=>\@allSetNames, -default=>$setName),
1037 :     " as ",
1038 :     CGI::popup_menu(-name=>'action.add_problem.file_type', -values=>['problem','set_header'], -default=>'problem'),
1039 :    
1040 :     ); #FIXME add -lables to the pop up menu
1041 :     return "";
1042 :     }
1043 :    
1044 :     sub add_problem_handler {
1045 :     my ($self, $genericParams, $actionParams, $tableParams) = @_;
1046 : gage 3811 #$self->addgoodmessage("add_problem_handler called");
1047 : gage 3718 my $courseName = $self->{courseID};
1048 :     my $setName = $self->{setID};
1049 :     my $problemNumber = $self->{problemID};
1050 :     my $sourceFilePath = $self->{editFilePath};
1051 :     my $displayMode = $self->{displayMode};
1052 :     my $problemSeed = $self->{problemSeed};
1053 :    
1054 :     my $targetSetName = $actionParams->{'action.add_problem.target_set'}->[0];
1055 :     my $targetFileType = $actionParams->{'action.add_problem.file_type'}->[0];
1056 :     my $templatesPath = $self->r->ce->{courseDirs}->{templates};
1057 :     $sourceFilePath =~ s|^$templatesPath/||;
1058 :    
1059 :     my $edit_level = $self->r->param("edit_level") || 0;
1060 :     $edit_level++;
1061 :    
1062 :     my $viewURL ='';
1063 :     if ($targetFileType eq 'problem') {
1064 :     my $targetProblemNumber = 1+ WeBWorK::Utils::max( $self->r->db->listGlobalProblems($targetSetName));
1065 :    
1066 :     #################################################
1067 :     # Update problem record
1068 :     #################################################
1069 :     my $problemRecord = $self->addProblemToSet(
1070 :     setName => $targetSetName,
1071 :     sourceFile => $sourceFilePath,
1072 : gage 3719 problemID => $targetProblemNumber, #added to end of set
1073 : gage 3718 );
1074 :     $self->assignProblemToAllSetUsers($problemRecord);
1075 :     $self->addgoodmessage("Added $sourceFilePath to ". $targetSetName. " as problem $targetProblemNumber") ;
1076 :     $self->{file_type} = 'problem'; # change file type to problem -- if it's not already that
1077 :    
1078 :     #################################################
1079 :     # Set up redirect Problem.pm
1080 :     #################################################
1081 :     my $problemPage = $self->r->urlpath->newFromModule("WeBWorK::ContentGenerator::Problem",
1082 :     courseID => $courseName,
1083 :     setID => $targetSetName,
1084 :     problemID => $targetProblemNumber,
1085 :     );
1086 :     $viewURL = $self->systemLink($problemPage,
1087 :     params => {
1088 :     displayMode => $displayMode,
1089 :     problemSeed => $problemSeed,
1090 :     editMode => "savedFile",
1091 :     edit_level => $edit_level,
1092 :     sourceFilePath => $sourceFilePath,
1093 :     status_message => uri_escape($self->{status_message})
1094 :    
1095 :     }
1096 :     );
1097 :     } elsif ($targetFileType eq 'set_header') {
1098 :     #################################################
1099 :     # Update set record
1100 :     #################################################
1101 : gage 3726 my $setRecord = $self->r->db->getGlobalSet($targetSetName);
1102 : gage 3718 $setRecord->set_header($sourceFilePath);
1103 : gage 3726 if( $self->r->db->putGlobalSet($setRecord) ) {
1104 : gage 3718 $self->addgoodmessage("Added $sourceFilePath to ". $targetSetName. " as new set header ") ;
1105 :     } else {
1106 :     $self->addbadmessage("Unable to make $sourceFilePath the set header for $targetSetName");
1107 :     }
1108 :     $self->{file_type} = 'set_header'; # change file type to set_header if it not already so
1109 :     #################################################
1110 :     # Set up redirect
1111 :     #################################################
1112 :     my $problemPage = $self->r->urlpath->newFromModule("WeBWorK::ContentGenerator::ProblemSet",
1113 :     courseID => $courseName, setID => $targetSetName
1114 :     );
1115 :     $viewURL = $self->systemLink($problemPage,
1116 :     params => {
1117 :     displayMode => $displayMode,
1118 :     editMode => "savedFile",
1119 :     edit_level => $edit_level,
1120 :     status_message => uri_escape($self->{status_message})
1121 :     }
1122 :     );
1123 :     } else {
1124 :     die "Don't know what to do with target file type $targetFileType";
1125 :     }
1126 :    
1127 :     $self->reply_with_redirect($viewURL);
1128 :     }
1129 :    
1130 :    
1131 :     sub save_form {
1132 :     my ($self, $onChange, %actionParams) = @_;
1133 :     my $r => $self->r;
1134 : gage 3790 if ($self->{editFilePath} =~ /$BLANKPROBLEM$/ ) {
1135 :     return ""; #Can't save blank problems without changing names
1136 :     } elsif (-w $self->{editFilePath}) {
1137 :     return "Save";
1138 : gage 3718 } else {
1139 :     return ""; #"Can't save -- No write permission";
1140 :     }
1141 :    
1142 :     }
1143 :    
1144 :     sub save_handler {
1145 :     my ($self, $genericParams, $actionParams, $tableParams) = @_;
1146 : gage 3811 #$self->addgoodmessage("save_handler called");
1147 : gage 3718 my $courseName = $self->{courseID};
1148 :     my $setName = $self->{setID};
1149 :     my $problemNumber = $self->{problemID};
1150 :     my $displayMode = $self->{displayMode};
1151 :     my $problemSeed = $self->{problemSeed};
1152 :    
1153 :     #################################################
1154 :     # grab the problemContents from the form in order to save it to a new permanent file
1155 :     # later we will unlink (delete) the current temporary file
1156 :     #################################################
1157 :     my $problemContents = fixProblemContents($self->r->param('problemContents'));
1158 :     $self->{r_problemContents} = \$problemContents;
1159 :    
1160 :     #################################################
1161 :     # Construct the output file path
1162 :     #################################################
1163 :     my $editFilePath = $self->{editFilePath};
1164 :     my $outputFilePath = $editFilePath;
1165 :    
1166 :     my $do_not_save = 0;
1167 :     my $file_type = $self->{file_type};
1168 :     $self->new_saveFileChanges($outputFilePath);
1169 :     #################################################
1170 :     # Set up redirect to Problem.pm
1171 :     #################################################
1172 :     my $viewURL;
1173 :     ########################################################
1174 :     # construct redirect URL and redirect
1175 :     ########################################################
1176 :     if ($file_type eq 'problem' ) { # redirect to Problem.pm
1177 :     my $problemPage = $self->r->urlpath->newFromModule("WeBWorK::ContentGenerator::Problem",
1178 :     courseID => $courseName, setID => $setName, problemID => $problemNumber
1179 :     );
1180 :    
1181 :     $viewURL = $self->systemLink($problemPage,
1182 :     params => {
1183 :     displayMode => $displayMode,
1184 :     problemSeed => $problemSeed,
1185 :     editMode => "savedFile",
1186 :     edit_level => 0,
1187 :     sourceFilePath => $editFilePath,
1188 :     status_message => uri_escape($self->{status_message})
1189 :    
1190 :     }
1191 :     );
1192 :     } elsif ($file_type eq 'set_header' or $file_type eq 'hardcopy_header') { # redirect to ProblemSet
1193 :     my $problemPage = $self->r->urlpath->newFromModule("WeBWorK::ContentGenerator::ProblemSet",
1194 :     courseID => $courseName, setID => $setName,
1195 :     );
1196 :    
1197 :     $viewURL = $self->systemLink($problemPage,
1198 :     params => {
1199 :     displayMode => $displayMode,
1200 :     problemSeed => $problemSeed,
1201 :     editMode => "savedFile",
1202 :     edit_level => 0,
1203 :     status_message => uri_escape($self->{status_message})
1204 :    
1205 :     }
1206 :     );
1207 :    
1208 :    
1209 :     } elsif ($file_type eq 'course_info') { # redirect to ProblemSets.pm
1210 :     my $problemSetsPage = $self->r->urlpath->newFromModule("WeBWorK::ContentGenerator::ProblemSets",
1211 :     courseID => $courseName);
1212 :     $viewURL = $self->systemLink($problemSetsPage,
1213 :     params => {
1214 :     editMode => ("savedFile"),
1215 :     edit_level => 0,
1216 :     status_message => uri_escape($self->{status_message})
1217 :     }
1218 :     );
1219 : gage 3724 } elsif ($file_type eq 'source_path_for_problem_file') { # redirect to ProblemSets.pm
1220 :     my $problemPage = $self->r->urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::PGProblemEditor",
1221 :     courseID => $courseName, setID => $setName, problemID => $problemNumber
1222 :     );
1223 :     my $viewURL = $self->systemLink($problemPage,
1224 :     params=>{
1225 :     displayMode => $displayMode,
1226 :     problemSeed => $problemSeed,
1227 :     editMode => "savedFile",
1228 :     edit_level => 0,
1229 :     sourceFilePath => $outputFilePath, #The path relative to the templates directory is required.
1230 :     file_type => 'source_path_for_problem_file',
1231 :     status_message => uri_escape($self->{status_message})
1232 : gage 3718
1233 : gage 3724 }
1234 :     );
1235 :    
1236 : gage 3718 } else {
1237 :     die "I don't know how to redirect this file type $file_type ";
1238 :     }
1239 :    
1240 :     $self->reply_with_redirect($viewURL);
1241 :     }
1242 :    
1243 :     sub save_as_form {
1244 :     my ($self, $onChange, %actionParams) = @_;
1245 : gage 3790 my $sourceFilePath = $self->{editFilePath};
1246 :     my $templatesPath = $self->r->ce->{courseDirs}->{templates};
1247 :     $sourceFilePath =~ s|^$templatesPath/||; # make sure path relative to templates directory
1248 : gage 3718
1249 : gage 3790 return "Save ".
1250 :     CGI::popup_menu(-name=>'action.save_as.saveMode', -values=>['rename','save_a_copy'],
1251 :     -default=>'rename',-labels=>{rename=>' as ',save_a_copy=>'a copy to'}
1252 :     ). ": [TMPL]/".CGI::textfield(-name=>'action.save_as.target_file', -size=>40, -value=>$sourceFilePath),;
1253 :    
1254 : gage 3718 }
1255 :    
1256 :     sub save_as_handler {
1257 :     my ($self, $genericParams, $actionParams, $tableParams) = @_;
1258 : gage 3811 #$self->addgoodmessage("save_as_handler called");
1259 : gage 3718 my $courseName = $self->{courseID};
1260 :     my $setName = $self->{setID};
1261 :     my $problemNumber = $self->{problemID};
1262 :     my $displayMode = $self->{displayMode};
1263 :     my $problemSeed = $self->{problemSeed};
1264 :    
1265 :     my $do_not_save = 0;
1266 : gage 3790 my $saveMode = $actionParams->{'action.save_as.saveMode'}->[0] || '';
1267 : gage 3718 my $new_file_name = $actionParams->{'action.save_as.target_file'}->[0] || '';
1268 :     $new_file_name =~ s/^\s*//; #remove initial and final white space
1269 :     $new_file_name =~ s/\s*$//;
1270 :     if ( $new_file_name !~ /\S/) { # need a non-blank file name
1271 :     # setting $self->{failure} stops saving and any redirects
1272 :     $do_not_save = 1;
1273 :     $self->addbadmessage(CGI::p("Please specify a file to save to."));
1274 :     last ACTION_CASES; #stop processing
1275 :     }
1276 :    
1277 :     #################################################
1278 :     # grab the problemContents from the form in order to save it to a new permanent file
1279 :     # later we will unlink (delete) the current temporary file
1280 :     #################################################
1281 :     my $problemContents = fixProblemContents($self->r->param('problemContents'));
1282 :     $self->{r_problemContents} = \$problemContents;
1283 :    
1284 :     #################################################
1285 :     # Rescue the user in case they forgot to end the file name with .pg
1286 :     #################################################
1287 :     my $file_type = $self->{file_type};
1288 :     if($self->{file_type} eq 'problem'
1289 :     or $self->{file_type} eq 'blank_problem'
1290 :     or $self->{file_type} eq 'set_header') {
1291 :     $new_file_name =~ s/\.pg$//; # remove it if it is there
1292 :     $new_file_name .= '.pg'; # put it there
1293 :    
1294 :     }
1295 :     #################################################
1296 :     # Construct the output file path
1297 :     #################################################
1298 :     my $outputFilePath = $self->r->ce->{courseDirs}->{templates} . '/' .
1299 :     $new_file_name;
1300 :     if (defined $outputFilePath and -e $outputFilePath) {
1301 :     # setting $do_not_save stops saving and any redirects
1302 :     $do_not_save = 1;
1303 :     $self->addbadmessage(CGI::p("File $outputFilePath exists. File not saved."));
1304 :     } else {
1305 :     $self->{editFilePath} = $outputFilePath;
1306 :     $self->{tempFilePath} = ''; # nothing needs to be unlinked.
1307 :     $self->{inputFilePath} = '';
1308 :     }
1309 :    
1310 :    
1311 :     unless ($do_not_save ) {
1312 : gage 3790 $self->new_saveFileChanges($outputFilePath, \$problemContents);
1313 :     my $sourceFilePath = $outputFilePath;
1314 :     my $templatesPath = $self->r->ce->{courseDirs}->{templates};
1315 :     $sourceFilePath =~ s|^$templatesPath/||; # make sure path relative to templates directory
1316 :    
1317 :     if ($saveMode eq 'rename') { #save to new file
1318 :     my $problemRecord = $self->r->db->getGlobalProblem($setName,$problemNumber);
1319 :     $problemRecord->source_file($new_file_name);
1320 :     if ( $self->r->db->putGlobalProblem($problemRecord) ) {
1321 :     $self->addgoodmessage("The current source file for problem $problemNumber has been renamed to [TMPL]/$sourceFilePath.") ;
1322 :     } else {
1323 :     $self->addbadmessage("Unable to change the source file path for set $setName, problem $problemNumber. Unknown error.");
1324 :     }
1325 :     } elsif ($saveMode eq 'save_a_copy') {
1326 :     $self->addgoodmessage("A new local, editable, copy of this problem has been created at [TMPL]/$sourceFilePath.") ;
1327 :     } else {
1328 :     $self->addbadmessage("Don't recognize saveMode: |$saveMode|. Unknown error.");
1329 :     }
1330 : gage 3718 }
1331 :     my $edit_level = $self->r->param("edit_level") || 0;
1332 :     $edit_level++;
1333 :    
1334 :     #################################################
1335 :     # Set up redirect
1336 :     # The redirect gives the server time to detect that the new file exists.
1337 :     #################################################
1338 : gage 3790 my $problemPage;
1339 :     if ($saveMode eq 'save_a_copy' ) {
1340 :     $problemPage = $self->r->urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::PGProblemEditor",
1341 :     courseID => $courseName, setID => 'Undefined_Set', problemID => 'Undefined_Set'
1342 :     );
1343 :     } elsif ($saveMode eq 'rename') {
1344 :     $problemPage = $self->r->urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::PGProblemEditor",
1345 :     courseID => $courseName, setID => $setName, problemID => $problemNumber
1346 :     );
1347 :    
1348 :     } else {
1349 :     $self->addbadmessage("Don't recognize saveMode: |$saveMode|. Unknown error.");
1350 :     }
1351 : gage 3718 my $viewURL = $self->systemLink($problemPage,
1352 :     params=>{
1353 :     sourceFilePath => $outputFilePath, #The path relative to the templates directory is required.
1354 :     edit_level => $edit_level,
1355 :     file_type => 'source_path_for_problem_file',
1356 :     status_message => uri_escape($self->{status_message})
1357 :    
1358 :     }
1359 :     );
1360 :    
1361 :     $self->reply_with_redirect($viewURL);
1362 :     return ""; # no redirect needed
1363 :     }
1364 :     sub revert_form {
1365 :     my ($self, $onChange, %actionParams) = @_;
1366 :     return "Revert" ;
1367 :    
1368 :     }
1369 :     sub revert_handler {
1370 :     my ($self, $genericParams, $actionParams, $tableParams) = @_;
1371 : gage 3811 #$self->addgoodmessage("revert_handler called");
1372 : gage 3718
1373 :     my $courseName = $self->{courseID};
1374 :     my $setName = $self->{setID};
1375 :     my $problemNumber = $self->{problemID};
1376 :     my $displayMode = $self->{displayMode};
1377 :     my $problemSeed = $self->{problemSeed};
1378 :     #################################################
1379 :     # Reset the problem paths
1380 :     #################################################
1381 :    
1382 :     my $editFilePath = $self->{editFilePath};
1383 :     $self->{inputFilePath} = $editFilePath;
1384 :     $self->{tempFilePath} = '';
1385 :     # unlink the temp files;
1386 :     unlink($self->{tempFilePath});
1387 :     my $problemContents ='';
1388 :     $self->{r_problemContents} = \$problemContents;
1389 :     $self->addgoodmessage("Reverting to original file $editFilePath");
1390 :     # no redirect is needed
1391 :     }
1392 :     sub make_local_copy_form {
1393 :     my ($self, $genericParams, $actionParams, $tableParams) = @_;
1394 :     my $editFilePath = $self->{editFilePath}; # path to the permanent file to be edited
1395 :     return "" unless -e $editFilePath;
1396 :     return "" if -w $editFilePath;
1397 : gage 3726 return "" unless $self->{file_type} eq 'problem';
1398 :     # or $self->{file_type} eq 'set_header' ; # need problem structure to make local copy -- not available for header
1399 : gage 3724 # or $self->{file_type} eq 'source_path_for_problem_file'; # need setID and problemID to make local copy
1400 : gage 3718 return join ("",
1401 : gage 3726 "Make local copy at: [TMPL]/".($self->determineLocalFilePath($editFilePath)),
1402 :     CGI::hidden(-name=>'action.make_local_copy.target_file', -value=>$self->determineLocalFilePath($editFilePath) ),
1403 : gage 3718 CGI::hidden(-name=>'action.make_local_copy.source_file', -value=>$editFilePath ),
1404 :     );
1405 :    
1406 :     }
1407 :    
1408 :     sub make_local_copy_handler {
1409 :     my ($self, $genericParams, $actionParams, $tableParams) = @_;
1410 : gage 3811 #$self->addgoodmessage("make_local_copy_handler called");
1411 : gage 3718
1412 :     my $courseName = $self->{courseID};
1413 :     my $setName = $self->{setID};
1414 :     my $problemNumber = $self->{problemID};
1415 :    
1416 :     my $displayMode = $self->{displayMode};
1417 :     my $problemSeed = $self->{problemSeed};
1418 :    
1419 :     my $do_not_save = 0; #error flag
1420 :     #################################################
1421 :     # Save the file locally
1422 :     #################################################
1423 :     my $new_file_name = $actionParams->{'action.make_local_copy.target_file'}->[0] || '';
1424 :     my $sourceFilePath = $actionParams->{'action.make_local_copy.source_file'}->[0] || '';
1425 :     my $templatesPath = $self->r->ce->{courseDirs}->{templates};
1426 :     $sourceFilePath =~ s|^$templatesPath/||; # make sure path relative to templates directory
1427 :    
1428 :     if ( $new_file_name !~ /\S/) { # need a non-blank file name
1429 :     # setting $self->{failure} stops saving and any redirects
1430 :     $do_not_save = 1;
1431 :     warn "new file name is $new_file_name";
1432 :     $self->addbadmessage(CGI::p("Please specify a file to save to."));
1433 :     }
1434 :    
1435 :     #################################################
1436 :     # grab the problemContents from the form in order to save it to a new permanent file
1437 :     # later we will unlink (delete) the current temporary file
1438 :     #################################################
1439 :    
1440 :     my $problemContents = fixProblemContents($self->r->param('problemContents'));
1441 :     $self->{r_problemContents} = \$problemContents;
1442 :     warn "problem contents is empty" unless $problemContents;
1443 :     #################################################
1444 :     # Construct the output file path
1445 :     #################################################
1446 :     my $outputFilePath = $self->r->ce->{courseDirs}->{templates} . '/' .
1447 :     $new_file_name;
1448 :     if (defined $outputFilePath and -e $outputFilePath) {
1449 :     # setting $do_not_save stops saving and any redirects
1450 :     $do_not_save = 1;
1451 :     $self->addbadmessage(CGI::p("File $outputFilePath exists. File not saved."));
1452 :     } else {
1453 :     #$self->addgoodmessage("Saving to file $outputFilePath.");
1454 :     }
1455 :     my $file_type = $self->{file_type};
1456 :     unless ($do_not_save ) {
1457 :     $self->new_saveFileChanges($outputFilePath);
1458 :     }
1459 :     #################################################
1460 :     # Modify source file in problem
1461 :     #################################################
1462 :     if (-r $outputFilePath and !$do_not_save) {
1463 :     my $problemRecord = $self->r->db->getGlobalProblem($setName,$problemNumber);
1464 :     $problemRecord->source_file($new_file_name);
1465 :     if ( $self->r->db->putGlobalProblem($problemRecord) ) {
1466 :     $self->addgoodmessage("A local, editable, copy of [TMPL]/$sourceFilePath has been made for problem $problemNumber.") ;
1467 :     } else {
1468 :     $self->addbadmessage("Unable to change the source file path for set $setName, problem $problemNumber. Unknown error.");
1469 :     }
1470 :     }
1471 :     my $edit_level = $self->r->param("edit_level") || 0;
1472 :     $edit_level++;
1473 :     #################################################
1474 :     # Set up redirect
1475 :     #################################################
1476 :    
1477 :     my $problemPage = $self->r->urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::PGProblemEditor",
1478 :     courseID => $courseName, setID => $setName, problemID => $problemNumber
1479 :     );
1480 :     my $viewURL = $self->systemLink($problemPage,
1481 :     params=>{
1482 :     sourceFilePath => $sourceFilePath,
1483 :     edit_level => $edit_level,
1484 :     file_type => 'problem',
1485 :     status_message => uri_escape($self->{status_message})
1486 :    
1487 :     }
1488 :     );
1489 :     $self->reply_with_redirect($viewURL);
1490 :     }
1491 :    
1492 : gage 3790 sub rename_form {
1493 :     my ($self, $onChange, %actionParams) = @_;
1494 :     my $problemPath = $self->{editFilePath};
1495 :     my $templatesDir = $self->r->ce->{courseDirs}->{templates};
1496 :     #warn "problemPath $problemPath $templatesDir";
1497 :     $problemPath =~ s|^$templatesDir/||;
1498 :     return join("",
1499 :     "Rename problem file to : [TMPL]/".CGI::textfield(-name=>'action.rename.target_file', -size=>40, -value=>$problemPath),
1500 :     CGI::hidden(-name=>'action.make_local_copy.source_file', -value=>$self->{editFilePath} ),
1501 :     );
1502 : gage 3718
1503 :    
1504 : gage 3790 }
1505 : gage 3718
1506 : gage 3790 sub rename_handler {
1507 :     my ($self, $genericParams, $actionParams, $tableParams) = @_;
1508 :     $actionParams->{'action.make_local_copy.target_file'}->[0] = $actionParams->{'action.rename.target_file'}->[0];
1509 :     make_local_copy_handler($self, $genericParams, $actionParams, $tableParams);
1510 :     }
1511 :    
1512 :    
1513 : gage 889 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9