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

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

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