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

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

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