File:  [LON-CAPA] / loncom / interface / londocs.pm
Revision 1.162: download - view: text, annotated - select for diffs
Thu Dec 23 20:50:35 2004 UTC (19 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: version_1_3_X, version_1_3_3, version_1_3_2, version_1_3_1, version_1_3_0, HEAD
Fix regexp so IMS export correctly distinguishes items in /res and fix url for retrieval of resources for which user is author.

    1: # The LearningOnline Network
    2: # Documents
    3: #
    4: # $Id: londocs.pm,v 1.162 2004/12/23 20:50:35 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::londocs;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common :http);
   33: use Apache::imsexport;
   34: use Apache::lonnet;
   35: use Apache::loncommon;
   36: use Apache::lonratedt;
   37: use Apache::lonratsrv;
   38: use Apache::lonxml;
   39: use Apache::loncreatecourse;
   40: use Apache::lonnavmaps;
   41: use HTML::Entities;
   42: use GDBM_File;
   43: use Apache::lonlocal;
   44: use Cwd;
   45: 
   46: my $iconpath;
   47: 
   48: my %hash;
   49: 
   50: my $hashtied;
   51: my %alreadyseen=();
   52: 
   53: my $hadchanges;
   54: 
   55: # Available help topics
   56: 
   57: my %help=();
   58: 
   59: # Mapread read maps into lonratedt::global arrays 
   60: # @order and @resources, determines status
   61: # sets @order - pointer to resources in right order
   62: # sets @resources - array with the resources with correct idx
   63: #
   64: 
   65: sub mapread {
   66:     my ($coursenum,$coursedom,$map)=@_;
   67:     return
   68:       &Apache::lonratedt::mapread('/uploaded/'.$coursedom.'/'.$coursenum.'/'.
   69:                                 $map);
   70: }
   71: 
   72: sub storemap {
   73:     my ($coursenum,$coursedom,$map)=@_;
   74:     my ($outtext,$errtext)=
   75:       &Apache::lonratedt::storemap('/uploaded/'.$coursedom.'/'.$coursenum.'/'.
   76:                                 $map,1);
   77:     if ($errtext) { return ($errtext,2); }
   78:     
   79:     $hadchanges=1;
   80:     return ($errtext,0);
   81: }
   82: 
   83: # ----------------------------------------- Return hash with valid author names
   84: 
   85: sub authorhosts {
   86:     my %outhash=();
   87:     my $home=0;
   88:     my $other=0;
   89:     foreach (keys %ENV) {
   90: 	if ($_=~/^user\.role\.(au|ca)\.(.+)$/) {
   91: 	    my $role=$1;
   92: 	    my $realm=$2;
   93: 	    my ($start,$end)=split(/\./,$ENV{$_});
   94: 	    if (($start) && ($start>time)) { next; }
   95: 	    if (($end) && (time>$end)) { next; }
   96: 	    my $ca; my $cd;
   97: 	    if ($1 eq 'au') {
   98: 		$ca=$ENV{'user.name'};
   99: 		$cd=$ENV{'user.domain'};
  100: 	    } else {
  101: 		($cd,$ca)=($realm=~/^\/(\w+)\/(\w+)$/);
  102: 	    }
  103: 	    my $allowed=0;
  104: 	    my $myhome=&Apache::lonnet::homeserver($ca,$cd);
  105: 	    my @ids=&Apache::lonnet::current_machine_ids();
  106: 	    foreach my $id (@ids) { if ($id eq $myhome) { $allowed=1; } }
  107: 	    if ($allowed) {
  108: 		$home++;
  109: 		$outhash{'home_'.$ca.'@'.$cd}=1;
  110: 	    } else {
  111: 		$outhash{'otherhome_'.$ca.'@'.$cd}=$myhome;
  112: 		$other++;
  113: 	    }
  114: 	}
  115:     }
  116:     return ($home,$other,%outhash);
  117: }
  118: # ------------------------------------------------------ Generate "dump" button
  119: 
  120: sub dumpbutton {
  121:     my ($home,$other,%outhash)=&authorhosts();
  122:     if ($home+$other==0) { return ''; }
  123:     my $output='</td><td bgcolor="#DDDDCC">';
  124:     if ($home) {
  125: 	return '</td><td bgcolor="#DDDDCC">'.
  126: 	    '<input type="submit" name="dumpcourse" value="'.
  127: 	    &mt('Dump Course DOCS to Construction Space').'" />'.
  128: 	    &Apache::loncommon::help_open_topic('Docs_Dump_Course_Docs');
  129:     } else {
  130: 	return'</td><td bgcolor="#DDDDCC">'.
  131:      &mt('Dump Course DOCS to Construction Space: available on other servers');
  132:     }
  133: }
  134: 
  135: # -------------------------------------------------------- Actually dump course
  136: 
  137: sub dumpcourse {
  138:     my $r=shift;
  139:     $r->print('<html><head><title>Dump DOCS</title></head>'.
  140:         &Apache::loncommon::bodytag('Dump Course DOCS to Construction Space').
  141: 	      '<form name="dumpdoc" method="post">');
  142:     my ($home,$other,%outhash)=&authorhosts();
  143:     unless ($home) { return ''; }
  144:     my $origcrsid=$ENV{'request.course.id'};
  145:     my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
  146:     if (($ENV{'form.authorspace'}) && ($ENV{'form.authorfolder'}=~/\w/)) {
  147: # Do the dumping
  148: 	unless ($outhash{'home_'.$ENV{'form.authorspace'}}) { return ''; }
  149: 	my ($ca,$cd)=split(/\@/,$ENV{'form.authorspace'});
  150: 	$r->print('<h3>'.&mt('Copying Files').'</h3>');
  151: 	my $title=$ENV{'form.authorfolder'};
  152: 	$title=~s/[^\w\/]+/\_/g;
  153: 	my %replacehash=();
  154: 	foreach (keys %ENV) {
  155: 	    if ($_=~/^form\.namefor\_(.+)/) {
  156: 		$replacehash{$1}=$ENV{$_};
  157: 	    }
  158: 	}
  159: 	my $crs='/uploaded/'.$ENV{'request.course.id'}.'/';
  160: 	$crs=~s/\_/\//g;
  161: 	foreach (keys %replacehash) {
  162: 	    my $newfilename=$title.'/'.$replacehash{$_};
  163: 	    $newfilename=~s/[^\w\/\.\/]+/\_/g;
  164: 	    my @dirs=split(/\//,$newfilename);
  165: 	    my $path='/home/'.$ca.'/public_html';
  166: 	    my $makepath=$path;
  167: 	    my $fail=0;
  168: 	    for (my $i=0;$i<$#dirs;$i++) {
  169: 		$makepath.='/'.$dirs[$i];
  170: 		unless (-e $makepath) { 
  171: 		    unless(mkdir($makepath,0777)) { $fail=1; } 
  172: 		}
  173: 	    }
  174: 	    $r->print('<br /><tt>'.$_.'</tt> => <tt>'.$newfilename.'</tt>: ');
  175: 	    if (my $fh=Apache::File->new('>'.$path.'/'.$newfilename)) {
  176: 		if ($_=~/\.(sequence|page|html|htm|xml|xhtml)$/) {
  177: 		    print $fh &Apache::loncreatecourse::rewritefile(
  178:          &Apache::loncreatecourse::readfile($ENV{'request.course.id'},$_),
  179: 				     (%replacehash,$crs => '')
  180: 								    );
  181: 		} else {
  182: 		    print $fh
  183:          &Apache::loncreatecourse::readfile($ENV{'request.course.id'},$_);
  184: 		       }
  185: 		$fh->close();
  186: 	    } else {
  187: 		$fail=1;
  188: 	    }
  189: 	    if ($fail) {
  190: 		$r->print('<font color="red">fail</font>');
  191: 	    } else {
  192: 		$r->print('<font color="green">ok</font>');
  193: 	    }
  194: 	}
  195:     } else {
  196: # Input form
  197: 	unless ($home==1) {
  198: 	    $r->print(
  199: 		      '<h3>'.&mt('Select the Construction Space').'</h3><select name="authorspace">');
  200: 	}
  201: 	foreach (sort keys %outhash) {
  202: 	    if ($_=~/^home_(.+)$/) {
  203: 		if ($home==1) {
  204: 		    $r->print(
  205: 		  '<input type="hidden" name="authorspace" value="'.$1.'" />');
  206: 		} else {
  207: 		    $r->print('<option value="'.$1.'">'.$1.' - '.
  208: 			      &Apache::loncommon::plainname(split(/\@/,$1)).'</option>');
  209: 		}
  210: 	    }
  211: 	}
  212: 	unless ($home==1) {
  213: 	    $r->print('</select>');
  214: 	}
  215: 	my $title=$origcrsdata{'description'};
  216: 	$title=~s/\s+/\_/gs;
  217: 	$title=~s/\W//gs;
  218: 	$r->print('<h3>'.&mt('Folder in Construction Space').'</h3><input type="text" size="50" name="authorfolder" value="'.$title.'" /><br />');
  219: 	&tiehash();
  220: 	$r->print('<h3>'.&mt('Filenames in Construction Space').'</h3><table border="2"><tr><th>'.&mt('Internal Filename').'</th><th>'.&mt('Title').'</th><th>'.&mt('Save as ...').'</th></tr>');
  221: 	foreach (&Apache::loncreatecourse::crsdirlist($origcrsid,'userfiles')) {
  222: 	    $r->print('<tr><td>'.$_.'</td>');
  223: 	    my ($ext)=($_=~/\.(\w+)$/);
  224: 	    my $title=$hash{'title_'.$hash{
  225: 		'ids_/uploaded/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/'.$_}};
  226: 	    $r->print('<td>'.($title?$title:'&nbsp;').'</td>');
  227: 	    unless ($title) {
  228: 		$title=$_;
  229: 	    }
  230: 	    $title=~s/\.(\w+)$//;
  231: 	    $title=~s/[^\w\/]+/\_/gs;
  232: 	    $title.='.'.$ext;
  233: 	    $r->print("\n<td><input type='text' size='60' name='namefor_".$_."' value='".$title."' /></td></tr>\n");
  234: 	}
  235: 	$r->print("</table>\n");
  236: 	&untiehash();
  237: 	$r->print(
  238:   '<p><input type="submit" name="dumpcourse" value="'.&mt('Dump Course DOCS').'" /></p></form>');
  239:     }
  240: }
  241: 
  242: # ------------------------------------------------------ Generate "export" button
  243: 
  244: sub exportbutton {
  245:     return '';
  246:     return '</td><td bgcolor="#DDDDCC">'.
  247:             '<input type="submit" name="exportcourse" value="'.
  248:             &mt('Export Course to IMS').'" />'.
  249:             &Apache::loncommon::help_open_topic('Docs_Export_Course_Docs');
  250: }
  251: 
  252: sub exportcourse {
  253:     my $r=shift;
  254:     my %discussiontime = &Apache::lonnet::dump('discussiontimes',
  255:                                                $ENV{'course.'.$ENV{'request.course.id'}.'.domain'}, $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  256:     my $numdisc = keys %discussiontime;
  257:     my $navmap = Apache::lonnavmaps::navmap->new();
  258:     my $it=$navmap->getIterator(undef,undef,undef,1,undef,undef);
  259:     my $curRes;
  260:     my $outcome;
  261: 
  262:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  263:                                             ['finishexport']);
  264:     if ($ENV{'form.finishexport'}) {
  265:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  266:                                             ['archive','discussion']);
  267: 
  268:         my @exportitems = ();
  269:         if (defined($ENV{'form.archive'})) {
  270:             if (ref($ENV{'form.archive'}) eq 'ARRAY') {
  271:                 @exportitems = @{$ENV{'form.archive'}};
  272:             } else {
  273:                 $exportitems[0] = $ENV{'form.archive'};
  274:             }
  275:         }
  276:         my @discussions = ();
  277:         if (defined($ENV{'form.discussion'})) {
  278:             if (ref($ENV{'form.discussion'}) eq 'ARRAY') {
  279:                 @discussions = $ENV{'form.discussion'};
  280:             } else {
  281:                 $discussions[0] = $ENV{'form.discussion'};
  282:             }
  283:         }
  284:         if (@exportitems == 0 && @discussions == 0) {
  285:             $outcome = '<br />As you did not select any content items or discussions for export, an IMS package has not been created.  Please <a href="javascript:history.go(-1)">go back</a> to select either content items or discussions for export';
  286:         } else {
  287:             my $now = time;
  288:             my %symbs;
  289:             my $manifestok = 0;
  290:             my $imsresources;
  291:             my $tempexport;
  292:             my $copyresult;
  293:             my $ims_manifest = &create_ims_store($now,\$manifestok,\$outcome,\$tempexport);
  294:             if ($manifestok) {
  295:                 &build_package($now,$navmap,\@exportitems,\@discussions,\$outcome,$tempexport,\$copyresult,$ims_manifest);
  296:                 close($ims_manifest);
  297: 
  298: #Create zip file in prtspool
  299:                 my $imszipfile = '/prtspool/'.
  300:                 $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
  301:                    time.'_'.rand(1000000000).'.zip';
  302: # zip can cause an sh launch which can pass along all of %ENV
  303: # which can be too large for /bin/sh to handle
  304:                 my %oldENV=%ENV;
  305:                 undef(%ENV);
  306:                 my $cwd = &Cwd::getcwd();
  307:                 my $imszip = '/home/httpd/'.$imszipfile;
  308:                 chdir $tempexport;
  309:                 open(OUTPUT, "zip -r $imszip *  2> /dev/null |");
  310:                 close(OUTPUT);
  311:                 chdir $cwd;
  312:                 %ENV=%oldENV;
  313:                 undef(%oldENV);
  314:                 $outcome .= 'Download the zip file from <a href="'.$imszipfile.'">IMS course archive</a><br />';
  315:                 if ($copyresult) {
  316:                     $outcome .= 'The following errors occurred during export - '.$copyresult;
  317:                 }
  318:             } else {
  319:                 $outcome = '<br />Unfortunately you will not be able to retrieve an IMS archive of this posts at this time, because there was a problem creating a manifest file.<br />';
  320:             }
  321:         }
  322: 
  323:         $r->print('<html><head><title>Export Course</title></head>'.
  324:             &Apache::loncommon::bodytag('Export course to IMS content package'));
  325:         $r->print($outcome);
  326:         $r->print('</body></html>');
  327:     } else {
  328:         my $display;
  329:         $display = '<form name="exportdoc" method="post">'."\n";
  330:         $display .= 'Choose which items you wish to export from your course.<br /><br />';
  331:         $display .= '<table border="0" cellspacing="0" cellpadding="3">'.
  332:                     '<tr><td><fieldset><legend>&nbsp;<b>Content items</b></legend>'.
  333:                     '<input type="button" value="check all" '.
  334:                     'onclick="javascript:checkAll(document.exportdoc.archive)" />'.
  335:                     '&nbsp;&nbsp;<input type="button" value="uncheck all"'.
  336:                     ' onclick="javascript:uncheckAll(document.exportdoc.archive)" /></fieldset></td>'.
  337:                     '<td>&nbsp;</td><td>&nbsp;</td>'.
  338:                     '<td align="right"><fieldset><legend>&nbsp;<b>Discussion posts'.
  339:                     '</b></legend><input type="button" value="check all"'.
  340:                     ' onclick="javascript:checkAll(document.exportdoc.discussion)" />'.
  341:                     '&nbsp;&nbsp;<input type="button" value="uncheck all"'.
  342:                     ' onclick="javascript:uncheckAll(document.exportdoc.discussion)" /></fieldset></td>'.
  343:                     '</tr></table>';
  344:         my $curRes;
  345:         my $depth = 0;
  346:         my $count = 0;
  347:         my $boards = 0;
  348:         my $startcount = 5;
  349:         my %parent = ();
  350:         my %children = ();
  351:         my $lastcontainer = $startcount;
  352:         my @bgcolors = ('#F6F6F6','#FFFFFF');
  353:         $display .= '<table cellspacing="0"><tr>'.
  354:             '<td><b>Export content item?<br /></b></td><td>&nbsp;</td><td align="right">'."\n";
  355:         if ($numdisc > 0) {
  356:             $display.='<b>Export&nbsp;discussion posts?</b>'."\n";
  357:         }
  358:         $display.='&nbsp;</td></tr>';
  359:         while ($curRes = $it->next()) {
  360:             if (ref($curRes)) {
  361:                 $count ++;
  362:             }
  363:             if ($curRes == $it->BEGIN_MAP()) {
  364:                 $depth++;
  365:                 $parent{$depth} = $lastcontainer;
  366:             }
  367:             if ($curRes == $it->END_MAP()) {
  368:                 $depth--;
  369:                 $lastcontainer = $parent{$depth};
  370:             }
  371:             if (ref($curRes)) {
  372:                 my $symb = $curRes->symb();
  373:                 my $ressymb = $symb;
  374:                 if ($ressymb =~ m|adm/(\w+)/(\w+)/(\d+)/bulletinboard$|) {
  375:                     unless ($ressymb =~ m|adm/wrapper/adm|) {
  376:                         $ressymb = 'bulletin___'.$3.'___adm/wrapper/adm/'.$1.'/'.$2.'/'.$3.'/bulletinboard';
  377:                     }
  378:                 }
  379:                 my $color = $count%2;
  380:                 $display .='<tr bgcolor='.$bgcolors[$color].'><td>'."\n".
  381:                     '<input type="checkbox" name="archive" value="'.$count.'" ';
  382:                 if (($curRes->is_sequence()) || ($curRes->is_page())) {
  383:                     my $checkitem = $count + $boards + $startcount;
  384:                     $display .= 'onClick="javascript:propagateCheck('."'$checkitem'".')"';
  385:                 }
  386:                 $display .= ' />'."\n";
  387:                 for (my $i=0; $i<$depth; $i++) {
  388:                     $display .= '<img src="/adm/lonIcons/whitespace1.gif" width="25" height="1" alt="" border="0" /><img src="/adm/lonIcons/whitespace1.gif" width="25" height="1" alt="" border="0" />'."\n";
  389:                 }
  390:                 if ($curRes->is_sequence()) {
  391:                     $display .= '<img src="/adm/lonIcons/navmap.folder.open.gif">&nbsp;'."\n";
  392:                     $lastcontainer = $count + $startcount + $boards;
  393:                 } elsif ($curRes->is_page()) {
  394:                     $display .= '<img src="/adm/lonIcons/navmap.page.open.gif">&nbsp;'."\n";
  395:                     $lastcontainer = $count + $startcount + $boards;
  396:                 }
  397:                 my $currelem = $count+$boards+$startcount;
  398:                 $children{$parent{$depth}} .= $currelem.':';
  399:                 $display .= '&nbsp;'.$curRes->title().'</td>';
  400:                 if ($discussiontime{$ressymb} > 0) {
  401:                     $boards ++;
  402:                     $currelem = $count+$boards+$startcount;
  403:                     $display .= '<td>&nbsp;</td><td align="right"><input type="checkbox" name="discussion" value="'.$count.'" />&nbsp;</td>'."\n";
  404:                 } else {
  405:                     $display .= '<td colspan="2">&nbsp;</td>'."\n";
  406:                 }
  407:             }
  408:         }
  409:         my $scripttag = qq|
  410: <script>
  411: 
  412: function checkAll(field) {
  413:     if (field.length > 0) {
  414:         for (i = 0; i < field.length; i++) {
  415:             field[i].checked = true ;
  416:         }
  417:     } else {
  418:         field.checked = true
  419:     }
  420: }
  421:                                                                                 
  422: function uncheckAll(field) {
  423:     if (field.length > 0) {
  424:         for (i = 0; i < field.length; i++) {
  425:             field[i].checked = false ;
  426:         }
  427:     } else {
  428:         field.checked = false ;
  429:     }
  430: }
  431: 
  432: function propagateCheck(item) {
  433:     if (document.exportdoc.elements[item].checked == true) {
  434:         containerCheck(item)
  435:     }
  436: } 
  437: 
  438: function containerCheck(item) {
  439:     document.exportdoc.elements[item].checked = true
  440:     var numitems = $count + $boards + $startcount
  441:     var parents = new Array(numitems)
  442:     for (var i=$startcount; i<numitems; i++) {
  443:         parents[i] = new Array
  444:     }
  445:         |;
  446: 
  447:         foreach my $container (sort { $a <=> $b } keys %children) {
  448:             my @contents = split/:/,$children{$container};
  449:             for (my $i=0; $i<@contents; $i ++) {
  450:                 $scripttag .= '    parents['.$container.']['.$i.'] = '.$contents[$i]."\n";
  451:             }
  452:         }
  453: 
  454:         $scripttag .= qq|
  455:     if (parents[item].length > 0) {
  456:         for (var j=0; j<parents[item].length; j++) {
  457:             containerCheck(parents[item][j])
  458:         }
  459:      }   
  460: }
  461: 
  462: </script>
  463:         |;
  464:         $r->print('<html><head><title>Export Course</title>'.$scripttag.'</head>'.
  465:             &Apache::loncommon::bodytag('Export course to IMS content package'
  466: ));
  467: 
  468:         $r->print($display.'</table>'.
  469:                   '<p><input type="hidden" name="finishexport" value="1">'.
  470:                   '<input type="submit" name="exportcourse" value="'.
  471:                   &mt('Export Course DOCS').'" /></p></form></body></html>');
  472:     }
  473: }
  474: 
  475: sub create_ims_store {
  476:     my ($now,$manifestok,$outcome,$tempexport) = @_;
  477:     $$tempexport = $Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/ims_exports';
  478:     my $ims_manifest;
  479:     if (!-e $$tempexport) {
  480:         mkdir($$tempexport,0700);
  481:     }
  482:     $$tempexport .= '/'.$now;
  483:     if (!-e $$tempexport) {
  484:         mkdir($$tempexport,0700);
  485:     }
  486:     $$tempexport .= '/'.$ENV{'user.domain'}.'_'.$ENV{'user.name'};
  487:     if (!-e $$tempexport) {
  488:         mkdir($$tempexport,0700);
  489:     }
  490:     if (!-e "$$tempexport/resources") {
  491:         mkdir("$$tempexport/resources",0700);
  492:     }
  493: # open manifest file
  494:     my $manifest = '/imsmanifest.xml';
  495:     my $manifestfilename = $$tempexport.$manifest;
  496:     if ($ims_manifest = Apache::File->new('>'.$manifestfilename)) {
  497:         $$manifestok=1;
  498:         print $ims_manifest
  499: '<?xml version="1.0" encoding="UTF-8"?>'."\n".
  500: '<manifest xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"'.
  501: ' xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2"'.
  502: ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'.
  503: ' identifier="MANIFEST-'.$ENV{'request.course.id'}.'-'.$now.'"'.
  504: '  xsi:schemaLocation="http://www.imsglobal.org/xsd/imscp_v1p1imscp_v1p1.xsd'.
  505: '  http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd">'."\n".
  506: '  <organizations default="ORG-'.$ENV{'request.course.id'}.'-'.$now.'">'."\n".
  507: '    <organization identifier="ORG-'.$ENV{'request.course.id'}.'-'.$now.'"'.
  508: ' structure="hierarchical">'."\n".
  509: '      <title>'.$ENV{'request.'.$ENV{'request.course.id'}.'.description'}.'</title>'
  510:     } else {
  511:         $$outcome .= 'An error occurred opening the IMS manifest file.<br />'
  512: ;
  513:     }
  514:     return $ims_manifest;
  515: }
  516: 
  517: sub build_package {
  518:     my ($now,$navmap,$exportitems,$discussions,$outcome,$tempexport,$copyresult,$ims_manifest) = @_;
  519: # first iterator to look for dependencies
  520:     my $it = $navmap->getIterator(undef,undef,undef,1,undef,undef);
  521:     my $curRes;
  522:     my $count = 0;
  523:     my $depth = 0;
  524:     my $lastcontainer = 0;
  525:     my %parent = ();
  526:     my @dependencies = ();
  527:     my $cnum = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
  528:     my $cdom = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
  529:     while ($curRes = $it->next()) {
  530:         if (ref($curRes)) {
  531:             $count ++;
  532:         }
  533:         if ($curRes == $it->BEGIN_MAP()) {
  534:             $depth++;
  535:             $parent{$depth} = $lastcontainer;
  536:         }
  537:         if ($curRes == $it->END_MAP()) {
  538:             $depth--;
  539:             $lastcontainer = $parent{$depth};
  540:         }
  541:         if (ref($curRes)) {
  542:             if ($curRes->is_sequence() || $curRes->is_page()) {
  543:                 $lastcontainer = $count;
  544:             }
  545:             if (grep/^$count$/,@$exportitems) {
  546:                 &get_dependencies($exportitems,\%parent,$depth,\@dependencies);
  547:             }
  548:         }
  549:     }
  550: # second iterator to build manifest and store resources
  551:     $it = $navmap->getIterator(undef,undef,undef,1,undef,undef);
  552:     $depth = 0;
  553:     my $prevdepth;
  554:     $count = 0;
  555:     my $imsresources;
  556:     my $pkgdepth;
  557:     my $included = 0;
  558:     while ($curRes = $it->next()) {
  559:         if ($curRes == $it->BEGIN_MAP()) {
  560:             $prevdepth = $depth;
  561:             $depth++;
  562:         }
  563:         if ($curRes == $it->END_MAP()) {
  564:             $prevdepth = $depth;
  565:             $depth--;
  566:         }
  567: 
  568:         if (ref($curRes)) {
  569:             $count ++;
  570:             if ((grep/^$count$/,@$exportitems) || (grep/^$count$/,@dependencies)) {
  571:                 my $symb = $curRes->symb();
  572:                 my $isvisible = 'true';
  573:                 my $resourceref;
  574:                 if ($curRes->randomout()) {
  575:                     $isvisible = 'false';
  576:                 }
  577:                 unless ($curRes->is_sequence()) {
  578:                     $resourceref = 'identifierref="RES-'.$ENV{'request.course.id'}.'-'.$count.'"';
  579:                 }
  580:                 if (($depth <= $prevdepth) && ($count > 1) && ($included)) {
  581:                     print $ims_manifest "\n".'  </item>'."\n";
  582:                 }
  583:                 $included = 1;
  584:                 $prevdepth = $depth;
  585: 
  586:                 my $itementry =
  587:               '<item identifier="ITEM-'.$ENV{'request.course.id'}.'-'.$count.
  588:               '" isvisible="'.$isvisible.'" '.$resourceref.'>'.
  589:               '<title>'.$curRes->title().'</title>';
  590:                 print $ims_manifest "\n".$itementry;
  591: 
  592:                 unless ($curRes->is_sequence()) {
  593:                     my $content_file;
  594:                     my @hrefs = ();
  595:                     &process_content($count,$curRes,$cdom,$cnum,$symb,\$content_file,\@hrefs,$copyresult,$tempexport);
  596:                     if ($content_file) {
  597:                         $imsresources .= "\n".
  598:                      '   <resource identifier="RES-'.$ENV{'request.course.id'}.'-'.$count.
  599:                      '" type="webcontent" href="'.$content_file.'">'."\n".
  600:                      '       <file href="'.$content_file.'" />'."\n";
  601:                         foreach (@hrefs) {
  602:                             $imsresources .=
  603:                      '        <file href="'.$_.'" />'."\n";
  604:                         }
  605:                         if (grep/^$count$/,@$discussions) {
  606:                             my $ressymb = $symb;
  607:                             my $mode;
  608:                             if ($ressymb =~ m|adm/(\w+)/(\w+)/(\d+)/bulletinboard$|) {
  609:                                 unless ($ressymb =~ m|adm/wrapper/adm|) {
  610:                                     $ressymb = 'bulletin___'.$3.'___adm/wrapper/adm/'.$1.'/'.$2.'/'.$3.'/bulletinboard';
  611:                                 }
  612:                                 $mode = 'board';
  613:                             }
  614:                             my %extras = (
  615:                                           caller => 'imsexport',
  616:                                           tempexport => $tempexport.'/resources',
  617:                                           count => $count
  618:                                          );
  619:                             my $discresult = &Apache::lonfeedback::list_discussion($mode,undef,$ressymb,\%extras);
  620:                         }
  621:                         $imsresources .= '    </resource>'."\n";
  622:                     }
  623:                 }
  624:                 $pkgdepth = $depth;
  625:             } else {
  626:                 $included = 0;
  627:             }
  628:         }
  629:     }
  630:     while ($pkgdepth > 0) {
  631:         print $ims_manifest "    </item>\n";
  632:         $pkgdepth --;
  633:     }
  634:     my $resource_text = qq|
  635:     </organization>
  636:   </organizations>
  637:   <resources>
  638:     $imsresources
  639:   </resources>
  640: </manifest>
  641:     |;
  642:     print $ims_manifest $resource_text;
  643: }
  644: 
  645: sub get_dependencies {
  646:     my ($exportitems,$parent,$depth,$dependencies) = @_;
  647:     if ($depth > 1) {
  648:         if ((!grep/^$$parent{$depth}$/,@$exportitems) && (!grep/^$$parent{$depth}$/,@$dependencies)) {
  649:             push @$dependencies, $$parent{$depth};
  650:             if ($depth > 2) {
  651:                 &get_dependencies($exportitems,$parent,$depth-1,$dependencies);
  652:             }
  653:         }
  654:     }
  655: }
  656: 
  657: sub process_content {
  658:     my ($count,$curRes,$cdom,$cnum,$symb,$content_file,$href,$copyresult,$tempexport) = @_;
  659:     my $content_type;
  660:     my $message;
  661: # find where user is author or co-author
  662:     my @uploads = ();
  663:     if ($curRes->is_sequence()) {
  664:         $content_type = 'sequence';
  665:     } elsif ($curRes->is_page()) {
  666:         $content_type = 'page'; # need to handle individual items in pages.
  667:     } elsif ($symb =~ m-public/$cdom/$cnum/syllabus$-) {
  668:         $content_type = 'syllabus';
  669:         my $contents = &Apache::imsexport::templatedpage($content_type);
  670:         if ($contents) {
  671:             $$content_file = &store_template($contents,$tempexport,$count,$content_type);
  672:         }
  673:     } elsif ($symb =~ m-\.sequence___\d+___ext-) {
  674:         $content_type = 'external';
  675:         my $title = $curRes->title;
  676:         my $contents =  &Apache::imsexport::external($symb,$title);
  677:         if ($contents) {
  678:             $$content_file = &store_template($contents,$tempexport,$count,$content_type);
  679:         }
  680:     } elsif ($symb =~ m-adm/navmaps$-) {
  681:         $content_type =  'navmap';
  682:     } elsif ($symb =~ m-adm/[^/]+/[^/]+/(\d+)/smppg$-) {
  683:         $content_type = 'simplepage';
  684:         my $contents = &Apache::imsexport::templatedpage($content_type,$1,$count,\@uploads);
  685:         if ($contents) {
  686:             $$content_file = &store_template($contents,$tempexport,$count,$content_type);
  687:         }
  688:     } elsif ($symb =~ m-lib/templates/simpleproblem\.problem$-) {
  689:         $content_type = 'simpleproblem';
  690:         my $contents =  &Apache::imsexport::simpleproblem($symb);
  691:         if ($contents) {
  692:             $$content_file = &store_template($contents,$tempexport,$count,$content_type);
  693:         }
  694:     } elsif ($symb =~ m-lib/templates/examupload\.problem-m) {
  695:         $content_type = 'examupload';
  696:     } elsif ($symb =~ m-adm/(\w+)/(\w+)/(\d+)/bulletinboard$-) {
  697:         $content_type = 'bulletinboard';
  698:         my $contents =  &Apache::imsexport::templatedpage($content_type,$3,$count,\@uploads,$1,$2);
  699:         if ($contents) {
  700:             $$content_file = &store_template($contents,$tempexport,$count,$content_type);
  701:         }
  702:     } elsif ($symb =~ m-adm/([^/]+)/([^/]+)/aboutme$-) {
  703:         $content_type = 'aboutme';
  704:         my $contents =  &Apache::imsexport::templatedpage($content_type,undef,$count,\@uploads,$1,$2);
  705:         if ($contents) {
  706:             $$content_file = &store_template($contents,$tempexport,$count,$content_type);
  707:         }
  708:     } elsif ($symb =~ m-\.(sequence|page)___\d+___uploaded/$cdom/$cnum/-) {
  709:         $$content_file = &replicate_content($cdom,$cnum,$tempexport,$symb,$count,\$message,$href,'uploaded');
  710:     } elsif ($symb =~ m-\.(sequence|page)___\d+___([^/]+)/([^/]+)-) {
  711:         my $canedit = 0;
  712:         if ($2 eq $ENV{'user.domain'} && $3 eq $ENV{'user.name'})  {
  713:             $canedit= 1;
  714:         }
  715:         if ($canedit) {
  716:             $$content_file = &replicate_content($cdom,$cnum,$tempexport,$symb,$count,\$message,$href,'resource');
  717:         } else {
  718:             $$content_file = &replicate_content($cdom,$cnum,$tempexport,$symb,$count,\$message,$href,'noedit');
  719:         }
  720:     } elsif ($symb =~ m-uploaded/$cdom/$cnum-) {
  721:         $$content_file = &replicate_content($cdom,$cnum,$tempexport,$symb,$count,\$message,$href,'uploaded');
  722:     }
  723:     if (@uploads > 0) {
  724:         foreach my $item (@uploads) {
  725:             my $uploadmsg = '';
  726:             &replicate_content($cdom,$cnum,$tempexport,$item,$count,\$uploadmsg,$href,'templateupload');
  727:             if ($uploadmsg) {
  728:                 $$copyresult .= $uploadmsg."\n";
  729:             }
  730:         }
  731:     }
  732:     if ($message) {
  733:         $$copyresult .= $message."\n";
  734:     }
  735: }
  736: 
  737: sub replicate_content {
  738:     my ($cdom,$cnum,$tempexport,$symb,$count,$message,$href,$caller) = @_;
  739:     my ($map,$ind,$url);
  740:     if ($caller eq 'templateupload') {
  741:         $url = $symb;
  742:         $url =~ s#//#/#g;
  743:     } else { 
  744:         ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
  745:     }
  746:     my $content;
  747:     my $filename;
  748:     my $repstatus;
  749:     my $content_name;
  750:     if ($url =~ m-/([^/]+)$-) {
  751:         $filename = $1;
  752:         if (!-e $tempexport.'/resources') {
  753:             mkdir($tempexport.'/resources',0700);
  754:         }
  755:         if (!-e $tempexport.'/resources/'.$count) {
  756:             mkdir($tempexport.'/resources/'.$count,0700);
  757:         }
  758:         my $destination = $tempexport.'/resources/'.$count.'/'.$filename;
  759:         my $copiedfile;
  760:         if ($copiedfile = Apache::File->new('>'.$destination)) {
  761:             my $content;
  762:             if ($caller eq 'resource') {
  763:                 $content = &Apache::lonnet::getfile('/home/httpd/html/res/'.$url);
  764:                 if ($content eq -1) {
  765:                     $$message = 'Could not copy file '.$filename;
  766:                 } else {
  767:                     &extract_media($content,$count,$tempexport,$href,'resource');
  768:                     $repstatus = 'ok';
  769:                 }
  770:             } elsif ($caller eq 'uploaded' || $caller eq 'templateupload') {
  771:                 my $rtncode;
  772:                 $repstatus = &Apache::lonnet::getuploaded('GET',$url,$cdom,$cnum,\$content,$rtncode);
  773:                 if ($repstatus eq 'ok') {
  774:                     if ($url =~ /\.html?$/i) {
  775:                         &extract_media(\$content,$count,$tempexport,$href,'uploaded');
  776:                     }
  777:                 } else {
  778:                     $$message = 'Could not render '.$url.' server message - '.$rtncode;
  779:                 }
  780:             } elsif ($caller eq 'noedit') {
  781: # Need to render the resource without the LON-CAPA Internal header and the Post discussion footer, and then set $content equal to this. 
  782:                 $repstatus = 'ok';
  783:                 $content = 'Not the owner of this resource'; 
  784:             }
  785:             if ($repstatus eq 'ok') {
  786:                 print $copiedfile $content;
  787:             }
  788:             close($copiedfile);
  789:         } else {
  790:             $$message = 'Could not open destination file for '.$filename."\n";
  791:         }
  792:     } else {
  793:         $$message = 'Could not determine name of file for '.$symb."\n";
  794:     }
  795:     if ($repstatus eq 'ok') {
  796:         $content_name = $count.'/'.$filename;
  797:     }
  798:     return $content_name;
  799: }
  800: 
  801: sub extract_media {
  802:     my ($content,$count,$tempexport,$href,$caller) = @_;
  803: # @$href will contain path to any embedded resources in the content.
  804: # For LON-CAPA problems this would be images. applets etc. 
  805: # For uploaded HTML files this would be images etc.
  806: # paths will be in the form $count/res/$file, and urls in the $content will be rewritten with the new paths. 
  807:     return;
  808: }
  809: 
  810: sub store_template {
  811:     my ($contents,$tempexport,$count,$content_type) = @_;
  812:     if ($contents) {
  813:         if ($tempexport) {
  814:             if (!-e $tempexport.'/resources') {
  815:                 mkdir($tempexport.'/resources',0700);
  816:             }
  817:             if (!-e $tempexport.'/resources/'.$count) {
  818:                 mkdir($tempexport.'/resources/'.$count,0700);
  819:             }
  820:             my $destination = $tempexport.'/resources/'.$count.'/'.$content_type.'.xml';
  821:             my $storetemplate;
  822:             if ($storetemplate = Apache::File->new('>'.$destination)) {
  823:                 print $storetemplate $contents;
  824:                 close($storetemplate);
  825:             }
  826:             if ($content_type eq 'external') {
  827:                 return $count.'/'.$content_type.'.html';
  828:             } else {
  829:                 return $count.'/'.$content_type.'.xml';
  830:             }
  831:         }
  832:     }
  833: }
  834: 
  835: # Imports the given (name, url) resources into the course
  836: # coursenum, coursedom, and folder must precede the list
  837: sub group_import {
  838:     my $coursenum = shift;
  839:     my $coursedom = shift;
  840:     my $folder = shift;
  841:     my $container = shift;
  842:     my $caller = shift;
  843:     while (@_) {
  844: 	my $name = shift;
  845: 	my $url = shift;
  846:         if (($url =~ m#^/uploaded/$coursedom/$coursenum/(default_\d+\.)(page|sequence)$#) && ($caller eq 'londocs')) {
  847:             my $errtext = '';
  848:             my $fatal = 0;
  849:             my $newmapstr = '<map>'."\n".
  850:                             '<resource id="1" src="" type="start"></resource>'."\n".
  851:                             '<link from="1" to="2" index="1"></link>'."\n".
  852:                             '<resource id="2" src="" type="finish"></resource>'."\n".
  853:                             '</map>';
  854:             $ENV{'form.output'}=$newmapstr;
  855:             my $home=&Apache::lonnet::homeserver($coursenum,$coursedom);
  856:             my $result=&Apache::lonnet::finishuserfileupload($coursenum,$coursedom,$home,
  857:                                                 'output',$1.$2);
  858:             if ($result != m|^/uploaded/|) {
  859:                 $errtext.='Map not saved: A network error occured when trying to save the new map. ';
  860:                 $fatal = 2;
  861:             }
  862:             if ($fatal) {
  863:                 return ($errtext,$fatal);
  864:             }
  865:         }
  866: 	if ($url) {
  867: 	    my $idx = $#Apache::lonratedt::resources + 1;
  868: 	    $Apache::lonratedt::order[$#Apache::lonratedt::order+1]=$idx;
  869: 	    my $ext = 'false';
  870: 	    if ($url=~/^http:\/\//) { $ext = 'true'; }
  871: 	    $url =~ s/:/\&colon;/g;
  872: 	    $name =~ s/:/\&colon;/g;
  873: 	    $Apache::lonratedt::resources[$idx] = 
  874: 		join ':', ($name, $url, $ext, 'normal', 'res');
  875: 	}
  876:     }
  877:     return &storemap($coursenum, $coursedom, $folder.'.'.$container);
  878: }
  879: 
  880: sub breadcrumbs {
  881:     my ($where)=@_;
  882:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  883:     my (@folders);
  884:     if ($ENV{'form.pagepath'}) {
  885:         @folders = split('&',$ENV{'form.pagepath'});
  886:     } else {
  887:         @folders=split('&',$ENV{'form.folderpath'});
  888:     }
  889:     my $folderpath;
  890:     while (@folders) {
  891: 	my $folder=shift(@folders);
  892: 	my $foldername=shift(@folders);
  893: 	if ($folderpath) {$folderpath.='&';}
  894: 	$folderpath.=$folder.'&'.$foldername;
  895: 	my $url='/adm/coursedocs?folderpath='.
  896: 	    &Apache::lonnet::escape($folderpath);
  897: 	    &Apache::lonhtmlcommon::add_breadcrumb(
  898: 		      {'href'=>$url,
  899: 		       'title'=>&Apache::lonnet::unescape($foldername),
  900: 		       'text'=>'<font size="+1">'.
  901: 			   &Apache::lonnet::unescape($foldername).'</font>'
  902: 		       });
  903: 		       
  904: 						 
  905:     }
  906:     return &Apache::lonhtmlcommon::breadcrumbs(undef,undef,undef,undef,undef,
  907: 					       0,'nohelp');
  908: }
  909: 
  910: sub editor {
  911:     my ($r,$coursenum,$coursedom,$folder,$allowed)=@_;
  912: 
  913:     $r->print(&breadcrumbs($folder));
  914:     my $errtext='';
  915:     my $fatal=0;
  916:     my $container='sequence';
  917:     if ($ENV{'form.pagepath'}) {
  918:         $container='page';
  919:     }
  920:     ($errtext,$fatal)=
  921:               &mapread($coursenum,$coursedom,$folder.'.'.$container);
  922:     if ($#Apache::lonratedt::order<1) {
  923:        	$Apache::lonratedt::order[0]=1;
  924:         $Apache::lonratedt::resources[1]='';
  925:     }
  926:     if ($fatal) {
  927: 	   $r->print('<p><font color="red">'.$errtext.'</font></p>');
  928:     } else {
  929: # ------------------------------------------------------------ Process commands
  930: 
  931: # ---------------- if they are for this folder and user allowed to make changes
  932: 	if (($allowed) && ($ENV{'form.folder'} eq $folder)) {
  933: # set parameters and change order
  934: 	    if (defined($ENV{'form.setparms'})) {
  935: 		my $idx=$ENV{'form.setparms'};
  936: # set parameters
  937: 		if ($ENV{'form.randpick_'.$idx}) {
  938: 		    &Apache::lonratedt::storeparameter($idx,'parameter_randompick',$ENV{'form.randpick_'.$idx},'int_pos');
  939: 		} else {
  940: 		    &Apache::lonratedt::delparameter($idx,'parameter_randompick');
  941: 		}
  942: 		if ($ENV{'form.hidprs_'.$idx}) {
  943: 		    &Apache::lonratedt::storeparameter($idx,'parameter_hiddenresource','yes','string_yesno');
  944: 		} else {
  945: 		    &Apache::lonratedt::delparameter($idx,'parameter_hiddenresource');
  946: 		}
  947: 		if ($ENV{'form.encprs_'.$idx}) {
  948: 		    &Apache::lonratedt::storeparameter($idx,'parameter_encrypturl','yes','string_yesno');
  949: 		} else {
  950: 		    &Apache::lonratedt::delparameter($idx,'parameter_encrypturl');
  951: 		}
  952: 
  953: 		if ($ENV{'form.newpos'}) {
  954: # change order
  955: 
  956: 		    my $newpos=$ENV{'form.newpos'}-1;
  957: 		    my $currentpos=$ENV{'form.currentpos'}-1;
  958: 		    my $i;
  959: 		    my @neworder=();
  960: 		    if ($newpos>$currentpos) {
  961: # moving stuff up
  962: 			for ($i=0;$i<$currentpos;$i++) {
  963: 			    $neworder[$i]=$Apache::lonratedt::order[$i];
  964: 			}
  965: 			for ($i=$currentpos;$i<$newpos;$i++) {
  966: 			    $neworder[$i]=$Apache::lonratedt::order[$i+1];
  967: 			}
  968:                         $neworder[$newpos]=$Apache::lonratedt::order[$currentpos];
  969: 			for ($i=$newpos+1;$i<=$#Apache::lonratedt::order;$i++) {
  970: 			    $neworder[$i]=$Apache::lonratedt::order[$i];
  971: 			}
  972: 		    } else {
  973: # moving stuff down
  974: 			for ($i=0;$i<$newpos;$i++) {
  975: 			    $neworder[$i]=$Apache::lonratedt::order[$i];
  976: 			}
  977: 			$neworder[$newpos]=$Apache::lonratedt::order[$currentpos];
  978: 			for ($i=$newpos+1;$i<$currentpos+1;$i++) {
  979: 			    $neworder[$i]=$Apache::lonratedt::order[$i-1];
  980: 			}
  981: 			for ($i=$currentpos+1;$i<=$#Apache::lonratedt::order;$i++) {
  982: 			    $neworder[$i]=$Apache::lonratedt::order[$i];
  983: 			}
  984: 		    }
  985: 		    @Apache::lonratedt::order=@neworder;
  986: 		}
  987: # store the changed version
  988: 
  989: 		($errtext,$fatal)=&storemap($coursenum,$coursedom,$folder.'.'.$container);
  990: 		if ($fatal) {
  991: 		    $r->print('<p><font color="red">'.$errtext.'</font></p>');
  992: 		    return;
  993: 		}
  994: 		
  995: 	    }
  996: 
  997: # upload a file, if present
  998:            if (($ENV{'form.uploaddoc.filename'}) &&
  999:                ($ENV{'form.cmd'}=~/^upload_(\w+)/)) {
 1000: 	    if ( ($folder=~/^$1/) || ($1 eq 'default') ) {
 1001: # this is for a course, not a user, so set coursedoc flag
 1002: # probably the only place in the system where this should be "1"
 1003: 	      my $url=&Apache::lonnet::userfileupload('uploaddoc',1,'docs');
 1004:               my $ext='false';
 1005:               if ($url=~/^http\:\/\//) { $ext='true'; }
 1006:               $url=~s/\:/\&colon;/g;
 1007: 	      my $comment=$ENV{'form.comment'};
 1008:               $comment=~s/\</\&lt\;/g;
 1009:               $comment=~s/\>/\&gt\;/g;
 1010:               $comment=~s/\:/\&colon;/g;
 1011:               if ($folder=~/^supplemental/) {
 1012: 		  $comment=time.'___&&&___'.$ENV{'user.name'}.'___&&&___'.
 1013: 		      $ENV{'user.domain'}.'___&&&___'.$comment;
 1014:               }
 1015:               my $newidx=$#Apache::lonratedt::resources+1;
 1016:               $Apache::lonratedt::resources[$newidx]=
 1017:                   $comment.':'.$url.':'.$ext.':normal:res';
 1018:               $Apache::lonratedt::order[$#Apache::lonratedt::order+1]=
 1019:                                                               $newidx;       
 1020: 
 1021: 	      ($errtext,$fatal)=&storemap($coursenum,$coursedom,$folder.'.'.$container);
 1022: 	      if ($fatal) {
 1023: 		  $r->print('<p><font color="red">'.$errtext.'</font></p>');
 1024: 		  return;
 1025: 	      }
 1026: 	     }
 1027:             }
 1028: 	    if ($ENV{'form.cmd'}) {
 1029:                 my ($cmd,$idx)=split(/\_/,$ENV{'form.cmd'});
 1030:                 if ($cmd eq 'del') {
 1031: 		    my (undef,$url)=split(':',$Apache::lonratedt::resources[$Apache::lonratedt::order[$idx]]);
 1032: 		    if ($url=~m|/+uploaded/\Q$coursedom\E/\Q$coursenum\E/|) {
 1033: 			&Apache::lonnet::removeuploadedurl($url);
 1034: 		    }
 1035: 		    for (my $i=$idx;$i<$#Apache::lonratedt::order;$i++) {
 1036:                         $Apache::lonratedt::order[$i]=
 1037:                           $Apache::lonratedt::order[$i+1];
 1038:                     }
 1039:                     $#Apache::lonratedt::order--;
 1040:                 } elsif ($cmd eq 'up') {
 1041: 		  if (($idx) && (defined($Apache::lonratedt::order[$idx-1]))) {
 1042:                     my $i=$Apache::lonratedt::order[$idx-1];
 1043:                     $Apache::lonratedt::order[$idx-1]=
 1044: 			$Apache::lonratedt::order[$idx];
 1045:                     $Apache::lonratedt::order[$idx]=$i;
 1046: 		   }
 1047:                 } elsif ($cmd eq 'down') {
 1048: 		   if (defined($Apache::lonratedt::order[$idx+1])) {
 1049:                     my $i=$Apache::lonratedt::order[$idx+1];
 1050:                     $Apache::lonratedt::order[$idx+1]=
 1051: 			$Apache::lonratedt::order[$idx];
 1052:                     $Apache::lonratedt::order[$idx]=$i;
 1053: 		   }
 1054:                 } elsif ($cmd eq 'rename') {
 1055:                     my $ratstr = $Apache::lonratedt::resources[$Apache::lonratedt::order[$idx]];
 1056:                     my ($rtitle,@rrest)=split(/\:/,
 1057:                        $Apache::lonratedt::resources[
 1058: 				       $Apache::lonratedt::order[$idx]]);
 1059:                     my $comment=
 1060:                      &HTML::Entities::decode($ENV{'form.title'});
 1061:                     $comment=~s/\</\&lt\;/g;
 1062:                     $comment=~s/\>/\&gt\;/g;
 1063:                     $comment=~s/\:/\&colon;/g;
 1064: 		    if ($comment=~/\S/) {
 1065: 			$Apache::lonratedt::resources[
 1066: 				       $Apache::lonratedt::order[$idx]]=
 1067: 				            $comment.':'.join(':',@rrest);
 1068: 		    }
 1069:                 }
 1070: # Store the changed version
 1071: 		($errtext,$fatal)=&storemap($coursenum,$coursedom,
 1072: 					    $folder.'.'.$container);
 1073: 		if ($fatal) {
 1074: 		    $r->print('<p><font color="red">'.$errtext.'</font></p>');
 1075: 		    return;
 1076: 		}
 1077:             }
 1078: # Group import/search
 1079: 	    if ($ENV{'form.importdetail'}) {
 1080: 		my @imports;
 1081: 		foreach (split(/\&/,$ENV{'form.importdetail'})) {
 1082: 		    if (defined($_)) {
 1083: 			my ($name,$url)=split(/\=/,$_);
 1084: 			$name=&Apache::lonnet::unescape($name);
 1085: 			$url=&Apache::lonnet::unescape($url);
 1086: 			push @imports, $name, $url;
 1087: 		    }
 1088: 		}
 1089: # Store the changed version
 1090: 		($errtext,$fatal)=group_import($coursenum, $coursedom, $folder,
 1091: 					       $container,'londocs',@imports);
 1092: 		if ($fatal) {
 1093: 		    $r->print('<p><font color="red">'.$errtext.'</font></p>');
 1094: 		    return;
 1095: 		}
 1096:             }
 1097: # Loading a complete map
 1098: 	   if (($ENV{'form.importmap'}) && ($ENV{'form.loadmap'})) {
 1099: 	       foreach (&Apache::lonsequence::attemptread(&Apache::lonnet::filelocation('',$ENV{'form.importmap'}))) {
 1100:                    my $idx=$#Apache::lonratedt::resources;
 1101:                    $idx++;
 1102:                    $Apache::lonratedt::resources[$idx]=$_;
 1103:                    $Apache::lonratedt::order
 1104: 		       [$#Apache::lonratedt::order+1]=$idx;
 1105: 	       }
 1106: 
 1107: # Store the changed version
 1108: 	       ($errtext,$fatal)=&storemap($coursenum,$coursedom,
 1109: 					   $folder.'.'.$container);
 1110: 	       if ($fatal) {
 1111: 		   $r->print('<p><font color="red">'.$errtext.'</font></p>');
 1112: 		   return;
 1113: 	       }
 1114:            }
 1115:        }
 1116: # ---------------------------------------------------------------- End commands
 1117: # ---------------------------------------------------------------- Print screen
 1118:         my $idx=0;
 1119: 	my $shown=0;
 1120:         $r->print('<table>');
 1121:         foreach (@Apache::lonratedt::order) {
 1122:            my ($name,$url)=split(/\:/,$Apache::lonratedt::resources[$_]);
 1123:            unless ($name) {  $name=(split(/\//,$url))[-1]; }
 1124:            unless ($name) { $idx++; next; }
 1125:            $r->print(&entryline($idx,$name,$url,$folder,$allowed,$_,$coursenum));
 1126:            $idx++;
 1127: 	   $shown++;
 1128:         }
 1129: 	unless ($shown) {
 1130: 	    $r->print('<tr><td>'.&mt('Currently no documents.').'</td></tr>');
 1131: 	}
 1132:         $r->print('</table>');
 1133:     }
 1134: }
 1135: 
 1136: # --------------------------------------------------------------- An entry line
 1137: 
 1138: sub entryline {
 1139:     my ($index,$title,$url,$folder,$allowed,$residx,$coursenum)=@_;
 1140:     $title=~s/\&colon\;/\:/g;
 1141:     $title=&HTML::Entities::encode(&HTML::Entities::decode(
 1142:      &Apache::lonnet::unescape($title)),'"<>&\'');
 1143:     my $renametitle=$title;
 1144:     my $foldertitle=$title;
 1145:     my $pagetitle=$title;
 1146:     my $orderidx=$Apache::lonratedt::order[$index];
 1147:     if ($title=~ /^(\d+)___&amp;&amp;&amp;___(\w+)___&amp;&amp;&amp;___(\w+)___&amp;&amp;&amp;___(.*)$/	) { 
 1148: 	$foldertitle=&Apache::lontexconvert::msgtexconverted($4);
 1149: 	$renametitle=$4;
 1150: 	$title='<i>'.&Apache::lonlocal::locallocaltime($1).'</i> '.
 1151: 	    &Apache::loncommon::plainname($2,$3).': <br />'.
 1152: 	    $foldertitle;
 1153:     }
 1154:     $renametitle=~s/\&quot\;/\\\"/g;
 1155:     my $line='<tr>';
 1156: # Edit commands
 1157:     my $container;
 1158:     my $folderpath;
 1159:     if ($ENV{'form.folderpath'}) {
 1160:         $container = 'sequence';
 1161: 	$folderpath=&Apache::lonnet::escape($ENV{'form.folderpath'});
 1162: 	# $htmlfoldername=&HTML::Entities::encode($ENV{'form.foldername'},'<>&"');
 1163:     }
 1164:     my ($pagepath,$pagesymb);
 1165:     if ($ENV{'form.pagepath'}) {
 1166:         $container = 'page';
 1167:         $pagepath=&Apache::lonnet::escape($ENV{'form.pagepath'});
 1168:         $pagesymb=&Apache::lonnet::escape($ENV{'form.pagesymb'});
 1169:     }
 1170:     if ($allowed) {
 1171: 	my $incindex=$index+1;
 1172: 	my $selectbox='';
 1173: 	if ($folder!~/^supplemental/) {
 1174: 	    $selectbox=
 1175: 		'<input type="hidden" name="currentpos" value="'.$incindex.'" />'.
 1176: 		'<select name="newpos" onChange="this.form.submit()">';
 1177: 	    for (my $i=1;$i<=$#Apache::lonratedt::order+1;$i++) {
 1178: 		if ($i==$incindex) {
 1179: 		    $selectbox.='<option value="" selected="1">('.$i.')</option>';
 1180: 		} else {
 1181: 		    $selectbox.='<option value="'.$i.'">'.$i.'</option>';
 1182: 		}
 1183: 	    }
 1184: 	    $selectbox.='</select>';
 1185: 	}
 1186: 	my %lt=&Apache::lonlocal::texthash(
 1187:                 'up' => 'Move Up',
 1188: 		'dw' => 'Move Down',
 1189: 		'rm' => 'Remove',
 1190: 		'rn' => 'Rename');
 1191:         if ($ENV{'form.pagepath'}) {
 1192:             $line.=(<<END);
 1193: <form name="entry_$index" action="/adm/coursedocs" method="post">
 1194: <input type="hidden" name="pagepath" value="$ENV{'form.pagepath'}" />
 1195: <input type="hidden" name="pagesymb" value="$ENV{'form.pagesymb'}" />
 1196: <input type="hidden" name="setparms" value="$orderidx" />
 1197: <td><table border='0' cellspacing='2' cellpadding='0'>
 1198: <tr><td bgcolor="#DDDDDD">
 1199: <a href='/adm/coursedocs?cmd=up_$index&pagepath=$pagepath&pagesymb=$pagesymb'>
 1200: <img src="${iconpath}move_up.gif" alt='$lt{'up'}' border='0' /></a></td></tr>
 1201: <tr><td bgcolor="#DDDDDD">
 1202: <a href='/adm/coursedocs?cmd=down_$index&pagepath=$pagepath&pagesymb=$pagesymb'>
 1203: <img src="${iconpath}move_down.gif" alt='$lt{'dw'}' border='0' /></a></td></tr>
 1204: </table></td>
 1205: <td>$selectbox
 1206: </td><td bgcolor="#DDDDDD">
 1207: <a href='javascript:removeres("$pagepath","$index","$renametitle","page","$pagesymb");'>
 1208: <font size="-2" color="#990000">$lt{'rm'}</font></a>
 1209: <a href='javascript:changename("$pagepath","$index","$renametitle","page","$pagesymb");'>
 1210: <font size="-2" color="#009900">$lt{'rn'}</font></a></td>
 1211: END
 1212:         } else {
 1213:             $line.=(<<END); 
 1214: <form name="entry_$index" action="/adm/coursedocs" method="post">
 1215: <input type="hidden" name="folderpath" value="$ENV{'form.folderpath'}" />
 1216: <input type="hidden" name="setparms" value="$orderidx" />
 1217: <td><table border='0' cellspacing='2' cellpadding='0'>
 1218: <tr><td bgcolor="#DDDDDD">
 1219: <a href='/adm/coursedocs?cmd=up_$index&folderpath=$folderpath'>
 1220: <img src="${iconpath}move_up.gif" alt='$lt{'up'}' border='0' /></a></td></tr>
 1221: <tr><td bgcolor="#DDDDDD">
 1222: <a href='/adm/coursedocs?cmd=down_$index&folderpath=$folderpath'>
 1223: <img src="${iconpath}move_down.gif" alt='$lt{'dw'}' border='0' /></a></td></tr>
 1224: </table></td>
 1225: <td>$selectbox
 1226: </td><td bgcolor="#DDDDDD">
 1227: <a href='javascript:removeres("$folderpath","$index","$renametitle","sequence");'>
 1228: <font size="-2" color="#990000">$lt{'rm'}</font></a>
 1229: <a href='javascript:changename("$folderpath","$index","$renametitle","sequence");'>
 1230: <font size="-2" color="#009900">$lt{'rn'}</font></a></td>
 1231: END
 1232:         }
 1233:     }
 1234: # Figure out what kind of a resource this is
 1235:     my ($extension)=($url=~/\.(\w+)$/);
 1236:     my $uploaded=($url=~/^\/*uploaded\//);
 1237:     my $icon=&Apache::loncommon::icon($url);
 1238:     my $isfolder=0;
 1239:     my $ispage=0;
 1240:     my $folderarg;
 1241:     my $pagearg;
 1242:     my $pagefile;
 1243:     if ($uploaded) {
 1244: 	if ($extension eq 'sequence') {
 1245: 	    $icon=$iconpath.'/folder_closed.gif';
 1246: 	    $url=~/$coursenum\/([\/\w]+)\.sequence$/;
 1247: 	    $url='/adm/coursedocs?';
 1248: 	    $folderarg=$1;
 1249: 	    $isfolder=1;
 1250:         } elsif ($extension eq 'page') {
 1251:             $icon=$iconpath.'/page.gif';
 1252:             $url=~/$coursenum\/([\/\w]+)\.page$/;
 1253:             $pagearg=$1;
 1254:             $url='/adm/coursedocs?';
 1255:             $ispage=1;
 1256: 	} else {
 1257: 	    &Apache::lonnet::allowuploaded('/adm/coursedoc',$url);
 1258: 	}
 1259:     }
 1260:     $url=~s/^http\&colon\;\/\//\/adm\/wrapper\/ext\//;
 1261:     if ((!$isfolder) && ($residx) && ($folder!~/supplemental/) && (!$ispage)) {
 1262: 	my $symb=&Apache::lonnet::symbclean(
 1263:           &Apache::lonnet::declutter('uploaded/'.
 1264:            $ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.'/'.
 1265:            $ENV{'course.'.$ENV{'request.course.id'}.'.num'}.'/'.$folder.
 1266:            '.sequence').
 1267:            '___'.$residx.'___'.
 1268: 	   &Apache::lonnet::declutter($url));
 1269: 	(undef,undef,$url)=&Apache::lonnet::decode_symb($symb);
 1270: 	$url=&Apache::lonnet::clutter($url);
 1271: 	if ($url=~/^\/*uploaded\//) {
 1272: 	    $url=~/\.(\w+)$/;
 1273: 	    my $embstyle=&Apache::loncommon::fileembstyle($1);
 1274: 	    if (($embstyle eq 'img') || ($embstyle eq 'emb')) {
 1275: 		$url='/adm/wrapper'.$url;
 1276: 	    } elsif ($embstyle eq 'ssi') {
 1277: 		#do nothing with these
 1278: 	    } elsif ($url!~/\.(sequence|page)$/) {
 1279: 		$url='/adm/coursedocs/showdoc'.$url;
 1280: 	    }
 1281: 	} elsif ($url=~m|^/ext/|) { 
 1282: 	    $url='/adm/wrapper'.$url;
 1283: 	}
 1284: 	$url.=(($url=~/\?/)?'&':'?').'symb='.&Apache::lonnet::escape($symb);
 1285: 	if ($container eq 'page') {
 1286: 	    my $symb=$ENV{'form.pagesymb'};
 1287: 	    	    
 1288: 	    $url=&Apache::lonnet::clutter((&Apache::lonnet::decode_symb($symb))[2]);
 1289: 	    $url.=(($url=~/\?/)?'&':'?').'symb='.&Apache::lonnet::escape($symb);
 1290: 	}
 1291:     }
 1292:     my $parameterset='&nbsp;';
 1293:     if ($isfolder) {
 1294: 	my $foldername=&Apache::lonnet::escape($foldertitle);
 1295: 	my $folderpath=$ENV{'form.folderpath'};
 1296: 	if ($folderpath) { $folderpath.='&' };
 1297: 	$folderpath.=$folderarg.'&'.$foldername;
 1298: 	$url.='folderpath='.&Apache::lonnet::escape($folderpath);
 1299: 	$parameterset='<label>'.&mt('Randomly Pick: ').
 1300: 	    '<input type="text" size="4" name="randpick_'.$orderidx.'" value="'.
 1301: 	    (&Apache::lonratedt::getparameter($orderidx,
 1302:                                               'parameter_randompick'))[0].
 1303:                                               '" />'.'</label>';
 1304:        
 1305:     }
 1306:     if ($ispage) {
 1307:         my $pagename=&Apache::lonnet::escape($pagetitle);
 1308:         my $pagepath;
 1309:         my $folderpath=$ENV{'form.folderpath'};
 1310:         if ($folderpath) { $pagepath = $folderpath.'&' };
 1311:         $pagepath.=$pagearg.'&'.$pagename;
 1312: 	my $symb=$ENV{'form.pagesymb'};
 1313: 	if (!$symb) {
 1314: 	    my $path='uploaded/'.
 1315: 		$ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.'/'.
 1316: 		$ENV{'course.'.$ENV{'request.course.id'}.'.num'}.'/';
 1317: 	    $symb=&Apache::lonnet::encode_symb($path.$folder.'.sequence',
 1318: 					       $residx,
 1319: 					       $path.$pagearg.'.page');
 1320: 	}
 1321: 	$url.='pagepath='.&Apache::lonnet::escape($pagepath).
 1322: 	    '&pagesymb='.&Apache::lonnet::escape($symb);
 1323:     }
 1324:     $line.='<td bgcolor="#FFFFBB"><a href="'.$url.'"><img src="'.$icon.
 1325: 	'" border="0"></a></td>'.
 1326:         "<td bgcolor='#FFFFBB'><a href='$url'>$title</a></td>";
 1327:     if (($allowed) && ($folder!~/^supplemental/)) {
 1328:  	my %lt=&Apache::lonlocal::texthash(
 1329:  			      'hd' => 'Hidden',
 1330:  			      'ec' => 'URL hidden',
 1331:  			      'sp' => 'Store Parameters');
 1332: 	my $enctext=
 1333: 	    ((&Apache::lonratedt::getparameter($orderidx,'parameter_encrypturl'))[0]=~/^yes$/i?' checked="1"':'');
 1334: 	my $hidtext=
 1335: 	    ((&Apache::lonratedt::getparameter($orderidx,'parameter_hiddenresource'))[0]=~/^yes$/i?' checked="1"':'');
 1336: 	$line.=(<<ENDPARMS);
 1337: <td bgcolor="#BBBBFF"><font size='-2'>
 1338: <nobr><label><input type="checkbox" name="hidprs_$orderidx" $hidtext/> $lt{'hd'}</label></nobr></td>
 1339: <td bgcolor="#BBBBFF"><font size='-2'>
 1340: <nobr><label><input type="checkbox" name="encprs_$orderidx" $enctext/> $lt{'ec'}</label></nobr></td>
 1341: <td bgcolor="#BBBBFF"><font size="-2">$parameterset</font></td>
 1342: <td bgcolor="#BBBBFF"><font size='-2'>
 1343: <input type="submit" value="$lt{'sp'}" />
 1344: </font></td>
 1345: ENDPARMS
 1346:     }
 1347:     $line.="</form></tr>";
 1348:     return $line;
 1349: }
 1350: 
 1351: # ---------------------------------------------------------------- tie the hash
 1352: 
 1353: sub tiehash {
 1354:     my ($mode)=@_;
 1355:     $hashtied=0;
 1356:     if ($ENV{'request.course.fn'}) {
 1357: 	if ($mode eq 'write') {
 1358: 	    if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.".db",
 1359: 		    &GDBM_WRCREAT(),0640)) {
 1360:                 $hashtied=2;
 1361: 	    }
 1362: 	} else {
 1363: 	    if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.".db",
 1364: 		    &GDBM_READER(),0640)) {
 1365:                 $hashtied=1;
 1366: 	    }
 1367: 	}
 1368:     }    
 1369: }
 1370: 
 1371: sub untiehash {
 1372:     if ($hashtied) { untie %hash; }
 1373:     $hashtied=0;
 1374: }
 1375: 
 1376: # --------------------------------------------------------------- check on this
 1377: 
 1378: sub checkonthis {
 1379:     my ($r,$url,$level,$title)=@_;
 1380:     $url=&Apache::lonnet::unescape($url);
 1381:     $alreadyseen{$url}=1;
 1382:     $r->rflush();
 1383:     if (($url) && ($url!~/^\/uploaded\//) && ($url!~/\*$/)) {
 1384:        $r->print("\n<br />");
 1385:        for (my $i=0;$i<=$level*5;$i++) {
 1386:            $r->print('&nbsp;');
 1387:        }
 1388:        $r->print('<a href="'.$url.'" target="cat">'.
 1389: 		 ($title?$title:$url).'</a> ');
 1390:        if ($url=~/^\/res\//) {
 1391: 	  my $result=&Apache::lonnet::repcopy(
 1392:                               &Apache::lonnet::filelocation('',$url));
 1393:           if ($result==OK) {
 1394:              $r->print('<font color="green">'.&mt('ok').'</font>');
 1395:              $r->rflush();
 1396:              &Apache::lonnet::countacc($url);
 1397:              $url=~/\.(\w+)$/;
 1398:              if (&Apache::loncommon::fileembstyle($1) eq 'ssi') {
 1399: 		 $r->print('<br />');
 1400:                  $r->rflush();
 1401:                  for (my $i=0;$i<=$level*5;$i++) {
 1402:                      $r->print('&nbsp;');
 1403:                  }
 1404:                  $r->print('- '.&mt('Rendering').': ');
 1405:                  my $oldpath=$ENV{'request.filename'};
 1406:                  $ENV{'request.filename'}=&Apache::lonnet::filelocation('',$url);
 1407:                  &Apache::lonxml::xmlparse($r,'web',
 1408:                    &Apache::lonnet::getfile(
 1409:                     &Apache::lonnet::filelocation('',$url)));
 1410: 		 undef($Apache::lonhomework::parsing_a_problem);
 1411: 		 $ENV{'request.filename'}=$oldpath;
 1412:                  if (($Apache::lonxml::errorcount) ||
 1413:                      ($Apache::lonxml::warningcount)) {
 1414: 		     if ($Apache::lonxml::errorcount) {
 1415:                         $r->print('<img src="/adm/lonMisc/bomb.gif" /><font color="red"><b>'.
 1416: 			  $Apache::lonxml::errorcount.' '.
 1417: 				  &mt('error(s)').'</b></font> ');
 1418:                      }
 1419: 		     if ($Apache::lonxml::warningcount) {
 1420:                         $r->print('<font color="blue">'.
 1421: 			  $Apache::lonxml::warningcount.' '.
 1422: 				  &mt('warning(s)').'</font>');
 1423:                      }
 1424:                  } else {
 1425:                      $r->print('<font color="green">'.&mt('ok').'</font>');
 1426:                  }
 1427:                  $r->rflush();
 1428:              }
 1429: 	     my $dependencies=
 1430:                 &Apache::lonnet::metadata($url,'dependencies');
 1431:              foreach (split(/\,/,$dependencies)) {
 1432: 		 if (($_=~/^\/res\//) && (!$alreadyseen{$_})) {
 1433:                     &checkonthis($r,$_,$level+1);
 1434:                  }
 1435:              }
 1436:           } elsif ($result==HTTP_SERVICE_UNAVAILABLE) {
 1437:              $r->print('<font color="red"><b>'.&mt('connection down').'</b></font>');
 1438:           } elsif ($result==HTTP_NOT_FOUND) {
 1439: 	      unless ($url=~/\$/) {
 1440: 		  $r->print('<font color="red"><b>'.&mt('not found').'</b></font>');
 1441: 	      } else {
 1442: 		  $r->print('<font color="yellow"><b>'.&mt('unable to verify variable URL').'</b></font>');
 1443: 	      }
 1444:           } else {
 1445:              $r->print('<font color="red"><b>'.&mt('access denied').'</b></font>');
 1446:           }
 1447:       }
 1448:    }
 1449: }
 1450: 
 1451: 
 1452: #
 1453: # -------------------------------------------------------------- Verify Content
 1454: # 
 1455: sub verifycontent {
 1456:    my $r=shift; 
 1457:    my $loaderror=&Apache::lonnet::overloaderror($r);
 1458:    if ($loaderror) { return $loaderror; }
 1459: 
 1460:    $r->print('<html><head><title>Verify Content</title></head>'.
 1461:               &Apache::loncommon::bodytag('Verify Course Documents'));
 1462:    $hashtied=0;
 1463:    undef %alreadyseen;
 1464:    %alreadyseen=();
 1465:    &tiehash();
 1466:    foreach (keys %hash) {
 1467:        if ($hash{$_}=~/\.(page|sequence)$/) {
 1468: 	   if (($_=~/^src_/) && ($alreadyseen{&Apache::lonnet::unescape($hash{$_})})) {
 1469: 	       $r->print('<hr /><font color="red">'.
 1470: 			 &mt('The following sequence or page is included more than once in your course: ').
 1471: 			 &Apache::lonnet::unescape($hash{$_}).'</font><br />'.
 1472: 			 &mt('Note that grading records for problems included in this sequence or folder will overlap.<hr />'));
 1473: 	   }
 1474:        }
 1475:        if (($_=~/^src\_(.+)$/) && (!$alreadyseen{&Apache::lonnet::unescape($hash{$_})})) {
 1476:            &checkonthis($r,$hash{$_},0,$hash{'title_'.$1});
 1477:        }
 1478:    }
 1479:    &untiehash();
 1480:    $r->print('<h1>'.&mt('Done').'.</h1>'.'<a href="/adm/coursedocs">'.
 1481: 	     &mt('Return to DOCS').'</a>');
 1482: }
 1483: 
 1484: # -------------------------------------------------------------- Check Versions
 1485: 
 1486: sub checkversions {
 1487:     my $r=shift;
 1488:     $r->print('<html><head><title>Check Versions</title></head>'.
 1489:               &Apache::loncommon::bodytag('Check Course Document Versions'));
 1490:     my $header='';
 1491:     my $startsel='';
 1492:     my $monthsel='';
 1493:     my $weeksel='';
 1494:     my $daysel='';
 1495:     my $allsel='';
 1496:     my %changes=();
 1497:     my $starttime=0;
 1498:     my $haschanged=0;
 1499:     my %setversions=&Apache::lonnet::dump('resourceversions',
 1500: 			  $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 1501: 			  $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 1502: 
 1503:     $hashtied=0;
 1504:     &tiehash();
 1505:     my %newsetversions=();
 1506:     if ($ENV{'form.setmostrecent'}) {
 1507: 	$haschanged=1;
 1508: 	foreach (keys %hash) {
 1509: 	    if ($_=~/^ids\_(\/res\/.+)$/) {
 1510: 		$newsetversions{$1}='mostrecent';
 1511: 	    }
 1512: 	}
 1513:     } elsif ($ENV{'form.setcurrent'}) {
 1514: 	$haschanged=1;
 1515: 	foreach (keys %hash) {
 1516: 	    if ($_=~/^ids\_(\/res\/.+)$/) {
 1517: 		my $getvers=&Apache::lonnet::getversion($1);
 1518: 		if ($getvers>0) {
 1519: 		    $newsetversions{$1}=$getvers;
 1520: 		}
 1521: 	    }
 1522: 	}
 1523:     } elsif ($ENV{'form.setversions'}) {
 1524: 	$haschanged=1;
 1525: 	foreach (keys %ENV) {
 1526: 	    if ($_=~/^form\.set_version_(.+)$/) {
 1527: 		my $src=$1;
 1528: 		if (($ENV{$_}) && ($ENV{$_} ne $setversions{$src})) {
 1529: 		    $newsetversions{$src}=$ENV{$_};
 1530: 		}
 1531: 	    }
 1532: 	}
 1533:     }
 1534:     if ($haschanged) {
 1535:         if (&Apache::lonnet::put('resourceversions',\%newsetversions,
 1536: 			  $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 1537: 			  $ENV{'course.'.$ENV{'request.course.id'}.'.num'}) eq 'ok') {		
 1538: 	    $r->print('<h1>'.&mt('Your Version Settings have been Stored').'</h1>');
 1539: 	} else {
 1540: 	    $r->print('<h1><font color="red">'.&mt('An Error Occured while Attempting to Store your Version Settings').'</font></h1>');
 1541: 	}
 1542: 	&mark_hash_old();
 1543:     }
 1544:     &changewarning($r,'');
 1545:     if ($ENV{'form.timerange'} eq 'all') {
 1546: # show all documents
 1547: 	$header=&mt('All Documents in Course');
 1548: 	$allsel=1;
 1549: 	foreach (keys %hash) {
 1550: 	    if ($_=~/^ids\_(\/res\/.+)$/) {
 1551: 		my $src=$1;
 1552: 		$changes{$src}=1;
 1553: 	    }
 1554: 	}
 1555:     } else {
 1556: # show documents which changed
 1557: 	%changes=&Apache::lonnet::dump
 1558: 	 ('versionupdate',$ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 1559:                      $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 1560: 	my $firstkey=(keys %changes)[0];
 1561: 	unless ($firstkey=~/^error\:/) {
 1562: 	    unless ($ENV{'form.timerange'}) {
 1563: 		$ENV{'form.timerange'}=604800;
 1564: 	    }
 1565: 	    my $seltext=&mt('during the last').' '.$ENV{'form.timerange'}.' '
 1566: 		.&mt('seconds');
 1567: 	    if ($ENV{'form.timerange'}==-1) {
 1568: 		$seltext='since start of course';
 1569: 		$startsel='selected';
 1570: 		$ENV{'form.timerange'}=time;
 1571: 	    }
 1572: 	    $starttime=time-$ENV{'form.timerange'};
 1573: 	    if ($ENV{'form.timerange'}==2592000) {
 1574: 		$seltext=&mt('during the last month').' ('.&Apache::lonlocal::locallocaltime($starttime).')';
 1575: 		$monthsel='selected';
 1576: 	    } elsif ($ENV{'form.timerange'}==604800) {
 1577: 		$seltext=&mt('during the last week').' ('.&Apache::lonlocal::locallocaltime($starttime).')';
 1578: 		$weeksel='selected';
 1579: 	    } elsif ($ENV{'form.timerange'}==86400) {
 1580: 		$seltext=&mt('since yesterday').' ('.&Apache::lonlocal::locallocaltime($starttime).')';
 1581: 		$daysel='selected';
 1582: 	    }
 1583: 	    $header=&mt('Content changed').' '.$seltext;
 1584: 	} else {
 1585: 	    $header=&mt('No content modifications yet.');
 1586: 	}
 1587:     }
 1588:     %setversions=&Apache::lonnet::dump('resourceversions',
 1589: 			  $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 1590: 			  $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 1591:     my %lt=&Apache::lonlocal::texthash
 1592: 	      ('st' => 'Version changes since start of Course',
 1593: 	       'lm' => 'Version changes since last Month',
 1594: 	       'lw' => 'Version changes since last Week',
 1595: 	       'sy' => 'Version changes since Yesterday',
 1596:                'al' => 'All Resources (possibly large output)',
 1597: 	       'sd' => 'Display',
 1598: 	       'fi' => 'File',
 1599: 	       'md' => 'Modification Date',
 1600:                'mr' => 'Most recently published Version',
 1601: 	       've' => 'Version used in Course',
 1602:                'vu' => 'Set Version to be used in Course',
 1603: 'sv' => 'Set Versions to be used in Course according to Selections below',
 1604: 'sm' => 'Keep all Resources up-to-date with most recent Versions (default)',
 1605: 'sc' => 'Set all Resource Versions to current Version (Fix Versions)',
 1606: 	       'di' => 'Differences');
 1607:     $r->print(<<ENDHEADERS);
 1608: <form action="/adm/coursedocs" method="post">
 1609: <input type="hidden" name="versions" value="1" />
 1610: <input type="submit" name="setmostrecent" value="$lt{'sm'}" />
 1611: <input type="submit" name="setcurrent" value="$lt{'sc'}" /><hr />
 1612: <select name="timerange">
 1613: <option value='all' $allsel>$lt{'al'}</option>
 1614: <option value="-1" $startsel>$lt{'st'}</option>
 1615: <option value="2592000" $monthsel>$lt{'lm'}</option>
 1616: <option value="604800" $weeksel>$lt{'lw'}</option>
 1617: <option value="86400" $daysel>$lt{'sy'}</option>
 1618: </select>
 1619: <input type="submit" name="display" value="$lt{'sd'}" />
 1620: <h3>$header</h3>
 1621: <input type="submit" name="setversions" value="$lt{'sv'}" />
 1622: <table border="0">
 1623: ENDHEADERS
 1624:     foreach (sort keys %changes) {
 1625: 	if ($changes{$_}>$starttime) {
 1626: 	    my ($root,$extension)=($_=~/^(.*)\.(\w+)$/);
 1627: 	    my $currentversion=&Apache::lonnet::getversion($_);
 1628: 	    if ($currentversion<0) {
 1629: 		$currentversion=&mt('Could not be determined.');
 1630: 	    }
 1631: 	    my $linkurl=&Apache::lonnet::clutter($_);
 1632: 	    $r->print(
 1633: 		      '<tr><td colspan="5"><br /><br /><font size="+1"><b>'.
 1634: 		      &Apache::lonnet::gettitle($linkurl).
 1635:                       '</b></font></td></tr>'.
 1636:                       '<tr><td>&nbsp;&nbsp;&nbsp;</td>'.
 1637:                       '<td colspan="4">'.
 1638:                       '<a href="'.$linkurl.'" target="cat">'.$linkurl.
 1639: 		      '</a></td></tr>'.
 1640:                       '<tr><td></td>'.
 1641:                       '<td title="'.$lt{'md'}.'">'.
 1642: 		      &Apache::lonlocal::locallocaltime(
 1643:                            &Apache::lonnet::metadata($root.'.'.$extension,
 1644:                                                      'lastrevisiondate')
 1645:                                                         ).
 1646:                       '</td>'.
 1647:                       '<td title="'.$lt{'mr'}.'"><nobr>Most Recent: '.
 1648:                       '<font size="+1">'.$currentversion.'</font>'.
 1649:                       '</nobr></td>'.
 1650:                       '<td title="'.$lt{'ve'}.'"><nobr>In Course: '.
 1651:                       '<font size="+1">');
 1652: # Used in course
 1653: 	    my $usedversion=$hash{'version_'.$linkurl};
 1654: 	    if (($usedversion) && ($usedversion ne 'mostrecent')) {
 1655: 		$r->print($usedversion);
 1656: 	    } else {
 1657: 		$r->print($currentversion);
 1658: 	    }
 1659: 	    $r->print('</font></nobr></td><td title="'.$lt{'vu'}.'">'.
 1660:                       '<nobr>Use: ');
 1661: # Set version
 1662: 	    $r->print(&Apache::loncommon::select_form($setversions{$linkurl},
 1663: 						      'set_version_'.$linkurl,
 1664: 						      ('select_form_order' =>
 1665: 						       ['',1..$currentversion,'mostrecent'],
 1666: 						       '' => '',
 1667: 						       'mostrecent' => 'most recent',
 1668: 						       map {$_,$_} (1..$currentversion))));
 1669: 	    $r->print('</nobr></td></tr><tr><td></td>');
 1670: 	    my $lastold=1;
 1671: 	    for (my $prevvers=1;$prevvers<$currentversion;$prevvers++) {
 1672: 		my $url=$root.'.'.$prevvers.'.'.$extension;
 1673: 		if (&Apache::lonnet::metadata($url,'lastrevisiondate')<
 1674: 		    $starttime) {
 1675: 		    $lastold=$prevvers;
 1676: 		}
 1677: 	    }
 1678:             # 
 1679:             # Code to figure out how many version entries should go in
 1680:             # each of the four columns
 1681:             my $entries_per_col = 0;
 1682:             my $num_entries = ($currentversion-$lastold);
 1683:             if ($num_entries % 4 == 0) {
 1684:                 $entries_per_col = $num_entries/4;
 1685:             } else {
 1686:                 $entries_per_col = $num_entries/4 + 1;
 1687:             }
 1688:             my $entries_count = 0;
 1689:             $r->print('<td valign="top"><font size="-2">'); 
 1690:             my $cols_output = 1;
 1691:             for (my $prevvers=$lastold;$prevvers<$currentversion;$prevvers++) {
 1692: 		my $url=$root.'.'.$prevvers.'.'.$extension;
 1693: 		$r->print('<nobr><a href="'.&Apache::lonnet::clutter($url).
 1694: 			  '">'.&mt('Version').' '.$prevvers.'</a> ('.
 1695: 			  &Apache::lonlocal::locallocaltime(
 1696:                                 &Apache::lonnet::metadata($url,
 1697:                                                           'lastrevisiondate')
 1698:                                                             ).
 1699: 			  ')');
 1700: 		if (&Apache::loncommon::fileembstyle($extension) eq 'ssi') {
 1701:                     $r->print(' <a href="/adm/diff?filename='.
 1702: 			      &Apache::lonnet::clutter($root.'.'.$extension).
 1703: 			      '&versionone='.$prevvers.
 1704: 			      '">'.&mt('Diffs').'</a>');
 1705: 		}
 1706: 		$r->print('</nobr><br />');
 1707:                 if (++$entries_count % $entries_per_col == 0) {
 1708:                     $r->print('</font></td>');
 1709:                     if ($cols_output != 4) {
 1710:                         $r->print('<td valign="top"><font size="-2">');
 1711:                         $cols_output++;
 1712:                     }
 1713:                 }
 1714: 	    }
 1715:             while($cols_output++ < 4) {
 1716:                 $r->print('</font></td><td><font>')
 1717:             }
 1718: 	    $r->print('</font></td></tr>'."\n");
 1719: 	}
 1720:     }
 1721:     $r->print('</table></form>');
 1722:     $r->print('<h1>'.&mt('Done').'.</h1>');
 1723: 
 1724:     &untiehash();
 1725: }
 1726: 
 1727: sub mark_hash_old {
 1728:     my $retie_hash=0;
 1729:     if ($hashtied) {
 1730: 	$retie_hash=1;
 1731: 	&untiehash();
 1732:     }
 1733:     &tiehash('write');
 1734:     $hash{'old'}=1;
 1735:     &untiehash();
 1736:     if ($retie_hash) { &tiehash(); }
 1737: }
 1738: 
 1739: sub is_hash_old {
 1740:     my $untie_hash=0;
 1741:     if (!$hashtied) {
 1742: 	$untie_hash=1;
 1743: 	&tiehash();
 1744:     }
 1745:     my $return=$hash{'old'};
 1746:     if ($untie_hash) { &untiehash(); }
 1747:     return $return;
 1748: }
 1749: 
 1750: sub changewarning {
 1751:     my ($r,$postexec)=@_;
 1752:     if (!&is_hash_old()) { return; }
 1753:     my $pathvar='folderpath';
 1754:     my $path=&Apache::lonnet::escape($ENV{'form.folderpath'});
 1755:     if (defined($ENV{'form.pagepath'})) {
 1756: 	$pathvar='pagepath';
 1757: 	$path=&Apache::lonnet::escape($ENV{'form.pagepath'});
 1758: 	$path.='&amp;symb='.&Apache::lonnet::escape($ENV{'form.pagesymb'});
 1759:     }
 1760:     $r->print(
 1761: '<script>function reinit(tf) { tf.submit();'.$postexec.' }</script>'. 
 1762: '<form method="post" action="/adm/roles" target="loncapaclient">'.
 1763: '<input type="hidden" name="orgurl" value="/adm/coursedocs?'.
 1764: $pathvar.'='.$path.
 1765: '" /><input type="hidden" name="selectrole" value="1" /><h3><font color="red">'.
 1766: &mt('Changes will become active for your current session after').
 1767: ' <input type="hidden" name="'.
 1768: $ENV{'request.role'}.'" value="1" /><input type="button" value="'.
 1769: &mt('re-initializing course').'" onClick="reinit(this.form)"/>'.&mt(', or the next time you log in.').
 1770: $help{'Caching'}.'</font></h3></form>');
 1771: }
 1772: 
 1773: # ================================================================ Main Handler
 1774: sub handler {
 1775:     my $r = shift;
 1776:     &Apache::loncommon::content_type($r,'text/html');
 1777:     $r->send_http_header;
 1778:     return OK if $r->header_only;
 1779: 
 1780: # --------------------------------------------- Initialize help topics for this
 1781:   foreach ('Adding_Course_Doc','Main_Course_Documents',
 1782:            'Adding_External_Resource','Navigate_Content',
 1783:            'Adding_Folders','Docs_Overview', 'Load_Map',
 1784:            'Supplemental','Score_Upload_Form','Adding_Pages',
 1785:            'Importing_LON-CAPA_Resource','Uploading_From_Harddrive',
 1786: 	   'Check_Resource_Versions','Verify_Content') {
 1787:       $help{$_}=&Apache::loncommon::help_open_topic('Docs_'.$_);
 1788:   }
 1789:     # Composite help files
 1790:     $help{'Syllabus'} = &Apache::loncommon::help_open_topic(
 1791: 		    'Docs_About_Syllabus,Docs_Editing_Templated_Pages');
 1792:     $help{'Simple Page'} = &Apache::loncommon::help_open_topic(
 1793: 		    'Docs_About_Simple_Page,Docs_Editing_Templated_Pages');
 1794:     $help{'Simple Problem'} = &Apache::loncommon::help_open_topic(
 1795: 		    'Option_Response_Simple');
 1796:     $help{'Bulletin Board'} = &Apache::loncommon::help_open_topic(
 1797: 		    'Docs_About_Bulletin_Board,Docs_Editing_Templated_Pages');
 1798:     $help{'My Personal Info'} = &Apache::loncommon::help_open_topic(
 1799: 		  'Docs_About_My_Personal_Info,Docs_Editing_Templated_Pages');
 1800:     $help{'Caching'} = &Apache::loncommon::help_open_topic('Caching');
 1801: 
 1802:   if ($ENV{'form.verify'}) {
 1803:       &verifycontent($r);
 1804:   } elsif ($ENV{'form.versions'}) {
 1805:       &checkversions($r);
 1806:   } elsif ($ENV{'form.dumpcourse'}) {
 1807:       &dumpcourse($r);
 1808:   } elsif ($ENV{'form.exportcourse'}) {
 1809:       &exportcourse($r);
 1810:   } else {
 1811: # is this a standard course?
 1812: 
 1813:     my $standard=($ENV{'request.course.uri'}=~/^\/uploaded\//);
 1814:     my $forcestandard = 0;
 1815:     my $forcesupplement;
 1816:     my $script='';
 1817:     my $allowed;
 1818:     my $events='';
 1819:     my $showdoc=0;
 1820:     my $containertag;
 1821:     my $uploadtag;
 1822:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1823: 					    ['folderpath','pagepath','pagesymb']);
 1824:     if ($ENV{'form.folderpath'}) {
 1825: 	my (@folderpath)=split('&',$ENV{'form.folderpath'});
 1826: 	$ENV{'form.foldername'}=&Apache::lonnet::unescape(pop(@folderpath));
 1827: 	$ENV{'form.folder'}=pop(@folderpath);
 1828:     }
 1829:     if ($ENV{'form.pagepath'}) {
 1830:         my (@pagepath)=split('&',$ENV{'form.pagepath'});
 1831:         $ENV{'form.pagename'}=&Apache::lonnet::unescape(pop(@pagepath));
 1832:         $ENV{'form.folder'}=pop(@pagepath);
 1833:         $containertag = '<input type="hidden" name="pagepath" value="" />'.
 1834: 	    '<input type="hidden" name="pagesymb" value="" />';
 1835:         $uploadtag = '<input type="hidden" name="pagepath" value="'.$ENV{'form.pagepath'}.'" />'.
 1836: 	    '<input type="hidden" name="pagesymb" value="'.$ENV{'form.pagesymb'}.'" />';
 1837:     }
 1838:     if ($r->uri=~/^\/adm\/coursedocs\/showdoc\/(.*)$/) {
 1839:        $showdoc='/'.$1;
 1840:     }
 1841:     unless ($showdoc) { # got called from remote
 1842:        if (($ENV{'form.folder'}=~/^default_/) || 
 1843:           ($ENV{'form.folder'} =~ m#^\d+/(pages|sequences)/#)) {
 1844:            $forcestandard = 1;
 1845:        } 
 1846:        $forcesupplement=($ENV{'form.folder'}=~/^supplemental_/);
 1847: 
 1848: # does this user have privileges to post, etc?
 1849:        $allowed=&Apache::lonnet::allowed('mdc',$ENV{'request.course.id'});
 1850:        if ($allowed) { 
 1851:          &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['cmd']);
 1852:          $script=&Apache::lonratedt::editscript('simple'); 
 1853:        }
 1854:     } else { # got called in sequence from course
 1855:        $allowed=0;
 1856:        $script='</script>'.&Apache::lonmenu::registerurl(1,undef).'<script>';
 1857:        $events='onLoad="'.&Apache::lonmenu::loadevents.
 1858:            '" onUnload="'.&Apache::lonmenu::unloadevents.'"';
 1859:     }
 1860: 
 1861: # get course data
 1862:     my $coursenum=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
 1863:     my $coursedom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
 1864: 
 1865: # get personal data
 1866:  
 1867:     my $uname=$ENV{'user.name'};
 1868:     my $udom=$ENV{'user.domain'};
 1869:     my $plainname=&Apache::lonnet::escape(
 1870:                      &Apache::loncommon::plainname($uname,$udom));
 1871: 
 1872: # graphics settings
 1873: 
 1874:     $iconpath = $r->dir_config('lonIconsURL') . "/";
 1875: 
 1876:     my $now=time;
 1877: 
 1878: # print screen
 1879:     $r->print(<<ENDDOCUMENT);
 1880: <html>
 1881: <head>
 1882: <title>The LearningOnline Network with CAPA</title>
 1883: <script>
 1884: $script
 1885: </script>
 1886: ENDDOCUMENT
 1887:    if ($allowed) {
 1888:     $r->print(<<ENDNEWSCRIPT);
 1889: <script>
 1890: function makenewfolder(targetform,folderseq) {
 1891:     var foldername=prompt('Name of New Folder','New Folder');
 1892:     if (foldername) {
 1893: 	targetform.importdetail.value=foldername+"="+folderseq;
 1894:         targetform.submit();
 1895:     }
 1896: }
 1897: 
 1898: function makenewpage(targetform,folderseq) {
 1899:     var pagename=prompt('Name of New Page','New Page');
 1900:     if (pagename) {
 1901:         targetform.importdetail.value=pagename+"="+folderseq;
 1902:         targetform.submit();
 1903:     }
 1904: }
 1905: 
 1906: function makenewext(targetname) {
 1907:     this.document.forms.extimport.useform.value=targetname;
 1908:     window.open('/adm/rat/extpickframe.html');
 1909: }
 1910: 
 1911: function makeexamupload() {
 1912:    var title=prompt('Listed Title for the Uploaded Score');
 1913:    if (title) { 
 1914:     this.document.forms.newexamupload.importdetail.value=
 1915: 	title+'=/res/lib/templates/examupload.problem';
 1916:     this.document.forms.newexamupload.submit();
 1917:    }
 1918: }
 1919: 
 1920: function makesmppage() {
 1921:    var title=prompt('Listed Title for the Page');
 1922:    if (title) { 
 1923:     this.document.forms.newsmppg.importdetail.value=
 1924: 	title+'=/adm/$udom/$uname/$now/smppg';
 1925:     this.document.forms.newsmppg.submit();
 1926:    }
 1927: }
 1928: 
 1929: function makesmpproblem() {
 1930:    var title=prompt('Listed Title for the Problem');
 1931:    if (title) { 
 1932:     this.document.forms.newsmpproblem.importdetail.value=
 1933: 	title+'=/res/lib/templates/simpleproblem.problem';
 1934:     this.document.forms.newsmpproblem.submit();
 1935:    }
 1936: }
 1937: 
 1938: function makebulboard() {
 1939:    var title=prompt('Listed Title for the Bulletin Board');
 1940:    if (title) {
 1941:     this.document.forms.newbul.importdetail.value=
 1942: 	title+'=/adm/$udom/$uname/$now/bulletinboard';
 1943:     this.document.forms.newbul.submit();
 1944:    }
 1945: }
 1946: 
 1947: function makeabout() {
 1948:    var user=prompt("Enter user\@domain for User's 'About Me' Page");
 1949:    if (user) {
 1950:        var comp=new Array();
 1951:        comp=user.split('\@');
 1952:        if ((typeof(comp[0])!=undefined) && (typeof(comp[1])!=undefined)) {
 1953: 	   if ((comp[0]) && (comp[1])) {
 1954: 	       this.document.forms.newaboutsomeone.importdetail.value=
 1955: 		   'About '+user+'=/adm/'+comp[1]+'/'+comp[0]+'/aboutme';
 1956: 	       this.document.forms.newaboutsomeone.submit();
 1957: 	   }
 1958:        }
 1959:    }
 1960: }
 1961: 
 1962: function makeims() {
 1963:     var caller = document.forms.ims.folder.value
 1964:     var newlocation = "/adm/imsimportdocs?folder="+caller+"&phase=one"
 1965:     newWindow = window.open("","IMSimport","HEIGHT=700,WIDTH=750,scrollbars=yes")
 1966:     newWindow.location.href = newlocation
 1967: }
 1968: 
 1969: 
 1970: function finishpick() {
 1971:     var title=this.document.forms.extimport.title.value;
 1972:     var url=this.document.forms.extimport.url.value;
 1973:     var form=this.document.forms.extimport.useform.value;
 1974:     eval
 1975:      ('this.document.forms.'+form+'.importdetail.value="'+title+'='+url+
 1976:     '";this.document.forms.'+form+'.submit();');
 1977: }
 1978: 
 1979: function changename(folderpath,index,oldtitle,container,pagesymb) {
 1980:     var title=prompt('New Title',oldtitle);
 1981:     if (title) {
 1982: 	this.document.forms.renameform.title.value=title;
 1983: 	this.document.forms.renameform.cmd.value='rename_'+index;
 1984:         if (container == 'sequence') {
 1985: 	    this.document.forms.renameform.folderpath.value=folderpath;
 1986:         }
 1987:         if (container == 'page') {
 1988:             this.document.forms.renameform.pagepath.value=folderpath;
 1989:             this.document.forms.renameform.pagesymb.value=pagesymb;
 1990:         }
 1991:         this.document.forms.renameform.submit();
 1992:     }
 1993: }
 1994: 
 1995: function removeres(folderpath,index,oldtitle,container,pagesymb) {
 1996:     if (confirm('Remove "'+oldtitle+'"?')) {
 1997: 	this.document.forms.renameform.cmd.value='del_'+index;
 1998:         if (container == 'sequence') {
 1999:             this.document.forms.renameform.folderpath.value=folderpath;
 2000:         }
 2001:         if (container == 'page') {
 2002:             this.document.forms.renameform.pagepath.value=folderpath;
 2003:             this.document.forms.renameform.pagesymb.value=pagesymb;
 2004:         }
 2005:         this.document.forms.renameform.submit();
 2006:     }
 2007: }
 2008: 
 2009: </script>
 2010: 
 2011: ENDNEWSCRIPT
 2012:   }
 2013: # -------------------------------------------------------------------- Body tag
 2014:   $r->print('</head>'.
 2015:             &Apache::loncommon::bodytag('Course Documents','',$events,
 2016: 					'','',$showdoc).
 2017: 	    &Apache::loncommon::help_open_menu('','','','',273,'RAT'));
 2018:   unless ($showdoc) {
 2019: # -----------------------------------------------------------------------------
 2020:        my %lt=&Apache::lonlocal::texthash(
 2021:                 'uplm' => 'Upload a new main course document',
 2022:                 'upls' => 'Upload a new supplemental course document',
 2023:                 'impp' => 'Import a published document',
 2024:                 'spec' => 'Special documents',
 2025:                 'upld' => 'Upload Document',
 2026:                 'srch' => 'Search',
 2027:                 'impo' => 'Import',
 2028:                 'selm' => 'Select Map',
 2029:                 'load' => 'Load Map',
 2030:                 'newf' => 'New Folder',
 2031:                 'newp' => 'New Composite Page',
 2032:                 'extr' => 'External Resource',
 2033:                 'syll' => 'Syllabus',
 2034:                 'navc' => 'Navigate Contents',
 2035:                 'sipa' => 'Simple Page',
 2036:                 'sipr' => 'Simple Problem',
 2037:                 'scuf' => 'Score Upload Form',
 2038:                 'bull' => 'Bulletin Board',
 2039:                 'mypi' => 'My Personal Info',
 2040: 		'abou' => 'About User',
 2041:                 'imsf' => 'Import IMS package',
 2042:                 'file' =>  'File',
 2043:                 'title' => 'Title',
 2044:                 'comment' => 'Comment' 
 2045: 					  );
 2046: # -----------------------------------------------------------------------------
 2047:     if ($allowed) {
 2048:        my $dumpbut=&dumpbutton();
 2049:        my $exportbut=&exportbutton();
 2050:        my %lt=&Apache::lonlocal::texthash(
 2051: 					 'vc' => 'Verify Content',
 2052: 					 'cv' => 'Check/Set Resource Versions',
 2053: 					  );
 2054: 
 2055:        my $folderpath=$ENV{'form.folderpath'};
 2056:        if (!$folderpath) {
 2057: 	   if ($ENV{'form.folder'} eq '' ||
 2058: 	       $ENV{'form.folder'} eq 'supplemental') {
 2059: 	       $folderpath='default&'.
 2060: 		   &Apache::lonnet::escape(&mt('Main Course Documents'));
 2061: 	   }
 2062:        }
 2063:        unless ($ENV{'form.pagepath'}) {
 2064:            $containertag = '<input type="hidden" name="folderpath" value="" />';
 2065:            $uploadtag = '<input type="hidden" name="folderpath" value="'.$folderpath.'" />';
 2066:        }
 2067: 
 2068:        $r->print(<<ENDCOURSEVERIFY);
 2069: <form name="renameform" method="post" action="/adm/coursedocs">
 2070: <input type="hidden" name="title" />
 2071: <input type="hidden" name="cmd" />
 2072: $containertag
 2073: </form>
 2074: <form name="simpleedit" method="post" action="/adm/coursedocs">
 2075: <input type=hidden name="importdetail" value="">
 2076: $uploadtag
 2077: </form>
 2078: <form action="/adm/coursedocs" method="post" name="courseverify">
 2079: <table bgcolor="#AAAAAA" width="100%" cellspacing="4" cellpadding="4">
 2080: <tr><td bgcolor="#DDDDCC">
 2081: <input type="submit" name="verify" value="$lt{'vc'}" />$help{'Verify_Content'}
 2082: </td><td bgcolor="#DDDDCC">
 2083:     <input type="submit" name="versions" value="$lt{'cv'}" />$help{'Check_Resource_Versions'}
 2084: $dumpbut
 2085: $exportbut
 2086: </td></tr></table>
 2087: </form>
 2088: ENDCOURSEVERIFY
 2089:        $r->print(&Apache::loncommon::help_open_topic('Docs_Adding_Course_Doc',
 2090: 		     &mt('Editing the Table of Contents for your Course')));
 2091:     }
 2092: # --------------------------------------------------------- Standard documents
 2093:     $r->print('<table border=2 cellspacing=4 cellpadding=4>');
 2094:     if (($standard) && ($allowed) && (!$forcesupplement)) {
 2095: 	$r->print('<tr><td bgcolor="#BBBBBB">');
 2096: #  '<h2>'.&mt('Main Course Documents').
 2097: #  ($allowed?' '.$help{'Main_Course_Documents'}:'').'</h2>');
 2098:        my $folder=$ENV{'form.folder'};
 2099:        if ($folder eq '' || $folder eq 'supplemental') {
 2100:            $folder='default';
 2101: 	   $ENV{'form.folderpath'}='default&'.&Apache::lonnet::escape(&mt('Main Course Documents'));
 2102:        }
 2103:        my $postexec='';
 2104:        if ($folder eq 'default') {
 2105: 	   $r->print('<script>this.window.name="loncapaclient";</script>');
 2106:        } else {
 2107:            #$postexec='self.close();';
 2108:        }
 2109:        $hadchanges=0;
 2110:        &editor($r,$coursenum,$coursedom,$folder,$allowed);
 2111:        if ($hadchanges) {
 2112: 	   &mark_hash_old()
 2113:        }
 2114:        &changewarning($r,$postexec);
 2115:        my $folderseq='/uploaded/'.$coursedom.'/'.$coursenum.'/default_'.time.
 2116:                      '.sequence';
 2117:        my $pageseq = '/uploaded/'.$coursedom.'/'.$coursenum.'/default_'.time.
 2118:                      '.page';
 2119: 
 2120:        $r->print(<<ENDFORM);
 2121: <table cellspacing=4 cellpadding=4><tr>
 2122: <th bgcolor="#DDDDDD">$lt{'uplm'}</th>
 2123: <th bgcolor="#DDDDDD">$lt{'impp'}</th>
 2124: <th bgcolor="#DDDDDD">$lt{'spec'}</th>
 2125: </tr>
 2126: <tr><td bgcolor="#DDDDDD">
 2127: $lt{'file'}:<br />
 2128: <form action="/adm/coursedocs" method="post" enctype="multipart/form-data">
 2129: <input type="file" name="uploaddoc" size="40">
 2130: <br />
 2131: $lt{'title'}:<br />
 2132: <input type="text" size="50" name="comment">
 2133: $uploadtag
 2134: <input type="hidden" name="cmd" value="upload_default">
 2135: <nobr>
 2136: <input type="submit" value="$lt{'upld'}">
 2137:  $help{'Uploading_From_Harddrive'}
 2138: </nobr>
 2139: </form>
 2140: </td>
 2141: <td bgcolor="#DDDDDD">
 2142: <form action="/adm/coursedocs" method="post" name="simpleeditdefault">
 2143: $uploadtag
 2144: <input type=button onClick="javascript:groupsearch()" value="$lt{'srch'}">
 2145: <nobr>
 2146: <input type=button onClick="javascript:groupimport();" value="$lt{'impo'}">
 2147: $help{'Importing_LON-CAPA_Resource'}
 2148: </nobr>
 2149: <p>
 2150: <hr />
 2151: <input type="text" size="20" name="importmap"><br />
 2152: <nobr><input type=button 
 2153: onClick="javascript:openbrowser('simpleeditdefault','importmap','sequence,page','')"
 2154: value="$lt{'selm'}"> <input type="submit" name="loadmap" value="$lt{'load'}">
 2155: $help{'Load_Map'}</nobr>
 2156: </p>
 2157: </form>
 2158: </td>
 2159: <td bgcolor="#DDDDDD">
 2160: ENDFORM
 2161:        unless ($ENV{'form.pagepath'}) {
 2162:            $r->print(<<ENDFORM);
 2163: <form action="/adm/coursedocs" method="post" name="newfolder">
 2164: <input type="hidden" name="folderpath" value="$ENV{'form.folderpath'}" />
 2165: <input type=hidden name="importdetail" value="">
 2166: <nobr>
 2167: <input name="newfolder" type="button"
 2168: onClick="javascript:makenewfolder(this.form,'$folderseq');"
 2169: value="$lt{'newf'}" />$help{'Adding_Folders'}
 2170: </nobr>
 2171: </form>
 2172: <form action="/adm/coursedocs" method="post" name="newpage">
 2173: <input type="hidden" name="folderpath" value="$ENV{'form.folderpath'}" />
 2174: <input type=hidden name="importdetail" value="">
 2175: <nobr>
 2176: <input name="newpage" type="button"
 2177: onClick="javascript:makenewpage(this.form,'$pageseq');"
 2178: value="$lt{'newp'}" />$help{'Adding_Pages'}
 2179: </nobr>
 2180: </form>
 2181: <form action="/adm/coursedocs" method="post" name="newext">
 2182: $uploadtag
 2183: <input type=hidden name="importdetail" value="">
 2184: <nobr>
 2185: <input name="newext" type="button" onClick="javascript:makenewext('newext');"
 2186: value="$lt{'extr'}" /> $help{'Adding_External_Resource'}
 2187: </nobr>
 2188: </form>
 2189: <form action="/adm/coursedocs" method="post" name="newsyl">
 2190: $uploadtag
 2191: <input type=hidden name="importdetail" 
 2192: value="Syllabus=/public/$coursedom/$coursenum/syllabus">
 2193: <nobr>
 2194: <input name="newsyl" type="submit" value="$lt{'syll'}" /> 
 2195:  $help{'Syllabus'}
 2196: </nobr>
 2197: </form>
 2198: <form action="/adm/coursedocs" method="post" name="newnav">
 2199: $uploadtag
 2200: <input type=hidden name="importdetail" 
 2201: value="Navigate Content=/adm/navmaps">
 2202: <nobr>
 2203: <input name="newnav" type="submit" value="$lt{'navc'}" />
 2204: $help{'Navigate_Content'}
 2205: </nobr>
 2206: </form>
 2207: <form action="/adm/coursedocs" method="post" name="newsmppg">
 2208: $uploadtag
 2209: <input type=hidden name="importdetail" value="">
 2210: <nobr>
 2211: <input name="newsmppg" type="button" value="$lt{'sipa'}"
 2212: onClick="javascript:makesmppage();" /> $help{'Simple Page'}
 2213: </nobr>
 2214: </form>
 2215: <form action="/adm/coursedocs" method="post" name="newsmpproblem">
 2216: $uploadtag
 2217: <input type=hidden name="importdetail" value="">
 2218: <nobr>
 2219: <input name="newsmpproblem" type="button" value="$lt{'sipr'}"
 2220: onClick="javascript:makesmpproblem();" />$help{'Simple Problem'}
 2221: </nobr>
 2222: </form>
 2223: <form action="/adm/coursedocs" method="post" name="newexamupload">
 2224: $uploadtag
 2225: <input type=hidden name="importdetail" value="">
 2226: <nobr>
 2227: <input name="newexamupload" type="button" value="$lt{'scuf'}"
 2228: onClick="javascript:makeexamupload();" />
 2229: $help{'Score_Upload_Form'}
 2230: </nobr>
 2231: </form>
 2232: <form action="/adm/coursedocs" method="post" name="newbul">
 2233: $uploadtag
 2234: <input type=hidden name="importdetail" value="">
 2235: <nobr>
 2236: <input name="newbulletin" type="button" value="$lt{'bull'}"
 2237: onClick="javascript:makebulboard();" />
 2238: $help{'Bulletin Board'}
 2239: </nobr>
 2240: </form>
 2241: <form action="/adm/coursedocs" method="post" name="newaboutme">
 2242: $uploadtag
 2243: <input type=hidden name="importdetail" 
 2244: value="$plainname=/adm/$udom/$uname/aboutme">
 2245: <nobr>
 2246: <input name="newaboutme" type="submit" value="$lt{'mypi'}" />
 2247: $help{'My Personal Info'}
 2248: </nobr>
 2249: </form>
 2250: <form action="/adm/coursedocs" method="post" name="newaboutsomeone">
 2251: $uploadtag
 2252: <input type=hidden name="importdetail" value="">
 2253: <nobr>
 2254: <input name="newaboutsomeone" type="button" value="$lt{'abou'}" 
 2255: onClick="javascript:makeabout();" />
 2256: </nobr>
 2257: ENDFORM
 2258:        }
 2259:        if ($ENV{'form.pagepath'}) {
 2260:            $r->print(<<ENDBLOCK);
 2261: <form action="/adm/coursedocs" method="post" name="newsmpproblem">
 2262: $uploadtag
 2263: <input type=hidden name="importdetail" value="">
 2264: <nobr>
 2265: <input name="newsmpproblem" type="button" value="$lt{'sipr'}"
 2266: onClick="javascript:makesmpproblem();" />$help{'Simple Problem'}
 2267: </nobr>
 2268: </form>
 2269: <form action="/adm/coursedocs" method="post" name="newexamupload">
 2270: $uploadtag
 2271: <input type=hidden name="importdetail" value="">
 2272: <nobr>
 2273: <input name="newexamupload" type="button" value="$lt{'scuf'}"
 2274: onClick="javascript:makeexamupload();" />
 2275: $help{'Score_Upload_Form'}
 2276: </nobr>
 2277: </form>
 2278: ENDBLOCK
 2279:        } else {
 2280:            $r->print(<<ENDFORM);
 2281: </form>
 2282: <form action="/adm/imsimportdocs" method="post" name="ims">
 2283: <input type="hidden" name="folder" value="$folder" />
 2284: <input name="imsimport" type="button" value="$lt{'imsf'}" onClick="javascript:makeims();" />
 2285: </nobr>
 2286: </form>
 2287: ENDFORM
 2288:        }
 2289:        $r->print('</td></tr>'."\n".
 2290: '</table>');
 2291:        $r->print('</td></tr>');
 2292:     }
 2293: # ----------------------------------------------------- Supplemental documents
 2294:     if (!$forcestandard) {
 2295:        $r->print('<tr><td bgcolor="#BBBBBB">');
 2296: # '<h2>'.&mt('Supplemental Course Documents').
 2297: #  ($allowed?' '.$help{'Supplemental'}:'').'</h2>');
 2298:        my $folder=$ENV{'form.folder'};
 2299:        unless ($folder=~/^supplemental/) {
 2300: 	   $folder='supplemental';
 2301:        }
 2302:        if ($folder =~ /^supplemental$/ &&
 2303: 	   $ENV{'form.folderpath'} =~ /^default\&/) {
 2304: 	   $ENV{'form.folderpath'}='supplemental&'.
 2305: 	       &Apache::lonnet::escape(&mt('Supplemental Course Documents'));
 2306:        }
 2307:        &editor($r,$coursenum,$coursedom,$folder,$allowed);
 2308:        if ($allowed) {
 2309:        my $folderseq=
 2310:                   '/uploaded/'.$coursedom.'/'.$coursenum.'/supplemental_'.time.
 2311:                      '.sequence';
 2312: 
 2313:           $r->print(<<ENDSUPFORM);
 2314: <table cellspacing=4 cellpadding=4><tr>
 2315: <th bgcolor="#DDDDDD">$lt{'upls'}</th>
 2316: <th bgcolor="#DDDDDD">$lt{'spec'}</th>
 2317: </tr>
 2318: <tr><td bgcolor="#DDDDDD">
 2319: <form action="/adm/coursedocs" method="post" enctype="multipart/form-data">
 2320: <input type="file" name="uploaddoc" size="40">
 2321: <br />$lt{'comment'}:<br />
 2322: <textarea cols=50 rows=4 name='comment'>
 2323: </textarea>
 2324: <br />
 2325: <input type="hidden" name="folderpath" value="$ENV{'form.folderpath'}" />
 2326: <input type="hidden" name="cmd" value="upload_supplemental">
 2327: <nobr>
 2328: <input type="submit" value="$lt{'upld'}">
 2329:  $help{'Uploading_From_Harddrive'}
 2330: </nobr>
 2331: </form>
 2332: </td>
 2333: <td bgcolor="#DDDDDD">
 2334: <form action="/adm/coursedocs" method="post" name="supnewfolder">
 2335: <input type="hidden" name="folderpath" value="$ENV{'form.folderpath'}" />
 2336: <input type=hidden name="importdetail" value="">
 2337: <nobr>
 2338: <input name="newfolder" type="button"
 2339: onClick="javascript:makenewfolder(this.form,'$folderseq');"
 2340: value="$lt{'newf'}" /> $help{'Adding_Folders'}
 2341: </nobr>
 2342: </form>
 2343: <form action="/adm/coursedocs" method="post" name="supnewext">
 2344: <input type="hidden" name="folderpath" value="$ENV{'form.folderpath'}" />
 2345: <input type=hidden name="importdetail" value="">
 2346: <nobr>
 2347: <input name="newext" type="button" 
 2348: onClick="javascript:makenewext('supnewext');"
 2349: value="$lt{'extr'}" /> $help{'Adding_External_Resource'}
 2350: </nobr>
 2351: </form>
 2352: <form action="/adm/coursedocs" method="post" name="supnewsyl">
 2353: <input type="hidden" name="folderpath" value="$ENV{'form.folderpath'}" />
 2354: <input type=hidden name="importdetail" 
 2355: value="Syllabus=/public/$coursedom/$coursenum/syllabus">
 2356: <nobr>
 2357: <input name="newsyl" type="submit" value="$lt{'syll'}" />
 2358: $help{'Syllabus'}
 2359: </nobr>
 2360: </form>
 2361: <form action="/adm/coursedocs" method="post" name="subnewaboutme">
 2362: <input type="hidden" name="folderpath" value="$ENV{'form.folderpath'}" />
 2363: <input type=hidden name="importdetail" 
 2364: value="$plainname=/adm/$udom/$uname/aboutme">
 2365: <nobr>
 2366: <input name="newaboutme" type="submit" value="$lt{'mypi'}" />
 2367: $help{'My Personal Info'}
 2368: </nobr>
 2369: </form>
 2370: </td></tr>
 2371: </table></td></tr>
 2372: ENDSUPFORM
 2373:        }
 2374:     }
 2375:     if ($allowed) {
 2376: 	$r->print('<form name="extimport"><input type="hidden" name="title"><input type="hidden" name="url"><input type="hidden" name="useform"></form>');
 2377:     }
 2378:     $r->print('</table>');
 2379:   } else {
 2380: # -------------------------------------------------------- This is showdoc mode
 2381:       $r->print("<h1>".&mt('Uploaded Document').' - '.
 2382: 		&Apache::lonnet::gettitle($r->uri).'</h1><p>'.
 2383: &mt('It is recommended that you use an up-to-date virus scanner before handling this file.')."</p><p><table>".
 2384:          &entryline(0,&mt("Click to download or use your browser's Save Link function"),$showdoc).'</table></p>');
 2385:   }
 2386:  }
 2387:  $r->print('</body></html>');
 2388:  return OK;
 2389: } 
 2390: 
 2391: 1;
 2392: __END__

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