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

Annotation of /trunk/webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9