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

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

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