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