File:  [LON-CAPA] / loncom / interface / londocs.pm
Revision 1.248: download - view: text, annotated - select for diffs
Sun Nov 12 06:29:33 2006 UTC (17 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- moved to LONCAPA::map

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

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