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

1.1       www         1: # The LearningOnline Network
                      2: # Pick a course
                      3: #
1.95    ! raeburn     4: # $Id: lonpickcourse.pm,v 1.94 2010/03/16 05:11:40 raeburn Exp $
1.1       www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: 
                     29: package Apache::lonpickcourse;
                     30: 
                     31: use strict;
                     32: use Apache::Constants qw(:common);
                     33: use Apache::loncommon;
                     34: use Apache::loncoursedata;
                     35: use Apache::lonnet;
1.8       www        36: use Apache::lonlocal;
1.46      raeburn    37: use Apache::longroup;
1.78      raeburn    38: use Apache::courseclassifier;
1.85      raeburn    39: use LONCAPA qw(:DEFAULT :match);
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.55      raeburn    49:     # Get parameters from query string
1.1       www        50:     &Apache::loncommon::get_unprocessed_cgi
1.55      raeburn    51:         ($ENV{'QUERY_STRING'},['domainfilter','form','cnumelement',
1.19      raeburn    52: 			       'cdomelement','cnameelement','roleelement',
1.85      raeburn    53:                                'multiple','type','setroles','fixeddom','cloner']);
1.90      raeburn    54:     my ($type,$title,$jscript,$multelement,$multiple,$roleelement,$typeelement,
1.85      raeburn    55:         $lastaction,$autosubmit,$submitopener,$cloneruname,$clonerudom);
1.55      raeburn    56: 
1.83      raeburn    57:     # Get course type - Course or Community.
1.54      raeburn    58:     $type = $env{'form.type'};
                     59:     if (!defined($env{'form.type'})) {
                     60:         $type = 'Course';
                     61:     }
1.82      bisitz     62:     $title = 'Selecting a '.$type;
1.54      raeburn    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();
1.82      bisitz     68:         $title = 'Selecting '.$type.'(s)';
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:     }
1.90      raeburn    78:     if ($env{'form.typeelement'} ne '') {
                     79:         $typeelement = '<input type="hidden" name="typeelement" value="'.$env{'form.typeelement'}.'" />';
                     80:     }
1.54      raeburn    81: 
1.92      raeburn    82:     # if called when a DC is creating a course for another user.
1.85      raeburn    83:     if ($env{'form.form'} eq 'ccrs') {
                     84:         ($cloneruname,$clonerudom) = ($env{'form.cloner'} =~ /^($match_username):($match_domain)$/);
                     85:     }
                     86: 
                     87:     # if called when requesting a course
                     88:     if ($env{'form.form'} eq 'requestcrs') {
                     89:         $cloneruname = $env{'user.name'};
                     90:         $clonerudom =  $env{'user.domain'};
                     91:     }
                     92: 
1.54      raeburn    93:     my $onlyown = 0;
1.55      raeburn    94:     # if called to assign course-based portfolio access control
1.59      raeburn    95:     if ((($env{'form.form'} eq 'portform') && (!$env{'user.adv'}))) {
1.54      raeburn    96:         $onlyown = 1;
1.49      raeburn    97:     }
1.55      raeburn    98: 
                     99:     my %loaditem;
1.84      raeburn   100:     if (($env{'form.type'} eq 'Course') && ($env{'form.numtitles'})) {
                    101:         if (($env{'form.official'} eq 'on') && ($env{'form.state'} eq 'listing')) {
                    102:             $loaditem{'onload'} = 'setElements(document.filterpicker); ';
                    103:         }
1.78      raeburn   104:     }
                    105: 
1.66      raeburn   106:     if ((($env{'form.form'} eq 'cu') || ($env{'form.form'} eq 'studentform')) && 
                    107:         ($env{'form.pickedcourse'})) {
1.89      raeburn   108:         $loaditem{'onload'} .= 'setRoles();setSections();';
1.30      raeburn   109:     }
1.85      raeburn   110:     my $js = &js_changer();
                    111:     $r->print(&Apache::loncommon::start_page($title,$js,
1.42      albertel  112: 					     {'add_entries' => \%loaditem,
1.41      albertel  113: 					      'no_nav_bar'  => 1, }));
1.55      raeburn   114: 
                    115:     if ($env{'form.form'} eq 'portform') {
                    116:         $lastaction = 'document.courselist.submit()';
1.67      raeburn   117:     } elsif ($env{'form.form'} eq 'cu' || ($env{'form.form'} eq 'studentform' &&
                    118:         !$multiple)) {
1.55      raeburn   119:         $lastaction = 
                    120:              'document.courselist.pickedcourse.value = cdom+"_"+cname;'."\n".
                    121:              'document.courselist.submit();';
1.34      albertel  122:     } else {
1.55      raeburn   123:         $lastaction = 'self.close()';
1.1       www       124:     }
1.19      raeburn   125: 
1.55      raeburn   126:     # if called to assign a role in a course to a user via CUSR
1.66      raeburn   127:     if ($env{'form.form'} eq 'cu' || $env{'form.form'} eq 'studentform') {
1.68      raeburn   128:         $r->print(&create_user_javascript($type));
1.55      raeburn   129:     }
1.54      raeburn   130: 
1.55      raeburn   131:     # print javascript functions for choosing a course 
1.85      raeburn   132:     if ((($env{'form.gosearch'}) && ($env{'form.updater'} eq '')) || 
                    133:         $onlyown) {
1.58      raeburn   134:         $r->print(&gochoose_javascript($type,$multiple,$autosubmit,$lastaction));
1.55      raeburn   135:     }
1.79      raeburn   136:     $r->print('<script type="text/javascript" language="Javascript">'.$jscript.
                    137:               '</script>');
1.55      raeburn   138:     $r->print($submitopener);
1.54      raeburn   139: 
1.55      raeburn   140: # ------------------------------------------ Display of filters to limit search
1.57      raeburn   141:     my $filter = {};
                    142:     my $action = '/adm/pickcourse';
1.79      raeburn   143:     my ($numtitles,$showroles,$nohost);
1.55      raeburn   144:     if (!$onlyown) {
1.94      raeburn   145:         my $filterlist = ['domainfilter'];
                    146:         # created filter for DCs only
                    147:         if ($env{'user.adv'} && $env{'form.domainfilter'} &&
1.95    ! raeburn   148:             exists($env{'user.role.dc./'.$env{'form.domainfilter'}.'/'})
        !           149:             && $env{'form.form'} ne 'portform') {
1.94      raeburn   150:             my $loncaparev = &Apache::lonnet::get_server_loncaparev($env{'form.domainfilter'});
                    151:             if ($loncaparev ne 'unknown_cmd') {
                    152:                 push(@{$filterlist},'createdfilter');
                    153:             }
                    154:         }
                    155:         push(@{$filterlist},('descriptfilter','instcodefilter'));
1.79      raeburn   156:         if ($env{'form.form'} eq 'rules') {
                    157:             push(@{$filterlist},'personfilter');
                    158:             if (($env{'form.personfilter'} ne '') && ($env{'form.persondomfilter'} ne '')) {
                    159:                 if (&Apache::lonnet::homeserver($env{'form.personfilter'},
                    160:                                                  $env{'form.persondomfilter'}) eq 'no_host') {
                    161:                     $nohost = 1;
                    162:                 } else {
                    163:                     $showroles = 1;
                    164:                 } 
                    165:             }
                    166:         } else {
                    167:             push(@{$filterlist},'ownerfilter');
                    168:         }
1.55      raeburn   169:         # course ID filter for DCs only
                    170:         if ($env{'user.adv'} && $env{'form.domainfilter'} &&
                    171:             exists($env{'user.role.dc./'.$env{'form.domainfilter'}.'/'})) {
                    172:             push(@{$filterlist},'coursefilter');
                    173:         }
1.85      raeburn   174:         if ($cloneruname ne '' && $clonerudom ne '') {
                    175:             push(@{$filterlist},'cloneableonly');
                    176:         }
                    177: 
1.55      raeburn   178:         $r->print(&build_filters($filterlist,$type,$roleelement,$multelement,
1.85      raeburn   179:                                  $filter,$action,\$numtitles,undef,$cloneruname,
1.90      raeburn   180:                                  $clonerudom,$typeelement));
1.55      raeburn   181:     }
1.54      raeburn   182: 
                    183: # ---------------------------------------------------------------- Get the data
1.85      raeburn   184:     if ((($env{'form.gosearch'}) && ($env{'form.updater'} eq '')) || 
                    185:          $onlyown) {
                    186:         my %courses = &search_courses($r,$type,$onlyown,$filter,$numtitles,
                    187:                                       $cloneruname,$clonerudom);
1.79      raeburn   188:         if ($nohost) {
                    189:             $r->print ('<span class="LC_warning">'.
                    190:                        &mt('User does not exist - username: [_1], domain: [_2].',
                    191:                            '<b>'.$filter->{'personfilter'}.'</b>',
                    192:                            '<b>'.$filter->{'persondomfilter'}.'</b>').'</span>');
                    193:         } else {
1.85      raeburn   194:             &display_matched_courses($r,$type,$multiple,$action,$showroles,$cloneruname,
                    195:                                      $clonerudom,%courses);
1.79      raeburn   196:         }
1.54      raeburn   197:     }
                    198:     $r->print(&Apache::loncommon::end_page());
                    199:     return OK;
                    200: }
                    201: 
1.85      raeburn   202: sub js_changer {
                    203:     return <<"ENDJS";
                    204: <script type="text/javascript">
                    205: 
                    206: function updateFilters(caller) {
                    207:     if (typeof(caller) != "undefined") {
                    208:         document.filterpicker.updater.value = caller.name;
                    209:     }
                    210:     document.filterpicker.submit();
                    211: }
                    212: 
                    213: </script>
                    214: 
                    215: ENDJS
                    216: 
                    217: }
                    218: 
1.66      raeburn   219: sub processpick {
                    220:     my $openerform = 'rolechoice';
                    221:     if ($env{'form.form'} eq 'studentform') {
                    222:         $openerform = $env{'form.form'};
                    223:     }
                    224:     my $process_pick = <<"ENDONE";
                    225: <script type="text/javascript">
                    226: function process_pick(dom) {
                    227:     var pickedCourse=opener.document.$openerform.$env{'form.cnumelement'}.value;
                    228:     var pickedDomain=opener.document.$openerform.$env{'form.cdomelement'}.value;
                    229:     var okDomain = 0;
                    230: ENDONE
                    231:     if ($openerform eq 'rolechoice') {
                    232:         $process_pick .= <<"ENDTWO";
                    233:     if (pickedDomain == dom) {
                    234:         if (pickedCourse != '') {
1.88      raeburn   235:             var ccrole = "cc";
                    236:             var pickedType = "$env{'form.type'}";
                    237:             if (pickedType == "Community") {
                    238:                 ccrole = "co";
                    239:             }
                    240:             var courseTarget = ccrole+"./"+pickedDomain+"/"+pickedCourse
1.66      raeburn   241:             opener.document.title='Role selected. Please stand by.';
                    242:             opener.status='Role selected. Please stand by.';
                    243:             opener.document.rolechoice.newrole.value=courseTarget
                    244:             opener.document.rolechoice.submit();
                    245:         }
                    246:     } 
                    247:     else {
                    248:         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");
                    249:     }
                    250: ENDTWO
                    251:     } else {
                    252:         $process_pick .= <<"ENDTHREE";
                    253:     if (pickedDomain != dom) {
                    254:         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");
                    255:         return;
                    256:     }
                    257: ENDTHREE
                    258:     }
                    259:     $process_pick .= "
                    260: }
                    261: 
                    262: </script>
                    263: ";
                    264:     return $process_pick;
                    265: }
                    266: 
1.55      raeburn   267: sub create_user_javascript {
                    268:     my ($type) = @_;
                    269:     my $output;
                    270:     #javascript for reporting sections and groups then closing
                    271:     if ($env{'form.pickedcourse'}) {
1.87      raeburn   272:         my %coursedescription = 
                    273:             &Apache::lonnet::coursedescription($env{'form.pickedcourse'},
                    274:                                                {'one_time' => '1'});
                    275:         my $cdom = $coursedescription{'domain'};
                    276:         my $cnum = $coursedescription{'num'};
                    277:         my $crstype = $coursedescription{'type'};
1.55      raeburn   278:         my $sec_element = 'currsec';
                    279:         my $grplist_element = 'groups';
                    280:         my ($sections,$groups) =
                    281:           &Apache::loncommon::get_secgrprole_info($cdom,$cnum,'',$type);
                    282:         my $num_sections = scalar(@{$sections});
                    283:         my $seclist = join(',',@{$sections});
                    284:         my $num_groups = scalar(@{$groups});
                    285:         my $groupslist = join(',',@{$groups});
                    286:         $output = qq|
                    287: <script type="text/javascript">
                    288: function setSections() {
                    289:     opener.document.$env{"form.form"}.$grplist_element.value='$groupslist';
                    290:     window.opener.setSect('$seclist');
1.87      raeburn   291:     self.close();
                    292: }
                    293: function setRoles() {
                    294:     window.opener.setRole('$crstype');
1.55      raeburn   295: }
                    296: </script>
                    297: |;
                    298: ;
                    299:     }
                    300:     return $output;
                    301: }
                    302: 
1.54      raeburn   303: sub display_matched_courses {
1.85      raeburn   304:     my ($r,$type,$multiple,$action,$showroles,$cloneruname,$clonerudom,%courses) = @_;
1.55      raeburn   305:     if ($env{'form.form'} eq 'portform') {
                    306:         $action = '/adm/portfolio';
                    307:     }
1.68      raeburn   308:     my $numcourses = keys(%courses);
1.55      raeburn   309:     $r->print('<form name="courselist" method="post" action="'.$action.'">');
1.68      raeburn   310:     if ($env{'form.form'} eq 'modifycourse') {
                    311:         if ($numcourses > 0) {
1.89      raeburn   312:             my $ccrole = 'cc';
                    313:             if ($type eq 'Community') {
                    314:                 $ccrole = 'co';
                    315:             }
                    316:             my $cctitle = &Apache::lonnet::plaintext($ccrole,$type);
1.68      raeburn   317:             my $dctitle = &Apache::lonnet::plaintext('dc');
1.93      raeburn   318:             my $ccrolechk = ' ';
                    319:             my $menuchk = ' checked="checked" ';
1.68      raeburn   320:             $r->print(
1.75      bisitz    321:                 '<fieldset>'
                    322:                .'<legend>'.&mt('Action').'</legend>'
                    323:                .'<div class="LC_nobreak"><label>'
                    324:                .'<input type="radio" name="phase" value="ccrole"'.$ccrolechk.'/>'
1.89      raeburn   325:                .'&nbsp;');
                    326:             if ($type eq 'Community') {
                    327:                 $r->print(&mt('Enter the community with the role of [_1].',$cctitle));
                    328:             } else {
                    329:                 $r->print(&mt('Enter the course with the role of [_1].',$cctitle));
                    330:             }
                    331:             $r->print('</label></div>'
1.75      bisitz    332:                .'<div class="LC_nobreak"><label>'
1.89      raeburn   333:                .'<input type="radio" name="phase" value="menu"'.$menuchk.'/>&nbsp;');
                    334:             if ($type eq 'Community') {
                    335:                 $r->print(&mt('View or modify community settings which only a [_1] may modify.',$dctitle));
                    336:             } else {
                    337:                 $r->print(&mt('View or modify course settings which only a [_1] may modify.',$dctitle));
                    338:             }
                    339:             $r->print('</label></div>'
1.75      bisitz    340:                .'</fieldset>'
                    341:                .'<br />'
                    342:             );
1.68      raeburn   343:         }
                    344:     }
1.54      raeburn   345:     my %by_descrip;
                    346:     foreach my $course (keys(%courses)) {
                    347:         my $descr;
1.64      raeburn   348:         if (ref($courses{$course}) eq 'HASH') {
1.65      raeburn   349:             $descr = $courses{$course}{'description'};
1.64      raeburn   350:         } elsif ($courses{$course} =~ m/^([^:]*):/i) {
1.54      raeburn   351:             $descr = &unescape($1);
1.34      albertel  352:         } else {
1.54      raeburn   353:             $descr = &unescape($courses{$course});
                    354:         }
                    355:         my $description = $descr;
                    356:         push (@{$by_descrip{$description}}, $course);
                    357:     }
1.71      bisitz    358:  
1.54      raeburn   359:     if ($numcourses > 1 && $multiple) {
                    360:         $r->print('<input type="button" value="check all"
                    361:                   onclick="javascript:checkAll(document.courselist.course_id)" />
                    362:                   &nbsp;&nbsp;<input type="button" value="uncheck all"
                    363:                   onclick="javascript:uncheckAll(document.courselist.course_id)" />
                    364:                   <br /><br />');
                    365:     }
1.71      bisitz    366: 
                    367:     if (%courses) {
                    368:         $r->print(&Apache::loncommon::start_data_table());
                    369:         $r->print(&Apache::loncommon::start_data_table_header_row());
1.89      raeburn   370:         my $titlehdr = &mt('Course Title');
                    371:         if ($type eq 'Community') {
                    372:             $titlehdr = &mt('Community Title');
                    373:         }
1.71      bisitz    374:         $r->print('<th>'.&mt('Select').'</th>'
1.89      raeburn   375:                  .'<th>'.$titlehdr.'</th>'
                    376:                  .'<th>'.&mt('Domain').'</th>');
                    377:         unless ($type eq 'Community') {
                    378:             $r->print('<th>'.&mt('Course Code').'</th>');
                    379:         }
                    380:         $r->print('<th>'.&mt('Owner/Co-owner(s)').'</th>');
1.79      raeburn   381:         if ($showroles) {
                    382:             $r->print('<th>'.&mt("Role(s) for [_1]",
                    383:                 &Apache::loncommon::plainname($env{'form.personfilter'},
                    384:                                               $env{'form.persondomfilter'},'firstname')).'</th>');
                    385:         }
1.71      bisitz    386:         $r->print(&Apache::loncommon::end_data_table_header_row());
                    387:     }
1.92      raeburn   388:     my %cc_cloneable;
                    389:     if (($env{'form.form'} eq 'ccrs') || ($env{'form.form'} eq 'requestcrs')) {
                    390:         my ($coord_cloneable,$warning) =
                    391:             &get_coordinator_cloneable($cloneruname,$clonerudom,$type);
                    392:         if ($coord_cloneable) {
                    393:             map {$cc_cloneable{$_} = 1;} split('&',$coord_cloneable);
                    394:         }
                    395:     }
1.54      raeburn   396:     foreach my $description (sort { lc($a) cmp lc($b) } (keys(%by_descrip))) {
                    397:         foreach my $course (@{$by_descrip{$description}}) {
1.72      raeburn   398:             $r->print(&Apache::loncommon::start_data_table_row());
1.54      raeburn   399:             my $cleandesc=&HTML::Entities::encode($description,'<>&"');
                    400:             $cleandesc=~s/'/\\'/g;
                    401:             my ($cdom,$cnum)=split(/\_/,$course);
1.85      raeburn   402:             my ($descr,$instcode,$ttype,$canclone,@owners);
1.64      raeburn   403:             if (ref($courses{$course}) eq 'HASH') {
                    404:                 $descr = $courses{$course}{'description'};
1.85      raeburn   405:                 $instcode = $courses{$course}{'inst_code'};
                    406:                 $ttype = $courses{$course}{'type'};
                    407:                 if (($env{'form.form'} eq 'ccrs') || ($env{'form.form'} eq 'requestcrs')) {
1.92      raeburn   408:                     if ($cc_cloneable{$courses{$course}{'cnum'}.':'.$courses{$course}{'domain'}}) {
                    409:                         $canclone = 1;
                    410:                     }
                    411:                     unless ($canclone) {
                    412:                         my $cloners = $courses{$course}{'cloners'};
                    413:                         if ($cloners ne '') { 
                    414:                             my @cloneable = split(',',$cloners);
                    415:                             if (grep(/^\*$/,@cloneable)) {
                    416:                                 $canclone = 1;
                    417:                             }
                    418:                             if (grep(/^\*:\Q$env{'form.clonerudom'}\E$/,@cloneable)) {
                    419:                                 $canclone = 1;
                    420:                             }
                    421:                             if (grep(/^\Q$cloneruname\E:\Q$clonerudom\E$/,@cloneable)) {
                    422:                                 $canclone = 1;
                    423:                             }
1.85      raeburn   424:                         }
                    425:                     }
                    426:                 }
1.64      raeburn   427:                 push(@owners,&unescape($courses{$course}{'owner'}));
                    428:                 if (ref($courses{$course}{'co-owners'}) eq 'ARRAY') {
                    429:                     foreach my $item (@{$courses{$course}{'co-owners'}}) {
                    430:                         push(@owners,&unescape($item));
                    431:                     }
                    432:                 }
                    433:             } else {
                    434:                 my $singleowner; 
                    435:                 ($descr,$instcode,$singleowner,$ttype)=split(/:/,$courses{$course});
                    436:                 push(@owners,&unescape($singleowner));
                    437:             }
1.93      raeburn   438:             my $ownerstr = join(', ',map { &Apache::loncommon::plainname(split(':',$_)); } @owners);
1.85      raeburn   439:             $r->print('<td>'.&course_chooser($multiple,$cdom,$cnum,$cleandesc,$canclone).'</td>');
1.71      bisitz    440:             $r->print('<td>'.$description.'</td>');
                    441:             $r->print('<td>');
                    442:             $r->print(&Apache::lonnet::domain($cdom,'description')?
                    443:                       $cdom.' ('.&Apache::lonnet::domain($cdom,'description').')':$cdom);
                    444:             $r->print('</td>');
1.89      raeburn   445:             unless ($type eq 'Community') { 
                    446:                 $r->print('<td>');
                    447:                 if ($instcode ne '') {
                    448:                     $r->print(&unescape($instcode));
                    449:                 } else {
                    450:                     $r->print('&nbsp;');
                    451:                 }
                    452:                 $r->print('</td>');
1.54      raeburn   453:             }
1.72      raeburn   454:             $r->print('<td>'.$ownerstr.'</td>');
1.79      raeburn   455:             if ($showroles) {
                    456:                 $r->print('<td>');
                    457:                 my $rolestr;
                    458:                 if (ref($courses{$course}{'roles'}) eq 'ARRAY') {
                    459:                     my @roles = sort(@{$courses{$course}{'roles'}});
                    460:                     foreach my $role (@roles) {
                    461:                         if ($role =~ /^cr/) {
                    462:                             my (undef,$crdom,$crname,$crtitle) = split('/',$role);
                    463:                             $rolestr .= $crtitle.', ';
                    464:                         } else {
1.89      raeburn   465:                             $rolestr .= &Apache::lonnet::plaintext($role,$type).', ';
1.79      raeburn   466:                         }
                    467:                     }
                    468:                     $rolestr =~ s/\, $//;
                    469:                 }
                    470:                 $r->print($rolestr.'</td>');
                    471:             }
1.54      raeburn   472:             if ($multiple) { $r->print("</label>\n"); }
1.72      raeburn   473:             $r->print(&Apache::loncommon::end_data_table_row());
1.71      bisitz    474:             # $r->print("<br />\n");
1.19      raeburn   475:         }
                    476:     }
1.72      raeburn   477:     if (%courses) {
                    478:         $r->print(&Apache::loncommon::end_data_table());
                    479:     }
1.71      bisitz    480: 
1.54      raeburn   481:     if (!%courses) {
                    482:         $r->print(&mt('None found'));
                    483:     } elsif ($multiple) {
1.57      raeburn   484:         $r->print('<input type="button" value="Submit" onClick="gochoose('."'','','')".'" />');
1.54      raeburn   485:     }
                    486:     $r->print('<input type="hidden" name="form" value="'.$env{'form.form'}.'" />'.
                    487:               "\n".'<input type="hidden" name="pickedcourse" value="" />'."\n".
                    488:               '<input type="hidden" name="type" value="'.$type.'" />'."\n");
                    489:     if ((exists($env{'form.roleelement'})) && ($env{'form.form'} eq 'rolechoice')) {
                    490:         $r->print('<input type="hidden" name="roleelement" value="'.
                    491:                   $env{'form.roleelement'}.'" />'."\n");
                    492:     }
1.55      raeburn   493:     if ($env{'form.form'} eq 'portform') {
                    494:         $r->print('<input type="hidden" name="cnum" value="" />');
                    495:         $r->print('<input type="hidden" name="cdom" value="" />');
                    496:         $r->print('<input type="hidden" name="setroles" value="'.$env{'form.setroles'}.'" />');
                    497:         $r->print('<input type="hidden" name="action" value="rolepicker" />');
1.57      raeburn   498:     } elsif ($env{'form.form'} eq 'modifycourse') {
1.85      raeburn   499:         $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','pickedcourse','type','form','numtitles','state']));
1.57      raeburn   500:     } else {
                    501:         $r->print('<input type="hidden" name="cnumelement" value="'.
                    502:                   $env{'form.cnumelement'}.'" />'."\n".  
                    503:                   '<input type="hidden" name="cdomelement" value="'.
                    504:                   $env{'form.cdomelement'}.'" />'."\n");
1.90      raeburn   505:         if ($env{'form.typeelement'} ne '') {
                    506:             $r->print('<input type="hidden" name="typeelement" value="'.
                    507:                       $env{'form.typeelement'}.'" />'."\n");
                    508: 
                    509:         }
1.55      raeburn   510:     }
1.78      raeburn   511:     if ((exists($env{'form.fixeddom'})) && ($env{'form.form'} eq 'rules')) {
                    512:         $r->print('<input type="hidden" name="fixeddom" value="'.
                    513:                   $env{'form.fixeddom'}.'" />');
                    514:     }
                    515:     if ($env{'form.numtitles'}) {
                    516:         $r->print('<input type="hidden" name="numtitles" value="'.
                    517:                   $env{'form.numtitles'}.'" />');
                    518:     }
1.54      raeburn   519:     $r->print("</form>\n");
                    520:     return;
                    521: }
                    522: 
                    523: sub multiples_tag {
1.55      raeburn   524:     my $jscript = &Apache::loncommon::check_uncheck_jscript();
                    525:     my $multelement = '<input type="hidden" name="multiple" value="1" />';
                    526:     return ($jscript,$multelement);
1.1       www       527: }
1.30      raeburn   528: 
1.54      raeburn   529: sub build_filters {
1.57      raeburn   530:     my ($filterlist,$type,$roleelement,$multelement,$filter,$action,
1.90      raeburn   531:         $numtitlesref,$caller,$cloneruname,$clonerudom,$typeelement) = @_;
1.85      raeburn   532:     my ($list,$formname,$fixeddom,$codedom,$jscript);
                    533:     $codedom = $env{'request.role.domain'};
1.57      raeburn   534:     if (defined($env{'form.form'})) {
1.81      raeburn   535:         $formname = $env{'form.form'};
                    536:     } else {
                    537:         $formname = $caller;
1.57      raeburn   538:     }
1.84      raeburn   539:     my $onchange;
                    540:     unless ($env{'form.interface'} eq 'textual') {
1.85      raeburn   541:         $onchange = 'javascript:updateFilters(this)';
1.84      raeburn   542:     }
1.94      raeburn   543:     my ($domainselectform,$sincefilterform,$createdfilterform,
                    544:         $ownerdomselectform,$persondomselectform,$instcodeform,
                    545:         $typeselectform,$instcodetitle);
1.55      raeburn   546:     foreach my $item (@{$filterlist}) {
1.54      raeburn   547:         $filter->{$item} = $env{'form.'.$item};
                    548:         if ($item ne 'descriptfilter' && $item ne 'instcodefilter') {
1.79      raeburn   549: 	    if ($item eq 'domainfilter') {
1.61      albertel  550: 		$filter->{$item} = &LONCAPA::clean_domain($filter->{$item});
                    551: 	    } elsif ($item eq 'coursefilter') {
                    552: 		$filter->{$item} = &LONCAPA::clean_courseid($filter->{$item});
                    553: 	    } elsif ($item eq 'ownerfilter') {
                    554: 		$filter->{$item} = &LONCAPA::clean_username($filter->{$item});
1.79      raeburn   555:                 $filter->{'ownerdomfilter'} = 
                    556:                     &LONCAPA::clean_domain($env{'form.ownerdomfilter'});
                    557:                 $ownerdomselectform =
                    558:                     &Apache::loncommon::select_dom_form($filter->{'ownerdomfilter'},
                    559:                                                         'ownerdomfilter',1);
                    560:             } elsif ($item eq 'personfilter') {
                    561:                 $filter->{$item} = &LONCAPA::clean_username($filter->{$item});
                    562:                 if ($env{'form.persondomfilter'} eq '') {
1.85      raeburn   563:                     unless ($env{'form.gosearch'}) {
1.79      raeburn   564:                         $filter->{'persondomfilter'} = $env{'request.role.domain'};
                    565:                     }
                    566:                 } else {
                    567:                     $filter->{'persondomfilter'} =
                    568:                         &LONCAPA::clean_domain($env{'form.persondomfilter'});
                    569:                 }
                    570:                 $persondomselectform =
                    571:                     &Apache::loncommon::select_dom_form($filter->{'persondomfilter'},
                    572:                                                         'persondomfilter',1);
1.61      albertel  573: 	    } else {
                    574: 		$filter->{$item} =~ s/\W//g;
                    575: 	    }
1.54      raeburn   576:             if (!$filter->{$item}) {
                    577:                 $filter->{$item} = '';
                    578:             }
                    579:         }
                    580:         if ($item eq 'domainfilter') {
1.63      albertel  581: 	    my $allow_blank = 1;
                    582: 	    if ($formname eq 'portform') {
                    583: 		$filter->{$item} ||= $env{'user.domain'};
                    584: 		$allow_blank=0;
1.67      raeburn   585: 	    } elsif ($formname eq 'studentform') {
                    586:                 $filter->{$item} ||= $env{'request.role.domain'};
                    587:                 $allow_blank=0;
                    588:             }
1.78      raeburn   589:             if ($env{'form.fixeddom'}) {
                    590:                 $domainselectform = '<input type="hidden" name="domainfilter"'.
                    591:                                     'value="'.$env{'request.role.domain'}.'" />'.
                    592:                                     &Apache::lonnet::domain($env{'request.role.domain'},
                    593:                                                             'description');
                    594:                 $codedom = $env{'request.role.domain'};
1.79      raeburn   595:             } else {
1.78      raeburn   596:                 $domainselectform =
                    597: 		    &Apache::loncommon::select_dom_form($filter->{$item},
                    598: 		    				        'domainfilter',
1.84      raeburn   599: 						        $allow_blank,'',$onchange);
1.78      raeburn   600:                 $codedom = $filter->{'domainfilter'};
                    601:             }
1.54      raeburn   602:         } else {
                    603:             $list->{$item} = &HTML::Entities::encode($filter->{$item},'<>&"');
                    604:         }
                    605:     }
                    606: 
                    607:     # last course activity filter and selection
1.94      raeburn   608:     $sincefilterform = &timebased_select_form('sincefilter',$filter);
                    609: 
                    610:     # course created filter and selection
                    611:     if (exists($filter->{'createdfilter'})) {
                    612:         $createdfilterform = &timebased_select_form('createdfilter',$filter);
                    613:     }
1.91      bisitz    614: 
                    615:     my %lt = &Apache::lonlocal::texthash(
                    616:                 'cac' => "$type Activity",
1.94      raeburn   617:                 'ccr' => "$type Created", 
1.91      bisitz    618:                 'cde' => "$type Title",
                    619:                 'cdo' => "$type Domain",
                    620:                 'ins' => 'Institutional Code',
                    621:                 'inc' => 'Institutional Categorization',
                    622:                 'cow' => "$type Owner/Co-owner",
                    623:                 'cop' => "$type Personnel Includes",
                    624:                 'cog' => 'Type',
1.54      raeburn   625:              );
1.49      raeburn   626: 
1.89      raeburn   627:     if (($env{'form.form'} eq 'ccrs') || ($env{'form.form'} eq 'requestcrs')) {
                    628:         my $typeval = 'Course';
                    629:         if ($type eq 'Community') {
                    630:             $typeval = 'Community';
                    631:         }
                    632:         $typeselectform = '<input type="hidden" name="type" value="'.$typeval.'" />';
                    633:     } else {
                    634:         $typeselectform =  '<select name="type" size="1"';
                    635:         if ($onchange) {
                    636:             $typeselectform .= 'onchange="'.$onchange.'"';
                    637:         }
                    638:         $typeselectform .= '>'."\n";
                    639:         foreach my $posstype ('Course','Community') {
                    640:             $typeselectform.='<option value="'.$posstype.'"'.
                    641:                 ($posstype eq $type ? ' selected="selected" ' : ''). ">".&mt($posstype)."</option>\n";
                    642:         }
                    643:         $typeselectform.="</select>";
1.84      raeburn   644:     }
1.85      raeburn   645:     
                    646:     my ($cloneableonlyform,$cloneabletitle);
                    647:     if (exists($filter->{'cloneableonly'})) {
                    648:         my $cloneableon = '';
                    649:         my $cloneableoff = ' checked="checked"';
                    650:         if ($filter->{'cloneableonly'}) {
                    651:             $cloneableon = $cloneableoff;
                    652:             $cloneableoff = '';
                    653:         }
1.86      raeburn   654:         $cloneableonlyform = '<span class="LC_nobreak"><label><input type="radio" name="cloneableonly" value="1" '.$cloneableon.'/>&nbsp;'.&mt('Required').'</label>'.('&nbsp;'x3).'<label><input type="radio" name="cloneableonly" value="" '.$cloneableoff.' />&nbsp;'.&mt('No restriction').'</label></span>';
1.85      raeburn   655:         if ($env{'form.form'} eq 'ccrs') {
                    656:             $cloneabletitle = &mt('Cloneable for').' '.$cloneruname.':'.$clonerudom;
                    657:         } else {
                    658:             $cloneabletitle = &mt('Cloneable by you');
                    659:         }
                    660:     }
1.80      raeburn   661:     my $officialjs;
1.54      raeburn   662:     if ($type eq 'Course') {
1.85      raeburn   663:         if (exists($filter->{'instcodefilter'})) {
                    664:             if (($env{'form.fixeddom'}) || ($formname eq 'requestcrs')
                    665:                 || ($formname eq 'modifycourse')) {
                    666:                 $officialjs = 1;
                    667:                 ($instcodeform,$jscript,$$numtitlesref) = 
                    668:                     &instcode_selectors($codedom,'filterpicker',$officialjs);
                    669:                 if ($jscript) {
                    670:                     $jscript = '<script type="text/javascript" language="Javascript">'.
                    671:                                $jscript.'</script>'."\n";
                    672:                 }
                    673:             }
                    674:             if ($instcodeform eq '') {
                    675:                 $instcodeform = 
                    676:                     '<input type="text" name="instcodefilter" size="10" value="'.
                    677:                     $list->{'instcodefilter'}.'" />';
                    678:                 $instcodetitle = $lt{'ins'};
                    679:             } else {
                    680:                 $instcodetitle = $lt{'inc'};
                    681:             }
                    682:             if ($env{'form.fixeddom'}) {
                    683:                 $instcodetitle .= '<br />('.$codedom.')';
                    684:             }
1.78      raeburn   685:         }
1.54      raeburn   686:     }
1.84      raeburn   687: 
1.57      raeburn   688:     my $output = qq|
1.85      raeburn   689: <form method="post" name="filterpicker" action="$action">
1.57      raeburn   690: <input type="hidden" name="form" value="$formname" />
                    691: |;
                    692:     if ($formname eq 'modifycourse') {
1.68      raeburn   693:         $output .= '<input type="hidden" name="phase" value="courselist" />'."\n".
                    694:                    '<input type="hidden" name="prevphase" value="'.
                    695:                    $env{'form.prevphase'}.'" />'."\n";
                    696:     } else {
1.57      raeburn   697:         my $name_input;
                    698:         if ($env{'form.cnameelement'} ne '') {
                    699:             $name_input = '<input type="hidden" name="cnameelement" value="'.
                    700:                           $env{'form.cnameelement'}.'" />';
                    701:         }
                    702:         $output .= qq|
1.27      albertel  703: <input type="hidden" name="cnumelement" value="$env{'form.cnumelement'}" />
                    704: <input type="hidden" name="cdomelement" value="$env{'form.cdomelement'}" />
1.13      albertel  705: $name_input
1.18      raeburn   706: $roleelement
1.35      raeburn   707: $multelement
1.90      raeburn   708: $typeelement
1.57      raeburn   709: |;
1.59      raeburn   710:         if ($formname eq 'portform') {
1.78      raeburn   711:             $output .= '<input type="hidden" name="setroles" value="'.$env{'form.setroles'}.'" />'."\n";
1.59      raeburn   712:         }
1.57      raeburn   713:     }
1.78      raeburn   714:     if ($env{'form.fixeddom'}) { 
                    715:         $output .= '<input type="hidden" name="fixeddom" value="'.$env{'form.fixeddom'}.'" />'."\n";
                    716:     }
1.74      bisitz    717:     $output .= "<br />\n".&Apache::lonhtmlcommon::start_pick_box();
1.57      raeburn   718:     if ($sincefilterform) { 
1.74      bisitz    719:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cac'})
                    720:                   .$sincefilterform
                    721:                   .&Apache::lonhtmlcommon::row_closure();
1.57      raeburn   722:     }
1.94      raeburn   723:     if ($createdfilterform) {
                    724:         $output .= &Apache::lonhtmlcommon::row_title($lt{'ccr'})
                    725:                   .$createdfilterform
                    726:                   .&Apache::lonhtmlcommon::row_closure();
                    727:     }
1.57      raeburn   728:     if ($domainselectform) {
1.74      bisitz    729:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cdo'})
                    730:                   .$domainselectform
                    731:                   .&Apache::lonhtmlcommon::row_closure();
1.78      raeburn   732:     } 
1.57      raeburn   733:     if ($typeselectform) {
1.89      raeburn   734:         if (($env{'form.form'} eq 'ccrs') || ($env{'form.form'} eq 'requestcrs')) {
                    735:             $output .= $typeselectform;
                    736:         } else {
                    737:             $output .= &Apache::lonhtmlcommon::row_title($lt{'cog'})
                    738:                       .$typeselectform
                    739:                       .&Apache::lonhtmlcommon::row_closure();
                    740:         }
1.57      raeburn   741:     }
                    742:     if ($instcodeform) {
1.78      raeburn   743:         $output .= &Apache::lonhtmlcommon::row_title($instcodetitle)
1.74      bisitz    744:                   .$instcodeform
                    745:                   .&Apache::lonhtmlcommon::row_closure();
1.57      raeburn   746:     }
                    747:     if (exists($filter->{'ownerfilter'})) {
1.79      raeburn   748:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cow'}).
                    749:                    '<table><tr><td>'.&mt('Username').'<br />'.
                    750:                    '<input type="text" name="ownerfilter" size="20" value="'.
                    751:                    $list->{'ownerfilter'}.'" /></td><td>'.&mt('Domain').'<br />'.
                    752:                    $ownerdomselectform.'</td></tr></table>'.
                    753:                    &Apache::lonhtmlcommon::row_closure();
                    754:     }
                    755:     if (exists($filter->{'personfilter'})) {
                    756:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cop'}).
                    757:                    '<table><tr><td>'.&mt('Username').'<br />'.
                    758:                    '<input type="text" name="personfilter" size="20" value="'.
                    759:                    $list->{'personfilter'}.'" /></td><td>'.&mt('Domain').'<br />'.
                    760:                    $persondomselectform.'</td></tr></table>'.
                    761:                    &Apache::lonhtmlcommon::row_closure();
1.57      raeburn   762:     }
                    763:     if (exists($filter->{'coursefilter'})) {
1.74      bisitz    764:         $output .= &Apache::lonhtmlcommon::row_title(&mt('LON-CAPA course ID'))
                    765:                   .'<input type="text" name="coursefilter" size="25" value="'
                    766:                   .$list->{'coursefilter'}.'" />'
                    767:                   .&Apache::lonhtmlcommon::row_closure();
1.57      raeburn   768:     }
1.85      raeburn   769:     if ($cloneableonlyform) {
                    770:         $output .= &Apache::lonhtmlcommon::row_title($cloneabletitle).
                    771:                    $cloneableonlyform.&Apache::lonhtmlcommon::row_closure();
                    772:     }
1.57      raeburn   773:     if (exists($filter->{'descriptfilter'})) {
1.74      bisitz    774:         $output .= &Apache::lonhtmlcommon::row_title($lt{'cde'})
                    775:                   .'<input type="text" name="descriptfilter" size="40" value="'
                    776:                   .$list->{'descriptfilter'}.'" />'
                    777:                   .&Apache::lonhtmlcommon::row_closure(1);
1.57      raeburn   778:     }
1.85      raeburn   779:     $output .= &Apache::lonhtmlcommon::end_pick_box().'<p>';
1.92      raeburn   780:     my ($coord_cloneable,$warning);
1.85      raeburn   781:     if (($env{'form.form'} eq 'ccrs') || ($env{'form.form'} eq 'requestcrs')) {
1.92      raeburn   782:         ($coord_cloneable,$warning) = 
                    783:             &get_coordinator_cloneable($cloneruname,$clonerudom,$type);
                    784:         if ($env{'form.form'} eq 'ccrs') {
                    785:             $output .= '<input type="hidden" name="cloner" value="'.$env{'form.cloner'}.'" />'."\n";
1.85      raeburn   786:         }
1.92      raeburn   787:         if ($coord_cloneable) {
                    788:             $output .= '<input type="hidden" name="cc_clone" value="'.$coord_cloneable.'" />';
1.85      raeburn   789:         }
                    790:     }
                    791:     $output .= '<input type="hidden" name="updater" value="">'."\n".
1.84      raeburn   792:                '<input type="submit" name="gosearch" value="'. 
1.70      raeburn   793:                &mt('Search').'" /></p>'."\n".'</form>'."\n".'<hr />'."\n";
1.85      raeburn   794:     return $jscript.$warning.$output;
1.78      raeburn   795: }
                    796: 
1.94      raeburn   797: sub timebased_select_form {
                    798:     my ($item,$filter) = @_;
                    799:     if (ref($filter) eq 'HASH') {
                    800:         $filter->{$item} = $env{'form.'.$item};
                    801:         $filter->{$item} =~ s/[^\d-]//g;
                    802:         if (!$filter->{$item}) { $filter->{$item}=-1; }
                    803:         return &Apache::loncommon::select_form( 
                    804:                             $filter->{$item},
                    805:                             $item,
1.95    ! raeburn   806:                             {      '-1' => '',
1.94      raeburn   807:                                 '86400' => &mt('today'),
                    808:                                '604800' => &mt('last week'),
                    809:                               '2592000' => &mt('last month'),
                    810:                               '7776000' => &mt('last three months'),
                    811:                              '15552000' => &mt('last six months'),
                    812:                              '31104000' => &mt('last year'),
                    813:                     'select_form_order' =>
                    814:                            ['-1','86400','604800','2592000','7776000',
1.95    ! raeburn   815:                             '15552000','31104000']});
1.94      raeburn   816:     }
                    817: }
                    818: 
1.92      raeburn   819: sub get_coordinator_cloneable {
                    820:     my ($cloneruname,$clonerudom,$type) = @_;
                    821:     if (&Apache::lonnet::homeserver($cloneruname,$clonerudom) eq 'no_host') {
                    822:         my $warning = '<div class="LC_error">'.&mt('Intended course owner does not exist').
                    823:                    '</div>';
                    824:         return ('',$warning);
                    825:     } else {
                    826:         my ($cc_clone,$ccrole);
                    827:         if ($type eq 'Community') {
                    828:             $ccrole = 'co';
                    829:         } elsif ($type eq 'Course') {
                    830:             $ccrole = 'cc';
                    831:         }
                    832:         my %ccroles = &Apache::lonnet::get_my_roles($cloneruname,$clonerudom,
                    833:                                                     'userroles',['active'], [$ccrole]);
                    834:         foreach my $key (sort(keys(%ccroles))) {
                    835:             my ($cnum,$cdom,$role) = split(':',$key);
                    836:             $cc_clone .= $cdom.':'.$cnum.'&';
                    837:         }
                    838:         $cc_clone =~ s/\&$//;
                    839:         return ($cc_clone);
                    840:     }
                    841: }
                    842: 
1.78      raeburn   843: sub instcode_selectors {
1.80      raeburn   844:     my ($codedom,$formname,$officialjs) = @_;
1.78      raeburn   845:     my ($output,@codetitles,%cat_titles,%cat_order,%cat_items);
                    846:     my ($jscript,$totcodes,$numtitles,$lasttitle) =
                    847:         &Apache::courseclassifier::instcode_selectors_data($codedom,$formname,
1.80      raeburn   848:                            \%cat_items,\@codetitles,\%cat_titles,\%cat_order,$officialjs);
1.78      raeburn   849:     if ($numtitles > 0) {
1.84      raeburn   850:         my $official = ' checked="checked" ';
                    851:         my $unofficial = '';
                    852:         if ($env{'form.official'} eq 'off') {
                    853:             $unofficial = $official;
                    854:             $official = '';
1.80      raeburn   855:         }
                    856:         $output .= '<span class="LC_nobreak">'.&mt('Official course:').'&nbsp;<label>'.
1.84      raeburn   857:                    '<input type="radio" name="official" value="on"'.$official.' />'.
1.80      raeburn   858:                    &mt('Yes').'</label>'.('&nbsp;'x3).'<label>'.
1.84      raeburn   859:                    '<input type="radio" name="official" value="off"'.$unofficial.
1.80      raeburn   860:                    ' onclick="toggleOfficial();" />'.&mt('No').'</label></span><br />'.
                    861:                    &Apache::courseclassifier::build_instcode_selectors($numtitles,
                    862:                    $lasttitle,\%cat_items,\@codetitles,\%cat_titles,\%cat_order)."\n".
                    863:                    '<input type="hidden" name="numtitles" value="'.$numtitles.'" />'."\n".
                    864:                    '<input type="hidden" name="state" value="listing" />'."\n";
1.78      raeburn   865:  
                    866:     }
                    867:     return ($output,$jscript,$numtitles);
1.54      raeburn   868: }
                    869: 
                    870: sub search_courses {
1.85      raeburn   871:     my ($r,$type,$onlyown,$filter,$numtitles,$cloneruname,$clonerudom) = @_;
1.89      raeburn   872:     my (%courses,%showcourses,$cloner,$ccrole);
                    873:     if ($type eq 'Community') {
                    874:         $ccrole = 'co';
                    875:     } else {
                    876:         $ccrole = 'cc';
                    877:     }
1.54      raeburn   878:     if (!$onlyown) {
1.71      bisitz    879:         $r->print(&mt('Searching ...').'<br />&nbsp;<br />');
1.6       www       880:         $r->rflush();
1.54      raeburn   881:         if (($filter->{'ownerfilter'} ne '') ||
                    882:             ($filter->{'ownerdomfilter'} ne '')) {
                    883:             $filter->{'combownerfilter'} = $filter->{'ownerfilter'}.':'.
                    884:                                            $filter->{'ownerdomfilter'};
                    885:         }
1.80      raeburn   886:         foreach my $item ('descriptfilter','coursefilter','combownerfilter') {
1.54      raeburn   887:             if (!$filter->{$item}) {
                    888:                 $filter->{$item}='.';
                    889:             }
1.52      raeburn   890:         }
1.94      raeburn   891:         my $now = time;
1.54      raeburn   892:         my $timefilter = 
1.94      raeburn   893:            ($filter->{'sincefilter'}==-1?1:$now-$filter->{'sincefilter'});
                    894:         my ($createdbefore,$createdafter);
                    895:         if (($filter->{'createdfilter'} ne '') && ($filter->{'createdfilter'} !=-1)) {
                    896:             $createdbefore = $now;
                    897:             $createdafter = $now-$filter->{'createdfilter'};
                    898:         }
1.78      raeburn   899:         my ($instcodefilter,$regexpok);
                    900:         if ($numtitles) {
1.84      raeburn   901:             if ($env{'form.official'} eq 'on') {
1.80      raeburn   902:                 $instcodefilter = 
1.84      raeburn   903:                     &Apache::courseclassifier::instcode_search_str($filter->{'domainfilter'},$numtitles);
1.80      raeburn   904:                 $regexpok = 1;
1.84      raeburn   905:             } elsif ($env{'form.official'} eq 'off') {
                    906:                 $instcodefilter = &Apache::courseclassifier::instcode_search_str($filter->{'domainfilter'},$numtitles);
                    907:                 unless ($instcodefilter eq '') {
                    908:                     $regexpok = -1;
                    909:                 }
1.80      raeburn   910:             }
1.78      raeburn   911:         } else {
                    912:             $instcodefilter = $filter->{'instcodefilter'};
                    913:         }
1.80      raeburn   914:         if ($instcodefilter eq '') { $instcodefilter = '.'; }
                    915:         if ($type eq '') { $type = '.'; }
1.85      raeburn   916:  
                    917:         if (($clonerudom ne '') && ($cloneruname ne '')) {
                    918:             $cloner = $cloneruname.':'.$clonerudom;  
                    919:         }
1.54      raeburn   920:         %courses = 
                    921:             &Apache::lonnet::courseiddump($filter->{'domainfilter'},
                    922:                                           $filter->{'descriptfilter'},
                    923:                                           $timefilter,
1.78      raeburn   924:                                           $instcodefilter,
1.54      raeburn   925:                                           $filter->{'combownerfilter'},
                    926:                                           $filter->{'coursefilter'},
1.85      raeburn   927:                                           undef,undef,$type,$regexpok,undef,undef,
                    928:                                           undef,undef,$cloner,$env{'form.cc_clone'},
1.94      raeburn   929:                                           $filter->{'cloneableonly'},
                    930:                                           $createdbefore,$createdafter);
1.79      raeburn   931:         if (($filter->{'personfilter'} ne '') && ($filter->{'persondomfilter'} ne '')) {
                    932:             my %rolehash = &Apache::lonnet::get_my_roles($filter->{'personfilter'},
                    933:                                                          $filter->{'persondomfilter'},
                    934:                                                          'userroles',undef,
1.89      raeburn   935:                                                          [$ccrole,'in','ad','ep','ta','cr'],
1.79      raeburn   936:                                                          $filter->{'domainfilter'});
                    937:             foreach my $role (keys(%rolehash)) {
                    938:                 my ($cnum,$cdom,$courserole) = split(':',$role);
                    939:                 my $cid = $cdom.'_'.$cnum;
                    940:                 if (exists($courses{$cid})) {
                    941:                     if (ref($courses{$cid}) eq 'HASH') {
                    942:                         if (ref($courses{$cid}{roles}) eq 'ARRAY') {
                    943:                             if (!grep(/^\Q$courserole\E$/,@{$courses{$cid}{roles}})) { 
                    944:                                 push (@{$courses{$cid}{roles}},$courserole);
                    945:                             }
                    946:                         } else {
                    947:                             $courses{$cid}{roles} = [$courserole];
                    948:                         }
                    949:                         $showcourses{$cid} = $courses{$cid};
                    950:                     }
                    951:                 }
                    952:             }
                    953:             %courses = %showcourses;
                    954:         }
1.54      raeburn   955:     } else {
                    956:         $r->print('<br />');
1.60      raeburn   957:         my %coursehash = &Apache::loncommon::findallcourses(); 
                    958:         foreach my $cid (sort(keys(%coursehash))) {
1.65      raeburn   959:             $courses{$cid}{'description'} = $env{'course.'.$cid.'.description'};
1.36      raeburn   960:         }
1.2       www       961:     }
1.54      raeburn   962:     return %courses;
1.18      raeburn   963: }
1.1       www       964: 
1.30      raeburn   965: sub course_chooser {
1.85      raeburn   966:     my ($multiple,$cdom,$cnum,$cleandesc,$canclone) = @_;
1.30      raeburn   967:     my $output; 
1.35      raeburn   968:     if ($multiple) {
1.32      albertel  969:         $output = '<label><input type="checkbox" name="course_id" value="'.$cdom.'_'.$cnum.'" />'."\n";
1.85      raeburn   970:     } elsif ((($env{'form.form'} eq 'ccrs') || ($env{'form.form'} eq 'requestcrs')) && (!$canclone))  {
                    971:         if ($env{'form.form'} eq 'ccrs') {
                    972:             $output = &mt('No cloning for ').$env{'form.cloner'}."\n";
                    973:         } else {
                    974:             $output = &mt('No rights to clone')."\n";
                    975:         }
1.30      raeburn   976:     } else {
1.71      bisitz    977:         $output = '<input type="button" value="'.&mt('Select').'" onClick="gochoose('.
1.30      raeburn   978:                   "'".$cnum."','".$cdom."','".$cleandesc."')".'" />'."\n";
                    979:     }
                    980:     return $output;
1.49      raeburn   981: }
                    982: 
1.55      raeburn   983: sub gochoose_javascript {
1.57      raeburn   984:     my ($type,$multiple,$autosubmit,$lastaction) = @_;
1.55      raeburn   985:     my %elements = (
                    986:                      'Course' => {
                    987:                                  name  => 'coursepick',
                    988:                                  total => 'coursetotal',
                    989:                                  list  => 'courselist',
                    990:                                  },
1.83      raeburn   991:                      'Community' => {
1.88      raeburn   992:                                  name  => 'coursepick',
                    993:                                  total => 'coursetotal',
                    994:                                  list  => 'courselist',
1.55      raeburn   995:                               },
                    996:                     );
1.57      raeburn   997:     my $output .= qq|
1.55      raeburn   998: <script type="text/javascript">
1.57      raeburn   999: function gochoose(cname,cdom,cdesc) {
1.55      raeburn  1000:     var openerForm = "$env{'form.form'}";
                   1001:     courseCount = 0;
                   1002:     var courses = '';
1.57      raeburn  1003: |;
                   1004:     if ($multiple) {
                   1005:         $output .= <<"ENDSCRIPT";
                   1006:     courseCount = 0;
                   1007:     var courses = '';
                   1008:     if (typeof(document.courselist.course_id.length) == 'undefined') {
                   1009:         // only 1 course checkbox was created
                   1010:         if (document.courselist.course_id.checked) {
                   1011:             courses = courses + document.courselist.course_id.value + "&&";
                   1012:             courseCount ++;
                   1013:         }
                   1014:     } else {
                   1015:         for (var j=0; j<document.courselist.course_id.length; j++) {
                   1016:             if (document.courselist.course_id[j].checked) {
                   1017:                 courses = courses + document.courselist.course_id[j].value + "&&";
1.55      raeburn  1018:                 courseCount ++;
                   1019:             }
1.57      raeburn  1020:         }
                   1021:     }
                   1022:     opener.document.$env{'form.form'}.$elements{$type}{'total'}.value = courseCount;
                   1023:     if (typeof(opener.document.$env{'form.form'}.$elements{$type}{'name'}.length) ==
                   1024:         'undefined') {
                   1025:         if (opener.document.$env{'form.form'}.$elements{$type}{'name'}.value == 'specific') {
                   1026:             opener.document.$env{'form.form'}.$elements{$type}{'name'}.checked = true;
1.55      raeburn  1027:         } else {
1.57      raeburn  1028:             opener.document.$env{'form.form'}.$elements{$type}{'name'}.checked = false;
1.55      raeburn  1029:         }
1.57      raeburn  1030:     } else {
                   1031:         for (var j=0; j<opener.document.$env{'form.form'}.$elements{$type}{'name'}.length; j++) {
                   1032:             if (opener.document.$env{'form.form'}.$elements{$type}{'name'}\[j].value == 'specific') {
                   1033:                 opener.document.$env{'form.form'}.$elements{$type}{'name'}\[j].checked = true;
1.55      raeburn  1034:             } else {
1.57      raeburn  1035:                 opener.document.$env{'form.form'}.$elements{$type}{'name'}\[j].checked = false;
1.54      raeburn  1036:             }
1.55      raeburn  1037:         }
1.57      raeburn  1038:     }
                   1039:     if (courseCount > 0) {
                   1040:         courses = courses.substr(0,courses.length-2);
                   1041:         opener.document.$env{'form.form'}.$elements{$type}{'list'}.value = courses;
                   1042:     }
                   1043: ENDSCRIPT
                   1044:     } else {
1.90      raeburn  1045:         my ($name_code,$type_code);
1.57      raeburn  1046:         if ($env{'form.cnameelement'} ne '') {
                   1047:             $name_code = 'opener.document.'.$env{'form.form'}.'.'.
                   1048:                           $env{'form.cnameelement'}.'.value=cdesc;';
1.55      raeburn  1049:         }
1.90      raeburn  1050:         if ($env{'form.typeelement'} ne '') {
                   1051:             $type_code = 'opener.document.'.$env{'form.form'}.'.'.
                   1052:                           $env{'form.typeelement'}.'.value=document.courselist.type;';
                   1053:         }
                   1054: 
1.57      raeburn  1055:         $output .= qq|
1.55      raeburn  1056:         $name_code
1.90      raeburn  1057:         $type_code
1.55      raeburn  1058:         opener.document.$env{'form.form'}.$env{'form.cnumelement'}.value=cname;
                   1059:         var slct=opener.document.$env{'form.form'}.$env{'form.cdomelement'};
                   1060:         if (slct.options == undefined) {
                   1061:             opener.document.$env{'form.form'}.$env{'form.cdomelement'}.value=cdom;
                   1062:         }
                   1063:         else {
                   1064:             var i;
                   1065:             for (i=0;i<slct.length;i++) {
                   1066:                 if (slct.options[i].value==cdom) { slct.selectedIndex=i; }
1.54      raeburn  1067:             }
                   1068:         }
1.57      raeburn  1069: |;
1.54      raeburn  1070:     }
1.57      raeburn  1071:     $output .= qq|
1.55      raeburn  1072:     if (openerForm == 'portform') {
                   1073:         document.courselist.cnum.value = cname;
                   1074:         document.courselist.cdom.value = cdom;
                   1075:     }
                   1076:     $autosubmit
                   1077:     $lastaction
1.54      raeburn  1078: }
1.55      raeburn  1079: </script>
1.57      raeburn  1080: |;
1.55      raeburn  1081:     return $output;
1.54      raeburn  1082: }
                   1083: 
1.55      raeburn  1084: 1;
                   1085: __END__
                   1086: 
                   1087: =pod
                   1088: 
                   1089: =head1 NAME
                   1090: 
                   1091: Apache::lonpickcourse - Search for course(s) based on user-specified criteria.   
                   1092: 
                   1093: =head1 SYNOPSIS
                   1094: 
                   1095: Invoked by other LON-CAPA modules, when course(s) need to be selected by the user. 
                   1096: 
                   1097: =head1 OVERVIEW
                   1098: 
                   1099: Two screens are typically displayed to the user.  The first is a set of criteria which are used to constrain the search for courses.
                   1100: 
                   1101: =head2 Search Criteria (Screen One)
                   1102: 
                   1103: =head3 Criteria:
                   1104: 
                   1105: =over 4
                   1106: 
                   1107: =item *
                   1108: Course Activity - how recently was course last visited by anyone.
                   1109: 
                   1110: =item *
                   1111: Course Domain - the domain of the course
                   1112: 
                   1113: =item *
1.84      raeburn  1114: Type - Course or Community
1.55      raeburn  1115: 
                   1116: =item *
                   1117: Course Institutional Code - the institutional identifier assigned to the course
                   1118: 
                   1119: =item * 
                   1120: Course Owner's Username - the username of the owner of the course (assigned by the Domain Coordinator and/or when the course was created).
                   1121: 
                   1122: =item *
                   1123: Course Owner's Domain - the domain of the owner of the course
                   1124: 
                   1125: =item * 
1.76      bisitz   1126: Course Title - text which appears in the Course Title, as set in the Course Parameters.
1.55      raeburn  1127: 
                   1128: =item *
                   1129: 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).
                   1130: 
                   1131: =back
                   1132: 
                   1133: 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'}).
                   1134: 
                   1135: =head2 Course Display (Screen Two)
                   1136: 
                   1137: 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:
                   1138: 
                   1139: =over 4
                   1140: 
                   1141: =item *
                   1142: Course description
                   1143: 
                   1144: =item *
                   1145: Domain description of course domain
                   1146: 
                   1147: =item *
                   1148: Course institutional code
                   1149: 
                   1150: =item * 
                   1151: Course owner (username:domain)   
                   1152:  
                   1153: =back
                   1154: 
                   1155: 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.
                   1156: 
                   1157: 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.
                   1158: 
                   1159: =head1 SUBROUTINES
                   1160: 
                   1161: =over 4
                   1162: 
                   1163: =item *
                   1164: X<create_user_javascript()>
                   1165: B<create_user_javascript($type)>:
                   1166: 
1.83      raeburn  1167: Input: 1 - $type  - the course type - Course or Community
1.55      raeburn  1168: 
                   1169: Output: 1 - $output - javascript wrapped in E<lt>scriptE<gt>E<lt>/scriptE<gt> tags 
                   1170: 
                   1171: Side Effects: None 
                   1172: 
                   1173: 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.
                   1174: 
                   1175: 
                   1176: =item *
                   1177: X<display_matched_courses()>
1.85      raeburn  1178: B<display_matched_courses($r,$type,$multiple,$action,$showroles,$cloneruname,$clonerudom,%courses)>:
1.55      raeburn  1179: 
1.85      raeburn  1180: Input: 7 - request object, course type, multiple (0 or 1), form action, whether to show roles (for course personnel filter), username of new course owner, domain of new course owner, hash of courses.
1.55      raeburn  1181: 
                   1182: Output: 0
                   1183: 
                   1184: 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.
                   1185: 
                   1186: =item *
                   1187: X<multiples_tag()>
                   1188: B<multiples_tag()>:
                   1189: 
                   1190: 
                   1191: Input: 0
                   1192: 
                   1193: Output: 2 - $jscript - javascript for check all/uncheck all checkboxes; $multelement - hidden form element with multiple set to 1.
                   1194: 
                   1195: Side Effects: None
                   1196: 
                   1197: =item *
                   1198: X<build_filters()>
1.90      raeburn  1199: B<build_filters($filterlist,$type,$roleelement,$multelement,$filter,$action,$numfiltersref,$caller,$cloneruname,$clonerudom,$typeelem)>:
1.55      raeburn  1200: 
                   1201: 
1.90      raeburn  1202: Input: 11 - anonymous array of search criteria; course type; $roleelement ; $multelement ; anonymous hash of criteria and their values; form action; ref to scalar (count of number of elements in institutional codes -- e.g., 4 for year, semester, department, and number); caller context (e.g., set to 'modifycourse' when routine is called from lonmodifycourse.pm), username and domain of new course owner who wants to clone, $tyepelem 
1.55      raeburn  1203: 
                   1204: Output: 1 - $output - HTML for display of search criteria, and hidden form elements. 
                   1205: 
                   1206: Side Effects: None
                   1207: 
                   1208: =item *
                   1209: X<search_courses()>
1.85      raeburn  1210: B<search_courses($r,$type,$onlyown,$filter,$numtitles,$cloneruname,$clonerudom)>:
1.55      raeburn  1211: 
                   1212: 
1.85      raeburn  1213: Input: 7 -  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; for institutional codes - number of categories; optional username of new course owner, optional domain of new course owner). Last two needed when search is for courses to clone from course request form, or course creation form (DC).   
1.55      raeburn  1214: 
                   1215: Output: 1 - %courses - hash of courses satisfying search criteria, keys = course IDs, values are corresponding colon-separated escaped description, institutional code, owner and type.
                   1216: 
                   1217: Side Effects: None
                   1218: 
                   1219: 
                   1220: =item *
                   1221: X<course_chooser()>
1.85      raeburn  1222: B<course_chooser($multiple,$cdom,$cnum,$cleandesc,$canclone)>:
1.55      raeburn  1223: 
1.85      raeburn  1224: Input: 5 - single (0) or multiple (1) courses; course domain, course number; course description; can clone course (1 if new course owner has cloning rights). 
1.55      raeburn  1225: 
1.85      raeburn  1226: Output: 1 - HTML for either checkbox (multiple=1) or select button (multiple=0) for user to indicate course selection.
1.55      raeburn  1227: 
                   1228: Side Effects: None
                   1229: 
                   1230: 
                   1231: =item *
                   1232: X<gochoose_javascript()>
1.57      raeburn  1233: B<gochoose_javascript($type,$multiple,$autosubmit,$lastaction)>:
1.55      raeburn  1234: 
1.66      raeburn  1235: 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  1236: 
                   1237: Output: 1  $output - javascript wrapped in E<lt>scriptE<gt>E<lt>/scriptE<gt> tags
                   1238: 
                   1239: Side Effects: None
                   1240: 
                   1241: javascript functions used when user selects a course(s). Different behavior depending on context:
                   1242: 
                   1243: =back
                   1244: 
                   1245: =over 8
                   1246: 
                   1247: =item
                   1248: 
                   1249: (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.
                   1250: 
                   1251: =item
                   1252: 
                   1253: (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.
                   1254: 
                   1255: =item
                   1256: 
                   1257: (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.
                   1258: 
                   1259: =item
1.54      raeburn  1260: 
1.55      raeburn  1261: (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  1262: 
1.55      raeburn  1263: =item
1.54      raeburn  1264: 
1.55      raeburn  1265: (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  1266: 
1.55      raeburn  1267: =item
1.54      raeburn  1268: 
1.55      raeburn  1269: (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  1270: 
1.55      raeburn  1271: =item
1.49      raeburn  1272: 
1.55      raeburn  1273: (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  1274: 
1.84      raeburn  1275: =item
                   1276: 
                   1277: (h) User requesting creation of a course, and selecting a course to clone - course number and domain written to visible form elements in opener window. Child window closes.
                   1278: 
1.55      raeburn  1279: =back
                   1280:      
                   1281: =cut

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