1: # The LearningOnline Network with CAPA
2: # Edit Handler for RAT Maps
3: #
4: # $Id: lonratedt.pm,v 1.66 2005/04/07 07:34:52 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:
30: package Apache::lonratedt;
31:
32: =pod
33:
34: =head1 NAME
35:
36: Apache::lonratedt: advanced resource assembly tool
37:
38: =head1 SYNOPSIS
39:
40: lonratedt provides the routines and the handler for the Advanced
41: Resource Assembly Tool (RAT), and ties the various pieces together
42: with Javascript.
43:
44: =head1 OVERVIEW
45:
46: =head2 Map Representation
47:
48: =begin latex
49:
50: %
51: \begin{figure}
52: \begin{center}\includegraphics[%
53: width=0.55\paperwidth,bb = 0 0 200 100, draft, type=eps]{Map_Example}\end{center}
54:
55:
56: \caption{\label{Map_In_Advanced_Editor}Example of a Map in the Advanced Editor}
57: \end{figure}
58: %
59: \begin{figure}
60: \begin{lyxcode}
61: <map>
62:
63: ~~<resource~id=\char`\"{}1\char`\"{}
64:
65: ~~~~src=\char`\"{}/res/msu/korte/phy231welcome.html\char`\"{}
66:
67: ~~~~type=\char`\"{}start\char`\"{}
68:
69: ~~~~title=\char`\"{}Start\char`\"{}>
70:
71: ~~~~</resource>
72:
73: ~~<resource~id=\char`\"{}2\char`\"{}
74:
75: ~~~~src=\char`\"{}\char`\"{}~type=\char`\"{}finish\char`\"{}
76:
77: ~~~~title=\char`\"{}Finish\char`\"{}>
78:
79: ~~~~</resource>
80:
81: ~~<resource~id=\char`\"{}6\char`\"{}
82:
83: ~~~~src=\char`\"{}/res/msu/korte/tests/units.problem\char`\"{}
84:
85: ~~~~type=\char`\"{}mandatory\char`\"{}
86:
87: ~~~~title=\char`\"{}Physical~Units~Test\char`\"{}>
88:
89: ~~~~</resource>
90:
91: ~~<resource~id=\char`\"{}9\char`\"{}
92:
93: ~~~~src=\char`\"{}/res/msu/korte/chapters/onedim.sequence\char`\"{}
94:
95: ~~~~title=\char`\"{}Motion~in~One~Dimension\char`\"{}>
96:
97: ~~~~</resource>
98:
99: ~~<resource~id=\char`\"{}11\char`\"{}
100:
101: ~~~~src=\char`\"{}/res/msu/bauer/bridges/units.sequence\char`\"{}
102:
103: ~~~~title=\char`\"{}Physical~Units~Refresher\char`\"{}>
104:
105: ~~~~</resource>
106:
107: ~~<condition~id=\char`\"{}19\char`\"{}
108:
109: ~~~~type=\char`\"{}stop\char`\"{}
110:
111: ~~~~value=\char`\"{}user.assessments{[}this./res/msu/korte/tests/units.problem{]}.status=solved\char`\"{}>
112:
113: ~~~~</condition>
114:
115: ~~<link~from=\char`\"{}1\char`\"{}~to=\char`\"{}6\char`\"{}></link>
116:
117: ~~<link~from=\char`\"{}6\char`\"{}~to=\char`\"{}9\char`\"{}~condition=\char`\"{}19\char`\"{}></link>
118:
119: ~~<link~from=\char`\"{}6\char`\"{}~to=\char`\"{}11\char`\"{}></link>
120:
121: ~~<link~from=\char`\"{}11\char`\"{}~to=\char`\"{}6\char`\"{}></link>
122:
123: ~~</map>
124: \end{lyxcode}
125:
126: \caption{\label{XML}XML for Map in Figure \ref{Map_In_Advanced_Editor}}
127: \end{figure}
128:
129: =end latex
130:
131: Fig. "XML for Map in Figure" shows the XML representation of the
132: resource map shown in Fig. "Example of a Map in the Advanced Editor",
133: which is the format in which maps are stored. In the figure, however,
134: additional graphical map layout information generated by the Advanced
135: Resource Assembly Tool is not displayed. This graphical information is
136: optional to re-generate the same graphical layout when the map is
137: brought up again in the Resource Assembly Tool, and is not needed for
138: any other system functionality.
139:
140: Maps can be generated by tools other than the Resource Assembly
141: Tool. In particular, an author might have some other representation of
142: a course sequence, which can be converted into a map using scripts. If
143: this map then were to be brought up in the Resource Assembly Tool, the
144: Tool would automatically generate a graphical layout for it. Each
145: entry of the map (resources, conditions and links) is stored in a
146: separate tag.
147:
148: Resources and conditionsX<conditions> have to have unique ID
149: numbers. These numbers are automatically generated by the Resource
150: Assembly Tool when the entry is first created, or added to the entries
151: when a map generated outside the Resource Assembly Tool is first
152: retrieved. They can also be assigned by custom scripts or added in by
153: hand.
154:
155: In the XML example, entry 1 is the start resource of the map. When
156: this map is accessed, the source (src) URL of this tag will be the
157: first resource rendered. Entry 2 is the finish resource of this
158: map. This resource will be the last resource in the sequence of
159: resources. Entry 6 is a problem resource with the given URL and title,
160: as well as the priority "mandatory". Entry 19 is a condition, which is
161: used by the link between entries 6, the problem, and 9, a
162: sequence. I<The final syntax for conditions has not yet been
163: determined.>
164:
165: =cut
166:
167: use strict;
168: use Apache::Constants qw(:common);
169: use Apache::lonnet;
170: use Apache::lonratsrv;
171: use Apache::lonsequence;
172: use Apache::loncommon;
173: use Apache::lonlocal;
174: use File::Copy;
175:
176: use vars qw(@order @resources @resparms);
177:
178:
179: # Mapread read maps into global arrays @links and @resources, determines status
180: # sets @order - pointer to resources in right order
181: # sets @resources - array with the resources with correct idx
182: #
183: sub mapread {
184: my $fn=shift;
185:
186: my @links;
187: undef @links;
188: undef @resources;
189: undef @order;
190: undef @resparms;
191: @resources=('');
192: @order=();
193: @resparms=();
194:
195: my ($outtext,$errtext)=&Apache::lonratsrv::loadmap($fn,'');
196: if ($errtext) { return ($errtext,2); }
197:
198: # -------------------------------------------------------------------- Read map
199: foreach (split(/\<\&\>/,$outtext)) {
200: my ($command,$number,$content)=split(/\<\:\>/,$_);
201: if ($command eq 'objcont') {
202: $resources[$number]=$content;
203: }
204: if ($command eq 'objlinks') {
205: $links[$number]=$content;
206: }
207: if ($command eq 'objparms') {
208: if ($resparms[$number]) {
209: $resparms[$number].='&&&'.$content;
210: } else {
211: $resparms[$number]=$content;
212: }
213: }
214: }
215: # ------------------------------------------------------- Is this a linear map?
216: my @starters=();
217: my @endings=();
218: undef @starters;
219: undef @endings;
220:
221: foreach (@links) {
222: if (defined($_)) {
223: my ($start,$end,$cond)=split(/\:/,$_);
224: if ((defined($starters[$start])) || (defined($endings[$end]))) {
225: return
226: (&mt('Map has branchings. Use advanced editor.'),1);
227: }
228: $starters[$start]=1;
229: $endings[$end]=1;
230: if ($cond) {
231: return
232: (&mt('Map has conditions. Use advanced editor.'),1);
233: }
234: }
235:
236: }
237: for (my $i=1; $i<=$#resources; $i++) {
238: if (defined($resources[$i])) {
239: unless (($starters[$i]) || ($endings[$i])) {
240: return
241: (&mt('Map has unconnected resources. Use advanced editor.'),1);
242: }
243: }
244: }
245: # ---------------------------------------------- Did we just read an empty map?
246: if ($#resources<1) {
247: undef $resources[0];
248: $resources[1]=':::start';
249: $resources[2]=':::finish';
250: }
251: # -------------------------------------------------- This is a linear map, sort
252:
253: my $startidx=0;
254: my $endidx=0;
255: for (my $i=0; $i<=$#resources; $i++) {
256: if (defined($resources[$i])) {
257: my ($title,$url,$ext,$type)=split(/\:/,$resources[$i]);
258: if ($type eq 'start') { $startidx=$i; }
259: if ($type eq 'finish') { $endidx=$i; }
260: }
261: }
262: my $k=0;
263: my $currentidx=$startidx;
264: $order[$k]=$currentidx;
265: for (my $i=0; $i<=$#resources; $i++) {
266: foreach (@links) {
267: my ($start,$end)=split(/\:/,$_);
268: if ($start==$currentidx) {
269: $currentidx=$end;
270: $k++;
271: $order[$k]=$currentidx;
272: last;
273: }
274: }
275: if ($currentidx==$endidx) { last; }
276: }
277: return $errtext;
278: }
279:
280: # ---------------------------------------------- Read a map as well as possible
281: # Also used by the sequence handler
282: # Call lonsequence::attemptread to read from resource space
283: #
284: sub attemptread {
285: my $fn=shift;
286:
287: my @links;
288: undef @links;
289: my @theseres;
290: undef @theseres;
291:
292: my ($outtext,$errtext)=&Apache::lonratsrv::loadmap($fn,'');
293: if ($errtext) { return @theseres }
294:
295: # -------------------------------------------------------------------- Read map
296: foreach (split(/\<\&\>/,$outtext)) {
297: my ($command,$number,$content)=split(/\<\:\>/,$_);
298: if ($command eq 'objcont') {
299: $theseres[$number]=$content;
300: }
301: if ($command eq 'objlinks') {
302: $links[$number]=$content;
303: }
304: }
305:
306: # --------------------------------------------------------------- Sort, sort of
307:
308: my @objsort=();
309: undef @objsort;
310:
311: my @data1=();
312: my @data2=();
313: undef @data1;
314: undef @data2;
315:
316: my $k;
317: my $kj;
318: my $j;
319: my $ij;
320:
321: for ($k=1;$k<=$#theseres;$k++) {
322: if (defined($theseres[$k])) {
323: $objsort[$#objsort+1]=$k;
324: }
325: }
326:
327: for ($k=1;$k<=$#links;$k++) {
328: if (defined($links[$k])) {
329: @data1=split(/\:/,$links[$k]);
330: $kj=-1;
331: for (my $j=0;$j<=$#objsort;$j++) {
332: if ((split(/\:/,$objsort[$j]))[0]==$data1[0]) {
333: $kj=$j;
334: }
335: }
336: if ($kj!=-1) { $objsort[$kj].=':'.$data1[1]; }
337: }
338: }
339: for ($k=0;$k<=$#objsort;$k++) {
340: for ($j=0;$j<=$#objsort;$j++) {
341: if ($k!=$j) {
342: @data1=split(/\:/,$objsort[$k]);
343: @data2=split(/\:/,$objsort[$j]);
344: my $dol=$#data1+1;
345: my $dtl=$#data2+1;
346: if ($dol+$dtl<1000) {
347: for ($kj=1;$kj<$dol;$kj++) {
348: if ($data1[$kj]==$data2[0]) {
349: for ($ij=1;$ij<$dtl;$ij++) {
350: $data1[$#data1+1]=$data2[$ij];
351: }
352: }
353: }
354: for ($kj=1;$kj<$dtl;$kj++) {
355: if ($data2[$kj]==$data1[0]) {
356: for ($ij=1;$ij<$dol;$ij++) {
357: $data2[$#data2+1]=$data1[$ij];
358: }
359: }
360: }
361: $objsort[$k]=join(':',@data1);
362: $objsort[$j]=join(':',@data2);
363: }
364: }
365: }
366: }
367: # ---------------------------------------------------------------- Now sort out
368:
369: @objsort=sort {
370: my @data1=split(/\:/,$a);
371: my @data2=split(/\:/,$b);
372: my $rvalue=0;
373: my $k;
374: for ($k=1;$k<=$#data1;$k++) {
375: if ($data1[$k]==$data2[0]) { $rvalue--; }
376: }
377: for ($k=1;$k<=$#data2;$k++) {
378: if ($data2[$k]==$data1[0]) { $rvalue++; }
379: }
380: if ($rvalue==0) { $rvalue=$#data2-$#data1; }
381: $rvalue;
382: } @objsort;
383:
384: my @outres=();
385: undef @outres;
386:
387: for ($k=0;$k<=$#objsort;$k++) {
388: $outres[$k]=$theseres[(split(/\:/,$objsort[$k]))[0]];
389: }
390: return @outres;
391: }
392:
393: # --------------------------------------------------------- Build up RAT screen
394: sub ratedt {
395: my ($r,$url)=@_;
396: my $frameset = '<frameset rows="1,70,*" border="0">';
397: if ($env{'environment.remote'} eq 'off') {
398: $frameset = '<frameset rows="1,250,*" border="0">';
399: }
400: $r->print(<<ENDDOCUMENT);
401:
402: <html>
403: <head>
404: <script language="JavaScript">
405: var flag=0;
406: </script>
407: </head>
408: $frameset
409: <frame name=server src="$url/loadonly/ratserver" noresize noscroll>
410: <frame name=code src="$url/loadonly/adveditmenu">
411: <frame name=mapout src="/adm/rat/map.html">
412: </frameset>
413: </html>
414:
415: ENDDOCUMENT
416: }
417:
418: # ---------------------------------------------------------------- Make buttons
419:
420: sub buttons {
421: my $adv=shift;
422: my $output='<form method=post>';
423: if ($adv==1) {
424: $output.='<input type=submit name=forceadv value="'.&mt('Edit').'">'.
425: &Apache::loncommon::help_open_topic('Sequence_Advanced_Editor_Creation');;
426: } else {
427: unless ($adv==2) {
428: $output.='<input type=submit name=forcesmp value="'.&mt('Simple Edit').'">'.
429: &Apache::loncommon::help_open_topic('Sequence_Simple_Editor_Creation');
430: }
431: $output.='<input type=submit name=forceadv value="'.&mt('Advanced Edit').'">'.
432: &Apache::loncommon::help_open_topic('Sequence_Advanced_Editor_Creation');
433: }
434: return $output.'</form><hr>';
435: }
436:
437: # ----------------------------------------------------------- Paste into target
438: # modifies @order, @resources
439:
440: sub pastetarget {
441: my ($after,@which)=@_;
442: my @insertorder=();
443: foreach (@which) {
444: if (defined($_)) {
445: my ($name,$url)=split(/\=/,$_);
446: $name=&Apache::lonnet::unescape($name);
447: $url=&Apache::lonnet::unescape($url);
448: if ($url) {
449: my $idx=$#resources+1;
450: $insertorder[$#insertorder+1]=$idx;
451: my $ext='false';
452: if ($url=~/^http\:\/\//) { $ext='true'; }
453: $url=~s/\:/\:/g;
454: $name=~s/\:/\:/g;
455: $resources[$idx]=$name.':'.$url.':'.$ext.':normal:res';
456: }
457: }
458: }
459: my @oldorder=splice(@order,$after);
460: @order=(@order,@insertorder,@oldorder);
461: }
462:
463: # ------------------------------------------------ Get start and finish correct
464: # modifies @resources
465:
466: sub startfinish {
467: # Remove all start and finish
468: foreach (@order) {
469: my ($name,$url,$ext)=split(/\:/,$resources[$_]);
470: if ($url=~/http\&colon\:\/\//) { $ext='true'; }
471: $resources[$_]=$name.':'.$url.':'.$ext.':normal:res';
472: }
473: # Garbage collection
474: my $stillchange=1;
475: while (($#order>1) && ($stillchange)) {
476: $stillchange=0;
477: for (my $i=0;$i<=$#order;$i++) {
478: my ($name,$url,$ext)=split(/\:/,$resources[$order[$i]]);
479: unless ($url) {
480: # Take out empty resource
481: for (my $j=$i+1;$j<=$#order;$j++) {
482: $order[$j-1]=$order[$j];
483: }
484: $#order--;
485: $stillchange=1;
486: last;
487: }
488: }
489: }
490: # Put in a start resource
491: my ($name,$url,$ext)=split(/\:/,$resources[$order[0]]);
492: $resources[$order[0]]=$name.':'.$url.':'.$ext.':start:res';
493: # Make sure this has at least start and finish
494: if ($#order==0) {
495: $resources[$#resources+1]='::false';
496: $order[1]=$#resources;
497: }
498: # Make the last one a finish resource
499: ($name,$url,$ext)=split(/\:/,$resources[$order[$#order]]);
500: $resources[$order[$#order]]=$name.':'.$url.':'.$ext.':finish:res';
501: }
502:
503: # ------------------------------------------------------------------- Store map
504:
505: sub storemap {
506: my $realfn=shift;
507: my $fn=$realfn;
508: # unless this is forced to work from the original file, use a temporary file
509: # instead
510: unless (shift) {
511: $fn=$realfn.'.tmp';
512: unless (-e $fn) {
513: copy($realfn,$fn);
514: }
515: }
516: # store data either into tmp or real file
517: &startfinish();
518: my $output='graphdef<:>no';
519: my $k=1;
520: for (my $i=0; $i<=$#order; $i++) {
521: if (defined($resources[$order[$i]])) {
522: $output.='<&>objcont<:>'.$order[$i].'<:>'.$resources[$order[$i]];
523: }
524: if (defined($resparms[$order[$i]])) {
525: foreach (split('&&&',$resparms[$order[$i]])) {
526: if ($_) {
527: $output.='<&>objparms<:>'.$order[$i].'<:>'.$_;
528: }
529: }
530: }
531: if (defined($order[$i+1])) {
532: if (defined($resources[$order[$i+1]])) {
533: $output.='<&>objlinks<:>'.$k.'<:>'.
534: $order[$i].':'.$order[$i+1].':0';
535: $k++;
536: }
537: }
538: }
539: $output=~s/http\&colon\;\/\///g;
540: $env{'form.output'}=$output;
541: return
542: &Apache::lonratsrv::loadmap($fn,&Apache::lonratsrv::savemap($fn,''));
543: }
544:
545: # ------------------------------------------ Store and get parameters in global
546:
547: sub storeparameter {
548: my ($to,$name,$value,$ptype)=@_;
549: my $newentry='';
550: my $nametype='';
551: foreach (split('&&&',$resparms[$to])) {
552: my ($thistype,$thisname,$thisvalue)=split('___',$_);
553: if ($thisname) {
554: unless ($thisname eq $name) {
555: $newentry.=$_.'&&&';
556: } else {
557: $nametype=$thistype;
558: }
559: }
560: }
561: unless ($ptype) { $ptype=$nametype; }
562: unless ($ptype) { $ptype='string'; }
563: $newentry.=$ptype.'___'.$name.'___'.$value;
564: $resparms[$to]=$newentry;
565: }
566:
567: sub delparameter {
568: my ($to,$name)=@_;
569: my $newentry='';
570: my $nametype='';
571: foreach (split('&&&',$resparms[$to])) {
572: my ($thistype,$thisname,$thisvalue)=split('___',$_);
573: if ($thisname) {
574: unless ($thisname eq $name) {
575: $newentry.=$_.'&&&';
576: }
577: }
578: }
579: $resparms[$to]=$newentry;
580: }
581:
582: sub getparameter {
583: my ($to,$name)=@_;
584: my $value=undef;
585: my $ptype=undef;
586: foreach (split('&&&',$resparms[$to])) {
587: my ($thistype,$thisname,$thisvalue)=split('___',$_);
588: if ($thisname eq $name) {
589: $value=$thisvalue;
590: $ptype=$thistype;
591: }
592: }
593: return ($value,$ptype);
594: }
595:
596: # ----------------------------------------------------------------- Edit script
597: sub editscript {
598: my $mode=shift;
599: my $resurl=&Apache::loncommon::lastresurl();
600: return(<<ENDSCRIPT);
601: var srch;
602: var srchflag=-1; // 1 means currently open
603: // 0 means closed (but has been open)
604: // -1 means never yet opened/defined
605: var srchmode='';
606:
607: var idx;
608: var idxflag=-1; // 1 means currently open
609: // 0 means closed (but has been open)
610: // -1 means never yet opened/defined
611: var idxmode='';
612:
613: // ------------------------------------------------------ Clears indexer window
614: function idxclear() {
615: idx.document.clear();
616: }
617:
618: // ------------------------------------------------------- Clears search window
619: function srchclear() {
620: srch.document.clear();
621: }
622:
623: // ------------------------------------------------------ Closes indexer window
624: function idxclose() {
625: if (idx && !idx.closed) {
626: idxflag=0;
627: idx.close();
628: }
629: }
630:
631: // ------------------------------------------------------- Closes search window
632: function srchclose() {
633: if (srch && !srch.closed) {
634: srchflag=0;
635: srch.close();
636: }
637: }
638:
639: // -------------------------------------------------------- Open indexer window
640: function idxopen(mode) {
641: var options="scrollbars=1,resizable=1,menubar=0";
642: idxmode=mode;
643: idxflag=1;
644: idx=open("$resurl/?launch=1&mode=$mode&catalogmode="+mode,"idxout",options);
645: idx.focus();
646: }
647:
648: // --------------------------------------------------------- Open search window
649: function srchopen(mode) {
650: var options="scrollbars=1,resizable=1,menubar=0";
651: srchmode=mode;
652: srchflag=1;
653: srch=open("/adm/searchcat?launch=1&mode=$mode&catalogmode="+mode,"srchout",options);
654: srch.focus();
655: }
656: // ----------------------------------------------------- launch indexer browser
657: function groupsearch() {
658: srchcheck('groupsearch');
659: }
660:
661: function groupimport() {
662: idxcheck('groupimport');
663: }
664: // ------------------------------------------------------- Do srch status check
665: function srchcheck(mode) {
666: if (!srch || srch.closed || srchmode!=mode) {
667: srchopen(mode);
668: }
669: srch.focus();
670: }
671:
672: // -------------------------------------------------------- Do idx status check
673: function idxcheck(mode) {
674: if (!idx || idx.closed || idxmode!=mode) {
675: idxopen(mode);
676: }
677: idx.focus();
678: }
679:
680:
681: var editbrowser;
682: function openbrowser(formname,elementname,only,omit) {
683: var url = '$resurl/?';
684: if (editbrowser == null) {
685: url += 'launch=1&';
686: }
687: url += 'catalogmode=interactive&';
688: url += 'mode=edit&';
689: url += 'form=' + formname + '&';
690: if (only != null) {
691: url += 'only=' + only + '&';
692: }
693: if (omit != null) {
694: url += 'omit=' + omit + '&';
695: }
696: url += 'element=' + elementname + '';
697: var title = 'Browser';
698: var options = 'scrollbars=1,resizable=1,menubar=0';
699: options += ',width=700,height=600';
700: editbrowser = open(url,title,options,'1');
701: editbrowser.focus();
702: }
703: ENDSCRIPT
704: }
705: # ------------------------------------------------------- Simple edit processor
706:
707: sub smpedt {
708: my ($r,$url,$errtext)=@_;
709: my $buttons=&buttons(2);
710: my $tmpfn=&Apache::lonnet::filelocation('',$url).'.tmp';
711: my $targetmsg='';
712: if ($env{'form.save'}) {
713: copy($tmpfn,&Apache::lonnet::filelocation('',$url));
714: unlink($tmpfn);
715: my ($errtext,$fatal)=
716: &mapread(&Apache::lonnet::filelocation('',$url),'');
717: unless ($fatal) {
718: $targetmsg='<b>'.&mt('Saved.').'</b><br />';
719: } else {
720: $targetmsg='<b>'.&mt('An error occured while saving.').'</b><br />';
721: }
722: }
723: if ($env{'form.revert'}) {
724: $targetmsg='<b>'.&mt('Reverted.').'</b><br />';
725: unlink($tmpfn);
726: my ($errtext,$fatal)=
727: &mapread(&Apache::lonnet::filelocation('',$url),'');
728: }
729: if (-e $tmpfn) {
730: $targetmsg=
731: '<b><font color="red">'.&mt('You are working with an unsaved version of your map.').'</font></b><br>';
732: my ($errtext,$fatal)=&mapread($tmpfn,'');
733: }
734: # ---------------------------------------------------------- Process form input
735:
736: my @importselect=&Apache::loncommon::get_env_multiple('form.importsel');
737: my @targetselect=&Apache::loncommon::get_env_multiple('form.target');
738: # ============================================================ Process commands
739:
740: my $targetdetail=$env{'form.targetdetail'};
741: my $importdetail=$env{'form.curimpdetail'};
742:
743: # ---------------------------------------------------- Importing from groupsort
744: if (($env{'form.importdetail'}) && (!$env{'form.impfortarget'})) {
745:
746: $importdetail='';
747: my @curimport=split(/\&/,$env{'form.curimpdetail'});
748:
749: my $lastsel;
750:
751: if (defined($importselect[-1])) {
752: $lastsel=$importselect[-1];
753: } else {
754: $lastsel=$#curimport;
755: }
756:
757: for (my $i=0;$i<=$lastsel;$i++) {
758: my ($name,$url)=split(/\=/,$curimport[$i]);
759: if ($url) {
760: $importdetail.='&'.$name.'='.$url;
761: }
762: }
763:
764: $importdetail.='&'.$env{'form.importdetail'};
765:
766: for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
767: my ($name,$url)=split(/\=/,$curimport[$i]);
768: if ($url) {
769: $importdetail.='&'.$name.'='.$url;
770: }
771: }
772: $importdetail=~s/\&+/\&/g;
773: $importdetail=~s/^\&//;
774:
775: # ------------------------------------------------------------------- Clear all
776: } elsif ($env{'form.clear'}) {
777: $importdetail='';
778: # ------------------------------------------------------------ Discard selected
779: } elsif ($env{'form.discard'}) {
780: $importdetail='';
781: my @curimport=split(/\&/,$env{'form.curimpdetail'});
782: foreach (@importselect) {
783: $curimport[$_]='';
784: }
785: for (my $i=0;$i<=$#curimport;$i++) {
786: my ($name,$url)=split(/\=/,$curimport[$i]);
787: if ($url) {
788: $importdetail.='&'.$name.'='.$url;
789: }
790: }
791: # --------------------------------------------------------- Loading another map
792: } elsif ($env{'form.loadmap'}) {
793: $importdetail='';
794: my @curimport=split(/\&/,$env{'form.curimpdetail'});
795:
796: my $lastsel;
797:
798: if (defined($importselect[-1])) {
799: $lastsel=$importselect[-1];
800: } else {
801: $lastsel=$#curimport;
802: }
803:
804: for (my $i=0;$i<=$lastsel;$i++) {
805: my ($name,$url)=split(/\=/,$curimport[$i]);
806: if ($url) {
807: $importdetail.='&'.$name.'='.$url;
808: }
809: }
810:
811: foreach (
812: &Apache::lonsequence::attemptread(&Apache::lonnet::filelocation('',$env{'form.importmap'}))) {
813: my ($name,$url)=split(/\:/,$_);
814: if ($url) {
815: $importdetail.='&'.&Apache::lonnet::escape($name).'='.
816: &Apache::lonnet::escape($url);
817: }
818: }
819:
820: for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
821: my ($name,$url)=split(/\=/,$curimport[$i]);
822: if ($url) {
823: $importdetail.='&'.$name.'='.$url;
824: }
825: }
826: $importdetail=~s/\&+/\&/g;
827: $importdetail=~s/^\&//;
828:
829: # ------------------------------------------------ Groupimport/search to target
830: } elsif ($env{'form.importdetail'}) {
831: my $lastsel;
832: if (defined($targetselect[-1])) {
833: $lastsel=$targetselect[-1];
834: } else {
835: $lastsel=$#order+1;
836: }
837: &pastetarget($lastsel,split(/\&/,$env{'form.importdetail'}));
838: &storemap(&Apache::lonnet::filelocation('',$url));
839: # ------------------------------------------------------------------------- Cut
840: } elsif (($env{'form.cut'}) || ($env{'form.copy'})) {
841: $importdetail='';
842: my @curimport=split(/\&/,$env{'form.curimpdetail'});
843:
844: my $lastsel;
845:
846: if (defined($importselect[-1])) {
847: $lastsel=$importselect[-1];
848: } else {
849: $lastsel=$#curimport;
850: }
851:
852: for (my $i=0;$i<=$lastsel;$i++) {
853: my ($name,$url)=split(/\=/,$curimport[$i]);
854: if ($url) {
855: $importdetail.='&'.$name.'='.$url;
856: }
857: }
858:
859: foreach (@targetselect) {
860: my ($name,$url)=split(/\:/,$resources[$order[$_-1]]);
861: if ($url) {
862: $importdetail.='&'.&Apache::lonnet::escape($name).'='.
863: &Apache::lonnet::escape($url);
864: }
865: }
866:
867: for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
868: my ($name,$url)=split(/\=/,$curimport[$i]);
869: if ($url) {
870: $importdetail.='&'.$name.'='.$url;
871: }
872: }
873: $importdetail=~s/\&+/\&/g;
874: $importdetail=~s/^\&//;
875:
876: if ($env{'form.cut'}) {
877: my @neworder=();
878: for (my $i=0;$i<=$#order;$i++) {
879: my $include=1;
880: foreach (@targetselect) {
881: if ($_-1==$i) { $include=0; }
882: }
883: if ($include) { $neworder[$#neworder+1]=$order[$i]; }
884: }
885: @order=@neworder;
886: &storemap(&Apache::lonnet::filelocation('',$url));
887: }
888:
889: # ----------------------------------------------------------------------- Paste
890: } elsif ($env{'form.paste'}) {
891: my $lastsel;
892: if (defined($targetselect[-1])) {
893: $lastsel=$targetselect[-1];
894: } else {
895: $lastsel=$#order+1;
896: }
897: my @newsequence;
898: my @curimport=split(/\&/,$env{'form.curimpdetail'});
899: foreach (@importselect) {
900: $newsequence[$#newsequence+1]=$curimport[$_];
901: }
902: &pastetarget($lastsel,@newsequence);
903: &storemap(&Apache::lonnet::filelocation('',$url));
904: # -------------------------------------------------------------------- Move up
905: } elsif ($env{'form.moveup'}) {
906: foreach (sort @targetselect) {
907: if ($_-1>0) {
908: my $movethis=$order[$_-1];
909: $order[$_-1]=$order[$_-2];
910: $order[$_-2]=$movethis;
911: }
912: }
913: &storemap(&Apache::lonnet::filelocation('',$url));
914: # ------------------------------------------------------------------ Move down
915: } elsif ($env{'form.movedown'}) {
916: foreach (reverse sort @targetselect) {
917: if ($_-1<$#order) {
918: my $movethis=$order[$_-1];
919: $order[$_-1]=$order[$_];
920: $order[$_]=$movethis;
921: }
922: }
923: &storemap(&Apache::lonnet::filelocation('',$url));
924: # --------------------------------------------------------------------- Rename
925: } elsif ($env{'form.renameres'}) {
926: my $residx=$Apache::lonratedt::order[$env{'form.renameidx'}-1];
927: my ($name,@resrest)=split(/\:/,$Apache::lonratedt::resources[$residx]);
928: $name=$env{'form.renametitle'};
929: $name=~s/\:/\&colon\;/g;
930: $Apache::lonratedt::resources[$residx]=$name.':'.join(':',@resrest);
931: &storemap(&Apache::lonnet::filelocation('',$url));
932: }
933: # ------------------------------------------------------------ Assemble windows
934:
935: my $idx=-1;
936: $importdetail='&'.$importdetail;
937: $importdetail=~s/^\&+/\&/;
938: my $importwindow=
939: '<option value="-1"> ---- '.&mt('Import and Paste Area').' ---- </option>'.
940: join("\n",map {
941: $idx++;
942: if ($_) {
943: my ($name,$url)=split(/\=/,$_);
944: unless ($name) { $name=(split(/\//,$url))[-1]; }
945: unless ($name) { $name='EMPTY'; }
946: '<option value="'.$idx.'">'.&Apache::lonnet::unescape($name).
947: '</option>';
948: }
949: } split(/\&/,$importdetail));
950:
951: $idx=0;
952: $targetdetail='';
953: my $targetwindow=
954: '<option value="0"> ------- '.&mt('Target Edit Map').' ------- </option>'.
955: join("\n",map {
956: my ($name,$url)=split(/\:/,$resources[$_]);
957: unless ($name) { $name=(split(/\//,$url))[-1]; }
958: unless ($name) { $name='EMPTY'; }
959: $targetdetail.='&'.&Apache::lonnet::escape($name).'='.
960: &Apache::lonnet::escape($url);
961: $idx++;
962: $name=~s/\:/\:/g;
963: '<option value="'.$idx.'">'.$name.'</option>';
964: } @order);
965:
966: # ----------------------------------------------------- Start simple RAT screen
967: my $editscript=&editscript('simple');
968: my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,
969: ($env{'environment.remote'} ne 'off')).
970: &Apache::loncommon::help_open_menu('','Sequence_Simple_Editor_Creation','Sequence_Simple_Editor_Creation','',6,'RAT');
971: my %lt=&Apache::lonlocal::texthash(
972: 'sa' => 'Save',
973: 'nt' => 'New Title',
974: 'se' => 'Search',
975: 'im' => 'Import',
976: 'vi' => 'View',
977: 'lm' => 'Load Map',
978: 'ds' => 'Discard Selected',
979: 'ca' => 'Clear All',
980: 'ta' => 'Temporary Assembly Workspace',
981: 'rv' => 'Revert to Last Saved',
982: 'sa' => 'Save',
983: 'mu' => 'Move Up',
984: 'md' => 'Move Down',
985: 're' => 'Rename',
986: 'as' => 'after selected',
987: 'cs' => 'Cut selected',
988: 'ps' => 'Copy selected',
989: 'pas' => 'Paste after selected',
990: );
991: $r->print(<<ENDSMPHEAD);
992: <html>
993: <head>
994: <script>
995:
996: $editscript
997:
998: function openview(entry) {
999: var url=unescape((entry.split('='))[1]);
1000: var parts=new Array;
1001: parts=url.split(':');
1002: url=parts.join(':');
1003: if (url) { open(url,'cat'); }
1004: }
1005:
1006: function viewtarget() {
1007: openview((document.forms.simpleedit.targetdetail.value.split('&'))
1008: [document.forms.simpleedit.target.selectedIndex]);
1009: }
1010:
1011: function viewimport() {
1012: openview((document.forms.simpleedit.curimpdetail.value.split('&'))
1013: [document.forms.simpleedit.importsel.selectedIndex]);
1014: }
1015:
1016: function renametarget() {
1017: var selidx=document.forms.simpleedit.target.selectedIndex;
1018: var entry=(document.forms.simpleedit.targetdetail.value.split('&'))
1019: [selidx];
1020: var oldname=unescape((entry.split('='))[0]);
1021: var nameparts=oldname.split(':');
1022: oldname=unescape(nameparts.join(':'));
1023: nameparts=oldname.split('"');
1024: oldname=unescape(nameparts.join('"'));
1025: nameparts=oldname.split(''');
1026: oldname=unescape(nameparts.join("'"));
1027: newtitle=prompt('$lt{'nt'}',oldname);
1028: if (newtitle) {
1029: document.forms.simpleedit.renameres.value=1;
1030: document.forms.simpleedit.renameidx.value=selidx;
1031: document.forms.simpleedit.renametitle.value=newtitle;
1032: document.forms.simpleedit.submit();
1033: }
1034: }
1035:
1036: </script>
1037: </head>
1038: $bodytag
1039: $buttons
1040: <font color=red>$errtext</font>
1041: <form name=simpleedit method=post>
1042: <input type=hidden name=forcesmp value=1>
1043: <input type=hidden name=renameres value=0>
1044: <input type=hidden name=renametitle value=''>
1045: <input type=hidden name=renameidx value=0>
1046: <table>
1047: <tr><th width="40%">$lt{'ta'}</th>
1048: <th> </th>
1049: <th width="40%">File: $url</th></tr>
1050: <tr><td bgcolor="#FFFFCC">
1051: <input type=button onClick="javascript:groupsearch()" value="$lt{'se'}">
1052: <input type=button onClick="javascript:groupimport();" value="$lt{'im'}">
1053: $lt{'as'}
1054: <hr>
1055: <input type=text size=20 name=importmap>
1056: <input type=button
1057: onClick="javascript:openbrowser('simpleedit','importmap','sequence,page','')"
1058: value="Select Map"><input type=submit name=loadmap value="$lt{'lm'}"><hr>
1059: <input type=submit name="discard" value="$lt{'ds'}">
1060: <input type=submit name="clear" value="$lt{'ca'}">
1061: <input type=button onClick="javascript:viewimport()" value="$lt{'vi'}">
1062:
1063: </td><td> </td><td bgcolor="#FFFFCC">
1064:
1065: <input type=button onClick=
1066: "javascript:impfortarget.value=1;groupsearch()" value="$lt{'se'}">
1067: <input type=button onClick=
1068: "javascript:impfortarget.value=1;groupimport();" value="$lt{'im'}">
1069: $lt{'as'}
1070: <hr>
1071: <input type=submit name="moveup" value="$lt{'mu'}">
1072: <input type=submit name="movedown" value="$lt{'md'}">
1073: <input type=button onClick="javascript:renametarget()" value="$lt{'re'}">
1074: <hr>$targetmsg
1075: <input type=submit name="revert" value="$lt{'rv'}">
1076: <input type=submit name="save" value="$lt{'sa'}">
1077: <input type=button onClick="javascript:viewtarget()" value="$lt{'vi'}">
1078: </td></tr>
1079:
1080: <tr><td bgcolor="#FFFFCC"><select name="importsel" size=10 multiple>
1081: $importwindow
1082: </select>
1083: </td>
1084: <td bgcolor="#FFFFAA" align="center">
1085: $lt{'cs'}<br>
1086: <input type=submit name=cut value='<<<'><p>
1087: <hr>
1088: $lt{'ps'}<br>
1089: <input type=submit name=copy value='<--'><p>
1090: <hr>
1091: $lt{'pas'}<br>
1092: <input type=submit name=paste value='-->'>
1093: </td>
1094: <td bgcolor="#FFFFCC"><select name="target" size=10 multiple>
1095: $targetwindow
1096: </select>
1097: </table>
1098: <input type=hidden name=importdetail value="">
1099: <input type=hidden name=curimpdetail value="$importdetail">
1100: <input type=hidden name=targetdetail value="$targetdetail">
1101: <input type=hidden name=impfortarget value="0">
1102: </form>
1103: </body></html>
1104: ENDSMPHEAD
1105: }
1106:
1107: # ----------------------------------------------------------------- No such dir
1108: sub nodir {
1109: my ($r,$dir)=@_;
1110: $dir=~s/^\/home\/\w+\/public\_html//;
1111: $r->print(<<ENDNODIR);
1112: <html>
1113: <body bgcolor='#FFFFFF'>
1114: <h1>No such directory: $dir</h1>
1115: </body>
1116: </html>
1117: ENDNODIR
1118: }
1119:
1120: # ---------------------------------------------------------------- View Handler
1121:
1122: sub viewmap {
1123: my ($r,$url,$adv,$errtext)=@_;
1124: $r->print('<html>'.
1125: &Apache::loncommon::bodytag('Edit Content of a Map').
1126: &Apache::loncommon::help_open_menu('','','','',6,'RAT').
1127: &buttons($adv));
1128: if ($errtext) {
1129: $r->print($errtext.'<hr />');
1130: }
1131: my $idx=0;
1132: $r->print('<h1>'.$url.'</h1>');
1133: if ($adv) {
1134: $r->print('<p><b><font color="red">'.&mt('Map contents are not shown in order.').'</font></b></p><br />');
1135: }
1136: $r->print( '<table border="0" cellspacing="2" cellpadding="4">'.
1137: '<tr bgcolor="#DDDDDD"><th>'.&mt('Type').'</th>'.
1138: '<th>'.&mt('Title in map').'</th>'.
1139: '<th>'.&mt('Filename of resource').'</th>'.
1140: '<th>'.&mt('Link to published resource').'</th>'.
1141: '<th>'.&mt('Link to resource in Construction Space').'</th>'.
1142: '</tr>');
1143: my @backgroundColors = ("#FFFFFF", "#F6F6F6");
1144: foreach (&attemptread(&Apache::lonnet::filelocation('',$url))) {
1145: if (defined($_)) {
1146: $idx++;
1147: my ($title,$url,$cond)=split(/\:/,$_);
1148: if ($cond eq 'cond') { next; }
1149: $title=~s/\&colon\;/\:/g;
1150: $url=~s/\&colon\;/\:/g;
1151: unless ($title) { $title=(split(/\//,$url))[-1] };
1152: unless ($title) { $title='<i>'.&mt('Empty').'</i>'; }
1153: my $resurl = &Apache::lonratsrv::qtescape($url);
1154: my $resfilepath = $Apache::lonnet::perlvar{'lonDocRoot'}.$resurl;
1155: my $filename;
1156: if ($resurl =~ m#/([^/]+)$#) {
1157: $filename = $1;
1158: }
1159: my $cstrurl = $resurl;
1160: $cstrurl =~ s#^/res/[^/]+/([^/]+)/#/priv/$1/#;
1161: my $bgcol = $idx%2;
1162: $r->print('<tr bgcolor='.$backgroundColors[$bgcol].'><td>'.
1163: '<img src="'.&Apache::loncommon::icon($resfilepath).
1164: '" /></td><td>'.&Apache::lonratsrv::qtescape($title).
1165: '</td><td>'.$filename.'</td><td>');
1166: if ($url) {
1167: if (-e $resfilepath) {
1168: $r->print('<a href="'.$resurl.'">'.&mt('Resource space').'</a>');
1169: } else {
1170: $r->print(&mt('unpublished'));
1171: }
1172: }
1173: $r->print('</td><td>');
1174: if ($url) {
1175: $r->print('<a href="'.$cstrurl.'">'.
1176: &mt('Construction space').'</a>');
1177: }
1178: $r->print('</td></tr>'."\n");
1179: }
1180: }
1181: $r->print('</table>');
1182: $r->print('</body></html>');
1183: }
1184:
1185: # ================================================================ Main Handler
1186:
1187: sub handler {
1188: my $r=shift;
1189: &Apache::loncommon::content_type($r,'text/html');
1190: $r->send_http_header;
1191:
1192: return OK if $r->header_only;
1193: my $target = $env{'form.grade_target'};
1194: if ($target eq 'meta') {
1195: &Apache::loncommon::content_type($r,'text/html');
1196: $r->send_http_header;
1197: return OK;
1198: }
1199:
1200: my $url=$r->uri;
1201: my $fn=&Apache::lonnet::filelocation('',$url);
1202:
1203: my ($dir)=($fn=~/^(.+)\/[^\/]+$/);
1204: unless (-e $dir) {
1205: &nodir($r,$dir);
1206: return OK;
1207: }
1208:
1209: # ------------------------------------------- Determine which tools can be used
1210: my $adv=0;
1211:
1212: unless ($env{'form.forcesmp'}) {
1213: if ($env{'form.forceadv'}) {
1214: $adv=1;
1215: } elsif (my $fh=Apache::File->new($fn)) {
1216: my $allmap=join('',<$fh>);
1217: $adv=($allmap=~/\<map[^\>]+mode\s*\=\s*(\'|\")rat/is);
1218: }
1219: }
1220:
1221: my $errtext='';
1222: my $fatal=0;
1223:
1224: # -------------------------------------------------------------------- Load map
1225: ($errtext,$fatal)=&mapread($fn,$errtext);
1226:
1227: if ($fatal==1) { $adv=1; }
1228:
1229: # ----------------------------------- adv==1 now means "graphical MUST be used"
1230:
1231: if ($env{'form.forceadv'}) {
1232: &ratedt($r,$url);
1233: } elsif ($env{'form.forcesmp'}) {
1234: &smpedt($r,$url,$errtext);
1235: } else {
1236: &viewmap($r,$url,$adv,$errtext);
1237: }
1238: return OK;
1239: }
1240:
1241: 1;
1242: __END__
1243:
1244:
1245:
1246:
1247:
1248:
1249:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>