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