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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : jj 1994 ################################################################################
2 :     # WeBWorK Online Homework Delivery System
3 :     # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/
4 : jj 2152 # $CVSHeader: webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm,v 1.13 2004/05/22 00:38:44 sh002i Exp $
5 : jj 1994 #
6 :     # This program is free software; you can redistribute it and/or modify it under
7 :     # the terms of either: (a) the GNU General Public License as published by the
8 :     # Free Software Foundation; either version 2, or (at your option) any later
9 :     # version, or (b) the "Artistic License" which comes with this package.
10 :     #
11 :     # This program is distributed in the hope that it will be useful, but WITHOUT
12 :     # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 :     # FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the
14 :     # Artistic License for more details.
15 :     ################################################################################
16 :    
17 :    
18 :     package WeBWorK::ContentGenerator::Instructor::SetMaker;
19 :     use base qw(WeBWorK::ContentGenerator::Instructor);
20 :    
21 :     =head1 NAME
22 :    
23 :     WeBWorK::ContentGenerator::Instructor::SetMaker - Make problem sets.
24 :    
25 :     =cut
26 :    
27 :     use strict;
28 :     use warnings;
29 :    
30 :     use CGI::Pretty qw();
31 :     use WeBWorK::Form;
32 :     use WeBWorK::Utils qw(readDirectory max);
33 :     use WeBWorK::Utils::Tasks qw(renderProblems);
34 :    
35 :     require WeBWorK::Utils::ListingDB;
36 :    
37 :     use constant MAX_SHOW => 20;
38 : jj 2152 use constant NO_LOCAL_SET_STRING => 'There are no local sets yet';
39 :     use constant SELECT_SET_STRING => 'Select a Set for This Course';
40 :     use constant SELECT_LOCAL_STRING => 'Select a Local Problem Collection';
41 : jj 1994
42 : jj 2116 ## Flags for operations on files
43 :    
44 :     use constant ADDED => 1;
45 :     use constant HIDDEN => (1 << 1);
46 :     use constant SUCCESS => (1 << 2);
47 :    
48 : jj 2039 ## This is for searching the disk for directories containing pg files.
49 : jj 1994 ## to make the recursion work, this returns an array where the first
50 :     ## item is 1 or 0 depending on whether or not the current
51 :     ## directory has any pg files. The second is a list of directories
52 :     ## which contain pg files.
53 :     sub get_library_sets {
54 : jj 2024 my $amtop = shift;
55 : jj 1994 my $topdir = shift;
56 :     my @lis = readDirectory($topdir);
57 :     my @pgs = grep { m/\.pg$/ and (not m/Header\.pg/) and -f "$topdir/$_"} @lis;
58 :     my $havepg = scalar(@pgs)>0 ? 1 : 0;
59 :     my @mdirs = grep {$_ ne "." and $_ ne ".." and $_ ne "Library"
60 :     and -d "$topdir/$_"} @lis;
61 : jj 2024 if($amtop) { # we don't want the library
62 :     @mdirs = grep {$_ ne "Library"} @mdirs;
63 :     }
64 : jj 1994 my ($adir, @results, @thisresult);
65 :     for $adir (@mdirs) {
66 : jj 2024 @results = get_library_sets(0, "$topdir/$adir");
67 : jj 1994 my $isadirok = shift @results;
68 :     @thisresult = (@thisresult, @results);
69 :     if ($isadirok) {
70 :     @thisresult = ("$topdir/$adir", @thisresult);
71 :     }
72 :     }
73 :     return(($havepg, @thisresult));
74 :     }
75 :    
76 :     ## List all the pg files in the requested directory
77 :     sub list_pg_files {
78 :     my $templatedir = shift;
79 :     my $topdir = shift;
80 :    
81 :     my @lis = readDirectory("$templatedir/$topdir");
82 :     my @pgs = grep { m/\.pg$/ and (not m/Header\.pg/) and -f "$templatedir/$topdir/$_"} @lis;
83 :     @pgs = map { "$topdir/$_" } @pgs;
84 :     return(@pgs);
85 :     }
86 :    
87 :     ## go through past page getting a list of identifiers for the problems
88 :     ## and whether or not they are selected, and whether or not they should
89 :     ## be hidden
90 :    
91 :     sub get_past_problem_files {
92 :     my $r = shift;
93 :     my @found=();
94 :     my $count =1;
95 :     while (defined($r->param("filetrial$count"))) {
96 : jj 2116 my $val = 0;
97 :     $val |= ADDED if($r->param("trial$count"));
98 :     $val |= HIDDEN if($r->param("hideme$count"));
99 :     push @found, [$r->param("filetrial$count"), $val];
100 : jj 1994 $count++;
101 :     }
102 : jj 2116 return(\@found);
103 : jj 1994 }
104 :    
105 :     #### For adding new problems
106 :    
107 :     sub add_selected {
108 :     my $self = shift;
109 :     my $db = shift;
110 :     my $setName = shift;
111 : jj 2116 my @past_problems = @{$self->{past_problems}};
112 :     my @selected = @past_problems;
113 : jj 1994 my (@path, $file, $selected, $freeProblemID);
114 :     $freeProblemID = max($db->listGlobalProblems($setName)) + 1;
115 : jj 2116 my $addedcount=0;
116 : jj 1994
117 :     for $selected (@selected) {
118 : jj 2116 if($selected->[1] & ADDED) {
119 :     $file = $selected->[0];
120 :     @path = split "/", $selected->[0];
121 :     pop @path; # Remove the file name from the path
122 :     shift @path if $path[0] eq ""; # remove the null element from the begining
123 :     my $problemRecord = $db->newGlobalProblem();
124 :     $problemRecord->problem_id($freeProblemID++);
125 :     $problemRecord->set_id($setName);
126 :     $problemRecord->source_file($file);
127 :     $problemRecord->value("1");
128 :     $problemRecord->max_attempts("-1");
129 :     $db->addGlobalProblem($problemRecord);
130 :     $self->assignProblemToAllSetUsers($problemRecord);
131 :     $selected->[1] &= SUCCESS;
132 :     $addedcount++;
133 :     }
134 : jj 1994 }
135 : jj 2116 return($addedcount);
136 : jj 1994 }
137 :    
138 :    
139 : jj 2039 ############# List of sets of problems in templates directory
140 : jj 1994
141 : jj 2115 sub get_problem_directories {
142 : jj 1994 my $ce = shift;
143 : jj 2115 my @all_problem_directories = get_library_sets(1, $ce->{courseDirs}->{templates});
144 :     my $includetop = shift @all_problem_directories;
145 : jj 1994 my $j;
146 : jj 2115 for ($j=0; $j<scalar(@all_problem_directories); $j++) {
147 :     $all_problem_directories[$j] =~ s|^$ce->{courseDirs}->{templates}/?||;
148 : jj 1994 }
149 : jj 2115 @all_problem_directories = sort @all_problem_directories;
150 :     unshift @all_problem_directories, ' -- Top -- ' if($includetop);
151 :     return (\@all_problem_directories);
152 : jj 1994 }
153 :    
154 :     ### The browsing panel has three versions
155 :     ##### Version 1 is local problems
156 :     sub browse_local_panel {
157 :     my $self = shift;
158 :     my $library_selected = shift;
159 :    
160 : jj 2115 my $list_of_prob_dirs= get_problem_directories($self->r->ce);
161 :     if(scalar(@$list_of_prob_dirs) == 0) {
162 :     $library_selected = "Found no directories containing problems";
163 :     unshift @{$list_of_prob_dirs}, $library_selected;
164 :     } else {
165 : jj 2152 my $default_value = SELECT_LOCAL_STRING;
166 : jj 2115 if (not $library_selected or $library_selected eq $default_value) {
167 :     unshift @{$list_of_prob_dirs}, $default_value;
168 :     $library_selected = $default_value;
169 :     }
170 : jj 1994 }
171 :    
172 :     print CGI::Tr(CGI::td({-class=>"InfoPanel"}, "Local Problems: ",
173 :     CGI::popup_menu(-name=> 'library_sets',
174 : jj 2115 -values=>$list_of_prob_dirs,
175 : jj 1994 -default=> $library_selected),
176 :     CGI::br(),
177 :     CGI::submit(-name=>"view_local_set", -value=>"View Problems"),
178 :     ));
179 :     }
180 :    
181 :     ##### Version 2 is local problem sets
182 :     sub browse_mysets_panel {
183 :     my $self = shift;
184 :     my $library_selected = shift;
185 :     my $list_of_local_sets = shift;
186 :     my $default_value = "Select a Problem Set";
187 :    
188 : jj 2024 if(scalar(@$list_of_local_sets) == 0) {
189 : jj 2152 $list_of_local_sets = [NO_LOCAL_SET_STRING];
190 : jj 2024 } elsif (not $library_selected or $library_selected eq $default_value) {
191 : jj 1994 unshift @{$list_of_local_sets}, $default_value;
192 :     $library_selected = $default_value;
193 :     }
194 :    
195 :     print CGI::Tr(CGI::td({-class=>"InfoPanel"}, "Browse from: ",
196 :     CGI::popup_menu(-name=> 'library_sets',
197 :     -values=>$list_of_local_sets,
198 :     -default=> $library_selected),
199 :     CGI::br(),
200 :     CGI::submit(-name=>"view_mysets_set", -value=>"View This Set"),
201 :     ));
202 :     }
203 :    
204 :     ##### Version 3 is the problem library
205 :    
206 :    
207 :     # There a different levels, and you can pick a new chapter,
208 :     # pick a new section, pick all from chapter, pick all from section
209 :     #
210 :     # Incoming data - current chapter, current section
211 :     sub browse_library_panel {
212 :     my $self = shift;
213 :     my $r = $self->r;
214 : jj 2039 my $ce = $r->ce;
215 : jj 1994
216 : jj 2140 my $libraryRoot = $r->{ce}->{problemLibrary}->{root};
217 : jj 1994
218 :     unless($libraryRoot) {
219 :     print CGI::Tr(CGI::td(CGI::div({class=>'ResultsWithError', align=>"center"},
220 :     "The problem library has not been installed.")));
221 :     return;
222 :     }
223 : jj 2039 # Test if the Library directory exists. If not, try to make it
224 :     unless(-d "$ce->{courseDirs}->{templates}/Library") {
225 :     unless(symlink($libraryRoot, "$ce->{courseDirs}->{templates}/Library")) {
226 : jj 2115 my $msg = <<"HERE";
227 : jj 2039 You are missing the directory <code>templates/Library</code>, which is needed
228 :     for the Problem Library to function. It should be a link pointing to
229 :     <code>$libraryRoot</code>, which you set in <code>conf/global.conf</code>.
230 :     I tried to make the link for you, but that failed. Check the permissions
231 :     in your <code>templates</code> directory.
232 :     HERE
233 : jj 2152 $self->addbadmessage($msg);
234 : jj 2039 }
235 :     }
236 : jj 1994
237 :     my $default_chap = "All Chapters";
238 :     my $default_sect = "All Sections";
239 :    
240 :     my @chaps = WeBWorK::Utils::ListingDB::getAllChapters($r->{ce});
241 :     unshift @chaps, $default_chap;
242 :     my $chapter_selected = $r->param('library_chapters') || $default_chap;
243 :    
244 :     my @sects=();
245 :     if ($chapter_selected ne $default_chap) {
246 :     @sects = WeBWorK::Utils::ListingDB::getAllSections($r->{ce}, $chapter_selected);
247 :     }
248 :    
249 :     my @textbooks = ('Textbook info not ready');
250 :    
251 :     unshift @sects, $default_sect;
252 :     my $section_selected = $r->param('library_sections') || $default_sect;
253 :    
254 :     print CGI::Tr(CGI::td({-class=>"InfoPanel"},
255 :     CGI::start_table(),
256 :     CGI::Tr(
257 :     CGI::td(["Chapter:",
258 :     CGI::popup_menu(-name=> 'library_chapters',
259 :     -values=>\@chaps,
260 :     -default=> $chapter_selected,
261 :     -onchange=>"submit();return true"
262 :     ),
263 :     CGI::submit(-name=>"lib_select_chapter", -value=>"Update Section List")])),
264 :     CGI::Tr(
265 :     CGI::td("Section:"),
266 :     CGI::td({-colspan=>2},
267 :     CGI::popup_menu(-name=> 'library_sections',
268 :     -values=>\@sects,
269 :     -default=> $section_selected
270 :     ))),
271 :    
272 : jj 2015 # CGI::Tr(
273 :     # CGI::td("Textbook:"),
274 :     # CGI::td({-colspan=>2},
275 :     # CGI::popup_menu(-name=> 'library_textbooks',
276 :     # -values=>\@textbooks,
277 :     # # -default=> $section_selected
278 :     # ))),
279 : jj 1994
280 : jj 2015 # CGI::Tr(
281 :     # CGI::td("Keywords:"),
282 :     # CGI::td({-colspan=>2}, CGI::textfield(-name=>"keywords",
283 :     # -default=>"Keywords not implemented yet",
284 :     # -override=>1, -size=>60))),
285 : jj 1994 CGI::Tr(CGI::td({-colspan=>3},CGI::submit(-name=>"lib_view", -value=>"View Problems"))),
286 :     CGI::end_table(),
287 :     ));
288 :     }
289 :    
290 :     sub make_top_row {
291 :     my $self = shift;
292 :     my $r = $self->r;
293 :     my %data = @_;
294 :    
295 :     my $list_of_local_sets = $data{all_set_defs};
296 : jj 2024 my $have_local_sets = scalar(@$list_of_local_sets);
297 : jj 1994 my $browse_which = $data{browse_which};
298 :     my $library_selected = $r->param('library_sets');
299 :     my $set_selected = $r->param('local_sets');
300 :    
301 :     my ($dis1, $dis2, $dis3) = ("","","");
302 :     $dis1 = '-disabled' if($browse_which eq 'browse_library');
303 :     $dis2 = '-disabled' if($browse_which eq 'browse_local');
304 :     $dis3 = '-disabled' if($browse_which eq 'browse_mysets');
305 :    
306 :     my $these_widths = "width: 27ex";
307 :     print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"center"},
308 :     CGI::submit(-name=>"browse_library", -value=>"Browse Problem Library", -style=>$these_widths, $dis1),
309 :     CGI::submit(-name=>"browse_local", -value=>"Browse Local Problems", -style=>$these_widths, $dis2),
310 :     CGI::submit(-name=>"browse_mysets", -value=>"Browse From This Course", -style=>$these_widths, $dis3),
311 :     ));
312 :    
313 :     print CGI::Tr(CGI::td({-bgcolor=>"black"}));
314 :    
315 :     if ($browse_which eq 'browse_local') {
316 : jj 2024 $self->browse_local_panel($library_selected);
317 : jj 1994 } elsif ($browse_which eq 'browse_mysets') {
318 : jj 2024 $self->browse_mysets_panel($library_selected, $list_of_local_sets);
319 : jj 1994 } else {
320 : jj 2024 $self->browse_library_panel();
321 : jj 1994 }
322 :    
323 :     print CGI::Tr(CGI::td({-bgcolor=>"black"}));
324 :    
325 : jj 2024 if($have_local_sets ==0) {
326 : jj 2152 $list_of_local_sets = [NO_LOCAL_SET_STRING];
327 :     } elsif (not $set_selected or $set_selected eq SELECT_SET_STRING) {
328 : jj 1994 if ($list_of_local_sets->[0] eq "Select a Problem Set") {
329 :     shift @{$list_of_local_sets};
330 :     }
331 : jj 2152 unshift @{$list_of_local_sets}, SELECT_SET_STRING;
332 :     $set_selected = SELECT_SET_STRING;
333 : jj 1994 }
334 :    
335 : jj 2152 print CGI::Tr(CGI::td({-class=>"InfoPanel"}, "Adding Problems to ",
336 :     CGI::b("Current Set: "),
337 : jj 1994 CGI::popup_menu(-name=> 'local_sets',
338 :     -values=>$list_of_local_sets,
339 :     -default=> $set_selected),
340 :     CGI::submit(-name=>"edit_local", -value=>"Edit Current Set"),
341 :     CGI::br(),
342 :     CGI::br(),
343 : jj 2152 CGI::submit(-name=>"new_local_set", -value=>"Create a New Set in This Course:"),
344 : jj 1994 " ",
345 :     CGI::textfield(-name=>"new_set_name",
346 :     -default=>"Name for new set here",
347 :     -override=>1, -size=>30),
348 :     CGI::br(),
349 :     ));
350 :    
351 :     print CGI::Tr(CGI::td({-bgcolor=>"black"}));
352 :    
353 :     print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"center"},
354 :     CGI::submit(-name=>"update", -style=>$these_widths,
355 :     -value=>"Act on Marked Problems"),
356 :     CGI::submit(-name=>"rerandomize",
357 :     -style=>$these_widths,
358 :     -value=>"Rerandomize"),
359 :     CGI::submit(-name=>"cleardisplay",
360 :     -style=>$these_widths,
361 :     -value=>"Clear Problem Display")));
362 :    
363 :     }
364 :    
365 :     sub make_data_row {
366 :     my $self = shift;
367 :     my $sourceFileName = shift;
368 :     my $pg = shift;
369 :     my $cnt = shift;
370 :    
371 : jj 2024 $sourceFileName =~ s|^./||; # clean up top ugliness
372 :    
373 : jj 1994 my $urlpath = $self->r->urlpath;
374 :     my $problem_output = $pg->{flags}->{error_flag} ?
375 : jj 2015 CGI::em("This problem produced an error")
376 :     : CGI::div({class=>"RenderSolo"}, $pg->{body_text});
377 : jj 1994
378 :    
379 : jj 2024 my $edit_link = '';
380 : jj 2040 #if($self->{r}->param('browse_which') ne 'browse_library') {
381 :     if($sourceFileName !~ /^Library\//) {
382 : jj 2039 $edit_link = CGI::a({href=>$self->systemLink($urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::PGProblemEditor",
383 :     courseID =>$urlpath->arg("courseID"),
384 :     setID=>"Undefined_Set",
385 :     problemID=>"1"),
386 :     params=>{sourceFilePath => "$sourceFileName"}
387 : jj 1994 )}, "Edit it" );
388 : jj 2024 }
389 : jj 1994
390 : jj 2039 my $try_link = CGI::a({href=>$self->systemLink($urlpath->newFromModule("WeBWorK::ContentGenerator::Problem",
391 :     courseID =>$urlpath->arg("courseID"),
392 :     setID=>"Undefined_Set",
393 :     problemID=>"1"),
394 : jj 2015 params =>{effectiveUser => $self->r->param('user'),
395 : jj 2021 editMode => "SetMaker",
396 : jj 2015 sourceFilePath => "$sourceFileName"} )}, "Try it");
397 : jj 1994
398 :    
399 :    
400 :     print CGI::Tr({-align=>"left"}, CGI::td(
401 :    
402 : jj 2024 CGI::div({-style=>"background-color: #DDDDDD; margin: 0px auto"},
403 :     CGI::span({-style=>"float:left ; text-align: left"},"File name: $sourceFileName "),
404 :     CGI::span({-style=>"float:right ; text-align: right"}, $edit_link, " ", $try_link)
405 :     ), CGI::br(),
406 : jj 1994
407 :    
408 :    
409 :    
410 :     CGI::checkbox(-name=>"hideme$cnt",-value=>1,-label=>"Don't show me on the next update"),
411 :     CGI::br(),
412 :     CGI::checkbox(-name=>"trial$cnt",-value=>1,-label=>"Add me to the current set on the next update"),
413 :     CGI::hidden(-name=>"filetrial$cnt", -default=>[$sourceFileName]).
414 :     CGI::p($problem_output),
415 :     ));
416 :     }
417 :    
418 :    
419 : jj 2115 sub pre_header_initialize {
420 : jj 1994 my ($self) = @_;
421 :     my $r = $self->r;
422 : jj 2115 ## For all cases, lets set some things
423 :     $self->{error}=0;
424 :     my $ce = $r->ce;
425 :     my $db = $r->db;
426 : jj 1994
427 : jj 2115
428 : jj 1994 my $userName = $r->param('user');
429 : jj 2115 my $user = $db->getUser($userName); # checked
430 :     die "record for user $userName (real user) does not exist."
431 : jj 1994 unless defined $user;
432 :     my $authz = $r->authz;
433 :     unless ($authz->hasPermissions($userName, "modify_problem_sets")) {
434 : jj 2115 return(""); # Error message already produced in the body
435 : jj 1994 }
436 :    
437 : jj 2115 ## Now one action we have to deal with here
438 :     if ($r->param('edit_local')) {
439 :     my $urlpath = $r->urlpath;
440 :     my $db = $r->db;
441 :     my $checkset = $db->getGlobalSet($r->param('local_sets'));
442 :     if (not defined($checkset)) {
443 :     $self->{error} = 1;
444 : jj 2152 $self->addbadmessage('You need to select a "Current Set" before you can edit it.');
445 : jj 2115 } else {
446 :     my $page = $urlpath->newFromModule('WeBWorK::ContentGenerator::Instructor::ProblemSetEditor', setID=>$r->param('local_sets'), courseID=>$urlpath->arg("courseID"));
447 :     my $url = $self->systemLink($page);
448 :     $self->reply_with_redirect($url);
449 :     }
450 :     }
451 : jj 1994
452 : jj 2115 ## Next, lots of set up so that errors can be reported with message()
453 :    
454 : jj 1994 ############# List of problems we have already printed
455 :    
456 : jj 2116 $self->{past_problems} = get_past_problem_files($r);
457 :     my $none_shown = scalar(@{$self->{past_problems}})==0;
458 : jj 2115 my @pg_files=();
459 : jj 1994 my $use_previous_problems = 1;
460 :     my $first_shown = $r->param('first_shown') || 0;
461 :     my $last_shown = $r->param('last_shown');
462 :     if (not defined($last_shown)) {
463 :     $last_shown = -1;
464 :     }
465 :     my @all_past_list = (); # these are include requested, but not shown
466 : jj 2115 my $j = 0;
467 : jj 1994 while (defined($r->param("all_past_list$j"))) {
468 :     push @all_past_list, $r->param("all_past_list$j");
469 :     $j++;
470 :     }
471 :    
472 :     ############# Default of which problem selector to display
473 :    
474 : jj 2115 my $browse_which = $r->param('browse_which') || 'browse_local';
475 : jj 1994
476 :     my $problem_seed = $r->param('problem_seed') || 0;
477 :     $r->param('problem_seed', $problem_seed); # if it wasn't defined before
478 :    
479 :     ########### Start the logic through if elsif elsif ...
480 :    
481 :     ##### Asked to browse certain problems
482 :     if ($r->param('browse_library')) {
483 :     $browse_which = 'browse_library';
484 :     $r->param('library_sets', "");
485 :     } elsif ($r->param('browse_local')) {
486 :     $browse_which = 'browse_local';
487 :     $r->param('library_sets', "");
488 :     } elsif ($r->param('browse_mysets')) {
489 :     $browse_which = 'browse_mysets';
490 :     $r->param('library_sets', "");
491 :    
492 :     ##### Change the seed value
493 :    
494 :     } elsif ($r->param('rerandomize')) {
495 :     $problem_seed++;
496 :     $r->param('problem_seed', $problem_seed);
497 : jj 2152 $self->addbadmessage('Changing the problem seed for display, but there are no problems showing.') if $none_shown;
498 : jj 1994
499 :     ##### Clear the display
500 :    
501 :     } elsif ($r->param('cleardisplay')) {
502 :     @pg_files = ();
503 :     $use_previous_problems=0;
504 : jj 2152 $self->addbadmessage('The display was already cleared.') if $none_shown;
505 : jj 1994
506 :     ##### View problems selected from the local list
507 :    
508 :     } elsif ($r->param('view_local_set')) {
509 :    
510 :     my $set_to_display = $r->param('library_sets');
511 : jj 2152 if (not defined($set_to_display) or $set_to_display eq SELECT_LOCAL_STRING or $set_to_display eq "Found no directories containing problems") {
512 :     $self->addbadmessage('You need to select a set to view.');
513 : jj 1994 } else {
514 : jj 2024 $set_to_display = '.' if $set_to_display eq ' -- Top -- ';
515 : jj 1994 @pg_files = list_pg_files($ce->{courseDirs}->{templates},
516 :     "$set_to_display");
517 :     $use_previous_problems=0;
518 :     }
519 :    
520 :     ##### View problems selected from the a set in this course
521 :    
522 :     } elsif ($r->param('view_mysets_set')) {
523 :    
524 :     my $set_to_display = $r->param('library_sets');
525 : jj 2024 if (not defined($set_to_display)
526 :     or $set_to_display eq "Select a Problem Set"
527 : jj 2152 or $set_to_display eq NO_LOCAL_SET_STRING) {
528 :     $self->addbadmessage("You need to select a set from this course to view.");
529 : jj 1994 } else {
530 :     my @problemList = $db->listGlobalProblems($set_to_display);
531 :     my $problem;
532 :     @pg_files=();
533 :     for $problem (@problemList) {
534 :     my $problemRecord = $db->getGlobalProblem($set_to_display, $problem); # checked
535 :     die "global $problem for set $set_to_display not found." unless
536 :     $problemRecord;
537 :     push @pg_files, $problemRecord->source_file;
538 :    
539 :     }
540 :     $use_previous_problems=0;
541 :     }
542 :    
543 :     ##### View whole chapter from the library
544 :     ## This will change somewhat later
545 :    
546 :     } elsif ($r->param('lib_view')) {
547 :    
548 :     @pg_files=();
549 :     my $chap = $r->param('library_chapters') || "";
550 :     $chap = "" if($chap eq "All Chapters");
551 :     my $sect = $r->param('library_sections') || "";
552 :     $sect = "" if($sect eq "All Sections");
553 :     my @dbsearch = WeBWorK::Utils::ListingDB::getSectionListings($r->{ce}, "$chap", "$sect");
554 :     my ($result, $tolibpath);
555 :     for $result (@dbsearch) {
556 : jj 2002 $tolibpath = "Library/$result->{path}/$result->{filename}";
557 : jj 1994
558 :     ## Too clunky!!!!
559 :     push @pg_files, $tolibpath;
560 :     }
561 :     $use_previous_problems=0;
562 :    
563 :     ##### Edit the current local problem set
564 :    
565 :     } elsif ($r->param('edit_local')) { ## Jump to set edit page
566 :    
567 : jj 2115 ; # already handled
568 :    
569 :    
570 : jj 1994 ##### Make a new local problem set
571 :    
572 :     } elsif ($r->param('new_local_set')) {
573 :     if ($r->param('new_set_name') !~ /^[\w.-]*$/) {
574 : jj 2152 $self->addbadmessage("The name ".$r->param('new_set_name')." is not a valid set name. Use only letters, digits, -, _, and .");
575 : jj 1994 } else {
576 :     my $newSetName = $r->param('new_set_name');
577 :     $newSetName =~ s/^set//;
578 :     $newSetName =~ s/\.def$//;
579 : jj 2112 $r->param('local_sets',$newSetName);
580 : jj 1994 my $newSetRecord = $db->getGlobalSet($newSetName);
581 :     if (defined($newSetRecord)) {
582 : jj 2152 $self->addbadmessage("The set name $newSetName is already in use. Pick a different name if you would like to start a new set.");
583 : jj 1994 } else { # Do it!
584 :     $newSetRecord = $db->{set}->{record}->new();
585 :     $newSetRecord->set_id($newSetName);
586 :     $newSetRecord->set_header("");
587 :     $newSetRecord->problem_header("");
588 :     $newSetRecord->open_date(time()+60*60*24*7); # in one week
589 :     $newSetRecord->due_date(time()+60*60*24*7*2); # in two weeks
590 :     $newSetRecord->answer_date(time()+60*60*24*7*3); # in three weeks
591 :     eval {$db->addGlobalSet($newSetRecord)};
592 :     }
593 :     }
594 :    
595 :     ##### Add selected problems to the current local set
596 :    
597 :     } elsif ($r->param('update')) {
598 :     ## first handle problems to be added before we hide them
599 :     my($localSet, @selected);
600 :    
601 : jj 2116 @pg_files = grep {($_->[1] & ADDED) != 0 } @{$self->{past_problems}};
602 : jj 1994 @selected = map {$_->[0]} @pg_files;
603 :    
604 : jj 2116 my @action_files = grep {$_->[1] > 0 } @{$self->{past_problems}};
605 :     if(scalar(@action_files) == 0) {
606 : jj 2152 $self->addbadmessage('Act on marked problems requested, but no problems were marked.');
607 : jj 2116 }
608 :    
609 : jj 1994 if (scalar(@selected)>0) { # if some are to be added, they need a place to go
610 :     $localSet = $r->param('local_sets');
611 : jj 2152 if (not defined($localSet) or
612 :     $localSet eq SELECT_SET_STRING or
613 :     $localSet eq NO_LOCAL_SET_STRING) {
614 :     $self->addbadmessage('You are trying to add problems to something, but you did not select a "Current Set" name as a target.');
615 : jj 1994 } else {
616 :     my $newSetRecord = $db->getGlobalSet($localSet);
617 :     if (not defined($newSetRecord)) {
618 : jj 2152 $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.");
619 : jj 1994 } else {
620 : jj 2116 my $addcount = add_selected($self, $db, $localSet);
621 :     if($addcount > 0) {
622 : jj 2152 $self->addgoodmessage("Added $addcount problem".(($addcount>1)?'s':'').
623 : jj 2116 " to $localSet.");
624 :     }
625 : jj 1994 }
626 :     }
627 :     }
628 :     ## now handle problems to be hidden
629 :    
630 : jj 2116 ## only keep the ones which are not hidden
631 :     @pg_files = grep {($_->[1] & HIDDEN) ==0 } @{$self->{past_problems}};
632 : jj 1994 @pg_files = map {$_->[0]} @pg_files;
633 :     @all_past_list = (@all_past_list[0..($first_shown-1)],
634 :     @pg_files,
635 :     @all_past_list[($last_shown+1)..(scalar(@all_past_list)-1)]);
636 :     $last_shown = $first_shown+MAX_SHOW -1;
637 :     $last_shown = (scalar(@all_past_list)-1) if($last_shown>=scalar(@all_past_list));
638 :    
639 :     ## FIXME: you should say something if no problems are selected
640 :     ## maybe the add button should be disabled if there are no problems
641 :     ## showing
642 :    
643 :    
644 :     } elsif ($r->param('next_page')) {
645 :     $first_shown = $last_shown+1;
646 :     $last_shown = $first_shown+MAX_SHOW-1;
647 :     $last_shown = (scalar(@all_past_list)-1) if($last_shown>=scalar(@all_past_list));
648 :     } elsif ($r->param('prev_page')) {
649 :     $last_shown = $first_shown-1;
650 :     $first_shown = $last_shown - MAX_SHOW+1;
651 :    
652 :     $first_shown = 0 if($first_shown<0);
653 :    
654 :     ##### No action requested, probably our first time here
655 :    
656 :     } else {
657 :     #my $c = $r->connection;
658 :     #print "Debug info: ". $r->get_remote_host ."<p>". $c->remote_ip ;
659 :     ;
660 :     } ##### end of the if elsif ...
661 :    
662 :    
663 :     ############# List of local sets
664 :    
665 : jj 2024 my @all_set_defs = $db->listGlobalSets;
666 : jj 1994 for ($j=0; $j<scalar(@all_set_defs); $j++) {
667 :     $all_set_defs[$j] =~ s|^set||;
668 :     $all_set_defs[$j] =~ s|\.def||;
669 :     }
670 :    
671 :     if ($use_previous_problems) {
672 :     @pg_files = @all_past_list;
673 :     } else {
674 :     $first_shown = 0;
675 :     $last_shown = scalar(@pg_files)<MAX_SHOW ? scalar(@pg_files) : MAX_SHOW;
676 :     $last_shown--; # to make it an array index
677 :     }
678 : jj 2115 ############# Now store data in self for retreival by body
679 :     $self->{first_shown} = $first_shown;
680 :     $self->{last_shown} = $last_shown;
681 :     $self->{browse_which} = $browse_which;
682 :     $self->{problem_seed} = $problem_seed;
683 :     $self->{pg_files} = \@pg_files;
684 :     $self->{all_set_defs} = \@all_set_defs;
685 : jj 1994
686 : jj 2115 }
687 :    
688 :    
689 :     sub title {
690 :     return "Problem Set Maker";
691 :     }
692 :    
693 :     sub body {
694 :     my ($self) = @_;
695 :    
696 :     my $r = $self->r;
697 :     my $ce = $r->ce; # course environment
698 :     my $db = $r->db; # database
699 :     my $j; # garden variety counter
700 :    
701 :     my $userName = $r->param('user');
702 :    
703 :     my $user = $db->getUser($userName); # checked
704 :     die "record for user $userName (real user) does not exist."
705 :     unless defined $user;
706 :    
707 :     ### Check that this is a professor
708 :     my $authz = $r->authz;
709 :     unless ($authz->hasPermissions($userName, "modify_problem_sets")) {
710 :     print "User $userName returned " .
711 :     $authz->hasPermissions($user, "modify_problem_sets") .
712 :     " for permission";
713 :     return(CGI::div({class=>'ResultsWithError'},
714 :     CGI::em("You are not authorized to access the Instructor tools.")));
715 :     }
716 :    
717 :     ########## Extract information computed in pre_header_initialize
718 :    
719 :     my $first_shown = $self->{first_shown};
720 :     my $last_shown = $self->{last_shown};
721 :     my $browse_which = $self->{browse_which};
722 :     my $problem_seed = $self->{problem_seed};
723 :     my @pg_files = @{$self->{pg_files}};
724 :     my @all_set_defs = @{$self->{all_set_defs}};
725 :    
726 :     my @pg_html=($last_shown>=$first_shown) ?
727 : jj 1994 renderProblems($r,$user, @pg_files[$first_shown..$last_shown]) : ();
728 :    
729 :     ########## Top part
730 :     print CGI::startform({-method=>"POST", -action=>$r->uri}),
731 :     $self->hidden_authen_fields,
732 :     '<div align="center">',
733 :     CGI::start_table({-border=>2});
734 : jj 2024 $self->make_top_row('all_set_defs'=>\@all_set_defs,
735 : jj 1994 'browse_which'=> $browse_which);
736 :     print CGI::hidden(-name=>'browse_which', -default=>[$browse_which]),
737 :     CGI::hidden(-name=>'problem_seed', -default=>[$problem_seed]);
738 :     for ($j = 0 ; $j < scalar(@pg_files) ; $j++) {
739 :     print CGI::hidden(-name=>"all_past_list$j", -default=>$pg_files[$j]);
740 :     }
741 :    
742 :     print CGI::hidden(-name=>'first_shown', -default=>[$first_shown]);
743 :     print CGI::hidden(-name=>'last_shown', -default=>[$last_shown]);
744 :    
745 :    
746 :     ########## Now print problems
747 :     my $jj;
748 :     for ($jj=0; $jj<scalar(@pg_html); $jj++) {
749 :     $pg_files[$jj] =~ s|^$ce->{courseDirs}->{templates}/?||;
750 : jj 2024 $self->make_data_row($pg_files[$jj+$first_shown], $pg_html[$jj], $jj+1);
751 : jj 1994 }
752 :    
753 :     ########## Finish things off
754 :     print CGI::end_table();
755 :     print '</div>';
756 :     # if($first_shown>0 or (1+$last_shown)<scalar(@pg_files)) {
757 :     my ($next_button, $prev_button) = ("", "");
758 :     if ($first_shown > 0) {
759 :     $prev_button = CGI::submit(-name=>"prev_page", -style=>"width:15ex",
760 :     -value=>"Previous page");
761 :     }
762 :     if ((1+$last_shown)<scalar(@pg_files)) {
763 :     $next_button = CGI::submit(-name=>"next_page", -style=>"width:15ex",
764 :     -value=>"Next page");
765 :     }
766 :     if (scalar(@pg_files)>0) {
767 :     print CGI::p(($first_shown+1)."-".($last_shown+1)." of ".scalar(@pg_files).
768 :     " shown.", $prev_button, " ", $next_button);
769 :     }
770 :     # }
771 :     print CGI::endform(), "\n";
772 :    
773 :     return "";
774 :     }
775 :    
776 :     ############################################## End of Body
777 :    
778 :     # SKEL: To emit your own HTTP header, uncomment this:
779 :     #
780 :     #sub header {
781 :     # my ($self) = @_;
782 :     #
783 :     # # Generate your HTTP header here.
784 :     #
785 :     # # If you return something, it will be used as the HTTP status code for this
786 :     # # request. The Apache::Constants module might be useful for gerating status
787 :     # # codes. If you don't return anything, the status code "OK" will be used.
788 :     # return "";
789 :     #}
790 :    
791 :     # SKEL: If you need to do any processing after the HTTP header is sent, but before
792 :     # any template processing occurs, or you need to calculate values that will be
793 :     # used in multiple methods, do it in this method:
794 :     #
795 :     #sub initialize {
796 :     #my ($self) = @_;
797 :     #}
798 :    
799 :     # SKEL: If you need to add tags to the document <HEAD>, uncomment this method:
800 :     #
801 :     #sub head {
802 :     # my ($self) = @_;
803 :     #
804 :     # # You can print head tags here, like <META>, <SCRIPT>, etc.
805 :     #
806 :     # return "";
807 :     #}
808 :    
809 :     # SKEL: To fill in the "info" box (to the right of the main body), use this
810 :     # method:
811 :     #
812 :     #sub info {
813 :     # my ($self) = @_;
814 :     #
815 :     # # Print HTML here.
816 :     #
817 :     # return "";
818 :     #}
819 :    
820 :     # SKEL: To provide navigation links, use this method:
821 :     #
822 :     #sub nav {
823 :     # my ($self, $args) = @_;
824 :     #
825 :     # # See the documentation of path() and pathMacro() in
826 :     # # WeBWorK::ContentGenerator for more information.
827 :     #
828 :     # return "";
829 :     #}
830 :    
831 :     # SKEL: For a little box for display options, etc., use this method:
832 :     #
833 :     #sub options {
834 :     # my ($self) = @_;
835 :     #
836 :     # # Print HTML here.
837 :     #
838 :     # return "";
839 :     #}
840 :    
841 :     # SKEL: For a list of sibling objects, use this method:
842 :     #
843 :     #sub siblings {
844 :     # my ($self, $args) = @_;
845 :     #
846 :     # # See the documentation of siblings() and siblingsMacro() in
847 :     # # WeBWorK::ContentGenerator for more information.
848 :     # #
849 :     # # Refer to implementations in ProblemSet and Problem.
850 :     #
851 :     # return "";
852 :     #}
853 :    
854 :     =head1 AUTHOR
855 :    
856 :     Written by John Jones, jj (at) asu.edu.
857 :    
858 :     =cut
859 :    
860 :    
861 :    
862 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9