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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : jj 1994 ################################################################################
2 :     # WeBWorK Online Homework Delivery System
3 :     # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/
4 : jj 3431 # $CVSHeader: webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm,v 1.46 2005/07/29 20:45:38 jj 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 : sh002i 2941 # FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the
14 : jj 1994 # 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 : jj 3402 use File::Find;
35 : jj 1994
36 :     require WeBWorK::Utils::ListingDB;
37 :    
38 : jj 2225 use constant MAX_SHOW_DEFAULT => 20;
39 : jj 3397 use constant NO_LOCAL_SET_STRING => 'No sets in this course yet';
40 : jj 3402 use constant SELECT_SET_STRING => 'Select a Set from this Course';
41 : dpvc 2431 use constant SELECT_LOCAL_STRING => 'Select a Problem Collection';
42 : sh002i 2944 use constant MY_PROBLEMS => ' My Problems ';
43 :     use constant MAIN_PROBLEMS => ' Main Problems ';
44 : jj 3397 use constant CREATE_SET_BUTTON => 'Create New Set';
45 : jj 3424 use constant ALL_CHAPTERS => 'All Chapters';
46 :     use constant ALL_SUBJECTS => 'All Subjects';
47 :     use constant ALL_SECTIONS => 'All Sections';
48 :     use constant ALL_TEXTBOOKS => 'All Textbooks';
49 : jj 1994
50 : jj 3431 #use constant LIB2_DATA => [
51 :     #[qw(dbchapter library_chapters), ALL_CHAPTERS],
52 :     #[qw(dbsection library_sections), ALL_SECTIONS],
53 :     #[qw(dbsubject library_subjects), ALL_SUBJECT],
54 :     #[qw(textbook library_textbook), ALL_TEXTBOOKS],
55 :     #];
56 :    
57 : jj 2116 ## Flags for operations on files
58 :    
59 :     use constant ADDED => 1;
60 :     use constant HIDDEN => (1 << 1);
61 :     use constant SUCCESS => (1 << 2);
62 :    
63 : sh002i 2941 ## for additional problib buttons
64 :     my %problib; ## filled in in global.conf
65 : dpvc 2734 my %ignoredir = (
66 : sh002i 2941 '.' => 1, '..' => 1, 'Library' => 1,
67 :     'headers' => 1, 'macros' => 1, 'email' => 1,
68 : dpvc 2734 );
69 : dpvc 2431
70 : jj 2039 ## This is for searching the disk for directories containing pg files.
71 : jj 1994 ## to make the recursion work, this returns an array where the first
72 : sh002i 2941 ## item is the number of pg files in the directory. The second is a
73 : dpvc 2734 ## list of directories which contain pg files.
74 :     ##
75 :     ## If a directory contains only one pg file and at least one other
76 :     ## file, the directory is considered to be part of the parent
77 :     ## directory (it is probably in a separate directory only because
78 :     ## it has auxiliarly files that want to be kept together with the
79 :     ## pg file).
80 :     ##
81 :     ## If a directory has a file named "=library-ignore", it is never
82 : sh002i 2941 ## included in the directory menu. If a directory contains a file
83 : dpvc 2734 ## called "=library-combine-up", then its pg are included with those
84 :     ## in the parent directory (and the directory does not appear in the
85 : sh002i 2941 ## menu). If it has a file called "=library-no-combine" then it is
86 : dpvc 2734 ## always listed as a separate directory even if it contains only one
87 :     ## pg file.
88 :    
89 : jj 1994 sub get_library_sets {
90 : sh002i 2941 my $top = shift; my $dir = shift;
91 : sh002i 2946 # ignore directories that give us an error
92 :     my @lis = eval { readDirectory($dir) };
93 :     if ($@) {
94 :     warn $@;
95 :     return (0);
96 :     }
97 : sh002i 2941 return (0) if grep /^=library-ignore$/, @lis;
98 : dpvc 2734
99 : sh002i 2946 my @pgdirs;
100 :    
101 : sh002i 2941 my $pgcount = scalar(grep { m/\.pg$/ and (not m/(Header|-text)\.pg$/) and -f "$dir/$_"} @lis);
102 :     my $others = scalar(grep { (!m/\.pg$/ || m/(Header|-text)\.pg$/) &&
103 :     !m/(\.(tmp|bak)|~)$/ && -f "$dir/$_" } @lis);
104 : dpvc 2734
105 : sh002i 2941 my @dirs = grep {!$ignoredir{$_} and -d "$dir/$_"} @lis;
106 :     if ($top == 1) {@dirs = grep {!$problib{$_}} @dirs}
107 :     foreach my $subdir (@dirs) {
108 :     my @results = get_library_sets(0, "$dir/$subdir");
109 :     $pgcount += shift @results; push(@pgdirs,@results);
110 :     }
111 : dpvc 2734
112 : sh002i 2941 return ($pgcount, @pgdirs) if $top || $pgcount == 0 || grep /^=library-combine-up$/, @lis;
113 :     return (0,@pgdirs,$dir) if $pgcount > 1 || $others == 0 || grep /^=library-no-combine$/, @lis;
114 :     return ($pgcount, @pgdirs);
115 : jj 1994 }
116 :    
117 : dpvc 2734 sub get_library_pgs {
118 : sh002i 2941 my $top = shift; my $base = shift; my $dir = shift;
119 :     my @lis = readDirectory("$base/$dir");
120 :     return () if grep /^=library-ignore$/, @lis;
121 :     return () if !$top && grep /^=library-no-combine$/, @lis;
122 : jj 1994
123 : sh002i 2941 my @pgs = grep { m/\.pg$/ and (not m/(Header|-text)\.pg$/) and -f "$base/$dir/$_"} @lis;
124 :     my $others = scalar(grep { (!m/\.pg$/ || m/(Header|-text)\.pg$/) &&
125 :     !m/(\.(tmp|bak)|~)$/ && -f "$base/$dir/$_" } @lis);
126 : dpvc 2734
127 : sh002i 2941 my @dirs = grep {!$ignoredir{$_} and -d "$base/$dir/$_"} @lis;
128 :     if ($top == 1) {@dirs = grep {!$problib{$_}} @dirs}
129 :     foreach my $subdir (@dirs) {push(@pgs, get_library_pgs(0,"$base/$dir",$subdir))}
130 : dpvc 2734
131 : sh002i 2941 return () unless $top || (scalar(@pgs) == 1 && $others) || grep /^=library-combine-up$/, @lis;
132 :     return (map {"$dir/$_"} @pgs);
133 : jj 1994 }
134 :    
135 : dpvc 2734 sub list_pg_files {
136 : sh002i 2941 my ($templates,$dir) = @_;
137 :     my $top = ($dir eq '.')? 1 : 2;
138 :     my @pgs = get_library_pgs($top,$templates,$dir);
139 :     return sortByName(undef,@pgs);
140 : dpvc 2734 }
141 :    
142 : jj 3402 ## Search for set definition files
143 :    
144 :     # initialize global variable for search
145 :     my @found_set_defs = ();
146 :    
147 :     sub get_set_defs_wanted {
148 :     my $fn = $_;
149 :     my $fdir = $File::Find::dir;
150 :     return() if($fn !~ /^set.*\.def$/);
151 :     #return() if(not -T $fn);
152 :     push @found_set_defs, "$fdir/$fn";
153 :     }
154 :    
155 :     sub get_set_defs {
156 :     my $topdir = shift;
157 :     @found_set_defs = ();
158 :     find({ wanted => \&get_set_defs_wanted, follow_fast=>1}, $topdir);
159 :     map { $_ =~ s|^$topdir/?|| } @found_set_defs;
160 :     return @found_set_defs;
161 :     }
162 :    
163 :     ## Try to make reading of set defs more flexible. Additional strategies
164 :     ## for fixing a path can be added here.
165 :    
166 :     sub munge_pg_file_path {
167 :     my $self = shift;
168 :     my $pg_path = shift;
169 :     my $path_to_set_def = shift;
170 :     my $end_path = $pg_path;
171 :     # if the path is ok, don't fix it
172 :     return($pg_path) if(-e $self->r->ce->{courseDirs}{templates}."/$pg_path");
173 :     # if we have followed a link into a self contained course to get
174 :     # to the set.def file, we need to insert the start of the path to
175 :     # the set.def file
176 :     $end_path = "$path_to_set_def/$pg_path";
177 :     return($end_path) if(-e $self->r->ce->{courseDirs}{templates}."/$end_path");
178 :     # if we got this far, this path is bad, but we let it produce
179 :     # an error so the user knows there is a troublesome path in the
180 :     # set.def file.
181 :     return($pg_path);
182 :     }
183 :    
184 :     ## Read a set definition file. This could be abstracted since it happens
185 :     ## elsewhere. Here we don't have to process so much of the file.
186 :    
187 :     sub read_set_def {
188 :     my $self = shift;
189 :     my $r = $self->r;
190 :     my $filePathOrig = shift;
191 :     my $filePath = $r->ce->{courseDirs}{templates}."/$filePathOrig";
192 :     $filePathOrig =~ s/set.*\.def$//;
193 :     $filePathOrig =~ s|/$||;
194 :     $filePathOrig = "." if ($filePathOrig !~ /\S/);
195 :     my @pg_files = ();
196 :     my ($line, $got_to_pgs, $name, @rest) = ("", 0, "");
197 :     if ( open (SETFILENAME, "$filePath") ) {
198 :     while($line = <SETFILENAME>) {
199 :     chomp($line);
200 :     $line =~ s|(#.*)||; # don't read past comments
201 :     if($got_to_pgs) {
202 :     unless ($line =~ /\S/) {next;} # skip blank lines
203 :     ($name,@rest) = split (/\s*,\s*/,$line);
204 :     $name =~ s/\s*//g;
205 :     push @pg_files, $name;
206 :     } else {
207 :     $got_to_pgs = 1 if ($line =~ /problemList\s*=/);
208 :     }
209 :     }
210 :     } else {
211 :     $self->addbadmessage("Cannot open $filePath");
212 :     }
213 :     # This is where we would potentially munge the pg file paths
214 :     # One possibility
215 :     @pg_files = map { $self->munge_pg_file_path($_, $filePathOrig) } @pg_files;
216 :     return(@pg_files);
217 :     }
218 :    
219 : jj 1994 ## go through past page getting a list of identifiers for the problems
220 :     ## and whether or not they are selected, and whether or not they should
221 :     ## be hidden
222 :    
223 :     sub get_past_problem_files {
224 : sh002i 2941 my $r = shift;
225 :     my @found=();
226 :     my $count =1;
227 :     while (defined($r->param("filetrial$count"))) {
228 :     my $val = 0;
229 :     $val |= ADDED if($r->param("trial$count"));
230 :     $val |= HIDDEN if($r->param("hideme$count"));
231 :     push @found, [$r->param("filetrial$count"), $val];
232 :     $count++;
233 :     }
234 :     return(\@found);
235 : jj 1994 }
236 :    
237 :     #### For adding new problems
238 :    
239 :     sub add_selected {
240 : sh002i 2941 my $self = shift;
241 :     my $db = shift;
242 :     my $setName = shift;
243 :     my @past_problems = @{$self->{past_problems}};
244 :     my @selected = @past_problems;
245 :     my (@path, $file, $selected, $freeProblemID);
246 :     $freeProblemID = max($db->listGlobalProblems($setName)) + 1;
247 :     my $addedcount=0;
248 : jj 1994
249 : sh002i 2941 for $selected (@selected) {
250 :     if($selected->[1] & ADDED) {
251 :     $file = $selected->[0];
252 :     my $problemRecord = $self->addProblemToSet(setName => $setName,
253 :     sourceFile => $file, problemID => $freeProblemID);
254 :     $freeProblemID++;
255 :     $self->assignProblemToAllSetUsers($problemRecord);
256 :     $selected->[1] |= SUCCESS;
257 :     $addedcount++;
258 :     }
259 :     }
260 :     return($addedcount);
261 : jj 1994 }
262 :    
263 :    
264 : jj 2039 ############# List of sets of problems in templates directory
265 : jj 1994
266 : jj 2115 sub get_problem_directories {
267 : sh002i 2941 my $ce = shift;
268 :     my $lib = shift;
269 :     my $source = $ce->{courseDirs}{templates};
270 :     my $main = MY_PROBLEMS; my $isTop = 1;
271 :     if ($lib) {$source .= "/$lib"; $main = MAIN_PROBLEMS; $isTop = 2}
272 :     my @all_problem_directories = get_library_sets($isTop, $source);
273 :     my $includetop = shift @all_problem_directories;
274 :     my $j;
275 :     for ($j=0; $j<scalar(@all_problem_directories); $j++) {
276 :     $all_problem_directories[$j] =~ s|^$ce->{courseDirs}->{templates}/?||;
277 :     }
278 :     @all_problem_directories = sortByName(undef, @all_problem_directories);
279 :     unshift @all_problem_directories, $main if($includetop);
280 :     return (\@all_problem_directories);
281 : jj 1994 }
282 :    
283 : sh002i 2941 ############# Everyone has a view problems line. Abstract it
284 : jj 2225 sub view_problems_line {
285 : sh002i 2941 my $internal_name = shift;
286 :     my $label = shift;
287 :     my $r = shift; # so we can get parameter values
288 :     my $result = CGI::submit(-name=>"$internal_name", -value=>$label);
289 : jj 2225
290 : sh002i 2941 my %display_modes = %{WeBWorK::PG::DISPLAY_MODES()};
291 :     my @active_modes = grep { exists $display_modes{$_} }
292 :     @{$r->ce->{pg}->{displayModes}};
293 :     push @active_modes, 'None';
294 :     # We have our own displayMode since its value may be None, which is illegal
295 :     # in other modules.
296 :     my $mydisplayMode = $r->param('mydisplayMode') || $r->ce->{pg}->{options}->{displayMode};
297 :     $result .= '&nbsp;Display&nbsp;Mode:&nbsp;'.CGI::popup_menu(-name=> 'mydisplayMode',
298 :     -values=>\@active_modes,
299 :     -default=> $mydisplayMode);
300 :     # Now we give a choice of the number of problems to show
301 :     my $defaultMax = $r->param('max_shown') || MAX_SHOW_DEFAULT;
302 :     $result .= '&nbsp;Max. Shown:&nbsp'.
303 :     CGI::popup_menu(-name=> 'max_shown',
304 :     -values=>[5,10,15,20,25,30,50,'All'],
305 :     -default=> $defaultMax);
306 :    
307 :     return($result);
308 : jj 2225 }
309 :    
310 :    
311 : jj 1994 ### The browsing panel has three versions
312 : sh002i 2941 ##### Version 1 is local problems
313 : jj 1994 sub browse_local_panel {
314 : sh002i 2941 my $self = shift;
315 :     my $library_selected = shift;
316 :     my $lib = shift || ''; $lib =~ s/^browse_//;
317 :     my $name = ($lib eq '')? 'Local' : $problib{$lib};
318 : jj 1994
319 : sh002i 2941 my $list_of_prob_dirs= get_problem_directories($self->r->ce,$lib);
320 :     if(scalar(@$list_of_prob_dirs) == 0) {
321 :     $library_selected = "Found no directories containing problems";
322 :     unshift @{$list_of_prob_dirs}, $library_selected;
323 :     } else {
324 :     my $default_value = SELECT_LOCAL_STRING;
325 :     if (not $library_selected or $library_selected eq $default_value) {
326 :     unshift @{$list_of_prob_dirs}, $default_value;
327 :     $library_selected = $default_value;
328 :     }
329 :     }
330 :     my $view_problem_line = view_problems_line('view_local_set', 'View Problems', $self->r);
331 :     print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "$name Problems: ",
332 :     CGI::popup_menu(-name=> 'library_sets',
333 :     -values=>$list_of_prob_dirs,
334 :     -default=> $library_selected),
335 :     CGI::br(),
336 :     $view_problem_line,
337 :     ));
338 : jj 1994 }
339 :    
340 : sh002i 2941 ##### Version 2 is local problem sets
341 : jj 1994 sub browse_mysets_panel {
342 : sh002i 2941 my $self = shift;
343 :     my $library_selected = shift;
344 :     my $list_of_local_sets = shift;
345 : sh002i 3357 my $default_value = "Select a Homework Set";
346 : jj 1994
347 : sh002i 2941 if(scalar(@$list_of_local_sets) == 0) {
348 :     $list_of_local_sets = [NO_LOCAL_SET_STRING];
349 :     } elsif (not $library_selected or $library_selected eq $default_value) {
350 :     unshift @{$list_of_local_sets}, $default_value;
351 :     $library_selected = $default_value;
352 :     }
353 : jj 1994
354 : sh002i 2941 my $view_problem_line = view_problems_line('view_mysets_set', 'View Problems', $self->r);
355 :     print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "Browse from: ",
356 :     CGI::popup_menu(-name=> 'library_sets',
357 :     -values=>$list_of_local_sets,
358 :     -default=> $library_selected),
359 :     CGI::br(),
360 :     $view_problem_line
361 :     ));
362 : jj 1994 }
363 :    
364 : sh002i 2941 ##### Version 3 is the problem library
365 : jj 3424 #
366 :     # This comes in 3 forms, problem library version 1, and for version 2 there
367 :     # is the basic, and the advanced interfaces. This function checks what we are
368 :     # supposed to do, or aborts if the problem library has not been installed.
369 : jj 1994
370 :     sub browse_library_panel {
371 : jj 3409 my $self=shift;
372 : sh002i 2941 my $r = $self->r;
373 :     my $ce = $r->ce;
374 : jj 3424
375 : jj 3409 # See if the problem library is installed
376 : sh002i 2941 my $libraryRoot = $r->{ce}->{problemLibrary}->{root};
377 : jj 1994
378 : sh002i 2941 unless($libraryRoot) {
379 :     print CGI::Tr(CGI::td(CGI::div({class=>'ResultsWithError', align=>"center"},
380 :     "The problem library has not been installed.")));
381 :     return;
382 :     }
383 : jj 3409 # Test if the Library directory link exists. If not, try to make it
384 : sh002i 2941 unless(-d "$ce->{courseDirs}->{templates}/Library") {
385 :     unless(symlink($libraryRoot, "$ce->{courseDirs}->{templates}/Library")) {
386 :     my $msg = <<"HERE";
387 : jj 2039 You are missing the directory <code>templates/Library</code>, which is needed
388 : sh002i 2941 for the Problem Library to function. It should be a link pointing to
389 : jj 2039 <code>$libraryRoot</code>, which you set in <code>conf/global.conf</code>.
390 : sh002i 2941 I tried to make the link for you, but that failed. Check the permissions
391 : jj 2039 in your <code>templates</code> directory.
392 :     HERE
393 : sh002i 2941 $self->addbadmessage($msg);
394 :     }
395 :     }
396 : jj 1994
397 : jj 3409 # Now check what version we are supposed to use
398 :     my $libraryVersion = $r->{ce}->{problemLibrary}->{version} || 1;
399 :     if($libraryVersion == 1) {
400 :     return $self->browse_library_panel1;
401 :     } elsif($libraryVersion == 2) {
402 : jj 3424 return $self->browse_library_panel2 if($self->{library_basic}==1);
403 :     return $self->browse_library_panel2adv;
404 : jj 3409 } else {
405 :     print CGI::Tr(CGI::td(CGI::div({class=>'ResultsWithError', align=>"center"},
406 :     "The problem library version is set to an illegal value.")));
407 :     return;
408 :     }
409 :     }
410 :    
411 :     sub browse_library_panel1 {
412 :     my $self = shift;
413 :     my $r = $self->r;
414 :     my $ce = $r->ce;
415 :    
416 : sh002i 2941 my @chaps = WeBWorK::Utils::ListingDB::getAllChapters($r->{ce});
417 : jj 3424 unshift @chaps, ALL_CHAPTERS;
418 :     my $chapter_selected = $r->param('library_chapters') || ALL_CHAPTERS;
419 : jj 1994
420 : sh002i 2941 my @sects=();
421 : jj 3424 if ($chapter_selected ne ALL_CHAPTERS) {
422 : sh002i 2941 @sects = WeBWorK::Utils::ListingDB::getAllSections($r->{ce}, $chapter_selected);
423 :     }
424 : jj 1994
425 : jj 3424 unshift @sects, ALL_SECTIONS;
426 :     my $section_selected = $r->param('library_sections') || ALL_SECTIONS;
427 : sh002i 2941 my $view_problem_line = view_problems_line('lib_view', 'View Problems', $self->r);
428 : jj 1994
429 : sh002i 2941 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"},
430 :     CGI::start_table(),
431 :     CGI::Tr(
432 : jj 1994 CGI::td(["Chapter:",
433 : sh002i 2941 CGI::popup_menu(-name=> 'library_chapters',
434 :     -values=>\@chaps,
435 :     -default=> $chapter_selected,
436 :     -onchange=>"submit();return true"
437 :     ),
438 :     CGI::submit(-name=>"lib_select_chapter", -value=>"Update Section List")])),
439 : jj 1994 CGI::Tr(
440 :     CGI::td("Section:"),
441 :     CGI::td({-colspan=>2},
442 :     CGI::popup_menu(-name=> 'library_sections',
443 : sh002i 2941 -values=>\@sects,
444 :     -default=> $section_selected
445 :     ))),
446 : jj 1994
447 : sh002i 2941 CGI::Tr(CGI::td({-colspan=>3}, $view_problem_line)),
448 : jj 1994 CGI::end_table(),
449 : sh002i 2941 ));
450 : jj 1994 }
451 :    
452 : jj 3409 sub browse_library_panel2 {
453 :     my $self = shift;
454 :     my $r = $self->r;
455 :     my $ce = $r->ce;
456 :    
457 : jj 3424 my @subjs = WeBWorK::Utils::ListingDB::getAllDBsubjects($r);
458 :     unshift @subjs, ALL_SUBJECTS;
459 : jj 3409
460 : jj 3424 my @chaps = WeBWorK::Utils::ListingDB::getAllDBchapters($r);
461 :     unshift @chaps, ALL_CHAPTERS;
462 : jj 3410
463 : jj 3424 my @sects=();
464 :     @sects = WeBWorK::Utils::ListingDB::getAllDBsections($r);
465 :     unshift @sects, ALL_SECTIONS;
466 : jj 3409
467 : jj 3424 my $subject_selected = $r->param('library_subjects') || ALL_SUBJECTS;
468 :     my $chapter_selected = $r->param('library_chapters') || ALL_CHAPTERS;
469 :     my $section_selected = $r->param('library_sections') || ALL_SECTIONS;
470 :    
471 : jj 3409 my $view_problem_line = view_problems_line('lib_view', 'View Problems', $self->r);
472 :    
473 :     print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"},
474 : jj 3424 CGI::hidden(-name=>"library_is_basic", -default=>[1]),
475 :     CGI::start_table({-width=>"100%"}),
476 : jj 3409 CGI::Tr(
477 : jj 3410 CGI::td(["Subject:",
478 :     CGI::popup_menu(-name=> 'library_subjects',
479 :     -values=>\@subjs,
480 :     -default=> $subject_selected,
481 :     -onchange=>"submit();return true"
482 : jj 3424 )]),
483 :     CGI::td({-colspan=>2, -align=>"right"},
484 :     CGI::submit(-name=>"lib_select_subject", -value=>"Update Chapter/Section Lists"))
485 :     ),
486 : jj 3410 CGI::Tr(
487 : jj 3409 CGI::td(["Chapter:",
488 :     CGI::popup_menu(-name=> 'library_chapters',
489 :     -values=>\@chaps,
490 :     -default=> $chapter_selected,
491 :     -onchange=>"submit();return true"
492 : jj 3424 )]),
493 :     CGI::td({-colspan=>2, -align=>"right"},
494 :     CGI::submit(-name=>"library_advanced", -value=>"Advanced Search"))
495 :     ),
496 : jj 3409 CGI::Tr(
497 : jj 3424 CGI::td(["Section:",
498 : jj 3409 CGI::popup_menu(-name=> 'library_sections',
499 :     -values=>\@sects,
500 :     -default=> $section_selected
501 : jj 3424 )]),
502 :     ),
503 : jj 3409 CGI::Tr(CGI::td({-colspan=>3}, $view_problem_line)),
504 :     CGI::end_table(),
505 :     ));
506 : jj 3424
507 :     }
508 : jj 3409
509 : jj 3424 sub browse_library_panel2adv {
510 :     my $self = shift;
511 :     my $r = $self->r;
512 :     my $ce = $r->ce;
513 : jj 3427 my $right_button_style = "width: 18ex";
514 : jj 3424
515 :     my @subjs = WeBWorK::Utils::ListingDB::getAllDBsubjects($r);
516 : jj 3431 if(! grep { $_ eq $r->param('library_subjects') } @subjs) {
517 :     $r->param('library_subjects', '');
518 :     }
519 : jj 3424 unshift @subjs, ALL_SUBJECTS;
520 :    
521 :     my @chaps = WeBWorK::Utils::ListingDB::getAllDBchapters($r);
522 : jj 3431 if(! grep { $_ eq $r->param('library_chapters') } @chaps) {
523 :     $r->param('library_chapters', '');
524 :     }
525 : jj 3424 unshift @chaps, ALL_CHAPTERS;
526 :    
527 :     my @sects = WeBWorK::Utils::ListingDB::getAllDBsections($r);
528 : jj 3431 if(! grep { $_ eq $r->param('library_sections') } @sects) {
529 :     $r->param('library_sections', '');
530 :     }
531 : jj 3424 unshift @sects, ALL_SECTIONS;
532 :    
533 :     my $texts = WeBWorK::Utils::ListingDB::getDBTextbooks($r);
534 :     my @textarray = map { $_->[0] } @{$texts};
535 :     my %textlabels = ();
536 :     for my $ta (@{$texts}) {
537 :     $textlabels{$ta->[0]} = $ta->[1]." by ".$ta->[2]." (edition ".$ta->[3].")";
538 :     }
539 : jj 3431 if(! grep { $_ eq $r->param('library_textbook') } @textarray) {
540 :     $r->param('library_textbook', '');
541 :     }
542 : jj 3424 unshift @textarray, ALL_TEXTBOOKS;
543 :     my $atb = ALL_TEXTBOOKS; $textlabels{$atb} = ALL_TEXTBOOKS;
544 :    
545 :     my $section_selected = $r->param('library_sections') || ALL_SECTIONS;
546 :     my $chapter_selected = $r->param('library_chapters') || ALL_CHAPTERS;
547 :     my $subject_selected = $r->param('library_subjects') || ALL_SUBJECTS;
548 :     my $textbook_selected = $r->param('library_textbook') || ALL_TEXTBOOKS;
549 :    
550 :     my $text_popup = CGI::popup_menu(-name => 'library_textbook',
551 :     -values =>\@textarray,
552 :     -labels => \%textlabels,
553 : jj 3427 -default=>$textbook_selected,
554 :     -onchange=>"submit();return true");
555 :    
556 :     my $library_keywords = $r->param('library_keywords') || '';
557 :    
558 : jj 3424 my $view_problem_line = view_problems_line('lib_view', 'View Problems', $self->r);
559 :    
560 : jj 3427 my $count_line = WeBWorK::Utils::ListingDB::countDBListings($r);
561 :     if($count_line==0) {
562 :     $count_line = "There are no matching pg files";
563 :     } else {
564 :     $count_line = "There are $count_line matching WeBWorK problem files";
565 :     }
566 :    
567 : jj 3424 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"},
568 :     CGI::hidden(-name=>"library_is_basic", -default=>[2]),
569 :     CGI::start_table({-width=>"100%"}),
570 :     # Html done by hand since it is temporary
571 :     CGI::Tr(CGI::td({-colspan=>4, -align=>"center"}, 'All Selected Constraints Joined by "And"')),
572 :     CGI::Tr(
573 :     CGI::td(["Subject:",
574 :     CGI::popup_menu(-name=> 'library_subjects',
575 :     -values=>\@subjs,
576 :     -default=> $subject_selected,
577 :     -onchange=>"submit();return true"
578 :     )]),
579 :     CGI::td({-colspan=>2, -align=>"right"},
580 : jj 3427 CGI::submit(-name=>"lib_select_subject", -value=>"Update Menus",
581 :     -style=> $right_button_style))),
582 : jj 3424 CGI::Tr(
583 :     CGI::td(["Chapter:",
584 :     CGI::popup_menu(-name=> 'library_chapters',
585 :     -values=>\@chaps,
586 :     -default=> $chapter_selected,
587 :     -onchange=>"submit();return true"
588 :     )]),
589 :     CGI::td({-colspan=>2, -align=>"right"},
590 : jj 3427 CGI::submit(-name=>"library_reset", -value=>"Reset",
591 :     -style=>$right_button_style))
592 : jj 3424 ),
593 :     CGI::Tr(
594 :     CGI::td(["Section:",
595 :     CGI::popup_menu(-name=> 'library_sections',
596 :     -values=>\@sects,
597 :     -default=> $section_selected,
598 :     -onchange=>"submit();return true"
599 :     )]),
600 : jj 3427 CGI::td({-colspan=>2, -align=>"right"},
601 :     CGI::submit(-name=>"library_basic", -value=>"Basic Search",
602 :     -style=>$right_button_style))
603 : jj 3424 ),
604 :     CGI::Tr(
605 :     CGI::td(["Textbook:", $text_popup]),
606 :     ),
607 : jj 3427 CGI::Tr(CGI::td("Keywords:"),CGI::td({-colspan=>2},
608 :     CGI::textfield(-name=>"library_keywords",
609 :     -default=>$library_keywords,
610 :     -override=>1,
611 :     -size=>40))),
612 : jj 3424 CGI::Tr(CGI::td({-colspan=>3}, $view_problem_line)),
613 : jj 3427 CGI::Tr(CGI::td({-colspan=>3, -align=>"center"}, $count_line)),
614 : jj 3424 CGI::end_table(),
615 :     ));
616 : jj 3409
617 :     }
618 :    
619 :    
620 : jj 3402 ##### Version 4 is the set definition file panel
621 :    
622 :     sub browse_setdef_panel {
623 :     my $self = shift;
624 :     my $r = $self->r;
625 :     my $ce = $r->ce;
626 :     my $library_selected = shift;
627 :     my $default_value = "Select a Set Definition File";
628 :     my @list_of_set_defs = get_set_defs($ce->{courseDirs}{templates});
629 :     if(scalar(@list_of_set_defs) == 0) {
630 :     @list_of_set_defs = (NO_LOCAL_SET_STRING);
631 :     } elsif (not $library_selected or $library_selected eq $default_value) {
632 :     unshift @list_of_set_defs, $default_value;
633 :     $library_selected = $default_value;
634 :     }
635 :     my $view_problem_line = view_problems_line('view_setdef_set', 'View Problems', $self->r);
636 :     my $popupetc = CGI::popup_menu(-name=> 'library_sets',
637 :     -values=>\@list_of_set_defs,
638 :     -default=> $library_selected).
639 :     CGI::br(). $view_problem_line;
640 :     if($list_of_set_defs[0] eq NO_LOCAL_SET_STRING) {
641 :     $popupetc = "there are no set definition files in this course to look at."
642 :     }
643 :     print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "Browse from: ",
644 :     $popupetc
645 :     ));
646 :     }
647 :    
648 : jj 1994 sub make_top_row {
649 : sh002i 2941 my $self = shift;
650 :     my $r = $self->r;
651 :     my $ce = $r->ce;
652 :     my %data = @_;
653 : jj 1994
654 : jj 3402 my $list_of_local_sets = $data{all_db_sets};
655 : sh002i 2941 my $have_local_sets = scalar(@$list_of_local_sets);
656 :     my $browse_which = $data{browse_which};
657 :     my $library_selected = $r->param('library_sets');
658 :     my $set_selected = $r->param('local_sets');
659 : jj 1994
660 : jj 3402 my ($dis1, $dis2, $dis3, $dis4) = ("","","", "");
661 : sh002i 2941 $dis1 = '-disabled' if($browse_which eq 'browse_library');
662 :     $dis2 = '-disabled' if($browse_which eq 'browse_local');
663 :     $dis3 = '-disabled' if($browse_which eq 'browse_mysets');
664 : jj 3402 $dis4 = '-disabled' if($browse_which eq 'browse_setdefs');
665 : jj 1994
666 : sh002i 2941 ## Make buttons for additional problem libraries
667 :     my $libs = '';
668 :     foreach my $lib (sort(keys(%problib))) {
669 :     $libs .= ' '. CGI::submit(-name=>"browse_$lib", -value=>$problib{$lib},
670 :     ($browse_which eq "browse_$lib")? '-disabled': '')
671 :     if (-d "$ce->{courseDirs}{templates}/$lib");
672 :     }
673 :     $libs = CGI::br()."or Problems from".$libs if $libs ne '';
674 : dpvc 2431
675 : jj 3397 my $these_widths = "width: 23ex";
676 : jj 1994
677 : sh002i 2941 if($have_local_sets ==0) {
678 :     $list_of_local_sets = [NO_LOCAL_SET_STRING];
679 :     } elsif (not $set_selected or $set_selected eq SELECT_SET_STRING) {
680 :     unshift @{$list_of_local_sets}, SELECT_SET_STRING;
681 :     $set_selected = SELECT_SET_STRING;
682 :     }
683 :     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;';
684 : jj 1994
685 : jj 3397 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "Add problems to ",
686 : sh002i 2941 CGI::b("Target Set: "),
687 :     CGI::popup_menu(-name=> 'local_sets',
688 :     -values=>$list_of_local_sets,
689 :     -default=> $set_selected),
690 :     CGI::submit(-name=>"edit_local", -value=>"Edit Target Set"),
691 :     CGI::hidden(-name=>"selfassign", -default=>[0]).
692 :     CGI::br(),
693 :     CGI::br(),
694 :     CGI::submit(-name=>"new_local_set", -value=>"Create a New Set in This Course:",
695 :     -onclick=>$myjs
696 :     ),
697 : jj 2959 " ",
698 : sh002i 2941 CGI::textfield(-name=>"new_set_name",
699 :     -default=>"Name for new set here",
700 :     -override=>1, -size=>30),
701 :     ));
702 : jj 1994
703 : sh002i 2941 print CGI::Tr(CGI::td({-bgcolor=>"black"}));
704 : jj 1994
705 : jj 3402 # Tidy this list up since it is used in two different places
706 :     if ($list_of_local_sets->[0] eq SELECT_SET_STRING) {
707 :     shift @{$list_of_local_sets};
708 :     }
709 :    
710 : sh002i 2941 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"center"},
711 : jj 3397 "Browse ",
712 :     CGI::submit(-name=>"browse_library", -value=>"Problem Library", -style=>$these_widths, $dis1),
713 :     CGI::submit(-name=>"browse_local", -value=>"Local Problems", -style=>$these_widths, $dis2),
714 :     CGI::submit(-name=>"browse_mysets", -value=>"From This Course", -style=>$these_widths, $dis3),
715 : jj 3402 CGI::submit(-name=>"browse_setdefs", -value=>"Set Definition Files", -style=>$these_widths, $dis4),
716 : jj 3397 $libs,
717 :     ));
718 :    
719 : jj 3405 #print CGI::Tr(CGI::td({-bgcolor=>"black"}));
720 :     print CGI::hr();
721 : jj 3397
722 :     if ($browse_which eq 'browse_local') {
723 :     $self->browse_local_panel($library_selected);
724 :     } elsif ($browse_which eq 'browse_mysets') {
725 :     $self->browse_mysets_panel($library_selected, $list_of_local_sets);
726 :     } elsif ($browse_which eq 'browse_library') {
727 :     $self->browse_library_panel();
728 : jj 3402 } elsif ($browse_which eq 'browse_setdefs') {
729 :     $self->browse_setdef_panel($library_selected);
730 : jj 3397 } else { ## handle other problem libraries
731 :     $self->browse_local_panel($library_selected,$browse_which);
732 :     }
733 :    
734 :     print CGI::Tr(CGI::td({-bgcolor=>"black"}));
735 :    
736 :     print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"center"},
737 : sh002i 2941 CGI::start_table({-border=>"0"}),
738 :     CGI::Tr( CGI::td({ -align=>"center"},
739 :     CGI::submit(-name=>"select_all", -style=>$these_widths,
740 :     -value=>"Mark All For Adding"),
741 :     CGI::submit(-name=>"select_none", -style=>$these_widths,
742 :     -value=>"Clear All Marks"),
743 :     )),
744 :     CGI::Tr(CGI::td(
745 :     CGI::submit(-name=>"update", -style=>$these_widths. "; font-weight:bold",
746 : jj 3397 -value=>"Update Set"),
747 : sh002i 2941 CGI::submit(-name=>"rerandomize",
748 :     -style=>$these_widths,
749 :     -value=>"Rerandomize"),
750 :     CGI::submit(-name=>"cleardisplay",
751 :     -style=>$these_widths,
752 :     -value=>"Clear Problem Display")
753 :     )),
754 :     CGI::end_table()));
755 : jj 1994 }
756 :    
757 :     sub make_data_row {
758 : sh002i 2941 my $self = shift;
759 :     my $sourceFileName = shift;
760 :     my $pg = shift;
761 :     my $cnt = shift;
762 :     my $mark = shift || 0;
763 : jj 1994
764 : sh002i 2941 $sourceFileName =~ s|^./||; # clean up top ugliness
765 : jj 2024
766 : sh002i 2941 my $urlpath = $self->r->urlpath;
767 :     my $problem_output = $pg->{flags}->{error_flag} ?
768 :     CGI::div({class=>"ResultsWithError"}, CGI::em("This problem produced an error"))
769 :     : CGI::div({class=>"RenderSolo"}, $pg->{body_text});
770 : jj 1994
771 :    
772 : sh002i 2941 #if($self->{r}->param('browse_which') ne 'browse_library') {
773 :     my $problem_seed = $self->{r}->param('problem_seed') || 0;
774 : jj 3410 my $edit_link = CGI::a({href=>$self->systemLink(
775 :     $urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::PGProblemEditor",
776 :     courseID =>$urlpath->arg("courseID"),
777 :     setID=>"Undefined_Set",
778 :     problemID=>"1"),
779 :     params=>{sourceFilePath => "$sourceFileName", problemSeed=> $problem_seed}
780 :     )}, "Edit it" );
781 : jj 1994
782 : sh002i 2941 my $try_link = CGI::a({href=>$self->systemLink(
783 :     $urlpath->newFromModule("WeBWorK::ContentGenerator::Problem",
784 :     courseID =>$urlpath->arg("courseID"),
785 :     setID=>"Undefined_Set",
786 :     problemID=>"1"),
787 :     params =>{
788 :     effectiveUser => scalar($self->r->param('user')),
789 :     editMode => "SetMaker",
790 :     problemSeed=> $problem_seed,
791 :     sourceFilePath => "$sourceFileName"
792 :     }
793 :     )}, "Try it");
794 : jj 1994
795 : sh002i 2941 my %add_box_data = ( -name=>"trial$cnt",-value=>1,-label=>"Add this problem to the current set on the next update");
796 :     if($mark & SUCCESS) {
797 :     $add_box_data{ -label } .= " (just added this problem)";
798 :     } elsif($mark & ADDED) {
799 :     $add_box_data{ -checked } = 1;
800 :     }
801 : jj 1994
802 : sh002i 2941 print CGI::Tr({-align=>"left"}, CGI::td(
803 :     CGI::div({-style=>"background-color: #DDDDDD; margin: 0px auto"},
804 :     CGI::span({-style=>"float:left ; text-align: left"},"File name: $sourceFileName "),
805 :     CGI::span({-style=>"float:right ; text-align: right"}, $edit_link, " ", $try_link)
806 :     ), CGI::br(),
807 :     CGI::checkbox(-name=>"hideme$cnt",-value=>1,-label=>"Don't show this problem on the next update"),
808 :     CGI::br(),
809 :     CGI::checkbox((%add_box_data)),
810 :     CGI::hidden(-name=>"filetrial$cnt", -default=>[$sourceFileName]).
811 :     CGI::p($problem_output),
812 :     ));
813 : jj 1994 }
814 :    
815 : jj 3431 sub clear_default {
816 :     my $r = shift;
817 :     my $param = shift;
818 :     my $default = shift;
819 :     my $newvalue = $r->param($param) || '';
820 :     $newvalue = '' if($newvalue eq $default);
821 :     $r->param($param, $newvalue);
822 :     }
823 : jj 1994
824 : jj 2115 sub pre_header_initialize {
825 : sh002i 2941 my ($self) = @_;
826 :     my $r = $self->r;
827 :     ## For all cases, lets set some things
828 :     $self->{error}=0;
829 :     my $ce = $r->ce;
830 :     my $db = $r->db;
831 :     my $maxShown = $r->param('max_shown') || MAX_SHOW_DEFAULT;
832 :     $maxShown = 10000000 if($maxShown eq 'All'); # let's hope there aren't more
833 : jj 3424 my $library_basic = $r->param('library_is_basic') || 1;
834 : jj 1994
835 : jj 3424 ## Fix some parameters
836 : jj 3431 clear_default($r,'library_subjects', ALL_SUBJECTS);
837 :     clear_default($r,'library_chapters', ALL_CHAPTERS);
838 :     clear_default($r,'library_sections', ALL_SECTIONS);
839 :     clear_default($r,'library_textbook', ALL_TEXTBOOKS);
840 : jj 3424
841 : sh002i 2941 ## These directories will have individual buttons
842 :     %problib = %{$ce->{courseFiles}{problibs}} if $ce->{courseFiles}{problibs};
843 : jj 2115
844 : sh002i 2941 my $userName = $r->param('user');
845 :     my $user = $db->getUser($userName); # checked
846 :     die "record for user $userName (real user) does not exist."
847 :     unless defined $user;
848 :     my $authz = $r->authz;
849 :     unless ($authz->hasPermissions($userName, "modify_problem_sets")) {
850 :     return(""); # Error message already produced in the body
851 :     }
852 : jj 1994
853 : sh002i 2941 ## Now one action we have to deal with here
854 :     if ($r->param('edit_local')) {
855 :     my $urlpath = $r->urlpath;
856 :     my $db = $r->db;
857 :     my $checkset = $db->getGlobalSet($r->param('local_sets'));
858 :     if (not defined($checkset)) {
859 :     $self->{error} = 1;
860 :     $self->addbadmessage('You need to select a "Target Set" before you can edit it.');
861 :     } else {
862 :     my $page = $urlpath->newFromModule('WeBWorK::ContentGenerator::Instructor::ProblemSetDetail', setID=>$r->param('local_sets'), courseID=>$urlpath->arg("courseID"));
863 :     my $url = $self->systemLink($page);
864 :     $self->reply_with_redirect($url);
865 :     }
866 :     }
867 : jj 1994
868 : sh002i 2941 ## Next, lots of set up so that errors can be reported with message()
869 : jj 2115
870 : sh002i 2941 ############# List of problems we have already printed
871 : jj 1994
872 : sh002i 2941 $self->{past_problems} = get_past_problem_files($r);
873 :     # if we don't end up reusing problems, this will be wiped out
874 :     # if we do redisplay the same problems, we must adjust this accordingly
875 :     my @past_marks = map {$_->[1]} @{$self->{past_problems}};
876 :     my $none_shown = scalar(@{$self->{past_problems}})==0;
877 :     my @pg_files=();
878 :     my $use_previous_problems = 1;
879 :     my $first_shown = $r->param('first_shown') || 0;
880 :     my $last_shown = $r->param('last_shown');
881 :     if (not defined($last_shown)) {
882 :     $last_shown = -1;
883 :     }
884 :     my @all_past_list = (); # these are include requested, but not shown
885 :     my $j = 0;
886 :     while (defined($r->param("all_past_list$j"))) {
887 :     push @all_past_list, $r->param("all_past_list$j");
888 :     $j++;
889 :     }
890 : jj 1994
891 : sh002i 2941 ############# Default of which problem selector to display
892 : jj 1994
893 : sh002i 2941 my $browse_which = $r->param('browse_which') || 'browse_local';
894 : jj 1994
895 : sh002i 2941 my $problem_seed = $r->param('problem_seed') || 0;
896 :     $r->param('problem_seed', $problem_seed); # if it wasn't defined before
897 : jj 1994
898 : sh002i 2941 ## check for problem lib buttons
899 :     my $browse_lib = '';
900 :     foreach my $lib (keys %problib) {
901 :     if ($r->param("browse_$lib")) {
902 :     $browse_lib = "browse_$lib";
903 :     last;
904 :     }
905 :     }
906 : dpvc 2431
907 : sh002i 2941 ########### Start the logic through if elsif elsif ...
908 : jj 1994
909 : sh002i 2941 ##### Asked to browse certain problems
910 :     if ($browse_lib ne '') {
911 :     $browse_which = $browse_lib;
912 :     $r->param('library_sets', "");
913 :     $use_previous_problems = 0; @pg_files = (); ## clear old problems
914 :     } elsif ($r->param('browse_library')) {
915 :     $browse_which = 'browse_library';
916 :     $r->param('library_sets', "");
917 :     $use_previous_problems = 0; @pg_files = (); ## clear old problems
918 :     } elsif ($r->param('browse_local')) {
919 :     $browse_which = 'browse_local';
920 :     $r->param('library_sets', "");
921 :     $use_previous_problems = 0; @pg_files = (); ## clear old problems
922 :     } elsif ($r->param('browse_mysets')) {
923 :     $browse_which = 'browse_mysets';
924 :     $r->param('library_sets', "");
925 :     $use_previous_problems = 0; @pg_files = (); ## clear old problems
926 : jj 3402 } elsif ($r->param('browse_setdefs')) {
927 :     $browse_which = 'browse_setdefs';
928 :     $r->param('library_sets', "");
929 :     $use_previous_problems = 0; @pg_files = (); ## clear old problems
930 : jj 1994
931 : sh002i 2941 ##### Change the seed value
932 : jj 1994
933 : sh002i 2941 } elsif ($r->param('rerandomize')) {
934 :     $problem_seed++;
935 :     $r->param('problem_seed', $problem_seed);
936 :     $self->addbadmessage('Changing the problem seed for display, but there are no problems showing.') if $none_shown;
937 : jj 1994
938 : sh002i 2941 ##### Clear the display
939 : jj 1994
940 : sh002i 2941 } elsif ($r->param('cleardisplay')) {
941 :     @pg_files = ();
942 :     $use_previous_problems=0;
943 :     $self->addbadmessage('The display was already cleared.') if $none_shown;
944 : jj 1994
945 : sh002i 2941 ##### View problems selected from the local list
946 : jj 1994
947 : sh002i 2941 } elsif ($r->param('view_local_set')) {
948 : jj 1994
949 : sh002i 2941 my $set_to_display = $r->param('library_sets');
950 :     if (not defined($set_to_display) or $set_to_display eq SELECT_LOCAL_STRING or $set_to_display eq "Found no directories containing problems") {
951 :     $self->addbadmessage('You need to select a set to view.');
952 :     } else {
953 :     $set_to_display = '.' if $set_to_display eq MY_PROBLEMS;
954 :     $set_to_display = substr($browse_which,7) if $set_to_display eq MAIN_PROBLEMS;
955 :     @pg_files = list_pg_files($ce->{courseDirs}->{templates},
956 : jj 1994 "$set_to_display");
957 : sh002i 2941 $use_previous_problems=0;
958 :     }
959 : jj 1994
960 : sh002i 2941 ##### View problems selected from the a set in this course
961 : jj 1994
962 : sh002i 2941 } elsif ($r->param('view_mysets_set')) {
963 : jj 1994
964 : sh002i 2941 my $set_to_display = $r->param('library_sets');
965 :     if (not defined($set_to_display)
966 : sh002i 3357 or $set_to_display eq "Select a Homework Set"
967 : sh002i 2941 or $set_to_display eq NO_LOCAL_SET_STRING) {
968 :     $self->addbadmessage("You need to select a set from this course to view.");
969 :     } else {
970 :     my @problemList = $db->listGlobalProblems($set_to_display);
971 :     my $problem;
972 :     @pg_files=();
973 :     for $problem (@problemList) {
974 : jj 3402 my $problemRecord = $db->getGlobalProblem($set_to_display, $problem); # checked
975 :     die "global $problem for set $set_to_display not found." unless
976 :     $problemRecord;
977 :     push @pg_files, $problemRecord->source_file;
978 : jj 1994
979 : sh002i 2941 }
980 :     $use_previous_problems=0;
981 :     }
982 : jj 1994
983 : jj 3424 ##### View from the library database
984 : jj 1994
985 : sh002i 2941 } elsif ($r->param('lib_view')) {
986 : jj 1994
987 : sh002i 2941 @pg_files=();
988 : jj 3424 my @dbsearch = WeBWorK::Utils::ListingDB::getSectionListings($r);
989 : sh002i 2941 my ($result, $tolibpath);
990 :     for $result (@dbsearch) {
991 :     $tolibpath = "Library/$result->{path}/$result->{filename}";
992 :    
993 :     ## Too clunky!!!!
994 :     push @pg_files, $tolibpath;
995 :     }
996 :     $use_previous_problems=0;
997 : jj 1994
998 : jj 3402 ##### View a set from a set*.def
999 :    
1000 :     } elsif ($r->param('view_setdef_set')) {
1001 :    
1002 :     my $set_to_display = $r->param('library_sets');
1003 :     if (not defined($set_to_display)
1004 :     or $set_to_display eq "Select a Set Definition File"
1005 :     or $set_to_display eq NO_LOCAL_SET_STRING) {
1006 :     $self->addbadmessage("You need to select a set from this course to view.");
1007 :     } else {
1008 :     @pg_files= $self->read_set_def($set_to_display);
1009 :     }
1010 :     $use_previous_problems=0;
1011 :    
1012 : sh002i 2941 ##### Edit the current local problem set
1013 : jj 1994
1014 : sh002i 2941 } elsif ($r->param('edit_local')) { ## Jump to set edit page
1015 : jj 1994
1016 : sh002i 2941 ; # already handled
1017 : jj 2115
1018 :    
1019 : sh002i 2941 ##### Make a new local problem set
1020 : jj 1994
1021 : sh002i 2941 } elsif ($r->param('new_local_set')) {
1022 : jj 3399 if ($r->param('new_set_name') !~ /^[\w .-]*$/) {
1023 : jj 2959 $self->addbadmessage("The name ".$r->param('new_set_name')." is not a valid set name. Use only letters, digits, -, _, and .");
1024 : sh002i 2941 } else {
1025 :     my $newSetName = $r->param('new_set_name');
1026 : jj 2960 # if we want to munge the input set name, do it here
1027 : jj 3399 $newSetName =~ s/\s/_/g;
1028 : sh002i 2941 $r->param('local_sets',$newSetName);
1029 :     my $newSetRecord = $db->getGlobalSet($newSetName);
1030 :     if (defined($newSetRecord)) {
1031 : jj 2959 $self->addbadmessage("The set name $newSetName is already in use. Pick a different name if you would like to start a new set.");
1032 : sh002i 2941 } else { # Do it!
1033 : jj 3399 $newSetRecord = $db->{set}->{record}->new();
1034 :     $newSetRecord->set_id($newSetName);
1035 :     $newSetRecord->set_header("");
1036 :     $newSetRecord->hardcopy_header("");
1037 :     $newSetRecord->open_date(time()+60*60*24*7); # in one week
1038 :     $newSetRecord->due_date(time()+60*60*24*7*2); # in two weeks
1039 :     $newSetRecord->answer_date(time()+60*60*24*7*3); # in three weeks
1040 :     eval {$db->addGlobalSet($newSetRecord)};
1041 :     if ($@) {
1042 :     $self->addbadmessage("Problem creating set $newSetName<br> $@");
1043 :     } else {
1044 :     $self->addgoodmessage("Set $newSetName has been created.");
1045 :     my $selfassign = $r->param('selfassign') || "";
1046 :     $selfassign = "" if($selfassign =~ /false/i); # deal with javascript false
1047 :     if($selfassign) {
1048 :     $self->assignSetToUser($userName, $newSetRecord);
1049 :     $self->addgoodmessage("Set $newSetName was assigned to $userName.");
1050 :     }
1051 : sh002i 2941 }
1052 :     }
1053 :     }
1054 : jj 1994
1055 : sh002i 2941 ##### Add selected problems to the current local set
1056 : jj 1994
1057 : sh002i 2941 } elsif ($r->param('update')) {
1058 :     ## first handle problems to be added before we hide them
1059 :     my($localSet, @selected);
1060 : jj 1994
1061 : sh002i 2941 @pg_files = grep {($_->[1] & ADDED) != 0 } @{$self->{past_problems}};
1062 :     @selected = map {$_->[0]} @pg_files;
1063 : jj 1994
1064 : sh002i 2941 my @action_files = grep {$_->[1] > 0 } @{$self->{past_problems}};
1065 :     # There are now good reasons to do an update without selecting anything.
1066 :     #if(scalar(@action_files) == 0) {
1067 :     # $self->addbadmessage('Update requested, but no problems were marked.');
1068 :     #}
1069 : jj 2116
1070 : sh002i 2941 if (scalar(@selected)>0) { # if some are to be added, they need a place to go
1071 :     $localSet = $r->param('local_sets');
1072 :     if (not defined($localSet) or
1073 :     $localSet eq SELECT_SET_STRING or
1074 :     $localSet eq NO_LOCAL_SET_STRING) {
1075 : jj 2264 $self->addbadmessage('You are trying to add problems to something, but you did not select a "Target Set" name as a target.');
1076 : sh002i 2941 } else {
1077 : jj 2959 my $newSetRecord = $db->getGlobalSet($localSet);
1078 : jj 1994 if (not defined($newSetRecord)) {
1079 : jj 2959 $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.");
1080 : jj 1994 } else {
1081 : sh002i 2941 my $addcount = add_selected($self, $db, $localSet);
1082 :     if($addcount > 0) {
1083 :     $self->addgoodmessage("Added $addcount problem".(($addcount>1)?'s':'').
1084 :     " to $localSet.");
1085 :     }
1086 : jj 1994 }
1087 : sh002i 2941 }
1088 :     }
1089 :     ## now handle problems to be hidden
1090 : jj 1994
1091 : sh002i 2941 ## only keep the ones which are not hidden
1092 :     @pg_files = grep {($_->[1] & HIDDEN) ==0 } @{$self->{past_problems}};
1093 :     @past_marks = map {$_->[1]} @pg_files;
1094 :     @pg_files = map {$_->[0]} @pg_files;
1095 :     @all_past_list = (@all_past_list[0..($first_shown-1)],
1096 :     @pg_files,
1097 :     @all_past_list[($last_shown+1)..(scalar(@all_past_list)-1)]);
1098 :     $last_shown = $first_shown+$maxShown -1;
1099 :     $last_shown = (scalar(@all_past_list)-1) if($last_shown>=scalar(@all_past_list));
1100 : jj 1994
1101 : sh002i 2941 } elsif ($r->param('next_page')) {
1102 :     $first_shown = $last_shown+1;
1103 :     $last_shown = $first_shown+$maxShown-1;
1104 :     $last_shown = (scalar(@all_past_list)-1) if($last_shown>=scalar(@all_past_list));
1105 :     @past_marks = ();
1106 :     } elsif ($r->param('prev_page')) {
1107 :     $last_shown = $first_shown-1;
1108 :     $first_shown = $last_shown - $maxShown+1;
1109 : jj 1994
1110 : sh002i 2941 $first_shown = 0 if($first_shown<0);
1111 :     @past_marks = ();
1112 : jj 1994
1113 : sh002i 2941 } elsif ($r->param('select_all')) {
1114 :     @past_marks = map {1} @past_marks;
1115 : jj 3424 } elsif ($r->param('library_basic')) {
1116 :     $library_basic = 1;
1117 :     } elsif ($r->param('library_advanced')) {
1118 :     $library_basic = 2;
1119 : sh002i 2941 } elsif ($r->param('select_none')) {
1120 :     @past_marks = ();
1121 : jj 2230
1122 : sh002i 2941 ##### No action requested, probably our first time here
1123 : jj 1994
1124 : sh002i 2941 } else {
1125 :     #my $c = $r->connection;
1126 :     #print "Debug info: ". $r->get_remote_host ."<p>". $c->remote_ip ;
1127 :     ;
1128 :     } ##### end of the if elsif ...
1129 : jj 1994
1130 :    
1131 : sh002i 2941 ############# List of local sets
1132 : jj 1994
1133 : jj 3402 my @all_db_sets = $db->listGlobalSets;
1134 :     @all_db_sets = sortByName(undef, @all_db_sets);
1135 : jj 1994
1136 : sh002i 2941 if ($use_previous_problems) {
1137 :     @pg_files = @all_past_list;
1138 :     } else {
1139 :     $first_shown = 0;
1140 :     $last_shown = scalar(@pg_files)<$maxShown ? scalar(@pg_files) : $maxShown;
1141 :     $last_shown--; # to make it an array index
1142 :     @past_marks = ();
1143 :     }
1144 :     ############# Now store data in self for retreival by body
1145 :     $self->{first_shown} = $first_shown;
1146 :     $self->{last_shown} = $last_shown;
1147 :     $self->{browse_which} = $browse_which;
1148 :     $self->{problem_seed} = $problem_seed;
1149 :     $self->{pg_files} = \@pg_files;
1150 :     $self->{past_marks} = \@past_marks;
1151 : jj 3402 $self->{all_db_sets} = \@all_db_sets;
1152 : jj 3424 $self->{library_basic} = $library_basic;
1153 : jj 2115 }
1154 :    
1155 :    
1156 :     sub title {
1157 : jj 3397 return "Library Browser";
1158 : jj 2115 }
1159 :    
1160 :     sub body {
1161 : sh002i 2941 my ($self) = @_;
1162 : jj 2115
1163 : sh002i 2941 my $r = $self->r;
1164 :     my $ce = $r->ce; # course environment
1165 :     my $db = $r->db; # database
1166 :     my $j; # garden variety counter
1167 : jj 2115
1168 : sh002i 2941 my $userName = $r->param('user');
1169 : jj 2115
1170 : sh002i 2941 my $user = $db->getUser($userName); # checked
1171 :     die "record for user $userName (real user) does not exist."
1172 :     unless defined $user;
1173 : jj 2115
1174 : sh002i 2941 ### Check that this is a professor
1175 :     my $authz = $r->authz;
1176 :     unless ($authz->hasPermissions($userName, "modify_problem_sets")) {
1177 :     print "User $userName returned " .
1178 :     $authz->hasPermissions($user, "modify_problem_sets") .
1179 : jj 2115 " for permission";
1180 : sh002i 2941 return(CGI::div({class=>'ResultsWithError'},
1181 :     CGI::em("You are not authorized to access the Instructor tools.")));
1182 :     }
1183 : jj 2115
1184 : sh002i 2941 ########## Extract information computed in pre_header_initialize
1185 : jj 2115
1186 : sh002i 2941 my $first_shown = $self->{first_shown};
1187 : jj 2959 my $last_shown = $self->{last_shown};
1188 : sh002i 2941 my $browse_which = $self->{browse_which};
1189 :     my $problem_seed = $self->{problem_seed};
1190 :     my @pg_files = @{$self->{pg_files}};
1191 : jj 3402 my @all_db_sets = @{$self->{all_db_sets}};
1192 : jj 2115
1193 : sh002i 2941 my @pg_html=($last_shown>=$first_shown) ?
1194 :     renderProblems(r=> $r,
1195 :     user => $user,
1196 :     problem_list => [@pg_files[$first_shown..$last_shown]],
1197 :     displayMode => $r->param('mydisplayMode')) : ();
1198 : jj 1994
1199 : sh002i 2941 ########## Top part
1200 :     print CGI::startform({-method=>"POST", -action=>$r->uri, -name=>'mainform'}),
1201 :     $self->hidden_authen_fields,
1202 :     '<div align="center">',
1203 : jj 1994 CGI::start_table({-border=>2});
1204 : jj 3402 $self->make_top_row('all_db_sets'=>\@all_db_sets,
1205 : sh002i 2941 'browse_which'=> $browse_which);
1206 :     print CGI::hidden(-name=>'browse_which', -default=>[$browse_which]),
1207 :     CGI::hidden(-name=>'problem_seed', -default=>[$problem_seed]);
1208 :     for ($j = 0 ; $j < scalar(@pg_files) ; $j++) {
1209 :     print CGI::hidden(-name=>"all_past_list$j", -default=>$pg_files[$j]);
1210 :     }
1211 : jj 1994
1212 : sh002i 2941 print CGI::hidden(-name=>'first_shown', -default=>[$first_shown]);
1213 :     print CGI::hidden(-name=>'last_shown', -default=>[$last_shown]);
1214 : jj 1994
1215 :    
1216 : sh002i 2941 ########## Now print problems
1217 :     my $jj;
1218 :     for ($jj=0; $jj<scalar(@pg_html); $jj++) {
1219 :     $pg_files[$jj] =~ s|^$ce->{courseDirs}->{templates}/?||;
1220 :     $self->make_data_row($pg_files[$jj+$first_shown], $pg_html[$jj], $jj+1, $self->{past_marks}->[$jj]);
1221 :     }
1222 : jj 1994
1223 : sh002i 2941 ########## Finish things off
1224 :     print CGI::end_table();
1225 :     print '</div>';
1226 :     # if($first_shown>0 or (1+$last_shown)<scalar(@pg_files)) {
1227 :     my ($next_button, $prev_button) = ("", "");
1228 :     if ($first_shown > 0) {
1229 :     $prev_button = CGI::submit(-name=>"prev_page", -style=>"width:15ex",
1230 :     -value=>"Previous page");
1231 :     }
1232 :     if ((1+$last_shown)<scalar(@pg_files)) {
1233 :     $next_button = CGI::submit(-name=>"next_page", -style=>"width:15ex",
1234 :     -value=>"Next page");
1235 :     }
1236 :     if (scalar(@pg_files)>0) {
1237 :     print CGI::p(($first_shown+1)."-".($last_shown+1)." of ".scalar(@pg_files).
1238 : jj 1994 " shown.", $prev_button, " ", $next_button);
1239 : sh002i 2941 }
1240 :     # }
1241 :     print CGI::endform(), "\n";
1242 : jj 1994
1243 : sh002i 2941 return "";
1244 : jj 1994 }
1245 :    
1246 :     =head1 AUTHOR
1247 :    
1248 :     Written by John Jones, jj (at) asu.edu.
1249 :    
1250 :     =cut
1251 :    
1252 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9