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

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

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

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

Legend:
Removed from v.2431  
changed lines
  Added in v.4735

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9