File:  [LON-CAPA] / loncom / imspackages / imsprocessor.pm
Revision 1.20: download - view: text, annotated - select for diffs
Tue May 3 18:38:37 2005 UTC (19 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: version_1_99_1_tmcc, HEAD
Fetch remotely stored images, referred to img src tags, and store locally, and convert img tags.  Other fix-ups to BB5 import code.  xhtml compliance.

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

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