File:  [LON-CAPA] / loncom / interface / londocs.pm
Revision 1.242: download - view: text, annotated - select for diffs
Thu Jul 20 20:42:48 2006 UTC (17 years, 10 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #3899: warning in randompick folders that modifying them will cause
randomization to change

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

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