[system] / trunk / webwork2 / lib / WeBWorK / ContentGenerator / Instructor / PGProblemEditor.pm Repository:
ViewVC logotype

Annotation of /trunk/webwork2/lib/WeBWorK/ContentGenerator/Instructor/PGProblemEditor.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 980 - (view) (download) (as text)

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9