File:  [LON-CAPA] / loncom / interface / londocs.pm
Revision 1.223: download - view: text, annotated - select for diffs
Mon Mar 6 17:15:29 2006 UTC (18 years, 4 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #1591: inform user if no map was specified for loading

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

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