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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : jj 1994 ################################################################################
2 :     # WeBWorK Online Homework Delivery System
3 :     # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/
4 : toenail 2805 # $CVSHeader: webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm,v 1.26 2004/09/04 23:31:46 dpvc Exp $
5 : jj 1994 #
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 :    
18 :     package WeBWorK::ContentGenerator::Instructor::SetMaker;
19 :     use base qw(WeBWorK::ContentGenerator::Instructor);
20 :    
21 :     =head1 NAME
22 :    
23 :     WeBWorK::ContentGenerator::Instructor::SetMaker - Make problem sets.
24 :    
25 :     =cut
26 :    
27 :     use strict;
28 :     use warnings;
29 :    
30 :     use CGI::Pretty qw();
31 :     use WeBWorK::Form;
32 : jj 2700 use WeBWorK::Utils qw(readDirectory max sortByName);
33 : jj 1994 use WeBWorK::Utils::Tasks qw(renderProblems);
34 :    
35 :     require WeBWorK::Utils::ListingDB;
36 :    
37 : jj 2225 use constant MAX_SHOW_DEFAULT => 20;
38 : jj 2152 use constant NO_LOCAL_SET_STRING => 'There are no local sets yet';
39 :     use constant SELECT_SET_STRING => 'Select a Set for This Course';
40 : dpvc 2431 use constant SELECT_LOCAL_STRING => 'Select a Problem Collection';
41 :     use constant MY_PROBLEMS => ' My Problems ';
42 :     use constant MAIN_PROBLEMS => ' Main Problems ';
43 : jj 1994
44 : jj 2116 ## Flags for operations on files
45 :    
46 :     use constant ADDED => 1;
47 :     use constant HIDDEN => (1 << 1);
48 :     use constant SUCCESS => (1 << 2);
49 :    
50 : dpvc 2431 ## for additional problib buttons
51 :     my %problib; ## filled in in global.conf
52 : dpvc 2734 my %ignoredir = (
53 :     '.' => 1, '..' => 1, 'Library' => 1,
54 :     'headers' => 1, 'macros' => 1, 'email' => 1,
55 :     );
56 : dpvc 2431
57 : dpvc 2734 ##
58 : jj 2039 ## This is for searching the disk for directories containing pg files.
59 : jj 1994 ## to make the recursion work, this returns an array where the first
60 : dpvc 2734 ## item is the number of pg files in the directory. The second is a
61 :     ## list of directories which contain pg files.
62 :     ##
63 :     ## If a directory contains only one pg file and at least one other
64 :     ## file, the directory is considered to be part of the parent
65 :     ## directory (it is probably in a separate directory only because
66 :     ## it has auxiliarly files that want to be kept together with the
67 :     ## pg file).
68 :     ##
69 :     ## If a directory has a file named "=library-ignore", it is never
70 :     ## included in the directory menu. If a directory contains a file
71 :     ## called "=library-combine-up", then its pg are included with those
72 :     ## in the parent directory (and the directory does not appear in the
73 :     ## menu). If it has a file called "=library-no-combine" then it is
74 :     ## always listed as a separate directory even if it contains only one
75 :     ## pg file.
76 :     ##
77 :    
78 : jj 1994 sub get_library_sets {
79 : dpvc 2734 my $top = shift; my $dir = shift;
80 :     my @lis = readDirectory($dir); my @pgdirs;
81 :     return (0) if grep /^=library-ignore$/, @lis;
82 :    
83 :     my $pgcount = scalar(grep { m/\.pg$/ and (not m/(Header|-text)\.pg$/) and -f "$dir/$_"} @lis);
84 :     my $others = scalar(grep { (!m/\.pg$/ || m/(Header|-text)\.pg$/) &&
85 :     !m/(\.(tmp|bak)|~)$/ && -f "$dir/$_" } @lis);
86 :    
87 :     my @dirs = grep {!$ignoredir{$_} and -d "$dir/$_"} @lis;
88 :     if ($top == 1) {@dirs = grep {!$problib{$_}} @dirs}
89 :     foreach my $subdir (@dirs) {
90 :     my @results = get_library_sets(0, "$dir/$subdir");
91 :     $pgcount += shift @results; push(@pgdirs,@results);
92 : jj 1994 }
93 : dpvc 2734
94 :     return ($pgcount, @pgdirs) if $top || $pgcount == 0 || grep /^=library-combine-up$/, @lis;
95 :     return (0,@pgdirs,$dir) if $pgcount > 1 || $others == 0 || grep /^=library-no-combine$/, @lis;
96 :     return ($pgcount, @pgdirs);
97 : jj 1994 }
98 :    
99 : dpvc 2734 sub get_library_pgs {
100 :     my $top = shift; my $base = shift; my $dir = shift;
101 :     my @lis = readDirectory("$base/$dir");
102 :     return () if grep /^=library-ignore$/, @lis;
103 :     return () if !$top && grep /^=library-no-combine$/, @lis;
104 : jj 1994
105 : dpvc 2734 my @pgs = grep { m/\.pg$/ and (not m/(Header|-text)\.pg$/) and -f "$base/$dir/$_"} @lis;
106 :     my $others = scalar(grep { (!m/\.pg$/ || m/(Header|-text)\.pg$/) &&
107 :     !m/(\.(tmp|bak)|~)$/ && -f "$base/$dir/$_" } @lis);
108 :    
109 :     my @dirs = grep {!$ignoredir{$_} and -d "$base/$dir/$_"} @lis;
110 :     if ($top == 1) {@dirs = grep {!$problib{$_}} @dirs}
111 :     foreach my $subdir (@dirs) {push(@pgs, get_library_pgs(0,"$base/$dir",$subdir))}
112 :    
113 :     return () unless $top || (scalar(@pgs) == 1 && $others) || grep /^=library-combine-up$/, @lis;
114 :     return (map {"$dir/$_"} @pgs);
115 : jj 1994 }
116 :    
117 : dpvc 2734 sub list_pg_files {
118 :     my ($templates,$dir) = @_;
119 :     my $top = ($dir eq '.')? 1 : 2;
120 :     my @pgs = get_library_pgs($top,$templates,$dir);
121 :     return sortByName(undef,@pgs);
122 :     }
123 :    
124 : jj 1994 ## go through past page getting a list of identifiers for the problems
125 :     ## and whether or not they are selected, and whether or not they should
126 :     ## be hidden
127 :    
128 :     sub get_past_problem_files {
129 :     my $r = shift;
130 :     my @found=();
131 :     my $count =1;
132 :     while (defined($r->param("filetrial$count"))) {
133 : jj 2116 my $val = 0;
134 :     $val |= ADDED if($r->param("trial$count"));
135 :     $val |= HIDDEN if($r->param("hideme$count"));
136 :     push @found, [$r->param("filetrial$count"), $val];
137 : jj 1994 $count++;
138 :     }
139 : jj 2116 return(\@found);
140 : jj 1994 }
141 :    
142 :     #### For adding new problems
143 :    
144 :     sub add_selected {
145 :     my $self = shift;
146 :     my $db = shift;
147 :     my $setName = shift;
148 : jj 2116 my @past_problems = @{$self->{past_problems}};
149 :     my @selected = @past_problems;
150 : jj 1994 my (@path, $file, $selected, $freeProblemID);
151 :     $freeProblemID = max($db->listGlobalProblems($setName)) + 1;
152 : jj 2116 my $addedcount=0;
153 : jj 1994
154 :     for $selected (@selected) {
155 : jj 2116 if($selected->[1] & ADDED) {
156 :     $file = $selected->[0];
157 : jj 2283 my $problemRecord = $self->addProblemToSet(setName => $setName,
158 :     sourceFile => $file, problemID => $freeProblemID);
159 :     $freeProblemID++;
160 : jj 2116 $self->assignProblemToAllSetUsers($problemRecord);
161 : jj 2230 $selected->[1] |= SUCCESS;
162 : jj 2116 $addedcount++;
163 :     }
164 : jj 1994 }
165 : jj 2116 return($addedcount);
166 : jj 1994 }
167 :    
168 :    
169 : jj 2039 ############# List of sets of problems in templates directory
170 : jj 1994
171 : jj 2115 sub get_problem_directories {
172 : jj 1994 my $ce = shift;
173 : dpvc 2431 my $lib = shift;
174 :     my $source = $ce->{courseDirs}{templates};
175 :     my $main = MY_PROBLEMS; my $isTop = 1;
176 : dpvc 2734 if ($lib) {$source .= "/$lib"; $main = MAIN_PROBLEMS; $isTop = 2}
177 : dpvc 2431 my @all_problem_directories = get_library_sets($isTop, $source);
178 : jj 2115 my $includetop = shift @all_problem_directories;
179 : jj 1994 my $j;
180 : jj 2115 for ($j=0; $j<scalar(@all_problem_directories); $j++) {
181 :     $all_problem_directories[$j] =~ s|^$ce->{courseDirs}->{templates}/?||;
182 : jj 1994 }
183 : dpvc 2734 @all_problem_directories = sortByName(undef, @all_problem_directories);
184 : dpvc 2431 unshift @all_problem_directories, $main if($includetop);
185 : jj 2115 return (\@all_problem_directories);
186 : jj 1994 }
187 :    
188 : jj 2225 ############# Everyone has a view problems line. Abstract it
189 :     sub view_problems_line {
190 :     my $internal_name = shift;
191 :     my $label = shift;
192 :     my $r = shift; # so we can get parameter values
193 :     my $result = CGI::submit(-name=>"$internal_name", -value=>$label);
194 :    
195 :     my %display_modes = %{WeBWorK::PG::DISPLAY_MODES()};
196 :     my @active_modes = grep { exists $display_modes{$_} }
197 :     @{$r->ce->{pg}->{displayModes}};
198 :     push @active_modes, 'None';
199 :     # We have our own displayMode since its value may be None, which is illegal
200 :     # in other modules.
201 :     my $mydisplayMode = $r->param('mydisplayMode') || $r->ce->{pg}->{options}->{displayMode};
202 :     $result .= '&nbsp;Display&nbsp;Mode:&nbsp;'.CGI::popup_menu(-name=> 'mydisplayMode',
203 :     -values=>\@active_modes,
204 :     -default=> $mydisplayMode);
205 :     # Now we give a choice of the number of problems to show
206 :     my $defaultMax = $r->param('max_shown') || MAX_SHOW_DEFAULT;
207 :     $result .= '&nbsp;Max. Shown:&nbsp'.
208 :     CGI::popup_menu(-name=> 'max_shown',
209 :     -values=>[5,10,15,20,25,30,50,'All'],
210 :     -default=> $defaultMax);
211 :    
212 :     return($result);
213 :     }
214 :    
215 :    
216 : jj 1994 ### The browsing panel has three versions
217 :     ##### Version 1 is local problems
218 :     sub browse_local_panel {
219 :     my $self = shift;
220 :     my $library_selected = shift;
221 : dpvc 2431 my $lib = shift || ''; $lib =~ s/^browse_//;
222 :     my $name = ($lib eq '')? 'Local' : $problib{$lib};
223 : jj 1994
224 : dpvc 2431 my $list_of_prob_dirs= get_problem_directories($self->r->ce,$lib);
225 : jj 2115 if(scalar(@$list_of_prob_dirs) == 0) {
226 :     $library_selected = "Found no directories containing problems";
227 :     unshift @{$list_of_prob_dirs}, $library_selected;
228 :     } else {
229 : jj 2152 my $default_value = SELECT_LOCAL_STRING;
230 : jj 2115 if (not $library_selected or $library_selected eq $default_value) {
231 :     unshift @{$list_of_prob_dirs}, $default_value;
232 :     $library_selected = $default_value;
233 :     }
234 : jj 1994 }
235 : jj 2225 my $view_problem_line = view_problems_line('view_local_set', 'View Problems', $self->r);
236 : dpvc 2431 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "$name Problems: ",
237 : jj 1994 CGI::popup_menu(-name=> 'library_sets',
238 : jj 2115 -values=>$list_of_prob_dirs,
239 : jj 1994 -default=> $library_selected),
240 :     CGI::br(),
241 : jj 2225 $view_problem_line,
242 : jj 1994 ));
243 :     }
244 :    
245 :     ##### Version 2 is local problem sets
246 :     sub browse_mysets_panel {
247 :     my $self = shift;
248 :     my $library_selected = shift;
249 :     my $list_of_local_sets = shift;
250 :     my $default_value = "Select a Problem Set";
251 :    
252 : jj 2024 if(scalar(@$list_of_local_sets) == 0) {
253 : jj 2152 $list_of_local_sets = [NO_LOCAL_SET_STRING];
254 : jj 2024 } elsif (not $library_selected or $library_selected eq $default_value) {
255 : jj 1994 unshift @{$list_of_local_sets}, $default_value;
256 :     $library_selected = $default_value;
257 :     }
258 :    
259 : jj 2225 my $view_problem_line = view_problems_line('view_mysets_set', 'View Problems', $self->r);
260 : jj 2230 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "Browse from: ",
261 : jj 1994 CGI::popup_menu(-name=> 'library_sets',
262 :     -values=>$list_of_local_sets,
263 :     -default=> $library_selected),
264 :     CGI::br(),
265 : jj 2225 $view_problem_line
266 : jj 1994 ));
267 :     }
268 :    
269 :     ##### Version 3 is the problem library
270 :    
271 :    
272 :     # There a different levels, and you can pick a new chapter,
273 :     # pick a new section, pick all from chapter, pick all from section
274 :     #
275 :     # Incoming data - current chapter, current section
276 :     sub browse_library_panel {
277 :     my $self = shift;
278 :     my $r = $self->r;
279 : jj 2039 my $ce = $r->ce;
280 : jj 1994
281 : jj 2140 my $libraryRoot = $r->{ce}->{problemLibrary}->{root};
282 : jj 1994
283 :     unless($libraryRoot) {
284 :     print CGI::Tr(CGI::td(CGI::div({class=>'ResultsWithError', align=>"center"},
285 :     "The problem library has not been installed.")));
286 :     return;
287 :     }
288 : jj 2039 # Test if the Library directory exists. If not, try to make it
289 :     unless(-d "$ce->{courseDirs}->{templates}/Library") {
290 :     unless(symlink($libraryRoot, "$ce->{courseDirs}->{templates}/Library")) {
291 : jj 2115 my $msg = <<"HERE";
292 : jj 2039 You are missing the directory <code>templates/Library</code>, which is needed
293 :     for the Problem Library to function. It should be a link pointing to
294 :     <code>$libraryRoot</code>, which you set in <code>conf/global.conf</code>.
295 :     I tried to make the link for you, but that failed. Check the permissions
296 :     in your <code>templates</code> directory.
297 :     HERE
298 : jj 2152 $self->addbadmessage($msg);
299 : jj 2039 }
300 :     }
301 : jj 1994
302 :     my $default_chap = "All Chapters";
303 :     my $default_sect = "All Sections";
304 :    
305 :     my @chaps = WeBWorK::Utils::ListingDB::getAllChapters($r->{ce});
306 :     unshift @chaps, $default_chap;
307 :     my $chapter_selected = $r->param('library_chapters') || $default_chap;
308 :    
309 :     my @sects=();
310 :     if ($chapter_selected ne $default_chap) {
311 :     @sects = WeBWorK::Utils::ListingDB::getAllSections($r->{ce}, $chapter_selected);
312 :     }
313 :    
314 :     my @textbooks = ('Textbook info not ready');
315 :    
316 :     unshift @sects, $default_sect;
317 :     my $section_selected = $r->param('library_sections') || $default_sect;
318 : jj 2225 my $view_problem_line = view_problems_line('lib_view', 'View Problems', $self->r);
319 : jj 1994
320 : jj 2230 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"},
321 : jj 1994 CGI::start_table(),
322 :     CGI::Tr(
323 :     CGI::td(["Chapter:",
324 :     CGI::popup_menu(-name=> 'library_chapters',
325 :     -values=>\@chaps,
326 :     -default=> $chapter_selected,
327 :     -onchange=>"submit();return true"
328 :     ),
329 :     CGI::submit(-name=>"lib_select_chapter", -value=>"Update Section List")])),
330 :     CGI::Tr(
331 :     CGI::td("Section:"),
332 :     CGI::td({-colspan=>2},
333 :     CGI::popup_menu(-name=> 'library_sections',
334 :     -values=>\@sects,
335 :     -default=> $section_selected
336 :     ))),
337 :    
338 : jj 2015 # CGI::Tr(
339 :     # CGI::td("Textbook:"),
340 :     # CGI::td({-colspan=>2},
341 :     # CGI::popup_menu(-name=> 'library_textbooks',
342 :     # -values=>\@textbooks,
343 :     # # -default=> $section_selected
344 :     # ))),
345 : jj 1994
346 : jj 2015 # CGI::Tr(
347 :     # CGI::td("Keywords:"),
348 :     # CGI::td({-colspan=>2}, CGI::textfield(-name=>"keywords",
349 :     # -default=>"Keywords not implemented yet",
350 :     # -override=>1, -size=>60))),
351 : jj 2225 CGI::Tr(CGI::td({-colspan=>3},
352 :     $view_problem_line)),
353 : jj 1994 CGI::end_table(),
354 :     ));
355 :     }
356 :    
357 :     sub make_top_row {
358 :     my $self = shift;
359 :     my $r = $self->r;
360 : dpvc 2431 my $ce = $r->ce;
361 : jj 1994 my %data = @_;
362 :    
363 :     my $list_of_local_sets = $data{all_set_defs};
364 : jj 2024 my $have_local_sets = scalar(@$list_of_local_sets);
365 : jj 1994 my $browse_which = $data{browse_which};
366 :     my $library_selected = $r->param('library_sets');
367 :     my $set_selected = $r->param('local_sets');
368 :    
369 :     my ($dis1, $dis2, $dis3) = ("","","");
370 :     $dis1 = '-disabled' if($browse_which eq 'browse_library');
371 :     $dis2 = '-disabled' if($browse_which eq 'browse_local');
372 :     $dis3 = '-disabled' if($browse_which eq 'browse_mysets');
373 :    
374 : dpvc 2431 ## Make buttons for additional problem libraries
375 :     my $libs = '';
376 :     foreach my $lib (sort(keys(%problib))) {
377 :     $libs .= ' '. CGI::submit(-name=>"browse_$lib", -value=>$problib{$lib},
378 :     ($browse_which eq "browse_$lib")? '-disabled': '')
379 :     if (-d "$ce->{courseDirs}{templates}/$lib");
380 :     }
381 :     $libs = CGI::br()."or Problems from".$libs if $libs ne '';
382 :    
383 :     my $these_widths = "width: 20ex";
384 : jj 1994 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"center"},
385 : dpvc 2431 "Browse ",
386 :     CGI::submit(-name=>"browse_library", -value=>"Problem Library", -style=>$these_widths, $dis1),
387 :     CGI::submit(-name=>"browse_local", -value=>"Local Problems", -style=>$these_widths, $dis2),
388 :     CGI::submit(-name=>"browse_mysets", -value=>"From This Course", -style=>$these_widths, $dis3),
389 :     $libs,
390 : jj 1994 ));
391 :    
392 :     print CGI::Tr(CGI::td({-bgcolor=>"black"}));
393 :    
394 :     if ($browse_which eq 'browse_local') {
395 : jj 2024 $self->browse_local_panel($library_selected);
396 : jj 1994 } elsif ($browse_which eq 'browse_mysets') {
397 : jj 2024 $self->browse_mysets_panel($library_selected, $list_of_local_sets);
398 : dpvc 2431 } elsif ($browse_which eq 'browse_library') {
399 : jj 2024 $self->browse_library_panel();
400 : dpvc 2431 } else { ## handle other problem libraries
401 :     $self->browse_local_panel($library_selected,$browse_which);
402 : jj 1994 }
403 :    
404 :     print CGI::Tr(CGI::td({-bgcolor=>"black"}));
405 :    
406 : jj 2024 if($have_local_sets ==0) {
407 : jj 2152 $list_of_local_sets = [NO_LOCAL_SET_STRING];
408 :     } elsif (not $set_selected or $set_selected eq SELECT_SET_STRING) {
409 : jj 1994 if ($list_of_local_sets->[0] eq "Select a Problem Set") {
410 :     shift @{$list_of_local_sets};
411 :     }
412 : jj 2152 unshift @{$list_of_local_sets}, SELECT_SET_STRING;
413 :     $set_selected = SELECT_SET_STRING;
414 : jj 1994 }
415 : jj 2269 my $myjs = 'document.mainform.selfassign.value=confirm("Should I assign the new set to you now?\nUse OK for yes and Cancel for no.");true;';
416 : jj 1994
417 : jj 2230 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "Adding Problems to ",
418 : jj 2264 CGI::b("Target Set: "),
419 : jj 1994 CGI::popup_menu(-name=> 'local_sets',
420 :     -values=>$list_of_local_sets,
421 :     -default=> $set_selected),
422 : jj 2264 CGI::submit(-name=>"edit_local", -value=>"Edit Target Set"),
423 : jj 2269 CGI::hidden(-name=>"selfassign", -default=>[0]).
424 : jj 1994 CGI::br(),
425 :     CGI::br(),
426 : jj 2230 CGI::submit(-name=>"new_local_set", -value=>"Create a New Set in This Course:",
427 : jj 2269 -onclick=>$myjs
428 : jj 2230 ),
429 : jj 1994 " ",
430 :     CGI::textfield(-name=>"new_set_name",
431 :     -default=>"Name for new set here",
432 :     -override=>1, -size=>30),
433 :     CGI::br(),
434 :     ));
435 :    
436 :     print CGI::Tr(CGI::td({-bgcolor=>"black"}));
437 :    
438 :     print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"center"},
439 : jj 2230 CGI::start_table({-border=>"0"}),
440 :     CGI::Tr( CGI::td({ -align=>"center"},
441 :     CGI::submit(-name=>"select_all", -style=>$these_widths,
442 :     -value=>"Mark All For Adding"),
443 :     CGI::submit(-name=>"select_none", -style=>$these_widths,
444 :     -value=>"Clear All Marks"),
445 :     )),
446 :     CGI::Tr( CGI::td(
447 :     CGI::submit(-name=>"update", -style=>$these_widths. "; font-weight:bold",
448 :     -value=>"Update"),
449 :     CGI::submit(-name=>"rerandomize",
450 : jj 1994 -style=>$these_widths,
451 :     -value=>"Rerandomize"),
452 : jj 2230 CGI::submit(-name=>"cleardisplay",
453 : jj 1994 -style=>$these_widths,
454 : jj 2230 -value=>"Clear Problem Display")
455 :     )),
456 :     CGI::end_table()));
457 : jj 1994
458 :     }
459 :    
460 :     sub make_data_row {
461 :     my $self = shift;
462 :     my $sourceFileName = shift;
463 :     my $pg = shift;
464 :     my $cnt = shift;
465 : jj 2230 my $mark = shift || 0;
466 : jj 1994
467 : jj 2024 $sourceFileName =~ s|^./||; # clean up top ugliness
468 :    
469 : jj 1994 my $urlpath = $self->r->urlpath;
470 :     my $problem_output = $pg->{flags}->{error_flag} ?
471 : jj 2225 CGI::div({class=>"ResultsWithError"}, CGI::em("This problem produced an error"))
472 : jj 2015 : CGI::div({class=>"RenderSolo"}, $pg->{body_text});
473 : jj 1994
474 :    
475 : jj 2024 my $edit_link = '';
476 : jj 2040 #if($self->{r}->param('browse_which') ne 'browse_library') {
477 :     if($sourceFileName !~ /^Library\//) {
478 : jj 2039 $edit_link = CGI::a({href=>$self->systemLink($urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::PGProblemEditor",
479 :     courseID =>$urlpath->arg("courseID"),
480 :     setID=>"Undefined_Set",
481 :     problemID=>"1"),
482 :     params=>{sourceFilePath => "$sourceFileName"}
483 : jj 1994 )}, "Edit it" );
484 : jj 2024 }
485 : jj 1994
486 : jj 2039 my $try_link = CGI::a({href=>$self->systemLink($urlpath->newFromModule("WeBWorK::ContentGenerator::Problem",
487 :     courseID =>$urlpath->arg("courseID"),
488 :     setID=>"Undefined_Set",
489 :     problemID=>"1"),
490 : jj 2015 params =>{effectiveUser => $self->r->param('user'),
491 : jj 2021 editMode => "SetMaker",
492 : jj 2015 sourceFilePath => "$sourceFileName"} )}, "Try it");
493 : jj 1994
494 : jj 2572 my %add_box_data = ( -name=>"trial$cnt",-value=>1,-label=>"Add this problem to the current set on the next update");
495 : jj 2230 if($mark & SUCCESS) {
496 :     $add_box_data{ -label } .= " (just added this problem)";
497 :     } elsif($mark & ADDED) {
498 :     $add_box_data{ -checked } = 1;
499 :     }
500 : jj 1994
501 :     print CGI::Tr({-align=>"left"}, CGI::td(
502 :    
503 : jj 2024 CGI::div({-style=>"background-color: #DDDDDD; margin: 0px auto"},
504 :     CGI::span({-style=>"float:left ; text-align: left"},"File name: $sourceFileName "),
505 :     CGI::span({-style=>"float:right ; text-align: right"}, $edit_link, " ", $try_link)
506 :     ), CGI::br(),
507 : jj 1994
508 :    
509 :    
510 :    
511 : jj 2572 CGI::checkbox(-name=>"hideme$cnt",-value=>1,-label=>"Don't show this problem on the next update"),
512 : jj 1994 CGI::br(),
513 : jj 2230 CGI::checkbox((%add_box_data)),
514 : jj 1994 CGI::hidden(-name=>"filetrial$cnt", -default=>[$sourceFileName]).
515 :     CGI::p($problem_output),
516 :     ));
517 :     }
518 :    
519 :    
520 : jj 2115 sub pre_header_initialize {
521 : jj 1994 my ($self) = @_;
522 :     my $r = $self->r;
523 : jj 2115 ## For all cases, lets set some things
524 :     $self->{error}=0;
525 :     my $ce = $r->ce;
526 :     my $db = $r->db;
527 : jj 2225 my $maxShown = $r->param('max_shown') || MAX_SHOW_DEFAULT;
528 :     $maxShown = 10000000 if($maxShown eq 'All'); # let's hope there aren't more
529 : jj 1994
530 : dpvc 2431 ## These directories will have individual buttons
531 :     %problib = %{$ce->{courseFiles}{problibs}} if $ce->{courseFiles}{problibs};
532 : jj 2115
533 : jj 1994 my $userName = $r->param('user');
534 : jj 2115 my $user = $db->getUser($userName); # checked
535 :     die "record for user $userName (real user) does not exist."
536 : jj 1994 unless defined $user;
537 :     my $authz = $r->authz;
538 :     unless ($authz->hasPermissions($userName, "modify_problem_sets")) {
539 : jj 2115 return(""); # Error message already produced in the body
540 : jj 1994 }
541 :    
542 : jj 2115 ## Now one action we have to deal with here
543 :     if ($r->param('edit_local')) {
544 :     my $urlpath = $r->urlpath;
545 :     my $db = $r->db;
546 :     my $checkset = $db->getGlobalSet($r->param('local_sets'));
547 :     if (not defined($checkset)) {
548 :     $self->{error} = 1;
549 : jj 2264 $self->addbadmessage('You need to select a "Target Set" before you can edit it.');
550 : jj 2115 } else {
551 : toenail 2805 my $page = $urlpath->newFromModule('WeBWorK::ContentGenerator::Instructor::ProblemSetDetail', setID=>$r->param('local_sets'), courseID=>$urlpath->arg("courseID"));
552 : jj 2115 my $url = $self->systemLink($page);
553 :     $self->reply_with_redirect($url);
554 :     }
555 :     }
556 : jj 1994
557 : jj 2115 ## Next, lots of set up so that errors can be reported with message()
558 :    
559 : jj 1994 ############# List of problems we have already printed
560 :    
561 : jj 2116 $self->{past_problems} = get_past_problem_files($r);
562 : jj 2230 # if we don't end up reusing problems, this will be wiped out
563 :     # if we do redisplay the same problems, we must adjust this accordingly
564 :     my @past_marks = map {$_->[1]} @{$self->{past_problems}};
565 : jj 2116 my $none_shown = scalar(@{$self->{past_problems}})==0;
566 : jj 2115 my @pg_files=();
567 : jj 1994 my $use_previous_problems = 1;
568 :     my $first_shown = $r->param('first_shown') || 0;
569 :     my $last_shown = $r->param('last_shown');
570 :     if (not defined($last_shown)) {
571 :     $last_shown = -1;
572 :     }
573 :     my @all_past_list = (); # these are include requested, but not shown
574 : jj 2115 my $j = 0;
575 : jj 1994 while (defined($r->param("all_past_list$j"))) {
576 :     push @all_past_list, $r->param("all_past_list$j");
577 :     $j++;
578 :     }
579 :    
580 :     ############# Default of which problem selector to display
581 :    
582 : jj 2115 my $browse_which = $r->param('browse_which') || 'browse_local';
583 : jj 1994
584 :     my $problem_seed = $r->param('problem_seed') || 0;
585 :     $r->param('problem_seed', $problem_seed); # if it wasn't defined before
586 :    
587 : dpvc 2431 ## check for problem lib buttons
588 :     my $browse_lib = '';
589 :     foreach my $lib (keys %problib) {
590 :     if ($r->param("browse_$lib")) {
591 :     $browse_lib = "browse_$lib";
592 :     last;
593 :     }
594 :     }
595 :    
596 : jj 1994 ########### Start the logic through if elsif elsif ...
597 :    
598 :     ##### Asked to browse certain problems
599 : dpvc 2431 if ($browse_lib ne '') {
600 :     $browse_which = $browse_lib;
601 :     $r->param('library_sets', "");
602 :     $use_previous_problems = 0; @pg_files = (); ## clear old problems
603 :     } elsif ($r->param('browse_library')) {
604 : jj 1994 $browse_which = 'browse_library';
605 :     $r->param('library_sets', "");
606 : dpvc 2431 $use_previous_problems = 0; @pg_files = (); ## clear old problems
607 : jj 1994 } elsif ($r->param('browse_local')) {
608 :     $browse_which = 'browse_local';
609 :     $r->param('library_sets', "");
610 : dpvc 2431 $use_previous_problems = 0; @pg_files = (); ## clear old problems
611 : jj 1994 } elsif ($r->param('browse_mysets')) {
612 :     $browse_which = 'browse_mysets';
613 :     $r->param('library_sets', "");
614 : dpvc 2431 $use_previous_problems = 0; @pg_files = (); ## clear old problems
615 : jj 1994
616 :     ##### Change the seed value
617 :    
618 :     } elsif ($r->param('rerandomize')) {
619 :     $problem_seed++;
620 :     $r->param('problem_seed', $problem_seed);
621 : jj 2152 $self->addbadmessage('Changing the problem seed for display, but there are no problems showing.') if $none_shown;
622 : jj 1994
623 :     ##### Clear the display
624 :    
625 :     } elsif ($r->param('cleardisplay')) {
626 :     @pg_files = ();
627 :     $use_previous_problems=0;
628 : jj 2152 $self->addbadmessage('The display was already cleared.') if $none_shown;
629 : jj 1994
630 :     ##### View problems selected from the local list
631 :    
632 :     } elsif ($r->param('view_local_set')) {
633 :    
634 :     my $set_to_display = $r->param('library_sets');
635 : jj 2152 if (not defined($set_to_display) or $set_to_display eq SELECT_LOCAL_STRING or $set_to_display eq "Found no directories containing problems") {
636 :     $self->addbadmessage('You need to select a set to view.');
637 : jj 1994 } else {
638 : dpvc 2431 $set_to_display = '.' if $set_to_display eq MY_PROBLEMS;
639 :     $set_to_display = substr($browse_which,7) if $set_to_display eq MAIN_PROBLEMS;
640 : jj 1994 @pg_files = list_pg_files($ce->{courseDirs}->{templates},
641 :     "$set_to_display");
642 :     $use_previous_problems=0;
643 :     }
644 :    
645 :     ##### View problems selected from the a set in this course
646 :    
647 :     } elsif ($r->param('view_mysets_set')) {
648 :    
649 :     my $set_to_display = $r->param('library_sets');
650 : jj 2024 if (not defined($set_to_display)
651 :     or $set_to_display eq "Select a Problem Set"
652 : jj 2152 or $set_to_display eq NO_LOCAL_SET_STRING) {
653 :     $self->addbadmessage("You need to select a set from this course to view.");
654 : jj 1994 } else {
655 :     my @problemList = $db->listGlobalProblems($set_to_display);
656 :     my $problem;
657 :     @pg_files=();
658 :     for $problem (@problemList) {
659 :     my $problemRecord = $db->getGlobalProblem($set_to_display, $problem); # checked
660 :     die "global $problem for set $set_to_display not found." unless
661 :     $problemRecord;
662 :     push @pg_files, $problemRecord->source_file;
663 :    
664 :     }
665 :     $use_previous_problems=0;
666 :     }
667 :    
668 :     ##### View whole chapter from the library
669 :     ## This will change somewhat later
670 :    
671 : jj 2230 } elsif ($r->param('lib_view')) {
672 : jj 1994
673 :     @pg_files=();
674 :     my $chap = $r->param('library_chapters') || "";
675 :     $chap = "" if($chap eq "All Chapters");
676 :     my $sect = $r->param('library_sections') || "";
677 :     $sect = "" if($sect eq "All Sections");
678 :     my @dbsearch = WeBWorK::Utils::ListingDB::getSectionListings($r->{ce}, "$chap", "$sect");
679 :     my ($result, $tolibpath);
680 :     for $result (@dbsearch) {
681 : jj 2002 $tolibpath = "Library/$result->{path}/$result->{filename}";
682 : jj 1994
683 :     ## Too clunky!!!!
684 :     push @pg_files, $tolibpath;
685 :     }
686 :     $use_previous_problems=0;
687 :    
688 :     ##### Edit the current local problem set
689 :    
690 :     } elsif ($r->param('edit_local')) { ## Jump to set edit page
691 :    
692 : jj 2115 ; # already handled
693 :    
694 :    
695 : jj 1994 ##### Make a new local problem set
696 :    
697 :     } elsif ($r->param('new_local_set')) {
698 :     if ($r->param('new_set_name') !~ /^[\w.-]*$/) {
699 : jj 2152 $self->addbadmessage("The name ".$r->param('new_set_name')." is not a valid set name. Use only letters, digits, -, _, and .");
700 : jj 1994 } else {
701 :     my $newSetName = $r->param('new_set_name');
702 :     $newSetName =~ s/^set//;
703 :     $newSetName =~ s/\.def$//;
704 : jj 2112 $r->param('local_sets',$newSetName);
705 : jj 1994 my $newSetRecord = $db->getGlobalSet($newSetName);
706 :     if (defined($newSetRecord)) {
707 : jj 2152 $self->addbadmessage("The set name $newSetName is already in use. Pick a different name if you would like to start a new set.");
708 : jj 1994 } else { # Do it!
709 :     $newSetRecord = $db->{set}->{record}->new();
710 :     $newSetRecord->set_id($newSetName);
711 :     $newSetRecord->set_header("");
712 : gage 2450 $newSetRecord->hardcopy_header("");
713 : jj 1994 $newSetRecord->open_date(time()+60*60*24*7); # in one week
714 :     $newSetRecord->due_date(time()+60*60*24*7*2); # in two weeks
715 :     $newSetRecord->answer_date(time()+60*60*24*7*3); # in three weeks
716 :     eval {$db->addGlobalSet($newSetRecord)};
717 : jj 2269 $self->addgoodmessage("Set $newSetName has been created.");
718 :     my $selfassign = $r->param('selfassign') || "";
719 :     $selfassign = "" if($selfassign =~ /false/i); # deal with javascript false
720 :     if($selfassign) {
721 :     $self->assignSetToUser($userName, $newSetRecord);
722 :     $self->addgoodmessage("Set $newSetName was assigned to $userName.");
723 :     }
724 : jj 1994 }
725 :     }
726 :    
727 :     ##### Add selected problems to the current local set
728 :    
729 :     } elsif ($r->param('update')) {
730 :     ## first handle problems to be added before we hide them
731 :     my($localSet, @selected);
732 :    
733 : jj 2116 @pg_files = grep {($_->[1] & ADDED) != 0 } @{$self->{past_problems}};
734 : jj 1994 @selected = map {$_->[0]} @pg_files;
735 :    
736 : jj 2116 my @action_files = grep {$_->[1] > 0 } @{$self->{past_problems}};
737 : jj 2230 # There are now good reasons to do an update without selecting anything.
738 :     #if(scalar(@action_files) == 0) {
739 :     # $self->addbadmessage('Update requested, but no problems were marked.');
740 :     #}
741 : jj 2116
742 : jj 1994 if (scalar(@selected)>0) { # if some are to be added, they need a place to go
743 :     $localSet = $r->param('local_sets');
744 : jj 2152 if (not defined($localSet) or
745 :     $localSet eq SELECT_SET_STRING or
746 :     $localSet eq NO_LOCAL_SET_STRING) {
747 : jj 2264 $self->addbadmessage('You are trying to add problems to something, but you did not select a "Target Set" name as a target.');
748 : jj 1994 } else {
749 :     my $newSetRecord = $db->getGlobalSet($localSet);
750 :     if (not defined($newSetRecord)) {
751 : jj 2152 $self->addbadmessage("You are trying to add problems to $localSet, but that set does not seem to exist! I bet you used your \"Back\" button.");
752 : jj 1994 } else {
753 : jj 2116 my $addcount = add_selected($self, $db, $localSet);
754 :     if($addcount > 0) {
755 : jj 2152 $self->addgoodmessage("Added $addcount problem".(($addcount>1)?'s':'').
756 : jj 2116 " to $localSet.");
757 :     }
758 : jj 1994 }
759 :     }
760 :     }
761 :     ## now handle problems to be hidden
762 :    
763 : jj 2116 ## only keep the ones which are not hidden
764 :     @pg_files = grep {($_->[1] & HIDDEN) ==0 } @{$self->{past_problems}};
765 : jj 2230 @past_marks = map {$_->[1]} @pg_files;
766 : jj 1994 @pg_files = map {$_->[0]} @pg_files;
767 :     @all_past_list = (@all_past_list[0..($first_shown-1)],
768 :     @pg_files,
769 :     @all_past_list[($last_shown+1)..(scalar(@all_past_list)-1)]);
770 : jj 2225 $last_shown = $first_shown+$maxShown -1;
771 : jj 1994 $last_shown = (scalar(@all_past_list)-1) if($last_shown>=scalar(@all_past_list));
772 :    
773 :     } elsif ($r->param('next_page')) {
774 :     $first_shown = $last_shown+1;
775 : jj 2225 $last_shown = $first_shown+$maxShown-1;
776 : jj 1994 $last_shown = (scalar(@all_past_list)-1) if($last_shown>=scalar(@all_past_list));
777 : jj 2230 @past_marks = ();
778 : jj 1994 } elsif ($r->param('prev_page')) {
779 :     $last_shown = $first_shown-1;
780 : jj 2225 $first_shown = $last_shown - $maxShown+1;
781 : jj 1994
782 :     $first_shown = 0 if($first_shown<0);
783 : jj 2230 @past_marks = ();
784 : jj 1994
785 : jj 2230 } elsif ($r->param('select_all')) {
786 :     @past_marks = map {1} @past_marks;
787 :     } elsif ($r->param('select_none')) {
788 :     @past_marks = ();
789 :    
790 : jj 1994 ##### No action requested, probably our first time here
791 :    
792 :     } else {
793 :     #my $c = $r->connection;
794 :     #print "Debug info: ". $r->get_remote_host ."<p>". $c->remote_ip ;
795 :     ;
796 :     } ##### end of the if elsif ...
797 :    
798 :    
799 :     ############# List of local sets
800 :    
801 : jj 2024 my @all_set_defs = $db->listGlobalSets;
802 : jj 2700 @all_set_defs = sortByName(undef, @all_set_defs);
803 : jj 1994
804 :     if ($use_previous_problems) {
805 :     @pg_files = @all_past_list;
806 :     } else {
807 :     $first_shown = 0;
808 : jj 2225 $last_shown = scalar(@pg_files)<$maxShown ? scalar(@pg_files) : $maxShown;
809 : jj 1994 $last_shown--; # to make it an array index
810 : jj 2230 @past_marks = ();
811 : jj 1994 }
812 : jj 2115 ############# Now store data in self for retreival by body
813 :     $self->{first_shown} = $first_shown;
814 :     $self->{last_shown} = $last_shown;
815 :     $self->{browse_which} = $browse_which;
816 :     $self->{problem_seed} = $problem_seed;
817 :     $self->{pg_files} = \@pg_files;
818 : jj 2230 $self->{past_marks} = \@past_marks;
819 : jj 2115 $self->{all_set_defs} = \@all_set_defs;
820 : jj 1994
821 : jj 2115 }
822 :    
823 :    
824 :     sub title {
825 :     return "Problem Set Maker";
826 :     }
827 :    
828 :     sub body {
829 :     my ($self) = @_;
830 :    
831 :     my $r = $self->r;
832 :     my $ce = $r->ce; # course environment
833 :     my $db = $r->db; # database
834 :     my $j; # garden variety counter
835 :    
836 :     my $userName = $r->param('user');
837 :    
838 :     my $user = $db->getUser($userName); # checked
839 :     die "record for user $userName (real user) does not exist."
840 :     unless defined $user;
841 :    
842 :     ### Check that this is a professor
843 :     my $authz = $r->authz;
844 :     unless ($authz->hasPermissions($userName, "modify_problem_sets")) {
845 :     print "User $userName returned " .
846 :     $authz->hasPermissions($user, "modify_problem_sets") .
847 :     " for permission";
848 :     return(CGI::div({class=>'ResultsWithError'},
849 :     CGI::em("You are not authorized to access the Instructor tools.")));
850 :     }
851 :    
852 :     ########## Extract information computed in pre_header_initialize
853 :    
854 :     my $first_shown = $self->{first_shown};
855 :     my $last_shown = $self->{last_shown};
856 :     my $browse_which = $self->{browse_which};
857 :     my $problem_seed = $self->{problem_seed};
858 :     my @pg_files = @{$self->{pg_files}};
859 :     my @all_set_defs = @{$self->{all_set_defs}};
860 :    
861 :     my @pg_html=($last_shown>=$first_shown) ?
862 : jj 2225 renderProblems(r=> $r,
863 :     user => $user,
864 :     problem_list => [@pg_files[$first_shown..$last_shown]],
865 :     displayMode => $r->param('mydisplayMode')) : ();
866 : jj 1994
867 :     ########## Top part
868 : jj 2269 print CGI::startform({-method=>"POST", -action=>$r->uri, -name=>'mainform'}),
869 : jj 1994 $self->hidden_authen_fields,
870 :     '<div align="center">',
871 :     CGI::start_table({-border=>2});
872 : jj 2024 $self->make_top_row('all_set_defs'=>\@all_set_defs,
873 : jj 1994 'browse_which'=> $browse_which);
874 :     print CGI::hidden(-name=>'browse_which', -default=>[$browse_which]),
875 :     CGI::hidden(-name=>'problem_seed', -default=>[$problem_seed]);
876 :     for ($j = 0 ; $j < scalar(@pg_files) ; $j++) {
877 :     print CGI::hidden(-name=>"all_past_list$j", -default=>$pg_files[$j]);
878 :     }
879 :    
880 :     print CGI::hidden(-name=>'first_shown', -default=>[$first_shown]);
881 :     print CGI::hidden(-name=>'last_shown', -default=>[$last_shown]);
882 :    
883 :    
884 :     ########## Now print problems
885 :     my $jj;
886 :     for ($jj=0; $jj<scalar(@pg_html); $jj++) {
887 :     $pg_files[$jj] =~ s|^$ce->{courseDirs}->{templates}/?||;
888 : jj 2230 $self->make_data_row($pg_files[$jj+$first_shown], $pg_html[$jj], $jj+1, $self->{past_marks}->[$jj]);
889 : jj 1994 }
890 :    
891 :     ########## Finish things off
892 :     print CGI::end_table();
893 :     print '</div>';
894 :     # if($first_shown>0 or (1+$last_shown)<scalar(@pg_files)) {
895 :     my ($next_button, $prev_button) = ("", "");
896 :     if ($first_shown > 0) {
897 :     $prev_button = CGI::submit(-name=>"prev_page", -style=>"width:15ex",
898 :     -value=>"Previous page");
899 :     }
900 :     if ((1+$last_shown)<scalar(@pg_files)) {
901 :     $next_button = CGI::submit(-name=>"next_page", -style=>"width:15ex",
902 :     -value=>"Next page");
903 :     }
904 :     if (scalar(@pg_files)>0) {
905 :     print CGI::p(($first_shown+1)."-".($last_shown+1)." of ".scalar(@pg_files).
906 :     " shown.", $prev_button, " ", $next_button);
907 :     }
908 :     # }
909 :     print CGI::endform(), "\n";
910 :    
911 :     return "";
912 :     }
913 :    
914 :     ############################################## End of Body
915 :    
916 :     # SKEL: To emit your own HTTP header, uncomment this:
917 :     #
918 :     #sub header {
919 :     # my ($self) = @_;
920 :     #
921 :     # # Generate your HTTP header here.
922 :     #
923 :     # # If you return something, it will be used as the HTTP status code for this
924 :     # # request. The Apache::Constants module might be useful for gerating status
925 :     # # codes. If you don't return anything, the status code "OK" will be used.
926 :     # return "";
927 :     #}
928 :    
929 :     # SKEL: If you need to do any processing after the HTTP header is sent, but before
930 :     # any template processing occurs, or you need to calculate values that will be
931 :     # used in multiple methods, do it in this method:
932 :     #
933 :     #sub initialize {
934 :     #my ($self) = @_;
935 :     #}
936 :    
937 :     # SKEL: If you need to add tags to the document <HEAD>, uncomment this method:
938 :     #
939 :     #sub head {
940 :     # my ($self) = @_;
941 :     #
942 :     # # You can print head tags here, like <META>, <SCRIPT>, etc.
943 :     #
944 :     # return "";
945 :     #}
946 :    
947 :     # SKEL: To fill in the "info" box (to the right of the main body), use this
948 :     # method:
949 :     #
950 :     #sub info {
951 :     # my ($self) = @_;
952 :     #
953 :     # # Print HTML here.
954 :     #
955 :     # return "";
956 :     #}
957 :    
958 :     # SKEL: To provide navigation links, use this method:
959 :     #
960 :     #sub nav {
961 :     # my ($self, $args) = @_;
962 :     #
963 :     # # See the documentation of path() and pathMacro() in
964 :     # # WeBWorK::ContentGenerator for more information.
965 :     #
966 :     # return "";
967 :     #}
968 :    
969 :     # SKEL: For a little box for display options, etc., use this method:
970 :     #
971 :     #sub options {
972 :     # my ($self) = @_;
973 :     #
974 :     # # Print HTML here.
975 :     #
976 :     # return "";
977 :     #}
978 :    
979 :     # SKEL: For a list of sibling objects, use this method:
980 :     #
981 :     #sub siblings {
982 :     # my ($self, $args) = @_;
983 :     #
984 :     # # See the documentation of siblings() and siblingsMacro() in
985 :     # # WeBWorK::ContentGenerator for more information.
986 :     # #
987 :     # # Refer to implementations in ProblemSet and Problem.
988 :     #
989 :     # return "";
990 :     #}
991 :    
992 :     =head1 AUTHOR
993 :    
994 :     Written by John Jones, jj (at) asu.edu.
995 :    
996 :     =cut
997 :    
998 :    
999 :    
1000 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9