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