File:  [LON-CAPA] / loncom / interface / londocs.pm
Revision 1.226: download - view: text, annotated - select for diffs
Mon May 1 19:37:33 2006 UTC (18 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- upadte breadcrumbs calls

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

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