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

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

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