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

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

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