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

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.10    ! raeburn   296:          <option value='bb5'>Blackboard 5
1.7       raeburn   297:          <option value='bb6'>Blackboard 6
1.2       raeburn   298:          <option value='angel'>ANGEL
1.10    ! raeburn   299:          <option value='webct4'>WebCT 4
1.2       raeburn   300:         </select>
                    301:         </font>
1.1       raeburn   302:        </td>
                    303:       </tr>
                    304:       <tr>
                    305:        <td colspan='2'>&nbsp;</td>
                    306:       </tr>
                    307:       <tr>
1.2       raeburn   308:        <td colspan='2'>&nbsp;</td>
                    309:       </tr>
                    310:       <tr bgcolor='#ccddaa'>
                    311:        <td valign='middle'><img src='/res/adm/pages/bl_step2.gif'>
                    312:        </td>
                    313:        <td width='100%' align='left'>&nbsp;&nbsp;
                    314:         <font face='arial,helvetica,sans-serif'><b>Create a directory where you will unpack your IMS package.</b>&nbsp;&nbsp;</font></td>
                    315:       </tr>
1.1       raeburn   316:       <tr>
1.2       raeburn   317:        <td colspan='2'>&nbsp;</td>
                    318:       </tr>
1.1       raeburn   319:        <td>&nbsp;</td>
                    320:        <td>
                    321:         <font face='Arial,Helvetica,sans-serif'>
1.2       raeburn   322: 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   323:        </td>
                    324:       </tr>
                    325:       <tr>
1.2       raeburn   326:        <td colspan='2'>&nbsp;<br /><br /></td>
1.1       raeburn   327:       </tr>
1.5       raeburn   328:       <tr>
                    329:        <td>&nbsp;</td>
                    330:        <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>
                    331:       </tr>
                    332:       <tr>
                    333:        <td colspan='2'>
                    334:           <input type="hidden" name="uploaduname" value="$uname">
                    335:           <input type="hidden" name="filename" value="$fn">
                    336:           <input type="hidden" name="phase" value="three">
                    337:        </td>
                    338:       </tr>
                    339:       <tr>
                    340:        <td colspan='2'>&nbsp;</td>
                    341:       </tr>
                    342:       <tr>
                    343:        <td colspan='2'>
                    344:         <table border='0' cellspacing='0' cellpadding='0' width="100%">
                    345:          <tr>
                    346:           <td align='left'>
                    347:            <input type='button' name='exitpage' value='Exit now' onClick="javascript:location.href='$fullpath'">
                    348:           </td>
                    349:           <td align='right'>
                    350:            <input type="button" name="nextpage" value="Proceed" onClick="javascript:nextPage()">
                    351:           </td>
                    352:          </tr>
                    353:         </table>
                    354:        </td>
                    355:       </tr>
                    356:      </table>
                    357:     </td>
                    358:    </tr>
                    359:   </table>
                    360: </form>
                    361: END_OF_ONE
                    362: }
                    363: 
                    364: # ---------------------------------------------------------------- Display Two
                    365: sub display_two {
                    366:     my ($r,$zipupload,$areas,$areaname,$cmsmap,$uname,$newdir,$numcrs,$fullpath) = @_;
                    367:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['folder','source']);
                    368:     my $cms = $ENV{'form.source'};
                    369:     my $dirname = $ENV{'form.newdir'};
                    370:     my $tempdir = &Apache::imsprocessor::create_tempdir('CSTR',$dirname,'');
                    371:     my $fname = &Apache::imsprocessor::uploadzip('CSTR',$tempdir,$zipupload);
                    372:     my $unzip_result = '';
                    373:     my $manifest_result = '';
                    374:     unless ($tempdir eq '') {
                    375:         $unzip_result = &Apache::imsprocessor::expand_zip($tempdir,$fname);
                    376:     }
                    377:     my %resources = ();
1.9       raeburn   378:     my %includedres = ();
                    379:     my %includeditems = ();
1.5       raeburn   380:     my %items = ();
                    381:     my %hrefs = ();
                    382:     my %resinfo = ();
                    383:     my %count = ();
                    384:     my @bgcolors = ("#eeeeee","#dddddd");
                    385: 
                    386:     my $counter = 0;
                    387:     my $iter = 0;
                    388:     my %count = (
                    389:                 announce => 0,
                    390:                 board => 0,
                    391:                 doc => 0,
                    392:                 extlink => 0,
                    393:                 msg => 0,
                    394:                 pool => 0,
                    395:                 quiz => 0,
                    396:                 staff => 0,
                    397:                 survey => 0,
                    398:                 users => 0,
                    399:                 );
                    400:     my $conditions;
                    401: 
                    402:     if ($unzip_result eq 'ok') {
1.9       raeburn   403:         $manifest_result = &Apache::imsprocessor::process_manifest($cms,$tempdir,\%resources,\%items,\%hrefs,\%resinfo,'choose',\%includedres,\%includeditems);
1.5       raeburn   404:         if ($manifest_result eq 'ok') {
                    405:             foreach my $res (sort keys %resources) {
1.10    ! raeburn   406:                 if ($cms eq 'bb5' || $cms eq 'bb6' || $cms eq 'webct4') {
1.5       raeburn   407:                     foreach my $area (keys %{$$cmsmap{$cms}}) {
                    408:                         if ($resources{$res}{type} eq $$cmsmap{$cms}{$area}) {
                    409:                             $count{$area} ++;
                    410:                         }
                    411:                     }
                    412:                 } elsif ($cms eq 'angel') {
                    413:                     foreach my $area (keys %{$$cmsmap{$cms}}) {
                    414:                         if ($area eq 'doc') {
                    415:                             if (grep/^$resources{$res}{type}$/,@{$$cmsmap{$cms}{doc}}) {
                    416:                                 $count{$area} ++;
                    417:                             }
                    418:                         } elsif ($resources{$res}{type} eq $$cmsmap{$cms}{$area}) {
                    419:                             $count{$area} ++;                                
                    420:                         }
                    421:                     }
                    422:                 }
                    423:             }
                    424:             if ($count{board} > 0) {
                    425:                 if ($count{users} > 0) {
                    426:                     $conditions = 'both';
                    427:                 } else {
                    428:                     $conditions = 'board';
                    429:                 }
                    430:             } elsif ($count{users} > 0) {
                    431:                 $conditions = 'users';
                    432:             } else {
                    433:                 $conditions = 'none';
                    434:             }
                    435: 
                    436:             $r->print(<<ENDBLOCK);
                    437: <form name="pickoptions" method="post">
                    438:   <table border='0' cellspacing='0' cellpadding ='0' width='100%'>
                    439:    <tr>
                    440:     <td colspan='2'>
                    441:      <table border='0' cellspacing='0' cellpadding='0'>
                    442:       <tr>
                    443:        <td colspan='2'  align='left'>&nbsp;
                    444:        </td>
                    445:       </tr>
1.1       raeburn   446:       <tr bgcolor='#ccddaa'>
1.2       raeburn   447:        <td valign='middle'><img src='/res/adm/pages/bl_step3.gif'>
1.1       raeburn   448:        </td>
                    449:        <td width='100%' align='left'>&nbsp;&nbsp;
1.5       raeburn   450:         <font face='arial,helvetica,sans-serif'><b>Choose which content types you wish to import</b></font>
1.1       raeburn   451:        </td>
                    452:       </tr>
                    453:       <tr>
                    454:        <td colspan='2'>&nbsp;</td>
                    455:       </tr>
                    456:       <tr>
                    457:        <td>&nbsp;</td>
                    458:        <td>
1.2       raeburn   459:         <table border='0' cellspacing='0' cellpadding='1' bgcolor='#000000'>
                    460:          <tr>
                    461:           <td>
                    462:            <table border='0' cellspacing='0' cellpadding='0' bgcolor='#ffffff' width='100%'>
                    463:             <tr>
                    464:              <td>
1.5       raeburn   465:               <table border='0' cellspacing='1' cellpadding='1' bgcolor='#ffffff' width='100%'>
1.2       raeburn   466:                <tr bgcolor='#ccddaa'>
1.5       raeburn   467:                 <td align='center'><font face='arial,helvetica,sans-serif'><b>Import?</b></font></td>           
                    468:                 <td align='center'><font face='arial,helvetica,sans-serif'><b>Content type</b></font></td>
                    469:                 <td align='center'><font face='arial,helvetica,sans-serif'><b>Additional options</b></font></td>
1.2       raeburn   470:                </tr>
1.5       raeburn   471: ENDBLOCK
                    472:             foreach my $area (@{$areas}) {
                    473:                 if ($count{$area} > 0) {
                    474:                     my $count_tag = 'flag_'.$counter;
                    475:                     $r->print("               <tr bgcolor='@bgcolors[$iter]'>
                    476:                 <td align='left'><font face='arial,helvetica,sans-serif'><input name='$area' type='checkbox' ");
                    477:                     if ($area eq 'board' || $area eq 'users') {
                    478:                         $r->print(qq|onClick='javascript:setOptions("$area","$counter")'|);
                    479:                     }
                    480:                     $r->print("/></font></td>
                    481:                 <td align='left'><font face='arial,helvetica,sans-serif'>&nbsp;&nbsp;$$areaname{$area}&nbsp;&nbsp; - $count{$area} item(s)</font></td>");
                    482:                     if ($area eq 'board') {
                    483:                         $r->print("            <td align='left'><font face='arial,helvetica,sans-serif'>&nbsp;&nbsp;
                    484:                  <select name='db_handling'>
                    485:                   <option value='-2'>&lt;-- Check Import first
                    486:                  </select></font>
                    487:                 </td>");
                    488:                     } elsif ($area eq 'users') {
                    489:                         $r->print("            <td align='left'><font face='arial,helvetica,sans-serif'>&nbsp;&nbsp;
                    490:                  <select name='user_handling'>
                    491:                   <option value='-2'>&lt;-- Check Import first
1.2       raeburn   492:                  </select>
1.5       raeburn   493:                  </font>        
                    494:                 </td>");
                    495:                     } else {
                    496:                         $r->print("            <td align='left'><font face='arial,helvetica,sans-serif'>&nbsp;&nbsp;None<input type='hidden' name='$count_tag' /></font></td>");
                    497:                     }
                    498:                     $counter ++;
                    499:                     $iter = $counter%2;
                    500:                 }
                    501:             }
                    502:             $r->print(<<ENDBLOCKTWO);
1.2       raeburn   503:                </tr>
                    504:               </table>
                    505:              </td>
                    506:             </tr>
                    507:            </table>
                    508:           </td>
                    509:          </tr>
                    510:         </table>
1.1       raeburn   511:        </td>
                    512:       </tr>
                    513:       <tr>
1.5       raeburn   514:        <td colspan='2'  align='left'>&nbsp;
                    515:        </td>
                    516:       </tr>
                    517: ENDBLOCKTWO
                    518:             if ($count{board} + $count{users} > 0) {
                    519:                 $r->print("
                    520:       <tr bgcolor='#ccddaa'>
                    521:        <td valign='middle'><img src='/res/adm/pages/bl_step4.gif'>
                    522:        </td>
                    523:        <td width='100%' align='left'>&nbsp;&nbsp;
                    524:         <font face='arial,helvetica,sans-serif'><b>Choose a course to receive bulletin boards and user enrollment.</b></font>
                    525:        </td>
                    526:       </tr>
                    527:       <tr>
                    528:        <td colspan='2'>&nbsp;</td>
                    529:       </tr>
                    530:                 ");
                    531:                 if ($$numcrs > 0) {
                    532:                     $r->print("      <tr>
                    533:        <td>&nbsp;</td>
                    534:        <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.
                    535: <br /><br />
                    536: Choose course:&nbsp;&nbsp;
                    537:                  <select name='targetcourse'>
                    538:                   <option value='-1'>Not required
                    539:                  </select></font>
                    540:        </td>
                    541:       </tr>
                    542: ");
                    543:                 } else {
                    544:                     $r->print("       <tr>
                    545: <td>&nbsp;</td>
                    546: <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>
                    547:         ");
                    548:                 }
                    549:             }
                    550:             $r->print(<<ENDBLOCK);
                    551:       <tr>
1.2       raeburn   552:        <td colspan='2'>&nbsp;<br /><br /></td>
1.1       raeburn   553:       </tr>
                    554:       <tr>
                    555:        <td>&nbsp;</td>
1.5       raeburn   556:        <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   557:       </tr>
                    558:       <tr>
1.5       raeburn   559:        <td colspan='2'>&nbsp;
                    560:           <input type="hidden" name="newdir" value="$ENV{'form.newdir'}" />
                    561:           <input type="hidden" name="conditions" value="$conditions" />
                    562:           <input type="hidden" name="source" value="$cms" />
                    563:           <input type="hidden" name="tempdir" value="$tempdir" />
1.1       raeburn   564:           <input type="hidden" name="uploaduname" value="$uname">
1.5       raeburn   565:           <input type="hidden" name="filename" value="$fname">
                    566:           <input type="hidden" name="phase" value="four" />
                    567: ENDBLOCK
                    568:             if ($count{board} == 0) {
                    569:                 $r->print('            <input type="hidden" name="board" value="" />'."\n");
                    570:             }
                    571:             if ($count{users} == 0) {
                    572:                 $r->print('            <input type="hidden" name="users" value="" />'."\n");
                    573:             }
                    574:             $r->print(<<ENDDOCUMENT);
1.1       raeburn   575:        </td>
                    576:       </tr>
                    577:       <tr>
                    578:        <td colspan='2'>
                    579:         <table border='0' cellspacing='0' cellpadding='0' width="100%">
                    580:          <tr>
1.2       raeburn   581:           <td align='left'>
                    582:            <input type='button' name='exitpage' value='Exit now' onClick="javascript:location.href='$fullpath'">
1.1       raeburn   583:           </td>
                    584:           <td align='right'>
1.5       raeburn   585:            <input type="button" name="nextpage" value="Import package" onClick="javascript:nextPage($counter)">
1.1       raeburn   586:           </td>
                    587:          </tr>
                    588:         </table>
                    589:        </td>
                    590:       </tr>
                    591:      </table>
                    592:     </td>
                    593:    </tr>
                    594:   </table>
1.5       raeburn   595: ENDDOCUMENT
                    596:         } else {
                    597:             $r->print("Unpacking of your IMS package failed because an IMS manifest file was not located in the package\n");
1.2       raeburn   598:         }
1.4       raeburn   599:     } else {
1.5       raeburn   600:         $r->print("Processing of your IMS package failed because the file you uploaded could not be unzipped\n");
                    601:     }
                    602: }
1.1       raeburn   603: 
1.5       raeburn   604: # ---------------------------------------------------------------- Display Three
                    605: sub display_three {
                    606:     my ($r,$uname,$udom,$areas,$areaname,$cmsmap,$destdir,$newdir) = @_;
                    607:     my $crs = '';
                    608:     my $cdom = '';
                    609:     my $chome = '';
                    610:     my $db_handling = '';
                    611:     my $timenow = time; 
                    612:     my $announce_handling = 'ok';
                    613:     my $cms = $ENV{'form.source'};
                    614:     if ( defined($ENV{'form.bb_crs'}) ) {
                    615:         ($cdom,$crs) = split/\//,$ENV{'form.bb_crs'};
                    616:         $chome = &Apache::lonnet::homeserver($crs,$cdom);
1.4       raeburn   617:     } 
1.5       raeburn   618:     my $user_crs = '';
                    619:     my $user_cdom = '';
                    620:     my $user_handling = '';
                    621:     if ( defined($ENV{'form.user_crs'}) ) {
                    622:         ($user_cdom,$user_crs) = split/\//,$ENV{'form.user_crs'};
                    623:     }
                    624:     my $seqstem = "/res/$udom/$uname/$newdir";
1.9       raeburn   625:     my %importareas = ();
                    626:     my %includedres = ();
                    627:     my %includeditems = ();
1.10    ! raeburn   628:     my %randompicks = ();
1.5       raeburn   629:     my @targets = ();
                    630:     my %resources = ();
                    631:     my %items = ();
                    632:     my %hrefs = ();
                    633:     my %urls = ();
                    634:     my %resinfo = ();
                    635:     my %total = (
                    636:                    page => 0,
                    637:                    prob => 0,
                    638:                    seq => 0,
                    639:                    board => 0,         
                    640:                    quiz => 0,
                    641:                    surv => 0,
                    642:     );
                    643: 
                    644:     my @pages = ();
                    645:     my @sequences = ();
                    646:     my @resrcfiles = ();
                    647: 
                    648:     my $tempdir = $ENV{'form.tempdir'};
                    649: 
                    650:     foreach my $area (@{$areas}) {
1.9       raeburn   651:         if (defined($ENV{"form.$area"}) ) {
1.5       raeburn   652:             if ($cms eq 'angel' && $area eq 'doc') {
                    653:                 foreach (@{$$cmsmap{$cms}{$area}}) {
1.9       raeburn   654:                     $importareas{$_} = 1;
1.1       raeburn   655:                 }
1.5       raeburn   656:             } else {
1.9       raeburn   657:                 $importareas{$$cmsmap{$cms}{$area}} = 1;
1.2       raeburn   658:             }
1.5       raeburn   659:             if ($area eq 'board') {
                    660:                 $db_handling = $ENV{'form.db_handling'};
                    661:             } elsif ($area eq 'users') {
                    662:                 $user_handling = $ENV{'form.user_handling'};
1.4       raeburn   663:             }
1.1       raeburn   664:         }
                    665:     }
1.9       raeburn   666: 
                    667:     my $manifest_result = &Apache::imsprocessor::process_manifest($cms,$tempdir,\%resources,\%items,\%hrefs,\%resinfo,'prepare',\%includedres);
                    668:     if ($manifest_result eq 'ok') {
                    669:         foreach my $res (sort keys %resources) {
                    670:             if ($importareas{$resources{$res}{type}}) {
                    671:                 $includedres{$res} = 1;
                    672:             }
                    673:         }
                    674:         foreach my $itm (sort keys %items) {
                    675:             &Apache::imsprocessor::get_imports(\%includeditems,\%items,\%resources,\%importareas,$itm);
                    676:         }
                    677:     }
                    678: 
                    679:     foreach my $itm (sort keys %includeditems) {
                    680:         &Apache::imsprocessor::get_parents(\%includeditems,\%items,$itm);
                    681:     }
                    682: 
                    683:     $manifest_result = &Apache::imsprocessor::process_manifest($cms,$tempdir,\%resources,\%items,\%hrefs,\%resinfo,'build',\%includedres,\%includeditems);
1.5       raeburn   684:     if ($manifest_result eq 'ok') {
1.9       raeburn   685:         &Apache::imsprocessor::target_resources(\%resources,\%importareas,\@targets);
1.5       raeburn   686: 
                    687:         my @boards = ();
                    688:         my @announcements = ();
                    689:         my @quizzes = ();
                    690:         my @surveys = ();
1.9       raeburn   691:         my @pools = ();
1.5       raeburn   692:         my @groups = ();
                    693:         my %messages = ();
                    694:         my @timestamp = ();
                    695:         my %boardnum = ();
                    696:         my @topurls = ();
                    697:         my @topnames = ();
1.7       raeburn   698:         my @packages = ();
1.5       raeburn   699: 
1.10    ! raeburn   700:         &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   701: 
1.9       raeburn   702:         my $copy_result = &Apache::imsprocessor::copy_resources('CSTR',$cms,\%hrefs,$tempdir,\@targets,\%urls,$crs,$cdom,$chome,$destdir,$timenow,\%importareas);
1.5       raeburn   703:    
1.10    ! raeburn   704:         &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   705: 
1.9       raeburn   706:         $r->print("<h3>IMS import completed</h3>");
1.5       raeburn   707: 
                    708:         if ($cms eq 'bb5') {
                    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, $total{board} bulletin boards, $total{quiz} quizzes, $total{surv} surveys and $total{prob} problems have been created.<br /><br />\n");
                    710:         } elsif ($cms eq 'angel') {
                    711:             $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");
                    712:         }
1.10    ! raeburn   713:        $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   714:         if ($destdir =~ m-^/home/$uname/public_html/-) { 
                    715:             system (" rm -r -f $destdir/temp");
                    716:         }
1.5       raeburn   717:     } elsif ($manifest_result eq 'nomanifest') {
1.9       raeburn   718:         $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   719:     }
1.5       raeburn   720: }
                    721: 
                    722: # ---------------------------------------------------------------- Get LON-CAPA Course Coordinator roles for this user
                    723: sub get_ccroles {
                    724:     my ($user,$dom,$crsentry,$crslist) = @_;
                    725:     my %roles = ();
                    726:     unless ($user eq '') {
                    727:         %roles = &Apache::lonnet::dump('roles',$dom,$user);
1.4       raeburn   728:     }
1.5       raeburn   729:     my $iter = 0;
                    730:     my @codes = ();
                    731:     my %courses = ();
                    732:     my @crslist = ();
                    733:     my %descrip =();
                    734:     foreach my $key (keys %roles ) {
                    735:         if ($key =~ m/^\/(\w+)\/(\w+)_cc$/) {
                    736:             my $cdom = $1;
                    737:             my $crs = $2;
                    738:             my $role_end = 0;
                    739:             my $role_start = 0;
                    740:             my $active_chk = 1;
                    741:             if ( $roles{$key} =~ m/^cc_(\d+)/ ) {
                    742:                 $role_end = $1;
                    743:                 if ( $roles{$key} =~ m/^cc_($role_end)_(\d+)$/ )
                    744:                 {
                    745:                     $role_start = $2;
                    746:                 }
                    747:             }
                    748:             if ($role_start > 0) {
                    749:                 if (time < $role_start) {
                    750:                     $active_chk = 0;
1.1       raeburn   751:                 }
1.5       raeburn   752:             }
                    753:             if ($role_end > 0) {
                    754:                 if (time > $role_end) {
                    755:                     $active_chk = 0;
1.1       raeburn   756:                 }
                    757:             }
1.5       raeburn   758:             if ($active_chk) {
                    759:                 my $currcode = '';
                    760:                 my %settings = &Apache::lonnet::get('environment',['internal.coursecode','description'],$cdom,$crs);
                    761:                 if (defined($settings{'description'}) ) {
                    762:                     $descrip{$crs} = $settings{'description'};
1.1       raeburn   763:                 } else {
1.5       raeburn   764:                     $descrip{$crs} = 'Unknown';
1.1       raeburn   765:                 }
1.5       raeburn   766:                 if (defined($settings{'internal.coursecode'}) ) {
                    767:                     $currcode = $settings{'internal.coursecode'};
                    768:                     if ($currcode eq '') {
                    769:                         $currcode = "____".$iter;
                    770:                         $iter ++;
1.1       raeburn   771:                     }
                    772:                 } else {
1.5       raeburn   773:                     $currcode = "____".$iter;
                    774:                     $iter ++;
                    775:                 }
                    776:                 unless (grep/^$currcode$/,@codes) {
                    777:                     push @codes,$currcode;
                    778:                     @{$courses{$currcode}} = ();
1.1       raeburn   779:                 }
1.5       raeburn   780:                 push @{$courses{$currcode}}, $cdom.'/'.$crs;
1.1       raeburn   781:             }
1.5       raeburn   782:         }
                    783:     }
                    784:     foreach my $code (sort @codes) {
                    785:         foreach my $crsdom (@{$courses{$code}}) {
1.2       raeburn   786:             my ($cdom,$crs) = split/\//,$crsdom;
                    787:             my $showcode = '';
                    788:             unless ($code =~m/^____\d+$/) {  $showcode = $code; }
                    789:             $$crsentry{$crsdom} = $showcode.':'.$descrip{$crs};
1.5       raeburn   790:             push @{$crslist}, $crsdom;
1.2       raeburn   791:         }
                    792:     }
1.5       raeburn   793:     return;
1.2       raeburn   794: }
1.1       raeburn   795: 
                    796: # ---------------------------------------------------------------- Main Handler
                    797: sub handler {
                    798:     my $r=shift;
                    799:     my $uname;
                    800:     my $udom;
                    801:     my $javascript = '';
                    802:     my $page_name = '';
                    803:     my $current_page = '';
                    804:     my $loadentries = '';
                    805:     my $qcount = '';
1.5       raeburn   806: 
                    807: # get personal information for this user
                    808:     my $user=$ENV{'user.name'};
                    809:     my $dom=$ENV{'user.domain'};
                    810: 
1.1       raeburn   811: #
1.5       raeburn   812: # re-attach user
1.1       raeburn   813: #
                    814:     if ($ENV{'form.uploaduname'}) {
                    815:         $ENV{'form.filename'}='/priv/'.$ENV{'form.uploaduname'}.'/'.
                    816:             $ENV{'form.filename'};
                    817:     }
                    818:     ($uname,$udom)=
                    819:         &Apache::loncacc::constructaccess($ENV{'form.filename'},
                    820:                                           $r->dir_config('lonDefDomain'));
                    821:     unless (($uname) && ($udom)) {
                    822:         $r->log_reason($uname.' at '.$udom.
                    823:                        ' trying to publish file '.$ENV{'form.filename'}.
                    824:                        ' - not authorized',
                    825:                        $r->filename);
                    826:         return HTTP_NOT_ACCEPTABLE;
                    827:     }
                    828:                                                                                              
                    829:     my $fn;
                    830:     if ($ENV{'form.filename'}) {
                    831:         $fn=$ENV{'form.filename'};
                    832:         $fn=~s/^http\:\/\/[^\/]+\///;
                    833:         $fn=~s/^\///;
                    834:         $fn=~s/(\~|priv\/)(\w+)//;
                    835:         $fn=~s/\/+/\//g;
                    836:     } else {
                    837:         $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                    838:                        ' unspecified filename for upload', $r->filename);
                    839:         return HTTP_NOT_FOUND;
                    840:     }
1.5       raeburn   841:     my $zipupload = '/home/'.$uname.'/public_html'.$fn;
1.1       raeburn   842:     my $pathname = &File::Basename::dirname($fn);
                    843:     my $fullpath = '/priv/'.$uname.$pathname;
                    844:     unless ($pathname eq '/') {
                    845:         $fullpath .= '/';
                    846:     }
                    847:     my $loadentries = '';
1.5       raeburn   848: 
                    849:     my @areas = ();
                    850:     my %cmsmap = ();
                    851:     my %areaname = ();
                    852:     my $numcrs = 0;
                    853:              
                    854:     &Apache::imsprocessor::ims_config(\@areas,\%cmsmap,\%areaname);
1.1       raeburn   855: # ----------------------------------------------------------- Start page output
                    856:     &Apache::loncommon::content_type($r,'text/html');
                    857:     $r->send_http_header;
1.5       raeburn   858: 
                    859:     if ($ENV{'form.phase'} eq 'two') {
                    860:         &jscript_one($fullpath,\$javascript,$uname,$udom);
                    861:     } elsif ($ENV{'form.phase'} eq 'three') {
                    862:         &jscript_two(\$javascript,$user,$dom,\$numcrs);
                    863:     } elsif ($ENV{'form.phase'} eq 'four') {
                    864:         &jscript_three(\$javascript);
1.1       raeburn   865:     }
                    866:     $r->print("<html><head><title>LON-CAPA Construction Space</title><script type=\"text/javascript\">\n//<!--\n$javascript\n// --></script>\n</head>");
                    867:                                                                                              
                    868:     $r->print(&Apache::loncommon::bodytag('Upload IMS package to Construction Space',undef,$loadentries));
                    869:                                                                                              
                    870:     if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
                    871:         $r->print('<h3><font color=red>'.&mt('Co-Author').': '.$uname.
                    872:                   &mt(' at ').$udom.'</font></h3>');
1.5       raeburn   873:     }   
                    874:     if ($ENV{'form.phase'} eq 'two') {
1.1       raeburn   875:         my $flag = &Apache::lonupload::phasetwo($r,$fn,$uname,$udom,'imsimport');
                    876:         if ($flag eq 'ok') {
1.5       raeburn   877:             &display_one($r,$uname,$fn,$fullpath);
                    878:         }
                    879:     } elsif ( ($ENV{'form.phase'} eq 'three') || ($ENV{'form.phase'} eq 'four') ) {
                    880:         my $docroot = $ENV{'form.newdir'};
                    881:         my $newdir = '';
                    882:         if ($docroot =~ m|public_html/(.+)$|) {
                    883:             $newdir = $1;
                    884:         }
                    885:         if ($ENV{'form.phase'} eq 'three') {
                    886:             &display_two ($r,$zipupload,\@areas,\%areaname,\%cmsmap,$uname,$newdir,\$numcrs,$fullpath);
                    887:         } elsif ($ENV{'form.phase'} eq 'four') {
                    888:             &display_three ($r,$uname,$udom,\@areas,\%areaname,\%cmsmap,$docroot,$newdir);
1.1       raeburn   889:         }
                    890:     } else {
                    891:         &Apache::lonupload::phaseone($r,$fn,$uname,$udom,'imsimport');
                    892:     }
                    893:     $r->print('</body></html>');
                    894:     return OK;
                    895: }
                    896: 1;
                    897: __END__

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