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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9