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