Annotation of loncom/interface/courseclassifier.pm, revision 1.7
1.5 raeburn 1: # The LearningOnline Network with CAPA
2: # Utilities for classifying courses based on institutional code
3: #
1.7 ! raeburn 4: # $Id: courseclassifier.pm,v 1.6 2009/05/01 01:30:08 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 {
1.7 ! raeburn 448: my ($codedom,$formname,$cat_items,$codetitles,$cat_titles,$cat_order,$officialjs) = @_;
1.6 raeburn 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);
1.7 ! raeburn 481: if ($officialjs) {
! 482: $jscript .= '
! 483:
! 484: function toggleOfficial() {';
! 485: for (my $i=0; $i<@{$codetitles}-1; $i++) {
! 486: $jscript .= '
! 487: document.'.$formname.'.'.$codetitles->[$i].'.selectedIndex = 0;';
! 488: }
! 489: $jscript .= '
! 490: }
! 491:
! 492: function setOfficial(caller) {
! 493: if (caller.options[caller.selectedIndex].value != 0) {
! 494: if (document.'.$formname.'.official.length > 0) {
! 495: for (var j=0; j<document.'.$formname.'.official.length; j++) {
! 496: if (document.'.$formname.'.official[j].value == 1) {
! 497: document.'.$formname.'.official[j].checked = true;
! 498: }
! 499: }
! 500: }
! 501: }
! 502: return;
! 503: }
! 504:
! 505: ';
! 506:
! 507: $officialjs = 'setOfficial(caller);';
! 508: }
! 509: $jscript .= &javascript_select_filler($formname,$scripttext,$codetitles,$longtitles_str,$allidlist,$officialjs);
1.6 raeburn 510: if ($env{'form.state'} eq 'listing') {
511: $jscript .= '
512: function setElements() {
513: ';
514: for (my $i=0; $i<@{$codetitles}-1; $i++) {
515: if ($env{'form.'.$codetitles->[$i]} != -1) {
516: $jscript .= '
517: for (var j=0; j<document.'.$formname.'.'.$codetitles->[$i].'.length; j++) {
518: if (document.'.$formname.'.'.$codetitles->[$i].'[j].value == "'.$env{'form.'.$codetitles->[$i]}.'") {
519: document.'.$formname.'.'.$codetitles->[$i].'.selectedIndex = j;
520: }
521: }
522: ';
523: }
524: }
525: $jscript .= ' courseSet()'."\n";
526: if ($env{'form.'.$codetitles->[-1]} != -1) {
527: $jscript .= '
528: for (var j=0; j<document.'.$formname.'.'.$codetitles->[-1].'.length; j++) {
529: if (document.'.$formname.'.'.$codetitles->[-1].'[j].value == "'.$env{'form.'.$codetitles->[-1]}.'") {
530: document.'.$formname.'.'.$codetitles->[-1].'.selectedIndex = j;
531: }
532: }
533: ';
534: }
535: $jscript .= '}';
536: }
537: }
538: }
539: }
540: return ($jscript,$totcodes,$numtitles,$lasttitle);
541: }
542:
543: sub build_instcode_selectors {
544: my ($numtitles,$lasttitle,$cat_items,$codetitles,$cat_titles,$cat_order) = @_;
545: my $output;
546: if ($numtitles > 0) {
547: $output .= '<table><tr>';
548: for (my $k=0; $k<$lasttitle-1; $k++) {
549: my (@items,@unsorted);
550: if (ref($cat_items->{$codetitles->[$k]}) eq 'ARRAY') {
551: @unsorted = @{$cat_items->{$codetitles->[$k]}};
552: }
553: &sort_cats($k,$cat_order,$codetitles,\@unsorted,\@items);
554: my @longitems;
555: if (defined($cat_titles->{$codetitles->[$k]})) {
556: foreach my $item (@items) {
557: push(@longitems,$cat_titles->{$codetitles->[$k]}{$item});
558: }
559: } else {
560: @longitems = @items;
561: }
562: $output .= '<td align="center">'.$codetitles->[$k].'<br />'."\n".
1.7 ! raeburn 563: '<select name="'.$codetitles->[$k].'" onChange="courseSet(this)"'.
1.6 raeburn 564: '>'."\n".'<option value="0" />All'."\n";
565: for (my $i=0; $i<@items; $i++) {
566: if ($longitems[$i] eq '') {
567: $longitems[$i] = $items[$i];
568: }
569: $output .= ' <option value="'.$items[$i].'">'.$longitems[$i].'</option>';
570: }
571: $output .= '</select></td>';
572: }
573: $output .= '<td align="center">'.$codetitles->[$lasttitle-1].'<br />'."\n".
574: '<select name="'.$codetitles->[$lasttitle-1].'">'."\n".
575: '<option value="0">'.&mt('All')."\n".
576: '</option>'."\n".'</select>'."\n".
577: '</td></tr></table>'."\n";
578: if ($numtitles > 4) {
579: $output .= '<br /><br />'.$codetitles->[$numtitles-1].'<br />'."\n".
580: '<input type="text" name="'.$codetitles->[$numtitles-1].
581: '" /><br />'."\n";
582: }
583: $output .= '<br />';
584: }
585: return $output;
586: }
587:
588: sub recurse_options {
589: my ($currkey,$currlist,$level,$cat,$cat_options,$data,$by_year,$by_sem,$by_dept) = @_;
590: if (ref($currlist) eq 'HASH') {
591: $level ++;
592: foreach my $key (sort(keys(%{$currlist}))) {
593: $$data[$level-1]= $key;
594: &recurse_options($key,$currlist->{$key},$level,$cat,$cat_options,$data,$by_year,$by_sem,$by_dept);
595: }
596: } else {
597: $level --;
598: my @contents = split(/","/,$currlist);
599: foreach my $item (@contents) {
600: if (!grep(/^\Q$item\E$/,@{$cat_options->{$cat}})) {
601: push(@{$cat_options->{$cat}},$item);
602: }
603: if ($level == 3) {
604: if (!grep/^\Q$item\E$/,@{$by_year->{$data->[1]}->{$currkey}}) {
605: push(@{$by_year->{$data->[1]}->{$currkey}},$item);
606: }
607: if (!grep/^\Q$item\E$/,@{$by_sem->{$data->[2]}->{$currkey}}) {
608: push(@{$by_sem->{$data->[2]}->{$currkey}},$item);
609: }
610: if (!grep/^\Q$item\E$/,@{$by_dept->{$currkey}}) {
611: push(@{$by_dept->{$currkey}},$item);
612: }
613:
614: }
615: }
616: }
617: return $level;
618: }
619:
620: sub build_javascript {
621: my ($by_year,$by_sem,$by_dept,$cat_order,$codetitles) = @_;
622: my @unsorted = keys(%{$by_year});
623: my @sorted_yrs;
624: &sort_cats('0',$cat_order,$codetitles,\@unsorted,\@sorted_yrs); my $output = 'var idcse_by_yr_year = new Array("'.join('","',@sorted_yrs).'");'."\n".
625: 'var idcse_by_yr_dept = new Array('.scalar(@sorted_yrs).');'."\n".
626: 'var idcse_by_yr_num = new Array('.scalar(@sorted_yrs).');'."\n";
627: for (my $i=0; $i<@sorted_yrs; $i++) {
628: my $numkeys = keys(%{$by_year->{$sorted_yrs[$i]}});
629: $output .= " idcse_by_yr_num[$i] = new Array($numkeys);\n";
630: if (ref($by_year->{$sorted_yrs[$i]}) eq 'HASH') {
631: @unsorted = keys(%{$by_year->{$sorted_yrs[$i]}});
632: my @sorted_depts;
633: &Apache::courseclassifier::sort_cats('2',$cat_order,$codetitles,\@unsorted,\@sorted_depts);
634: $output .= qq| idcse_by_yr_dept[$i] = new Array ("|.join('","',@sorted_depts).'");'."\n";
635: for (my $j=0; $j<@sorted_depts; $j++) {
636: $output .= qq| idcse_by_yr_num[$i][$j] = new Array ("|;
637: $output .= join('","',sort(@{$by_year->{$sorted_yrs[$i]}->{$sorted_depts[$j]}})).'");'."\n";
638: }
639: }
640: }
641: @unsorted = keys(%{$by_sem});
642: my @sorted_sems;
643: &sort_cats('1',$cat_order,$codetitles,\@unsorted,\@sorted_sems);
644: $output .= 'idcse_by_sem_sems = new Array("'.join('","',@sorted_sems).'");'."\n".
645: 'idcse_by_sem_dept = new Array('.scalar(@sorted_sems).');'."\n".
646: 'idcse_by_sem_num = new Array('.scalar(@sorted_sems).');'."\n";
647: for (my $i=0; $i<@sorted_sems; $i++) {
648: my $numkeys = keys(%{$by_sem->{$sorted_sems[$i]}});
649: $output .= " idcse_by_sem_num[$i] = new Array($numkeys);\n";
650: if (ref($by_sem->{$sorted_sems[$i]}) eq 'HASH') {
651: @unsorted = keys(%{$by_sem->{$sorted_sems[$i]}});
652: my @sorted_depts;
653: &sort_cats('2',$cat_order,$codetitles,\@unsorted,\@sorted_depts);
654: $output .= qq| idcse_by_sem_dept[$i] = new Array("|.join('","',@sorted_depts).'");'."\n";
655: for (my $j=0; $j<@sorted_depts; $j++) {
656: $output .= qq| idcse_by_sem_num[$i][$j] = new Array ("|.join('","',sort(@{$by_sem->{$sorted_sems[$i]}->{$sorted_depts[$j]}})).'");'."\n";
657: }
658: }
659: }
660: @unsorted = keys(%{$by_dept});
661: my @sorted_deps;
662: &sort_cats('2',$cat_order,$codetitles,\@unsorted,\@sorted_deps);
663: $output .= 'idcse_by_dep = new Array('.scalar(@sorted_deps).');'."\n";
664: for (my $k=0; $k<@sorted_deps; $k++) {
665: $output .= qq| idcse_by_dep[$k] = new Array ("|.join('","',sort(@{$by_dept->{$sorted_deps[$k]}})).'");'."\n";
666: }
667: return $output;
668: }
669:
670: sub javascript_select_filler {
1.7 ! raeburn 671: my ($formname,$scripttext,$codetitles,$longtitles_str,$allidlist,$officialjs) = @_;
1.6 raeburn 672: my $output = <<END;
673:
1.7 ! raeburn 674: function courseSet(caller) {
1.6 raeburn 675: var longtitles = new Array ("$longtitles_str");
676: var valyr = document.$formname.Year.options[document.$formname.Year.selectedIndex].value
677: var valsem = document.$formname.Semester.options[document.$formname.Semester.selectedIndex].value
678: var valdept = document.$formname.Department.options[document.$formname.Department.selectedIndex].value
679: var valclass = document.$formname.Number.options[document.$formname.Number.selectedIndex].value
680: var idyears = new Array("$allidlist");
681: var idyr = -1;
682: var idsem = -1;
683: var iddept = -1;
684: document.$formname.Number.length = 0;
685:
686: $scripttext
687:
688: selYear = document.$formname.Year.selectedIndex-1;
689: selSemester = document.$formname.Semester.selectedIndex-1;
690: selDepartment = document.$formname.Department.selectedIndex-1;
691: if (selYear == -1) {
692: if (selSemester == -1) {
693: if (selDepartment > -1) {
694: document.$formname.Number.options[0] = new Option('All','0',false,false);
695: for (var k=0; k<idcse_by_dep[selDepartment].length; k++) {
696: document.$formname.Number.options[k+1] = new Option(idcse_by_dep[selDepartment][k],idcse_by_dep[selDepartment][k],false,false);
697:
698: }
699: }
700: else {
701: document.$formname.Number.options[0] = new Option("All","0",true,true);
702: }
703: }
704: else {
705: if (selDepartment > -1) {
706: for (var i=0; i<idcse_by_sem_sems.length; i++) {
707: if (idcse_by_sem_sems[i] == valsem) {
708: idsem = i;
709: }
710: }
711: if (idsem != -1) {
712: for (var i=0; i<idcse_by_sem_dept[idsem].length; i++) {
713: if (idcse_by_sem_dept[idsem][i] == valdept) {
714: iddept = i;
715: }
716: }
717: }
718: if (iddept != -1) {
719: document.$formname.Number.options[0] = new Option('All','0',false,false);
720: for (var k=0; k<idcse_by_sem_num[idsem][iddept].length; k++) {
721: document.$formname.Number.options[k+1] = new Option(idcse_by_sem_num[idsem][iddept][k],idcse_by_sem_num[idsem][iddept][k],false,false);
722: }
723: }
724: else {
725: document.$formname.Number.options[0] = new Option('No courses','0',true,true);
726: }
727: }
728: else {
729: document.$formname.Number.options[0] = new Option("All","0",true,true);
730: }
731: }
732: }
733: else {
734: if (selSemester == -1) {
735: if (selDepartment > -1) {
736: for (var i=0; i<idcse_by_yr_year.length; i++) {
737: if (idcse_by_yr_year[i] == valyr) {
738: idyr = i;
739: }
740: }
741: if (idyr != -1) {
742: for (var i=0; i<idcse_by_yr_dept[idyr].length; i++) {
743: if (idcse_by_yr_dept[idyr][i] == valdept) {
744: iddept = i;
745: }
746: }
747: }
748: if (iddept != -1) {
749: document.$formname.Number.options[0] = new Option('All','0',false,false);
750: for (var k=0; k<idcse_by_yr_num[idyr][iddept].length; k++) {
751: document.$formname.Number.options[k+1] = new Option(idcse_by_yr_num[idyr][iddept][k],idcse_by_yr_num[idyr][iddept][k],false,false);
752: }
753: }
754: else {
755: document.$formname.Number.options[0] = new Option('No courses','0',true,true);
756: }
757: }
758: else {
759: document.$formname.Number.options[0] = new Option("All","0",true,true);
760: }
761: }
762: else {
763: if (selDepartment > -1) {
764: for (var k=0; k<idyears.length; k++) {
765: if (idyears[k] == valyr) {
766: idyr = k;
767: }
768: }
769: if (idyr != -1) {
770: for (var k=0; k<idsems[idyr].length; k++) {
771: if (idsems[idyr][k] == valsem) {
772: idsem = k;
773: }
774: }
775: }
776: if (idsem != -1) {
777: for (var k=0; k<idcodes[idyr][idsem].length; k++) {
778: if (idcodes[idyr][idsem][k] == valdept) {
779: iddept = k;
780: }
781: }
782: }
783: if (iddept != -1) {
784: document.$formname.Number.options[0] = new Option('All','0',false,false);
785: for (var i=0; i<idcourses[idyr][idsem][iddept].length; i++) {
786: var display = idcourses[idyr][idsem][iddept][i];
787: if (longtitles[3] == 1) {
788: if (idcourseslongs[idyr][idsem][iddept][i] != "") {
789: display = idcourseslongs[idyr][idsem][iddept][i]
790: }
791: }
792: document.$formname.Number.options[i+1] = new Option(display,idcourses[idyr][idsem][iddept][i],false,false)
793: }
794: }
795: else {
796: document.$formname.Number.options[0] = new Option('No courses','0',true,true);
797: }
798: }
799: else {
800: document.$formname.Number.options[0] = new Option('All','0',true,true);
801: }
802: }
803: document.$formname.Number.selectedIndex = 0
804: }
1.7 ! raeburn 805: $officialjs
1.6 raeburn 806: }
807: END
808: return $output;
809: }
810:
811: sub instcode_search_str {
812: my ($domain,$numtitles) = @_;
813: my $instcode;
814: if (defined($numtitles) && $numtitles == 0) {
815: $instcode = '.+';
816: } else {
817: my (%codedefaults,@code_order);
818: my $defaults_result =
819: &Apache::lonnet::auto_instcode_defaults($domain,\%codedefaults,
820: \@code_order);
821: if ($defaults_result eq 'ok') {
822: $instcode ='^';
823: foreach my $item (@code_order) {
824: if ($env{'form.'.$item} eq '0' ) {
825: $instcode .= $codedefaults{$item};
826: } else {
827: $instcode .= $env{'form.'.$item};
828: }
829: }
830: $instcode .= '$';
831: } else {
832: $instcode = '.+';
833: }
834: }
835: return $instcode;
836: }
837:
1.1 raeburn 838: 1;
839:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>