Annotation of loncom/interface/lonsource.pm, revision 1.13

1.1       taceyjo1    1: # The LearningOnline Network with CAPA
1.2       albertel    2: # Souce Code handler
1.1       taceyjo1    3: #
1.13    ! www         4: # $Id: lonsource.pm,v 1.12 2005/06/23 23:07:04 www Exp $
1.1       taceyjo1    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;
1.11      albertel   34: use Apache::lonnet;
1.1       taceyjo1   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 {
1.4       taceyjo1   45:     my ($filename, $listname) = @_;
1.12      www        46:     my $sourcelink = "/adm/source?filename=".$filename."&listname=".$listname;
1.2       albertel   47: 
                     48:     return $sourcelink;
1.1       taceyjo1   49: }
                     50: 
                     51: sub stage_2 {
1.5       taceyjo1   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;
1.9       albertel   57:     my $html=&Apache::lonxml::xmlbegin();
                     58:     $r->print($html.'<head><title>LON-CAPA Move source to construction space</title>');
1.5       taceyjo1   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 />");
1.13    ! www        62:     $r->print('<form name="copy" action="/adm/source" target="_parent" method="post">
1.5       taceyjo1   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" name="newpath" value="/shared_source/'.$author.'" />&nbsp;
                     67:               <input type="submit" value="Copy" />
                     68:               </form>');
                     69:     return OK;
1.4       taceyjo1   70: }
                     71: 
                     72: 
                     73: sub copy_stage {
                     74:     my ($r, $filename, $listname, $newpath) = @_;
                     75:     my $role;
                     76:     my $domain;
                     77:     my $author_name;
1.5       taceyjo1   78: #Figure out if we are author or co-author
                     79: 
1.11      albertel   80:     if($env{'request.role'} =~ m|ca.|) {
                     81:         ($role, $domain, $author_name) = split(/\//,$env{'request.role'});
1.4       taceyjo1   82:     } else {
1.5       taceyjo1   83:         $role = "au.";
1.11      albertel   84:         $domain = $env{'user.domain'};
                     85:         $author_name = $env{'user.name'};
1.5       taceyjo1   86:     }
                     87: 
1.4       taceyjo1   88:     my $path_to_new_file = '/home/'.$author_name.'/public_html/'.$newpath.'/'.$listname;
1.5       taceyjo1   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: 
1.4       taceyjo1   91:     my ($uname,$udom)= &Apache::loncacc::constructaccess($path_to_new_file,$domain);
                     92:     unless (($uname) && ($udom)) {
1.5       taceyjo1   93:         return HTTP_NOT_ACCEPTABLE;
1.4       taceyjo1   94:     }
                     95:     &Apache::loncommon::content_type($r,'text/html');
                     96:     $r->send_http_header;
1.9       albertel   97:     my $html=&Apache::lonxml::xmlbegin();
                     98:     $r->print($html.'<head><title>LON-CAPA Move source to construction space</title>');
1.5       taceyjo1   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|) ) {
1.6       taceyjo1  103:         &delete_copy_file($r, $author_name, $newpath, $filename, $path_to_new_file, '1');
1.5       taceyjo1  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: 
1.4       taceyjo1  112: }
                    113: 
                    114: sub confirm {
1.5       taceyjo1  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 /	>");
1.13    ! www       117:     $r->print('<form name="delete_confirm" action="/adm/source" target="_parent" method="post">
1.5       taceyjo1  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>');
1.4       taceyjo1  126: }
                    127: 
1.6       taceyjo1  128: sub delete_copy_file {
1.5       taceyjo1  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 />");
1.6       taceyjo1  133:         $r->print('<input type="button" value="Close Window" name="close" onClick="window.close()" />');
1.5       taceyjo1  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:     }
1.1       taceyjo1  146: }
                    147: 
1.4       taceyjo1  148: sub copy_file {
1.5       taceyjo1  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>");
1.6       taceyjo1  172:     my $problem_filename = $Apache::lonnet::perlvar{'lonDocRoot'}.$filename;
1.5       taceyjo1  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: }
1.4       taceyjo1  182: 
1.1       taceyjo1  183: sub print_item {
1.5       taceyjo1  184:     my ($r, $filename) = @_;
1.6       taceyjo1  185:     $filename = $Apache::lonnet::perlvar{'lonDocRoot'}.$filename;
1.5       taceyjo1  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: 
1.1       taceyjo1  203: }
                    204: 
                    205: 
1.5       taceyjo1  206: sub handler {
1.2       albertel  207:     my $r=shift;
1.3       www       208:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.5       taceyjo1  209:                                             ['filename','listname']);
1.11      albertel  210:     my $filename = $env{'form.filename'};
                    211:     my $listname = $env{'form.listname'};
1.3       www       212:     my $source = &Apache::lonnet::metadata($filename,'sourceavail');
                    213:     if ($source ne 'open') {
1.11      albertel  214:         $env{'user.error.msg'}="$filename:cre:1:1:Source code not available";
1.5       taceyjo1  215:         return HTTP_NOT_ACCEPTABLE;
1.7       taceyjo1  216:     } 
                    217:     if ((!&Apache::lonnet::allowed('cre',$filename)) ||
1.5       taceyjo1  218:             (!&Apache::lonnet::allowed('bre',$filename))) {
1.11      albertel  219:         $env{'user.error.msg'}="$filename:bre:1:1:Access to resource denied";
1.5       taceyjo1  220:         return HTTP_NOT_ACCEPTABLE;
1.7       taceyjo1  221:     } 
1.11      albertel  222:     if ($env{'form.action'} eq 'stage2') {
1.5       taceyjo1  223:         my $author = &Apache::lonnet::metadata($filename,'authorspace');
1.10      albertel  224:         ($author) = split('@',$author); #strip the domain of the author name
1.11      albertel  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') {
1.5       taceyjo1  229:         &Apache::loncommon::content_type($r,'text/html');
                    230:         $r->send_http_header;
1.11      albertel  231:         &delete_copy_file($r, $env{'form.author'}, $env{'form.newpath'}, $env{'form.filename'}, $env{'form.path'}, '0');
1.6       taceyjo1  232:     } else {
1.5       taceyjo1  233:         &Apache::loncommon::content_type($r,'text/html');
                    234:         $r->send_http_header;
1.13    ! www       235:         $r->print('<form name="copy" action="/adm/source" target="_parent" method="post">
1.6       taceyjo1  236:                   <input type="button" value="Close Window" name="close" onClick="window.close()" />
1.5       taceyjo1  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 />');
1.11      albertel  243:         &print_item($r, $env{'form.filename'});
1.5       taceyjo1  244:     }
1.2       albertel  245:     return OK;
1.1       taceyjo1  246: }
                    247: 
                    248: 1;
1.5       taceyjo1  249: 
                    250: 

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