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