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

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

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