1: # The LearningOnline Network with CAPA
2: # Edit Handler for RAT Maps
3: #
4: # $Id: lonratedt.pm,v 1.17 2002/05/15 19:50:37 www 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: # (TeX Content Handler
29: #
30: # 05/29/00,05/30 Gerd Kortemeyer)
31: # 7/1,6/30 Gerd Kortemeyer
32:
33: package Apache::lonratedt;
34:
35: use strict;
36: use Apache::Constants qw(:common);
37: use Apache::lonnet;
38: use Apache::lonratsrv;
39:
40: my @order=();
41: my @resources=();
42:
43:
44: # Mapread read maps into global arrays @links and @resources, determines status
45: # sets @order - pointer to resources in right order
46: # sets @resources - array with the resources with correct idx
47: #
48: sub mapread {
49: my $fn=shift;
50:
51: my @links;
52: undef @links;
53: undef @resources;
54: undef @order;
55:
56: my ($outtext,$errtext)=&Apache::lonratsrv::loadmap($fn,'');
57: if ($errtext) { return ($errtext,2); }
58:
59: # -------------------------------------------------------------------- Read map
60: foreach (split(/\<\&\>/,$outtext)) {
61: my ($command,$number,$content)=split(/\<\:\>/,$_);
62: if ($command eq 'objcont') {
63: $resources[$number]=$content;
64: }
65: if ($command eq 'objlinks') {
66: $links[$number]=$content;
67: }
68: }
69: # ------------------------------------------------------- Is this a linear map?
70: my @starters=();
71: my @endings=();
72: undef @starters;
73: undef @endings;
74:
75: foreach (@links) {
76: if (defined($_)) {
77: my ($start,$end,$cond)=split(/\:/,$_);
78: if ((defined($starters[$start])) || (defined($endings[$end]))) {
79: return
80: ('Map has branchings. Use advanced editor.',1);
81: }
82: $starters[$start]=1;
83: $endings[$end]=1;
84: if ($cond) {
85: return
86: ('Map has conditions. Use advanced editor.',1);
87: }
88: }
89:
90: }
91: for (my $i=0; $i<=$#resources; $i++) {
92: if (defined($resources[$i])) {
93: unless (($starters[$i]) || ($endings[$i])) {
94: return
95: ('Map has unconnected resources. Use advanced editor.',1);
96: }
97: }
98: }
99:
100: # -------------------------------------------------- This is a linear map, sort
101:
102: my $startidx=0;
103: my $endidx=0;
104: for (my $i=0; $i<=$#resources; $i++) {
105: if (defined($resources[$i])) {
106: my ($title,$url,$ext,$type)=split(/\:/,$resources[$i]);
107: if ($type eq 'start') { $startidx=$i; }
108: if ($type eq 'finish') { $endidx=$i; }
109: }
110: }
111: my $k=0;
112: my $currentidx=$startidx;
113: $order[$k]=$currentidx;
114: for (my $i=0; $i<=$#resources; $i++) {
115: foreach (@links) {
116: my ($start,$end)=split(/\:/,$_);
117: if ($start==$currentidx) {
118: $currentidx=$end;
119: $k++;
120: $order[$k]=$currentidx;
121: last;
122: }
123: }
124: if ($currentidx==$endidx) { last; }
125: }
126: return $errtext;
127: }
128:
129: # ---------------------------------------------- Read a map as well as possible
130:
131: sub attemptread {
132: my $fn=shift;
133:
134: my @links;
135: undef @links;
136: my @theseres;
137: undef @theseres;
138:
139: my ($outtext,$errtext)=&Apache::lonratsrv::loadmap($fn,'');
140: if ($errtext) { return @theseres }
141:
142: # -------------------------------------------------------------------- Read map
143: foreach (split(/\<\&\>/,$outtext)) {
144: my ($command,$number,$content)=split(/\<\:\>/,$_);
145: if ($command eq 'objcont') {
146: $theseres[$number]=$content;
147: }
148: if ($command eq 'objlinks') {
149: $links[$number]=$content;
150: }
151: }
152:
153: # --------------------------------------------------------------- Sort, sort of
154:
155: my @objsort=();
156: undef @objsort;
157:
158: my @data1=();
159: my @data2=();
160: undef @data1;
161: undef @data2;
162:
163: my $k;
164: my $kj;
165: my $j;
166: my $ij;
167:
168: for ($k=1;$k<=$#theseres;$k++) {
169: if (defined($theseres[$k])) {
170: $objsort[$#objsort+1]=$k;
171: }
172: }
173:
174: for ($k=1;$k<=$#links;$k++) {
175: if (defined($links[$k])) {
176: @data1=split(/\:/,$links[$k]);
177: $kj=-1;
178: for (my $j=0;$j<=$#objsort;$j++) {
179: if ((split(/\:/,$objsort[$j]))[0]==$data1[0]) {
180: $kj=$j;
181: }
182: }
183: if ($kj!=-1) { $objsort[$kj].=':'.$data1[1]; }
184: }
185: }
186: for ($k=0;$k<=$#objsort;$k++) {
187: for ($j=0;$j<=$#objsort;$j++) {
188: if ($k!=$j) {
189: @data1=split(/\:/,$objsort[$k]);
190: @data2=split(/\:/,$objsort[$j]);
191: my $dol=$#data1+1;
192: my $dtl=$#data2+1;
193: if ($dol+$dtl<1000) {
194: for ($kj=1;$kj<$dol;$kj++) {
195: if ($data1[$kj]==$data2[0]) {
196: for ($ij=1;$ij<$dtl;$ij++) {
197: $data1[$#data1+1]=$data2[$ij];
198: }
199: }
200: }
201: for ($kj=1;$kj<$dtl;$kj++) {
202: if ($data2[$kj]==$data1[0]) {
203: for ($ij=1;$ij<$dol;$ij++) {
204: $data2[$#data2+1]=$data1[$ij];
205: }
206: }
207: }
208: $objsort[$k]=join(':',@data1);
209: $objsort[$j]=join(':',@data2);
210: }
211: }
212: }
213: }
214: # ---------------------------------------------------------------- Now sort out
215:
216: @objsort=sort {
217: my @data1=split(/\:/,$a);
218: my @data2=split(/\:/,$b);
219: my $rvalue=0;
220: my $k;
221: for ($k=1;$k<=$#data1;$k++) {
222: if ($data1[$k]==$data2[0]) { $rvalue--; }
223: }
224: for ($k=1;$k<=$#data2;$k++) {
225: if ($data2[$k]==$data1[0]) { $rvalue++; }
226: }
227: if ($rvalue==0) { $rvalue=$#data2-$#data1; }
228: $rvalue;
229: } @objsort;
230:
231: my @outres=();
232: undef @outres;
233:
234: for ($k=0;$k<=$#objsort;$k++) {
235: $outres[$k]=$theseres[(split(/\:/,$objsort[$k]))[0]];
236: }
237: return @outres;
238: }
239:
240: # --------------------------------------------------------- Build up RAT screen
241: sub ratedt {
242: my ($r,$url)=@_;
243: $r->print(<<ENDDOCUMENT);
244:
245: <html>
246: <head>
247: <script language="JavaScript">
248: var flag=0;
249: </script>
250: </head>
251: <frameset rows="1,50,*" border=0>
252: <frame name=server src="$url/loadonly/ratserver" noresize noscroll>
253: <frame name=code src="/adm/rat/code.html">
254: <frame name=mapout src="/adm/rat/map.html">
255: </frameset>
256: </html>
257:
258: ENDDOCUMENT
259: }
260:
261: # ---------------------------------------------------------------- Make buttons
262:
263: sub buttons {
264: my $adv=shift;
265: my $output='<form method=post>';
266: if ($adv==1) {
267: $output.='<input type=submit name=forceadv value="Edit">';
268: } else {
269: unless ($adv==2) {
270: $output.='<input type=submit name=forcesmp value="Simple Edit">';
271: }
272: $output.='<input type=submit name=forceadv value="Advanced Edit">';
273: }
274: return $output.'</form><hr>';
275: }
276:
277: sub smpedt {
278: my ($r,$errtext)=@_;
279: my $buttons=&buttons(2);
280:
281: # ---------------------------------------------------------- Process form input
282:
283: my @importselect=();
284: my @targetselect=();
285: undef @importselect;
286: undef @targetselect;
287: if (defined($ENV{'form.import'})) {
288: if (ref($ENV{'form.import'})) {
289: @importselect=sort($ENV->{'form.import'});
290: } else {
291: @importselect=($ENV{'form.import'});
292: }
293: }
294: if (defined($ENV{'form.target'})) {
295: if (ref($ENV{'form.target'})) {
296: @targetselect=sort($ENV->{'form.target'});
297: } else {
298: @targetselect=($ENV{'form.target'});
299: }
300: }
301: # ============================================================ Process commands
302:
303: my $targetdetail=$ENV{'form.targetdetail'};
304: my $importdetail=$ENV{'form.curimpdetail'};
305:
306: # ---------------------------------------------------- Importing from groupsort
307: if (($ENV{'form.importdetail'}) && (!$ENV{'form.impfortarget'})) {
308:
309: $importdetail='';
310: my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
311:
312: my $lastsel;
313:
314: if (defined($importselect[-1])) {
315: $lastsel=$importselect[-1];
316: } else {
317: $lastsel=$#curimport;
318: }
319:
320: for (my $i=0;$i<=$lastsel;$i++) {
321: my ($name,$url)=split(/\=/,$curimport[$i]);
322: if ($url) {
323: $importdetail.='&'.&Apache::lonnet::escape($name).'='.
324: &Apache::lonnet::escape($url);
325: }
326: }
327:
328: $importdetail.='&'.$ENV{'form.importdetail'};
329:
330: for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
331: my ($name,$url)=split(/\=/,$curimport[$i]);
332: if ($url) {
333: $importdetail.='&'.&Apache::lonnet::escape($name).'='.
334: &Apache::lonnet::escape($url);
335: }
336: }
337: $importdetail=~s/\&+/\&/g;
338: $importdetail=~s/^\&//;
339:
340: # ------------------------------------------------------------------- Clear all
341: } elsif ($ENV{'form.clear'}) {
342: $importdetail='';
343: # ------------------------------------------------------------ Discard selected
344: } elsif ($ENV{'form.discard'}) {
345: $importdetail='';
346: my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
347: foreach (@importselect) {
348: $curimport[$_]='';
349: }
350: for (my $i=0;$i<=$#curimport;$i++) {
351: my ($name,$url)=split(/\=/,$curimport[$i]);
352: if ($url) {
353: $importdetail.='&'.&Apache::lonnet::escape($name).'='.
354: &Apache::lonnet::escape($url);
355: }
356: }
357: # --------------------------------------------------------- Loading another map
358: } elsif ($ENV{'form.loadmap'}) {
359: $importdetail='';
360: my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
361:
362: my $lastsel;
363:
364: if (defined($importselect[-1])) {
365: $lastsel=$importselect[-1];
366: } else {
367: $lastsel=$#curimport;
368: }
369:
370: for (my $i=0;$i<=$lastsel;$i++) {
371: my ($name,$url)=split(/\=/,$curimport[$i]);
372: if ($url) {
373: $importdetail.='&'.&Apache::lonnet::escape($name).'='.
374: &Apache::lonnet::escape($url);
375: }
376: }
377:
378: foreach (
379: &attemptread(&Apache::lonnet::filelocation('',$ENV{'form.importmap'}))) {
380: my ($name,$url)=split(/\:/,$_);
381: if ($url) {
382: $importdetail.='&'.&Apache::lonnet::escape($name).'='.
383: &Apache::lonnet::escape($url);
384: }
385: }
386:
387: for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
388: my ($name,$url)=split(/\=/,$curimport[$i]);
389: if ($url) {
390: $importdetail.='&'.&Apache::lonnet::escape($name).'='.
391: &Apache::lonnet::escape($url);
392: }
393: }
394: $importdetail=~s/\&+/\&/g;
395: $importdetail=~s/^\&//;
396:
397: # ------------------------------------
398: }
399:
400: # ------------------------------------------------------------ Assemble windows
401:
402: my $idx=-1;
403: my $importwindow=join("\n",map {
404: $idx++;
405: if ($_) {
406: my ($name,$url)=split(/\=/,$_);
407: unless ($name) { $name=(split(/\//,$url))[-1]; }
408: unless ($name) { $name='EMPTY'; }
409: '<option value="'.$idx.'">'.&Apache::lonnet::unescape($name).
410: '</option>';
411: }
412: } split(/\&/,$importdetail));
413:
414: $idx=0;
415: my $targetwindow=join("\n",map {
416: my ($name,$url)=split(/\:/,$resources[$_]);
417: unless ($name) { $name=(split(/\//,$url))[-1]; }
418: unless ($name) { $name='EMPTY'; }
419: $targetdetail.='&'.&Apache::lonnet::escape($name).'='.
420: &Apache::lonnet::escape($url);
421: $idx++;
422: '<option value="'.$idx.'_'.$_.'">'.$name.'</option>';
423: } @order);
424:
425: # ----------------------------------------------------- Start simple RAT screen
426: $r->print(<<ENDSMPHEAD);
427: <html>
428: <head>
429: <script>
430: var srch;
431: var srchflag=-1; // 1 means currently open
432: // 0 means closed (but has been open)
433: // -1 means never yet opened/defined
434: var srchmode='';
435:
436: var idx;
437: var idxflag=-1; // 1 means currently open
438: // 0 means closed (but has been open)
439: // -1 means never yet opened/defined
440: var idxmode='';
441:
442: // ------------------------------------------------------ Clears indexer window
443: function idxclear() {
444: idx.document.clear();
445: }
446:
447: // ------------------------------------------------------- Clears search window
448: function srchclear() {
449: srch.document.clear();
450: }
451:
452: // ------------------------------------------------------ Closes indexer window
453: function idxclose() {
454: if (idx && !idx.closed) {
455: idxflag=0;
456: idx.close();
457: }
458: }
459:
460: // ------------------------------------------------------- Closes search window
461: function srchclose() {
462: if (srch && !srch.closed) {
463: srchflag=0;
464: srch.close();
465: }
466: }
467:
468: // -------------------------------------------------------- Open indexer window
469: function idxopen(mode) {
470: var options="scrollbars=1,resizable=1,menubar=0";
471: idxmode=mode;
472: idxflag=1;
473: idx=open("/res/?launch=1&mode=simple&catalogmode="+mode,"idxout",options);
474: idx.focus();
475: }
476:
477: // --------------------------------------------------------- Open search window
478: function srchopen(mode) {
479: var options="scrollbars=1,resizable=1,menubar=0";
480: srchmode=mode;
481: srchflag=1;
482: srch=open("/adm/searchcat?launch=1&mode=simple&catalogmode="+mode,"srchout",options);
483: srch.focus();
484: }
485: // ----------------------------------------------------- launch indexer browser
486: function groupsearch() {
487: srchcheck('groupsearch');
488: }
489:
490: function groupimport() {
491: idxcheck('groupimport');
492: }
493: // ------------------------------------------------------- Do srch status check
494: function srchcheck(mode) {
495: if (!srch || srch.closed || srchmode!=mode) {
496: srchopen(mode);
497: }
498: srch.focus();
499: }
500:
501: // -------------------------------------------------------- Do idx status check
502: function idxcheck(mode) {
503: if (!idx || idx.closed || idxmode!=mode) {
504: idxopen(mode);
505: }
506: idx.focus();
507: }
508:
509:
510: var editbrowser;
511: function openbrowser(formname,elementname,only,omit) {
512: var url = '/res/?';
513: if (editbrowser == null) {
514: url += 'launch=1&';
515: }
516: url += 'catalogmode=interactive&';
517: url += 'mode=edit&';
518: url += 'form=' + formname + '&';
519: if (only != null) {
520: url += 'only=' + only + '&';
521: }
522: if (omit != null) {
523: url += 'omit=' + omit + '&';
524: }
525: url += 'element=' + elementname + '';
526: var title = 'Browser';
527: var options = 'scrollbars=1,resizable=1,menubar=0';
528: options += ',width=700,height=600';
529: editbrowser = open(url,title,options,'1');
530: editbrowser.focus();
531: }
532: </script>
533: </head>
534: <body bgcolor='#FFFFFF'>
535: $buttons
536: <font color=red>$errtext</font>
537: <form name=simpleedit method=post>
538: <input type=hidden name=forcesmp value=1>
539: <table>
540: <tr><th width="40%">Import</th>
541: <th> </th>
542: <th width="40%">Target</th></tr>
543: <tr><td bgcolor="#FFFFCC">
544: <input type=button onClick="javascript:groupsearch()" value="Group Search">
545: <input type=button onClick="javascript:groupimport();" value="Group Import">
546: after selected
547: <hr>
548: <input type=text size=20 name=importmap>
549: <input type=button
550: onClick="javascript:openbrowser('simpleedit','importmap','sequence,page','')"
551: value="Browse"><input type=submit name=loadmap value="Load Map"><hr>
552: <input type=submit name="discard" value="Discard Selected">
553: <input type=submit name="clear" value="Clear All">
554: <input type=button onClick="javascript:viewimport()" value="View">
555:
556: </td><td> </td><td bgcolor="#FFFFCC">
557:
558: <input type=button onClick=
559: "javascript:impfortarget.value=1;groupsearch()" value="Group Search">
560: <input type=button onClick=
561: "javascript:impfortarget.value=1;groupimport();" value="Group Import">
562: after selected
563: <hr><input type=button onClick="javascript:viewtarget()" value="View">
564: </td></tr>
565:
566: <tr><td bgcolor="#FFFFCC"><select name="import" multiple>
567: $importwindow
568: </select>
569: </td>
570: <td bgcolor="#FFFFAA" align="center">
571: Cut selected<br>
572: <input type=submit name=cut value='<<<'><p>
573: <hr>
574: Paste after selected<br>
575: <input type=submit name=paste value='>>>'>
576: </td>
577: <td bgcolor="#FFFFCC"><select name="target" multiple>
578: $targetwindow
579: </select>
580: </table>
581: <input type=hidden name=importdetail value="">
582: <input type=hidden name=curimpdetail value="$importdetail">
583: <input type=hidden name=targetdetail value="$targetdetail">
584: <input type=hidden name=impfortarget value="0">
585: </form>
586: </body></html>
587: ENDSMPHEAD
588: }
589:
590: # ----------------------------------------------------------------- No such dir
591: sub nodir {
592: my ($r,$dir)=@_;
593: $dir=~s/^\/home\/\w+\/public\_html//;
594: $r->print(<<ENDNODIR);
595: <html>
596: <body bgcolor='#FFFFFF'>
597: <h1>No such directory: $dir</h1>
598: </body>
599: </html>
600: ENDNODIR
601: }
602:
603: # ---------------------------------------------------------------- View Handler
604:
605: sub viewmap {
606: my ($r,$url,$adv,$errtext)=@_;
607: $r->print('<html><body bgcolor="#FFFFFF">'.&buttons($adv));
608: if ($errtext) {
609: $r->print($errtext.'<hr>');
610: }
611: foreach (&attemptread(&Apache::lonnet::filelocation('',$url))) {
612: if (defined($_)) {
613: my ($title,$url)=split(/\:/,$_);
614: $title=~s/\&colon\;/\:/g;
615: $url=~s/\&colon\;/\:/g;
616: unless ($title) { $title=(split(/\//,$url))[-1] };
617: unless ($title) { $title='<i>Empty</i>'; }
618: if ($url) {
619: $r->print('<a href="'.&Apache::lonratsrv::qtescape($url).'">');
620: }
621: $r->print(&Apache::lonratsrv::qtescape($title));
622: if ($url) { $r->print('</a>'); }
623: $r->print('<br>');
624: }
625: }
626: $r->print('</body></html>');
627: }
628:
629: # ================================================================ Main Handler
630:
631: sub handler {
632: my $r=shift;
633: $r->content_type('text/html');
634: $r->send_http_header;
635:
636: return OK if $r->header_only;
637:
638: my $url=$r->uri;
639: my $fn=&Apache::lonnet::filelocation('',$url);
640:
641: my ($dir)=($fn=~/^(.+)\/[^\/]+$/);
642: unless (-e $dir) {
643: &nodir($r,$dir);
644: return OK;
645: }
646:
647: # ------------------------------------------- Determine which tools can be used
648: my $adv=0;
649:
650: unless ($ENV{'form.forcesmp'}) {
651: if ($ENV{'form.forceadv'}) {
652: $adv=1;
653: } elsif (my $fh=Apache::File->new($fn)) {
654: my $allmap=join('',<$fh>);
655: $adv=($allmap=~/\<map[^\>]+mode\s*\=\s*(\'|\")rat/is);
656: }
657: }
658:
659: my $errtext='';
660: my $fatal=0;
661:
662: # -------------------------------------------------------------------- Load map
663: ($errtext,$fatal)=&mapread($fn,$errtext);
664:
665: if ($fatal==1) { $adv=1; }
666:
667: # ----------------------------------- adv==1 now means "graphical MUST be used"
668:
669: if ($ENV{'form.forceadv'}) {
670: &ratedt($r,$url);
671: } elsif ($ENV{'form.forcesmp'}) {
672: &smpedt($r,$errtext);
673: } else {
674: &viewmap($r,$url,$adv,$errtext);
675: }
676: return OK;
677: }
678:
679: 1;
680: __END__
681:
682:
683:
684:
685:
686:
687:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>