File:
[LON-CAPA] /
loncom /
interface /
lonmeta.pm
Revision
1.201:
download - view:
text,
annotated -
select for diffs
Fri Jan 19 14:23:23 2007 UTC (17 years, 7 months ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
Prefer a null value as the option value in a select box when the user chooses "Not specified" - searching in MySQL for IS NULL seems better.
In cases where there are multiple ways to enter metadata for a single field, eliminate storage of comma(s) when the user has not specified anything
for the field.
Advanced search for portfolio files can now include the name(s) of added (course-specified) field(s) with/without the value(s).
addedfieldnames and addedfieldvalues eliminated from the portfolio_metadata table in MySQL - searches do a join on the portfolio_addedfields table instead.
1: # The LearningOnline Network with CAPA
2: # Metadata display handler
3: #
4: # $Id: lonmeta.pm,v 1.201 2007/01/19 14:23:23 raeburn 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::lonmeta;
30:
31: use strict;
32: use LONCAPA::lonmetadata();
33: use Apache::Constants qw(:common);
34: use Apache::lonnet;
35: use Apache::loncommon();
36: use Apache::lonhtmlcommon();
37: use Apache::lonmsg;
38: use Apache::lonpublisher;
39: use Apache::lonlocal;
40: use Apache::lonmysql;
41: use Apache::lonmsg;
42: use LONCAPA qw(:DEFAULT :match);
43:
44:
45: ############################################################
46: ############################################################
47: ##
48: ## &get_dynamic_metadata_from_sql($url)
49: ##
50: ## Queries sql database for dynamic metdata
51: ## Returns a hash of hashes, with keys of urls which match $url
52: ## Returned fields are given below.
53: ##
54: ## Examples:
55: ##
56: ## %DynamicMetadata = &Apache::lonmeta::get_dynmaic_metadata_from_sql
57: ## ('/res/msu/korte/');
58: ##
59: ## $DynamicMetadata{'/res/msu/korte/example.problem'}->{$field}
60: ##
61: ############################################################
62: ############################################################
63: sub get_dynamic_metadata_from_sql {
64: my ($url) = shift();
65: my ($authordom,$author)=($url=~m{^/res/($match_domain)/($match_username)/});
66: if (! defined($authordom)) {
67: $authordom = shift();
68: }
69: if (! defined($author)) {
70: $author = shift();
71: }
72: if (! defined($authordom) || ! defined($author)) {
73: return ();
74: }
75: my $query = 'SELECT * FROM metadata WHERE url LIKE "'.$url.'%"';
76: my $server = &Apache::lonnet::homeserver($author,$authordom);
77: my $reply = &Apache::lonnet::metadata_query($query,undef,undef,
78: ,[$server]);
79: return () if (! defined($reply) || ref($reply) ne 'HASH');
80: my $filename = $reply->{$server};
81: if (! defined($filename) || $filename =~ /^error/) {
82: return ();
83: }
84: my $max_time = time + 10; # wait 10 seconds for results at most
85: my %ReturnHash;
86: #
87: # Look for results
88: my $finished = 0;
89: while (! $finished && time < $max_time) {
90: my $datafile=$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename;
91: if (! -e "$datafile.end") { next; }
92: my $fh;
93: if (!($fh=Apache::File->new($datafile))) { next; }
94: while (my $result = <$fh>) {
95: chomp($result);
96: next if (! $result);
97: my %hash=&LONCAPA::lonmetadata::metadata_col_to_hash('metadata',
98: map { &unescape($_) } split(/\,/,$result));
99: foreach my $key (keys(%hash)) {
100: $ReturnHash{$hash{'url'}}->{$key}=$hash{$key};
101: }
102: }
103: $finished = 1;
104: }
105: #
106: return %ReturnHash;
107: }
108:
109:
110: # Fetch and evaluate dynamic metadata
111: sub dynamicmeta {
112: my $url=&Apache::lonnet::declutter(shift);
113: $url=~s/\.meta$//;
114: my ($adomain,$aauthor)=($url=~/^($match_domain)\/($match_username)\//);
115: my $regexp=$url;
116: $regexp=~s/(\W)/\\$1/g;
117: $regexp='___'.$regexp.'___';
118: my %evaldata=&Apache::lonnet::dump('nohist_resevaldata',$adomain,
119: $aauthor,$regexp);
120: my %DynamicData = &LONCAPA::lonmetadata::process_reseval_data(\%evaldata);
121: my %Data = &LONCAPA::lonmetadata::process_dynamic_metadata($url,
122: \%DynamicData);
123: #
124: # Deal with 'count' separately
125: $Data{'count'} = &access_count($url,$aauthor,$adomain);
126: #
127: # Debugging code I will probably need later
128: if (0) {
129: &Apache::lonnet::logthis('Dynamic Metadata');
130: while(my($k,$v)=each(%Data)){
131: &Apache::lonnet::logthis(' "'.$k.'"=>"'.$v.'"');
132: }
133: &Apache::lonnet::logthis('-------------------');
134: }
135: return %Data;
136: }
137:
138: sub access_count {
139: my ($src,$author,$adomain) = @_;
140: my %countdata=&Apache::lonnet::dump('nohist_accesscount',$adomain,
141: $author,$src);
142: if (! exists($countdata{$src})) {
143: return &mt('Not Available');
144: } else {
145: return $countdata{$src};
146: }
147: }
148:
149: # Try to make an alt tag if there is none
150: sub alttag {
151: my ($base,$src)=@_;
152: my $fullpath=&Apache::lonnet::hreflocation($base,$src);
153: my $alttag=&Apache::lonnet::metadata($fullpath,'title').' '.
154: &Apache::lonnet::metadata($fullpath,'subject').' '.
155: &Apache::lonnet::metadata($fullpath,'abstract');
156: $alttag=~s/\s+/ /gs;
157: $alttag=~s/\"//gs;
158: $alttag=~s/\'//gs;
159: $alttag=~s/\s+$//gs;
160: $alttag=~s/^\s+//gs;
161: if ($alttag) {
162: return $alttag;
163: } else {
164: return &mt('No information available');
165: }
166: }
167:
168: # Author display
169: sub authordisplay {
170: my ($aname,$adom)=@_;
171: return &Apache::loncommon::aboutmewrapper
172: (&Apache::loncommon::plainname($aname,$adom),
173: $aname,$adom,'preview').' <tt>['.$aname.':'.$adom.']</tt>';
174: }
175:
176: # Pretty display
177: sub evalgraph {
178: my $value=shift;
179: if (! $value) {
180: return '';
181: }
182: my $val=int($value*10.+0.5)-10;
183: my $output='<table border="0" cellpadding="0" cellspacing="0"><tr>';
184: if ($val>=20) {
185: $output.='<td width="20" bgcolor="#555555">  </td>';
186: } else {
187: $output.='<td width="'.($val).'" bgcolor="#555555"> </td>'.
188: '<td width="'.(20-$val).'" bgcolor="#FF3333"> </td>';
189: }
190: $output.='<td bgcolor="#FFFF33"> </td>';
191: if ($val>20) {
192: $output.='<td width="'.($val-20).'" bgcolor="#33FF33"> </td>'.
193: '<td width="'.(40-$val).'" bgcolor="#555555"> </td>';
194: } else {
195: $output.='<td width="20" bgcolor="#555555">  </td>';
196: }
197: $output.='<td> ('.sprintf("%5.2f",$value).') </td></tr></table>';
198: return $output;
199: }
200:
201: sub diffgraph {
202: my $value=shift;
203: if (! $value) {
204: return '';
205: }
206: my $val=int(40.0*$value+0.5);
207: my @colors=('#FF9933','#EEAA33','#DDBB33','#CCCC33',
208: '#BBDD33','#CCCC33','#DDBB33','#EEAA33');
209: my $output='<table border="0" cellpadding="0" cellspacing="0"><tr>';
210: for (my $i=0;$i<8;$i++) {
211: if ($val>$i*5) {
212: $output.='<td width="5" bgcolor="'.$colors[$i].'"> </td>';
213: } else {
214: $output.='<td width="5" bgcolor="#555555"> </td>';
215: }
216: }
217: $output.='<td> ('.sprintf("%3.2f",$value).') </td></tr></table>';
218: return $output;
219: }
220:
221:
222: # The field names
223: sub fieldnames {
224: my $file_type=shift;
225: my %fields =
226: ('title' => 'Title',
227: 'author' =>'Author(s)',
228: 'authorspace' => 'Author Space',
229: 'modifyinguser' => 'Last Modifying User',
230: 'subject' => 'Subject',
231: 'standards' => 'Standards',
232: 'keywords' => 'Keyword(s)',
233: 'notes' => 'Notes',
234: 'abstract' => 'Abstract',
235: 'lowestgradelevel' => 'Lowest Grade Level',
236: 'highestgradelevel' => 'Highest Grade Level');
237:
238: if ( !defined($file_type) || ($file_type ne 'portfolio' && $file_type ne 'groups') ) {
239: %fields =
240: (%fields,
241: 'domain' => 'Domain',
242: 'mime' => 'MIME Type',
243: 'language' => 'Language',
244: 'creationdate' => 'Creation Date',
245: 'lastrevisiondate' => 'Last Revision Date',
246: 'owner' => 'Publisher/Owner',
247: 'copyright' => 'Copyright/Distribution',
248: 'customdistributionfile' => 'Custom Distribution File',
249: 'sourceavail' => 'Source Available',
250: 'sourcerights' => 'Source Custom Distribution File',
251: 'obsolete' => 'Obsolete',
252: 'obsoletereplacement' => 'Suggested Replacement for Obsolete File',
253: 'count' => 'Network-wide number of accesses (hits)',
254: 'course' => 'Network-wide number of courses using resource',
255: 'course_list' => 'Network-wide courses using resource',
256: 'sequsage' => 'Number of resources using or importing resource',
257: 'sequsage_list' => 'Resources using or importing resource',
258: 'goto' => 'Number of resources that follow this resource in maps',
259: 'goto_list' => 'Resources that follow this resource in maps',
260: 'comefrom' => 'Number of resources that lead up to this resource in maps',
261: 'comefrom_list' => 'Resources that lead up to this resource in maps',
262: 'clear' => 'Material presented in clear way',
263: 'depth' => 'Material covered with sufficient depth',
264: 'helpful' => 'Material is helpful',
265: 'correct' => 'Material appears to be correct',
266: 'technical' => 'Resource is technically correct',
267: 'avetries' => 'Average number of tries till solved',
268: 'stdno' => 'Total number of students who have worked on this problem',
269: 'difficulty' => 'Degree of difficulty',
270: 'disc' => 'Degree of discrimination',
271: 'dependencies' => 'Resources used by this resource',
272: );
273: }
274: return &Apache::lonlocal::texthash(%fields);
275: }
276:
277: sub portfolio_linked_path {
278: my ($path,$group,$port_path) = @_;
279:
280: my $start = 'portfolio';
281: if ($group) {
282: $start = "groups/$group/".$start;
283: }
284: my %anchor_fields = (
285: 'selectfile' => $start,
286: 'currentpath' => '/'
287: );
288: my $result = &Apache::portfolio::make_anchor($port_path,\%anchor_fields,$start);
289: my $fullpath = '/';
290: my (undef,@tree) = split('/',$path);
291: my $filename = pop(@tree);
292: foreach my $dir (@tree) {
293: $fullpath .= $dir.'/';
294: $result .= '/';
295: my %anchor_fields = (
296: 'selectfile' => $dir,
297: 'currentpath' => $fullpath
298: );
299: $result .= &Apache::portfolio::make_anchor($port_path,\%anchor_fields,$dir);
300: }
301: $result .= "/$filename";
302: return $result;
303: }
304:
305: sub get_port_path_and_group {
306: my ($uri)=@_;
307:
308: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
309: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
310:
311: my ($port_path,$group);
312: if ($uri =~ m{^/editupload/\Q$cdom\E/\Q$cnum\E/groups/}) {
313: $group = (split('/',$uri))[5];
314: $port_path = '/adm/coursegrp_portfolio';
315: } else {
316: $port_path = '/adm/portfolio';
317: }
318: if ($env{'form.group'} ne $group) {
319: $env{'form.group'} = $group;
320: }
321: return ($port_path,$group);
322: }
323:
324: sub portfolio_display_uri {
325: my ($uri,$as_links)=@_;
326:
327: my ($port_path,$group) = &get_port_path_and_group($uri);
328:
329: $uri =~ s|.*/(portfolio/.*)$|$1|;
330: my ($res_uri,$meta_uri) = ($uri,$uri);
331: if ($uri =~ /\.meta$/) {
332: $res_uri =~ s/\.meta//;
333: } else {
334: $meta_uri .= '.meta';
335: }
336:
337: my ($path) = ($res_uri =~ m|^portfolio(.*/)[^/]*$|);
338: if ($as_links) {
339: $res_uri = &portfolio_linked_path($res_uri,$group,$port_path);
340: $meta_uri = &portfolio_linked_path($meta_uri,$group,$port_path);
341: }
342: return ($res_uri,$meta_uri,$path);
343: }
344:
345: sub pre_select_course {
346: my ($r,$uri) = @_;
347: my $output;
348: my $fn=&Apache::lonnet::filelocation('',$uri);
349: my ($res_uri,$meta_uri,$path) = &portfolio_display_uri($uri);
350: %Apache::lonpublisher::metadatafields=();
351: %Apache::lonpublisher::metadatakeys=();
352: my $result=&Apache::lonnet::getfile($fn);
353: if ($result == -1){
354: $r->print(&mt('Creating new file [_1]'),$meta_uri);
355: } else {
356: &Apache::lonpublisher::metaeval($result);
357: }
358: $r->print('<hr /><form method="post" action="" >');
359: $r->print('<p>'.&mt('If you would like to associate this resource ([_1]) with a current or previous course, please select one from the list below, otherwise select, \'None\'','<tt>'.$res_uri.'</tt>').'</p>');
360: $output = &select_course();
361: $r->print($output.'<br /><input type="submit" name="store" value="'.
362: &mt('Associate Resource With Selected Course').'" />');
363: $r->print('<input type="hidden" name="currentpath" value="'.$env{'form.currentpath'}.'" />');
364: $r->print('<input type="hidden" name="associate" value="true" />');
365: $r->print('</form>');
366:
367: my ($port_path,$group) = &get_port_path_and_group($uri);
368: my $group_input;
369: if ($group) {
370: $group_input = '<input type="hidden" name="group" value="'.$group.'" />';
371: }
372: $r->print('<br /><br /><form method="post" action="'.$port_path.'">'.
373: '<input type="hidden" name="currentpath" value="'.$path.'" />'.
374: $group_input.
375: '<input type="submit" name="cancel" value="'.&mt('Cancel').'" />'.
376: '</form>');
377:
378: return;
379: }
380: sub select_course {
381: my $output=$/;
382: my $current_restriction=
383: $Apache::lonpublisher::metadatafields{'courserestricted'};
384: my $selected = ($current_restriction eq 'none' ? 'selected="selected"'
385: : '');
386: if ($current_restriction =~ /^course\.($match_domain\_$match_courseid)$/) {
387: my $assoc_crs = $1;
388: my $added_metadata_fields = &Apache::lonparmset::get_added_meta_fieldnames($assoc_crs);
389: if (ref($added_metadata_fields) eq 'HASH') {
390: if (keys(%{$added_metadata_fields}) > 0) {
391: my $transfernotes;
392: foreach my $field_name (keys(%{$added_metadata_fields})) {
393: my $value = $Apache::lonpublisher::metadatafields{$field_name};
394: if ($value) {
395: $transfernotes .=
396: &Apache::loncommon::start_data_table_row().
397: '<td><input type="checkbox" name="transfer_'.
398: $field_name.'" value="1" /></td><td>'.
399: $field_name.'</td><td>'.$value.'</td>'.
400: &Apache::loncommon::end_data_table_row();
401: }
402: }
403: if ($transfernotes ne '') {
404: my %courseinfo = &Apache::lonnet::coursedescription($assoc_crs,{'one_time' => 1});
405: my $assoc_crs_description = $courseinfo{'description'};
406: $output .= &mt('This resource is currently associated with a course ([_1]) which includes added metadata fields specific to the course.',$assoc_crs_description).'<br />'."\n".
407: &mt('You can choose to transfer data from the added fields to the "Notes" field if you are planning to change the course association.').'<br /><br />'.
408: &Apache::loncommon::start_data_table().
409: &Apache::loncommon::start_data_table_header_row().
410: '<th>Copy to notes?</th>'."\n".
411: '<th>Field name</th>'."\n".
412: '<th>Values</th>'."\n".
413: &Apache::loncommon::end_data_table_header_row().
414: $transfernotes.
415: &Apache::loncommon::end_data_table().'<br />';
416: }
417: }
418: }
419: }
420: $output .= '<select name="new_courserestricted" >';
421: $output .= '<option value="none" '.$selected.'>'.
422: &mt('None').'</option>'.$/;
423: my %courses;
424: foreach my $key (keys(%env)) {
425: if ($key !~ m/^course\.(.+)\.description$/) { next; }
426: my $cid = $1;
427: if ($env{$key} !~ /\S/) { next; }
428: $courses{$key} = $cid;
429: }
430: foreach my $key (sort { lc($env{$a}) cmp lc($env{$b}) } (keys(%courses))) {
431: my $cid = 'course.'.$courses{$key};
432: my $selected = ($current_restriction eq $cid ? 'selected="selected"'
433: : '');
434: if ($env{$key} !~ /\S/) { next; }
435: $output .= '<option value="'.$cid.'" '.$selected.'>';
436: $output .= $env{$key};
437: $output .= '</option>'.$/;
438: $selected = '';
439: }
440: $output .= '</select><br />';
441: return ($output);
442: }
443: # Pretty printing of metadata field
444:
445: sub prettyprint {
446: my ($type,$value,$target,$prefix,$form,$noformat)=@_;
447: # $target,$prefix,$form are optional and for filecrumbs only
448: if (! defined($value)) {
449: return ' ';
450: }
451: # Title
452: if ($type eq 'title') {
453: return '<font size="+1" face="arial">'.$value.'</font>';
454: }
455: # Dates
456: if (($type eq 'creationdate') ||
457: ($type eq 'lastrevisiondate')) {
458: return ($value?&Apache::lonlocal::locallocaltime(
459: &Apache::lonmysql::unsqltime($value)):
460: &mt('not available'));
461: }
462: # Language
463: if ($type eq 'language') {
464: return &Apache::loncommon::languagedescription($value);
465: }
466: # Copyright
467: if ($type eq 'copyright') {
468: return &Apache::loncommon::copyrightdescription($value);
469: }
470: # Copyright
471: if ($type eq 'sourceavail') {
472: return &Apache::loncommon::source_copyrightdescription($value);
473: }
474: # MIME
475: if ($type eq 'mime') {
476: return '<img src="'.&Apache::loncommon::icon($value).'" /> '.
477: &Apache::loncommon::filedescription($value);
478: }
479: # Person
480: if (($type eq 'author') ||
481: ($type eq 'owner') ||
482: ($type eq 'modifyinguser') ||
483: ($type eq 'authorspace')) {
484: $value=~s/($match_username)(\:|\@)($match_domain)/&authordisplay($1,$3)/gse;
485: return $value;
486: }
487: # Gradelevel
488: if (($type eq 'lowestgradelevel') ||
489: ($type eq 'highestgradelevel')) {
490: return &Apache::loncommon::gradeleveldescription($value);
491: }
492: # Only for advance users below
493: if (! $env{'user.adv'}) {
494: return '<i>- '.&mt('not displayed').' -</i>';
495: }
496: # File
497: if (($type eq 'customdistributionfile') ||
498: ($type eq 'obsoletereplacement') ||
499: ($type eq 'goto_list') ||
500: ($type eq 'comefrom_list') ||
501: ($type eq 'sequsage_list') ||
502: ($type eq 'dependencies')) {
503: return '<font size="-1"><ul>'.join("\n",map {
504: my $url = &Apache::lonnet::clutter_with_no_wrapper($_);
505: my $title = &Apache::lonnet::gettitle($url);
506: if ($title eq '') {
507: $title = 'Untitled';
508: if ($url =~ /\.sequence$/) {
509: $title .= ' Sequence';
510: } elsif ($url =~ /\.page$/) {
511: $title .= ' Page';
512: } elsif ($url =~ /\.problem$/) {
513: $title .= ' Problem';
514: } elsif ($url =~ /\.html$/) {
515: $title .= ' HTML document';
516: } elsif ($url =~ m:/syllabus$:) {
517: $title .= ' Syllabus';
518: }
519: }
520: $_ = '<li>'.$title.' '.
521: &Apache::lonhtmlcommon::crumbs($url,$target,$prefix,$form,'-1',$noformat).
522: '</li>'
523: } split(/\s*\,\s*/,$value)).'</ul></font>';
524: }
525: # Evaluations
526: if (($type eq 'clear') ||
527: ($type eq 'depth') ||
528: ($type eq 'helpful') ||
529: ($type eq 'correct') ||
530: ($type eq 'technical')) {
531: return &evalgraph($value);
532: }
533: # Difficulty
534: if ($type eq 'difficulty' || $type eq 'disc') {
535: return &diffgraph($value);
536: }
537: # List of courses
538: if ($type=~/\_list/) {
539: my @Courses = split(/\s*\,\s*/,$value);
540: my $Str='<font size="-1"><ul>';
541: my %descriptions;
542: foreach my $course (@Courses) {
543: my %courseinfo =
544: &Apache::lonnet::coursedescription($course,
545: {'one_time' => 1});
546: if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
547: next;
548: }
549: $descriptions{join('\0',@courseinfo{'domain','description'})} .=
550: '<li><a href="/public/'.$courseinfo{'domain'}.'/'.
551: $courseinfo{'num'}.'/syllabus" target="preview">'.
552: $courseinfo{'description'}.' ('.$courseinfo{'domain'}.
553: ')</a></li>';
554: }
555: foreach my $course (sort {lc($a) cmp lc($b)} (keys(%descriptions))) {
556: $Str .= $descriptions{$course};
557: }
558:
559: return $Str.'</ul></font>';
560: }
561: # No pretty print found
562: return $value;
563: }
564:
565: # Pretty input of metadata field
566: sub direct {
567: return shift;
568: }
569:
570: sub selectbox {
571: my ($name,$value,$functionref,@idlist)=@_;
572: if (! defined($functionref)) {
573: $functionref=\&direct;
574: }
575: my $selout='<select name="'.$name.'">';
576: foreach (@idlist) {
577: $selout.='<option value=\''.$_.'\'';
578: if ($_ eq $value) {
579: $selout.=' selected>'.&{$functionref}($_).'</option>';
580: }
581: else {$selout.='>'.&{$functionref}($_).'</option>';}
582: }
583: return $selout.'</select>';
584: }
585:
586: sub relatedfield {
587: my ($show,$relatedsearchflag,$relatedsep,$fieldname,$relatedvalue)=@_;
588: if (! $relatedsearchflag) {
589: return '';
590: }
591: if (! defined($relatedsep)) {
592: $relatedsep=' ';
593: }
594: if (! $show) {
595: return $relatedsep.' ';
596: }
597: return $relatedsep.'<input type="checkbox" name="'.$fieldname.'_related"'.
598: ($relatedvalue?' checked="1"':'').' />';
599: }
600:
601: sub prettyinput {
602: my ($type,$value,$fieldname,$formname,
603: $relatedsearchflag,$relatedsep,$relatedvalue,$size,$course_key)=@_;
604: if (! defined($size)) {
605: $size = 80;
606: }
607: my $output;
608: if (defined($course_key)
609: && exists($env{$course_key.'.metadata.'.$type.'.options'})) {
610: my $stu_add;
611: my $only_one;
612: my %meta_options;
613: my @cur_values_inst;
614: my $cur_values_stu;
615: my $values = $env{$course_key.'.metadata.'.$type.'.values'};
616: if ($env{$course_key.'.metadata.'.$type.'.options'} =~ m/stuadd/) {
617: $stu_add = 'true';
618: }
619: if ($env{$course_key.'.metadata.'.$type.'.options'} =~ m/onlyone/) {
620: $only_one = 'true';
621: }
622: # need to take instructor values out of list where instructor and student
623: # values may be mixed.
624: if ($values) {
625: foreach my $item (split(/,/,$values)) {
626: $item =~ s/^\s+//;
627: $meta_options{$item} = $item;
628: }
629: foreach my $item (split(/,/,$value)) {
630: $item =~ s/^\s+//;
631: if ($meta_options{$item}) {
632: push(@cur_values_inst,$item);
633: } else {
634: if ($item ne '') {
635: $cur_values_stu .= $item.',';
636: }
637: }
638: }
639: my @key_order = sort(keys(%meta_options));
640: unshift (@key_order,'');
641: $meta_options{''} = 'Not specified';
642: $meta_options{'select_form_order'} = \@key_order;
643: } else {
644: $cur_values_stu = $value;
645: }
646: if ($type eq 'courserestricted') {
647: return (&select_course());
648: # return ('<input type="hidden" name="new_courserestricted" value="'.$course_key.'" />');
649: }
650: if (($type eq 'keywords') || ($type eq 'subject')
651: || ($type eq 'author')||($type eq 'notes')
652: || ($type eq 'abstract')|| ($type eq 'title')|| ($type eq 'standards')
653: || (exists($env{$course_key.'.metadata.'.$type.'.added'}))) {
654:
655: if ($values) {
656: if ($only_one) {
657: $output .= (&Apache::loncommon::select_form($cur_values_inst[0],'new_'.$type,%meta_options));
658: } else {
659: $output .= (&Apache::loncommon::multiple_select_form('new_'.$type,\@cur_values_inst,undef,\%meta_options));
660: }
661: }
662: if ($stu_add) {
663: $output .= '<input type="text" name="'.$fieldname.'" size="'.$size.'" '.
664: 'value="'.$cur_values_stu.'" />'.
665: &relatedfield(1,$relatedsearchflag,$relatedsep,$fieldname,
666: $relatedvalue);
667: }
668: return ($output);
669: }
670: if (($type eq 'lowestgradelevel') ||
671: ($type eq 'highestgradelevel')) {
672: return &Apache::loncommon::select_level_form($value,$fieldname).
673: &relatedfield(0,$relatedsearchflag,$relatedsep);
674: }
675: return();
676: }
677: # Language
678: if ($type eq 'language') {
679: return &selectbox($fieldname,
680: $value,
681: \&Apache::loncommon::languagedescription,
682: (&Apache::loncommon::languageids)).
683: &relatedfield(0,$relatedsearchflag,$relatedsep);
684: }
685: # Copyright
686: if ($type eq 'copyright') {
687: return &selectbox($fieldname,
688: $value,
689: \&Apache::loncommon::copyrightdescription,
690: (&Apache::loncommon::copyrightids)).
691: &relatedfield(0,$relatedsearchflag,$relatedsep);
692: }
693: # Source Copyright
694: if ($type eq 'sourceavail') {
695: return &selectbox($fieldname,
696: $value,
697: \&Apache::loncommon::source_copyrightdescription,
698: (&Apache::loncommon::source_copyrightids)).
699: &relatedfield(0,$relatedsearchflag,$relatedsep);
700: }
701: # Gradelevels
702: if (($type eq 'lowestgradelevel') ||
703: ($type eq 'highestgradelevel')) {
704: return &Apache::loncommon::select_level_form($value,$fieldname).
705: &relatedfield(0,$relatedsearchflag,$relatedsep);
706: }
707: # Obsolete
708: if ($type eq 'obsolete') {
709: return '<input type="checkbox" name="'.$fieldname.'"'.
710: ($value?' checked="1"':'').' />'.
711: &relatedfield(0,$relatedsearchflag,$relatedsep);
712: }
713: # Obsolete replacement file
714: if ($type eq 'obsoletereplacement') {
715: return '<input type="text" name="'.$fieldname.
716: '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
717: "('".$formname."','".$fieldname."'".
718: ",'')\">".&mt('Select').'</a>'.
719: &relatedfield(0,$relatedsearchflag,$relatedsep);
720: }
721: # Customdistribution file
722: if ($type eq 'customdistributionfile') {
723: return '<input type="text" name="'.$fieldname.
724: '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
725: "('".$formname."','".$fieldname."'".
726: ",'rights')\">".&mt('Select').'</a>'.
727: &relatedfield(0,$relatedsearchflag,$relatedsep);
728: }
729: # Source Customdistribution file
730: if ($type eq 'sourcerights') {
731: return '<input type="text" name="'.$fieldname.
732: '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
733: "('".$formname."','".$fieldname."'".
734: ",'rights')\">".&mt('Select').'</a>'.
735: &relatedfield(0,$relatedsearchflag,$relatedsep);
736: }
737: if ($type eq 'courserestricted') {
738: return (&select_course());
739: #return ('<input type="hidden" name="new_courserestricted" value="'.$course_key.'" />');
740: }
741:
742: # Dates
743: if (($type eq 'creationdate') ||
744: ($type eq 'lastrevisiondate')) {
745: return
746: &Apache::lonhtmlcommon::date_setter($formname,$fieldname,$value).
747: &relatedfield(0,$relatedsearchflag,$relatedsep);
748: }
749: # No pretty input found
750: $value=~s/^\s+//gs;
751: $value=~s/\s+$//gs;
752: $value=~s/\s+/ /gs;
753: $value=~s/\"/\"\;/gs;
754: return
755: '<input type="text" name="'.$fieldname.'" size="'.$size.'" '.
756: 'value="'.$value.'" />'.
757: &relatedfield(1,$relatedsearchflag,$relatedsep,$fieldname,
758: $relatedvalue);
759: }
760:
761: # Main Handler
762: sub handler {
763: my $r=shift;
764: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
765: ['currentpath','changecourse']);
766: my $uri=$r->uri;
767: #
768: # Set document type
769: &Apache::loncommon::content_type($r,'text/html');
770: $r->send_http_header;
771: return OK if $r->header_only;
772: my ($resdomain,$resuser)=
773: (&Apache::lonnet::declutter($uri)=~/^($match_domain)\/($match_username)\//);
774: if ($uri=~m:/adm/bombs/(.*)$:) {
775: $r->print(&Apache::loncommon::start_page('Error Messages'));
776: # Looking for all bombs?
777: &report_bombs($r,$uri);
778: } elsif ($uri=~m|^/editupload/[^/]+/[^/]+/portfolio/|) {
779: ($resdomain,$resuser)=
780: (&Apache::lonnet::declutter($uri)=~m|^($match_domain)/($match_name)/portfolio|);
781: $r->print(&Apache::loncommon::start_page('Edit Portfolio File Catalog Information',
782: undef,
783: {'domain' => $resdomain,}));
784: if ($env{'form.store'}) {
785: &present_editable_metadata($r,$uri,'portfolio');
786: } else {
787: my $fn=&Apache::lonnet::filelocation('',$uri);
788: %Apache::lonpublisher::metadatafields=();
789: %Apache::lonpublisher::metadatakeys=();
790: my $result=&Apache::lonnet::getfile($fn);
791: &Apache::lonpublisher::metaeval($result);
792: if ((!$Apache::lonpublisher::metadatafields{'courserestricted'}) ||
793: ($env{'form.changecourse'} eq 'true')) {
794: &pre_select_course($r,$uri);
795: } else {
796: &present_editable_metadata($r,$uri,'portfolio');
797: }
798: }
799: } elsif ($uri=~m|^/editupload/[^/]+/[^/]+/groups/|) {
800: $r->print(&Apache::loncommon::start_page('Edit Group Portfolio File Catalog Information',
801: undef,
802: {'domain' => $resdomain,}));
803: &present_editable_metadata($r,$uri,'groups');
804: } elsif ($uri=~m|^/~|) {
805: # Construction space
806: $r->print(&Apache::loncommon::start_page('Edit Catalog nformation',
807: undef,
808: {'domain' => $resdomain,}));
809: &present_editable_metadata($r,$uri);
810: } else {
811: $r->print(&Apache::loncommon::start_page('Catalog Information',
812: undef,
813: {'domain' => $resdomain,}));
814: &present_uneditable_metadata($r,$uri);
815: }
816: $r->print(&Apache::loncommon::end_page());
817: return OK;
818: }
819:
820: #####################################################
821: #####################################################
822: ### ###
823: ### Report Bombs ###
824: ### ###
825: #####################################################
826: #####################################################
827: sub report_bombs {
828: my ($r,$uri) = @_;
829: # Set document type
830: $uri =~ s:/adm/bombs/::;
831: $uri = &Apache::lonnet::declutter($uri);
832: $r->print('<h1>'.&Apache::lonnet::clutter($uri).'</h1>');
833: my ($domain,$author)=($uri=~/^($match_domain)\/($match_username)\//);
834: if (&Apache::loncacc::constructaccess('/~'.$author.'/',$domain)) {
835: if ($env{'form.clearbombs'}) {
836: &Apache::lonmsg::clear_author_res_msg($uri);
837: }
838: my $clear=&mt('Clear all Messages in Subdirectory');
839: $r->print(<<ENDCLEAR);
840: <form method="post">
841: <input type="submit" name="clearbombs" value="$clear" />
842: </form>
843: ENDCLEAR
844: my %brokenurls =
845: &Apache::lonmsg::all_url_author_res_msg($author,$domain);
846: foreach (sort(keys(%brokenurls))) {
847: if ($_=~/^\Q$uri\E/) {
848: $r->print
849: ('<a href="'.&Apache::lonnet::clutter($_).'">'.$_.'</a>'.
850: &Apache::lonmsg::retrieve_author_res_msg($_).
851: '<hr />');
852: }
853: }
854: } else {
855: $r->print(&mt('Not authorized'));
856: }
857: return;
858: }
859:
860: #####################################################
861: #####################################################
862: ### ###
863: ### Uneditable Metadata Display ###
864: ### ###
865: #####################################################
866: #####################################################
867: sub present_uneditable_metadata {
868: my ($r,$uri) = @_;
869: #
870: my $uploaded = ($uri =~ m|/uploaded/|);
871: my %content=();
872: # Read file
873: foreach (split(/\,/,&Apache::lonnet::metadata($uri,'keys'))) {
874: $content{$_}=&Apache::lonnet::metadata($uri,$_);
875: }
876: # Render Output
877: # displayed url
878: my ($thisversion)=($uri=~/\.(\d+)\.(\w+)\.meta$/);
879: $uri=~s/\.meta$//;
880: my $disuri=&Apache::lonnet::clutter_with_no_wrapper($uri);
881: # version
882: my $versiondisplay='';
883: if (!$uploaded) {
884: my $currentversion=&Apache::lonnet::getversion($disuri);
885: if ($thisversion) {
886: $versiondisplay=&mt('Version').': '.$thisversion.
887: ' ('.&mt('most recent version').': '.
888: ($currentversion>0 ?
889: $currentversion :
890: &mt('information not available')).')';
891: } else {
892: $versiondisplay='Version: '.$currentversion;
893: }
894: }
895: # crumbify displayed URL uri target prefix form size
896: $disuri=&Apache::lonhtmlcommon::crumbs($disuri,undef, undef, undef,'+1');
897: $disuri =~ s:<br />::g;
898: # obsolete
899: my $obsolete=$content{'obsolete'};
900: my $obsoletewarning='';
901: if (($obsolete) && ($env{'user.adv'})) {
902: $obsoletewarning='<p><font color="red">'.
903: &mt('This resource has been marked obsolete by the author(s)').
904: '</font></p>';
905: }
906: #
907: my %lt=&fieldnames();
908: my $table='';
909: my $title = $content{'title'};
910: if (! defined($title)) {
911: $title = 'Untitled Resource';
912: }
913: my @fields;
914: if ($uploaded) {
915: @fields = ('title','author','subject','keywords','notes','abstract',
916: 'lowestgradelevel','highestgradelevel','standards','mime',
917: 'owner');
918: } else {
919: @fields = ('title',
920: 'author',
921: 'subject',
922: 'keywords',
923: 'notes',
924: 'abstract',
925: 'lowestgradelevel',
926: 'highestgradelevel',
927: 'standards',
928: 'mime',
929: 'language',
930: 'creationdate',
931: 'lastrevisiondate',
932: 'owner',
933: 'copyright',
934: 'customdistributionfile',
935: 'sourceavail',
936: 'sourcerights',
937: 'obsolete',
938: 'obsoletereplacement');
939: }
940: foreach my $field (@fields) {
941: $table.='<tr><td bgcolor="#AAAAAA">'.$lt{$field}.
942: '</td><td bgcolor="#CCCCCC">'.
943: &prettyprint($field,$content{$field}).'</td></tr>';
944: delete($content{$field});
945: }
946: #
947: $r->print(<<ENDHEAD);
948: <h2>$title</h2>
949: <p>
950: $disuri<br />
951: $obsoletewarning
952: $versiondisplay
953: </p>
954: <table cellspacing="2" border="0">
955: $table
956: </table>
957: ENDHEAD
958: if (!$uploaded && $env{'user.adv'}) {
959: &print_dynamic_metadata($r,$uri,\%content);
960: }
961: return;
962: }
963:
964: sub print_dynamic_metadata {
965: my ($r,$uri,$content) = @_;
966: #
967: my %content = %$content;
968: my %lt=&fieldnames();
969: #
970: my $description = 'Dynamic Metadata (updated periodically)';
971: $r->print('<h3>'.&mt($description).'</h3>'.
972: &mt('Processing'));
973: $r->rflush();
974: my %items=&fieldnames();
975: my %dynmeta=&dynamicmeta($uri);
976: #
977: # General Access and Usage Statistics
978: if (exists($dynmeta{'count'}) ||
979: exists($dynmeta{'sequsage'}) ||
980: exists($dynmeta{'comefrom'}) ||
981: exists($dynmeta{'goto'}) ||
982: exists($dynmeta{'course'})) {
983: $r->print('<h4>'.&mt('Access and Usage Statistics').'</h4>'.
984: '<table cellspacing="2" border="0">');
985: foreach ('count',
986: 'sequsage','sequsage_list',
987: 'comefrom','comefrom_list',
988: 'goto','goto_list',
989: 'course','course_list') {
990: $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
991: '<td bgcolor="#CCCCCC">'.
992: &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
993: }
994: $r->print('</table>');
995: } else {
996: $r->print('<h4>'.&mt('No Access or Usages Statistics are available for this resource.').'</h4>');
997: }
998: #
999: # Assessment statistics
1000: if ($uri=~/\.(problem|exam|quiz|assess|survey|form)$/) {
1001: if (exists($dynmeta{'stdno'}) ||
1002: exists($dynmeta{'avetries'}) ||
1003: exists($dynmeta{'difficulty'}) ||
1004: exists($dynmeta{'disc'})) {
1005: # This is an assessment, print assessment data
1006: $r->print('<h4>'.
1007: &mt('Overall Assessment Statistical Data').
1008: '</h4>'.
1009: '<table cellspacing="2" border="0">');
1010: $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{'stdno'}.'</td>'.
1011: '<td bgcolor="#CCCCCC">'.
1012: &prettyprint('stdno',$dynmeta{'stdno'}).
1013: '</td>'."</tr>\n");
1014: foreach ('avetries','difficulty','disc') {
1015: $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
1016: '<td bgcolor="#CCCCCC">'.
1017: &prettyprint($_,sprintf('%5.2f',$dynmeta{$_})).
1018: '</td>'."</tr>\n");
1019: }
1020: $r->print('</table>');
1021: }
1022: if (exists($dynmeta{'stats'})) {
1023: #
1024: # New assessment statistics
1025: $r->print('<h4>'.
1026: &mt('Detailed Assessment Statistical Data').
1027: '</h4>');
1028: my $table = '<table cellspacing="2" border="0">'.
1029: '<tr>'.
1030: '<th>Course</th>'.
1031: '<th>Section(s)</th>'.
1032: '<th>Num Students</th>'.
1033: '<th>Mean Tries</th>'.
1034: '<th>Degree of Difficulty</th>'.
1035: '<th>Degree of Discrimination</th>'.
1036: '<th>Time of computation</th>'.
1037: '</tr>'.$/;
1038: foreach my $identifier (sort(keys(%{$dynmeta{'stats'}}))) {
1039: my $data = $dynmeta{'stats'}->{$identifier};
1040: my $course = $data->{'course'};
1041: my %courseinfo =
1042: &Apache::lonnet::coursedescription($course,
1043: {'one_time' => 1});
1044: if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
1045: &Apache::lonnet::logthis('lookup for '.$course.' failed');
1046: next;
1047: }
1048: $table .= '<tr>';
1049: $table .=
1050: '<td><nobr>'.$courseinfo{'description'}.'</nobr></td>';
1051: $table .=
1052: '<td align="right">'.$data->{'sections'}.'</td>';
1053: $table .=
1054: '<td align="right">'.$data->{'stdno'}.'</td>';
1055: foreach ('avetries','difficulty','disc') {
1056: $table .= '<td align="right">';
1057: if (exists($data->{$_})) {
1058: $table .= sprintf('%.2f',$data->{$_}).' ';
1059: } else {
1060: $table .= '';
1061: }
1062: $table .= '</td>';
1063: }
1064: $table .=
1065: '<td><nobr>'.
1066: &Apache::lonlocal::locallocaltime($data->{'timestamp'}).
1067: '</nobr></td>';
1068: $table .=
1069: '</tr>'.$/;
1070: }
1071: $table .= '</table>'.$/;
1072: $r->print($table);
1073: } else {
1074: $r->print('No new dynamic data found.');
1075: }
1076: } else {
1077: $r->print('<h4>'.
1078: &mt('No Assessment Statistical Data is available for this resource').
1079: '</h4>');
1080: }
1081:
1082: #
1083: #
1084: if (exists($dynmeta{'clear'}) ||
1085: exists($dynmeta{'depth'}) ||
1086: exists($dynmeta{'helpful'}) ||
1087: exists($dynmeta{'correct'}) ||
1088: exists($dynmeta{'technical'})){
1089: $r->print('<h4>'.&mt('Evaluation Data').'</h4>'.
1090: '<table cellspacing="2" border="0">');
1091: foreach ('clear','depth','helpful','correct','technical') {
1092: $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
1093: '<td bgcolor="#CCCCCC">'.
1094: &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
1095: }
1096: $r->print('</table>');
1097: } else {
1098: $r->print('<h4>'.&mt('No Evaluation Data is available for this resource.').'</h4>');
1099: }
1100: $uri=~/^\/res\/($match_domain)\/($match_username)\//;
1101: if ((($env{'user.domain'} eq $1) && ($env{'user.name'} eq $2))
1102: || ($env{'user.role.ca./'.$1.'/'.$2})) {
1103: if (exists($dynmeta{'comments'})) {
1104: $r->print('<h4>'.&mt('Evaluation Comments').' ('.
1105: &mt('visible to author and co-authors only').
1106: ')</h4>'.
1107: '<blockquote>'.$dynmeta{'comments'}.'</blockquote>');
1108: } else {
1109: $r->print('<h4>'.&mt('There are no Evaluation Comments on this resource.').'</h4>');
1110: }
1111: my $bombs = &Apache::lonmsg::retrieve_author_res_msg($uri);
1112: if (defined($bombs) && $bombs ne '') {
1113: $r->print('<a name="bombs" /><h4>'.&mt('Error Messages').' ('.
1114: &mt('visible to author and co-authors only').')'.
1115: '</h4>'.$bombs);
1116: } else {
1117: $r->print('<h4>'.&mt('There are currently no Error Messages for this resource.').'</h4>');
1118: }
1119: }
1120: #
1121: # All other stuff
1122: $r->print('<h3>'.
1123: &mt('Additional Metadata (non-standard, parameters, exports)').
1124: '</h3><table border="0" cellspacing="1">');
1125: foreach (sort(keys(%content))) {
1126: my $name=$_;
1127: if ($name!~/\.display$/) {
1128: my $display=&Apache::lonnet::metadata($uri,
1129: $name.'.display');
1130: if (! $display) {
1131: $display=$name;
1132: };
1133: my $otherinfo='';
1134: foreach ('name','part','type','default') {
1135: if (defined(&Apache::lonnet::metadata($uri,
1136: $name.'.'.$_))) {
1137: $otherinfo.=' '.$_.'='.
1138: &Apache::lonnet::metadata($uri,
1139: $name.'.'.$_).'; ';
1140: }
1141: }
1142: $r->print('<tr><td bgcolor="#bbccbb"><font size="-1" color="#556655">'.$display.'</font></td><td bgcolor="#ccddcc"><font size="-1" color="#556655">'.$content{$name});
1143: if ($otherinfo) {
1144: $r->print(' ('.$otherinfo.')');
1145: }
1146: $r->print("</font></td></tr>\n");
1147: }
1148: }
1149: $r->print("</table>");
1150: return;
1151: }
1152:
1153:
1154:
1155: #####################################################
1156: #####################################################
1157: ### ###
1158: ### Editable metadata display ###
1159: ### ###
1160: #####################################################
1161: #####################################################
1162: sub present_editable_metadata {
1163: my ($r,$uri,$file_type) = @_;
1164: # Construction Space Call
1165: # Header
1166: my $disuri=$uri;
1167: my $fn=&Apache::lonnet::filelocation('',$uri);
1168: $disuri=~s{^/\~}{/priv/};
1169: $disuri=~s/\.meta$//;
1170: my $meta_uri = $disuri;
1171: my $path;
1172: if ($disuri =~ m|/portfolio/|) {
1173: ($disuri, $meta_uri, $path) = &portfolio_display_uri($disuri,1);
1174: }
1175: my $target=$uri;
1176: $target=~s{^/\~}{/res/$env{'request.role.domain'}/};
1177: $target=~s/\.meta$//;
1178: my $bombs=&Apache::lonmsg::retrieve_author_res_msg($target);
1179: if ($bombs) {
1180: my $showdel=1;
1181: if ($env{'form.delmsg'}) {
1182: if (&Apache::lonmsg::del_url_author_res_msg($target) eq 'ok') {
1183: $bombs=&mt('Messages deleted.');
1184: $showdel=0;
1185: } else {
1186: $bombs=&mt('Error deleting messages');
1187: }
1188: }
1189: if ($env{'form.clearmsg'}) {
1190: my $cleardir=$target;
1191: $cleardir=~s/\/[^\/]+$/\//;
1192: if (&Apache::lonmsg::clear_author_res_msg($cleardir) eq 'ok') {
1193: $bombs=&mt('Messages cleared.');
1194: $showdel=0;
1195: } else {
1196: $bombs=&mt('Error clearing messages');
1197: }
1198: }
1199: my $del=&mt('Delete Messages for this Resource');
1200: my $clear=&mt('Clear all Messages in Subdirectory');
1201: my $goback=&mt('Back to Source File');
1202: $r->print(<<ENDBOMBS);
1203: <h1>$disuri</h1>
1204: <form method="post" action="" name="defaultmeta">
1205: ENDBOMBS
1206: if ($showdel) {
1207: $r->print(<<ENDDEL);
1208: <input type="submit" name="delmsg" value="$del" />
1209: <input type="submit" name="clearmsg" value="$clear" />
1210: ENDDEL
1211: } else {
1212: $r->print('<a href="'.$disuri.'" />'.$goback.'</a>');
1213: }
1214: $r->print('<br />'.$bombs);
1215: } else {
1216: my $displayfile='Catalog Information for '.$disuri;
1217: if ($disuri=~/\/default$/) {
1218: my $dir=$disuri;
1219: $dir=~s/default$//;
1220: $displayfile=
1221: &mt('Default Cataloging Information for Directory').' '.
1222: $dir;
1223: }
1224: %Apache::lonpublisher::metadatafields=();
1225: %Apache::lonpublisher::metadatakeys=();
1226: my $result=&Apache::lonnet::getfile($fn);
1227: if ($result == -1){
1228: $r->print(&mt('Creating new file [_1]'),$meta_uri);
1229: } else {
1230: &Apache::lonpublisher::metaeval($result);
1231: }
1232: if ($env{'form.new_courserestricted'}) {
1233: my $new_assoc_course = $env{'form.new_courserestricted'};
1234: my $prev_courserestricted = $Apache::lonpublisher::metadatafields{'courserestricted'};
1235: if (($prev_courserestricted) &&
1236: ($prev_courserestricted ne $new_assoc_course)) {
1237: my $transfers = [];
1238: foreach my $key (keys(%env)) {
1239: if ($key =~ /^form\.transfer_(.+)$/) {
1240: push(@{$transfers},$1);
1241: }
1242: }
1243: if (@{$transfers} > 0) {
1244: &store_transferred_addedfields($fn,$uri,$transfers);
1245: }
1246: }
1247: }
1248: $r->print(<<ENDEDIT);
1249: <h1>$displayfile</h1>
1250: <form method="post" action="" name="defaultmeta">
1251: ENDEDIT
1252: $r->print('<script type="JavaScript">'.
1253: &Apache::loncommon::browser_and_searcher_javascript().
1254: '</script>');
1255: my %lt=&fieldnames($file_type);
1256: my $output;
1257: my @fields;
1258: my $added_metadata_fields;
1259: my @added_order;
1260: if ($file_type eq 'groups') {
1261: $Apache::lonpublisher::metadatafields{'courserestricted'}=
1262: 'course.'.$env{'request.course.id'};
1263: }
1264: if ((! $Apache::lonpublisher::metadatafields{'courserestricted'}) &&
1265: (! $env{'form.new_courserestricted'}) && (! $file_type eq 'groups')) {
1266: $Apache::lonpublisher::metadatafields{'courserestricted'}=
1267: 'none';
1268: } elsif ($env{'form.new_courserestricted'}) {
1269: $Apache::lonpublisher::metadatafields{'courserestricted'}=
1270: $env{'form.new_courserestricted'};
1271: }
1272: if ($file_type eq 'portfolio' || $file_type eq 'groups') {
1273: if(exists ($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'})) {
1274: # retrieve fieldnames (in order) from the course restricted list
1275: @fields = (split(/,/,$env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'}));
1276: } else {
1277: # no saved field list, use default list
1278: @fields = ('author','title','subject','keywords','abstract',
1279: 'notes','lowestgradelevel',
1280: 'highestgradelevel','standards');
1281: if ($Apache::lonpublisher::metadatafields{'courserestricted'} =~ /^course\.($match_domain\_$match_courseid)$/) {
1282: my $assoc_crs = $1;
1283: $added_metadata_fields = &Apache::lonparmset::get_added_meta_fieldnames($assoc_crs);
1284: if ($env{'course.'.$assoc_crs.'.metadata.addedorder'}) {
1285: @added_order = split(/,/,$env{'course.'.$assoc_crs.'.metadata.addedorder'});
1286: }
1287: $env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'} = join(",",@fields);
1288: }
1289: }
1290: } else {
1291: @fields = ('author','title','subject','keywords','abstract','notes',
1292: 'copyright','customdistributionfile','language',
1293: 'standards',
1294: 'lowestgradelevel','highestgradelevel','sourceavail','sourcerights',
1295: 'obsolete','obsoletereplacement');
1296: }
1297: if (! $Apache::lonpublisher::metadatafields{'copyright'}) {
1298: $Apache::lonpublisher::metadatafields{'copyright'}=
1299: 'default';
1300: }
1301: if (($file_type eq 'portfolio') || ($file_type eq 'groups')) {
1302: if (! $Apache::lonpublisher::metadatafields{'mime'}) {
1303: ($Apache::lonpublisher::metadatafields{'mime'}) =
1304: ( $target=~/\.(\w+)$/ );
1305: }
1306: if (! $Apache::lonpublisher::metadatafields{'owner'}) {
1307: $Apache::lonpublisher::metadatafields{'owner'} =
1308: $env{'user.name'}.':'.$env{'user.domain'};
1309: }
1310: if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none') {
1311:
1312: if ($file_type eq 'portfolio') {
1313: $r->print(&mt('Associated with course [_1]',
1314: '<strong><a href="'.$uri.'?changecourse=true">'.
1315: $env{$Apache::lonpublisher::metadatafields{'courserestricted'}.
1316: ".description"}.
1317: '</a></strong>').'<br />');
1318: } else {
1319: $r->print(&mt('Associated with course [_1]',
1320: '<strong>'.
1321: $env{$Apache::lonpublisher::metadatafields{'courserestricted'}.
1322: ".description"}.'</strong>').'<br />');
1323: }
1324: } else {
1325: $r->print('<a href="'.$uri.'?changecourse=true">This resource is not associated with a course.</a><br />');
1326: }
1327: }
1328: if (@added_order) {
1329: foreach my $field_name (@added_order) {
1330: push(@fields,$field_name);
1331: $lt{$field_name} = $$added_metadata_fields{$field_name};
1332: }
1333: } else {
1334: foreach my $field_name (keys(%$added_metadata_fields)) {
1335: push(@fields,$field_name);
1336: $lt{$field_name} = $$added_metadata_fields{$field_name};
1337: }
1338: }
1339: $output .= &Apache::loncommon::start_data_table();
1340: my $row_alt = 1;
1341: foreach my $field_name (@fields) {
1342: if (defined($env{'form.new_'.$field_name})) {
1343: my @values = &Apache::loncommon::get_env_multiple('form.new_'.$field_name);
1344: my $newvalue = '';
1345: foreach my $item (@values) {
1346: if ($item ne '') {
1347: $newvalue .= $item.',';
1348: }
1349: }
1350: $newvalue =~ s/,$//;
1351: $Apache::lonpublisher::metadatafields{$field_name}=$newvalue;
1352: }
1353: if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none'
1354: && exists($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'})) {
1355: # handle restrictions here
1356: if ((($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'} =~ m/active/) ||
1357: ($field_name eq 'courserestricted'))&&
1358: (!($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'} =~ m/deleted/))){
1359:
1360: $output .= "\n".&Apache::loncommon::start_data_table_row();
1361: $output .= ('<td><span class="LC_metadata">'.$lt{$field_name}.':</span></td><td> '.
1362: &prettyinput($field_name,
1363: $Apache::lonpublisher::metadatafields{$field_name},
1364: 'new_'.$field_name,'defaultmeta',
1365: undef,undef,undef,undef,
1366: $Apache::lonpublisher::metadatafields{'courserestricted'}).'</td>');
1367: $output .= &Apache::loncommon::end_data_table_row();
1368: }
1369: } else {
1370:
1371: $output.=(&Apache::loncommon::start_data_table_row().'<td><span class="LC_metadata">'.$lt{$field_name}.':</span></td><td> '.
1372: &prettyinput($field_name,
1373: $Apache::lonpublisher::metadatafields{$field_name},
1374: 'new_'.$field_name,'defaultmeta').'</td>'.&Apache::loncommon::end_data_table_row());
1375:
1376: }
1377: }
1378: $output .= &Apache::loncommon::end_data_table();
1379: if ($env{'form.store'}) {
1380: my ($outcome,$result) = &store_metadata($fn,$uri,'store');
1381: $r->print($result);
1382: }
1383: $r->print($output.'<br /><input type="submit" name="store" value="'.
1384: &mt('Store Catalog Information').'" />');
1385:
1386: if ($file_type eq 'portfolio' || $file_type eq 'groups') {
1387: my ($port_path,$group) = &get_port_path_and_group($uri);
1388: if ($group ne '') {
1389: $r->print('<input type="hidden" name="group" value="'.$group.'" />');
1390: }
1391: $r->print('<input type="hidden" name="currentpath" value="'.$env{'form.currentpath'}.'" />');
1392: $r->print('</form><br /><br /><form method="post" action="'.$port_path.'">');
1393: if ($group ne '') {
1394: $r->print('<input type="hidden" name="group" value="'.$group.'" />');
1395: }
1396: $r->print('<input type="hidden" name="currentpath" value="'.$path.'" />'.
1397: '<input type="submit" name="cancel" value="'.&mt('Discard Edits and Return to Portfolio').'" />');
1398: }
1399: }
1400:
1401: $r->print('</form>');
1402:
1403: return;
1404: }
1405:
1406: sub store_metadata {
1407: my ($fn,$uri,$caller) = @_;
1408: my $mfh;
1409: my $formname='store';
1410: my ($file_content,$output,$outcome);
1411: if (&Apache::loncommon::get_env_multiple('form.new_keywords')) {
1412: $Apache::lonpublisher::metadatafields{'keywords'} =
1413: join (',', &Apache::loncommon::get_env_multiple('form.new_keywords'));
1414: }
1415: foreach my $field (sort(keys(%Apache::lonpublisher::metadatafields))) {
1416: next if ($field =~ /\./);
1417: my $unikey=$field;
1418: $unikey=~/^([A-Za-z_]+)/;
1419: my $tag=$1;
1420: $tag=~tr/A-Z/a-z/;
1421: $file_content.= "\n\<$tag";
1422: foreach my $key (split(/\,/,$Apache::lonpublisher::metadatakeys{$unikey})) {
1423: my $value = $Apache::lonpublisher::metadatafields{$unikey.'.'.$key};
1424: $value=~s/\"/\'\'/g;
1425: $file_content.=' '.$key.'="'.$value.'"' ;
1426: }
1427: $file_content.= '>'.
1428: &HTML::Entities::encode
1429: ($Apache::lonpublisher::metadatafields{$unikey},'<>&"').
1430: '</'.$tag.'>';
1431: }
1432: if ($fn =~ m|^$Apache::lonnet::perlvar{'lonDocRoot'}/userfiles|) {
1433: my ($path, $new_fn);
1434: if ($fn =~ m|$match_name/groups/\w+/portfolio/|) {
1435: ($path, $new_fn) = ($fn =~ m|/(groups/\w+/portfolio.*)/([^/]*)$|);
1436: } else {
1437: ($path, $new_fn) = ($fn =~ m|/(portfolio.*)/([^/]*)$|);
1438: }
1439: ($outcome,my $result) =
1440: &store_portfolio_metadata($formname,$file_content,
1441: $path,$new_fn,$uri,$caller);
1442: $output .= $result;
1443: } else {
1444: if (! ($mfh=Apache::File->new('>'.$fn))) {
1445: $output .= '<p><font color="red">';
1446: if ($caller eq 'transfer') {
1447: $output .= &mt('Could not transfer data in added fields to notes');
1448: } else {
1449: $output .= &mt('Could not write metadata');
1450: }
1451: $output .= ', '.&mt('FAIL').'</font></p>';
1452: $outcome = 'fail';
1453: } else {
1454: print $mfh ($file_content);
1455: close($mfh);
1456: &update_metadata_table($uri);
1457: $output .= '<p><font color="blue">';
1458: if ($caller eq 'transfer') {
1459: $output .= &mt('Transferred data in added fields to notes');
1460: } else {
1461: $output .= &mt('Wrote Metadata');
1462: }
1463: $output .= ' '.&Apache::lonlocal::locallocaltime(time).
1464: '</font></p>';
1465: $outcome = 'ok';
1466: }
1467: }
1468: return ($outcome,$output);
1469: }
1470:
1471: sub store_transferred_addedfields {
1472: my ($fn,$uri,$transfers) = @_;
1473: foreach my $item (@{$transfers}) {
1474: $Apache::lonpublisher::metadatafields{'notes'} .=
1475: ' '.$item.' = '.$Apache::lonpublisher::metadatafields{$item};
1476: }
1477: my ($outcome,$output) = &store_metadata($fn,$uri,'transfer');
1478: if ($outcome eq 'ok') {
1479: foreach my $item (@{$transfers}) {
1480: delete($Apache::lonpublisher::metadatafields{$item});
1481: }
1482: }
1483: }
1484:
1485: sub store_portfolio_metadata {
1486: my ($formname,$content,$path,$new_fn,$uri,$caller) = @_;
1487: my ($outcome,$output);
1488: $env{'form.'.$formname}=$content."\n";
1489: $env{'form.'.$formname.'.filename'}=$new_fn;
1490: my $result =&Apache::lonnet::userfileupload($formname,'',$path);
1491: if ($result =~ /(error|notfound)/) {
1492: $output = '<p><font color="red">';
1493: if ($caller eq 'transfer') {
1494: $output .=
1495: &mt('Could not transfer data in added fields to notes');
1496: } else {
1497: $output .= &mt('Could not write metadata');
1498: }
1499: $output .= ', '.&mt('FAIL').'</font></p>';
1500: $outcome = 'fail';
1501: } else {
1502: &update_metadata_table($uri);
1503: $output = '<p><font color="blue">';
1504: if ($caller eq 'transfer') {
1505: $output .= &mt('Transferred data in added fields to notes');
1506: } else {
1507: $output .= &mt('Wrote Metadata');
1508: }
1509: $output .= ' '.&Apache::lonlocal::locallocaltime(time).
1510: '</font></p>';
1511: $outcome = 'ok';
1512: }
1513: return ($outcome,$output);
1514: }
1515:
1516: sub update_metadata_table {
1517: my ($uri) = @_;
1518: my ($type,$udom,$uname,$file_name,$group) =
1519: &Apache::lonnet::parse_portfolio_url($uri);
1520: $file_name =~ s/\.meta$//;
1521: my $current_permissions =
1522: &Apache::lonnet::get_portfile_permissions($udom,$uname);
1523: my %access_controls =
1524: &Apache::lonnet::get_access_controls($current_permissions,$group,
1525: $file_name);
1526: my $access_hash = $access_controls{$file_name};
1527: my $available = 0;
1528: if (ref($access_hash) eq 'HASH') {
1529: foreach my $key (keys(%{$access_hash})) {
1530: my ($num,$scope,$end,$start) =
1531: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
1532: if ($scope eq 'public' || $scope eq 'guest') {
1533: $available = 1;
1534: last;
1535: }
1536: }
1537: }
1538: if ($available) {
1539: my $result =
1540: &Apache::lonnet::update_portfolio_table($uname,$udom,
1541: $file_name,'portfolio_metadata',$group,'update');
1542: }
1543: }
1544:
1545:
1546: 1;
1547: __END__
1548:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>