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