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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9