[system] / trunk / webwork-modperl / lib / WeBWorK / ContentGenerator / Instructor / SetMaker.pm Repository:
ViewVC logotype

Diff of /trunk/webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.2450  
changed lines
  Added in v.4207

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9