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

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

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