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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9