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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9