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

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

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