File:  [LON-CAPA] / loncom / interface / londocs.pm
Revision 1.229: download - view: text, annotated - select for diffs
Tue May 30 20:09:25 2006 UTC (18 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Adding thin veneer to support "ANGEL-style" groups.

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

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