1: # The LearningOnline Network
2: # portfolio browser
3: #
4: # $Id: portfolio.pm,v 1.168 2006/11/01 16:09:04 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::portfolio;
30: use strict;
31: use Apache::Constants qw(:common :http);
32: use Apache::loncommon;
33: use Apache::lonnet;
34: use Apache::lontexconvert;
35: use Apache::lonfeedback;
36: use Apache::lonlocal;
37: use Apache::lonnet;
38: use Apache::longroup;
39: use Apache::lonhtmlcommon;
40: use HTML::Entities;
41: use LONCAPA;
42:
43: sub group_args {
44: my $output;
45: if (defined($env{'form.group'})) {
46: $output .= '&group='.$env{'form.group'};
47: if (defined($env{'form.ref'})) {
48: $output .= '&ref='.$env{'form.ref'};
49: }
50: }
51: return $output;
52: }
53:
54: sub group_form_data {
55: my $output;
56: if (defined($env{'form.group'})) {
57: $output = '<input type="hidden" name="group" value="'.$env{'form.group'}.'" />';
58: if (exists($env{'form.ref'})) {
59: $output .= '<input type="hidden" name="ref" value="'.
60: $env{'form.ref'}.'" />';
61: }
62: }
63: return $output;
64: }
65:
66: # receives a file name and path stub from username/userfiles/portfolio/
67: # returns an anchor tag consisting encoding filename and currentpath
68: sub make_anchor {
69: my ($url, $anchor_fields, $inner_text) = @_;
70: if ($$anchor_fields{'continue'} ne 'true') {$$anchor_fields{'continue'} = 'false'};
71: my $anchor = '<a href="'.$url.'?';
72: foreach my $field_name (keys(%$anchor_fields)) {
73: $anchor .= $field_name.'='.$$anchor_fields{$field_name}.'&';
74: }
75: $anchor =~ s/&$//;
76: $anchor .= &group_args();
77: $anchor .= '">'.$inner_text.'</a>';
78: return $anchor;
79: }
80:
81: my $dirptr=16384;
82: sub display_common {
83: my ($r,$url,$current_path,$is_empty,$dir_list,$can_upload)=@_;
84: my $namespace = &get_namespace();
85: my $port_path = &get_port_path();
86: if ($can_upload) {
87: my $groupitem = &group_form_data();
88:
89: my $iconpath= $r->dir_config('lonIconsURL') . "/";
90: my %text=&Apache::lonlocal::texthash(
91: 'upload' => 'Upload',
92: 'upload_label' =>
93: 'Upload file to current directory:',
94: 'createdir' => 'Create Subdirectory',
95: 'createdir_label' =>
96: 'Create subdirectory in current directory:');
97: my $escuri = &HTML::Entities::encode($r->uri,'&<>"');
98: my $help_fileupload = &Apache::loncommon::help_open_topic('Portfolio AddFiles');
99: my $help_createdir = &Apache::loncommon::help_open_topic('Portfolio CreateDirectory');
100:
101: # FIXME: This line should be deleted once Portfolio uses breadcrumbs
102: $r->print(&Apache::loncommon::help_open_topic('Portfolio About', 'Help on the portfolio'));
103:
104:
105: $r->print(<<"TABLE");
106: <table id="LC_portfolio_actions">
107: <tr id="LC_portfolio_upload">
108: <td class="LC_label">
109: $text{'upload_label'}
110: </td>
111: <td class="LC_value">
112: <form method="post" enctype="multipart/form-data" action="$escuri">
113: $groupitem
114: <input name="uploaddoc" type="file" />
115: <input type="hidden" name="currentpath" value="$current_path" />
116: <input type="hidden" name="action" value="$env{"form.action"}" />
117: <input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
118: <input type="hidden" name="mode" value="$env{"form.mode"}" />
119: <input type="submit" name="storeupl" value="$text{'upload'}" />$help_fileupload
120: </form>
121: </td>
122: </tr>
123: <tr id="LC_portfolio_createdir">
124: <td class="LC_label">
125: $text{'createdir_label'}
126: </td>
127: <td class="LC_value">
128: <form method="post" action="$escuri">
129: <input name="newdir" type="input" />$groupitem
130: <input type="hidden" name="currentpath" value="$current_path" />
131: <input type="hidden" name="action" value="$env{"form.action"}" />
132: <input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
133: <input type="hidden" name="mode" value="$env{"form.mode"}" />
134: <input type="submit" name="createdir" value="$text{'createdir'}" />$help_createdir
135: </form>
136: </td>
137: </tr>
138: </table>
139: TABLE
140: }
141: my @tree = split (/\//,$current_path);
142: my %anchor_fields = (
143: 'selectfile' => $port_path,
144: 'currentpath' => '/',
145: 'mode' => $env{"form.mode"},
146: 'fieldname' => $env{"form.fieldname"},
147: 'continue' => $env{"form.continue"}
148: );
149: $r->print('<span class="LC_current_location">'.&make_anchor($url,\%anchor_fields,$port_path).'/');
150: if (@tree > 1){
151: my $newCurrentPath = '';
152: for (my $i = 1; $i< @tree; $i++){
153: $newCurrentPath .= $tree[$i].'/';
154: my %anchor_fields = (
155: 'selectfile' => $tree[$i],
156: 'currentpath' => '/',
157: 'mode' => $env{"form.mode"},
158: 'fieldname' => $env{"form.fieldname"},
159: 'continue' => $env{"form.continue"}
160: );
161: $r->print(&make_anchor($url,\%anchor_fields,$tree[$i]).'/');
162: }
163: }
164: $r->print('</span>');
165: $r->print(&Apache::loncommon::help_open_topic('Portfolio ChangeDirectory'));
166: &Apache::lonhtmlcommon::store_recent($namespace,$current_path,$current_path);
167: $r->print('<br /><form method="post" action="'.$url.'?mode='.$env{"form.mode"}.'&fieldname='.$env{"form.fieldname"}.&group_args());
168: $r->print('">'.
169: &Apache::lonhtmlcommon::select_recent($namespace,'currentpath',
170: 'this.form.submit();'));
171: $r->print("</form>");
172: }
173:
174: sub display_directory_line {
175: my ($r,$select_mode, $filename, $mtime, $size, $css_class,
176: $line, $access_controls, $curr_access, $now, $version_flag,
177: $href_location, $url, $current_path, $access_admin_text, $versions)=@_;
178:
179: my $fullpath = &prepend_group($current_path.$filename);
180: $r->print('<tr class="'.$css_class.'">');
181: $r->print($line); # contains first two cells of table
182: my $lock_info;
183: if ($version_flag) { # versioned can't be versioned, so TRUE when root file
184: $r->print('<td><img alt="" src="'.&Apache::loncommon::icon($filename).'" /></td>');
185: $r->print('<td>'.$version_flag.'</td>');
186: } else { # this is a graded or handed back file
187: my ($user,$domain) = &get_name_dom();
188: my $permissions_hash = &Apache::lonnet::get_portfile_permissions($domain,$user);
189: if (defined($$permissions_hash{$fullpath})) {
190: foreach my $array_item (@{$$permissions_hash{$fullpath}}) {
191: if (ref($array_item) eq 'ARRAY') {
192: if ($$array_item[-1] eq 'handback') {
193: $lock_info = 'Handback';
194: } elsif ($$array_item[-1] eq 'graded') {
195: $lock_info = 'Graded';
196: }
197: }
198: }
199: }
200: if ($lock_info) {
201: my %anchor_fields = ('lockinfo' => $fullpath);
202: if ($versions) { # hold the folder open
203: my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($fullpath);
204: $fname =~ s|^/||;
205: $anchor_fields{'showversions'} = $fname.'.'.$extension;
206: }
207: $lock_info = &make_anchor(undef,\%anchor_fields,$lock_info);
208: }
209: $r->print('<td colspan="2">'.$lock_info.'</td>');
210: }
211: # $r->print('<td>'.$$version_flag{$filename}.'</td><td>');
212: $r->print('<td>'.&make_anchor($href_location.$filename,undef,$filename).'</td>');
213: $r->print('<td>'.$size.'</td>');
214: $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
215: if ($select_mode ne 'true') {
216: $r->print('<td><span style="white-space: nowrap">'.
217: &mt($curr_access).' ');
218: my %anchor_fields = (
219: 'access' => $filename,
220: 'currentpath' => $current_path
221: );
222: $r->print(&make_anchor($url, \%anchor_fields, $access_admin_text).'</span></td>');
223: }
224: $r->print('</tr>'.$/);
225: }
226:
227: sub display_directory {
228: my ($r,$url,$current_path,$is_empty,$dir_list,$group,$can_upload,
229: $can_modify,$can_delete,$can_setacl)=@_;
230: my $iconpath= $r->dir_config('lonIconsURL') . "/";
231: my $display_out;
232: my $select_mode;
233: my $checked_files;
234: my $port_path = &get_port_path();
235: my ($uname,$udom) = &get_name_dom();
236: my $access_admin_text = &mt('View Status');
237: if ($can_setacl) {
238: $access_admin_text = &mt('View/Change Status');
239: }
240:
241: my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
242: $uname);
243: my %locked_files = &Apache::lonnet::get_marked_as_readonly_hash(
244: $current_permissions,$group);
245: my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group);
246: my $now = time;
247: if ($env{"form.mode"} eq 'selectfile'){
248: &select_files($r);
249: $checked_files =&Apache::lonnet::files_in_path($uname,$env{'form.currentpath'});
250: $select_mode = 'true';
251: }
252: if ($is_empty && ($current_path ne '/') && $can_delete) {
253: $display_out = '<form method="post" action="'.$url.'">'.
254: &group_form_data().
255: '<input type="hidden" name="action" value="deletedir" />'.
256: '<input type="submit" name="deletedir" value="'.&mt("Delete Directory").'" />'.
257: '<input type="hidden" name="selectfile" value="" />'.
258: '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
259: '</form>';
260:
261: $r->print($display_out);
262: return;
263: }
264: if ($select_mode eq 'true') {
265: $r->print('<form method="post" name="checkselect" action="'.$url.'">');
266: $r->print('<table id="LC_browser">'.
267: '<tr><th>Select</th><th> </th><th> </th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
268: } else {
269: $r->print('<form method="post" action="'.$url.'">');
270: $r->print(&Apache::loncommon::help_open_topic('Portfolio FileList',
271: 'Using the portfolio file list'));
272: $r->print('<table id="LC_browser">'.
273: '<tr><th colspan="2">Actions</th><th> </th><th> </th><th>Name'.
274: &Apache::loncommon::help_open_topic('Portfolio OpenFile').
275: '</th><th>Size</th><th>Last Modified</th><th>Current Access Status'.
276: &Apache::loncommon::help_open_topic('Portfolio ShareFile').
277: '</th></tr>');
278: }
279: $r->print("\n".&group_form_data()."\n");
280:
281: my $href_location="/uploaded/$udom/$uname/$port_path".$current_path;
282: my $href_edit_location="/editupload/$udom/$uname/$port_path".$current_path;
283: my @dir_lines;
284: my %versioned;
285: foreach my $dir_line (sort
286: {
287: my ($afile)=split('&',$a,2);
288: my ($bfile)=split('&',$b,2);
289: return (lc($afile) cmp lc($bfile));
290: } (@$dir_list)) {
291: #$strip holds directory/file name
292: #$dom
293: my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$dir_line,16);
294: $filename =~ s/\s+$//;
295: my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
296: if ($version) {
297: my $fullpath = &prepend_group($current_path.$fname.'.'.$extension);
298: push(@{ $versioned{$fullpath} },
299: [$filename,$dom,$testdir,$size,$mtime,$obs,]);
300: } else {
301: push(@dir_lines, [$filename,$dom,$testdir,$size,$mtime,$obs]);
302: }
303: }
304: foreach my $dir_line (@dir_lines) {
305: my ($filename,$dom,$testdir,$size,$mtime,$obs) = @$dir_line;
306: my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
307: if (($filename ne '.') && ($filename ne '..') && ($filename !~ /\.meta$/ ) && ($filename !~ /(.*)\.(\d+)\.([^\.]*)$/)) {
308: my $version_flag;
309: my $show_versions;
310: my $fullpath = &prepend_group($current_path.$filename);
311: if ($env{'form.showversions'} =~ /$filename/) {
312: $show_versions = 'true';
313: }
314: if (exists($versioned{$fullpath})) {
315: my %anchor_fields = (
316: 'selectfile' => $fullpath,
317: 'continue' => 'false',
318: 'currentpath' => $current_path,
319: );
320: if ($show_versions) {
321: # Must preserve other possible showversion files
322: my $version_remainder = $env{'form.showversions'};
323: $version_remainder =~ s/$filename//g;
324: $anchor_fields{'showversions'} = $version_remainder;
325: $version_flag = &make_anchor('portfolio',\%anchor_fields,
326: '<img class="LC_icon" alt="'.&mt('opened folder').'" src="'.$iconpath.'folder_pointer_opened.gif" />');
327: } else {
328: # allow multiple files to show versioned
329: $anchor_fields{'showversions'} = $env{'form.showversions'}.','.$filename;
330: $version_flag = &make_anchor('portfolio',\%anchor_fields,
331: '<img class="LC_icon" alt="'.&mt('closed folder').'" src="'.$iconpath.'folder_pointer_closed.gif" />');
332: }
333: } else {
334: $version_flag = ' ';
335: }
336: if ($dirptr&$testdir) {
337: my $colspan='colspan="2"';
338: if ($select_mode eq 'true'){
339: undef($colspan);
340: }
341: $r->print('<tr class="LC_browser_folder"><td '.$colspan.'><img alt="'.&mt('closed folder').'" src="'.$iconpath.'folder_closed.gif" /></td>');
342: $r->print('<td>Go to ...</td>');
343: my %anchor_fields = (
344: 'selectfile' => $filename.'/',
345: 'currentpath' => $current_path.$filename.'/',
346: 'mode' => $env{"form.mode"},
347: 'fieldname' => $env{"form.fieldname"},
348: 'continue' => $env{"form.continue"}
349: );
350: $r->print('<td>'.$version_flag.'</td><td>'.&make_anchor($url,\%anchor_fields,$filename.'/').'</td>');
351: $r->print('</tr>');
352: } else {
353: my $css_class = 'LC_browser_file';
354: my $line;
355: if ($select_mode eq 'true') {
356: $line='<td><input type="checkbox" name="checkfile" value="'.$filename.'"';
357: if ($$checked_files{$filename} eq 'selected') {
358: $line.=" checked ";
359: }
360: $line.=' /></td>';
361: } else {
362: if (exists $locked_files{$fullpath}) {
363: my %anchor_fields = (
364: 'lockinfo' => $fullpath
365: );
366: $line.='<td colspan="2">'.&make_anchor($url,\%anchor_fields,'Locked').'</td>';
367: $css_class= 'LC_browser_file_locked';
368: } else {
369: if (!$can_modify) {
370: $line .= '<td colspan="2">';
371: } else {
372: $line .= '<td>';
373: }
374: if ($can_delete) {
375: $line .= '<input type="checkbox" name="selectfile" value="'.$filename.'" />';
376: }
377: if ($can_modify) {
378: my $cat='<img class="LC_icon" alt="'.&mt('Catalog Information').
379: '" src="'.&Apache::loncommon::lonhttpdurl('/res/adm/pages/catalog.gif').'" />';
380: my %anchor_fields = (
381: 'rename' => $filename,
382: currentpath => $current_path
383: );
384: $line .= &make_anchor($url,\%anchor_fields,'Rename');
385: $line .= '</td><td>'.&make_anchor($href_edit_location.$filename.'.meta',\%anchor_fields,$cat);
386: # '<a href="'.$href_edit_location.$filename.'.meta">'.$cat.'</a>';
387: }
388: $line .= '</td>';
389: }
390: }
391: my $curr_access;
392: if ($select_mode ne 'true') {
393: my $pub_access = 0;
394: my $guest_access = 0;
395: my $cond_access = 0;
396: foreach my $key (sort(keys(%{$access_controls{$fullpath}}))) {
397: my ($num,$scope,$end,$start) = &unpack_acc_key($key);
398: if (($now > $start) && (!$end || $end > $now)) {
399: if ($scope eq 'public') {
400: $pub_access = 1;
401: } elsif ($scope eq 'guest') {
402: $guest_access = 1;
403: } else {
404: $cond_access = 1;
405: }
406: }
407: }
408: if (!$pub_access && !$guest_access && !$cond_access) {
409: $curr_access = &mt('Private');
410: } else {
411: my @allaccesses;
412: if ($pub_access) {
413: push(@allaccesses,&mt('Public'));
414: }
415: if ($guest_access) {
416: push(@allaccesses,&mt('Passphrase-protected'));
417: }
418: if ($cond_access) {
419: push(@allaccesses,&mt('Conditional'));
420: }
421: $curr_access = join('+ ',@allaccesses);
422: }
423: }
424: &display_directory_line($r,$select_mode, $filename, $mtime, $size, $css_class, $line,
425: \%access_controls, $curr_access,$now, $version_flag, $href_location,
426: $url, $current_path, $access_admin_text);
427: if ($show_versions) {
428: foreach my $dir_line (@{ $versioned{$fullpath} }) {
429: my ($v_filename,$dom,$testdir,$size,$mtime,$obs) =
430: @$dir_line;
431: $line = '<td colspan="2"> </td>';
432: &display_directory_line($r,$select_mode, $v_filename, $mtime, $size,
433: $css_class, $line, \%access_controls, $curr_access, $now,
434: undef, $href_location, $url, $current_path, $access_admin_text, 1);
435: }
436: }
437: }
438: }
439: }
440: if ($select_mode eq 'true') {
441: $r->print('</table>
442: <input type="hidden" name="continue" value="true" />
443: <input type="hidden" name="fieldname" value="'.$env{'form.fieldname'}.'" />
444: <input type="hidden" name="mode" value="selectfile" />
445: <input type="submit" name="submit" value="Select checked files, and continue selecting." /><br />
446: <input type="button" name="doit" onClick= "finishSelect();" value="Select checked files, and close window" />
447: <input type="hidden" name="currentpath" value="'.$current_path.'" />
448: </form>');
449: } else {
450: $r->print('</table>');
451: if ($can_delete) {
452: $r->print('
453: <input type="submit" name="doit" value="Delete Checked Files" />'.
454: &Apache::loncommon::help_open_topic('Portfolio DeleteFile').'
455: <input type="hidden" name="action" value="delete" />
456: <input type="hidden" name="currentpath" value="'.$current_path.'" />
457: </form>'
458: );
459: }
460: }
461: }
462:
463: sub open_form {
464: my ($r,$url)=@_;
465: my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
466: $r->print('<form name="portform" method="post" action="'.$url.'">');
467: $r->print('<input type="hidden" name="action" value="'.
468: $env{'form.action'}.'" />');
469: $r->print('<input type="hidden" name="confirmed" value="1" />');
470: foreach (@files) {
471: $r->print('<input type="hidden" name="selectfile" value="'.
472: $_.'" />');
473: }
474: $r->print('<input type="hidden" name="currentpath" value="'.
475: $env{'form.currentpath'}.'" />');
476: }
477:
478: sub close_form {
479: my ($r,$url,$button_text)=@_;
480: if (!defined($button_text)) {
481: $button_text = {
482: 'continue' => &mt('Continue'),
483: 'cancel' => &mt('Cancel'),
484: };
485: }
486: $r->print('<p><input type="submit" value="'.$button_text->{'continue'}.'" />');
487: $r->print(&group_form_data().'</p></form>');
488: $r->print('<form action="'.$url.'" method="post">
489: <p>
490: <input type="hidden" name="currentpath" value="'.
491: $env{'form.currentpath'}.'" />'.
492: &group_form_data());
493: $r->print("\n".' <input type="submit" value="'.$button_text->{'cancel'}.'" />
494: </p></form>');
495: }
496:
497: sub display_file {
498: my ($path,$filename)=@_;
499: my $display_file_text;
500: my $file_start='<span class="LC_filename">';
501: my $file_end='</span>';
502: if (!defined($path)) { $path=$env{'form.currentpath'}; }
503: if (!defined($filename)) {
504: $filename=$env{'form.selectfile'};
505: $display_file_text = $file_start.$path.$filename.$file_end;
506: } elsif (ref($filename) eq "ARRAY") {
507: foreach my $file (@$filename) {
508: $display_file_text .= $file_start.$path.$file.$file_end.'<br />';
509: }
510: } elsif (ref($filename) eq "SCALAR") {
511: $display_file_text = $file_start.$path.$$filename.$file_end;
512: } else {
513: $display_file_text = $file_start.$path.$filename.$file_end;
514: }
515: return $display_file_text;
516: }
517:
518: sub done {
519: my ($message,$url)=@_;
520: unless (defined $message) {
521: $message='Done';
522: }
523: my %anchor_fields = (
524: 'showversions' => $env{'form.showversions'},
525: 'currentpath' => $env{'form.currentpath'},
526: 'fieldname' => $env{'form.fieldname'},
527: 'mode' => $env{'form.mode'}
528: );
529: my $result = '<h3>'.&make_anchor($url,\%anchor_fields,&mt($message)).'</h3>';
530: return $result;
531: }
532:
533: sub delete {
534: my ($r,$url)=@_;
535: my @check;
536: my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
537: $file_name = &prepend_group($file_name);
538: my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
539: my ($uname,$udom) = &get_name_dom();
540: if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
541: $r->print ("The file is locked and cannot be deleted.<br />");
542: $r->print(&done('Back',$url));
543: } else {
544: if (scalar(@files)) {
545: &open_form($r,$url);
546: $r->print('<p>'.&mt('Delete').' '.&display_file(undef,\@files).'?</p>');
547: &close_form($r,$url);
548: } else {
549: $r->print("No file was checked to delete.<br />");
550: $r->print(&done(undef,$url));
551: }
552: }
553: }
554:
555: sub delete_confirmed {
556: my ($r,$url,$group)=@_;
557: my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
558: my $result;
559: my ($uname,$udom) = &get_name_dom();
560: my $port_path = &get_port_path();
561: my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
562: $uname);
563: foreach my $delete_file (@files) {
564: $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
565: $env{'form.currentpath'}.
566: $delete_file);
567: if ($result ne 'ok') {
568: $r->print('<span class="LC_error">'.
569: &mt('An error occured ([_1]) while trying to delete
570: [_2].',$result,&display_file(undef, $delete_file)).
571: '</span><br /><br />');
572: } else {
573: $r->print(&mt('File: [_1] deleted.',
574: &display_file(undef,$delete_file)));
575: my $file_name = $env{'form.currentpath'}.$delete_file;
576: $file_name = &prepend_group($file_name);
577: my %access_controls =
578: &Apache::lonnet::get_access_controls($current_permissions,
579: $group,$file_name);
580: if (keys(%access_controls) > 0) {
581: my %changes;
582: foreach my $key (keys(%{$access_controls{$file_name}})) {
583: $changes{'delete'}{$key} = 1;
584: }
585: if (keys(%changes) > 0) {
586: my ($outcome,$deloutcome,$new_values,$translation) =
587: &Apache::lonnet::modify_access_controls($file_name,\%changes,
588: $udom,$uname);
589: if ($outcome ne 'ok') {
590: $r->print('<br />'.&mt("An error occured ([_1]) while ".
591: "trying to delete access controls for the file.",$outcome).
592: '</span><br /><br />');
593: } else {
594: if ($deloutcome eq 'ok') {
595: $r->print('<br />'.&mt('Access controls also deleted for the file.').'<br /><br />');
596: } else {
597: $r->print('<span class="LC_error">'.'<br />'.
598: &mt("An error occured ([_1]) while ".
599: "trying to delete access controls for the file.",$deloutcome).
600: '</span><br /><br />');
601: }
602: }
603: }
604: }
605: }
606: }
607: $r->print(&done(undef,$url));
608: }
609:
610: sub delete_dir {
611: my ($r,$url)=@_;
612: &open_form($r,$url);
613: $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
614: &close_form($r,$url);
615: }
616:
617: sub delete_dir_confirmed {
618: my ($r,$url)=@_;
619: my $directory_name = $env{'form.currentpath'};
620: $directory_name =~ s|/$||; # remove any trailing slash
621: my ($uname,$udom) = &get_name_dom();
622: my $namespace = &get_namespace();
623: my $port_path = &get_port_path();
624: my $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
625: $directory_name);
626:
627: if ($result ne 'ok') {
628: $r->print('<span class="LC_error"> An error occured (dir) ('.$result.
629: ') while trying to delete '.$directory_name.'</span><br />');
630: } else {
631: # now remove from recent
632: # $r->print('<br /> removing '.$directory_name.'<br /');
633: &Apache::lonhtmlcommon::remove_recent($namespace,[$directory_name.'/']);
634: my @dirs = split m!/!, $directory_name;
635:
636: # $directory_name =~ m/^(\/*\/)(\/*.)$/;
637: $directory_name='/';
638: for (my $i=1; $i < (@dirs - 1); $i ++){
639: $directory_name .= $dirs[$i].'/';
640: }
641: $env{'form.currentpath'} = $directory_name;
642: }
643: $r->print(&done(undef,$url));
644: }
645:
646: sub rename {
647: my ($r,$url)=@_;
648: my $file_name = $env{'form.currentpath'}.$env{'form.rename'};
649: my ($uname,$udom) = &get_name_dom();
650: $file_name = &prepend_group($file_name);
651: if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
652: $r->print ("The file is locked and cannot be renamed.<br />");
653: $r->print(&done(undef,$url));
654: } else {
655: &open_form($r,$url);
656: $r->print('<p>'.&mt('Rename').' '.&display_file().' to
657: <input name="filenewname" type="input" size="50" />?</p>');
658: &close_form($r,$url);
659: }
660: }
661:
662: sub rename_confirmed {
663: my ($r,$url,$group)=@_;
664: my $filenewname=&Apache::lonnet::clean_filename($env{'form.filenewname'});
665: my ($uname,$udom) = &get_name_dom();
666: my $port_path = &get_port_path();
667: if ($filenewname eq '') {
668: $r->print('<span class="LC_error">'.
669: &mt("Error: no valid filename was provided to rename to.").
670: '</span><br />');
671: $r->print(&done(undef,$url));
672: return;
673: }
674: my $chg_access;
675: my $result=
676: &Apache::lonnet::renameuserfile($uname,$udom,
677: $port_path.$env{'form.currentpath'}.$env{'form.selectfile'},
678: $port_path.$env{'form.currentpath'}.$filenewname);
679: if ($result eq 'ok') {
680: $chg_access = &access_for_renamed($filenewname,$group,$udom,$uname);
681: } else {
682: $r->print('<span class="LC_error">'.
683: &mt('An error occured ([_1]) while trying to rename [_2]'
684: .' to [_3]',$result,&display_file(),
685: &display_file('',$filenewname)).'</span><br />');
686: return;
687: }
688: if ($filenewname ne $env{'form.filenewname'}) {
689: $r->print(&mt("The new file name was changed from:<br />[_1] to [_2]",
690: '<strong>'.&display_file('',$env{'form.filenewname'}).'</strong>',
691: '<strong>'.&display_file('',$filenewname).'</strong>'));
692: }
693: $r->print($chg_access);
694: $r->print(&done(undef,$url));
695: }
696:
697: sub access_for_renamed {
698: my ($filenewname,$group,$udom,$uname) = @_;
699: my $oldfile = $env{'form.currentpath'}.$env{'form.selectfile'};
700: $oldfile = &prepend_group($oldfile);
701: my $newfile = $env{'form.currentpath'}.$filenewname;
702: $newfile = &prepend_group($newfile);
703: my $current_permissions =
704: &Apache::lonnet::get_portfile_permissions($udom,$uname);
705: my %access_controls =
706: &Apache::lonnet::get_access_controls($current_permissions,
707: $group,$oldfile);
708: my $chg_text;
709: if (keys(%access_controls) > 0) {
710: my %change_old;
711: my %change_new;
712: foreach my $key (keys(%{$access_controls{$oldfile}})) {
713: $change_old{'delete'}{$key} = 1;
714: $change_new{'activate'}{$key} = $access_controls{$oldfile}{$key};
715: }
716: my ($outcome,$deloutcome,$new_values,$translation) =
717: &Apache::lonnet::modify_access_controls($oldfile,\%change_old,
718: $udom,$uname);
719: if ($outcome ne 'ok') {
720: $chg_text ='<br /><br />'.&mt("An error occured ([_1]) while ".
721: "trying to delete access control records for the old name.",$outcome).
722: '</span><br />';
723: } else {
724: if ($deloutcome ne 'ok') {
725: $chg_text = '<br /><br /><span class="LC_error"><br />'.
726: &mt("An error occured ([_1]) while ".
727: "trying to delete access control records for the old name.",$deloutcome).
728: '</span><br />';
729: }
730: }
731: ($outcome,$deloutcome,$new_values,$translation) =
732: &Apache::lonnet::modify_access_controls($newfile,\%change_new,
733: $udom,$uname);
734: if ($outcome ne 'ok') {
735: $chg_text .= '<br /><br />'.
736: &mt("An error occured ([_1]) while ".
737: "trying to update access control records for the new name.",$outcome).
738: '</span><br />';
739: }
740: if ($chg_text eq '') {
741: $chg_text = '<br /><br />'.&mt('Access controls updated to reflect the name change.');
742: }
743: }
744: return $chg_text;
745: }
746:
747: sub display_access {
748: my ($r,$url,$group,$can_setacl,$port_path) = @_;
749: my ($uname,$udom) = &get_name_dom();
750: my $file_name = $env{'form.currentpath'}.$env{'form.access'};
751: $file_name = &prepend_group($file_name);
752: my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
753: $uname);
754: my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group,$file_name);
755: my $aclcount = keys(%access_controls);
756: my $header = '<h3>'.&mt('Allowing others to retrieve file: [_1]',$port_path.$env{'form.currentpath'}.$env{'form.access'}).'</h3>';
757: my $info .= &mt('Access to this file by others can be set to be one or more of the following types: public, passphrase-protected or conditional.');
758: $info .= '<br /><ul><li>'.&mt('Public files are available to anyone without the need for login.');
759: $info .= '</li><li>'.&mt('Passphrase-protected files do not require log-in, but will require the viewer to enter the passphrase you set.');
760: $info .= '</li><li>'.&mt('Conditional files are accessible to logged-in users with accounts in the LON-CAPA network, who satisfy the conditions you set.').'<br />';
761: $info .= &mt('The conditions can include affiliation with a particular course or group, or a user account in a specific domain.').'<br />';
762: $info .= &mt('Alternatively access can be granted to people with specific LON-CAPA usernames and domains.').'</li></ul>';
763: if ($group eq '') {
764: $info .= (&mt("A listing of files viewable without log-in is available at: <a href='/adm/$udom/$uname/aboutme/portfolio'>http://$ENV{'SERVER_NAME'}/adm/$udom/$uname/aboutme/portfolio</a>.<br />For logged in users a 'Display file listing' link will also appear (when there are viewable files) on your personal information page:<br /><a href='/adm/$udom/$uname/aboutme'>http://$ENV{'SERVER_NAME'}/adm/$udom/$uname/aboutme</a><br />"));
765: }
766: if ($can_setacl) {
767: &open_form($r,$url);
768: $r->print($header.$info);
769: $r->print ('<br/>'.&Apache::loncommon::help_open_topic('Portfolio ShareFile SetAccess', 'Help on setting up share access'));
770: $r->print (&Apache::loncommon::help_open_topic('Portfolio ShareFile ChangeSetting', 'Help on changing settings'));
771: $r->print (&Apache::loncommon::help_open_topic('Portfolio ShareFile StopAccess', 'Help on removing share access'));
772: &access_setting_table($r,$access_controls{$file_name});
773: my $button_text = {
774: 'continue' => &mt('Proceed'),
775: 'cancel' => &mt('Back to directory listing'),
776: };
777: &close_form($r,$url,$button_text);
778: } else {
779: $r->print($header);
780: if ($aclcount) {
781: $r->print($info);
782: }
783: &view_access_settings($r,$url,$access_controls{$file_name},$aclcount);
784: }
785: }
786:
787: sub view_access_settings {
788: my ($r,$url,$access_controls,$aclcount) = @_;
789: my ($showstart,$showend);
790: my %todisplay;
791: foreach my $key (sort(keys(%{$access_controls}))) {
792: my ($num,$scope,$end,$start) = &unpack_acc_key($key);
793: $todisplay{$scope}{$key} = $$access_controls{$key};
794: }
795: if ($aclcount) {
796: $r->print(&mt('<h4>Current access controls defined for this file:</h4>'));
797: $r->print(&Apache::loncommon::start_data_table());
798: $r->print(&Apache::loncommon::start_data_table_header_row());
799: $r->print('<th>'.&mt('Access control').'</th><th>'.&mt('Dates available').
800: '</th><th>'.&mt('Additional information').'</th>');
801: $r->print(&Apache::loncommon::end_data_table_header_row());
802: my $count = 1;
803: my $chg = 'none';
804: &build_access_summary($r,$count,$chg,%todisplay);
805: $r->print(&Apache::loncommon::end_data_table());
806: } else {
807: $r->print(&mt('No access control settings currently exist for this file.<br />' ));
808: }
809: my %anchor_fields = (
810: 'currentpath' => $env{'form.currentpath'}
811: );
812: $r->print('<br />'.&make_anchor($url, \%anchor_fields, &mt('Return to directory listing')));
813: return;
814: }
815:
816: sub build_access_summary {
817: my ($r,$count,$chg,%todisplay) = @_;
818: my ($showstart,$showend);
819: my %scope_desc = (
820: public => 'Public',
821: guest => 'Passphrase-protected',
822: domains => 'Conditional: domain-based',
823: users => 'Conditional: user-based',
824: course => 'Conditional: course-based',
825: group => 'Conditional: group-based',
826: );
827: my @allscopes = ('public','guest','domains','users','course','group');
828: foreach my $scope (@allscopes) {
829: if ((!(exists($todisplay{$scope}))) || (ref($todisplay{$scope}) ne 'HASH')) {
830: next;
831: }
832: foreach my $key (sort(keys(%{$todisplay{$scope}}))) {
833: if ($count) {
834: $r->print(&Apache::loncommon::start_data_table_row());
835: }
836: my ($num,$scope,$end,$start) = &unpack_acc_key($key);
837: my $content = $todisplay{$scope}{$key};
838: if ($chg eq 'delete') {
839: $showstart = &mt('Deleted');
840: $showend = $showstart;
841: } else {
842: $showstart = localtime($start);
843: if ($end == 0) {
844: $showend = &mt('No end date');
845: } else {
846: $showend = localtime($end);
847: }
848: }
849: $r->print('<td>'.&mt($scope_desc{$scope}));
850: if (($scope eq 'course') || ($scope eq 'group')) {
851: if ($chg ne 'delete') {
852: my $cid = $content->{'domain'}.'_'.$content->{'number'};
853: my %course_description = &Apache::lonnet::coursedescription($cid);
854: $r->print('<br />('.$course_description{'description'}.')');
855: }
856: }
857: $r->print('</td><td>'.&mt('Start: ').$showstart.
858: '<br />'.&mt('End: ').$showend.'</td><td>');
859: if ($chg ne 'delete') {
860: if ($scope eq 'guest') {
861: $r->print(&mt('Passphrase').': '.$content->{'password'});
862: } elsif ($scope eq 'course' || $scope eq 'group') {
863: $r->print('<table><tr>');
864: $r->print('<th>'.&mt('Roles').'</th><th>'.
865: &mt('Access').'</th><th>'.
866: &mt('Sections').'</th>');
867: if ($scope eq 'course') {
868: $r->print('<th>'.&mt('Groups').'</th>');
869: } else {
870: $r->print('<th>'.&mt('Teams').'</th>');
871: }
872: $r->print('</tr>');
873: foreach my $id (sort(keys(%{$content->{'roles'}}))) {
874: $r->print('<tr>');
875: foreach my $item ('role','access','section','group') {
876: $r->print('<td>');
877: if ($item eq 'role') {
878: my $ucscope = $scope;
879: $ucscope =~ s/^(\w)/uc($1)/e;
880: my $role_output;
881: foreach my $role (@{$content->{'roles'}{$id}{$item}}) {
882: if ($role eq 'all') {
883: $role_output .= $role.',';
884: } elsif ($role =~ /^cr/) {
885: $role_output .= (split('/',$role))[3].',';
886: } else {
887: $role_output .= &Apache::lonnet::plaintext($role,$ucscope).',';
888: }
889: }
890: $role_output =~ s/,$//;
891: $r->print($role_output);
892: } else {
893: $r->print(join(',',@{$content->{'roles'}{$id}{$item}}));
894: }
895: $r->print('</tr>');
896: }
897: $r->print("</table>");
898: }
899: $r->print("</tr></table>");
900: } elsif ($scope eq 'domains') {
901: $r->print(&mt('Domains: ').join(',',@{$content->{'dom'}}));
902: } elsif ($scope eq 'users') {
903: my $curr_user_list = &sort_users($content->{'users'});
904: $r->print(&mt('Users: ').$curr_user_list);
905: } else {
906: $r->print(' ');
907: }
908: } else {
909: $r->print(' ');
910: }
911: $r->print('</td>');
912: $r->print(&Apache::loncommon::end_data_table_row());
913: $count ++;
914: }
915: }
916: }
917:
918:
919: sub update_access {
920: my ($r,$url,$group,$port_path) = @_;
921: my $totalprocessed = 0;
922: my %processing;
923: my %title = (
924: 'activate' => 'New control(s) added',
925: 'delete' => 'Existing control(s) deleted',
926: 'update' => 'Existing control(s) modified',
927: );
928: my $changes;
929: foreach my $chg (sort(keys(%title))) {
930: @{$processing{$chg}} = &Apache::loncommon::get_env_multiple('form.'.$chg);
931: $totalprocessed += @{$processing{$chg}};
932: foreach my $num (@{$processing{$chg}}) {
933: my $scope = $env{'form.scope_'.$num};
934: my ($start,$end) = &get_dates_from_form($num);
935: my $newkey = $num.':'.$scope.'_'.$end.'_'.$start;
936: if ($chg eq 'delete') {
937: $$changes{$chg}{$newkey} = 1;
938: } else {
939: $$changes{$chg}{$newkey} =
940: &build_access_record($num,$scope,$start,$end,$chg);
941: }
942: }
943: }
944: my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
945: $r->print('<h3>'.&mt('Allowing others to retrieve file: [_1]',
946: $port_path.$file_name).'</h3>'."\n");
947: $file_name = &prepend_group($file_name);
948: my ($uname,$udom) = &get_name_dom();
949: my ($errors,$outcome,$deloutcome,$new_values,$translation);
950: if ($totalprocessed) {
951: ($outcome,$deloutcome,$new_values,$translation) =
952: &Apache::lonnet::modify_access_controls($file_name,$changes,$udom,
953: $uname);
954: }
955: my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
956: $uname);
957: my %access_controls =
958: &Apache::lonnet::get_access_controls($current_permissions,
959: $group,$file_name);
960: if ($totalprocessed) {
961: if ($outcome eq 'ok') {
962: my $updated_controls = $access_controls{$file_name};
963: my ($showstart,$showend);
964: $r->print(&Apache::loncommon::start_data_table());
965: $r->print(&Apache::loncommon::start_data_table_header_row());
966: $r->print('<th>'.&mt('Type of change').'</th><th>'.
967: &mt('Access control').'</th><th>'.&mt('Dates available').
968: '</th><th>'.&mt('Additional information').'</th>');
969: $r->print(&Apache::loncommon::end_data_table_header_row());
970: foreach my $chg (sort(keys(%processing))) {
971: if (@{$processing{$chg}} > 0) {
972: if ($chg eq 'delete') {
973: if (!($deloutcome eq 'ok')) {
974: $errors .='<span class="LC_error">'.
975: &mt('A problem occurred deleting access controls: [_1]',$deloutcome).
976: '</span>';
977: next;
978: }
979: }
980: my $numchgs = @{$processing{$chg}};
981: $r->print(&Apache::loncommon::start_data_table_row());
982: $r->print('<td rowspan="'.$numchgs.'">'.&mt($title{$chg}).
983: '.</td>');
984: my $count = 0;
985: my %todisplay;
986: foreach my $key (sort(keys(%{$$changes{$chg}}))) {
987: my ($num,$scope,$end,$start) = &unpack_acc_key($key);
988: my $newkey = $key;
989: if ($chg eq 'activate') {
990: $newkey =~ s/^(\d+)/$$translation{$1}/;
991: }
992: $todisplay{$scope}{$newkey} = $$updated_controls{$newkey};
993: }
994: &build_access_summary($r,$count,$chg,%todisplay);
995: }
996: }
997: $r->print(&Apache::loncommon::end_data_table());
998: } else {
999: if ((@{$processing{'activate'}} > 0) || (@{$processing{'update'}} > 0)) {
1000: $errors .= '<span class="LC_error">'.
1001: &mt('A problem occurred storing access control settings: [_1]',$outcome).
1002: '</span>';
1003: }
1004: }
1005: if ($errors) {
1006: $r->print($errors);
1007: }
1008: }
1009: my $allnew = 0;
1010: my $totalnew = 0;
1011: my $status = 'new';
1012: my ($firstitem,$lastitem);
1013: foreach my $newitem ('course','group','domains','users') {
1014: $allnew += $env{'form.new'.$newitem};
1015: }
1016: if ($allnew > 0) {
1017: my $now = time;
1018: my $then = $now + (60*60*24*180); # six months approx.
1019: &open_form($r,$url);
1020: foreach my $newitem ('course','group','domains','users') {
1021: if ($env{'form.new'.$newitem} > 0) {
1022: $r->print('<br />'.&mt('Add new <b>[_1]-based</b> access control for portfolio file: <b>[_2]</b>',$newitem,$env{'form.currentpath'}.$env{'form.selectfile'}).'<br /><br />');
1023: $firstitem = $totalnew;
1024: $lastitem = $totalnew + $env{'form.new'.$newitem};
1025: $totalnew = $lastitem;
1026: my @numbers;
1027: for (my $i=$firstitem; $i<$lastitem; $i++) {
1028: push (@numbers,$i);
1029: }
1030: &display_access_row($r,$status,$newitem,\@numbers,
1031: $access_controls{$file_name},$now,$then);
1032: }
1033: }
1034: &close_form($r,$url);
1035: } else {
1036: my %anchor_fields = (
1037: 'currentpath' => $env{'form.currentpath'},
1038: 'access' => $env{'form.selectfile'}
1039: );
1040: $r->print('<br />'.&make_anchor($url, \%anchor_fields, &mt('Display all access settings for this file')));
1041: delete $anchor_fields{'access'};
1042: $r->print(' '.&make_anchor($url,\%anchor_fields,&mt('Return to directory listing')));
1043: }
1044: return;
1045: }
1046:
1047: sub build_access_record {
1048: my ($num,$scope,$start,$end,$chg) = @_;
1049: my $record = {
1050: type => $scope,
1051: time => {
1052: start => $start,
1053: end => $end
1054: },
1055: };
1056:
1057: if ($scope eq 'guest') {
1058: $record->{'password'} = $env{'form.password'};
1059: } elsif (($scope eq 'course') || ($scope eq 'group')) {
1060: $record->{'domain'} = $env{'form.crsdom_'.$num};
1061: $record->{'number'} = $env{'form.crsnum_'.$num};
1062: my @role_ids;
1063: my @delete_role_ids =
1064: &Apache::loncommon::get_env_multiple('form.delete_role_'.$num);
1065: my @preserves =
1066: &Apache::loncommon::get_env_multiple('form.preserve_role_'.$num);
1067: if (@delete_role_ids) {
1068: foreach my $id (@preserves) {
1069: if (grep {$_ = $id} (@delete_role_ids)) {
1070: next;
1071: }
1072: push(@role_ids,$id);
1073: }
1074: } else {
1075: push(@role_ids,@preserves);
1076: }
1077:
1078: my $next_id = $env{'form.add_role_'.$num};
1079: if ($next_id) {
1080: push(@role_ids,$next_id);
1081: }
1082:
1083: foreach my $id (@role_ids) {
1084: my (@roles,@accesses,@sections,@groups);
1085: if (($id == $next_id) && ($chg eq 'update')) {
1086: @roles = split(/,/,$env{'form.role_'.$num.'_'.$next_id});
1087: @accesses = split(/,/,$env{'form.access_'.$num.'_'.$next_id});
1088: @sections = split(/,/,$env{'form.section_'.$num.'_'.$next_id});
1089: @groups = split(/,/,$env{'form.group_'.$num.'_'.$next_id});
1090: } else {
1091: @roles = &Apache::loncommon::get_env_multiple('form.role_'.$num.'_'.$id);
1092: @accesses = &Apache::loncommon::get_env_multiple('form.access_'.$num.'_'.$id);
1093: @sections = &Apache::loncommon::get_env_multiple('form.section_'.$num.'_'.$id);
1094: @groups = &Apache::loncommon::get_env_multiple('form.group_'.$num.'_'.$id);
1095: }
1096: $record->{'roles'}{$id}{'role'} = \@roles;
1097: $record->{'roles'}{$id}{'access'} = \@accesses;
1098: $record->{'roles'}{$id}{'section'} = \@sections;
1099: $record->{'roles'}{$id}{'group'} = \@groups;
1100: }
1101: } elsif ($scope eq 'domains') {
1102: my @doms = &Apache::loncommon::get_env_multiple('form.dom_'.$num);
1103: $record->{'dom'} = \@doms;
1104: } elsif ($scope eq 'users') {
1105: my $userlist = $env{'form.users_'.$num};
1106: $userlist =~ s/\s+//sg;
1107: my %userhash = map { ($_,1) } (split(/,/,$userlist));
1108: foreach my $user (keys(%userhash)) {
1109: my ($uname,$udom) = split(/:/,$user);
1110: push(@{$record->{'users'}}, {
1111: 'uname' => $uname,
1112: 'udom' => $udom
1113: });
1114: }
1115: }
1116: return $record;
1117: }
1118:
1119: sub get_dates_from_form {
1120: my ($id) = @_;
1121: my $startdate;
1122: my $enddate;
1123: $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$id);
1124: $enddate = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$id);
1125: if ( exists ($env{'form.noend_'.$id}) ) {
1126: $enddate = 0;
1127: }
1128: return ($startdate,$enddate);
1129: }
1130:
1131: sub sort_users {
1132: my ($users) = @_;
1133: my @curr_users = map {
1134: $_->{'uname'}.':'.$_->{'udom'}
1135: } (@{$users});
1136: my $curr_user_list = join(",\n",sort(@curr_users));
1137: return $curr_user_list;
1138: }
1139:
1140: sub access_setting_table {
1141: my ($r,$access_controls) = @_;
1142: my ($public,$publictext);
1143: $publictext = &mt('Off');
1144: my ($guest,$guesttext);
1145: $guesttext = &mt('Off');
1146: my @courses = ();
1147: my @groups = ();
1148: my @domains = ();
1149: my @users = ();
1150: my $now = time;
1151: my $then = $now + (60*60*24*180); # six months approx.
1152: my ($num,$scope,$publicnum,$guestnum);
1153: my (%acl_count,%end,%start);
1154: foreach my $key (sort(keys(%{$access_controls}))) {
1155: ($num,$scope,$end{$key},$start{$key}) = &unpack_acc_key($key);
1156: if ($scope eq 'public') {
1157: $public = $key;
1158: $publicnum = $num;
1159: $publictext = &acl_status($start{$key},$end{$key},$now);
1160: } elsif ($scope eq 'guest') {
1161: $guest=$key;
1162: $guestnum = $num;
1163: $guesttext = &acl_status($start{$key},$end{$key},$now);
1164: } elsif ($scope eq 'course') {
1165: push(@courses,$key);
1166: } elsif ($scope eq 'group') {
1167: push(@groups,$key);
1168: } elsif ($scope eq 'domains') {
1169: push(@domains,$key);
1170: } elsif ($scope eq 'users') {
1171: push(@users,$key);
1172: }
1173: $acl_count{$scope} ++;
1174: }
1175: $r->print('<table border="0"><tr><td valign="top">');
1176: $r->print('<h3>'.&mt('Public access:').' '.$publictext.'</h3>');
1177: $r->print(&Apache::loncommon::start_data_table());
1178: $r->print(&Apache::loncommon::start_data_table_header_row());
1179: $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').'</th>');
1180: $r->print(&Apache::loncommon::end_data_table_header_row());
1181: $r->print(&Apache::loncommon::start_data_table_row());
1182: if ($public) {
1183: $r->print('<td>'.&actionbox('old',$publicnum,'public').'</td><td>'.
1184: &dateboxes($publicnum,$start{$public},$end{$public}).'</td>');
1185: } else {
1186: $r->print('<td>'.&actionbox('new','0','public').'</td><td>'.
1187: &dateboxes('0',$now,$then).'</td>');
1188: }
1189: $r->print(&Apache::loncommon::end_data_table_row());
1190: $r->print(&Apache::loncommon::end_data_table());
1191: $r->print('</td><td width="40"> </td><td valign="top">');
1192: $r->print('<h3>'.&mt('Passphrase-protected access:').' '.$guesttext.'</h3>');
1193: $r->print(&Apache::loncommon::start_data_table());
1194: $r->print(&Apache::loncommon::start_data_table_header_row());
1195: $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').
1196: '</th><th>'. &mt('Passphrase').'</th>');
1197: $r->print(&Apache::loncommon::end_data_table_header_row());
1198: $r->print(&Apache::loncommon::start_data_table_row());
1199: my $passwd;
1200: if ($guest) {
1201: $passwd = $$access_controls{$guest}{'password'};
1202: $r->print('<td>'.&actionbox('old',$guestnum,'guest').'</td><td>'.
1203: &dateboxes($guestnum,$start{$guest},$end{$guest}).'</td>');
1204: } else {
1205: $r->print('<td>'.&actionbox('new','1','guest').'</td><td>'.
1206: &dateboxes('1',$now,$then).'</td>');
1207: }
1208: $r->print('<td><input type="text" size="15" name="password" value="'.
1209: $passwd.'" /></td>');
1210: $r->print(&Apache::loncommon::end_data_table_row());
1211: $r->print(&Apache::loncommon::end_data_table());
1212: $r->print('</td></tr><tr><td colspan="3"> </td></tr><tr><td valign="top">');
1213: &access_element($r,'domains',\%acl_count,\@domains,$access_controls,$now,$then);
1214: $r->print('</td><td> </td><td valign="top">');
1215: &access_element($r,'users',\%acl_count,\@users,$access_controls,$now,$then);
1216: $r->print('</td></tr><tr><td colspan="3"></td></tr><tr>');
1217: if (@courses > 0 || @groups > 0) {
1218: $r->print('<td colspan="3" valign="top">');
1219: } else {
1220: $r->print('<td valign="top">');
1221: }
1222: &access_element($r,'course',\%acl_count,\@courses,$access_controls,$now,$then);
1223: $r->print('</td>');
1224: if (@courses > 0 || @groups > 0) {
1225: $r->print('</tr><tr><td colspan="3"> </td></tr><tr><td colspan="3" valign="top">');
1226: } else {
1227: $r->print('<td> </td><td valign="top">');
1228: }
1229: &access_element($r,'group',\%acl_count,\@groups,$access_controls,$now,$then);
1230: $r->print('</td></tr></table>');
1231: }
1232:
1233: sub acl_status {
1234: my ($start,$end,$now) = @_;
1235: if ($start > $now) {
1236: return &mt('Inactive');
1237: }
1238: if ($end && $end<$now) {
1239: return &mt('Inactive');
1240: }
1241: return &mt('Active');
1242: }
1243:
1244: sub access_element {
1245: my ($r,$type,$acl_count,$items,$access_controls,$now,$then) = @_;
1246: my $title = $type;
1247: $title =~ s/s$//;
1248: $title =~ s/^(\w)/uc($1)/e;
1249: $r->print('<h3>'.&mt('[_1]-based conditional access: ',$title));
1250: if ($$acl_count{$type}) {
1251: $r->print($$acl_count{$type}.' ');
1252: if ($$acl_count{$type} > 1) {
1253: $r->print(&mt('conditions'));
1254: } else {
1255: $r->print(&mt('condition'));
1256: }
1257: } else {
1258: $r->print(&mt('Off'));
1259: }
1260: $r->print('</h3>');
1261: &display_access_row($r,'old',$type,$items,$access_controls,$now,$then);
1262: return;
1263: }
1264:
1265: sub display_access_row {
1266: my ($r,$status,$type,$items,$access_controls,$now,$then) = @_;
1267: if (@{$items} > 0) {
1268: my @all_doms;
1269: my $colspan = 3;
1270: my $uctype = $type;
1271: $uctype =~ s/^(\w)/uc($1)/e;
1272: $r->print(&Apache::loncommon::start_data_table());
1273: $r->print(&Apache::loncommon::start_data_table_header_row());
1274: $r->print('<th>'.&mt('Action?').'</th><th>'.&mt($uctype).'</th><th>'.
1275: &mt('Dates available').'</th>');
1276: if (($type eq 'course') || ($type eq 'group')) {
1277: $r->print('<th>'.&mt('Allowed [_1] member affiliations',$type).
1278: '</th>');
1279: $colspan ++;
1280: } elsif ($type eq 'domains') {
1281: @all_doms = &Apache::loncommon::get_domains();
1282: }
1283: $r->print(&Apache::loncommon::end_data_table_header_row());
1284: foreach my $key (@{$items}) {
1285: $r->print(&Apache::loncommon::start_data_table_row());
1286: if (($type eq 'course') || ($type eq 'group')) {
1287: &course_row($r,$status,$type,$key,$access_controls,$now,$then);
1288: } elsif ($type eq 'domains') {
1289: &domains_row($r,$status,$key,\@all_doms,$access_controls,$now,
1290: $then);
1291: } elsif ($type eq 'users') {
1292: &users_row($r,$status,$key,$access_controls,$now,$then);
1293: }
1294: $r->print(&Apache::loncommon::end_data_table_row());
1295: }
1296: if ($status eq 'old') {
1297: $r->print(&Apache::loncommon::start_data_table_row());
1298: $r->print('<td colspan="',$colspan.'">'.&additional_item($type).
1299: '</td>');
1300: $r->print(&Apache::loncommon::end_data_table_row());
1301: }
1302: $r->print(&Apache::loncommon::end_data_table());
1303: } else {
1304: $r->print(&mt('No [_1]-based conditions defined.<br />',$type).
1305: &additional_item($type));
1306: }
1307: return;
1308: }
1309:
1310: sub course_js {
1311: return qq|
1312: <script type="text/javascript">
1313: function setRoleOptions(caller,num,cdom,cnum,type) {
1314: addIndexnum = getCallerIndex(caller);
1315: updateIndexnum = getIndex('update',num);
1316: if (caller.checked) {
1317: document.portform.elements[updateIndexnum].checked = true;
1318: var url = '/adm/portfolio?action=rolepicker&setroles='+addIndexnum+'&cnum='+cnum+'&cdom='+cdom+'&type='+type;
1319: var title = 'Roles_Chooser';
1320: var options = 'scrollbars=1,resizable=1,menubar=0';
1321: options += ',width=700,height=600';
1322: rolebrowser = open(url,title,options,'1');
1323: rolebrowser.focus();
1324: } else {
1325: for (var j=0;j<5;j++) {
1326: document.portform.elements[addIndexnum+j].value = '';
1327: }
1328: }
1329: }
1330:
1331: function getCallerIndex(caller) {
1332: for (var i=0;i<document.portform.elements.length;i++) {
1333: if (document.portform.elements[i] == caller) {
1334: return i;
1335: }
1336: }
1337: return -1;
1338: }
1339:
1340: function getIndex(name,value) {
1341: for (var i=0;i<document.portform.elements.length;i++) {
1342: if (document.portform.elements[i].name == name && document.portform.elements[i].value == value) {
1343: return i;
1344: }
1345: }
1346: return -1;
1347: }
1348:
1349: </script>
1350: |;
1351: }
1352:
1353: sub course_row {
1354: my ($r,$status,$type,$item,$access_controls,$now,$then) = @_;
1355: my $content;
1356: my $defdom = $env{'user.domain'};
1357: if ($status eq 'old') {
1358: $content = $$access_controls{$item};
1359: $defdom = $content->{'domain'};
1360: }
1361: my $js = &Apache::loncommon::coursebrowser_javascript($defdom)
1362: .&course_js();
1363: my $crsgrptext = 'Groups';
1364: if ($type eq 'group') {
1365: $crsgrptext = 'Teams';
1366: }
1367: my $uctype = $type;
1368: $uctype =~ s/^(\w)/uc($1)/e;
1369: my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
1370: $type);
1371: $r->print('<td>'.$js.&actionbox($status,$num,$scope).'</td>');
1372: if ($status eq 'old') {
1373: my $cid = $content->{'domain'}.'_'.$content->{'number'};
1374: my %course_description = &Apache::lonnet::coursedescription($cid);
1375: $r->print('<td><input type="hidden" name="crsdom_'.$num.'" value="'.$content->{'domain'}.'" /><input type="hidden" name="crsnum_'.$num.'" value="'.$content->{'number'}.'" />'.$course_description{'description'}.'</td>');
1376: } elsif ($status eq 'new') {
1377: $r->print('<td>'.&Apache::loncommon::selectcourse_link('portform','crsnum_'.$num,'crsdom_'.$num,'description_'.$num,undef,undef,$uctype).' <input type="text" name="description_'.$num.'" size="30" /><input type="hidden" name="crsdom_'.$num.'" /><input type="hidden" name="crsnum_'.$num.'" /></td>');
1378: }
1379: $r->print('<td>'.&dateboxes($num,$start,$end).'</td>');
1380: $r->print('<td><table><tr>');
1381: $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Roles').'</th><th>'.
1382: &mt('Access').'</th><th>'.&mt('Sections').'</th><th>'.
1383: &mt($crsgrptext).'</th></tr>');
1384: if ($status eq 'old') {
1385: my $max_id = 0;
1386: foreach my $role_id (sort(keys(%{$content->{'roles'}}))) {
1387: if ($role_id > $max_id) {
1388: $max_id = $role_id;
1389: }
1390: $max_id ++;
1391: my $role_selects = &role_selectors($num,$role_id,$status,$type,$content,'display');
1392: $r->print('<tr><td><span style="white-space: nowrap"><label><input type="checkbox" name="delete_role_'.$num.'" value="'.$role_id.'" />'.&mt('Delete').'</label></span><br /><input type="hidden" name="preserve_role_'.$num.'" value="'.$role_id.'" /></td>'.$role_selects.'</tr>');
1393: }
1394: $r->print('</table><br />'.&mt('Add a roles-based condition').' <input type="checkbox" name ="add_role_'.$num.'" onClick="javascript:setRoleOptions(this,'."'$num','$content->{'domain'}','$content->{'number'}','$uctype'".')" value="'.$max_id.'" /><input type="hidden" name="role_'.$num.'_'.$max_id.'" /><input type="hidden" name="access_'.$num.'_'.$max_id.'" /><input type="hidden" name="section_'.$num.'_'.$max_id.'" /><input type="hidden" name="group_'.$num.'_'.$max_id.'" /></td>');
1395: } elsif ($status eq 'new') {
1396: my $role_id = 1;
1397: my $role_selects = &role_selectors($num,$role_id,$status,$type,undef,'display');
1398: $r->print('<tr><td><input type="checkbox" name="add_role_'.$num.'" value="'.$role_id.'" checked="checked" />'.&mt('Add').'<input type="hidden" name="grplist_'.$num.'_'.$role_id.'" /></td>'.$role_selects);
1399: $r->print('</tr></table></td>');
1400: }
1401: return;
1402: }
1403:
1404: sub domains_row {
1405: my ($r,$status,$item,$all_doms,$access_controls,$now,$then) = @_;
1406: my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
1407: 'domains');
1408: my $dom_select = '<select name="dom_'.$num.'" size="4" multiple="true">'.
1409: ' <option value="">'.&mt('Please select').'</option>';
1410: if ($status eq 'old') {
1411: my $content = $$access_controls{$item};
1412: foreach my $dom (@{$all_doms}) {
1413: if ((@{$content->{'dom'}} > 0)
1414: && (grep(/^\Q$dom\E$/,@{$content->{'dom'}}))) {
1415: $dom_select .= '<option value="'.$dom.'" selected>'.
1416: $dom.'</option>';
1417: } else {
1418: $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
1419: }
1420: }
1421: } else {
1422: foreach my $dom (@{$all_doms}) {
1423: $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
1424: }
1425: }
1426: $dom_select .= '</select>';
1427: $r->print('<td>'.&actionbox($status,$num,$scope).'</td><td>'.$dom_select.
1428: '</td><td>'.&dateboxes($num,$start,$end).'</td>');
1429: }
1430:
1431: sub users_row {
1432: my ($r,$status,$item,$access_controls,$now,$then) = @_;
1433: my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
1434: 'users');
1435: my $curr_user_list;
1436: if ($status eq 'old') {
1437: my $content = $$access_controls{$item};
1438: $curr_user_list = &sort_users($content->{'users'});
1439: }
1440: $r->print('<td>'.&actionbox($status,$num,$scope).'</td><td>'.&mt("Format for users' username:domain information:").'<br /><tt>sparty:msu,illini:uiuc ... etc.</tt><br /><textarea name="users_'.$num.'" cols="30" rows="5">'.$curr_user_list.'</textarea></td><td>'.&dateboxes($num,$start,$end).'</td>');
1441: }
1442:
1443: sub additional_item {
1444: my ($type) = @_;
1445: my $output = &mt('Add new [_1] condition(s)?',$type).' '.&mt('Number to add: ').'<input type="text" name="new'.$type.'" size="3" value="0" />';
1446: return $output;
1447: }
1448:
1449: sub actionbox {
1450: my ($status,$num,$scope) = @_;
1451: my $output = '<span style="white-space: nowrap"><label>';
1452: if ($status eq 'new') {
1453: $output .= '<input type="checkbox" name="activate" value="'.$num.'" />'.
1454: &mt('Activate');
1455: } else {
1456: $output .= '<input type="checkbox" name="delete" value="'.$num.
1457: '" />'.&mt('Delete').'</label></span><br /><span style="white-space: nowrap">'.
1458: '<label><input type="checkbox" name="update" value="'.
1459: $num.'" />'.&mt('Update');
1460: }
1461: $output .= '</label></span><input type="hidden" name="scope_'.$num. '" value="'.$scope.'" />';
1462: return $output;
1463: }
1464:
1465: sub dateboxes {
1466: my ($num,$start,$end) = @_;
1467: my $noend;
1468: if ($end == 0) {
1469: $noend = 'checked="checked"';
1470: }
1471: my $startdate = &Apache::lonhtmlcommon::date_setter('portform',
1472: 'startdate_'.$num,$start,undef,undef,undef,1,undef,
1473: undef,undef,1);
1474: my $enddate = &Apache::lonhtmlcommon::date_setter('portform',
1475: 'enddate_'.$num,$end,undef,undef,undef,1,undef,
1476: undef,undef,1). ' <span style="white-space: nowrap"><label>'.
1477: '<input type="checkbox" name="noend_'.
1478: $num.'" '.$noend.' />'.&mt('No end date').
1479: '</label></span>';
1480:
1481: my $output = &mt('Start: ').$startdate.'<br />'.&mt('End: ').$enddate;
1482: return $output;
1483: }
1484:
1485: sub unpack_acc_key {
1486: my ($acc_key) = @_;
1487: my ($num,$scope,$end,$start) = ($acc_key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
1488: return ($num,$scope,$end,$start);
1489: }
1490:
1491: sub set_identifiers {
1492: my ($status,$item,$now,$then,$scope) = @_;
1493: if ($status eq 'old') {
1494: return(&unpack_acc_key($item));
1495: } else {
1496: return($item,$scope,$then,$now);
1497: }
1498: }
1499:
1500: sub role_selectors {
1501: my ($num,$role_id,$status,$type,$content,$caller) = @_;
1502: my ($output,$cdom,$cnum,$longid);
1503: if ($caller eq 'display') {
1504: $longid = '_'.$num.'_'.$role_id;
1505: if ($status eq 'new') {
1506: foreach my $item ('role','access','section','group') {
1507: $output .= '<td><select name="'.$item.$longid.'">'.
1508: '<option value="">'.&mt('Pick [_1] first',$type).
1509: '</option></select></td>';
1510: }
1511: return $output;
1512: } else {
1513: $cdom = $$content{'domain'};
1514: $cnum = $$content{'number'};
1515: }
1516: } elsif ($caller eq 'rolepicker') {
1517: $cdom = $env{'form.cdom'};
1518: $cnum = $env{'form.cnum'};
1519: }
1520: my $uctype = $type;
1521: $uctype =~ s/^(\w)/uc($1)/e;
1522: my ($sections,$groups,$allroles,$rolehash,$accesshash) =
1523: &Apache::loncommon::get_secgrprole_info($cdom,$cnum,1,$uctype);
1524: if (!@{$sections}) {
1525: @{$sections} = ('none');
1526: } else {
1527: unshift(@{$sections},('all','none'));
1528: }
1529: if (!@{$groups}) {
1530: @{$groups} = ('none');
1531: } else {
1532: unshift(@{$groups},('all','none'));
1533: }
1534: my @allacesses = sort(keys(%{$accesshash}));
1535: my (%sectionhash,%grouphash);
1536: foreach my $sec (@{$sections}) {
1537: $sectionhash{$sec} = $sec;
1538: }
1539: foreach my $grp (@{$groups}) {
1540: $grouphash{$grp} = $grp;
1541: }
1542: my %lookup = (
1543: 'role' => $rolehash,
1544: 'access' => $accesshash,
1545: 'section' => \%sectionhash,
1546: 'group' => \%grouphash,
1547: );
1548: my @allaccesses = sort(keys(%{$accesshash}));
1549: my %allitems = (
1550: 'role' => $allroles,
1551: 'access' => \@allaccesses,
1552: 'section' => $sections,
1553: 'group' => $groups,
1554: );
1555: foreach my $item ('role','access','section','group') {
1556: $output .= '<td><select name="'.$item.$longid.'" multiple="true" size="4">'."\n";
1557: foreach my $entry (@{$allitems{$item}}) {
1558: if ($caller eq 'display') {
1559: if ((@{$$content{'roles'}{$role_id}{$item}} > 0) &&
1560: (grep(/^\Q$entry\E$/,@{$$content{'roles'}{$role_id}{$item}}))) {
1561: $output .= ' <option value="'.$entry.'" selected>'.
1562: $lookup{$item}{$entry}.'</option>';
1563: next;
1564: }
1565: }
1566: $output .= ' <option value="'.$entry.'">'.
1567: $lookup{$item}{$entry}.'</option>';
1568: }
1569: $output .= '</select>';
1570: }
1571: $output .= '</td>';
1572: return $output;
1573: }
1574:
1575: sub role_options_window {
1576: my ($r) = @_;
1577: my $cdom = $env{'form.cdom'};
1578: my $cnum = $env{'form.cnum'};
1579: my $type = $env{'form.type'};
1580: my $addindex = $env{'form.setroles'};
1581: my $grouptitle = 'Groups';
1582: if ($type eq 'Group') {
1583: $grouptitle = 'Teams';
1584: }
1585: my $role_selects = &role_selectors(1,1,'new',$type,undef,'rolepicker');
1586: $r->print(<<"END_SCRIPT");
1587: <script type="text/javascript">
1588: function setRoles() {
1589: var addidx = $addindex+1;
1590: for (var i=0; i<4; i++) {
1591: var copylist = '';
1592: for (var j=0; j<document.rolepicker.elements[i].length; j++) {
1593: if (document.rolepicker.elements[i].options[j].selected) {
1594: copylist = copylist + document.rolepicker.elements[i].options[j].value + ',';
1595: }
1596: }
1597: copylist = copylist.substr(0,copylist.length-1);
1598: opener.document.portform.elements[addidx+i].value = copylist;
1599: }
1600: self.close();
1601: }
1602: </script>
1603: END_SCRIPT
1604: $r->print(&mt('Select roles, course status, section(s) and group(s) for users who will be able to access the portfolio file.'));
1605: $r->print('<form name="rolepicker" action="/adm/portfolio" method="post"><table><tr><th>'.&mt('Roles').'</th><th>'.&mt('[_1] status',$type).'</th><th>'.&mt('Sections').'</th><th>'.&mt($grouptitle).'</th></tr><tr>'.$role_selects.'</tr></table><br /><input type="button" name="rolepickbutton" value="Save selections" onclick="setRoles()" />');
1606: return;
1607: }
1608:
1609: sub select_files {
1610: my ($r) = @_;
1611: if ($env{'form.continue'} eq 'true') {
1612: # here we update the selections for the currentpath
1613: # eventually, have to handle removing those not checked, but . . .
1614: my @items=&Apache::loncommon::get_env_multiple('form.checkfile');
1615: if (scalar(@items)){
1616: &Apache::lonnet::save_selected_files($env{'user.name'}, $env{'form.currentpath'}, @items);
1617: }
1618: } else {
1619: #empty the file for a fresh start
1620: &Apache::lonnet::clear_selected_files($env{'user.name'});
1621: }
1622: my @files = &Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
1623: my $java_files = join ",", @files;
1624: if ($java_files) {
1625: $java_files.=',';
1626: }
1627: my $javascript =(<<ENDSMP);
1628: <script type="text/javascript">
1629: function finishSelect() {
1630: ENDSMP
1631: $javascript .= 'fileList = "'.$java_files.'";';
1632: $javascript .= (<<ENDSMP);
1633: for (i=0;i<document.forms.checkselect.length;i++) {
1634: if (document.forms.checkselect[i].checked){
1635: fileList = fileList + document.forms.checkselect.currentpath.value + document.forms.checkselect[i].value + "," ;
1636: }
1637: }
1638: opener.document.forms.lonhomework.$env{'form.fieldname'}.value=fileList;
1639: self.close();
1640: }
1641: </script>
1642: ENDSMP
1643: $r->print($javascript);
1644: $r->print("<h1>Select portfolio files</h1>
1645: Check as many as you wish in response to the problem.<br />");
1646: my @otherfiles=&Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
1647: if (@otherfiles) {
1648: $r->print("<strong>Files selected from other directories:</strong><br />");
1649: foreach my $file (@otherfiles) {
1650: $r->print($file."<br />");
1651: }
1652: }
1653: }
1654:
1655: sub upload {
1656: my ($r,$url,$group)=@_;
1657: my $fname=$env{'form.uploaddoc.filename'};
1658: my $filesize = (length($env{'form.uploaddoc'})) / 1000; #express in k (1024?)
1659: my $disk_quota = 20000; # expressed in k
1660: if (defined($group)) {
1661: my $grp_quota = &get_group_quota($group); # quota expressed in k
1662: if ($grp_quota ne '') {
1663: $disk_quota = $grp_quota;
1664: } else {
1665: $disk_quota = 0;
1666: }
1667: }
1668: $fname=&Apache::lonnet::clean_filename($fname);
1669:
1670: my $portfolio_root=&get_portfolio_root();
1671: my ($uname,$udom) = &get_name_dom();
1672: my $port_path = &get_port_path();
1673: # Fixme --- Move the checking for existing file to LOND error return
1674: my @dir_list=&get_dir_list($portfolio_root);
1675: my $found_file = 0;
1676: my $locked_file = 0;
1677: foreach my $line (@dir_list) {
1678: my ($file_name)=split(/\&/,$line,2);
1679: if ($file_name eq $fname){
1680: $file_name = $env{'form.currentpath'}.$file_name;
1681: $file_name = &prepend_group($file_name);
1682: $found_file = 1;
1683: if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
1684: $locked_file = 1;
1685: }
1686: }
1687: }
1688: my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,$portfolio_root);
1689: if (($current_disk_usage + $filesize) > $disk_quota){
1690: $r->print('<span class="LC_error">Unable to upload <strong>'.$fname.' (size = '.$filesize.' kilobytes)</strong>. Disk quota will be exceeded.</span>'.
1691: '<br />Disk quota is '.$disk_quota.' kilobytes. Your current disk usage is '.$current_disk_usage.' kilobytes.');
1692: $r->print(&done('Back',$url));
1693: }
1694: elsif ($found_file){
1695: if ($locked_file){
1696: $r->print('<span class="LC_error">'.'Unable to upload <strong>'.$fname.'</strong>, a <strong>locked</strong> file by that name was found in <strong>'.$port_path.$env{'form.currentpath'}.'</strong></span>'.
1697: '<br />You will be able to rename or delete existing '.$fname.' after a grade has been assigned.');
1698: $r->print(&done('Back',$url));
1699: } else {
1700: $r->print('<span class="LC_error">'.'Unable to upload <strong>'.$fname.'</strong>, a file by that name was found in <strong>'.$port_path.$env{'form.currentpath'}.'</strong></span>'.
1701: '<br />To upload, rename or delete existing '.$fname.' in '.$port_path.$env{'form.currentpath'});
1702: $r->print(&done('Back',$url));
1703: }
1704: } else {
1705: my $result=&Apache::lonnet::userfileupload('uploaddoc','',
1706: $port_path.$env{'form.currentpath'});
1707: if ($result !~ m|^/uploaded/|) {
1708: $r->print('<span class="LC_error">'.'An errror occured ('.$result.
1709: ') while trying to upload '.&display_file().'</span><br />');
1710: $r->print(&done('Back',$url));
1711: } else {
1712: $r->print(&done(undef,$url));
1713: }
1714: }
1715: }
1716:
1717: sub lock_info {
1718: my ($r,$url,$group) = @_;
1719: my ($uname,$udom) = &get_name_dom();
1720: my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
1721: $uname);
1722: my $file_name = $env{'form.lockinfo'};
1723: $file_name = &prepend_group($file_name);
1724: if (defined($file_name) && defined($$current_permissions{$file_name})) {
1725: foreach my $array_item (@{$$current_permissions{$file_name}}) {
1726: next if (ref($array_item) ne 'ARRAY');
1727:
1728: my $filetext;
1729: if (defined($group)) {
1730: $filetext = '<strong>'.$env{'form.lockinfo'}.
1731: '</strong> (group: '.$group.')';
1732: } else {
1733: $filetext = '<strong>'.$file_name.'</strong>';
1734: }
1735:
1736: my $title ='<strong>'.&Apache::lonnet::gettitle($$array_item[0]).
1737: '</strong><br />';
1738: if ($$array_item[-1] eq 'graded') {
1739: $r->print(&mt('[_1] was submitted in response to problem: [_2]',
1740: $filetext,$title));
1741: } elsif ($$array_item[-1] eq 'handback') {
1742: $r->print(&mt('[_1] was handed back in response to problem: [_2]',
1743: $filetext,$title));
1744: } else {
1745: # submission style lock
1746: $r->print(&mt('[_1] was submitted in response to problem: [_2]',
1747: $filetext,$title));
1748: }
1749: my %course_description =
1750: &Apache::lonnet::coursedescription($$array_item[1]);
1751: if ( $course_description{'description'} ne '') {
1752: $r->print(&mt('In the course: <strong>[_1]</strong><br />',
1753: $course_description{'description'}));
1754: }
1755: }
1756: }
1757: $r->print(&done('Back',$url));
1758: return 'ok';
1759: }
1760:
1761: sub createdir {
1762: my ($r,$url)=@_;
1763: my $newdir=&Apache::lonnet::clean_filename($env{'form.newdir'});
1764: if ($newdir eq '') {
1765: $r->print('<span class="LC_error">'.
1766: &mt("Error: no directory name was provided.").
1767: '</span><br />');
1768: $r->print(&done(undef,$url));
1769: return;
1770: }
1771: my $portfolio_root = &get_portfolio_root();
1772: my @dir_list=&get_dir_list($portfolio_root);
1773: my $found_file = 0;
1774: foreach my $line (@dir_list) {
1775: my ($filename)=split(/\&/,$line,2);
1776: if ($filename eq $newdir){
1777: $found_file = 1;
1778: }
1779: }
1780: if ($found_file){
1781: $r->print('<span class="LC_error">'.'Unable to create a directory named <strong>'.$newdir.
1782: ' </strong>a file or directory by that name already exists.</span><br />');
1783: } else {
1784: my ($uname,$udom) = &get_name_dom();
1785: my $port_path = &get_port_path();
1786: my $result=&Apache::lonnet::mkdiruserfile($uname,$udom,
1787: $port_path.$env{'form.currentpath'}.$newdir);
1788: if ($result ne 'ok') {
1789: $r->print('<span class="LC_error">'.'An errror occured ('.$result.
1790: ') while trying to create a new directory '.&display_file().'</span><br />');
1791: }
1792: }
1793: if ($newdir ne $env{'form.newdir'}) {
1794: $r->print("The new directory name was changed from:<br /><strong>".$env{'form.newdir'}."</strong> to <strong>$newdir </strong>");
1795: }
1796: $r->print(&done(undef,$url));
1797: }
1798:
1799: sub get_portfolio_root {
1800: my ($udom,$uname,$group) = @_;
1801: if (!(defined($udom)) || !(defined($uname))) {
1802: ($uname,$udom) = &get_name_dom();
1803: }
1804: my $path = '/userfiles/portfolio';
1805: if (!defined($group)) {
1806: if (defined($env{'form.group'})) {
1807: $group = $env{'form.group'};
1808: }
1809: }
1810: if (defined($group)) {
1811: $path = '/userfiles/groups/'.$group.'/portfolio';
1812: }
1813: return (&Apache::loncommon::propath($udom,$uname).$path);
1814: }
1815:
1816: sub get_group_quota {
1817: my ($group) = @_;
1818: my $group_quota;
1819: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1820: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1821: my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
1822: if (%curr_groups) {
1823: my %group_info = &Apache::longroup::get_group_settings(
1824: $curr_groups{$group});
1825: $group_quota = $group_info{'quota'}; #expressed in Mb
1826: if ($group_quota) {
1827: $group_quota = 1000 * $group_quota; #expressed in k
1828: }
1829: }
1830: return $group_quota;
1831: }
1832:
1833: sub get_dir_list {
1834: my ($portfolio_root) = @_;
1835: my ($uname,$udom) = &get_name_dom();
1836: return &Apache::lonnet::dirlist($env{'form.currentpath'},
1837: $udom,$uname,$portfolio_root);
1838: }
1839:
1840: sub get_name_dom {
1841: my ($uname,$udom);
1842: if (defined($env{'form.group'})) {
1843: $udom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1844: $uname = $env{'course.'.$env{'request.course.id'}.'.num'};
1845: } else {
1846: $udom = $env{'user.domain'};
1847: $uname = $env{'user.name'};
1848: }
1849: return ($uname,$udom);
1850: }
1851:
1852: sub prepend_group {
1853: my ($filename) = @_;
1854: if (defined($env{'form.group'})) {
1855: $filename = $env{'form.group'}.$filename;
1856: }
1857: return $filename;
1858: }
1859:
1860: sub get_namespace {
1861: my $namespace = 'portfolio';
1862: if (defined($env{'form.group'})) {
1863: my ($uname,$udom) = &get_name_dom();
1864: $namespace .= '_'.$udom.'_'.$uname.'_'.$env{'form.group'};
1865: }
1866: return $namespace;
1867: }
1868:
1869: sub get_port_path {
1870: my $port_path;
1871: if (defined($env{'form.group'})) {
1872: $port_path = "groups/$env{'form.group'}/portfolio";
1873: } else {
1874: $port_path = 'portfolio';
1875: }
1876: return $port_path;
1877: }
1878:
1879: sub missing_priv {
1880: my ($r,$url,$priv) = @_;
1881: my $longtext = {
1882: upload => 'upload files',
1883: delete => 'delete files',
1884: rename => 'rename files',
1885: setacl => 'set access controls for files',
1886: };
1887: my $escpath = &HTML::Entities::encode($env{'form.currentpath'},'&<>"');
1888: my $rtnlink = '<a href="'.$url;
1889: if ($url =~ /\?/) {
1890: $rtnlink .= '&';
1891: } else {
1892: $rtnlink .= '?';
1893: }
1894: $rtnlink .= 'currentpath='.$escpath;
1895: $r->print(&mt('<h3>Action disallowed</h3>'));
1896: $r->print(&mt('You do not have sufficient privileges to [_1] ',
1897: $longtext->{$priv}));
1898: if (defined($env{'form.group'})) {
1899: $r->print(&mt("in the group's file repository."));
1900: $rtnlink .= &group_args()
1901: } else {
1902: $r->print(&mt('in this portfolio.'));
1903: }
1904: $rtnlink .= '">'.&mt('Return to directory listing page').'</a>';
1905: $r->print('<br />'.$rtnlink);
1906: $r->print(&Apache::loncommon::end_page());
1907: return;
1908: }
1909:
1910: sub coursegrp_portfolio_header {
1911: my ($cdom,$cnum,$grp_desc)=@_;
1912: my $gpterm = &Apache::loncommon::group_term();
1913: my $ucgpterm = $gpterm;
1914: $ucgpterm =~ s/^(\w)/uc($1)/e;
1915: if ($env{'form.ref'}) {
1916: &Apache::lonhtmlcommon::add_breadcrumb
1917: ({href=>"/adm/coursegroups",
1918: text=>"Groups",
1919: title=>"Course Groups"});
1920: }
1921: &Apache::lonhtmlcommon::add_breadcrumb
1922: ({href=>"/adm/$cdom/$cnum/$env{'form.group'}/smppg?ref=$env{'form.ref'}",
1923: text=>"$ucgpterm: $grp_desc",
1924: title=>"Go to group's home page"},
1925: {href=>"/adm/coursegrp_portfolio?".&group_args(),
1926: text=>"Group Portfolio",
1927: title=>"Display group portfolio"});
1928: my $output = &Apache::lonhtmlcommon::breadcrumbs(
1929: &mt('[_1] portfolio files - [_2]',$gpterm,$grp_desc));
1930: return $output;
1931: }
1932:
1933:
1934: sub handler {
1935: # this handles file management
1936: my $r = shift;
1937: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1938: ['selectfile','currentpath','meta','lockinfo','currentfile','action',
1939: 'fieldname','mode','rename','continue','group','access','setnum',
1940: 'cnum','cdom','type','setroles','showversions','ref']);
1941: my ($uname,$udom,$portfolio_root,$url,$caller,$title,$group,$grp_desc);
1942: if ($r->uri =~ m|^(/adm/)([^/]+)|) {
1943: $url = $1.$2;
1944: $caller = $2;
1945: }
1946: my ($can_modify,$can_delete,$can_upload,$can_setacl);
1947: if ($caller eq 'coursegrp_portfolio') {
1948: # Needs to be in a course
1949: if (! ($env{'request.course.fn'})) {
1950: # Not in a course
1951: $env{'user.error.msg'}=
1952: "/adm/coursegrp_portfolio:rgf:0:0:Cannot view group portfolio";
1953: return HTTP_NOT_ACCEPTABLE;
1954: }
1955: my $earlyout = 0;
1956: my $view_permission =
1957: &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
1958: $env{'form.group'} =~ s/\W//g;
1959: $group = $env{'form.group'};
1960: if ($group) {
1961: ($uname,$udom) = &get_name_dom();
1962: my %curr_groups = &Apache::longroup::coursegroups($udom,$uname,
1963: $group);
1964: if (%curr_groups) {
1965: my %grp_content = &Apache::longroup::get_group_settings(
1966: $curr_groups{$group});
1967: $grp_desc = &unescape($grp_content{'description'});
1968: if (($view_permission) || (&Apache::lonnet::allowed('rgf',
1969: $env{'request.course.id'}.'/'.$group))) {
1970: $portfolio_root = &get_portfolio_root();
1971: } else {
1972: $r->print('You do not have the privileges required to access the shared files space for this group.');
1973: $earlyout = 1;
1974: }
1975: } else {
1976: $r->print('Not a valid group for this course');
1977: $earlyout = 1;
1978: }
1979: $title = &mt('Group files').' for '.$group;
1980: } else {
1981: $r->print('Invalid group');
1982: $earlyout = 1;
1983: }
1984: if ($earlyout) { return OK; }
1985: if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
1986: $can_modify = 1;
1987: $can_delete = 1;
1988: $can_upload = 1;
1989: $can_setacl = 1;
1990: } else {
1991: if (&Apache::lonnet::allowed('agf',$env{'request.course.id'}.'/'.$group)) {
1992: $can_setacl = 1;
1993: }
1994: if (&Apache::lonnet::allowed('ugf',$env{'request.course.id'}.'/'.$group)) {
1995: $can_upload = 1;
1996: }
1997: if (&Apache::lonnet::allowed('mgf',$env{'request.course.id'}.'/'.$group)) {
1998: $can_modify = 1;
1999: }
2000: if (&Apache::lonnet::allowed('dgf',$env{'request.course.id'}.'/'.$group)) {
2001: $can_delete = 1;
2002: }
2003: }
2004: } else {
2005: ($uname,$udom) = &get_name_dom();
2006: $portfolio_root = &get_portfolio_root();
2007: $title = &mt('Portfolio Manager');
2008: $can_modify = 1;
2009: $can_delete = 1;
2010: $can_upload = 1;
2011: $can_setacl = 1;
2012: }
2013:
2014: my $port_path = &get_port_path();
2015: &Apache::loncommon::no_cache($r);
2016: &Apache::loncommon::content_type($r,'text/html');
2017: $r->send_http_header;
2018: # Give the LON-CAPA page header
2019: if ($env{"form.mode"} eq 'selectfile'){
2020: $r->print(&Apache::loncommon::start_page($title,undef,
2021: {'only_body' => 1}));
2022: } elsif ($env{'form.action'} eq 'rolepicker') {
2023: $r->print(&Apache::loncommon::start_page('New role-based condition',undef,
2024: {'no_nav_bar' => 1, }));
2025: } else {
2026: $r->print(&Apache::loncommon::start_page($title));
2027: }
2028: $r->rflush();
2029: if (($env{'form.storeupl'}) & (!$env{'form.uploaddoc.filename'})){
2030: $r->print('<span class="LC_error">'.
2031: 'No file was selected to upload.'.
2032: 'To upload a file, click <strong>Browse...</strong>'.
2033: ', select a file, then click <strong>Upload</strong>.'.
2034: '</span>');
2035: }
2036: if ($env{'form.meta'}) {
2037: &open_form($r,$url);
2038: # $r->print(&edit_meta_data($r, $env{'form.currentpath'}.$env{'form.selectfile'}));
2039: $r->print('Edit the meta data<br />');
2040: &close_form($r,$url);
2041: }
2042: if ($env{'form.store'}) {
2043: }
2044:
2045: if ($env{'form.uploaddoc.filename'}) {
2046: if ($can_upload) {
2047: &upload($r,$url,$group);
2048: } else {
2049: &missing_priv($r,$url,'upload');
2050: }
2051: } elsif ($env{'form.action'} eq 'delete' && $env{'form.confirmed'}) {
2052: if ($can_delete) {
2053: &delete_confirmed($r,$url,$group);
2054: } else {
2055: &missing_priv($r,$url,'delete');
2056: }
2057: } elsif ($env{'form.action'} eq 'delete') {
2058: if ($can_delete) {
2059: &delete($r,$url);
2060: } else {
2061: &missing_priv($r,$url,'delete');
2062: }
2063: } elsif ($env{'form.action'} eq 'deletedir' && $env{'form.confirmed'}) {
2064: if ($can_delete) {
2065: &delete_dir_confirmed($r,$url);
2066: } else {
2067: &missing_priv($r,$url,'delete');
2068: }
2069: } elsif ($env{'form.action'} eq 'deletedir') {
2070: if ($can_delete) {
2071: &delete_dir($r,$url);
2072: } else {
2073: &missing_priv($r,$url,'delete');
2074: }
2075: } elsif ($env{'form.action'} eq 'rename' && $env{'form.confirmed'}) {
2076: if ($can_modify) {
2077: &rename_confirmed($r,$url,$group);
2078: } else {
2079: &missing_priv($r,$url,'rename');
2080: }
2081: } elsif ($env{'form.rename'}) {
2082: $env{'form.selectfile'} = $env{'form.rename'};
2083: $env{'form.action'} = 'rename';
2084: if ($can_modify) {
2085: &rename($r,$url);
2086: } else {
2087: &missing_priv($r,$url,'rename');
2088: }
2089: } elsif ($env{'form.access'}) {
2090: $env{'form.selectfile'} = $env{'form.access'};
2091: $env{'form.action'} = 'chgaccess';
2092: &display_access($r,$url,$group,$can_setacl,$port_path);
2093: } elsif ($env{'form.action'} eq 'chgaccess') {
2094: if ($can_setacl) {
2095: &update_access($r,$url,$group,$port_path);
2096: } else {
2097: &missing_priv($r,$url,'setacl');
2098: }
2099: } elsif ($env{'form.action'} eq 'rolepicker') {
2100: if ($can_setacl) {
2101: &role_options_window($r);
2102: } else {
2103: &missing_priv($r,$url,'setacl');
2104: }
2105: } elsif ($env{'form.createdir'}) {
2106: if ($can_upload) {
2107: &createdir($r,$url);
2108: } else {
2109: &missing_priv($r,$url,'upload');
2110: }
2111: } elsif ($env{'form.lockinfo'}) {
2112: &lock_info($r,$url,$group);
2113: } else {
2114: my $current_path='/';
2115: if ($env{'form.currentpath'}) {
2116: $current_path = $env{'form.currentpath'};
2117: }
2118: if ($caller eq 'coursegrp_portfolio') {
2119: &Apache::lonhtmlcommon::clear_breadcrumbs();
2120: $r->print(&coursegrp_portfolio_header($udom,$uname,$grp_desc));
2121: }
2122: my @dir_list=&get_dir_list($portfolio_root);
2123: if ($dir_list[0] eq 'no_such_dir'){
2124: # two main reasons for this:
2125: # 1) never been here, so directory structure not created
2126: # 2) back-button navigation after deleting a directory
2127: if ($current_path eq '/'){
2128: &Apache::lonnet::mkdiruserfile($uname,$udom,
2129: &get_port_path());
2130: } else {
2131: # some directory that snuck in get rid of the directory
2132: # from the recent pulldown, just in case
2133: &Apache::lonhtmlcommon::remove_recent('portfolio',
2134: [$current_path]);
2135: $current_path = '/'; # force it back to the root
2136: }
2137: # now grab the directory list again, for the first time
2138: @dir_list=&Apache::lonnet::dirlist($current_path,
2139: $udom,$uname,$portfolio_root);
2140: }
2141: # need to know if directory is empty so it can be removed if desired
2142: my $is_empty=(@dir_list == 2);
2143: &display_common($r,$url,$current_path,$is_empty,\@dir_list,
2144: $can_upload);
2145: &display_directory($r,$url,$current_path,$is_empty,\@dir_list,$group,
2146: $can_upload,$can_modify,$can_delete,$can_setacl);
2147: $r->print(&Apache::loncommon::end_page());
2148: }
2149: return OK;
2150: }
2151:
2152: 1;
2153: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>