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

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

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