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