File:  [LON-CAPA] / loncom / interface / lonpickcourse.pm
Revision 1.44: download - view: text, annotated - select for diffs
Tue May 9 14:38:10 2006 UTC (18 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- changing loncommon::coursegroups to be easier to use

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

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