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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9