1: # The LearningOnline Network with CAPA
2: # Metadata display handler
3: #
4: # $Id: lonmeta.pm,v 1.144 2005/12/19 21:17:25 albertel Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27:
28:
29: package Apache::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:
43:
44: ############################################################
45: ############################################################
46: ##
47: ## &get_dynamic_metadata_from_sql($url)
48: ##
49: ## Queries sql database for dynamic metdata
50: ## Returns a hash of hashes, with keys of urls which match $url
51: ## Returned fields are given below.
52: ##
53: ## Examples:
54: ##
55: ## %DynamicMetadata = &Apache::lonmeta::get_dynmaic_metadata_from_sql
56: ## ('/res/msu/korte/');
57: ##
58: ## $DynamicMetadata{'/res/msu/korte/example.problem'}->{$field}
59: ##
60: ############################################################
61: ############################################################
62: sub get_dynamic_metadata_from_sql {
63: my ($url) = shift();
64: my ($authordom,$author)=($url=~m:^/res/(\w+)/(\w+)/:);
65: if (! defined($authordom)) {
66: $authordom = shift();
67: }
68: if (! defined($author)) {
69: $author = shift();
70: }
71: if (! defined($authordom) || ! defined($author)) {
72: return ();
73: }
74: my @Fields = ('url','count','course',
75: 'goto','goto_list',
76: 'comefrom','comefrom_list',
77: 'sequsage','sequsage_list',
78: 'stdno','stdno_list',
79: 'dependencies',
80: 'avetries','avetries_list',
81: 'difficulty','difficulty_list',
82: 'disc','disc_list',
83: 'clear','technical','correct',
84: 'helpful','depth');
85: #
86: my $query = 'SELECT '.join(',',@Fields).
87: ' FROM metadata WHERE url LIKE "'.$url.'%"';
88: my $server = &Apache::lonnet::homeserver($author,$authordom);
89: my $reply = &Apache::lonnet::metadata_query($query,undef,undef,
90: ,[$server]);
91: return () if (! defined($reply) || ref($reply) ne 'HASH');
92: my $filename = $reply->{$server};
93: if (! defined($filename) || $filename =~ /^error/) {
94: return ();
95: }
96: my $max_time = time + 10; # wait 10 seconds for results at most
97: my %ReturnHash;
98: #
99: # Look for results
100: my $finished = 0;
101: while (! $finished && time < $max_time) {
102: my $datafile=$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename;
103: if (! -e "$datafile.end") { next; }
104: my $fh;
105: if (!($fh=Apache::File->new($datafile))) { next; }
106: while (my $result = <$fh>) {
107: chomp($result);
108: next if (! $result);
109: my @Data =
110: map {
111: &Apache::lonnet::unescape($_);
112: } split(',',$result);
113: my $url = $Data[0];
114: for (my $i=0;$i<=$#Fields;$i++) {
115: $ReturnHash{$url}->{$Fields[$i]}=$Data[$i];
116: }
117: }
118: $finished = 1;
119: }
120: #
121: return %ReturnHash;
122: }
123:
124:
125: # Fetch and evaluate dynamic metadata
126: sub dynamicmeta {
127: my $url=&Apache::lonnet::declutter(shift);
128: $url=~s/\.meta$//;
129: my ($adomain,$aauthor)=($url=~/^(\w+)\/(\w+)\//);
130: my $regexp=$url;
131: $regexp=~s/(\W)/\\$1/g;
132: $regexp='___'.$regexp.'___';
133: my %evaldata=&Apache::lonnet::dump('nohist_resevaldata',$adomain,
134: $aauthor,$regexp);
135: my %DynamicData = &LONCAPA::lonmetadata::process_reseval_data(\%evaldata);
136: my %Data = &LONCAPA::lonmetadata::process_dynamic_metadata($url,
137: \%DynamicData);
138: #
139: # Deal with 'count' separately
140: $Data{'count'} = &access_count($url,$aauthor,$adomain);
141: #
142: # Debugging code I will probably need later
143: if (0) {
144: &Apache::lonnet::logthis('Dynamic Metadata');
145: while(my($k,$v)=each(%Data)){
146: &Apache::lonnet::logthis(' "'.$k.'"=>"'.$v.'"');
147: }
148: &Apache::lonnet::logthis('-------------------');
149: }
150: return %Data;
151: }
152:
153: sub access_count {
154: my ($src,$author,$adomain) = @_;
155: my %countdata=&Apache::lonnet::dump('nohist_accesscount',$adomain,
156: $author,$src);
157: if (! exists($countdata{$src})) {
158: return &mt('Not Available');
159: } else {
160: return $countdata{$src};
161: }
162: }
163:
164: # Try to make an alt tag if there is none
165: sub alttag {
166: my ($base,$src)=@_;
167: my $fullpath=&Apache::lonnet::hreflocation($base,$src);
168: my $alttag=&Apache::lonnet::metadata($fullpath,'title').' '.
169: &Apache::lonnet::metadata($fullpath,'subject').' '.
170: &Apache::lonnet::metadata($fullpath,'abstract');
171: $alttag=~s/\s+/ /gs;
172: $alttag=~s/\"//gs;
173: $alttag=~s/\'//gs;
174: $alttag=~s/\s+$//gs;
175: $alttag=~s/^\s+//gs;
176: if ($alttag) {
177: return $alttag;
178: } else {
179: return &mt('No information available');
180: }
181: }
182:
183: # Author display
184: sub authordisplay {
185: my ($aname,$adom)=@_;
186: return &Apache::loncommon::aboutmewrapper
187: (&Apache::loncommon::plainname($aname,$adom),
188: $aname,$adom,'preview').' <tt>['.$aname.'@'.$adom.']</tt>';
189: }
190:
191: # Pretty display
192: sub evalgraph {
193: my $value=shift;
194: if (! $value) {
195: return '';
196: }
197: my $val=int($value*10.+0.5)-10;
198: my $output='<table border="0" cellpadding="0" cellspacing="0"><tr>';
199: if ($val>=20) {
200: $output.='<td width="20" bgcolor="#555555">  </td>';
201: } else {
202: $output.='<td width="'.($val).'" bgcolor="#555555"> </td>'.
203: '<td width="'.(20-$val).'" bgcolor="#FF3333"> </td>';
204: }
205: $output.='<td bgcolor="#FFFF33"> </td>';
206: if ($val>20) {
207: $output.='<td width="'.($val-20).'" bgcolor="#33FF33"> </td>'.
208: '<td width="'.(40-$val).'" bgcolor="#555555"> </td>';
209: } else {
210: $output.='<td width="20" bgcolor="#555555">  </td>';
211: }
212: $output.='<td> ('.sprintf("%5.2f",$value).') </td></tr></table>';
213: return $output;
214: }
215:
216: sub diffgraph {
217: my $value=shift;
218: if (! $value) {
219: return '';
220: }
221: my $val=int(40.0*$value+0.5);
222: my @colors=('#FF9933','#EEAA33','#DDBB33','#CCCC33',
223: '#BBDD33','#CCCC33','#DDBB33','#EEAA33');
224: my $output='<table border="0" cellpadding="0" cellspacing="0"><tr>';
225: for (my $i=0;$i<8;$i++) {
226: if ($val>$i*5) {
227: $output.='<td width="5" bgcolor="'.$colors[$i].'"> </td>';
228: } else {
229: $output.='<td width="5" bgcolor="#555555"> </td>';
230: }
231: }
232: $output.='<td> ('.sprintf("%3.2f",$value).') </td></tr></table>';
233: return $output;
234: }
235:
236:
237: # The field names
238: sub fieldnames {
239: my $file_type=shift;
240: my %fields =
241: ('title' => 'Title',
242: 'author' =>'Author(s)',
243: 'authorspace' => 'Author Space',
244: 'modifyinguser' => 'Last Modifying User',
245: 'subject' => 'Subject',
246: 'standards' => 'Standards',
247: 'keywords' => 'Keyword(s)',
248: 'notes' => 'Notes',
249: 'abstract' => 'Abstract',
250: 'lowestgradelevel' => 'Lowest Grade Level',
251: 'highestgradelevel' => 'Highest Grade Level',
252: 'courserestricted' => 'Course Restricting Metadata');
253:
254: if (! defined($file_type) || $file_type ne 'portfolio') {
255: %fields =
256: (%fields,
257: 'domain' => 'Domain',
258: 'mime' => 'MIME Type',
259: 'language' => 'Language',
260: 'creationdate' => 'Creation Date',
261: 'lastrevisiondate' => 'Last Revision Date',
262: 'owner' => 'Publisher/Owner',
263: 'copyright' => 'Copyright/Distribution',
264: 'customdistributionfile' => 'Custom Distribution File',
265: 'sourceavail' => 'Source Available',
266: 'sourcerights' => 'Source Custom Distribution File',
267: 'obsolete' => 'Obsolete',
268: 'obsoletereplacement' => 'Suggested Replacement for Obsolete File',
269: 'count' => 'Network-wide number of accesses (hits)',
270: 'course' => 'Network-wide number of courses using resource',
271: 'course_list' => 'Network-wide courses using resource',
272: 'sequsage' => 'Number of resources using or importing resource',
273: 'sequsage_list' => 'Resources using or importing resource',
274: 'goto' => 'Number of resources that follow this resource in maps',
275: 'goto_list' => 'Resources that follow this resource in maps',
276: 'comefrom' => 'Number of resources that lead up to this resource in maps',
277: 'comefrom_list' => 'Resources that lead up to this resource in maps',
278: 'clear' => 'Material presented in clear way',
279: 'depth' => 'Material covered with sufficient depth',
280: 'helpful' => 'Material is helpful',
281: 'correct' => 'Material appears to be correct',
282: 'technical' => 'Resource is technically correct',
283: 'avetries' => 'Average number of tries till solved',
284: 'stdno' => 'Total number of students who have worked on this problem',
285: 'difficulty' => 'Degree of difficulty',
286: 'disc' => 'Degree of discrimination',
287: 'dependencies' => 'Resources used by this resource',
288: );
289: }
290: return &Apache::lonlocal::texthash(%fields);
291: }
292:
293: sub portfolio_display_uri {
294: my ($uri)=@_;
295: $uri =~ s|.*/(portfolio/.*)$|$1|;
296: my ($res_uri,$meta_uri) = ($uri,$uri);
297:
298: if ($uri =~ /\.meta$/) {
299: $res_uri =~ s/\.meta//;
300: } else {
301: $meta_uri .= '.meta';
302: }
303: return ($res_uri,$meta_uri);
304: }
305:
306: sub pre_select_course {
307: my ($r,$uri) = @_;
308: my $output;
309: my $fn=&Apache::lonnet::filelocation('',$uri);
310: my ($res_uri,$meta_uri) = &portfolio_display_uri($uri);
311: %Apache::lonpublisher::metadatafields=();
312: %Apache::lonpublisher::metadatakeys=();
313: my $result=&Apache::lonnet::getfile($fn);
314: if ($result == -1){
315: $r->print(&mt('Creating new file [_1]'),$meta_uri);
316: } else {
317: &Apache::lonpublisher::metaeval($result);
318: }
319: $r->print('<hr /><form method="post" action="" >');
320: $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>');
321: $output = &select_course();
322: $r->print($output.'<br /><input type="submit" name="store" value="'.
323: &mt('Associate Resource With Selected Course').'">');
324: $r->print('</form>');
325: return;
326: }
327: sub select_course {
328: my %courses;
329: my $output;
330: my $selected;
331: foreach my $key (keys (%env)) {
332: if ($key =~ m/\.metadata\./) {
333: $key =~ m/^course\.(.+)(\.metadata.+$)/;
334: my $course = $1;
335: my $coursekey = 'course.'.$course.'.description';
336: my $value = $env{$coursekey};
337: $courses{$coursekey} = $value;
338: }
339: }
340: &Apache::lonnet::logthis('the restricted is'.$Apache::lonpublisher::metadatafields{'courserestricted'});
341: my $meta_not_found = 1;
342: if ($Apache::lonpublisher::metadatafields{'courserestricted'} eq 'none') {
343: $selected = ' SELECTED ';
344: } else {
345: $selected = '';
346: }
347: $output .= '<select name="new_courserestricted" >';
348: $output .= '<option value="none" '.$selected.'>None</option>';
349: foreach my $key (keys (%courses)) {
350: $key =~ m/(^.+)\.description$/;
351: if ($Apache::lonpublisher::metadatafields{'courserestricted'} eq $1) {
352: $selected = ' SELECTED ';
353: } else {
354: $selected = '';
355: }
356: $output .= '<option value="'.$1.'"'.$selected.'>';
357: $output .= $courses{$key};
358: $output .= '</option>';
359: }
360: $output .= '</select><br />';
361: return ($output);
362: }
363: # Pretty printing of metadata field
364:
365: sub prettyprint {
366: my ($type,$value,$target,$prefix,$form,$noformat)=@_;
367: # $target,$prefix,$form are optional and for filecrumbs only
368: if (! defined($value)) {
369: return ' ';
370: }
371: # Title
372: if ($type eq 'title') {
373: return '<font size="+1" face="arial">'.$value.'</font>';
374: }
375: # Dates
376: if (($type eq 'creationdate') ||
377: ($type eq 'lastrevisiondate')) {
378: return ($value?&Apache::lonlocal::locallocaltime(
379: &Apache::lonmysql::unsqltime($value)):
380: &mt('not available'));
381: }
382: # Language
383: if ($type eq 'language') {
384: return &Apache::loncommon::languagedescription($value);
385: }
386: # Copyright
387: if ($type eq 'copyright') {
388: return &Apache::loncommon::copyrightdescription($value);
389: }
390: # Copyright
391: if ($type eq 'sourceavail') {
392: return &Apache::loncommon::source_copyrightdescription($value);
393: }
394: # MIME
395: if ($type eq 'mime') {
396: return '<img src="'.&Apache::loncommon::icon($value).'" /> '.
397: &Apache::loncommon::filedescription($value);
398: }
399: # Person
400: if (($type eq 'author') ||
401: ($type eq 'owner') ||
402: ($type eq 'modifyinguser') ||
403: ($type eq 'authorspace')) {
404: $value=~s/(\w+)(\:|\@)(\w+)/&authordisplay($1,$3)/gse;
405: return $value;
406: }
407: # Gradelevel
408: if (($type eq 'lowestgradelevel') ||
409: ($type eq 'highestgradelevel')) {
410: return &Apache::loncommon::gradeleveldescription($value);
411: }
412: # Only for advance users below
413: if (! $env{'user.adv'}) {
414: return '<i>- '.&mt('not displayed').' -</i>';
415: }
416: # File
417: if (($type eq 'customdistributionfile') ||
418: ($type eq 'obsoletereplacement') ||
419: ($type eq 'goto_list') ||
420: ($type eq 'comefrom_list') ||
421: ($type eq 'sequsage_list') ||
422: ($type eq 'dependencies')) {
423: return '<ul><font size="-1">'.join("\n",map {
424: my $url = &Apache::lonnet::clutter($_);
425: my $title = &Apache::lonnet::gettitle($url);
426: if ($title eq '') {
427: $title = 'Untitled';
428: if ($url =~ /\.sequence$/) {
429: $title .= ' Sequence';
430: } elsif ($url =~ /\.page$/) {
431: $title .= ' Page';
432: } elsif ($url =~ /\.problem$/) {
433: $title .= ' Problem';
434: } elsif ($url =~ /\.html$/) {
435: $title .= ' HTML document';
436: } elsif ($url =~ m:/syllabus$:) {
437: $title .= ' Syllabus';
438: }
439: }
440: $_ = '<li>'.$title.' '.
441: &Apache::lonhtmlcommon::crumbs($url,$target,$prefix,$form,'-1',$noformat).
442: '</li>'
443: } split(/\s*\,\s*/,$value)).'</ul></font>';
444: }
445: # Evaluations
446: if (($type eq 'clear') ||
447: ($type eq 'depth') ||
448: ($type eq 'helpful') ||
449: ($type eq 'correct') ||
450: ($type eq 'technical')) {
451: return &evalgraph($value);
452: }
453: # Difficulty
454: if ($type eq 'difficulty' || $type eq 'disc') {
455: return &diffgraph($value);
456: }
457: # List of courses
458: if ($type=~/\_list/) {
459: my @Courses = split(/\s*\,\s*/,$value);
460: my $Str;
461: foreach my $course (@Courses) {
462: my %courseinfo = &Apache::lonnet::coursedescription($course);
463: if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
464: next;
465: }
466: if ($Str ne '') { $Str .= '<br />'; }
467: $Str .= '<a href="/public/'.$courseinfo{'domain'}.'/'.
468: $courseinfo{'num'}.'/syllabus" target="preview">'.
469: $courseinfo{'description'}.'</a>';
470: }
471: return $Str;
472: }
473: # No pretty print found
474: return $value;
475: }
476:
477: # Pretty input of metadata field
478: sub direct {
479: return shift;
480: }
481:
482: sub selectbox {
483: my ($name,$value,$functionref,@idlist)=@_;
484: if (! defined($functionref)) {
485: $functionref=\&direct;
486: }
487: my $selout='<select name="'.$name.'">';
488: foreach (@idlist) {
489: $selout.='<option value=\''.$_.'\'';
490: if ($_ eq $value) {
491: $selout.=' selected>'.&{$functionref}($_).'</option>';
492: }
493: else {$selout.='>'.&{$functionref}($_).'</option>';}
494: }
495: return $selout.'</select>';
496: }
497:
498: sub relatedfield {
499: my ($show,$relatedsearchflag,$relatedsep,$fieldname,$relatedvalue)=@_;
500: if (! $relatedsearchflag) {
501: return '';
502: }
503: if (! defined($relatedsep)) {
504: $relatedsep=' ';
505: }
506: if (! $show) {
507: return $relatedsep.' ';
508: }
509: return $relatedsep.'<input type="checkbox" name="'.$fieldname.'_related"'.
510: ($relatedvalue?' checked="1"':'').' />';
511: }
512:
513: sub prettyinput {
514: my ($type,$value,$fieldname,$formname,
515: $relatedsearchflag,$relatedsep,$relatedvalue,$size,$course_key)=@_;
516: if (! defined($size)) {
517: $size = 80;
518: }
519: my $output;
520: if (defined($course_key)) {
521: my $stu_add;
522: my $only_one;
523: my %meta_options;
524: my @cur_values_inst;
525: my $cur_values_stu;
526: my $values = $env{$course_key.'.metadata.'.$type.'.values'};
527: if ($env{$course_key.'.metadata.'.$type.'.options'} =~ m/stuadd/) {
528: $stu_add = 'true';
529: }
530: if ($env{$course_key.'.metadata.'.$type.'.options'} =~ m/onlyone/) {
531: $only_one = 'true';
532: }
533: # need to take instructor values out of list where instructor and student
534: # values may be mixed.
535: if ($values) {
536: foreach my $item (split(/,/,$values)) {
537: $item =~ s/^\s+//;
538: $meta_options{$item} = $item;
539: }
540: foreach my $item (split(/,/,$value)) {
541: $item =~ s/^\s+//;
542: if ($meta_options{$item}) {
543: push(@cur_values_inst,$item);
544: } else {
545: $cur_values_stu .= $item.',';
546: }
547: }
548: } else {
549: $cur_values_stu = $value;
550: }
551: if ($type eq 'courserestricted') {
552: return (&select_course());
553: # return ('<input type="hidden" name="new_courserestricted" value="'.$course_key.'" />');
554: }
555: if (($type eq 'keywords') || ($type eq 'subject')
556: || ($type eq 'author')||($type eq 'notes')
557: || ($type eq 'abstract')|| ($type eq 'title')|| ($type eq 'standards')) {
558: if ($values) {
559: if ($only_one) {
560: $output .= (&Apache::loncommon::select_form($cur_values_inst[0],'new_'.$type,%meta_options));
561: } else {
562: $output .= (&Apache::loncommon::multiple_select_form('new_'.$type,\@cur_values_inst,undef,\%meta_options));
563: }
564: }
565: if ($stu_add) {
566: $output .= '<input type="text" name="'.$fieldname.'" size="'.$size.'" '.
567: 'value="'.$cur_values_stu.'" />'.
568: &relatedfield(1,$relatedsearchflag,$relatedsep,$fieldname,
569: $relatedvalue);
570: }
571: return ($output);
572: }
573: if (($type eq 'lowestgradelevel') ||
574: ($type eq 'highestgradelevel')) {
575: return &Apache::loncommon::select_level_form($value,$fieldname).
576: &relatedfield(0,$relatedsearchflag,$relatedsep);
577: }
578: return();
579: }
580: # Language
581: if ($type eq 'language') {
582: return &selectbox($fieldname,
583: $value,
584: \&Apache::loncommon::languagedescription,
585: (&Apache::loncommon::languageids)).
586: &relatedfield(0,$relatedsearchflag,$relatedsep);
587: }
588: # Copyright
589: if ($type eq 'copyright') {
590: return &selectbox($fieldname,
591: $value,
592: \&Apache::loncommon::copyrightdescription,
593: (&Apache::loncommon::copyrightids)).
594: &relatedfield(0,$relatedsearchflag,$relatedsep);
595: }
596: # Source Copyright
597: if ($type eq 'sourceavail') {
598: return &selectbox($fieldname,
599: $value,
600: \&Apache::loncommon::source_copyrightdescription,
601: (&Apache::loncommon::source_copyrightids)).
602: &relatedfield(0,$relatedsearchflag,$relatedsep);
603: }
604: # Gradelevels
605: if (($type eq 'lowestgradelevel') ||
606: ($type eq 'highestgradelevel')) {
607: return &Apache::loncommon::select_level_form($value,$fieldname).
608: &relatedfield(0,$relatedsearchflag,$relatedsep);
609: }
610: # Obsolete
611: if ($type eq 'obsolete') {
612: return '<input type="checkbox" name="'.$fieldname.'"'.
613: ($value?' checked="1"':'').' />'.
614: &relatedfield(0,$relatedsearchflag,$relatedsep);
615: }
616: # Obsolete replacement file
617: if ($type eq 'obsoletereplacement') {
618: return '<input type="text" name="'.$fieldname.
619: '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
620: "('".$formname."','".$fieldname."'".
621: ",'')\">".&mt('Select').'</a>'.
622: &relatedfield(0,$relatedsearchflag,$relatedsep);
623: }
624: # Customdistribution file
625: if ($type eq 'customdistributionfile') {
626: return '<input type="text" name="'.$fieldname.
627: '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
628: "('".$formname."','".$fieldname."'".
629: ",'rights')\">".&mt('Select').'</a>'.
630: &relatedfield(0,$relatedsearchflag,$relatedsep);
631: }
632: # Source Customdistribution file
633: if ($type eq 'sourcerights') {
634: return '<input type="text" name="'.$fieldname.
635: '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
636: "('".$formname."','".$fieldname."'".
637: ",'rights')\">".&mt('Select').'</a>'.
638: &relatedfield(0,$relatedsearchflag,$relatedsep);
639: }
640: if ($type eq 'courserestricted') {
641: return (&select_course());
642: #return ('<input type="hidden" name="new_courserestricted" value="'.$course_key.'" />');
643: }
644:
645: # Dates
646: if (($type eq 'creationdate') ||
647: ($type eq 'lastrevisiondate')) {
648: return
649: &Apache::lonhtmlcommon::date_setter($formname,$fieldname,$value).
650: &relatedfield(0,$relatedsearchflag,$relatedsep);
651: }
652: # No pretty input found
653: $value=~s/^\s+//gs;
654: $value=~s/\s+$//gs;
655: $value=~s/\s+/ /gs;
656: $value=~s/\"/\"\;/gs;
657: return
658: '<input type="text" name="'.$fieldname.'" size="'.$size.'" '.
659: 'value="'.$value.'" />'.
660: &relatedfield(1,$relatedsearchflag,$relatedsep,$fieldname,
661: $relatedvalue);
662: }
663:
664: # Main Handler
665: sub handler {
666: my $r=shift;
667: #
668: my $uri=$r->uri;
669: #
670: # Set document type
671: &Apache::loncommon::content_type($r,'text/html');
672: $r->send_http_header;
673: return OK if $r->header_only;
674: #
675: my ($resdomain,$resuser)=
676: (&Apache::lonnet::declutter($uri)=~/^(\w+)\/(\w+)\//);
677: my $html=&Apache::lonxml::xmlbegin();
678: $r->print($html.'<head><title>'.
679: 'Catalog Information'.
680: '</title></head>');
681: if ($uri=~m:/adm/bombs/(.*)$:) {
682: $r->print(&Apache::loncommon::bodytag('Error Messages'));
683: # Looking for all bombs?
684: &report_bombs($r,$uri);
685: } elsif ($uri=~/\/portfolio\//) {
686: ($resdomain,$resuser)=
687: (&Apache::lonnet::declutter($uri)=~m|^(\w+)/(\w+)/portfolio|);
688: $r->print(&Apache::loncommon::bodytag
689: ('Edit Portfolio File Information','','','',$resdomain));
690: if ($env{'form.store'}) {
691: &present_editable_metadata($r,$uri,'portfolio');
692: } else {
693: &pre_select_course($r,$uri);
694: }
695: } elsif ($uri=~/^\/\~/) {
696: # Construction space
697: $r->print(&Apache::loncommon::bodytag
698: ('Edit Catalog Information','','','',$resdomain));
699: &present_editable_metadata($r,$uri);
700: } else {
701: $r->print(&Apache::loncommon::bodytag
702: ('Catalog Information','','','',$resdomain));
703: &present_uneditable_metadata($r,$uri);
704: }
705: $r->print('</body></html>');
706: return OK;
707: }
708:
709: #####################################################
710: #####################################################
711: ### ###
712: ### Report Bombs ###
713: ### ###
714: #####################################################
715: #####################################################
716: sub report_bombs {
717: my ($r,$uri) = @_;
718: # Set document type
719: $uri =~ s:/adm/bombs/::;
720: $uri = &Apache::lonnet::declutter($uri);
721: $r->print('<h1>'.&Apache::lonnet::clutter($uri).'</h1>');
722: my ($domain,$author)=($uri=~/^(\w+)\/(\w+)\//);
723: if (&Apache::loncacc::constructaccess('/~'.$author.'/',$domain)) {
724: if ($env{'form.clearbombs'}) {
725: &Apache::lonmsg::clear_author_res_msg($uri);
726: }
727: my $clear=&mt('Clear all Messages in Subdirectory');
728: $r->print(<<ENDCLEAR);
729: <form method="post">
730: <input type="submit" name="clearbombs" value="$clear" />
731: </form>
732: ENDCLEAR
733: my %brokenurls =
734: &Apache::lonmsg::all_url_author_res_msg($author,$domain);
735: foreach (sort(keys(%brokenurls))) {
736: if ($_=~/^\Q$uri\E/) {
737: $r->print
738: ('<a href="'.&Apache::lonnet::clutter($_).'">'.$_.'</a>'.
739: &Apache::lonmsg::retrieve_author_res_msg($_).
740: '<hr />');
741: }
742: }
743: } else {
744: $r->print(&mt('Not authorized'));
745: }
746: return;
747: }
748:
749: #####################################################
750: #####################################################
751: ### ###
752: ### Uneditable Metadata Display ###
753: ### ###
754: #####################################################
755: #####################################################
756: sub present_uneditable_metadata {
757: my ($r,$uri) = @_;
758: #
759: my %content=();
760: # Read file
761: foreach (split(/\,/,&Apache::lonnet::metadata($uri,'keys'))) {
762: $content{$_}=&Apache::lonnet::metadata($uri,$_);
763: }
764: # Render Output
765: # displayed url
766: my ($thisversion)=($uri=~/\.(\d+)\.(\w+)\.meta$/);
767: $uri=~s/\.meta$//;
768: my $disuri=&Apache::lonnet::clutter($uri);
769: # version
770: my $currentversion=&Apache::lonnet::getversion($disuri);
771: my $versiondisplay='';
772: if ($thisversion) {
773: $versiondisplay=&mt('Version').': '.$thisversion.
774: ' ('.&mt('most recent version').': '.
775: ($currentversion>0 ?
776: $currentversion :
777: &mt('information not available')).')';
778: } else {
779: $versiondisplay='Version: '.$currentversion;
780: }
781: # crumbify displayed URL uri target prefix form size
782: $disuri=&Apache::lonhtmlcommon::crumbs($disuri,undef, undef, undef,'+1');
783: $disuri =~ s:<br />::g;
784: # obsolete
785: my $obsolete=$content{'obsolete'};
786: my $obsoletewarning='';
787: if (($obsolete) && ($env{'user.adv'})) {
788: $obsoletewarning='<p><font color="red">'.
789: &mt('This resource has been marked obsolete by the author(s)').
790: '</font></p>';
791: }
792: #
793: my %lt=&fieldnames();
794: my $table='';
795: my $title = $content{'title'};
796: if (! defined($title)) {
797: $title = 'Untitled Resource';
798: }
799: foreach ('title',
800: 'author',
801: 'subject',
802: 'keywords',
803: 'notes',
804: 'abstract',
805: 'lowestgradelevel',
806: 'highestgradelevel',
807: 'standards',
808: 'mime',
809: 'language',
810: 'creationdate',
811: 'lastrevisiondate',
812: 'owner',
813: 'copyright',
814: 'customdistributionfile',
815: 'sourceavail',
816: 'sourcerights',
817: 'obsolete',
818: 'obsoletereplacement') {
819: $table.='<tr><td bgcolor="#AAAAAA">'.$lt{$_}.
820: '</td><td bgcolor="#CCCCCC">'.
821: &prettyprint($_,$content{$_}).'</td></tr>';
822: delete $content{$_};
823: }
824: #
825: $r->print(<<ENDHEAD);
826: <h2>$title</h2>
827: <p>
828: $disuri<br />
829: $obsoletewarning
830: $versiondisplay
831: </p>
832: <table cellspacing="2" border="0">
833: $table
834: </table>
835: ENDHEAD
836: if ($env{'user.adv'}) {
837: &print_dynamic_metadata($r,$uri,\%content);
838: }
839: return;
840: }
841:
842: sub print_dynamic_metadata {
843: my ($r,$uri,$content) = @_;
844: #
845: my %content = %$content;
846: my %lt=&fieldnames();
847: #
848: my $description = 'Dynamic Metadata (updated periodically)';
849: $r->print('<h3>'.&mt($description).'</h3>'.
850: &mt('Processing'));
851: $r->rflush();
852: my %items=&fieldnames();
853: my %dynmeta=&dynamicmeta($uri);
854: #
855: # General Access and Usage Statistics
856: if (exists($dynmeta{'count'}) ||
857: exists($dynmeta{'sequsage'}) ||
858: exists($dynmeta{'comefrom'}) ||
859: exists($dynmeta{'goto'}) ||
860: exists($dynmeta{'course'})) {
861: $r->print('<h4>'.&mt('Access and Usage Statistics').'</h4>'.
862: '<table cellspacing="2" border="0">');
863: foreach ('count',
864: 'sequsage','sequsage_list',
865: 'comefrom','comefrom_list',
866: 'goto','goto_list',
867: 'course','course_list') {
868: $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
869: '<td bgcolor="#CCCCCC">'.
870: &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
871: }
872: $r->print('</table>');
873: } else {
874: $r->print('<h4>'.&mt('No Access or Usages Statistics are available for this resource.').'</h4>');
875: }
876: #
877: # Assessment statistics
878: if ($uri=~/\.(problem|exam|quiz|assess|survey|form)$/) {
879: if (exists($dynmeta{'stdno'}) ||
880: exists($dynmeta{'avetries'}) ||
881: exists($dynmeta{'difficulty'}) ||
882: exists($dynmeta{'disc'})) {
883: # This is an assessment, print assessment data
884: $r->print('<h4>'.
885: &mt('Overall Assessment Statistical Data').
886: '</h4>'.
887: '<table cellspacing="2" border="0">');
888: $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{'stdno'}.'</td>'.
889: '<td bgcolor="#CCCCCC">'.
890: &prettyprint('stdno',$dynmeta{'stdno'}).
891: '</td>'."</tr>\n");
892: foreach ('avetries','difficulty','disc') {
893: $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
894: '<td bgcolor="#CCCCCC">'.
895: &prettyprint($_,sprintf('%5.2f',$dynmeta{$_})).
896: '</td>'."</tr>\n");
897: }
898: $r->print('</table>');
899: }
900: if (exists($dynmeta{'stats'})) {
901: #
902: # New assessment statistics
903: $r->print('<h4>'.
904: &mt('Detailed Assessment Statistical Data').
905: '</h4>');
906: my $table = '<table cellspacing="2" border="0">'.
907: '<tr>'.
908: '<th>Course</th>'.
909: '<th>Section(s)</th>'.
910: '<th>Num Students</th>'.
911: '<th>Mean Tries</th>'.
912: '<th>Degree of Difficulty</th>'.
913: '<th>Degree of Discrimination</th>'.
914: '<th>Time of computation</th>'.
915: '</tr>'.$/;
916: foreach my $identifier (sort(keys(%{$dynmeta{'stats'}}))) {
917: my $data = $dynmeta{'stats'}->{$identifier};
918: my $course = $data->{'course'};
919: my %courseinfo = &Apache::lonnet::coursedescription($course);
920: if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
921: &Apache::lonnet::logthis('lookup for '.$course.' failed');
922: next;
923: }
924: $table .= '<tr>';
925: $table .=
926: '<td><nobr>'.$courseinfo{'description'}.'</nobr></td>';
927: $table .=
928: '<td align="right">'.$data->{'sections'}.'</td>';
929: $table .=
930: '<td align="right">'.$data->{'stdno'}.'</td>';
931: foreach ('avetries','difficulty','disc') {
932: $table .= '<td align="right">';
933: if (exists($data->{$_})) {
934: $table .= sprintf('%.2f',$data->{$_}).' ';
935: } else {
936: $table .= '';
937: }
938: $table .= '</td>';
939: }
940: $table .=
941: '<td><nobr>'.
942: &Apache::lonlocal::locallocaltime($data->{'timestamp'}).
943: '</nobr></td>';
944: $table .=
945: '</tr>'.$/;
946: }
947: $table .= '</table>'.$/;
948: $r->print($table);
949: } else {
950: $r->print('No new dynamic data found.');
951: }
952: } else {
953: $r->print('<h4>'.
954: &mt('No Assessment Statistical Data is available for this resource').
955: '</h4>');
956: }
957:
958: #
959: #
960: if (exists($dynmeta{'clear'}) ||
961: exists($dynmeta{'depth'}) ||
962: exists($dynmeta{'helpful'}) ||
963: exists($dynmeta{'correct'}) ||
964: exists($dynmeta{'technical'})){
965: $r->print('<h4>'.&mt('Evaluation Data').'</h4>'.
966: '<table cellspacing="2" border="0">');
967: foreach ('clear','depth','helpful','correct','technical') {
968: $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
969: '<td bgcolor="#CCCCCC">'.
970: &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
971: }
972: $r->print('</table>');
973: } else {
974: $r->print('<h4>'.&mt('No Evaluation Data is available for this resource.').'</h4>');
975: }
976: $uri=~/^\/res\/(\w+)\/(\w+)\//;
977: if ((($env{'user.domain'} eq $1) && ($env{'user.name'} eq $2))
978: || ($env{'user.role.ca./'.$1.'/'.$2})) {
979: if (exists($dynmeta{'comments'})) {
980: $r->print('<h4>'.&mt('Evaluation Comments').' ('.
981: &mt('visible to author and co-authors only').
982: ')</h4>'.
983: '<blockquote>'.$dynmeta{'comments'}.'</blockquote>');
984: } else {
985: $r->print('<h4>'.&mt('There are no Evaluation Comments on this resource.').'</h4>');
986: }
987: my $bombs = &Apache::lonmsg::retrieve_author_res_msg($uri);
988: if (defined($bombs) && $bombs ne '') {
989: $r->print('<a name="bombs" /><h4>'.&mt('Error Messages').' ('.
990: &mt('visible to author and co-authors only').')'.
991: '</h4>'.$bombs);
992: } else {
993: $r->print('<h4>'.&mt('There are currently no Error Messages for this resource.').'</h4>');
994: }
995: }
996: #
997: # All other stuff
998: $r->print('<h3>'.
999: &mt('Additional Metadata (non-standard, parameters, exports)').
1000: '</h3><table border="0" cellspacing="1">');
1001: foreach (sort(keys(%content))) {
1002: my $name=$_;
1003: if ($name!~/\.display$/) {
1004: my $display=&Apache::lonnet::metadata($uri,
1005: $name.'.display');
1006: if (! $display) {
1007: $display=$name;
1008: };
1009: my $otherinfo='';
1010: foreach ('name','part','type','default') {
1011: if (defined(&Apache::lonnet::metadata($uri,
1012: $name.'.'.$_))) {
1013: $otherinfo.=' '.$_.'='.
1014: &Apache::lonnet::metadata($uri,
1015: $name.'.'.$_).'; ';
1016: }
1017: }
1018: $r->print('<tr><td bgcolor="#bbccbb"><font size="-1" color="#556655">'.$display.'</font></td><td bgcolor="#ccddcc"><font size="-1" color="#556655">'.$content{$name});
1019: if ($otherinfo) {
1020: $r->print(' ('.$otherinfo.')');
1021: }
1022: $r->print("</font></td></tr>\n");
1023: }
1024: }
1025: $r->print("</table>");
1026: return;
1027: }
1028:
1029:
1030:
1031: #####################################################
1032: #####################################################
1033: ### ###
1034: ### Editable metadata display ###
1035: ### ###
1036: #####################################################
1037: #####################################################
1038: sub present_editable_metadata {
1039: my ($r,$uri, $file_type) = @_;
1040: # Construction Space Call
1041: # Header
1042: my $disuri=$uri;
1043: my $fn=&Apache::lonnet::filelocation('',$uri);
1044: $disuri=~s/^\/\~/\/priv\//;
1045: $disuri=~s/\.meta$//;
1046: my $meta_uri = $disuri;
1047: if ($disuri =~ m|/portfolio/|) {
1048: ($disuri, $meta_uri) = &portfolio_display_uri($disuri);
1049: }
1050: my $target=$uri;
1051: $target=~s/^\/\~/\/res\/$env{'request.role.domain'}\//;
1052: $target=~s/\.meta$//;
1053: my $bombs=&Apache::lonmsg::retrieve_author_res_msg($target);
1054: if ($bombs) {
1055: my $showdel=1;
1056: if ($env{'form.delmsg'}) {
1057: if (&Apache::lonmsg::del_url_author_res_msg($target) eq 'ok') {
1058: $bombs=&mt('Messages deleted.');
1059: $showdel=0;
1060: } else {
1061: $bombs=&mt('Error deleting messages');
1062: }
1063: }
1064: if ($env{'form.clearmsg'}) {
1065: my $cleardir=$target;
1066: $cleardir=~s/\/[^\/]+$/\//;
1067: if (&Apache::lonmsg::clear_author_res_msg($cleardir) eq 'ok') {
1068: $bombs=&mt('Messages cleared.');
1069: $showdel=0;
1070: } else {
1071: $bombs=&mt('Error clearing messages');
1072: }
1073: }
1074: my $del=&mt('Delete Messages for this Resource');
1075: my $clear=&mt('Clear all Messages in Subdirectory');
1076: my $goback=&mt('Back to Source File');
1077: $r->print(<<ENDBOMBS);
1078: <h1>$disuri</h1>
1079: <form method="post" name="defaultmeta">
1080: ENDBOMBS
1081: if ($showdel) {
1082: $r->print(<<ENDDEL);
1083: <input type="submit" name="delmsg" value="$del" />
1084: <input type="submit" name="clearmsg" value="$clear" />
1085: ENDDEL
1086: } else {
1087: $r->print('<a href="'.$disuri.'" />'.$goback.'</a>');
1088: }
1089: $r->print('<br />'.$bombs);
1090: } else {
1091: my $displayfile='Catalog Information for '.$disuri;
1092: if ($disuri=~/\/default$/) {
1093: my $dir=$disuri;
1094: $dir=~s/default$//;
1095: $displayfile=
1096: &mt('Default Cataloging Information for Directory').' '.
1097: $dir;
1098: }
1099: %Apache::lonpublisher::metadatafields=();
1100: %Apache::lonpublisher::metadatakeys=();
1101: my $result=&Apache::lonnet::getfile($fn);
1102: if ($result == -1){
1103: $r->print(&mt('Creating new file [_1]'),$meta_uri);
1104: } else {
1105: &Apache::lonpublisher::metaeval($result);
1106: }
1107: $r->print(<<ENDEDIT);
1108: <h1>$displayfile</h1>
1109: <form method="post" name="defaultmeta">
1110: ENDEDIT
1111: $r->print('<script language="JavaScript">'.
1112: &Apache::loncommon::browser_and_searcher_javascript().
1113: '</script>');
1114: my %lt=&fieldnames($file_type);
1115: my $output;
1116: my @fields;
1117: if ($file_type eq 'portfolio') {
1118: @fields = ('author','title','subject','keywords','abstract','notes','lowestgradelevel',
1119: 'highestgradelevel','standards');
1120: } else {
1121: @fields = ('author','title','subject','keywords','abstract','notes',
1122: 'copyright','customdistributionfile','language',
1123: 'standards',
1124: 'lowestgradelevel','highestgradelevel','sourceavail','sourcerights',
1125: 'obsolete','obsoletereplacement');
1126: }
1127: if ((! $Apache::lonpublisher::metadatafields{'courserestricted'}) &&
1128: (! $env{'form.new_courserestricted'})) {
1129: $Apache::lonpublisher::metadatafields{'courserestricted'}=
1130: 'none';
1131: } elsif ($env{'form.new_courserestricted'}) {
1132: $Apache::lonpublisher::metadatafields{'courserestricted'}=
1133: $env{'form.new_courserestricted'};
1134: }
1135: if (! $Apache::lonpublisher::metadatafields{'copyright'}) {
1136: $Apache::lonpublisher::metadatafields{'copyright'}=
1137: 'default';
1138: }
1139: if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none') {
1140: $r->print(&mt('Associated with course [_1]','<strong>'.$env{$Apache::lonpublisher::metadatafields{'courserestricted'}.".description"}.
1141: '</strong>').'<br />');
1142: } else {
1143: $r->print("This resource is not associated with a course.<br />");
1144: }
1145: foreach my $field_name (@fields) {
1146:
1147: if (defined($env{'form.new_'.$field_name})) {
1148: $Apache::lonpublisher::metadatafields{$field_name}=
1149: join(',',&Apache::loncommon::get_env_multiple('form.new_'.$field_name));
1150: }
1151: if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none') {
1152: # handle restrictions here
1153: if (($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'} =~ m/active/) ||
1154: ($field_name eq 'courserestricted')){
1155: $output.=("\n".'<p>'.$lt{$field_name}.': '.
1156: &prettyinput($field_name,
1157: $Apache::lonpublisher::metadatafields{$field_name},
1158: 'new_'.$field_name,'defaultmeta',
1159: undef,undef,undef,undef,
1160: $Apache::lonpublisher::metadatafields{'courserestricted'}).'</p>'."\n");
1161: }
1162: } else {
1163:
1164: $output.=('<p>'.$lt{$field_name}.': '.
1165: &prettyinput($field_name,
1166: $Apache::lonpublisher::metadatafields{$field_name},
1167: 'new_'.$field_name,'defaultmeta').'</p>');
1168:
1169: }
1170: }
1171: if ($env{'form.store'}) {
1172: my $mfh;
1173: my $formname='store';
1174: my $file_content;
1175: if (&Apache::loncommon::get_env_multiple('form.new_keywords')) {
1176: $Apache::lonpublisher::metadatafields{'keywords'} =
1177: join (',', &Apache::loncommon::get_env_multiple('form.new_keywords'));
1178: }
1179:
1180: foreach (sort keys %Apache::lonpublisher::metadatafields) {
1181: next if ($_ =~ /\./);
1182: my $unikey=$_;
1183: $unikey=~/^([A-Za-z]+)/;
1184: my $tag=$1;
1185: $tag=~tr/A-Z/a-z/;
1186: $file_content.= "\n\<$tag";
1187: foreach (split(/\,/,
1188: $Apache::lonpublisher::metadatakeys{$unikey})
1189: ) {
1190: my $value=
1191: $Apache::lonpublisher::metadatafields{$unikey.'.'.$_};
1192: $value=~s/\"/\'\'/g;
1193: $file_content.=' '.$_.'="'.$value.'"' ;
1194: # print $mfh ' '.$_.'="'.$value.'"';
1195: }
1196: $file_content.= '>'.
1197: &HTML::Entities::encode
1198: ($Apache::lonpublisher::metadatafields{$unikey},
1199: '<>&"').
1200: '</'.$tag.'>';
1201: }
1202: &Apache::lonnet::logthis(" file ".$file_content);
1203: if ($fn =~ m|/portfolio/|) {
1204: my ($path, $new_fn) = ($fn =~ m|/(portfolio.*)/([^/]*)$|);
1205: $env{'form.'.$formname}=$file_content."\n";
1206: $env{'form.'.$formname.'.filename'}=$new_fn;
1207: my $result =&Apache::lonnet::userfileupload($formname,'',
1208: $path);
1209:
1210: if ($result =~ /(error|notfound)/) {
1211: $r->print('<p><font color="red">'.
1212: &mt('Could not write metadata').', '.
1213: &mt('FAIL').'</font></p>');
1214: } else {
1215: $r->print('<p><font color="blue">'.&mt('Wrote Metadata').
1216: ' '.&Apache::lonlocal::locallocaltime(time).
1217: '</font></p>');
1218: }
1219: } else {
1220: if (! ($mfh=Apache::File->new('>'.$fn))) {
1221: $r->print('<p><font color="red">'.
1222: &mt('Could not write metadata').', '.
1223: &mt('FAIL').'</font></p>');
1224: } else {
1225: print $mfh $file_content;
1226: $r->print('<p><font color="blue">'.&mt('Wrote Metadata').
1227: ' '.&Apache::lonlocal::locallocaltime(time).
1228: '</font></p>');
1229: }
1230: }
1231: }
1232:
1233: $r->print($output.'<br /><input type="submit" name="store" value="'.
1234: &mt('Store Catalog Information').'">');
1235:
1236: }
1237: $r->print('</form>');
1238:
1239: return;
1240: }
1241:
1242: 1;
1243: __END__
1244:
1245:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>