Annotation of loncom/interface/courseclassifier.pm, revision 1.6
1.5 raeburn 1: # The LearningOnline Network with CAPA
2: # Utilities for classifying courses based on institutional code
3: #
1.6 ! raeburn 4: # $Id: courseclassifier.pm,v 1.5 2008/03/21 18:00:18 raeburn Exp $
1.1 raeburn 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: # /home/httpd/html/adm/gpl.txt
24: #
25: # http://www.lon-capa.org/
26: #
27:
28: package Apache::courseclassifier;
29:
30: use strict;
31: use lib '/home/httpd/lib/perl/';
32: use Apache::lonnet;
1.6 ! raeburn 33: use Apache::lonlocal;
1.1 raeburn 34: use LONCAPA;
35:
36: sub retrieve_instcodes {
1.2 raeburn 37: my ($coursecodes,$codedom) = @_;
38: my $totcodes;
1.1 raeburn 39: my %courses = &Apache::lonnet::courseiddump($codedom,'.',1,'.','.','.', undef,undef,'Course');
1.2 raeburn 40: foreach my $course (keys(%courses)) {
41: if (ref($courses{$course}) eq 'HASH') {
1.3 raeburn 42: $$coursecodes{$course} = $courses{$course}{'inst_code'};
1.1 raeburn 43: $totcodes ++;
44: }
45: }
46: return $totcodes;
47: }
48:
49: sub sort_cats {
50: my ($num,$cat_order,$codetitles,$idsarrayref,$sorted) = @_;
51: my @unsorted = @{$idsarrayref};
52: if (defined($$cat_order{$$codetitles[$num]})) {
53: foreach my $item (@{$$cat_order{$$codetitles[$num]}}) {
54: if (grep/^\Q$item\E$/,@unsorted) {
55: push @{$sorted}, $item;
56: }
57: }
58: } else {
59: @{$sorted} = sort (@unsorted);
60: }
61: }
62:
63: sub recurse_list {
64: my ($cid,$codetitles,$codes,$num,$idarrays) = @_;
65: if ($num == 0) {
66: if (!grep/^$$codes{$cid}{$$codetitles[0]}$/,@{$$idarrays{$$codetitles[0]}}) {
67: push @{$$idarrays{$$codetitles[0]}}, $$codes{$cid}{$$codetitles[0]};
68: }
69: } elsif ($num == 1) {
70: if (defined($$idarrays{$$codetitles[1]}{$$codes{$cid}{$$codetitles[0]}})) {
71: if (!grep/^$$codes{$cid}{$$codetitles[1]}$/,@{$$idarrays{$$codetitles[1]}{$$codes{$cid}{$$codetitles[0]}}}) {
72: push @{$$idarrays{$$codetitles[1]}{$$codes{$cid}{$$codetitles[0]}}}, $$codes{$cid}{$$codetitles[1]};
73: }
74: } else {
75: @{$$idarrays{$$codetitles[1]}{$$codes{$cid}{$$codetitles[0]}}} = ("$$codes{$cid}{$$codetitles[1]}");
76: }
77: } elsif ($num == 2) {
78: if (defined($$idarrays{$$codetitles[2]}{$$codes{$cid}{$$codetitles[0]}})) {
79: if (defined($$idarrays{$$codetitles[2]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}})) {
80: if (!grep/^$$codes{$cid}{$$codetitles[2]}$/,@{$$idarrays{$$codetitles[2]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}}) {
81: push @{$$idarrays{$$codetitles[2]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}}, $$codes{$cid}{$$codetitles[2]};
82: }
83: } else {
84: @{$$idarrays{$$codetitles[2]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}} = ("$$codes{$cid}{$$codetitles[2]}"); }
85: } else {
86: %{$$idarrays{$$codetitles[2]}{$$codes{$cid}{$$codetitles[0]}}} = ();
87: @{$$idarrays{$$codetitles[2]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}} = ("$$codes{$cid}{$$codetitles[2]}");
88: }
89: } elsif ($num == 3) {
90: if (defined($$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}})) {
91: if (defined($$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}})) {
92: if (defined($$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}})) {
93: if (!grep/^$$codes{$cid}{$$codetitles[3]}$/,@{$$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}}) {
94: push @{$$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}}, $$codes{$cid}{$$codetitles[3]};
95: }
96: } else {
97: @{$$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}} = ("$$codes{$cid}{$$codetitles[3]}");
98: }
99: } else {
100: %{$$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}} = ();
101: @{$$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}} = ("$$codes{$cid}{$$codetitles[3]}");
102: }
103: } else {
104: %{$$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}} = ();
105: %{$$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}} = ();
106: @{$$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}} = ("$$codes{$cid}{$$codetitles[3]}");
107: }
108: } elsif ($num == 4) {
109: if (defined($$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}})) {
110: if (defined($$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}})) {
111: if (defined($$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}})) {
112: if (defined($$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}{$$codes{$cid}{$$codetitles[3]}})) {
113: if (!grep/^$$codes{$cid}{$$codetitles[4]}$/,@{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}{$$codes{$cid}{$$codetitles[3]}}}) {
114: push @{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}{$$codes{$cid}{$$codetitles[3]}}}, $$codes{$cid}{$$codetitles[4]};
115: }
116: } else {
117: @{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}{$$codes{$cid}{$$codetitles[3]}}} = ("$$codes{$cid}{$$codetitles[4]}");
118: }
119: } else {
120: %{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}} = ();
121: @{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}{$$codes{$cid}{$$codetitles[3]}}} = ("$$codes{$cid}{$$codetitles[4]}");
122: }
123: } else {
124: %{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}} = ();
1.4 raeburn 125: %{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}} = ();
126: @{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}{$$codes{$cid}{$$codetitles[3]}}} = ("$$codes{$cid}{$$codetitles[4]}");
1.1 raeburn 127: }
128: } else {
129: %{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}} = ();
130: %{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}} = ();
131: %{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}} = ();
132: @{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[3]}}} = ("$$codes{$cid}{$$codetitles[3]}");
133: }
134: }
135: $num ++;
136: if ($num <@{$codetitles}) {
137: &recurse_list($cid,$codetitles,$codes,$num,$idarrays);
138: }
139: }
140:
141: sub build_code_selections {
142: my ($codes,$codetitles,$cat_titles,$cat_order,$idlist,$idnums,$idlist_titles) = @_;
143: my %idarrays = ();
144: for (my $i=1; $i<@{$codetitles}; $i++) {
145: %{$idarrays{$$codetitles[$i]}} = ();
146: }
147: foreach my $cid (sort keys %{$codes}) {
148: &recurse_list($cid,$codetitles,$codes,0,\%idarrays);
149: }
150: for (my $num=0; $num<@{$codetitles}; $num++) {
151: if ($num == 0) {
152: my @contents = ();
153: my @contents_titles = ();
154: &sort_cats($num,$cat_order,$codetitles,\@{$idarrays{$$codetitles[0]}},\@contents);
155: if (defined($$cat_titles{$$codetitles[0]})) {
156: foreach (@contents) {
157: push @contents_titles, $$cat_titles{$$codetitles[0]}{$_};
158: }
159: }
160: $$idlist{$$codetitles[0]} = join('","',@contents);
161: $$idnums{$$codetitles[0]} = scalar(@contents);
162: if (defined($$cat_titles{$$codetitles[0]})) {
163: $$idlist_titles{$$codetitles[0]} = join('","',@contents_titles);
164: }
165: } elsif ($num == 1) {
166: %{$$idlist{$$codetitles[1]}} = ();
167: %{$$idlist_titles{$$codetitles[1]}} = ();
168: foreach my $key_a (keys %{$idarrays{$$codetitles[1]}}) {
169: my @sorted_a = ();
170: my @sorted_a_titles = ();
171: &sort_cats($num,$cat_order,$codetitles,\@{$idarrays{$$codetitles[1]}{$key_a}},\@sorted_a);
172: if (defined($$cat_titles{$$codetitles[1]})) {
173: foreach (@sorted_a) {
174: push @sorted_a_titles, $$cat_titles{$$codetitles[1]}{$_};
175: }
176: }
177: $$idlist{$$codetitles[1]}{$key_a} = join('","',@sorted_a);
178: $$idnums{$$codetitles[1]}{$key_a} = scalar(@sorted_a);
179: if (defined($$cat_titles{$$codetitles[1]})) {
180: $$idlist_titles{$$codetitles[1]}{$key_a} = join('","',@sorted_a_titles);
181: }
182: }
183: } elsif ($num == 2) {
184: %{$$idlist{$$codetitles[2]}} = ();
185: %{$$idlist_titles{$$codetitles[2]}} = ();
186: foreach my $key_a (keys %{$idarrays{$$codetitles[2]}}) {
187: %{$$idlist{$$codetitles[2]}{$key_a}} = ();
188: %{$$idlist_titles{$$codetitles[2]}{$key_a}} = ();
189: foreach my $key_b (keys %{$idarrays{$$codetitles[2]}{$key_a}}) {
190: my @sorted_b = ();
191: my @sorted_b_titles = ();
192: &sort_cats($num,$cat_order,$codetitles,\@{$idarrays{$$codetitles[2]}{$key_a}{$key_b}},\@sorted_b);
193: if (defined($$cat_titles{$$codetitles[2]})) {
194: foreach (@sorted_b) {
195: push @sorted_b_titles, $$cat_titles{$$codetitles[2]}{$_};
196: }
197: }
198: $$idlist{$$codetitles[2]}{$key_a}{$key_b} = join('","',@sorted_b);
199: $$idnums{$$codetitles[2]}{$key_a}{$key_b} = scalar(@sorted_b);
200: if (defined($$cat_titles{$$codetitles[2]})) {
201: $$idlist_titles{$$codetitles[2]}{$key_a}{$key_b} = join('","',@sorted_b_titles);
202: }
203: }
204: }
205: } elsif ($num == 3) {
206: %{$$idlist{$$codetitles[3]}} = ();
207: foreach my $key_a (keys %{$idarrays{$$codetitles[3]}}) {
208: %{$$idlist{$$codetitles[3]}{$key_a}} = ();
209: foreach my $key_b (keys %{$idarrays{$$codetitles[3]}{$key_a}}) {
210: %{$$idlist{$$codetitles[3]}{$key_a}{$key_b}} = ();
211: foreach my $key_c (keys %{$idarrays{$$codetitles[3]}{$key_a}{$key_b}}) {
212: my @sorted_c = ();
213: my @sorted_c_titles = ();
214: &sort_cats($num,$cat_order,$codetitles,\@{$idarrays{$$codetitles[3]}{$key_a}{$key_b}{$key_c}},\@sorted_c);
215: if (defined($$cat_titles{$$codetitles[3]})) {
216: foreach (@sorted_c) {
217: push @sorted_c_titles, $$cat_titles{$$codetitles[3]}{$_};
218: }
219: }
220: $$idlist{$$codetitles[3]}{$key_a}{$key_b}{$key_c} = join('","',@sorted_c);
221: $$idnums{$$codetitles[3]}{$key_a}{$key_b}{$key_c} = scalar(@sorted_c);
222: if (defined($$cat_titles{$$codetitles[3]})) {
1.4 raeburn 223: $$idlist_titles{$$codetitles[3]}{$key_a}{$key_b}{$key_c} = join('","',@sorted_c_titles);
1.1 raeburn 224: }
225: }
226: }
227: }
228: } elsif ($num == 4) {
229: %{$$idlist{$$codetitles[4]}} = ();
230: foreach my $key_a (keys %{$idarrays{$$codetitles[4]}}) {
231: %{$$idlist{$$codetitles[4]}{$key_a}} = ();
232: foreach my $key_b (keys %{$idarrays{$$codetitles[4]}{$key_a}}) {
233: %{$$idlist{$$codetitles[4]}{$key_a}{$key_b}} = ();
234: foreach my $key_c (keys %{$idarrays{$$codetitles[4]}{$key_a}{$key_b}}) {
235: %{$$idlist{$$codetitles[4]}{$key_a}{$key_b}{$key_c}} = ();
236: foreach my $key_d (keys %{$idarrays{$$codetitles[4]}{$key_a}{$key_b}{$key_c}}) {
237: my @sorted_d = ();
238: my @sorted_d_titles = ();
239: &sort_cats($num,$cat_order,$codetitles,$idarrays{$$codetitles[4]}{$key_a}{$key_b}{$key_c}{$key_d},\@sorted_d);
240: if (defined($$cat_titles{$$codetitles[4]})) {
241: foreach (@sorted_d) {
242: push @sorted_d_titles, $$cat_titles{$$codetitles[4]}{$_};
243: }
244: }
245: $$idlist{$$codetitles[4]}{$key_a}{$key_b}{$key_c}{$key_d} = join('","',@sorted_d);
246: $$idnums{$$codetitles[4]}{$key_a}{$key_b}{$key_c}{$key_d} = scalar(@sorted_d);
1.4 raeburn 247: if (defined($$cat_titles{$$codetitles[4]})) {
248: $$idlist_titles{$$codetitles[3]}{$key_a}{$key_b}{$key_c}{$key_d} = join('","',@sorted_d_titles);
249: }
1.1 raeburn 250: }
251: }
252: }
253: }
254: }
255: }
256: }
257:
258: sub javascript_code_selections {
259: my ($formname,@codetitles) = @_;
260: my $numtitles = @codetitles;
261: my $output = (<<END_OF_BLOCK);
262: var display = new Array($numtitles)
263: if (caller == "" || caller == "$codetitles[0]") {
264: if (caller == "") {
265: document.$formname.Year.length = 0
266: document.$formname.Year.options[0] = new Option("Select","-1",true,true)
267: display[0] = new Array(idyears.length)
268: for (var i=0; i<idyears.length; i++) {
269: display[0][i] = idyears[i]
270: if (longtitles[0] == 1) {
271: if (idyearslongs[i] != "") {
272: display[0][i] = idyearslongs[i]
273: }
274: }
275: else {
276: if (idyearslongs[i] != "") {
277: display[0][i] = idyears[i]
278: }
279: }
280: document.$formname.Year.options[i+1] = new Option(display[0][i],idyears[i],false,false)
281: }
282: document.$formname.Year.selectedIndex = 0;
283: }
284: document.$formname.Semester.length = 0
285: document.$formname.Department.length = 0;
286: document.$formname.Number.length = 0
287: document.$formname.Department.options[0] = new Option("<-Pick $codetitles[1]","-1",true,true)
288: document.$formname.Number.options[0] = new Option("<-Pick $codetitles[2]","-1",true,true)
289: if (idyr == 0 || caller == "") {
290: document.$formname.Semester.options[0] = new Option("<-Pick $codetitles[0]","-1",true,true)
291: }
292: else {
293: document.$formname.Semester.options[0] = new Option("Select","-1",true,true)
294: display[1] = new Array(idsems[idyr-1].length)
295: for (var i=0; i<idsems[idyr-1].length; i++) {
296: display[1][i] = idsems[idyr-1][i]
297: if (longtitles[1] == 1) {
298: if (idsemslongs[idyr-1][i] != "") {
299: display[1][i] = idsemslongs[idyr-1][i]
300: }
301: }
302: document.$formname.Semester.options[i+1] = new Option(display[1][i],idsems[idyr-1][i],false,false)
303: }
304: }
305: document.$formname.Semester.selectedIndex = 0;
306: }
307: if (caller == "$codetitles[1]") {
308: document.$formname.Department.length = 0
309: document.$formname.Number.length = 0
310: document.$formname.Number.options[0] = new Option("<-Pick $codetitles[2]","-1",true,true)
311: if (idsem == 0) {
312: document.$formname.Department.options[0] = new Option("<-Pick $codetitles[1]","-1",true,true)
313: }
314: else {
315: document.$formname.Department.options[0] = new Option("Select","-1",true,true)
316: display[2] = new Array(idcodes[idyr-1][idsem-1].length)
317: for (var i=0; i<idcodes[idyr-1][idsem-1].length; i++) {
318: display[2][i] = idcodes[idyr-1][idsem-1][i]
319: if (longtitles[2] == 1) {
320: if (idcodeslongs[idyr-1][idsem-1][i] != "") {
321: display[2][i] = idcodeslongs[idyr-1][idsem-1][i]
322: }
323: }
324: document.$formname.Department.options[i+1] = new Option(display[2][i],idcodes[idyr-1][idsem-1][i],false,false)
325: }
326: }
327: document.$formname.Department.selectedIndex = 0
328: }
329: if (caller == "$codetitles[2]") {
330: document.$formname.Number.length = 0
331: if (iddept == 0) {
332: document.$formname.Number.options[0] = new Option("<-Pick $codetitles[2]","-1",true,true)
333: }
334: else {
335: document.$formname.Number.options[0] = new Option("Select","-1",true,true)
336: display[3] = new Array (idcourses[idyr-1][idsem-1][iddept-1].length)
337: for (var i=0; i<idcourses[idyr-1][idsem-1][iddept-1].length; i++) {
338: display[3][i] = idcourses[idyr-1][idsem-1][iddept-1][i]
339: if (longtitles[3] == 1) {
340: if (idcourseslongs[idyr-1][idsem-1][iddept-1][i] != "") {
341: display[3][i] = idcourseslongs[idyr-1][idsem-1][iddept-1][i]
342: }
343: }
344: document.$formname.Number.options[i+1] = new Option(display[3][i],idcourses[idyr-1][idsem-1][iddept-1][i],false,false)
345: }
346: }
347: document.$formname.Number.selectedIndex = 0
348: }
349: }
350:
351: function initialize_codes() {
352: courseSet();
353: return;
354: }
355: END_OF_BLOCK
356: }
357:
358:
359: sub javascript_definitions {
360: my ($codetitles,$idlist,$idlist_titles,$idnums,$cat_titles) = @_;
361: my $numtitles = @{$codetitles};
362: my $longtitles = [];
363: for (my $i=0; $i<$numtitles; $i++) {
364: if (defined($cat_titles->{$codetitles->[$i]})) {
365: $longtitles->[$i] = 1;
366: } else {
367: $longtitles->[$i] = 0;
368: }
369: }
370: my $scripttext;
371: if ($longtitles->[0]) {
372: $scripttext =
373: qq| var idyearslongs = new Array("$idlist_titles->{$codetitles->[0]}")\n|;
374: }
375: $scripttext .=
376: " var idsems = new Array ($idnums->{$codetitles->[0]})\n";
377: if ($longtitles->[1]) {
378: $scripttext .=
379: " var idsemslongs = new Array ($idnums->{$codetitles->[0]})\n"; }
380: $scripttext .=
381: " var idcodes = new Array ($idnums->{$codetitles->[0]})\n";
382: if ($longtitles->[2]) {
383: $scripttext .=
384: " var idcodeslongs = new Array ($idnums->{$codetitles->[0]})\n";
385: }
386: $scripttext .=
387: " var idcourses = new Array ($idnums->{$codetitles->[0]})\n";
388: if ($longtitles->[3]) {
389: $scripttext .=
390: " var idcourseslongs = new Array ($idnums->{$codetitles->[0]})\n";
391: }
392: my @sort_a = split/","/,$idlist->{$codetitles->[0]};
393: for (my $j=0; $j<@sort_a; $j++) {
394: $scripttext .= qq| idsems[$j] = new Array("$idlist->{$codetitles->[1]}{$sort_a[$j]}")\n|;
395: if ($longtitles->[1]) {
396: $scripttext .= qq| idsemslongs[$j] = new Array("$idlist_titles->{$codetitles->[1]}{$sort_a[$j]}")\n|;
397: }
398: $scripttext .= qq| idcodes[$j] = new Array($idnums->{$codetitles->[1]}{$sort_a[$j]})\n|;
399: if ($longtitles->[2]) {
400: $scripttext .= qq| idcodeslongs[$j] = new Array($idnums->{$codetitles->[1]}{$sort_a[$j]})\n|;
401: }
402: $scripttext .= qq| idcourses[$j] = new Array($idnums->{$codetitles->[1]}{$sort_a[$j]})\n|;
403: if ($longtitles->[3]) {
404: $scripttext .= qq| idcourseslongs[$j] = new Array($idnums->{$codetitles->[1]}{$sort_a[$j]})\n|;
405: }
406: my @sort_b = split/","/,$idlist->{$codetitles->[1]}{$sort_a[$j]};
407: for (my $k=0; $k<@sort_b; $k++) {
408: my $idcode_entry = $idlist->{$codetitles->[2]}{$sort_a[$j]}{$sort_b[$k]};
409: $scripttext .= qq| idcodes[$j][$k] = new Array("$idcode_entry")\n|;
410: if ($longtitles->[2]) {
411: my $idcodelong_entry = $idlist_titles->{$codetitles->[2]}{$sort_a[$j]}{$sort_b[$k]};
412: $scripttext .= qq| idcodeslongs[$j][$k] = new Array("$idcodelong_entry")\n|;
413: }
414: $scripttext .= qq| idcourses[$j][$k] = new Array($idnums->{$codetitles->[2]}{$sort_a[$j]}{$sort_b[$k]})\n|;
415: if ($longtitles->[3]) {
416: $scripttext .= qq| idcourseslongs[$j][$k] = new Array($idnums->{$codetitles->[2]}{$sort_a[$j]}{$sort_b[$k]})\n|;
417: }
418: my @sort_c = split/","/,$idlist->{$codetitles->[2]}{$sort_a[$j]}{$sort_b[$k]};
419: for (my $l=0; $l<@sort_c; $l++) {
420: my $idcourse_entry = $idlist->{$codetitles->[3]}{$sort_a[$j]}{$sort_b[$k]}{$sort_c[$l]};
421: $scripttext .= qq| idcourses[$j][$k][$l] = new Array("$idcourse_entry")\n|;
422: if ($longtitles->[3]) {
423: my $idcourselong_entry = $idlist_titles->{$codetitles->[3]}{$sort_a[$j]}{$sort_b[$k]}{$sort_c[$l]};
424: $scripttext .= qq| idcourseslongs[$j][$k][$l] = new Array("$idcourselong_entry")\n|;
425: }
426: }
427: }
428: }
429: return ($scripttext,$longtitles);
430: }
431:
432: sub courseset_js_start {
433: my ($formname,$longtitles_str,$allidlist) = @_;
434: my $output = <<END;
1.6 ! raeburn 435:
1.1 raeburn 436: function courseSet(caller) {
437: var longtitles = new Array ("$longtitles_str");
438: var idyr = document.$formname.Year.selectedIndex
439: var idsem = document.$formname.Semester.selectedIndex
440: var iddept = document.$formname.Department.selectedIndex
441: var idclass = document.$formname.Number.selectedIndex
442: var idyears = new Array("$allidlist");
443: END
444: return $output;
445: }
446:
1.6 ! raeburn 447: sub instcode_selectors_data {
! 448: my ($codedom,$formname,$cat_items,$codetitles,$cat_titles,$cat_order) = @_;
! 449: my ($jscript,%coursecodes,%codes,%idlist,%idnums,%idlist_titles,
! 450: %by_year,%by_sem,%by_dept);
! 451: my ($numtitles,$lasttitle);
! 452: my $caller = 'global';
! 453: my $totcodes = &retrieve_instcodes(\%coursecodes,$codedom);
! 454: if ($totcodes > 0) {
! 455: if (&Apache::lonnet::auto_instcode_format($caller,$codedom,\%coursecodes,
! 456: \%codes,$codetitles,$cat_titles,$cat_order) eq 'ok') {
! 457: &build_code_selections(\%codes,$codetitles,$cat_titles,$cat_order,
! 458: \%idlist,\%idnums,\%idlist_titles);
! 459: my ($scripttext,$longtitles) =
! 460: &javascript_definitions($codetitles,\%idlist,\%idlist_titles,
! 461: \%idnums,$cat_titles);
! 462: my $longtitles_str = join('","',@{$longtitles});
! 463: my $allidlist = $idlist{$codetitles->[0]};
! 464: $numtitles = @{$codetitles};
! 465: $lasttitle = $numtitles;
! 466: if ($numtitles > 4) {
! 467: $lasttitle = 4;
! 468: }
! 469: if ($numtitles == 0) {
! 470: if (!defined($env{'form.state'})) {
! 471: $env{'form.state'} = 'listing';
! 472: }
! 473: } else {
! 474: my @data = ('top');
! 475: for (my $k=0; $k<$lasttitle; $k++) {
! 476: my $cat = $codetitles->[$k];
! 477: my $level = 1;
! 478: $level = &recurse_options($codetitles->[$k],$idlist{$codetitles->[$k]},$level,$cat,$cat_items,\@data,\%by_year,\%by_sem,\%by_dept);
! 479: }
! 480: $scripttext .= &build_javascript(\%by_year,\%by_sem,\%by_dept,$cat_order,$codetitles);
! 481: $jscript .= &javascript_select_filler($formname,$scripttext,$codetitles,$longtitles_str,$allidlist);
! 482: if ($env{'form.state'} eq 'listing') {
! 483: $jscript .= '
! 484: function setElements() {
! 485: ';
! 486: for (my $i=0; $i<@{$codetitles}-1; $i++) {
! 487: if ($env{'form.'.$codetitles->[$i]} != -1) {
! 488: $jscript .= '
! 489: for (var j=0; j<document.'.$formname.'.'.$codetitles->[$i].'.length; j++) {
! 490: if (document.'.$formname.'.'.$codetitles->[$i].'[j].value == "'.$env{'form.'.$codetitles->[$i]}.'") {
! 491: document.'.$formname.'.'.$codetitles->[$i].'.selectedIndex = j;
! 492: }
! 493: }
! 494: ';
! 495: }
! 496: }
! 497: $jscript .= ' courseSet()'."\n";
! 498: if ($env{'form.'.$codetitles->[-1]} != -1) {
! 499: $jscript .= '
! 500: for (var j=0; j<document.'.$formname.'.'.$codetitles->[-1].'.length; j++) {
! 501: if (document.'.$formname.'.'.$codetitles->[-1].'[j].value == "'.$env{'form.'.$codetitles->[-1]}.'") {
! 502: document.'.$formname.'.'.$codetitles->[-1].'.selectedIndex = j;
! 503: }
! 504: }
! 505: ';
! 506: }
! 507: $jscript .= '}';
! 508: }
! 509: }
! 510: }
! 511: }
! 512: return ($jscript,$totcodes,$numtitles,$lasttitle);
! 513: }
! 514:
! 515: sub build_instcode_selectors {
! 516: my ($numtitles,$lasttitle,$cat_items,$codetitles,$cat_titles,$cat_order) = @_;
! 517: my $output;
! 518: if ($numtitles > 0) {
! 519: $output .= '<table><tr>';
! 520: for (my $k=0; $k<$lasttitle-1; $k++) {
! 521: my (@items,@unsorted);
! 522: if (ref($cat_items->{$codetitles->[$k]}) eq 'ARRAY') {
! 523: @unsorted = @{$cat_items->{$codetitles->[$k]}};
! 524: }
! 525: &sort_cats($k,$cat_order,$codetitles,\@unsorted,\@items);
! 526: my @longitems;
! 527: if (defined($cat_titles->{$codetitles->[$k]})) {
! 528: foreach my $item (@items) {
! 529: push(@longitems,$cat_titles->{$codetitles->[$k]}{$item});
! 530: }
! 531: } else {
! 532: @longitems = @items;
! 533: }
! 534: $output .= '<td align="center">'.$codetitles->[$k].'<br />'."\n".
! 535: '<select name="'.$codetitles->[$k].'" onChange="courseSet()"'.
! 536: '>'."\n".'<option value="0" />All'."\n";
! 537: for (my $i=0; $i<@items; $i++) {
! 538: if ($longitems[$i] eq '') {
! 539: $longitems[$i] = $items[$i];
! 540: }
! 541: $output .= ' <option value="'.$items[$i].'">'.$longitems[$i].'</option>';
! 542: }
! 543: $output .= '</select></td>';
! 544: }
! 545: $output .= '<td align="center">'.$codetitles->[$lasttitle-1].'<br />'."\n".
! 546: '<select name="'.$codetitles->[$lasttitle-1].'">'."\n".
! 547: '<option value="0">'.&mt('All')."\n".
! 548: '</option>'."\n".'</select>'."\n".
! 549: '</td></tr></table>'."\n";
! 550: if ($numtitles > 4) {
! 551: $output .= '<br /><br />'.$codetitles->[$numtitles-1].'<br />'."\n".
! 552: '<input type="text" name="'.$codetitles->[$numtitles-1].
! 553: '" /><br />'."\n";
! 554: }
! 555: $output .= '<br />';
! 556: }
! 557: return $output;
! 558: }
! 559:
! 560: sub recurse_options {
! 561: my ($currkey,$currlist,$level,$cat,$cat_options,$data,$by_year,$by_sem,$by_dept) = @_;
! 562: if (ref($currlist) eq 'HASH') {
! 563: $level ++;
! 564: foreach my $key (sort(keys(%{$currlist}))) {
! 565: $$data[$level-1]= $key;
! 566: &recurse_options($key,$currlist->{$key},$level,$cat,$cat_options,$data,$by_year,$by_sem,$by_dept);
! 567: }
! 568: } else {
! 569: $level --;
! 570: my @contents = split(/","/,$currlist);
! 571: foreach my $item (@contents) {
! 572: if (!grep(/^\Q$item\E$/,@{$cat_options->{$cat}})) {
! 573: push(@{$cat_options->{$cat}},$item);
! 574: }
! 575: if ($level == 3) {
! 576: if (!grep/^\Q$item\E$/,@{$by_year->{$data->[1]}->{$currkey}}) {
! 577: push(@{$by_year->{$data->[1]}->{$currkey}},$item);
! 578: }
! 579: if (!grep/^\Q$item\E$/,@{$by_sem->{$data->[2]}->{$currkey}}) {
! 580: push(@{$by_sem->{$data->[2]}->{$currkey}},$item);
! 581: }
! 582: if (!grep/^\Q$item\E$/,@{$by_dept->{$currkey}}) {
! 583: push(@{$by_dept->{$currkey}},$item);
! 584: }
! 585:
! 586: }
! 587: }
! 588: }
! 589: return $level;
! 590: }
! 591:
! 592: sub build_javascript {
! 593: my ($by_year,$by_sem,$by_dept,$cat_order,$codetitles) = @_;
! 594: my @unsorted = keys(%{$by_year});
! 595: my @sorted_yrs;
! 596: &sort_cats('0',$cat_order,$codetitles,\@unsorted,\@sorted_yrs); my $output = 'var idcse_by_yr_year = new Array("'.join('","',@sorted_yrs).'");'."\n".
! 597: 'var idcse_by_yr_dept = new Array('.scalar(@sorted_yrs).');'."\n".
! 598: 'var idcse_by_yr_num = new Array('.scalar(@sorted_yrs).');'."\n";
! 599: for (my $i=0; $i<@sorted_yrs; $i++) {
! 600: my $numkeys = keys(%{$by_year->{$sorted_yrs[$i]}});
! 601: $output .= " idcse_by_yr_num[$i] = new Array($numkeys);\n";
! 602: if (ref($by_year->{$sorted_yrs[$i]}) eq 'HASH') {
! 603: @unsorted = keys(%{$by_year->{$sorted_yrs[$i]}});
! 604: my @sorted_depts;
! 605: &Apache::courseclassifier::sort_cats('2',$cat_order,$codetitles,\@unsorted,\@sorted_depts);
! 606: $output .= qq| idcse_by_yr_dept[$i] = new Array ("|.join('","',@sorted_depts).'");'."\n";
! 607: for (my $j=0; $j<@sorted_depts; $j++) {
! 608: $output .= qq| idcse_by_yr_num[$i][$j] = new Array ("|;
! 609: $output .= join('","',sort(@{$by_year->{$sorted_yrs[$i]}->{$sorted_depts[$j]}})).'");'."\n";
! 610: }
! 611: }
! 612: }
! 613: @unsorted = keys(%{$by_sem});
! 614: my @sorted_sems;
! 615: &sort_cats('1',$cat_order,$codetitles,\@unsorted,\@sorted_sems);
! 616: $output .= 'idcse_by_sem_sems = new Array("'.join('","',@sorted_sems).'");'."\n".
! 617: 'idcse_by_sem_dept = new Array('.scalar(@sorted_sems).');'."\n".
! 618: 'idcse_by_sem_num = new Array('.scalar(@sorted_sems).');'."\n";
! 619: for (my $i=0; $i<@sorted_sems; $i++) {
! 620: my $numkeys = keys(%{$by_sem->{$sorted_sems[$i]}});
! 621: $output .= " idcse_by_sem_num[$i] = new Array($numkeys);\n";
! 622: if (ref($by_sem->{$sorted_sems[$i]}) eq 'HASH') {
! 623: @unsorted = keys(%{$by_sem->{$sorted_sems[$i]}});
! 624: my @sorted_depts;
! 625: &sort_cats('2',$cat_order,$codetitles,\@unsorted,\@sorted_depts);
! 626: $output .= qq| idcse_by_sem_dept[$i] = new Array("|.join('","',@sorted_depts).'");'."\n";
! 627: for (my $j=0; $j<@sorted_depts; $j++) {
! 628: $output .= qq| idcse_by_sem_num[$i][$j] = new Array ("|.join('","',sort(@{$by_sem->{$sorted_sems[$i]}->{$sorted_depts[$j]}})).'");'."\n";
! 629: }
! 630: }
! 631: }
! 632: @unsorted = keys(%{$by_dept});
! 633: my @sorted_deps;
! 634: &sort_cats('2',$cat_order,$codetitles,\@unsorted,\@sorted_deps);
! 635: $output .= 'idcse_by_dep = new Array('.scalar(@sorted_deps).');'."\n";
! 636: for (my $k=0; $k<@sorted_deps; $k++) {
! 637: $output .= qq| idcse_by_dep[$k] = new Array ("|.join('","',sort(@{$by_dept->{$sorted_deps[$k]}})).'");'."\n";
! 638: }
! 639: return $output;
! 640: }
! 641:
! 642: sub javascript_select_filler {
! 643: my ($formname,$scripttext,$codetitles,$longtitles_str,$allidlist) = @_;
! 644: my $output = <<END;
! 645:
! 646: function courseSet() {
! 647: var longtitles = new Array ("$longtitles_str");
! 648: var valyr = document.$formname.Year.options[document.$formname.Year.selectedIndex].value
! 649: var valsem = document.$formname.Semester.options[document.$formname.Semester.selectedIndex].value
! 650: var valdept = document.$formname.Department.options[document.$formname.Department.selectedIndex].value
! 651: var valclass = document.$formname.Number.options[document.$formname.Number.selectedIndex].value
! 652: var idyears = new Array("$allidlist");
! 653: var idyr = -1;
! 654: var idsem = -1;
! 655: var iddept = -1;
! 656: document.$formname.Number.length = 0;
! 657:
! 658: $scripttext
! 659:
! 660: selYear = document.$formname.Year.selectedIndex-1;
! 661: selSemester = document.$formname.Semester.selectedIndex-1;
! 662: selDepartment = document.$formname.Department.selectedIndex-1;
! 663: if (selYear == -1) {
! 664: if (selSemester == -1) {
! 665: if (selDepartment > -1) {
! 666: document.$formname.Number.options[0] = new Option('All','0',false,false);
! 667: for (var k=0; k<idcse_by_dep[selDepartment].length; k++) {
! 668: document.$formname.Number.options[k+1] = new Option(idcse_by_dep[selDepartment][k],idcse_by_dep[selDepartment][k],false,false);
! 669:
! 670: }
! 671: }
! 672: else {
! 673: document.$formname.Number.options[0] = new Option("All","0",true,true);
! 674: }
! 675: }
! 676: else {
! 677: if (selDepartment > -1) {
! 678: for (var i=0; i<idcse_by_sem_sems.length; i++) {
! 679: if (idcse_by_sem_sems[i] == valsem) {
! 680: idsem = i;
! 681: }
! 682: }
! 683: if (idsem != -1) {
! 684: for (var i=0; i<idcse_by_sem_dept[idsem].length; i++) {
! 685: if (idcse_by_sem_dept[idsem][i] == valdept) {
! 686: iddept = i;
! 687: }
! 688: }
! 689: }
! 690: if (iddept != -1) {
! 691: document.$formname.Number.options[0] = new Option('All','0',false,false);
! 692: for (var k=0; k<idcse_by_sem_num[idsem][iddept].length; k++) {
! 693: document.$formname.Number.options[k+1] = new Option(idcse_by_sem_num[idsem][iddept][k],idcse_by_sem_num[idsem][iddept][k],false,false);
! 694: }
! 695: }
! 696: else {
! 697: document.$formname.Number.options[0] = new Option('No courses','0',true,true);
! 698: }
! 699: }
! 700: else {
! 701: document.$formname.Number.options[0] = new Option("All","0",true,true);
! 702: }
! 703: }
! 704: }
! 705: else {
! 706: if (selSemester == -1) {
! 707: if (selDepartment > -1) {
! 708: for (var i=0; i<idcse_by_yr_year.length; i++) {
! 709: if (idcse_by_yr_year[i] == valyr) {
! 710: idyr = i;
! 711: }
! 712: }
! 713: if (idyr != -1) {
! 714: for (var i=0; i<idcse_by_yr_dept[idyr].length; i++) {
! 715: if (idcse_by_yr_dept[idyr][i] == valdept) {
! 716: iddept = i;
! 717: }
! 718: }
! 719: }
! 720: if (iddept != -1) {
! 721: document.$formname.Number.options[0] = new Option('All','0',false,false);
! 722: for (var k=0; k<idcse_by_yr_num[idyr][iddept].length; k++) {
! 723: document.$formname.Number.options[k+1] = new Option(idcse_by_yr_num[idyr][iddept][k],idcse_by_yr_num[idyr][iddept][k],false,false);
! 724: }
! 725: }
! 726: else {
! 727: document.$formname.Number.options[0] = new Option('No courses','0',true,true);
! 728: }
! 729: }
! 730: else {
! 731: document.$formname.Number.options[0] = new Option("All","0",true,true);
! 732: }
! 733: }
! 734: else {
! 735: if (selDepartment > -1) {
! 736: for (var k=0; k<idyears.length; k++) {
! 737: if (idyears[k] == valyr) {
! 738: idyr = k;
! 739: }
! 740: }
! 741: if (idyr != -1) {
! 742: for (var k=0; k<idsems[idyr].length; k++) {
! 743: if (idsems[idyr][k] == valsem) {
! 744: idsem = k;
! 745: }
! 746: }
! 747: }
! 748: if (idsem != -1) {
! 749: for (var k=0; k<idcodes[idyr][idsem].length; k++) {
! 750: if (idcodes[idyr][idsem][k] == valdept) {
! 751: iddept = k;
! 752: }
! 753: }
! 754: }
! 755: if (iddept != -1) {
! 756: document.$formname.Number.options[0] = new Option('All','0',false,false);
! 757: for (var i=0; i<idcourses[idyr][idsem][iddept].length; i++) {
! 758: var display = idcourses[idyr][idsem][iddept][i];
! 759: if (longtitles[3] == 1) {
! 760: if (idcourseslongs[idyr][idsem][iddept][i] != "") {
! 761: display = idcourseslongs[idyr][idsem][iddept][i]
! 762: }
! 763: }
! 764: document.$formname.Number.options[i+1] = new Option(display,idcourses[idyr][idsem][iddept][i],false,false)
! 765: }
! 766: }
! 767: else {
! 768: document.$formname.Number.options[0] = new Option('No courses','0',true,true);
! 769: }
! 770: }
! 771: else {
! 772: document.$formname.Number.options[0] = new Option('All','0',true,true);
! 773: }
! 774: }
! 775: document.$formname.Number.selectedIndex = 0
! 776: }
! 777: }
! 778: END
! 779: return $output;
! 780: }
! 781:
! 782: sub instcode_search_str {
! 783: my ($domain,$numtitles) = @_;
! 784: my $instcode;
! 785: if (defined($numtitles) && $numtitles == 0) {
! 786: $instcode = '.+';
! 787: } else {
! 788: my (%codedefaults,@code_order);
! 789: my $defaults_result =
! 790: &Apache::lonnet::auto_instcode_defaults($domain,\%codedefaults,
! 791: \@code_order);
! 792: if ($defaults_result eq 'ok') {
! 793: $instcode ='^';
! 794: foreach my $item (@code_order) {
! 795: if ($env{'form.'.$item} eq '0' ) {
! 796: $instcode .= $codedefaults{$item};
! 797: } else {
! 798: $instcode .= $env{'form.'.$item};
! 799: }
! 800: }
! 801: $instcode .= '$';
! 802: } else {
! 803: $instcode = '.+';
! 804: }
! 805: }
! 806: return $instcode;
! 807: }
! 808:
1.1 raeburn 809: 1;
810:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>