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