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

Annotation of /trunk/webwork2/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2264 - (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 2264 # $CVSHeader: webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm,v 1.17 2004/05/31 02:20:27 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 : jj 2230 $selected->[1] |= SUCCESS;
129 : jj 2116 $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 : jj 2264 unshift @all_problem_directories, ' My Problems ' if($includetop);
148 : jj 2115 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 2230 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "Local Problems: ",
198 : jj 1994 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 2230 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "Browse from: ",
222 : jj 1994 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 : jj 2230 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"},
282 : jj 1994 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 2230 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "Adding Problems to ",
364 : jj 2264 CGI::b("Target Set: "),
365 : jj 1994 CGI::popup_menu(-name=> 'local_sets',
366 :     -values=>$list_of_local_sets,
367 :     -default=> $set_selected),
368 : jj 2264 CGI::submit(-name=>"edit_local", -value=>"Edit Target Set"),
369 : jj 1994 CGI::br(),
370 :     CGI::br(),
371 : jj 2230 CGI::submit(-name=>"new_local_set", -value=>"Create a New Set in This Course:",
372 :     #-onclick=>$myjs
373 :     ),
374 : jj 1994 " ",
375 :     CGI::textfield(-name=>"new_set_name",
376 :     -default=>"Name for new set here",
377 :     -override=>1, -size=>30),
378 :     CGI::br(),
379 :     ));
380 :    
381 :     print CGI::Tr(CGI::td({-bgcolor=>"black"}));
382 :    
383 :     print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"center"},
384 : jj 2230 CGI::start_table({-border=>"0"}),
385 :     CGI::Tr( CGI::td({ -align=>"center"},
386 :     CGI::submit(-name=>"select_all", -style=>$these_widths,
387 :     -value=>"Mark All For Adding"),
388 :     CGI::submit(-name=>"select_none", -style=>$these_widths,
389 :     -value=>"Clear All Marks"),
390 :     )),
391 :     CGI::Tr( CGI::td(
392 :     CGI::submit(-name=>"update", -style=>$these_widths. "; font-weight:bold",
393 :     -value=>"Update"),
394 :     CGI::submit(-name=>"rerandomize",
395 : jj 1994 -style=>$these_widths,
396 :     -value=>"Rerandomize"),
397 : jj 2230 CGI::submit(-name=>"cleardisplay",
398 : jj 1994 -style=>$these_widths,
399 : jj 2230 -value=>"Clear Problem Display")
400 :     )),
401 :     CGI::end_table()));
402 : jj 1994
403 :     }
404 :    
405 :     sub make_data_row {
406 :     my $self = shift;
407 :     my $sourceFileName = shift;
408 :     my $pg = shift;
409 :     my $cnt = shift;
410 : jj 2230 my $mark = shift || 0;
411 : jj 1994
412 : jj 2024 $sourceFileName =~ s|^./||; # clean up top ugliness
413 :    
414 : jj 1994 my $urlpath = $self->r->urlpath;
415 :     my $problem_output = $pg->{flags}->{error_flag} ?
416 : jj 2225 CGI::div({class=>"ResultsWithError"}, CGI::em("This problem produced an error"))
417 : jj 2015 : CGI::div({class=>"RenderSolo"}, $pg->{body_text});
418 : jj 1994
419 :    
420 : jj 2024 my $edit_link = '';
421 : jj 2040 #if($self->{r}->param('browse_which') ne 'browse_library') {
422 :     if($sourceFileName !~ /^Library\//) {
423 : jj 2039 $edit_link = CGI::a({href=>$self->systemLink($urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::PGProblemEditor",
424 :     courseID =>$urlpath->arg("courseID"),
425 :     setID=>"Undefined_Set",
426 :     problemID=>"1"),
427 :     params=>{sourceFilePath => "$sourceFileName"}
428 : jj 1994 )}, "Edit it" );
429 : jj 2024 }
430 : jj 1994
431 : jj 2039 my $try_link = CGI::a({href=>$self->systemLink($urlpath->newFromModule("WeBWorK::ContentGenerator::Problem",
432 :     courseID =>$urlpath->arg("courseID"),
433 :     setID=>"Undefined_Set",
434 :     problemID=>"1"),
435 : jj 2015 params =>{effectiveUser => $self->r->param('user'),
436 : jj 2021 editMode => "SetMaker",
437 : jj 2015 sourceFilePath => "$sourceFileName"} )}, "Try it");
438 : jj 1994
439 : jj 2230 my %add_box_data = ( -name=>"trial$cnt",-value=>1,-label=>"Add me to the current set on the next update");
440 :     if($mark & SUCCESS) {
441 :     $add_box_data{ -label } .= " (just added this problem)";
442 :     } elsif($mark & ADDED) {
443 :     $add_box_data{ -checked } = 1;
444 :     }
445 : jj 1994
446 :     print CGI::Tr({-align=>"left"}, CGI::td(
447 :    
448 : jj 2024 CGI::div({-style=>"background-color: #DDDDDD; margin: 0px auto"},
449 :     CGI::span({-style=>"float:left ; text-align: left"},"File name: $sourceFileName "),
450 :     CGI::span({-style=>"float:right ; text-align: right"}, $edit_link, " ", $try_link)
451 :     ), CGI::br(),
452 : jj 1994
453 :    
454 :    
455 :    
456 :     CGI::checkbox(-name=>"hideme$cnt",-value=>1,-label=>"Don't show me on the next update"),
457 :     CGI::br(),
458 : jj 2230 CGI::checkbox((%add_box_data)),
459 : jj 1994 CGI::hidden(-name=>"filetrial$cnt", -default=>[$sourceFileName]).
460 :     CGI::p($problem_output),
461 :     ));
462 :     }
463 :    
464 :    
465 : jj 2115 sub pre_header_initialize {
466 : jj 1994 my ($self) = @_;
467 :     my $r = $self->r;
468 : jj 2115 ## For all cases, lets set some things
469 :     $self->{error}=0;
470 :     my $ce = $r->ce;
471 :     my $db = $r->db;
472 : jj 2225 my $maxShown = $r->param('max_shown') || MAX_SHOW_DEFAULT;
473 :     $maxShown = 10000000 if($maxShown eq 'All'); # let's hope there aren't more
474 : jj 1994
475 : jj 2115
476 : jj 1994 my $userName = $r->param('user');
477 : jj 2115 my $user = $db->getUser($userName); # checked
478 :     die "record for user $userName (real user) does not exist."
479 : jj 1994 unless defined $user;
480 :     my $authz = $r->authz;
481 :     unless ($authz->hasPermissions($userName, "modify_problem_sets")) {
482 : jj 2115 return(""); # Error message already produced in the body
483 : jj 1994 }
484 :    
485 : jj 2115 ## Now one action we have to deal with here
486 :     if ($r->param('edit_local')) {
487 :     my $urlpath = $r->urlpath;
488 :     my $db = $r->db;
489 :     my $checkset = $db->getGlobalSet($r->param('local_sets'));
490 :     if (not defined($checkset)) {
491 :     $self->{error} = 1;
492 : jj 2264 $self->addbadmessage('You need to select a "Target Set" before you can edit it.');
493 : jj 2115 } else {
494 :     my $page = $urlpath->newFromModule('WeBWorK::ContentGenerator::Instructor::ProblemSetEditor', setID=>$r->param('local_sets'), courseID=>$urlpath->arg("courseID"));
495 :     my $url = $self->systemLink($page);
496 :     $self->reply_with_redirect($url);
497 :     }
498 :     }
499 : jj 1994
500 : jj 2115 ## Next, lots of set up so that errors can be reported with message()
501 :    
502 : jj 1994 ############# List of problems we have already printed
503 :    
504 : jj 2116 $self->{past_problems} = get_past_problem_files($r);
505 : jj 2230 # if we don't end up reusing problems, this will be wiped out
506 :     # if we do redisplay the same problems, we must adjust this accordingly
507 :     my @past_marks = map {$_->[1]} @{$self->{past_problems}};
508 : jj 2116 my $none_shown = scalar(@{$self->{past_problems}})==0;
509 : jj 2115 my @pg_files=();
510 : jj 1994 my $use_previous_problems = 1;
511 :     my $first_shown = $r->param('first_shown') || 0;
512 :     my $last_shown = $r->param('last_shown');
513 :     if (not defined($last_shown)) {
514 :     $last_shown = -1;
515 :     }
516 :     my @all_past_list = (); # these are include requested, but not shown
517 : jj 2115 my $j = 0;
518 : jj 1994 while (defined($r->param("all_past_list$j"))) {
519 :     push @all_past_list, $r->param("all_past_list$j");
520 :     $j++;
521 :     }
522 :    
523 :     ############# Default of which problem selector to display
524 :    
525 : jj 2115 my $browse_which = $r->param('browse_which') || 'browse_local';
526 : jj 1994
527 :     my $problem_seed = $r->param('problem_seed') || 0;
528 :     $r->param('problem_seed', $problem_seed); # if it wasn't defined before
529 :    
530 :     ########### Start the logic through if elsif elsif ...
531 :    
532 :     ##### Asked to browse certain problems
533 :     if ($r->param('browse_library')) {
534 :     $browse_which = 'browse_library';
535 :     $r->param('library_sets', "");
536 :     } elsif ($r->param('browse_local')) {
537 :     $browse_which = 'browse_local';
538 :     $r->param('library_sets', "");
539 :     } elsif ($r->param('browse_mysets')) {
540 :     $browse_which = 'browse_mysets';
541 :     $r->param('library_sets', "");
542 :    
543 :     ##### Change the seed value
544 :    
545 :     } elsif ($r->param('rerandomize')) {
546 :     $problem_seed++;
547 :     $r->param('problem_seed', $problem_seed);
548 : jj 2152 $self->addbadmessage('Changing the problem seed for display, but there are no problems showing.') if $none_shown;
549 : jj 1994
550 :     ##### Clear the display
551 :    
552 :     } elsif ($r->param('cleardisplay')) {
553 :     @pg_files = ();
554 :     $use_previous_problems=0;
555 : jj 2152 $self->addbadmessage('The display was already cleared.') if $none_shown;
556 : jj 1994
557 :     ##### View problems selected from the local list
558 :    
559 :     } elsif ($r->param('view_local_set')) {
560 :    
561 :     my $set_to_display = $r->param('library_sets');
562 : 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") {
563 :     $self->addbadmessage('You need to select a set to view.');
564 : jj 1994 } else {
565 : jj 2264 $set_to_display = '.' if $set_to_display eq ' My Problems ';
566 : jj 1994 @pg_files = list_pg_files($ce->{courseDirs}->{templates},
567 :     "$set_to_display");
568 :     $use_previous_problems=0;
569 :     }
570 :    
571 :     ##### View problems selected from the a set in this course
572 :    
573 :     } elsif ($r->param('view_mysets_set')) {
574 :    
575 :     my $set_to_display = $r->param('library_sets');
576 : jj 2024 if (not defined($set_to_display)
577 :     or $set_to_display eq "Select a Problem Set"
578 : jj 2152 or $set_to_display eq NO_LOCAL_SET_STRING) {
579 :     $self->addbadmessage("You need to select a set from this course to view.");
580 : jj 1994 } else {
581 :     my @problemList = $db->listGlobalProblems($set_to_display);
582 :     my $problem;
583 :     @pg_files=();
584 :     for $problem (@problemList) {
585 :     my $problemRecord = $db->getGlobalProblem($set_to_display, $problem); # checked
586 :     die "global $problem for set $set_to_display not found." unless
587 :     $problemRecord;
588 :     push @pg_files, $problemRecord->source_file;
589 :    
590 :     }
591 :     $use_previous_problems=0;
592 :     }
593 :    
594 :     ##### View whole chapter from the library
595 :     ## This will change somewhat later
596 :    
597 : jj 2230 } elsif ($r->param('lib_view')) {
598 : jj 1994
599 :     @pg_files=();
600 :     my $chap = $r->param('library_chapters') || "";
601 :     $chap = "" if($chap eq "All Chapters");
602 :     my $sect = $r->param('library_sections') || "";
603 :     $sect = "" if($sect eq "All Sections");
604 :     my @dbsearch = WeBWorK::Utils::ListingDB::getSectionListings($r->{ce}, "$chap", "$sect");
605 :     my ($result, $tolibpath);
606 :     for $result (@dbsearch) {
607 : jj 2002 $tolibpath = "Library/$result->{path}/$result->{filename}";
608 : jj 1994
609 :     ## Too clunky!!!!
610 :     push @pg_files, $tolibpath;
611 :     }
612 :     $use_previous_problems=0;
613 :    
614 :     ##### Edit the current local problem set
615 :    
616 :     } elsif ($r->param('edit_local')) { ## Jump to set edit page
617 :    
618 : jj 2115 ; # already handled
619 :    
620 :    
621 : jj 1994 ##### Make a new local problem set
622 :    
623 :     } elsif ($r->param('new_local_set')) {
624 :     if ($r->param('new_set_name') !~ /^[\w.-]*$/) {
625 : jj 2152 $self->addbadmessage("The name ".$r->param('new_set_name')." is not a valid set name. Use only letters, digits, -, _, and .");
626 : jj 1994 } else {
627 :     my $newSetName = $r->param('new_set_name');
628 :     $newSetName =~ s/^set//;
629 :     $newSetName =~ s/\.def$//;
630 : jj 2112 $r->param('local_sets',$newSetName);
631 : jj 1994 my $newSetRecord = $db->getGlobalSet($newSetName);
632 :     if (defined($newSetRecord)) {
633 : 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.");
634 : jj 1994 } else { # Do it!
635 :     $newSetRecord = $db->{set}->{record}->new();
636 :     $newSetRecord->set_id($newSetName);
637 :     $newSetRecord->set_header("");
638 :     $newSetRecord->problem_header("");
639 :     $newSetRecord->open_date(time()+60*60*24*7); # in one week
640 :     $newSetRecord->due_date(time()+60*60*24*7*2); # in two weeks
641 :     $newSetRecord->answer_date(time()+60*60*24*7*3); # in three weeks
642 :     eval {$db->addGlobalSet($newSetRecord)};
643 :     }
644 :     }
645 :    
646 :     ##### Add selected problems to the current local set
647 :    
648 :     } elsif ($r->param('update')) {
649 :     ## first handle problems to be added before we hide them
650 :     my($localSet, @selected);
651 :    
652 : jj 2116 @pg_files = grep {($_->[1] & ADDED) != 0 } @{$self->{past_problems}};
653 : jj 1994 @selected = map {$_->[0]} @pg_files;
654 :    
655 : jj 2116 my @action_files = grep {$_->[1] > 0 } @{$self->{past_problems}};
656 : jj 2230 # There are now good reasons to do an update without selecting anything.
657 :     #if(scalar(@action_files) == 0) {
658 :     # $self->addbadmessage('Update requested, but no problems were marked.');
659 :     #}
660 : jj 2116
661 : jj 1994 if (scalar(@selected)>0) { # if some are to be added, they need a place to go
662 :     $localSet = $r->param('local_sets');
663 : jj 2152 if (not defined($localSet) or
664 :     $localSet eq SELECT_SET_STRING or
665 :     $localSet eq NO_LOCAL_SET_STRING) {
666 : jj 2264 $self->addbadmessage('You are trying to add problems to something, but you did not select a "Target Set" name as a target.');
667 : jj 1994 } else {
668 :     my $newSetRecord = $db->getGlobalSet($localSet);
669 :     if (not defined($newSetRecord)) {
670 : 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.");
671 : jj 1994 } else {
672 : jj 2116 my $addcount = add_selected($self, $db, $localSet);
673 :     if($addcount > 0) {
674 : jj 2152 $self->addgoodmessage("Added $addcount problem".(($addcount>1)?'s':'').
675 : jj 2116 " to $localSet.");
676 :     }
677 : jj 1994 }
678 :     }
679 :     }
680 :     ## now handle problems to be hidden
681 :    
682 : jj 2116 ## only keep the ones which are not hidden
683 :     @pg_files = grep {($_->[1] & HIDDEN) ==0 } @{$self->{past_problems}};
684 : jj 2230 @past_marks = map {$_->[1]} @pg_files;
685 : jj 1994 @pg_files = map {$_->[0]} @pg_files;
686 :     @all_past_list = (@all_past_list[0..($first_shown-1)],
687 :     @pg_files,
688 :     @all_past_list[($last_shown+1)..(scalar(@all_past_list)-1)]);
689 : jj 2225 $last_shown = $first_shown+$maxShown -1;
690 : jj 1994 $last_shown = (scalar(@all_past_list)-1) if($last_shown>=scalar(@all_past_list));
691 :    
692 :     } elsif ($r->param('next_page')) {
693 :     $first_shown = $last_shown+1;
694 : jj 2225 $last_shown = $first_shown+$maxShown-1;
695 : jj 1994 $last_shown = (scalar(@all_past_list)-1) if($last_shown>=scalar(@all_past_list));
696 : jj 2230 @past_marks = ();
697 : jj 1994 } elsif ($r->param('prev_page')) {
698 :     $last_shown = $first_shown-1;
699 : jj 2225 $first_shown = $last_shown - $maxShown+1;
700 : jj 1994
701 :     $first_shown = 0 if($first_shown<0);
702 : jj 2230 @past_marks = ();
703 : jj 1994
704 : jj 2230 } elsif ($r->param('select_all')) {
705 :     @past_marks = map {1} @past_marks;
706 :     } elsif ($r->param('select_none')) {
707 :     @past_marks = ();
708 :    
709 : jj 1994 ##### No action requested, probably our first time here
710 :    
711 :     } else {
712 :     #my $c = $r->connection;
713 :     #print "Debug info: ". $r->get_remote_host ."<p>". $c->remote_ip ;
714 :     ;
715 :     } ##### end of the if elsif ...
716 :    
717 :    
718 :     ############# List of local sets
719 :    
720 : jj 2024 my @all_set_defs = $db->listGlobalSets;
721 : jj 1994 for ($j=0; $j<scalar(@all_set_defs); $j++) {
722 :     $all_set_defs[$j] =~ s|^set||;
723 :     $all_set_defs[$j] =~ s|\.def||;
724 :     }
725 :    
726 :     if ($use_previous_problems) {
727 :     @pg_files = @all_past_list;
728 :     } else {
729 :     $first_shown = 0;
730 : jj 2225 $last_shown = scalar(@pg_files)<$maxShown ? scalar(@pg_files) : $maxShown;
731 : jj 1994 $last_shown--; # to make it an array index
732 : jj 2230 @past_marks = ();
733 : jj 1994 }
734 : jj 2115 ############# Now store data in self for retreival by body
735 :     $self->{first_shown} = $first_shown;
736 :     $self->{last_shown} = $last_shown;
737 :     $self->{browse_which} = $browse_which;
738 :     $self->{problem_seed} = $problem_seed;
739 :     $self->{pg_files} = \@pg_files;
740 : jj 2230 $self->{past_marks} = \@past_marks;
741 : jj 2115 $self->{all_set_defs} = \@all_set_defs;
742 : jj 1994
743 : jj 2115 }
744 :    
745 :    
746 :     sub title {
747 :     return "Problem Set Maker";
748 :     }
749 :    
750 :     sub body {
751 :     my ($self) = @_;
752 :    
753 :     my $r = $self->r;
754 :     my $ce = $r->ce; # course environment
755 :     my $db = $r->db; # database
756 :     my $j; # garden variety counter
757 :    
758 :     my $userName = $r->param('user');
759 :    
760 :     my $user = $db->getUser($userName); # checked
761 :     die "record for user $userName (real user) does not exist."
762 :     unless defined $user;
763 :    
764 :     ### Check that this is a professor
765 :     my $authz = $r->authz;
766 :     unless ($authz->hasPermissions($userName, "modify_problem_sets")) {
767 :     print "User $userName returned " .
768 :     $authz->hasPermissions($user, "modify_problem_sets") .
769 :     " for permission";
770 :     return(CGI::div({class=>'ResultsWithError'},
771 :     CGI::em("You are not authorized to access the Instructor tools.")));
772 :     }
773 :    
774 :     ########## Extract information computed in pre_header_initialize
775 :    
776 :     my $first_shown = $self->{first_shown};
777 :     my $last_shown = $self->{last_shown};
778 :     my $browse_which = $self->{browse_which};
779 :     my $problem_seed = $self->{problem_seed};
780 :     my @pg_files = @{$self->{pg_files}};
781 :     my @all_set_defs = @{$self->{all_set_defs}};
782 :    
783 :     my @pg_html=($last_shown>=$first_shown) ?
784 : jj 2225 renderProblems(r=> $r,
785 :     user => $user,
786 :     problem_list => [@pg_files[$first_shown..$last_shown]],
787 :     displayMode => $r->param('mydisplayMode')) : ();
788 : jj 1994
789 :     ########## Top part
790 :     print CGI::startform({-method=>"POST", -action=>$r->uri}),
791 :     $self->hidden_authen_fields,
792 :     '<div align="center">',
793 :     CGI::start_table({-border=>2});
794 : jj 2024 $self->make_top_row('all_set_defs'=>\@all_set_defs,
795 : jj 1994 'browse_which'=> $browse_which);
796 :     print CGI::hidden(-name=>'browse_which', -default=>[$browse_which]),
797 :     CGI::hidden(-name=>'problem_seed', -default=>[$problem_seed]);
798 :     for ($j = 0 ; $j < scalar(@pg_files) ; $j++) {
799 :     print CGI::hidden(-name=>"all_past_list$j", -default=>$pg_files[$j]);
800 :     }
801 :    
802 :     print CGI::hidden(-name=>'first_shown', -default=>[$first_shown]);
803 :     print CGI::hidden(-name=>'last_shown', -default=>[$last_shown]);
804 :    
805 :    
806 :     ########## Now print problems
807 :     my $jj;
808 :     for ($jj=0; $jj<scalar(@pg_html); $jj++) {
809 :     $pg_files[$jj] =~ s|^$ce->{courseDirs}->{templates}/?||;
810 : jj 2230 $self->make_data_row($pg_files[$jj+$first_shown], $pg_html[$jj], $jj+1, $self->{past_marks}->[$jj]);
811 : jj 1994 }
812 :    
813 :     ########## Finish things off
814 :     print CGI::end_table();
815 :     print '</div>';
816 :     # if($first_shown>0 or (1+$last_shown)<scalar(@pg_files)) {
817 :     my ($next_button, $prev_button) = ("", "");
818 :     if ($first_shown > 0) {
819 :     $prev_button = CGI::submit(-name=>"prev_page", -style=>"width:15ex",
820 :     -value=>"Previous page");
821 :     }
822 :     if ((1+$last_shown)<scalar(@pg_files)) {
823 :     $next_button = CGI::submit(-name=>"next_page", -style=>"width:15ex",
824 :     -value=>"Next page");
825 :     }
826 :     if (scalar(@pg_files)>0) {
827 :     print CGI::p(($first_shown+1)."-".($last_shown+1)." of ".scalar(@pg_files).
828 :     " shown.", $prev_button, " ", $next_button);
829 :     }
830 :     # }
831 :     print CGI::endform(), "\n";
832 :    
833 :     return "";
834 :     }
835 :    
836 :     ############################################## End of Body
837 :    
838 :     # SKEL: To emit your own HTTP header, uncomment this:
839 :     #
840 :     #sub header {
841 :     # my ($self) = @_;
842 :     #
843 :     # # Generate your HTTP header here.
844 :     #
845 :     # # If you return something, it will be used as the HTTP status code for this
846 :     # # request. The Apache::Constants module might be useful for gerating status
847 :     # # codes. If you don't return anything, the status code "OK" will be used.
848 :     # return "";
849 :     #}
850 :    
851 :     # SKEL: If you need to do any processing after the HTTP header is sent, but before
852 :     # any template processing occurs, or you need to calculate values that will be
853 :     # used in multiple methods, do it in this method:
854 :     #
855 :     #sub initialize {
856 :     #my ($self) = @_;
857 :     #}
858 :    
859 :     # SKEL: If you need to add tags to the document <HEAD>, uncomment this method:
860 :     #
861 :     #sub head {
862 :     # my ($self) = @_;
863 :     #
864 :     # # You can print head tags here, like <META>, <SCRIPT>, etc.
865 :     #
866 :     # return "";
867 :     #}
868 :    
869 :     # SKEL: To fill in the "info" box (to the right of the main body), use this
870 :     # method:
871 :     #
872 :     #sub info {
873 :     # my ($self) = @_;
874 :     #
875 :     # # Print HTML here.
876 :     #
877 :     # return "";
878 :     #}
879 :    
880 :     # SKEL: To provide navigation links, use this method:
881 :     #
882 :     #sub nav {
883 :     # my ($self, $args) = @_;
884 :     #
885 :     # # See the documentation of path() and pathMacro() in
886 :     # # WeBWorK::ContentGenerator for more information.
887 :     #
888 :     # return "";
889 :     #}
890 :    
891 :     # SKEL: For a little box for display options, etc., use this method:
892 :     #
893 :     #sub options {
894 :     # my ($self) = @_;
895 :     #
896 :     # # Print HTML here.
897 :     #
898 :     # return "";
899 :     #}
900 :    
901 :     # SKEL: For a list of sibling objects, use this method:
902 :     #
903 :     #sub siblings {
904 :     # my ($self, $args) = @_;
905 :     #
906 :     # # See the documentation of siblings() and siblingsMacro() in
907 :     # # WeBWorK::ContentGenerator for more information.
908 :     # #
909 :     # # Refer to implementations in ProblemSet and Problem.
910 :     #
911 :     # return "";
912 :     #}
913 :    
914 :     =head1 AUTHOR
915 :    
916 :     Written by John Jones, jj (at) asu.edu.
917 :    
918 :     =cut
919 :    
920 :    
921 :    
922 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9