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