Annotation of loncom/interface/lonpickcourse.pm, revision 1.46

1.1       www         1: # The LearningOnline Network
                      2: # Pick a course
                      3: #
1.46    ! raeburn     4: # $Id: lonpickcourse.pm,v 1.45 2006/05/09 15:09:27 albertel Exp $
1.1       www         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: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: 
                     29: package Apache::lonpickcourse;
                     30: 
                     31: use strict;
                     32: use Apache::Constants qw(:common);
                     33: use Apache::loncommon;
                     34: use Apache::loncoursedata;
                     35: use Apache::lonnet;
1.8       www        36: use Apache::lonlocal;
1.46    ! raeburn    37: use Apache::longroup;
1.1       www        38: 
                     39: sub handler {
                     40:     my $r = shift;
1.8       www        41:     &Apache::loncommon::content_type($r,'text/html');
1.1       www        42:     $r->send_http_header;
                     43:     return OK if $r->header_only;
                     44: 
                     45: # ------------------------------------------------------------ Print the screen
1.40      albertel   46: 
1.5       www        47: # Get parameters from query string
1.1       www        48:     &Apache::loncommon::get_unprocessed_cgi
                     49:         ($ENV{'QUERY_STRING'},['domainfilter','descriptfilter',
1.13      albertel   50: 			       'sincefilter','form','cnumelement',
1.19      raeburn    51: 			       'cdomelement','cnameelement','roleelement',
1.28      raeburn    52:                                'pickedcourse','instcodefilter',
1.35      raeburn    53:                                'ownerfilter','coursefilter','multiple']);
1.5       www        54: # domain filter and selection
1.27      albertel   55:     my $domainfilter=$env{'form.domainfilter'};
1.1       www        56:     $domainfilter=~s/\W//g;
1.4       www        57:     unless ($domainfilter) { $domainfilter=''; }
                     58:     my $domainselectform=&Apache::loncommon::select_dom_form($domainfilter,
                     59:                                                           'domainfilter',1);
1.5       www        60: # description filter
1.27      albertel   61:     my $descriptfilter=$env{'form.descriptfilter'};
1.4       www        62:     unless ($descriptfilter) { $descriptfilter=''; }
1.16      albertel   63:     my $listdescript=&HTML::Entities::encode($descriptfilter,'<>&"');
1.20      raeburn    64: # institutional code filter
1.27      albertel   65:     my $instcodefilter=$env{'form.instcodefilter'};
1.20      raeburn    66:     unless ($instcodefilter) { $instcodefilter=''; }
                     67:     my $listinstcode=&HTML::Entities::encode($instcodefilter,'<>&"');
                     68: # course owner filter
1.27      albertel   69:     my $ownerfilter=$env{'form.ownerfilter'};
1.20      raeburn    70:     unless ($ownerfilter) { $ownerfilter=''; }
                     71:     my $listowner=&HTML::Entities::encode($ownerfilter,'<>&"');
1.28      raeburn    72: # course ID filter for DCs only
                     73:     my ($courseidtag,$coursefilter);
                     74:     if ($env{'user.adv'} && $domainfilter && exists($env{'user.role.dc./'.
                     75:                         $domainfilter.'/'})) {
                     76:         $coursefilter=$env{'form.coursefilter'};
                     77:         unless ($coursefilter) { $coursefilter=''; }
                     78:         my $listcourseid=&HTML::Entities::encode($coursefilter,'<>&"');
                     79:         $courseidtag = &mt('LON-CAPA course ID');
1.37      albertel   80:         $courseidtag .= qq|: <input type="text" name="coursefilter" size="25" value="$listcourseid" />
1.28      raeburn    81:         <br />
                     82:         |;
                     83:     }
1.5       www        84: # last course activity filter and selection
1.27      albertel   85:     my $sincefilter=$env{'form.sincefilter'};
1.5       www        86:     $sincefilter=~s/[^\d-]//g;
                     87:     unless ($sincefilter) { $sincefilter=-1; }
                     88:     my $sincefilterform=&Apache::loncommon::select_form($sincefilter,
                     89:        'sincefilter',('-1'=>'',
                     90:                       '86400' => 'today',
                     91:                       '604800' => 'last week',
                     92:                       '2592000' => 'last month',
                     93:                       '7776000' => 'last three months',
                     94:                       '15552000' => 'last six months',
1.9       albertel   95:                       '31104000' => 'last year',
                     96:                       'select_form_order' =>
                     97:                       ['-1','86400','604800','2592000','7776000',
                     98: 		      '15552000','31104000']));
1.19      raeburn    99: # if called from loncreateuser, report sections, then close
1.43      raeburn   100:     my (%loaditem,$seclist,$groupslist);
1.19      raeburn   101:     my $num_sections = 0;
1.27      albertel  102:     if ($env{'form.form'} eq 'cu' && $env{'form.pickedcourse'}) {
1.42      albertel  103:         $loaditem{'onload'} ="setSections()";
1.27      albertel  104:         my ($cdom,$cnum) = split/_/,$env{'form.pickedcourse'};
1.45      albertel  105: 
                    106:         my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                    107:         my @sections =  (sort {$a <=> $b} keys(%sections_count));
                    108: 	$seclist = join('","',@sections);
                    109: 
1.46    ! raeburn   110:         my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
1.44      albertel  111: 	$groupslist = join(',',sort(keys(%curr_groups)));
1.19      raeburn   112:     }
1.30      raeburn   113:     my $jscript;
                    114:     my $title = 'Selecting a course';
1.35      raeburn   115: # Setup for multiple course selections, if flag for multiples set.
                    116:     my $multiple = $env{'form.multiple'};
                    117:     my $multelement;
                    118:     if ($multiple) {
1.30      raeburn   119:         $title = 'Selecting course(s)';
                    120:         $jscript = &Apache::loncommon::check_uncheck_jscript();
1.35      raeburn   121:         $multelement = '<input type="hidden" name="multiple" value="'.$multiple.'" />';
1.30      raeburn   122:     }
1.41      albertel  123:     $r->print(&Apache::loncommon::start_page($title,undef,
1.42      albertel  124: 					     {'add_entries' => \%loaditem,
1.41      albertel  125: 					      'no_nav_bar'  => 1, }));
1.8       www       126:     my %lt=&Apache::lonlocal::texthash(
                    127: 				       'cac' => 'Course Activity',
                    128: 				       'cde' => 'Course Description',
1.20      raeburn   129: 				       'cdo' => 'Course Domain',
                    130:                                        'cin' => 'Course Institutional Code',
1.38      www       131:                                        'cow' => "Course Owner's Username");
1.13      albertel  132:     my ($name_code,$name_input);
1.27      albertel  133:     if (defined($env{'form.cnameelement'}) && $env{'form.cnameelement'} ne '') {
                    134: 	$name_code = "opener.document.$env{'form.form'}.$env{'form.cnameelement'}.value=cdesc;";
1.13      albertel  135: 	$name_input ='<input type="hidden" name="cnameelement" value="'.
1.27      albertel  136: 	    $env{'form.cnameelement'}.'" />';
1.13      albertel  137:     }
1.18      raeburn   138:     my $submitopener = '';
                    139:     my $autosubmit = '';
                    140:     my $roleelement = '';
1.19      raeburn   141:     my $lastaction = 'self.close()';
1.27      albertel  142:     if ($env{'form.form'} eq 'cu') {
1.19      raeburn   143:         $lastaction = 'document.courselist.pickedcourse.value = cdom+"_"+cname;'."\n".
                    144:                       'document.courselist.submit();';
                    145:     }
1.27      albertel  146:     my $roledom = $env{'form.roleelement'};
1.18      raeburn   147:     if ($roledom) {
                    148:         $roleelement = '<input type="hidden" name="roleelement" value="'.$roledom.'" />';
1.36      raeburn   149:         $submitopener = &Apache::lonroles::processpick();
1.18      raeburn   150:         $autosubmit = 'process_pick("'.$roledom.'")';
                    151:     }
1.1       www       152:     $r->print(<<ENDSCRIPT);
                    153: <script>
1.35      raeburn   154: function gochoose(cname,cdom,cdesc,multiple,caller) {
1.27      albertel  155:     var openerForm = "$env{'form.form'}";
1.30      raeburn   156:     courseCount = 0;
                    157:     var courses = '';
1.35      raeburn   158:     if (multiple) {
1.33      albertel  159: 	if (typeof(document.courselist.course_id.length) == 'undefined') {
                    160: 	    // only 1 course checkbox was created
                    161: 	    if (document.courselist.course_id.checked) {
                    162: 		courses = courses + document.courselist.course_id.value + "&&";
                    163: 		courseCount ++;
                    164: 	    }
                    165: 	} else {
                    166: 	    for (var j=0; j<document.courselist.course_id.length; j++) {
                    167: 		if (document.courselist.course_id[j].checked) {
                    168: 		    courses = courses + document.courselist.course_id[j].value + "&&";
                    169: 		    courseCount ++;
                    170: 		}
                    171: 	    }
                    172: 	} 
1.30      raeburn   173:         opener.document.$env{'form.form'}.coursetotal.value = courseCount;
1.33      albertel  174: 	if (typeof(opener.document.$env{'form.form'}.coursepick.length) ==
                    175: 	    'undefined') {
                    176: 	    if (opener.document.$env{'form.form'}.coursepick.value == 'specific') {  
                    177: 		opener.document.$env{'form.form'}.coursepick.checked = true;
                    178: 	    } else {
                    179: 		opener.document.$env{'form.form'}.coursepick.checked = false;
                    180: 	    }
                    181: 	} else {
                    182: 	    for (var j=0; j<opener.document.$env{'form.form'}.coursepick.length; j++) { 
                    183: 		if (opener.document.$env{'form.form'}.coursepick[j].value == 'specific') {  
                    184: 		    opener.document.$env{'form.form'}.coursepick[j].checked = true;
1.34      albertel  185: 		} else {
1.33      albertel  186: 		    opener.document.$env{'form.form'}.coursepick[j].checked = false;
                    187: 		}
                    188: 	    }
                    189: 	}
                    190: 	if (courseCount > 0) {
                    191: 	    courses = courses.substr(0,courses.length-2);
                    192: 	    opener.document.$env{'form.form'}.courselist.value = courses;
                    193: 	}
1.34      albertel  194:     } else {
1.30      raeburn   195:         $name_code
                    196:         opener.document.$env{'form.form'}.$env{'form.cnumelement'}.value=cname;
1.35      raeburn   197:         var slct=opener.document.$env{'form.form'}.$env{'form.cdomelement'};
                    198:         if (slct.options == undefined) {
1.30      raeburn   199:             opener.document.$env{'form.form'}.$env{'form.cdomelement'}.value=cdom;
1.35      raeburn   200:         }
                    201:         else {
1.30      raeburn   202:             var i;
                    203:             for (i=0;i<slct.length;i++) {
                    204:                 if (slct.options[i].value==cdom) { slct.selectedIndex=i; }
                    205:             }
1.19      raeburn   206:         }
1.1       www       207:     }
1.18      raeburn   208:     $autosubmit
1.19      raeburn   209:     $lastaction
                    210: }
                    211: 
                    212: function setSections() {
                    213:     var numSections = $num_sections
                    214:     var sectionsArray = new Array("$seclist")
1.27      albertel  215:     opener.document.$env{"form.form"}.currsec.length = 0
1.19      raeburn   216:     if (numSections == 0) {
1.27      albertel  217:         opener.document.$env{"form.form"}.currsec.multiple=false
                    218:         opener.document.$env{"form.form"}.currsec.size=1;
                    219:         opener.document.$env{"form.form"}.currsec.options[0] = new Option('No existing sections','',false,false)
1.34      albertel  220:     } else {
1.23      raeburn   221:         if (numSections == 1) {
1.27      albertel  222:             opener.document.$env{"form.form"}.currsec.multiple=false;
                    223:             opener.document.$env{"form.form"}.currsec.size=1;
                    224:             opener.document.$env{"form.form"}.currsec.options[0] = new Option('Select','',true,true);
                    225:             opener.document.$env{"form.form"}.currsec.options[1] = new Option('No section','',false,false) 
                    226:             opener.document.$env{"form.form"}.currsec.options[2] = new Option(sectionsArray[0],sectionsArray[0],false,false);
1.34      albertel  227:         } else {
1.23      raeburn   228:             for (var i=0; i<numSections; i++) {
1.27      albertel  229:                 opener.document.$env{"form.form"}.currsec.options[i] = new Option(sectionsArray[i],sectionsArray[i],false,false)
1.23      raeburn   230:             }
1.27      albertel  231:             opener.document.$env{"form.form"}.currsec.multiple=true
1.23      raeburn   232:             if (numSections < 3) {
1.27      albertel  233:                 opener.document.$env{"form.form"}.currsec.size=numSections;
1.34      albertel  234:             } else {
1.27      albertel  235:                 opener.document.$env{"form.form"}.currsec.size=3;
1.23      raeburn   236:             }
1.27      albertel  237:             opener.document.$env{"form.form"}.currsec.options[0].selected = false
1.19      raeburn   238:         }
                    239:     }
1.43      raeburn   240:     opener.document.$env{"form.form"}.groups.value='$groupslist';
1.19      raeburn   241:     self.close()
1.1       www       242: }
1.30      raeburn   243: 
                    244: $jscript
1.1       www       245: </script>
1.18      raeburn   246: $submitopener
1.30      raeburn   247: <form method="post" name="filterpicker">
1.27      albertel  248: <input type="hidden" name="cnumelement" value="$env{'form.cnumelement'}" />
                    249: <input type="hidden" name="cdomelement" value="$env{'form.cdomelement'}" />
1.13      albertel  250: $name_input
1.27      albertel  251: <input type="hidden" name="form" value="$env{'form.form'}" />
1.18      raeburn   252: $roleelement
1.35      raeburn   253: $multelement
1.8       www       254: $lt{'cac'}: $sincefilterform
1.4       www       255: <br />
1.8       www       256: $lt{'cdo'}: $domainselectform
1.4       www       257: <br />
1.20      raeburn   258: $lt{'cin'}:
                    259: <input type="text" name="instcodefilter" size="10" value="$listinstcode" />
                    260: <br />
                    261: $lt{'cow'}:
                    262: <input type="text" name="ownerfilter" size="10" value="$listowner" />
                    263: <br />
1.28      raeburn   264: $courseidtag
1.8       www       265: $lt{'cde'}:
1.15      www       266: <input type="text" name="descriptfilter" size="40" value="$listdescript" />
1.5       www       267: <p><input type="submit" name="gosearch" value="Search" /></p>
1.4       www       268: </form>
1.5       www       269: <hr />
1.1       www       270: ENDSCRIPT
1.2       www       271: # ---------------------------------------------------------------- Get the data
1.27      albertel  272:     if ($env{'form.gosearch'}) {
1.8       www       273:         $r->print(&mt('Searching').' ...<br />&nbsp;<br />');
1.6       www       274:         $r->rflush();
1.5       www       275: 	unless ($descriptfilter) { $descriptfilter='.'; }
1.20      raeburn   276:         unless ($instcodefilter) { $instcodefilter='.'; }
                    277:         unless ($ownerfilter) { $ownerfilter='.'; }
1.28      raeburn   278:         unless ($coursefilter) { $coursefilter='.'; }
1.5       www       279:         my $timefilter=($sincefilter==-1?1:time-$sincefilter);
                    280: 	my %courses=
                    281: 	    &Apache::lonnet::courseiddump
1.28      raeburn   282: 	    ($domainfilter,$descriptfilter,$timefilter,$instcodefilter,$ownerfilter,$coursefilter);
1.19      raeburn   283: 	$r->print('<form name="courselist">');
1.10      albertel  284: 	my %by_descrip;
1.31      albertel  285:         my $numcourses = keys(%courses);
                    286: 	foreach my $course (keys(%courses)) {
1.17      raeburn   287:             my $descr;
1.38      www       288:             if ($courses{$course} =~ m/^([^:]*):/i) {
1.17      raeburn   289:                 $descr = &Apache::lonnet::unescape($1);
                    290:             } else {
                    291:                 $descr = &Apache::lonnet::unescape($courses{$course});
                    292:             }
1.38      www       293:             my $description = $descr;
1.25      raeburn   294:             push (@{$by_descrip{$description}}, $course);
1.10      albertel  295: 	}
1.35      raeburn   296:         if ($numcourses > 1 && $multiple) {
1.30      raeburn   297:             $r->print('<input type="button" value="check all"
                    298:                     onclick="javascript:checkAll(document.courselist.course_id)" />                    &nbsp;&nbsp;<input type="button" value="uncheck all"
                    299:                     onclick="javascript:uncheckAll(document.courselist.course_id)" />
                    300:                     <br /><br />');
                    301:         }
1.39      albertel  302: 	foreach my $description (sort 
                    303: 				 { lc($a) cmp lc($b) } (keys(%by_descrip))) {
1.24      raeburn   304:             foreach my $course (@{$by_descrip{$description}}) {
                    305: 	        my $cleandesc=&HTML::Entities::encode($description,'<>&"');
                    306: 	        $cleandesc=~s/'/\\'/g;
                    307: 	        my ($cdom,$cnum)=split(/\_/,$course);
                    308:                 my ($descr,$instcode,$owner) = split/:/,$courses{$course};
1.35      raeburn   309:                 $r->print(&course_chooser($multiple,$cdom,$cnum,$cleandesc));
1.30      raeburn   310: 	        $r->print($description.'('.
1.24      raeburn   311: 		      ($Apache::lonnet::domaindescription{$cdom}?
                    312:                        $Apache::lonnet::domaindescription{$cdom}:$cdom).")");
                    313:                 unless ($instcode eq '') {
                    314:                     $r->print(" - ".&Apache::lonnet::unescape($instcode));
                    315:                 }
                    316:                 unless ($owner eq '') {
                    317:                     $r->print(", owner - ".&Apache::lonnet::unescape($owner));
                    318:                 }
1.35      raeburn   319: 		if ($multiple) { $r->print("</label>\n"); }
1.24      raeburn   320:                 $r->print("<br />\n");
                    321:             }
1.5       www       322: 	}
1.30      raeburn   323: 	if (!%courses) { 
                    324:             $r->print(&mt('None found'));
1.35      raeburn   325:         } elsif ($multiple) {
1.30      raeburn   326:             $r->print('<input type="button" value="Submit" onClick="gochoose('."'','','',1)".'" />');
                    327:         }
1.27      albertel  328:         $r->print('<input type="hidden" name="form" value="'.$env{'form.form'}.'" />'."\n".
1.19      raeburn   329:                    '<input type="hidden" name="pickedcourse" value="" />'."\n".
1.27      albertel  330:                    '<input type="hidden" name="cnumelement" value="'.$env{'form.cnumelement'}.'" />'."\n".
1.36      raeburn   331:                    '<input type="hidden" name="cdomelement" value="'.$env{'form.cdomelement'}.'" />'."\n");
                    332:         if ((exists($env{'form.roleelement'})) && ($env{'form.form'} eq 'rolechoice')) {
                    333:            $r->print('<input type="hidden" name="roleelement" value="'.$env{'form.roleelement'}.'" />'."\n");
                    334:         }
                    335:         $r->print("</form>\n");
1.2       www       336:     }
1.40      albertel  337:     $r->print(&Apache::loncommon::end_page());
1.1       www       338:     return OK;
1.18      raeburn   339: }
1.1       www       340: 
1.30      raeburn   341: sub course_chooser {
1.35      raeburn   342:     my ($multiple,$cdom,$cnum,$cleandesc) = @_;
1.30      raeburn   343:     my $output; 
1.35      raeburn   344:     if ($multiple) {
1.32      albertel  345:         $output = '<label><input type="checkbox" name="course_id" value="'.$cdom.'_'.$cnum.'" />'."\n";
1.30      raeburn   346:     } else {
                    347:         $output = '<input type="button" value="Select" onClick="gochoose('.
                    348:                   "'".$cnum."','".$cdom."','".$cleandesc."')".'" />'."\n";
                    349:     }
                    350:     return $output;
                    351: } 
                    352: 
1.1       www       353: 1;
                    354: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>