File:  [LON-CAPA] / loncom / interface / londocs.pm
Revision 1.228: download - view: text, annotated - select for diffs
Tue May 30 12:46:09 2006 UTC (18 years, 1 month ago) by www
Branches: MAIN
CVS tags: HEAD
&Apache::lonnet::unescape -> &unescape
&Apache::lonnet::escape -> &escape

    1: # The LearningOnline Network
    2: # Documents
    3: #
    4: # $Id: londocs.pm,v 1.228 2006/05/30 12:46:09 www 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: use lib '/home/httpd/lib/perl/';
   46: use LONCAPA;
   47: 
   48: my $iconpath;
   49: 
   50: my %hash;
   51: 
   52: my $hashtied;
   53: my %alreadyseen=();
   54: 
   55: my $hadchanges;
   56: 
   57: # Available help topics
   58: 
   59: my %help=();
   60: 
   61: # Mapread read maps into lonratedt::global arrays 
   62: # @order and @resources, determines status
   63: # sets @order - pointer to resources in right order
   64: # sets @resources - array with the resources with correct idx
   65: #
   66: 
   67: sub mapread {
   68:     my ($coursenum,$coursedom,$map)=@_;
   69:     return
   70:       &Apache::lonratedt::mapread('/uploaded/'.$coursedom.'/'.$coursenum.'/'.
   71:                                 $map);
   72: }
   73: 
   74: sub storemap {
   75:     my ($coursenum,$coursedom,$map)=@_;
   76:     my ($outtext,$errtext)=
   77:       &Apache::lonratedt::storemap('/uploaded/'.$coursedom.'/'.$coursenum.'/'.
   78:                                 $map,1);
   79:     if ($errtext) { return ($errtext,2); }
   80:     
   81:     $hadchanges=1;
   82:     return ($errtext,0);
   83: }
   84: 
   85: # ----------------------------------------- Return hash with valid author names
   86: 
   87: sub authorhosts {
   88:     my %outhash=();
   89:     my $home=0;
   90:     my $other=0;
   91:     foreach (keys %env) {
   92: 	if ($_=~/^user\.role\.(au|ca)\.(.+)$/) {
   93: 	    my $role=$1;
   94: 	    my $realm=$2;
   95: 	    my ($start,$end)=split(/\./,$env{$_});
   96: 	    if (($start) && ($start>time)) { next; }
   97: 	    if (($end) && (time>$end)) { next; }
   98: 	    my $ca; my $cd;
   99: 	    if ($1 eq 'au') {
  100: 		$ca=$env{'user.name'};
  101: 		$cd=$env{'user.domain'};
  102: 	    } else {
  103: 		($cd,$ca)=($realm=~/^\/(\w+)\/(\w+)$/);
  104: 	    }
  105: 	    my $allowed=0;
  106: 	    my $myhome=&Apache::lonnet::homeserver($ca,$cd);
  107: 	    my @ids=&Apache::lonnet::current_machine_ids();
  108: 	    foreach my $id (@ids) { if ($id eq $myhome) { $allowed=1; } }
  109: 	    if ($allowed) {
  110: 		$home++;
  111: 		$outhash{'home_'.$ca.'@'.$cd}=1;
  112: 	    } else {
  113: 		$outhash{'otherhome_'.$ca.'@'.$cd}=$myhome;
  114: 		$other++;
  115: 	    }
  116: 	}
  117:     }
  118:     return ($home,$other,%outhash);
  119: }
  120: # ------------------------------------------------------ Generate "dump" button
  121: 
  122: sub dumpbutton {
  123:     my ($home,$other,%outhash)=&authorhosts();
  124:     if ($home+$other==0) { return ''; }
  125:     my $output='</td><td bgcolor="#DDDDCC">';
  126:     if ($home) {
  127: 	return '</td><td bgcolor="#DDDDCC">'.
  128: 	    '<input type="submit" name="dumpcourse" value="'.
  129: 	    &mt('Dump Course DOCS to Construction Space').'" />'.
  130: 	    &Apache::loncommon::help_open_topic('Docs_Dump_Course_Docs');
  131:     } else {
  132: 	return'</td><td bgcolor="#DDDDCC">'.
  133:      &mt('Dump Course DOCS to Construction Space: available on other servers');
  134:     }
  135: }
  136: 
  137: sub clean {
  138:     my ($title)=@_;
  139:     $title=~s/[^\w\/\!\$\%\^\*\-\_\=\+\;\:\,\\\|\`\~]+/\_/gs;
  140:     return $title;	
  141: }
  142: # -------------------------------------------------------- Actually dump course
  143: 
  144: sub dumpcourse {
  145:     my ($r) = @_;
  146: 
  147:     $r->print(&Apache::loncommon::start_page('Dump Course DOCS to Construction Space').
  148: 	      '<form name="dumpdoc" method="post">');
  149:     my ($home,$other,%outhash)=&authorhosts();
  150:     unless ($home) { return ''; }
  151:     my $origcrsid=$env{'request.course.id'};
  152:     my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
  153:     if (($env{'form.authorspace'}) && ($env{'form.authorfolder'}=~/\w/)) {
  154: # Do the dumping
  155: 	unless ($outhash{'home_'.$env{'form.authorspace'}}) { return ''; }
  156: 	my ($ca,$cd)=split(/\@/,$env{'form.authorspace'});
  157: 	$r->print('<h3>'.&mt('Copying Files').'</h3>');
  158: 	my $title=$env{'form.authorfolder'};
  159: 	$title=&clean($title);
  160: 	my %replacehash=();
  161: 	foreach (keys %env) {
  162: 	    if ($_=~/^form\.namefor\_(.+)/) {
  163: 		$replacehash{$1}=$env{$_};
  164: 	    }
  165: 	}
  166: 	my $crs='/uploaded/'.$env{'request.course.id'}.'/';
  167: 	$crs=~s/\_/\//g;
  168: 	foreach (keys %replacehash) {
  169: 	    my $newfilename=$title.'/'.$replacehash{$_};
  170: 	    $newfilename=~s/\.(\w+)$//;
  171: 	    my $ext=$1;
  172: 	    $newfilename=&clean($newfilename);
  173: 	    $newfilename.='.'.$ext;
  174: 	    my @dirs=split(/\//,$newfilename);
  175: 	    my $path='/home/'.$ca.'/public_html';
  176: 	    my $makepath=$path;
  177: 	    my $fail=0;
  178: 	    for (my $i=0;$i<$#dirs;$i++) {
  179: 		$makepath.='/'.$dirs[$i];
  180: 		unless (-e $makepath) { 
  181: 		    unless(mkdir($makepath,0777)) { $fail=1; } 
  182: 		}
  183: 	    }
  184: 	    $r->print('<br /><tt>'.$_.'</tt> => <tt>'.$newfilename.'</tt>: ');
  185: 	    if (my $fh=Apache::File->new('>'.$path.'/'.$newfilename)) {
  186: 		if ($_=~/\.(sequence|page|html|htm|xml|xhtml)$/) {
  187: 		    print $fh &Apache::loncreatecourse::rewritefile(
  188:          &Apache::loncreatecourse::readfile($env{'request.course.id'},$_),
  189: 				     (%replacehash,$crs => '')
  190: 								    );
  191: 		} else {
  192: 		    print $fh
  193:          &Apache::loncreatecourse::readfile($env{'request.course.id'},$_);
  194: 		       }
  195: 		$fh->close();
  196: 	    } else {
  197: 		$fail=1;
  198: 	    }
  199: 	    if ($fail) {
  200: 		$r->print('<font color="red">fail</font>');
  201: 	    } else {
  202: 		$r->print('<font color="green">ok</font>');
  203: 	    }
  204: 	}
  205:     } else {
  206: # Input form
  207: 	unless ($home==1) {
  208: 	    $r->print(
  209: 		      '<h3>'.&mt('Select the Construction Space').'</h3><select name="authorspace">');
  210: 	}
  211: 	foreach (sort keys %outhash) {
  212: 	    if ($_=~/^home_(.+)$/) {
  213: 		if ($home==1) {
  214: 		    $r->print(
  215: 		  '<input type="hidden" name="authorspace" value="'.$1.'" />');
  216: 		} else {
  217: 		    $r->print('<option value="'.$1.'">'.$1.' - '.
  218: 			      &Apache::loncommon::plainname(split(/\@/,$1)).'</option>');
  219: 		}
  220: 	    }
  221: 	}
  222: 	unless ($home==1) {
  223: 	    $r->print('</select>');
  224: 	}
  225: 	my $title=$origcrsdata{'description'};
  226: 	$title=~s/[\/\s]+/\_/gs;
  227: 	$title=&clean($title);
  228: 	$r->print('<h3>'.&mt('Folder in Construction Space').'</h3><input type="text" size="50" name="authorfolder" value="'.$title.'" /><br />');
  229: 	&tiehash();
  230: 	$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>');
  231: 	foreach (&Apache::loncreatecourse::crsdirlist($origcrsid,'userfiles')) {
  232: 	    $r->print('<tr><td>'.$_.'</td>');
  233: 	    my ($ext)=($_=~/\.(\w+)$/);
  234: 	    my $title=$hash{'title_'.$hash{
  235: 		'ids_/uploaded/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/'.$_}};
  236: 	    $title=~s/&colon;/:/g;
  237: 	    $r->print('<td>'.($title?$title:'&nbsp;').'</td>');
  238: 	    unless ($title) {
  239: 		$title=$_;
  240: 	    }
  241: 	    $title=~s/\.(\w+)$//;
  242: 	    $title=&clean($title);
  243: 	    $title.='.'.$ext;
  244: 	    $r->print("\n<td><input type='text' size='60' name='namefor_".$_."' value='".$title."' /></td></tr>\n");
  245: 	}
  246: 	$r->print("</table>\n");
  247: 	&untiehash();
  248: 	$r->print(
  249:   '<p><input type="submit" name="dumpcourse" value="'.&mt('Dump Course DOCS').'" /></p></form>');
  250:     }
  251: }
  252: 
  253: # ------------------------------------------------------ Generate "export" button
  254: 
  255: sub exportbutton {
  256:     return '</td><td bgcolor="#DDDDCC">'.
  257:             '<input type="submit" name="exportcourse" value="'.
  258:             &mt('Export Course to IMS').'" />'.
  259:     &Apache::loncommon::help_open_topic('Docs_Export_Course_Docs');
  260: }
  261: 
  262: sub exportcourse {
  263:     my $r=shift;
  264:     my %discussiontime = &Apache::lonnet::dump('discussiontimes',
  265:                                                $env{'course.'.$env{'request.course.id'}.'.domain'}, $env{'course.'.$env{'request.course.id'}.'.num'});
  266:     my $numdisc = keys %discussiontime;
  267:     my $navmap = Apache::lonnavmaps::navmap->new();
  268:     my $it=$navmap->getIterator(undef,undef,undef,1,undef,undef);
  269:     my $curRes;
  270:     my $outcome;
  271: 
  272:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  273:                                             ['finishexport']);
  274:     if ($env{'form.finishexport'}) {
  275:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  276:                                             ['archive','discussion']);
  277: 
  278:         my @exportitems = &Apache::loncommon::get_env_multiple('form.archive');
  279:         my @discussions = &Apache::loncommon::get_env_multiple('form.discussion');
  280:         if (@exportitems == 0 && @discussions == 0) {
  281:             $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';
  282:         } else {
  283:             my $now = time;
  284:             my %symbs;
  285:             my $manifestok = 0;
  286:             my $imsresources;
  287:             my $tempexport;
  288:             my $copyresult;
  289:             my $ims_manifest = &create_ims_store($now,\$manifestok,\$outcome,\$tempexport);
  290:             if ($manifestok) {
  291:                 &build_package($now,$navmap,\@exportitems,\@discussions,\$outcome,$tempexport,\$copyresult,$ims_manifest);
  292:                 close($ims_manifest);
  293: 
  294: #Create zip file in prtspool
  295:                 my $imszipfile = '/prtspool/'.
  296:                 $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
  297:                    time.'_'.rand(1000000000).'.zip';
  298:                 my $cwd = &Cwd::getcwd();
  299:                 my $imszip = '/home/httpd/'.$imszipfile;
  300:                 chdir $tempexport;
  301:                 open(OUTPUT, "zip -r $imszip *  2> /dev/null |");
  302:                 close(OUTPUT);
  303:                 chdir $cwd;
  304:                 $outcome .= 'Download the zip file from <a href="'.$imszipfile.'">IMS course archive</a><br />';
  305:                 if ($copyresult) {
  306:                     $outcome .= 'The following errors occurred during export - '.$copyresult;
  307:                 }
  308:             } else {
  309:                 $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 />';
  310:             }
  311:         }
  312:         $r->print(&Apache::loncommon::start_page('Export course to IMS content package'));
  313:         $r->print($outcome);
  314:         $r->print(&Apache::loncommon::end_page());
  315:     } else {
  316:         my $display;
  317:         $display = '<form name="exportdoc" method="post">'."\n";
  318:         $display .= 'Choose which items you wish to export from your course.<br /><br />';
  319:         $display .= '<table border="0" cellspacing="0" cellpadding="3">'.
  320:                     '<tr><td><fieldset><legend>&nbsp;<b>Content items</b></legend>'.
  321:                     '<input type="button" value="check all" '.
  322:                     'onclick="javascript:checkAll(document.exportdoc.archive)" />'.
  323:                     '&nbsp;&nbsp;<input type="button" value="uncheck all"'.
  324:                     ' onclick="javascript:uncheckAll(document.exportdoc.archive)" /></fieldset></td>'.
  325:                     '<td>&nbsp;</td><td>&nbsp;</td>'.
  326:                     '<td align="right"><fieldset><legend>&nbsp;<b>Discussion posts'.
  327:                     '</b></legend><input type="button" value="check all"'.
  328:                     ' onclick="javascript:checkAll(document.exportdoc.discussion)" />'.
  329:                     '&nbsp;&nbsp;<input type="button" value="uncheck all"'.
  330:                     ' onclick="javascript:uncheckAll(document.exportdoc.discussion)" /></fieldset></td>'.
  331:                     '</tr></table>';
  332:         my $curRes;
  333:         my $depth = 0;
  334:         my $count = 0;
  335:         my $boards = 0;
  336:         my $startcount = 5;
  337:         my %parent = ();
  338:         my %children = ();
  339:         my $lastcontainer = $startcount;
  340:         my @bgcolors = ('#F6F6F6','#FFFFFF');
  341:         $display .= '<table cellspacing="0"><tr>'.
  342:             '<td><b>Export content item?<br /></b></td><td>&nbsp;</td><td align="right">'."\n";
  343:         if ($numdisc > 0) {
  344:             $display.='<b>Export&nbsp;discussion posts?</b>'."\n";
  345:         }
  346:         $display.='&nbsp;</td></tr>';
  347:         while ($curRes = $it->next()) {
  348:             if (ref($curRes)) {
  349:                 $count ++;
  350:             }
  351:             if ($curRes == $it->BEGIN_MAP()) {
  352:                 $depth++;
  353:                 $parent{$depth} = $lastcontainer;
  354:             }
  355:             if ($curRes == $it->END_MAP()) {
  356:                 $depth--;
  357:                 $lastcontainer = $parent{$depth};
  358:             }
  359:             if (ref($curRes)) {
  360:                 my $symb = $curRes->symb();
  361:                 my $ressymb = $symb;
  362:                 if ($ressymb =~ m|adm/(\w+)/(\w+)/(\d+)/bulletinboard$|) {
  363:                     unless ($ressymb =~ m|adm/wrapper/adm|) {
  364:                         $ressymb = 'bulletin___'.$3.'___adm/wrapper/adm/'.$1.'/'.$2.'/'.$3.'/bulletinboard';
  365:                     }
  366:                 }
  367:                 my $color = $count%2;
  368:                 $display .='<tr bgcolor='.$bgcolors[$color].'><td>'."\n".
  369:                     '<input type="checkbox" name="archive" value="'.$count.'" ';
  370:                 if (($curRes->is_sequence()) || ($curRes->is_page())) {
  371:                     my $checkitem = $count + $boards + $startcount;
  372:                     $display .= 'onClick="javascript:propagateCheck('."'$checkitem'".')"';
  373:                 }
  374:                 $display .= ' />'."\n";
  375:                 for (my $i=0; $i<$depth; $i++) {
  376:                     $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";
  377:                 }
  378:                 if ($curRes->is_sequence()) {
  379:                     $display .= '<img src="/adm/lonIcons/navmap.folder.open.gif">&nbsp;'."\n";
  380:                     $lastcontainer = $count + $startcount + $boards;
  381:                 } elsif ($curRes->is_page()) {
  382:                     $display .= '<img src="/adm/lonIcons/navmap.page.open.gif">&nbsp;'."\n";
  383:                     $lastcontainer = $count + $startcount + $boards;
  384:                 }
  385:                 my $currelem = $count+$boards+$startcount;
  386:                 $children{$parent{$depth}} .= $currelem.':';
  387:                 $display .= '&nbsp;'.$curRes->title().'</td>';
  388:                 if ($discussiontime{$ressymb} > 0) {
  389:                     $boards ++;
  390:                     $currelem = $count+$boards+$startcount;
  391:                     $display .= '<td>&nbsp;</td><td align="right"><input type="checkbox" name="discussion" value="'.$count.'" />&nbsp;</td>'."\n";
  392:                 } else {
  393:                     $display .= '<td colspan="2">&nbsp;</td>'."\n";
  394:                 }
  395:             }
  396:         }
  397:         my $scripttag = qq|
  398: <script>
  399: 
  400: function checkAll(field) {
  401:     if (field.length > 0) {
  402:         for (i = 0; i < field.length; i++) {
  403:             field[i].checked = true ;
  404:         }
  405:     } else {
  406:         field.checked = true
  407:     }
  408: }
  409:                                                                                 
  410: function uncheckAll(field) {
  411:     if (field.length > 0) {
  412:         for (i = 0; i < field.length; i++) {
  413:             field[i].checked = false ;
  414:         }
  415:     } else {
  416:         field.checked = false ;
  417:     }
  418: }
  419: 
  420: function propagateCheck(item) {
  421:     if (document.exportdoc.elements[item].checked == true) {
  422:         containerCheck(item)
  423:     }
  424: } 
  425: 
  426: function containerCheck(item) {
  427:     document.exportdoc.elements[item].checked = true
  428:     var numitems = $count + $boards + $startcount
  429:     var parents = new Array(numitems)
  430:     for (var i=$startcount; i<numitems; i++) {
  431:         parents[i] = new Array
  432:     }
  433:         |;
  434: 
  435:         foreach my $container (sort { $a <=> $b } keys %children) {
  436:             my @contents = split/:/,$children{$container};
  437:             for (my $i=0; $i<@contents; $i ++) {
  438:                 $scripttag .= '    parents['.$container.']['.$i.'] = '.$contents[$i]."\n";
  439:             }
  440:         }
  441: 
  442:         $scripttag .= qq|
  443:     if (parents[item].length > 0) {
  444:         for (var j=0; j<parents[item].length; j++) {
  445:             containerCheck(parents[item][j])
  446:         }
  447:      }   
  448: }
  449: 
  450: </script>
  451:         |;
  452: 	$r->print(&Apache::loncommon::start_page('Export course to IMS content package',
  453: 						 $scripttag));
  454: 	$r->print($display.'</table>'.
  455:                   '<p><input type="hidden" name="finishexport" value="1">'.
  456:                   '<input type="submit" name="exportcourse" value="'.
  457:                   &mt('Export Course DOCS').'" /></p></form>'.
  458: 		  &Apache::loncommon::end_page());
  459:     }
  460: }
  461: 
  462: sub create_ims_store {
  463:     my ($now,$manifestok,$outcome,$tempexport) = @_;
  464:     $$tempexport = $Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/ims_exports';
  465:     my $ims_manifest;
  466:     if (!-e $$tempexport) {
  467:         mkdir($$tempexport,0700);
  468:     }
  469:     $$tempexport .= '/'.$now;
  470:     if (!-e $$tempexport) {
  471:         mkdir($$tempexport,0700);
  472:     }
  473:     $$tempexport .= '/'.$env{'user.domain'}.'_'.$env{'user.name'};
  474:     if (!-e $$tempexport) {
  475:         mkdir($$tempexport,0700);
  476:     }
  477:     if (!-e "$$tempexport/resources") {
  478:         mkdir("$$tempexport/resources",0700);
  479:     }
  480: # open manifest file
  481:     my $manifest = '/imsmanifest.xml';
  482:     my $manifestfilename = $$tempexport.$manifest;
  483:     if ($ims_manifest = Apache::File->new('>'.$manifestfilename)) {
  484:         $$manifestok=1;
  485:         print $ims_manifest
  486: '<?xml version="1.0" encoding="UTF-8"?>'."\n".
  487: '<manifest xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"'.
  488: ' xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2"'.
  489: ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'.
  490: ' identifier="MANIFEST-'.$env{'request.course.id'}.'-'.$now.'"'.
  491: '  xsi:schemaLocation="http://www.imsglobal.org/xsd/imscp_v1p1imscp_v1p1.xsd'.
  492: '  http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd">'."\n".
  493: '  <metadata>
  494:     <schema></schema>
  495:     <imsmd:lom>
  496:       <imsmd:general>
  497:         <imsmd:identifier>'.$env{'request.course.id'}.'</imsmd:identifier>
  498:         <imsmd:title>
  499:           <imsmd:langstring xml:lang="en">'.$env{'course.'.$env{'request.course.id'}.'.description'}.'</imsmd:langstring>
  500:         </imsmd:title>
  501:       </imsmd:general>
  502:     </imsmd:lom>
  503:   </metadata>'."\n".
  504: '  <organizations default="ORG-'.$env{'request.course.id'}.'-'.$now.'">'."\n".
  505: '    <organization identifier="ORG-'.$env{'request.course.id'}.'-'.$now.'"'.
  506: ' structure="hierarchical">'."\n".
  507: '      <title>'.$env{'course.'.$env{'request.course.id'}.'.description'}.'</title>'
  508:     } else {
  509:         $$outcome .= 'An error occurred opening the IMS manifest file.<br />'
  510: ;
  511:     }
  512:     return $ims_manifest;
  513: }
  514: 
  515: sub build_package {
  516:     my ($now,$navmap,$exportitems,$discussions,$outcome,$tempexport,$copyresult,$ims_manifest) = @_;
  517: # first iterator to look for dependencies
  518:     my $it = $navmap->getIterator(undef,undef,undef,1,undef,undef);
  519:     my $curRes;
  520:     my $count = 0;
  521:     my $depth = 0;
  522:     my $lastcontainer = 0;
  523:     my %parent = ();
  524:     my @dependencies = ();
  525:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  526:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  527:     while ($curRes = $it->next()) {
  528:         if (ref($curRes)) {
  529:             $count ++;
  530:         }
  531:         if ($curRes == $it->BEGIN_MAP()) {
  532:             $depth++;
  533:             $parent{$depth} = $lastcontainer;
  534:         }
  535:         if ($curRes == $it->END_MAP()) {
  536:             $depth--;
  537:             $lastcontainer = $parent{$depth};
  538:         }
  539:         if (ref($curRes)) {
  540:             if ($curRes->is_sequence() || $curRes->is_page()) {
  541:                 $lastcontainer = $count;
  542:             }
  543:             if (grep/^$count$/,@$exportitems) {
  544:                 &get_dependencies($exportitems,\%parent,$depth,\@dependencies);
  545:             }
  546:         }
  547:     }
  548: # second iterator to build manifest and store resources
  549:     $it = $navmap->getIterator(undef,undef,undef,1,undef,undef);
  550:     $depth = 0;
  551:     my $prevdepth;
  552:     $count = 0;
  553:     my $imsresources;
  554:     my $pkgdepth;
  555:     while ($curRes = $it->next()) {
  556:         if ($curRes == $it->BEGIN_MAP()) {
  557:             $prevdepth = $depth;
  558:             $depth++;
  559:         }
  560:         if ($curRes == $it->END_MAP()) {
  561:             $prevdepth = $depth;
  562:             $depth--;
  563:         }
  564: 
  565:         if (ref($curRes)) {
  566:             $count ++;
  567:             if ((grep/^$count$/,@$exportitems) || (grep/^$count$/,@dependencies)) {
  568:                 my $symb = $curRes->symb();
  569:                 my $isvisible = 'true';
  570:                 my $resourceref;
  571:                 if ($curRes->randomout()) {
  572:                     $isvisible = 'false';
  573:                 }
  574:                 unless ($curRes->is_sequence()) {
  575:                     $resourceref = 'identifierref="RES-'.$env{'request.course.id'}.'-'.$count.'"';
  576:                 }
  577:                 my $step = $prevdepth - $depth;
  578:                 if (($step >= 0) && ($count > 1)) {
  579:                     while ($step >= 0) {
  580:                         print $ims_manifest "\n".'  </item>'."\n";
  581:                         $step --;
  582:                     }
  583:                 }
  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:             }
  626:         }
  627:     }
  628:     while ($pkgdepth > 0) {
  629:         print $ims_manifest "    </item>\n";
  630:         $pkgdepth --;
  631:     }
  632:     my $resource_text = qq|
  633:     </organization>
  634:   </organizations>
  635:   <resources>
  636:     $imsresources
  637:   </resources>
  638: </manifest>
  639:     |;
  640:     print $ims_manifest $resource_text;
  641: }
  642: 
  643: sub get_dependencies {
  644:     my ($exportitems,$parent,$depth,$dependencies) = @_;
  645:     if ($depth > 1) {
  646:         if ((!grep/^$$parent{$depth}$/,@$exportitems) && (!grep/^$$parent{$depth}$/,@$dependencies)) {
  647:             push @$dependencies, $$parent{$depth};
  648:             if ($depth > 2) {
  649:                 &get_dependencies($exportitems,$parent,$depth-1,$dependencies);
  650:             }
  651:         }
  652:     }
  653: }
  654: 
  655: sub process_content {
  656:     my ($count,$curRes,$cdom,$cnum,$symb,$content_file,$href,$copyresult,$tempexport) = @_;
  657:     my $content_type;
  658:     my $message;
  659:     my @uploads = ();
  660:     if ($curRes->is_sequence()) {
  661:         $content_type = 'sequence';
  662:     } elsif ($curRes->is_page()) {
  663:         $content_type = 'page'; # need to handle individual items in pages.
  664:     } elsif ($symb =~ m-public/$cdom/$cnum/syllabus$-) {
  665:         $content_type = 'syllabus';
  666:         my $contents = &Apache::imsexport::templatedpage($content_type);
  667:         if ($contents) {
  668:             $$content_file = &store_template($contents,$tempexport,$count,$content_type);
  669:         }
  670:     } elsif ($symb =~ m-\.sequence___\d+___ext-) {
  671:         $content_type = 'external';
  672:         my $title = $curRes->title;
  673:         my $contents =  &Apache::imsexport::external($symb,$title);
  674:         if ($contents) {
  675:             $$content_file = &store_template($contents,$tempexport,$count,$content_type);
  676:         }
  677:     } elsif ($symb =~ m-adm/navmaps$-) {
  678:         $content_type =  'navmap';
  679:     } elsif ($symb =~ m-adm/[^/]+/[^/]+/(\d+)/smppg$-) {
  680:         $content_type = 'simplepage';
  681:         my $contents = &Apache::imsexport::templatedpage($content_type,$1,$count,\@uploads);
  682:         if ($contents) {
  683:             $$content_file = &store_template($contents,$tempexport,$count,$content_type);
  684:         }
  685:     } elsif ($symb =~ m-lib/templates/simpleproblem\.problem$-) {
  686:         $content_type = 'simpleproblem';
  687:         my $contents =  &Apache::imsexport::simpleproblem($symb);
  688:         if ($contents) {
  689:             $$content_file = &store_template($contents,$tempexport,$count,$content_type);
  690:         }
  691:     } elsif ($symb =~ m-lib/templates/examupload\.problem$-) {
  692:         $content_type = 'examupload';
  693:     } elsif ($symb =~ m-adm/(\w+)/(\w+)/(\d+)/bulletinboard$-) {
  694:         $content_type = 'bulletinboard';
  695:         my $contents =  &Apache::imsexport::templatedpage($content_type,$3,$count,\@uploads,$1,$2);
  696:         if ($contents) {
  697:             $$content_file = &store_template($contents,$tempexport,$count,$content_type);
  698:         }
  699:     } elsif ($symb =~ m-adm/([^/]+)/([^/]+)/aboutme$-) {
  700:         $content_type = 'aboutme';
  701:         my $contents =  &Apache::imsexport::templatedpage($content_type,undef,$count,\@uploads,$1,$2);
  702:         if ($contents) {
  703:             $$content_file = &store_template($contents,$tempexport,$count,$content_type);
  704:         }
  705:     } elsif ($symb =~ m-\.(sequence|page)___\d+___uploaded/$cdom/$cnum/-) {
  706:         $$content_file = &replicate_content($cdom,$cnum,$tempexport,$symb,$count,\$message,$href,'uploaded');
  707:     } elsif ($symb =~ m-\.(sequence|page)___\d+___([^/]+)/([^/]+)-) {
  708:         my $canedit = 0;
  709:         if ($2 eq $env{'user.domain'} && $3 eq $env{'user.name'})  {
  710:             $canedit= 1;
  711:         }
  712: # only include problem code where current user is author
  713:         if ($canedit) {
  714:             $$content_file = &replicate_content($cdom,$cnum,$tempexport,$symb,$count,\$message,$href,'resource');
  715:         } else {
  716:             $$content_file = &replicate_content($cdom,$cnum,$tempexport,$symb,$count,\$message,$href,'noedit');
  717:         }
  718:     } elsif ($symb =~ m-uploaded/$cdom/$cnum-) {
  719:         $$content_file = &replicate_content($cdom,$cnum,$tempexport,$symb,$count,\$message,$href,'uploaded');
  720:     }
  721:     if (@uploads > 0) {
  722:         foreach my $item (@uploads) {
  723:             my $uploadmsg = '';
  724:             &replicate_content($cdom,$cnum,$tempexport,$item,$count,\$uploadmsg,$href,'templateupload');
  725:             if ($uploadmsg) {
  726:                 $$copyresult .= $uploadmsg."\n";
  727:             }
  728:         }
  729:     }
  730:     if ($message) {
  731:         $$copyresult .= $message."\n";
  732:     }
  733: }
  734: 
  735: sub replicate_content {
  736:     my ($cdom,$cnum,$tempexport,$symb,$count,$message,$href,$caller) = @_;
  737:     my ($map,$ind,$url);
  738:     if ($caller eq 'templateupload') {
  739:         $url = $symb;
  740:         $url =~ s#//#/#g;
  741:     } else { 
  742:         ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
  743:     }
  744:     my $content;
  745:     my $filename;
  746:     my $repstatus;
  747:     my $content_name;
  748:     if ($url =~ m-/([^/]+)$-) {
  749:         $filename = $1;
  750:         if (!-e $tempexport.'/resources') {
  751:             mkdir($tempexport.'/resources',0700);
  752:         }
  753:         if (!-e $tempexport.'/resources/'.$count) {
  754:             mkdir($tempexport.'/resources/'.$count,0700);
  755:         }
  756:         my $destination = $tempexport.'/resources/'.$count.'/'.$filename;
  757:         my $copiedfile;
  758:         if ($copiedfile = Apache::File->new('>'.$destination)) {
  759:             my $content;
  760:             if ($caller eq 'resource') {
  761:                 my $respath =  $Apache::lonnet::perlvar{'lonDocRoot'}.'/res';
  762:                 my $filepath = &Apache::lonnet::filelocation($respath,$url);
  763:                 $content = &Apache::lonnet::getfile($filepath);
  764:                 if ($content eq -1) {
  765:                     $$message = 'Could not copy file '.$filename;
  766:                 } else {
  767:                     &extract_media($url,$cdom,$cnum,\$content,$count,$tempexport,$href,$message,'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($url,$cdom,$cnum,\$content,$count,$tempexport,$href,$message,'uploaded');
  776:                     }
  777:                 } else {
  778:                     $$message = 'Could not render '.$url.' server message - '.$rtncode."<br />\n";
  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."<br />\n";
  791:         }
  792:     } else {
  793:         $$message = 'Could not determine name of file for '.$symb."<br />\n";
  794:     }
  795:     if ($repstatus eq 'ok') {
  796:         $content_name = 'resources/'.$count.'/'.$filename;
  797:     }
  798:     return $content_name;
  799: }
  800: 
  801: sub extract_media {
  802:     my ($url,$cdom,$cnum,$content,$count,$tempexport,$href,$message,$caller) = @_;
  803:     my ($dirpath,$container);
  804:     my %allfiles = ();
  805:     my %codebase = ();
  806:     if ($url =~ m-(.*/)([^/]+)$-) {
  807:         $dirpath = $1;
  808:         $container = $2;
  809:     } else {
  810:         $dirpath = $url;
  811:         $container = '';
  812:     }
  813:     &Apache::lonnet::extract_embedded_items(undef,undef,\%allfiles,\%codebase,$content);
  814:     foreach my $embed_file (keys(%allfiles)) {
  815:         my $filename;
  816:         if ($embed_file =~ m#([^/]+)$#) {
  817:             $filename = $1;
  818:         } else {
  819:             $filename = $embed_file;
  820:         }
  821:         my $newname = 'res/'.$filename;
  822:         my ($rtncode,$embed_content,$repstatus);
  823:         my $embed_url;
  824:         if ($embed_file =~ m-^/-) {
  825:             $embed_url = $embed_file;           # points to absolute path
  826:         } else {
  827:             if ($embed_file =~ m-https?://-) {
  828:                 next;                           # points to url
  829:             } else {
  830:                 $embed_url = $dirpath.$embed_file;  # points to relative path
  831:             }
  832:         }
  833:         if ($caller eq 'resource') {
  834:             my $respath =  $Apache::lonnet::perlvar{'lonDocRoot'}.'/res';  
  835:             my $embed_path = &Apache::lonnet::filelocation($respath,$embed_url); 
  836:             $embed_content = &Apache::lonnet::getfile($embed_path);
  837:             unless ($embed_content eq -1) {
  838:                 $repstatus = 'ok';
  839:             }
  840:         } elsif ($caller eq 'uploaded') {
  841:             
  842:             $repstatus = &Apache::lonnet::getuploaded('GET',$embed_url,$cdom,$cnum,\$embed_content,$rtncode);
  843:         }
  844:         if ($repstatus eq 'ok') {
  845:             my $destination = $tempexport.'/resources/'.$count.'/res';
  846:             if (!-e "$destination") {
  847:                 mkdir($destination,0755);
  848:             }
  849:             $destination .= '/'.$filename;
  850:             my $copiedfile;
  851:             if ($copiedfile = Apache::File->new('>'.$destination)) {
  852:                 print $copiedfile $embed_content;
  853:                 push @{$href}, 'resources/'.$count.'/res/'.$filename;
  854:                 my $attrib_regexp = '';
  855:                 if (@{$allfiles{$embed_file}} > 1) {
  856:                     $attrib_regexp = join('|',@{$allfiles{$embed_file}});
  857:                 } else {
  858:                     $attrib_regexp = $allfiles{$embed_file}[0];
  859:                 }
  860:                 $$content =~ s#($attrib_regexp\s*=\s*['"]?)\Q$embed_file\E(['"]?)#$1$newname$2#gi;
  861:                 if ($caller eq 'resource' && $container =~ /\.(problem|library)$/) {
  862:                     $$content =~ s#\Q$embed_file\E#$newname#gi;
  863:                 }
  864:             }
  865:         } else {
  866:             $$message .= 'replication of embedded file - '.$embed_file.' in '.$url.' failed, reason -'.$rtncode."<br />\n";
  867:         }
  868:     }
  869:     return;
  870: }
  871: 
  872: sub store_template {
  873:     my ($contents,$tempexport,$count,$content_type) = @_;
  874:     if ($contents) {
  875:         if ($tempexport) {
  876:             if (!-e $tempexport.'/resources') {
  877:                 mkdir($tempexport.'/resources',0700);
  878:             }
  879:             if (!-e $tempexport.'/resources/'.$count) {
  880:                 mkdir($tempexport.'/resources/'.$count,0700);
  881:             }
  882:             my $destination = $tempexport.'/resources/'.$count.'/'.$content_type.'.xml';
  883:             my $storetemplate;
  884:             if ($storetemplate = Apache::File->new('>'.$destination)) {
  885:                 print $storetemplate $contents;
  886:                 close($storetemplate);
  887:             }
  888:             if ($content_type eq 'external') {
  889:                 return 'resources/'.$count.'/'.$content_type.'.html';
  890:             } else {
  891:                 return 'resources/'.$count.'/'.$content_type.'.xml';
  892:             }
  893:         }
  894:     }
  895: }
  896: 
  897: # Imports the given (name, url) resources into the course
  898: # coursenum, coursedom, and folder must precede the list
  899: sub group_import {
  900:     my $coursenum = shift;
  901:     my $coursedom = shift;
  902:     my $folder = shift;
  903:     my $container = shift;
  904:     my $caller = shift;
  905:     while (@_) {
  906: 	my $name = shift;
  907: 	my $url = shift;
  908:         if (($url =~ m#^/uploaded/$coursedom/$coursenum/(default_\d+\.)(page|sequence)$#) && ($caller eq 'londocs')) {
  909:             my $errtext = '';
  910:             my $fatal = 0;
  911:             my $newmapstr = '<map>'."\n".
  912:                             '<resource id="1" src="" type="start"></resource>'."\n".
  913:                             '<link from="1" to="2" index="1"></link>'."\n".
  914:                             '<resource id="2" src="" type="finish"></resource>'."\n".
  915:                             '</map>';
  916:             $env{'form.output'}=$newmapstr;
  917:             my $result=&Apache::lonnet::finishuserfileupload($coursenum,$coursedom,
  918:                                                 'output',$1.$2);
  919:             if ($result != m|^/uploaded/|) {
  920:                 $errtext.='Map not saved: A network error occured when trying to save the new map. ';
  921:                 $fatal = 2;
  922:             }
  923:             if ($fatal) {
  924:                 return ($errtext,$fatal);
  925:             }
  926:         }
  927: 	if ($url) {
  928: 	    my $idx = &Apache::lonratedt::getresidx($url);
  929: 	    $Apache::lonratedt::order[$#Apache::lonratedt::order+1]=$idx;
  930: 	    my $ext = 'false';
  931: 	    if ($url=~/^http:\/\//) { $ext = 'true'; }
  932: 	    $url =~ s/:/\&colon;/g;
  933: 	    $name =~ s/:/\&colon;/g;
  934: 	    $Apache::lonratedt::resources[$idx] = 
  935: 		join ':', ($name, $url, $ext, 'normal', 'res');
  936: 	}
  937:     }
  938:     return &storemap($coursenum, $coursedom, $folder.'.'.$container);
  939: }
  940: 
  941: sub breadcrumbs {
  942:     my ($where)=@_;
  943:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  944:     my (@folders);
  945:     if ($env{'form.pagepath'}) {
  946:         @folders = split('&',$env{'form.pagepath'});
  947:     } else {
  948:         @folders=split('&',$env{'form.folderpath'});
  949:     }
  950:     my $folderpath;
  951:     my $cpinfo='';
  952:     if ($env{'form.markedcopy_url'}) {
  953: 	&Apache::lonnet::logthis('Found '.$env{'form.markedcopy_url'});
  954:        $cpinfo='&markedcopy_url='.
  955:                &escape($env{'form.markedcopy_url'}).
  956:                '&markedcopy_title='.
  957:                &escape($env{'form.markedcopy_title'});
  958:     }
  959:     while (@folders) {
  960: 	my $folder=shift(@folders);
  961: 	my $foldername=shift(@folders);
  962: 	if ($folderpath) {$folderpath.='&';}
  963: 	$folderpath.=$folder.'&'.$foldername;
  964: 	my $url='/adm/coursedocs?folderpath='.
  965: 	    &escape($folderpath);
  966: 	    &Apache::lonhtmlcommon::add_breadcrumb(
  967: 		      {'href'=>$url.$cpinfo,
  968: 		       'title'=>&unescape($foldername),
  969: 		       'text'=>'<font size="+1">'.
  970: 			   &unescape($foldername).'</font>'
  971: 		       });
  972: 		       
  973: 						 
  974:     }
  975:     return &Apache::lonhtmlcommon::breadcrumbs(undef,undef,0,'nohelp');
  976: }
  977: 
  978: sub editor {
  979:     my ($r,$coursenum,$coursedom,$folder,$allowed,$upload_output)=@_;
  980:     my $errtext='';
  981:     my $fatal=0;
  982:     my $container='sequence';
  983:     if ($env{'form.pagepath'}) {
  984:         $container='page';
  985:     }
  986:     ($errtext,$fatal)=
  987:               &mapread($coursenum,$coursedom,$folder.'.'.$container);
  988:     if ($#Apache::lonratedt::order<1) {
  989: 	my $idx=&Apache::lonratedt::getresidx();
  990: 	if ($idx<=0) { $idx=1; }
  991:        	$Apache::lonratedt::order[0]=$idx;
  992:         $Apache::lonratedt::resources[$idx]='';
  993:     }
  994:     if (defined($env{'form.markcopy'})) {
  995: # Mark for copying
  996: 	my ($title,$url)=split(':',$Apache::lonratedt::resources[$Apache::lonratedt::order[$env{'form.markcopy'}]]);
  997: 	$env{'form.markedcopy_title'}=$title;
  998: 	$env{'form.markedcopy_url'}=$url;
  999:     }
 1000:     $r->print(&breadcrumbs($folder));
 1001:     if ($fatal) {
 1002: 	   $r->print('<p><font color="red">'.$errtext.'</font></p>');
 1003:     } else {
 1004: # ------------------------------------------------------------ Process commands
 1005: 
 1006: # ---------------- if they are for this folder and user allowed to make changes
 1007: 	if (($allowed) && ($env{'form.folder'} eq $folder)) {
 1008: # set parameters and change order
 1009: 	    if (defined($env{'form.setparms'})) {
 1010: 		my $idx=$env{'form.setparms'};
 1011: # set parameters
 1012: 		if ($env{'form.randpick_'.$idx}) {
 1013: 		    &Apache::lonratedt::storeparameter($idx,'parameter_randompick',$env{'form.randpick_'.$idx},'int_pos');
 1014: 		} else {
 1015: 		    &Apache::lonratedt::delparameter($idx,'parameter_randompick');
 1016: 		}
 1017: 		if ($env{'form.hidprs_'.$idx}) {
 1018: 		    &Apache::lonratedt::storeparameter($idx,'parameter_hiddenresource','yes','string_yesno');
 1019: 		} else {
 1020: 		    &Apache::lonratedt::delparameter($idx,'parameter_hiddenresource');
 1021: 		}
 1022: 		if ($env{'form.encprs_'.$idx}) {
 1023: 		    &Apache::lonratedt::storeparameter($idx,'parameter_encrypturl','yes','string_yesno');
 1024: 		} else {
 1025: 		    &Apache::lonratedt::delparameter($idx,'parameter_encrypturl');
 1026: 		}
 1027: 
 1028: 		if ($env{'form.newpos'}) {
 1029: # change order
 1030: 
 1031: 		    my $newpos=$env{'form.newpos'}-1;
 1032: 		    my $currentpos=$env{'form.currentpos'}-1;
 1033: 		    my $i;
 1034: 		    my @neworder=();
 1035: 		    if ($newpos>$currentpos) {
 1036: # moving stuff up
 1037: 			for ($i=0;$i<$currentpos;$i++) {
 1038: 			    $neworder[$i]=$Apache::lonratedt::order[$i];
 1039: 			}
 1040: 			for ($i=$currentpos;$i<$newpos;$i++) {
 1041: 			    $neworder[$i]=$Apache::lonratedt::order[$i+1];
 1042: 			}
 1043:                         $neworder[$newpos]=$Apache::lonratedt::order[$currentpos];
 1044: 			for ($i=$newpos+1;$i<=$#Apache::lonratedt::order;$i++) {
 1045: 			    $neworder[$i]=$Apache::lonratedt::order[$i];
 1046: 			}
 1047: 		    } else {
 1048: # moving stuff down
 1049: 			for ($i=0;$i<$newpos;$i++) {
 1050: 			    $neworder[$i]=$Apache::lonratedt::order[$i];
 1051: 			}
 1052: 			$neworder[$newpos]=$Apache::lonratedt::order[$currentpos];
 1053: 			for ($i=$newpos+1;$i<$currentpos+1;$i++) {
 1054: 			    $neworder[$i]=$Apache::lonratedt::order[$i-1];
 1055: 			}
 1056: 			for ($i=$currentpos+1;$i<=$#Apache::lonratedt::order;$i++) {
 1057: 			    $neworder[$i]=$Apache::lonratedt::order[$i];
 1058: 			}
 1059: 		    }
 1060: 		    @Apache::lonratedt::order=@neworder;
 1061: 		}
 1062: # store the changed version
 1063: 
 1064: 		($errtext,$fatal)=&storemap($coursenum,$coursedom,$folder.'.'.$container);
 1065: 		if ($fatal) {
 1066: 		    $r->print('<p><font color="red">'.$errtext.'</font></p>');
 1067: 		    return;
 1068: 		}
 1069: 		
 1070: 	    }
 1071: 	    if ($env{'form.pastemarked'}) {
 1072: # paste resource to end of list
 1073:                 my $url=$env{'form.markedcopy_url'};
 1074: 		my $title=$env{'form.markedcopy_title'};
 1075: # Maps need to be copied first
 1076: 		if (($url=~/\.(page|sequence)$/) || ($url=~/^\/uploaded\//)) {
 1077: 		    $title=&mt('Copy of').' '.$title;
 1078:                     my $newid=$$.time;
 1079: 		    $url=~/^(.+)\.(\w+)$/;
 1080: 		    my $newurl=$1.$newid.'.'.$2;
 1081: 		    my $storefn=$newurl;
 1082:                     $storefn=~s/^\/\w+\/\w+\/\w+\///;
 1083: 		    &Apache::loncreatecourse::writefile
 1084: 			($env{'request.course.id'},$storefn,
 1085: 			 &Apache::lonnet::getfile($url));
 1086: 		    $url=$newurl;
 1087: 		}
 1088: 		$title=~s/\</\&lt\;/g;
 1089: 		$title=~s/\>/\&gt\;/g;
 1090: 		$title=~s/\:/\&colon;/g;
 1091: 		my $ext='false';
 1092: 		if ($url=~/^http\:\/\//) { $ext='true'; }
 1093: 		$url=~s/\:/\&colon;/g;
 1094: # Now insert the URL at the bottom
 1095:                 my $newidx=&Apache::lonratedt::getresidx($url);
 1096: 		$Apache::lonratedt::resources[$newidx]=
 1097: 		    $title.':'.$url.':'.$ext.':normal:res';
 1098: 		$Apache::lonratedt::order[1+$#Apache::lonratedt::order]=$newidx;
 1099: # Store the result
 1100: 		($errtext,$fatal)=&storemap($coursenum,$coursedom,$folder.'.'.$container);
 1101: 		if ($fatal) {
 1102: 		    $r->print('<p><font color="red">'.$errtext.'</font></p>');
 1103: 		    return;
 1104: 		}
 1105: 
 1106: 	    }
 1107:             $r->print($upload_output);
 1108: 	    if ($env{'form.cmd'}) {
 1109:                 my ($cmd,$idx)=split(/\_/,$env{'form.cmd'});
 1110:                 if ($cmd eq 'del') {
 1111: 		    my (undef,$url)=split(':',$Apache::lonratedt::resources[$Apache::lonratedt::order[$idx]]);
 1112: 		    if (($url=~m|/+uploaded/\Q$coursedom\E/\Q$coursenum\E/|) &&
 1113: 			($url!~/\.(page|sequence|problem|exam|quiz|assess|survey|form|library|task)$/)) {
 1114: 			&Apache::lonnet::removeuploadedurl($url);
 1115: 		    } else {
 1116: 			&Apache::lonratedt::makezombie($Apache::lonratedt::order[$idx]);
 1117: 		    }
 1118: 		    for (my $i=$idx;$i<$#Apache::lonratedt::order;$i++) {
 1119:                         $Apache::lonratedt::order[$i]=
 1120:                           $Apache::lonratedt::order[$i+1];
 1121:                     }
 1122:                     $#Apache::lonratedt::order--;
 1123:                 } elsif ($cmd eq 'cut') {
 1124: 		    my (undef,$url)=split(':',$Apache::lonratedt::resources[$Apache::lonratedt::order[$idx]]);
 1125: 		    &Apache::lonratedt::makezombie($Apache::lonratedt::order[$idx]);
 1126: 		    for (my $i=$idx;$i<$#Apache::lonratedt::order;$i++) {
 1127:                         $Apache::lonratedt::order[$i]=
 1128:                           $Apache::lonratedt::order[$i+1];
 1129:                     }
 1130:                     $#Apache::lonratedt::order--;
 1131:                 } elsif ($cmd eq 'up') {
 1132: 		  if (($idx) && (defined($Apache::lonratedt::order[$idx-1]))) {
 1133:                     my $i=$Apache::lonratedt::order[$idx-1];
 1134:                     $Apache::lonratedt::order[$idx-1]=
 1135: 			$Apache::lonratedt::order[$idx];
 1136:                     $Apache::lonratedt::order[$idx]=$i;
 1137: 		   }
 1138:                 } elsif ($cmd eq 'down') {
 1139: 		   if (defined($Apache::lonratedt::order[$idx+1])) {
 1140:                     my $i=$Apache::lonratedt::order[$idx+1];
 1141:                     $Apache::lonratedt::order[$idx+1]=
 1142: 			$Apache::lonratedt::order[$idx];
 1143:                     $Apache::lonratedt::order[$idx]=$i;
 1144: 		   }
 1145:                 } elsif ($cmd eq 'rename') {
 1146:                     my $ratstr = $Apache::lonratedt::resources[$Apache::lonratedt::order[$idx]];
 1147:                     my ($rtitle,@rrest)=split(/\:/,
 1148:                        $Apache::lonratedt::resources[
 1149: 				       $Apache::lonratedt::order[$idx]]);
 1150:                     my $comment=
 1151:                      &HTML::Entities::decode($env{'form.title'});
 1152:                     $comment=~s/\</\&lt\;/g;
 1153:                     $comment=~s/\>/\&gt\;/g;
 1154:                     $comment=~s/\:/\&colon;/g;
 1155: 		    if ($comment=~/\S/) {
 1156: 			$Apache::lonratedt::resources[
 1157: 				       $Apache::lonratedt::order[$idx]]=
 1158: 				            $comment.':'.join(':',@rrest);
 1159: 		    }
 1160:                 }
 1161: # Store the changed version
 1162: 		($errtext,$fatal)=&storemap($coursenum,$coursedom,
 1163: 					    $folder.'.'.$container);
 1164: 		if ($fatal) {
 1165: 		    $r->print('<p><font color="red">'.$errtext.'</font></p>');
 1166: 		    return;
 1167: 		}
 1168:             }
 1169: # Group import/search
 1170: 	    if ($env{'form.importdetail'}) {
 1171: 		my @imports;
 1172: 		foreach (split(/\&/,$env{'form.importdetail'})) {
 1173: 		    if (defined($_)) {
 1174: 			my ($name,$url)=split(/\=/,$_);
 1175: 			$name=&unescape($name);
 1176: 			$url=&unescape($url);
 1177: 			push @imports, $name, $url;
 1178: 		    }
 1179: 		}
 1180: # Store the changed version
 1181: 		($errtext,$fatal)=group_import($coursenum, $coursedom, $folder,
 1182: 					       $container,'londocs',@imports);
 1183: 		if ($fatal) {
 1184: 		    $r->print('<p><font color="red">'.$errtext.'</font></p>');
 1185: 		    return;
 1186: 		}
 1187:             }
 1188: # Loading a complete map
 1189: 	   if ($env{'form.loadmap'}) {
 1190:                if ($env{'form.importmap'}=~/\w/) {
 1191: 	          foreach (&Apache::lonsequence::attemptread(&Apache::lonnet::filelocation('',$env{'form.importmap'}))) {
 1192: 		      my ($title,$url,$ext,$type)=split(/\:/,$_);
 1193:                       my $idx=&Apache::lonratedt::getresidx($url);
 1194:                       $Apache::lonratedt::resources[$idx]=$_;
 1195:                       $Apache::lonratedt::order
 1196: 		          [$#Apache::lonratedt::order+1]=$idx;
 1197: 	          }
 1198: # Store the changed version
 1199:   	          ($errtext,$fatal)=&storemap($coursenum,$coursedom,
 1200: 					   $folder.'.'.$container);
 1201: 	          if ($fatal) {
 1202: 		      $r->print('<p><font color="red">'.$errtext.'</font></p>');
 1203: 		      return;
 1204: 	          }
 1205:                } else {
 1206:                    $r->print('<p><font color="red">'.&mt('No map selected.').'</font></p>');
 1207:                }
 1208:            }
 1209:        }
 1210: # ---------------------------------------------------------------- End commands
 1211: # ---------------------------------------------------------------- Print screen
 1212:         my $idx=0;
 1213: 	my $shown=0;
 1214:         $r->print('<table>');
 1215:         foreach (@Apache::lonratedt::order) {
 1216:            my ($name,$url)=split(/\:/,$Apache::lonratedt::resources[$_]);
 1217: 	   $name=&Apache::lonratsrv::qtescape($name);
 1218: 	   $url=&Apache::lonratsrv::qtescape($url);
 1219:            unless ($name) {  $name=(split(/\//,$url))[-1]; }
 1220:            unless ($name) { $idx++; next; }
 1221:            $r->print(&entryline($idx,$name,$url,$folder,$allowed,$_,$coursenum));
 1222:            $idx++;
 1223: 	   $shown++;
 1224:         }
 1225: 	unless ($shown) {
 1226: 	    $r->print('<tr><td>'.&mt('Currently no documents.').'</td></tr>');
 1227: 	}
 1228:         $r->print("\n</table>\n");
 1229: 	if ($env{'form.markedcopy_url'}) {
 1230: 	    $r->print(<<ENDPASTE);
 1231: <p><form name="pasteform" action="/adm/coursedocs" method="post">
 1232: <input type="hidden" name="markedcopy_url" value="$env{'form.markedcopy_url'}" />
 1233: <input type="hidden" name="markedcopy_title" value="$env{'form.markedcopy_title'}" />
 1234: ENDPASTE
 1235:             $r->print(
 1236: 	   '<input type="submit" name="pastemarked" value="'.&mt('Paste').
 1237: 		      '" /> '.&Apache::loncommon::filedescription(
 1238: 		(split(/\./,$env{'form.markedcopy_url'}))[-1]).': '.
 1239: 		      $env{'form.markedcopy_title'});
 1240:             if ($container eq 'page') {
 1241: 		$r->print(<<PAGEINFO);
 1242: <input type="hidden" name="pagepath" value="$env{'form.pagepath'}" />
 1243: <input type="hidden" name="pagesymb" value="$env{'form.pagesymb'}" />
 1244: PAGEINFO
 1245:             } else {
 1246: 		$r->print(<<FOLDERINFO);
 1247: <input type="hidden" name="folderpath" value="$env{'form.folderpath'}" />
 1248: FOLDERINFO
 1249: 	    }
 1250: 	    $r->print('</form></p>');
 1251: 	}
 1252:     }
 1253: }
 1254: 
 1255: sub process_file_upload {
 1256:     my ($upload_output,$coursenum,$coursedom,$allfiles,$codebase,$uploadcmd) = @_;
 1257: # upload a file, if present
 1258:     my $parseaction;
 1259:    if ($env{'form.parserflag'}) {
 1260:         $parseaction = 'parse';
 1261:     }
 1262:     my $phase_status;
 1263:     my $folder=$env{'form.folder'};
 1264:     if ($folder eq '') {
 1265:         $folder='default';
 1266:     }
 1267:     if ( ($folder=~/^$uploadcmd/) || ($uploadcmd eq 'default') ) {
 1268:         my $errtext='';
 1269:         my $fatal=0;
 1270:         my $container='sequence';
 1271:         if ($env{'form.pagepath'}) {
 1272:             $container='page';
 1273:         }
 1274:         ($errtext,$fatal)=
 1275:               &mapread($coursenum,$coursedom,$folder.'.'.$container);
 1276:         if ($#Apache::lonratedt::order<1) {
 1277:             $Apache::lonratedt::order[0]=1;
 1278:             $Apache::lonratedt::resources[1]='';
 1279:         }
 1280:         if ($fatal) {
 1281:             return 'failed';
 1282:         }
 1283:         my $destination = 'docs/';
 1284:         if ($folder =~ /^supplemental/) {
 1285:             $destination = 'supplemental/';
 1286:         }
 1287:         if (($folder eq 'default') || ($folder eq 'supplemental')) {
 1288:             $destination .= 'default/';
 1289:         } elsif ($folder =~ /^(default|supplemental)_(\d+)$/) {
 1290:             $destination .=  $2.'/';
 1291:         }
 1292: # this is for a course, not a user, so set coursedoc flag
 1293: # probably the only place in the system where this should be "1"
 1294:         my $newidx=&Apache::lonratedt::getresidx();
 1295:         $destination .= $newidx;
 1296:         my $url=&Apache::lonnet::userfileupload('uploaddoc',1,$destination,
 1297: 						$parseaction,$allfiles,
 1298: 						$codebase);
 1299:         my $ext='false';
 1300:         if ($url=~/^http\:\/\//) { $ext='true'; }
 1301:         $url=~s/\:/\&colon;/g;
 1302:         my $comment=$env{'form.comment'};
 1303:         $comment=~s/\</\&lt\;/g;
 1304:         $comment=~s/\>/\&gt\;/g;
 1305:         $comment=~s/\:/\&colon;/g;
 1306:         if ($folder=~/^supplemental/) {
 1307:               $comment=time.'___&&&___'.$env{'user.name'}.'___&&&___'.
 1308:                   $env{'user.domain'}.'___&&&___'.$comment;
 1309:         }
 1310: 
 1311:         $Apache::lonratedt::resources[$newidx]=
 1312:                   $comment.':'.$url.':'.$ext.':normal:res';
 1313:         $Apache::lonratedt::order[$#Apache::lonratedt::order+1]= $newidx;
 1314:         ($errtext,$fatal)=&storemap($coursenum,$coursedom,
 1315: 				    $folder.'.'.$container);
 1316:         if ($fatal) {
 1317:             $$upload_output .= '<p><font color="red">'.$errtext.'</font></p>';
 1318:             return 'failed';
 1319:         } else {
 1320:             if ($parseaction eq 'parse') {
 1321:                 my $total_embedded = keys(%{$allfiles});
 1322:                 if ($total_embedded > 0) {
 1323:                     my $num = 0;
 1324:                     $$upload_output .= 'This file contains embedded multimedia objects, which need to be uploaded to LON-CAPA.<br />
 1325:    <form name="upload_embedded" action="/adm/coursedocs"
 1326:                   method="post" enctype="multipart/form-data">
 1327:    <input type="hidden" name="folderpath" value="'.$env{'form.folderpath'}.'" />   <input type="hidden" name="cmd" value="upload_embedded" />
 1328:    <input type="hidden" name="newidx" value="'.$newidx.'" />
 1329:    <input type="hidden" name="primaryurl" value="'.&escape($url).'" />
 1330:    <input type="hidden" name="phasetwo" value="'.$total_embedded.'" />';
 1331:                     $$upload_output .= '<b>Upload embedded files</b>:<br />
 1332:    <table>';
 1333:                     foreach my $embed_file (keys(%{$allfiles})) {
 1334:                         $$upload_output .= '<tr><td>'.$embed_file.
 1335:           '<input name="embedded_item_'.$num.'" type="file" />
 1336:            <input name="embedded_orig_'.$num.'" type="hidden" value="'.&escape($embed_file).'" />';
 1337:                         my $attrib;
 1338:                         if (@{$$allfiles{$embed_file}} > 1) {
 1339:                             $attrib = join(':',@{$$allfiles{$embed_file}});
 1340:                         } else {
 1341:                             $attrib = $$allfiles{$embed_file}[0];
 1342:                         }
 1343:                         $$upload_output .=
 1344:            '<input name="embedded_attrib_'.$num.'" type="hidden" value="'.$attrib.'" />';
 1345:                         if (exists($$codebase{$embed_file})) {
 1346:                             $$upload_output .= 
 1347:           '<input name="codebase_'.$num.'" type="hidden" value="'.&escape($$codebase{$embed_file}).'" />';
 1348:                         }
 1349:                         $$upload_output .= '</td></tr>';
 1350:                         $num ++;
 1351:                     }
 1352:                     $phase_status = 'phasetwo';
 1353:                     $$upload_output .= '</table><br />
 1354:    <input type ="submit" value="Complete upload" />
 1355:    </form>';
 1356:                 } else {
 1357:                     $$upload_output .= 'No embedded items identified<br />';
 1358:                 }
 1359:             }
 1360:         }
 1361:     }
 1362:     return $phase_status;
 1363: }
 1364: 
 1365: sub process_secondary_uploads {
 1366:     my ($upload_output,$coursedom,$coursenum,$formname,$num,$newidx) = @_;
 1367:     my $folder=$env{'form.folder'};
 1368:     my $destination = 'docs/';
 1369:     if ($folder =~ /^supplemental/) {
 1370:         $destination = 'supplemental/';
 1371:     }
 1372:     if (($folder eq 'default') || ($folder eq 'supplemental')) {
 1373:         $destination .= 'default/';
 1374:     } elsif ($folder =~ /^(default|supplemental)_(\d+)$/) {
 1375:         $destination .=  $2.'/';
 1376:     }
 1377:     $destination .= $newidx;
 1378:     my ($url,$filename);
 1379:     $url=&Apache::lonnet::userfileupload($formname.$num,1,$destination);
 1380:     ($filename) = ($url =~ m-^/uploaded/$coursedom/$coursenum/$destination/(.+)$-);
 1381:     return $filename;
 1382: }
 1383: 
 1384: # --------------------------------------------------------------- An entry line
 1385: 
 1386: sub entryline {
 1387:     my ($index,$title,$url,$folder,$allowed,$residx,$coursenum)=@_;
 1388:     $title=~s/\&colon\;/\:/g;
 1389:     $title=&HTML::Entities::encode(&HTML::Entities::decode(
 1390:      &unescape($title)),'"<>&\'');
 1391:     my $renametitle=$title;
 1392:     my $foldertitle=$title;
 1393:     my $pagetitle=$title;
 1394:     my $orderidx=$Apache::lonratedt::order[$index];
 1395:     if ($title=~ /^(\d+)___&amp;&amp;&amp;___(\w+)___&amp;&amp;&amp;___(\w+)___&amp;&amp;&amp;___(.*)$/	) { 
 1396: 	$foldertitle=&Apache::lontexconvert::msgtexconverted($4);
 1397: 	$renametitle=$4;
 1398: 	$title='<i>'.&Apache::lonlocal::locallocaltime($1).'</i> '.
 1399: 	    &Apache::loncommon::plainname($2,$3).': <br />'.
 1400: 	    $foldertitle;
 1401:     }
 1402:     $renametitle=~s/\\/\\\\/g;
 1403:     $renametitle=~s/\&quot\;/\\\"/g;
 1404:     my $line='<tr>';
 1405: # Edit commands
 1406:     my $container;
 1407:     my $folderpath;
 1408:     if ($env{'form.folderpath'}) {
 1409:         $container = 'sequence';
 1410: 	$folderpath=&escape($env{'form.folderpath'});
 1411: 	# $htmlfoldername=&HTML::Entities::encode($env{'form.foldername'},'<>&"');
 1412:     }
 1413:     my ($pagepath,$pagesymb);
 1414:     if ($env{'form.pagepath'}) {
 1415:         $container = 'page';
 1416:         $pagepath=&escape($env{'form.pagepath'});
 1417:         $pagesymb=&escape($env{'form.pagesymb'});
 1418:     }
 1419:     my $cpinfo='';
 1420:     if ($env{'form.markedcopy_url'}) {
 1421:        $cpinfo='&markedcopy_url='.
 1422:                &escape($env{'form.markedcopy_url'}).
 1423:                '&markedcopy_title='.
 1424:                &escape($env{'form.markedcopy_title'});
 1425:     }
 1426:     if ($allowed) {
 1427: 	my $incindex=$index+1;
 1428: 	my $selectbox='';
 1429: 	if (($folder!~/^supplemental/) &&
 1430: 	    ($#Apache::lonratedt::order>0) && 
 1431: 	    ((split(/\:/,
 1432: 	     $Apache::lonratedt::resources[$Apache::lonratedt::order[0]]))[1] 
 1433: 	     ne '') && 
 1434: 	    ((split(/\:/,
 1435: 	     $Apache::lonratedt::resources[$Apache::lonratedt::order[1]]))[1] 
 1436: 	     ne '')) {
 1437: 	    $selectbox=
 1438: 		'<input type="hidden" name="currentpos" value="'.$incindex.'" />'.
 1439: 		'<select name="newpos" onChange="this.form.submit()">';
 1440: 	    for (my $i=1;$i<=$#Apache::lonratedt::order+1;$i++) {
 1441: 		if ($i==$incindex) {
 1442: 		    $selectbox.='<option value="" selected="1">('.$i.')</option>';
 1443: 		} else {
 1444: 		    $selectbox.='<option value="'.$i.'">'.$i.'</option>';
 1445: 		}
 1446: 	    }
 1447: 	    $selectbox.='</select>';
 1448: 	}
 1449: 	my %lt=&Apache::lonlocal::texthash(
 1450:                 'up' => 'Move Up',
 1451: 		'dw' => 'Move Down',
 1452: 		'rm' => 'Remove',
 1453:                 'ct' => 'Cut',
 1454: 		'rn' => 'Rename',
 1455: 		'cp' => 'Copy');
 1456: 	my $nocopy=0;
 1457:         if ($url=~/\.(page|sequence)$/) {
 1458: 	    foreach (&Apache::lonsequence::attemptread(&Apache::lonnet::filelocation('',$url))) {
 1459: 		my ($title,$url,$ext,$type)=split(/\:/,$_);
 1460: 		if (($url=~/\.(page|sequence)/) && ($type ne 'zombie')) {
 1461: 		    $nocopy=1;
 1462: 		    last;
 1463: 		}
 1464: 	    }
 1465: 	}
 1466:         my $copylink='&nbsp;';
 1467:         if ($env{'form.pagepath'}) {
 1468:            unless ($nocopy) {
 1469:                $copylink=(<<ENDCOPY);
 1470: <a href='javascript:markcopy("$pagepath","$index","$renametitle","page","$pagesymb");'>
 1471: <font size="-2" color="#000099">$lt{'cp'}</font></a></td>
 1472: ENDCOPY
 1473:             }
 1474:             $line.=(<<END);
 1475: <form name="entry_$index" action="/adm/coursedocs" method="post">
 1476: <input type="hidden" name="pagepath" value="$env{'form.pagepath'}" />
 1477: <input type="hidden" name="pagesymb" value="$env{'form.pagesymb'}" />
 1478: <input type="hidden" name="markedcopy_url" value="$env{'form.markedcopy_url'}" />
 1479: <input type="hidden" name="markedcopy_title" value="$env{'form.markedcopy_title'}" />
 1480: <input type="hidden" name="setparms" value="$orderidx" />
 1481: <td><table border='0' cellspacing='2' cellpadding='0'>
 1482: <tr><td bgcolor="#DDDDDD">
 1483: <a href='/adm/coursedocs?cmd=up_$index&pagepath=$pagepath&pagesymb=$pagesymb$cpinfo'>
 1484: <img src="${iconpath}move_up.gif" alt='$lt{'up'}' border='0' /></a></td></tr>
 1485: <tr><td bgcolor="#DDDDDD">
 1486: <a href='/adm/coursedocs?cmd=down_$index&pagepath=$pagepath&pagesymb=$pagesymb$cpinfo'>
 1487: <img src="${iconpath}move_down.gif" alt='$lt{'dw'}' border='0' /></a></td></tr>
 1488: </table></td>
 1489: <td>$selectbox
 1490: </td><td bgcolor="#DDDDDD">
 1491: <a href='javascript:removeres("$pagepath","$index","$renametitle","page","$pagesymb");'>
 1492: <font size="-2" color="#990000">$lt{'rm'}</font></a>
 1493: <a href='javascript:cutres("$pagepath","$index","$renametitle","page","$pagesymb");'>
 1494: <font size="-2" color="#550044">$lt{'ct'}</font></a>
 1495: <a href='javascript:changename("$pagepath","$index","$renametitle","page","$pagesymb");'>
 1496: <font size="-2" color="#009900">$lt{'rn'}</font></a>
 1497: $copylink
 1498: END
 1499:         } else {
 1500:            unless ($nocopy) {
 1501:                $copylink=(<<ENDCOPY);
 1502: <a href='javascript:markcopy("$folderpath","$index","$renametitle","sequence");'>
 1503: <font size="-2" color="#000099">$lt{'cp'}</font></a></td>
 1504: ENDCOPY
 1505:             }
 1506:             $line.=(<<END); 
 1507: <form name="entry_$index" action="/adm/coursedocs" method="post">
 1508: <input type="hidden" name="folderpath" value="$env{'form.folderpath'}" />
 1509: <input type="hidden" name="markedcopy_url" value="$env{'form.markedcopy_url'}" />
 1510: <input type="hidden" name="markedcopy_title" value="$env{'form.markedcopy_title'}" />
 1511: <input type="hidden" name="setparms" value="$orderidx" />
 1512: <td><table border='0' cellspacing='2' cellpadding='0'>
 1513: <tr><td bgcolor="#DDDDDD">
 1514: <a href='/adm/coursedocs?cmd=up_$index&folderpath=$folderpath$cpinfo'>
 1515: <img src="${iconpath}move_up.gif" alt='$lt{'up'}' border='0' /></a></td></tr>
 1516: <tr><td bgcolor="#DDDDDD">
 1517: <a href='/adm/coursedocs?cmd=down_$index&folderpath=$folderpath$cpinfo'>
 1518: <img src="${iconpath}move_down.gif" alt='$lt{'dw'}' border='0' /></a></td></tr>
 1519: </table></td>
 1520: <td>$selectbox
 1521: </td><td bgcolor="#DDDDDD">
 1522: <a href='javascript:removeres("$folderpath","$index","$renametitle","sequence");'>
 1523: <font size="-2" color="#990000">$lt{'rm'}</font></a>
 1524: <a href='javascript:cutres("$folderpath","$index","$renametitle","sequence");'>
 1525: <font size="-2" color="#550044">$lt{'ct'}</font></a>
 1526: <a href='javascript:changename("$folderpath","$index","$renametitle","sequence");'>
 1527: <font size="-2" color="#009900">$lt{'rn'}</font></a>
 1528: $copylink
 1529: END
 1530:         }
 1531:     }
 1532: # Figure out what kind of a resource this is
 1533:     my ($extension)=($url=~/\.(\w+)$/);
 1534:     my $uploaded=($url=~/^\/*uploaded\//);
 1535:     my $icon=&Apache::loncommon::icon($url);
 1536:     my $isfolder=0;
 1537:     my $ispage=0;
 1538:     my $folderarg;
 1539:     my $pagearg;
 1540:     my $pagefile;
 1541:     if ($uploaded) {
 1542: 	if ($extension eq 'sequence') {
 1543: 	    $icon=$iconpath.'/folder_closed.gif';
 1544: 	    $url=~/$coursenum\/([\/\w]+)\.sequence$/;
 1545: 	    $url='/adm/coursedocs?';
 1546: 	    $folderarg=$1;
 1547: 	    $isfolder=1;
 1548:         } elsif ($extension eq 'page') {
 1549:             $icon=$iconpath.'/page.gif';
 1550:             $url=~/$coursenum\/([\/\w]+)\.page$/;
 1551:             $pagearg=$1;
 1552:             $url='/adm/coursedocs?';
 1553:             $ispage=1;
 1554: 	} else {
 1555: 	    &Apache::lonnet::allowuploaded('/adm/coursedoc',$url);
 1556: 	}
 1557:     }
 1558:     $url=~s-^http(\&colon\;|:)//-/adm/wrapper/ext/-;
 1559:     if ((!$isfolder) && ($residx) && ($folder!~/supplemental/) && (!$ispage)) {
 1560: 	my $symb=&Apache::lonnet::symbclean(
 1561:           &Apache::lonnet::declutter('uploaded/'.
 1562:            $env{'course.'.$env{'request.course.id'}.'.domain'}.'/'.
 1563:            $env{'course.'.$env{'request.course.id'}.'.num'}.'/'.$folder.
 1564:            '.sequence').
 1565:            '___'.$residx.'___'.
 1566: 	   &Apache::lonnet::declutter($url));
 1567: 	(undef,undef,$url)=&Apache::lonnet::decode_symb($symb);
 1568: 	$url=&Apache::lonnet::clutter($url);
 1569: 	if ($url=~/^\/*uploaded\//) {
 1570: 	    $url=~/\.(\w+)$/;
 1571: 	    my $embstyle=&Apache::loncommon::fileembstyle($1);
 1572: 	    if (($embstyle eq 'img') || ($embstyle eq 'emb')) {
 1573: 		$url='/adm/wrapper'.$url;
 1574: 	    } elsif ($embstyle eq 'ssi') {
 1575: 		#do nothing with these
 1576: 	    } elsif ($url!~/\.(sequence|page)$/) {
 1577: 		$url='/adm/coursedocs/showdoc'.$url;
 1578: 	    }
 1579: 	} elsif ($url=~m|^/ext/|) { 
 1580: 	    $url='/adm/wrapper'.$url;
 1581: 	}
 1582: 	$url.=(($url=~/\?/)?'&':'?').'symb='.&escape($symb);
 1583: 	if ($container eq 'page') {
 1584: 	    my $symb=$env{'form.pagesymb'};
 1585: 	    	    
 1586: 	    $url=&Apache::lonnet::clutter((&Apache::lonnet::decode_symb($symb))[2]);
 1587: 	    $url.=(($url=~/\?/)?'&':'?').'symb='.&escape($symb);
 1588: 	}
 1589:     }
 1590:     my $parameterset='&nbsp;';
 1591:     if ($isfolder || $extension eq 'sequence') {
 1592: 	my $foldername=&escape($foldertitle);
 1593: 	my $folderpath=$env{'form.folderpath'};
 1594: 	if ($folderpath) { $folderpath.='&' };
 1595: 	$folderpath.=$folderarg.'&'.$foldername;
 1596: 	$url.='folderpath='.&escape($folderpath).$cpinfo;
 1597: 	$parameterset='<label>'.&mt('Randomly Pick: ').
 1598: 	    '<input type="text" size="4" onChange="this.form.submit()" name="randpick_'.$orderidx.'" value="'.
 1599: 	    (&Apache::lonratedt::getparameter($orderidx,
 1600:                                               'parameter_randompick'))[0].
 1601:                                               '" />'.
 1602: '<font size="-2"><a href="javascript:void(0)">'.&mt('Store').'</a></font></label>';
 1603:        
 1604:     }
 1605:     if ($ispage) {
 1606:         my $pagename=&escape($pagetitle);
 1607:         my $pagepath;
 1608:         my $folderpath=$env{'form.folderpath'};
 1609:         if ($folderpath) { $pagepath = $folderpath.'&' };
 1610:         $pagepath.=$pagearg.'&'.$pagename;
 1611: 	my $symb=$env{'form.pagesymb'};
 1612: 	if (!$symb) {
 1613: 	    my $path='uploaded/'.
 1614: 		$env{'course.'.$env{'request.course.id'}.'.domain'}.'/'.
 1615: 		$env{'course.'.$env{'request.course.id'}.'.num'}.'/';
 1616: 	    $symb=&Apache::lonnet::encode_symb($path.$folder.'.sequence',
 1617: 					       $residx,
 1618: 					       $path.$pagearg.'.page');
 1619: 	}
 1620: 	$url.='pagepath='.&escape($pagepath).
 1621: 	    '&pagesymb='.&escape($symb).$cpinfo;
 1622:     }
 1623:     $line.='<td bgcolor="#FFFFBB"><a href="'.$url.'"><img src="'.$icon.
 1624: 	'" border="0"></a></td>'.
 1625:         "<td bgcolor='#FFFFBB'><a href=\"$url\">$title</a></td>";
 1626:     if (($allowed) && ($folder!~/^supplemental/)) {
 1627:  	my %lt=&Apache::lonlocal::texthash(
 1628:  			      'hd' => 'Hidden',
 1629:  			      'ec' => 'URL hidden');
 1630: 	my $enctext=
 1631: 	    ((&Apache::lonratedt::getparameter($orderidx,'parameter_encrypturl'))[0]=~/^yes$/i?' checked="1"':'');
 1632: 	my $hidtext=
 1633: 	    ((&Apache::lonratedt::getparameter($orderidx,'parameter_hiddenresource'))[0]=~/^yes$/i?' checked="1"':'');
 1634: 	$line.=(<<ENDPARMS);
 1635: <td bgcolor="#BBBBFF"><font size='-2'>
 1636: <nobr><label><input type="checkbox" name="hidprs_$orderidx" onClick="this.form.submit()" $hidtext /> $lt{'hd'}</label></nobr></td>
 1637: <td bgcolor="#BBBBFF"><font size='-2'>
 1638: <nobr><label><input type="checkbox" name="encprs_$orderidx" onClick="this.form.submit()" $enctext /> $lt{'ec'}</label></nobr></td>
 1639: <td bgcolor="#BBBBFF"><font size="-2">$parameterset</font></td>
 1640: ENDPARMS
 1641:     }
 1642:     $line.="</form></tr>";
 1643:     return $line;
 1644: }
 1645: 
 1646: # ---------------------------------------------------------------- tie the hash
 1647: 
 1648: sub tiehash {
 1649:     my ($mode)=@_;
 1650:     $hashtied=0;
 1651:     if ($env{'request.course.fn'}) {
 1652: 	if ($mode eq 'write') {
 1653: 	    if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.".db",
 1654: 		    &GDBM_WRCREAT(),0640)) {
 1655:                 $hashtied=2;
 1656: 	    }
 1657: 	} else {
 1658: 	    if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.".db",
 1659: 		    &GDBM_READER(),0640)) {
 1660:                 $hashtied=1;
 1661: 	    }
 1662: 	}
 1663:     }    
 1664: }
 1665: 
 1666: sub untiehash {
 1667:     if ($hashtied) { untie %hash; }
 1668:     $hashtied=0;
 1669:     return OK;
 1670: }
 1671: 
 1672: # --------------------------------------------------------------- check on this
 1673: 
 1674: sub checkonthis {
 1675:     my ($r,$url,$level,$title)=@_;
 1676:     $url=&unescape($url);
 1677:     $alreadyseen{$url}=1;
 1678:     $r->rflush();
 1679:     if (($url) && ($url!~/^\/uploaded\//) && ($url!~/\*$/)) {
 1680:        $r->print("\n<br />");
 1681:        for (my $i=0;$i<=$level*5;$i++) {
 1682:            $r->print('&nbsp;');
 1683:        }
 1684:        $r->print('<a href="'.$url.'" target="cat">'.
 1685: 		 ($title?$title:$url).'</a> ');
 1686:        if ($url=~/^\/res\//) {
 1687: 	  my $result=&Apache::lonnet::repcopy(
 1688:                               &Apache::lonnet::filelocation('',$url));
 1689:           if ($result eq 'ok') {
 1690:              $r->print('<font color="green">'.&mt('ok').'</font>');
 1691:              $r->rflush();
 1692:              &Apache::lonnet::countacc($url);
 1693:              $url=~/\.(\w+)$/;
 1694:              if (&Apache::loncommon::fileembstyle($1) eq 'ssi') {
 1695: 		 $r->print('<br />');
 1696:                  $r->rflush();
 1697:                  for (my $i=0;$i<=$level*5;$i++) {
 1698:                      $r->print('&nbsp;');
 1699:                  }
 1700:                  $r->print('- '.&mt('Rendering').': ');
 1701: 		 my ($errorcount,$warningcount)=split(/:/,
 1702: 	       &Apache::lonnet::ssi_body($url,
 1703: 			       ('grade_target'=>'web',
 1704: 				'return_only_error_and_warning_counts' => 1)));
 1705:                  if (($errorcount) ||
 1706:                      ($warningcount)) {
 1707: 		     if ($errorcount) {
 1708:                         $r->print('<img src="/adm/lonMisc/bomb.gif" /><font color="red"><b>'.
 1709: 			  $errorcount.' '.
 1710: 				  &mt('error(s)').'</b></font> ');
 1711:                      }
 1712: 		     if ($warningcount) {
 1713:                         $r->print('<font color="blue">'.
 1714: 			  $warningcount.' '.
 1715: 				  &mt('warning(s)').'</font>');
 1716:                      }
 1717:                  } else {
 1718:                      $r->print('<font color="green">'.&mt('ok').'</font>');
 1719:                  }
 1720:                  $r->rflush();
 1721:              }
 1722: 	     my $dependencies=
 1723:                 &Apache::lonnet::metadata($url,'dependencies');
 1724:              foreach (split(/\,/,$dependencies)) {
 1725: 		 if (($_=~/^\/res\//) && (!$alreadyseen{$_})) {
 1726:                     &checkonthis($r,$_,$level+1);
 1727:                  }
 1728:              }
 1729:           } elsif ($result eq 'unavailable') {
 1730:              $r->print('<font color="red"><b>'.&mt('connection down').'</b></font>');
 1731:           } elsif ($result eq 'not_found') {
 1732: 	      unless ($url=~/\$/) {
 1733: 		  $r->print('<font color="red"><b>'.&mt('not found').'</b></font>');
 1734: 	      } else {
 1735: 		  $r->print('<font color="yellow"><b>'.&mt('unable to verify variable URL').'</b></font>');
 1736: 	      }
 1737:           } else {
 1738:              $r->print('<font color="red"><b>'.&mt('access denied').'</b></font>');
 1739:           }
 1740:       }
 1741:    }
 1742: }
 1743: 
 1744: 
 1745: #
 1746: # ----------------------------------------------------------------- List Symbs
 1747: # 
 1748: sub list_symbs {
 1749:     my ($r) = @_;
 1750: 
 1751:     $r->print(&Apache::loncommon::start_page('Symb List'));
 1752:     my $navmap = Apache::lonnavmaps::navmap->new();
 1753:     $r->print("<pre>\n");
 1754:     foreach my $res ($navmap->retrieveResources()) {
 1755: 	$r->print($res->compTitle()."\t".$res->symb()."\n");
 1756:     }
 1757:     $r->print("\n</pre>\n");
 1758:     $r->print('<a href="/adm/coursedocs">'.&mt('Return to DOCS').'</a>');
 1759: }
 1760: 
 1761: 
 1762: #
 1763: # -------------------------------------------------------------- Verify Content
 1764: # 
 1765: sub verifycontent {
 1766:     my ($r) = @_;
 1767:    my $loaderror=&Apache::lonnet::overloaderror($r);
 1768:    if ($loaderror) { return $loaderror; }
 1769:    $r->print(&Apache::loncommon::start_page('Verify Course Documents'));
 1770:    $hashtied=0;
 1771:    undef %alreadyseen;
 1772:    %alreadyseen=();
 1773:    &tiehash();
 1774:    foreach (keys %hash) {
 1775:        if ($hash{$_}=~/\.(page|sequence)$/) {
 1776: 	   if (($_=~/^src_/) && ($alreadyseen{&unescape($hash{$_})})) {
 1777: 	       $r->print('<hr /><font color="red">'.
 1778: 			 &mt('The following sequence or page is included more than once in your course: ').
 1779: 			 &unescape($hash{$_}).'</font><br />'.
 1780: 			 &mt('Note that grading records for problems included in this sequence or folder will overlap.<hr />'));
 1781: 	   }
 1782:        }
 1783:        if (($_=~/^src\_(.+)$/) && (!$alreadyseen{&unescape($hash{$_})})) {
 1784:            &checkonthis($r,$hash{$_},0,$hash{'title_'.$1});
 1785:        }
 1786:    }
 1787:    &untiehash();
 1788:    $r->print('<h1>'.&mt('Done').'.</h1>'.'<a href="/adm/coursedocs">'.
 1789: 	     &mt('Return to DOCS').'</a>');
 1790: }
 1791: 
 1792: 
 1793: # -------------------------------------------------------------- Check Versions
 1794: 
 1795: sub devalidateversioncache {
 1796:     my $src=shift;
 1797:     &Apache::lonnet::devalidate_cache_new('courseresversion',$env{'request.course.id'}.'_'.
 1798: 					  &Apache::lonnet::clutter($src));
 1799: }
 1800: 
 1801: sub checkversions {
 1802:     my ($r) = @_;
 1803:     $r->print(&Apache::loncommon::start_page('Check Course Document Versions'));
 1804:     my $header='';
 1805:     my $startsel='';
 1806:     my $monthsel='';
 1807:     my $weeksel='';
 1808:     my $daysel='';
 1809:     my $allsel='';
 1810:     my %changes=();
 1811:     my $starttime=0;
 1812:     my $haschanged=0;
 1813:     my %setversions=&Apache::lonnet::dump('resourceversions',
 1814: 			  $env{'course.'.$env{'request.course.id'}.'.domain'},
 1815: 			  $env{'course.'.$env{'request.course.id'}.'.num'});
 1816: 
 1817:     $hashtied=0;
 1818:     &tiehash();
 1819:     my %newsetversions=();
 1820:     if ($env{'form.setmostrecent'}) {
 1821: 	$haschanged=1;
 1822: 	foreach (keys %hash) {
 1823: 	    if ($_=~/^ids\_(\/res\/.+)$/) {
 1824: 		$newsetversions{$1}='mostrecent';
 1825:                 &devalidateversioncache($1);
 1826: 	    }
 1827: 	}
 1828:     } elsif ($env{'form.setcurrent'}) {
 1829: 	$haschanged=1;
 1830: 	foreach (keys %hash) {
 1831: 	    if ($_=~/^ids\_(\/res\/.+)$/) {
 1832: 		my $getvers=&Apache::lonnet::getversion($1);
 1833: 		if ($getvers>0) {
 1834: 		    $newsetversions{$1}=$getvers;
 1835: 		    &devalidateversioncache($1);
 1836: 		}
 1837: 	    }
 1838: 	}
 1839:     } elsif ($env{'form.setversions'}) {
 1840: 	$haschanged=1;
 1841: 	foreach (keys %env) {
 1842: 	    if ($_=~/^form\.set_version_(.+)$/) {
 1843: 		my $src=$1;
 1844: 		if (($env{$_}) && ($env{$_} ne $setversions{$src})) {
 1845: 		    $newsetversions{$src}=$env{$_};
 1846: 		    &devalidateversioncache($src);
 1847: 		}
 1848: 	    }
 1849: 	}
 1850:     }
 1851:     if ($haschanged) {
 1852:         if (&Apache::lonnet::put('resourceversions',\%newsetversions,
 1853: 			  $env{'course.'.$env{'request.course.id'}.'.domain'},
 1854: 			  $env{'course.'.$env{'request.course.id'}.'.num'}) eq 'ok') {		
 1855: 	    $r->print('<h1>'.&mt('Your Version Settings have been Stored').'</h1>');
 1856: 	} else {
 1857: 	    $r->print('<h1><font color="red">'.&mt('An Error Occured while Attempting to Store your Version Settings').'</font></h1>');
 1858: 	}
 1859: 	&mark_hash_old();
 1860:     }
 1861:     &changewarning($r,'');
 1862:     if ($env{'form.timerange'} eq 'all') {
 1863: # show all documents
 1864: 	$header=&mt('All Documents in Course');
 1865: 	$allsel=1;
 1866: 	foreach (keys %hash) {
 1867: 	    if ($_=~/^ids\_(\/res\/.+)$/) {
 1868: 		my $src=$1;
 1869: 		$changes{$src}=1;
 1870: 	    }
 1871: 	}
 1872:     } else {
 1873: # show documents which changed
 1874: 	%changes=&Apache::lonnet::dump
 1875: 	 ('versionupdate',$env{'course.'.$env{'request.course.id'}.'.domain'},
 1876:                      $env{'course.'.$env{'request.course.id'}.'.num'});
 1877: 	my $firstkey=(keys %changes)[0];
 1878: 	unless ($firstkey=~/^error\:/) {
 1879: 	    unless ($env{'form.timerange'}) {
 1880: 		$env{'form.timerange'}=604800;
 1881: 	    }
 1882: 	    my $seltext=&mt('during the last').' '.$env{'form.timerange'}.' '
 1883: 		.&mt('seconds');
 1884: 	    if ($env{'form.timerange'}==-1) {
 1885: 		$seltext='since start of course';
 1886: 		$startsel='selected';
 1887: 		$env{'form.timerange'}=time;
 1888: 	    }
 1889: 	    $starttime=time-$env{'form.timerange'};
 1890: 	    if ($env{'form.timerange'}==2592000) {
 1891: 		$seltext=&mt('during the last month').' ('.&Apache::lonlocal::locallocaltime($starttime).')';
 1892: 		$monthsel='selected';
 1893: 	    } elsif ($env{'form.timerange'}==604800) {
 1894: 		$seltext=&mt('during the last week').' ('.&Apache::lonlocal::locallocaltime($starttime).')';
 1895: 		$weeksel='selected';
 1896: 	    } elsif ($env{'form.timerange'}==86400) {
 1897: 		$seltext=&mt('since yesterday').' ('.&Apache::lonlocal::locallocaltime($starttime).')';
 1898: 		$daysel='selected';
 1899: 	    }
 1900: 	    $header=&mt('Content changed').' '.$seltext;
 1901: 	} else {
 1902: 	    $header=&mt('No content modifications yet.');
 1903: 	}
 1904:     }
 1905:     %setversions=&Apache::lonnet::dump('resourceversions',
 1906: 			  $env{'course.'.$env{'request.course.id'}.'.domain'},
 1907: 			  $env{'course.'.$env{'request.course.id'}.'.num'});
 1908:     my %lt=&Apache::lonlocal::texthash
 1909: 	      ('st' => 'Version changes since start of Course',
 1910: 	       'lm' => 'Version changes since last Month',
 1911: 	       'lw' => 'Version changes since last Week',
 1912: 	       'sy' => 'Version changes since Yesterday',
 1913:                'al' => 'All Resources (possibly large output)',
 1914: 	       'sd' => 'Display',
 1915: 	       'fi' => 'File',
 1916: 	       'md' => 'Modification Date',
 1917:                'mr' => 'Most recently published Version',
 1918: 	       've' => 'Version used in Course',
 1919:                'vu' => 'Set Version to be used in Course',
 1920: 'sv' => 'Set Versions to be used in Course according to Selections below',
 1921: 'sm' => 'Keep all Resources up-to-date with most recent Versions (default)',
 1922: 'sc' => 'Set all Resource Versions to current Version (Fix Versions)',
 1923: 	       'di' => 'Differences');
 1924:     $r->print(<<ENDHEADERS);
 1925: <form action="/adm/coursedocs" method="post">
 1926: <input type="hidden" name="versions" value="1" />
 1927: <input type="submit" name="setmostrecent" value="$lt{'sm'}" />
 1928: <input type="submit" name="setcurrent" value="$lt{'sc'}" /><hr />
 1929: <select name="timerange">
 1930: <option value='all' $allsel>$lt{'al'}</option>
 1931: <option value="-1" $startsel>$lt{'st'}</option>
 1932: <option value="2592000" $monthsel>$lt{'lm'}</option>
 1933: <option value="604800" $weeksel>$lt{'lw'}</option>
 1934: <option value="86400" $daysel>$lt{'sy'}</option>
 1935: </select>
 1936: <input type="submit" name="display" value="$lt{'sd'}" />
 1937: <h3>$header</h3>
 1938: <input type="submit" name="setversions" value="$lt{'sv'}" />
 1939: <table border="0">
 1940: ENDHEADERS
 1941:     foreach (sort keys %changes) {
 1942: 	if ($changes{$_}>$starttime) {
 1943: 	    my ($root,$extension)=($_=~/^(.*)\.(\w+)$/);
 1944: 	    my $currentversion=&Apache::lonnet::getversion($_);
 1945: 	    if ($currentversion<0) {
 1946: 		$currentversion=&mt('Could not be determined.');
 1947: 	    }
 1948: 	    my $linkurl=&Apache::lonnet::clutter($_);
 1949: 	    $r->print(
 1950: 		      '<tr><td colspan="5"><br /><br /><font size="+1"><b>'.
 1951: 		      &Apache::lonnet::gettitle($linkurl).
 1952:                       '</b></font></td></tr>'.
 1953:                       '<tr><td>&nbsp;&nbsp;&nbsp;</td>'.
 1954:                       '<td colspan="4">'.
 1955:                       '<a href="'.$linkurl.'" target="cat">'.$linkurl.
 1956: 		      '</a></td></tr>'.
 1957:                       '<tr><td></td>'.
 1958:                       '<td title="'.$lt{'md'}.'">'.
 1959: 		      &Apache::lonlocal::locallocaltime(
 1960:                            &Apache::lonnet::metadata($root.'.'.$extension,
 1961:                                                      'lastrevisiondate')
 1962:                                                         ).
 1963:                       '</td>'.
 1964:                       '<td title="'.$lt{'mr'}.'"><nobr>Most Recent: '.
 1965:                       '<font size="+1">'.$currentversion.'</font>'.
 1966:                       '</nobr></td>'.
 1967:                       '<td title="'.$lt{'ve'}.'"><nobr>In Course: '.
 1968:                       '<font size="+1">');
 1969: # Used in course
 1970: 	    my $usedversion=$hash{'version_'.$linkurl};
 1971: 	    if (($usedversion) && ($usedversion ne 'mostrecent')) {
 1972: 		$r->print($usedversion);
 1973: 	    } else {
 1974: 		$r->print($currentversion);
 1975: 	    }
 1976: 	    $r->print('</font></nobr></td><td title="'.$lt{'vu'}.'">'.
 1977:                       '<nobr>Use: ');
 1978: # Set version
 1979: 	    $r->print(&Apache::loncommon::select_form($setversions{$linkurl},
 1980: 						      'set_version_'.$linkurl,
 1981: 						      ('select_form_order' =>
 1982: 						       ['',1..$currentversion,'mostrecent'],
 1983: 						       '' => '',
 1984: 						       'mostrecent' => 'most recent',
 1985: 						       map {$_,$_} (1..$currentversion))));
 1986: 	    $r->print('</nobr></td></tr><tr><td></td>');
 1987: 	    my $lastold=1;
 1988: 	    for (my $prevvers=1;$prevvers<$currentversion;$prevvers++) {
 1989: 		my $url=$root.'.'.$prevvers.'.'.$extension;
 1990: 		if (&Apache::lonnet::metadata($url,'lastrevisiondate')<
 1991: 		    $starttime) {
 1992: 		    $lastold=$prevvers;
 1993: 		}
 1994: 	    }
 1995:             # 
 1996:             # Code to figure out how many version entries should go in
 1997:             # each of the four columns
 1998:             my $entries_per_col = 0;
 1999:             my $num_entries = ($currentversion-$lastold);
 2000:             if ($num_entries % 4 == 0) {
 2001:                 $entries_per_col = $num_entries/4;
 2002:             } else {
 2003:                 $entries_per_col = $num_entries/4 + 1;
 2004:             }
 2005:             my $entries_count = 0;
 2006:             $r->print('<td valign="top"><font size="-2">'); 
 2007:             my $cols_output = 1;
 2008:             for (my $prevvers=$lastold;$prevvers<$currentversion;$prevvers++) {
 2009: 		my $url=$root.'.'.$prevvers.'.'.$extension;
 2010: 		$r->print('<nobr><a href="'.&Apache::lonnet::clutter($url).
 2011: 			  '">'.&mt('Version').' '.$prevvers.'</a> ('.
 2012: 			  &Apache::lonlocal::locallocaltime(
 2013:                                 &Apache::lonnet::metadata($url,
 2014:                                                           'lastrevisiondate')
 2015:                                                             ).
 2016: 			  ')');
 2017: 		if (&Apache::loncommon::fileembstyle($extension) eq 'ssi') {
 2018:                     $r->print(' <a href="/adm/diff?filename='.
 2019: 			      &Apache::lonnet::clutter($root.'.'.$extension).
 2020: 			      '&versionone='.$prevvers.
 2021: 			      '">'.&mt('Diffs').'</a>');
 2022: 		}
 2023: 		$r->print('</nobr><br />');
 2024:                 if (++$entries_count % $entries_per_col == 0) {
 2025:                     $r->print('</font></td>');
 2026:                     if ($cols_output != 4) {
 2027:                         $r->print('<td valign="top"><font size="-2">');
 2028:                         $cols_output++;
 2029:                     }
 2030:                 }
 2031: 	    }
 2032:             while($cols_output++ < 4) {
 2033:                 $r->print('</font></td><td><font>')
 2034:             }
 2035: 	    $r->print('</font></td></tr>'."\n");
 2036: 	}
 2037:     }
 2038:     $r->print('</table></form>');
 2039:     $r->print('<h1>'.&mt('Done').'.</h1>');
 2040: 
 2041:     &untiehash();
 2042: }
 2043: 
 2044: sub mark_hash_old {
 2045:     my $retie_hash=0;
 2046:     if ($hashtied) {
 2047: 	$retie_hash=1;
 2048: 	&untiehash();
 2049:     }
 2050:     &tiehash('write');
 2051:     $hash{'old'}=1;
 2052:     &untiehash();
 2053:     if ($retie_hash) { &tiehash(); }
 2054: }
 2055: 
 2056: sub is_hash_old {
 2057:     my $untie_hash=0;
 2058:     if (!$hashtied) {
 2059: 	$untie_hash=1;
 2060: 	&tiehash();
 2061:     }
 2062:     my $return=$hash{'old'};
 2063:     if ($untie_hash) { &untiehash(); }
 2064:     return $return;
 2065: }
 2066: 
 2067: sub changewarning {
 2068:     my ($r,$postexec,$message,$url)=@_;
 2069:     if (!&is_hash_old()) { return; }
 2070:     my $pathvar='folderpath';
 2071:     my $path=&escape($env{'form.folderpath'});
 2072:     if (!defined($url)) {
 2073: 	if (defined($env{'form.pagepath'})) {
 2074: 	    $pathvar='pagepath';
 2075: 	    $path=&escape($env{'form.pagepath'});
 2076: 	    $path.='&amp;pagesymb='.&escape($env{'form.pagesymb'});
 2077: 	}
 2078: 	$url='/adm/coursedocs?'.$pathvar.'='.$path;
 2079:     }
 2080:     if (!defined($message)) {
 2081: 	$message='Changes will become active for your current session after [_1], or the next time you log in.';
 2082:     }
 2083:     $r->print("\n\n".
 2084: '<script>function reinit(tf) { tf.submit();'.$postexec.' }</script>'."\n". 
 2085: '<form name="reinitform" method="post" action="/adm/roles" target="loncapaclient">'.
 2086: '<input type="hidden" name="orgurl" value="'.$url.
 2087: '" /><input type="hidden" name="selectrole" value="1" /><h3><font color="red">'.
 2088: &mt($message,' <input type="hidden" name="'.
 2089:     $env{'request.role'}.'" value="1" /><input type="button" value="'.
 2090:     &mt('re-initializing course').'" onClick="reinit(this.form)" />').
 2091: $help{'Caching'}.'</font></h3></form>'."\n\n");
 2092: }
 2093: 
 2094: # ================================================================ Main Handler
 2095: sub handler {
 2096:     my $r = shift;
 2097:     &Apache::loncommon::content_type($r,'text/html');
 2098:     $r->send_http_header;
 2099:     return OK if $r->header_only;
 2100: 
 2101: # --------------------------------------------- Initialize help topics for this
 2102:     foreach ('Adding_Course_Doc','Main_Course_Documents',
 2103: 	     'Adding_External_Resource','Navigate_Content',
 2104: 	     'Adding_Folders','Docs_Overview', 'Load_Map',
 2105: 	     'Supplemental','Score_Upload_Form','Adding_Pages',
 2106: 	     'Importing_LON-CAPA_Resource','Uploading_From_Harddrive',
 2107: 	     'Check_Resource_Versions','Verify_Content') {
 2108: 	$help{$_}=&Apache::loncommon::help_open_topic('Docs_'.$_);
 2109:     }
 2110:     # Composite help files
 2111:     $help{'Syllabus'} = &Apache::loncommon::help_open_topic(
 2112: 		    'Docs_About_Syllabus,Docs_Editing_Templated_Pages');
 2113:     $help{'Simple Page'} = &Apache::loncommon::help_open_topic(
 2114: 		    'Docs_About_Simple_Page,Docs_Editing_Templated_Pages');
 2115:     $help{'Simple Problem'} = &Apache::loncommon::help_open_topic(
 2116: 		    'Option_Response_Simple');
 2117:     $help{'Bulletin Board'} = &Apache::loncommon::help_open_topic(
 2118: 		    'Docs_About_Bulletin_Board,Docs_Editing_Templated_Pages');
 2119:     $help{'My Personal Info'} = &Apache::loncommon::help_open_topic(
 2120: 		  'Docs_About_My_Personal_Info,Docs_Editing_Templated_Pages');
 2121:     $help{'Caching'} = &Apache::loncommon::help_open_topic('Caching');
 2122: 
 2123: # does this user have privileges to modify docs
 2124:     my $allowed=&Apache::lonnet::allowed('mdc',$env{'request.course.id'});
 2125: 
 2126:   if ($allowed && $env{'form.verify'}) {
 2127:       &verifycontent($r);
 2128:   } elsif ($allowed && $env{'form.listsymbs'}) {
 2129:       &list_symbs($r);
 2130:   } elsif ($allowed && $env{'form.versions'}) {
 2131:       &checkversions($r);
 2132:   } elsif ($allowed && $env{'form.dumpcourse'}) {
 2133:       &dumpcourse($r);
 2134:   } elsif ($allowed && $env{'form.exportcourse'}) {
 2135:       &exportcourse($r);
 2136:   } else {
 2137: # is this a standard course?
 2138: 
 2139:     my $standard=($env{'request.course.uri'}=~/^\/uploaded\//);
 2140:     my $forcestandard = 0;
 2141:     my $forcesupplement;
 2142:     my $script='';
 2143:     my $showdoc=0;
 2144:     my $containertag;
 2145:     my $uploadtag;
 2146:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 2147: 					    ['folderpath','pagepath',
 2148: 					     'pagesymb','markedcopy_url',
 2149: 					     'markedcopy_title']);
 2150:     if ($env{'form.folderpath'}) {
 2151: 	my (@folderpath)=split('&',$env{'form.folderpath'});
 2152: 	$env{'form.foldername'}=&unescape(pop(@folderpath));
 2153: 	$env{'form.folder'}=pop(@folderpath);
 2154:     }
 2155:     if ($env{'form.pagepath'}) {
 2156:         my (@pagepath)=split('&',$env{'form.pagepath'});
 2157:         $env{'form.pagename'}=&unescape(pop(@pagepath));
 2158:         $env{'form.folder'}=pop(@pagepath);
 2159:         $containertag = '<input type="hidden" name="pagepath" value="" />'.
 2160: 	    '<input type="hidden" name="pagesymb" value="" />';
 2161:         $uploadtag = '<input type="hidden" name="pagepath" value="'.$env{'form.pagepath'}.'" />'.
 2162: 	    '<input type="hidden" name="pagesymb" value="'.$env{'form.pagesymb'}.'" />';
 2163:     }
 2164:     if ($r->uri=~/^\/adm\/coursedocs\/showdoc\/(.*)$/) {
 2165:        $showdoc='/'.$1;
 2166:     }
 2167:     unless ($showdoc) { # got called from remote
 2168:        if (($env{'form.folder'}=~/^default_/) || 
 2169:           ($env{'form.folder'} =~ m:^\d+/(pages|sequences)/:)) {
 2170:            $forcestandard = 1;
 2171:        } 
 2172:        $forcesupplement=($env{'form.folder'}=~/^supplemental_/);
 2173: 
 2174:        if ($allowed) { 
 2175:          &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['cmd']);
 2176:          $script=&Apache::lonratedt::editscript('simple'); 
 2177:        }
 2178:     } else { # got called in sequence from course
 2179:        $allowed=0;
 2180:     }
 2181: 
 2182: # get course data
 2183:     my $coursenum=$env{'course.'.$env{'request.course.id'}.'.num'};
 2184:     my $coursedom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 2185: 
 2186: # get personal data 
 2187:     my $uname=$env{'user.name'};
 2188:     my $udom=$env{'user.domain'};
 2189:     my $plainname=&escape(
 2190:                      &Apache::loncommon::plainname($uname,$udom));
 2191: 
 2192: # graphics settings
 2193: 
 2194:     $iconpath = &Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL') . "/");
 2195: 
 2196:     if ($allowed) {
 2197: 	$script .= &editing_js($udom,$uname);
 2198:     }
 2199: # -------------------------------------------------------------------- Body tag
 2200:     $script = '<script type="text/javascript">'."\n".$script."\n".'</script>';
 2201:     $r->print(&Apache::loncommon::start_page('Course Documents', $script,
 2202: 					     {'force_register' => $showdoc,}).
 2203: 	      &Apache::loncommon::help_open_menu('','','','',273,'RAT'));
 2204:   
 2205:   my %allfiles = ();
 2206:   my %codebase = ();
 2207:   my ($upload_result,$upload_output);
 2208:   if ($allowed) {
 2209:       if (($env{'form.uploaddoc.filename'}) &&                                               ($env{'form.cmd'}=~/^upload_(\w+)/)) {
 2210: # Process file upload - phase one - upload and parse primary file.  
 2211:           $upload_result = &process_file_upload(\$upload_output,$coursenum,
 2212: 						$coursedom,\%allfiles,
 2213: 						\%codebase,$1);
 2214:           if ($upload_result eq 'phasetwo') {
 2215:               $r->print($upload_output);
 2216:           }
 2217:       } elsif ($env{'form.phasetwo'}) {
 2218:           my %newname = ();
 2219:           my %origname = ();
 2220:           my %attribs = ();
 2221:           my $updateflag = 0;
 2222:           my $residx = $env{'form.newidx'};
 2223:           my $primary_url = &unescape($env{'form.primaryurl'});
 2224: # Process file upload - phase two - gather secondary files.
 2225:           for (my $i=0; $i<$env{'form.phasetwo'}; $i++) {
 2226:               if ($env{'form.embedded_item_'.$i.'.filename'}) {
 2227:                   my $javacodebase;
 2228:                   $newname{$i} = &process_secondary_uploads(\$upload_output,$coursedom,$coursenum,'embedded_item_',$i,$residx);
 2229:                   $origname{$i} = &unescape($env{'form.embedded_orig_'.$i});
 2230:                   if (exists($env{'form.embedded_codebase_'.$i})) {
 2231:                       $javacodebase =  &unescape($env{'form.embedded_codebase_'.$i});  
 2232:                       $origname{$i} =~ s#^\Q$javacodebase\E/##; 
 2233:                   }
 2234:                   my @attributes = ();
 2235:                   if ($env{'form.embedded_attrib_'.$i} =~ /:/) {
 2236:                       @attributes = split/:/,$env{'form.embedded_attrib_'.$i};
 2237:                   } else {
 2238:                       @attributes = ($env{'form.embedded_attrib_'.$i});
 2239:                   }
 2240:                   foreach (@attributes) {
 2241:                       push(@{$attribs{$i}},&unescape($_));
 2242:                   }
 2243:                   if ($javacodebase) {
 2244:                       $codebase{$i} = $javacodebase;
 2245:                       $codebase{$i} =~ s#/$##;
 2246:                       $updateflag = 1;
 2247:                   }
 2248:               }
 2249:               unless ($newname{$i} eq $origname{$i}) {
 2250:                   $updateflag = 1;
 2251:               }
 2252:           }
 2253: # Process file upload - phase three - modify primary file
 2254:           if ($updateflag) {
 2255:               my ($content,$rtncode);
 2256:               my $updateflag = 0;
 2257:               my $getstatus = &Apache::lonnet::getuploaded('GET',$primary_url,$coursedom,$coursenum,\$content,\$rtncode);
 2258:               if ($getstatus eq 'ok') {
 2259:                   foreach my $item (keys %newname) {
 2260:                       if ($newname{$item} ne $origname{$item}) {
 2261:                           my $attrib_regexp = '';
 2262:                           if (@{$attribs{$item}} > 1) {
 2263:                               $attrib_regexp = join('|',@{$attribs{$item}});
 2264:                           } else {
 2265:                               $attrib_regexp = $attribs{$item}[0];
 2266:                           }
 2267:                           if ($content =~ m#($attrib_regexp\s*=\s*['"]?)\Q$origname{$item}\E(['"]?)#) {
 2268:                           } 
 2269:                           $content =~ s#($attrib_regexp\s*=\s*['"]?)\Q$origname{$item}\E(['"]?)#$1$newname{$item}$2#gi; 
 2270:                       }
 2271:                       if (exists($codebase{$item})) {
 2272:                           $content =~ s/(codebase\s*=\s*["']?)\Q$codebase{$item}\E(["']?)/$1.$2/i; #' stupid emacs
 2273:                       }
 2274:                   }
 2275: # Save edited file.
 2276:                   my $saveresult;
 2277:                   my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 2278:                   my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 2279:                   my $url = &Apache::lonnet::store_edited_file($primary_url,$content,$docudom,$docuname,\$saveresult);
 2280:               } else {
 2281:                   &Apache::lonnet::logthis('retrieval of uploaded file - '.$primary_url.' - for editing, failed: '.$getstatus); 
 2282:               }
 2283:           }
 2284:       }
 2285:   }
 2286: 
 2287:   unless ($showdoc ||  $upload_result eq 'phasetwo') {
 2288: # -----------------------------------------------------------------------------
 2289:        my %lt=&Apache::lonlocal::texthash(
 2290:                 'uplm' => 'Upload a new main course document',
 2291:                 'upls' => 'Upload a new supplemental course document',
 2292:                 'impp' => 'Import a document',
 2293:                 'pubd' => 'Published documents',
 2294: 		'copm' => 'All documents out of a published map into this folder',
 2295:                 'spec' => 'Special documents',
 2296:                 'upld' => 'Upload Document',
 2297:                 'srch' => 'Search',
 2298:                 'impo' => 'Import',
 2299:                 'selm' => 'Select Map',
 2300:                 'load' => 'Load Map',
 2301:                 'reco' => 'Recover Deleted Resources',
 2302:                 'newf' => 'New Folder',
 2303:                 'newp' => 'New Composite Page',
 2304:                 'extr' => 'External Resource',
 2305:                 'syll' => 'Syllabus',
 2306:                 'navc' => 'Navigate Contents',
 2307:                 'sipa' => 'Simple Page',
 2308:                 'sipr' => 'Simple Problem',
 2309:                 'drbx' => 'Drop Box',
 2310:                 'scuf' => 'Score Upload Form',
 2311:                 'bull' => 'Bulletin Board',
 2312:                 'mypi' => 'My Personal Info',
 2313: 		'abou' => 'About User',
 2314:                 'imsf' => 'Import IMS package',
 2315:                 'file' =>  'File',
 2316:                 'title' => 'Title',
 2317:                 'comment' => 'Comment',
 2318:                 'parse' => 'If HTML file, upload embedded images/multimedia files'
 2319: 					  );
 2320: # -----------------------------------------------------------------------------
 2321:     if ($allowed) {
 2322:        my $dumpbut=&dumpbutton();
 2323:        my $exportbut=&exportbutton();
 2324:        my %lt=&Apache::lonlocal::texthash(
 2325: 					 'vc' => 'Verify Content',
 2326: 					 'cv' => 'Check/Set Resource Versions',
 2327: 					 'ls' => 'List Symbs',
 2328: 					  );
 2329: 
 2330:        my $folderpath=$env{'form.folderpath'};
 2331:        if (!$folderpath) {
 2332: 	   if ($env{'form.folder'} eq '' ||
 2333: 	       $env{'form.folder'} eq 'supplemental') {
 2334: 	       $folderpath='default&'.
 2335: 		   &escape(&mt('Main Course Documents'));
 2336: 	   }
 2337:        }
 2338:        unless ($env{'form.pagepath'}) {
 2339:            $containertag = '<input type="hidden" name="folderpath" value="" />';
 2340:            $uploadtag = '<input type="hidden" name="folderpath" value="'.$folderpath.'" />';
 2341:        }
 2342: 
 2343:        $r->print(<<ENDCOURSEVERIFY);
 2344: <form name="renameform" method="post" action="/adm/coursedocs">
 2345: <input type="hidden" name="title" />
 2346: <input type="hidden" name="cmd" />
 2347: <input type="hidden" name="markcopy" />
 2348: $containertag
 2349: </form>
 2350: <form name="simpleedit" method="post" action="/adm/coursedocs">
 2351: <input type=hidden name="importdetail" value="">
 2352: $uploadtag
 2353: </form>
 2354: <form action="/adm/coursedocs" method="post" name="courseverify">
 2355: <table bgcolor="#AAAAAA" width="100%" cellspacing="4" cellpadding="4">
 2356: <tr><td bgcolor="#DDDDCC">
 2357: <input type="submit" name="verify" value="$lt{'vc'}" />$help{'Verify_Content'}
 2358: </td><td bgcolor="#DDDDCC">
 2359:     <input type="submit" name="versions" value="$lt{'cv'}" />$help{'Check_Resource_Versions'}
 2360: $dumpbut
 2361: $exportbut
 2362: </td><td bgcolor="#DDDDCC">
 2363:     <input type="submit" name="listsymbs" value="$lt{'ls'}" />
 2364: </td></tr></table>
 2365: </form>
 2366: ENDCOURSEVERIFY
 2367:        $r->print(&Apache::loncommon::help_open_topic('Docs_Adding_Course_Doc',
 2368: 		     &mt('Editing the Table of Contents for your Course')));
 2369:     }
 2370: # --------------------------------------------------------- Standard documents
 2371:     $r->print('<table border=2 cellspacing=4 cellpadding=4>');
 2372:     if (($standard) && ($allowed) && (!$forcesupplement)) {
 2373: 	$r->print('<tr><td bgcolor="#BBBBBB">');
 2374: #  '<h2>'.&mt('Main Course Documents').
 2375: #  ($allowed?' '.$help{'Main_Course_Documents'}:'').'</h2>');
 2376:        my $folder=$env{'form.folder'};
 2377:        if ($folder eq '' || $folder eq 'supplemental') {
 2378:            $folder='default';
 2379: 	   $env{'form.folderpath'}='default&'.&escape(&mt('Main Course Documents'));
 2380:        }
 2381:        my $postexec='';
 2382:        if ($folder eq 'default') {
 2383: 	   $r->print('<script>this.window.name="loncapaclient";</script>');
 2384:        } else {
 2385:            #$postexec='self.close();';
 2386:        }
 2387:        $hadchanges=0;
 2388:        &editor($r,$coursenum,$coursedom,$folder,$allowed,$upload_output);
 2389:        if ($hadchanges) {
 2390: 	   &mark_hash_old()
 2391:        }
 2392:        &changewarning($r,$postexec);
 2393:        my $folderseq='/uploaded/'.$coursedom.'/'.$coursenum.'/default_'.time.
 2394:                      '.sequence';
 2395:        my $pageseq = '/uploaded/'.$coursedom.'/'.$coursenum.'/default_'.time.
 2396:                      '.page';
 2397: 	my $container='sequence';
 2398: 	if ($env{'form.pagepath'}) {
 2399: 	    $container='page';
 2400: 	}
 2401: 	my $readfile='/uploaded/'.$coursedom.'/'.$coursenum.'/'.$folder.'.'.$container;
 2402:        $r->print(<<ENDFORM);
 2403: <table cellspacing=4 cellpadding=4><tr>
 2404: <th bgcolor="#DDDDDD">$lt{'uplm'}</th>
 2405: <th bgcolor="#DDDDDD">$lt{'impp'}</th>
 2406: <th bgcolor="#DDDDDD">$lt{'spec'}</th>
 2407: </tr>
 2408: <tr><td bgcolor="#DDDDDD">
 2409: $lt{'file'}:<br />
 2410: <form name="uploaddocument" action="/adm/coursedocs" method="post" enctype="multipart/form-data">
 2411: <input type="file" name="uploaddoc" size="40">
 2412: <br />
 2413: $lt{'title'}:<br />
 2414: <input type="text" size="50" name="comment">
 2415: $uploadtag
 2416: <input type="hidden" name="cmd" value="upload_default">
 2417: <br />
 2418: <nobr>
 2419: <label>$lt{'parse'}?
 2420: <input type="checkbox" name="parserflag" />
 2421: </label>
 2422: </nobr>
 2423: <br />
 2424: <br />
 2425: <nobr>
 2426: <input type="submit" value="$lt{'upld'}">
 2427:  $help{'Uploading_From_Harddrive'}
 2428: </nobr>
 2429: </form>
 2430: </td>
 2431: <td bgcolor="#DDDDDD">
 2432: <form action="/adm/coursedocs" method="post" name="simpleeditdefault">
 2433: $lt{'pubd'}<br />
 2434: $uploadtag
 2435: <input type=button onClick="javascript:groupsearch()" value="$lt{'srch'}">
 2436: <nobr>
 2437: <input type=button onClick="javascript:groupimport();" value="$lt{'impo'}">
 2438: $help{'Importing_LON-CAPA_Resource'}
 2439: </nobr>
 2440: <p>
 2441: <hr />
 2442: $lt{'copm'}<br />
 2443: <input type="text" size="40" name="importmap"><br />
 2444: <nobr><input type=button 
 2445: onClick="javascript:openbrowser('simpleeditdefault','importmap','sequence,page','')"
 2446: value="$lt{'selm'}"> <input type="submit" name="loadmap" value="$lt{'load'}">
 2447: $help{'Load_Map'}</nobr>
 2448: </p>
 2449: </form>
 2450: <hr />
 2451: <form action="/adm/groupsort" method="post" name="recover">
 2452: <input type="button" name="recovermap" onClick="javascript:groupopen('$readfile',1)" value="$lt{'reco'}" />
 2453: </form>
 2454: ENDFORM
 2455:        unless ($env{'form.pagepath'}) {
 2456: 	   $r->print(<<ENDFORM);
 2457: <hr />
 2458: <form action="/adm/coursedocs" method="post" name="newext">
 2459: $uploadtag
 2460: <input type=hidden name="importdetail" value="">
 2461: <nobr>
 2462: <input name="newext" type="button" onClick="javascript:makenewext('newext');"
 2463: value="$lt{'extr'}" /> $help{'Adding_External_Resource'}
 2464: </nobr>
 2465: </form>
 2466: <br /><form action="/adm/imsimportdocs" method="post" name="ims">
 2467: <input type="hidden" name="folder" value="$folder" />
 2468: <input name="imsimport" type="button" value="$lt{'imsf'}" onClick="javascript:makeims();" />
 2469: </nobr>
 2470: </form>
 2471: ENDFORM
 2472:        }
 2473:        $r->print('</td><td bgcolor="#DDDDDD">');
 2474:        unless ($env{'form.pagepath'}) {
 2475:            $r->print(<<ENDFORM);
 2476: <br /><form action="/adm/coursedocs" method="post" name="newfolder">
 2477: <input type="hidden" name="folderpath" value="$env{'form.folderpath'}" />
 2478: <input type=hidden name="importdetail" value="">
 2479: <nobr>
 2480: <input name="newfolder" type="button"
 2481: onClick="javascript:makenewfolder(this.form,'$folderseq');"
 2482: value="$lt{'newf'}" />$help{'Adding_Folders'}
 2483: </nobr>
 2484: </form>
 2485: <br /><form action="/adm/coursedocs" method="post" name="newpage">
 2486: <input type="hidden" name="folderpath" value="$env{'form.folderpath'}" />
 2487: <input type=hidden name="importdetail" value="">
 2488: <nobr>
 2489: <input name="newpage" type="button"
 2490: onClick="javascript:makenewpage(this.form,'$pageseq');"
 2491: value="$lt{'newp'}" />$help{'Adding_Pages'}
 2492: </nobr>
 2493: </form>
 2494: <br /><form action="/adm/coursedocs" method="post" name="newsyl">
 2495: $uploadtag
 2496: <input type=hidden name="importdetail" 
 2497: value="Syllabus=/public/$coursedom/$coursenum/syllabus">
 2498: <nobr>
 2499: <input name="newsyl" type="submit" value="$lt{'syll'}" /> 
 2500:  $help{'Syllabus'}
 2501: </nobr>
 2502: </form>
 2503: <br /><form action="/adm/coursedocs" method="post" name="newnav">
 2504: $uploadtag
 2505: <input type=hidden name="importdetail" 
 2506: value="Navigate Content=/adm/navmaps">
 2507: <nobr>
 2508: <input name="newnav" type="submit" value="$lt{'navc'}" />
 2509: $help{'Navigate_Content'}
 2510: </nobr>
 2511: </form>
 2512: <br /><form action="/adm/coursedocs" method="post" name="newsmppg">
 2513: $uploadtag
 2514: <input type=hidden name="importdetail" value="">
 2515: <nobr>
 2516: <input name="newsmppg" type="button" value="$lt{'sipa'}"
 2517: onClick="javascript:makesmppage();" /> $help{'Simple Page'}
 2518: </nobr>
 2519: </form>
 2520: <br /><form action="/adm/coursedocs" method="post" name="newsmpproblem">
 2521: $uploadtag
 2522: <input type=hidden name="importdetail" value="">
 2523: <nobr>
 2524: <input name="newsmpproblem" type="button" value="$lt{'sipr'}"
 2525: onClick="javascript:makesmpproblem();" />$help{'Simple Problem'}
 2526: </nobr>
 2527: </form>
 2528: <br /><form action="/adm/coursedocs" method="post" name="newdropbox">
 2529: $uploadtag      
 2530: <input type=hidden name="importdetail" value="">
 2531: <nobr>          
 2532: <input name="newdropbox" type="button" value="$lt{'drbx'}"
 2533: onClick="javascript:makedropbox();" />
 2534: </nobr>         
 2535: </form> 
 2536: <br /><form action="/adm/coursedocs" method="post" name="newexamupload">
 2537: $uploadtag
 2538: <input type=hidden name="importdetail" value="">
 2539: <nobr>
 2540: <input name="newexamupload" type="button" value="$lt{'scuf'}"
 2541: onClick="javascript:makeexamupload();" />
 2542: $help{'Score_Upload_Form'}
 2543: </nobr>
 2544: </form>
 2545: <br /><form action="/adm/coursedocs" method="post" name="newbul">
 2546: $uploadtag
 2547: <input type=hidden name="importdetail" value="">
 2548: <nobr>
 2549: <input name="newbulletin" type="button" value="$lt{'bull'}"
 2550: onClick="javascript:makebulboard();" />
 2551: $help{'Bulletin Board'}
 2552: </nobr>
 2553: </form>
 2554: <br /><form action="/adm/coursedocs" method="post" name="newaboutme">
 2555: $uploadtag
 2556: <input type=hidden name="importdetail" 
 2557: value="$plainname=/adm/$udom/$uname/aboutme">
 2558: <nobr>
 2559: <input name="newaboutme" type="submit" value="$lt{'mypi'}" />
 2560: $help{'My Personal Info'}
 2561: </nobr>
 2562: </form>
 2563: <br /><form action="/adm/coursedocs" method="post" name="newaboutsomeone">
 2564: $uploadtag
 2565: <input type=hidden name="importdetail" value="">
 2566: <nobr>
 2567: <input name="newaboutsomeone" type="button" value="$lt{'abou'}" 
 2568: onClick="javascript:makeabout();" />
 2569: </nobr>
 2570: </form>
 2571: ENDFORM
 2572:        }
 2573:        if ($env{'form.pagepath'}) {
 2574:            $r->print(<<ENDBLOCK);
 2575: <form action="/adm/coursedocs" method="post" name="newsmpproblem">
 2576: $uploadtag
 2577: <input type=hidden name="importdetail" value="">
 2578: <nobr>
 2579: <input name="newsmpproblem" type="button" value="$lt{'sipr'}"
 2580: onClick="javascript:makesmpproblem();" />$help{'Simple Problem'}
 2581: </nobr>
 2582: </form>
 2583: <br /><form action="/adm/coursedocs" method="post" name="newexamupload">
 2584: $uploadtag
 2585: <input type=hidden name="importdetail" value="">
 2586: <nobr>
 2587: <input name="newexamupload" type="button" value="$lt{'scuf'}"
 2588: onClick="javascript:makeexamupload();" />
 2589: $help{'Score_Upload_Form'}
 2590: </nobr>
 2591: </form>
 2592: ENDBLOCK
 2593:        }
 2594:        $r->print('</td></tr>'."\n".
 2595: '</table>');
 2596:        $r->print('</td></tr>');
 2597:     }
 2598: # ----------------------------------------------------- Supplemental documents
 2599:     if (!$forcestandard) {
 2600:        $r->print('<tr><td bgcolor="#BBBBBB">');
 2601: # '<h2>'.&mt('Supplemental Course Documents').
 2602: #  ($allowed?' '.$help{'Supplemental'}:'').'</h2>');
 2603:        my $folder=$env{'form.folder'};
 2604:        unless ($folder=~/^supplemental/) {
 2605: 	   $folder='supplemental';
 2606:        }
 2607:        if ($folder =~ /^supplemental$/ &&
 2608: 	   $env{'form.folderpath'} =~ /^default\&/) {
 2609: 	   $env{'form.folderpath'}='supplemental&'.
 2610: 	       &escape(&mt('Supplemental Course Documents'));
 2611:        }
 2612:        &editor($r,$coursenum,$coursedom,$folder,$allowed);
 2613:        if ($allowed) {
 2614:        my $folderseq=
 2615:                   '/uploaded/'.$coursedom.'/'.$coursenum.'/supplemental_'.time.
 2616:                      '.sequence';
 2617: 
 2618:           $r->print(<<ENDSUPFORM);
 2619: <table cellspacing=4 cellpadding=4><tr>
 2620: <th bgcolor="#DDDDDD">$lt{'upls'}</th>
 2621: <th bgcolor="#DDDDDD">$lt{'spec'}</th>
 2622: </tr>
 2623: <tr><td bgcolor="#DDDDDD">
 2624: <form action="/adm/coursedocs" method="post" enctype="multipart/form-data">
 2625: <input type="file" name="uploaddoc" size="40">
 2626: <br />
 2627: <br />
 2628: <nobr>
 2629: <label>$lt{'parse'}?
 2630: <input type="checkbox" name="parserflag" />
 2631: </label>
 2632: </nobr>
 2633: <br /><br />
 2634: $lt{'comment'}:<br />
 2635: <textarea cols=50 rows=4 name='comment'>
 2636: </textarea>
 2637: <br />
 2638: <input type="hidden" name="folderpath" value="$env{'form.folderpath'}" />
 2639: <input type="hidden" name="cmd" value="upload_supplemental">
 2640: <nobr>
 2641: <input type="submit" value="$lt{'upld'}">
 2642:  $help{'Uploading_From_Harddrive'}
 2643: </nobr>
 2644: </form>
 2645: </td>
 2646: <td bgcolor="#DDDDDD">
 2647: <form action="/adm/coursedocs" method="post" name="supnewfolder">
 2648: <input type="hidden" name="folderpath" value="$env{'form.folderpath'}" />
 2649: <input type=hidden name="importdetail" value="">
 2650: <nobr>
 2651: <input name="newfolder" type="button"
 2652: onClick="javascript:makenewfolder(this.form,'$folderseq');"
 2653: value="$lt{'newf'}" /> $help{'Adding_Folders'}
 2654: </nobr>
 2655: </form>
 2656: <br /><form action="/adm/coursedocs" method="post" name="supnewext">
 2657: <input type="hidden" name="folderpath" value="$env{'form.folderpath'}" />
 2658: <input type=hidden name="importdetail" value="">
 2659: <nobr>
 2660: <input name="newext" type="button" 
 2661: onClick="javascript:makenewext('supnewext');"
 2662: value="$lt{'extr'}" /> $help{'Adding_External_Resource'}
 2663: </nobr>
 2664: </form>
 2665: <br /><form action="/adm/coursedocs" method="post" name="supnewsyl">
 2666: <input type="hidden" name="folderpath" value="$env{'form.folderpath'}" />
 2667: <input type=hidden name="importdetail" 
 2668: value="Syllabus=/public/$coursedom/$coursenum/syllabus">
 2669: <nobr>
 2670: <input name="newsyl" type="submit" value="$lt{'syll'}" />
 2671: $help{'Syllabus'}
 2672: </nobr>
 2673: </form>
 2674: <br /><form action="/adm/coursedocs" method="post" name="subnewaboutme">
 2675: <input type="hidden" name="folderpath" value="$env{'form.folderpath'}" />
 2676: <input type=hidden name="importdetail" 
 2677: value="$plainname=/adm/$udom/$uname/aboutme">
 2678: <nobr>
 2679: <input name="newaboutme" type="submit" value="$lt{'mypi'}" />
 2680: $help{'My Personal Info'}
 2681: </nobr>
 2682: </form>
 2683: </td></tr>
 2684: </table></td></tr>
 2685: ENDSUPFORM
 2686:        }
 2687:     }
 2688:     if ($allowed) {
 2689: 	$r->print('<form method="POST" name="extimport" action="/adm/coursedocs"><input type="hidden" name="title" /><input type="hidden" name="url" /><input type="hidden" name="useform" /></form>');
 2690:     }
 2691:     $r->print('</table>');
 2692:   } else {
 2693:       unless ($upload_result eq 'phasetwo') {
 2694: # -------------------------------------------------------- This is showdoc mode
 2695:           $r->print("<h1>".&mt('Uploaded Document').' - '.
 2696: 		&Apache::lonnet::gettitle($r->uri).'</h1><p>'.
 2697: &mt('It is recommended that you use an up-to-date virus scanner before handling this file.')."</p><p><table>".
 2698:           &entryline(0,&mt("Click to download or use your browser's Save Link function"),$showdoc).'</table></p>');
 2699:       }
 2700:   }
 2701:  }
 2702:  $r->print(&Apache::loncommon::end_page());
 2703:  return OK;
 2704: } 
 2705: 
 2706: 
 2707: sub editing_js {
 2708:     my ($udom,$uname) = @_;
 2709:     my $now = time();
 2710: 
 2711:     return <<ENDNEWSCRIPT;
 2712: function makenewfolder(targetform,folderseq) {
 2713:     var foldername=prompt('Name of New Folder','New Folder');
 2714:     if (foldername) {
 2715:        targetform.importdetail.value=foldername+"="+folderseq;
 2716:         targetform.submit();
 2717:     }
 2718: }
 2719: 
 2720: function makenewpage(targetform,folderseq) {
 2721:     var pagename=prompt('Name of New Page','New Page');
 2722:     if (pagename) {
 2723:         targetform.importdetail.value=pagename+"="+folderseq;
 2724:         targetform.submit();
 2725:     }
 2726: }
 2727: 
 2728: function makenewext(targetname) {
 2729:     this.document.forms.extimport.useform.value=targetname;
 2730:     window.open('/adm/rat/extpickframe.html');
 2731: }
 2732: 
 2733: function makeexamupload() {
 2734:    var title=prompt('Listed Title for the Uploaded Score');
 2735:    if (title) { 
 2736:     this.document.forms.newexamupload.importdetail.value=
 2737: 	title+'=/res/lib/templates/examupload.problem';
 2738:     this.document.forms.newexamupload.submit();
 2739:    }
 2740: }
 2741: 
 2742: function makesmppage() {
 2743:    var title=prompt('Listed Title for the Page');
 2744:    if (title) { 
 2745:     this.document.forms.newsmppg.importdetail.value=
 2746: 	title+'=/adm/$udom/$uname/$now/smppg';
 2747:     this.document.forms.newsmppg.submit();
 2748:    }
 2749: }
 2750: 
 2751: function makesmpproblem() {
 2752:    var title=prompt('Listed Title for the Problem');
 2753:    if (title) { 
 2754:     this.document.forms.newsmpproblem.importdetail.value=
 2755: 	title+'=/res/lib/templates/simpleproblem.problem';
 2756:     this.document.forms.newsmpproblem.submit();
 2757:    }
 2758: }
 2759: 
 2760: function makedropbox() {
 2761:    var title=prompt('Listed Title for the Drop Box');
 2762:    if (title) { 
 2763:     this.document.forms.newdropbox.importdetail.value=
 2764:         title+'=/res/lib/templates/DropBox.problem';
 2765:     this.document.forms.newdropbox.submit();
 2766:    }
 2767: }
 2768: 
 2769: function makebulboard() {
 2770:    var title=prompt('Listed Title for the Bulletin Board');
 2771:    if (title) {
 2772:     this.document.forms.newbul.importdetail.value=
 2773: 	title+'=/adm/$udom/$uname/$now/bulletinboard';
 2774:     this.document.forms.newbul.submit();
 2775:    }
 2776: }
 2777: 
 2778: function makeabout() {
 2779:    var user=prompt("Enter user\@domain for User's 'About Me' Page");
 2780:    if (user) {
 2781:        var comp=new Array();
 2782:        comp=user.split('\@');
 2783:        if ((typeof(comp[0])!=undefined) && (typeof(comp[1])!=undefined)) {
 2784: 	   if ((comp[0]) && (comp[1])) {
 2785: 	       this.document.forms.newaboutsomeone.importdetail.value=
 2786: 		   'About '+user+'=/adm/'+comp[1]+'/'+comp[0]+'/aboutme';
 2787: 	       this.document.forms.newaboutsomeone.submit();
 2788: 	   } else {
 2789:                alert("Not a valid user\@domain");
 2790:            }
 2791:        } else {
 2792:            alert("Please enter both user and domain in the format user\@domain"); 
 2793:        }
 2794:    }
 2795: }
 2796: 
 2797: function makeims() {
 2798:     var caller = document.forms.ims.folder.value;
 2799:     var newlocation = "/adm/imsimportdocs?folder="+caller+"&phase=one";
 2800:     newWindow = window.open("","IMSimport","HEIGHT=700,WIDTH=750,scrollbars=yes");
 2801:     newWindow.location.href = newlocation;
 2802: }
 2803: 
 2804: 
 2805: function finishpick() {
 2806:     var title=this.document.forms.extimport.title.value;
 2807:     var url=this.document.forms.extimport.url.value;
 2808:     var form=this.document.forms.extimport.useform.value;
 2809:     eval
 2810:      ('this.document.forms.'+form+'.importdetail.value="'+title+'='+url+
 2811:     '";this.document.forms.'+form+'.submit();');
 2812: }
 2813: 
 2814: function changename(folderpath,index,oldtitle,container,pagesymb) {
 2815:     var title=prompt('New Title',oldtitle);
 2816:     if (title) {
 2817: 	this.document.forms.renameform.title.value=title;
 2818: 	this.document.forms.renameform.cmd.value='rename_'+index;
 2819:         if (container == 'sequence') {
 2820: 	    this.document.forms.renameform.folderpath.value=folderpath;
 2821:         }
 2822:         if (container == 'page') {
 2823:             this.document.forms.renameform.pagepath.value=folderpath;
 2824:             this.document.forms.renameform.pagesymb.value=pagesymb;
 2825:         }
 2826:         this.document.forms.renameform.submit();
 2827:     }
 2828: }
 2829: 
 2830: function removeres(folderpath,index,oldtitle,container,pagesymb) {
 2831:     if (confirm('WARNING: Removing a resource makes associated grades and scores inaccessible!\\nRemove "'+oldtitle+'"?')) {
 2832: 	this.document.forms.renameform.cmd.value='del_'+index;
 2833:         if (container == 'sequence') {
 2834:             this.document.forms.renameform.folderpath.value=folderpath;
 2835:         }
 2836:         if (container == 'page') {
 2837:             this.document.forms.renameform.pagepath.value=folderpath;
 2838:             this.document.forms.renameform.pagesymb.value=pagesymb;
 2839:         }
 2840:         this.document.forms.renameform.submit();
 2841:     }
 2842: }
 2843: 
 2844: function cutres(folderpath,index,oldtitle,container,pagesymb) {
 2845:     if (confirm('WARNING: Cutting a resource makes associated grades and scores inaccessible!\\nGrades remain inaccessible if resource is pasted into another folder.\\nCut "'+oldtitle+'"?')) {
 2846: 	this.document.forms.renameform.cmd.value='cut_'+index;
 2847: 	this.document.forms.renameform.markcopy.value=index;
 2848:         if (container == 'sequence') {
 2849:             this.document.forms.renameform.folderpath.value=folderpath;
 2850:         }
 2851:         if (container == 'page') {
 2852:             this.document.forms.renameform.pagepath.value=folderpath;
 2853:             this.document.forms.renameform.pagesymb.value=pagesymb;
 2854:         }
 2855:         this.document.forms.renameform.submit();
 2856:     }
 2857: }
 2858: 
 2859: function markcopy(folderpath,index,oldtitle,container,pagesymb) {
 2860:     this.document.forms.renameform.markcopy.value=index;
 2861:     if (container == 'sequence') {
 2862: 	this.document.forms.renameform.folderpath.value=folderpath;
 2863:     }
 2864:     if (container == 'page') {
 2865: 	this.document.forms.renameform.pagepath.value=folderpath;
 2866: 	this.document.forms.renameform.pagesymb.value=pagesymb;
 2867:     }
 2868:     this.document.forms.renameform.submit();
 2869: }
 2870: 
 2871: ENDNEWSCRIPT
 2872: }
 2873: 1;
 2874: __END__

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