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

1.1       www         1: # The LearningOnline Network
                      2: # Pick a course
                      3: #
1.75    ! bisitz      4: # $Id: lonpickcourse.pm,v 1.74 2008/08/06 10:45:36 bisitz 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 LONCAPA;
1.1       www        39: 
                     40: sub handler {
                     41:     my $r = shift;
1.8       www        42:     &Apache::loncommon::content_type($r,'text/html');
1.1       www        43:     $r->send_http_header;
                     44:     return OK if $r->header_only;
                     45: 
                     46: # ------------------------------------------------------------ Print the screen
1.40      albertel   47: 
1.55      raeburn    48:     # Get parameters from query string
1.1       www        49:     &Apache::loncommon::get_unprocessed_cgi
1.55      raeburn    50:         ($ENV{'QUERY_STRING'},['domainfilter','form','cnumelement',
1.19      raeburn    51: 			       'cdomelement','cnameelement','roleelement',
1.55      raeburn    52:                                'multiple','type','setroles']);
1.54      raeburn    53: 
1.55      raeburn    54:     my ($type,$title,$jscript,$multelement,$multiple,$roleelement,
                     55:         $lastaction,$autosubmit,$submitopener);
                     56: 
1.57      raeburn    57:     # Get course type - Course or Non-standard Course.
1.54      raeburn    58:     $type = $env{'form.type'};
                     59:     if (!defined($env{'form.type'})) {
                     60:         $type = 'Course';
                     61:     }
                     62:     $title = &mt('Selecting a [_1]',$type);
                     63: 
                     64:     # Setup for multiple course selections, if flag for multiples set.
1.55      raeburn    65:     $multiple = $env{'form.multiple'};
                     66:     if ($multiple) {
                     67:         ($jscript,$multelement) = &multiples_tag();
                     68:         $title = &mt('Selecting [_1](s)',lc($type));
1.54      raeburn    69:     }
                     70: 
1.55      raeburn    71:     # if called when a DC is selecting a course 
1.54      raeburn    72:     my $roledom = $env{'form.roleelement'};
                     73:     if ($roledom) {
                     74:         $roleelement = '<input type="hidden" name="roleelement" value="'.$roledom.'" />';
1.66      raeburn    75:         $submitopener = &processpick();
1.54      raeburn    76:         $autosubmit = 'process_pick("'.$roledom.'")';
                     77:     }
                     78: 
                     79:     my $onlyown = 0;
1.55      raeburn    80:     # if called to assign course-based portfolio access control
1.59      raeburn    81:     if ((($env{'form.form'} eq 'portform') && (!$env{'user.adv'}))) {
1.54      raeburn    82:         $onlyown = 1;
1.49      raeburn    83:     }
1.55      raeburn    84: 
                     85:     my %loaditem;
1.66      raeburn    86:     if ((($env{'form.form'} eq 'cu') || ($env{'form.form'} eq 'studentform')) && 
                     87:         ($env{'form.pickedcourse'})) {
1.42      albertel   88:         $loaditem{'onload'} ="setSections()";
1.30      raeburn    89:     }
1.41      albertel   90:     $r->print(&Apache::loncommon::start_page($title,undef,
1.42      albertel   91: 					     {'add_entries' => \%loaditem,
1.41      albertel   92: 					      'no_nav_bar'  => 1, }));
1.55      raeburn    93: 
                     94:     if ($env{'form.form'} eq 'portform') {
                     95:         $lastaction = 'document.courselist.submit()';
1.67      raeburn    96:     } elsif ($env{'form.form'} eq 'cu' || ($env{'form.form'} eq 'studentform' &&
                     97:         !$multiple)) {
1.55      raeburn    98:         $lastaction = 
                     99:              'document.courselist.pickedcourse.value = cdom+"_"+cname;'."\n".
                    100:              'document.courselist.submit();';
1.34      albertel  101:     } else {
1.55      raeburn   102:         $lastaction = 'self.close()';
1.1       www       103:     }
1.19      raeburn   104: 
1.55      raeburn   105:     # if called to assign a role in a course to a user via CUSR
1.66      raeburn   106:     if ($env{'form.form'} eq 'cu' || $env{'form.form'} eq 'studentform') {
1.68      raeburn   107:         $r->print(&create_user_javascript($type));
1.55      raeburn   108:     }
1.54      raeburn   109: 
1.55      raeburn   110:     # print javascript functions for choosing a course 
                    111:     if ($env{'form.gosearch'} || $onlyown) {
1.58      raeburn   112:         $r->print(&gochoose_javascript($type,$multiple,$autosubmit,$lastaction));
1.55      raeburn   113:     }
                    114:     $r->print('<script type="text/javascript">'.$jscript.'</script>');
                    115:     $r->print($submitopener);
1.54      raeburn   116: 
1.55      raeburn   117: # ------------------------------------------ Display of filters to limit search
1.57      raeburn   118:     my $filter = {};
                    119:     my $action = '/adm/pickcourse';
1.55      raeburn   120:     if (!$onlyown) {
                    121:         my $filterlist = ['domainfilter','descriptfilter',
                    122:                           'instcodefilter','ownerfilter',
                    123:                           'ownerdomfilter'];
                    124:         # course ID filter for DCs only
                    125:         if ($env{'user.adv'} && $env{'form.domainfilter'} &&
                    126:             exists($env{'user.role.dc./'.$env{'form.domainfilter'}.'/'})) {
                    127:             push(@{$filterlist},'coursefilter');
                    128:         }
                    129:         $r->print(&build_filters($filterlist,$type,$roleelement,$multelement,
1.57      raeburn   130:                                  $filter,$action));
1.55      raeburn   131:     }
1.54      raeburn   132: 
                    133: # ---------------------------------------------------------------- Get the data
                    134:     if ($env{'form.gosearch'} || $onlyown) {
1.57      raeburn   135:         my %courses = &search_courses($r,$type,$onlyown,$filter);
                    136:         &display_matched_courses($r,$type,$multiple,$action,%courses);
1.54      raeburn   137:     }
                    138:     $r->print(&Apache::loncommon::end_page());
                    139:     return OK;
                    140: }
                    141: 
1.66      raeburn   142: sub processpick {
                    143:     my $openerform = 'rolechoice';
                    144:     if ($env{'form.form'} eq 'studentform') {
                    145:         $openerform = $env{'form.form'};
                    146:     }
                    147:     my $process_pick = <<"ENDONE";
                    148: <script type="text/javascript">
                    149: function process_pick(dom) {
                    150:     var pickedCourse=opener.document.$openerform.$env{'form.cnumelement'}.value;
                    151:     var pickedDomain=opener.document.$openerform.$env{'form.cdomelement'}.value;
                    152:     var okDomain = 0;
                    153: ENDONE
                    154:     if ($openerform eq 'rolechoice') {
                    155:         $process_pick .= <<"ENDTWO";
                    156:     if (pickedDomain == dom) {
                    157:         if (pickedCourse != '') {
                    158:             var courseTarget = "cc./"+pickedDomain+"/"+pickedCourse
                    159:             opener.document.title='Role selected. Please stand by.';
                    160:             opener.status='Role selected. Please stand by.';
                    161:             opener.document.rolechoice.newrole.value=courseTarget
                    162:             opener.document.rolechoice.submit();
                    163:         }
                    164:     } 
                    165:     else {
                    166:         alert("You may only use this screen to select courses in the current domain: "+dom+"\\nPlease return to the roles page window and click the 'Select Course' link for domain: "+pickedDomain+",\\n if you are a Domain Coordinator in that domain, and wish to become a Course Coordinator in a course in the domain");
                    167:     }
                    168: ENDTWO
                    169:     } else {
                    170:         $process_pick .= <<"ENDTHREE";
                    171:     if (pickedDomain != dom) {
                    172:         alert("You may only use this screen to select courses in the current domain: "+dom+"\\nPlease return to the roles page window and click the 'Select Course' link for domain: "+pickedDomain+",\\n if you are a Domain Coordinator in that domain, and wish to become a Course Coordinator in a course in the domain");
                    173:         return;
                    174:     }
                    175: ENDTHREE
                    176:     }
                    177:     $process_pick .= "
                    178: }
                    179: 
                    180: </script>
                    181: ";
                    182:     return $process_pick;
                    183: }
                    184: 
1.55      raeburn   185: sub create_user_javascript {
                    186:     my ($type) = @_;
                    187:     my $output;
                    188:     #javascript for reporting sections and groups then closing
                    189:     if ($env{'form.pickedcourse'}) {
                    190:         my ($cdom,$cnum) = split/_/,$env{'form.pickedcourse'};
                    191:         my $sec_element = 'currsec';
                    192:         my $grplist_element = 'groups';
                    193:         my ($sections,$groups) =
                    194:           &Apache::loncommon::get_secgrprole_info($cdom,$cnum,'',$type);
                    195:         my $num_sections = scalar(@{$sections});
                    196:         my $seclist = join(',',@{$sections});
                    197:         my $num_groups = scalar(@{$groups});
                    198:         my $groupslist = join(',',@{$groups});
                    199:         $output = qq|
                    200: <script type="text/javascript">
                    201: function setSections() {
                    202:     opener.document.$env{"form.form"}.$grplist_element.value='$groupslist';
                    203:     window.opener.setSect('$seclist');
                    204:     self.close()
                    205: }
                    206: </script>
                    207: |;
                    208: ;
                    209:     }
                    210:     return $output;
                    211: }
                    212: 
1.54      raeburn   213: sub display_matched_courses {
1.57      raeburn   214:     my ($r,$type,$multiple,$action,%courses) = @_;
1.55      raeburn   215:     if ($env{'form.form'} eq 'portform') {
                    216:         $action = '/adm/portfolio';
                    217:     }
1.68      raeburn   218:     my $numcourses = keys(%courses);
1.55      raeburn   219:     $r->print('<form name="courselist" method="post" action="'.$action.'">');
1.68      raeburn   220:     if ($env{'form.form'} eq 'modifycourse') {
                    221:         if ($numcourses > 0) {
                    222:             my $cctitle = &Apache::lonnet::plaintext('cc',$type);
                    223:             my $dctitle = &Apache::lonnet::plaintext('dc');
                    224:             my $ccrolechk = ' checked="checked" ';
                    225:             my $menuchk = ' ';
                    226:             if ($env{'form.prevphase'} ne '') {
                    227:                  $ccrolechk = ' ';
                    228:                  $menuchk = ' checked="checked" ';
                    229:             }
                    230:             $r->print(
1.75    ! bisitz    231:                 '<fieldset>'
        !           232:                .'<legend>'.&mt('Action').'</legend>'
        !           233:                .'<div class="LC_nobreak"><label>'
        !           234:                .'<input type="radio" name="phase" value="ccrole"'.$ccrolechk.'/>'
        !           235:                .'&nbsp;'.&mt('Enter the course with the role of [_1].',$cctitle)
        !           236:                .'</label></div>'
        !           237:                .'<div class="LC_nobreak"><label>'
        !           238:                .'<input type="radio" name="phase" value="menu"'.$menuchk.'/>&nbsp;'
        !           239:                .&mt('View or modify course settings which only a [_1] may modify.',$dctitle)
        !           240:                .'</label></div>'
        !           241:                .'</fieldset>'
        !           242:                .'<br />'
        !           243:             );
1.68      raeburn   244:         }
                    245:     }
1.54      raeburn   246:     my %by_descrip;
                    247:     foreach my $course (keys(%courses)) {
                    248:         my $descr;
1.64      raeburn   249:         if (ref($courses{$course}) eq 'HASH') {
1.65      raeburn   250:             $descr = $courses{$course}{'description'};
1.64      raeburn   251:         } elsif ($courses{$course} =~ m/^([^:]*):/i) {
1.54      raeburn   252:             $descr = &unescape($1);
1.34      albertel  253:         } else {
1.54      raeburn   254:             $descr = &unescape($courses{$course});
                    255:         }
                    256:         my $description = $descr;
                    257:         push (@{$by_descrip{$description}}, $course);
                    258:     }
1.71      bisitz    259:  
1.54      raeburn   260:     if ($numcourses > 1 && $multiple) {
                    261:         $r->print('<input type="button" value="check all"
                    262:                   onclick="javascript:checkAll(document.courselist.course_id)" />
                    263:                   &nbsp;&nbsp;<input type="button" value="uncheck all"
                    264:                   onclick="javascript:uncheckAll(document.courselist.course_id)" />
                    265:                   <br /><br />');
                    266:     }
1.71      bisitz    267: 
                    268:     if (%courses) {
                    269:         $r->print(&Apache::loncommon::start_data_table());
                    270:         $r->print(&Apache::loncommon::start_data_table_header_row());
                    271:         $r->print('<th>'.&mt('Select').'</th>'
                    272:                  .'<th>'.&mt('Course Description').'</th>'
                    273:                  .'<th>'.&mt('Domain').'</th>'
                    274:                  .'<th>'.&mt('Course Code').'</th>'
1.72      raeburn   275:                  .'<th>'.&mt('Owner/Co-owner(s)').'</th>'
1.71      bisitz    276:                  .'<th>'.&mt('Type').'</th>'
                    277:                  );
                    278:         $r->print(&Apache::loncommon::end_data_table_header_row());
                    279:     }
1.54      raeburn   280:     foreach my $description (sort { lc($a) cmp lc($b) } (keys(%by_descrip))) {
                    281:         foreach my $course (@{$by_descrip{$description}}) {
1.72      raeburn   282:             $r->print(&Apache::loncommon::start_data_table_row());
1.54      raeburn   283:             my $cleandesc=&HTML::Entities::encode($description,'<>&"');
                    284:             $cleandesc=~s/'/\\'/g;
                    285:             my ($cdom,$cnum)=split(/\_/,$course);
1.64      raeburn   286:             my ($descr,$instcode,$ttype,@owners);
                    287:             if (ref($courses{$course}) eq 'HASH') {
                    288:                 $descr = $courses{$course}{'description'};
1.65      raeburn   289:                 $instcode =  $courses{$course}{'inst_code'};
1.64      raeburn   290:                 $ttype =  $courses{$course}{'type'};
                    291:                 push(@owners,&unescape($courses{$course}{'owner'}));
                    292:                 if (ref($courses{$course}{'co-owners'}) eq 'ARRAY') {
                    293:                     foreach my $item (@{$courses{$course}{'co-owners'}}) {
                    294:                         push(@owners,&unescape($item));
                    295:                     }
                    296:                 }
                    297:             } else {
                    298:                 my $singleowner; 
                    299:                 ($descr,$instcode,$singleowner,$ttype)=split(/:/,$courses{$course});
                    300:                 push(@owners,&unescape($singleowner));
                    301:             }
1.72      raeburn   302:             my $ownerstr = join(', ',@owners);
1.71      bisitz    303:             $r->print('<td>'.&course_chooser($multiple,$cdom,$cnum,$cleandesc).'</td>');
                    304:             $r->print('<td>'.$description.'</td>');
                    305:             $r->print('<td>');
                    306:             $r->print(&Apache::lonnet::domain($cdom,'description')?
                    307:                       $cdom.' ('.&Apache::lonnet::domain($cdom,'description').')':$cdom);
                    308:             $r->print('</td>');
                    309:             $r->print('<td>');
1.64      raeburn   310:             if ($instcode ne '') {
1.71      bisitz    311:                 $r->print(&unescape($instcode));
                    312:             } else {
                    313:                 $r->print('&nbsp;');
1.54      raeburn   314:             }
1.71      bisitz    315:             $r->print('</td>');
1.72      raeburn   316:             $r->print('<td>'.$ownerstr.'</td>');
1.71      bisitz    317:             $r->print('<td>');
1.64      raeburn   318:             if ($ttype ne '') {
1.71      bisitz    319:                 $r->print(&mt(&unescape($ttype)));
                    320:             } else {
                    321:                 $r->print('&nbsp;');
1.23      raeburn   322:             }
1.71      bisitz    323:             $r->print('</td>');
1.54      raeburn   324:             if ($multiple) { $r->print("</label>\n"); }
1.72      raeburn   325:             $r->print(&Apache::loncommon::end_data_table_row());
1.71      bisitz    326:             # $r->print("<br />\n");
1.19      raeburn   327:         }
                    328:     }
1.72      raeburn   329:     if (%courses) {
                    330:         $r->print(&Apache::loncommon::end_data_table());
                    331:     }
1.71      bisitz    332: 
1.54      raeburn   333:     if (!%courses) {
                    334:         $r->print(&mt('None found'));
                    335:     } elsif ($multiple) {
1.57      raeburn   336:         $r->print('<input type="button" value="Submit" onClick="gochoose('."'','','')".'" />');
1.54      raeburn   337:     }
                    338:     $r->print('<input type="hidden" name="form" value="'.$env{'form.form'}.'" />'.
                    339:               "\n".'<input type="hidden" name="pickedcourse" value="" />'."\n".
                    340:               '<input type="hidden" name="type" value="'.$type.'" />'."\n");
                    341:     if ((exists($env{'form.roleelement'})) && ($env{'form.form'} eq 'rolechoice')) {
                    342:         $r->print('<input type="hidden" name="roleelement" value="'.
                    343:                   $env{'form.roleelement'}.'" />'."\n");
                    344:     }
1.55      raeburn   345:     if ($env{'form.form'} eq 'portform') {
                    346:         $r->print('<input type="hidden" name="cnum" value="" />');
                    347:         $r->print('<input type="hidden" name="cdom" value="" />');
                    348:         $r->print('<input type="hidden" name="setroles" value="'.$env{'form.setroles'}.'" />');
                    349:         $r->print('<input type="hidden" name="action" value="rolepicker" />');
1.57      raeburn   350:     } elsif ($env{'form.form'} eq 'modifycourse') {
                    351:         $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','pickedcourse','type','form']));
                    352:     } else {
                    353:         $r->print('<input type="hidden" name="cnumelement" value="'.
                    354:                   $env{'form.cnumelement'}.'" />'."\n".  
                    355:                   '<input type="hidden" name="cdomelement" value="'.
                    356:                   $env{'form.cdomelement'}.'" />'."\n");
1.55      raeburn   357:     }
1.54      raeburn   358:     $r->print("</form>\n");
                    359:     return;
                    360: }
                    361: 
                    362: 
                    363: sub multiples_tag {
1.55      raeburn   364:     my $jscript = &Apache::loncommon::check_uncheck_jscript();
                    365:     my $multelement = '<input type="hidden" name="multiple" value="1" />';
                    366:     return ($jscript,$multelement);
1.1       www       367: }
1.30      raeburn   368: 
1.54      raeburn   369: sub build_filters {
1.57      raeburn   370:     my ($filterlist,$type,$roleelement,$multelement,$filter,$action,
                    371:         $caller) = @_;
1.55      raeburn   372:     my $list;
1.57      raeburn   373:     my $formname;
                    374:     if (defined($env{'form.form'})) {
                    375:          $formname = $env{'form.form'};
                    376:     } else {
                    377:          $formname = $caller;
                    378:     }
1.55      raeburn   379:     my ($domainselectform,$sincefilterform,$ownerdomselectform,
1.54      raeburn   380:         $instcodeform,$typeselectform);
1.55      raeburn   381:     foreach my $item (@{$filterlist}) {
1.54      raeburn   382:         $filter->{$item} = $env{'form.'.$item};
                    383:         if ($item ne 'descriptfilter' && $item ne 'instcodefilter') {
1.61      albertel  384: 	    if ($item eq 'domainfilter' || $item eq 'ownerdomfilter') {
                    385: 		$filter->{$item} = &LONCAPA::clean_domain($filter->{$item});
                    386: 	    } elsif ($item eq 'coursefilter') {
                    387: 		$filter->{$item} = &LONCAPA::clean_courseid($filter->{$item});
                    388: 	    } elsif ($item eq 'ownerfilter') {
                    389: 		$filter->{$item} = &LONCAPA::clean_username($filter->{$item});
                    390: 	    } else {
                    391: 		$filter->{$item} =~ s/\W//g;
                    392: 	    }
1.54      raeburn   393:             if (!$filter->{$item}) {
                    394:                 $filter->{$item} = '';
                    395:             }
                    396:         }
                    397:         if ($item eq 'domainfilter') {
1.63      albertel  398: 	    my $allow_blank = 1;
                    399: 	    if ($formname eq 'portform') {
                    400: 		$filter->{$item} ||= $env{'user.domain'};
                    401: 		$allow_blank=0;
1.67      raeburn   402: 	    } elsif ($formname eq 'studentform') {
                    403:                 $filter->{$item} ||= $env{'request.role.domain'};
                    404:                 $allow_blank=0;
                    405:             }
1.54      raeburn   406:             $domainselectform =
1.63      albertel  407: 		&Apache::loncommon::select_dom_form($filter->{$item},
                    408: 						    'domainfilter',
                    409: 						    $allow_blank);
1.54      raeburn   410:         } elsif ($item eq 'ownerdomfilter') {
                    411:             $ownerdomselectform =
                    412:      &Apache::loncommon::select_dom_form($filter->{$item},'ownerdomfilter',1);
                    413:         } else {
                    414:             $list->{$item} = &HTML::Entities::encode($filter->{$item},'<>&"');
                    415:         }
                    416:     }
                    417: 
                    418:     # last course activity filter and selection
                    419:     $filter->{'sincefilter'} = $env{'form.sincefilter'};
                    420:     $filter->{'sincefilter'} =~ s/[^\d-]//g;
                    421:     if (!$filter->{'sincefilter'}) { $filter->{'sincefilter'}=-1; }
                    422:     $sincefilterform=&Apache::loncommon::select_form($filter->{'sincefilter'},
                    423:        'sincefilter',('-1'=>'',
                    424:                       '86400' => 'today',
                    425:                       '604800' => 'last week',
                    426:                       '2592000' => 'last month',
                    427:                       '7776000' => 'last three months',
                    428:                       '15552000' => 'last six months',
                    429:                       '31104000' => 'last year',
                    430:                       'select_form_order' =>
                    431:                       ['-1','86400','604800','2592000','7776000',
                    432:                       '15552000','31104000']));
                    433: 
                    434:     my %lt = (
1.74      bisitz    435:               'cac' => &mt("$type Activity"),
                    436:               'cde' => &mt("$type Description"),
                    437:               'cdo' => &mt("$type Domain"),
1.54      raeburn   438:               'cin' => &mt('Course Institutional Code'),
1.74      bisitz    439:               'cow' => &mt("$type Owner's Username"),
                    440:               'cod' => &mt("$type Owner's Domain"),
1.54      raeburn   441:               'cog' => &mt('Course Type')
                    442:              );
1.49      raeburn   443: 
1.65      raeburn   444:     $typeselectform =  '<select name="type" size="1">'."\n";
1.54      raeburn   445:     if ($type eq 'Course') {
1.57      raeburn   446:         $instcodeform = 
                    447:                 '<input type="text" name="instcodefilter" size="10" value="'.
                    448:                 $list->{'instcodefilter'}.'" />';
1.54      raeburn   449:     }
1.57      raeburn   450:     foreach my $posstype ('Course','Non-standard Course') {
1.72      raeburn   451:         $typeselectform.='<option value="'.$posstype.'"'.
1.73      bisitz    452:             ($posstype eq $type ? ' selected="selected" ' : ''). ">".&mt($posstype)."</option>\n";
1.54      raeburn   453:     }
                    454:     $typeselectform.="</select>";
1.57      raeburn   455:     my $output = qq|
                    456: <form method="post" name="filterpicker" action="$action">
                    457: <input type="hidden" name="form" value="$formname" />
                    458: |;
                    459:     if ($formname eq 'modifycourse') {
1.68      raeburn   460:         $output .= '<input type="hidden" name="phase" value="courselist" />'."\n".
                    461:                    '<input type="hidden" name="prevphase" value="'.
                    462:                    $env{'form.prevphase'}.'" />'."\n";
                    463:     } else {
1.57      raeburn   464:         my $name_input;
                    465:         if ($env{'form.cnameelement'} ne '') {
                    466:             $name_input = '<input type="hidden" name="cnameelement" value="'.
                    467:                           $env{'form.cnameelement'}.'" />';
                    468:         }
                    469:         $output .= qq|
1.27      albertel  470: <input type="hidden" name="cnumelement" value="$env{'form.cnumelement'}" />
                    471: <input type="hidden" name="cdomelement" value="$env{'form.cdomelement'}" />
1.13      albertel  472: $name_input
1.18      raeburn   473: $roleelement
1.35      raeburn   474: $multelement
1.57      raeburn   475: |;
1.59      raeburn   476:         if ($formname eq 'portform') {
                    477:             $output .= '<input type="hidden" name="setroles" value="'.$env{'form.setroles'}.'" />';
                    478:         }
1.57      raeburn   479:     }
1.74      bisitz    480: 
                    481:     $output .= "<br />\n".&Apache::lonhtmlcommon::start_pick_box();
1.57      raeburn   482:     if ($sincefilterform) { 
1.74      bisitz    483:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cac'})
                    484:                   .$sincefilterform
                    485:                   .&Apache::lonhtmlcommon::row_closure();
1.57      raeburn   486:     }
                    487:     if ($domainselectform) {
1.74      bisitz    488:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cdo'})
                    489:                   .$domainselectform
                    490:                   .&Apache::lonhtmlcommon::row_closure();
1.57      raeburn   491:     }
                    492:     if ($typeselectform) {
1.74      bisitz    493:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cog'})
                    494:                   .$typeselectform
                    495:                   .&Apache::lonhtmlcommon::row_closure();
1.57      raeburn   496:     }
                    497:     if ($instcodeform) {
1.74      bisitz    498:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cin'})
                    499:                   .$instcodeform
                    500:                   .&Apache::lonhtmlcommon::row_closure();
1.57      raeburn   501:     }
                    502:     if (exists($filter->{'ownerfilter'})) {
1.74      bisitz    503:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cow'})
                    504:                   .'<input type="text" name="ownerfilter" size="10" value="'
                    505:                   .$list->{'ownerfilter'}.'" />'
                    506:                   .&Apache::lonhtmlcommon::row_closure()
                    507:                   .&Apache::lonhtmlcommon::row_title($lt{'cod'})
                    508:                   .$ownerdomselectform
                    509:                   .&Apache::lonhtmlcommon::row_closure();
1.57      raeburn   510:     }
                    511:     if (exists($filter->{'coursefilter'})) {
1.74      bisitz    512:         $output .= &Apache::lonhtmlcommon::row_title(&mt('LON-CAPA course ID'))
                    513:                   .'<input type="text" name="coursefilter" size="25" value="'
                    514:                   .$list->{'coursefilter'}.'" />'
                    515:                   .&Apache::lonhtmlcommon::row_closure();
1.57      raeburn   516:     }
                    517:     if (exists($filter->{'descriptfilter'})) {
1.74      bisitz    518:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cde'})
                    519:                   .'<input type="text" name="descriptfilter" size="40" value="'
                    520:                   .$list->{'descriptfilter'}.'" />'
                    521:                   .&Apache::lonhtmlcommon::row_closure(1);
1.57      raeburn   522:     }
1.74      bisitz    523:     $output .= &Apache::lonhtmlcommon::end_pick_box();
1.70      raeburn   524:     $output .= '<p><input type="submit" name="gosearch" value="'. 
                    525:                &mt('Search').'" /></p>'."\n".'</form>'."\n".'<hr />'."\n";
1.54      raeburn   526:     return $output; 
                    527: }
                    528: 
                    529: sub search_courses {
                    530:     my ($r,$type,$onlyown,$filter) = @_;
                    531:     my %courses;
                    532:     if (!$onlyown) {
1.71      bisitz    533:         $r->print(&mt('Searching ...').'<br />&nbsp;<br />');
1.6       www       534:         $r->rflush();
1.54      raeburn   535:         if (($filter->{'ownerfilter'} ne '') ||
                    536:             ($filter->{'ownerdomfilter'} ne '')) {
                    537:             $filter->{'combownerfilter'} = $filter->{'ownerfilter'}.':'.
                    538:                                            $filter->{'ownerdomfilter'};
                    539:         }
                    540:         foreach my $item ('descriptfilter','instcodefilter','coursefilter',
                    541:                           'combownerfilter') {
                    542:             if (!$filter->{$item}) {
                    543:                 $filter->{$item}='.';
                    544:             }
1.52      raeburn   545:         }
                    546:         if ($type eq '') { $type = '.'; }
1.54      raeburn   547:         my $timefilter = 
                    548:            ($filter->{'sincefilter'}==-1?1:time-$filter->{'sincefilter'});
                    549:         %courses = 
                    550:             &Apache::lonnet::courseiddump($filter->{'domainfilter'},
                    551:                                           $filter->{'descriptfilter'},
                    552:                                           $timefilter,
                    553:                                           $filter->{'instcodefilter'},
                    554:                                           $filter->{'combownerfilter'},
                    555:                                           $filter->{'coursefilter'},
                    556:                                           undef,undef,$type);
                    557:     } else {
                    558:         $r->print('<br />');
1.60      raeburn   559:         my %coursehash = &Apache::loncommon::findallcourses(); 
                    560:         foreach my $cid (sort(keys(%coursehash))) {
1.65      raeburn   561:             $courses{$cid}{'description'} = $env{'course.'.$cid.'.description'};
1.36      raeburn   562:         }
1.2       www       563:     }
1.54      raeburn   564:     return %courses;
1.18      raeburn   565: }
1.1       www       566: 
1.30      raeburn   567: sub course_chooser {
1.35      raeburn   568:     my ($multiple,$cdom,$cnum,$cleandesc) = @_;
1.30      raeburn   569:     my $output; 
1.35      raeburn   570:     if ($multiple) {
1.32      albertel  571:         $output = '<label><input type="checkbox" name="course_id" value="'.$cdom.'_'.$cnum.'" />'."\n";
1.30      raeburn   572:     } else {
1.71      bisitz    573:         $output = '<input type="button" value="'.&mt('Select').'" onClick="gochoose('.
1.30      raeburn   574:                   "'".$cnum."','".$cdom."','".$cleandesc."')".'" />'."\n";
                    575:     }
                    576:     return $output;
1.49      raeburn   577: }
                    578: 
1.55      raeburn   579: sub gochoose_javascript {
1.57      raeburn   580:     my ($type,$multiple,$autosubmit,$lastaction) = @_;
1.55      raeburn   581:     my %elements = (
                    582:                      'Course' => {
                    583:                                  name  => 'coursepick',
                    584:                                  total => 'coursetotal',
                    585:                                  list  => 'courselist',
                    586:                                  },
1.57      raeburn   587:                      'Non-standard Course' => {
1.55      raeburn   588:                                  name  => 'grouppick',
                    589:                                  total => 'grouptotal',
                    590:                                  list  => 'grouplist',
                    591:                               },
                    592:                     );
1.57      raeburn   593:     my $output .= qq|
1.55      raeburn   594: <script type="text/javascript">
1.57      raeburn   595: function gochoose(cname,cdom,cdesc) {
1.55      raeburn   596:     var openerForm = "$env{'form.form'}";
                    597:     courseCount = 0;
                    598:     var courses = '';
1.57      raeburn   599: |;
                    600:     if ($multiple) {
                    601:         $output .= <<"ENDSCRIPT";
                    602:     courseCount = 0;
                    603:     var courses = '';
                    604:     if (typeof(document.courselist.course_id.length) == 'undefined') {
                    605:         // only 1 course checkbox was created
                    606:         if (document.courselist.course_id.checked) {
                    607:             courses = courses + document.courselist.course_id.value + "&&";
                    608:             courseCount ++;
                    609:         }
                    610:     } else {
                    611:         for (var j=0; j<document.courselist.course_id.length; j++) {
                    612:             if (document.courselist.course_id[j].checked) {
                    613:                 courses = courses + document.courselist.course_id[j].value + "&&";
1.55      raeburn   614:                 courseCount ++;
                    615:             }
1.57      raeburn   616:         }
                    617:     }
                    618:     opener.document.$env{'form.form'}.$elements{$type}{'total'}.value = courseCount;
                    619:     if (typeof(opener.document.$env{'form.form'}.$elements{$type}{'name'}.length) ==
                    620:         'undefined') {
                    621:         if (opener.document.$env{'form.form'}.$elements{$type}{'name'}.value == 'specific') {
                    622:             opener.document.$env{'form.form'}.$elements{$type}{'name'}.checked = true;
1.55      raeburn   623:         } else {
1.57      raeburn   624:             opener.document.$env{'form.form'}.$elements{$type}{'name'}.checked = false;
1.55      raeburn   625:         }
1.57      raeburn   626:     } else {
                    627:         for (var j=0; j<opener.document.$env{'form.form'}.$elements{$type}{'name'}.length; j++) {
                    628:             if (opener.document.$env{'form.form'}.$elements{$type}{'name'}\[j].value == 'specific') {
                    629:                 opener.document.$env{'form.form'}.$elements{$type}{'name'}\[j].checked = true;
1.55      raeburn   630:             } else {
1.57      raeburn   631:                 opener.document.$env{'form.form'}.$elements{$type}{'name'}\[j].checked = false;
1.54      raeburn   632:             }
1.55      raeburn   633:         }
1.57      raeburn   634:     }
                    635:     if (courseCount > 0) {
                    636:         courses = courses.substr(0,courses.length-2);
                    637:         opener.document.$env{'form.form'}.$elements{$type}{'list'}.value = courses;
                    638:     }
                    639: ENDSCRIPT
                    640:     } else {
                    641:         my $name_code;
                    642:         if ($env{'form.cnameelement'} ne '') {
                    643:             $name_code = 'opener.document.'.$env{'form.form'}.'.'.
                    644:                           $env{'form.cnameelement'}.'.value=cdesc;';
1.55      raeburn   645:         }
1.57      raeburn   646:         $output .= qq|
1.55      raeburn   647:         $name_code
                    648:         opener.document.$env{'form.form'}.$env{'form.cnumelement'}.value=cname;
                    649:         var slct=opener.document.$env{'form.form'}.$env{'form.cdomelement'};
                    650:         if (slct.options == undefined) {
                    651:             opener.document.$env{'form.form'}.$env{'form.cdomelement'}.value=cdom;
                    652:         }
                    653:         else {
                    654:             var i;
                    655:             for (i=0;i<slct.length;i++) {
                    656:                 if (slct.options[i].value==cdom) { slct.selectedIndex=i; }
1.54      raeburn   657:             }
                    658:         }
1.57      raeburn   659: |;
1.54      raeburn   660:     }
1.57      raeburn   661:     $output .= qq|
1.55      raeburn   662:     if (openerForm == 'portform') {
                    663:         document.courselist.cnum.value = cname;
                    664:         document.courselist.cdom.value = cdom;
                    665:     }
                    666:     $autosubmit
                    667:     $lastaction
1.54      raeburn   668: }
1.55      raeburn   669: </script>
1.57      raeburn   670: |;
1.55      raeburn   671:     return $output;
1.54      raeburn   672: }
                    673: 
1.55      raeburn   674: 1;
                    675: __END__
                    676: 
                    677: =pod
                    678: 
                    679: =head1 NAME
                    680: 
                    681: Apache::lonpickcourse - Search for course(s) based on user-specified criteria.   
                    682: 
                    683: =head1 SYNOPSIS
                    684: 
                    685: Invoked by other LON-CAPA modules, when course(s) need to be selected by the user. 
                    686: 
                    687: =head1 OVERVIEW
                    688: 
                    689: Two screens are typically displayed to the user.  The first is a set of criteria which are used to constrain the search for courses.
                    690: 
                    691: =head2 Search Criteria (Screen One)
                    692: 
                    693: =head3 Criteria:
                    694: 
                    695: =over 4
                    696: 
                    697: =item *
                    698: Course Activity - how recently was course last visited by anyone.
                    699: 
                    700: =item *
                    701: Course Domain - the domain of the course
                    702: 
                    703: =item *
1.57      raeburn   704: Course Type - Course or Non-standard Course
1.55      raeburn   705: 
                    706: =item *
                    707: Course Institutional Code - the institutional identifier assigned to the course
                    708: 
                    709: =item * 
                    710: Course Owner's Username - the username of the owner of the course (assigned by the Domain Coordinator and/or when the course was created).
                    711: 
                    712: =item *
                    713: Course Owner's Domain - the domain of the owner of the course
                    714: 
                    715: =item * 
                    716: Course Description - text which appears in the Course Description (or title), as set in the Course Parameters.
                    717: 
                    718: =item *
                    719: Course ID - the internal course number (course ID part after initial 'domain_') used by LON-CAPA (this criterion is only displayed to Domain Coordinators selecting a course in the same domain as their DC role).
                    720: 
                    721: =back
                    722: 
                    723: The criteria setting screen is not displayed if course picking is done by a user who does not have advanced privileges (as defined by $env{'user.adv'}).
                    724: 
                    725: =head2 Course Display (Screen Two)
                    726: 
                    727: A list of courses matching the search criteria is displayed.  If the user is not an advanced user, screen one will have been skipped and the courses displayed will be all courses in which the user has currently active roles. The information displayed for each course is:
                    728: 
                    729: =over 4
                    730: 
                    731: =item *
                    732: Course description
                    733: 
                    734: =item *
                    735: Domain description of course domain
                    736: 
                    737: =item *
                    738: Course institutional code
                    739: 
                    740: =item * 
                    741: Course owner (username:domain)   
                    742:  
                    743: =back
                    744: 
                    745: Depending on context, the display may include a single select box for each course, allowing selection of only a single course, or may include checkboxes allowing selection of more than one course.
                    746: 
                    747: Following selection, and/or submission, the course description, number and domain are transferred to the browser window from which the course picker window was opened.  In most cases, the child window containing the course picker screens will be closed.  However, in some cases closure will be delayed until a third screen has been displayed (e.g., setting of course-based conditional access controls for portfolio files).  In this case the page is generated via /adm/portfolio and the page features select boxes to allow the user to select roles, access types, sections and groups.
                    748: 
                    749: =head1 SUBROUTINES
                    750: 
                    751: =over 4
                    752: 
                    753: =item *
                    754: X<create_user_javascript()>
                    755: B<create_user_javascript($type)>:
                    756: 
1.57      raeburn   757: Input: 1 - $type  - the course type - Course or Non-standard Course
1.55      raeburn   758: 
                    759: Output: 1 - $output - javascript wrapped in E<lt>scriptE<gt>E<lt>/scriptE<gt> tags 
                    760: 
                    761: Side Effects: None 
                    762: 
                    763: javascript code for reporting selected sections (as a string of comma separated sections) and groups in the selected course (as a comma separated list) then calling setSect() javscript function in the opener window (to populate section select box) then closing current window.
                    764: 
                    765: 
                    766: =item *
                    767: X<display_matched_courses()>
1.57      raeburn   768: B<display_matched_courses($r,$type,$multiple,$action,%courses)>:
1.55      raeburn   769: 
1.57      raeburn   770: Input: 5 - request object, course type, multiple (0 or 1), form action; hash of courses.
1.55      raeburn   771: 
                    772: Output: 0
                    773: 
                    774: Side Effects: prints select buttons (multiple = 0) or checkboxes (multiple = 1) and hidden form elements for selection of one or more courses which met search criteria.
                    775: 
                    776: =item *
                    777: X<multiples_tag()>
                    778: B<multiples_tag()>:
                    779: 
                    780: 
                    781: Input: 0
                    782: 
                    783: Output: 2 - $jscript - javascript for check all/uncheck all checkboxes; $multelement - hidden form element with multiple set to 1.
                    784: 
                    785: Side Effects: None
                    786: 
                    787: =item *
                    788: X<build_filters()>
1.57      raeburn   789: B<build_filters($filterlist,$type,$roleelement,$multelement,$filter,$action)>:
1.55      raeburn   790: 
                    791: 
1.57      raeburn   792: Input: 6 - anonymous array of search criteria; course type; $roleelement ; $multelement ; anonymous hash of criteria and their values; form action.
1.55      raeburn   793: 
                    794: Output: 1 - $output - HTML for display of search criteria, and hidden form elements. 
                    795: 
                    796: Side Effects: None
                    797: 
                    798: =item *
                    799: X<search_courses()>
                    800: B<search_courses($r,$type,$onlyown,$filter)>:
                    801: 
                    802: 
                    803: Input: 5 -  request object, course type, search scope: only courses in which user has active role (1), or any course (0); anonymous hash of criteria and their values.
                    804: 
                    805: Output: 1 - %courses - hash of courses satisfying search criteria, keys = course IDs, values are corresponding colon-separated escaped description, institutional code, owner and type.
                    806: 
                    807: Side Effects: None
                    808: 
                    809: 
                    810: =item *
                    811: X<course_chooser()>
                    812: B<course_chooser($multiple,$cdom,$cnum,$cleandesc)>:
                    813: 
                    814: Input: 4 - single (0) or multiple (1) courses; course domain, course number; course description. 
                    815: 
                    816: Output: 1 - HTML for either checkbox (multiple=1) or select button (multiple=0) for user yo indicate course selection.
                    817: 
                    818: Side Effects: None
                    819: 
                    820: 
                    821: =item *
                    822: X<gochoose_javascript()>
1.57      raeburn   823: B<gochoose_javascript($type,$multiple,$autosubmit,$lastaction)>:
1.55      raeburn   824: 
1.66      raeburn   825: Input: 4 - course type; single (0) or multiple courses (1); in context of DC selecting a CC role in a course: javascript code from &processpick(); final action to take after user chooses course(s):  either close window, or submit form for display of next page etc.
1.55      raeburn   826: 
                    827: Output: 1  $output - javascript wrapped in E<lt>scriptE<gt>E<lt>/scriptE<gt> tags
                    828: 
                    829: Side Effects: None
                    830: 
                    831: javascript functions used when user selects a course(s). Different behavior depending on context:
                    832: 
                    833: =back
                    834: 
                    835: =over 8
                    836: 
                    837: =item
                    838: 
                    839: (a) Domain Coordinator using MAIL to select recipients of broadcast e-mail - && separated list of selected courses written to hidden form element in opener window. Child window closes.
                    840: 
                    841: =item
                    842: 
                    843: (b) Domain Coordinator choosing a course for adoption of a CC role from roles screen - write course identifying information to hidden form elements in opener window and automatically submit role selection form in opener window. Child window closes.
                    844: 
                    845: =item
                    846: 
                    847: (c) Domain Coordinator creating a course, and selecting a course to clone - course number and domain written to visible form elements in opener window. Child window closes.
                    848: 
                    849: =item
1.54      raeburn   850: 
1.55      raeburn   851: (d) User selecting a course for course-based conditional access control for a portfolio file - form is submitted, and new page is displayed for selection of roles, access types, sections and groups to be used in conditional ACL. New page is generated by /adm/portfolio. 
1.54      raeburn   852: 
1.55      raeburn   853: =item
1.54      raeburn   854: 
1.55      raeburn   855: (e) Domain Coordinator assigning a role to a user - form is submitted, and new page does an onload call to a javascript function to (a) write lists of sections and groups to hidden elements in opener window, (b) call function in opener window to dynamically populate select box showing current sections.
1.54      raeburn   856: 
1.55      raeburn   857: =item
1.54      raeburn   858: 
1.55      raeburn   859: (f) Author modifying a rights entry in a .rights file - selected course number and domain are witten to visible form elements in opener window.  Child window closes. 
1.54      raeburn   860: 
1.55      raeburn   861: =item
1.49      raeburn   862: 
1.55      raeburn   863: (g) Scantron Operator uploading a scantron file to a course - course number is written to visible form element in opener window. Child window closes.
1.30      raeburn   864: 
1.55      raeburn   865: =back
                    866:      
                    867: =cut

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