Annotation of loncom/imspackages/imsimport.pm, revision 1.21

1.7       raeburn     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: 
1.1       raeburn    24: package Apache::imsimport;
                     25: 
1.4       raeburn    26: use strict;
                     27: use Apache::Constants qw(:common :http :methods);
                     28: use Apache::loncacc;
                     29: use Apache::loncommon();
                     30: use Apache::lonnet;
1.5       raeburn    31: use Apache::imsprocessor;
1.4       raeburn    32: use HTML::Parser;
                     33: use HTML::Entities();
                     34: use Apache::lonlocal;
                     35: use Apache::lonupload;
1.5       raeburn    36: use File::Basename();
1.21    ! albertel   37: use LONCAPA;
        !            38: 
1.5       raeburn    39: # ----------------------------------------------------------------  Jscript One
                     40: sub jscript_one {
                     41:     my ($fullpath,$jsref) = @_;
1.14      albertel   42: 
1.20      albertel   43:     my %body_layout = ('rightmargin'  => "0",
                     44: 		       'leftmargin'   => "0",
                     45: 		       'marginwidth'  => "0",
                     46: 		       'topmargin'    => "0",
                     47: 		       'marginheight' => "0");
1.14      albertel   48:     my $start_page = 
                     49: 	&Apache::loncommon::start_page('Create IMS import directory',undef,
                     50: 				       {'only_body'   => 1,
1.20      albertel   51: 					'add_entries' => \%body_layout,
1.14      albertel   52: 					'js_ready'    => 1,});
                     53:     my $end_page = 
                     54: 	&Apache::loncommon::end_page({'js_ready' => 1,});
                     55: 
1.1       raeburn    56:     $$jsref = <<"END_OF_ONE";
                     57: function verify() {
                     58:  if ((document.forms.dataForm.newdir.value == '')  || (!document.forms.dataForm.newdir.value)) {
                     59:    alert("You must choose a destination directory for the import")
                     60:    return false
                     61:  }
                     62:  if (document.forms.dataForm.source.selectedIndex == 0) {
                     63:    alert("You must choose the Course Management System from which the IMS package was exported");
                     64:    return false
1.2       raeburn    65:  }
1.1       raeburn    66:  return true
                     67: }
1.2       raeburn    68: 
1.1       raeburn    69: function nextPage() {
1.5       raeburn    70:   if (verify()) {
                     71:     document.forms.dataForm.submit()
                     72:   }
1.1       raeburn    73: }
                     74: 
                     75: function createWin() {
                     76:   document.dataForm.newdir.value = "";
                     77:   newWindow = window.open("","CreateDir","HEIGHT=400,WIDTH=750,scrollbars=yes")
                     78:   newWindow.document.open()
1.14      albertel   79:   newWindow.document.write('$start_page')
                     80:   newWindow.document.write("\\n<img border='0' src='/adm/lonInterFace/author.jpg' alt='[Author Header]'>\\n")
1.1       raeburn    81:   newWindow.document.write("<table border='0' cellspacing='0' cellpadding='0' width='600' bgcolor='#CCFFDD'>\\n")
                     82:   newWindow.document.write("<tr><td width='2'>&nbsp;</td><td width='3'>&nbsp;</td>\\n")
                     83:   newWindow.document.write("<td><h3>Location: <tt>$fullpath</tt></h3><h3>New Directory</h3></td></tr>\\n")
                     84:   newWindow.document.write("<tr><td width='2'>&nbsp;</td><td width='3'>&nbsp;</td>\\n")
                     85:   newWindow.document.write("<td><form name='fileaction' action='/adm/cfile' method='post'>\\n")
                     86:   newWindow.document.write("<font face='arial,helvetica,sans-serif'>Enter the name of the new directory where you will store the contents of your IMS package.<br /><br />")
1.15      albertel   87:   newWindow.document.write("<input type='hidden' name='filename' value='$fullpath' />")
                     88:   newWindow.document.write("<input type='hidden' name='action' value='newdir' />")
                     89:   newWindow.document.write("<input type='hidden' name='callingmode' value='imsimport' />")
                     90:   newWindow.document.write("$fullpath<input type='text' name='newfilename' value='' />")
1.1       raeburn    91:   newWindow.document.write("<input type='button' value='Go' onClick='document.fileaction.submit();' />")
                     92:   newWindow.document.write("</td></tr>\\n")
1.14      albertel   93:   newWindow.document.write("</table>")
                     94:   newWindow.document.write('$end_page')
1.1       raeburn    95:   newWindow.document.close()
                     96:   newWindow.focus()
                     97: }
1.2       raeburn    98: 
1.5       raeburn    99: END_OF_ONE
                    100: 
                    101: }
                    102: 
                    103: # ----------------------------------------------------------------  Jscript Two
                    104: sub jscript_two {
                    105:     my ($javascript,$user,$dom,$numcrs) = @_;
                    106:     my %crsentry = ();
                    107:     my $course_list;
                    108:     my $title_list;
                    109:     my @crslist = ();
                    110:     &get_ccroles($user,$dom,\%crsentry,\@crslist);
                    111:     if (@crslist > 0) {
                    112:         $crsentry{$crslist[0]} =~ s/("|,)//g;
                    113:         $title_list = '"'.$crsentry{$crslist[0]}.'"';
                    114:         if (@crslist > 1) {
                    115:             for (my $i=1; $i<@crslist; $i++) {
                    116:                 $crsentry{$crslist[$i]} =~ s/("|,)//g;
                    117:                 $title_list .= ',"'.$crsentry{$crslist[$i]}.'"';
                    118:             }
                    119:         }
                    120:     }
                    121:     $course_list = '"'.join('","',@crslist).'"';
                    122:     $$numcrs = @crslist;
                    123: 
                    124:     $$javascript = qq#
                    125: 
                    126: function checkCourse() {
                    127:   courseID_array = new Array($course_list)
                    128:   courseTitle_array = new Array($title_list)
                    129:   var step2Form = document.forms.pickoptions
                    130:   var conditionType = step2Form.conditions.value
                    131:   var curVal = step2Form.targetcourse.options[step2Form.targetcourse.selectedIndex].value
                    132:   if (curVal == -1) {
                    133:       if ( conditionType == 'both'  )  {
                    134:           if ( step2Form.board.checked == true || step2Form.users.checked == true ) {
                    135:               setCourse(step2Form,'add')
                    136:           }
                    137:       }
                    138:       if ( conditionType == 'users'  )  {
                    139:           if ( step2Form.users.checked == true ) {
                    140:               setCourse(step2Form,'add')
                    141:           }
                    142:       }
                    143:       if ( conditionType == 'board'  )  {
                    144:           if ( step2Form.board.checked == true ) {
                    145:               setCourse(step2Form,'add')
                    146:           }
                    147:       }
                    148:   }
                    149:   else { 
                    150:       if ( conditionType == 'both'  )  {
                    151:           if ( step2Form.board.checked == false && step2Form.users.checked == false ) {
                    152:               setCourse(step2Form,'clear')
                    153:           }
                    154:       }
                    155:       if ( conditionType == 'users'  )  {
                    156:           if ( step2Form.users.checked == false ) {
                    157:               setCourse(step2Form,'clear')
                    158:           }
                    159:       }
                    160:       if ( conditionType == 'board'  )  {
                    161:           if ( step2Form.board.checked == false ) {
                    162:               setCourse(step2Form,'clear')
                    163:           }
                    164:       }
                    165:   }
                    166: }
                    167: 
                    168: function setCourse(step2Form,call) {
                    169:     step2Form.targetcourse.length = 0
                    170:     if (call == 'add') {
                    171:         step2Form.targetcourse.length = 0
                    172:         step2Form.targetcourse.options[0] = new Option("Please Select","0",true,true)
                    173:         for (var i=0; i<courseID_array.length; i++) {
                    174:             step2Form.targetcourse.options[i+1] = new Option(courseTitle_array[i],courseID_array[i],false,false)
                    175:         }
                    176:         step2Form.targetcourse.selectedIndex = 0
                    177:     }
                    178:     else {
                    179:         step2Form.targetcourse.options[0] = new Option("Not required","-1",true,true)
                    180:         step2Form.targetcourse.selectedIndex = 0
                    181:     }
                    182: }
                    183: 
                    184: 
                    185: function setOptions(caller,itemnum) {
                    186:   var numCrs = $$numcrs
                    187:   var opForm = document.forms.pickoptions
                    188:   var menu = 1 + itemnum*2
                    189:   opForm.elements[menu].length = 0
                    190:   if (opForm.elements[itemnum*2].checked == true) {
                    191:     if (caller == "board") {
                    192:       opForm.elements[menu].options[0] = new Option("Select","-1",true,true)
                    193:       opForm.elements[menu].options[1] = new Option("Import topics only","topics",true,true)
                    194:       opForm.elements[menu].options[2] = new Option("Import topics + posts (with author)","allpost",true,true)
                    195:       opForm.elements[menu].options[3] = new Option("Import topics + posts (no author)","allanon",true,true)
                    196:     }
                    197:     else { 
                    198:       if (caller == "users") {
                    199:         opForm.elements[menu].length = 0
                    200:         opForm.elements[menu].options[0] = new Option("Select","-1",true,true)
                    201:         opForm.elements[menu].options[1] = new Option("Enroll students only","students",true,true)
                    202:         opForm.elements[menu].options[2] = new Option("Enroll all users","all",true,true)
                    203:       }
                    204:     }
                    205:   }
                    206:   else {
                    207:     opForm.elements[menu].options[0] = new Option("Not required","0",true,true)
                    208:   }
                    209:   opForm.elements[menu].selectedIndex = 0
                    210:   if (numCrs > 0) {
                    211:       checkCourse()
                    212:   }
                    213: }
                    214: 
                    215: function verify(caller) {
                    216:   var numCrs = $$numcrs
                    217:   var opForm = document.forms.pickoptions
                    218:   var totcheck = 0;
                    219:   var totchg = 0;
                    220:   for (var i=0; i<caller; i++) {
                    221:     if (opForm.elements[2*i].checked == true) {
                    222:       totcheck ++
                    223:       if (opForm.elements[2*i].name == "board") { 
                    224:         if (opForm.elements[2*i+1].selectedIndex == 0) {     
                    225:           alert("You must select one of the additional options when importing Discussion Boards ")
                    226:           return false
                    227:         }
                    228:         if (numCrs == 0) {
                    229:             opForm.elements[2*i].checked = false
                    230:             totchg ++
                    231:         }
                    232:         else {
                    233:           if (opForm.targetcourse.selectedIndex == 0) {
                    234:             alert("You must select a target course when importing Discussion Boards")
                    235:             return false
                    236:           }
                    237:         }
                    238:       }
                    239:       if (opForm.elements[2*i].name == "users") {
                    240:         if (opForm.elements[2*i+1].selectedIndex == 0) {     
                    241:           alert("You must select one of the additional options when importing Enrollment")
                    242:           return false
                    243:         }
                    244:         if (numCrs == 0) {
                    245:             opForm.elements[2*i].checked = false
                    246:             totchg ++ 
                    247:         }
                    248:         else {
                    249:           if (opForm.targetcourse.selectedIndex == 0) {
                    250:             alert("You must select a target course when importing enrollment information")
                    251:             return false
                    252:           }
                    253:         }
                    254:       }
                    255:     }
                    256:   }
                    257:   if (totcheck == 0) {
                    258:     alert("You must check the Checkbox for at least one Content Type");
                    259:     return false
                    260:   }
                    261:   return true
                    262: }
                    263: 
                    264: function nextPage(caller) {
                    265:   if (verify(caller)) {
                    266:     document.forms.pickoptions.submit()
                    267:   }
1.2       raeburn   268: }
                    269: 
1.5       raeburn   270: #;
                    271: 
                    272: }
1.1       raeburn   273: 
1.5       raeburn   274: # ----------------------------------------------------------------  Jscript Three
                    275: sub jscript_three {
                    276:     my $javascript = shift;
1.1       raeburn   277: }
                    278: 
1.5       raeburn   279: # ---------------------------------------------------------------- Display One
                    280: sub display_one {
                    281:     my ($r,$uname,$fn,$fullpath) = @_;
1.1       raeburn   282:     $r->print(<<"END_OF_ONE");
                    283: <form name="dataForm" method="post">
                    284: <table border='0' bgcolor='#CCFFDD' cellspacing='0' cellpadding ='0' width='100%'>
                    285:     <tr>
                    286:      <td colspan='2'>
                    287:       <table border='0' cellspacing='0' cellpadding='0'>
                    288:        <tr>
                    289:         <td colspan='2'  align='left'>&nbsp;
                    290:         </td>
                    291:        </tr>
                    292:        <tr bgcolor='#ccddaa'>
1.2       raeburn   293:         <td valign='middle'><img src='/res/adm/pages/bl_step1.gif'>&nbsp;
1.1       raeburn   294:         </td>
                    295:         <td width='100%' align='left'>&nbsp;&nbsp;
1.2       raeburn   296:          <font face='arial,helvetica,sans-serif'><b>Specify the Course Management system used to create the package.</b>&nbsp;&nbsp;
                    297:          </font>
1.1       raeburn   298:        </td>
                    299:       </tr>
                    300:       <tr>
                    301:        <td colspan='2'>&nbsp;</td>
                    302:       </tr>
                    303:       <tr>
                    304:        <td>&nbsp;</td>
                    305:        <td>
                    306:         <font face='Arial,Helvetica,sans-serif'>
1.2       raeburn   307: Please choose the CMS used to create your IMS content package.&nbsp;&nbsp;
                    308:         <select name="source">
1.15      albertel  309:          <option value='-1' selected="true">Please select</option>
                    310:          <option value='bb5'>Blackboard 5</option>
                    311:          <option value='bb6'>Blackboard 6</option>
                    312:          <option value='angel'>ANGEL</option>
1.16      raeburn   313:          <option value='webctce4'>WebCT 4 Campus Edition</option>
1.19      raeburn   314:          <option value='webctvista4'>WebCT Vista 4</option>
1.2       raeburn   315:         </select>
                    316:         </font>
1.1       raeburn   317:        </td>
                    318:       </tr>
                    319:       <tr>
                    320:        <td colspan='2'>&nbsp;</td>
                    321:       </tr>
                    322:       <tr>
1.2       raeburn   323:        <td colspan='2'>&nbsp;</td>
                    324:       </tr>
                    325:       <tr bgcolor='#ccddaa'>
                    326:        <td valign='middle'><img src='/res/adm/pages/bl_step2.gif'>
                    327:        </td>
                    328:        <td width='100%' align='left'>&nbsp;&nbsp;
                    329:         <font face='arial,helvetica,sans-serif'><b>Create a directory where you will unpack your IMS package.</b>&nbsp;&nbsp;</font></td>
                    330:       </tr>
1.1       raeburn   331:       <tr>
1.2       raeburn   332:        <td colspan='2'>&nbsp;</td>
                    333:       </tr>
1.1       raeburn   334:        <td>&nbsp;</td>
                    335:        <td>
                    336:         <font face='Arial,Helvetica,sans-serif'>
1.15      albertel  337: Please choose a destination LON-CAPA directory in which to store the contents of the IMS package file. <input type="button" name="createdir" value="Create Directory" onClick="javascript:createWin()" /><input type="hidden" name="newdir" value="" /></font>
1.1       raeburn   338:        </td>
                    339:       </tr>
                    340:       <tr>
1.2       raeburn   341:        <td colspan='2'>&nbsp;<br /><br /></td>
1.1       raeburn   342:       </tr>
1.5       raeburn   343:       <tr>
                    344:        <td>&nbsp;</td>
                    345:        <td><font face='arial,helvetica,sans-serif'>If you have selected the CMS used to create the IMS package, and have created a destination directory, click the 'Proceed' button to continue the IMS package upload process.</font></td>
                    346:       </tr>
                    347:       <tr>
                    348:        <td colspan='2'>
1.15      albertel  349:           <input type="hidden" name="uploaduname" value="$uname" />
                    350:           <input type="hidden" name="filename" value="$fn" />
                    351:           <input type="hidden" name="phase" value="three" />
1.5       raeburn   352:        </td>
                    353:       </tr>
                    354:       <tr>
                    355:        <td colspan='2'>&nbsp;</td>
                    356:       </tr>
                    357:       <tr>
                    358:        <td colspan='2'>
                    359:         <table border='0' cellspacing='0' cellpadding='0' width="100%">
                    360:          <tr>
                    361:           <td align='left'>
1.15      albertel  362:            <input type='button' name='exitpage' value='Exit now' onClick="javascript:location.href='$fullpath'" />
1.5       raeburn   363:           </td>
                    364:           <td align='right'>
1.15      albertel  365:            <input type="button" name="nextpage" value="Proceed" onClick="javascript:nextPage()" />
1.5       raeburn   366:           </td>
                    367:          </tr>
                    368:         </table>
                    369:        </td>
                    370:       </tr>
                    371:      </table>
                    372:     </td>
                    373:    </tr>
                    374:   </table>
                    375: </form>
                    376: END_OF_ONE
                    377: }
                    378: 
                    379: # ---------------------------------------------------------------- Display Two
                    380: sub display_two {
                    381:     my ($r,$zipupload,$areas,$areaname,$cmsmap,$uname,$newdir,$numcrs,$fullpath) = @_;
                    382:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['folder','source']);
1.11      albertel  383:     my $cms = $env{'form.source'};
                    384:     my $dirname = $env{'form.newdir'};
1.5       raeburn   385:     my $tempdir = &Apache::imsprocessor::create_tempdir('CSTR',$dirname,'');
                    386:     my $fname = &Apache::imsprocessor::uploadzip('CSTR',$tempdir,$zipupload);
                    387:     my $unzip_result = '';
                    388:     my $manifest_result = '';
                    389:     unless ($tempdir eq '') {
                    390:         $unzip_result = &Apache::imsprocessor::expand_zip($tempdir,$fname);
                    391:     }
                    392:     my %resources = ();
1.9       raeburn   393:     my %includedres = ();
                    394:     my %includeditems = ();
1.5       raeburn   395:     my %items = ();
                    396:     my %hrefs = ();
                    397:     my %resinfo = ();
                    398:     my %count = ();
                    399:     my @bgcolors = ("#eeeeee","#dddddd");
                    400: 
                    401:     my $counter = 0;
                    402:     my $iter = 0;
                    403:     my %count = (
                    404:                 announce => 0,
                    405:                 board => 0,
                    406:                 doc => 0,
                    407:                 extlink => 0,
                    408:                 msg => 0,
                    409:                 pool => 0,
                    410:                 quiz => 0,
                    411:                 staff => 0,
                    412:                 survey => 0,
                    413:                 users => 0,
                    414:                 );
                    415:     my $conditions;
                    416: 
                    417:     if ($unzip_result eq 'ok') {
1.19      raeburn   418:         $manifest_result = &Apache::imsprocessor::process_manifest($cms,
                    419:                             $tempdir,\%resources,\%items,\%hrefs,\%resinfo,
                    420:                             'choose',\%includedres,\%includeditems);
1.5       raeburn   421:         if ($manifest_result eq 'ok') {
                    422:             foreach my $res (sort keys %resources) {
1.19      raeburn   423:                 if ($cms eq 'bb5' || $cms eq 'bb6' || $cms eq 'webctce4' 
                    424:                     || $cms eq 'webctvista4') {
1.5       raeburn   425:                     foreach my $area (keys %{$$cmsmap{$cms}}) {
                    426:                         if ($resources{$res}{type} eq $$cmsmap{$cms}{$area}) {
                    427:                             $count{$area} ++;
                    428:                         }
                    429:                     }
                    430:                 } elsif ($cms eq 'angel') {
                    431:                     foreach my $area (keys %{$$cmsmap{$cms}}) {
                    432:                         if ($area eq 'doc') {
                    433:                             if (grep/^$resources{$res}{type}$/,@{$$cmsmap{$cms}{doc}}) {
                    434:                                 $count{$area} ++;
                    435:                             }
                    436:                         } elsif ($resources{$res}{type} eq $$cmsmap{$cms}{$area}) {
                    437:                             $count{$area} ++;                                
                    438:                         }
                    439:                     }
                    440:                 }
                    441:             }
                    442:             if ($count{board} > 0) {
                    443:                 if ($count{users} > 0) {
                    444:                     $conditions = 'both';
                    445:                 } else {
                    446:                     $conditions = 'board';
                    447:                 }
                    448:             } elsif ($count{users} > 0) {
                    449:                 $conditions = 'users';
                    450:             } else {
                    451:                 $conditions = 'none';
                    452:             }
                    453: 
                    454:             $r->print(<<ENDBLOCK);
                    455: <form name="pickoptions" method="post">
                    456:   <table border='0' cellspacing='0' cellpadding ='0' width='100%'>
                    457:    <tr>
                    458:     <td colspan='2'>
                    459:      <table border='0' cellspacing='0' cellpadding='0'>
                    460:       <tr>
                    461:        <td colspan='2'  align='left'>&nbsp;
                    462:        </td>
                    463:       </tr>
1.1       raeburn   464:       <tr bgcolor='#ccddaa'>
1.2       raeburn   465:        <td valign='middle'><img src='/res/adm/pages/bl_step3.gif'>
1.1       raeburn   466:        </td>
                    467:        <td width='100%' align='left'>&nbsp;&nbsp;
1.5       raeburn   468:         <font face='arial,helvetica,sans-serif'><b>Choose which content types you wish to import</b></font>
1.1       raeburn   469:        </td>
                    470:       </tr>
                    471:       <tr>
                    472:        <td colspan='2'>&nbsp;</td>
                    473:       </tr>
                    474:       <tr>
                    475:        <td>&nbsp;</td>
                    476:        <td>
1.2       raeburn   477:         <table border='0' cellspacing='0' cellpadding='1' bgcolor='#000000'>
                    478:          <tr>
                    479:           <td>
                    480:            <table border='0' cellspacing='0' cellpadding='0' bgcolor='#ffffff' width='100%'>
                    481:             <tr>
                    482:              <td>
1.5       raeburn   483:               <table border='0' cellspacing='1' cellpadding='1' bgcolor='#ffffff' width='100%'>
1.2       raeburn   484:                <tr bgcolor='#ccddaa'>
1.5       raeburn   485:                 <td align='center'><font face='arial,helvetica,sans-serif'><b>Import?</b></font></td>           
                    486:                 <td align='center'><font face='arial,helvetica,sans-serif'><b>Content type</b></font></td>
                    487:                 <td align='center'><font face='arial,helvetica,sans-serif'><b>Additional options</b></font></td>
1.2       raeburn   488:                </tr>
1.5       raeburn   489: ENDBLOCK
                    490:             foreach my $area (@{$areas}) {
                    491:                 if ($count{$area} > 0) {
                    492:                     my $count_tag = 'flag_'.$counter;
                    493:                     $r->print("               <tr bgcolor='@bgcolors[$iter]'>
                    494:                 <td align='left'><font face='arial,helvetica,sans-serif'><input name='$area' type='checkbox' ");
                    495:                     if ($area eq 'board' || $area eq 'users') {
                    496:                         $r->print(qq|onClick='javascript:setOptions("$area","$counter")'|);
                    497:                     }
                    498:                     $r->print("/></font></td>
                    499:                 <td align='left'><font face='arial,helvetica,sans-serif'>&nbsp;&nbsp;$$areaname{$area}&nbsp;&nbsp; - $count{$area} item(s)</font></td>");
                    500:                     if ($area eq 'board') {
                    501:                         $r->print("            <td align='left'><font face='arial,helvetica,sans-serif'>&nbsp;&nbsp;
                    502:                  <select name='db_handling'>
1.15      albertel  503:                   <option value='-2'>&lt;-- Check Import first</option>
1.5       raeburn   504:                  </select></font>
                    505:                 </td>");
                    506:                     } elsif ($area eq 'users') {
                    507:                         $r->print("            <td align='left'><font face='arial,helvetica,sans-serif'>&nbsp;&nbsp;
                    508:                  <select name='user_handling'>
1.15      albertel  509:                   <option value='-2'>&lt;-- Check Import first</option>
1.2       raeburn   510:                  </select>
1.5       raeburn   511:                  </font>        
                    512:                 </td>");
                    513:                     } else {
                    514:                         $r->print("            <td align='left'><font face='arial,helvetica,sans-serif'>&nbsp;&nbsp;None<input type='hidden' name='$count_tag' /></font></td>");
                    515:                     }
                    516:                     $counter ++;
                    517:                     $iter = $counter%2;
                    518:                 }
                    519:             }
                    520:             $r->print(<<ENDBLOCKTWO);
1.2       raeburn   521:                </tr>
                    522:               </table>
                    523:              </td>
                    524:             </tr>
                    525:            </table>
                    526:           </td>
                    527:          </tr>
                    528:         </table>
1.1       raeburn   529:        </td>
                    530:       </tr>
                    531:       <tr>
1.5       raeburn   532:        <td colspan='2'  align='left'>&nbsp;
                    533:        </td>
                    534:       </tr>
                    535: ENDBLOCKTWO
                    536:             if ($count{board} + $count{users} > 0) {
                    537:                 $r->print("
                    538:       <tr bgcolor='#ccddaa'>
                    539:        <td valign='middle'><img src='/res/adm/pages/bl_step4.gif'>
                    540:        </td>
                    541:        <td width='100%' align='left'>&nbsp;&nbsp;
                    542:         <font face='arial,helvetica,sans-serif'><b>Choose a course to receive bulletin boards and user enrollment.</b></font>
                    543:        </td>
                    544:       </tr>
                    545:       <tr>
                    546:        <td colspan='2'>&nbsp;</td>
                    547:       </tr>
                    548:                 ");
                    549:                 if ($$numcrs > 0) {
                    550:                     $r->print("      <tr>
                    551:        <td>&nbsp;</td>
                    552:        <td><font face='arial,helvetica,sans-serif'>A listing of possible course targets will be displayed if import of bulletin boards and/or enrollment is checked above (step 3). If you do not plan to import either of these content types, there is no need to specify a course.
                    553: <br /><br />
                    554: Choose course:&nbsp;&nbsp;
                    555:                  <select name='targetcourse'>
1.15      albertel  556:                   <option value='-1'>Not required</option>
1.5       raeburn   557:                  </select></font>
                    558:        </td>
                    559:       </tr>
                    560: ");
                    561:                 } else {
                    562:                     $r->print("       <tr>
                    563: <td>&nbsp;</td>
                    564: <td><font face='arial,helvetica,sans-serif'>You do not have active course coordinator status in any LON-CAPA courses currently, so bulletin boards and enrollment information included in your IMS package will be discarded, regardless of your import choice for these two items above (step 3). If you wish to import bulletin boards and/or user information into LON-CAPA please click 'Exit now' to quit the current IMS import process, and contact your domain coordinator and request a course coordinator role in a LON-CAPA course into which you can upload bulletin boards and/or enroll users.</font>
                    565:         ");
                    566:                 }
                    567:             }
                    568:             $r->print(<<ENDBLOCK);
                    569:       <tr>
1.2       raeburn   570:        <td colspan='2'>&nbsp;<br /><br /></td>
1.1       raeburn   571:       </tr>
                    572:       <tr>
                    573:        <td>&nbsp;</td>
1.5       raeburn   574:        <td><font face='arial,helvetica,sans-serif'>Once you have checked the checkboxes for all areas you wish to import from the IMS package, and selected additional options (if available) you should click the 'Import package' button.</font></td>
1.1       raeburn   575:       </tr>
                    576:       <tr>
1.5       raeburn   577:        <td colspan='2'>&nbsp;
1.11      albertel  578:           <input type="hidden" name="newdir" value="$env{'form.newdir'}" />
1.5       raeburn   579:           <input type="hidden" name="conditions" value="$conditions" />
                    580:           <input type="hidden" name="source" value="$cms" />
                    581:           <input type="hidden" name="tempdir" value="$tempdir" />
1.1       raeburn   582:           <input type="hidden" name="uploaduname" value="$uname">
1.5       raeburn   583:           <input type="hidden" name="filename" value="$fname">
                    584:           <input type="hidden" name="phase" value="four" />
                    585: ENDBLOCK
                    586:             if ($count{board} == 0) {
                    587:                 $r->print('            <input type="hidden" name="board" value="" />'."\n");
                    588:             }
                    589:             if ($count{users} == 0) {
                    590:                 $r->print('            <input type="hidden" name="users" value="" />'."\n");
                    591:             }
                    592:             $r->print(<<ENDDOCUMENT);
1.1       raeburn   593:        </td>
                    594:       </tr>
                    595:       <tr>
                    596:        <td colspan='2'>
                    597:         <table border='0' cellspacing='0' cellpadding='0' width="100%">
                    598:          <tr>
1.2       raeburn   599:           <td align='left'>
1.15      albertel  600:            <input type='button' name='exitpage' value='Exit now' onClick="javascript:location.href='$fullpath'" />
1.1       raeburn   601:           </td>
                    602:           <td align='right'>
1.15      albertel  603:            <input type="button" name="nextpage" value="Import package" onClick="javascript:nextPage($counter)" />
1.1       raeburn   604:           </td>
                    605:          </tr>
                    606:         </table>
                    607:        </td>
                    608:       </tr>
                    609:      </table>
                    610:     </td>
                    611:    </tr>
                    612:   </table>
1.5       raeburn   613: ENDDOCUMENT
                    614:         } else {
                    615:             $r->print("Unpacking of your IMS package failed because an IMS manifest file was not located in the package\n");
1.2       raeburn   616:         }
1.4       raeburn   617:     } else {
1.5       raeburn   618:         $r->print("Processing of your IMS package failed because the file you uploaded could not be unzipped\n");
                    619:     }
                    620: }
1.1       raeburn   621: 
1.5       raeburn   622: # ---------------------------------------------------------------- Display Three
                    623: sub display_three {
                    624:     my ($r,$uname,$udom,$areas,$areaname,$cmsmap,$destdir,$newdir) = @_;
                    625:     my $crs = '';
                    626:     my $cdom = '';
                    627:     my $db_handling = '';
                    628:     my $timenow = time; 
                    629:     my $announce_handling = 'ok';
1.11      albertel  630:     my $cms = $env{'form.source'};
                    631:     if ( defined($env{'form.bb_crs'}) ) {
                    632:         ($cdom,$crs) = split/\//,$env{'form.bb_crs'};
1.4       raeburn   633:     } 
1.5       raeburn   634:     my $user_crs = '';
                    635:     my $user_cdom = '';
                    636:     my $user_handling = '';
1.11      albertel  637:     if ( defined($env{'form.user_crs'}) ) {
                    638:         ($user_cdom,$user_crs) = split/\//,$env{'form.user_crs'};
1.5       raeburn   639:     }
                    640:     my $seqstem = "/res/$udom/$uname/$newdir";
1.9       raeburn   641:     my %importareas = ();
                    642:     my %includedres = ();
                    643:     my %includeditems = ();
1.10      raeburn   644:     my %randompicks = ();
1.5       raeburn   645:     my @targets = ();
                    646:     my %resources = ();
                    647:     my %items = ();
                    648:     my %hrefs = ();
                    649:     my %urls = ();
                    650:     my %resinfo = ();
                    651:     my %total = (
                    652:                    page => 0,
                    653:                    prob => 0,
                    654:                    seq => 0,
                    655:                    board => 0,         
                    656:                    quiz => 0,
                    657:                    surv => 0,
                    658:     );
                    659: 
                    660:     my @pages = ();
                    661:     my @sequences = ();
                    662:     my @resrcfiles = ();
1.19      raeburn   663:     my @assessmentfiles = ();
1.5       raeburn   664: 
1.11      albertel  665:     my $tempdir = $env{'form.tempdir'};
1.5       raeburn   666: 
                    667:     foreach my $area (@{$areas}) {
1.11      albertel  668:         if (defined($env{"form.$area"}) ) {
1.5       raeburn   669:             if ($cms eq 'angel' && $area eq 'doc') {
                    670:                 foreach (@{$$cmsmap{$cms}{$area}}) {
1.9       raeburn   671:                     $importareas{$_} = 1;
1.1       raeburn   672:                 }
1.5       raeburn   673:             } else {
1.9       raeburn   674:                 $importareas{$$cmsmap{$cms}{$area}} = 1;
1.2       raeburn   675:             }
1.5       raeburn   676:             if ($area eq 'board') {
1.11      albertel  677:                 $db_handling = $env{'form.db_handling'};
1.5       raeburn   678:             } elsif ($area eq 'users') {
1.11      albertel  679:                 $user_handling = $env{'form.user_handling'};
1.4       raeburn   680:             }
1.1       raeburn   681:         }
                    682:     }
1.9       raeburn   683: 
1.19      raeburn   684:     my $manifest_result = &Apache::imsprocessor::process_manifest($cms,$tempdir,
                    685:                           \%resources,\%items,\%hrefs,\%resinfo,'prepare',
                    686:                           \%includedres);
1.9       raeburn   687:     if ($manifest_result eq 'ok') {
                    688:         foreach my $res (sort keys %resources) {
                    689:             if ($importareas{$resources{$res}{type}}) {
                    690:                 $includedres{$res} = 1;
1.19      raeburn   691:                 if ($resources{$res}{type} eq 'webct.manifest' || 
                    692:                      $resources{$res}{type} eq 'webct.assessment' ||
                    693:                      $resources{$res}{type} eq 'webct.question') {
                    694:                     push(@assessmentfiles,$res);
                    695:                 }   
1.9       raeburn   696:             }
                    697:         }
                    698:         foreach my $itm (sort keys %items) {
                    699:             &Apache::imsprocessor::get_imports(\%includeditems,\%items,\%resources,\%importareas,$itm);
                    700:         }
                    701:     }
                    702: 
                    703:     foreach my $itm (sort keys %includeditems) {
                    704:         &Apache::imsprocessor::get_parents(\%includeditems,\%items,$itm);
                    705:     }
                    706: 
1.19      raeburn   707:     $manifest_result = &Apache::imsprocessor::process_manifest($cms,$tempdir,
                    708:                        \%resources,\%items,\%hrefs,\%resinfo,'build',
                    709:                        \%includedres,\%includeditems);
1.5       raeburn   710:     if ($manifest_result eq 'ok') {
1.9       raeburn   711:         &Apache::imsprocessor::target_resources(\%resources,\%importareas,\@targets);
1.5       raeburn   712: 
                    713:         my @boards = ();
                    714:         my @announcements = ();
                    715:         my @quizzes = ();
                    716:         my @surveys = ();
1.9       raeburn   717:         my @pools = ();
1.5       raeburn   718:         my @groups = ();
                    719:         my %messages = ();
                    720:         my @timestamp = ();
                    721:         my %boardnum = ();
                    722:         my @topurls = ();
                    723:         my @topnames = ();
1.7       raeburn   724:         my @packages = ();
1.5       raeburn   725: 
1.10      raeburn   726:         &Apache::imsprocessor::process_resinfo($cms,'CSTR',$tempdir,$destdir,\%items,\%resources,\@targets,\@boards,\@announcements,\@quizzes,\@surveys,\@pools,\@groups,\%messages,\@timestamp,\%boardnum,\%resinfo,$udom,$uname,$cdom,$crs,$db_handling,$user_handling,\%total,$seqstem,$seqstem,\@resrcfiles,\@packages,\%hrefs,\@pages,\@sequences,\%randompicks);
1.7       raeburn   727: 
1.19      raeburn   728:         my $copy_result = &Apache::imsprocessor::copy_resources('CSTR',$cms,\%hrefs,$tempdir,\@targets,\%urls,$crs,$cdom,$destdir,$timenow,\%importareas,\@assessmentfiles);
1.5       raeburn   729:    
1.10      raeburn   730:         &Apache::imsprocessor::build_structure($cms,'CSTR',$destdir,\%items,\%resinfo,\%resources,\@targets,\%hrefs,$udom,$uname,$newdir,$timenow,$cdom,$crs,\@timestamp,\%total,\@boards,\@announcements,\@quizzes,\@surveys,\@pools,\%boardnum,\@pages,\@sequences,\@topurls,\@topnames,\@packages,\%includeditems,\%randompicks);
1.5       raeburn   731: 
1.9       raeburn   732:         $r->print("<h3>IMS import completed</h3>");
1.5       raeburn   733: 
                    734:         if ($cms eq 'bb5') {
                    735:             $r->print("<font face='arial,helvetica,sans-serif'>Your IMS package has been processed successfully. A total of $total{seq} sequences, $total{page} pages, $total{board} bulletin boards, $total{quiz} quizzes, $total{surv} surveys and $total{prob} problems have been created.<br /><br />\n");
                    736:         } elsif ($cms eq 'angel') {
                    737:             $r->print("<font face='arial,helvetica,sans-serif'>Your IMS package has been processed successfully. A total of $total{seq} sequences, $total{page} pages, and $total{board} bulletin boards have been created.<br /><br />\n");
                    738:         }
1.10      raeburn   739:        $r->print("Please view the imported items and use the LON-CAPA editing tools to make changes.  The sequences directory contains a file named 'Top.sequence' which includes links to the items found at the top level of your IMS package. From there you can follow links to display all the imported items. Alternatively, you can browse the pages, sequences, problems and resfiles directories directly. Note if you rename a file, you will need to modify any .sequence files or .page files which include a reference to the renamed file.<br /><br />The final step in the IMS import process is to publish the materials you have imported into your Construction Space so that you can use them in a course. Once your file are published, subsequent re-publication will result in the storage of information about changes between the different versions.<br /><br /><a href='/priv/".$uname."/".$newdir."'>Display new directory</a></font>"); 
1.9       raeburn   740:         if ($destdir =~ m-^/home/$uname/public_html/-) { 
                    741:             system (" rm -r -f $destdir/temp");
                    742:         }
1.5       raeburn   743:     } elsif ($manifest_result eq 'nomanifest') {
1.9       raeburn   744:         $r->print("<font face='arial,helvetica,sans-serif'>Processing of your IMS package failed, because the IMS content package did not contain an IMS manifest file.</font>");
1.4       raeburn   745:     }
1.5       raeburn   746: }
                    747: 
                    748: # ---------------------------------------------------------------- Get LON-CAPA Course Coordinator roles for this user
                    749: sub get_ccroles {
                    750:     my ($user,$dom,$crsentry,$crslist) = @_;
                    751:     my %roles = ();
                    752:     unless ($user eq '') {
                    753:         %roles = &Apache::lonnet::dump('roles',$dom,$user);
1.4       raeburn   754:     }
1.5       raeburn   755:     my $iter = 0;
                    756:     my @codes = ();
                    757:     my %courses = ();
                    758:     my @crslist = ();
                    759:     my %descrip =();
                    760:     foreach my $key (keys %roles ) {
1.21    ! albertel  761:         if ($key =~ m{^/($LONCAPA::domain_re)/($LONCAPA::username_re)_cc$}) {
1.5       raeburn   762:             my $cdom = $1;
                    763:             my $crs = $2;
                    764:             my $role_end = 0;
                    765:             my $role_start = 0;
                    766:             my $active_chk = 1;
                    767:             if ( $roles{$key} =~ m/^cc_(\d+)/ ) {
                    768:                 $role_end = $1;
                    769:                 if ( $roles{$key} =~ m/^cc_($role_end)_(\d+)$/ )
                    770:                 {
                    771:                     $role_start = $2;
                    772:                 }
                    773:             }
                    774:             if ($role_start > 0) {
                    775:                 if (time < $role_start) {
                    776:                     $active_chk = 0;
1.1       raeburn   777:                 }
1.5       raeburn   778:             }
                    779:             if ($role_end > 0) {
                    780:                 if (time > $role_end) {
                    781:                     $active_chk = 0;
1.1       raeburn   782:                 }
                    783:             }
1.5       raeburn   784:             if ($active_chk) {
                    785:                 my $currcode = '';
                    786:                 my %settings = &Apache::lonnet::get('environment',['internal.coursecode','description'],$cdom,$crs);
                    787:                 if (defined($settings{'description'}) ) {
                    788:                     $descrip{$crs} = $settings{'description'};
1.1       raeburn   789:                 } else {
1.5       raeburn   790:                     $descrip{$crs} = 'Unknown';
1.1       raeburn   791:                 }
1.5       raeburn   792:                 if (defined($settings{'internal.coursecode'}) ) {
                    793:                     $currcode = $settings{'internal.coursecode'};
                    794:                     if ($currcode eq '') {
                    795:                         $currcode = "____".$iter;
                    796:                         $iter ++;
1.1       raeburn   797:                     }
                    798:                 } else {
1.5       raeburn   799:                     $currcode = "____".$iter;
                    800:                     $iter ++;
                    801:                 }
                    802:                 unless (grep/^$currcode$/,@codes) {
                    803:                     push @codes,$currcode;
                    804:                     @{$courses{$currcode}} = ();
1.1       raeburn   805:                 }
1.5       raeburn   806:                 push @{$courses{$currcode}}, $cdom.'/'.$crs;
1.1       raeburn   807:             }
1.5       raeburn   808:         }
                    809:     }
                    810:     foreach my $code (sort @codes) {
                    811:         foreach my $crsdom (@{$courses{$code}}) {
1.2       raeburn   812:             my ($cdom,$crs) = split/\//,$crsdom;
                    813:             my $showcode = '';
                    814:             unless ($code =~m/^____\d+$/) {  $showcode = $code; }
                    815:             $$crsentry{$crsdom} = $showcode.':'.$descrip{$crs};
1.5       raeburn   816:             push @{$crslist}, $crsdom;
1.2       raeburn   817:         }
                    818:     }
1.5       raeburn   819:     return;
1.2       raeburn   820: }
1.1       raeburn   821: 
                    822: # ---------------------------------------------------------------- Main Handler
                    823: sub handler {
                    824:     my $r=shift;
                    825:     my $uname;
                    826:     my $udom;
                    827:     my $javascript = '';
                    828:     my $page_name = '';
                    829:     my $current_page = '';
                    830:     my $qcount = '';
1.5       raeburn   831: 
                    832: # get personal information for this user
1.11      albertel  833:     my $user=$env{'user.name'};
                    834:     my $dom=$env{'user.domain'};
1.5       raeburn   835: 
1.1       raeburn   836: #
1.5       raeburn   837: # re-attach user
1.1       raeburn   838: #
1.11      albertel  839:     if ($env{'form.uploaduname'}) {
                    840:         $env{'form.filename'}='/priv/'.$env{'form.uploaduname'}.'/'.
                    841:             $env{'form.filename'};
1.1       raeburn   842:     }
                    843:     ($uname,$udom)=
1.11      albertel  844:         &Apache::loncacc::constructaccess($env{'form.filename'},
1.1       raeburn   845:                                           $r->dir_config('lonDefDomain'));
                    846:     unless (($uname) && ($udom)) {
                    847:         $r->log_reason($uname.' at '.$udom.
1.11      albertel  848:                        ' trying to publish file '.$env{'form.filename'}.
1.1       raeburn   849:                        ' - not authorized',
                    850:                        $r->filename);
                    851:         return HTTP_NOT_ACCEPTABLE;
                    852:     }
                    853:                                                                                              
                    854:     my $fn;
1.11      albertel  855:     if ($env{'form.filename'}) {
                    856:         $fn=$env{'form.filename'};
1.1       raeburn   857:         $fn=~s/^http\:\/\/[^\/]+\///;
                    858:         $fn=~s/^\///;
                    859:         $fn=~s/(\~|priv\/)(\w+)//;
                    860:         $fn=~s/\/+/\//g;
                    861:     } else {
1.11      albertel  862:         $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.1       raeburn   863:                        ' unspecified filename for upload', $r->filename);
                    864:         return HTTP_NOT_FOUND;
                    865:     }
1.5       raeburn   866:     my $zipupload = '/home/'.$uname.'/public_html'.$fn;
1.1       raeburn   867:     my $pathname = &File::Basename::dirname($fn);
                    868:     my $fullpath = '/priv/'.$uname.$pathname;
                    869:     unless ($pathname eq '/') {
                    870:         $fullpath .= '/';
                    871:     }
1.5       raeburn   872: 
                    873:     my @areas = ();
                    874:     my %cmsmap = ();
                    875:     my %areaname = ();
                    876:     my $numcrs = 0;
                    877:              
                    878:     &Apache::imsprocessor::ims_config(\@areas,\%cmsmap,\%areaname);
1.1       raeburn   879: # ----------------------------------------------------------- Start page output
                    880:     &Apache::loncommon::content_type($r,'text/html');
                    881:     $r->send_http_header;
1.5       raeburn   882: 
1.11      albertel  883:     if ($env{'form.phase'} eq 'two') {
1.5       raeburn   884:         &jscript_one($fullpath,\$javascript,$uname,$udom);
1.11      albertel  885:     } elsif ($env{'form.phase'} eq 'three') {
1.5       raeburn   886:         &jscript_two(\$javascript,$user,$dom,\$numcrs);
1.11      albertel  887:     } elsif ($env{'form.phase'} eq 'four') {
1.5       raeburn   888:         &jscript_three(\$javascript);
1.1       raeburn   889:     }
1.14      albertel  890:     $javascript = "<script type=\"text/javascript\">\n//<!--\n$javascript\n// --></script>\n";
                    891: 
                    892:     my $title = 'Upload IMS package to Construction Space';
1.20      albertel  893:     $r->print(&Apache::loncommon::start_page($title, $javascript));
1.14      albertel  894: 
1.11      albertel  895:     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.1       raeburn   896:         $r->print('<h3><font color=red>'.&mt('Co-Author').': '.$uname.
                    897:                   &mt(' at ').$udom.'</font></h3>');
1.5       raeburn   898:     }   
1.11      albertel  899:     if ($env{'form.phase'} eq 'two') {
1.1       raeburn   900:         my $flag = &Apache::lonupload::phasetwo($r,$fn,$uname,$udom,'imsimport');
                    901:         if ($flag eq 'ok') {
1.5       raeburn   902:             &display_one($r,$uname,$fn,$fullpath);
                    903:         }
1.11      albertel  904:     } elsif ( ($env{'form.phase'} eq 'three') || ($env{'form.phase'} eq 'four') ) {
                    905:         my $docroot = $env{'form.newdir'};
1.5       raeburn   906:         my $newdir = '';
                    907:         if ($docroot =~ m|public_html/(.+)$|) {
                    908:             $newdir = $1;
                    909:         }
1.11      albertel  910:         if ($env{'form.phase'} eq 'three') {
1.5       raeburn   911:             &display_two ($r,$zipupload,\@areas,\%areaname,\%cmsmap,$uname,$newdir,\$numcrs,$fullpath);
1.11      albertel  912:         } elsif ($env{'form.phase'} eq 'four') {
1.5       raeburn   913:             &display_three ($r,$uname,$udom,\@areas,\%areaname,\%cmsmap,$docroot,$newdir);
1.1       raeburn   914:         }
                    915:     } else {
                    916:         &Apache::lonupload::phaseone($r,$fn,$uname,$udom,'imsimport');
                    917:     }
1.14      albertel  918:     $r->print(&Apache::loncommon::end_page());
1.1       raeburn   919:     return OK;
                    920: }
                    921: 1;
                    922: __END__

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