File:  [LON-CAPA] / loncom / imspackages / imsprocessor.pm
Revision 1.17: download - view: text, annotated - select for diffs
Wed Mar 23 19:54:27 2005 UTC (19 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Clean up. Remove or replace debugging to STDERR with logging via &logthis(). Cetegory info and settings hashes for webct4 question database need to be passed by reference to process_assessment() because questionDB.xml is only processed the first time process_assessment() is called.  Change to imsmanifest parser so quiz_properties attributes are stored.

    1: # Copyright Michigan State University Board of Trustees
    2: #
    3: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    4: #
    5: # LON-CAPA is free software; you can redistribute it and/or modify
    6: # it under the terms of the GNU General Public License as published by
    7: # the Free Software Foundation; either version 2 of the License, or
    8: # (at your option) any later version.
    9: #
   10: # LON-CAPA is distributed in the hope that it will be useful,
   11: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   12: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13: # GNU General Public License for more details.
   14: #
   15: # You should have received a copy of the GNU General Public License
   16: # along with LON-CAPA; if not, write to the Free Software
   17: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   18: #
   19: # /home/httpd/html/adm/gpl.txt
   20: #
   21: # http://www.lon-capa.org/
   22: #
   23: 
   24: package Apache::imsprocessor;
   25: 
   26: use Apache::lonnet;
   27: use LONCAPA::Configuration;
   28: use strict;
   29: 
   30: sub ims_config {
   31:     my ($areas,$cmsmap,$areaname) = @_;
   32:     @{$areas} = ("doc","extlink","announce","staff","board","quiz","survey","pool","users");
   33:     %{$$cmsmap{bb5}} = (
   34:                 announce => 'resource/x-bb-announcement',
   35:                 board => 'resource/x-bb-discussionboard',
   36:                 doc => 'resource/x-bb-document',
   37:                 extlink => 'resource/x-bb-externallink',
   38:                 pool => 'assessment/x-bb-pool',
   39:                 quiz => 'assessment/x-bb-quiz',
   40:                 staff => 'resource/x-bb-staffinfo',
   41:                 survey => 'assessment/x-bb-survey',
   42:                 users => 'course/x-bb-user',
   43:                 );
   44:     %{$$cmsmap{bb6}} =  %{$$cmsmap{bb5}};
   45:     $$cmsmap{bb6}{conference} = 'resource/x-bb-conference';
   46:     %{$$cmsmap{angel}} =  (
   47:                 board => 'BOARD',
   48:                 extlink => 'LINK',
   49:                 msg => 'MESSAGE',
   50:                 quiz => 'QUIZ',
   51:                 survey => 'FORM',
   52:                 );
   53:     @{$$cmsmap{angel}{doc}} = ('FILE','PAGE');
   54:     %{$$cmsmap{webct4}} = (
   55:                 quiz => 'webctquiz',
   56:                 survey => 'webctsurvey',
   57:                 doc => 'webcontent'
   58:                 );
   59:     %{$areaname} = (
   60:                 announce => 'Announcements',
   61:                 board => 'Discussion Boards',
   62:                 doc => 'Documents, pages, and folders',
   63:                 extlink => 'Links to external sites',
   64:                 pool => 'Question pools',
   65:                 quiz => 'Quizzes',
   66:                 staff => 'Staff information',
   67:                 survey => 'Surveys',
   68:                 users => 'Enrollment',
   69:                 );
   70: }
   71:  
   72: sub create_tempdir {
   73:     my ($context,$pathinfo,$timenow) = @_;   
   74:     my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
   75:     my $tempdir;
   76:     if ($context eq 'DOCS') {
   77:         $tempdir =  $$configvars{'lonDaemons'}.'/tmp/'.$pathinfo;
   78:         if (!-e "$tempdir") {
   79:             mkdir("$tempdir",0770);
   80:         } 
   81:         $tempdir .= '/'.$timenow;
   82:         if (!-e "$tempdir") {
   83:             mkdir("$tempdir",0770);
   84:         } 
   85:     } elsif ($context eq "CSTR") {
   86:         if (!-e "$pathinfo/temp") {
   87:             mkdir("$pathinfo/temp",0770);
   88:         }
   89:         $tempdir =  $pathinfo.'/temp';
   90:     }
   91:     return $tempdir;
   92: }
   93: 
   94: sub uploadzip {
   95:     my ($context,$tempdir,$source) = @_;
   96:     my $fname;
   97:     if ($context eq 'DOCS') {
   98:         $fname=$ENV{'form.uploadname.filename'};
   99: # Replace Windows backslashes by forward slashes
  100:         $fname=~s/\\/\//g;
  101: # Get rid of everything but the actual filename
  102:         $fname=~s/^.*\/([^\/]+)$/$1/;
  103: # Replace spaces by underscores
  104:         $fname=~s/\s+/\_/g;
  105: # Replace all other weird characters by nothing
  106:         $fname=~s/[^\w\.\-]//g;
  107: # See if there is anything left
  108:         unless ($fname) { return 'error: no uploaded file'; }
  109: # Save the file
  110:         chomp($ENV{'form.uploadname'});
  111:         open(my $fh,'>'.$tempdir.'/'.$fname);
  112:         print $fh $ENV{'form.uploadname'};
  113:         close($fh);
  114:     } elsif ($context eq 'CSTR') {
  115:         if ($source =~ m/\/([^\/]+)$/) {
  116:             $fname = $1;
  117:             my $destination = $tempdir.'/'.$fname;
  118:             rename($source,$destination);
  119:         }
  120:     }
  121:     return $fname;   
  122: }
  123: 
  124: sub expand_zip {
  125:     my ($tempdir,$filename) = @_;
  126:     my $zipfile = "$tempdir/$filename";
  127:     if (!-e "$zipfile") {
  128:         return 'no zip';
  129:     }
  130:     if ($filename =~ m|\.zip$|i) {
  131:     # unzip can cause an sh launch which can pass along all of %ENV
  132:     # which can be too large for /bin/sh to handle
  133:         my %oldENV=%ENV;
  134:         undef(%ENV);
  135:         open(OUTPUT, "unzip -o $zipfile -d $tempdir  2> /dev/null |");
  136:         close(OUTPUT);
  137:         %ENV=%oldENV;
  138:         undef(%oldENV);
  139:     } else {
  140:         return 'nozip';
  141:     }
  142:     if ($filename =~ m|\.zip$|i) {
  143:         unlink($zipfile);
  144:     }
  145:     return 'ok';
  146: }
  147: 
  148: sub process_manifest {
  149:     my ($cms,$tempdir,$resources,$items,$hrefs,$resinfo,$phase,$includedres,$includeditems) = @_;
  150:     my %toc = (
  151:               bb6 => 'organization',
  152:               bb5 => 'tableofcontents',
  153:               angel => 'organization',
  154:               webct4 => 'organization',
  155:               );
  156:     my %contents = ();
  157:     my @state = ();
  158:     my $itm = '';
  159:     my $identifier = '';
  160:     my @seq = "Top";
  161:     my $lastitem;
  162:     %{$$items{'Top'}} = (
  163:                       contentscount => 0,
  164:                       resnum => 'toplevel',
  165:                       );
  166:     %{$$resources{'toplevel'}} = (
  167:                                   revitm => 'Top'
  168:                                  );
  169:  
  170:     if ($cms eq 'angel') {
  171:         $$resources{'toplevel'}{type} = "FOLDER";
  172:     } elsif ($cms eq 'bb5' || $cms eq 'bb6') {
  173:         $$resources{'toplevel'}{type} = 'resource/x-bb-document';
  174:     } else {
  175:         $$resources{'toplevel'}{type} = 'webcontent';
  176:     }
  177: 
  178:     unless (-e "$tempdir/imsmanifest.xml") {
  179:         return 'nomanifest';
  180:     }
  181: 
  182:     my $xmlfile = $tempdir.'/imsmanifest.xml';
  183:     my $p = HTML::Parser->new
  184:     (
  185:        xml_mode => 1,
  186:        start_h =>
  187:            [sub {
  188:                 my ($tagname, $attr) = @_;
  189:                 push @state, $tagname;
  190:                 my $start = @state - 3;
  191:                 if ( ($state[0] eq "manifest") && ($state[1] eq "organizations") && ($state[2] eq $toc{$cms}) ) {
  192:                     if ($state[-1] eq 'item') {
  193:                         $itm = $attr->{identifier};
  194:                         if ($$includeditems{$itm} || $phase ne 'build') {
  195:                             %{$$items{$itm}} = ();
  196:                             $$items{$itm}{contentscount} = 0;
  197:                             @{$$items{$itm}{contents}} = ();
  198:                             if ($cms eq 'bb5' || $cms eq 'bb6' || $cms eq 'webct4') {
  199:                                 $$items{$itm}{resnum} = $attr->{identifierref};
  200:                                 if ($cms eq 'bb5') {
  201:                                     $$items{$itm}{title} = $attr->{title};
  202:                                 }
  203:                             } elsif ($cms eq 'angel') {
  204:                                 if ($attr->{identifierref} =~ m/^res(.+)$/) {
  205:                                     $$items{$itm}{resnum} = $1;
  206:                                 }
  207:                             }
  208:                             unless (defined(%{$$resources{$$items{$itm}{resnum}}}) ) {
  209:                                 %{$$resources{$$items{$itm}{resnum}}} = ();
  210:                             }
  211:                             $$resources{$$items{$itm}{resnum}}{revitm} = $itm;
  212:                             if ($start > @seq) {
  213:                                 unless ($lastitem eq '') {
  214:                                     push @seq, $lastitem;
  215:                                     unless ( defined($contents{$seq[-1]}) ) {
  216:                                         @{$contents{$seq[-1]}} = ();
  217:                                     }
  218:                                     push @{$contents{$seq[-1]}},$itm;
  219:                                     $$items{$itm}{parentseq} = $seq[-1];
  220:                                 }
  221:                             } elsif ($start < @seq) {
  222:                                 my $diff = @seq - $start;
  223:                                 while ($diff > 0) {
  224:                                     pop @seq;
  225:                                     $diff --;
  226:                                 }
  227:                                 if (@seq) {
  228:                                     push @{$contents{$seq[-1]}}, $itm;
  229:                                 }
  230:                             } else {
  231:                                 push @{$contents{$seq[-1]}}, $itm;
  232:                             }
  233:                             my $path;
  234:                             if (@seq > 1) {
  235:                                 $path = join(',',@seq);
  236:                             } elsif (@seq > 0) {
  237:                                 $path = $seq[0];
  238:                             }
  239:                             $$items{$itm}{filepath} = $path;
  240:                             if ($cms eq 'bb5' || $cms eq 'bb6') {
  241:                                 if ($$items{$itm}{filepath} eq 'Top') {
  242:                                     $$items{$itm}{resnum} = $itm;
  243:                                     $$resources{$$items{$itm}{resnum}}{type} = 'resource/x-bb-document';
  244:                                     $$resources{$$items{$itm}{resnum}}{revitm} = $itm;
  245:                                     $$resinfo{$$items{$itm}{resnum}}{'isfolder'} = 'true';
  246:                                 }
  247:                             }
  248:                             $$items{$seq[-1]}{contentscount} ++;
  249:                             $lastitem = $itm;
  250:                         }
  251:                     }
  252:                     if ($cms eq 'webct4') {
  253:                         if (($state[-1] eq "webct:properties") && (@state > 4)) {
  254:                             $$items{$itm}{properties} = $attr->{identifierref};
  255:                         }
  256:                     }
  257:                 } elsif ("@state" eq "manifest resources resource" ) {
  258:                     $identifier = $attr->{identifier};
  259:                     if ($$includedres{$identifier} || $phase ne 'build') { 
  260:                         if ($cms eq 'bb5' || $cms eq 'bb6') {
  261:                             $$resources{$identifier}{file} = $attr->{file};
  262:                             $$resources{$identifier}{type} = $attr->{type};
  263:                         } elsif ($cms eq 'webct4') {
  264:                             $$resources{$identifier}{type} = $attr->{type};
  265:                             $$resources{$identifier}{file} = $attr->{href};
  266:                         } elsif ($cms eq 'angel') {
  267:                             $identifier = substr($identifier,3);
  268:                             if ($attr->{href} =~ m-^_assoc/$identifier/(.+)$-) {
  269:                                 $$resources{$identifier}{file} = $1;
  270:                             }
  271:                         }
  272:                         @{$$hrefs{$identifier}} = ();
  273:                     }
  274:                 } elsif ("@state" eq "manifest resources resource file") {
  275:                     if ($$includedres{$identifier} || $phase ne 'build') {
  276:                         if ($cms eq 'bb5' || $cms eq 'bb6' || $cms eq 'webct4') {
  277:                             push @{$$hrefs{$identifier}},$attr->{href};
  278:                         } elsif ($cms eq 'angel') {
  279:                             if ($attr->{href} =~ m/^_assoc\\$identifier\\(.+)$/) {
  280:                                 push @{$$hrefs{$identifier}},$1;
  281:                             } elsif ($attr->{href} =~ m/^Icons\\icon(\w+)\.gif/) {
  282:                                 $$resources{$identifier}{type} = $1;
  283:                             }
  284:                         }
  285:                     }
  286:                 }
  287:            }, "tagname, attr"],
  288:         text_h =>
  289:             [sub {
  290:                 my ($text) = @_;
  291:                 if ("@state" eq "manifest metadata lom general title langstring") {
  292:                     $$items{'Top'}{title} = $text;
  293:                 }
  294:                 if ($state[0] eq "manifest" && $state[1] eq "organizations" && $state[2] eq $toc{$cms} && $state[-1] eq "title") {
  295:                     if ($$includeditems{$itm} || $phase ne 'build') {
  296:                         if ($cms eq 'angel' || $cms eq 'bb6') {
  297:                             $$items{$itm}{title} = $text;
  298:                         }
  299:                         if ($cms eq 'webct4') {
  300:                             $$items{$itm}{title} = $text;
  301:                             $$items{$itm}{title} =~ s/(<[^>]*>)//g;
  302:                         }
  303:                     }
  304:                 }
  305:               }, "dtext"],
  306:         end_h =>
  307:               [sub {
  308:                   my ($tagname) = @_;
  309:                   pop @state;
  310:                }, "tagname"],
  311:     );
  312:     $p->parse_file($xmlfile);
  313:     $p->eof;
  314: 
  315:     foreach my $itm (keys %contents) {
  316:         @{$$items{$itm}{contents}} = @{$contents{$itm}};
  317:     }
  318:     return 'ok' ;
  319: }
  320: 
  321: sub get_imports {
  322:     my ($includeditems,$items,$resources,$importareas,$itm) = @_;
  323:     if (exists($$items{$itm}{resnum})) {
  324:         if ($$importareas{$$resources{$$items{$itm}{resnum}}{type}}) {
  325:             unless (exists($$includeditems{$itm})) {
  326:                 $$includeditems{$itm} = 1;
  327:             }
  328:         }
  329:     }
  330:     if ($$items{$itm}{contentscount} > 0) {
  331:         foreach my $child (@{$$items{$itm}{contents}}) {
  332:             &get_imports($includeditems,$items,$resources,$importareas,$child);
  333:         }
  334:     }
  335: }
  336: 
  337: sub get_parents {
  338:     my ($includeditems,$items,$itm) = @_;
  339:     my @pathitems = ();
  340:     if ($$items{$itm}{filepath} =~ m/,/) {
  341:        @pathitems = split/,/,$$items{$itm}{filepath};
  342:     } else {
  343:        $pathitems[0] = $$items{$itm}{filepath};
  344:     }
  345:     foreach (@pathitems) {
  346:         $$includeditems{$_} = 1;
  347:     }
  348: }
  349: 
  350: sub target_resources {
  351:     my ($resources,$oktypes,$targets) = @_;
  352:     foreach my $key (keys %{$resources}) {
  353:         if ( defined($$oktypes{$$resources{$key}{type}}) ) {
  354:             push @{$targets}, $key;
  355:         }
  356:     }
  357:     return;
  358: }
  359: 
  360: sub copy_resources {
  361:     my ($context,$cms,$hrefs,$tempdir,$targets,$url,$crs,$cdom,$chome,$destdir,$timenow) = @_;
  362:     if ($context eq 'DOCS') {
  363:         foreach my $key (sort keys %{$hrefs}) {
  364:             if (grep/^$key$/,@{$targets}) {
  365:                 %{$$url{$key}} = ();
  366:                 foreach my $file (@{$$hrefs{$key}}) {
  367:                     my $source = $tempdir.'/'.$key.'/'.$file;
  368:                     if ($cms eq 'webct4') {
  369:                         $source = $tempdir.'/'.$file;
  370:                     }
  371:                     my $filename = '';
  372:                     my $fpath = $timenow.'/resfiles/'.$key.'/';
  373:                     if ($cms eq 'angel') {
  374:                         if ($file eq 'pg'.$key.'.htm') {
  375:                             next;
  376:                         }
  377:                     }
  378:                     $file =~ s-\\-/-g;
  379:                     my $copyfile = $file;
  380:                     if ($cms eq 'webct4') {
  381:                         if ($file =~ m-/my_files/(.+)$-) {
  382:                             $copyfile = $1;
  383:                         }
  384:                     }
  385:                     unless (($cms eq 'webct4') && ($copyfile =~ m/questionDB\.xml$/ || $copyfile =~ m/quiz_QIZ_\d+\.xml$/ || $copyfile =~ m/properties_QIZ_\d+\.xml$/)) {
  386:                         $copyfile = $fpath.$copyfile;
  387:                         my $fileresult;
  388:                         if (-e $source) {
  389:                             $fileresult = &Apache::lonnet::process_coursefile('copy',$crs,$cdom,$chome,$copyfile,$source);
  390:                         }
  391:                     }
  392:                 }
  393:             }
  394:         }
  395:     } elsif ($context eq 'CSTR') {
  396:         if (!-e "$destdir/resfiles") {
  397:             mkdir("$destdir/resfiles",0770);
  398:         }
  399:         foreach my $key (sort keys %{$hrefs}) {
  400:             if (grep/^$key$/,@{$targets}) {
  401:                 foreach my $file (@{$$hrefs{$key}}) {
  402:                     $file =~ s-\\-/-g;
  403:                     if ( ($cms eq 'angel' && $file ne 'pg'.$key.'.htm') || ($cms eq 'bb5') || ($cms eq 'bb6')) {
  404:                         if (!-e "$destdir/resfiles/$key") {
  405:                             mkdir("$destdir/resfiles/$key",0770);
  406:                         }
  407:                         my $filepath = $file;
  408:                         my $front = '';
  409:                         while ($filepath =~ m-(\w+)/(.+)-) {
  410:                             $front .= $1.'/';
  411:                             $filepath = $2;
  412:                             my $fulldir = "$destdir/resfiles/$key/$front";
  413:                             chop($fulldir);
  414:                             if (!-e "$fulldir") {
  415:                                 mkdir("$fulldir",0770);
  416:                             }
  417:                         }
  418:                         if ($cms eq 'angel') {
  419:                             rename("$tempdir/_assoc/$key/$file","$destdir/resfiles/$key/$file");
  420:                         } elsif ($cms eq 'bb5' || $cms eq 'bb6') {
  421:                             rename("$tempdir/$key/$file","$destdir/resfiles/$key/$file");
  422:                         }
  423:                     } elsif ($cms eq 'webct4') {
  424:                         if ($file =~ m-/my_files/(.+)$-) {
  425:                             my $copyfile = $1;
  426:                             if ($copyfile =~ m-^[^/]+/[^/]+-) {
  427:                                 my @dirs = split/\//,$copyfile;
  428:                                 my $path = "$destdir/resfiles";
  429:                                 while (@dirs > 1) {
  430:                                     $path .= '/'.$dirs[0];
  431:                                     if (!-e "$path") {
  432:                                         mkdir("$path",0755);
  433:                                     }
  434:                                     shift @dirs;
  435:                                 }
  436:                             }
  437:                             if (-e "$tempdir/$file") {
  438:                                 rename("$tempdir/$file","$destdir/resfiles/$copyfile");
  439:                             }
  440:                         } elsif ($file !~ m-/data/(.+)$-) {
  441:                             &Apache::lonnet::logthis("IMS import error: WebCT4 - file $file is in unexpected location");
  442:                         }
  443:                     }
  444:                 }
  445:             }
  446:         }
  447:     }
  448: }
  449: 
  450: sub process_resinfo {
  451:     my ($cms,$context,$docroot,$destdir,$items,$resources,$targets,$boards,$announcements,$quizzes,$surveys,$pools,$groups,$messages,$timestamp,$boardnum,$resinfo,$udom,$uname,$cdom,$crs,$db_handling,$user_handling,$total,$dirname,$seqstem,$resrcfiles,$packages,$hrefs,$pagesfiles,$sequencesfiles,$randompicks) = @_;
  452:     my $board_id = time;
  453:     my $board_count = 0;
  454:     my $dbparse = 0;
  455:     my $announce_handling = 'include';
  456:     my $longcrs = '';
  457:     my %qzdbsettings = ();
  458:     my %catinfo = ();
  459:     if ($crs =~ m/^(\d)(\d)(\d)/) {
  460:         $longcrs = $1.'/'.$2.'/'.$3.'/'.$crs;
  461:     }
  462:     if ($context eq 'CSTR') {
  463:         if (!-e "$destdir/resfiles") {
  464:             mkdir("$destdir/resfiles",0770);
  465:         }
  466:     }
  467:     if ($cms eq 'angel') {
  468:         my $currboard = '';
  469:         foreach my $key (sort keys %{$resources}) {
  470:           if (grep/^$key$/,@{$targets}) {
  471:             if ($$resources{$key}{type} eq "BOARD") {
  472:                 push @{$boards}, $key;
  473:                 $$boardnum{$$resources{$key}{revitm}} = $board_count;
  474:                 $currboard = $key;
  475:                 @{$$messages{$key}} = ();
  476:                 $$timestamp[$board_count] = $board_id;
  477:                 $board_id ++;
  478:                 $board_count ++;
  479:             } elsif ($$resources{$key}{type} eq "MESSAGE") {
  480:                 push @{$$messages{$currboard}}, $key;
  481:             } elsif ($$resources{$key}{type} eq "PAGE" || $$resources{$key}{type} eq "LINK") {
  482:                 %{$$resinfo{$key}} = ();
  483:                 &angel_content($key,$docroot,$destdir,\%{$$resinfo{$key}},$udom,$uname,$$resources{$key}{type},$$items{$$resources{$key}{revitm}}{title},$resrcfiles);
  484:             } elsif ($$resources{$key}{type} eq "QUIZ") {
  485:                 %{$$resinfo{$key}} = ();
  486:                 push @{$quizzes}, $key;
  487: #               &angel_assessment($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
  488:             } elsif ($$resources{$key}{type} eq "FORM") {
  489:                 %{$$resinfo{$key}} = ();
  490:                 push @{$surveys}, $key;
  491: #                &angel_assessment($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
  492:             } elsif ($$resources{$key}{type} eq "DROPBOX") {
  493:                 %{$$resinfo{$key}} = ();
  494:             }
  495:           }
  496:         }
  497:     } elsif ($cms eq 'bb5' || $cms eq 'bb6') {
  498:         foreach my $key (sort keys %{$resources}) {
  499:           if (grep/^$key$/,@{$targets}) {
  500:             if ($$resources{$key}{type} eq "resource/x-bb-document") {
  501:                 unless ($$items{$$resources{$key}{revitm}}{filepath} eq 'Top') {
  502:                     %{$$resinfo{$key}} = ();
  503:                     &process_content($cms,$key,$context,$docroot,$destdir,\%{$$resinfo{$key}},$udom,$uname,$resrcfiles,$packages,$hrefs);
  504:                 }
  505:             } elsif ($$resources{$key}{type} eq "resource/x-bb-staffinfo") {
  506:                 %{$$resinfo{$key}} = ();
  507:                 &process_staff($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
  508:             } elsif ($$resources{$key}{type} eq "resource/x-bb-externallink") {
  509:                 %{$$resinfo{$key}} = ();
  510:                 &process_link($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
  511:             } elsif ($$resources{$key}{type} eq "resource/x-bb-discussionboard") {
  512:                 %{$$resinfo{$key}} = ();
  513:                 unless ($db_handling eq 'ignore') {
  514:                     push @{$boards}, $key;
  515:                     $$timestamp[$board_count] = $board_id;
  516:                     &process_db($key,$docroot,$destdir,$board_id,$crs,$cdom,$db_handling,$uname,\%{$$resinfo{$key}},$longcrs);
  517:                     $board_id ++;
  518:                     $board_count ++;
  519:                 }
  520:             } elsif ($$resources{$key}{type} eq "assessment/x-bb-pool") {
  521:                 %{$$resinfo{$key}} = ();
  522:                 &process_assessment($cms,$context,$key,$docroot,'pool',$dirname,$destdir,\%{$$resinfo{$key}},$total,$udom,$uname,$pagesfiles,$sequencesfiles,$randompicks,\$dbparse,$resources,$items,\%catinfo,\%qzdbsettings);
  523:                 push @{$pools}, $key;
  524:             } elsif ($$resources{$key}{type} eq "assessment/x-bb-quiz") {
  525:                 %{$$resinfo{$key}} = ();
  526:                 &process_assessment($cms,$context,$key,$docroot,'quiz',$dirname,$destdir,\%{$$resinfo{$key}},$total,$udom,$uname,$pagesfiles,$sequencesfiles,$randompicks,\$dbparse,$resources,$items,\%catinfo,\%qzdbsettings);
  527:                 push @{$quizzes}, $key;
  528:             } elsif ($$resources{$key}{type} eq "assessment/x-bb-survey") {
  529:                 %{$$resinfo{$key}} = ();
  530:                 &process_assessment($cms,$context,$key,$docroot,'survey',$dirname,$destdir,\%{$$resinfo{$key}},$total,$udom,$uname,$pagesfiles,$sequencesfiles,$randompicks,\$dbparse,$resources,$items,\%catinfo,\%qzdbsettings);
  531:                 push @{$surveys}, $key;
  532:             } elsif ($$resources{$key}{type} eq "assessment/x-bb-group") {
  533:                 %{$$resinfo{$key}} = ();
  534:                 push @{$groups}, $key;
  535:                 &process_group($key,$docroot,$destdir,\%{$$resinfo{$key}});
  536:             } elsif ($$resources{$key}{type} eq "resource/x-bb-user") {   
  537:                 %{$$resinfo{$key}} = ();
  538:                 unless ($user_handling eq 'ignore') {
  539:                     &process_user($key,$docroot,$destdir,\%{$$resinfo{$key}},$crs,$cdom,$user_handling);
  540:                 }
  541:             } elsif ($$resources{$key}{type} eq "resource/x-bb-announcement") {
  542:                 unless ($announce_handling eq 'ignore') {
  543:                     push @{$announcements}, $key;
  544:                     %{$$resinfo{$key}} = ();
  545:                     &process_announce($key,$docroot,$destdir,\%{$$resinfo{$key}},$resinfo,$seqstem,$resrcfiles);
  546:                 }
  547:             }
  548:           }
  549:         }
  550:         if (@{$announcements}) {
  551:             $$items{'Top'}{'contentscount'} ++;
  552:         }
  553:         if (@{$boards}) {
  554:             $$items{'Top'}{'contentscount'} ++;
  555:         }
  556:         if (@{$quizzes}) {
  557:             $$items{'Top'}{'contentscount'} ++;
  558:         }
  559:         if (@{$surveys}) {
  560:             $$items{'Top'}{'contentscount'} ++;
  561:         }
  562:         if (@{$pools}) {
  563:             $$items{'Top'}{'contentscount'} ++;
  564:         }
  565:     } elsif ($cms eq 'webct4') {
  566:         foreach my $key (sort keys %{$resources}) {
  567:             if (grep/^$key$/,@{$targets}) {
  568:                 if ($$resources{$key}{type} eq "webcontent") {
  569:                     %{$$resinfo{$key}} = ();
  570:                     &webct4_content($key,$docroot,$destdir,\%{$$resinfo{$key}},$udom,$uname,$$resources{$key}{type},$$items{$$resources{$key}{revitm}}{title},$resrcfiles);
  571:                 } elsif ($$resources{$key}{type} eq "webctquiz") {
  572:                     &process_assessment($cms,$context,$key,$docroot,'quiz',$dirname,$destdir,\%{$$resinfo{$key}},$total,$udom,$uname,$pagesfiles,$sequencesfiles,$randompicks,\$dbparse,$resources,$items,\%catinfo,\%qzdbsettings);
  573:                 }
  574:             }
  575:         }
  576:     }
  577: 
  578:     $$total{'board'} = $board_count;
  579:     $$total{'quiz'} = @{$quizzes};
  580:     $$total{'surv'} = @{$surveys};
  581:     $$total{'pool'} = @{$pools};
  582: }
  583: 
  584: sub build_structure {
  585:     my ($cms,$context,$destdir,$items,$resinfo,$resources,$targets,$hrefs,$udom,$uname,$newdir,$timenow,$cdom,$crs,$timestamp,$total,$boards,$announcements,$quizzes,$surveys,$pools,$boardnum,$pagesfiles,$seqfiles,$topurls,$topnames,$packages,$includeditems,$randompicks) = @_;
  586:     my %flag = ();
  587:     my %count = ();
  588:     my %pagecontents = ();
  589:     my %seqtext = ();
  590:     my $topnum = 0;
  591:     my $topspecials = @$announcements + @$boards + @$quizzes + @$surveys + @$pools;
  592: 
  593:     if (!-e "$destdir") {
  594:         mkdir("$destdir",0755);
  595:     }
  596:     if (!-e "$destdir/sequences") {
  597:         mkdir("$destdir/sequences",0770);
  598:     }
  599:     if (!-e "$destdir/resfiles") {
  600:         mkdir("$destdir/resfiles",0770);
  601:     }
  602:     if (!-e "$destdir/pages") {
  603:         mkdir("$destdir/pages",0770);
  604:     }
  605:     if (!-e "$destdir/problems") {
  606:         mkdir("$destdir/problems",0770);
  607:     }
  608: 
  609:     $seqtext{'Top'} = qq|<map>\n|;       
  610:     %{$$resinfo{$$items{'Top'}{resnum}}} = (
  611:                                          isfolder => 'true',
  612:                                         );
  613: 
  614:     my $srcstem = "";
  615:  
  616:     if ($context eq 'DOCS') {
  617:         $srcstem = "/uploaded/$cdom/$crs/$timenow";
  618:     } elsif ($context eq 'CSTR') {
  619:         $srcstem = "/res/$udom/$uname/$newdir";
  620:     }
  621: 
  622:     foreach my $key (sort keys %{$items}) {
  623:       if ($$includeditems{$key}) {
  624:         %{$flag{$key}} = (
  625:                           page => 0,
  626:                           seq => 0,
  627:                           board => 0,
  628:                           file => 0,
  629:                          );
  630: 
  631:         %{$count{$key}} = (
  632:                            page => -1,
  633:                            seq => 0,
  634:                            board => 0,
  635:                            file => 0,
  636:                           );
  637: 
  638:         my $src = "";
  639: 
  640:         my $next_id = 2;
  641:         my $curr_id = 1;
  642:         my $resnum = $$items{$key}{resnum};
  643:         my $type = $$resources{$resnum}{type};
  644:         my $contentscount = $$items{$key}{'contentscount'}; 
  645:         if (($cms eq 'angel' && $type eq "FOLDER") || (($cms eq 'bb5' || $cms eq 'bb6') && $$resinfo{$resnum}{'isfolder'} eq "true") && (($type eq "resource/x-bb-document") || ($type eq "resource/x-bb-staffinfo") || ($type eq "resource/x-bb-externallink")) || ($cms eq 'webct4' &&  $contentscount > 0)) {
  646:             unless (($cms eq 'bb5') && $key eq 'Top') {
  647:                 $seqtext{$key} = "<map>\n";
  648:             }
  649:             if ($contentscount == 0) {
  650: 	        if ($key eq 'Top') {
  651:                     unless ($topspecials) {
  652:                         $seqtext{$key} .= qq|<resource id="$curr_id" src="" type="start"></resource>
  653: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
  654: <resource id="$next_id" src="" type="finish"></resource>\n|;
  655:                     }
  656:                 } else {
  657:                     $seqtext{$key} .= qq|<resource id="$curr_id" src="" type="start"></resource>
  658: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
  659: <resource id="$next_id" src="" type="finish"></resource>\n|;
  660:                 }
  661:             } else {
  662:                 my $contcount = 0;
  663:                 if (defined($$items{$key}{contents})) { 
  664:                     $contcount = @{$$items{$key}{contents}};
  665:                 } else {
  666:                     &Apache::lonnet::logthis("IMS Import error for item: $key- contents count = $contentscount, but identity of contents not defined.");
  667:                 }
  668:                 my $contitem = $$items{$key}{contents}[0];
  669:                 my $contitemcount = $$items{$contitem}{contentscount}; 
  670:                 my ($res,$itm,$type,$file);
  671:                 if (exists($$items{$contitem}{resnum})) {
  672:                     $res = $$items{$contitem}{resnum};
  673:                     $itm = $$resources{$res}{revitm};
  674:                     $type = $$resources{$res}{type};
  675:                     $file = $$resources{$res}{file};
  676:                 }
  677:                 my $title = $$items{$contitem}{title};
  678:                 my $packageflag = 0;
  679:                 if (grep/^$res$/,@{$packages}) {
  680:                     $packageflag = 1;
  681:                 }
  682:                 $src = &make_structure($cms,$key,$srcstem,\%flag,\%count,$timestamp,$boardnum,$hrefs,\%pagecontents,$res,$type,$file,$resinfo,$contitem,$uname,$cdom,$contcount,$packageflag,$contitemcount);
  683:                 unless ($flag{$key}{page} == 1) {
  684:                     $seqtext{$key} .= qq|<resource id="$curr_id" src="$src" title="$title" type="start"|;
  685:                     unless ($flag{$key}{seq} || $flag{$key}{board} || $flag{$key}{file}) {
  686:                         $flag{$key}{page} = 1;
  687:                     }
  688:                     if ($key eq 'Top') {
  689:                         push @{$topurls}, $src;
  690:                         push @{$topnames}, $title;
  691:                     }
  692:                 }
  693:                 if ($contcount == 1) {
  694:                     $seqtext{$key} .= qq|></resource>
  695: <link from="$curr_id" to="$next_id" index="$curr_id"></link>|;
  696:                     if ($key eq 'Top') {
  697:                         unless ($topspecials) {
  698:                             $seqtext{$key} .= qq|
  699: <resource id="$next_id" src="" type="finish"></resource>\n|;
  700:                         }
  701:                     } else {
  702:                         $seqtext{$key} .= qq|
  703: <resource id="$next_id" src="" type="finish"></resource>\n|;
  704:                     }
  705:                 } else {
  706:                     if ($contcount > 2 ) {
  707:                         for (my $i=1; $i<$contcount-1; $i++) {
  708:                             my $contitem = $$items{$key}{contents}[$i];
  709:                             my $contitemcount = $$items{$contitem}{contentscount};
  710:                             my $res = $$items{$contitem}{resnum};
  711:                             my $type = $$resources{$res}{type};
  712:                             my $file = $$resources{$res}{file};
  713:                             my $title = $$items{$contitem}{title};
  714:                             my $packageflag = 0;
  715:                             if (grep/^$res$/,@{$packages}) {
  716:                                 $packageflag = 1;
  717:                             }
  718:                             $src = &make_structure($cms,$key,$srcstem,\%flag,\%count,$timestamp,$boardnum,$hrefs,\%pagecontents,$res,$type,$file,$resinfo,$contitem,$uname,$cdom,$contcount,$packageflag,$contitemcount);
  719: 
  720:                             unless ($flag{$key}{page} == 1) {
  721:                                 $seqtext{$key} .= qq|></resource>
  722: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
  723: <resource id="$next_id" src="$src" title="$title"|;
  724:                                 $curr_id ++;
  725:                                 $next_id ++;
  726:                                 unless ($flag{$key}{seq} || $flag{$key}{board} || $flag{$key}{file}) {
  727:                                     $flag{$key}{page} = 1;
  728:                                 }
  729:                                 if ($key eq 'Top') {
  730:                                     push @{$topurls}, $src;
  731:                                     push @{$topnames}, $title;
  732:                                 }
  733:                             }
  734:                         }
  735:                     }
  736:                     my $contitem = $$items{$key}{contents}[-1];
  737:                     my $contitemcount = $$items{$contitem}{contentscount};
  738:                     my $res = $$items{$contitem}{resnum};
  739:                     my $type = $$resources{$res}{type};
  740:                     my $file = $$resources{$res}{file};
  741:                     my $title = $$items{$contitem}{title};
  742:                     my $packageflag = 0;
  743:                     if (grep/^$res$/,@{$packages}) {
  744:                         $packageflag = 1;
  745:                     }
  746:                     $src = &make_structure($cms,$key,$srcstem,\%flag,\%count,$timestamp,$boardnum,$hrefs,\%pagecontents,$res,$type,$file,$resinfo,$contitem,$uname,$cdom,$contcount,$packageflag,$contitemcount);
  747: 
  748:                     if ($flag{$key}{page}) {
  749:                         if ($count{$key}{seq} + $count{$key}{page} + $count{$key}{board} + $count{$key}{file} +1 == 1) {
  750:                             $seqtext{$key} .= qq|></resource>
  751: <link from="$curr_id" index="$curr_id" to="$next_id">
  752: <resource id ="$next_id" src="" |;
  753:                         }
  754:                     } else {
  755:                         $seqtext{$key} .= qq|></resource>
  756: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
  757: <resource id="$next_id" src="$src" title="$title" |;
  758:                         if ($key eq 'Top') {
  759:                             push @{$topurls}, $src;
  760:                             push @{$topnames}, $title;
  761:                         }
  762:                     }
  763:                     if ($contcount == $$items{$key}{contentscount}) {
  764:                         $seqtext{$key} .= qq|type="finish"></resource>\n|;
  765:                     } else {
  766:                         $curr_id ++;
  767:                         $next_id ++;
  768:                         $seqtext{$key} .= qq|></resource>
  769: <link from="$curr_id" to="$next_id" index="$curr_id"></link>\n|;
  770:                     } 
  771:                 }
  772:             }
  773:             unless (($cms eq 'bb5') && $key eq 'Top') {
  774:                 $seqtext{$key} .= "</map>\n";
  775:                 open(LOCFILE,">$destdir/sequences/$key.sequence");
  776:                 print LOCFILE $seqtext{$key};
  777:                 close(LOCFILE);
  778:                 push @{$seqfiles}, "$key.sequence";
  779:             }
  780:             $count{$key}{page} ++;
  781:             $$total{page} += $count{$key}{page};
  782:         }
  783:         $$total{seq} += $count{$key}{seq};
  784:       }
  785:     }
  786:     $topnum += ($count{'Top'}{page} + $count{'Top'}{seq});
  787: 
  788:     if ($cms eq 'bb5' || $cms eq 'bb6') {
  789:         if (@{$announcements} > 0) {
  790:             &process_specials($context,'announcements',$announcements,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  791:         }
  792:         if (@{$boards} > 0) {
  793:             &process_specials($context,'boards',$boards,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  794:         }
  795:         if (@{$quizzes} > 0) {
  796:             &process_specials($context,'quizzes',$quizzes,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  797:         }
  798:         if (@{$surveys} > 0)  {
  799:             &process_specials($context,'surveys',$surveys,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  800:         }
  801:         if (@{$pools} > 0)  {
  802:             &process_specials($context,'pools',$pools,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  803:         }
  804:         $seqtext{'Top'} .= "</map>\n";
  805:         open(TOPFILE,">$destdir/sequences/Top.sequence");
  806:         print TOPFILE $seqtext{'Top'};
  807:         close(TOPFILE);
  808:         push @{$seqfiles}, 'Top.sequence';
  809:     }
  810: 
  811:     my $filestem;
  812:     if ($context eq 'DOCS') {
  813:         $filestem = "/uploaded/$cdom/$crs/$timenow";
  814:     } elsif ($context eq 'CSTR') {
  815:         $filestem = "/res/$udom/$uname/$newdir";
  816:     }
  817: 
  818:     foreach my $key (sort keys %pagecontents) {
  819:         for (my $i=0; $i<@{$pagecontents{$key}}; $i++) {
  820:             my $filename = $destdir.'/pages/'.$key.'_'.$i.'.page';
  821:             my $resource = "$filestem/resfiles/$$items{$pagecontents{$key}[$i][0]}{resnum}.html";
  822:             my $res = $$items{$pagecontents{$key}[$i][0]}{resnum};
  823:             my $resource = $filestem.'/resfiles/'.$res.'.html';
  824:             if (grep/^$res$/,@{$packages}) {
  825:                 $resource =  $filestem.'/resfiles/'.$res.'./index.html'; # should be entry_point
  826:             }
  827:             open(PAGEFILE,">$filename");
  828:             print PAGEFILE qq|<map>
  829: <resource src="$resource" id="1" type="start" title="$$items{$pagecontents{$key}[$i][0]}{title}"></resource>
  830: <link to="2" index="1" from="1">\n|;
  831:             if (@{$pagecontents{$key}[$i]} == 1) {
  832:                 print PAGEFILE qq|<resource src="" id="2" type="finish"></resource>\n|;
  833:             } elsif (@{$pagecontents{$key}[$i]} == 2)  {
  834:                 my $res = $$items{$pagecontents{$key}[$i][1]}{resnum};
  835:                 my $resource = $filestem.'/resfiles/'.$res.'.html';
  836:                 if (grep/^$res$/,@{$packages}) {
  837:                     $resource =  $filestem.'/resfiles/'.$res.'./index.html'; # should be entry_point
  838:                 }
  839:                 print PAGEFILE qq|<resource src="$resource" id="2" type="finish" title="$$items{$pagecontents{$key}[$i][1]}{title}"></resource>\n|;
  840:             } else {
  841:                 for (my $j=1; $j<@{$pagecontents{$key}[$i]}-1; $j++) {
  842:                     my $curr_id = $j+1;
  843:                     my $next_id = $j+2;
  844:                     my $res = $$items{$pagecontents{$key}[$i][$j]}{resnum};
  845:                     my $resource = $filestem.'/resfiles/'.$res.'.html';
  846:                     if (grep/^$res$/,@{$packages}) {
  847:                         $resource =  $filestem.'/resfiles/'.$res.'./index.html'; # entry_point
  848:                     }
  849:                     print PAGEFILE qq|<resource src="$resource" id="$curr_id" title="$$items{$pagecontents{$key}[$i][$j]}{title}"></resource>
  850: <link to="$next_id" index="$curr_id" from="$curr_id">\n|;
  851:                 }
  852:                 my $final_id = @{$pagecontents{$key}[$i]};
  853:                 my $res = $$items{$pagecontents{$key}[$i][-1]}{resnum};
  854:                 my $resource = $filestem.'/resfiles/'.$res.'.html';
  855:                 if (grep/^$res$/,@{$packages}) {
  856:                     $resource =  $filestem.'/resfiles/'.$res.'./index.html'; # entry_point
  857:                 }
  858:                 print PAGEFILE qq|<resource src="$resource" id="$final_id" type="finish" title="$$items{$pagecontents{$key}[$i][-1]}{title}"></resource>\n|;
  859:             }
  860:             print PAGEFILE "</map>";
  861:             close(PAGEFILE);
  862:             push @{$pagesfiles}, $key.'_'.$i.'.page'; 
  863:         }
  864:     }
  865: }
  866: 
  867: sub make_structure {
  868:     my ($cms,$key,$srcstem,$flag,$count,$timestamp,$boardnum,$hrefs,$pagecontents,$res,$type,$file,$resinfo,$contitem,$uname,$cdom,$contcount,$packageflag,$contitemcount) = @_;
  869:     my $src ='';
  870:     if (($cms eq 'angel' && $type eq 'FOLDER') || (($cms eq 'bb5' || $cms eq 'bb6') && (($$resinfo{$res}{'isfolder'} eq 'true') || $key eq 'Top')) || ($cms eq 'webct4' && $contitemcount > 0)) {
  871:         $src = $srcstem.'/sequences/'.$contitem.'.sequence';
  872:         $$flag{$key}{page} = 0;
  873:         $$flag{$key}{seq} = 1;
  874:         $$count{$key}{seq} ++;
  875:     } elsif ($cms eq 'angel' && $type eq 'BOARD') {
  876:         $src = '/adm/'.$cdom.'/'.$uname.'/'.$$timestamp[$$boardnum{$res}].'/bulletinboard'; 
  877:         $$flag{$key}{page} = 0;
  878:         $$flag{$key}{board} = 1;
  879:         $$count{$key}{board} ++;
  880:     } elsif ($cms eq 'angel' && $type eq "FILE") {
  881:         foreach my $file (@{$$hrefs{$res}}) {
  882:             unless ($file eq 'pg'.$res.'.htm') {
  883:                 $src = $srcstem.'/resfiles/'.$res.'/'.$file;
  884:             }
  885:         }
  886:         $$flag{$key}{page} = 0;
  887:         $$flag{$key}{file} = 1;
  888:     } elsif ($cms eq 'angel' && (($type eq "PAGE") || ($type eq "LINK")) )  {
  889:         if ($$flag{$key}{page}) {
  890:             if ($$count{$key}{page} == -1) {
  891:                 &Apache::lonnet::logthis("IMS Angel import error in array index for page: value = -1, resource is $key, type is $type.");
  892:             } else { 
  893:                 push @{$$pagecontents{$key}[$$count{$key}{page}]},$contitem;
  894:             }
  895:         } else {
  896:             $$count{$key}{page} ++;
  897:             $src = $srcstem.'/pages/'.$key.'_'.$$count{$key}{page}.'.page';
  898:             @{$$pagecontents{$key}[$$count{$key}{page}]} = ("$contitem");
  899:             $$flag{$key}{seq} = 0;
  900:         }
  901:     } elsif ($cms eq 'bb5' || $cms eq 'bb6') {
  902:         if ($$flag{$key}{page}) {
  903:             push @{$$pagecontents{$key}[$$count{$key}{page}]},$contitem;
  904:         } else {
  905:             if ($contcount == 1) {
  906:                 if ($packageflag) {
  907:                     $src = $srcstem.'/resfiles/'.$res.'/index.html'; # Needs to be entry point
  908:                 } else {
  909:                     $src = $srcstem.'/resfiles/'.$res.'.html';
  910:                 }
  911:             } else {
  912:                 $$count{$key}{page} ++;
  913:                 $src = $srcstem.'/pages/'.$key.'_'.$$count{$key}{page}.'.page';
  914:                 @{$$pagecontents{$key}[$$count{$key}{page}]} = ("$contitem");
  915:             }
  916:             $$flag{$key}{seq} = 0;
  917:         }
  918:     } elsif ($cms eq 'webct4') {
  919:         if ($type eq 'webctquiz') {
  920:             $src =  $srcstem.'/pages/'.$res.'.page';
  921:         } else {
  922:             if (grep/^$file$/,@{$$hrefs{$res}}) {
  923:                 my $filename;
  924:                 if ($file =~ m-/([^/]+)$-) {
  925:                     $filename = $1;
  926:                 }
  927:                 $src =  $srcstem.'/resfiles/'.$res.'/'.$filename;
  928:             } else {
  929:                 foreach my $file (@{$$hrefs{$res}}) {
  930:                     my $filename;
  931:                     if ($file =~ m-/([^/]+)$-) {
  932:                         $filename = $1;
  933:                     }
  934:                     $src = $srcstem.'/resfiles/'.$res.'/'.$filename;
  935:                 }
  936:             }
  937:             $$flag{$key}{page} = 0;
  938:             $$flag{$key}{file} = 1;
  939:         }
  940:     }
  941:     return $src;
  942: }
  943: 
  944: 
  945: # ---------------------------------------------------------------- Process Blackboard specials - announcements, bulletin boards, quizzes and surveys
  946: sub process_specials {
  947:     my ($context,$type,$specials,$topnum,$contentscount,$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,$seqtext,$pagesfiles,$seqfiles,$topurls,$topnames) = @_;
  948:     my $src = '';
  949:     my $specialsrc = '';
  950:     my $nextnum = 0;
  951:     my $seqstem = '';
  952:     if ($context eq 'CSTR') {
  953:         $seqstem = "/res/$udom/$uname/$newdir";
  954:     } elsif ($context eq 'DOCS') {
  955:         $seqstem = '/uploaded/'.$cdom.'/'.$crs.'/'.$timenow;
  956:     }
  957:     my %seqnames = (
  958:                   boards => 'bulletinboards',
  959:                   quizzes => 'quizzes',
  960:                   surveys => 'surveys',
  961:                   announcements => 'announcements',
  962:                   pools => 'pools'
  963:                   );
  964:     my %seqtitles = (
  965:                   boards => 'Course Bulletin Boards',
  966:                   quizzes => 'Course Quizzes',
  967:                   surveys => 'Course Surveys',
  968:                   announcements => 'Course Announcements',
  969:                   pools => 'Course Question Pools'
  970:                    );
  971:     $$topnum ++;
  972: 
  973:     if ($type eq 'announcements') {
  974:         $src = "$seqstem/pages/$seqnames{$type}.page";
  975:     } else {
  976:         $src = "$seqstem/sequences/$seqnames{$type}.sequence";
  977:     }
  978: 
  979:     push @{$topurls}, $src;
  980:     push @{$topnames}, $seqtitles{$type};
  981: 
  982:     $$seqtext .= qq|<resource id="$$topnum" src="$src" title="$seqtitles{$type}"|;
  983:     $nextnum = $$topnum +1;
  984:     if ($$topnum == 1) {
  985:         $$seqtext .= qq| type="start"></resource>
  986: <link from="$$topnum" to="$nextnum" index="$$topnum"></link>\n|;
  987:         if ($$topnum == $contentscount) {
  988:             $$seqtext .= qq|<resource id="$nextnum" src="" type="finish"></resource>\n|;
  989:         }
  990:     } else {
  991:         if ($$topnum == $contentscount) {
  992:             $$seqtext .= qq| type="finish"></resource>\n|;
  993:         } else {
  994:             $$seqtext .= qq|></resource>
  995: <link from="$$topnum" to="$nextnum" index="$$topnum"></link>\n|;
  996:         }
  997:     }
  998: 
  999:     if ($type eq "announcements") {
 1000:         push @{$pagesfiles}, "$seqnames{$type}.page";
 1001:         open(ITEM,">$destdir/pages/$seqnames{$type}.page");
 1002:     } else {
 1003:         push @{$seqfiles}, "$seqnames{$type}.sequence";
 1004:         open(ITEM,">$destdir/sequences/$seqnames{$type}.sequence");
 1005:     }
 1006: 
 1007:     if ($type eq 'boards') {
 1008:         $specialsrc = "/adm/$udom/$uname/$$timestamp[0]/bulletinboard";
 1009:     } elsif ($type eq 'announcements') {
 1010:         $specialsrc = "$seqstem/resfiles/$$specials[0].html";
 1011:     } elsif ($type eq 'pools') {
 1012:         $specialsrc = "$seqstem/sequences/$$specials[0].sequence";
 1013:     } else {
 1014:         $specialsrc = "$seqstem/pages/$$specials[0].page";
 1015:     }
 1016:     print ITEM qq|<map>
 1017: <resource id="1" src="$specialsrc" title="$$resinfo{$$specials[0]}{title}" type="start"></resource>
 1018: <link from="1" to="2" index="1"></link>|;
 1019:     if (@{$specials} == 1) {
 1020:         print ITEM qq|
 1021: <resource id="2" src="" type="finish"></resource>\n|;
 1022:     } else {
 1023:         for (my $i=1; $i<@{$specials}; $i++) {
 1024:             my $curr = $i+1;
 1025:             my $next = $i+2;
 1026:             if ($type eq 'boards') {
 1027:                 $specialsrc = "/adm/$udom/$uname/$$timestamp[$i]/bulletinboard";
 1028:             } elsif ($type eq 'announcements') {
 1029:                 $specialsrc = "$seqstem/resfiles/$$specials[$i].html";
 1030:             } else {
 1031:                 $specialsrc = "$seqstem/pages/$$specials[$i].page";
 1032:             }
 1033:             print ITEM qq|<resource id="$curr" src="$specialsrc" title="$$resinfo{$$specials[$i]}{title}"|;
 1034:             if (@{$specials} == $i+1) {
 1035:                 print ITEM qq| type="finish"></resource>\n|;
 1036:             } else {
 1037:                 print ITEM qq|></resource>
 1038: <link from="$curr" to="$next" index="$next">\n|;
 1039:             }
 1040:         }
 1041:     }
 1042:     print ITEM qq|</map>|;
 1043:     close(ITEM);
 1044: }
 1045: 
 1046: # ---------------------------------------------------------------- Process Blackboard users
 1047: sub process_user {
 1048:   my ($res,$docroot,$destdir,$settings,$user_crs,$user_cdom,$user_handling) = @_;
 1049:   my $xmlfile = $docroot.'/'.$res.".dat";
 1050:   my $filecount = 0;
 1051:   my @state;
 1052:   my $userid = '';
 1053:   my $linknum = 0;
 1054: 
 1055:   my $p = HTML::Parser->new
 1056:     (
 1057:      xml_mode => 1,
 1058:      start_h =>
 1059:      [sub {
 1060:         my ($tagname, $attr) = @_;
 1061:         push @state, $tagname;
 1062:         if ("@state" eq "USERS USER") {
 1063:             $userid = $attr->{value};
 1064:             %{$$settings{$userid}} = ();
 1065:             @{$$settings{$userid}{links}} = ();
 1066:         } elsif ("@state" eq "USERS USER LOGINID") {  
 1067:             $$settings{$userid}{loginid} = $attr->{value};
 1068:         } elsif ("@state" eq "USERS USER PASSPHRASE") {  
 1069:             $$settings{$userid}{passphrase} = $attr->{value};
 1070:         } elsif ("@state" eq "USERS USER STUDENTID" ) {
 1071:             $$settings{$userid}{studentid} = $attr->{value};
 1072:         } elsif ("@state" eq "USERS USER NAMES FAMILY" ) {
 1073:             $$settings{$userid}{family} = $attr->{value};
 1074:         } elsif ("@state" eq "USERS USER NAMES GIVEN" ) {
 1075:             $$settings{$userid}{given} = $attr->{value};
 1076:         } elsif ("@state" eq "USERS USER ADDRESSES BUSINESS DATA EMAIL") {
 1077:             $$settings{$userid}{email} = $attr->{value};
 1078:         } elsif ("@state" eq "USERS USER USER_ROLE") {
 1079:             $$settings{$userid}{user_role} = $attr->{value};
 1080:         } elsif ("@state" eq "USERS USER FLAGS ISAVAILABLE") {
 1081:             $$settings{$userid}{isavailable} = $attr->{value};
 1082:         } elsif ("@state" eq "USERS USER PERSONALPAGE FILELIST IMAGE") {
 1083:             $$settings{$userid}{image} = $attr->{value};
 1084:         } elsif ( ($state[-2] eq "LINKLIST") && ($state[-1] eq "LINK") ) {
 1085:             %{$$settings{$userid}{links}[$linknum]} = ();
 1086:             $$settings{$userid}{links}[$linknum]{url} = $attr->{value};
 1087:             $linknum ++;
 1088:         }
 1089:      }, "tagname, attr"],
 1090:      text_h =>
 1091:      [sub {
 1092:         my ($text) = @_;
 1093:         if ("@state" eq "USERS USER PERSONALPAGE TITLE") {
 1094:             $$settings{$userid}{title} = $text;
 1095:         } elsif ("@state" eq "USERS USER PERSONALPAGE DESCRIPTION") {
 1096:             $$settings{$userid}{description} = $text;
 1097:         } elsif (($state[-2] eq "LINK") && ($state[-1] eq "TITLE")) {
 1098:             $$settings{$userid}{links}[$linknum]{title} = $text;
 1099:         } elsif (($state[-3] eq "LINK") && ($state[-2] eq  "DESCRIPTION") && ($state[-1] eq "TEXT")) {
 1100:             $$settings{$userid}{links}[$linknum]{text} = $text;
 1101:         }
 1102:       }, "dtext"],
 1103:      end_h =>
 1104:      [sub {
 1105:         my ($tagname) = @_;
 1106:         if ("@state" eq "USERS USER") {
 1107:             $linknum = 0;
 1108:         }
 1109:         pop @state;
 1110:      }, "tagname"],
 1111:     );
 1112:   $p->unbroken_text(1);
 1113:   $p->parse_file($xmlfile);
 1114:   $p->eof;
 1115:   
 1116:   my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
 1117:   my $xmlstem =  $$configvars{'lonDaemons'}."/tmp/".$user_cdom."_".$user_crs."_";
 1118: 
 1119:   foreach my $user_id (keys %{$settings}) {
 1120:       if ($$settings{$user_id}{user_role} eq "s") {
 1121:            
 1122:       } elsif ($user_handling eq 'enrollall') {
 1123: 
 1124:       }
 1125:   }
 1126: }
 1127: 
 1128: # ---------------------------------------------------------------- Process Blackboard groups
 1129: sub process_group {  
 1130:   my ($res,$docroot,$destdir,$settings) = @_;
 1131:   my $xmlfile = $docroot.'/'.$res.".dat";
 1132:   my $filecount = 0;
 1133:   my @state;
 1134:   my $grp;
 1135: 
 1136:   my $p = HTML::Parser->new
 1137:     (
 1138:      xml_mode => 1,
 1139:      start_h =>
 1140:      [sub {
 1141:         my ($tagname, $attr) = @_;
 1142:         push @state, $tagname;
 1143:         if ("@state" eq "GROUPS GROUP") {
 1144:             $grp = $attr->{id};
 1145:         }        
 1146:         if ("@state" eq "GROUPS GROUP TITLE") {
 1147:             $$settings{$grp}{title} = $attr->{value};
 1148:         } elsif ("@state" eq "GROUPS GROUP FLAGS ISAVAILABLE") {  
 1149:             $$settings{$grp}{isavailable} = $attr->{value};
 1150:         } elsif ("@state" eq "GROUPS GROUP FLAGS HASCHATROOM") {  
 1151:             $$settings{$grp}{chat} = $attr->{value};
 1152:         } elsif ("@state" eq "GROUPS GROUP FLAGS HASDISCUSSIONBOARD") {
 1153:             $$settings{$grp}{discussion} = $attr->{value};
 1154:         } elsif ("@state" eq "GROUPS GROUP FLAGS HASTRANSFERAREA") {
 1155:             $$settings{$grp}{transfer} = $attr->{value};
 1156:         } elsif ("@state" eq "GROUPS GROUP FLAGS ISPUBLIC") {
 1157:             $$settings{$grp}{public} = $attr->{value};
 1158:         }
 1159:      }, "tagname, attr"],
 1160:      text_h =>
 1161:      [sub {
 1162:         my ($text) = @_;
 1163:         if ("@state" eq "GROUPS DESCRIPTION") {
 1164:           $$settings{$grp}{description} = $text;
 1165: #          print "Staff text is $text\n";
 1166:         }
 1167:       }, "dtext"],
 1168:      end_h =>
 1169:      [sub {
 1170:         my ($tagname) = @_;
 1171:         pop @state;
 1172:      }, "tagname"],
 1173:     );
 1174:   $p->unbroken_text(1);
 1175:   $p->parse_file($xmlfile);
 1176:   $p->eof;
 1177: }
 1178: 
 1179: # ---------------------------------------------------------------- Process Blackboard Staff
 1180: sub process_staff {
 1181:   my ($res,$docroot,$dirname,$destdir,$settings,$resrcfiles) = @_;
 1182:   my $xmlfile = $docroot.'/'.$res.".dat";
 1183:   my $filecount = 0;
 1184:   my @state;
 1185:   %{$$settings{name}} = ();
 1186:   %{$$settings{office}} = ();  
 1187: 
 1188:   my $p = HTML::Parser->new
 1189:     (
 1190:      xml_mode => 1,
 1191:      start_h =>
 1192:      [sub {
 1193:         my ($tagname, $attr) = @_;
 1194:         push @state, $tagname;
 1195:         if ("@state" eq "STAFFINFO TITLE") {
 1196:             $$settings{title} = $attr->{value};
 1197:         } elsif ("@state" eq "STAFFINFO BIOGRAPHY TEXTCOLOR") {
 1198:             $$settings{textcolor} = $attr->{value};
 1199:         } elsif ("@state" eq "STAFFINFO BIOGRAPHY FLAGS ISHTML") {
 1200:             $$settings{ishtml} = $attr->{value};
 1201:         } elsif ("@state" eq "STAFFINFO FLAGS ISAVAILABLE" ) {
 1202:             $$settings{isavailable} = $attr->{value};
 1203:         } elsif ("@state" eq "STAFFINFO FLAGS ISFOLDER" ) {
 1204:             $$settings{isfolder} = $attr->{value};
 1205:         } elsif ("@state" eq "STAFFINFO POSITION" ) {
 1206:             $$settings{position} = $attr->{value};
 1207:         } elsif ("@state" eq "STAFFINFO HOMEPAGE" ) {
 1208:             $$settings{homepage} = $attr->{value};
 1209:         } elsif ("@state" eq "STAFFINFO IMAGE") {
 1210:             $$settings{image} = $attr->{value};
 1211:         }
 1212:      }, "tagname, attr"],
 1213:      text_h =>
 1214:      [sub {
 1215:         my ($text) = @_;
 1216:         if ("@state" eq "STAFFINFO BIOGRAPHY TEXT") {
 1217:           $$settings{text} = $text;
 1218: #          print "Staff text is $text\n";
 1219:         } elsif ("@state" eq "STAFFINFO CONTACT PHONE") {
 1220:           $$settings{phone} = $text;
 1221:         } elsif ("@state" eq "STAFFINFO CONTACT EMAIL") {
 1222:           $$settings{email} = $text;
 1223:         } elsif ("@state" eq "STAFFINFO CONTACT NAME FORMALTITLE") {
 1224:           $$settings{name}{formaltitle} = $text;
 1225:         } elsif ("@state" eq "STAFFINFO CONTACT NAME FAMILY") {
 1226:           $$settings{name}{family} = $text;
 1227:         } elsif ("@state" eq "STAFFINFO CONTACT NAME GIVEN") {
 1228:           $$settings{name}{given} = $text;
 1229:         } elsif ("@state" eq "STAFFINFO CONTACT OFFICE HOURS") {
 1230:           $$settings{office}{hours} = $text;
 1231:         }  elsif ("@state" eq "STAFFINFO CONTACT OFFICE ADDRESS") {
 1232:           $$settings{office}{address} = $text;
 1233:         }        
 1234:       }, "dtext"],
 1235:      end_h =>
 1236:      [sub {
 1237:         my ($tagname) = @_;
 1238:         pop @state;
 1239:      }, "tagname"],
 1240:     );
 1241:   $p->unbroken_text(1);
 1242:   $p->parse_file($xmlfile);
 1243:   $p->eof;
 1244: 
 1245:     my $fontcol = '';
 1246:     if (defined($$settings{textcolor})) {
 1247:         $fontcol =  qq|color="$$settings{textcolor}"|;
 1248:     }
 1249:     if (defined($$settings{text})) {
 1250:         if ($$settings{ishtml} eq "true") {
 1251:             $$settings{text} = &HTML::Entities::decode($$settings{text});
 1252:         }
 1253:     }
 1254:     my $staffentry = qq|
 1255: <table border="0" cellpadding="0" cellspacing="0" width="100%">
 1256:   <tr>
 1257:     <td colspan="2"><hr /><font face="arial,helv" size="3"><b>$$settings{name}{formaltitle} $$settings{name}{given} $$settings{name}{family}</b></font>
 1258:     </td>
 1259:   </tr>
 1260:   <tr>
 1261:     <td valign="top">
 1262:       <table width="100% border="0" cols="2" cellpadding="0" cellspacing="0">|;
 1263:     if ( defined($$settings{email}) && $$settings{email} ne '') {
 1264:         $staffentry .= qq|
 1265:         <tr>
 1266:           <td width="100" valign="top">
 1267:            <font face="arial" size="2"><b>Email:</b></font>
 1268:           </td>
 1269:           <td>
 1270:            <font face="arial" size="2"><a href="mailto:$$settings{email}">$$settings{email}</a></font>
 1271:           </td>
 1272:         </tr>
 1273:         |;
 1274:     }
 1275:     if (defined($$settings{phone}) && $$settings{phone} ne '') {
 1276:         $staffentry .= qq|
 1277:         <tr>
 1278:           <td width="100" valign="top">
 1279:             <font face="arial" size="2"><b>Phone:</b></font>
 1280:           </td>
 1281:           <td>
 1282:             <font face="arial" size="2">$$settings{phone}</font>
 1283:           </td>
 1284:         </tr>
 1285:         |;
 1286:     }
 1287:     if (defined($$settings{office}{address}) && $$settings{office}{address} ne '') {
 1288:         $staffentry .= qq|
 1289:         <tr>
 1290:          <td width="100" valign="top">
 1291:            <font face="arial" size="2"><b>Address:</b></font>
 1292:          </td>
 1293:          <td>
 1294:            <font face="arial" size="2">$$settings{office}{address}</font>
 1295:          </td>
 1296:         </tr>
 1297:         |;
 1298:     }
 1299:     if (defined($$settings{office}{hours}) && $$settings{office}{hours} ne '') {
 1300:         $staffentry .= qq|
 1301:         <tr>
 1302:           <td width="100" valign="top">
 1303:             <font face="arial" size="2"><b>Office Hours:</b></font>
 1304:           </td>
 1305:           <td>
 1306:             <font face=arial size=2>$$settings{office}{hours}</font>
 1307:           </td>
 1308:         </tr>
 1309:         |;
 1310:     }
 1311:     if ( defined($$settings{homepage}) && $$settings{homepage} ne '') {
 1312:         $staffentry .= qq|
 1313:         <tr>
 1314:           <td width="100" valign="top">
 1315:             <font face="arial" size="2"><b>Personal Link:</b></font>
 1316:           </td>
 1317:           <td>
 1318:             <font face="arial" size="2"><a href="$$settings{homepage}">$$settings{homepage}</a></font>
 1319:           </td>
 1320:         </tr>
 1321:         |;
 1322:     }
 1323:     if (defined($$settings{text}) && $$settings{text} ne '') {
 1324:         $staffentry .= qq|
 1325:         <tr>
 1326:           <td colspan="2">
 1327:             <font face="arial" size="2" $fontcol><b>Other Information:</b><br/>$$settings{text}</font>
 1328:           </td>
 1329:         </tr>
 1330:         |;
 1331:      }
 1332:      $staffentry .= qq|
 1333:       </table>
 1334:     </td>
 1335:     <td align="right" valign="top">
 1336:      |;
 1337:      if ( defined($$settings{image}) ) {
 1338:          $staffentry .= qq|
 1339:       <img src="$dirname/resfiles/$res/$$settings{image}">
 1340:          |;
 1341:      }
 1342:      $staffentry .= qq|
 1343:     </td>
 1344:   </tr>
 1345: </table>
 1346:     |;
 1347:     open(FILE,">$destdir/resfiles/$res.html");
 1348:     push @{$resrcfiles}, "$res.html";
 1349:     print FILE qq|<html>
 1350: <head>
 1351: <title>$$settings{title}</title>
 1352: </head>
 1353: <body bgcolor='#ffffff'>
 1354: $staffentry
 1355: </body>
 1356: </html>|;
 1357:     close(FILE);
 1358: }
 1359: 
 1360: # ---------------------------------------------------------------- Process Blackboard Links
 1361: sub process_link {
 1362:     my ($res,$docroot,$dirname,$destdir,$settings,$resrcfiles) = @_;
 1363:     my $xmlfile = $docroot.'/'.$res.".dat";
 1364:     my @state = ();
 1365:     my $p = HTML::Parser->new
 1366:     (
 1367:         xml_mode => 1,
 1368:         start_h =>
 1369:         [sub {
 1370:             my ($tagname, $attr) = @_;
 1371:             push @state, $tagname;
 1372:             if ("@state" eq "EXTERNALLINK TITLE") {
 1373:                 $$settings{title} = $attr->{value};
 1374:             } elsif ("@state" eq "EXTERNALLINK TEXTCOLOR") {  
 1375:                 $$settings{textcolor} = $attr->{value};
 1376:             } elsif ("@state" eq "EXTERNALLINK DESCRIPTION FLAGS ISHTML") {  
 1377:                 $$settings{ishtml} = $attr->{value};
 1378:             } elsif ("@state" eq "EXTERNALLINK FLAGS ISAVAILABLE" ) {
 1379:                 $$settings{isavailable} = $attr->{value};
 1380:             } elsif ("@state" eq "EXTERNALLINK FLAGS LAUNCHINNEWWINDOW" ) {
 1381:                 $$settings{newwindow} = $attr->{value};
 1382:             } elsif ("@state" eq "EXTERNALLINK FLAGS ISFOLDER" ) {
 1383:                 $$settings{isfolder} = $attr->{value};
 1384:             } elsif ("@state" eq "EXTERNALLINK POSITION" ) {
 1385:                 $$settings{position} = $attr->{value};
 1386:             } elsif ("@state" eq "EXTERNALLINK URL" ) {
 1387:                 $$settings{url} = $attr->{value};
 1388:             }
 1389:         }, "tagname, attr"],
 1390:         text_h =>
 1391:         [sub {
 1392:             my ($text) = @_;
 1393:             if ("@state" eq "EXTERNALLINK DESCRIPTION TEXT") {
 1394:                $$settings{text} = $text;
 1395:             }
 1396:         }, "dtext"],
 1397:         end_h =>
 1398:         [sub {
 1399:             my ($tagname) = @_;
 1400:             pop @state;
 1401:         }, "tagname"],
 1402:     );
 1403:     $p->unbroken_text(1);
 1404:     $p->parse_file($xmlfile);
 1405:     $p->eof;
 1406: 
 1407:     my $linktag = '';
 1408:     my $fontcol = '';
 1409:     if (defined($$settings{textcolor})) {
 1410:         $fontcol =  qq|<font color="$$settings{textcolor}">|;
 1411:     }
 1412:     if (defined($$settings{text})) {
 1413:         if ($$settings{ishtml} eq "true") {
 1414:             $$settings{text} = &HTML::Entities::decode($$settings{text});
 1415:         }
 1416:     }
 1417: 
 1418:     if (defined($$settings{url}) ) {
 1419:         $linktag = qq|<a href="$$settings{url}"|;
 1420:         if ($$settings{newwindow} eq "true") {
 1421:             $linktag .= qq| target="launch"|;
 1422:         }
 1423:         $linktag .= qq|>$$settings{title}</a>|;
 1424:     }
 1425: 
 1426:     open(FILE,">$destdir/resfiles/$res.html");
 1427:     push @{$resrcfiles}, "$res.html";
 1428:     print FILE qq|<html>
 1429: <head>
 1430: <title>$$settings{title}</title>
 1431: </head>
 1432: <body bgcolor='#ffffff'>
 1433: $fontcol
 1434: $linktag
 1435: $$settings{text}
 1436: |;
 1437:     if (defined($$settings{textcolor})) {
 1438:         print FILE qq|</font>|;
 1439:     }
 1440:     print FILE qq|
 1441:   </body>
 1442:  </html>|;
 1443:     close(FILE);
 1444: }
 1445: 
 1446: # ---------------------------------------------------------------- Process Blackboard Discussion Boards
 1447: sub process_db {
 1448:     my ($res,$docroot,$destdir,$timestamp,$crs,$cdom,$handling,$uname,$settings,$longcrs) = @_;
 1449:     my $xmlfile = $docroot.'/'.$res.".dat";
 1450:     my @state = ();
 1451:     my @allmsgs = ();
 1452:     my %msgidx = ();
 1453:     my %threads; # all threads, keyed by message ID
 1454:     my $msg_id; # the current message ID
 1455:     my %message; # the current message being accumulated for $msg_id
 1456: 
 1457:     my $p = HTML::Parser->new
 1458:     (
 1459:        xml_mode => 1,
 1460:        start_h =>
 1461:        [sub {
 1462:            my ($tagname, $attr) = @_;
 1463:            push @state, $tagname;
 1464:            my $depth = 0;
 1465:            my @seq = ();
 1466:            if ("@state" eq "FORUM TITLE") {
 1467:                $$settings{title} = $attr->{value};
 1468:            } elsif ("@state" eq "FORUM DESCRIPTION TEXTCOLOR") {  
 1469:                $$settings{textcolor} = $attr->{value};
 1470:            } elsif ("@state" eq "FORUM DESCRIPTION FLAGS ISHTML") {  
 1471:                $$settings{ishtml} = $attr->{value};
 1472:            } elsif ("@state" eq "FORUM DESCRIPTION FLAGS ISNEWLINELITERAL") {  
 1473:                $$settings{newline} = $attr->{value};
 1474:            } elsif ("@state" eq "FORUM POSITION" ) {
 1475:                $$settings{position} = $attr->{value};
 1476:            } elsif ("@state" eq "FORUM FLAGS ISREADONLY") {
 1477:                $$settings{isreadonly} = $attr->{value};
 1478:            } elsif ("@state" eq "FORUM FLAGS ISAVAILABLE" ) {
 1479:                $$settings{isavailable} = $attr->{value};
 1480:            } elsif ("@state" eq "FORUM FLAGS ALLOWANONYMOUSPOSTINGS" ) {
 1481:                $$settings{allowanon} = $attr->{value};
 1482:            } elsif ( ($state[0] eq "FORUM") && ($state[1] eq "MESSAGETHREADS") && ($state[2] eq "MSG") ) {
 1483:                if ($state[-1] eq "MSG") {
 1484:                    unless ($msg_id eq '') {
 1485:                        push @{$threads{$msg_id}}, { %message };
 1486:                        $depth = @state - 3;
 1487:                        if ($depth > @seq) {
 1488:                            push @seq, $msg_id; 
 1489:                        }
 1490:                    }
 1491:                    if ($depth < @seq) {
 1492:                        pop @seq;
 1493:                    }                
 1494:                    $msg_id = $attr->{id};
 1495:                    push @allmsgs, $msg_id;
 1496:                    $msgidx{$msg_id} = @allmsgs;
 1497:                    %message = ();
 1498:                    $message{depth} = $depth;
 1499:                    if ($depth > 0) {
 1500:                        $message{parent} = $seq[-1];
 1501:                    } else {
 1502:                        $message{parent} = "None";
 1503:                    }
 1504:                } elsif ($state[-1] eq "TITLE") {
 1505:                    $message{title} = $attr->{value};
 1506:                } elsif ( ( $state[-3] eq "MESSAGETEXT" ) && ( $state[-2] eq "FLAGS" ) && ( $state[-1] eq "ISHTML" ) ) {
 1507:                    $message{ishtml} = $attr->{value};
 1508:                } elsif ( ( $state[-3] eq "MESSAGETEXT" ) && ( $state[-2] eq "FLAGS" ) && ( $state[-1] eq "ISNEWLINELITERAL" ) ) {
 1509:                    $message{newline} = $attr->{value};
 1510:                } elsif ( ( $state[-2] eq "DATES" ) && ( $state[-1] eq "CREATED" ) ) {
 1511:                    $message{created} = $attr->{value};
 1512:                } elsif ( $state[@state-2] eq "FLAGS") {
 1513:                    if ($state[@state-1] eq "ISANONYMOUS") {
 1514:                        $message{isanonymous} =  $attr->{value};
 1515:                    }
 1516:                } elsif ( $state[-2] eq "USER" ) {
 1517:                    if ($state[-1] eq "USERID") {
 1518:                        $message{userid} =  $attr->{value};
 1519:                    } elsif ($state[@state-1] eq "USERNAME") {
 1520:                        $message{username} =  $attr->{value};
 1521:                    } elsif ($state[@state-1] eq "EMAIL") {
 1522:                        $message{email} =  $attr->{value};
 1523:                    }          
 1524:                } elsif ( ($state[-2] eq "FILELIST") && ($state[-1] eq "IMAGE") ) {
 1525:                    $message{attachment} = $attr->{value};
 1526:                }
 1527:            }
 1528:        }, "tagname, attr"],
 1529:        text_h =>
 1530:        [sub {
 1531:            my ($text) = @_;
 1532:            if ("@state" eq "FORUM DESCRIPTION TEXT") {
 1533:                $$settings{text} = $text;
 1534:            } elsif ( ($state[0] eq "FORUM") && ($state[1] eq "MESSAGETHREADS") && ($state[2] eq "MSG") ) {
 1535:                if ( ($state[-2] eq "MESSAGETEXT") && ($state[-1] eq "TEXT") ){
 1536:                    $message{text} = $text;
 1537:                }
 1538:            }
 1539:        }, "dtext"],
 1540:        end_h =>
 1541:        [sub {
 1542:            my ($tagname) = @_;
 1543:            if ( $state[-1] eq "MESSAGETHREADS" ) {
 1544:                push @{$threads{$msg_id}}, { %message };
 1545:            }
 1546:            pop @state;
 1547:        }, "tagname"],
 1548:     );
 1549:     $p->unbroken_text(1);
 1550:     $p->parse_file($xmlfile);
 1551:     $p->eof;
 1552: 
 1553:     if (defined($$settings{text})) {
 1554:         if ($$settings{ishtml} eq "false") {
 1555:             if ($$settings{isnewline} eq "true") {
 1556:                 $$settings{text} =~ s#\n#<br/>#g;
 1557:             }
 1558:         } else {
 1559:             $$settings{text} = &HTML::Entities::decode($$settings{text});
 1560:         }
 1561:         if (defined($$settings{fontcolor}) ) {
 1562:             $$settings{text} = "<font color=\"".$$settings{textcolor}."\">".$$settings{text}."</font>";
 1563:         }
 1564:     }
 1565:     my $boardname = 'bulletinpage_'.$timestamp;
 1566:     my %boardinfo = (
 1567:                   'aaa_title' => $$settings{title},
 1568:                   'bbb_content' => $$settings{text},
 1569:                   'ccc_webreferences' => '',
 1570:                   'uploaded.lastmodified' => time,
 1571:                   );
 1572:   
 1573:     my $putresult = &Apache::lonnet::put($boardname,\%boardinfo,$cdom,$crs);
 1574:     if ($handling eq 'importall') {
 1575:         foreach my $msg_id (@allmsgs) {
 1576:             foreach my $message ( @{$threads{$msg_id}} ) {
 1577:                 my %contrib = (
 1578:                             'sendername' => $$message{userid},
 1579:                             'senderdomain' => $cdom,
 1580:                             'screenname' => '',
 1581:                             'plainname' => $$message{username},
 1582:                             );
 1583:                 unless ($$message{parent} eq 'None') {
 1584:                     $contrib{replyto} = $msgidx{$$message{parent}};
 1585:                 }
 1586:                 if (defined($$message{isanonymous}) ) {
 1587:                     if ($$message{isanonymous} eq 'true') {
 1588:                         $contrib{'anonymous'} = 'true';
 1589:                     }
 1590:                 }
 1591:                 if ( defined($$message{attachment}) )  {
 1592:                     my $url = $$message{attachment};
 1593:                     my $oldurl = $url;
 1594:                     my $newurl = $url;
 1595:                     unless ($url eq '') {
 1596:                         $newurl =~ s/\//_/g;
 1597:                         unless ($longcrs eq '') {
 1598:                             if (!-e "/home/httpd/lonUsers/$cdom/$longcrs/userfiles") {
 1599:                                 mkdir("/home/httpd/lonUsers/$cdom/$longcrs/userfiles",0755);
 1600:                             }
 1601:                             if (!-e "/home/httpd/lonUsers/$cdom/$longcrs/userfiles/$newurl") {
 1602:                                 system("cp $destdir/resfiles/$res/$$message{attachment} /home/httpd/lonUsers/$cdom/$longcrs/userfiles/$newurl");
 1603:                             }
 1604:                             $contrib{attachmenturl} = '/uploaded/'.$cdom.'/'.$crs.'/'.$newurl;
 1605:                         }
 1606:                     }
 1607:                 }
 1608:                 if (defined($$message{title}) ) {
 1609:                     $contrib{'message'} = $$message{title};
 1610:                 }
 1611:                 if (defined($$message{text})) {
 1612:                     if ($$message{ishtml} eq "false") {
 1613:                         if ($$message{isnewline} eq "true") {
 1614:                             $$message{text} =~ s#\n#<br/>#g;
 1615:                         }
 1616:                     } else {
 1617:                         $$message{text} = &HTML::Entities::decode($$message{text});
 1618:                     }
 1619:                     $contrib{'message'} .= '<br /><br />'.$$message{text};
 1620:                     my $symb = 'bulletin___'.$timestamp.'___adm/wrapper/adm/'.$cdom.'/'.$uname.'/'.$timestamp.'/bulletinboard';
 1621:                     my $postresult = &addposting($symb,\%contrib,$cdom,$crs);
 1622:                 }
 1623:             }
 1624:         }
 1625:     }
 1626: }
 1627: 
 1628: # ---------------------------------------------------------------- Add Posting to Bulletin Board
 1629: sub addposting {
 1630:     my ($symb,$contrib,$cdom,$crs)=@_;
 1631:     my $status='';
 1632:     if (($symb) && ($$contrib{message})) {
 1633:          my $crsdom = $cdom.'_'.$crs;
 1634:          &Apache::lonnet::store($contrib,$symb,$crsdom,$cdom,$crs);
 1635:          my %storenewentry=($symb => time);
 1636:          &Apache::lonnet::put('discussiontimes',\%storenewentry,$cdom,$crs);
 1637:     }
 1638:     my %record=&Apache::lonnet::restore('_discussion');
 1639:     my ($temp)=keys %record;
 1640:     unless ($temp=~/^error\:/) {
 1641:         my %newrecord=();
 1642:         $newrecord{'resource'}=$symb;
 1643:         $newrecord{'subnumber'}=$record{'subnumber'}+1;
 1644:         &Apache::lonnet::cstore(\%newrecord,'_discussion');
 1645:         $status = 'ok';
 1646:     } else {
 1647:         $status.='Failed.';
 1648:     }
 1649:     return $status;
 1650: }
 1651: 
 1652: sub parse_bb5_assessment {
 1653:     my ($res,$docroot,$container,$settings,$allanswers,$allchoices,$allids) = @_;
 1654:     my $xmlfile = $docroot.'/'.$res.".dat";
 1655:     my @state = ();
 1656:     my $id; # the current question ID
 1657:     my $answer_id; # the current answer ID
 1658:     my %toptag = ( pool => 'POOL',
 1659:                  quiz => 'ASSESSMENT',
 1660:                  survey => 'ASSESSMENT'
 1661:                );
 1662: 
 1663:     my $p = HTML::Parser->new
 1664:     (
 1665:      xml_mode => 1,
 1666:      start_h =>
 1667:      [sub {
 1668:         my ($tagname, $attr) = @_;
 1669:         push @state, $tagname;
 1670:         my $depth = 0;
 1671:         my @seq = ();
 1672:         my $class;
 1673:         my $state_str = join(" ",@state);
 1674:         if ($container eq "pool") {
 1675:             if ("@state" eq "POOL TITLE") {
 1676:                 $$settings{title} = $attr->{value};
 1677:             }
 1678:         } else {
 1679:             if ("@state" eq "ASSESSMENT TITLE") {  
 1680:                 $$settings{title} = $attr->{value};          
 1681:             } elsif ("@state" eq "ASSESSMENT FLAG" ) {
 1682:                 $$settings{isnewline} = $attr->{value};
 1683:             } elsif ("@state" eq "ASSESSMENT FLAGS ISAVAILABLE") {
 1684:                 $$settings{isavailable} = $attr->{value};
 1685:             } elsif ("@state" eq "ASSESSMENT FLAGS ISANONYMOUS" ) {
 1686:                 $$settings{isanonymous} = $attr->{id};
 1687:             } elsif ("@state" eq "ASSESSMENT FLAGS GIVE FEEDBACK" ) {
 1688:                 $$settings{feedback} = $attr->{id};        
 1689:             } elsif ("@state" eq "ASSESSMENT FLAGS SHOWCORRECT" ) {
 1690:                 $$settings{showcorrect} = $attr->{id};        
 1691:             } elsif ("@state" eq "ASSESSMENT FLAGS SHOWRESULTS" ) {
 1692:                 $$settings{showresults} = $attr->{id};        
 1693:             } elsif ("@state" eq "ASSESSMENT FLAGS ALLOWMULTIPLE" ) {
 1694:                 $$settings{allowmultiple} = $attr->{id};        
 1695:             } elsif ("@state" eq "ASSESSMENT ASSESSMENTTYPE" ) {
 1696:                 $$settings{type} = $attr->{id};        
 1697:             }
 1698:         }    
 1699:         if ("@state" eq "$toptag{$container} QUESTIONLIST QUESTION") {  
 1700:             $id = $attr->{id};
 1701:             push @{$allids}, $id;
 1702:             %{$$settings{$id}} = ();
 1703:             @{$$allanswers{$id}} = ();
 1704:             $$settings{$id}{class} = $attr->{class};
 1705:             unless ($container eq "pool") {
 1706:                 $$settings{$id}{points} = $attr->{points};
 1707:             }
 1708:             @{$$settings{$id}{correctanswer}} = ();                              
 1709:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[-1] =~ m/^QUESTION_(\w+)$/) ) {
 1710:             $id = $attr->{id};
 1711:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "BODY") && ($state[3] eq "FLAGS") ) {
 1712:             if ($state[4] eq "ISHTML") {
 1713:                 $$settings{$id}{html} = $attr->{value};
 1714:             } elsif ($state[4] eq "ISNEWLINELITERAL") {
 1715:                 $$settings{$id}{newline} = $attr->{value};
 1716:             }
 1717:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "IMAGE") ) {
 1718:             $$settings{$id}{image} = $attr->{value};
 1719:             $$settings{$id}{style} = $attr->{style};
 1720:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "URL") ) {
 1721:             $$settings{$id}{url} = $attr->{value};
 1722:             $$settings{$id}{name} = $attr->{name};
 1723:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[-1] eq "ANSWER") ) {
 1724:             $answer_id = $attr->{id};
 1725:             push @{$$allanswers{$id}},$answer_id;
 1726:             %{$$settings{$id}{$answer_id}} = ();
 1727:             $$settings{$id}{$answer_id}{position} = $attr->{position};
 1728:             if ($$settings{$id}{class} eq 'QUESTION_MATCH') {
 1729:                 $$settings{$id}{$answer_id}{placement} = $attr->{placement};
 1730:                 $$settings{$id}{$answer_id}{type} = 'answer';
 1731:             }
 1732:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[-1] eq "CHOICE") ) {
 1733:             $answer_id = $attr->{id};
 1734:             push @{$$allchoices{$id}},$answer_id; 
 1735:             %{$$settings{$id}{$answer_id}} = ();
 1736:             $$settings{$id}{$answer_id}{position} = $attr->{position};
 1737:             $$settings{$id}{$answer_id}{placement} = $attr->{placement};
 1738:             $$settings{$id}{$answer_id}{type} = 'choice';
 1739:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "ANSWER") ) {
 1740:             if ($state[3] eq "IMAGE") {
 1741:                 $$settings{$id}{$answer_id}{image} = $attr->{value};
 1742:                 $$settings{$id}{$answer_id}{style} = $attr->{style};
 1743:             } elsif ($state[3] eq "URL") {
 1744:                 $$settings{$id}{$answer_id}{url} = $attr->{value};
 1745:             }
 1746:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "CHOICE") ) {
 1747:             if ($state[3] eq "IMAGE") {
 1748:                 $$settings{$id}{$answer_id}{image} = $attr->{value};
 1749:                 $$settings{$id}{$answer_id}{style} = $attr->{style};
 1750:             } elsif ($state[3] eq "URL") {
 1751:                 $$settings{$id}{$answer_id}{url} = $attr->{value};
 1752:             }
 1753:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "GRADABLE") && ($state[3] eq "CORRECTANSWER") ) {
 1754:             my $corr_answer = $attr->{answer_id};
 1755:             push @{$$settings{$id}{correctanswer}}, $corr_answer;
 1756:             my $type = $1;
 1757:             if ($type eq 'TRUEFALSE') {
 1758:                 $$settings{$id}{$corr_answer}{answer_position} = $attr->{position};
 1759:             } elsif ($type eq 'ORDER') {
 1760:                 $$settings{$id}{$corr_answer}{order} = $attr->{order};
 1761:             } elsif ($type eq 'MATCH') {
 1762:                 $$settings{$id}{$corr_answer}{choice_id} = $attr->{choice_id};
 1763:             }
 1764:         }
 1765:      }, "tagname, attr"],
 1766:      text_h =>
 1767:      [sub {
 1768:         my ($text) = @_;
 1769:         $text =~ s/^\s+//g;
 1770:         $text =~ s/\s+$//g;
 1771:         unless ($container eq "pool") {        
 1772:             if ("@state" eq "ASSESSMENT DESCRIPTION TEXT") {
 1773:                 $$settings{description} = $text;
 1774:             } elsif ("@state" eq "ASSESSMENT INSTRUCTIONS ") {
 1775:                 $$settings{instructions}{text} = $text;
 1776:             }
 1777:         }
 1778:         if ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "BODY") && ($state[-1] eq "TEXT") ) {
 1779:             unless ($text eq '') { 
 1780:                 $$settings{$id}{text} = $text;
 1781:             }
 1782:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "ANSWER") && ($state[-1] eq "TEXT") ) {
 1783:             unless ($text eq '') {
 1784:                 $$settings{$id}{$answer_id}{text} = $text;
 1785:             }
 1786:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "CHOICE") && ($state[-1] eq "TEXT") ) {
 1787:             unless ($text eq '') {
 1788:                 $$settings{$id}{$answer_id}{text} = $text;
 1789:             }
 1790:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "GRADABLE") && ($state[-1] eq "FEEDBACK_WHEN_CORRECT") ) {
 1791:             unless ($text eq '') {
 1792:                 $$settings{$id}{feedback_corr} = $text;
 1793:             }
 1794:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "GRADABLE") && ($state[-1] eq "FEEDBACK_WHEN_INCORRECT") ) {
 1795:             unless ($text eq '') {
 1796:                 $$settings{$id}{feedback_incorr} = $text;
 1797:             }
 1798:         }
 1799:       }, "dtext"],
 1800:      end_h =>
 1801:      [sub {
 1802:         my ($tagname) = @_;
 1803:         pop @state;
 1804:      }, "tagname"],
 1805:     );
 1806:     $p->unbroken_text(1);
 1807:     $p->marked_sections(1);
 1808:     $p->parse_file($xmlfile);
 1809:     $p->eof;
 1810: }
 1811: 
 1812: sub parse_bb6_assessment {
 1813:      my ($res,$docroot,$container,$settings,$allanswers,$allchoices,$allids) = @_;
 1814:      return;
 1815: }
 1816: 
 1817: sub parse_webct4_assessment {
 1818:     my ($res,$docroot,$href,$container,$allids) = @_;
 1819:     my $xmlfile = $docroot.'/'.$href; #quiz file
 1820:     my @state = ();
 1821:     my $id; # the current question ID
 1822:     my $p = HTML::Parser->new
 1823:     (
 1824:      xml_mode => 1,
 1825:      start_h =>
 1826:      [sub {
 1827:         my ($tagname, $attr) = @_;
 1828:         push @state, $tagname;
 1829:         my $depth = 0;
 1830:         my @seq = ();
 1831:         if ("@state" eq "questestinterop assessment section itemref") {
 1832:             $id = $attr->{linkrefid}; 
 1833:             push(@{$allids},$id);
 1834:         }
 1835:      }, "tagname, attr"],
 1836:      text_h =>
 1837:      [sub {
 1838:         my ($text) = @_;
 1839:       }, "dtext"],
 1840:      end_h =>
 1841:      [sub {
 1842:         my ($tagname) = @_;
 1843:         pop @state;
 1844:      }, "tagname"],
 1845:     );
 1846:     $p->unbroken_text(1);
 1847:     $p->parse_file($xmlfile);
 1848:     $p->eof;
 1849: }
 1850: 
 1851: sub parse_webct4_quizprops {
 1852:     my ($res,$docroot,$href,$container,$qzparams) = @_;
 1853:     my $xmlfile = $docroot.'/'.$href; #properties file
 1854:     my @state = ();
 1855:     %{$$qzparams{$res}} = ();
 1856:     my $p = HTML::Parser->new
 1857:     (
 1858:      xml_mode => 1,
 1859:      start_h =>
 1860:      [sub {
 1861:         my ($tagname, $attr) = @_;
 1862:         push @state, $tagname;
 1863:         my $depth = 0;
 1864:         my @seq = ();
 1865:         if ($state[0] eq 'properties' && $state[1] eq 'processing')  {
 1866:             if ($state[2] eq 'scores' && $state[3] eq 'score') {
 1867:                 $$qzparams{$res}{weight} = $attr->{linkrefid};
 1868:             } elsif ($state[2] eq 'selection' && $state[3] eq 'select') {
 1869:                 $$qzparams{$res}{numpick} = $attr->{linkrefid};      
 1870:             }
 1871:         }
 1872:      }, "tagname, attr"],
 1873:      text_h =>
 1874:      [sub {
 1875:         my ($text) = @_;
 1876:         if ($state[0] eq 'properties' && $state[1] eq 'delivery')  {
 1877:             if ($state[2] eq 'time_available') {
 1878:                 $$qzparams{$res}{opendate} = $text;
 1879:             } elsif ($state[2] eq 'time_due') {
 1880:                 $$qzparams{$res}{opendate} = $text;
 1881:             } elsif ($state[3] eq 'max_attempt') {
 1882:                 $$qzparams{$res}{tries} = $text;
 1883:             } elsif ($state[3] eq 'post_submission') {
 1884:                 $$qzparams{$res}{posts} = $text;
 1885:             }
 1886:         } elsif ($state[0] eq 'properties' && $state[1] eq 'result') {
 1887:             if ($state[2] eq 'display_answer') {
 1888:                 $$qzparams{$res}{answerdate} = $text;
 1889:             }
 1890:         } 
 1891:       }, "dtext"],
 1892:      end_h =>
 1893:      [sub {
 1894:         my ($tagname) = @_;
 1895:         pop @state;
 1896:      }, "tagname"],
 1897:     );
 1898:     $p->unbroken_text(1);
 1899:     $p->parse_file($xmlfile);
 1900:     $p->eof;
 1901: }
 1902: 
 1903: sub parse_webct4_questionDB {
 1904:     my ($docroot,$href,$catinfo,$settings,$allanswers,$allchoices,$allids) = @_;
 1905:     $href =~ s#[^/]+$##;
 1906:     my $xmlfile = $docroot.'/'.$href.'questionDB.xml'; #quizDB file
 1907:     my @state = ();
 1908:     my $category; # the current category ID
 1909:     my $id; # the current question ID
 1910:     my $list; # the current list ID for multiple choice questions
 1911:     my $numid; # the current answer ID for numerical questions
 1912:     my $grp; # the current group ID for matching questions
 1913:     my $label; # the current reponse label for string questions 
 1914:     my $str_id; # the current string ID for string questions
 1915:     my $unitid; # the current unit ID for numerical questions
 1916:     my $answer_id; # the current answer ID
 1917:     my $fdbk; # the current feedback ID
 1918:     my $currvar; # the current variable for numerical problems
 1919:     my $fibtype; # the current fill-in-blank type for numerical or string
 1920:     my $prompt;
 1921:     my $boxnum; 
 1922:     my %setvar = (
 1923:                    varname => '',
 1924:                    action => '',
 1925:                  );
 1926:     my $currtexttype;
 1927:     my $currimagtype;  
 1928:     my $p = HTML::Parser->new
 1929:     (
 1930:      xml_mode => 1,
 1931:      start_h =>
 1932:      [sub {
 1933:         my ($tagname, $attr) = @_;
 1934:         push @state, $tagname;
 1935:         if ("@state" eq "questestinterop section") {
 1936:             $category = $attr->{ident};
 1937:             %{$$catinfo{$category}} = ();
 1938:             $$catinfo{$category}{title} = $attr->{title};   
 1939:         }
 1940:         if ("@state" eq "questestinterop section item") {
 1941:             $id = $attr->{ident};
 1942:             push @{$allids}, $id;
 1943:             push(@{$$catinfo{$category}{contents}},$id);
 1944:             %{$$settings{$id}} = ();
 1945:             @{$$allchoices{$id}} = ();
 1946:             @{$$settings{$id}{grps}} = ();
 1947:             @{$$settings{$id}{lists}} = ();
 1948:             @{$$settings{$id}{feedback}} = ();
 1949:             @{$$settings{$id}{str}} = ();
 1950:             %{$$settings{$id}{strings}} = ();
 1951:             @{$$settings{$id}{numids}} = ();
 1952:             @{$$settings{$id}{boxes}} = ();
 1953:             %{$$allanswers{$id}} = ();
 1954:             $$settings{$id}{title} = $attr->{title};
 1955:             $$settings{$id}{category} = $category;
 1956:             $boxnum = 0;
 1957:         }
 1958: 
 1959:         if ("@state" eq "questestinterop section item presentation material mattext") {
 1960:             $$settings{$id}{texttype} = $attr->{texttype};
 1961:             $currtexttype = $attr->{texttype};
 1962:         }
 1963:         if ("@state" eq "questestinterop section item presentation material matimage") {
 1964:             $$settings{$id}{imagtype} = $attr->{imagtype};
 1965:             $currimagtype = $attr->{imagtype};
 1966:             $$settings{$id}{uri} = $attr->{uri};
 1967:         }
 1968: 
 1969: # Matching
 1970:         if ("@state" eq "questestinterop section item presentation response_grp") {
 1971:             $$settings{$id}{class} = 'match';
 1972:             $grp = $attr->{ident};
 1973:             push(@{$$settings{$id}{grps}},$grp);
 1974:             %{$$settings{$id}{$grp}} = ();
 1975:             @{$$settings{$id}{$grp}{correctanswer}} = ();
 1976:             $$settings{$id}{$grp}{rcardinality} = $attr->{rcardinality};
 1977:         }
 1978:         if ("@state" eq "questestinterop section item presentation response_grp material mattext") { 
 1979:             $$settings{$id}{$grp}{texttype} = $attr->{texttype};
 1980:             $currtexttype = $attr->{texttype};
 1981:         }
 1982:         if ("@state" eq "questestinterop section item presentation response_grp render_choice response_label") {
 1983:             $answer_id = $attr->{ident};
 1984:             push(@{$$allanswers{$id}{$grp}},$answer_id);
 1985:             %{$$settings{$id}{$grp}{$answer_id}} = ();
 1986:             $$settings{$id}{$grp}{$answer_id}{texttype} =  $attr->{texttype};
 1987:             $currtexttype = $attr->{texttype};
 1988:         }
 1989: 
 1990: # Multiple choice
 1991: 
 1992:         if ("@state" eq "questestinterop section item presentation flow material mattext") {
 1993:             $$settings{$id}{texttype} = $attr->{texttype};
 1994:             $currtexttype = $attr->{texttype};
 1995:         }
 1996:         if ("@state" eq "questestinterop section item presentation flow response_lid") {
 1997:             $$settings{$id}{class} = 'multiplechoice';
 1998:             $list = $attr->{ident};
 1999:             push(@{$$settings{$id}{lists}},$list);
 2000:             %{$$settings{$id}{$list}} = ();
 2001:             @{$$allanswers{$id}{$list}} = ();
 2002:             @{$$settings{$id}{$list}{correctanswer}} = ();
 2003:             $$settings{$id}{$list}{rcardinality} = $attr->{rcardinality};
 2004:         }
 2005:         if ("@state" eq "questestinterop section item presentation flow response_lid render_choice") {
 2006:             $$settings{$id}{$list}{randomize} = $attr->{shuffle};
 2007:         }
 2008:         if ("@state" eq "questestinterop section item presentation flow response_lid render_choice flow_label response_label") {
 2009:             $answer_id = $attr->{ident};
 2010:             push(@{$$allanswers{$id}{$list}},$answer_id);
 2011:             %{$$settings{$id}{$list}{$answer_id}} = ();
 2012:         }
 2013:         if ("@state" eq "questestinterop section item presentation flow response_lid render_choice flow_label response_label material mattext") {
 2014:             $$settings{$id}{$list}{$answer_id}{texttype} = $attr->{texttype};
 2015:             $currtexttype = $attr->{texttype};
 2016:         }
 2017: 
 2018: # Numerical
 2019:         if ("@state" eq "questestinterop section item presentation material mat_extension webct:x_webct_v01_dynamicmattext") {
 2020:             $$settings{$id}{texttype} = $attr->{texttype};
 2021:             $currtexttype = $attr->{texttype};
 2022:         }
 2023:         if ("@state" eq "questestinterop section item presentation response_num") {
 2024:             $$settings{$id}{class} = 'numerical';
 2025:             $numid = $attr->{ident};
 2026:             push(@{$$settings{$id}{numids}},$numid);
 2027:             %{$$settings{$id}{$numid}} = ();
 2028:             %{$$settings{$id}{$numid}{vars}} = ();
 2029:             @{$$settings{$id}{$numid}{units}} = ();
 2030:             $$settings{$id}{$numid}{rcardinality} = $attr->{rcardinality};
 2031:         }
 2032:         if ("@state" eq "questestinterop section item presentation response_num material mat_extension webct:x_webct_v01_dynamicdata webct:x_webct_v01_datarange webct:x_webct_v01_minvalue webct:x_webct_v01_variable") {            
 2033:             $currvar = $attr->{name};
 2034:             %{$$settings{$id}{$numid}{vars}{$currvar}} = ();
 2035:         }
 2036:         if ("@state" eq "questestinterop section item presentation response_num material mat_extension webct:x_webct_v01_dynamicdata webct:x_webct_v01_datarange webct:x_webct_v01_maxvalue webct:x_webct_v01_variable") {
 2037:             $currvar = $attr->{name};
 2038:         }
 2039:         if ("@state" eq "questestinterop section item presentation response_num material mat_extension webct:x_webct_v01_dynamicdata webct:x_webct_v01_datarange webct:x_webct_v01_decimalnum webct:x_webct_v01_variable") {
 2040:             $currvar = $attr->{name};
 2041:         }
 2042:         if ("@state" eq "questestinterop section item presentation response_num render_fib") {
 2043:             $fibtype = $attr->{fibtype};
 2044:             $prompt = $attr->{prompt};
 2045:         }
 2046:         if ("@state" eq "questestinterop section item presentation response_num render_fib response_label") {
 2047:             $$settings{$id}{$numid}{label} = $attr->{ident};
 2048:         }
 2049: 
 2050: # String or Numerical
 2051:         if ("@state" eq "questestinterop section item presentation response_str") {
 2052:             $str_id = $attr->{ident};
 2053:             push(@{$$settings{$id}{str}},$str_id);
 2054:             @{$$settings{$id}{boxes}[$boxnum]} = ();
 2055:             $boxnum ++;
 2056:             %{$$settings{$id}{$str_id}} = ();
 2057:             @{$$settings{$id}{$str_id}{labels}} = ();
 2058:             $$settings{$id}{$str_id}{rcardinality} = $attr->{rcardinality};
 2059:         }
 2060: 
 2061:         if ("@state" eq "questestinterop section item presentation response_str render_fib") {
 2062:             $fibtype = $attr->{fibtype};
 2063:             $prompt = $attr->{prompt};
 2064:         }    
 2065:         if ("@state" eq "questestinterop section item presentation response_str render_fib response_label") {
 2066:             $label = $attr->{ident};
 2067:             push(@{$$settings{$id}{$str_id}{labels}},$label);
 2068:             @{$$settings{$id}{strings}{$label}} = ();
 2069:             %{$$settings{$id}{$str_id}{$label}} = ();
 2070:             $$settings{$id}{$str_id}{$label}{fibtype} = $fibtype;
 2071:         }
 2072: 
 2073: # Numerical
 2074:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_anspresentation") {
 2075:             $$settings{$id}{$numid}{digits} = $attr->{digits};
 2076:             $$settings{$id}{$numid}{format} = $attr->{format};
 2077:         }
 2078:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_anstolerance") {
 2079:             $$settings{$id}{$numid}{toltype} = $attr->{type};
 2080:         }
 2081:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_unit") {
 2082:             my $unitid = $attr->{ident};
 2083:             %{$$settings{$id}{$numid}{$unitid}} = ();
 2084:             push(@{$$settings{$id}{$numid}{units}},$unitid);
 2085:             $$settings{$id}{$numid}{$unitid}{value} = $attr->{value}; 
 2086:             $$settings{$id}{$numid}{$unitid}{space} = $attr->{space};
 2087:             $$settings{$id}{$numid}{$unitid}{case} = $attr->{case};
 2088:         }
 2089: 
 2090: # Matching 
 2091:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar varequal") {
 2092:             if ($$settings{$id}{class} eq 'match') {
 2093:                 unless ($attr->{respident} eq 'WebCT_Incorrect') {
 2094:                     $grp = $attr->{respident};
 2095:                 }
 2096: # String
 2097:             } else {
 2098:                 $label = $attr->{respident};
 2099:                 $$settings{$id}{$label}{case} = $attr->{case};   
 2100:             } 
 2101:         }
 2102:         if ("@state" eq "questestinterop section item resprocessing respcondition setvar") {
 2103:             $setvar{varname} = $attr->{varname};
 2104:             if ($setvar{varname} eq 'WebCT_Correct') {
 2105:                 push(@{$$settings{$id}{$grp}{correctanswer}},$answer_id);
 2106:             }
 2107:         }
 2108: 
 2109: # String
 2110:         if ("@state" eq "questestinterop section item resprocessing") {
 2111:             $boxnum = -1;
 2112:         }
 2113:         if ("@state" eq "questestinterop section item resprocessing respcondition") {            $boxnum ++;
 2114:         }
 2115:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar varsubset") {
 2116:             $$settings{$id}{class} = 'string';
 2117:             $label = $attr->{respident};
 2118:         }
 2119:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar not") {
 2120:             $$settings{$id}{class} = 'paragraph';
 2121:         }
 2122:  
 2123: 
 2124: # Feedback
 2125:  
 2126:         if ("@state" eq "questestinterop section item respcondition displayfeedback") {
 2127:             $fdbk = $attr->{linkrefid};
 2128:             push(@{$$settings{$id}{feedback}},$fdbk);
 2129:             $$settings{$id}{$fdbk} = ();
 2130:             $$settings{$id}{$fdbk}{feedbacktype} = $attr->{feedbacktype};
 2131:         }
 2132:         if ("@state" eq "questestinterop section item itemfeedback") {
 2133:             $fdbk = $attr->{ident};
 2134:             $$settings{$id}{$fdbk}{view} = $attr->{view};
 2135:         }
 2136:         if ("@state" eq "questestinterop section item itemfeedback material mattext") {
 2137:             $$settings{$id}{$fdbk}{texttype} = $attr->{texttype};
 2138:             $currtexttype = $attr->{texttype};
 2139:         }
 2140:      }, "tagname, attr"],
 2141:      text_h =>
 2142:      [sub {
 2143:         my ($text) = @_;
 2144:         if ($currtexttype eq '/text/html') {
 2145:             $text =~ s#(&lt;img\ssrc=")([^"]+)"&gt;#$1../resfiles/$2#g;
 2146:         }
 2147:         if ("@state" eq "questestinterop section item itemmetadata qmd_itemtype") {
 2148:             $$settings{$id}{itemtype} = $text;
 2149:             if ($text eq 'String') {
 2150:                 $$settings{$id}{class} = 'string';
 2151:             }
 2152:         }
 2153: 
 2154:         if ("@state" eq "questestinterop section item presentation material mattext") {
 2155:             $$settings{$id}{text} = $text;
 2156:         }
 2157: # Matching
 2158:         if ("@state" eq "questestinterop section item presentation response_grp material mattext") {
 2159:             $$settings{$id}{$grp}{text} = $text;
 2160:             unless ($text eq '') {
 2161:                 push(@{$$allchoices{$id}},$grp);
 2162:             }
 2163:         }
 2164:         if ("@state" eq "questestinterop section item presentation response_grp render_choice response_label material mattext") {
 2165:             $$settings{$id}{$grp}{$answer_id}{text} = $text;
 2166:         }
 2167: 
 2168: # Multiple choice
 2169: 
 2170:         if ("@state" eq "questestinterop section item presentation flow material mattext") {
 2171:             $$settings{$id}{text} = $text;
 2172:         }
 2173: 
 2174:         if ("@state" eq "questestinterop section item presentation flow response_lid render_choice flow_label response_label material mattext") {
 2175:             $$settings{$id}{$list}{$answer_id}{text} = $text;
 2176:         }
 2177: 
 2178: # Numerical
 2179:         if ("@state" eq "questestinterop section item presentation material mat_extension webct:x_webct_v01_dynamicmattext") {
 2180:             $$settings{$id}{text} = $text;
 2181:         }
 2182:         if ("@state" eq "questestinterop section item presentation response_num material mat_extension webct:x_webct_v01_dynamicdata webct:x_webct_v01_datarange webct:x_webct_v01_minvalue webct:x_webct_v01_variable") {
 2183:              $$settings{$id}{$numid}{vars}{$currvar}{min} = $text;
 2184:         }
 2185:         if ("@state" eq "questestinterop section item presentation response_num material mat_extension webct:x_webct_v01_dynamicdata webct:x_webct_v01_datarange webct:x_webct_v01_maxvalue webct:x_webct_v01_variable") {
 2186:              $$settings{$id}{$numid}{vars}{$currvar}{max} = $text;
 2187:         }
 2188:         if ("@state" eq "questestinterop section item presentation response_num material mat_extension webct:x_webct_v01_dynamicdata webct:x_webct_v01_datarange webct:x_webct_v01_decimalnum webct:x_webct_v01_variable") {
 2189:              $$settings{$id}{$numid}{vars}{$currvar}{dec} = $text;
 2190:         }
 2191:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_formula") {
 2192:             $$settings{$id}{$numid}{formula} = $text;
 2193:         }
 2194:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar varequal") {
 2195:             if ($$settings{$id}{class} eq 'string') {
 2196:                 unless (grep/^$text$/,@{$$settings{$id}{strings}{$label}}) {
 2197:                     push(@{$$settings{$id}{strings}{$label}},$text);
 2198:                 }
 2199:                 unless (grep/^$text$/,@{$$settings{$id}{boxes}[$boxnum]}) {
 2200:                     push(@{$$settings{$id}{boxes}[$boxnum]},$text);
 2201:                 }
 2202:             } else {
 2203:                 $answer_id = $text;
 2204:             }
 2205:         }
 2206:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar varsubset") { # String
 2207:             unless (grep/^$text$/,@{$$settings{$id}{strings}{$label}}) {
 2208:                 push(@{$$settings{$id}{strings}{$label}},$text);
 2209:             }
 2210:             unless (grep/^$text$/,@{$$settings{$id}{boxes}[$boxnum]}) {
 2211:                 push(@{$$settings{$id}{boxes}[$boxnum]},$text);
 2212:             }
 2213:         }
 2214:         if ("@state" eq "questestinterop section item resprocessing respcondition setvar") {
 2215:             if ($setvar{varname} eq "answerValue") { # Multiple Choice
 2216:                 if ($text =~ m/^\d+$/) {
 2217:                     if ($text > 0) {
 2218:                         push(@{$$settings{$id}{$list}{correctanswer}},$answer_id);   
 2219:                     }
 2220:                 }
 2221:             }
 2222:         }
 2223:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_anstolerance") {
 2224:             $$settings{$id}{$numid}{tolerance} = $text;
 2225:         }
 2226:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_unit") {
 2227:             $$settings{$id}{$numid}{$unitid}{text} = $text;
 2228:         }
 2229: 
 2230:         if ("@state" eq "questestinterop section item itemfeedback material mattext") {
 2231:             $$settings{$id}{$fdbk}{text} = $text;
 2232:         }
 2233:       }, "dtext"],
 2234:      end_h =>
 2235:      [sub {
 2236:         my ($tagname) = @_;
 2237:         pop @state;
 2238:      }, "tagname"],
 2239:     );
 2240:     $p->unbroken_text(1);
 2241:     $p->parse_file($xmlfile);
 2242:     $p->eof;
 2243:     my $boxcount;
 2244:     foreach my $id (keys %{$settings}) {
 2245:         if ($$settings{$id}{class} eq 'string') {
 2246:             $boxcount = 0;
 2247:             if (@{$$settings{$id}{boxes}} > 1) {
 2248:                 foreach my $str_id (@{$$settings{$id}{str}}) {
 2249:                     foreach my $label (@{$$settings{$id}{$str_id}{labels}}) {
 2250:                         @{$$settings{$id}{strings}{$label}} = @{$$settings{$id}{boxes}[$boxcount]};
 2251:                         $boxcount ++;
 2252:                     }
 2253:                 }
 2254:             }
 2255:         }
 2256:     }
 2257: }
 2258: 
 2259: sub process_assessment {
 2260:     my ($cms,$context,$res,$docroot,$container,$dirname,$destdir,$settings,$total,$udom,$uname,$pagesfiles,$sequencesfiles,$randompicks,$dbparse,$resources,$items,$catinfo,$qzdbsettings) = @_;
 2261:     my @allids = ();
 2262:     my %allanswers = ();
 2263:     my %allchoices = ();
 2264:     my %qzparams = ();
 2265:     my @allquestids = ();
 2266:     my %alldbanswers = ();
 2267:     my %alldbchoices = ();
 2268:     my @alldbquestids = ();
 2269:     my $containerdir;
 2270:     my $newdir;
 2271:     my $randompickflag = 0;
 2272:     my ($cid,$cdom,$cnum);
 2273:     if ($context eq 'DOCS') {
 2274:         $cid = $ENV{'request.course.id'};
 2275:         ($cdom,$cnum) = split/_/,$cid;
 2276:     }
 2277:     my $destresdir = $destdir;
 2278:     if ($context eq 'CSTR') {
 2279:         $destresdir =~ s|/home/$uname/public_html/|/res/$udom/$uname/|;
 2280:     } elsif ($context eq 'DOCS') {
 2281:         $destresdir =~ s|^/home/httpd/html/userfiles|/uploaded|;
 2282:     }
 2283:     if ($cms eq 'bb5') {
 2284:         &parse_bb5_assessment($res,$docroot,$container,$settings,\%allanswers,\%allchoices,\@allids);
 2285:     } elsif ($cms eq 'bb6') {
 2286:         &parse_bb6_assessment($res,$docroot,$container,$settings,\%allanswers,\%allchoices,\@allids);
 2287:     } elsif ($cms eq 'webct4') {
 2288:         unless($$dbparse) {
 2289:             &parse_webct4_questionDB($docroot,$$resources{$res}{file},$catinfo,$qzdbsettings,\%alldbanswers,\%alldbchoices,\@alldbquestids);
 2290:             if (!-e "$destdir/sequences") {
 2291:                 mkdir("$destdir/sequences",0755);
 2292:             }
 2293:             my $numcats = scalar(keys %{$catinfo});
 2294:             my $curr_id = 0;
 2295:             my $next_id = 1;
 2296:             my $fh;
 2297:             open($fh,">$destdir/sequences/question_database.sequence");
 2298:             push @{$sequencesfiles},'question_database.sequence';
 2299:             foreach my $category (sort keys %{$catinfo}) {
 2300:                 my $seqname = $$catinfo{$category}{title}.'_'.$category;
 2301:                 $seqname =~ s/\s/_/g;
 2302:                 $seqname =~ s/\W//g;
 2303:                 push(@{$sequencesfiles},$seqname.'.sequence');
 2304:                 my $catsrc = "$destresdir/sequences/$seqname.sequence";
 2305:                 if ($curr_id == 0) {
 2306:                     print $fh qq|<resource id="1" src="$catsrc" type="start" title="$$catinfo{$category}{title}"></resource>|;
 2307:                 }
 2308:                 if ($numcats == 1) {
 2309:                     print $fh qq|
 2310: <link from="1" to="2" index="1"></link>
 2311: <resource id="2" src="" type="finish">\n|;
 2312:                 } else {
 2313:                     $curr_id = $next_id;
 2314:                     $next_id = $curr_id + 1;
 2315:                     $catsrc = "$destresdir/sequences/$seqname.sequence";
 2316:                     print $fh qq|
 2317: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
 2318: <resource id="$next_id" src="$catsrc" title="$$catinfo{$category}{title}"|;
 2319:                     if ($next_id == $numcats) {
 2320:                         print $fh qq| type="finish"></resource>\n|;
 2321:                     } else {
 2322:                         print $fh qq|></resource>\n|;
 2323:                     }
 2324:                 }
 2325:                 print $fh qq|</map>|;
 2326:                 if (!-e "$destdir/problems") {
 2327:                     mkdir("$destdir/problems",0755);
 2328:                 }
 2329:                 if (!-e "$destdir/problems/$seqname") {
 2330:                     mkdir("$destdir/problems/$seqname",0755);
 2331:                 }
 2332:                 my $newdir = "$destdir/problems/$seqname";
 2333:                 my $dbcontainerdir;
 2334:                 &build_problem_container($cms,$seqname,$destdir,'database',$seqname,$total,$sequencesfiles,$pagesfiles,$randompickflag,$context,\@{$$catinfo{$category}{contents}},$udom,$uname,$dirname,\$dbcontainerdir,$cid,$cdom,$cnum,$catinfo,$qzdbsettings);
 2335:             }
 2336:             close($fh);
 2337:             &write_webct4_questions(\@alldbquestids,$context,$qzdbsettings,$dirname,\%alldbanswers,\%alldbchoices,$total,$cid,$cdom,$cnum,$destdir,$catinfo);
 2338:             $$dbparse = 1;
 2339:         }
 2340:         &parse_webct4_assessment($res,$docroot,$$resources{$res}{file},$container,\@allids);
 2341:         &parse_webct4_quizprops($res,$docroot,$$resources{$$items{$$resources{$res}{revitm}}{properties}}{file},$container,\%qzparams);
 2342:         foreach (sort keys %qzparams) {
 2343:             if (exists($qzparams{$res}{numpick})) { 
 2344:                 if ($qzparams{$res}{numpick} < @allids) {
 2345:                     $$randompicks{$res} = $qzparams{$res}{numpick};
 2346:                     $randompickflag = 1;
 2347:                 }
 2348:             }
 2349:         }
 2350:     }
 2351:     my $dirtitle;
 2352:     unless ($cms eq 'webct4') {
 2353:         $dirtitle = $$settings{'title'};
 2354:         $dirtitle =~ s/\W//g;
 2355:         $dirtitle .= '_'.$res;
 2356:         if (!-e "$destdir/problems") {
 2357:             mkdir("$destdir/problems",0755);
 2358:         }
 2359:         if (!-e "$destdir/problems/$dirtitle") {
 2360:             mkdir("$destdir/problems/$dirtitle",0755);
 2361:         }
 2362:         my $newdir = "$destdir/problems/$dirtitle";
 2363:     }
 2364: 
 2365:     &build_problem_container($cms,$dirtitle,$destdir,$container,$res,$total,$sequencesfiles,$pagesfiles,$randompickflag,$context,\@allids,$udom,$uname,$dirname,\$containerdir,$cid,$cdom,$cnum,$catinfo,$qzdbsettings);
 2366:     if ($cms eq 'bb5') {
 2367:         &write_bb5_questions(\@allids,$containerdir,$context,$settings,$dirname,$res,\%allanswers,\%allchoices,$total,$newdir,$cid,$cdom,$cnum);
 2368:     } elsif ($cms eq 'bb6') {
 2369:         &write_bb6_questions(\@allids,$containerdir,$context,$settings,$dirname,$res,\%allanswers,\%allchoices,$total,$newdir,$cid,$cdom,$cnum);
 2370:     }
 2371: }
 2372: 
 2373: sub build_problem_container {
 2374:     my ($cms,$dirtitle,$destdir,$container,$res,$total,$sequencesfiles,$pagesfiles,$randompickflag,$context,$allids,$udom,$uname,$dirname,$containerdir,$cid,$cdom,$cnum,$catinfo,$settings) = @_;
 2375:     my $seqdir = "$destdir/sequences";
 2376:     my $pagedir = "$destdir/pages";
 2377:     my $curr_id = 0;
 2378:     my $next_id = 1;
 2379:     my $fh;
 2380:     if ($container eq 'pool' || $randompickflag || $container eq 'database') {
 2381:         $$containerdir = $seqdir.'/'.$res.'.sequence';
 2382:         if (!-e "$seqdir") {
 2383:             mkdir("$seqdir",0770);
 2384:         }
 2385:         open($fh,">$$containerdir");
 2386:         $$total{seq} ++;
 2387:         push @{$sequencesfiles},$res.'.sequence';
 2388:     } else {
 2389:         $$containerdir = $pagedir.'/'.$res.'.page';
 2390:         if (!-e "$destdir/pages") {
 2391:             mkdir("$destdir/pages",0770);
 2392:         }
 2393:         open($fh,">$$containerdir");
 2394:         $$total{page} ++;
 2395:         push @{$pagesfiles},$res.'.page';
 2396:     }
 2397:     print $fh qq|<map>
 2398: |;
 2399:     my %probtitle = ();
 2400:     my $probsrc = "/res/lib/templates/simpleproblem.problem";
 2401:     if ($context eq 'CSTR') {
 2402:         foreach my $id (@{$allids}) {
 2403:             $probtitle{$id} = $$settings{$id}{title};
 2404:             $probtitle{$id} =~ s/\s/_/g;
 2405:             $probtitle{$id} =~ s/\W//g;
 2406:             $probtitle{$id} .= '_'.$id;
 2407:         }
 2408:         if ($cms eq 'webct4' && $container ne 'database') {
 2409:             my $catid = $$settings{$$allids[0]}{category};
 2410:             my $probdir = $$catinfo{$catid}{title}.'_'.$catid;
 2411:             $probdir =~ s/\s/_/g;
 2412:             $probdir =~ s/\W//g;
 2413:             $probsrc = "$dirname/problems/$probdir/$probtitle{$$allids[0]}.problem";
 2414:         } else {
 2415:             $probsrc="$dirname/problems/$dirtitle/$$allids[0].problem";
 2416:         }
 2417:     }
 2418:     print $fh qq|<resource id="1" src="$probsrc" type="start" title="question_0001"></resource>|;
 2419:     if (@{$allids} == 1) {
 2420:         print $fh qq|
 2421: <link from="1" to="2" index="1"></link>
 2422: <resource id="2" src="" type="finish">\n|;
 2423:     } else {
 2424:         for (my $j=1; $j<@{$allids}; $j++) {
 2425:             my $qntitle = $j+1;
 2426:             while (length($qntitle) <4) {
 2427:                 $qntitle = '0'.$qntitle;
 2428:             }
 2429:             $curr_id = $j;
 2430:             $next_id = $curr_id + 1;
 2431:             if ($context eq 'CSTR') {
 2432:                 if ($cms eq 'webct4' && $container ne 'database') {
 2433:                     my $catid = $$settings{$$allids[$j]}{category};
 2434:                     my $probdir = $$catinfo{$catid}{title}.'_'.$catid;
 2435:                     $probdir =~ s/\s/_/g;
 2436:                     $probdir =~ s/\W//g;
 2437:                     $probsrc = "$dirname/problems/$probdir/$probtitle{$$allids[$j]}.problem";
 2438:                 } else {
 2439:                     $probsrc = "$dirname/problems/$dirtitle/$$allids[$j].problem";
 2440:                 }
 2441:             }
 2442:             print $fh qq|
 2443: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
 2444: <resource id="$next_id" src="$probsrc" title="question_$qntitle"|;
 2445:             if ($next_id == @{$allids}) {
 2446:                 print $fh qq| type="finish"></resource>\n|;
 2447:             } else {
 2448:                 print $fh qq|></resource>|;
 2449:             }
 2450:         }
 2451:     }
 2452:     print $fh qq|</map>|;
 2453:     close($fh);
 2454: }
 2455: 
 2456: sub write_bb5_questions {
 2457:     my ($allids,$containerdir,$context,$settings,$dirname,$res,$allanswers,$allchoices,$total,$newdir,$cid,$cdom,$cnum) = @_;
 2458:     my $qnum = 0;
 2459:     foreach my $id (@{$allids}) {
 2460:         $qnum ++;
 2461:         my $output;
 2462:         my $permcontainer = $containerdir;
 2463:         $permcontainer =~ s#/home/httpd/html/userfiles#uploaded#;
 2464:         my $symb = $cid.'.'.$permcontainer.'___'.$qnum.'___lib/templates/simpleproblem.problem.0.';
 2465:         my %resourcedata = ();
 2466:         for (my $i=0; $i<10; $i++) {
 2467:             my $iter = $i+1;
 2468:             $resourcedata{$symb.'text'.$iter} = "";
 2469:             $resourcedata{$symb.'value'.$iter} = "unused";
 2470:             $resourcedata{$symb.'position'.$iter} = "random";
 2471:         }
 2472:         $resourcedata{$symb.'randomize'} = 'yes';
 2473:         $resourcedata{$symb.'maxfoils'} = 10;
 2474:         if ($context eq 'CSTR') {
 2475:             $output = qq|<problem>
 2476: |;
 2477:         }
 2478:         $$total{prob} ++;
 2479:         if ($$settings{$id}{class} eq "QUESTION_ESSAY") {
 2480:             if ($context eq 'CSTR') {
 2481:                 $output .= qq|<startouttext />$$settings{$id}{text}<endouttext />
 2482:  <essayresponse>
 2483:  <textfield></textfield>
 2484:  </essayresponse>
 2485:  <postanswerdate>
 2486:   $$settings{$id}{feedbackcorr} 
 2487:  </postanswerdate>
 2488: |;
 2489:              } else {
 2490: 		 $resourcedata{$symb.'questiontext'} = $$settings{$id}{text};
 2491:                  $resourcedata{$symb.'hiddenparts'} = '!essay';
 2492:                  $resourcedata{$symb.'questiontype'} = 'essay';
 2493:              }
 2494:         } else {
 2495:             if ($context eq 'CSTR') {
 2496:                 $output .= qq|<startouttext />$$settings{$id}{text}\n|;
 2497:             } else {
 2498:                 $resourcedata{$symb.'questiontext'} = $$settings{$id}{text};
 2499:             }
 2500:             my ($image,$imglink,$url);
 2501:             if ( defined($$settings{$id}{image}) ) {
 2502:                 if ( $$settings{$id}{style} eq 'embed' ) {
 2503:                     $image = qq|<br /><img src="$dirname/resfiles/$res/$$settings{$id}{image}" /><br />|;
 2504:                 } else {
 2505:                     $imglink = qq|<br /><a href="$dirname/resfiles/$res/$$settings{$id}{image}">Link to file</a><br />|;
 2506:                 }
 2507:             }
 2508:             if ( defined($$settings{$id}{url}) ) {
 2509:                 $url = qq|<br /><a href="$$settings{$id}{url}">$$settings{$id}{name}</a><br />|;
 2510:             }
 2511:             if ($context eq 'CSTR') {
 2512:                 $output .= $image.$imglink.$url.'
 2513: <endouttext />';
 2514:             } else {
 2515:                 $resourcedata{$symb.'questiontext'} .= $image.$imglink.$url;
 2516:             }
 2517:             if ($$settings{$id}{class} eq 'QUESTION_MULTIPLECHOICE') {
 2518:                 my $numfoils = @{$$allanswers{$id}};
 2519:                 if ($context eq 'CSTR') {
 2520:                     $output .= qq|
 2521:  <radiobuttonresponse max="$numfoils" randomize="yes">
 2522:   <foilgroup>
 2523: |;
 2524:                 } else {
 2525:                     $resourcedata{$symb.'hiddenparts'} = '!radio';
 2526:                     $resourcedata{$symb.'questiontype'} = 'radio';
 2527:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 2528:                 }
 2529:                 for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 2530:                     my $iter = $k+1;
 2531:                     $output .= "   <foil name=\"foil".$k."\" value=\"";
 2532:                     if (grep/^$$allanswers{$id}[$k]$/,@{$$settings{$id}{correctanswer}}) {
 2533:                         $output .= "true\" location=\"";
 2534:                         $resourcedata{$symb.'value'.$iter} = "true";
 2535:                     } else {
 2536:                         $output .= "false\" location=\"";
 2537:                         $resourcedata{$symb.'value'.$iter} = "false";
 2538:                     }
 2539:                     if (lc ($$allanswers{$id}[$k]) =~ m/^\s?([Aa]ll)|([Nn]one)\s(of\s)?the\sabove\.?/) {
 2540:                         $output .= "bottom\"";
 2541:                         $resourcedata{$symb.'position'.$iter} = "bottom";
 2542:                     } else {
 2543:                         $output .= "random\"";
 2544:                     }
 2545:                     $output .= "\><startouttext />".$$settings{$id}{$$allanswers{$id}[$k]}{text};
 2546:                     $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 2547:                     my ($ans_image,$ans_link);
 2548:                     if ( defined($$settings{$id}{$$allanswers{$id}[$k]}{image}) ) {
 2549:                         if ( $$settings{$id}{$$allanswers{$id}[$k]}{style} eq 'embed' ) {
 2550:                             $ans_image .= qq|<br /><img src="$dirname/resfiles/$res/$$settings{$id}{$$allanswers{$id}[$k]}{image}" /><br />|;
 2551:                         } else {
 2552:                             $ans_link .= qq|<br /><a href="$dirname/resfiles/$res/$$settings{$id}{$$allanswers{$id}[$k]}{image}" />Link to file</a><br/>|;
 2553:                         }
 2554:                     }
 2555:                     $output .= $ans_image.$ans_link.'<endouttext /></foil>'."\n";
 2556:                     $resourcedata{$symb.'text'.$iter} .= $ans_image.$ans_link;
 2557:                 }
 2558:                 if ($context eq 'CSTR') {
 2559:                     chomp($output);
 2560:                     $output .= qq|
 2561:   </foilgroup>
 2562:  </radiobuttonresponse>
 2563: |;
 2564:                 }
 2565:             } elsif ($$settings{$id}{class} eq 'QUESTION_TRUEFALSE') {
 2566:                 my $numfoils = @{$$allanswers{$id}};
 2567:                 if ($context eq 'CSTR') {
 2568:                     $output .= qq|
 2569:    <radiobuttonresponse max="$numfoils" randomize="yes">
 2570:     <foilgroup>
 2571: |;
 2572:                 } else {
 2573:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 2574:                     $resourcedata{$symb.'hiddenparts'} = '!radio';
 2575:                     $resourcedata{$symb.'questiontype'} = 'radio';
 2576:                 }
 2577:                 for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 2578:                     my $iter = $k+1;
 2579:                     $output .= "   <foil name=\"foil".$k."\" value=\"";
 2580:                     if (grep/^$$allanswers{$id}[$k]$/,@{$$settings{$id}{correctanswer}}) {
 2581:                         $output .= "true\" location=\"random\"";
 2582:                         $resourcedata{$symb.'value'.$iter} = "true";
 2583:                     } else {
 2584:                         $output .= "false\" location=\"random\"";
 2585:                         $resourcedata{$symb.'value'.$iter} = "false";
 2586:                     }
 2587:                     $output .= "\><startouttext />".$$settings{$id}{$$allanswers{$id}[$k]}{text}."<endouttext /></foil>\n";
 2588:                     $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 2589:                 }
 2590:                 if ($context eq 'CSTR') {
 2591:                     chomp($output);
 2592:                     $output .= qq|
 2593:     </foilgroup>
 2594:    </radiobuttonresponse>
 2595: |;
 2596:                 }
 2597:             } elsif ($$settings{$id}{class} eq 'QUESTION_MULTIPLEANSWER') {
 2598:                 my $numfoils = @{$$allanswers{$id}};
 2599:                 if ($context eq 'CSTR') {
 2600:                     $output .= qq|
 2601:    <optionresponse max="$numfoils" randomize="yes">
 2602:     <foilgroup options="('True','False')">
 2603: |;
 2604:                 } else {
 2605:                     $resourcedata{$symb.'newopt'} = '';
 2606:                     $resourcedata{$symb.'delopt'} = '';
 2607:                     $resourcedata{$symb.'options'} = "('True','False')";
 2608:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 2609:                     $resourcedata{$symb.'questiontype'} = 'option';
 2610:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 2611:                 }
 2612:                 for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 2613:                     my $iter = $k+1;
 2614:                     $output .= "   <foil name=\"foil".$k."\" value=\"";
 2615:                     if (grep/^$$allanswers{$id}[$k]$/,@{$$settings{$id}{correctanswer}}) {
 2616:                         $output .= "True\"";
 2617:                         $resourcedata{$symb.'value'.$iter} = "True";
 2618:                     } else {
 2619:                         $output .= "False\"";
 2620:                         $resourcedata{$symb.'value'.$iter} = "False";
 2621:                     }
 2622:                     $output .= "\><startouttext />".$$settings{$id}{$$allanswers{$id}[$k]}{text}."<endouttext /></foil>\n";
 2623:                     $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 2624:                 }
 2625:                 if ($context eq 'CSTR') {  
 2626:                     chomp($output);
 2627:                     $output .= qq|
 2628:     </foilgroup>
 2629:    </optionresponse>
 2630: |;
 2631:                 }
 2632:             } elsif ($$settings{$id}{class} eq 'QUESTION_ORDER') {
 2633:                 my $numfoils = @{$$allanswers{$id}};
 2634:                 my @allorder = ();
 2635:                 if ($context eq 'CSTR') {
 2636:                     $output .= qq|
 2637:    <rankresponse max="$numfoils" randomize="yes">
 2638:     <foilgroup>
 2639: |;
 2640:                 } else {
 2641:                     $resourcedata{$symb.'newopt'} = '';
 2642:                     $resourcedata{$symb.'delopt'} = '';
 2643:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 2644:                     $resourcedata{$symb.'questiontype'} = 'option';
 2645:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 2646:                 }
 2647:                 for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 2648:                     if ($context eq 'CSTR') {
 2649:                         $output .= "   <foil location=\"random\" name=\"foil".$k."\" value=\"".$$settings{$id}{$$allanswers{$id}[$k]}{order}."\"><startouttext />".$$settings{$id}{$$allanswers{$id}[$k]}{text}."<endouttext /></foil>\n";
 2650:                     } else {
 2651:                         my $iter = $k+1;
 2652:                         $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 2653:                         if (!grep/^$$settings{$id}{$$allanswers{$id}[$k]}{order}$/,@allorder) {
 2654:                             push @allorder, $$settings{$id}{$$allanswers{$id}[$k]}{order};
 2655:                         }
 2656:                     }
 2657:                 }
 2658:                 if ($context eq 'CSTR') {
 2659:                     chomp($output);
 2660:                     $output .= qq|
 2661:     </foilgroup>
 2662:    </rankresponse>
 2663: |;
 2664:                 } else {
 2665:                     @allorder = sort {$a <=> $b} @allorder;
 2666:                     $resourcedata{$symb.'options'} = "('".join("','",@allorder)."')";
 2667:                 }
 2668:             } elsif ($$settings{$id}{class} eq 'QUESTION_FILLINBLANK') {
 2669:                 my $numerical = 1;
 2670:                 if ($context eq 'DOCS') {
 2671:                     $numerical = 0;
 2672:                 } else {
 2673:                     for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 2674:                         if ($$settings{$id}{$$allanswers{$id}[$k]}{text} =~ m/([^\d\.]|\.\.)/) {
 2675:                             $numerical = 0;
 2676:                         }
 2677:                     }
 2678:                 }
 2679:                 if ($numerical) {
 2680:                     my $numans;
 2681:                     my $tol;
 2682:                     if (@{$$allanswers{$id}} == 1) {
 2683:                         $tol = 5;
 2684:                         $numans = $$settings{$id}{$$allanswers{$id}[0]}{text};
 2685:                     } else {
 2686:                         my $min = $$settings{$id}{$$allanswers{$id}[0]}{text};
 2687:                         my $max = $$settings{$id}{$$allanswers{$id}[0]}{text};
 2688:                         for (my $k=1; $k<@{$$allanswers{$id}}; $k++) {
 2689:                             if ($$settings{$id}{$$allanswers{$id}[$k]}{text} <= $min) {
 2690:                                 $min = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 2691:                             }
 2692:                             if ($$settings{$id}{$$allanswers{$id}[$k]}{text} >= $max) {
 2693:                                 $max = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 2694:                             }
 2695:                         }
 2696:                         $numans = ($max + $min)/2;
 2697:                         $tol = 100*($max - $min)/($numans*2);
 2698:                     }
 2699:                     if ($context eq 'CSTR') {
 2700:                         $output .= qq|
 2701: <numericalresponse answer="$numans">
 2702:         <responseparam type="tolerance" default="$tol%" name="tol" description="Numerical Tolerance" />
 2703:         <responseparam name="sig" type="int_range,0-16" default="0,15" description="Significant Figures"
 2704: />
 2705:         <textline />
 2706: </numericalresponse>
 2707: |;
 2708:                     }
 2709:                 } else {
 2710:                     if ($context eq 'DOCS') {
 2711:                         $resourcedata{$symb.'hiddenparts'} = '!string';
 2712:                         $resourcedata{$symb.'questiontype'} = 'string';
 2713:                         $resourcedata{$symb.'maxfoils'} = @{$$allanswers{$id}};
 2714:                         $resourcedata{$symb.'hiddenparts'} = '!string';
 2715:                         $resourcedata{$symb.'stringtype'} = 'ci';
 2716:                         $resourcedata{$symb.'stringanswer'} = $$settings{$id}{$$allanswers{$id}[0]}{text};
 2717:                     } else {
 2718:                         if (@{$$allanswers{$id}} == 1) {
 2719:                             $output .= qq|
 2720: <stringresponse answer="$$settings{$id}{$$allanswers{$id}[0]}{text}" type="ci">
 2721: <textline>
 2722: </textline>
 2723: </stringresponse>
 2724: |;
 2725:                         } else {
 2726:                             my @answertext = ();
 2727:                             for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 2728:                                 $$settings{$id}{$$allanswers{$id}[$k]}{text} =~ s/\|/\|/g;
 2729:                                 push @answertext, $$settings{$id}{$$allanswers{$id}[$k]}{text};
 2730:                             }
 2731:                             my $regexpans = join('|',@answertext);
 2732:                             $regexpans = '/^('.$regexpans.')\b/';
 2733:                             $output .= qq|
 2734: <stringresponse answer="$regexpans" type="re">
 2735: <textline>
 2736: </textline>
 2737: </stringresponse>
 2738: |;
 2739:                         }
 2740:                     } 
 2741:                 }
 2742:             } elsif ($$settings{$id}{class} eq "QUESTION_MATCH") {
 2743:                 my @allmatchers = ();
 2744:                 my %matchtext = ();
 2745:                 if ($context eq 'CSTR') {
 2746:                     $output .= qq|
 2747: <matchresponse max="10" randomize="yes">
 2748:     <foilgroup>
 2749:         <itemgroup>
 2750: |;
 2751:                 } else {
 2752:                     $resourcedata{$symb.'newopt'} = '';
 2753:                     $resourcedata{$symb.'delopt'} = '';
 2754:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 2755:                     $resourcedata{$symb.'questiontype'} = 'option';
 2756:                     $resourcedata{$symb.'maxfoils'} =  @{$$allanswers{$id}};
 2757:                 }
 2758:                 for (my $k=0; $k<@{$$allchoices{$id}}; $k++) {
 2759:                     if ($context eq 'CSTR') {
 2760:                         $output .= qq|
 2761: <item name="$$allchoices{$id}[$k]">
 2762: <startouttext />$$settings{$id}{$$allchoices{$id}[$k]}{text}<endouttext />
 2763: </item>
 2764:                     |;
 2765:                     } else {
 2766:                         if (!grep/^$$settings{$id}{$$allchoices{$id}[$k]}{text}$/,@allmatchers) {
 2767:                             push @allmatchers, $$settings{$id}{$$allchoices{$id}[$k]}{text};
 2768:                             $matchtext{$$allchoices{$id}[$k]} = $$settings{$id}{$$allchoices{$id}[$k]}{text};
 2769:                         }
 2770:                     }
 2771:                 }
 2772:                 if ($context eq 'CSTR') {
 2773:                     $output .= qq|
 2774:         </itemgroup>
 2775: |;
 2776:                 }
 2777:                 for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 2778:                     if ($context eq 'CSTR') {
 2779:                         $output .= qq|
 2780:         <foil location="random" value="$$settings{$id}{$$allanswers{$id}[$k]}{choice_id}" name="$$allanswers{$id}[$k]">
 2781:          <startouttext />$$settings{$id}{$$allanswers{$id}[$k]}{text}<endouttext />
 2782:         </foil>
 2783: |;
 2784:                     } else {
 2785:                         my $iter = $k+1;
 2786:                         $resourcedata{$symb.'value'.$iter} = $matchtext{$$settings{$id}{$$allanswers{$id}[$k]}{choice_id}};
 2787:                         $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 2788:                     }
 2789:                 }
 2790:                 if ($context eq 'CSTR') {
 2791:                     $output .= qq|
 2792:     </foilgroup>
 2793: </matchresponse>
 2794: |;
 2795:                 } else {
 2796:                     $resourcedata{$symb.'options'} = "('".join("','",@allmatchers)."')";
 2797:                 }
 2798:             }
 2799:         }
 2800:         if ($context eq 'CSTR') {
 2801:             $output .= qq|</problem>
 2802: |;
 2803:             open(PROB,">$newdir/problems/$id.problem");
 2804:             print PROB $output;
 2805:             close PROB;
 2806:         } else {
 2807: # put %resourcedata;
 2808:             my $reply=&Apache::lonnet::cput
 2809:                 ('resourcedata',\%resourcedata,$cdom,$cnum);
 2810:         }
 2811:     }
 2812: }
 2813: 
 2814: sub write_webct4_questions {
 2815:     my ($alldbquestids,$context,$settings,$dirname,$allanswers,$allchoices,$total,$cid,$cdom,$cnum,$destdir,$catinfo) = @_;
 2816:     my $qnum = 0;
 2817:     foreach my $id (@{$alldbquestids}) {
 2818:         $qnum ++;
 2819:         my $output;
 2820:         my $permcontainer = $destdir.'/sequences/'.$id.'.sequence';
 2821:         my $allfeedback;
 2822:         my $questionimage;
 2823:         foreach my $fdbk (@{$$settings{$id}{feedback}}) {
 2824:             my $feedback =  $$settings{$id}{$fdbk}{text};
 2825:             if ($$settings{$id}{$fdbk}{texttype} eq 'text/html') {
 2826:                 $feedback = &HTML::Entities::decode($feedback);
 2827:             }
 2828:             $allfeedback .= $feedback;
 2829:         }
 2830:         if ($$settings{$id}{texttype} eq 'text/html') {
 2831:             $$settings{$id}{text} = &HTML::Entities::decode($$settings{$id}{text});
 2832:             $$settings{$id}{text} = &Apache::lonxml::htmlclean($$settings{$id}{text});
 2833:             $$settings{$id}{text} =~ s#(<img src=")([^>]+)>#$1../../resfiles/$2 />#gi;
 2834:             $$settings{$id}{text} =~ s#<hr>#<hr />#g;
 2835: #            $$settings{$id}{text} =~ s#<p>#</p><p>#g;
 2836: #            $$settings{$id}{text} =~ s#</p></p>#</p>#;
 2837: #            $$settings{$id}{text} =~ s#<p></p>##g;
 2838:             $$settings{$id}{text} =~ s#<p>#<br /><br />#g;
 2839:             $$settings{$id}{text} =~ s#<\\p>##g;
 2840:         }
 2841:         if ($$settings{$id}{class} eq 'numerical') {
 2842:             foreach my $numid (@{$$settings{$id}{numids}}) {
 2843:                 foreach my $var (keys %{$$settings{$id}{$numid}{vars}}) {
 2844:                     $$settings{$id}{text} =~ s/{($var)}/\$$1 /g;
 2845:                 }
 2846:             }
 2847:         }
 2848:         $permcontainer =~ s#/home/httpd/html/userfiles#uploaded#;
 2849:         my $symb = $cid.'.'.$permcontainer.'___'.$qnum.'___lib/templates/simpleproblem.problem.0.';
 2850:         my %resourcedata = ();
 2851:         for (my $i=0; $i<10; $i++) {
 2852:             my $iter = $i+1;
 2853:             $resourcedata{$symb.'text'.$iter} = "";
 2854:             $resourcedata{$symb.'value'.$iter} = "unused";
 2855:             $resourcedata{$symb.'position'.$iter} = "random";
 2856:         }
 2857:         $resourcedata{$symb.'randomize'} = 'yes';
 2858:         $resourcedata{$symb.'maxfoils'} = 10;
 2859:         if ($context eq 'CSTR') {
 2860:             $output = qq|<problem>
 2861: |;
 2862:         }
 2863:         $$total{prob} ++;
 2864:         if (exists($$settings{$id}{uri})) {
 2865:             if ($$settings{$id}{imagtype} =~ /^image\//) {
 2866:                 $questionimage = '<p><img src="../../resfiles/'.$$settings{$id}{uri}.'" /></p>'."\n";
 2867:             }
 2868:         }
 2869:         if ($$settings{$id}{class} eq "paragraph") {
 2870:             if ($context eq 'CSTR') {
 2871:                 $output .= qq|<startouttext /><p>$$settings{$id}{text}</p>$questionimage<endouttext />
 2872:  <essayresponse>
 2873:  <textfield></textfield>
 2874:  </essayresponse>
 2875:  <postanswerdate>
 2876:   $allfeedback
 2877:  </postanswerdate>
 2878: |;
 2879:             } else {
 2880:                 $resourcedata{$symb.'questiontext'} = '<p>'.$$settings{$id}{text}.'</p>'.$questionimage;
 2881:                 $resourcedata{$symb.'hiddenparts'} = '!essay';
 2882:                 $resourcedata{$symb.'questiontype'} = 'essay';
 2883:             }
 2884:         } else {
 2885:             if ($context eq 'CSTR') {
 2886:                 $output .= qq|<startouttext /><p>$$settings{$id}{text}</p>$questionimage<endouttext />\n|;
 2887:             } else {
 2888:                 $resourcedata{$symb.'questiontext'} = '<p>'.$$settings{$id}{text}.'</p>'.$questionimage;
 2889:             }
 2890:             if ($$settings{$id}{class} eq 'multiplechoice') {
 2891:                 foreach my $list (@{$$settings{$id}{lists}}) {
 2892:                     my $numfoils = @{$$allanswers{$id}{$list}};
 2893:                     if ($$settings{$id}{$list}{rcardinality} eq 'Single') {
 2894:                         if ($context eq 'CSTR') {
 2895:                             $output .= qq|
 2896:  <radiobuttonresponse max="$numfoils" randomize="$$settings{$id}{$list}{randomize}">
 2897:   <foilgroup>
 2898: |;
 2899:                         } else {
 2900:                             $resourcedata{$symb.'hiddenparts'} = '!radio';
 2901:                             $resourcedata{$symb.'questiontype'} = 'radio';
 2902:                             $resourcedata{$symb.'maxfoils'} = $numfoils;
 2903:                         }
 2904:                         for (my $k=0; $k<@{$$allanswers{$id}{$list}}; $k++) {
 2905:                             my $iter = $k+1;
 2906:                             $output .= "   <foil name=\"foil".$k."\" value=\"";
 2907:                             if (grep/^$$allanswers{$id}{$list}[$k]$/,@{$$settings{$id}{$list}{correctanswer}}) {
 2908:                                 $output .= "true\" location=\"";
 2909:                                 $resourcedata{$symb.'value'.$iter} = "true";
 2910:                             } else {
 2911:                                 $output .= "false\" location=\"";
 2912:                                 $resourcedata{$symb.'value'.$iter} = "false";
 2913:                             }
 2914:                             if (lc ($$allanswers{$id}{$list}[$k]) =~ m/^\s?([Aa]ll)|([Nn]one)\s(of\s)?the\sabove\.?/) {
 2915:                                 $output .= "bottom\"";
 2916:                                 $resourcedata{$symb.'position'.$iter} = "bottom";
 2917:                             } else {
 2918:                                 $output .= "random\"";
 2919:                             }
 2920:                             if ($$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{texttype} eq 'text/html') {
 2921:                                 $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text} = &HTML::Entities::decode($$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text});
 2922:                                 $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text} = &Apache::lonxml::htmlclean($$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text});
 2923:                                 $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text} =~  s#(<img src=")([^>]+)>#$1../../resfiles/$2 />#gi;
 2924:                                 $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text} =~  s#</?p>##g;
 2925: 
 2926:                             }
 2927:                             $output .= "\><startouttext />".$$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text};
 2928:                             $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text};
 2929:                             $output .= '<endouttext /></foil>'."\n";
 2930:                         }
 2931:                         if ($context eq 'CSTR') {
 2932:                             chomp($output);
 2933:                             $output .= qq|
 2934:   </foilgroup>
 2935:  </radiobuttonresponse>
 2936: |;
 2937:                         }
 2938:                     } else {
 2939:                         if ($context eq 'CSTR') {
 2940:                             $output .= qq|
 2941:    <optionresponse max="$numfoils" randomize="yes">
 2942:     <foilgroup options="('True','False')">
 2943: |;
 2944:                         } else {
 2945:                             $resourcedata{$symb.'newopt'} = '';
 2946:                             $resourcedata{$symb.'delopt'} = '';
 2947:                             $resourcedata{$symb.'options'} = "('True','False')";
 2948:                             $resourcedata{$symb.'hiddenparts'} = '!option';
 2949:                             $resourcedata{$symb.'questiontype'} = 'option';
 2950:                             $resourcedata{$symb.'maxfoils'} = $numfoils;
 2951:                         }
 2952:                         for (my $k=0; $k<@{$$allanswers{$id}{$list}}; $k++) {
 2953:                             my $iter = $k+1;
 2954:                             $output .= "   <foil name=\"foil".$k."\" value=\"";
 2955:                             if (grep/^$$allanswers{$id}{$list}[$k]$/,@{$$settings{$id}{$list}{correctanswer}}) {
 2956:                                 $output .= "True\"";
 2957:                                 $resourcedata{$symb.'value'.$iter} = "True";
 2958:                             } else {
 2959:                                 $output .= "False\"";
 2960:                                 $resourcedata{$symb.'value'.$iter} = "False";
 2961:                             }
 2962:                             if ($$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{texttype} eq 'text/html') {
 2963:                                 $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text} = &HTML::Entities::decode($$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text});
 2964:                                 $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text} = &Apache::lonxml::htmlclean($$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text});
 2965: 
 2966:                                 $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text} =~  s#(<img src=")([^>]+)>#$1../../resfiles/$2 />#gi;
 2967:                                 $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text} =~  s#</?p>##g;
 2968:                             }
 2969:                             $output .= "\><startouttext />".$$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text}."<br /><endouttext /></foil>\n";
 2970:                             $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text};
 2971:                         }
 2972:                         if ($context eq 'CSTR') {
 2973:                             chomp($output);
 2974:                             $output .= qq|
 2975:     </foilgroup>
 2976:    </optionresponse>
 2977: |;
 2978:                         }
 2979:                     }
 2980:                 }
 2981:             } elsif ($$settings{$id}{class} eq 'match') {
 2982:                 my %allmatchers = ();
 2983:                 my @allmatch = ();
 2984:                 my %matchtext = ();
 2985:                 my $anscount = 0;
 2986:                 my %ansnum = ();
 2987:                 my $maxfoils = 0;
 2988:                 my $test_for_html = 0; 
 2989:                 foreach my $grp (@{$$allchoices{$id}}) {
 2990:                     $maxfoils += @{$$settings{$id}{$grp}{correctanswer}};
 2991:                     foreach my $answer_id (@{$$allanswers{$id}{$grp}}) {
 2992:                         if ($$settings{$id}{$grp}{$answer_id}{texttype} eq '/text/html') {
 2993:                              
 2994:                             $$settings{$id}{$grp}{$answer_id}{text} = &HTML::Entities::decode($$settings{$id}{$grp}{$answer_id}{text});
 2995:                             $test_for_html = &test_for_html($$settings{$id}{$grp}{$answer_id}{text});
 2996:                             $$settings{$id}{$grp}{$answer_id}{text} = &Apache::lonxml::chtmlclean($$settings{$id}{$grp}{$answer_id}{text});
 2997:                             $$settings{$id}{$grp}{$answer_id}{text} =~  s#(<img src=")([^>]+)>#$1../../resfiles/$2 />#gi;
 2998:                             $$settings{$id}{$grp}{$answer_id}{text} =~  s#</?p>##g;
 2999:                         }
 3000:                         unless (exists($allmatchers{$$settings{$id}{$grp}{$answer_id}{text}})) {
 3001:                             $allmatchers{$$settings{$id}{$grp}{$answer_id}{text}} = $anscount;
 3002:                             $allmatch[$anscount] = $$settings{$id}{$grp}{$answer_id}{text};
 3003:                             $anscount ++;
 3004:                             
 3005:                         }
 3006:                         if (grep/^$answer_id$/,@{$$settings{$id}{$grp}{correctanswer}}) {
 3007:                             push(@{$ansnum{$grp}},$allmatchers{$$settings{$id}{$grp}{$answer_id}{text}});
 3008:                         }
 3009:                     }
 3010:                     if ($context eq 'DOCS') {
 3011:                         $matchtext{$ansnum{$grp}[0]} = $allmatch[$ansnum{$grp}[0]-1];
 3012:                     }
 3013:                 }
 3014:                 my $allmatchlist = "('".join("','",@allmatch)."')";
 3015:                 if ($context eq 'CSTR') {
 3016:                     if ($test_for_html) {
 3017:                         $output .= qq|
 3018: <matchresponse max="$maxfoils" randomize="yes">
 3019:     <foilgroup>
 3020:         <itemgroup>
 3021: |;
 3022:                     } else {
 3023:                         $output .= qq|
 3024: <optionresponse max="10" randomize="yes">
 3025:     <foilgroup options=$allmatchlist>
 3026: |;
 3027:                     }
 3028:                 } else {
 3029:                     $resourcedata{$symb.'newopt'} = '';
 3030:                     $resourcedata{$symb.'delopt'} = '';
 3031:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 3032:                     $resourcedata{$symb.'questiontype'} = 'option';
 3033:                     $resourcedata{$symb.'maxfoils'} =  $maxfoils;
 3034:                 }
 3035:                 my $iter = 0;
 3036:                 foreach my $match (@allmatch) {  
 3037:                     $iter ++;
 3038:                     if ($context eq 'CSTR') {
 3039:                         if ($test_for_html) {
 3040:                             $output .= qq|
 3041: <item name="ans_$iter">
 3042: <startouttext />$match<endouttext />
 3043: </item>
 3044: |;
 3045:                         }
 3046:                     }
 3047:                 }
 3048:                 if ($context eq 'CSTR') {
 3049:                     if ($test_for_html) {
 3050:                         $output .= qq|
 3051:         </itemgroup>
 3052: |;
 3053:                     }
 3054:                 }
 3055:                 $iter = 0;
 3056:                 for (my $k=0; $k<@{$$allchoices{$id}}; $k++) {
 3057:                     if ($$settings{$id}{$$allchoices{$id}[$k]}{texttype} eq 'text/html') {
 3058:                         $$settings{$id}{$$allchoices{$id}[$k]}{text} = &HTML::Entities::decode($$settings{$id}{$$allchoices{$id}[$k]}{text});
 3059:                         $$settings{$id}{$$allchoices{$id}[$k]}{text} = &Apache::lonxml::htmlclean($$settings{$id}{$$allchoices{$id}[$k]}{text});
 3060:                         $$settings{$id}{$$allchoices{$id}[$k]}{text} =~  s#(<img src=")([^>]+)>#$1../../resfiles/$2 />#gi;
 3061:                         $$settings{$id}{$$allchoices{$id}[$k]}{text} =~  s#</?p>##g;
 3062:                     }
 3063:                     foreach my $ans (@{$ansnum{$$allchoices{$id}[$k]}}) {
 3064:                         $iter ++;
 3065:                         my $ans_id = $ans + 1;
 3066:                         if ($context eq 'CSTR') {
 3067:                             my $value;
 3068:                             if ($test_for_html) {
 3069:                                 $value = 'ans_'.$ans_id;
 3070:                             } else {
 3071:                                 $value = $allmatch[$ans];
 3072:                             }
 3073:                             $output .= qq|
 3074:         <foil location="random" value="$value" name="foil_$iter">
 3075:          <startouttext />$$settings{$id}{$$allchoices{$id}[$k]}{text}<endouttext />
 3076:         </foil>
 3077:                            
 3078: |;
 3079:                         }
 3080:                     }
 3081:                     if ($context eq 'DOCS') {
 3082:                         $resourcedata{$symb.'value'.$iter} = $matchtext{$ansnum{$$allchoices{$id}[$k]}[0]};
 3083:                         $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allchoices{$id}[0]}{text};
 3084:                     }
 3085:                 }
 3086:                 if ($context eq 'CSTR') {
 3087:                     $output .= qq|
 3088:     </foilgroup>
 3089: |;
 3090:                     if ($test_for_html) {
 3091:                         $output .= qq|
 3092: </matchresponse>
 3093: |;
 3094:                     } else {
 3095:                         $output .= qq|
 3096: </optionresponse>
 3097: |;
 3098:                     }
 3099:                 } else {
 3100:                     $resourcedata{$symb.'options'} = "('".join("','",@allmatch)."')";
 3101:                 }
 3102:             } elsif ($$settings{$id}{class} eq 'string') {
 3103:                 my $labelnum = 0;
 3104:                 foreach my $str_id (@{$$settings{$id}{str}}) {
 3105:                     foreach my $label (@{$$settings{$id}{$str_id}{labels}}) {
 3106:                         $labelnum ++;
 3107:                         my $numerical = 1;
 3108:                         if ($context eq 'DOCS') {
 3109:                             $numerical = 0;
 3110:                         } else {
 3111:                             for (my $i=0; $i<@{$$settings{$id}{strings}{$label}}; $i++) {
 3112:                                 $$settings{$id}{strings}{$label}[$i] =~ s/^\s+//;
 3113:                                 $$settings{$id}{strings}{$label}[$i] =~ s/\s+$//; 
 3114:                                 if ($$settings{$id}{strings}{$label}[$i] =~ m/([^-\d\.]|\.\.)/) {
 3115:                                     $numerical = 0;
 3116:                                 }
 3117:                             }
 3118:                         }
 3119:                         if ($numerical) {
 3120:                             my $numans;
 3121:                             my $tol;
 3122:                             if (@{$$settings{$id}{strings}{$label}} == 1) {
 3123:                                 $tol = '5%';
 3124:                                 $numans = $$settings{$id}{strings}{$label}[0];
 3125:                             } else {
 3126:                                 my $min = $$settings{$id}{strings}{$label}[0];
 3127:                                 my $max = $$settings{$id}{strings}{$label}[0];
 3128:                                 for (my $k=1; $k<@{$$settings{$id}{strings}{$label}}; $k++) {
 3129:                                     if ($$settings{$id}{strings}{$label}[$k] <= $min) {
 3130:                                         $min = $$settings{$id}{strings}{$label}[$k];
 3131:                                     }
 3132:                                     if ($$settings{$id}{strings}{$label}[$k] >= $max) {
 3133:                                         $max = $$settings{$id}{strings}{$label}[$k];
 3134:                                     }
 3135:                                 }
 3136:                                 $numans = ($max + $min)/2;
 3137:                                 if ($numans == 0) {
 3138:                                     my $dev = abs($max - $numans);
 3139:                                     if (abs($numans - $min) > $dev) {
 3140:                                         $dev = abs($numans - $min);
 3141:                                     }
 3142:                                     $tol = $dev;
 3143:                                 } else {
 3144:                                     $tol = 100*($max - $min)/($numans*2);
 3145:                                     $tol .= '%';
 3146:                                 }
 3147:                             }
 3148:                             if ($context eq 'CSTR') {
 3149:                                 if (@{$$settings{$id}{str}} > 1) {
 3150:                                     $output .= qq|
 3151: <startouttext />$labelnum.<endouttext />
 3152: |;
 3153:                                 }
 3154:                                 $output .= qq|
 3155: <numericalresponse answer="$numans">
 3156:         <responseparam type="tolerance" default="$tol" name="tol" description="Numerical Tolerance" />
 3157:         <responseparam name="sig" type="int_range,0-16" default="0,15" description="Significant Figures"
 3158: />
 3159:         <textline />
 3160: </numericalresponse>
 3161: <startouttext /><br /><endouttext />
 3162: |;
 3163:                             }
 3164:                         } else {
 3165:                             if ($context eq 'DOCS') {
 3166:                                 $resourcedata{$symb.'hiddenparts'} = '!string';
 3167:                                 $resourcedata{$symb.'questiontype'} = 'string';
 3168:                                 $resourcedata{$symb.'maxfoils'} = @{$$allanswers{$id}{strings}{$label}};
 3169:                                 $resourcedata{$symb.'hiddenparts'} = '!string';
 3170:                                 if ($$settings{$id}{$label}{case} eq "No") {
 3171:                                     $resourcedata{$symb.'stringtype'} = 'ci';
 3172:                                 } elsif ($$settings{$id}{$label}{case} eq "Yes") {
 3173:                                     $resourcedata{$symb.'stringtype'} = 'cs';
 3174:                                 }
 3175:                                 $resourcedata{$symb.'stringanswer'} = $$settings{$id}{strings}{$label}[0];
 3176:                             } else {
 3177:                                 if (@{$$settings{$id}{str}} > 1) {                                    $output .= qq|
 3178: <startouttext />$labelnum.<endouttext />
 3179: |;
 3180:                                 }
 3181:                                 if (@{$$settings{$id}{strings}{$label}} == 1) {
 3182:                                     my $casetype;
 3183:                                     if ($$settings{$id}{$label}{case} eq "No") {
 3184:                                         $casetype = 'ci';
 3185:                                     } elsif ($$settings{$id}{$label}{case} eq "Yes") {
 3186:                                         $casetype = 'cs';
 3187:                                     }
 3188:                                     $output .= qq|
 3189: <stringresponse answer="$$settings{$id}{strings}{$label}[0]" type="$casetype">
 3190: <textline>
 3191: </textline>
 3192: </stringresponse>
 3193: <startouttext /><br /><endouttext />
 3194: |;
 3195:                                 } else {
 3196:                                     my @answertext = ();
 3197:                                     for (my $k=0; $k<@{$$settings{$id}{strings}{$label}}; $k++) {
 3198:                                         $$settings{$id}{strings}{$label}[$k] =~ s/\|/\|/g;
 3199:                                         push @answertext, $$settings{$id}{strings}{$label}[$k];
 3200:                                     }
 3201:                                     my $regexpans = join('|',@answertext);
 3202:                                     $regexpans = '/^('.$regexpans.')\b/';
 3203:                                     $output .= qq|
 3204: <stringresponse answer="$regexpans" type="re">
 3205: <textline>
 3206: </textline>
 3207: </stringresponse>
 3208: <startouttext /><br /><endouttext />
 3209: |;
 3210:                                 }
 3211:                             }
 3212:                         }
 3213:                     }
 3214:                 }
 3215:             } elsif ($$settings{$id}{class} eq 'numerical') {
 3216:                 my $scriptblock = qq|
 3217: <script type="loncapa/perl">
 3218: |;
 3219:                 foreach my $numid (@{$$settings{$id}{numids}}) {
 3220:                     my $formula = $$settings{$id}{$numid}{formula};
 3221:                     foreach my $var (keys %{$$settings{$id}{$numid}{vars}}) {
 3222:                         my $decnum = $$settings{$id}{$numid}{vars}{$var}{dec};
 3223:                         my $increment = '0.';
 3224:                         if ($decnum == 0) {
 3225:                             $increment = 1; 
 3226:                         } else {
 3227:                             my $deccount = $decnum;
 3228:                             while ($deccount > 1) {
 3229:                                 $increment.= '0';
 3230:                                 $deccount --;
 3231:                             }
 3232:                             $increment .= '1';
 3233:                         } 
 3234:                         $formula =~ s/{($var)}/\$$1/g;
 3235:                         $formula =~ s/ln\(?([^\)])\)?/ &log($1) /g;
 3236:                         $formula =~ s/sqrt/\&sqrt/g;
 3237:                         $scriptblock .= qq|
 3238: \$$var=&random($$settings{$id}{$numid}{vars}{$var}{min},$$settings{$id}{$numid}{vars}{$var}{max},$increment);
 3239: |;
 3240:                     }
 3241:                     $scriptblock .= qq|
 3242: \$answervar = $formula;
 3243: </script>
 3244: |;
 3245:                     if ($context eq 'CSTR') {
 3246:                         $output = $scriptblock.$output;
 3247:                         my $ansformat = '';
 3248:                         my $sigfig = '0,15';
 3249:                         if ($$settings{$id}{$numid}{format} eq 'sig') {
 3250:                             $sigfig = $$settings{$id}{$numid}{digits}.','.$$settings{$id}{$numid}{digits};
 3251:                         } elsif ($$settings{$id}{$numid}{format} eq 'dec') {
 3252:                             $ansformat = $$settings{$id}{$numid}{digits}.'f';
 3253:                         }
 3254:                         if ($ansformat) {
 3255:                             $ansformat = 'format="'.$ansformat.'"';
 3256:                         }
 3257:                         my $tolerance = $$settings{$id}{$numid}{tolerance};
 3258:                         if ($$settings{$id}{$numid}{toltype} eq 'percent') {
 3259:                             $tolerance .= '%';
 3260:                         }
 3261:                         my $unit = '';
 3262:                         foreach my $unitid (@{$$settings{$id}{$numid}{units}}) {
 3263:                             $unit .=  $$settings{$id}{$numid}{$unitid}{text};
 3264:                         }
 3265:                         my $unitentry = '';
 3266:                         if ($unit ne '') {
 3267:                             $unitentry =  'unit='.$unit;
 3268:                         }
 3269:                         $output .= qq|
 3270: <numericalresponse $unitentry $ansformat  answer="\$answervar">
 3271:         <responseparam type="tolerance" default="$tolerance" name="tol" description="Numerical Tolerance" />
 3272:         <responseparam name="sig" type="int_range" default="$sigfig" description="Significant Figures"
 3273: />
 3274:         <textline />
 3275: </numericalresponse>
 3276: |;
 3277:                     }
 3278:                 }
 3279:             }
 3280:         }
 3281:         if ($context eq 'CSTR') {
 3282:             my $catid = $$settings{$id}{category};
 3283:             my $probdir = $$catinfo{$catid}{title}.'_'.$catid;
 3284:             $probdir =~ s/\s/_/g;
 3285:             $probdir =~ s/\W//g;
 3286:             if (!-e "$destdir/problems/$probdir") {
 3287:                 mkdir("$destdir/problems/$probdir",0755);
 3288:             }
 3289:             $output .= qq|</problem>
 3290: |;
 3291:             my $title = $$settings{$id}{title};
 3292:             $title =~ s/\s/_/g;
 3293:             $title =~ s/\W//g;
 3294:             $title .= '_'.$id; 
 3295:             open(PROB,">:utf8", "$destdir/problems/$probdir/$title.problem");
 3296:             print PROB $output;
 3297:             close PROB;
 3298:         } else {
 3299: # put %resourcedata;
 3300:             my $reply=&Apache::lonnet::cput
 3301:                 ('resourcedata',\%resourcedata,$cdom,$cnum);
 3302:         }
 3303:     }
 3304: }
 3305: 
 3306: sub test_for_html {
 3307:     my ($source) = @_; 
 3308:     my @tags = ();
 3309:     my $p = HTML::Parser->new
 3310:     (
 3311:      xml_mode => 1,
 3312:      start_h =>
 3313:      [sub {
 3314:         my ($tagname) = @_;
 3315:         push @tags, $tagname;
 3316:      }, "tagname"],
 3317:     );
 3318:     $p->parse($source);
 3319:     $p->eof;
 3320:     return length(@tags); 
 3321: } 
 3322: 
 3323: sub write_bb6_questions {
 3324:     my ($allids,$containerdir,$context,$settings,$dirname,$res,$allanswers,$allchoices) = @_;
 3325: }
 3326: 
 3327: # ---------------------------------------------------------------- Process Blackboard Announcements
 3328: sub process_announce {
 3329:     my ($res,$docroot,$destdir,$settings,$globalresref,$seqstem,$resrcfiles) = @_;
 3330:     my $xmlfile = $docroot.'/'.$res.".dat";
 3331:     my @state = ();
 3332:     my @assess = ();
 3333:     my $id;
 3334:     my $p = HTML::Parser->new
 3335:     (
 3336:      xml_mode => 1,
 3337:      start_h =>
 3338:      [sub {
 3339:         my ($tagname, $attr) = @_;
 3340:         push @state, $tagname;
 3341:         if ("@state" eq "ANNOUNCEMENT TITLE") {
 3342:             $$settings{title} = $attr->{value};
 3343:             $$settings{startassessment} = ();
 3344:         } elsif ("@state" eq "ANNOUNCEMENT DESCRIPTION FLAGS ISHTML") {  
 3345:             $$settings{ishtml} = $attr->{value};          
 3346:         } elsif ("@state" eq "ANNOUNCEMENT DESCRIPTION FLAGS ISNEWLINELITERAL" ) {
 3347:             $$settings{isnewline} = $attr->{value};
 3348:         } elsif ("@state" eq "ANNOUNCEMENT ISPERMANENT" ) {
 3349:             $$settings{ispermanent} = $attr->{value};
 3350:         } elsif ("@state" eq "ANNOUNCEMENT DATES UPDATED") {
 3351:             $$settings{dates} = $attr->{value}; 
 3352:         } elsif ("@state" eq "ANNOUNCEMENT FILES STARTASSESSMENT" ) {
 3353:             $id = $attr->{id};
 3354:             %{$$settings{startassessment}{$id}} = ();
 3355:             push @assess,$id;
 3356:         } elsif ("@state" eq "ANNOUNCEMENT FILES STARTASSESSMENT ATTRIB" ) {
 3357:             my $key = $attr->{key};
 3358:             $$settings{startassessment}{$id}{$key} = $attr->{value};
 3359:         }
 3360:      }, "tagname, attr"],
 3361:      text_h =>
 3362:      [sub {
 3363:         my ($text) = @_;
 3364:         if ("@state" eq "ANNOUNCEMENT DESCRIPTION TEXT") {
 3365:             $$settings{text} = $text;
 3366:         }
 3367:       }, "dtext"],
 3368:      end_h =>
 3369:      [sub {
 3370:         my ($tagname) = @_;
 3371:         pop @state;
 3372:      }, "tagname"],
 3373:     );
 3374:     $p->unbroken_text(1);
 3375:     $p->parse_file($xmlfile);
 3376:     $p->eof;
 3377: 
 3378:     if (defined($$settings{text})) {
 3379:         if ($$settings{ishtml} eq "false") {
 3380:             if ($$settings{isnewline} eq "true") {
 3381:                 $$settings{text} =~ s#\n#<br/>#g;
 3382:             }
 3383:         } else {
 3384:             $$settings{text} = &HTML::Entities::decode($$settings{text});
 3385:         }
 3386:     }
 3387:   
 3388:     if (@assess > 0) {
 3389:         foreach my $id (@assess) {
 3390:             $$settings{text} = "A $$settings{startassessment}{$id}{assessment_type}, entitled $$globalresref{$$settings{startassessment}{$id}{assessment_id}}{title} is available. Click <a href='$seqstem/pages/$$settings{startassessment}{$id}{assessment_id}.page' target='quizpage'>here</a> to enter the page that contains the problems in this assessment.";
 3391:         }
 3392:     }
 3393: 
 3394:     open(FILE,">$destdir/resfiles/$res.html");
 3395:     push @{$resrcfiles}, "$res.html";
 3396:     print FILE qq|<html>
 3397: <head>
 3398: <title>$$settings{title}</title>
 3399: </head>
 3400: <body bgcolor='#ffffff'>
 3401: <table>
 3402:  <tr>
 3403:   <td bgcolor='#CCCCFF'>$$settings{title} - announcement date: $$settings{dates}</td>
 3404:  </tr>
 3405: </table>
 3406: <br/>
 3407: $$settings{text}
 3408: |;
 3409:     print FILE qq|
 3410:   </body>
 3411:  </html>|;
 3412:     close(FILE);
 3413: }
 3414: 
 3415: # ---------------------------------------------------------------- Process Blackboard Content
 3416: sub process_content {
 3417:     my ($cms,$res,$context,$docroot,$destdir,$settings,$dom,$user,$resrcfiles,$packages,$hrefs) = @_;
 3418:     my $xmlfile = $docroot.'/'.$res.".dat";
 3419:     my $destresdir = $destdir;
 3420:     if ($context eq 'CSTR') {
 3421:         $destresdir =~ s|/home/$user/public_html/|/res/$dom/$user/|;
 3422:     } elsif ($context eq 'DOCS') {
 3423:         $destresdir =~ s|^/home/httpd/html/userfiles|/uploaded|;
 3424:     }
 3425:     my $filetag = '';
 3426:     if ($cms eq 'bb5') {
 3427:         $filetag = 'FILEREF';
 3428:     } elsif ($cms eq 'bb6') {
 3429:         $filetag = 'FILE';
 3430:     }
 3431:     my $filecount = 0;
 3432:     my @allrelfiles = ();
 3433:     my @state;
 3434:     @{$$settings{files}} = (); 
 3435:     my $p = HTML::Parser->new
 3436:     (
 3437:       xml_mode => 1,
 3438:       start_h =>
 3439:       [sub {
 3440:         my ($tagname, $attr) = @_;
 3441:         push @state, $tagname;
 3442:         if ("@state" eq "CONTENT ") {
 3443:             %{$$settings{maindata}} = ();
 3444:         } elsif ("@state" eq "CONTENT TITLECOLOR") {
 3445:             $$settings{titlecolor} =  $attr->{value};
 3446:         } elsif ("@state" eq "CONTENT MAINDATA TEXTCOLOR") {
 3447:             $$settings{maindata}{color} = $attr->{value};
 3448:         } elsif ("@state" eq "CONTENT MAINDATA FLAGS ISHTML") {  
 3449:             $$settings{maindata}{ishtml} = $attr->{value}; 
 3450:         } elsif ("@state" eq "CONTENT MAINDATA FLAGS ISNEWLINELITERAL") {  
 3451:             $$settings{maindata}{isnewline} = $attr->{value};
 3452:         } elsif ("@state" eq "CONTENT BODY TYPE") {
 3453:             $$settings{maindata}{bodytype} =  $attr->{value};
 3454:         } elsif ("@state" eq "CONTENT FLAGS ISAVAILABLE" ) {
 3455:             $$settings{isavailable} = $attr->{value};
 3456:         } elsif ("@state" eq "CONTENT FLAGS ISFOLDER" ) {
 3457:             $$settings{isfolder} = $attr->{value};
 3458:         } elsif ("@state" eq "CONTENT FLAGS LAUNCHINNEWWINDOW" ) {
 3459:             $$settings{newwindow} = $attr->{value};
 3460:         } elsif ("@state" eq "CONTENT FILES $filetag") {
 3461:             %{$$settings{files}[$filecount]} = ();
 3462:             %{$$settings{files}[$filecount]{registry}} = (); 
 3463:         } elsif ("@state" eq "CONTENT FILES FILEREF RELFILE" ) {
 3464:             $$settings{files}[$filecount]{'relfile'} = $attr->{value};
 3465:             push @allrelfiles, $attr->{value};
 3466:         } elsif ("@state" eq "CONTENT FILES $filetag MIMETYPE") {
 3467:             $$settings{files}[$filecount]{mimetype} = $attr->{value};
 3468:         } elsif ("@state" eq "CONTENT FILES $filetag CONTENTTYPE") {
 3469:             $$settings{files}[$filecount]{contenttype} = $attr->{value};
 3470:         } elsif ("@state" eq "CONTENT FILES $filetag FILEACTION") {
 3471:             $$settings{files}[$filecount]{fileaction} = $attr->{value};
 3472:         } elsif ("@state" eq "CONTENT FILES $filetag PACKAGEPARENT") {
 3473:             $$settings{files}[$filecount]{packageparent} = $attr->{value};
 3474:         } elsif ("@state" eq "CONTENT FILES $filetag LINKNAME") {
 3475:             $$settings{files}[$filecount]{linkname} = $attr->{value};
 3476:         } elsif ("@state" eq "CONTENT FILES $filetag REGISTRY REGISTRYENTRY") {
 3477:             my $key = $attr->{key};
 3478:             $$settings{files}[$filecount]{registry}{$key} = $attr->{value};
 3479:         }
 3480:       }, "tagname, attr"],
 3481:       text_h =>
 3482:       [sub {
 3483:         my ($text) = @_;
 3484:         if ("@state" eq "CONTENT TITLE") {
 3485:             $$settings{title} = $text;
 3486:         } elsif ( ("@state" eq "CONTENT MAINDATA TEXT") || ("@state" eq "CONTENT BODY TEXT") ) {
 3487:             $$settings{maindata}{text} = $text;
 3488:         }  elsif ("@state" eq "CONTENT FILES $filetag REFTEXT") {
 3489:             $$settings{files}[$filecount]{reftext} = $text;
 3490:         } elsif ("@state" eq "CONTENT FILES FILE NAME" ) {
 3491:             $$settings{files}[$filecount]{'relfile'} = $text;
 3492:             push @allrelfiles, $text;
 3493:         }
 3494:        }, "dtext"],
 3495:       end_h =>
 3496:       [sub {
 3497:         my ($tagname) = @_;
 3498:         if ("@state" eq "CONTENT FILES $filetag") {
 3499:             $filecount ++;
 3500:         }
 3501:         pop @state;
 3502:       }, "tagname"],
 3503:      );
 3504:     $p->unbroken_text(1);
 3505:     $p->parse_file($xmlfile);
 3506:     $p->eof;
 3507:     my $linktag = '';
 3508:     my $fontcol = '';
 3509:     if (@{$$settings{files}} > 0) {
 3510:         for (my $filecount=0;  $filecount<@{$$settings{files}}; $filecount++) {
 3511:             if ($$settings{files}[$filecount]{'fileaction'} eq 'embed') {
 3512:                 if ( $$settings{files}[$filecount]{reftext} =~ m#<\!\-\-\s_(\d+)\\_\s\-\-\>#) { 
 3513:                     my $newtag = qq|<img src="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}"/>|;
 3514:                     $$settings{maindata}{text} =~ s#<\!\-\-\s_/($1)\\_\s\-\-\>#$newtag#;
 3515:                 } elsif ( $$settings{files}[$filecount]{reftext} =~m#^_/(\d+)\\_$# ) {
 3516:                     my $reftag = $1;
 3517:                     my $newtag;
 3518:                     if ($$settings{files}[$filecount]{mimetype} =~ m/^image/) {
 3519:                         $newtag = qq|<img src="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}"|;
 3520:                         if ( defined($$settings{files}[$filecount]{registry}{alttext}) ) {
 3521:                             $newtag .= qq| alt="$$settings{files}[$filecount]{registry}{alttext}"|;
 3522:                         }
 3523:                         if ( defined($$settings{files}[$filecount]{registry}{alignment}) )
 3524: {
 3525:                             $newtag .= qq| align="$$settings{files}[$filecount]{registry}{alignment}"|; 
 3526:                         }
 3527:                         if ( defined($$settings{files}[$filecount]{registry}{border}) ) {
 3528:                             $newtag .= qq| border="$$settings{files}[$filecount]{registry}{border}"|;
 3529:                         }
 3530:                         $newtag .= " />";
 3531:                         my $reftext =  $$settings{files}[$filecount]{reftext};
 3532:                         my $fname = $$settings{files}[$filecount]{'relfile'};
 3533:                         $$settings{maindata}{text} =~ s/<!\-\-\sCOMMENT\sBLOCK\sFOR\sEMBEDDED\sFILE:\s$fname[\s\n]+DO\sNOT\sEDIT\sTHIS\sCOMMENT\sBLOCK[\s\n]+//;
 3534: #                      $$settings{maindata}{text} =~ s/DO\sNOT\sEDIT\sTHIS\sCOMMENT\sBLOCK[\s\n]+//;
 3535:                         $$settings{maindata}{text} =~ s/Move\swhole\scomment\sto\schange\sfile\splacement\swithin\spage\.[\s\n]+//;
 3536:                         $$settings{maindata}{text} =~ s/_\/$reftag\\_/$newtag/;
 3537:                         $$settings{maindata}{text} =~ s/END\sOF\sBLOCK\sON\sNEXT\sLINE[\s\n]+//;
 3538:                         $$settings{maindata}{text} =~ s/\-\->//;
 3539: #                      $$settings{maindata}{text} =~ s/<!\-\-\sCOMMENT\sBLOCK\sFOR\sEMBEDDED\sFILE:\s$fname[\s\n]+DO\sNOT\sEDIT\sTHIS\sCOMMENT\sBLOCK[\s\n\]+_\/$reftag\\_[\s\n]+END\sOF\sBLOCK\sON\sNEXT\sLINE[\s\n\]+\-\->/$newtag/;
 3540: #                      print STDERR $$settings{maindata}{text};
 3541:                     }
 3542:                 } else {
 3543:                     my $filename=$$settings{files}[$filecount]{'relfile'};
 3544:                     my $newfilename="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}";
 3545:                     $$settings{maindata}{text} =~ s#(src|SRC|value)=("|&quot;)$filename("|&quot;)#$1="$newfilename"#g;
 3546:                 }
 3547:             } elsif ($$settings{files}[$filecount]{fileaction} eq 'link') {
 3548:                 unless (($$settings{files}[$filecount]{packageparent} ne '') && (grep/^$$settings{files}[$filecount]{packageparent}$/,@{$$settings{files}}) ) {
 3549:                     $linktag .= qq|<a href="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}"|;
 3550:                     if ($$settings{newwindow} eq "true") {
 3551:                         $linktag .= qq| target="$res$filecount"|;
 3552:                     }
 3553:                     foreach my $entry (keys %{$$settings{files}[$filecount]{registry}}) {
 3554:                         $linktag .= qq| $entry="$$settings{files}[$filecount]{registry}{$entry}"|;
 3555:                     }
 3556:                       $linktag .= qq|>$$settings{files}[$filecount]{linkname}</a><br/>\n|;
 3557:                 }
 3558:             } elsif ( ($$settings{files}[$filecount]{fileaction} eq 'PACKAGE') || ($$settings{files}[$filecount]{fileaction} eq 'package') ) {
 3559:                my $open_package = '';
 3560:                if ($$settings{files}[$filecount]{'relfile'} =~ m|\.zip$|i) {
 3561:                    $open_package = &expand_zip("$docroot/$res",$$settings{files}[$filecount]{'relfile'});
 3562:                }
 3563:                if ($open_package eq 'ok') {
 3564:                    opendir(DIR,"$docroot/$res");
 3565:                    my @dircontents = grep(!/^\./,readdir(DIR));
 3566:                    closedir(DIR);
 3567:                    push @{$resrcfiles}, @dircontents;
 3568:                    @{$$hrefs{$res}} = @dircontents;
 3569:                    push @{$packages}, $res;
 3570:                }
 3571:             } elsif ( ($$settings{files}[$filecount]{fileaction} eq 'BROKEN_IMAGE') && ($cms eq 'bb6') ) {
 3572:                 my $filename=$$settings{files}[$filecount]{'relfile'};
 3573:                 my $newfilename="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}";
 3574:                 $$settings{maindata}{text} =~ s#(src|SRC|value)=("|&quot;)$filename("|&quot;)#$1="$newfilename"#g;
 3575:             } elsif ( ($$settings{files}[$filecount]{fileaction} eq 'LINK') && ($cms eq 'bb6') ) {
 3576:                 my $filename=$$settings{files}[$filecount]{'relfile'};
 3577:                 my $newfilename="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}";
 3578:                 my $filetitle = $$settings{files}[$filecount]{'linkname'};
 3579:                 $$settings{maindata}{text} = '<a href="'.$newfilename.'">'.$filetitle.'</a><br /><br />'. $$settings{maindata}{text};
 3580:             }
 3581:         }
 3582:     }
 3583:     if (defined($$settings{maindata}{textcolor})) {
 3584:         $fontcol =  qq|<font color="$$settings{maindata}{textcolor}">|;
 3585:     }
 3586:     if (defined($$settings{maindata}{text})) {
 3587:         if ($$settings{maindata}{bodytype} eq "S") {
 3588:             $$settings{maindata}{text} =~ s#\n#<br/>#g;
 3589:         }
 3590:         if ($$settings{maindata}{ishtml} eq "false") {
 3591:             if ($$settings{maindata}{isnewline} eq "true") {
 3592:                 $$settings{maindata}{text} =~ s#\n#<br/>#g;
 3593:             }
 3594:         } else {
 3595: #            $$settings{maindata}{text} = &HTML::Entities::decode($$settings{maindata}{text});
 3596:         }
 3597:     }
 3598: 
 3599:     if (!open(FILE,">$destdir/resfiles/$res.html")) {
 3600:         &Apache::lonnet::logthis("IMS import error: Cannot open file - $destdir/resfiles/$res.html - $!");
 3601:     } else {
 3602:         push @{$resrcfiles}, "$res.html";
 3603:         my $htmldoc = 0;
 3604: #        if ($$settings{maindata}{text} =~ m-&lt;(html|HTML)>.+&lt;\\(html|HTML)-) {
 3605:         if ($$settings{maindata}{text} =~ m-<(html|HTML)>-) {
 3606:             $htmldoc = 1;
 3607:         }
 3608:         unless ($htmldoc) {
 3609:             print FILE qq|<html>
 3610: <head>
 3611: <title>$$settings{title}</title>
 3612: </head>
 3613: <body bgcolor='#ffffff'>
 3614: $fontcol
 3615: |;
 3616:         }
 3617:         unless ($$settings{title} eq '') { 
 3618:             print FILE qq|$$settings{title}<br/><br/>\n|;
 3619:         }
 3620:         print FILE qq|
 3621: $$settings{maindata}{text}
 3622: $linktag|;
 3623:         unless ($htmldoc) {
 3624:             if (defined($$settings{maindata}{textcolor})) {
 3625:                 print FILE qq|</font>|;
 3626:             }
 3627:             print FILE qq|
 3628:   </body>
 3629:  </html>|;
 3630:         }
 3631:         close(FILE);
 3632:     }
 3633: }
 3634: 
 3635: 
 3636: sub process_angelboards {
 3637:     my ($context,$destdir,$boards,$timestamp,$crs,$cdom,$uname,$db_handling,$messages,$items,$resources,$hrefs,$tempdir,$longcrs) = @_;
 3638:     for (my $i=0; $i<@{$boards}; $i++) {
 3639:         my %msgidx = ();
 3640:         my $forumtext = '';
 3641:         my $boardname = 'bulletinpage_'.$$timestamp[$i];
 3642:         my $forumfile = $tempdir.'/_assoc/'.$$boards[$i].'/pg'.$$boards[$i].'.htm';
 3643:         my @state = ();
 3644:         my $p = HTML::Parser->new
 3645:         (
 3646:            xml_mode => 1,
 3647:            start_h =>
 3648:            [sub {
 3649:                 my ($tagname, $attr) = @_;
 3650:                 push @state, $tagname;
 3651:                 },  "tagname, attr"],
 3652:            text_h =>
 3653:            [sub {
 3654:                 my ($text) = @_;
 3655:                 if ("@state" eq "html body div div") {
 3656:                     $forumtext = $text;
 3657:                 }
 3658:               }, "dtext"],
 3659:             end_h =>
 3660:             [sub {
 3661:                   my ($tagname) = @_;
 3662:                   pop @state;
 3663:                }, "tagname"],
 3664:         );
 3665:         $p->parse_file($forumfile);
 3666:         $p->eof;
 3667: 
 3668:         my %boardinfo = (
 3669:                   'aaa_title' => $$items{$$resources{$$boards[$i]}{revitm}}{title},
 3670:                   'bbb_content' => $forumtext,
 3671:                   'ccc_webreferences' => '',
 3672:                   'uploaded.lastmodified' => time,
 3673:                   );
 3674:         my $msgcount = 0; 
 3675:                                                                                                      
 3676:         my $putresult = &Apache::lonnet::put($boardname,\%boardinfo,$cdom,$crs);
 3677:         if ($db_handling eq 'importall') {
 3678:             foreach my $msg_id (@{$$messages{$$boards[$i]}}) {
 3679:                 $msgcount ++;
 3680:                 $msgidx{$msg_id} = $msgcount;
 3681:                 my %contrib = (
 3682:                             'sendername' => 'NoName',
 3683:                             'senderdomain' => $cdom,
 3684:                             'screenname' => '',
 3685:                             'message' => $$items{$$resources{$msg_id}{revitm}}{title}
 3686:                             );
 3687:                 unless ( $$items{$$resources{$msg_id}{revitm}}{parentseq} eq $$resources{$$boards[$i]}{revitm} ) {
 3688:                     unless ( $msgidx{$$items{$$items{$$resources{$msg_id}{revitm}}{parentseq}}{resnum}} eq ''){
 3689:                         $contrib{replyto} = $msgidx{$$items{$$items{$$resources{$msg_id}{revitm}}{parentseq}}{resnum}};
 3690:                     }
 3691:                 }
 3692:                 if ( @{$$hrefs{$msg_id}} > 1 )  {
 3693:                     my $newurl = '';
 3694:                     foreach my $file (@{$$hrefs{$msg_id}}) {
 3695:                         unless ($file eq 'pg'.$msg_id.'.htm') {
 3696:                             $newurl = $msg_id.$file;
 3697:                              unless ($longcrs eq '') {
 3698:                                 if ($context eq 'CSTR') {
 3699:                                     if (!-e "/home/httpd/lonUsers/$cdom/$longcrs/userfiles") {
 3700:                                         mkdir("/home/httpd/lonUsers/$cdom/$longcrs/userfiles",0755);
 3701:                                     }
 3702:                                     if (!-e "/home/httpd/lonUsers/$cdom/$longcrs/userfiles/$newurl") {
 3703:                                         rename("$destdir/resfiles/$msg_id/$file","/home/httpd/lonUsers/$cdom/$longcrs/userfiles/$newurl");
 3704:                                     }
 3705:                                 }
 3706:                                 $contrib{attachmenturl} = '/uploaded/'.$cdom.'/'.$crs.'/'.$file;
 3707:                             }
 3708:                         }
 3709:                     }
 3710:                 }
 3711:                 my $xmlfile = $tempdir.'/_assoc/'.$msg_id.'/'.$$resources{$msg_id}{file};
 3712:                 &angel_message($msg_id,\%contrib,$xmlfile);
 3713:                 unless ($$resources{$msg_id}{file} eq '') {
 3714:                     unlink($xmlfile);
 3715:                 }
 3716:                 my $symb = 'bulletin___'.$$timestamp[$i].'___adm/wrapper/adm/'.$cdom.'/'.$uname.'/'.$$timestamp[$i].'/bulletinboard';
 3717:                 my $postresult = &addposting($symb,\%contrib,$cdom,$crs);
 3718:             }
 3719:         }
 3720:     }
 3721: }
 3722: 
 3723: # ---------------------------------------------------------------- Process ANGEL message board messages
 3724: sub angel_message {
 3725:     my ($msg_id,$contrib,$xmlfile) = @_;
 3726:     my @state = ();
 3727:     my $p = HTML::Parser->new
 3728:     (
 3729:        xml_mode => 1,
 3730:        start_h =>
 3731:        [sub {
 3732:              my ($tagname, $attr) = @_;
 3733:              push @state, $tagname;
 3734:              },  "tagname, attr"],
 3735:         text_h =>
 3736:         [sub {
 3737:              my ($text) = @_;
 3738:              if ("@state" eq "html body table tr td div small span") {
 3739:                   $$contrib{'plainname'} = $text;
 3740:              } elsif ("@state" eq "html body div div") {
 3741:                   $$contrib{'message'} .= '<br /><br />'.$text;
 3742:              }
 3743:            }, "dtext"],
 3744:          end_h =>
 3745:          [sub {
 3746:                my ($tagname) = @_;
 3747:                pop @state;
 3748:             }, "tagname"],
 3749:     );
 3750:     $p->parse_file($xmlfile);
 3751:     $p->eof;
 3752: }
 3753: 
 3754: # ---------------------------------------------------------------- ANGEL content
 3755: sub angel_content {
 3756:     my ($res,$docroot,$destdir,$settings,$dom,$user,$type,$title,$resrcfiles) = @_;
 3757:     my $xmlfile = $docroot.'/_assoc/'.$res.'/pg'.$res.'.htm';
 3758:     my $filecount = 0;
 3759:     my $firstline;
 3760:     my $lastline;
 3761:     my @buffer = ();
 3762:     my @state;
 3763:     @{$$settings{links}} = ();
 3764:     my $p = HTML::Parser->new
 3765:     (
 3766:        xml_mode => 1,
 3767:        start_h =>
 3768:        [sub {
 3769:              my ($tagname, $attr) = @_;
 3770:              push @state, $tagname;
 3771:             },  "tagname, attr"],
 3772:        text_h =>
 3773:        [sub {
 3774:              my ($text) = @_;
 3775:              if ("@state" eq "html body table tr td div small span") {
 3776:                  $$settings{'subtitle'} = $text;
 3777:              } elsif ("@state" eq "html body div div") {
 3778:                  $$settings{'text'} = $text;
 3779:              } elsif ("@state" eq "html body div div a") {
 3780:                 push @{$$settings{'links'}}, $text;
 3781:              }
 3782:             }, "dtext"],
 3783:        end_h =>
 3784:        [sub {
 3785:              my ($tagname) = @_;
 3786:              pop @state;
 3787:             }, "tagname"],
 3788:     );
 3789:     $p->parse_file($xmlfile);
 3790:     $p->eof;
 3791:     if ($type eq "PAGE") {
 3792:         open(FILE,"<$xmlfile");
 3793:         @buffer = <FILE>;
 3794:         close(FILE);
 3795:         chomp(@buffer);
 3796:         $firstline = -1;
 3797:         $lastline = 0;
 3798:         for (my $i=0; $i<@buffer; $i++) {
 3799:             if (($firstline == -1) && ($buffer[$i] =~ m/<div\sclass="normalDiv"><div\sclass="normalSpan">/)) {
 3800:                 $firstline = $i;
 3801:                 $buffer[$i] = substr($buffer[$i],index($buffer[$i],'"normalSpan"')+13);
 3802:             }
 3803:             if (($firstline > -1) && ($buffer[$i] =~ m-<p></p></div></div>-)) {
 3804:                 $buffer[$i] = substr($buffer[$i],0,index($buffer[$i],'<p></p></div></div>'));
 3805:                 $lastline = $i;
 3806:             }
 3807:         }
 3808:     }
 3809:     open(FILE,">$destdir/resfiles/$res.html");
 3810:     push @{$resrcfiles}, "$res.html";
 3811:     print FILE qq|<html>
 3812: <head>
 3813: <title>$title</title>
 3814: </head>
 3815: <body bgcolor='#ffffff'>
 3816:     |;
 3817:     unless ($title eq '') {
 3818:         print FILE qq|<b>$title</b><br/>\n|;
 3819:     }
 3820:     unless ($$settings{subtitle} eq '') {
 3821:         print FILE qq|$$settings{subtitle}<br/>\n|;
 3822:     }
 3823:     print FILE "<br/>\n";
 3824:     if ($type eq "LINK") {
 3825:         foreach my $link (@{$$settings{links}}) {
 3826:             print FILE qq|<a href="$link">$link</a><br/>\n|; 
 3827:         }
 3828:     } elsif ($type eq "PAGE") {
 3829:         if ($firstline > -1) {
 3830:             for (my $i=$firstline; $i<=$lastline; $i++) {
 3831:                 print FILE "$buffer[$i]\n";
 3832:             }
 3833:         }
 3834:     }
 3835:     print FILE qq|
 3836:   </body>
 3837:  </html>|;
 3838:     close(FILE);
 3839: }
 3840: 
 3841: # ---------------------------------------------------------------- WebCT content
 3842: sub webct4_content {
 3843:     my ($res,$docroot,$destdir,$settings,$dom,$user,$type,$title,$resrcfiles) = @_;
 3844:     if (!open(FILE,">$destdir/resfiles/$res.html")) {
 3845:         &Apache::lonnet::logthis("IMS import error: Cannot open file - $destdir/resfiles/$res.html - $!");
 3846:     } else {
 3847:         push(@{$resrcfiles}, "$res.html");
 3848:         my $linktag = '';
 3849:         if (defined($$settings{url})) {
 3850:             $linktag = qq|<a href="$$settings{url}"|;
 3851:             if ($title ne '') {
 3852:                 $linktag .= qq|>$title</a>|;
 3853:             } else {
 3854:                 $linktag .= qq|>$$settings{url}|;
 3855:             }
 3856:         }
 3857:         print FILE qq|<html>
 3858: <head>
 3859: <title>$title</title>
 3860: </head>
 3861: <body bgcolor='#ffffff'>
 3862: $linktag
 3863: </body>
 3864: </html>|;
 3865:         close(FILE);
 3866:     }
 3867: }
 3868: 
 3869: 1;
 3870: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>