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