File:  [LON-CAPA] / loncom / interface / londocs.pm
Revision 1.16: download - view: text, annotated - select for diffs
Wed Sep 4 13:06:20 2002 UTC (21 years, 10 months ago) by www
Branches: MAIN
CVS tags: HEAD
Can now create new "folders" (uploaded sequences)

    1: # The LearningOnline Network
    2: # Documents
    3: #
    4: # $Id: londocs.pm,v 1.16 2002/09/04 13:06:20 www Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::londocs;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common);
   33: use Apache::lonnet;
   34: use Apache::loncommon;
   35: use Apache::lonratedt;
   36: use Apache::lonratsrv;
   37: use Apache::lonxml;
   38: 
   39: my $iconpath;
   40: 
   41: # Mapread read maps into lonratedt::global arrays 
   42: # @order and @resources, determines status
   43: # sets @order - pointer to resources in right order
   44: # sets @resources - array with the resources with correct idx
   45: #
   46: 
   47: sub mapread {
   48:     my ($coursenum,$coursedom,$map)=@_;
   49:     return
   50:       &Apache::lonratedt::mapread('/uploaded/'.$coursedom.'/'.$coursenum.'/'.
   51:                                 $map);
   52: }
   53: 
   54: sub storemap {
   55:     my ($coursenum,$coursedom,$map)=@_;
   56:     return
   57:       &Apache::lonratedt::storemap('/uploaded/'.$coursedom.'/'.$coursenum.'/'.
   58:                                 $map,1);
   59: }
   60: 
   61: sub editor {
   62:     my ($r,$coursenum,$coursedom,$folder,$allowed)=@_;
   63:     my $errtext='';
   64:     my $fatal=0;
   65:     ($errtext,$fatal)=
   66:           &mapread($coursenum,$coursedom,$folder.'.sequence');
   67:     if ($fatal) {
   68: 	   $r->print('<p><font color="red">'.$errtext.'</font></p>');
   69:     } else {
   70: # ------------------------------------------------------------ Process commands
   71: # ---------------- if they are for this folder and user allowed to make changes
   72: 	if (($allowed) && ($ENV{'form.folder'} eq $folder)) {
   73: # upload a file, if present
   74:            if (($ENV{'form.uploaddoc.filename'}) &&
   75:                ($ENV{'form.cmd'}=~/^upload_(\w+)/)) {
   76: 	    if ($folder=~/^$1/) {
   77: # this is for a course, not a user, so set coursedoc flag
   78: # probably the only place in the system where this should be "1"
   79: 	      my $url=&Apache::lonnet::userfileupload('uploaddoc',1);
   80:               my $ext='false';
   81:               if ($url=~/^http\:\/\//) { $ext='true'; }
   82:               $url=~s/\:/\&colon;/g;
   83: 	      my $comment=$ENV{'form.comment'};
   84:               $comment=~s/\</\&lt\;/g;
   85:               $comment=~s/\>/\&gt\;/g;
   86:               $comment=~s/\:/\&colon;/g;
   87:               my $newidx=$#Apache::lonratedt::resources+1;
   88:               $Apache::lonratedt::resources[$newidx]=
   89:                   $comment.':'.$url.':'.$ext.':normal:res';
   90:               $Apache::lonratedt::order[$#Apache::lonratedt::order+1]=
   91:                                                               $newidx;       
   92:  	      &storemap($coursenum,$coursedom,$folder.'.sequence');
   93: 	     }
   94:             }
   95: 	    if ($ENV{'form.cmd'}) {
   96:                 my ($cmd,$idx)=split(/\_/,$ENV{'form.cmd'});
   97:                 if ($cmd eq 'del') {
   98: 		    for (my $i=$idx;$i<$#Apache::lonratedt::order;$i++) {
   99:                         $Apache::lonratedt::order[$i]=
  100:                           $Apache::lonratedt::order[$i+1];
  101:                     }
  102:                     $#Apache::lonratedt::order--;
  103:                 } elsif ($cmd eq 'up') {
  104:                     my $i=$Apache::lonratedt::order[$idx-1];
  105:                     $Apache::lonratedt::order[$idx-1]=
  106: 			$Apache::lonratedt::order[$idx];
  107:                     $Apache::lonratedt::order[$idx]=$i;
  108:                 } elsif ($cmd eq 'down') {
  109:                     my $i=$Apache::lonratedt::order[$idx+1];
  110:                     $Apache::lonratedt::order[$idx+1]=
  111: 			$Apache::lonratedt::order[$idx];
  112:                     $Apache::lonratedt::order[$idx]=$i;
  113:                 }
  114: # Store the changed version
  115: 		&storemap($coursenum,$coursedom,$folder.'.sequence');
  116:             }
  117: # Group import/search
  118: 	    if ($ENV{'form.importdetail'}) {
  119:                foreach (split(/\&/,$ENV{'form.importdetail'})) {
  120:                   if (defined($_)) {
  121: 	             my ($name,$url)=split(/\=/,$_);
  122:                      $name=&Apache::lonnet::unescape($name);
  123:                      $url=&Apache::lonnet::unescape($url);
  124:                      if ($url) {
  125: 	                my $idx=$#Apache::lonratedt::resources+1;
  126:                         $Apache::lonratedt::order
  127:                            [$#Apache::lonratedt::order+1]=$idx;
  128:                         my $ext='false';
  129:                         if ($url=~/^http\:\/\//) { $ext='true'; }
  130:                         $url=~s/\:/\&colon;/g;
  131:                         $Apache::lonratedt::resources[$idx]=
  132:                            $name.':'.$url.':'.$ext.':normal:res';
  133: 	             }
  134:                  }
  135: 	      }
  136: # Store the changed version
  137: 		&storemap($coursenum,$coursedom,$folder.'.sequence');
  138:             }
  139:        }
  140: # ---------------------------------------------------------------- End commands
  141: # ---------------------------------------------------------------- Print screen
  142:         my $idx=0;
  143:         $r->print('<table>');
  144:         foreach (@Apache::lonratedt::order) {
  145:            my ($name,$url)=split(/\:/,$Apache::lonratedt::resources[$_]);
  146:            unless ($name) {  $name=(split(/\//,$url))[-1]; }
  147:            unless ($name) { $name='EMPTY'; }
  148:            $r->print(&entryline($idx,$name,$url,$folder,$allowed));
  149:            $idx++;
  150:         }
  151:         $r->print('</table>');
  152:     }
  153: }
  154: 
  155: # --------------------------------------------------------------- An entry line
  156: 
  157: sub entryline {
  158:     my ($index,$title,$url,$folder,$allowed)=@_;
  159:     my $line='<tr>';
  160: # Edit commands
  161:     if ($allowed) {
  162:        $line.=(<<END);
  163: <td><table border='0' cellspacing='0' cellpadding='0'>
  164: <tr><td><a href='/adm/coursedocs?folder=$folder&cmd=up_$index'>
  165: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
  166: <tr><td><a href='/adm/coursedocs?folder=$folder&cmd=down_$index'>
  167: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
  168: </table></td><td>
  169: <a href='/adm/coursedocs?folder=$folder&cmd=del_$index'>Remove</td>
  170: END
  171:     }
  172: # Figure out what kind of a resource this is
  173:     my ($extension)=($url=~/\.(\w+)$/);
  174:     my $uploaded=($url=~/^\/*uploaded\//);
  175:     my $icon='unknown';
  176:     if (-e "/home/httpd/html/adm/lonIcons/$extension.gif") {
  177: 	$icon=$extension;
  178:     }
  179:     if ($uploaded) {
  180:        if ($extension eq 'sequence') {
  181: 	  $icon='folder_closed';
  182:           $url=~/\/(\w+)\.sequence/;
  183:           $url='/adm/coursedocs?folder='.$1;
  184:        } else {
  185: 	  $url=&Apache::lonnet::tokenwrapper($url);
  186:        } 
  187:     }
  188:     $line.='<td bgcolor="#FFFFBB"><a href="'.$url.'"><img src="/adm/lonIcons/'.
  189:         $icon.'.gif" border="0"></a></td>';
  190: # Title
  191:     $title=&Apache::lonnet::unescape($title);
  192:     if ($title=~
  193:  /^(\d+)\_\_\_\&\&\&\_\_\_(\w+)\_\_\_\&\&\&\_\_\_(\w+)\_\_\_\&\&\&\_\_\_(.*)$/
  194: 	) { $title='<i>'.localtime($1).'</i> '.$2.' at '.$3.': <br>'.
  195: 		&Apache::lontexconvert::msgtexconverted($4);
  196: 	}
  197:     $line.="<td bgcolor='#FFFFBB'><a href='$url'>$title</a></td>";
  198:     $line.='</tr>';
  199:     return $line;
  200: }
  201: 
  202: # ================================================================ Main Handler
  203: sub handler {
  204:     my $r = shift;
  205:     $r->content_type('text/html');
  206:     $r->send_http_header;
  207:     return OK if $r->header_only;
  208: 
  209: # is this a standard course?
  210: 
  211:     my $standard=($ENV{'request.course.uri'}=~/^\/uploaded\//);
  212:     my $forcestandard;
  213:     my $forcesupplement;
  214:     my $script='';
  215:     my $allowed;
  216:     my $events='';
  217:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  218:        ['folder','showdoc']);
  219:     unless ($ENV{'form.showdoc'}) { # got called from remote
  220:        $forcestandard=($ENV{'form.folder'}=~/^default_/);
  221:        $forcesupplement=($ENV{'form.folder'}=~/^supplemental_/);
  222: 
  223: # does this user have privileges to post, etc?
  224:        $allowed=&Apache::lonnet::allowed('srm',$ENV{'request.course.id'});
  225:        if ($allowed) { 
  226:          &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['cmd']);
  227:          $script=&Apache::lonratedt::editscript('simple'); 
  228:        }
  229:     } else { # got called in sequence from course
  230:        $allowed=0;
  231:        $forcestandard=1;
  232:        $forcesupplement=0;
  233:        $script=&Apache::&Apache::lonxml::registerurl(1,undef);
  234:        $events='onLoad="'.&Apache::lonxml::loadevents.
  235:            '" onUnload="'.&Apache::lonxml::unloadevents.'"';
  236:     }
  237: 
  238: # get course data
  239:     my $coursenum=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
  240:     my $coursedom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
  241: 
  242: # get personal data
  243:  
  244:     my $uname=$ENV{'user.name'};
  245:     my $udom=$ENV{'user.domain'};
  246:     my $plainname=&Apache::lonnet::escape(
  247:                      &Apache::loncommon::plainname($uname,$udom));
  248: 
  249: # graphics settings
  250: 
  251:     $iconpath = $r->dir_config('lonIconsURL') . "/";
  252: 
  253: # print screen
  254:     $r->print(<<ENDDOCUMENT);
  255: <html>
  256: <head>
  257: <title>The LearningOnline Network with CAPA</title>
  258: <script>
  259: $script
  260: 
  261: function makenewfolder(targetform,folderseq) {
  262:     var foldername=prompt('Name of New Folder','New Folder');
  263:     if (foldername) {
  264: 	targetform.importdetail.value=foldername+"="+folderseq;
  265:         targetform.submit();
  266:     }
  267: }
  268: 
  269: function makenewext(targetform) {
  270: }
  271: </script>
  272: </head>
  273: ENDDOCUMENT
  274: $r->print(&Apache::loncommon::bodytag('Course Documents','',$events));
  275: # --------------------------------------------------0------ Standard documents
  276:     if (($standard) && ($allowed) && (!$forcesupplement)) {
  277:        $r->print('<h2>Main Course Documents</h2>');
  278:        my $folder=$ENV{'form.folder'};
  279:        unless ($folder=~/^default/) { $folder='default'; }
  280:        &editor($r,$coursenum,$coursedom,$folder,$allowed);
  281:        my $folderseq='/uploaded/'.$coursedom.'/'.$coursenum.'/default_'.time.
  282:                      '.sequence';
  283:        $r->print(<<ENDFORM);
  284: <table cellspacing=2><tr>
  285: <th bgcolor="#DDDDDD">Upload a new main course document</th>
  286: <th bgcolor="#DDDDDD">Import a published document</th>
  287: <th bgcolor="#DDDDDD">Special documents</th>
  288: </tr>
  289: <tr><td bgcolor="#DDDDDD">
  290: File:<br />
  291: <form action="/adm/coursedocs" method="post" enctype="multipart/form-data">
  292: <input type="file" name="uploaddoc" size="50">
  293: <br />
  294: Title:<br /> 
  295: <input type="text" size="50" name="comment">
  296: <input type="hidden" name="folder" value="$folder">
  297: <input type="hidden" name="cmd" value="upload_default">
  298: <input type="submit" value="Upload Document">
  299: </form>
  300: </td>
  301: <td bgcolor="#DDDDDD">
  302: <form action="/adm/coursedocs" method="post" name="simpleedit">
  303: <input type="hidden" name="folder" value="$folder">
  304: <input type=hidden name="importdetail" value="">
  305: <input type=button onClick=
  306: "javascript:groupsearch()" value="Search">
  307: <input type=button onClick=
  308: "javascript:groupimport();" value="Import">
  309: </form>
  310: </td><td bgcolor="#DDDDDD">
  311: <form action="/adm/coursedocs" method="post" name="newfolder">
  312: <input type="hidden" name="folder" value="$folder">
  313: <input type=hidden name="importdetail" value="">
  314: <input name="newfolder" type="button"
  315: onClick="javascript:makenewfolder(this.form,'$folderseq');"
  316: value="New Folder" />
  317: </form>
  318: <form action="/adm/coursedocs" method="post" name="newext">
  319: <input type="hidden" name="folder" value="$folder">
  320: <input type=hidden name="importdetail" value="">
  321: <input name="newext" type="button" onClick="javascript:makenewext(this.form);"
  322: value="External Resource" />
  323: </form>
  324: <form action="/adm/coursedocs" method="post" name="newsyl">
  325: <input type="hidden" name="folder" value="$folder">
  326: <input type=hidden name="importdetail" 
  327: value="Syllabus=/public/$coursedom/$coursenum/syllabus">
  328: <input name="newsyl" type="submit" value="Syllabus" />
  329: </form>
  330: <form action="/adm/coursedocs" method="post" name="newsyl">
  331: <input type=hidden name="importdetail" 
  332: value="Navigate Content=/adm/navmaps">
  333: <input name="newsyl" type="submit" value="Navigate Content" />
  334: </form>
  335: <form action="/adm/coursedocs" method="post" name="newaboutme">
  336: <input type="hidden" name="folder" value="$folder">
  337: <input type=hidden name="importdetail" 
  338: value="$plainname=/adm/$udom/$uname/aboutme">
  339: <input name="newaboutme" type="submit" value="My Personal Info" />
  340: </form>
  341: </td></tr>
  342: </table>
  343: ENDFORM
  344:        $r->print('<hr />');
  345:     }
  346: # ----------------------------------------------------- Supplemental documents
  347:     if (!$forcestandard) {
  348:        $r->print('<h2>Supplemental Course Documents</h2>');
  349:        my $folder=$ENV{'form.folder'};
  350:        unless ($folder=~/supplemental/) { $folder='supplemental'; }
  351:        &editor($r,$coursenum,$coursedom,$folder,$allowed);
  352:        if ($allowed) {
  353:           $r->print(<<ENDSUPFORM);
  354: <h3>Post a new supplemental course document</h3>
  355: <form action="/adm/coursedocs" method="post" enctype="multipart/form-data">
  356: <input type="file" name="uploaddoc" size="50">
  357: <br />Comment:<br />
  358: <textarea cols=50 rows=4 name='comment'>
  359: </textarea>
  360: <input type="hidden" name="folder" value="$folder">
  361: <input type="hidden" name="cmd" value="upload_supplemental">
  362: <input type="submit" value="Upload Document">
  363: </form>
  364: ENDSUPFORM
  365:        }
  366:     }
  367: 
  368:     $r->print('</body></html>');
  369:     return OK;
  370: } 
  371: 
  372: 1;
  373: __END__

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