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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9