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

Annotation of /branches/rel-2-4-patches/webwork2/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5318 - (view) (download) (as text)
Original Path: branches/rel-2-4-dev/webwork2/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9