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

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

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