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

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

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