[system] / branches / rel-2-3-dev / webwork-modperl / lib / WeBWorK / ContentGenerator / Instructor / SetMaker.pm Repository:
ViewVC logotype

Annotation of /branches/rel-2-3-dev/webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3475 - (view) (download) (as text)
Original Path: trunk/webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9