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

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

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