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

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

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