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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9