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

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

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