| … | |
… | |
| 33 | # various actions depending on state. |
33 | # various actions depending on state. |
| 34 | if ( defined($submit_button) and ($submit_button eq 'Save' or $submit_button eq 'Refresh') ) { |
34 | if ( defined($submit_button) and ($submit_button eq 'Save' or $submit_button eq 'Refresh') ) { |
| 35 | |
35 | |
| 36 | $self->initialize($setName,$problemNumber); # write the necessary files |
36 | $self->initialize($setName,$problemNumber); # write the necessary files |
| 37 | # return file path for viewing problem |
37 | # return file path for viewing problem |
| 38 | # in $self->{ce}->{currentSourceFilePath} |
38 | # in $self->{currentSourceFilePath} |
| 39 | #redirect to view the problem |
39 | #redirect to view the problem |
| 40 | |
40 | |
| 41 | my $hostname = $r->hostname(); |
41 | my $hostname = $r->hostname(); |
| 42 | my $port = $r->get_server_port(); |
42 | my $port = $r->get_server_port(); |
| 43 | my $uri = $r->uri; |
43 | my $uri = $r->uri; |
| … | |
… | |
| 49 | my $viewURL = "http://$hostname:$port"; |
49 | my $viewURL = "http://$hostname:$port"; |
| 50 | $viewURL .= "/webwork/$courseName/$setName/$problemNumber/?"; |
50 | $viewURL .= "/webwork/$courseName/$setName/$problemNumber/?"; |
| 51 | $viewURL .= $self->url_authen_args; |
51 | $viewURL .= $self->url_authen_args; |
| 52 | $viewURL .= "&displayMode=$displayMode&problemSeed=$problemSeed"; # optional displayMode and problemSeed overrides |
52 | $viewURL .= "&displayMode=$displayMode&problemSeed=$problemSeed"; # optional displayMode and problemSeed overrides |
| 53 | $viewURL .= "&editMode=temporaryFile"; |
53 | $viewURL .= "&editMode=temporaryFile"; |
| 54 | $viewURL .= '&sourceFilePath='.$self->{ce}->{currentSourceFilePath}; # path to pg text for viewing |
54 | $viewURL .= '&sourceFilePath='. $self->{currentSourceFilePath}; # path to pg text for viewing |
| 55 | $viewURL .= "&submit_button=$submit_button"; # allows Problem.pg to recognize state |
55 | $viewURL .= "&submit_button=$submit_button"; # allows Problem.pg to recognize state |
| 56 | $viewURL .= '&editErrors='.$self->{ce}->{editErrors}; # of problem being viewed. |
56 | $viewURL .= '&editErrors='.$self->{ce}->{editErrors}; # of problem being viewed. |
| 57 | $r->header_out(Location => $viewURL ); |
57 | $r->header_out(Location => $viewURL ); |
| 58 | return REDIRECT; |
58 | return REDIRECT; |
| 59 | } else { |
59 | } else { |
| 60 | # initialize and |
60 | # initialize and |
| … | |
… | |
| 100 | my $currentSourceFilePath = ''; |
100 | my $currentSourceFilePath = ''; |
| 101 | # update the .pg and .pg.tmp files in the directory |
101 | # update the .pg and .pg.tmp files in the directory |
| 102 | if (not defined($submit_button) ) { |
102 | if (not defined($submit_button) ) { |
| 103 | # this is a fresh editing job |
103 | # this is a fresh editing job |
| 104 | # copy the pg file to a new file with the same name with .tmp added |
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 |
105 | # store this name in the $self->currentSourceFilePath for use in body |
| 106 | |
106 | |
| 107 | eval { $problemContents = WeBWorK::Utils::readFile($problemPath) |
107 | eval { $problemContents = WeBWorK::Utils::readFile($problemPath) |
| 108 | }; # try to read file |
108 | }; # try to read file |
| 109 | $problemContents = $@ if $@; |
109 | $problemContents = $@ if $@; |
| 110 | $currentSourceFilePath = "$problemPath.$editFileSuffix"; |
110 | $currentSourceFilePath = "$problemPath.$editFileSuffix"; |
| 111 | $ce->{currentSourceFilePath} = $currentSourceFilePath; |
111 | $self->{currentSourceFilePath} = $currentSourceFilePath; |
| 112 | } elsif ($submit_button eq 'Refresh' ) { |
112 | } elsif ($submit_button eq 'Refresh' ) { |
| 113 | # grab the problemContents from the form and save it to the tmp file |
113 | # grab the problemContents from the form in order to save it to the tmp file |
| 114 | # store tmp file name in the $ce->currentSourceFilePath for use in body |
114 | # store tmp file name in the $self->currentSourceFilePath for use in body |
| 115 | |
115 | |
| 116 | $problemContents = $r->param('problemContents'); |
116 | $problemContents = $r->param('problemContents'); |
| 117 | $currentSourceFilePath = "$problemPath.$editFileSuffix"; |
117 | $currentSourceFilePath = "$problemPath.$editFileSuffix"; |
| 118 | $ce->{currentSourceFilePath} = $currentSourceFilePath; |
118 | $self->{currentSourceFilePath} = $currentSourceFilePath; |
| 119 | } elsif ($submit_button eq 'Save') { |
119 | } elsif ($submit_button eq 'Save') { |
| 120 | # grab the problemContents from the form and save it to the permanent file |
120 | # grab the problemContents from the form in order to save it to the permanent file |
| 121 | # unlink (delete) the temporary file |
121 | # later we will unlink (delete) the temporary file |
| 122 | # store the permanent file name in the $ce->problemContents for use in body |
122 | # store permanent file name in the $self->currentSourceFilePath for use in body |
| 123 | |
123 | |
| 124 | $problemContents = $r->param('problemContents'); |
124 | $problemContents = $r->param('problemContents'); |
| 125 | $currentSourceFilePath = "$problemPath"; |
125 | $currentSourceFilePath = "$problemPath"; |
| 126 | $ce->{currentSourceFilePath} = $currentSourceFilePath; |
126 | $self->{currentSourceFilePath} = $currentSourceFilePath; |
| 127 | } else { |
127 | } else { |
| 128 | # give a warning |
128 | # give a warning |
| 129 | die "Unrecognized submit command $submit_button"; |
129 | die "Unrecognized submit command $submit_button"; |
| 130 | } |
130 | } |
| 131 | # print changed pg files |
131 | # print changed pg files |
| … | |
… | |
| 136 | open OUTPUTFILE, ">", $currentSourceFilePath |
136 | open OUTPUTFILE, ">", $currentSourceFilePath |
| 137 | or die "Failed to write to $currentSourceFilePath: $!"; |
137 | or die "Failed to write to $currentSourceFilePath: $!"; |
| 138 | print OUTPUTFILE $problemContents; |
138 | print OUTPUTFILE $problemContents; |
| 139 | close OUTPUTFILE; |
139 | close OUTPUTFILE; |
| 140 | }; |
140 | }; |
|
|
141 | # record an error string for later use if there was a difficulty in writing to the file |
|
|
142 | # FIXME is this string every inspected? |
| 141 | my $errors = $@ if $@; |
143 | my $errors = $@ if $@; |
| 142 | if ( $errors) { |
144 | if ( $errors) { |
| 143 | |
145 | |
| 144 | $ce->{editErrors} = "Unable to write to $currentSourceFilePath: $errors"; |
146 | $self->{editErrors} = "Unable to write to $currentSourceFilePath: $errors"; |
| 145 | |
147 | |
| 146 | } else { # unlink the temporary file if there are no errors. |
148 | |
|
|
149 | } else { |
|
|
150 | # unlink the temporary file if there are no errors and the save button has been pushed |
|
|
151 | |
| 147 | $ce->{editErrors} = ''; |
152 | $self->{editErrors} = ''; |
| 148 | unlink("$problemPath.$editFileSuffix") if defined($submit_button) and $submit_button eq 'Save'; |
153 | unlink("$problemPath.$editFileSuffix") if defined($submit_button) and $submit_button eq 'Save'; |
| 149 | |
|
|
| 150 | }; |
154 | }; |
| 151 | |
155 | |
| 152 | |
156 | |
| 153 | # return values. FIXME -- is this the right way to pass the values to body?? |
157 | # return values for use in the body subroutine |
| 154 | # Should temporary results be passed in self or in ce?? |
|
|
| 155 | # $ce->{viewProblemURL} = $viewProblemURL; |
|
|
| 156 | $ce->{problemPath} = $problemPath; |
158 | $self->{problemPath} = $problemPath; |
| 157 | $self->{displayMode} = $displayMode; |
159 | $self->{displayMode} = $displayMode; |
| 158 | $self->{problemSeed} = $problemSeed; |
160 | $self->{problemSeed} = $problemSeed; |
| 159 | # $ce->{path_components} = join("/",$setID,$problemNumber); |
|
|
| 160 | |
161 | |
| 161 | # FIXME there is no way to edit in a temporary file -- all editing takes place on disk!!! |
162 | # FIXME there is no way to edit in a temporary file -- all editing takes place on disk!!! |
| 162 | |
163 | |
| 163 | |
164 | |
| 164 | |
165 | |
| … | |
… | |
| 166 | |
167 | |
| 167 | sub body { |
168 | sub body { |
| 168 | my $self = shift; |
169 | my $self = shift; |
| 169 | |
170 | |
| 170 | # test area |
171 | # test area |
| 171 | my $r = $self->{r}; |
172 | my $r = $self->{r}; |
| 172 | my $db = $self->{db}; |
173 | my $db = $self->{db}; |
| 173 | my $ce = $self->{ce}; |
174 | my $ce = $self->{ce}; |
| 174 | my $user = $r->param('user'); |
175 | my $user = $r->param('user'); |
| 175 | my $key = $db->getKey($user)->key(); |
176 | my $key = $db->getKey($user)->key(); |
| 176 | |
177 | |
| 177 | |
178 | |
| 178 | ################ |
179 | ################ |
| 179 | # Gathering info |
180 | # Gathering info |
| 180 | # What is needed |
181 | # What is needed |
| 181 | # $problemPath -- |
182 | # $problemPath -- |
| 182 | # $formURL -- given by $r->uri |
183 | # $formURL -- given by $r->uri |
| 183 | # $tmpProblemPath |
184 | # $tmpProblemPath |
| 184 | #my ($problemPath,$formURL,$tmpProblemPath) = $self->initialize(); |
|
|
| 185 | my $problemPath = $ce->{problemPath}; |
185 | my $problemPath = $self->{problemPath}; |
| 186 | |
186 | |
| 187 | #my $tmpProblemPath = $ce->{tmpProblemPath}; |
|
|
| 188 | |
187 | |
| 189 | |
188 | |
| 190 | |
189 | |
| 191 | |
190 | |
| 192 | |
191 | |