File:  [LON-CAPA] / loncom / interface / lonpickcourse.pm
Revision 1.23: download - view: text, annotated - select for diffs
Tue Dec 28 05:55:11 2004 UTC (19 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: version_1_3_1, HEAD
When lonpickcourse is being used to display existing sections in CUSR, use a select box with single select if there is only one existing section, to minimize confusion.

    1: # The LearningOnline Network
    2: # Pick a course
    3: #
    4: # $Id: lonpickcourse.pm,v 1.23 2004/12/28 05:55:11 raeburn 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:     $r->print(<<ENDDOCUMENT);
   46: <html>
   47: <head>
   48: <title>The LearningOnline Network with CAPA</title>
   49: </head>
   50: ENDDOCUMENT
   51: # Get parameters from query string
   52:     &Apache::loncommon::get_unprocessed_cgi
   53:         ($ENV{'QUERY_STRING'},['domainfilter','descriptfilter',
   54: 			       'sincefilter','form','cnumelement',
   55: 			       'cdomelement','cnameelement','roleelement',
   56:                                'pickedcourse','instcodefilter','ownerfilter']);
   57: # domain filter and selection
   58:     my $domainfilter=$ENV{'form.domainfilter'};
   59:     $domainfilter=~s/\W//g;
   60:     unless ($domainfilter) { $domainfilter=''; }
   61:     my $domainselectform=&Apache::loncommon::select_dom_form($domainfilter,
   62:                                                           'domainfilter',1);
   63: # description filter
   64:     my $descriptfilter=$ENV{'form.descriptfilter'};
   65:     unless ($descriptfilter) { $descriptfilter=''; }
   66:     my $listdescript=&HTML::Entities::encode($descriptfilter,'<>&"');
   67: # institutional code filter
   68:     my $instcodefilter=$ENV{'form.instcodefilter'};
   69:     unless ($instcodefilter) { $instcodefilter=''; }
   70:     my $listinstcode=&HTML::Entities::encode($instcodefilter,'<>&"');
   71: # course owner filter
   72:     my $ownerfilter=$ENV{'form.ownerfilter'};
   73:     unless ($ownerfilter) { $ownerfilter=''; }
   74:     my $listowner=&HTML::Entities::encode($ownerfilter,'<>&"');
   75: # last course activity filter and selection
   76:     my $sincefilter=$ENV{'form.sincefilter'};
   77:     $sincefilter=~s/[^\d-]//g;
   78:     unless ($sincefilter) { $sincefilter=-1; }
   79:     my $sincefilterform=&Apache::loncommon::select_form($sincefilter,
   80:        'sincefilter',('-1'=>'',
   81:                       '86400' => 'today',
   82:                       '604800' => 'last week',
   83:                       '2592000' => 'last month',
   84:                       '7776000' => 'last three months',
   85:                       '15552000' => 'last six months',
   86:                       '31104000' => 'last year',
   87:                       'select_form_order' =>
   88:                       ['-1','86400','604800','2592000','7776000',
   89: 		      '15552000','31104000']));
   90: # if called from loncreateuser, report sections, then close
   91:     my ($loaditem,$seclist);
   92:     my $num_sections = 0;
   93:     if ($ENV{'form.form'} eq 'cu' && $ENV{'form.pickedcourse'}) {
   94:         $loaditem = 'onLoad="setSections()"';
   95:         my ($cdom,$cnum) = split/_/,$ENV{'form.pickedcourse'};
   96:         my %sections_count = ();
   97:         $num_sections = &Apache::loncommon::get_sections($cdom,$cnum,\%sections_count);
   98:         my @sections = ();
   99:         if ($num_sections > 0) {
  100:             @sections = (sort {$a <=> $b} keys %sections_count);
  101:             $seclist = join('","',@sections);
  102:         }
  103:     }
  104:     $r->print(&Apache::loncommon::bodytag("Selecting a Course",undef,$loaditem));
  105:     my %lt=&Apache::lonlocal::texthash(
  106: 				       'cac' => 'Course Activity',
  107: 				       'cde' => 'Course Description',
  108: 				       'cdo' => 'Course Domain',
  109:                                        'cin' => 'Course Institutional Code',
  110:                                        'cow' => "Course Owner's usename");
  111:     my ($name_code,$name_input);
  112:     if (defined($ENV{'form.cnameelement'}) && $ENV{'form.cnameelement'} ne '') {
  113: 	$name_code = "opener.document.$ENV{'form.form'}.$ENV{'form.cnameelement'}.value=cdesc;";
  114: 	$name_input ='<input type="hidden" name="cnameelement" value="'.
  115: 	    $ENV{'form.cnameelement'}.'" />';
  116:     }
  117:     my $submitopener = '';
  118:     my $autosubmit = '';
  119:     my $roleelement = '';
  120:     my $lastaction = 'self.close()';
  121:     if ($ENV{'form.form'} eq 'cu') {
  122:         $lastaction = 'document.courselist.pickedcourse.value = cdom+"_"+cname;'."\n".
  123:                       'document.courselist.submit();';
  124:     }
  125:     my $roledom = $ENV{'form.roleelement'};
  126:     if ($roledom) {
  127:         $roleelement = '<input type="hidden" name="roleelement" value="'.$roledom.'" />';
  128:         $submitopener = &Apache::lonroles::processpick($roledom);
  129:         $autosubmit = 'process_pick("'.$roledom.'")';
  130:     }
  131:     $r->print(<<ENDSCRIPT);
  132: <script>
  133: function gochoose(cname,cdom,cdesc) {
  134:     $name_code
  135:     var openerForm = "$ENV{'form.form'}";
  136:     opener.document.$ENV{'form.form'}.$ENV{'form.cnumelement'}.value=cname;
  137:     if (openerForm == 'cu' || openerForm == 'rolechoice') {
  138:          opener.document.$ENV{'form.form'}.$ENV{'form.cdomelement'}.value=cdom;
  139:     }
  140:     else {
  141:         var slct=opener.document.$ENV{'form.form'}.$ENV{'form.cdomelement'};
  142:         var i;
  143:         for (i=0;i<slct.length;i++) {
  144:             if (slct.options[i].value==cdom) { slct.selectedIndex=i; }
  145:         }
  146:     }
  147:     $autosubmit
  148:     $lastaction
  149: }
  150: 
  151: function setSections() {
  152:     var numSections = $num_sections
  153:     var sectionsArray = new Array("$seclist")
  154:     opener.document.$ENV{"form.form"}.currsec.length = 0
  155:     if (numSections == 0) {
  156:         opener.document.$ENV{"form.form"}.currsec.multiple=false
  157:         opener.document.$ENV{"form.form"}.currsec.size=1;
  158:         opener.document.$ENV{"form.form"}.currsec.options[0] = new Option('No existing sections','',false,false)
  159:     }
  160:     else {
  161:         if (numSections == 1) {
  162:             opener.document.$ENV{"form.form"}.currsec.multiple=false;
  163:             opener.document.$ENV{"form.form"}.currsec.size=1;
  164:             opener.document.$ENV{"form.form"}.currsec.options[0] = new Option('Select','',true,true);
  165:             opener.document.$ENV{"form.form"}.currsec.options[1] = new Option('No section','',false,false) 
  166:             opener.document.$ENV{"form.form"}.currsec.options[2] = new Option(sectionsArray[0],sectionsArray[0],false,false);
  167:         }
  168:         else {
  169:             for (var i=0; i<numSections; i++) {
  170:                 opener.document.$ENV{"form.form"}.currsec.options[i] = new Option(sectionsArray[i],sectionsArray[i],false,false)
  171:             }
  172:             opener.document.$ENV{"form.form"}.currsec.multiple=true
  173:             if (numSections < 3) {
  174:                 opener.document.$ENV{"form.form"}.currsec.size=numSections;
  175:             }
  176:             else {
  177:                 opener.document.$ENV{"form.form"}.currsec.size=3;
  178:             }
  179:             opener.document.$ENV{"form.form"}.currsec.options[0].selected = false
  180:         }
  181:     }
  182:     self.close()
  183: }
  184: </script>
  185: $submitopener
  186: <form method="post">
  187: <input type="hidden" name="cnumelement" value="$ENV{'form.cnumelement'}" />
  188: <input type="hidden" name="cdomelement" value="$ENV{'form.cdomelement'}" />
  189: $name_input
  190: <input type="hidden" name="form" value="$ENV{'form.form'}" />
  191: $roleelement
  192: $lt{'cac'}: $sincefilterform
  193: <br />
  194: $lt{'cdo'}: $domainselectform
  195: <br />
  196: $lt{'cin'}:
  197: <input type="text" name="instcodefilter" size="10" value="$listinstcode" />
  198: <br />
  199: $lt{'cow'}:
  200: <input type="text" name="ownerfilter" size="10" value="$listowner" />
  201: <br />
  202: $lt{'cde'}:
  203: <input type="text" name="descriptfilter" size="40" value="$listdescript" />
  204: <p><input type="submit" name="gosearch" value="Search" /></p>
  205: </form>
  206: <hr />
  207: ENDSCRIPT
  208: # ---------------------------------------------------------------- Get the data
  209:     if ($ENV{'form.gosearch'}) {
  210:         $r->print(&mt('Searching').' ...<br />&nbsp;<br />');
  211:         $r->rflush();
  212: 	unless ($descriptfilter) { $descriptfilter='.'; }
  213:         unless ($instcodefilter) { $instcodefilter='.'; }
  214:         unless ($ownerfilter) { $ownerfilter='.'; }
  215:         my $timefilter=($sincefilter==-1?1:time-$sincefilter);
  216: 	my %courses=
  217: 	    &Apache::lonnet::courseiddump
  218: 	    ($domainfilter,$descriptfilter,$timefilter,$instcodefilter,$ownerfilter);
  219: 	$r->print('<form name="courselist">');
  220: 	my %by_descrip;
  221: 	foreach my $course (keys %courses) {
  222:             my $descr;
  223:             if ($courses{$course} =~ m/^([^:]*):/) {
  224:                 $descr = &Apache::lonnet::unescape($1);
  225:             } else {
  226:                 $descr = &Apache::lonnet::unescape($courses{$course});
  227:             }
  228:             $by_descrip{lc($descr)}=$course;
  229: 	}
  230: 	foreach my $description (sort(keys(%by_descrip))) {
  231: 	    my $course=$by_descrip{$description};
  232: 	    my $cleandesc=&HTML::Entities::encode($description,'<>&"');
  233: 	    $cleandesc=~s/'/\\'/g;
  234: 	    my ($cdom,$cnum)=split(/\_/,$course);
  235: 	    $r->print('<input type="button" value="Select" onClick="gochoose('.
  236: 		  "'".$cnum."','".$cdom."','".$cleandesc."')".'" /> '.$description.' ('.
  237: 		  ($Apache::lonnet::domaindescription{$cdom}?
  238:                    $Apache::lonnet::domaindescription{$cdom}:$cdom).
  239:                  ")<br />\n");
  240: 	}
  241: 	if (!%courses) { $r->print(&mt('None found')); }
  242:         $r->print('<input type="hidden" name="form" value="'.$ENV{'form.form'}.'" />'."\n".
  243:                    '<input type="hidden" name="pickedcourse" value="" />'."\n".
  244:                    '<input type="hidden" name="cnumelement" value="'.$ENV{'form.cnumelement'}.'" />'."\n".
  245:                    '<input type="hidden" name="cdomelement" value="'.$ENV{'form.cdomelement'}.'" />'."\n".
  246:                    '</form>');
  247:     }
  248:     $r->print('</body></html>');
  249:     return OK;
  250: }
  251: 
  252: 1;
  253: __END__

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