[system] / branches / gage_dev / webwork2 / lib / WeBWorK / ContentGenerator / Instructor / ProblemSetDetail.pm Repository:
ViewVC logotype

Annotation of /branches/gage_dev/webwork2/lib/WeBWorK/ContentGenerator/Instructor/ProblemSetDetail.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : toenail 2794 ################################################################################
2 :     # WeBWorK Online Homework Delivery System
3 : sh002i 3973 # Copyright © 2000-2006 The WeBWorK Project, http://openwebwork.sf.net/
4 : toenail 2794 #
5 :     #
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 :     package WeBWorK::ContentGenerator::Instructor::ProblemSetDetail;
18 :     use base qw(WeBWorK::ContentGenerator::Instructor);
19 :    
20 :     =head1 NAME
21 :    
22 :     WeBWorK::ContentGenerator::Instructor::ProblemSetDetail - Edit general set and specific user/set information as well as problem information
23 :    
24 :     =cut
25 :    
26 :     use strict;
27 :     use warnings;
28 : gage 4234 #use CGI qw(-nosticky );
29 :     use WeBWorK::CGI;
30 : toenail 2816 use WeBWorK::HTML::ComboBox qw/comboBox/;
31 :     use WeBWorK::Utils qw(readDirectory list2hash listFilesRecursive max);
32 : toenail 2794 use WeBWorK::Utils::Tasks qw(renderProblems);
33 : toenail 2901 use WeBWorK::Debug;
34 : glarose 4904 # IP RESTRICT
35 :     use WeBWorK::HTML::ScrollingRecordList qw/scrollingRecordList/;
36 : toenail 2794
37 :     # Important Note: the following two sets of constants may seem similar
38 :     # but they are functionally and semantically different
39 :    
40 :     # these constants determine which fields belong to what type of record
41 : glarose 4904 # IP RESTRICT
42 :     use constant SET_FIELDS => [qw(set_header hardcopy_header open_date due_date answer_date published restrict_ip assignment_type attempts_per_version version_time_limit versions_per_interval time_interval problem_randorder problems_per_page hide_score hide_work)];
43 : toenail 2794 use constant PROBLEM_FIELDS =>[qw(source_file value max_attempts)];
44 :     use constant USER_PROBLEM_FIELDS => [qw(problem_seed status num_correct num_incorrect)];
45 :    
46 :     # these constants determine what order those fields should be displayed in
47 :     use constant HEADER_ORDER => [qw(set_header hardcopy_header)];
48 :     use constant PROBLEM_FIELD_ORDER => [qw(problem_seed status value max_attempts attempted last_answer num_correct num_incorrect)];
49 :    
50 : glarose 3377 # we exclude the gateway set fields from the set field order, because they
51 :     # are only displayed for sets that are gateways. this results in a bit of
52 :     # convoluted logic below, but it saves burdening people who are only using
53 :     # homework assignments with all of the gateway parameters
54 : glarose 4693 # FIXME: in the long run, we may want to let hide_score and hide_work be
55 :     # FIXME: set for non-gateway assignments. right now (11/30/06) they are
56 :     # FIXME: only used for gateways
57 : glarose 4904 # IP RESTRICT
58 :     use constant SET_FIELD_ORDER => [qw(open_date due_date answer_date published restrict_ip assignment_type)];
59 : glarose 4861 # use constant GATEWAY_SET_FIELD_ORDER => [qw(attempts_per_version version_time_limit time_interval versions_per_interval problem_randorder problems_per_page hide_score hide_work)];
60 :     use constant GATEWAY_SET_FIELD_ORDER => [qw(version_time_limit time_limit_cap attempts_per_version time_interval versions_per_interval problem_randorder problems_per_page hide_score hide_work)];
61 : glarose 3377
62 : toenail 2794 # this constant is massive hash of information corresponding to each db field.
63 :     # override indicates for how many students at a time a field can be overridden
64 :     # this hash should make it possible to NEVER have explicitly: if (somefield) { blah() }
65 :     #
66 :     # All but name are optional
67 :     # some_field => {
68 :     # name => "Some Field",
69 :     # type => "edit", # edit, choose, hidden, view - defines how the data is displayed
70 :     # size => "50", # size of the edit box (if any)
71 :     # override => "none", # none, one, any, all - defines for whom this data can/must be overidden
72 :     # module => "problem_list", # WeBWorK module
73 :     # default => 0 # if a field cannot default to undefined/empty what should it default to
74 :     # labels => { # special values can be hashed to display labels
75 :     # 1 => "Yes",
76 :     # 0 => "No",
77 :     # },
78 : glarose 4861 # convertby => 60, # divide incoming database field values by this, and multiply when saving
79 : gage 3790
80 :     use constant BLANKPROBLEM => 'blankProblem.pg';
81 :    
82 : toenail 2794 use constant FIELD_PROPERTIES => {
83 :     # Set information
84 :     set_header => {
85 :     name => "Set Header",
86 :     type => "edit",
87 :     size => "50",
88 :     override => "all",
89 :     module => "problem_list",
90 :     default => "",
91 :     },
92 :     hardcopy_header => {
93 :     name => "Hardcopy Header",
94 :     type => "edit",
95 :     size => "50",
96 :     override => "all",
97 :     module => "hardcopy_preselect_set",
98 :     default => "",
99 :     },
100 :     open_date => {
101 :     name => "Opens",
102 :     type => "edit",
103 : toenail 2901 size => "26",
104 : toenail 2794 override => "any",
105 : toenail 2841 labels => {
106 : toenail 3061 #0 => "None Specified",
107 : toenail 2841 "" => "None Specified",
108 :     },
109 : toenail 2794 },
110 :     due_date => {
111 :     name => "Answers Due",
112 :     type => "edit",
113 : toenail 2901 size => "26",
114 : toenail 2794 override => "any",
115 : toenail 2841 labels => {
116 : toenail 3061 #0 => "None Specified",
117 : toenail 2841 "" => "None Specified",
118 :     },
119 : toenail 2794 },
120 :     answer_date => {
121 :     name => "Answers Available",
122 :     type => "edit",
123 : toenail 2901 size => "26",
124 : toenail 2794 override => "any",
125 : toenail 2841 labels => {
126 : toenail 3061 #0 => "None Specified",
127 : toenail 2841 "" => "None Specified",
128 :     },
129 : toenail 2794 },
130 :     published => {
131 :     name => "Visible to Students",
132 :     type => "choose",
133 :     override => "all",
134 :     choices => [qw( 0 1 )],
135 :     labels => {
136 :     1 => "Yes",
137 :     0 => "No",
138 :     },
139 :     },
140 : glarose 4904 restrict_ip => {
141 :     name => "Restrict Access by IP",
142 :     type => "choose",
143 :     override => "any",
144 :     choices => [qw( No RestrictTo DenyFrom )],
145 :     labels => {
146 :     No => "No",
147 :     RestrictTo => "Restrict To",
148 :     DenyFrom => "Deny From",
149 :     },
150 :     default => 'No',
151 :     },
152 : glarose 3377 assignment_type => {
153 :     name => "Assignment type",
154 :     type => "choose",
155 :     override => "all",
156 :     choices => [qw( default gateway proctored_gateway )],
157 :     labels => { default => "homework",
158 :     gateway => "gateway/quiz",
159 :     proctored_gateway => "proctored gateway/quiz",
160 :     },
161 :     },
162 :     version_time_limit => {
163 : glarose 4861 name => "Test Time Limit (min)",
164 : glarose 3377 type => "edit",
165 :     size => "4",
166 : glarose 3853 override => "any",
167 : glarose 3377 labels => { "" => 0 }, # I'm not sure this is quite right
168 : glarose 4861 convertby => 60,
169 : glarose 3377 },
170 : glarose 4861 time_limit_cap => {
171 :     name => "Cap Test Time at Set Due Date?",
172 :     type => "choose",
173 :     override => "all",
174 :     choices => [qw(0 1)],
175 :     labels => { '0' => 'No', '1' => 'Yes' },
176 :     },
177 :     attempts_per_version => {
178 :     name => "Number of Graded Submissions per Test",
179 :     type => "edit",
180 :     size => "3",
181 :     override => "any",
182 :     # labels => { "" => 1 },
183 :     },
184 : glarose 3377 time_interval => {
185 : glarose 4861 name => "Time Interval for New Test Versions (min; 0=infty)",
186 : glarose 3377 type => "edit",
187 :     size => "5",
188 : glarose 4861 override => "any",
189 : glarose 3377 labels => { "" => 0 },
190 : glarose 4861 convertby => 60,
191 : glarose 3377 },
192 :     versions_per_interval => {
193 : glarose 4861 name => "Number of Tests per Time Interval (0=infty)",
194 : glarose 3377 type => "edit",
195 :     size => "3",
196 : glarose 4861 override => "any",
197 : glarose 4411 default => "0",
198 :     # labels => { "" => 0 },
199 : glarose 3377 # labels => { "" => 1 },
200 :     },
201 :     problem_randorder => {
202 :     name => "Order Problems Randomly",
203 :     type => "choose",
204 :     choices => [qw( 0 1 )],
205 : glarose 4861 override => "any",
206 : glarose 3377 labels => { 0 => "No", 1 => "Yes" },
207 :     },
208 : glarose 4306 problems_per_page => {
209 :     name => "Number of Problems per Page (0=all)",
210 :     type => "edit",
211 :     size => "3",
212 : glarose 4861 override => "any",
213 : glarose 4411 default => "0",
214 :     # labels => { "" => 0 },
215 : glarose 4306 },
216 : glarose 4693 hide_score => {
217 : glarose 4861 name => "Show Score on Finished Assignments",
218 : glarose 4693 type => "choose",
219 : glarose 4874 choices => [ qw(N Y BeforeAnswerDate) ],
220 : glarose 4861 override => "any",
221 : glarose 4874 labels => { 'N' => "Yes", 'Y' => "No", 'BeforeAnswerDate' => 'Only after set answer date' },
222 : glarose 4693 },
223 :     hide_work => {
224 : glarose 4861 name => "Show Student Work on Finished Tests",
225 : glarose 4693 type => "choose",
226 : glarose 4874 choices => [ qw(N Y BeforeAnswerDate) ],
227 : glarose 4861 override => "any",
228 : glarose 4874 labels => { 'N' => "Yes", 'Y' => "No", 'BeforeAnswerDate' => 'Only after set answer date' },
229 : glarose 4693 },
230 : toenail 2794 # Problem information
231 :     source_file => {
232 :     name => "Source File",
233 :     type => "edit",
234 :     size => 50,
235 :     override => "any",
236 :     default => "",
237 :     },
238 :     value => {
239 :     name => "Weight",
240 :     type => "edit",
241 : toenail 2901 size => 6,
242 : toenail 2794 override => "any",
243 :     },
244 :     max_attempts => {
245 :     name => "Max attempts",
246 :     type => "edit",
247 : toenail 2901 size => 6,
248 : toenail 2794 override => "any",
249 :     labels => {
250 :     "-1" => "unlimited",
251 :     },
252 :     },
253 :     problem_seed => {
254 :     name => "Seed",
255 :     type => "edit",
256 : toenail 2901 size => 6,
257 : toenail 2794 override => "one",
258 :    
259 :     },
260 :     status => {
261 :     name => "Status",
262 :     type => "edit",
263 : toenail 2901 size => 6,
264 :     override => "one",
265 : toenail 2794 default => 0,
266 :     },
267 :     attempted => {
268 :     name => "Attempted",
269 :     type => "hidden",
270 :     override => "none",
271 :     choices => [qw( 0 1 )],
272 :     labels => {
273 :     1 => "Yes",
274 :     0 => "No",
275 :     },
276 :     default => 0,
277 :     },
278 :     last_answer => {
279 :     name => "Last Answer",
280 :     type => "hidden",
281 :     override => "none",
282 :     },
283 :     num_correct => {
284 :     name => "Correct",
285 :     type => "hidden",
286 :     override => "none",
287 :     default => 0,
288 :     },
289 :     num_incorrect => {
290 :     name => "Incorrect",
291 :     type => "hidden",
292 :     override => "none",
293 :     default => 0,
294 :     },
295 :     };
296 :    
297 :     # Create a table of fields for the given parameters, one row for each db field
298 :     # if only the setID is included, it creates a table of set information
299 :     # if the problemID is included, it creates a table of problem information
300 :     sub FieldTable {
301 : sh002i 2913 my ($self, $userID, $setID, $problemID, $globalRecord, $userRecord) = @_;
302 : toenail 2794
303 :     my $r = $self->r;
304 :     my @editForUser = $r->param('editForUser');
305 :     my $forUsers = scalar(@editForUser);
306 :     my $forOneUser = $forUsers == 1;
307 :    
308 :     my @fieldOrder;
309 : glarose 4904
310 : glarose 3377 my $gwoutput = '';
311 : glarose 4904 # IP RESTRICT
312 :     my $db = $r->{db};
313 :     my $ipSelector = '';
314 :     my $glIPlist;
315 :     my $orChecked;
316 :    
317 : toenail 2794 if (defined $problemID) {
318 :     @fieldOrder = @{ PROBLEM_FIELD_ORDER() };
319 :     } else {
320 :     @fieldOrder = @{ SET_FIELD_ORDER() };
321 : glarose 3377
322 :     # gateway data fields are included only if the set is a gateway
323 :     if ( $globalRecord->assignment_type() =~ /gateway/ ) {
324 : glarose 3853 my $gwhdr = "\n<!-- begin gwoutput table -->\n";
325 :     my $nF = 0;
326 :    
327 :     foreach my $gwfield ( @{ GATEWAY_SET_FIELD_ORDER() } ) {
328 :     my @fieldData =
329 :     ($self->FieldHTML($userID, $setID, $problemID,
330 :     $globalRecord, $userRecord,
331 :     $gwfield));
332 : gage 4063 if ( @fieldData && defined($fieldData[1]) and $fieldData[1] ne '' ) {
333 : glarose 3853 $nF = @fieldData if ( @fieldData > $nF );
334 :     $gwoutput .= CGI::Tr({}, CGI::td({}, [@fieldData]));
335 : glarose 3377 }
336 : glarose 3853 }
337 :     $gwhdr .= CGI::Tr({},CGI::td({colspan=>$nF},
338 :     CGI::em("Gateway parameters")))
339 :     if ( $nF );
340 :     $gwoutput = "$gwhdr$gwoutput\n" .
341 :     "<!-- end gwoutput table -->\n";
342 : glarose 3377 }
343 : glarose 4904 # IP RESTRICT
344 :     # similarly, we only include an ip selector if restrict_ip is not 'No'
345 :     if ( ( ! $forUsers && $globalRecord->restrict_ip ne 'No' ) ||
346 :     ( $forUsers && $userRecord->restrict_ip ne 'No' ) ) {
347 :     my @locations = sort {$a cmp $b} ($db->listLocations());
348 :     my @globalLocations = $db->listGlobalSetLocations($setID);
349 :    
350 :     # what ip locations should be selected?
351 :     my @defaultLocations = ();
352 :     if ( $forUsers &&
353 :     ! $db->countUserSetLocations($userID, $setID) ) {
354 :     @defaultLocations = @globalLocations;
355 :     $orChecked = 0;
356 :     } elsif ( $forUsers ) {
357 :     @defaultLocations = $db->listUserSetLocations($userID, $setID);
358 :     $orChecked = 1;
359 :     } else {
360 :     @defaultLocations = @globalLocations;
361 :     }
362 :    
363 :     $ipSelector = CGI::scrolling_list({
364 :     -name => "set.$setID.selected_ip_locations",
365 :     -values => [ @locations ],
366 :     -default => [ @defaultLocations ],
367 :     -size => 3,
368 :     -multiple => 'true'});
369 :    
370 :     # also show global set location list when editing
371 :     # user sets
372 :     $glIPlist = join(', ', @globalLocations);
373 :     }
374 : toenail 2794 }
375 :    
376 :     my $output = CGI::start_table({border => 0, cellpadding => 1});
377 : sh002i 2948 if ($forUsers) {
378 : gage 4258 $output .= CGI::Tr({},
379 : gage 2952 CGI::th({colspan=>"2"}, "&nbsp;"),
380 : toenail 3061 CGI::th({colspan=>"1"}, "User Values"),
381 :     CGI::th({}, "Class values"),
382 : sh002i 2948 );
383 :     }
384 :    
385 : toenail 2794 foreach my $field (@fieldOrder) {
386 :     my %properties = %{ FIELD_PROPERTIES()->{$field} };
387 :     unless ($properties{type} eq "hidden") {
388 : glarose 3377 $output .= CGI::Tr({}, CGI::td({}, [$self->FieldHTML($userID, $setID, $problemID, $globalRecord, $userRecord, $field)])) . "\n";
389 : toenail 2794 }
390 : glarose 4904 # IP RESTRICT
391 :     # we insert the list of locations after the restrict_ip selector,
392 :     # only if ip restrictions are turned on
393 :     if ( $field eq 'restrict_ip' && $ipSelector ) {
394 :     # FIXME: need && $check for canoverride; requires adding selected_ip_locations
395 :     # FIXME: to the field values hash, above
396 :     my $override = ($forUsers) ?
397 :     CGI::checkbox({ type => "checkbox",
398 :     name => "set.$setID.selected_ip_locations.override",
399 :     label => "",
400 :     checked => $orChecked }) : '';
401 :     $output .= CGI::Tr({-valign=>'top'},
402 :     CGI::td({}, [ $override, 'Restrict Locations',
403 :     $ipSelector,
404 :     $forUsers ? " $glIPlist" : '', ]
405 :     ),
406 :     );
407 :     }
408 :    
409 : glarose 3377 # this is a rather artifical addition to include gateway fields, which we
410 :     # only want to show for gateways
411 : glarose 4904 if ( $field eq 'assignment_type' && $gwoutput ) {
412 :     $output .= "$gwoutput\n";
413 :     }
414 : toenail 2794 }
415 :    
416 :     if (defined $problemID) {
417 : sh002i 2913 #my $problemRecord = $r->{db}->getUserProblem($userID, $setID, $problemID);
418 :     my $problemRecord = $userRecord; # we get this from the caller, hopefully
419 : toenail 2794 $output .= CGI::Tr({}, CGI::td({}, ["","Attempts", ($problemRecord->num_correct || 0) + ($problemRecord->num_incorrect || 0)])) if $forOneUser;
420 :     }
421 :     $output .= CGI::end_table();
422 :    
423 :     return $output;
424 :     }
425 :    
426 :     # Returns a list of information and HTML widgets
427 :     # for viewing and editing the specified db fields
428 :     # if only the setID is included, it creates a list of set information
429 :     # if the problemID is included, it creates a list of problem information
430 :     sub FieldHTML {
431 : sh002i 2913 my ($self, $userID, $setID, $problemID, $globalRecord, $userRecord, $field) = @_;
432 : toenail 2794
433 :     my $r = $self->r;
434 :     my $db = $r->db;
435 :     my @editForUser = $r->param('editForUser');
436 :     my $forUsers = scalar(@editForUser);
437 :     my $forOneUser = $forUsers == 1;
438 :    
439 : sh002i 2913 #my ($globalRecord, $userRecord, $mergedRecord);
440 :     #if (defined $problemID) {
441 :     # $globalRecord = $db->getGlobalProblem($setID, $problemID);
442 :     # $userRecord = $db->getUserProblem($userID, $setID, $problemID);
443 :     # #$mergedRecord = $db->getMergedProblem($userID, $setID, $problemID); # never used --sam
444 :     #} else {
445 :     # $globalRecord = $db->getGlobalSet($setID);
446 :     # $userRecord = $db->getUserSet($userID, $setID);
447 :     # #$mergedRecord = $db->getMergedSet($userID, $setID); # never user --sam
448 :     #}
449 : toenail 2794
450 :     return "No data exists for set $setID and problem $problemID" unless $globalRecord;
451 :     return "No user specific data exists for user $userID" if $forOneUser and $globalRecord and not $userRecord;
452 :    
453 :     my %properties = %{ FIELD_PROPERTIES()->{$field} };
454 :     my %labels = %{ $properties{labels} };
455 :     return "" if $properties{type} eq "hidden";
456 :     return "" if $properties{override} eq "one" && not $forOneUser;
457 :     return "" if $properties{override} eq "none" && not $forOneUser;
458 :     return "" if $properties{override} eq "all" && $forUsers;
459 :    
460 :     my $edit = ($properties{type} eq "edit") && ($properties{override} ne "none");
461 :     my $choose = ($properties{type} eq "choose") && ($properties{override} ne "none");
462 :    
463 :     my $globalValue = $globalRecord->{$field};
464 : gage 3046 # use defined instead of value in order to allow 0 to printed, e.g. for the 'value' field
465 :     $globalValue = (defined($globalValue)) ? ($labels{$globalValue || ""} || $globalValue) : "";
466 : toenail 2794 my $userValue = $userRecord->{$field};
467 : gage 3046 $userValue = (defined($userValue)) ? ($labels{$userValue || ""} || $userValue) : "";
468 : toenail 2794
469 :     if ($field =~ /_date/) {
470 : toenail 3061 $globalValue = $self->formatDateTime($globalValue) if defined $globalValue && $globalValue ne $labels{""};
471 :     $userValue = $self->formatDateTime($userValue) if defined $userValue && $userValue ne $labels{""};
472 : toenail 2794 }
473 :    
474 : glarose 4861 if ( defined($properties{convertby}) && $properties{convertby} ) {
475 :     $globalValue = $globalValue/$properties{convertby} if $globalValue;
476 :     $userValue = $userValue/$properties{convertby} if $userValue;
477 :     }
478 :    
479 : toenail 2794 # check to make sure that a given value can be overridden
480 :     my %canOverride = map { $_ => 1 } (@{ PROBLEM_FIELDS() }, @{ SET_FIELDS() });
481 :     my $check = $canOverride{$field};
482 :    
483 :     # $recordType is a shorthand in the return statement for problem or set
484 :     # $recordID is a shorthand in the return statement for $problemID or $setID
485 :     my $recordType = "";
486 :     my $recordID = "";
487 :     if (defined $problemID) {
488 :     $recordType = "problem";
489 :     $recordID = $problemID;
490 :     } else {
491 :     $recordType = "set";
492 :     $recordID = $setID;
493 :     }
494 :    
495 :     # $inputType contains either an input box or a popup_menu for changing a given db field
496 :     my $inputType = "";
497 :     if ($edit) {
498 :     $inputType = CGI::input({
499 :     name => "$recordType.$recordID.$field",
500 : toenail 2834 value => $r->param("$recordType.$recordID.$field") || ($forUsers ? $userValue : $globalValue),
501 : toenail 2794 size => $properties{size} || 5,
502 :     });
503 :     } elsif ($choose) {
504 :     # Note that in popup menus, you're almost guaranteed to have the choices hashed to labels in %properties
505 :     # but $userValue and and $globalValue are the values in the hash not the keys
506 :     # so we have to use the actual db record field values to select our default here.
507 :     $inputType = CGI::popup_menu({
508 :     name => "$recordType.$recordID.$field",
509 :     values => $properties{choices},
510 :     labels => \%labels,
511 : glarose 4861 default => $r->param("$recordType.$recordID.$field") || ($forUsers && $userRecord->$field ne '' ? $userRecord->$field : $globalRecord->$field),
512 : toenail 2794 });
513 :     }
514 :    
515 : glarose 4861 my $gDisplVal = defined($properties{labels}) && defined($properties{labels}->{$globalValue}) ? $properties{labels}->{$globalValue} : $globalValue;
516 :    
517 :     # return (($forUsers && $edit && $check) ? CGI::checkbox({
518 :     return (($forUsers && $check) ? CGI::checkbox({
519 : toenail 2794 type => "checkbox",
520 :     name => "$recordType.$recordID.$field.override",
521 :     label => "",
522 :     value => $field,
523 : toenail 3061 checked => $r->param("$recordType.$recordID.$field.override") || ($userValue ne ($labels{""} || "") ? 1 : 0),
524 : toenail 2794 }) : "",
525 :     $properties{name},
526 :     $inputType,
527 : glarose 4861 $forUsers ? " $gDisplVal" : "",
528 : toenail 2794 );
529 :     }
530 :    
531 :     # creates a popup menu of all possible problem numbers (for possible rearranging)
532 :     sub problem_number_popup {
533 :     my $num = shift;
534 :     my $total = shift;
535 :     return (CGI::popup_menu(-name => "problem_num_$num",
536 :     -values => [1..$total],
537 :     -default => $num));
538 :     }
539 :    
540 :     # handles rearrangement necessary after changes to problem ordering
541 :     sub handle_problem_numbers {
542 :     my $newProblemNumbersref = shift;
543 :     my %newProblemNumbers = %$newProblemNumbersref;
544 :     my $maxNum = shift;
545 :     my $db = shift;
546 : toenail 2816 my $setID = shift;
547 : toenail 2794 my $force = shift || 0;
548 :     my @sortme=();
549 :     my ($j, $val);
550 :    
551 : sh002i 3802 # keys are current problem numbers, values are target problem numbers
552 : toenail 2794 foreach $j (keys %newProblemNumbers) {
553 : sh002i 3802 # we don't want to act unless all problems have been assigned a new problem number, so if any have not, return
554 : toenail 2794 return "" if (not defined $newProblemNumbers{"$j"});
555 : sh002i 3802 # if the problem has been given a new number, we reduce the "score" of the problem by the original number of the problem
556 :     # when multiple problems are assigned the same number, this results in the last one ending up first -- FIXME?
557 : toenail 2794 if ($newProblemNumbers{"$j"} != $j) {
558 : sh002i 3802 # force always gets set if reordering is done, so don't expect to be able to delete a problem,
559 :     # reorder some other problems, and end up with a hole -- FIXME
560 : toenail 2794 $force = 1;
561 :     $val = 1000 * $newProblemNumbers{$j} - $j;
562 :     } else {
563 :     $val = 1000 * $newProblemNumbers{$j};
564 :     }
565 : sh002i 3802 # store a mapping between current problem number and score (based on currnet and new problem number)
566 : toenail 2794 push @sortme, [$j, $val];
567 : sh002i 3802 # replace new problem numbers in hash with the (global) problems themselves
568 : toenail 2816 $newProblemNumbers{$j} = $db->getGlobalProblem($setID, $j);
569 :     die "global $j for set $setID not found." unless $newProblemNumbers{$j};
570 : toenail 2794 }
571 :    
572 : sh002i 3802 # we don't have to do anything if we're not getting rid of holes
573 : toenail 2794 return "" unless $force;
574 :    
575 : sh002i 3802 # sort the curr. prob. num./score pairs by score
576 : toenail 2794 @sortme = sort {$a->[1] <=> $b->[1]} @sortme;
577 :     # now, for global and each user with this set, loop through problem list
578 :     # get all of the problem records
579 :     # assign new problem numbers
580 :     # loop - if number is new, put the problem record
581 :     # print "Sorted to get ". join(', ', map {$_->[0] } @sortme) ."<p>\n";
582 :    
583 :    
584 :     # Now, three stages. First global values
585 :    
586 :     for ($j = 0; $j < scalar @sortme; $j++) {
587 : sh002i 3802 if($sortme[$j][0] == $j + 1) {
588 :     # if the jth problem (according to the new ordering) is in the right place (problem IDs are numbered from 1, hence $j+1)
589 : toenail 2794 # do nothing
590 :     } elsif (not defined $newProblemNumbers{$j + 1}) {
591 : sh002i 3802 # otherwise, if there's a hole for it, add it there
592 :     $newProblemNumbers{$sortme[$j][0]}->problem_id($j + 1);
593 :     $db->addGlobalProblem($newProblemNumbers{$sortme[$j][0]});
594 : toenail 2794 } else {
595 : sh002i 3802 # otherwise, overwrite the data for the problem that's already there with the jth problem's data (with a changed problemID)
596 :     $newProblemNumbers{$sortme[$j][0]}->problem_id($j + 1);
597 :     $db->putGlobalProblem($newProblemNumbers{$sortme[$j][0]});
598 : toenail 2794 }
599 :     }
600 :    
601 : toenail 2816 my @setUsers = $db->listSetUsers($setID);
602 : toenail 2794 my (@problist, $user);
603 :    
604 :     foreach $user (@setUsers) {
605 : sh002i 3802 # grab a copy of each UserProblem for this user. @problist can be sparse (if problems were deleted)
606 : toenail 2794 for $j (keys %newProblemNumbers) {
607 : toenail 2816 $problist[$j] = $db->getUserProblem($user, $setID, $j);
608 : toenail 2794 }
609 :     for($j = 0; $j < scalar @sortme; $j++) {
610 : sh002i 3802 if ($sortme[$j][0] == $j + 1) {
611 :     # same as above -- the jth problem is in the right place, so don't worry about it
612 : toenail 2794 # do nothing
613 : sh002i 3802 } elsif ($problist[$sortme[$j][0]]) {
614 :     # we've made sure the user's problem actually exists HERE, since we want to be able to fail gracefullly if it doesn't
615 :     # the problem with the original conditional below is that %newProblemNumbers maps oldids => global problem record
616 :     # we need to check if the target USER PROBLEM exists, which is what @problist knows
617 :     #if (not defined $newProblemNumbers{$j + 1}) {
618 :     if (not defined $problist[$j+1]) {
619 :     # same as above -- there's a hole for that problem to go into, so add it in its new place
620 :     $problist[$sortme[$j][0]]->problem_id($j + 1);
621 :     $db->addUserProblem($problist[$sortme[$j][0]]);
622 :     } else {
623 :     # same as above -- there's a problem already there, so overwrite its data with the data from the jth problem
624 :     $problist[$sortme[$j][0]]->problem_id($j + 1);
625 :     $db->putUserProblem($problist[$sortme[$j][0]]);
626 :     }
627 :     } else {
628 :     warn "UserProblem missing for user=$user set=$setID problem=$sortme[$j][0]. This may indicate database corruption.\n";
629 :     # when a problem doesn't exist in the target slot, a new problem gets added there, but the original problem
630 :     # never gets overwritten (because there wan't a problem it would have to get exchanged with)
631 :     # i think this can get pretty complex. consider 1=>2, 2=>3, 3=>4, 4=>1 where problem 1 doesn't exist for some user:
632 :     # @sortme[$j][0] will contain: 4, 1, 2, 3
633 :     # - problem 1 will get **added** with the data from problem 4 (because problem 1 doesn't exist for this user)
634 :     # - problem 2 will get overwritten with the data from problem 1
635 :     # - problem 3 will get overwritten with the data from problem 2
636 :     # - nothing will happend to problem 4, since problem 1 doesn't exit
637 :     # so the solution is to delete problem 4 altogether!
638 :     # here's the fix:
639 :    
640 :     # the data from problem $j+1 was/will be moved to another problem slot,
641 :     # but there's no problem $sortme[$j][0] to replace it. thus, we delete it now.
642 :     $db->deleteUserProblem($user, $setID, $j+1);
643 :     }
644 : toenail 2794 }
645 :     }
646 :    
647 : sh002i 3802 # any problems with IDs above $maxNum get deleted -- presumably their data has been copied into problems with lower IDs
648 : toenail 2794 foreach ($j = scalar @sortme; $j < $maxNum; $j++) {
649 :     if (defined $newProblemNumbers{$j + 1}) {
650 : toenail 2816 $db->deleteGlobalProblem($setID, $j+1);
651 : toenail 2794 }
652 :     }
653 :    
654 : sh002i 3802 # return a string form of the old problem IDs in the new order (not used by caller, incidentally)
655 : toenail 2794 return join(', ', map {$_->[0]} @sortme);
656 :     }
657 :    
658 :     # swap index given with next bigger index
659 :     # leftover from when we had up/down buttons
660 :     # maybe we will bring them back
661 :    
662 : sh002i 2913 #sub moveme {
663 :     # my $index = shift;
664 :     # my $db = shift;
665 :     # my $setID = shift;
666 :     # my (@problemIDList) = @_;
667 :     # my ($prob1, $prob2, $prob);
668 :     #
669 :     # foreach my $problemID (@problemIDList) {
670 :     # my $problemRecord = $db->getGlobalProblem($setID, $problemID); # checked
671 :     # die "global $problemID for set $setID not found." unless $problemRecord;
672 :     # if ($problemRecord->problem_id == $index) {
673 :     # $prob1 = $problemRecord;
674 :     # } elsif ($problemRecord->problem_id == $index + 1) {
675 :     # $prob2 = $problemRecord;
676 :     # }
677 :     # }
678 :     # if (not defined $prob1 or not defined $prob2) {
679 :     # die "cannot find problem $index or " . ($index + 1);
680 :     # }
681 :     #
682 :     # $prob1->problem_id($index + 1);
683 :     # $prob2->problem_id($index);
684 :     # $db->putGlobalProblem($prob1);
685 :     # $db->putGlobalProblem($prob2);
686 :     #
687 :     # my @setUsers = $db->listSetUsers($setID);
688 :     #
689 :     # my $user;
690 :     # foreach $user (@setUsers) {
691 :     # $prob1 = $db->getUserProblem($user, $setID, $index); #checked
692 :     # die " problem $index for set $setID and effective user $user not found"
693 :     # unless $prob1;
694 :     # $prob2 = $db->getUserProblem($user, $setID, $index+1); #checked
695 :     # die " problem $index for set $setID and effective user $user not found"
696 :     # unless $prob2;
697 :     # $prob1->problem_id($index+1);
698 :     # $prob2->problem_id($index);
699 :     # $db->putUserProblem($prob1);
700 :     # $db->putUserProblem($prob2);
701 :     # }
702 :     #}
703 : toenail 2794
704 :     # primarily saves any changes into the correct set or problem records (global vs user)
705 :     # also deals with deleting or rearranging problems
706 :     sub initialize {
707 :     my ($self) = @_;
708 :     my $r = $self->r;
709 :     my $db = $r->db;
710 :     my $ce = $r->ce;
711 :     my $authz = $r->authz;
712 :     my $user = $r->param('user');
713 :     my $setID = $r->urlpath->arg("setID");
714 :     my $setRecord = $db->getGlobalSet($setID); # checked
715 :     die "global set $setID not found." unless $setRecord;
716 :    
717 :     $self->{set} = $setRecord;
718 :     my @editForUser = $r->param('editForUser');
719 :     # some useful booleans
720 :     my $forUsers = scalar(@editForUser);
721 :     my $forOneUser = $forUsers == 1;
722 :    
723 :     # Check permissions
724 :     return unless ($authz->hasPermissions($user, "access_instructor_tools"));
725 :     return unless ($authz->hasPermissions($user, "modify_problem_sets"));
726 :    
727 :    
728 :     my %properties = %{ FIELD_PROPERTIES() };
729 :    
730 :     # takes a hash of hashes and inverts it
731 :     my %undoLabels;
732 :     foreach my $key (keys %properties) {
733 :     %{ $undoLabels{$key} } = map { $properties{$key}->{labels}->{$_} => $_ } keys %{ $properties{$key}->{labels} };
734 :     }
735 :    
736 : toenail 2816 # Unfortunately not everyone uses Javascript enabled browsers so
737 :     # we must fudge the information coming from the ComboBoxes
738 :     # Since the textfield and menu both have the same name, we get an array of two elements
739 :     # We then reset the param to the first if its not-empty or the second (empty or not).
740 :     foreach ( @{ HEADER_ORDER() } ) {
741 :     my @values = $r->param("set.$setID.$_");
742 : toenail 2817 my $value = $values[0] || $values[1] || "";
743 : toenail 2816 $r->param("set.$setID.$_", $value);
744 :     }
745 : toenail 2794
746 : toenail 2901 #####################################################################
747 :     # Check date information
748 :     #####################################################################
749 :    
750 :     my ($open_date, $due_date, $answer_date);
751 :     my $error = 0;
752 : toenail 2794 if (defined $r->param('submit_changes')) {
753 : toenail 3061 my @names = ("open_date", "due_date", "answer_date");
754 :    
755 :     my %dates = map { $_ => $r->param("set.$setID.$_") } @names;
756 :     %dates = map {
757 :     my $unlabel = $undoLabels{$_}->{$dates{$_}};
758 :     $_ => defined $unlabel ? $setRecord->$_ : $self->parseDateTime($dates{$_})
759 :     } @names;
760 : toenail 2901
761 : toenail 3061 ($open_date, $due_date, $answer_date) = map { $dates{$_} } @names;
762 : toenail 2794
763 : toenail 2901 if ($answer_date < $due_date || $answer_date < $open_date) {
764 :     $self->addbadmessage("Answers cannot be made available until on or after the due date!");
765 :     $error = $r->param('submit_changes');
766 :     }
767 :    
768 :     if ($due_date < $open_date) {
769 :     $self->addbadmessage("Answers cannot be due until on or after the open date!");
770 :     $error = $r->param('submit_changes');
771 :     }
772 :    
773 : sh002i 3647 # make sure the dates are not more than 10 years in the future
774 :     my $curr_time = time;
775 :     my $seconds_per_year = 31_556_926;
776 :     my $cutoff = $curr_time + $seconds_per_year*10;
777 :     if ($open_date > $cutoff) {
778 :     $self->addbadmessage("Error: open date cannot be more than 10 years from now in set $setID");
779 :     $error = $r->param('submit_changes');
780 :     }
781 :     if ($due_date > $cutoff) {
782 :     $self->addbadmessage("Error: due date cannot be more than 10 years from now in set $setID");
783 :     $error = $r->param('submit_changes');
784 :     }
785 :     if ($answer_date > $cutoff) {
786 :     $self->addbadmessage("Error: answer date cannot be more than 10 years from now in set $setID");
787 :     $error = $r->param('submit_changes');
788 :     }
789 :    
790 :    
791 : toenail 2901 if ($error) {
792 :     $self->addbadmessage("No changes were saved!");
793 :     }
794 :     }
795 : sh002i 3647
796 : toenail 2901 if (defined $r->param('submit_changes') && !$error) {
797 :    
798 : sh002i 2913 #my $setRecord = $db->getGlobalSet($setID); # already fetched above --sam
799 : toenail 2901
800 : toenail 2794 #####################################################################
801 :     # Save general set information (including headers)
802 :     #####################################################################
803 :    
804 :     if ($forUsers) {
805 : sh002i 4518 # DBFIXME use a WHERE clause, iterator
806 : toenail 2794 my @userRecords = $db->getUserSets(map { [$_, $setID] } @editForUser);
807 :     foreach my $record (@userRecords) {
808 :     foreach my $field ( @{ SET_FIELDS() } ) {
809 :     next unless canChange($forUsers, $field);
810 : toenail 3061 my $override = $r->param("set.$setID.$field.override");
811 : toenail 2794
812 :     if (defined $override && $override eq $field) {
813 :    
814 :     my $param = $r->param("set.$setID.$field");
815 : toenail 2841 $param = $properties{$field}->{default} || "" unless defined $param && $param ne "";
816 : toenail 3061 my $unlabel = $undoLabels{$field}->{$param};
817 :     $param = $unlabel if defined $unlabel;
818 :     # $param = $undoLabels{$field}->{$param} || $param;
819 : toenail 2794 if ($field =~ /_date/) {
820 : toenail 3061 $param = $self->parseDateTime($param) unless defined $unlabel;
821 : toenail 2794 }
822 : glarose 4861 if (defined($properties{$field}->{convertby}) && $properties{$field}->{convertby}) {
823 :     $param = $param*$properties{$field}->{convertby};
824 :     }
825 : toenail 2794 $record->$field($param);
826 :     } else {
827 :     $record->$field(undef);
828 :     }
829 : toenail 3061
830 : toenail 2794 }
831 :     $db->putUserSet($record);
832 :     }
833 : glarose 4904
834 :     # IP RESTRICT
835 :     # the locations for ip restrictions are saved in the
836 :     # set_locations_user table, so we have to update
837 :     # these separately
838 :     # FIXME: need && $check for canoverride; requires adding selected_ip_locations
839 :     # FIXME: to the field values hash, above
840 :     if ( $r->param("set.$setID.selected_ip_locations.override") ) {
841 :     foreach my $record ( @userRecords ) {
842 :     my $userID = $record->user_id;
843 :     my @selectedLocations = $r->param("set.$setID.selected_ip_locations");
844 :     my @userSetLocations = $db->listUserSetLocations($userID,$setID);
845 :     my @addSetLocations = ();
846 :     my @delSetLocations = ();
847 :     foreach my $loc ( @selectedLocations ) {
848 :     push( @addSetLocations, $loc ) if ( ! grep( /^$loc$/, @userSetLocations ) );
849 :     }
850 :     foreach my $loc ( @userSetLocations ) {
851 :     push( @delSetLocations, $loc ) if ( ! grep( /^$loc$/, @selectedLocations ) );
852 :     }
853 :     # then update the user set_locations
854 :     foreach ( @addSetLocations ) {
855 :     my $Loc = $db->newUserSetLocation;
856 :     $Loc->set_id( $setID );
857 :     $Loc->user_id( $userID );
858 :     $Loc->location_id($_);
859 :     $db->addUserSetLocation($Loc);
860 :     }
861 :     foreach ( @delSetLocations ) {
862 :     $db->deleteUserSetLocation($userID,$setID,$_);
863 :     }
864 :     }
865 :     } else {
866 :     # if override isn't selected, then we want
867 :     # to be sure that there are no
868 :     # set_locations_user entries setting around
869 :     foreach my $record ( @userRecords ) {
870 :     my $userID = $record->user_id;
871 :     my @userLocations = $db->listUserSetLocations($userID,$setID);
872 :     foreach ( @userLocations ) {
873 :     $db->deleteUserSetLocation($userID,$setID,$_);
874 :     }
875 :     }
876 :     }
877 : toenail 2794 } else {
878 :     foreach my $field ( @{ SET_FIELDS() } ) {
879 :     next unless canChange($forUsers, $field);
880 :    
881 :     my $param = $r->param("set.$setID.$field");
882 : toenail 2841 $param = $properties{$field}->{default} || "" unless defined $param && $param ne "";
883 : glarose 4411
884 : toenail 3061 my $unlabel = $undoLabels{$field}->{$param};
885 :     $param = $unlabel if defined $unlabel;
886 : toenail 2794 if ($field =~ /_date/) {
887 : toenail 3061 $param = $self->parseDateTime($param) unless defined $unlabel;
888 : toenail 2794 }
889 : glarose 4861 if (defined($properties{$field}->{convertby}) && $properties{$field}->{convertby}) {
890 :     $param = $param*$properties{$field}->{convertby};
891 :     }
892 : toenail 2794 $setRecord->$field($param);
893 :     }
894 :     $db->putGlobalSet($setRecord);
895 : glarose 4904
896 :     # IP RESTRICT
897 :     # the locations for ip restrictions are saved in the
898 :     # set_locations table, so we have to update these
899 :     # separately
900 :     # FIXME: need && $check for canoverride; requires adding selected_ip_locations
901 :     # FIXME: to the field values hash, above
902 :     if ( $r->param("set.$setID.restrict_ip") ne 'No' ) {
903 :     my @selectedLocations = $r->param("set.$setID.selected_ip_locations");
904 :     my @globalSetLocations = $db->listGlobalSetLocations($setID);
905 :     my @addSetLocations = ();
906 :     my @delSetLocations = ();
907 :     foreach my $loc ( @selectedLocations ) {
908 :     push( @addSetLocations, $loc ) if ( ! grep( /^$loc$/, @globalSetLocations ) );
909 :     }
910 :     foreach my $loc ( @globalSetLocations ) {
911 :     push( @delSetLocations, $loc ) if ( ! grep( /^$loc$/, @selectedLocations ) );
912 :     }
913 :     # then update the global set_locations
914 :     foreach ( @addSetLocations ) {
915 :     my $Loc = $db->newGlobalSetLocation;
916 :     $Loc->set_id( $setID );
917 :     $Loc->location_id($_);
918 :     $db->addGlobalSetLocation($Loc);
919 :     }
920 :     foreach ( @delSetLocations ) {
921 :     $db->deleteGlobalSetLocation($setID,$_);
922 :     }
923 :     } else {
924 :     my @globalSetLocations = $db->listGlobalSetLocations($setID);
925 :     foreach ( @globalSetLocations ) {
926 :     $db->deleteGlobalSetLocation($setID,$_);
927 :     }
928 :     }
929 : toenail 2794 }
930 : sh002i 3721
931 : toenail 2794 #####################################################################
932 :     # Save problem information
933 :     #####################################################################
934 :    
935 : sh002i 4518 # DBFIXME use a WHERE clause, iterator?
936 : toenail 2901 my @problemIDs = sort { $a <=> $b } $db->listGlobalProblems($setID);;
937 : toenail 2816 my @problemRecords = $db->getGlobalProblems(map { [$setID, $_] } @problemIDs);
938 :     foreach my $problemRecord (@problemRecords) {
939 : toenail 2794 my $problemID = $problemRecord->problem_id;
940 :     die "Global problem $problemID for set $setID not found." unless $problemRecord;
941 :    
942 :     if ($forUsers) {
943 :     # Since we're editing for specific users, we don't allow the GlobalProblem record to be altered on that same page
944 :     # So we only need to make changes to the UserProblem record and only then if we are overriding a value
945 :     # in the GlobalProblem record or for fields unique to the UserProblem record.
946 :    
947 :     my @userIDs = @editForUser;
948 :     my @userProblemIDs = map { [$_, $setID, $problemID] } @userIDs;
949 : sh002i 4518 # DBFIXME where clause? iterator?
950 : toenail 2794 my @userProblemRecords = $db->getUserProblems(@userProblemIDs);
951 :     foreach my $record (@userProblemRecords) {
952 :    
953 :     my $changed = 0; # keep track of any changes, if none are made, avoid unnecessary db accesses
954 :     foreach my $field ( @{ PROBLEM_FIELDS() } ) {
955 :     next unless canChange($forUsers, $field);
956 :    
957 :     my $override = $r->param("problem.$problemID.$field.override");
958 :     if (defined $override && $override eq $field) {
959 :    
960 :     my $param = $r->param("problem.$problemID.$field");
961 : toenail 2841 $param = $properties{$field}->{default} || "" unless defined $param && $param ne "";
962 : toenail 3061 my $unlabel = $undoLabels{$field}->{$param};
963 :     $param = $unlabel if defined $unlabel;
964 : toenail 2794 $changed ||= changed($record->$field, $param);
965 :     $record->$field($param);
966 :     } else {
967 :     $changed ||= changed($record->$field, undef);
968 :     $record->$field(undef);
969 :     }
970 :    
971 :     }
972 :    
973 :     foreach my $field ( @{ USER_PROBLEM_FIELDS() } ) {
974 :     next unless canChange($forUsers, $field);
975 :    
976 :     my $param = $r->param("problem.$problemID.$field");
977 : toenail 2841 $param = $properties{$field}->{default} || "" unless defined $param && $param ne "";
978 : toenail 3061 my $unlabel = $undoLabels{$field}->{$param};
979 :     $param = $unlabel if defined $unlabel;
980 : toenail 2794 $changed ||= changed($record->$field, $param);
981 :     $record->$field($param);
982 :     }
983 :     $db->putUserProblem($record) if $changed;
984 :     }
985 :     } else {
986 :     # Since we're editing for ALL set users, we will make changes to the GlobalProblem record.
987 :     # We may also have instances where a field is unique to the UserProblem record but we want
988 :     # all users to (at least initially) have the same value
989 :    
990 :     # this only edits a globalProblem record
991 :     my $changed = 0; # keep track of any changes, if none are made, avoid unnecessary db accesses
992 :     foreach my $field ( @{ PROBLEM_FIELDS() } ) {
993 :     next unless canChange($forUsers, $field);
994 :    
995 :     my $param = $r->param("problem.$problemID.$field");
996 : toenail 2841 $param = $properties{$field}->{default} || "" unless defined $param && $param ne "";
997 : toenail 3061 my $unlabel = $undoLabels{$field}->{$param};
998 :     $param = $unlabel if defined $unlabel;
999 : toenail 2794 $changed ||= changed($problemRecord->$field, $param);
1000 :     $problemRecord->$field($param);
1001 :     }
1002 :     $db->putGlobalProblem($problemRecord) if $changed;
1003 :    
1004 :    
1005 :     # sometimes (like for status) we might want to change an attribute in
1006 :     # the userProblem record for every assigned user
1007 :     # However, since this data is stored in the UserProblem records,
1008 :     # it won't be displayed once its been changed and if you hit "Save Changes" again
1009 :     # it gets erased
1010 :    
1011 :     # So we'll enforce that there be something worth putting in all the UserProblem records
1012 :     # This also will make hitting "Save Changes" on the global page MUCH faster
1013 :     my %useful;
1014 :     foreach my $field ( @{ USER_PROBLEM_FIELDS() } ) {
1015 :     my $param = $r->param("problem.$problemID.$field");
1016 :     $useful{$field} = 1 if defined $param and $param ne "";
1017 :     }
1018 :    
1019 :     if (keys %useful) {
1020 : sh002i 4518 # DBFIXME where clause, iterator
1021 : toenail 2794 my @userIDs = $db->listProblemUsers($setID, $problemID);
1022 :     my @userProblemIDs = map { [$_, $setID, $problemID] } @userIDs;
1023 :     my @userProblemRecords = $db->getUserProblems(@userProblemIDs);
1024 :     foreach my $record (@userProblemRecords) {
1025 :     my $changed = 0; # keep track of any changes, if none are made, avoid unnecessary db accesses
1026 : toenail 2901 foreach my $field ( keys %useful ) {
1027 : toenail 2794 next unless canChange($forUsers, $field);
1028 : toenail 2901
1029 : toenail 2794 my $param = $r->param("problem.$problemID.$field");
1030 : toenail 2841 $param = $properties{$field}->{default} || "" unless defined $param && $param ne "";
1031 : toenail 3061 my $unlabel = $undoLabels{$field}->{$param};
1032 :     $param = $unlabel if defined $unlabel;
1033 : toenail 2794 $changed ||= changed($record->$field, $param);
1034 :     $record->$field($param);
1035 :     }
1036 :     $db->putUserProblem($record) if $changed;
1037 :     }
1038 :     }
1039 :     }
1040 :     }
1041 : toenail 2834
1042 : toenail 2901 # Mark the specified problems as correct for all users
1043 :     foreach my $problemID ($r->param('markCorrect')) {
1044 : sh002i 4518 # DBFIXME where clause, iterator
1045 : toenail 2901 my @userProblemIDs = map { [$_, $setID, $problemID] } ($forUsers ? @editForUser : $db->listProblemUsers($setID, $problemID));
1046 :     my @userProblemRecords = $db->getUserProblems(@userProblemIDs);
1047 :     foreach my $record (@userProblemRecords) {
1048 :     if (defined $record && ($record->status eq "" || $record->status < 1)) {
1049 :     $record->status(1);
1050 :     $record->attempted(1);
1051 :     $db->putUserProblem($record);
1052 :     }
1053 :     }
1054 : toenail 2834 }
1055 : sh002i 3721
1056 :     # Delete all problems marked for deletion
1057 :     foreach my $problemID ($r->param('deleteProblem')) {
1058 :     $db->deleteGlobalProblem($setID, $problemID);
1059 :     }
1060 :    
1061 :     #####################################################################
1062 :     # Add blank problem if needed
1063 :     #####################################################################
1064 :     if (defined($r->param("add_blank_problem") ) and $r->param("add_blank_problem") == 1) {
1065 :     my $targetProblemNumber = 1+ WeBWorK::Utils::max( $self->r->db->listGlobalProblems($setID));
1066 : gage 3790 ##################################################
1067 :     # make local copy of the blankProblem
1068 :     ##################################################
1069 : sh002i 3721 my $blank_file_path = $ce->{webworkFiles}->{screenSnippets}->{blankProblem};
1070 : gage 3790 my $problemContents = WeBWorK::Utils::readFile($blank_file_path);
1071 :     my $new_file_path = "set$setID/".BLANKPROBLEM();
1072 :     my $fullPath = WeBWorK::Utils::surePathToFile($ce->{courseDirs}->{templates},'/'.$new_file_path);
1073 :     local(*TEMPFILE);
1074 :     open(TEMPFILE, ">$fullPath") or warn "Can't write to file $fullPath";
1075 :     print TEMPFILE $problemContents;
1076 :     close(TEMPFILE);
1077 :    
1078 : sh002i 3721 #################################################
1079 :     # Update problem record
1080 :     #################################################
1081 :     my $problemRecord = $self->addProblemToSet(
1082 :     setName => $setID,
1083 : gage 3790 sourceFile => $new_file_path,
1084 : sh002i 3721 problemID => $targetProblemNumber, #added to end of set
1085 :     );
1086 : gage 3787 $self->assignProblemToAllSetUsers($problemRecord);
1087 : gage 3790 $self->addgoodmessage("Added $new_file_path to ". $setID. " as problem $targetProblemNumber") ;
1088 : sh002i 3721 }
1089 :    
1090 :     # Sets the specified header to "" so that the default file will get used.
1091 :     foreach my $header ($r->param('defaultHeader')) {
1092 :     $setRecord->$header("");
1093 :     }
1094 : toenail 2901 }
1095 : toenail 2834
1096 : toenail 2794 # Leftover code from when there were up/down buttons
1097 :    
1098 :     # } else {
1099 :     # # Look for up and down buttons
1100 :     # my $index = 2;
1101 :     # while ($index <= scalar @problemList) {
1102 :     # if (defined $r->param("move.up.$index.x")) {
1103 :     # moveme($index-1, $db, $setID, @problemList);
1104 :     # }
1105 :     # $index++;
1106 :     # }
1107 :     # $index = 1;
1108 :     #
1109 :     # while ($index < scalar @problemList) {
1110 :     # if (defined $r->param("move.down.$index.x")) {
1111 :     # moveme($index, $db, $setID, @problemList);
1112 :     # }
1113 :     # $index++;
1114 :     # }
1115 : toenail 2834 # }
1116 :    
1117 : toenail 2794
1118 : toenail 2901 # This erases any sticky fields if the user saves changes, resets the form, or reorders problems
1119 :     # It may not be obvious why this is necessary when saving changes or reordering problems
1120 :     # but when the problems are reorder the param problem.1.source_file needs to be the source
1121 :     # file of the problem that is NOW #1 and not the problem that WAS #1.
1122 :     unless (defined $r->param('refresh')) {
1123 :    
1124 :     # reset all the parameters dealing with set/problem/header information
1125 :     # if the current naming scheme is changed/broken, this could reek havoc
1126 :     # on all kinds of things
1127 :     foreach my $param ($r->param) {
1128 : toenail 3102 $r->param($param, "") if $param =~ /^(set|problem|header)\./ && $param !~ /displaymode/;
1129 : toenail 2901 }
1130 :     }
1131 : toenail 2794 }
1132 :    
1133 :     # helper method for debugging
1134 : toenail 2901 sub definedness ($) {
1135 : toenail 2794 my ($variable) = @_;
1136 :    
1137 :     return "undefined" unless defined $variable;
1138 :     return "empty" unless $variable ne "";
1139 :     return $variable;
1140 :     }
1141 :    
1142 :     # helper method for checking if two things are different
1143 :     # the return values will usually be thrown away, but they could be useful for debugging
1144 :     sub changed ($$) {
1145 :     my ($first, $second) = @_;
1146 :    
1147 :     return "def/undef" if defined $first and not defined $second;
1148 :     return "undef/def" if not defined $first and defined $second;
1149 : toenail 2834 return "" if not defined $first and not defined $second;
1150 : toenail 2794 return "ne" if $first ne $second;
1151 : toenail 2834 return ""; # if they're equal, there's no change
1152 : toenail 2794 }
1153 :    
1154 : toenail 2834 # helper method that determines for how many users at a time a field can be changed
1155 : toenail 2794 # none means it can't be changed for anyone
1156 :     # any means it can be changed for anyone
1157 :     # one means it can ONLY be changed for one at a time. (eg problem_seed)
1158 :     # all means it can ONLY be changed for all at a time. (eg set_header)
1159 :     sub canChange ($$) {
1160 :     my ($forUsers, $field) = @_;
1161 :    
1162 :     my %properties = %{ FIELD_PROPERTIES() };
1163 :     my $forOneUser = $forUsers == 1;
1164 :    
1165 :     my $howManyCan = $properties{$field}->{override};
1166 :    
1167 :     return 0 if $howManyCan eq "none";
1168 :     return 1 if $howManyCan eq "any";
1169 :     return 1 if $howManyCan eq "one" && $forOneUser;
1170 : toenail 2816 return 1 if $howManyCan eq "all" && !$forUsers;
1171 : toenail 2794 return 0; # FIXME: maybe it should default to 1?
1172 :     }
1173 :    
1174 : toenail 2834 # helper method that determines if a file is valid and returns a pretty error message
1175 :     sub checkFile ($) {
1176 :     my ($self, $file) = @_;
1177 :    
1178 :     my $r = $self->r;
1179 :     my $ce = $r->ce;
1180 :    
1181 :     return "No source file specified" unless $file;
1182 :     $file = $ce->{courseDirs}->{templates} . '/' . $file unless $file =~ m|^/|;
1183 :    
1184 :     my $text = "This source file ";
1185 :     my $fileError;
1186 :     return "" if -e $file && -f $file && -r $file;
1187 :     return $text . "is not readable!" if -e $file && -f $file;
1188 :     return $text . "is a directory!" if -d $file;
1189 :     return $text . "does not exist!" unless -e $file;
1190 :     return $text . "is not a plain file!";
1191 :     }
1192 :    
1193 : sh002i 3476 # don't show view options -- we provide display mode controls for headers/problems separately
1194 :     sub options {
1195 :     return "";
1196 :     }
1197 :    
1198 : toenail 2794 # Creates two separate tables, first of the headers, and the of the problems in a given set
1199 :     # If one or more users are specified in the "editForUser" param, only the data for those users
1200 :     # becomes editable, not all the data
1201 :     sub body {
1202 :    
1203 :     my ($self) = @_;
1204 :     my $r = $self->r;
1205 :     my $db = $r->db;
1206 :     my $ce = $r->ce;
1207 :     my $authz = $r->authz;
1208 :     my $userID = $r->param('user');
1209 :     my $urlpath = $r->urlpath;
1210 : toenail 2901 my $courseID = $urlpath->arg("courseID");
1211 :     my $setID = $urlpath->arg("setID");
1212 :     my $setRecord = $db->getGlobalSet($setID) or die "No record for global set $setID.";
1213 :    
1214 :     my $userRecord = $db->getUser($userID) or die "No record for user $userID.";
1215 :     # Check permissions
1216 :     return CGI::div({class=>"ResultsWithError"}, "You are not authorized to access the Instructor tools.")
1217 :     unless $authz->hasPermissions($userRecord->user_id, "access_instructor_tools");
1218 :    
1219 :     return CGI::div({class=>"ResultsWithError"}, "You are not authorized to modify problems.")
1220 :     unless $authz->hasPermissions($userRecord->user_id, "modify_problem_sets");
1221 :    
1222 : toenail 2794 my @editForUser = $r->param('editForUser');
1223 :    
1224 : toenail 2901 # Check that every user that we're editing for has a valid UserSet
1225 :     my @assignedUsers;
1226 :     my @unassignedUsers;
1227 :     if (scalar @editForUser) {
1228 :     foreach my $ID (@editForUser) {
1229 : sh002i 4518 # DBFIXME iterator
1230 : toenail 2901 if ($db->getUserSet($ID, $setID)) {
1231 :     unshift @assignedUsers, $ID;
1232 :     } else {
1233 :     unshift @unassignedUsers, $ID;
1234 :     }
1235 :     }
1236 : gage 3790 @editForUser = sort @assignedUsers;
1237 : toenail 2901 $r->param("editForUser", \@editForUser);
1238 :    
1239 :     if (scalar @editForUser && scalar @unassignedUsers) {
1240 :     print CGI::div({class=>"ResultsWithError"}, "The following users are NOT assigned to this set and will be ignored: " . CGI::b(join(", ", @unassignedUsers)));
1241 :     } elsif (scalar @editForUser == 0) {
1242 :     print CGI::div({class=>"ResultsWithError"}, "None of the selected users are assigned to this set: " . CGI::b(join(", ", @unassignedUsers)));
1243 :     print CGI::div({class=>"ResultsWithError"}, "Global set data will be shown instead of user specific data");
1244 :     }
1245 :     }
1246 :    
1247 : toenail 2794 # some useful booleans
1248 :     my $forUsers = scalar(@editForUser);
1249 :     my $forOneUser = $forUsers == 1;
1250 :    
1251 : toenail 2901 # If you're editing for users, initially their records will be different but
1252 : toenail 2794 # if you make any changes to them they will be the same.
1253 :     # if you're editing for one user, the problems shown should be his/hers
1254 : toenail 2901 my $userToShow = $forUsers ? $editForUser[0] : $userID;
1255 : toenail 2794
1256 : sh002i 4518 # DBFIXME no need to get ID lists -- counts would be fine
1257 : toenail 2794 my $userCount = $db->listUsers();
1258 : gage 3790 my $setCount = $db->listGlobalSets(); # if $forOneUser;
1259 : toenail 2816 my $setUserCount = $db->countSetUsers($setID);
1260 : toenail 2807 my $userSetCount = $db->countUserSets($editForUser[0]) if $forOneUser;
1261 : toenail 2901
1262 :    
1263 : toenail 2794 my $editUsersAssignedToSetURL = $self->systemLink(
1264 :     $urlpath->newFromModule(
1265 :     "WeBWorK::ContentGenerator::Instructor::UsersAssignedToSet",
1266 : toenail 2816 courseID => $courseID, setID => $setID));
1267 : toenail 2807 my $editSetsAssignedToUserURL = $self->systemLink(
1268 :     $urlpath->newFromModule(
1269 : gage 3856 "WeBWorK::ContentGenerator::Instructor::UserDetail",
1270 : toenail 2816 courseID => $courseID, userID => $editForUser[0])) if $forOneUser;
1271 : toenail 2794
1272 : toenail 2807
1273 : toenail 2816 my $setDetailPage = $urlpath -> newFromModule($urlpath->module, courseID => $courseID, setID => $setID);
1274 : gage 3863 my $setDetailURL = $self->systemLink($setDetailPage, authen=>0);
1275 : toenail 2794
1276 :    
1277 : toenail 2807 my $userCountMessage = CGI::a({href=>$editUsersAssignedToSetURL}, $self->userCountMessage($setUserCount, $userCount));
1278 :     my $setCountMessage = CGI::a({href=>$editSetsAssignedToUserURL}, $self->setCountMessage($userSetCount, $setCount)) if $forOneUser;
1279 : toenail 2794
1280 : toenail 2816 $userCountMessage = "The set $setID is assigned to " . $userCountMessage . ".";
1281 : toenail 2807 $setCountMessage = "The user $editForUser[0] has been assigned " . $setCountMessage . "." if $forOneUser;
1282 : toenail 2794
1283 : toenail 2807 if ($forUsers) {
1284 : gage 3790 ##############################################
1285 :     # calculate links for the users being edited:
1286 :     ##############################################
1287 :     my @userLinks = ();
1288 :     foreach my $userID (@editForUser) {
1289 :     my $u = $db->getUser($userID);
1290 : apizer 4064 my $email_address = $u->email_address;
1291 :     my $line = $u->last_name.", ".$u->first_name."&nbsp;&nbsp;(".CGI::a({-href=>"mailto:$email_address"},"email "). $u->user_id."). Assigned to ";
1292 : gage 3790 my $editSetsAssignedToUserURL = $self->systemLink(
1293 :     $urlpath->newFromModule(
1294 : gage 3856 "WeBWorK::ContentGenerator::Instructor::UserDetail",
1295 : gage 3790 courseID => $courseID, userID => $u->user_id));
1296 :     $line .= CGI::a({href=>$editSetsAssignedToUserURL},
1297 :     $self->setCountMessage($db->countUserSets($u->user_id), $setCount));
1298 :     unshift @userLinks,$line;
1299 : toenail 2807 }
1300 : gage 3790 @userLinks = sort @userLinks;
1301 :    
1302 :     print CGI::table({border=>2,cellpadding=>10},
1303 : gage 4276 CGI::Tr({},
1304 : gage 3790 CGI::td([
1305 :     "Editing problem set ".CGI::strong($setID)." data for these individual students:".CGI::br().
1306 :     CGI::strong(join CGI::br(), @userLinks),
1307 : gage 3863 CGI::a({href=>$self->systemLink($setDetailPage) },"Edit set ".CGI::strong($setID)." data for ALL students assigned to this set."),
1308 : gage 3790
1309 :     ])
1310 :     )
1311 :     );
1312 : toenail 2794 } else {
1313 : gage 3790 print CGI::table({border=>2,cellpadding=>10},
1314 : gage 4276 CGI::Tr({},
1315 : gage 3790 CGI::td([
1316 :     "This set ".CGI::strong($setID)." is assigned to ".$self->userCountMessage($setUserCount, $userCount).'.' ,
1317 :     'Edit '.CGI::a({href=>$editUsersAssignedToSetURL},'individual versions '). "of set $setID.",
1318 :    
1319 :     ])
1320 :     )
1321 :     );
1322 : toenail 2794 }
1323 :    
1324 : toenail 2901 # handle renumbering of problems if necessary
1325 :     print CGI::a({name=>"problems"});
1326 : toenail 2794
1327 : toenail 2901 my %newProblemNumbers = ();
1328 :     my $maxProblemNumber = -1;
1329 :     for my $jj (sort { $a <=> $b } $db->listGlobalProblems($setID)) {
1330 :     $newProblemNumbers{$jj} = $r->param('problem_num_' . $jj);
1331 :     $maxProblemNumber = $jj if $jj > $maxProblemNumber;
1332 :     }
1333 :    
1334 :     my $forceRenumber = $r->param('force_renumber') || 0;
1335 :     handle_problem_numbers(\%newProblemNumbers, $maxProblemNumber, $db, $setID, $forceRenumber) unless defined $r->param('undo_changes');
1336 :    
1337 : toenail 2794 my %properties = %{ FIELD_PROPERTIES() };
1338 :    
1339 :     my %display_modes = %{WeBWorK::PG::DISPLAY_MODES()};
1340 :     my @active_modes = grep { exists $display_modes{$_} } @{$r->ce->{pg}->{displayModes}};
1341 :     push @active_modes, 'None';
1342 :     my $default_header_mode = $r->param('header.displaymode') || 'None';
1343 :     my $default_problem_mode = $r->param('problem.displaymode') || 'None';
1344 :    
1345 : toenail 2816 #####################################################################
1346 :     # Browse available header/problem files
1347 :     #####################################################################
1348 :    
1349 :     my $templates = $r->ce->{courseDirs}->{templates};
1350 : sh002i 4642 my $skip = join("|", keys %{ $r->ce->{courseFiles}->{problibs} });
1351 : toenail 2794
1352 : toenail 2816 my @headerFileList = listFilesRecursive(
1353 :     $templates,
1354 :     qr/header.*\.pg$/i, # match these files
1355 :     qr/^(?:$skip|CVS)$/, # prune these directories
1356 :     0, # match against file name only
1357 :     1, # prune against path relative to $templates
1358 :     );
1359 :    
1360 :     # this just takes too much time to search
1361 :     # my @problemFileList = listFilesRecursive(
1362 :     # $templates,
1363 :     # qr/\.pg$/i, # problem files don't say problem
1364 :     # qr/^(?:$skip|CVS)$/, # prune these directories
1365 :     # 0, # match against file name only
1366 :     # 1, # prune against path relative to $templates
1367 :     # );
1368 :    
1369 : toenail 2794 # Display a useful warning message
1370 :     if ($forUsers) {
1371 :     print CGI::p(CGI::b("Any changes made below will be reflected in the set for ONLY the student" .
1372 :     ($forOneUser ? "" : "s") . " listed above."));
1373 :     } else {
1374 :     print CGI::p(CGI::b("Any changes made below will be reflected in the set for ALL students."));
1375 :     }
1376 :    
1377 :     print CGI::start_form({method=>"POST", action=>$setDetailURL});
1378 : toenail 2834 print $self->hiddenEditForUserFields(@editForUser);
1379 :     print $self->hidden_authen_fields;
1380 : toenail 2794 print CGI::input({type=>"submit", name=>"submit_changes", value=>"Save Changes"});
1381 : toenail 2834 print CGI::input({type=>"submit", name=>"undo_changes", value => "Reset Form"});
1382 :    
1383 : toenail 2794 # spacing
1384 :     print CGI::p();
1385 :    
1386 :     #####################################################################
1387 :     # Display general set information
1388 :     #####################################################################
1389 :    
1390 :     print CGI::start_table({border=>1, cellpadding=>4});
1391 :     print CGI::Tr({}, CGI::th({}, [
1392 :     "General Information",
1393 :     ]));
1394 : sh002i 2913
1395 :     # this is kind of a hack -- we need to get a user record here, so we can
1396 :     # pass it to FieldTable, so FieldTable can pass it to FieldHTML, so
1397 :     # FieldHTML doesn't have to fetch it itself.
1398 :     my $userSetRecord = $db->getUserSet($userToShow, $setID);
1399 :    
1400 : toenail 2794 print CGI::Tr({}, CGI::td({}, [
1401 : sh002i 2913 $self->FieldTable($userToShow, $setID, undef, $setRecord, $userSetRecord),
1402 : toenail 2794 ]));
1403 :     print CGI::end_table();
1404 :    
1405 :     # spacing
1406 :     print CGI::p();
1407 :    
1408 :    
1409 :     #####################################################################
1410 :     # Display header information
1411 :     #####################################################################
1412 :     my @headers = @{ HEADER_ORDER() };
1413 : toenail 2834 my %headerModules = (set_header => 'problem_list', hardcopy_header => 'hardcopy_preselect_set');
1414 :     my %headerDefaults = (set_header => $ce->{webworkFiles}->{screenSnippets}->{setHeader}, hardcopy_header => $ce->{webworkFiles}->{hardcopySnippets}->{setHeader});
1415 : toenail 2794 my @headerFiles = map { $setRecord->{$_} } @headers;
1416 :     if (scalar @headers and not $forUsers) {
1417 :    
1418 :     print CGI::start_table({border=>1, cellpadding=>4});
1419 :     print CGI::Tr({}, CGI::th({}, [
1420 :     "Headers",
1421 :     # "Data",
1422 :     "Display&nbsp;Mode:&nbsp;" .
1423 :     CGI::popup_menu(-name => "header.displaymode", -values => \@active_modes, -default => $default_header_mode) . '&nbsp;'.
1424 : gage 2904 CGI::input({type => "submit", name => "refresh", value => "Refresh Display"}),
1425 : toenail 2794 ]));
1426 :    
1427 :     my %header_html;
1428 :    
1429 : toenail 2834 my %error;
1430 : toenail 2794 foreach my $header (@headers) {
1431 : toenail 2834 my $headerFile = $r->param("set.$setID.$header") || $setRecord->{$header} || $headerDefaults{$header};
1432 :    
1433 :     $error{$header} = $self->checkFile($headerFile);
1434 : glarose 4763 my $this_set = $db->getMergedSet($userToShow, $setID);
1435 : toenail 2834 unless ($error{$header}) {
1436 : sh002i 4846 my @temp = renderProblems(
1437 :     r=> $r,
1438 :     user => $db->getUser($userToShow),
1439 :     displayMode=> $default_header_mode,
1440 :     problem_number=> 0,
1441 :     this_set => $this_set,
1442 :     problem_list => [$headerFile],
1443 : toenail 2834 );
1444 :     $header_html{$header} = $temp[0];
1445 :     }
1446 : toenail 2794 }
1447 :    
1448 :     foreach my $header (@headers) {
1449 :    
1450 : toenail 2816 my $editHeaderPage = $urlpath->new(type => 'instructor_problem_editor_withset_withproblem', args => { courseID => $courseID, setID => $setID, problemID => 0 });
1451 : toenail 2794 my $editHeaderLink = $self->systemLink($editHeaderPage, params => { file_type => $header, make_local_copy => 1 });
1452 :    
1453 : toenail 2816 my $viewHeaderPage = $urlpath->new(type => $headerModules{$header}, args => { courseID => $courseID, setID => $setID });
1454 : toenail 2794 my $viewHeaderLink = $self->systemLink($viewHeaderPage);
1455 :    
1456 :     print CGI::Tr({}, CGI::td({}, [
1457 :     CGI::start_table({border => 0, cellpadding => 0}) .
1458 :     CGI::Tr({}, CGI::td({}, $properties{$header}->{name})) .
1459 : dpvc 3901 CGI::Tr({}, CGI::td({}, CGI::a({href => $editHeaderLink, target=>"WW_Editor"}, "Edit it"))) .
1460 :     CGI::Tr({}, CGI::td({}, CGI::a({href => $viewHeaderLink, target=>"WW_View"}, "View it"))) .
1461 : toenail 2834 # CGI::Tr({}, CGI::td({}, CGI::checkbox({name => "defaultHeader", value => $header, label => "Use Default"}))) .
1462 : toenail 2794 CGI::end_table(),
1463 :     # "",
1464 : toenail 2816 # CGI::input({ name => "set.$setID.$header", value => $setRecord->{$header}, size => 50}) .
1465 :     # join ("\n", $self->FieldHTML($userToShow, $setID, $problemID, "source_file")) .
1466 :     # CGI::br() . CGI::div({class=> "RenderSolo"}, $problem_html[0]->{body_text}),
1467 :    
1468 :     comboBox({
1469 :     name => "set.$setID.$header",
1470 :     request => $r,
1471 : toenail 2834 default => $r->param("set.$setID.$header") || $setRecord->{$header},
1472 : toenail 2816 multiple => 0,
1473 :     values => ["", @headerFileList],
1474 :     labels => { "" => "Use Default Header File" },
1475 :     }) .
1476 : toenail 2834 ($error{$header} ?
1477 :     CGI::div({class=>"ResultsWithError", style=>"font-weight: bold"}, $error{$header})
1478 :     : CGI::div({class=> "RenderSolo"}, $header_html{$header}->{body_text})
1479 :     ),
1480 : toenail 2794 ]));
1481 :     }
1482 :    
1483 :     print CGI::end_table();
1484 :     } else {
1485 :     print CGI::p(CGI::b("Screen and Hardcopy set header information can not be overridden for individual students."));
1486 :     }
1487 :    
1488 :     # spacing
1489 :     print CGI::p();
1490 :    
1491 :    
1492 :     #####################################################################
1493 :     # Display problem information
1494 :     #####################################################################
1495 :    
1496 : toenail 2901 my @problemIDList = sort { $a <=> $b } $db->listGlobalProblems($setID);
1497 : sh002i 2913
1498 : sh002i 4518 # DBFIXME use iterators instead of getting all at once
1499 :    
1500 : sh002i 2913 # get global problem records for all problems in one go
1501 :     my %GlobalProblems;
1502 :     my @globalKeypartsRef = map { [$setID, $_] } @problemIDList;
1503 : sh002i 4518 # DBFIXME shouldn't need to get key list here
1504 : sh002i 2913 @GlobalProblems{@problemIDList} = $db->getGlobalProblems(@globalKeypartsRef);
1505 :    
1506 :     # if needed, get user problem records for all problems in one go
1507 :     my (%UserProblems, %MergedProblems);
1508 :     if ($forOneUser) {
1509 :     my @userKeypartsRef = map { [$editForUser[0], $setID, $_] } @problemIDList;
1510 : sh002i 4518 # DBFIXME shouldn't need to get key list here
1511 : sh002i 2913 @UserProblems{@problemIDList} = $db->getUserProblems(@userKeypartsRef);
1512 :     @MergedProblems{@problemIDList} = $db->getMergedProblems(@userKeypartsRef);
1513 :     }
1514 :    
1515 : toenail 2816 if (scalar @problemIDList) {
1516 : toenail 2794
1517 :     print CGI::start_table({border=>1, cellpadding=>4});
1518 :     print CGI::Tr({}, CGI::th({}, [
1519 :     "Problems",
1520 :     "Data",
1521 :     "Display&nbsp;Mode:&nbsp;" .
1522 :     CGI::popup_menu(-name => "problem.displaymode", -values => \@active_modes, -default => $default_problem_mode) . '&nbsp;'.
1523 : gage 2904 CGI::input({type => "submit", name => "refresh", value => "Refresh Display"}),
1524 : toenail 2794 ]));
1525 :    
1526 : toenail 2834 my %shownYet;
1527 :     my $repeatFile;
1528 : toenail 2816 foreach my $problemID (@problemIDList) {
1529 : toenail 2794
1530 :     my $problemRecord;
1531 :     if ($forOneUser) {
1532 : sh002i 2913 #$problemRecord = $db->getMergedProblem($editForUser[0], $setID, $problemID);
1533 :     $problemRecord = $MergedProblems{$problemID}; # already fetched above --sam
1534 : toenail 2794 } else {
1535 : sh002i 2913 #$problemRecord = $db->getGlobalProblem($setID, $problemID);
1536 :     $problemRecord = $GlobalProblems{$problemID}; # already fetched above --sam
1537 : toenail 2794 }
1538 :    
1539 : sh002i 2913 #$self->addgoodmessage("");
1540 :     #$self->addbadmessage($problemRecord->toString());
1541 :    
1542 :    
1543 : toenail 2816 my $editProblemPage = $urlpath->new(type => 'instructor_problem_editor_withset_withproblem', args => { courseID => $courseID, setID => $setID, problemID => $problemID });
1544 :     my $editProblemLink = $self->systemLink($editProblemPage, params => { make_local_copy => 0 });
1545 : gage 3036
1546 :    
1547 : toenail 2794 # FIXME: should we have an "act as" type link here when editing for multiple users?
1548 : toenail 2841 my $viewProblemPage = $urlpath->new(type => 'problem_detail', args => { courseID => $courseID, setID => $setID, problemID => $problemID });
1549 : toenail 2794 my $viewProblemLink = $self->systemLink($viewProblemPage, params => { effectiveUser => ($forOneUser ? $editForUser[0] : $userID)});
1550 :    
1551 :     my @fields = @{ PROBLEM_FIELDS() };
1552 :     push @fields, @{ USER_PROBLEM_FIELDS() } if $forOneUser;
1553 :    
1554 : toenail 2834 my $problemFile = $r->param("problem.$problemID.source_file") || $problemRecord->source_file;
1555 :    
1556 :     # warn of repeat problems
1557 :     if (defined $shownYet{$problemFile}) {
1558 :     $repeatFile = "This problem uses the same source file as number " . $shownYet{$problemFile} . ".";
1559 :     } else {
1560 :     $shownYet{$problemFile} = $problemID;
1561 : toenail 2966 $repeatFile = "";
1562 : toenail 2834 }
1563 :    
1564 :     my $error = $self->checkFile($problemFile);
1565 : glarose 4763 my $this_set = $db->getMergedSet($userToShow, $setID);
1566 : toenail 2834 my @problem_html;
1567 :     unless ($error) {
1568 : sh002i 4846 @problem_html = renderProblems(
1569 :     r=> $r,
1570 :     user => $db->getUser($userToShow),
1571 :     displayMode=> $default_problem_mode,
1572 :     problem_number=> $problemID,
1573 :     this_set => $this_set,
1574 :     problem_seed => $forOneUser ? $problemRecord->problem_seed : 0,
1575 :     problem_list => [$problemRecord->source_file],
1576 : toenail 2834 );
1577 :     }
1578 : toenail 2794
1579 :     print CGI::Tr({}, CGI::td({}, [
1580 :     CGI::start_table({border => 0, cellpadding => 1}) .
1581 : toenail 2816 CGI::Tr({}, CGI::td({}, problem_number_popup($problemID, $maxProblemNumber))) .
1582 : dpvc 3901 CGI::Tr({}, CGI::td({}, CGI::a({href => $editProblemLink, target=>"WW_Editor"}, "Edit it"))) .
1583 :     CGI::Tr({}, CGI::td({}, CGI::a({href => $viewProblemLink, target=>"WW_View"}, "Try it" . ($forOneUser ? " (as $editForUser[0])" : "")))) .
1584 : toenail 2816 ($forUsers ? "" : CGI::Tr({}, CGI::td({}, CGI::checkbox({name => "deleteProblem", value => $problemID, label => "Delete it?"})))) .
1585 :     # CGI::Tr({}, CGI::td({}, "Delete&nbsp;it?" . CGI::input({type => "checkbox", name => "deleteProblem", value => $problemID}))) .
1586 : toenail 2901 ($forOneUser ? "" : CGI::Tr({}, CGI::td({}, CGI::checkbox({name => "markCorrect", value => $problemID, label => "Mark Correct?"})))) .
1587 : toenail 2794 CGI::end_table(),
1588 : sh002i 2913 $self->FieldTable($userToShow, $setID, $problemID, $GlobalProblems{$problemID}, $UserProblems{$problemID}),
1589 : toenail 2816 # A comprehensive list of problems is just TOO big to be handled well
1590 :     # comboBox({
1591 :     # name => "set.$setID.$problemID",
1592 :     # request => $r,
1593 :     # default => $problemRecord->{problem_id},
1594 :     # multiple => 0,
1595 :     # values => \@problemFileList,
1596 :     # }) .
1597 :    
1598 : sh002i 2913 join ("\n", $self->FieldHTML(
1599 :     $userToShow,
1600 :     $setID,
1601 :     $problemID,
1602 :     $GlobalProblems{$problemID}, # pass previously fetched global record to FieldHTML --sam
1603 :     $UserProblems{$problemID}, # pass previously fetched user record to FieldHTML --sam
1604 :     "source_file"
1605 :     )) .
1606 : toenail 2834 CGI::br() .
1607 :     ($error ?
1608 :     CGI::div({class=>"ResultsWithError", style=>"font-weight: bold"}, $error)
1609 :     : CGI::div({class=> "RenderSolo"}, $problem_html[0]->{body_text})
1610 :     ) .
1611 :     ($repeatFile ? CGI::div({class=>"ResultsWithError", style=>"font-weight: bold"}, $repeatFile) : ''),
1612 : toenail 2794 ]));
1613 :     }
1614 :    
1615 : gage 3036
1616 :     # print final lines
1617 : toenail 2794 print CGI::end_table();
1618 :     print CGI::checkbox({
1619 : sh002i 3803 label=> "Force problems to be numbered consecutively from one (always done when reordering problems)",
1620 : gage 4285 name=>"force_renumber", value=>"1"});
1621 : gage 3790 print CGI::p(<<EOF);
1622 : toenail 2794 Any time problem numbers are intentionally changed, the problems will
1623 :     always be renumbered consecutively, starting from one. When deleting
1624 :     problems, gaps will be left in the numbering unless the box above is
1625 :     checked.
1626 : gage 3790 EOF
1627 :     print CGI::p("It is before the open date. You probably want to renumber the problems if you are deleting some from the middle.") if ($setRecord->open_date>time());
1628 : gage 4258 print CGI::p("When changing problem numbers, we will move the problem to be ". CGI::em("before"). " the chosen number.");
1629 : toenail 2794
1630 :     } else {
1631 :     print CGI::p(CGI::b("This set doesn't contain any problems yet."));
1632 :     }
1633 : gage 3175 # always allow one to add a new problem.
1634 : gage 4285 print CGI::checkbox({
1635 :     label=> "Add blank problem template to end of homework set",
1636 :     name=>"add_blank_problem", value=>"1"}
1637 :     ),CGI::br(),CGI::br(),
1638 :     CGI::input({type=>"submit", name=>"submit_changes", value=>"Save Changes"}),
1639 :     CGI::input({type=>"submit", name=>"handle_numbers", value=>"Reorder problems only"}),
1640 :     "(Any unsaved changes will be lost.)"
1641 :     ;
1642 : toenail 2794
1643 : gage 4285
1644 :    
1645 :     #my $editNewProblemPage = $urlpath->new(type => 'instructor_problem_editor_withset_withproblem', args => { courseID => $courseID, setID => $setID, problemID =>'new_problem' });
1646 :     #my $editNewProblemLink = $self->systemLink($editNewProblemPage, params => { make_local_copy => 1, file_type => 'blank_problem' });
1647 : gage 3811 # This next feature isn't fully supported and is causing problems. Remove for now. #FIXME
1648 :     #print CGI::p( CGI::a({href=>$editNewProblemLink},'Edit'). ' a new blank problem');
1649 : gage 3175
1650 : toenail 2794 print CGI::end_form();
1651 :    
1652 :     return "";
1653 :     }
1654 :    
1655 :     1;
1656 :    
1657 :     =head1 AUTHOR
1658 :    
1659 :     Written by Robert Van Dam, toenail (at) cif.rochester.edu
1660 :    
1661 :     =cut

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9