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

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

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