Annotation of rat/lonratedt.pm, revision 1.18
1.1 www 1: # The LearningOnline Network with CAPA
2: # Edit Handler for RAT Maps
1.5 www 3: #
1.18 ! www 4: # $Id: lonratedt.pm,v 1.17 2002/05/15 19:50:37 www Exp $
1.5 www 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: #
1.1 www 28: # (TeX Content Handler
29: #
30: # 05/29/00,05/30 Gerd Kortemeyer)
1.4 www 31: # 7/1,6/30 Gerd Kortemeyer
1.1 www 32:
33: package Apache::lonratedt;
34:
35: use strict;
36: use Apache::Constants qw(:common);
1.3 www 37: use Apache::lonnet;
1.7 www 38: use Apache::lonratsrv;
1.1 www 39:
1.10 www 40: my @order=();
1.8 www 41: my @resources=();
42:
43:
44: # Mapread read maps into global arrays @links and @resources, determines status
1.10 www 45: # sets @order - pointer to resources in right order
46: # sets @resources - array with the resources with correct idx
47: #
1.8 www 48: sub mapread {
49: my $fn=shift;
50:
1.10 www 51: my @links;
1.8 www 52: undef @links;
53: undef @resources;
1.10 www 54: undef @order;
1.8 www 55:
56: my ($outtext,$errtext)=&Apache::lonratsrv::loadmap($fn,'');
57: if ($errtext) { return ($errtext,2); }
58:
1.9 www 59: # -------------------------------------------------------------------- Read map
1.8 www 60: foreach (split(/\<\&\>/,$outtext)) {
1.9 www 61: my ($command,$number,$content)=split(/\<\:\>/,$_);
1.8 www 62: if ($command eq 'objcont') {
1.9 www 63: $resources[$number]=$content;
1.8 www 64: }
65: if ($command eq 'objlinks') {
1.9 www 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]))) {
1.8 www 79: return
1.10 www 80: ('Map has branchings. Use advanced editor.',1);
1.8 www 81: }
1.9 www 82: $starters[$start]=1;
83: $endings[$end]=1;
84: if ($cond) {
1.8 www 85: return
1.10 www 86: ('Map has conditions. Use advanced editor.',1);
1.8 www 87: }
88: }
89:
90: }
1.10 www 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: }
1.8 www 126: return $errtext;
127: }
128:
1.16 www 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:
1.17 www 155: my @objsort=();
156: undef @objsort;
1.16 www 157:
1.17 www 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: }
1.16 www 173:
1.17 www 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
1.16 www 215:
1.17 www 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;
1.16 www 238: }
239:
1.3 www 240: # --------------------------------------------------------- Build up RAT screen
241: sub ratedt {
242: my ($r,$url)=@_;
1.1 www 243: $r->print(<<ENDDOCUMENT);
244:
245: <html>
1.2 harris41 246: <head>
247: <script language="JavaScript">
248: var flag=0;
249: </script>
250: </head>
1.1 www 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
1.3 www 259: }
260:
1.8 www 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:
1.3 www 277: sub smpedt {
1.8 www 278: my ($r,$errtext)=@_;
279: my $buttons=&buttons(2);
1.12 www 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'})) {
1.13 www 289: @importselect=sort($ENV->{'form.import'});
1.12 www 290: } else {
291: @importselect=($ENV{'form.import'});
292: }
293: }
294: if (defined($ENV{'form.target'})) {
295: if (ref($ENV{'form.target'})) {
1.13 www 296: @targetselect=sort($ENV->{'form.target'});
1.12 www 297: } else {
298: @targetselect=($ENV{'form.target'});
299: }
300: }
1.13 www 301: # ============================================================ Process commands
1.12 www 302:
1.14 www 303: my $targetdetail=$ENV{'form.targetdetail'};
304: my $importdetail=$ENV{'form.curimpdetail'};
1.13 www 305:
306: # ---------------------------------------------------- Importing from groupsort
1.16 www 307: if (($ENV{'form.importdetail'}) && (!$ENV{'form.impfortarget'})) {
1.13 www 308:
1.14 www 309: $importdetail='';
1.13 www 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) {
1.18 ! www 323: $importdetail.='&'.$name.'='.$url;
1.13 www 324: }
325: }
326:
327: $importdetail.='&'.$ENV{'form.importdetail'};
328:
329: for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
330: my ($name,$url)=split(/\=/,$curimport[$i]);
331: if ($url) {
1.18 ! www 332: $importdetail.='&'.$name.'='.$url;
1.13 www 333: }
334: }
335: $importdetail=~s/\&+/\&/g;
336: $importdetail=~s/^\&//;
337:
1.14 www 338: # ------------------------------------------------------------------- Clear all
339: } elsif ($ENV{'form.clear'}) {
340: $importdetail='';
341: # ------------------------------------------------------------ Discard selected
342: } elsif ($ENV{'form.discard'}) {
343: $importdetail='';
344: my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
345: foreach (@importselect) {
346: $curimport[$_]='';
347: }
348: for (my $i=0;$i<=$#curimport;$i++) {
349: my ($name,$url)=split(/\=/,$curimport[$i]);
350: if ($url) {
1.18 ! www 351: $importdetail.='&'.$name.'='.$url;
1.14 www 352: }
353: }
1.17 www 354: # --------------------------------------------------------- Loading another map
355: } elsif ($ENV{'form.loadmap'}) {
356: $importdetail='';
357: my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
358:
359: my $lastsel;
360:
361: if (defined($importselect[-1])) {
362: $lastsel=$importselect[-1];
363: } else {
364: $lastsel=$#curimport;
365: }
366:
367: for (my $i=0;$i<=$lastsel;$i++) {
368: my ($name,$url)=split(/\=/,$curimport[$i]);
369: if ($url) {
1.18 ! www 370: $importdetail.='&'.$name.'='.$url;
1.17 www 371: }
372: }
373:
374: foreach (
375: &attemptread(&Apache::lonnet::filelocation('',$ENV{'form.importmap'}))) {
376: my ($name,$url)=split(/\:/,$_);
377: if ($url) {
378: $importdetail.='&'.&Apache::lonnet::escape($name).'='.
379: &Apache::lonnet::escape($url);
380: }
381: }
382:
383: for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
384: my ($name,$url)=split(/\=/,$curimport[$i]);
385: if ($url) {
1.18 ! www 386: $importdetail.='&'.$name.'='.$url;
1.17 www 387: }
388: }
389: $importdetail=~s/\&+/\&/g;
390: $importdetail=~s/^\&//;
391:
392: # ------------------------------------
1.13 www 393: }
1.12 www 394:
395: # ------------------------------------------------------------ Assemble windows
396:
1.13 www 397: my $idx=-1;
398: my $importwindow=join("\n",map {
399: $idx++;
400: if ($_) {
1.15 www 401: my ($name,$url)=split(/\=/,$_);
402: unless ($name) { $name=(split(/\//,$url))[-1]; }
403: unless ($name) { $name='EMPTY'; }
1.13 www 404: '<option value="'.$idx.'">'.&Apache::lonnet::unescape($name).
405: '</option>';
406: }
407: } split(/\&/,$importdetail));
1.12 www 408:
1.13 www 409: $idx=0;
1.11 www 410: my $targetwindow=join("\n",map {
1.13 www 411: my ($name,$url)=split(/\:/,$resources[$_]);
1.15 www 412: unless ($name) { $name=(split(/\//,$url))[-1]; }
413: unless ($name) { $name='EMPTY'; }
1.13 www 414: $targetdetail.='&'.&Apache::lonnet::escape($name).'='.
415: &Apache::lonnet::escape($url);
416: $idx++;
417: '<option value="'.$idx.'_'.$_.'">'.$name.'</option>';
1.11 www 418: } @order);
419:
1.8 www 420: # ----------------------------------------------------- Start simple RAT screen
1.3 www 421: $r->print(<<ENDSMPHEAD);
422: <html>
1.6 www 423: <head>
424: <script>
425: var srch;
426: var srchflag=-1; // 1 means currently open
427: // 0 means closed (but has been open)
428: // -1 means never yet opened/defined
429: var srchmode='';
430:
431: var idx;
432: var idxflag=-1; // 1 means currently open
433: // 0 means closed (but has been open)
434: // -1 means never yet opened/defined
435: var idxmode='';
436:
437: // ------------------------------------------------------ Clears indexer window
438: function idxclear() {
439: idx.document.clear();
440: }
441:
442: // ------------------------------------------------------- Clears search window
443: function srchclear() {
444: srch.document.clear();
445: }
446:
447: // ------------------------------------------------------ Closes indexer window
448: function idxclose() {
449: if (idx && !idx.closed) {
450: idxflag=0;
451: idx.close();
452: }
453: }
454:
455: // ------------------------------------------------------- Closes search window
456: function srchclose() {
457: if (srch && !srch.closed) {
458: srchflag=0;
459: srch.close();
460: }
461: }
462:
463: // -------------------------------------------------------- Open indexer window
464: function idxopen(mode) {
465: var options="scrollbars=1,resizable=1,menubar=0";
466: idxmode=mode;
467: idxflag=1;
468: idx=open("/res/?launch=1&mode=simple&catalogmode="+mode,"idxout",options);
469: idx.focus();
470: }
471:
472: // --------------------------------------------------------- Open search window
473: function srchopen(mode) {
474: var options="scrollbars=1,resizable=1,menubar=0";
475: srchmode=mode;
476: srchflag=1;
477: srch=open("/adm/searchcat?launch=1&mode=simple&catalogmode="+mode,"srchout",options);
478: srch.focus();
479: }
480: // ----------------------------------------------------- launch indexer browser
481: function groupsearch() {
482: srchcheck('groupsearch');
483: }
484:
485: function groupimport() {
486: idxcheck('groupimport');
487: }
488: // ------------------------------------------------------- Do srch status check
489: function srchcheck(mode) {
490: if (!srch || srch.closed || srchmode!=mode) {
491: srchopen(mode);
492: }
493: srch.focus();
494: }
495:
496: // -------------------------------------------------------- Do idx status check
497: function idxcheck(mode) {
498: if (!idx || idx.closed || idxmode!=mode) {
499: idxopen(mode);
500: }
501: idx.focus();
502: }
1.15 www 503:
504:
505: var editbrowser;
506: function openbrowser(formname,elementname,only,omit) {
507: var url = '/res/?';
508: if (editbrowser == null) {
509: url += 'launch=1&';
510: }
511: url += 'catalogmode=interactive&';
512: url += 'mode=edit&';
513: url += 'form=' + formname + '&';
514: if (only != null) {
515: url += 'only=' + only + '&';
516: }
517: if (omit != null) {
518: url += 'omit=' + omit + '&';
519: }
520: url += 'element=' + elementname + '';
521: var title = 'Browser';
522: var options = 'scrollbars=1,resizable=1,menubar=0';
523: options += ',width=700,height=600';
524: editbrowser = open(url,title,options,'1');
525: editbrowser.focus();
526: }
1.6 www 527: </script>
528: </head>
1.3 www 529: <body bgcolor='#FFFFFF'>
1.8 www 530: $buttons
1.7 www 531: <font color=red>$errtext</font>
1.13 www 532: <form name=simpleedit method=post>
1.11 www 533: <input type=hidden name=forcesmp value=1>
534: <table>
1.12 www 535: <tr><th width="40%">Import</th>
536: <th> </th>
537: <th width="40%">Target</th></tr>
538: <tr><td bgcolor="#FFFFCC">
539: <input type=button onClick="javascript:groupsearch()" value="Group Search">
1.13 www 540: <input type=button onClick="javascript:groupimport();" value="Group Import">
1.15 www 541: after selected
1.14 www 542: <hr>
1.15 www 543: <input type=text size=20 name=importmap>
544: <input type=button
545: onClick="javascript:openbrowser('simpleedit','importmap','sequence,page','')"
546: value="Browse"><input type=submit name=loadmap value="Load Map"><hr>
1.14 www 547: <input type=submit name="discard" value="Discard Selected">
548: <input type=submit name="clear" value="Clear All">
1.12 www 549: <input type=button onClick="javascript:viewimport()" value="View">
1.16 www 550:
1.12 www 551: </td><td> </td><td bgcolor="#FFFFCC">
1.16 www 552:
553: <input type=button onClick=
554: "javascript:impfortarget.value=1;groupsearch()" value="Group Search">
555: <input type=button onClick=
556: "javascript:impfortarget.value=1;groupimport();" value="Group Import">
557: after selected
558: <hr><input type=button onClick="javascript:viewtarget()" value="View">
1.12 www 559: </td></tr>
1.16 www 560:
1.12 www 561: <tr><td bgcolor="#FFFFCC"><select name="import" multiple>
562: $importwindow
563: </select>
1.11 www 564: </td>
1.12 www 565: <td bgcolor="#FFFFAA" align="center">
566: Cut selected<br>
1.11 www 567: <input type=submit name=cut value='<<<'><p>
1.12 www 568: <hr>
569: Paste after selected<br>
1.11 www 570: <input type=submit name=paste value='>>>'>
571: </td>
1.12 www 572: <td bgcolor="#FFFFCC"><select name="target" multiple>
1.11 www 573: $targetwindow
574: </select>
1.12 www 575: </table>
1.13 www 576: <input type=hidden name=importdetail value="">
577: <input type=hidden name=curimpdetail value="$importdetail">
1.12 www 578: <input type=hidden name=targetdetail value="$targetdetail">
1.16 www 579: <input type=hidden name=impfortarget value="0">
1.12 www 580: </form>
581: </body></html>
1.3 www 582: ENDSMPHEAD
583: }
584:
1.11 www 585: # ----------------------------------------------------------------- No such dir
1.4 www 586: sub nodir {
587: my ($r,$dir)=@_;
588: $dir=~s/^\/home\/\w+\/public\_html//;
589: $r->print(<<ENDNODIR);
590: <html>
591: <body bgcolor='#FFFFFF'>
592: <h1>No such directory: $dir</h1>
593: </body>
594: </html>
595: ENDNODIR
596: }
597:
1.8 www 598: # ---------------------------------------------------------------- View Handler
599:
600: sub viewmap {
1.17 www 601: my ($r,$url,$adv,$errtext)=@_;
1.8 www 602: $r->print('<html><body bgcolor="#FFFFFF">'.&buttons($adv));
1.10 www 603: if ($errtext) {
604: $r->print($errtext.'<hr>');
605: }
1.17 www 606: foreach (&attemptread(&Apache::lonnet::filelocation('',$url))) {
1.9 www 607: if (defined($_)) {
608: my ($title,$url)=split(/\:/,$_);
609: $title=~s/\&colon\;/\:/g;
610: $url=~s/\&colon\;/\:/g;
1.15 www 611: unless ($title) { $title=(split(/\//,$url))[-1] };
612: unless ($title) { $title='<i>Empty</i>'; }
1.9 www 613: if ($url) {
614: $r->print('<a href="'.&Apache::lonratsrv::qtescape($url).'">');
615: }
616: $r->print(&Apache::lonratsrv::qtescape($title));
617: if ($url) { $r->print('</a>'); }
618: $r->print('<br>');
619: }
620: }
1.8 www 621: $r->print('</body></html>');
622: }
623:
1.3 www 624: # ================================================================ Main Handler
625:
626: sub handler {
627: my $r=shift;
628: $r->content_type('text/html');
629: $r->send_http_header;
630:
631: return OK if $r->header_only;
632:
633: my $url=$r->uri;
634: my $fn=&Apache::lonnet::filelocation('',$url);
635:
1.4 www 636: my ($dir)=($fn=~/^(.+)\/[^\/]+$/);
637: unless (-e $dir) {
638: &nodir($r,$dir);
639: return OK;
640: }
1.8 www 641:
642: # ------------------------------------------- Determine which tools can be used
1.3 www 643: my $adv=0;
644:
645: unless ($ENV{'form.forcesmp'}) {
646: if ($ENV{'form.forceadv'}) {
647: $adv=1;
648: } elsif (my $fh=Apache::File->new($fn)) {
649: my $allmap=join('',<$fh>);
650: $adv=($allmap=~/\<map[^\>]+mode\s*\=\s*(\'|\")rat/is);
651: }
652: }
653:
1.8 www 654: my $errtext='';
655: my $fatal=0;
656:
657: # -------------------------------------------------------------------- Load map
658: ($errtext,$fatal)=&mapread($fn,$errtext);
659:
660: if ($fatal==1) { $adv=1; }
661:
662: # ----------------------------------- adv==1 now means "graphical MUST be used"
663:
664: if ($ENV{'form.forceadv'}) {
1.3 www 665: &ratedt($r,$url);
1.8 www 666: } elsif ($ENV{'form.forcesmp'}) {
667: &smpedt($r,$errtext);
1.3 www 668: } else {
1.17 www 669: &viewmap($r,$url,$adv,$errtext);
1.3 www 670: }
1.1 www 671: return OK;
672: }
673:
674: 1;
675: __END__
676:
677:
678:
679:
680:
681:
682:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>