Parent Directory
|
Revision Log
formatting fixes (sorry jj, dpvc).
1 ################################################################################ 2 # WeBWorK Online Homework Delivery System 3 # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/ 4 # $CVSHeader: webwork2/lib/WeBWorK/ContentGenerator/Instructor/SetMaker.pm,v 1.29 2004/10/11 13:32:01 gage Exp $ 5 # 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 sortByName); 33 use WeBWorK::Utils::Tasks qw(renderProblems); 34 35 require WeBWorK::Utils::ListingDB; 36 37 use constant MAX_SHOW_DEFAULT => 20; 38 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 Problem Collection'; 41 use constant MY_PROBLEMS => ' My Problems '; 42 use constant MAIN_PROBLEMS => ' Main Problems '; 43 44 ## Flags for operations on files 45 46 use constant ADDED => 1; 47 use constant HIDDEN => (1 << 1); 48 use constant SUCCESS => (1 << 2); 49 50 ## for additional problib buttons 51 my %problib; ## filled in in global.conf 52 my %ignoredir = ( 53 '.' => 1, '..' => 1, 'Library' => 1, 54 'headers' => 1, 'macros' => 1, 'email' => 1, 55 ); 56 57 ## 58 ## This is for searching the disk for directories containing pg files. 59 ## to make the recursion work, this returns an array where the first 60 ## item is the number of pg files in the directory. The second is a 61 ## list of directories which contain pg files. 62 ## 63 ## If a directory contains only one pg file and at least one other 64 ## file, the directory is considered to be part of the parent 65 ## directory (it is probably in a separate directory only because 66 ## it has auxiliarly files that want to be kept together with the 67 ## pg file). 68 ## 69 ## If a directory has a file named "=library-ignore", it is never 70 ## included in the directory menu. If a directory contains a file 71 ## called "=library-combine-up", then its pg are included with those 72 ## in the parent directory (and the directory does not appear in the 73 ## menu). If it has a file called "=library-no-combine" then it is 74 ## always listed as a separate directory even if it contains only one 75 ## pg file. 76 ## 77 78 sub get_library_sets { 79 my $top = shift; my $dir = shift; 80 my @lis = readDirectory($dir); my @pgdirs; 81 return (0) if grep /^=library-ignore$/, @lis; 82 83 my $pgcount = scalar(grep { m/\.pg$/ and (not m/(Header|-text)\.pg$/) and -f "$dir/$_"} @lis); 84 my $others = scalar(grep { (!m/\.pg$/ || m/(Header|-text)\.pg$/) && 85 !m/(\.(tmp|bak)|~)$/ && -f "$dir/$_" } @lis); 86 87 my @dirs = grep {!$ignoredir{$_} and -d "$dir/$_"} @lis; 88 if ($top == 1) {@dirs = grep {!$problib{$_}} @dirs} 89 foreach my $subdir (@dirs) { 90 my @results = get_library_sets(0, "$dir/$subdir"); 91 $pgcount += shift @results; push(@pgdirs,@results); 92 } 93 94 return ($pgcount, @pgdirs) if $top || $pgcount == 0 || grep /^=library-combine-up$/, @lis; 95 return (0,@pgdirs,$dir) if $pgcount > 1 || $others == 0 || grep /^=library-no-combine$/, @lis; 96 return ($pgcount, @pgdirs); 97 } 98 99 sub get_library_pgs { 100 my $top = shift; my $base = shift; my $dir = shift; 101 my @lis = readDirectory("$base/$dir"); 102 return () if grep /^=library-ignore$/, @lis; 103 return () if !$top && grep /^=library-no-combine$/, @lis; 104 105 my @pgs = grep { m/\.pg$/ and (not m/(Header|-text)\.pg$/) and -f "$base/$dir/$_"} @lis; 106 my $others = scalar(grep { (!m/\.pg$/ || m/(Header|-text)\.pg$/) && 107 !m/(\.(tmp|bak)|~)$/ && -f "$base/$dir/$_" } @lis); 108 109 my @dirs = grep {!$ignoredir{$_} and -d "$base/$dir/$_"} @lis; 110 if ($top == 1) {@dirs = grep {!$problib{$_}} @dirs} 111 foreach my $subdir (@dirs) {push(@pgs, get_library_pgs(0,"$base/$dir",$subdir))} 112 113 return () unless $top || (scalar(@pgs) == 1 && $others) || grep /^=library-combine-up$/, @lis; 114 return (map {"$dir/$_"} @pgs); 115 } 116 117 sub list_pg_files { 118 my ($templates,$dir) = @_; 119 my $top = ($dir eq '.')? 1 : 2; 120 my @pgs = get_library_pgs($top,$templates,$dir); 121 return sortByName(undef,@pgs); 122 } 123 124 ## go through past page getting a list of identifiers for the problems 125 ## and whether or not they are selected, and whether or not they should 126 ## be hidden 127 128 sub get_past_problem_files { 129 my $r = shift; 130 my @found=(); 131 my $count =1; 132 while (defined($r->param("filetrial$count"))) { 133 my $val = 0; 134 $val |= ADDED if($r->param("trial$count")); 135 $val |= HIDDEN if($r->param("hideme$count")); 136 push @found, [$r->param("filetrial$count"), $val]; 137 $count++; 138 } 139 return(\@found); 140 } 141 142 #### For adding new problems 143 144 sub add_selected { 145 my $self = shift; 146 my $db = shift; 147 my $setName = shift; 148 my @past_problems = @{$self->{past_problems}}; 149 my @selected = @past_problems; 150 my (@path, $file, $selected, $freeProblemID); 151 $freeProblemID = max($db->listGlobalProblems($setName)) + 1; 152 my $addedcount=0; 153 154 for $selected (@selected) { 155 if($selected->[1] & ADDED) { 156 $file = $selected->[0]; 157 my $problemRecord = $self->addProblemToSet(setName => $setName, 158 sourceFile => $file, problemID => $freeProblemID); 159 $freeProblemID++; 160 $self->assignProblemToAllSetUsers($problemRecord); 161 $selected->[1] |= SUCCESS; 162 $addedcount++; 163 } 164 } 165 return($addedcount); 166 } 167 168 169 ############# List of sets of problems in templates directory 170 171 sub get_problem_directories { 172 my $ce = shift; 173 my $lib = shift; 174 my $source = $ce->{courseDirs}{templates}; 175 my $main = MY_PROBLEMS; my $isTop = 1; 176 if ($lib) {$source .= "/$lib"; $main = MAIN_PROBLEMS; $isTop = 2} 177 my @all_problem_directories = get_library_sets($isTop, $source); 178 my $includetop = shift @all_problem_directories; 179 my $j; 180 for ($j=0; $j<scalar(@all_problem_directories); $j++) { 181 $all_problem_directories[$j] =~ s|^$ce->{courseDirs}->{templates}/?||; 182 } 183 @all_problem_directories = sortByName(undef, @all_problem_directories); 184 unshift @all_problem_directories, $main if($includetop); 185 return (\@all_problem_directories); 186 } 187 188 ############# Everyone has a view problems line. Abstract it 189 sub view_problems_line { 190 my $internal_name = shift; 191 my $label = shift; 192 my $r = shift; # so we can get parameter values 193 my $result = CGI::submit(-name=>"$internal_name", -value=>$label); 194 195 my %display_modes = %{WeBWorK::PG::DISPLAY_MODES()}; 196 my @active_modes = grep { exists $display_modes{$_} } 197 @{$r->ce->{pg}->{displayModes}}; 198 push @active_modes, 'None'; 199 # We have our own displayMode since its value may be None, which is illegal 200 # in other modules. 201 my $mydisplayMode = $r->param('mydisplayMode') || $r->ce->{pg}->{options}->{displayMode}; 202 $result .= ' Display Mode: '.CGI::popup_menu(-name=> 'mydisplayMode', 203 -values=>\@active_modes, 204 -default=> $mydisplayMode); 205 # Now we give a choice of the number of problems to show 206 my $defaultMax = $r->param('max_shown') || MAX_SHOW_DEFAULT; 207 $result .= ' Max. Shown: '. 208 CGI::popup_menu(-name=> 'max_shown', 209 -values=>[5,10,15,20,25,30,50,'All'], 210 -default=> $defaultMax); 211 212 return($result); 213 } 214 215 216 ### The browsing panel has three versions 217 ##### Version 1 is local problems 218 sub browse_local_panel { 219 my $self = shift; 220 my $library_selected = shift; 221 my $lib = shift || ''; $lib =~ s/^browse_//; 222 my $name = ($lib eq '')? 'Local' : $problib{$lib}; 223 224 my $list_of_prob_dirs= get_problem_directories($self->r->ce,$lib); 225 if(scalar(@$list_of_prob_dirs) == 0) { 226 $library_selected = "Found no directories containing problems"; 227 unshift @{$list_of_prob_dirs}, $library_selected; 228 } else { 229 my $default_value = SELECT_LOCAL_STRING; 230 if (not $library_selected or $library_selected eq $default_value) { 231 unshift @{$list_of_prob_dirs}, $default_value; 232 $library_selected = $default_value; 233 } 234 } 235 my $view_problem_line = view_problems_line('view_local_set', 'View Problems', $self->r); 236 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "$name Problems: ", 237 CGI::popup_menu(-name=> 'library_sets', 238 -values=>$list_of_prob_dirs, 239 -default=> $library_selected), 240 CGI::br(), 241 $view_problem_line, 242 )); 243 } 244 245 ##### Version 2 is local problem sets 246 sub browse_mysets_panel { 247 my $self = shift; 248 my $library_selected = shift; 249 my $list_of_local_sets = shift; 250 my $default_value = "Select a Problem Set"; 251 252 if(scalar(@$list_of_local_sets) == 0) { 253 $list_of_local_sets = [NO_LOCAL_SET_STRING]; 254 } elsif (not $library_selected or $library_selected eq $default_value) { 255 unshift @{$list_of_local_sets}, $default_value; 256 $library_selected = $default_value; 257 } 258 259 my $view_problem_line = view_problems_line('view_mysets_set', 'View Problems', $self->r); 260 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "Browse from: ", 261 CGI::popup_menu(-name=> 'library_sets', 262 -values=>$list_of_local_sets, 263 -default=> $library_selected), 264 CGI::br(), 265 $view_problem_line 266 )); 267 } 268 269 ##### Version 3 is the problem library 270 271 272 # There a different levels, and you can pick a new chapter, 273 # pick a new section, pick all from chapter, pick all from section 274 # 275 # Incoming data - current chapter, current section 276 sub browse_library_panel { 277 my $self = shift; 278 my $r = $self->r; 279 my $ce = $r->ce; 280 281 my $libraryRoot = $r->{ce}->{problemLibrary}->{root}; 282 283 unless($libraryRoot) { 284 print CGI::Tr(CGI::td(CGI::div({class=>'ResultsWithError', align=>"center"}, 285 "The problem library has not been installed."))); 286 return; 287 } 288 # Test if the Library directory exists. If not, try to make it 289 unless(-d "$ce->{courseDirs}->{templates}/Library") { 290 unless(symlink($libraryRoot, "$ce->{courseDirs}->{templates}/Library")) { 291 my $msg = <<"HERE"; 292 You are missing the directory <code>templates/Library</code>, which is needed 293 for the Problem Library to function. It should be a link pointing to 294 <code>$libraryRoot</code>, which you set in <code>conf/global.conf</code>. 295 I tried to make the link for you, but that failed. Check the permissions 296 in your <code>templates</code> directory. 297 HERE 298 $self->addbadmessage($msg); 299 } 300 } 301 302 my $default_chap = "All Chapters"; 303 my $default_sect = "All Sections"; 304 305 my @chaps = WeBWorK::Utils::ListingDB::getAllChapters($r->{ce}); 306 unshift @chaps, $default_chap; 307 my $chapter_selected = $r->param('library_chapters') || $default_chap; 308 309 my @sects=(); 310 if ($chapter_selected ne $default_chap) { 311 @sects = WeBWorK::Utils::ListingDB::getAllSections($r->{ce}, $chapter_selected); 312 } 313 314 my @textbooks = ('Textbook info not ready'); 315 316 unshift @sects, $default_sect; 317 my $section_selected = $r->param('library_sections') || $default_sect; 318 my $view_problem_line = view_problems_line('lib_view', 'View Problems', $self->r); 319 320 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, 321 CGI::start_table(), 322 CGI::Tr( 323 CGI::td(["Chapter:", 324 CGI::popup_menu(-name=> 'library_chapters', 325 -values=>\@chaps, 326 -default=> $chapter_selected, 327 -onchange=>"submit();return true" 328 ), 329 CGI::submit(-name=>"lib_select_chapter", -value=>"Update Section List")])), 330 CGI::Tr( 331 CGI::td("Section:"), 332 CGI::td({-colspan=>2}, 333 CGI::popup_menu(-name=> 'library_sections', 334 -values=>\@sects, 335 -default=> $section_selected 336 ))), 337 338 #CGI::Tr( 339 # CGI::td("Textbook:"), 340 # CGI::td({-colspan=>2}, 341 # CGI::popup_menu(-name=> 'library_textbooks', 342 # -values=>\@textbooks, 343 # #-default=> $section_selected 344 #))), 345 346 #CGI::Tr( 347 # CGI::td("Keywords:"), 348 # CGI::td({-colspan=>2}, 349 # CGI::textfield(-name=>"keywords", 350 # -default=>"Keywords not implemented yet", 351 # -override=>1, -size=>60 352 #))), 353 CGI::Tr(CGI::td({-colspan=>3}, $view_problem_line)), 354 CGI::end_table(), 355 )); 356 } 357 358 sub make_top_row { 359 my $self = shift; 360 my $r = $self->r; 361 my $ce = $r->ce; 362 my %data = @_; 363 364 my $list_of_local_sets = $data{all_set_defs}; 365 my $have_local_sets = scalar(@$list_of_local_sets); 366 my $browse_which = $data{browse_which}; 367 my $library_selected = $r->param('library_sets'); 368 my $set_selected = $r->param('local_sets'); 369 370 my ($dis1, $dis2, $dis3) = ("","",""); 371 $dis1 = '-disabled' if($browse_which eq 'browse_library'); 372 $dis2 = '-disabled' if($browse_which eq 'browse_local'); 373 $dis3 = '-disabled' if($browse_which eq 'browse_mysets'); 374 375 ## Make buttons for additional problem libraries 376 my $libs = ''; 377 foreach my $lib (sort(keys(%problib))) { 378 $libs .= ' '. CGI::submit(-name=>"browse_$lib", -value=>$problib{$lib}, 379 ($browse_which eq "browse_$lib")? '-disabled': '') 380 if (-d "$ce->{courseDirs}{templates}/$lib"); 381 } 382 $libs = CGI::br()."or Problems from".$libs if $libs ne ''; 383 384 my $these_widths = "width: 20ex"; 385 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"center"}, 386 "Browse ", 387 CGI::submit(-name=>"browse_library", -value=>"Problem Library", -style=>$these_widths, $dis1), 388 CGI::submit(-name=>"browse_local", -value=>"Local Problems", -style=>$these_widths, $dis2), 389 CGI::submit(-name=>"browse_mysets", -value=>"From This Course", -style=>$these_widths, $dis3), 390 $libs, 391 )); 392 393 print CGI::Tr(CGI::td({-bgcolor=>"black"})); 394 395 if ($browse_which eq 'browse_local') { 396 $self->browse_local_panel($library_selected); 397 } elsif ($browse_which eq 'browse_mysets') { 398 $self->browse_mysets_panel($library_selected, $list_of_local_sets); 399 } elsif ($browse_which eq 'browse_library') { 400 $self->browse_library_panel(); 401 } else { ## handle other problem libraries 402 $self->browse_local_panel($library_selected,$browse_which); 403 } 404 405 print CGI::Tr(CGI::td({-bgcolor=>"black"})); 406 407 if($have_local_sets ==0) { 408 $list_of_local_sets = [NO_LOCAL_SET_STRING]; 409 } elsif (not $set_selected or $set_selected eq SELECT_SET_STRING) { 410 if ($list_of_local_sets->[0] eq "Select a Problem Set") { 411 shift @{$list_of_local_sets}; 412 } 413 unshift @{$list_of_local_sets}, SELECT_SET_STRING; 414 $set_selected = SELECT_SET_STRING; 415 } 416 my $myjs = 'document.mainform.selfassign.value=confirm("Should I assign the new set to you now?\nUse OK for yes and Cancel for no.");true;'; 417 418 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"left"}, "Adding Problems to ", 419 CGI::b("Target Set: "), 420 CGI::popup_menu(-name=> 'local_sets', 421 -values=>$list_of_local_sets, 422 -default=> $set_selected), 423 CGI::submit(-name=>"edit_local", -value=>"Edit Target Set"), 424 CGI::hidden(-name=>"selfassign", -default=>[0]). 425 CGI::br(), 426 CGI::br(), 427 CGI::submit(-name=>"new_local_set", -value=>"Create a New Set in This Course:", 428 -onclick=>$myjs 429 ), 430 " ", 431 CGI::textfield(-name=>"new_set_name", 432 -default=>"Name for new set here", 433 -override=>1, -size=>30), 434 CGI::br(), 435 )); 436 437 print CGI::Tr(CGI::td({-bgcolor=>"black"})); 438 439 print CGI::Tr(CGI::td({-class=>"InfoPanel", -align=>"center"}, 440 CGI::start_table({-border=>"0"}), 441 CGI::Tr( CGI::td({ -align=>"center"}, 442 CGI::submit(-name=>"select_all", -style=>$these_widths, 443 -value=>"Mark All For Adding"), 444 CGI::submit(-name=>"select_none", -style=>$these_widths, 445 -value=>"Clear All Marks"), 446 )), 447 CGI::Tr(CGI::td( 448 CGI::submit(-name=>"update", -style=>$these_widths. "; font-weight:bold", 449 -value=>"Update"), 450 CGI::submit(-name=>"rerandomize", 451 -style=>$these_widths, 452 -value=>"Rerandomize"), 453 CGI::submit(-name=>"cleardisplay", 454 -style=>$these_widths, 455 -value=>"Clear Problem Display") 456 )), 457 CGI::end_table())); 458 459 } 460 461 sub make_data_row { 462 my $self = shift; 463 my $sourceFileName = shift; 464 my $pg = shift; 465 my $cnt = shift; 466 my $mark = shift || 0; 467 468 $sourceFileName =~ s|^./||; # clean up top ugliness 469 470 my $urlpath = $self->r->urlpath; 471 my $problem_output = $pg->{flags}->{error_flag} ? 472 CGI::div({class=>"ResultsWithError"}, CGI::em("This problem produced an error")) 473 : CGI::div({class=>"RenderSolo"}, $pg->{body_text}); 474 475 476 my $edit_link = ''; 477 #if($self->{r}->param('browse_which') ne 'browse_library') { 478 my $problem_seed = $self->{r}->param('problem_seed') || 0; 479 if($sourceFileName !~ /^Library\//) { 480 $edit_link = CGI::a({href=>$self->systemLink( 481 $urlpath->newFromModule("WeBWorK::ContentGenerator::Instructor::PGProblemEditor", 482 courseID =>$urlpath->arg("courseID"), 483 setID=>"Undefined_Set", 484 problemID=>"1"), 485 params=>{sourceFilePath => "$sourceFileName", problemSeed=> $problem_seed} 486 )}, "Edit it" ); 487 } 488 489 my $try_link = CGI::a({href=>$self->systemLink( 490 $urlpath->newFromModule("WeBWorK::ContentGenerator::Problem", 491 courseID =>$urlpath->arg("courseID"), 492 setID=>"Undefined_Set", 493 problemID=>"1"), 494 params =>{ 495 effectiveUser => scalar($self->r->param('user')), 496 editMode => "SetMaker", 497 problemSeed=> $problem_seed, 498 sourceFilePath => "$sourceFileName" 499 } 500 )}, "Try it"); 501 502 my %add_box_data = ( -name=>"trial$cnt",-value=>1,-label=>"Add this problem to the current set on the next update"); 503 if($mark & SUCCESS) { 504 $add_box_data{ -label } .= " (just added this problem)"; 505 } elsif($mark & ADDED) { 506 $add_box_data{ -checked } = 1; 507 } 508 509 print CGI::Tr({-align=>"left"}, CGI::td( 510 CGI::div({-style=>"background-color: #DDDDDD; margin: 0px auto"}, 511 CGI::span({-style=>"float:left ; text-align: left"},"File name: $sourceFileName "), 512 CGI::span({-style=>"float:right ; text-align: right"}, $edit_link, " ", $try_link) 513 ), CGI::br(), 514 CGI::checkbox(-name=>"hideme$cnt",-value=>1,-label=>"Don't show this problem on the next update"), 515 CGI::br(), 516 CGI::checkbox((%add_box_data)), 517 CGI::hidden(-name=>"filetrial$cnt", -default=>[$sourceFileName]). 518 CGI::p($problem_output), 519 )); 520 } 521 522 523 sub pre_header_initialize { 524 my ($self) = @_; 525 my $r = $self->r; 526 ## For all cases, lets set some things 527 $self->{error}=0; 528 my $ce = $r->ce; 529 my $db = $r->db; 530 my $maxShown = $r->param('max_shown') || MAX_SHOW_DEFAULT; 531 $maxShown = 10000000 if($maxShown eq 'All'); # let's hope there aren't more 532 533 ## These directories will have individual buttons 534 %problib = %{$ce->{courseFiles}{problibs}} if $ce->{courseFiles}{problibs}; 535 536 my $userName = $r->param('user'); 537 my $user = $db->getUser($userName); # checked 538 die "record for user $userName (real user) does not exist." 539 unless defined $user; 540 my $authz = $r->authz; 541 unless ($authz->hasPermissions($userName, "modify_problem_sets")) { 542 return(""); # Error message already produced in the body 543 } 544 545 ## Now one action we have to deal with here 546 if ($r->param('edit_local')) { 547 my $urlpath = $r->urlpath; 548 my $db = $r->db; 549 my $checkset = $db->getGlobalSet($r->param('local_sets')); 550 if (not defined($checkset)) { 551 $self->{error} = 1; 552 $self->addbadmessage('You need to select a "Target Set" before you can edit it.'); 553 } else { 554 my $page = $urlpath->newFromModule('WeBWorK::ContentGenerator::Instructor::ProblemSetDetail', setID=>$r->param('local_sets'), courseID=>$urlpath->arg("courseID")); 555 my $url = $self->systemLink($page); 556 $self->reply_with_redirect($url); 557 } 558 } 559 560 ## Next, lots of set up so that errors can be reported with message() 561 562 ############# List of problems we have already printed 563 564 $self->{past_problems} = get_past_problem_files($r); 565 # if we don't end up reusing problems, this will be wiped out 566 # if we do redisplay the same problems, we must adjust this accordingly 567 my @past_marks = map {$_->[1]} @{$self->{past_problems}}; 568 my $none_shown = scalar(@{$self->{past_problems}})==0; 569 my @pg_files=(); 570 my $use_previous_problems = 1; 571 my $first_shown = $r->param('first_shown') || 0; 572 my $last_shown = $r->param('last_shown'); 573 if (not defined($last_shown)) { 574 $last_shown = -1; 575 } 576 my @all_past_list = (); # these are include requested, but not shown 577 my $j = 0; 578 while (defined($r->param("all_past_list$j"))) { 579 push @all_past_list, $r->param("all_past_list$j"); 580 $j++; 581 } 582 583 ############# Default of which problem selector to display 584 585 my $browse_which = $r->param('browse_which') || 'browse_local'; 586 587 my $problem_seed = $r->param('problem_seed') || 0; 588 $r->param('problem_seed', $problem_seed); # if it wasn't defined before 589 590 ## check for problem lib buttons 591 my $browse_lib = ''; 592 foreach my $lib (keys %problib) { 593 if ($r->param("browse_$lib")) { 594 $browse_lib = "browse_$lib"; 595 last; 596 } 597 } 598 599 ########### Start the logic through if elsif elsif ... 600 601 ##### Asked to browse certain problems 602 if ($browse_lib ne '') { 603 $browse_which = $browse_lib; 604 $r->param('library_sets', ""); 605 $use_previous_problems = 0; @pg_files = (); ## clear old problems 606 } elsif ($r->param('browse_library')) { 607 $browse_which = 'browse_library'; 608 $r->param('library_sets', ""); 609 $use_previous_problems = 0; @pg_files = (); ## clear old problems 610 } elsif ($r->param('browse_local')) { 611 $browse_which = 'browse_local'; 612 $r->param('library_sets', ""); 613 $use_previous_problems = 0; @pg_files = (); ## clear old problems 614 } elsif ($r->param('browse_mysets')) { 615 $browse_which = 'browse_mysets'; 616 $r->param('library_sets', ""); 617 $use_previous_problems = 0; @pg_files = (); ## clear old problems 618 619 ##### Change the seed value 620 621 } elsif ($r->param('rerandomize')) { 622 $problem_seed++; 623 $r->param('problem_seed', $problem_seed); 624 $self->addbadmessage('Changing the problem seed for display, but there are no problems showing.') if $none_shown; 625 626 ##### Clear the display 627 628 } elsif ($r->param('cleardisplay')) { 629 @pg_files = (); 630 $use_previous_problems=0; 631 $self->addbadmessage('The display was already cleared.') if $none_shown; 632 633 ##### View problems selected from the local list 634 635 } elsif ($r->param('view_local_set')) { 636 637 my $set_to_display = $r->param('library_sets'); 638 if (not defined($set_to_display) or $set_to_display eq SELECT_LOCAL_STRING or $set_to_display eq "Found no directories containing problems") { 639 $self->addbadmessage('You need to select a set to view.'); 640 } else { 641 $set_to_display = '.' if $set_to_display eq MY_PROBLEMS; 642 $set_to_display = substr($browse_which,7) if $set_to_display eq MAIN_PROBLEMS; 643 @pg_files = list_pg_files($ce->{courseDirs}->{templates}, 644 "$set_to_display"); 645 $use_previous_problems=0; 646 } 647 648 ##### View problems selected from the a set in this course 649 650 } elsif ($r->param('view_mysets_set')) { 651 652 my $set_to_display = $r->param('library_sets'); 653 if (not defined($set_to_display) 654 or $set_to_display eq "Select a Problem Set" 655 or $set_to_display eq NO_LOCAL_SET_STRING) { 656 $self->addbadmessage("You need to select a set from this course to view."); 657 } else { 658 my @problemList = $db->listGlobalProblems($set_to_display); 659 my $problem; 660 @pg_files=(); 661 for $problem (@problemList) { 662 my $problemRecord = $db->getGlobalProblem($set_to_display, $problem); # checked 663 die "global $problem for set $set_to_display not found." unless 664 $problemRecord; 665 push @pg_files, $problemRecord->source_file; 666 667 } 668 $use_previous_problems=0; 669 } 670 671 ##### View whole chapter from the library 672 ## This will change somewhat later 673 674 } elsif ($r->param('lib_view')) { 675 676 @pg_files=(); 677 my $chap = $r->param('library_chapters') || ""; 678 $chap = "" if($chap eq "All Chapters"); 679 my $sect = $r->param('library_sections') || ""; 680 $sect = "" if($sect eq "All Sections"); 681 my @dbsearch = WeBWorK::Utils::ListingDB::getSectionListings($r->{ce}, "$chap", "$sect"); 682 my ($result, $tolibpath); 683 for $result (@dbsearch) { 684 $tolibpath = "Library/$result->{path}/$result->{filename}"; 685 686 ## Too clunky!!!! 687 push @pg_files, $tolibpath; 688 } 689 $use_previous_problems=0; 690 691 ##### Edit the current local problem set 692 693 } elsif ($r->param('edit_local')) { ## Jump to set edit page 694 695 ; # already handled 696 697 698 ##### Make a new local problem set 699 700 } elsif ($r->param('new_local_set')) { 701 if ($r->param('new_set_name') !~ /^[\w.-]*$/) { 702 $self->addbadmessage("The name ".$r->param('new_set_name')." is not a valid set name. Use only letters, digits, -, _, and ."); 703 } else { 704 my $newSetName = $r->param('new_set_name'); 705 $newSetName =~ s/^set//; 706 $newSetName =~ s/\.def$//; 707 $r->param('local_sets',$newSetName); 708 my $newSetRecord = $db->getGlobalSet($newSetName); 709 if (defined($newSetRecord)) { 710 $self->addbadmessage("The set name $newSetName is already in use. Pick a different name if you would like to start a new set."); 711 } else { # Do it! 712 $newSetRecord = $db->{set}->{record}->new(); 713 $newSetRecord->set_id($newSetName); 714 $newSetRecord->set_header(""); 715 $newSetRecord->hardcopy_header(""); 716 $newSetRecord->open_date(time()+60*60*24*7); # in one week 717 $newSetRecord->due_date(time()+60*60*24*7*2); # in two weeks 718 $newSetRecord->answer_date(time()+60*60*24*7*3); # in three weeks 719 eval {$db->addGlobalSet($newSetRecord)}; 720 $self->addgoodmessage("Set $newSetName has been created."); 721 my $selfassign = $r->param('selfassign') || ""; 722 $selfassign = "" if($selfassign =~ /false/i); # deal with javascript false 723 if($selfassign) { 724 $self->assignSetToUser($userName, $newSetRecord); 725 $self->addgoodmessage("Set $newSetName was assigned to $userName."); 726 } 727 } 728 } 729 730 ##### Add selected problems to the current local set 731 732 } elsif ($r->param('update')) { 733 ## first handle problems to be added before we hide them 734 my($localSet, @selected); 735 736 @pg_files = grep {($_->[1] & ADDED) != 0 } @{$self->{past_problems}}; 737 @selected = map {$_->[0]} @pg_files; 738 739 my @action_files = grep {$_->[1] > 0 } @{$self->{past_problems}}; 740 # There are now good reasons to do an update without selecting anything. 741 #if(scalar(@action_files) == 0) { 742 # $self->addbadmessage('Update requested, but no problems were marked.'); 743 #} 744 745 if (scalar(@selected)>0) { # if some are to be added, they need a place to go 746 $localSet = $r->param('local_sets'); 747 if (not defined($localSet) or 748 $localSet eq SELECT_SET_STRING or 749 $localSet eq NO_LOCAL_SET_STRING) { 750 $self->addbadmessage('You are trying to add problems to something, but you did not select a "Target Set" name as a target.'); 751 } else { 752 my $newSetRecord = $db->getGlobalSet($localSet); 753 if (not defined($newSetRecord)) { 754 $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."); 755 } else { 756 my $addcount = add_selected($self, $db, $localSet); 757 if($addcount > 0) { 758 $self->addgoodmessage("Added $addcount problem".(($addcount>1)?'s':''). 759 " to $localSet."); 760 } 761 } 762 } 763 } 764 ## now handle problems to be hidden 765 766 ## only keep the ones which are not hidden 767 @pg_files = grep {($_->[1] & HIDDEN) ==0 } @{$self->{past_problems}}; 768 @past_marks = map {$_->[1]} @pg_files; 769 @pg_files = map {$_->[0]} @pg_files; 770 @all_past_list = (@all_past_list[0..($first_shown-1)], 771 @pg_files, 772 @all_past_list[($last_shown+1)..(scalar(@all_past_list)-1)]); 773 $last_shown = $first_shown+$maxShown -1; 774 $last_shown = (scalar(@all_past_list)-1) if($last_shown>=scalar(@all_past_list)); 775 776 } elsif ($r->param('next_page')) { 777 $first_shown = $last_shown+1; 778 $last_shown = $first_shown+$maxShown-1; 779 $last_shown = (scalar(@all_past_list)-1) if($last_shown>=scalar(@all_past_list)); 780 @past_marks = (); 781 } elsif ($r->param('prev_page')) { 782 $last_shown = $first_shown-1; 783 $first_shown = $last_shown - $maxShown+1; 784 785 $first_shown = 0 if($first_shown<0); 786 @past_marks = (); 787 788 } elsif ($r->param('select_all')) { 789 @past_marks = map {1} @past_marks; 790 } elsif ($r->param('select_none')) { 791 @past_marks = (); 792 793 ##### No action requested, probably our first time here 794 795 } else { 796 #my $c = $r->connection; 797 #print "Debug info: ". $r->get_remote_host ."<p>". $c->remote_ip ; 798 ; 799 } ##### end of the if elsif ... 800 801 802 ############# List of local sets 803 804 my @all_set_defs = $db->listGlobalSets; 805 @all_set_defs = sortByName(undef, @all_set_defs); 806 807 if ($use_previous_problems) { 808 @pg_files = @all_past_list; 809 } else { 810 $first_shown = 0; 811 $last_shown = scalar(@pg_files)<$maxShown ? scalar(@pg_files) : $maxShown; 812 $last_shown--; # to make it an array index 813 @past_marks = (); 814 } 815 ############# Now store data in self for retreival by body 816 $self->{first_shown} = $first_shown; 817 $self->{last_shown} = $last_shown; 818 $self->{browse_which} = $browse_which; 819 $self->{problem_seed} = $problem_seed; 820 $self->{pg_files} = \@pg_files; 821 $self->{past_marks} = \@past_marks; 822 $self->{all_set_defs} = \@all_set_defs; 823 824 } 825 826 827 sub title { 828 return "Problem Set Maker"; 829 } 830 831 sub body { 832 my ($self) = @_; 833 834 my $r = $self->r; 835 my $ce = $r->ce; # course environment 836 my $db = $r->db; # database 837 my $j; # garden variety counter 838 839 my $userName = $r->param('user'); 840 841 my $user = $db->getUser($userName); # checked 842 die "record for user $userName (real user) does not exist." 843 unless defined $user; 844 845 ### Check that this is a professor 846 my $authz = $r->authz; 847 unless ($authz->hasPermissions($userName, "modify_problem_sets")) { 848 print "User $userName returned " . 849 $authz->hasPermissions($user, "modify_problem_sets") . 850 " for permission"; 851 return(CGI::div({class=>'ResultsWithError'}, 852 CGI::em("You are not authorized to access the Instructor tools."))); 853 } 854 855 ########## Extract information computed in pre_header_initialize 856 857 my $first_shown = $self->{first_shown}; 858 my $last_shown = $self->{last_shown}; 859 my $browse_which = $self->{browse_which}; 860 my $problem_seed = $self->{problem_seed}; 861 my @pg_files = @{$self->{pg_files}}; 862 my @all_set_defs = @{$self->{all_set_defs}}; 863 864 my @pg_html=($last_shown>=$first_shown) ? 865 renderProblems(r=> $r, 866 user => $user, 867 problem_list => [@pg_files[$first_shown..$last_shown]], 868 displayMode => $r->param('mydisplayMode')) : (); 869 870 ########## Top part 871 print CGI::startform({-method=>"POST", -action=>$r->uri, -name=>'mainform'}), 872 $self->hidden_authen_fields, 873 '<div align="center">', 874 CGI::start_table({-border=>2}); 875 $self->make_top_row('all_set_defs'=>\@all_set_defs, 876 'browse_which'=> $browse_which); 877 print CGI::hidden(-name=>'browse_which', -default=>[$browse_which]), 878 CGI::hidden(-name=>'problem_seed', -default=>[$problem_seed]); 879 for ($j = 0 ; $j < scalar(@pg_files) ; $j++) { 880 print CGI::hidden(-name=>"all_past_list$j", -default=>$pg_files[$j]); 881 } 882 883 print CGI::hidden(-name=>'first_shown', -default=>[$first_shown]); 884 print CGI::hidden(-name=>'last_shown', -default=>[$last_shown]); 885 886 887 ########## Now print problems 888 my $jj; 889 for ($jj=0; $jj<scalar(@pg_html); $jj++) { 890 $pg_files[$jj] =~ s|^$ce->{courseDirs}->{templates}/?||; 891 $self->make_data_row($pg_files[$jj+$first_shown], $pg_html[$jj], $jj+1, $self->{past_marks}->[$jj]); 892 } 893 894 ########## Finish things off 895 print CGI::end_table(); 896 print '</div>'; 897 # if($first_shown>0 or (1+$last_shown)<scalar(@pg_files)) { 898 my ($next_button, $prev_button) = ("", ""); 899 if ($first_shown > 0) { 900 $prev_button = CGI::submit(-name=>"prev_page", -style=>"width:15ex", 901 -value=>"Previous page"); 902 } 903 if ((1+$last_shown)<scalar(@pg_files)) { 904 $next_button = CGI::submit(-name=>"next_page", -style=>"width:15ex", 905 -value=>"Next page"); 906 } 907 if (scalar(@pg_files)>0) { 908 print CGI::p(($first_shown+1)."-".($last_shown+1)." of ".scalar(@pg_files). 909 " shown.", $prev_button, " ", $next_button); 910 } 911 # } 912 print CGI::endform(), "\n"; 913 914 return ""; 915 } 916 917 ############################################## End of Body 918 919 # SKEL: To emit your own HTTP header, uncomment this: 920 # 921 #sub header { 922 # my ($self) = @_; 923 # 924 # # Generate your HTTP header here. 925 # 926 # # If you return something, it will be used as the HTTP status code for this 927 # # request. The Apache::Constants module might be useful for gerating status 928 # # codes. If you don't return anything, the status code "OK" will be used. 929 # return ""; 930 #} 931 932 # SKEL: If you need to do any processing after the HTTP header is sent, but before 933 # any template processing occurs, or you need to calculate values that will be 934 # used in multiple methods, do it in this method: 935 # 936 #sub initialize { 937 #my ($self) = @_; 938 #} 939 940 # SKEL: If you need to add tags to the document <HEAD>, uncomment this method: 941 # 942 #sub head { 943 # my ($self) = @_; 944 # 945 # # You can print head tags here, like <META>, <SCRIPT>, etc. 946 # 947 # return ""; 948 #} 949 950 # SKEL: To fill in the "info" box (to the right of the main body), use this 951 # method: 952 # 953 #sub info { 954 # my ($self) = @_; 955 # 956 # # Print HTML here. 957 # 958 # return ""; 959 #} 960 961 # SKEL: To provide navigation links, use this method: 962 # 963 #sub nav { 964 # my ($self, $args) = @_; 965 # 966 # # See the documentation of path() and pathMacro() in 967 # # WeBWorK::ContentGenerator for more information. 968 # 969 # return ""; 970 #} 971 972 # SKEL: For a little box for display options, etc., use this method: 973 # 974 #sub options { 975 # my ($self) = @_; 976 # 977 # # Print HTML here. 978 # 979 # return ""; 980 #} 981 982 # SKEL: For a list of sibling objects, use this method: 983 # 984 #sub siblings { 985 # my ($self, $args) = @_; 986 # 987 # # See the documentation of siblings() and siblingsMacro() in 988 # # WeBWorK::ContentGenerator for more information. 989 # # 990 # # Refer to implementations in ProblemSet and Problem. 991 # 992 # return ""; 993 #} 994 995 =head1 AUTHOR 996 997 Written by John Jones, jj (at) asu.edu. 998 999 =cut 1000 1001 1002 1003 1;
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |