1: # The LearningOnline Network with CAPA
2: # Edit Handler for RAT Maps
3: #
4: # $Id: lonratedt.pm,v 1.14 2002/05/13 19:38:32 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: # --------------------------------------------------------- Build up RAT screen
130: sub ratedt {
131: my ($r,$url)=@_;
132: $r->print(<<ENDDOCUMENT);
133:
134: <html>
135: <head>
136: <script language="JavaScript">
137: var flag=0;
138: </script>
139: </head>
140: <frameset rows="1,50,*" border=0>
141: <frame name=server src="$url/loadonly/ratserver" noresize noscroll>
142: <frame name=code src="/adm/rat/code.html">
143: <frame name=mapout src="/adm/rat/map.html">
144: </frameset>
145: </html>
146:
147: ENDDOCUMENT
148: }
149:
150: # ---------------------------------------------------------------- Make buttons
151:
152: sub buttons {
153: my $adv=shift;
154: my $output='<form method=post>';
155: if ($adv==1) {
156: $output.='<input type=submit name=forceadv value="Edit">';
157: } else {
158: unless ($adv==2) {
159: $output.='<input type=submit name=forcesmp value="Simple Edit">';
160: }
161: $output.='<input type=submit name=forceadv value="Advanced Edit">';
162: }
163: return $output.'</form><hr>';
164: }
165:
166: sub smpedt {
167: my ($r,$errtext)=@_;
168: my $buttons=&buttons(2);
169:
170: # ---------------------------------------------------------- Process form input
171:
172: my @importselect=();
173: my @targetselect=();
174: undef @importselect;
175: undef @targetselect;
176: if (defined($ENV{'form.import'})) {
177: if (ref($ENV{'form.import'})) {
178: @importselect=sort($ENV->{'form.import'});
179: } else {
180: @importselect=($ENV{'form.import'});
181: }
182: }
183: if (defined($ENV{'form.target'})) {
184: if (ref($ENV{'form.target'})) {
185: @targetselect=sort($ENV->{'form.target'});
186: } else {
187: @targetselect=($ENV{'form.target'});
188: }
189: }
190: # ============================================================ Process commands
191:
192: my $targetdetail=$ENV{'form.targetdetail'};
193: my $importdetail=$ENV{'form.curimpdetail'};
194:
195: # ---------------------------------------------------- Importing from groupsort
196: if ($ENV{'form.importdetail'}) {
197:
198: $importdetail='';
199: my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
200:
201: my $lastsel;
202:
203: if (defined($importselect[-1])) {
204: $lastsel=$importselect[-1];
205: } else {
206: $lastsel=$#curimport;
207: }
208:
209: for (my $i=0;$i<=$lastsel;$i++) {
210: my ($name,$url)=split(/\=/,$curimport[$i]);
211: if ($url) {
212: $importdetail.='&'.&Apache::lonnet::escape($name).'='.
213: &Apache::lonnet::escape($url);
214: }
215: }
216:
217: $importdetail.='&'.$ENV{'form.importdetail'};
218:
219: for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
220: my ($name,$url)=split(/\=/,$curimport[$i]);
221: if ($url) {
222: $importdetail.='&'.&Apache::lonnet::escape($name).'='.
223: &Apache::lonnet::escape($url);
224: }
225: }
226: $importdetail=~s/\&+/\&/g;
227: $importdetail=~s/^\&//;
228:
229: # ------------------------------------------------------------------- Clear all
230: } elsif ($ENV{'form.clear'}) {
231: $importdetail='';
232: # ------------------------------------------------------------ Discard selected
233: } elsif ($ENV{'form.discard'}) {
234: $importdetail='';
235: my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
236: foreach (@importselect) {
237: $curimport[$_]='';
238: }
239: for (my $i=0;$i<=$#curimport;$i++) {
240: my ($name,$url)=split(/\=/,$curimport[$i]);
241: if ($url) {
242: $importdetail.='&'.&Apache::lonnet::escape($name).'='.
243: &Apache::lonnet::escape($url);
244: }
245: }
246: # ---------------------------
247: }
248:
249: # ------------------------------------------------------------ Assemble windows
250:
251: my $idx=-1;
252: my $importwindow=join("\n",map {
253: $idx++;
254: if ($_) {
255: my ($name)=split(/\=/,$_);
256: unless ($name) { $name='UNKNOWN'; }
257: '<option value="'.$idx.'">'.&Apache::lonnet::unescape($name).
258: '</option>';
259: }
260: } split(/\&/,$importdetail));
261:
262: $idx=0;
263: my $targetwindow=join("\n",map {
264: my ($name,$url)=split(/\:/,$resources[$_]);
265: unless ($name) { $name='UNKNOWN'; }
266: $targetdetail.='&'.&Apache::lonnet::escape($name).'='.
267: &Apache::lonnet::escape($url);
268: $idx++;
269: '<option value="'.$idx.'_'.$_.'">'.$name.'</option>';
270: } @order);
271:
272: # ----------------------------------------------------- Start simple RAT screen
273: $r->print(<<ENDSMPHEAD);
274: <html>
275: <head>
276: <script>
277: var srch;
278: var srchflag=-1; // 1 means currently open
279: // 0 means closed (but has been open)
280: // -1 means never yet opened/defined
281: var srchmode='';
282:
283: var idx;
284: var idxflag=-1; // 1 means currently open
285: // 0 means closed (but has been open)
286: // -1 means never yet opened/defined
287: var idxmode='';
288:
289: // ------------------------------------------------------ Clears indexer window
290: function idxclear() {
291: idx.document.clear();
292: }
293:
294: // ------------------------------------------------------- Clears search window
295: function srchclear() {
296: srch.document.clear();
297: }
298:
299: // ------------------------------------------------------ Closes indexer window
300: function idxclose() {
301: if (idx && !idx.closed) {
302: idxflag=0;
303: idx.close();
304: }
305: }
306:
307: // ------------------------------------------------------- Closes search window
308: function srchclose() {
309: if (srch && !srch.closed) {
310: srchflag=0;
311: srch.close();
312: }
313: }
314:
315: // -------------------------------------------------------- Open indexer window
316: function idxopen(mode) {
317: var options="scrollbars=1,resizable=1,menubar=0";
318: idxmode=mode;
319: idxflag=1;
320: idx=open("/res/?launch=1&mode=simple&catalogmode="+mode,"idxout",options);
321: idx.focus();
322: }
323:
324: // --------------------------------------------------------- Open search window
325: function srchopen(mode) {
326: var options="scrollbars=1,resizable=1,menubar=0";
327: srchmode=mode;
328: srchflag=1;
329: srch=open("/adm/searchcat?launch=1&mode=simple&catalogmode="+mode,"srchout",options);
330: srch.focus();
331: }
332: // ----------------------------------------------------- launch indexer browser
333: function groupsearch() {
334: srchcheck('groupsearch');
335: }
336:
337: function groupimport() {
338: idxcheck('groupimport');
339: }
340: // ------------------------------------------------------- Do srch status check
341: function srchcheck(mode) {
342: if (!srch || srch.closed || srchmode!=mode) {
343: srchopen(mode);
344: }
345: srch.focus();
346: }
347:
348: // -------------------------------------------------------- Do idx status check
349: function idxcheck(mode) {
350: if (!idx || idx.closed || idxmode!=mode) {
351: idxopen(mode);
352: }
353: idx.focus();
354: }
355: </script>
356: </head>
357: <body bgcolor='#FFFFFF'>
358: $buttons
359: <font color=red>$errtext</font>
360: <form name=simpleedit method=post>
361: <input type=hidden name=forcesmp value=1>
362: <table>
363: <tr><th width="40%">Import</th>
364: <th> </th>
365: <th width="40%">Target</th></tr>
366: <tr><td bgcolor="#FFFFCC">
367: <input type=button onClick="javascript:groupsearch()" value="Group Search">
368: <input type=button onClick="javascript:groupimport();" value="Group Import">
369: <br>after selected
370: <hr>
371: <input type=submit name="discard" value="Discard Selected">
372: <input type=submit name="clear" value="Clear All">
373: <input type=button onClick="javascript:viewimport()" value="View">
374: </td><td> </td><td bgcolor="#FFFFCC">
375: <input type=button onClick="javascript:viewtarget()" value="View">
376: </td></tr>
377: <tr><td bgcolor="#FFFFCC"><select name="import" multiple>
378: $importwindow
379: </select>
380: </td>
381: <td bgcolor="#FFFFAA" align="center">
382: Cut selected<br>
383: <input type=submit name=cut value='<<<'><p>
384: <hr>
385: Paste after selected<br>
386: <input type=submit name=paste value='>>>'>
387: </td>
388: <td bgcolor="#FFFFCC"><select name="target" multiple>
389: $targetwindow
390: </select>
391: </table>
392: <input type=hidden name=importdetail value="">
393: <input type=hidden name=curimpdetail value="$importdetail">
394: <input type=hidden name=targetdetail value="$targetdetail">
395: </form>
396: </body></html>
397: ENDSMPHEAD
398: }
399:
400: # ----------------------------------------------------------------- No such dir
401: sub nodir {
402: my ($r,$dir)=@_;
403: $dir=~s/^\/home\/\w+\/public\_html//;
404: $r->print(<<ENDNODIR);
405: <html>
406: <body bgcolor='#FFFFFF'>
407: <h1>No such directory: $dir</h1>
408: </body>
409: </html>
410: ENDNODIR
411: }
412:
413: # ---------------------------------------------------------------- View Handler
414:
415: sub viewmap {
416: my ($r,$adv,$errtext)=@_;
417: $r->print('<html><body bgcolor="#FFFFFF">'.&buttons($adv));
418: if ($errtext) {
419: $r->print($errtext.'<hr>');
420: }
421: foreach (@resources) {
422: if (defined($_)) {
423: my ($title,$url)=split(/\:/,$_);
424: $title=~s/\&colon\;/\:/g;
425: $url=~s/\&colon\;/\:/g;
426: unless ($title) { $title='<i>Unknown</i>'; }
427: if ($url) {
428: $r->print('<a href="'.&Apache::lonratsrv::qtescape($url).'">');
429: }
430: $r->print(&Apache::lonratsrv::qtescape($title));
431: if ($url) { $r->print('</a>'); }
432: $r->print('<br>');
433: }
434: }
435: $r->print('</body></html>');
436: }
437:
438: # ================================================================ Main Handler
439:
440: sub handler {
441: my $r=shift;
442: $r->content_type('text/html');
443: $r->send_http_header;
444:
445: return OK if $r->header_only;
446:
447: my $url=$r->uri;
448: my $fn=&Apache::lonnet::filelocation('',$url);
449:
450: my ($dir)=($fn=~/^(.+)\/[^\/]+$/);
451: unless (-e $dir) {
452: &nodir($r,$dir);
453: return OK;
454: }
455:
456: # ------------------------------------------- Determine which tools can be used
457: my $adv=0;
458:
459: unless ($ENV{'form.forcesmp'}) {
460: if ($ENV{'form.forceadv'}) {
461: $adv=1;
462: } elsif (my $fh=Apache::File->new($fn)) {
463: my $allmap=join('',<$fh>);
464: $adv=($allmap=~/\<map[^\>]+mode\s*\=\s*(\'|\")rat/is);
465: }
466: }
467:
468: my $errtext='';
469: my $fatal=0;
470:
471: # -------------------------------------------------------------------- Load map
472: ($errtext,$fatal)=&mapread($fn,$errtext);
473:
474: if ($fatal==1) { $adv=1; }
475:
476: # ----------------------------------- adv==1 now means "graphical MUST be used"
477:
478: if ($ENV{'form.forceadv'}) {
479: &ratedt($r,$url);
480: } elsif ($ENV{'form.forcesmp'}) {
481: &smpedt($r,$errtext);
482: } else {
483: &viewmap($r,$adv,$errtext);
484: }
485: return OK;
486: }
487:
488: 1;
489: __END__
490:
491:
492:
493:
494:
495:
496:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>