File:  [LON-CAPA] / loncom / interface / lonblockingmenu.pm
Revision 1.9: download - view: text, annotated - select for diffs
Fri Jan 4 05:45:50 2013 UTC (11 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_0_RC1, HEAD
- Remove duplicate lines
- Correct syntax for decrement operator.

    1: # The LearningOnline Network with CAPA
    2: # Routines for configuring blocking of access to collaborative functions, 
    3: # and specific resources during an exam
    4: #
    5: # $Id: lonblockingmenu.pm,v 1.9 2013/01/04 05:45:50 raeburn Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: ##############################################################
   30: ##############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: lonblockingmenu - Handler to set/modify exam blocks in a course.
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: lonblockingmenu provides an interface for setting exam blocks in a course.  
   41: 
   42: =head1 DESCRIPTION
   43: 
   44: This module is used to configure blocking of access to collaborative tools
   45: and/or resources during an exam.
   46: 
   47: =head1 OVERVIEW
   48: 
   49: To support high-stakes testing, LON-CAPA provides Coordinators with the
   50: ability to disable communication and collaborative features within the
   51: system for the duration of an exam.
   52: 
   53: Features which can be disabled include:
   54: (a) those which a student could use to communicate with another student.
   55: Messaging, discussion, chat, blogs, and some functionality in groups fall 
   56: into this category.
   57: (b) those which a student could use to access materials prepared by the
   58: student in advance of an exam, (e.g., for use during an online exam, to
   59: gain an unfair advantage). Blogs and portfolio fall into this category.
   60: 
   61: For communication blocking to be truly effective in preventing unwanted
   62: communication, or access to online materials, online testing needs to
   63: take place in a lab setting where use of tools outside LON-CAPA, and use
   64: of web sites beyond LON-CAPA are unavailable.
   65: 
   66: Access to specified folder(s) and/or resources in the course contents 
   67: can also be restricted for the duration of an exam.
   68: 
   69: Exam blocks are of two types:
   70: (a) Blocks with a defined start and end date.
   71: (b) Blocks associated with a timed interval set for a specific folder,
   72: or resource.
   73: 
   74: When a student attempts to use a collaboration or communication feature
   75: which is currently blocked, information will be available about the
   76: duration of the block, and the identity of the Course Coordinator who
   77: set the block.
   78: 
   79: Although LON-CAPA communication can be blocked during an exam, course
   80: personnel with the 'evb' (evade blocking) privilege will continue to
   81: receive LON-CAPA messages sent from students in a course with an active
   82: block on messaging. Students will not be able to view messages sent by
   83: other students in the same course for the duration of the blocking event.
   84: 
   85: Because students may be enrolled in more than one LON-CAPA course at a time
   86: it is important to use reasonable time windows for blocking events, or, in
   87: the case of blocks triggered by clicking a button to start a timed quiz, 
   88: quiz durations that are of limited duration. This is especially important
   89: when blocking prtfolio access, as other courses may require students to use
   90: the portfolio as a mechanism for submitting assigments.
   91: 
   92: Information about blocks in a course will be cached for 10 minutes, so,
   93: as with parameters set for resources, it can take up to 10 minutes for
   94: new blocks, or changes to existing blocks, to propagate to other servers.
   95: 
   96: Changes to existing blocks on the server hosting your current session
   97: are available immediately, as cached data on blocks is devalidated
   98: automatically on the current server whenever a change is made to a 
   99: block (including deletion), or when a new block is added. 
  100: 
  101: =head1 INTERNAL SUBROUTINES
  102: 
  103: =over
  104: 
  105: =item &get_timed_items()
  106: 
  107: Provides perl data structure with information about timed interval
  108: parameters set in a course.
  109: 
  110: Inputs: 2 (optional)
  111:        $cdom - course's domain
  112: 
  113:        $cnum - course's ID
  114: 
  115: Output: 1 Hash 
  116:        nested hashes containing information about timed interval
  117:        parameters in course). Top level keys are type: course,
  118:        map, resource. Next inner keys are map or symb. Next
  119:        inner keys are scope (all, section, group, users).
  120:        Values are interval (in seconds).
  121: 
  122: =item &blockstore()
  123: 
  124: Stores changes to exam blocks in comm_block.db file for course.
  125: Processes deletions, modifications and additions.
  126: 
  127: Inputs: 2
  128:       $crstype - Container type: Course or Community.
  129: 
  130:       $blockcount - Total number of blocking events in course.
  131: 
  132: Outputs: 2
  133:       $changestotal - Total number of changes made.
  134: 
  135:       $output - Information about changes made.
  136: 
  137: 
  138: =item &enumerate_course_contents()
  139: 
  140: Create hashes of maps (for folders/pages) and symbs (for resources) in
  141: a course, where keys are numbers (starting with 1) and values are
  142: map url, or symb, for an iteration through the course, as seen by
  143: a Course Coordinator. Used to generate numerical IDs to facilitate
  144: storage of lists of maps or resources to be blocked during an exam.   
  145: 
  146: Inputs: 3
  147:       $navmap - navmaps object
  148: 
  149:       $map_url - reference to hash to contain URLs of maps in course
  150: 
  151:       $resource_symb - reference to hash to contain symbs for
  152:                        resources in course
  153: 
  154: Outputs: None
  155: 
  156: Side Effects: $map_url and $resource_symb hashrefs are populated.
  157: 
  158: 
  159: =item &get_dates_from_form()
  160: 
  161: Extract start and end dates from web form input for blocks with
  162: defined start/end time.
  163: 
  164: Inputs: 1 - $item - numeric ID of current block.
  165: 
  166: Outputs: 2 - $startdate, $enddate (UNIX times for start and end times
  167:              for blocks with defined start/end   
  168: 
  169: 
  170: =item &get_blockdates()
  171: 
  172: Retrieves contents of comm_block.db file for a course.
  173: 
  174: Inputs: 1 - $records - reference to hash to contain blocks 
  175: 
  176: Outputs: 1 - $blockcount - number of blocks
  177: 
  178: Side Effects: populates records hashref.
  179: 
  180: 
  181: =item &get_block_choices()
  182: 
  183: Extract information from web form about which communication/
  184: collaboration features are to be blocked, for a partilcuar event,
  185: and also which content areas will have access blocked for the
  186: duration of the block.
  187: 
  188: Inputs: 3 
  189:     - $item - numeric ID of current block 
  190: 
  191:     - $map_ref - reference to hash mapping numeric IDs to map urls 
  192: 
  193:     - $symb_ref - reference to hash mapping numeric IDs to symbs
  194: 
  195: Outputs: 2
  196:     - $blocktypes - reference to hash of features to be blocked
  197: 
  198:     - $blockdocs - boolean - 0 if no blocking of content, 1 if blocking 
  199:                              of content access 
  200: 
  201: 
  202: =item &check_release_required()
  203: 
  204: Update LON-CAPA version requirements for course if blocked items
  205: (content) or blocking type (triggered by student starting timer)
  206: require specific LON-CAPA version (i.e., 2.11).
  207: 
  208: Inputs: 1 - type of constraint (currently: 'docs' or 'timer'). 
  209: 
  210: Outputs: None
  211: 
  212: Side Effects: &update_released_required() called in lonnet, if
  213:               needed to update version requirements for course.   
  214: 
  215: 
  216: =item &display_blocker_status()
  217: 
  218: Generates web form elements used to display, cancel, or modify 
  219: existing blocking events. 
  220: 
  221: Inputs: 7 
  222:       - $r - Apache request object
  223: 
  224:       - $records - Reference to hash of current blocks
  225: 
  226:       - $ltext - Reference to hash of phrases (localized)
  227: 
  228:       - $intervals - Reference to hash of parameters for timed intervals
  229: 
  230:       - $navmap - navmaps object.
  231: 
  232:       - $errormsg - error message for display, if navmaps object
  233:                     could not be instantiated
  234: 
  235:       - $blockcount - number of existing blocking events in course
  236: 
  237: Output: None
  238: 
  239: Side Effects: prints web form elements (in a table) for current blocks. 
  240: 
  241: =item &convlim()
  242: 
  243: Convert a time interval used for a timed quiz (in seconds) to
  244: days, hours. minutes and seconds.
  245: 
  246: Inputs: 1 - $timelimit  - time interval in seconds
  247: 
  248: Outputs: 1 - $output - time in format: DD days, HH hours, MM minutes, SS seconds  
  249: 
  250: 
  251: =item &display_addblocker_table()
  252: 
  253: Generate web form elements used to define a new blocking event. 
  254: 
  255: Inputs: 6
  256:     - $r - Apache resource object
  257: 
  258:     - $parmcount - current ID for block (same as number of current blocks,
  259:                    block IDs in web form have zero-based index)
  260: 
  261:     - $ltext - reference to hash of phrases (localized)
  262: 
  263:     - $intervals - Reference to hash of parameters for timed intervals
  264: 
  265:     - $navmap - navmaps object
  266: 
  267:     - $errormsg - error message for display, if navmaps object
  268:                   could not be instantiated
  269: 
  270: Outputs: None
  271:  
  272: Side Effects: prints web form elements (in a table) for adding a new block.
  273: 
  274: 
  275: =item &blocker_checkboxes()
  276: 
  277: Generates web form elements in a table for checkboxes used to indicate
  278: which types of communication/collaboration and/or content should be blocked.
  279: 
  280: Inputs: 4 
  281:     - $parmcount - numeric ID of current block
  282: 
  283:     - $blocks - reference to hash of functionalities to block 
  284: 
  285:     - $jschg - text of javascript call to execute when checkbox clicked  
  286:                use within a box via 'onclick="$jchg"'
  287:  
  288:     - $lookups - reference to hash to map urls or symbs to numeric IDs
  289:                  used to populate hodden form elements containing list
  290:                  of resources and folders with access blocking currently set.
  291: 
  292: Output: 1 - HTML for table of checkboxes for current block  
  293: 
  294: 
  295: =item &create_interval_form()
  296: 
  297: Creates web form elements used to select one of the defined timed interval 
  298: items in the course for use in an exam block of type: "Triggered by 
  299: Activating Timer".
  300: 
  301: Inputs: 7 (three required, last four optional)
  302:    - $intervals - Reference to hash of parameters for timed intervals
  303: 
  304:    - $parmcount - numeric ID of current block
  305: 
  306:    - $navmap - navmaps object
  307: 
  308:    - $currkey - current interval (where this is a block already using
  309:                 an interval-based trigger).  
  310: 
  311:    - $jschg - text of javascript call to execute when radiobutton clicked
  312:               use within a box via 'onclick="$jchg"'
  313: 
  314:    - $itemname - name/scope of current interval used for this block 
  315: 
  316:    - $iteminfo - Expandable/collapsible block showing which users are
  317:                  able to activate the timer using the current trigger item.
  318: 
  319: Outputs: 1 - $intervalform - web form elements used to select a time interval
  320: 
  321: 
  322: =item &trigger_details_toggle()
  323:  
  324: Creates link used to expand item showing information about timer for current
  325: trigger for exam block. 
  326: 
  327: Inputs: 1 - $parmcount - numericID of exam block in web form. 
  328: 
  329: Outputs: 1 - returns HTML for link to display contents of information item 
  330: 
  331: =item &show_timer_path()
  332: 
  333: Display hierarchy of names of folders/sub-folders containing the current
  334: item identified as an item with an interval timer set.
  335: 
  336: Inputs: 3
  337:     - $type - map or resource
  338: 
  339:     - $item - map URL or resource symb
  340: 
  341:     - $navmap - navmaps object
  342: 
  343: Outputs: 1 - HTML containing hierarchy of folders/subfolders (raquo entity separated).  
  344: 
  345: 
  346: =item &blocktype_text()
  347: 
  348: Inputs: None
  349: 
  350: Output: 2 
  351:      - $typeorder - reference to array of blockable communication/collaboration/content
  352: 
  353:      - $types -reference to hash of descriptions (localized) of blockable types.
  354:  
  355: 
  356: =item &blockingmenu_javascript()
  357: 
  358: Create Javascript used to launch pop-up used for content selection, and to
  359: toggle visibility of a number of expandable/collapsible divs.
  360: 
  361: Inputs: 1 - $blockcount - 
  362: 
  363: Output: 1 - Javascript (with <script></script> tags) for functions used to:
  364:             (a) launch pop-up window for selection of course content to which
  365:             access could be blocked. 
  366:             (b) toggle visibility of a number of divs:
  367: 
  368: =over 
  369: 
  370: =item *  for block type - defined dates or timer activated
  371: 
  372: =item *  for action to take -- add or modify block
  373: 
  374: =item *  for display of detailed information about intervals 
  375: 
  376: =back
  377: 
  378: 
  379: =back  
  380: 
  381: =cut
  382: 
  383: package Apache::lonblockingmenu;
  384: 
  385: use strict;
  386: use Apache::lonnet;
  387: use Apache::Constants qw(:common :http);
  388: use Apache::loncommon();
  389: use Apache::lonhtmlcommon();
  390: use Apache::lonparmset();
  391: use HTML::Entities();
  392: use Apache::lonlocal;
  393: use lib '/home/httpd/lib/perl/';
  394: use LONCAPA qw(:DEFAULT :match);
  395: 
  396: sub handler {
  397:     my $r=shift;
  398: 
  399: # ----------------------------------------------------------- Set document type
  400: 
  401:     &Apache::loncommon::content_type($r,'text/html');
  402:     $r->send_http_header;
  403: 
  404:     return OK if $r->header_only;
  405: 
  406:     #  Needs to be in a course
  407:     if (! ($env{'request.course.fn'})) {
  408:         # Not in a course
  409:         $env{'user.error.msg'}=
  410:      "/adm/setblock:dcm:0:0:Cannot set blocking of communications in a course";
  411:         return HTTP_NOT_ACCEPTABLE;
  412:     }
  413: 
  414: # ----------------------------------------------------------- Permissions check
  415: 
  416:     unless ((&Apache::lonnet::allowed('dcm',$env{'request.course.id'})) ||
  417:             (&Apache::lonnet::allowed('dcm',$env{'request.course.id'}.
  418:                                       '/'.$env{'request.course.sec'}))) {
  419:         $env{'user.error.msg'}=
  420:      "/adm/setblock:dcm:0:0:Cannot set blocking of communications in a course";
  421:         return HTTP_NOT_ACCEPTABLE;
  422:     }
  423: 
  424: # -----------------------------Get action and calling context from query string
  425: 
  426:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  427:                                             ['action','caller','block']);
  428: 
  429:     my $crstype = &Apache::loncommon::course_type();
  430:     my $action = $env{'form.action'};
  431:     my %records = ();
  432:     my $blockcount = 0;
  433: 
  434: # ------------------------------------------------------ Retrieve current blocks
  435:     $blockcount = &get_blockdates(\%records);
  436: 
  437: # -------------------- Generate display for pop-up of Maps and Resources blocked   
  438:     if ($action eq 'showdocs') {
  439:         my ($navmap,$errormsg) = 
  440:             &Apache::loncourserespicker::get_navmap_object($crstype,'examblock');
  441:         if (ref($navmap)) {
  442:             my (%blockedmaps,%blockedresources);
  443:             if ($env{'form.block'} =~ /^\d+$/) {
  444:                 my @currblocks = sort(keys(%records));
  445:                 my $block = $currblocks[$env{'form.block'}];
  446:                 if (($block ne '') && (ref($records{$block}) eq 'HASH')) {
  447:                     if (ref($records{$block}{'blocks'}) eq 'HASH') {
  448:                         if (ref($records{$block}{'blocks'}{'docs'}) eq 'HASH') {
  449:                             if (ref($records{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
  450:                                 %blockedmaps = %{$records{$block}{'blocks'}{'docs'}{'maps'}};
  451:                             }
  452:                             if (ref($records{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
  453:                                 %blockedresources = %{$records{$block}{'blocks'}{'docs'}{'resources'}};
  454:                             }
  455:                         }
  456:                     }
  457:                 }
  458:             }
  459:             $r->print(&Apache::loncourserespicker::create_picker($navmap,
  460:                                      'examblock','resourceblocks',$crstype,
  461:                                      \%blockedmaps,\%blockedresources,
  462:                                      $env{'form.block'}));
  463:         } else {
  464:             $r->print($errormsg);
  465:         }
  466:         return OK;
  467:     }
  468: 
  469: # -------------------------- Store changes and retrieve latest block information
  470:     my $storeresult;
  471:     if ($env{'form.action'} eq 'store') {
  472:         (my $numchanges,$storeresult) = &blockstore($crstype,$blockcount);
  473:         if ($numchanges > 0) {
  474:             $blockcount = &get_blockdates(\%records);
  475:         }
  476:     }
  477: 
  478: # ------------------------------------------------------------------ Breadcrumbs
  479:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  480:     if ($env{'form.caller'} eq 'email') {  
  481:         &Apache::lonhtmlcommon::add_breadcrumb
  482:             ({href=>'/adm/communicate',
  483:               text=>'Communication/Messages',
  484:               faq=>12,bug=>'Communication Tools',});
  485:     } else {
  486:         &Apache::lonhtmlcommon::add_breadcrumb
  487:             ({href=>'/adm/parmset',
  488:               text=>'Content and Problem Settings'});
  489:     }
  490:     &Apache::lonhtmlcommon::add_breadcrumb
  491:         ({href=>'/adm/setblock',
  492:           text=>'Blocking communication/content access'});
  493: 
  494:     my $js = &blockingmenu_javascript($blockcount);
  495: 
  496:     $r->print(
  497:         &Apache::loncommon::start_page('Blocking communication/content access',$js).
  498:         &Apache::lonhtmlcommon::breadcrumbs('Blocking communication/content access'));
  499: 
  500:     my $usertype;
  501:     if ($crstype eq 'Community') {
  502:         $usertype = 'members';
  503:     } else {
  504:         $usertype = 'students';
  505:     }
  506:     my $lctype = lc($crstype);
  507:     my %lt=&Apache::lonlocal::texthash (
  508:             'cbds' => 'Blocking communication and/or content access during exams',
  509:             'prev' => "For the duration of an exam, or a timed quiz, students in this course can be prevented from:",
  510:             'blca' => "Blocks can potentially interrupt legitimate communication between $usertype who are also both enrolled in a different LON-CAPA $lctype.",
  511:             'pobl' => "Portfolio blocking can impact a student's ability to complete assigments in courses besides your own. Please use this feature wisely.",
  512:             'actt' => "Action to take:",
  513:             'addn' => 'Add new blocking event',
  514:             'mexb' => 'Modify existing blocking event(s)', 
  515:             'ncbc' => 'There are no blocking events currently saved.',
  516:             'stor' => 'Save',
  517:     );
  518: 
  519:     my %ltext = &Apache::lonlocal::texthash(
  520:             'type' => 'Type',
  521:             'defs' => 'Defined Start/End',
  522:             'trig' => 'Triggered by Activating Timer', 
  523:             'setb' => 'Set by',
  524:             'even' => 'Event',
  525:             'blck' => 'Blocked?',
  526:             'star' => 'Start',
  527:             'endd' => 'End',
  528:             'chda' => 'Choose dates',
  529:             'chtr' => 'Choose trigger',
  530:             'when' => 'When using defined start/end times for an event, please set dates carefully.',
  531:             'yes'  => 'Yes',
  532:             'no'   => 'No',
  533:     );
  534: 
  535:     $r->print('<h3>'.$lt{'cbds'}.'</h3>');
  536: 
  537: # ---------------------------------------------------- Get Time Limit parameters
  538:     my %intervals = &get_timed_items();
  539: 
  540: # -------------------------------------------- Display information about changes 
  541:     if ($env{'form.action'} eq 'store') {
  542:         $r->print($storeresult);
  543:     } else {
  544:         $r->print(
  545:             $lt{'prev'}.
  546:             '<ul>'."\n".
  547:             '<li>'.&mt("displaying LON-CAPA messages sent by other $usertype in the $lctype").'</li>'."\n".
  548:             '<li>'.&mt("displaying or posting to LON-CAPA discussion boards or live chat in the $lctype").'</li>'."\n".
  549:             '<li>'.&mt('accessing content in LON-CAPA portfolios or blogs').'</li>'."\n".
  550:             '<li>'.&mt("generating printouts of $lctype content").'</li>'.
  551:             '<li>'.&mt("accessing $lctype content in specified folders or resources").'</li>'.
  552:             '</ul>'.
  553:             '<p class="LC_warning">'.$lt{'blca'}.'<br />'.$lt{'pobl'}.'</p>'
  554:         );
  555:     }
  556: 
  557: # ------------------------ Choose between modifying existing block or adding new
  558:     $r->print('<form name="blockform" method="post" action="/adm/setblock?action=store">');
  559:     if ($blockcount > 0) {
  560:          $r->print(<<"END");
  561: <div class="LC_left_float">
  562: <fieldset><legend>$lt{'actt'}</legend>
  563: <span class="LC_nobreak">
  564: <label><input type="radio" name="blockaction" value="modify" id="modifyaction" 
  565: onclick="toggleAddModify();" checked="checked" />$lt{'mexb'}</label>
  566: </span>
  567: <br />
  568: <span class="LC_nobreak">
  569: <label><input type="radio" name="blockaction" value="add" id="addaction" 
  570: onclick="toggleAddModify();" />$lt{'addn'}</label>
  571: </span>
  572: </fieldset>
  573: </div>
  574: <br clear="all" />
  575: <div id="showadd" style="display:none">
  576: END
  577:     } else {
  578:         $r->print($lt{'ncbc'}.'<br /><br />'.
  579:                   '<h4>'.$lt{'addn'}.'</h4>'.
  580:                   '<input type="hidden" name="blockaction" value="add" />');
  581:     }
  582:     my ($navmap,$errormsg) =
  583:         &Apache::loncourserespicker::get_navmap_object($crstype,'examblock');
  584: 
  585: # --------------------------------------------- Interface for adding a new block
  586:     &display_addblocker_table($r,$blockcount,\%ltext,\%intervals,
  587:                               $navmap,$errormsg);
  588: 
  589: # ------------------------------------------------- Interface for existig blocks
  590:     if ($blockcount > 0) {
  591:         $r->print('</div>');
  592:         &display_blocker_status($r,\%records,\%ltext,\%intervals,
  593:                                 $navmap,$errormsg,$blockcount);
  594:     }
  595:     $r->print(<<"END");
  596: <br />
  597: <input type ="submit" value="$lt{'stor'}" />
  598: </form>
  599: END
  600:     $r->print(&Apache::loncommon::end_page());
  601:     return OK;
  602: }
  603: 
  604: sub get_timed_items {
  605:     my ($cdom,$cnum) = @_;
  606:     my ($cid,%intervals);
  607:     if ($cdom eq '' || $cnum eq '') {
  608:         $cid = $env{'request.course.id'};
  609:         $cdom = $env{'course.'.$cid.'.domain'};
  610:         $cnum = $env{'course.'.$cid.'.num'};
  611:     } else {
  612:         $cid = $cdom.'_'.$cnum;
  613:     }
  614:     if ($cid eq '') {
  615:         return %intervals;
  616:     }
  617:     my $resourcedata=&Apache::lonparmset::readdata($cnum,$cdom);
  618:     if (ref($resourcedata) eq 'HASH') {
  619:         foreach my $key (keys(%{$resourcedata})) {
  620:             if ($key =~ /^\Q$cid\E(.+)\.0\.interval$/) {
  621:                 my $middle = $1;
  622:                 if ($middle eq '') {
  623:                     $intervals{'course'}{'all'} = $resourcedata->{$key};
  624:                 } elsif ($middle =~ /^\.\[(\w+)\]$/) {
  625:                     $intervals{'course'}{'secgrp'}{$1} = $resourcedata->{$key};
  626:                 } elsif ($middle =~ /^\.\[useropt\:($match_username\:$match_domain)\]$/) {
  627:                     $intervals{'course'}{'users'}{$1} = $resourcedata->{$key};
  628:                 } elsif ($middle =~ /^\.(.+)\Q___(all)\E$/) {
  629:                     my $inner = $1;
  630:                     if ($inner =~ /^\[(\w+)\]\.([^\]]+)$/) {
  631:                         $intervals{'map'}{$2}{'secgrp'}{$1} = $resourcedata->{$key};
  632:                     } elsif ($inner =~ /^\[useropt\:($match_username\:$match_domain)\]\.([^\]]+)$/) {
  633:                         $intervals{'map'}{$2}{'users'}{$1} = $resourcedata->{$key};
  634:                     } else {
  635:                         $intervals{'map'}{$inner}{'all'} = $resourcedata->{$key};
  636:                     }
  637:                 } elsif ($middle =~ /^\.\[(\w+)\]\.([^\]]+)$/) {
  638:                     $intervals{'resource'}{$2}{'secgrp'}{$1} = $resourcedata->{$key}; 
  639:                 } elsif ($middle =~ /^\.\[useropt\:($match_username\:$match_domain)\]\.([^\]]+)$/) {
  640:                     $intervals{'resource'}{$2}{'users'}{$1} = $resourcedata->{$key};
  641:                 } else {
  642:                     my ($symb) = ($middle =~ /^\.(.+)$/);
  643:                     $intervals{'resource'}{$symb}{'all'} = $resourcedata->{$key};
  644:                 }
  645:             }
  646:         }
  647:     }
  648:     return %intervals;
  649: }
  650: 
  651: sub blockstore {
  652:     my ($crstype,$blockcount) = @_;
  653:     my %lt=&Apache::lonlocal::texthash(
  654:             'tfcm' => 'The following changes were made',
  655:             'ncwm' => 'No changes were made.',
  656:             'unna' => 'Unable to retrieve contents of course.', 
  657:     );
  658:     my %adds = ();
  659:     my %removals = ();
  660:     my %cancels = ();
  661:     my $modtotal = 0;
  662:     my $canceltotal = 0;
  663:     my $addtotal = 0;
  664:     my $changestotal = 0;
  665:     my $addtimer = 0;
  666:     my %blocking = ();
  667:     my (%map_url,%resource_symb,$output);
  668:     $output = '<h3>'.$lt{'head'}.'</h3>';
  669:     if ($env{'form.blockaction'} eq 'modify') {
  670:         foreach my $envkey (keys(%env)) {
  671:             if ($envkey =~ m/^form\.action_(\d+)$/) {
  672:                 if ($env{$envkey} eq 'modify') {
  673:                     $adds{$1} = 1;
  674:                     $removals{$1} = 1;
  675:                 } elsif ($env{$envkey} eq 'cancel') {
  676:                     $cancels{$1} = $1;
  677:                     unless ( defined($removals{$1}) ) {
  678:                         $removals{$1} = 1;
  679:                         $canceltotal ++;
  680:                     }
  681:                 }
  682:             }
  683:         }
  684:     } elsif ($env{'form.blockaction'} eq 'add') {
  685:         $adds{$blockcount} = 1;
  686:     }
  687:     my ($navmap,$errormsg) =
  688:         &Apache::loncourserespicker::get_navmap_object($crstype,'examblock');
  689:     unless (ref($navmap)) {
  690:         $output = $lt{'unna'}.' '.$lt{'ncwm'}.'</br>';
  691:         return ($changestotal,$output);
  692:     }
  693:     &enumerate_course_contents($navmap,\%map_url,\%resource_symb);
  694:     foreach my $key (keys(%removals)) {
  695:         my $hashkey = $env{'form.key_'.$key};
  696:         &Apache::lonnet::del('comm_block',["$hashkey"],
  697:                          $env{'course.'.$env{'request.course.id'}.'.domain'},
  698:                          $env{'course.'.$env{'request.course.id'}.'.num'}
  699:                          );
  700:     }
  701:     foreach my $key (keys(%adds)) {
  702:         unless ( defined($cancels{$key}) ) {
  703:             my $newkey;
  704:             if ($env{'form.firstaccess_'.$key}) {
  705:                 my $interval = 
  706:                     &HTML::Entities::decode($env{'form.firstaccess_'.$key});
  707:                 if ($interval ne '') {
  708:                     if ($interval eq 'course') {
  709:                         $newkey = 'firstaccess____'.$interval;
  710:                     } elsif ($interval =~ /___\d+___/) {
  711:                         my ($map,$resid,$url) = 
  712:                             &Apache::lonnet::decode_symb($interval);
  713:                         if (&Apache::lonnet::is_on_map($url)) {
  714:                             $newkey = 'firstaccess____'.$interval;
  715:                         }
  716:                     } elsif (&Apache::lonnet::is_on_map($interval)) {
  717:                         $newkey = 'firstaccess____'.$interval;
  718:                     }
  719:                     if ($newkey ne '') {
  720:                         unless (defined($removals{$key})) {
  721:                             $addtimer ++;
  722:                         }
  723:                     }
  724:                 }
  725:             } else {
  726:                 my ($newstart,$newend) = &get_dates_from_form($key);
  727:                 $newkey = $newstart.'____'.$newend;
  728:             }
  729:             if ($newkey ne '') {
  730:                 my ($blocktypes,$blockdocs) = 
  731:                     &get_block_choices($key,\%map_url,\%resource_symb);
  732:                 $blocking{$newkey} = {
  733:                           setter => $env{'user.name'}.':'.$env{'user.domain'},
  734:                           event  => &escape($env{'form.title_'.$key}),
  735:                           blocks => $blocktypes,
  736:                         };
  737:                 if ($blockdocs) {
  738:                     &check_release_required('docs');
  739:                 }
  740:                 if (ref($blocktypes) eq 'HASH') {
  741:                     if ($blocktypes->{'printout'} eq 'on') {
  742:                         &check_release_required('printout');
  743:                     }
  744:                 }
  745:                 if (exists($removals{$key})) {
  746:                     $modtotal ++;
  747:                 } else {
  748:                     $addtotal ++;
  749:                 }
  750:             } else {
  751:                 if ($env{'form.toggle_'.$key} eq 'timer') {
  752:                     $output .= '<p class="LC_warning">'.
  753:                                &mt('Invalid trigger for new blocking event').
  754:                                '</p>';
  755:                 } else {
  756:                     $output .= '<p class="LC_warning">'.
  757:                                &mt('No date range found for new blocking event').
  758:                                '</p>';
  759:                 }
  760:             }
  761:         }
  762:     }
  763:     if ($addtotal + $modtotal > 0) {
  764:         &Apache::lonnet::put('comm_block',\%blocking,
  765:                      $env{'course.'.$env{'request.course.id'}.'.domain'},
  766:                      $env{'course.'.$env{'request.course.id'}.'.num'}
  767:                      );
  768:         if ($addtimer) {
  769:             &check_release_required('timer');
  770:         }
  771:     }
  772:     $changestotal = $canceltotal + $modtotal + $addtotal;
  773:     if ($changestotal > 0) {
  774:         &Apache::lonnet::devalidate_cache_new('comm_block',
  775:                                               $env{'request.course.id'});
  776:         $output .= $lt{'tfcm'}.'<ul>';
  777:         if ($canceltotal > 0) {
  778:             $output .= '<li>'.
  779:                        &mt('[quant,_1,blocking event was,blocking events were] removed.',
  780:                            $canceltotal).
  781:                        '</li>';
  782:         }
  783:         if ($modtotal > 0) {
  784:             $output .= '<li>'.
  785:                        &mt('[quant,_1,blocking event was,blocking events were] modified.',
  786:                            $modtotal).
  787:                        '</li>';
  788:         }
  789:         if ($addtotal > 0) {
  790:             $output .= '<li>'.
  791:                        &mt('[quant,_1,blocking event was,blocking events were] added.',
  792:                            $addtotal).
  793:                        '</li>';
  794:         }
  795:         $output .= '</ul>';
  796:     } else {
  797:         $output .= $lt{'ncwm'};
  798:     }
  799:     $output .= '<br />';
  800:     return ($changestotal,$output);
  801: }
  802: 
  803: sub enumerate_course_contents {
  804:     my ($navmap,$map_url,$resource_symb) = @_;
  805:     if ((ref($navmap)) && (ref($map_url) eq 'HASH') && 
  806:         (ref($resource_symb) eq 'HASH')) {
  807:         my $it = $navmap->getIterator(undef,undef,undef,1,undef,undef);
  808:         my $count = 0;
  809:         while (my $curRes = $it->next()) {
  810:             if (ref($curRes)) {
  811:                 $count ++;
  812:                 my $symb = $curRes->symb();
  813:                 my $ressymb = $symb;
  814:                 if ($ressymb =~ m|adm/($match_domain)/($match_username)/(\d+)/bulletinboard$|) {
  815:                     unless ($ressymb =~ m|adm/wrapper/adm|) {
  816:                         $ressymb = 'bulletin___'.$3.'___adm/wrapper/adm/'.$1.'/'.$2.'/'.$3.
  817:                                    '/bulletinboard';
  818:                     }
  819:                 }
  820:                 if (($curRes->is_sequence()) || ($curRes->is_page())) {
  821:                     $map_url->{$count} = (&Apache::lonnet::decode_symb($symb))[2];
  822:                 } else {
  823:                     $resource_symb->{$count} = $ressymb;
  824:                 }
  825:             }
  826:         }
  827:     }
  828:     return;
  829: }
  830: 
  831: sub get_dates_from_form {
  832:     my $item = shift;
  833:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$item);
  834:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$item);
  835:     return ($startdate,$enddate);
  836: }
  837: 
  838: sub get_blockdates {
  839:     my ($records) = @_;
  840:     my $blockcount = 0;
  841:     %{$records} = &Apache::lonnet::dump('comm_block',
  842:                          $env{'course.'.$env{'request.course.id'}.'.domain'},
  843:                          $env{'course.'.$env{'request.course.id'}.'.num'}
  844:                          );
  845:     $blockcount = keys(%{$records});
  846: 
  847:     if ((keys(%{$records}))[0] =~ /^error: 2 /) {
  848:         $blockcount = 0;
  849:     }
  850:     return $blockcount;
  851: }
  852: 
  853: sub get_block_choices {
  854:     my ($item,$map_ref,$symb_ref) = @_;
  855:     my $blocklist;
  856:     my $blockdocs;
  857:     my ($typeorder,$types) = &blocktype_text();
  858:     foreach my $type (@{$typeorder}) {
  859:         if ($type eq 'docs') {
  860:             if ($env{'form.'.$type.'_'.$item}) {
  861:                 $blocklist->{$type} = {};
  862:                 if ($env{'form.docs_resources_'.$item}) {
  863:                     $env{'form.docs_resources_'.$item} =~ s/,$//;
  864:                     if (ref($symb_ref) eq 'HASH') {
  865:                         my %resources = map { $symb_ref->{$_} => 1; } 
  866:                                             (split(/,/,$env{'form.docs_resources_'.$item}));
  867:                         $blocklist->{$type}->{resources} = \%resources;
  868:                         if (keys(%resources) > 0) {
  869:                             $blockdocs = 1;
  870:                         }
  871:                     }
  872:                 }
  873:                 if ($env{'form.docs_maps_'.$item}) {
  874:                     $env{'form.docs_maps_'.$item} =~ s/,$//;
  875:                     if (ref($map_ref) eq 'HASH') {
  876:                         my %maps = map { $map_ref->{$_} => 1; }                             
  877:                                        (split(/,/,$env{'form.docs_maps_'.$item}));
  878:                         $blocklist->{$type}->{maps} = \%maps;
  879:                         if (keys(%maps) > 0) {
  880:                             $blockdocs = 1;
  881:                         }
  882:                     }
  883:                 }
  884:             }
  885:         } else {
  886:             if ($env{'form.'.$type.'_'.$item}) {
  887:                 $blocklist->{$type} = 'on';
  888:             } else {
  889:                 $blocklist->{$type} = 'off';
  890:             }
  891:         }
  892:     }
  893:     return ($blocklist,$blockdocs);
  894: }
  895: 
  896: sub check_release_required {
  897:     my ($value) = @_; 
  898:     my $needsrelease = $Apache::lonnet::needsrelease{'course:commblock:'.$value};
  899:     if ($needsrelease) {
  900:         my $curr_required = 
  901:             $env{'course.'.$env{'request.course.id'}.'.internal.releaserequired'};
  902:         if ($curr_required eq '') {
  903:             &Apache::lonnet::update_released_required($needsrelease);
  904:         } else {
  905:             my ($currmajor,$currminor) = split(/\./,$curr_required);
  906:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
  907:             if (($currmajor < $needsmajor) || 
  908:                 ($currmajor == $needsmajor && $currminor < $needsminor)) {
  909:                 &Apache::lonnet::update_released_required($needsrelease);
  910:             }
  911:         }
  912:     }
  913:     return;
  914: }
  915: 
  916: sub display_blocker_status {
  917:     my ($r,$records,$ltext,$intervals,$navmap,$errormsg,$blockcount) = @_;
  918:     my $parmcount = 0;
  919:     my (%map_url,%resource_symb,%lookups);
  920:     &enumerate_course_contents($navmap,\%map_url,\%resource_symb);
  921:     %{$lookups{'maps'}} = reverse(%map_url);
  922:     %{$lookups{'resources'}} = reverse(%resource_symb);
  923:     my %lt = &Apache::lonlocal::texthash(
  924:         'modi' => 'Modify',
  925:         'dele' => 'Delete',
  926:         'noch' => 'No change',
  927:     );
  928:     $r->print('<div id="showmodify" style="display:block">'.
  929:               &Apache::loncommon::start_data_table());
  930:     $r->print(<<"END");
  931:   <tr>
  932:     <th></th>
  933:     <th>$ltext->{'type'}</th>
  934:     <th>$ltext->{'even'}</th>
  935:     <th>$ltext->{'blck'}</th>
  936:   </tr>
  937: END
  938:     foreach my $record (sort(keys(%{$records}))) {
  939:         my $jschg = 
  940:             'javascript:window.document.forms['. "'blockform'".']'.
  941:             '.elements['."'action_$parmcount'".'][0].checked=true;';
  942:         my $onchange = 'onfocus="'.$jschg.'"';
  943:         my ($setuname,$setudom,$title,$blocks) =
  944:             &Apache::loncommon::parse_block_record($$records{$record});
  945:         $title = &HTML::Entities::encode($title,'"<>&');
  946:         my $blockid = &HTML::Entities::encode($record,'"<>&');
  947:         my $settername =
  948:            &Apache::loncommon::aboutmewrapper(
  949:                            &Apache::loncommon::plainname($setuname,$setudom),
  950:                            $setuname,$setudom);
  951:         $r->print(&Apache::loncommon::start_data_table_row());
  952:         $r->print(<<"ACT");
  953: 
  954:         <td valign="middle"><span class="LC_nobreak"><label>
  955:         <input type="radio" name="action_$parmcount" value="modify" />$lt{'modi'}
  956:         </label></span><br />
  957:         <span class="LC_nobreak"><label>
  958:         <input type="radio" name="action_$parmcount" value="cancel" />$lt{'dele'}
  959:         </label></span><br />
  960:         <span class="LC_nobreak"><label>
  961:         <input type="radio" name="action_$parmcount" id="nochange_$parmcount" 
  962:          value="nochange" checked="checked" />$lt{'noch'}
  963:         </label></span>
  964:         </td>
  965: ACT
  966:         my ($start,$end,$startform,$endform); 
  967:         if ($record =~ /^(\d+)____(\d+)$/) {
  968:             ($start,$end) = split(/____/,$record);
  969:             $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.
  970:                                                              $parmcount,$start,$onchange);
  971:             $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.
  972:                                                            $parmcount,$end,$onchange);
  973:             $r->print('<td><fieldset><legend>'.$ltext->{'defs'}.'</legend>'.
  974:                       $ltext->{'star'}.':&nbsp;'.$startform.'<br />'.
  975:                       $ltext->{'endd'}.':&nbsp;&nbsp;'.$endform.'</fieldset></td>');
  976:         } elsif ($record =~ /^firstaccess____(.+)$/) {
  977:             my $item = $1;
  978:             my ($itemname,$iteminfo,$skipdetails);
  979:             my $type = 'map';
  980:             my $url;
  981:             if ($item eq 'course') {
  982:                 $type = 'course';
  983:             } elsif ($item =~ /___\d+___/) {
  984:                 $type = 'resource';
  985:                 (my $map, my $resid, $url) = &Apache::lonnet::decode_symb($item);  
  986:             } else {
  987:                 $url = $item;
  988:             }
  989:             $r->print('<td><fieldset><legend>'.$ltext->{'trig'}.'</legend>');
  990:             if ($type eq 'course') {
  991:                 $itemname = &mt('Timer for all items in course.');
  992:             } else {
  993:                 if (&Apache::lonnet::is_on_map($url)) { 
  994:                     if ($type eq 'map') {
  995:                         if (ref($navmap)) {
  996:                             my $res = $navmap->getResourceByUrl($item);
  997:                             my $title = $res->compTitle();
  998:                             $itemname = &mt('Timer for all items in folder: [_1]',
  999:                                             '<span style="font-style:italic">'.
 1000:                                             $title.'</span>');
 1001:                         }
 1002:                     } else {
 1003:                         if (ref($navmap)) {
 1004:                             my $res = $navmap->getBySymb($item);
 1005:                             my $title = $res->compTitle();
 1006:                             $itemname = &mt('Timer for resource: [_1]',
 1007:                                              '<span style="font-style:italic">'.
 1008:                                              $title.'</span>');
 1009:                         }
 1010:                     }
 1011:                     if (ref($navmap)) {
 1012:                         my $path = &show_timer_path($type,$item);
 1013:                         if ($path) {
 1014:                             $iteminfo  = ' <span style="font-size:90%;">'.
 1015:                                          &mt('(in: [_1])',$path).
 1016:                                          '</span>';
 1017:                         }
 1018:                     }
 1019:                 } else {
 1020:                     $skipdetails = 1;
 1021:                     $itemname = '<span style="LC_warning">'.
 1022:                                 &mt('Timer folder/resource not in course').
 1023:                                 '</span>';  
 1024:                 }
 1025:             }
 1026:             if ((!$skipdetails) && (ref($intervals) eq 'HASH')) {
 1027:                 if (ref($intervals->{$type}) eq 'HASH') {
 1028:                     $iteminfo .= &trigger_details_toggle($parmcount).
 1029:                                 '<ul id="trigdetails_'.$parmcount.'" style="display:none">';
 1030:                     if ($type eq 'course') {
 1031:                         foreach my $scope (keys(%{$intervals->{$type}})) {
 1032:                             if ($scope eq 'all') {
 1033:                                 $iteminfo .= '<li>'.&mt('All users -- time limit: [_1]',
 1034:                                          &convlim($intervals->{$type}->{$scope})).'</li>';
 1035:                             } elsif ($scope eq 'secgrp') {
 1036:                                 if (ref($intervals->{$type}->{$scope}) eq 'HASH') {
 1037:                                     $iteminfo .= '<li>'.&mt('Sections/groups').'<ul>';
 1038:                                     foreach my $item (sort(keys(%{$intervals->{$type}->{$scope}}))) {
 1039:                                         $iteminfo .= '<li>'.&mt('[_1] -- time limit: [_2]',$item,
 1040:                                                      &convlim($intervals->{$type}->{$scope}->{$item})).
 1041:                                                      '</li>';
 1042:                                     }
 1043:                                     $iteminfo .= '</ul></li>';
 1044:                                 }
 1045:                             } elsif ($scope eq 'users') {
 1046:                                 if (ref($intervals->{$type}->{$scope}) eq 'HASH') {
 1047:                                     $iteminfo .= '<li>'.&mt('Users').'<ul>'; 
 1048:                                     foreach my $item (sort(keys(%{$intervals->{$type}->{$scope}}))) {
 1049:                                         $iteminfo .= '<li>'.&mt('[_1] -- time limit: [_2]',
 1050:                                                      &convlim($item,$intervals->{$type}->{$scope}->{$item})).
 1051:                                                      '</li>';
 1052:                                     }
 1053:                                     $iteminfo .= '</ul></li>';
 1054:                                 }
 1055:                             }
 1056:                         }
 1057:                     } elsif (($type eq 'map') || ($type eq 'resource')) {
 1058:                         if (ref($intervals->{$type}->{$item}) eq 'HASH') { 
 1059:                             foreach my $scope (keys(%{$intervals->{$type}->{$item}})) {
 1060:                                 if ($scope eq 'all') {
 1061:                                     $iteminfo .= '<li>'.&mt('All users -- time limit: [_1]',
 1062:                                                   &convlim($intervals->{$type}->{$item}->{$scope})).
 1063:                                                   '</li>';
 1064:                                 } elsif ($scope eq 'secgrp') {
 1065:                                     if (ref($intervals->{$type}->{$item}->{$scope}) eq 'HASH') {
 1066:                                         $iteminfo .= '<li>'.&mt('Sections/groups').'<ul>';
 1067:                                         foreach my $sec (sort(keys(%{$intervals->{$type}->{$item}->{$scope}}))) {
 1068:                                             $iteminfo .= '<li>'.&mt('[_1] -- time limit: [_2]',$sec,
 1069:                                                          &convlim($intervals->{$type}->{$item}->{$scope}->{$sec})).
 1070:                                                          '</li>';
 1071:                                         }
 1072:                                         $iteminfo .= '</ul></li>'; 
 1073:                                     }
 1074:                                 } elsif ($scope eq 'users') {
 1075:                                     if (ref($intervals->{$type}->{$item}->{$scope}) eq 'HASH') {
 1076:                                         $iteminfo .= '<li>'.&mt('Users').'<ul>';
 1077:                                         foreach my $user (sort(keys(%{$intervals->{$type}->{$item}->{$scope}}))) {
 1078:                                             $iteminfo .= '<li>'.&mt('[_1] -- time limit: [_2]',$user,
 1079:                                                          &convlim($intervals->{$type}->{$item}->{$scope}->{$user})).
 1080:                                                          '</li>';
 1081:                                         }
 1082:                                         $iteminfo .= '</ul></li>';
 1083:                                     }
 1084:                                 }
 1085:                             }
 1086:                         }
 1087:                     }
 1088:                     $iteminfo .= '</ul>';
 1089:                 }
 1090:             }
 1091:             $r->print(&create_interval_form($intervals,$parmcount,$navmap,$item,$jschg,
 1092:                                             $itemname,$iteminfo).'</fieldset></td>');
 1093:         }
 1094:         $r->print(<<"END");
 1095:         <td>
 1096:          <input type="text" name="title_$parmcount" size="15" value="$title" onfocus="$jschg" />
 1097:          <input type="hidden" name="key_$parmcount" value="$blockid" />
 1098:          <br />
 1099:          <br />
 1100:          $ltext->{'setb'}: $settername
 1101:         </td>
 1102: END
 1103:         $r->print('<td>'.&blocker_checkboxes($parmcount,$blocks,$jschg,\%lookups).'</td>'.
 1104:                   &Apache::loncommon::end_data_table_row());
 1105:         $parmcount++;
 1106:     }
 1107:     $r->print(<<"END");
 1108: </table>
 1109: </div>
 1110: END
 1111:     return;
 1112: }
 1113: 
 1114: sub convlim {
 1115:     my ($timelimit) = @_;
 1116:     my @order = ('days','hours','minutes','seconds');
 1117:     my %catlimits = ( 
 1118:                       days    => 86400,
 1119:                       hours   => 3600,
 1120:                       minutes => 60,
 1121:                     );
 1122:     my @toshow;
 1123:     foreach my $cat (@order) {
 1124:         if ($cat eq 'seconds') {
 1125:             if ($timelimit > 0) {
 1126:                 push(@toshow,&mt("[_1] $cat",$timelimit));
 1127:             }
 1128:         } elsif ($timelimit >= $catlimits{$cat}) {
 1129:             my $val = int($timelimit/$catlimits{$cat});
 1130:             if ($val > 0) {
 1131:                 push(@toshow,&mt("[_1] $cat",$val));
 1132:             }
 1133:             $timelimit -= $val*$catlimits{$cat};
 1134:         }
 1135:     }
 1136:     my $output = join(', ',@toshow);
 1137:     return $output;
 1138: }
 1139: 
 1140: sub display_addblocker_table {
 1141:     my ($r,$parmcount,$ltext,$intervals,$navmap,$errormsg) = @_;
 1142:     return unless ((ref($ltext) eq 'HASH') && (ref($intervals) eq 'HASH'));
 1143:     my $start = time;
 1144:     my $end = $start + (60 * 60 * 2); #Default is an exam of 2 hours duration.
 1145:     my $onchange = 'onfocus="javascript:window.document.forms['.
 1146:                    "'blockform'].elements['addaction'].".
 1147:                    'checked=true;"';
 1148:     my $startform = &Apache::lonhtmlcommon::date_setter('blockform','startdate_'.
 1149:                                                         $parmcount,$start,$onchange);
 1150:     my $endform = &Apache::lonhtmlcommon::date_setter('blockform','enddate_'.
 1151:                                                       $parmcount,$end,$onchange);
 1152:     my %lt = &Apache::lonlocal::texthash(
 1153:         'exam' => 'e.g., Exam 1',
 1154:     );
 1155:     my $intervalform = &create_interval_form($intervals,$parmcount,$navmap);
 1156:     if ($intervalform ne '') {
 1157:         $intervalform = '<fieldset>'.
 1158:                         '<legend>'.$ltext->{'chtr'}.'</legend>'.
 1159:                         $intervalform.
 1160:                         '</fieldset>';
 1161:     }
 1162:     $r->print(&Apache::loncommon::start_data_table());
 1163:     $r->print(<<"END");
 1164:    <tr>
 1165:      <th>$ltext->{'type'}</th>
 1166:      <th>$ltext->{'even'} $lt{'exam'}</th>
 1167:      <th>$ltext->{'blck'}</th>
 1168:    </tr>
 1169: END
 1170:     $r->print(&Apache::loncommon::start_data_table_row().'<td>');
 1171:     $r->print(<<"END");
 1172:      <span class="LC_nobreak"><label><input type="radio" id="toggle_startstop" 
 1173:      name="toggle_$parmcount" value="startstop" onclick="showBlockType();" checked="checked" />
 1174:      $ltext->{'defs'}</label></span>&nbsp;&nbsp; 
 1175:      <span class="LC_nobreak"><label><input type="radio" id="toggle_timer" name="toggle_$parmcount" 
 1176:      value="timer" onclick="showBlockType();" />$ltext->{'trig'}</label></span><br />
 1177:      <div id="show_startstop" style="display:block">
 1178:      <fieldset><legend>$ltext->{'chda'}</legend>
 1179:      $ltext->{'star'}:&nbsp;$startform<br />$ltext->{'endd'}:&nbsp;&nbsp;$endform</fieldset>
 1180:      <span class="LC_warning">$ltext->{'when'}</span></div>
 1181:      <div id="show_timer" style="display:none">$intervalform</div>
 1182:      </td>
 1183:      <td><input type="text" name="title_$parmcount" size="15" value="" /></td>
 1184: END
 1185:     $r->print('<td>'.&blocker_checkboxes($parmcount).'</td>'.
 1186:               &Apache::loncommon::end_data_table_row().
 1187:               &Apache::loncommon::end_data_table()."\n".
 1188:               '<br />');
 1189:     return;
 1190: }
 1191: 
 1192: sub blocker_checkboxes {
 1193:     my ($parmcount,$blocks,$jschg,$lookups) = @_;
 1194:     my ($typeorder,$types) = &blocktype_text();
 1195:     my $numinrow = 2;
 1196:     my %currdocs;
 1197:     my $output = '<table>';
 1198:     for (my $i=0; $i<@{$typeorder}; $i++) {
 1199:         my $block = $typeorder->[$i];
 1200:         my ($clickaction,$blockstatus);
 1201:         if ($jschg) {
 1202:             $clickaction = $jschg;
 1203:         } 
 1204:         if ($block eq 'docs') {
 1205:             if ((ref($blocks) eq 'HASH') && (ref($lookups) eq 'HASH')) {
 1206:                 if (ref($blocks->{$block}) eq 'HASH') {
 1207:                     if (keys(%{$blocks->{$block}}) > 0) {
 1208:                         $blockstatus = 'checked="checked"';
 1209:                         foreach my $key (sort(keys(%{$blocks->{$block}}))) {
 1210:                             if (ref($blocks->{$block}{$key}) eq 'HASH') {
 1211:                                 my @current = ();
 1212:                                 foreach my $item (keys(%{$blocks->{$block}{$key}})) {
 1213:                                     if ($lookups->{$key}{$item}) {
 1214:                                         push(@current,$lookups->{$key}{$item});
 1215:                                     }
 1216:                                 }
 1217:                                 if (@current > 0) {
 1218:                                     @current=sort { $a <=> $b } (@current);
 1219:                                     $currdocs{$key} = join(',',@current);
 1220:                                 }
 1221:                             }
 1222:                         }
 1223:                     }
 1224:                 }
 1225:             }
 1226:             $clickaction .= 'javascript:resblockinfo('."'$parmcount'".');';
 1227:         } else {
 1228:             if (ref($blocks) eq 'HASH') { 
 1229:                 if ($blocks->{$block} eq 'on') {
 1230:                     $blockstatus = 'checked="checked"';
 1231:                 }
 1232:             }
 1233:         }
 1234:         my $rem = $i%($numinrow);
 1235:         if ($rem == 0) {
 1236:             if ($i > 0) {
 1237:                 $output .= '</tr>';
 1238:             }
 1239:             $output .= '<tr>';
 1240:         }
 1241:         if ($i == scalar(@{$typeorder})-1) {
 1242:             my $colsleft = $numinrow-$rem;
 1243:             if ($colsleft > 1) {
 1244:                 $output .= '<td colspan="'.$colsleft.'">';
 1245:             } else {
 1246:                 $output .= '<td>';
 1247:             }
 1248:         } else {
 1249:             $output .= '<td>';
 1250:         }
 1251:         my $item = $block.'_'.$parmcount;
 1252:         if ($clickaction) {
 1253:             $clickaction = ' onclick="'.$clickaction.'"';
 1254:         }
 1255:         if ($blockstatus) {
 1256:             $blockstatus = ' '.$blockstatus;
 1257:         } 
 1258:         $output .= '<span class="LC_nobreak"><label>'."\n".
 1259:                    '<input type="checkbox" id="'.$item.'" name="'.$item.'"'.
 1260:                    $blockstatus.$clickaction.' value="1" />'.
 1261:                    $types->{$block}.'</label></span>'."\n";
 1262:         if ($block eq 'docs') {
 1263:             if ($blockstatus ne '') {
 1264:                 $output .= '&nbsp;<a href="javascript:resblockinfo('."'$parmcount'".')">'.
 1265:                             &mt('Details').'</a>';
 1266:             }
 1267:         }
 1268:         $output .= '<br /></td>';
 1269:     }
 1270:     $output .= '</tr></table>'.
 1271:                '<input type="hidden" name="docs_maps_'.$parmcount.'"'.
 1272:                ' id="docs_maps_'.$parmcount.'" value="'.$currdocs{'maps'}.'" />'.
 1273:                '<input type="hidden" name="docs_resources_'.$parmcount.'"'.
 1274:                ' id="docs_resources_'.$parmcount.'" value="'.$currdocs{'resources'}.'" />';
 1275:     return $output;
 1276: }
 1277: 
 1278: sub create_interval_form {
 1279:     my ($intervals,$parmcount,$navmap,$currkey,$jschg,$itemname,$iteminfo) = @_;
 1280:     return unless ((ref($intervals) eq 'HASH') && (ref($navmap)));
 1281:     my $intervalform;
 1282:     if (keys(%{$intervals}) > 0) {
 1283:         foreach my $type (sort(keys(%{$intervals}))) {
 1284:             if ($type eq 'course') {
 1285:                 my ($checked,$clickaction);
 1286:                 if ($currkey eq 'course') {
 1287:                     $checked = ' checked="checked"';
 1288:                 } elsif ($jschg) {
 1289:                     $clickaction = ' onclick="'.$jschg.'"';
 1290:                 }
 1291:                 $intervalform .= '<label><input type="radio" name="firstaccess_'.$parmcount.
 1292:                                  '" value="course"'.$checked.$clickaction.' />';
 1293:                 if ($currkey eq 'course') {
 1294:                     $intervalform .= $itemname;
 1295:                 } else {
 1296:                     $intervalform .= &mt('Timer for all items in course');
 1297:                 }
 1298:                 $intervalform .= '</label>';
 1299:                 if ($currkey eq 'course') {
 1300:                     $intervalform .= $iteminfo;
 1301:                 }
 1302:                 $intervalform .= '<br />';
 1303:             } elsif ($type eq 'map') {
 1304:                 if (ref($intervals->{$type}) eq 'HASH') {
 1305:                     if (ref($navmap)) {
 1306:                         foreach my $map (sort(keys(%{$intervals->{$type}}))) {
 1307:                             my ($checked,$clickaction);
 1308:                             if ($currkey eq $map) {
 1309:                                 $checked = ' checked="checked"';
 1310:                             } elsif ($jschg) {
 1311:                                 $clickaction = ' onclick="'.$jschg.'"';
 1312:                             }
 1313:                             $intervalform .= '<label><input type="radio" name="firstaccess_'.$parmcount.
 1314:                                              '" value="'.&HTML::Entities::encode($map,'"<>&').'"'.
 1315:                                              $checked.$clickaction.' />';
 1316:                             if ($currkey eq $map) {
 1317:                                 $intervalform .= $itemname.'</label>';
 1318:                             } else {
 1319:                                 my $res = $navmap->getResourceByUrl($map);
 1320:                                 my $title = $res->compTitle();
 1321:                                 my $path;
 1322:                                 my $hierarchy = &show_timer_path($type,$map,$navmap);
 1323:                                 if ($hierarchy) {
 1324:                                     $path = ' <span style="font-size:90%;">'.
 1325:                                             &mt('(in: [_1])',$hierarchy).
 1326:                                             '</span>';
 1327:                                 }
 1328:                                 $intervalform .= &mt('Timer for all items in folder: [_1]',
 1329:                                                      '<i>'.$title.'</i>').
 1330:                                                  '</label>'.$path;
 1331:                             }
 1332:                             if ($currkey eq $map) {
 1333:                                 $intervalform .= $iteminfo;
 1334:                             }
 1335:                             $intervalform .= '<br />';
 1336:                         }
 1337:                     }
 1338:                 }
 1339:             } elsif ($type eq 'resource') {
 1340:                 if (ref($intervals->{$type}) eq 'HASH') {
 1341:                     if (ref($navmap)) {
 1342:                         foreach my $resource (sort(keys(%{$intervals->{$type}}))) {
 1343:                             my ($checked,$clickaction);
 1344:                             if ($currkey eq $resource) {
 1345:                                 $checked = ' checked="checked"';
 1346:                             } elsif ($jschg) {
 1347:                                 $clickaction = ' onclick="'.$jschg.'"';
 1348:                             }
 1349:                             $intervalform .= '<label><input type="radio" name="firstaccess_'.$parmcount.
 1350:                                              '" value="'.&HTML::Entities::encode($resource,'"<>&').'"'.
 1351:                                              $checked.$clickaction.' />';
 1352:                             if ($currkey eq $resource) {
 1353:                                 $intervalform .= $itemname.'</label>';
 1354:                             } else {
 1355:                                 my $res = $navmap->getBySymb($resource);
 1356:                                 my $title = $res->compTitle();
 1357:                                 my $path;
 1358:                                 my $hierarchy = &show_timer_path($type,$resource,$navmap);
 1359:                                 if ($hierarchy) {
 1360:                                     $path = ' <span style="font-size:90%;">'.
 1361:                                             &mt('(in: [_1])',$hierarchy).
 1362:                                             '</span>';
 1363:                                 }
 1364:                                 $intervalform .= &mt('Timer for resource: [_1]','<i>'.$title.'</i>').
 1365:                                                  '</label>'.
 1366:                                                  $path;
 1367:                             }
 1368:                             if ($currkey eq $resource) {
 1369:                                 $intervalform .= $iteminfo;
 1370:                             }
 1371:                             $intervalform .= '<br />';
 1372:                         }
 1373:                     }
 1374:                 }
 1375:             }
 1376:         }
 1377:     } else {
 1378:         if ($currkey ne '') {
 1379:             $intervalform = '<input type="radio" name="firstaccess_'.$parmcount.
 1380:                             '" checked="checked" value="'.
 1381:                             &HTML::Entities::encode($currkey,'"<>&').' />'.
 1382:                             $itemname.'<br />';
 1383:         } else {
 1384:             $intervalform = &mt('No timed items defined.').' '.
 1385:                             &mt('Use [_1]Settings[_2] to assign a timer, then return here.',
 1386:                                 '<a href="/adm/parmset">','</a>');
 1387:         }
 1388:     }
 1389:     return $intervalform;
 1390: }
 1391: 
 1392: sub trigger_details_toggle {
 1393:     my ($parmcount) = @_;
 1394:     return ' <span id="toggletext_'.$parmcount.'" class="LC_cusr_subheading LC_nobreak">'.
 1395:            '<a href="javascript:showTriggerDetails('."'$parmcount'".');" '.
 1396:            'style="text-decoration: none;"><b>'.&mt('(More ...)').'</b></a></span>';
 1397: }
 1398: 
 1399: sub show_timer_path {
 1400:     my ($type,$item,$navmap) = @_;
 1401:     return unless(ref($navmap));
 1402:     my @pathitems;
 1403:     if ($type eq 'map') {
 1404:         @pathitems = 
 1405:             &Apache::loncommon::get_folder_hierarchy($navmap,$item);
 1406:     } elsif ($type eq 'resource') {
 1407:         my ($map,$id,$resource) = &Apache::lonnet::decode_symb($item);
 1408:         @pathitems = 
 1409:             &Apache::loncommon::get_folder_hierarchy($navmap,$map,1);
 1410:     }
 1411:     if (@pathitems) {
 1412:         return join(' &raquo; ',@pathitems);
 1413:     }
 1414:     return;
 1415: }
 1416: 
 1417: sub blocktype_text {
 1418:     my %types = &Apache::lonlocal::texthash(
 1419:         'com' => 'Messaging',
 1420:         'chat' => 'Chat Room',
 1421:         'boards' => 'Discussion',
 1422:         'port' => 'Portfolio',
 1423:         'groups' => 'Groups',
 1424:         'blogs' => 'Blogs',
 1425:         'docs' => 'Content',
 1426:         'printout' => 'Printouts',
 1427:     );
 1428:     my $typeorder = ['com','chat','boards','port','groups','blogs','printout','docs'];
 1429:     return ($typeorder,\%types);
 1430: }
 1431: 
 1432: sub blockingmenu_javascript {
 1433:     my ($blockcount) = @_;
 1434:     my %lt = &Apache::lonlocal::texthash (
 1435:                                            more => 'More ...',
 1436:                                            less => 'Less ...',
 1437:                                          );
 1438:     return <<ENDSCRIPT;
 1439: <script type="text/javascript">
 1440: // <![CDATA[
 1441: function resblockinfo(blockid) {
 1442:     if (document.getElementById('docs_'+blockid).checked) {
 1443:         var resblockwin = null;
 1444:         var url = '/adm/setblock?action=showdocs&block='+blockid;
 1445:         if (!resblockwin || resblockwin.closed) {
 1446:             resblockwin=window.open(url,'blockingwin','height=480,width=600,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
 1447:         }
 1448:         resblockwin.focus();
 1449:     } else {
 1450:         document.getElementById('docs_resources_'+blockid).value = '';
 1451:         document.getElementById('docs_maps_'+blockid).value = '';
 1452:     }
 1453:     return;
 1454: }
 1455: 
 1456: function showBlockType() {
 1457:     if (document.getElementById('toggle_startstop').checked == true) {
 1458:         document.getElementById('show_startstop').style.display='block';
 1459:     } else {
 1460:         document.getElementById('show_startstop').style.display='none';
 1461:     }
 1462:     if (document.getElementById('toggle_timer').checked == true) {
 1463:         document.getElementById('show_timer').style.display='block';
 1464:     } else {
 1465:         document.getElementById('show_timer').style.display='none';
 1466:     }
 1467:     return;
 1468: }
 1469: 
 1470: function toggleAddModify() {
 1471:     for (var i=0; i<document.blockform.blockaction.length; i++) {
 1472:         if (document.blockform.blockaction[i].checked) {
 1473:             if (document.blockform.blockaction[i].value == 'add') {
 1474:                document.getElementById('showadd').style.display='block';
 1475:                document.getElementById('showmodify').style.display='none';
 1476:                var blocktotal = $blockcount;
 1477:                if (blocktotal > 0) {
 1478:                    for (var i=0; i<blocktotal; i++) {
 1479:                        document.getElementById('nochange_'+i).checked = true;
 1480:                    }
 1481:                }
 1482:                document.getElementById('showmodify').style.display='none';
 1483:                document.getElementById('showadd').style.display='block';
 1484:             } else {
 1485:                document.getElementById('showadd').style.display='none';
 1486:                document.getElementById('showmodify').style.display='block';
 1487:             }
 1488:         }
 1489:     }
 1490:     return;
 1491: }
 1492: 
 1493: function showTriggerDetails(item) {
 1494:     document.getElementById('trigdetails_'+item).style.display='block';
 1495:     document.getElementById('trigdetails_'+item).style.textAlign='left';
 1496:     document.getElementById('trigdetails_'+item).style.textFace='normal';
 1497:     document.getElementById('toggletext_'+item).innerHTML = '<a href="javascript:hideTriggerDetails('+item+');" style="text-decoration: none;"><b>($lt{'less'})</b></a>';
 1498:     return;
 1499: }
 1500: 
 1501: function hideTriggerDetails(item) {
 1502:     document.getElementById('trigdetails_'+item).style.display='none';
 1503:     document.getElementById('toggletext_'+item).innerHTML = '<a href="javascript:showTriggerDetails('+item+');" style="text-decoration: none;"><b>($lt{'more'})</b></a>';
 1504:     return;
 1505: }
 1506: 
 1507: // ]]>
 1508: </script>
 1509: ENDSCRIPT
 1510: 
 1511: }
 1512: 
 1513: 1;
 1514: 
 1515: __END__

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