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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2117 - (view) (download) (as text)

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9