File:  [LON-CAPA] / loncom / interface / lonsource.pm
Revision 1.15: download - view: text, annotated - select for diffs
Fri Jul 8 10:39:49 2005 UTC (18 years, 10 months ago) by www
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_0_X, version_2_0_99_1, version_2_0_2, version_2_0_1, version_2_0_0, version_1_99_3, version_1_99_2, HEAD
Back in town.

    1: # The LearningOnline Network with CAPA
    2: # Souce Code handler
    3: #
    4: # $Id: lonsource.pm,v 1.15 2005/07/08 10:39:49 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: 
   30: 
   31: package Apache::lonsource;
   32: 
   33: use strict;
   34: use Apache::lonnet;
   35: use Apache::loncommon();
   36: use Apache::lonhtmlcommon();
   37: use Apache::lonsequence();
   38: use Apache::Constants qw(:common :http);
   39: use Apache::lonmeta;
   40: use Apache::File;
   41: use Apache::lonlocal;
   42: use HTML::Entities;
   43: 
   44: sub make_link {
   45:     my ($filename, $listname) = @_;
   46:     my $sourcelink = "/adm/source?filename=".$filename."&listname=".$listname;
   47: 
   48:     return $sourcelink;
   49: }
   50: 
   51: sub stage_2 {
   52:     my ($r, $filename, $author, $listname) = @_;
   53:     $filename = $filename;
   54:     &Apache::loncommon::content_type($r,'text/html');
   55:     my ($uname, $udom) = &Apache::loncacc::constructaccess('/~'.$author.'/',$r->dir_config('lonDefDomain'));
   56:     $r->send_http_header;
   57:     my $html=&Apache::lonxml::xmlbegin();
   58:     $r->print($html.'<head><title>LON-CAPA Move source to construction space</title>');
   59:     $r->print(&Apache::loncommon::bodytag('Problem source code moving operation'));
   60:     $r->print("Please enter the directory that you would like the source code to go into, a default has also been 			provided <br />");
   61:     $r->print("Also note, the path is in reference to the root of your construction space, and new directories will be 			automatically created. <br /><br />");
   62:     $r->print('<form name="copy" action="/adm/source" target="_parent" method="post">
   63:               <input type="hidden" name="filename" value="'.$filename.'" />
   64:               <input type="hidden" name="listname" value="'.$listname.'" />
   65:               <input type="hidden" name="action" value="copy_stage" />
   66:               <input type="text" size="50" name="newpath" value="/shared_source/'.$author.'" />&nbsp;
   67:               <input type="submit" value="Copy" />
   68:               </form>');
   69:     return OK;
   70: }
   71: 
   72: 
   73: sub copy_stage {
   74:     my ($r, $filename, $listname, $newpath) = @_;
   75:     my $role;
   76:     my $domain;
   77:     my $author_name;
   78: #Figure out if we are author or co-author
   79: 
   80:     if($env{'request.role'} =~ m|ca.|) {
   81:         ($role, $domain, $author_name) = split(/\//,$env{'request.role'});
   82:     } else {
   83:         $role = "au.";
   84:         $domain = $env{'user.domain'};
   85:         $author_name = $env{'user.name'};
   86:     }
   87: 
   88:     my $path_to_new_file = '/home/'.$author_name.'/public_html/'.$newpath.'/'.$listname;
   89: #Just checking again for access as we want to make sure that it is really ok now that we have the real path
   90: 
   91:     my ($uname,$udom)= &Apache::loncacc::constructaccess($path_to_new_file,$domain);
   92:     unless (($uname) && ($udom)) {
   93:         return HTTP_NOT_ACCEPTABLE;
   94:     }
   95:     &Apache::loncommon::content_type($r,'text/html');
   96:     $r->send_http_header;
   97:     my $html=&Apache::lonxml::xmlbegin();
   98:     $r->print($html.'<head><title>LON-CAPA Move source to construction space</title>');
   99:     $r->print(&Apache::loncommon::bodytag('Copying Source'));
  100:     my $result = &Apache::loncfile::exists($uname, $udom, $path_to_new_file);
  101:     $r->print($result);
  102:     if(($result) && ($result =~ m|published|) ) {
  103:         &delete_copy_file($r, $author_name, $newpath, $filename, $path_to_new_file, '1');
  104:     } elsif(($result) && ($result =~ m|exists!|)) {
  105:         &confirm($r, $author_name, $newpath, $filename, $path_to_new_file);
  106:     } else {
  107:         &copy_file($r, $author_name, $newpath, $filename, $path_to_new_file);
  108:     }
  109: 
  110:     return OK;
  111: 
  112: }
  113: 
  114: sub confirm {
  115:     my ($r, $author_name, $newpath, $filename, $path_to_new_file) = @_;
  116:     $r->print("<b>Press delete to remove file and replace it with a copy of the source you are viewing</b><br /><br /	>");
  117:     $r->print('<form name="delete_confirm" action="/adm/source" target="_parent" method="post">
  118:               <input type="hidden" name="filename" value="'.$filename.'" />
  119:               <input type="hidden" name="path" value="'.$path_to_new_file.'" />
  120:               <input type="hidden" name="author" value="'.$author_name.'" />
  121:               <input type="hidden" name="newpath" value="'.$newpath.'" />
  122:               <input type="hidden" name="action" value="delete_confirm" />
  123: 
  124:               <input type="submit" value="Delete" />
  125:               </form>');
  126: }
  127: 
  128: sub delete_copy_file {
  129:     my ($r, $author_name, $newpath, $filename, $path_to_new_file, $type) = @_;
  130:     if($type eq '1') {
  131:         $r->print("<b>Cannot delete non-obsolete published file</b><br />Please
  132:               use the code view in previous window to use shared code<br /><br />");
  133:         $r->print('<input type="button" value="Close Window" name="close" onClick="window.close()" />');
  134:     } else {
  135:         if(-e $path_to_new_file) {
  136:             unless(unlink($path_to_new_file)) {
  137:                 $r->print('<font color="red">'.&mt('Error').': '.$!.'</font>');
  138:                 return 0;
  139:             }
  140:         } else {
  141:             $r->print('<p> '.&mt('No such file').'. </p></form>');
  142:             return 0;
  143:         }
  144:         &copy_file($r, $author_name, $newpath, $filename, $path_to_new_file);
  145:     }
  146: }
  147: 
  148: sub copy_file {
  149:     my ($r, $author_name, $newpath, $filename, $path_to_new_file) = @_;
  150:     $r->print("<b>Creating directories</b>");
  151:     my $path = '/home/'.$author_name.'/public_html/';
  152:     my @directories = split(/\//,$newpath);
  153:     foreach my $now_checking (@directories) {
  154:         if($now_checking ne '') {
  155:             $path = $path.'/'.$now_checking;
  156:             if(-e $path) {} #More moving along, isn't recursion fun'
  157: 
  158:             else {
  159:                 unless(mkdir($path, 02770)) {
  160:                     $r->print('<font color="red">'.&mt('Error').': '.$!.'</font>');
  161:                     return 0;
  162:                 }
  163:                 unless(chmod(02770, ($path))) {
  164:                     $r->print('<font color="red"> '.&mt('Error').': '.$!.'</font>');
  165:                     return 0;
  166:                 }
  167:             }
  168:         } else { } #Just move along
  169: 
  170:     }
  171:     $r->print("<br /><b>Copying File</b>");
  172:     my $problem_filename = $Apache::lonnet::perlvar{'lonDocRoot'}.$filename;
  173:     my $file_output = &Apache::lonnet::getfile($problem_filename);
  174:     my $fs=Apache::File->new(">$path_to_new_file");
  175:     if (defined($fs)) {
  176:         print $fs $file_output;
  177:     }
  178:     $r->print("<br /><br />");
  179:     $r->print('<input type="button" value="Close Window" name="close" onClick="window.close()">');
  180:     #Some 1.3'ish feature is to include the derivative feature, will go here..'
  181: }
  182: 
  183: sub print_item {
  184:     my ($r, $filename) = @_;
  185:     $filename = $Apache::lonnet::perlvar{'lonDocRoot'}.$filename;
  186:     &Apache::lonnet::logthis("print_item filename = $filename");
  187:     my $file_output = &Apache::lonnet::getfile($filename);
  188:     my $count=0;
  189:     my $maxlength=-1;
  190:     foreach (split ("\n", $file_output)) {
  191:         $count+=int(length($_)/79);
  192:         $count++;
  193:         if (length($_) > $maxlength) {
  194:             $maxlength = length($_);
  195:         }
  196:     }
  197:     my $rows = $count;
  198:     my $cols = $maxlength;
  199:     $r->print('<textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
  200:               &HTML::Entities::encode($file_output,'<>&"').'</textarea>');
  201:     return OK;
  202: 
  203: }
  204: 
  205: 
  206: sub handler {
  207:     my $r=shift;
  208:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  209:                                             ['filename','listname']);
  210:     my $filename = $env{'form.filename'};
  211:     my $listname = $env{'form.listname'};
  212:     my $source = &Apache::lonnet::metadata($filename,'sourceavail');
  213:     if ($source ne 'open') {
  214:         $env{'user.error.msg'}="$filename:cre:1:1:Source code not available";
  215:         return HTTP_NOT_ACCEPTABLE;
  216:     } 
  217:     unless ((&Apache::lonnet::allowed('bre',$filename)) &&
  218: 	    (&Apache::lonnet::allowed('cre','/'))) {
  219:         $env{'user.error.msg'}="$filename:bre:1:1:Access to resource denied";
  220:         return HTTP_NOT_ACCEPTABLE;
  221:     } 
  222:     if ($env{'form.action'} eq 'stage2') {
  223:         my $author = &Apache::lonnet::metadata($filename,'authorspace');
  224:         ($author) = split('@',$author); #strip the domain of the author name
  225:         &stage_2($r, $env{'form.filename'}, $author, $listname);
  226:     } elsif($env{'form.action'} eq 'copy_stage') {
  227:         &copy_stage($r, $filename,$env{'form.listname'},$env{'form.newpath'});
  228:     } elsif($env{'form.action'} eq 'delete_confirm') {
  229:         &Apache::loncommon::content_type($r,'text/html');
  230:         $r->send_http_header;
  231:         &delete_copy_file($r, $env{'form.author'}, $env{'form.newpath'}, $env{'form.filename'}, $env{'form.path'}, '0');
  232:     } else {
  233:         &Apache::loncommon::content_type($r,'text/html');
  234:         $r->send_http_header;
  235:         $r->print('<form name="copy" action="/adm/source" target="_parent" method="post">
  236:                   <input type="button" value="Close Window" name="close" onClick="window.close()" />
  237:                   <input type="hidden" name="filename" value="'.$filename.'" />
  238:                   <input type="hidden" name="listname" value="'.$listname.'" />
  239:                   <input type="hidden" name="action" value="stage2" />
  240:                   <input type="submit" value="Copy to CSTR" />
  241:                   </form>');
  242:         $r->print('<hr />');
  243:         &print_item($r, $env{'form.filename'});
  244:     }
  245:     return OK;
  246: }
  247: 
  248: 1;
  249: 
  250: 

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