File:  [LON-CAPA] / loncom / interface / lonwishlist.pm
Revision 1.24: download - view: text, annotated - select for diffs
Sat Dec 20 15:35:40 2014 UTC (9 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_1, HEAD
- Accommodate single quotes in resource URL in "Stored Links" and in "Edit" link
- Accommodate single quotes in URLs in options in "Select Recent" dropdown list
  when browsing Resource Space.
- Consistent escaping of special characters for Stored Links added when displaying
  a resource in a course, and when browsing Resource Space.

    1: # The LearningOnline Network with CAPA
    2: # Utility-routines for wishlist
    3: #
    4: # $Id: lonwishlist.pm,v 1.24 2014/12/20 15:35:40 raeburn Exp $
    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: =pod
   30: 
   31: =head1 NAME
   32: 
   33: Apache::lonwishlist - Wishlist-Module
   34:   
   35: =head1 SYNOPSIS
   36: 
   37: The wishlist offers a possibility to store links to resources from the resource-pool and external websites in a hierarchical list.
   38: It is only available for user with access to the resource-pool. The list can be structured by folders.
   39: 
   40: The wishlist-module uses the CPAN-module "Tree" for easily handling the directory-structure of the wishlist. Each node in the tree has an index to be referenced by.
   41: 
   42: =back
   43: 
   44: =cut
   45: 
   46: package Apache::lonwishlist;
   47: 
   48: use strict;
   49: use Apache::lonnet;
   50: use Apache::loncommon();
   51: use Apache::lonhtmlcommon;
   52: use Apache::lonlocal;
   53: use LONCAPA qw(:DEFAULT :match);
   54: use Tree;
   55: 
   56: 
   57: # Global variables
   58: my $root;
   59: my @childrenRt;
   60: my %TreeHash;
   61: my %TreeToHash;
   62: my @allFolders;
   63: my @allNodes;
   64: my $indentConst = 20;
   65: my $foldersOption;
   66: 
   67: =pod
   68: 
   69: =head2 Routines for getting and putting the wishlist data from and accordingly to users data.
   70: 
   71: =over 4
   72: 
   73: =item * &getWishlist()
   74: 
   75:      Get the wishlist-data via lonnet::getkeys() and lonnet::get() and returns the got data in a hash.
   76: 
   77: 
   78: =item * &putWishlist(wishlist)
   79: 
   80:      Parameter is a reference to a hash. Puts the wishlist-data contained in the given hash via lonnet::put() to user-data.
   81: 
   82: 
   83: =item * &deleteWishlist()
   84: 
   85:      Deletes all entries from the user-data for wishlist. Do this before putting in new data.
   86: 
   87: 
   88: =back
   89: 
   90: =cut
   91: 
   92: 
   93: # Read wishlist from user-data
   94: sub getWishlist {
   95:     my @keys = &Apache::lonnet::getkeys('wishlist');
   96:     my %wishlist = &Apache::lonnet::get('wishlist',\@keys);
   97:     foreach my $i (keys(%wishlist)) {
   98:         #File not found. This appears at the first time using the wishlist
   99:         #Create file and put 'root' into it
  100:        if ($i =~m/^\Qerror:No such file\E/) {
  101:            &Apache::lonnet::logthis($i.'! Create file by putting in the "root" of the directory tree.');
  102:            &Apache::lonnet::put('wishlist', {'root' => ''});
  103:            my $options = '<option value="" selected="selected">('.&mt('Top level').')</option>';
  104:            &Apache::lonnet::put('wishlist', {'folders' => $options});
  105:            @keys = &Apache::lonnet::getkeys('wishlist');
  106:            %wishlist = &Apache::lonnet::get('wishlist',\@keys);
  107:        }
  108:        elsif ($i =~ /^(con_lost|error|no_such_host)/i) {
  109:            &Apache::lonnet::logthis('ERROR while attempting to get wishlist: '.$i);
  110:            return 'error';
  111:        }
  112:     }
  113: 
  114:     # if we got no keys in hash returned by get(), return error.
  115:     # wishlist will not be loaded, instead the user will be asked to try again later
  116:     if ((keys(%wishlist)) == 0) {
  117:         &Apache::lonnet::logthis('ERROR while attempting to get wishlist: no keys retrieved!');
  118:         return 'error';
  119:     }
  120:     
  121:     return %wishlist;
  122: }
  123: 
  124: 
  125: # Write wishlist to user-data
  126: sub putWishlist {
  127:     my $wishlist = shift;
  128:     &Apache::lonnet::put('wishlist',$wishlist);
  129: }
  130: 
  131: 
  132: # Removes all existing entrys for wishlist in user-data
  133: sub deleteWishlist {
  134:     my @wishlistkeys = &Apache::lonnet::getkeys('wishlist');
  135:     my %wishlist = &Apache::lonnet::del('wishlist',\@wishlistkeys);
  136: }
  137: 
  138: 
  139: =pod
  140: 
  141: =head2 Routines for changing the directory struture of the wishlist.
  142: 
  143: =over 4
  144: 
  145: =item * &newEntry(title, path, note)
  146: 
  147:      Creates a new entry in the wishlist containing the given informations. Additionally saves the date of creation in the entry.  
  148: 
  149: 
  150: =item * &deleteEntries(marked)
  151: 
  152:      Parameter is a reference to an array containing the indices of all nodes that should be removed from the tree. 
  153: 
  154: 
  155: =item * &sortEntries(indexNode, at)
  156: 
  157:      Changes the position of a node given by indexNode within its siblings. New position is given by at.
  158: 
  159: 
  160: =item * &moveEntries(indexNodesToMove, indexParent)
  161: 
  162:      Parameter is a reference to an array containing the indices of all nodes that should be moved. indexParent specifies the node that will become the new Parent for these nodes. 
  163: 
  164: 
  165: =item * &setNewTitle(nodeindex, newTitle)
  166: 
  167:      Sets the title for the node given by nodeindex to newTitle.
  168: 
  169: 
  170: =item * &setNewPath(nodeindex, newPath)
  171: 
  172:      Sets the path for the node given by nodeindex to newPath.
  173: 
  174: 
  175: =item * &setNewNote(nodeindex, newNote)
  176: 
  177:      Sets the note for the node given by nodeindex to newNote.     
  178: 
  179: 
  180: =item * &saveChanges()
  181: 
  182:      Prepares the wishlist-hash to save it via &putWishlist(wishlist).   
  183: 
  184: 
  185: =back
  186: 
  187: =cut
  188: 
  189: 
  190: # Create a new entry
  191: sub newEntry() {
  192:     my ($rootgiven, $title, $path, $note) = @_;
  193: 
  194:     $root = $rootgiven;
  195:     @childrenRt = $root->children();
  196: 
  197:     my $date = gmtime();
  198:     # Create Entry-Object
  199:     my $entry = Entry->new(title => $title, path => $path, note => $note, date => $date);
  200:     # Create Tree-Object, this corresponds a node in the wishlist-tree
  201:     my $tree = Tree->new($entry);
  202:     # Add this node to wishlist-tree
  203:     my $folderIndex = $env{'form.folders'};
  204:     if ($folderIndex ne '') {
  205:         @allFolders = ();
  206:         &getFoldersToArray(\@childrenRt);
  207:         my $folderToInsertOn = &Apache::Tree::getNodeByIndex($folderIndex,\@allFolders);
  208:         $folderToInsertOn->add_child($tree);
  209:     }
  210:     else {
  211:         $root->add_child($tree);
  212:     }
  213:     return &saveChanges();
  214: }
  215: 
  216: 
  217: # Delete entries
  218: sub deleteEntries {
  219:     my $rootgiven = shift;
  220:     my $marked = shift;
  221: 
  222:     $root = $rootgiven;
  223:     @childrenRt = $root->children();
  224: 
  225:     &getNodesToArray(\@childrenRt);
  226:     foreach my $m (@$marked) {
  227:         my $found = &Apache::Tree::getNodeByIndex($m, \@allNodes);
  228:         # be sure, that entry exists (may have been deleted before, e.g. in an other browsertab)
  229:         if (defined $found) {
  230:             &Apache::Tree::removeNode($found);
  231:         }
  232:     }
  233:     @allNodes = ();
  234:     return &saveChanges();
  235: }
  236: 
  237: 
  238: # Sort entries
  239: sub sortEntries {
  240:     my $rootgiven = shift;
  241:     my $indexNode = shift;
  242:     my $at = shift;
  243: 
  244:     $root = $rootgiven;
  245:     @childrenRt = $root->children();
  246:     
  247:     &getNodesToArray(\@childrenRt);
  248:     my $foundNode = &Apache::Tree::getNodeByIndex($indexNode, \@allNodes);
  249: 
  250:     &Apache::Tree::moveNode($foundNode,$at,undef);
  251:     @allNodes = ();
  252:     return &saveChanges();
  253: }
  254: 
  255: 
  256: # Move entries
  257: sub moveEntries {
  258:     my $rootgiven = shift;
  259:     my $indexNodesToMove = shift;
  260:     my $indexParent = shift;
  261:     my @nodesToMove = ();
  262: 
  263:     $root = $rootgiven;
  264:     @childrenRt = $root->children();
  265: 
  266:     # get all nodes that should be moved
  267:     &getNodesToArray(\@childrenRt);
  268:     foreach my $index (@$indexNodesToMove) {
  269:         my $foundNode = &Apache::Tree::getNodeByIndex($index, \@allNodes);
  270:         push(@nodesToMove, $foundNode);
  271:     }
  272: 
  273:     foreach my $node (@nodesToMove) {
  274:         my $foundParent;
  275:         my $parentIsIn = 0;
  276:         foreach my $n (@nodesToMove) {
  277:             if ($node->parent()->value() ne "root") {
  278:                if ($node->parent()->value()->nindex() == $n->value()->nindex()) {
  279:                     $parentIsIn = 1;
  280:                 }
  281:             }
  282:         }
  283:         if (!$parentIsIn) {
  284:             if ($indexParent ne "root") {
  285:                 $foundParent = &Apache::Tree::getNodeByIndex($indexParent, \@allNodes);
  286:                 &Apache::Tree::moveNode($node,undef,$foundParent);
  287:             }
  288:             else {
  289:                 &Apache::Tree::moveNode($node,undef,$root);
  290:             }
  291:         }
  292:     }
  293:     @allNodes = ();
  294:     return &saveChanges();
  295: }
  296: 
  297: 
  298: # Set a new title for an entry
  299: sub setNewTitle {
  300:     my ($rootgiven, $nodeindex, $newTitle) = @_;
  301: 
  302:     $root = $rootgiven;
  303:     @childrenRt = $root->children();
  304: 
  305:     &getNodesToArray(\@childrenRt);
  306:     my $found = &Apache::Tree::getNodeByIndex($nodeindex, \@allNodes);
  307:     $found->value()->title($newTitle); 
  308:     @allNodes = ();
  309:     return &saveChanges();
  310: }
  311: 
  312: 
  313: # Set a new path for an entry
  314: sub setNewPath {
  315:     my ($rootgiven, $nodeindex, $newPath) = @_;
  316: 
  317:     $root = $rootgiven;
  318:     @childrenRt = $root->children();
  319: 
  320:     &getNodesToArray(\@childrenRt);
  321:     my $found = &Apache::Tree::getNodeByIndex($nodeindex, \@allNodes);
  322:     if ($found->value()->path()) {
  323:         $found->value()->path($newPath); 
  324:         return &saveChanges();
  325:     }
  326:     @allNodes = ();
  327:     return 0;
  328: }
  329: 
  330: 
  331: # Set a new note for an entry
  332: sub setNewNote {
  333:     my ($rootgiven, $nodeindex, $newNote) = @_;
  334: 
  335:     $root = $rootgiven;
  336:     @childrenRt = $root->children();
  337: 
  338:     &getNodesToArray(\@childrenRt);
  339:     my $found = &Apache::Tree::getNodeByIndex($nodeindex, \@allNodes);
  340:     $found->value()->note($newNote); 
  341:     @allNodes = ();
  342:     return &saveChanges();
  343: }
  344: 
  345: 
  346: # Save all changes
  347: sub saveChanges {
  348:     @childrenRt = $root->children();
  349:     &Apache::Tree::TreeIndex(\@childrenRt);
  350:     &Apache::Tree::setCountZero();
  351:     &Apache::Tree::RootToHash(\@childrenRt);
  352:     &Apache::Tree::TreeToHash(\@childrenRt);
  353:     &deleteWishlist();
  354:     &putWishlist(\%TreeToHash);
  355:     return $root;
  356: 
  357: }
  358: 
  359: 
  360: =pod
  361: 
  362: =head2 Routines for handling the directory structure
  363: 
  364: =over 4
  365: 
  366: =item * &getFoldersForOption(nodes)
  367: 
  368:      Return the titles for all exiting folders in an option-tag, used to offer the users a possibility to create a new link or folder in an existing folder.
  369:      Recursive call starting with all children of the root of the tree (parameter nodes is reference to an array containing the nodes of the current level). 
  370: 
  371: 
  372: =item * &getFoldersToArray(children)
  373: 
  374:      Puts all nodes that represent folders in the wishlist into an array. 
  375:      Recursive call starting with all children of the root of the tree (parameter nodes is reference to an array containing the nodes of the current level).     
  376: 
  377: 
  378: =item * &getNodesToArray(children)
  379: 
  380:      Puts all existing nodes into an array (apart from the root node, because this one does not represent an entry in the wishlist).
  381:      Recursive call starting with all children of the root of the tree (parameter nodes is reference to an array containing the nodes of the current level).     
  382:  
  383: 
  384: =back
  385: 
  386: =cut
  387: 
  388: 
  389: # Return the names for all exiting folders in option-tags, so
  390: # a new link or a new folder can be created in an existing folder
  391: my $indent = 0;
  392: sub getFoldersForOption {
  393:     my $nodes = shift;
  394: 
  395:     foreach my $n (@$nodes) {
  396:         if ($n->value()->path() eq '') {
  397:             $foldersOption .= '<option value="'.$n->value()->nindex().'" style="margin-left:'.$indent.'px">'.
  398:                                    $n->value()->title().
  399:                                '</option>';
  400: 
  401:         my @children = $n->children();
  402:         if ($#children >=0) {
  403:             $indent += 10;
  404:             &getFoldersForOption(\@children);
  405:             $indent -= 10;
  406:             }
  407:         }
  408:     }
  409: }
  410: 
  411: 
  412: # Put all folder-nodes to an array
  413: sub getFoldersToArray {
  414:     my $children = shift;
  415:     foreach my $c (@$children) {
  416:         if ($c->value()->path() eq '') {
  417:             push(@allFolders,$c);
  418:         }
  419:         my @newchildren = $c->children();
  420:         if ($#newchildren >= 0) {
  421:             &getFoldersToArray(\@newchildren);
  422:         }
  423:     }
  424: }
  425: 
  426: 
  427: # Put all nodes to an array
  428: sub getNodesToArray {
  429:     my $children = shift;
  430:     foreach my $c (@$children) {
  431:         push(@allNodes,$c);
  432:         my @newchildren = $c->children();
  433:         if ($#newchildren >= 0) {
  434:             &getNodesToArray(\@newchildren);
  435:         }
  436:     }
  437: }
  438: 
  439: 
  440: =pod
  441: 
  442: =head2 Routines for the user-interface of the wishlist
  443: 
  444: =over 4
  445: 
  446: =item * &JSforWishlist()
  447: 
  448:      Returns JavaScript-functions needed for wishlist actions like open and close folders.
  449: 
  450: 
  451: =item * &wishlistView(nodes)
  452: 
  453:      Returns the table-HTML-markup for the wishlist in mode "view".
  454:      Recursive call starting with all children of the root of the tree (parameter nodes is reference to an array containing the nodes of the current level).     
  455: 
  456: 
  457: =item * &wishlistEdit(nodes)
  458: 
  459:      Returns the table-HTML-markup for the wishlist in mode "edit".
  460:      Recursive call starting with all children of the root of the tree (parameter nodes is reference to an array containing the nodes of the current level).     
  461: 
  462: 
  463: =item * &wishlistMove(nodes, marked)
  464: 
  465:      Returns the table-HTML-markup for the wishlist in mode "move". Highlights all entry "selected to move" contained in marked (reference to array).
  466:      Recursive call starting with all children of the root of the tree (parameter nodes is reference to an array containing the nodes of the current level).     
  467: 
  468: 
  469: =item * &wishlistImport(nodes, numskipped)
  470: 
  471:      Returns the table-HTML-markup for the wishlist in mode "import".
  472:      Recursive call starting with all children of the root of the tree (parameter nodes is reference to an array containing the nodes of the current level).
  473:      Side effect: increments the scalar ref: numskipped with a count of items in 
  474:      Stored Links unavailable for selection, (e.g., now marked obsolete or
  475:      inaccessible in Community context).
  476: 
  477: =item * &makePage(mode, marked)
  478: 
  479:      Returns the HTML-markup for the whole wishlist depending on mode. If mode is "move" we need the marked entries to be highlighted a "selected to move". 
  480:      Calls &wishlistView(nodes), &wishlistEdit(nodes) or &wishlistMove(nodes, marked).
  481:  
  482: 
  483: =item * &makePopUpNewLink(title, path)
  484: 
  485:      Returns the HTML-markup for the pop-up-window 'Add Link'. If this is called up from a browsed resource, the input-fields titel and path are pre-filled with the resources' meta-data-title and it's path. 
  486: 
  487: 
  488: =item * &makePopUpNewFolder()
  489: 
  490:      Returns the HTML-markup for the pop-up-window 'Add Folder'.
  491: 
  492: 
  493: =item * &makePageSet()
  494: 
  495:      Returns the HTML-Markup for the page shown when a link was set by using the icon when viewing a resource.
  496: 
  497: 
  498: =item * &makePageImport()
  499: 
  500:      Returns the HTML-Markup for the page shown when links should be imported into courses.
  501:  
  502: 
  503: =item * &makeErrorPage ()
  504: 
  505:      Returns the HTML-Markup for an error-page shown if the wishlist could not be loaded.
  506:  
  507: 
  508: =back
  509: 
  510: =cut
  511: 
  512: 
  513: # Return a script-tag containing Javascript-function
  514: # needed for wishlist actions like 'new link' ect.
  515: sub JSforWishlist {
  516:     my $startPagePopup = &Apache::loncommon::start_page('Stored Links',undef,
  517:                                                             {'only_body' => 1,
  518:                                                              'js_ready'  => 1,
  519:                                                              'bgcolor'   => '#FFFFFF',});
  520:     my $endPagePopup = &Apache::loncommon::end_page({'js_ready' => 1});
  521: 
  522:     @allFolders = ();
  523:     &getFoldersToArray(\@childrenRt);
  524:     &getFoldersForOption(\@childrenRt);
  525: 
  526:     # it is checked, wether a path links to a LON-CAPA-resource or an external website. links to course-contents are not allowed
  527:     # because they probably will return a kind of 'no access' (unless the user is already in the course, the path links to).
  528:     # also importing these kind of links into a course does not make much sense.
  529:     # to find out if a path (not starting with /res/...) links to course-contents, the same filter as in lonwrapper is used,
  530:     # that means that it is checked wether a path contains .problem, .quiz, .exam etc.
  531:     # this is good for most cases but crashes as soon as a real external website contains one of this pattern in its URL.
  532:     # so maybe there's a better way to find out wether a given URL belongs to a LON-CAPA-server or not ...?
  533:     my $warningLinkNotAllowed1 =
  534:         &mt('You can only insert links to LON-CAPA resources from the resource-pool'.
  535:             ' or to external websites.'.
  536:             ' Paths to LON-CAPA resources must be of the form /res/domain/user/...'.
  537:             ' Paths to external websites must contain the network protocol, e.g. http://...');
  538:     my $warningLinkNotAllowed2 = &mt('The following link is not allowed:').' ';
  539:     my $warningLink = &mt('You must insert a title and a path!');
  540:     my $warningFolder = &mt('You must insert a title!');
  541:     my $warningDelete = &mt('Are you sure you want to delete the selected entries? Deleting a folder also deletes all entries within this folder!');
  542:     my $warningSave = &mt('You have unsaved changes. You can either save these changes now by clicking "OK" or click "Cancel" if you do not want to save your changes.');
  543:     my $warningMoveS = &mt('You must select at minimum one entry to move!');
  544:     my $warningMoveD = &mt('You must select a destination folder!');
  545:     $foldersOption = '';
  546: 
  547:     my $js = &Apache::lonhtmlcommon::scripttag(<<JAVASCRIPT);
  548:     function newLink() {
  549:         newlinkWin=window.open('/adm/wishlist?mode=newLink','newlinkWin','width=580,height=350, scrollbars=yes');
  550:     }
  551: 
  552:     function newFolder() {
  553:         newfolderWin=window.open('/adm/wishlist?mode=newFolder','newfolderWin','width=580,height=270, scrollbars=yes');
  554:     }
  555: 
  556:     function setFormAction(action,mode) {
  557:         var r = true;
  558:         setAction('');
  559:         if (action == 'delete') {
  560:             r = confirm("$warningDelete");
  561:             setAction('delete');
  562:         }
  563:         else if (action == 'save') {
  564:             var d = getDifferences();
  565:             if (d) {
  566:                 if (!confirm('$warningSave')) {
  567:                     setAction('noSave');
  568:                     r = true;
  569:                 }
  570:                 else {
  571:                     r = linksOK();
  572:                 }
  573:             }
  574:         }
  575:         else if (action == 'saveOK') {
  576:             r = linksOK();
  577:         }
  578:         else if (action == 'move') {
  579:             r = selectDestinationFolder(mode);
  580:         }
  581:         document.getElementsByName('list')[0].setAttribute("action", "/adm/wishlist?mode="+mode); 
  582:         if (r) {
  583:             document.getElementsByName('list')[0].submit(); 
  584:         }
  585:     }
  586: 
  587:     function setAction(action) {
  588:         document.getElementById('action').value = action; 
  589:     }
  590: 
  591:     function getDifferences() {
  592:         var newtitles = document.getElementsByName('newtitle');
  593:         var i = 0;
  594:         for (i=0;i<newtitles.length;i++) {
  595:             var newt = newtitles[i].value;
  596:             var oldt = newtitles[i].alt;
  597:             if (newt != oldt) {
  598:                 return true;
  599:             }
  600:         }
  601:         var newpath = document.getElementsByName('newpath');
  602:         var i = 0;
  603:         for (i=0;i<newpath.length;i++) {
  604:             var newp = newpath[i].value;
  605:             var oldp = newpath[i].alt;
  606:             if (newp != oldp) {
  607:                 return true;
  608:             }
  609:         }
  610:         var newnote = document.getElementsByName('newnote');
  611:         var i = 0;
  612:         for (i=0;i<newnote.length;i++) {
  613:             var newn = newnote[i].value;
  614:             var oldn = newnote[i].innerHTML;
  615:             if (newn != oldn) {
  616:                 return true;
  617:             }
  618:         }
  619:         return false;
  620:     }
  621: 
  622:     function linksOK() {
  623:         var newpath = document.getElementsByName('newpath');
  624:         var i = 0;
  625:         for (i=0;i<newpath.length;i++) {
  626:             var path = newpath[i].value;
  627:             var linkOK = (path.match(/^http:\\/\\//) || path.match(/^https:\\/\\//))
  628:                          && !(path.match(/\\.problem/) || path.match(/\\.exam/)
  629:                          || path.match(/\\.quiz/) || path.match(/\\.assess/)
  630:                          || path.match(/\\.survey/) || path.match(/\\.form/)
  631:                          || path.match(/\\.library/) || path.match(/\\.page/)
  632:                          || path.match(/\\.sequence/));
  633:             if (!path.match(/^(\\/res\\/)/) && !linkOK) {
  634:                 alert("$warningLinkNotAllowed1 $warningLinkNotAllowed2"+path);
  635:                 return false;
  636:             }
  637:          }
  638:         return true;
  639:     }
  640: 
  641:     function onLoadAction(mode) {
  642:         window.name = 'wishlist';
  643:         if (mode == "edit") {
  644:             var deepestRows = getDeepestRows();
  645:             setDisplaySelect(deepestRows, '');
  646:         }
  647:     }
  648: 
  649:     function folderAction(rowid) {
  650:         var row = document.getElementById(rowid);
  651:         var indent = getIndent(row);
  652:         var displ;
  653:         var status;
  654:         if (getImage(row) == 'closed') {
  655:             displ = '';
  656:             status = 'open';
  657:         }
  658:         else {
  659:             displ = 'LC_hidden';
  660:             status = 'closed';
  661:         }
  662:         setImage(row,status);
  663:         if (getNextRow(row) != null) {
  664:             var nextIndent = getIndent(getNextRow(row));
  665:             row = getNextRow(row);
  666:             while (nextIndent > indent) {
  667:                 if (displ == '') {
  668:                     row.className = (row.className).replace('LC_hidden','');
  669:                 }
  670:                 else if (displ != '' && !((row.className).match('LC_hidden'))) {
  671:                     var oldClass = row.className;
  672:                     row.className = oldClass+' LC_hidden';
  673:                     setDisplayNote(row.id.replace('row','note'),'LC_hidden');
  674:                 }
  675:                 if (status == 'open' && getImage(row).match('closed')) {
  676:                     row = getNextRowWithIndent(row, getIndent(row));
  677:                 }
  678:                 else {
  679:                     row = getNextRow(row);
  680:                 } 
  681:                 if (row != null) {
  682:                     nextIndent = getIndent(row);
  683:                 } 
  684:                 else {
  685:                     nextIndent = indent;
  686:                 }
  687:             }
  688:         }
  689:         setClasses();
  690:         var newtitles = document.getElementsByName('newtitle');
  691:         if (newtitles.length>0) {
  692:             var deepestRows = getDeepestRows();
  693:             var otherRows = getOtherRows(deepestRows);
  694:             setDisplaySelect(deepestRows,'');
  695:             setDisplaySelect(otherRows,'LC_hidden');
  696:         }
  697:     }
  698: 
  699:     function selectAction(rowid) {
  700:         var row = document.getElementById(rowid);
  701:         var indent = getIndent(row);
  702:         var checked = getChecked(row);
  703:         var previousFolderRows = new Array();
  704:         if (indent != 0) {
  705:             previousFolderRows = getPreviousFolderRows(row);
  706:         }
  707:         if (getNextRow(row) != null) {
  708:             var nextIndent = getIndent(getNextRow(row));
  709:             row = getNextRow(row);
  710:                 while (nextIndent > indent) {
  711:                     setChecked(row,checked);
  712:                     if (status == 'open' && getImage(row).match('closed')) {
  713:                         row = getNextRowWithIndent(row, getIndent(row));
  714:                     }
  715:                     else {
  716:                         row = getNextRow(row);
  717:                     }
  718:                     if (row != null) {
  719:                         nextIndent = getIndent(row);
  720:                     }
  721:                     else {
  722:                         nextIndent = indent;
  723:                     }
  724:                 }
  725:         }
  726:         if (!checked) {
  727:             var i = 0;
  728:             for (i=0;i<previousFolderRows.length;i++) {
  729:                 setChecked(previousFolderRows[i], false);
  730:             }
  731:         }
  732:     }
  733: 
  734:     function getNextNote(row) {
  735:         var rowId = row.id;
  736:         var nextRowId = parseInt(rowId.substr(3,rowId.length))+1;
  737:         nextRowId = "note"+nextRowId;
  738:         var nextRow = document.getElementById(nextRowId);
  739:         return nextRow;
  740:     }
  741: 
  742:     function getNextRow(row) {
  743:         var rowId = row.id;
  744:         var nextRowId = parseInt(rowId.substr(3,rowId.length))+1;
  745:         nextRowId = "row"+nextRowId;
  746:         var nextRow = document.getElementById(nextRowId);
  747:         return nextRow;
  748:     }
  749: 
  750:     function getPreviousRow(row) {
  751:         var rowId = row.id;
  752:         var previousRowId =  parseInt(rowId.substr(3,rowId.length))-1;
  753:         previousRowId = "row"+previousRowId;
  754:         var previousRow =document.getElementById(previousRowId);
  755:         return previousRow;
  756:     }
  757: 
  758:     function getIndent(row) {
  759:         var childPADD = document.getElementById(row.id.replace('row','padd'));
  760:         indent = childPADD.style.paddingLeft;
  761:         indent = parseInt(indent.substr(0,(indent.length-2)));
  762:  
  763:         if (getImage(row).match('link')) {
  764:             indent -= $indentConst;
  765:         }
  766:         return indent;
  767:     }
  768: 
  769:     function getNextRowWithIndent(row, indent) {
  770:         var nextRow = getNextRow(row);
  771:         if (nextRow != null) {
  772:         var nextIndent = getIndent(nextRow);
  773:         while (nextIndent >= indent) {
  774:             if (nextIndent == indent) {
  775:                 return nextRow;
  776:             }
  777:             nextRow = getNextRow(nextRow);
  778:             if (nextRow == null) {
  779:                 return null;
  780:             }
  781:             nextIndent = getIndent(nextRow);
  782:         }
  783:         }
  784:         return nextRow;
  785:     }
  786: 
  787:     function getImage(row) {
  788:         var childIMG = document.getElementById(row.id.replace('row','img'));
  789:         if ((childIMG.src).match('closed')) {
  790:             return 'closed';
  791:         }
  792:         else if ((childIMG.src).match('open')) {
  793:             return 'open;'
  794:         }
  795:         else {
  796:             return 'link';
  797:         }
  798:     } 
  799: 
  800:     function setImage(row, status) {
  801:         var childIMG = document.getElementById(row.id.replace('row','img'));
  802:         var childIMGFolder = document.getElementById(row.id.replace('row','imgFolder'));
  803:         childIMG.src = "/adm/lonIcons/arrow."+status+".gif";
  804:         childIMGFolder.src="/adm/lonIcons/navmap.folder."+status+".gif"; 
  805:     }
  806: 
  807:     function getChecked(row) {
  808:         var childCHECK = document.getElementById(row.id.replace('row','check'));
  809:         var checked = childCHECK.checked;
  810:         return checked;
  811:     }
  812: 
  813:     function setChecked(row,checked) {
  814:         var childCHECK = document.getElementById(row.id.replace('row','check'));
  815:         if (!childCHECK.disabled) {
  816:             childCHECK.checked = checked;
  817:         }
  818:     }
  819: 
  820:     function getPreviousFolderRows(row) {
  821:         var previousRow = getPreviousRow(row);
  822:         var indent = getIndent(previousRow);
  823:         var kindOfEntry = getImage(previousRow);
  824:         var rows = new Array();
  825:         if (kindOfEntry != 'link') {
  826:             rows.push(previousRow);
  827:         }
  828: 
  829:         while (indent >0) {
  830:             previousRow = getPreviousRow(previousRow);
  831:             if (previousRow != null) {
  832:                 indent = getIndent(previousRow);
  833:                 kindOfEntry = getImage(previousRow);
  834:                 if (kindOfEntry != 'link') {
  835:                     rows.push(previousRow);
  836:                 }
  837:             }
  838:             else {
  839:                 indent = 0; 
  840:             }
  841:         }
  842:         return rows;
  843:     }
  844: 
  845:     function getDeepestRows() {
  846:         var row = document.getElementById('row0');
  847:         var firstRow = row;
  848:         var indent = getIndent(row);
  849:         var maxIndent = indent;
  850:         while (getNextRow(row) != null) {
  851:             row = getNextRow(row);
  852:             indent = getIndent(row);
  853:             if (indent>maxIndent && !((row.className).match('LC_hidden'))) {
  854:                 maxIndent = indent;
  855:             }
  856:         }
  857:         var deepestRows = new Array();
  858:         row = firstRow;
  859:         var rowIndent;
  860:         while (getNextRow(row) != null) {
  861:             rowIndent = getIndent(row);
  862:             if (rowIndent == maxIndent) {
  863:                 deepestRows.push(row);
  864:             }
  865:             row = getNextRow(row);
  866:         }
  867:         rowIndent = getIndent(row);
  868:         if (rowIndent == maxIndent) {
  869:             deepestRows.push(row);
  870:         }
  871:         return deepestRows;
  872:     }
  873: 
  874:     function getOtherRows(deepestRows) {
  875:         var row = document.getElementById('row0');
  876:         var otherRows = new Array();
  877:         var isIn = false;
  878:         while (getNextRow(row) != null) {
  879:             var i = 0;
  880:             for (i=0; i < deepestRows.length; i++) {
  881:                 if (row.id == deepestRows[i].id) {
  882:                     isIn = true;
  883:                 }
  884:             }
  885:             if (!isIn) {
  886:                 otherRows.push(row);
  887:             }
  888:             row = getNextRow(row);
  889:             isIn = false;
  890:         }
  891:         for (i=0; i < deepestRows.length; i++) {
  892:             if (row.id == deepestRows[i].id) {
  893:                 isIn = true;
  894:             }
  895:         }
  896:         if (!isIn) {
  897:             otherRows.push(row);
  898:         }
  899:         return otherRows;
  900:     }
  901: 
  902:     function setDisplaySelect(deepestRows, displ) {
  903:         var i = 0;
  904:         for (i = 0; i < deepestRows.length; i++) {
  905:             var row = deepestRows[i];
  906:             var childSEL = document.getElementById(row.id.replace('row','sel'));
  907:             childSEL.className = displ;
  908:         } 
  909:     }
  910: 
  911:     function submitSelect() {
  912:        var list = document.getElementsByName('list')[0];
  913:        list.setAttribute("action","/adm/wishlist?mode=edit");
  914:        list.submit();
  915:     }
  916: 
  917:     function setDisplayNote(rowid, displ) {
  918:         var row = document.getElementById(rowid);
  919:         if (!displ) {
  920:             if ((row.className).match('LC_hidden')) {
  921:                 row.className = (row.className).replace('LC_hidden','');
  922:             }
  923:             else {
  924:                 var oldClass = row.className;
  925:                 row.className = oldClass+' LC_hidden';
  926:             }
  927:         }
  928:         else {
  929:             if (displ == '') {
  930:                 row.className = (row.className).replace('LC_hidden','');
  931:             }
  932:             else if (displ != '' && !((row.className).match('LC_hidden'))) {
  933:                 var oldClass = row.className;
  934:                 row.className = oldClass+' LC_hidden';
  935:             }
  936:         }
  937:         var noteText = document.getElementById(rowid.replace('note','noteText'));
  938:         var noteImg = document.getElementById(rowid.replace('note','noteImg'));
  939:         if (noteText.value) {
  940:             noteImg.src = "/res/adm/pages/anot2.png";
  941:         }
  942:         else {
  943:             noteImg.src = "/res/adm/pages/anot.png";
  944:         }
  945: 
  946:     }
  947: 
  948:     function setClasses() {
  949:         var row = document.getElementById("row0");
  950:         var note = document.getElementById("note0");
  951:         var LC_class = 0;
  952:         if (getNextRow(row) != null) {
  953:             while (getNextRow(row) != null) {
  954:                 if (!(row.className).match('LC_hidden')) {
  955:                     note.className = (note.className).replace('LC_even_row','');
  956:                     note.className = (note.className).replace('LC_odd_row','');
  957:                     if (LC_class) {
  958:                         row.className = 'LC_even_row';
  959:                         note.className = 'LC_even_row'+note.className;
  960:                     }
  961:                     else {
  962:                         row.className = 'LC_odd_row';
  963:                         note.className = 'LC_odd_row'+note.className;;
  964:                     }
  965:                     LC_class = !LC_class;
  966:                 }
  967:                 note = getNextNote(row);
  968:                 row = getNextRow(row);
  969:             }
  970:         }
  971:         if (!(row.className).match('LC_hidden')) {
  972:             note.className = (note.className).replace('LC_even_row','');
  973:             note.className = (note.className).replace('LC_odd_row','');
  974:             if (LC_class) {
  975:                 row.className = 'LC_even_row';
  976:                 note.className = 'LC_even_row'+note.className;
  977:             }
  978:             else {
  979:                 row.className = 'LC_odd_row';
  980:                 note.className = 'LC_odd_row'+note.className;
  981:             }
  982:         }
  983:     }
  984: 
  985:     function selectDestinationFolder(mode) {
  986:         var mark = document.getElementsByName('mark');
  987:         var i = 0;
  988:         for (i = 0; i < mark.length; i++) {
  989:             if (mark[i].checked) {
  990:                 document.getElementsByName('list')[0].submit();
  991:                 return true;
  992:             }
  993:         }
  994:         if (mode == 'move') {
  995:             alert('$warningMoveS');
  996:         }
  997:         else {
  998:             alert('$warningMoveD');
  999:         }
 1000:         return false;
 1001:     }
 1002: 
 1003:     function preview(url) {
 1004:        var newWin;
 1005:        if (!(url.match(/^http:\\/\\//) || url.match(/^https:\\/\\//))) {
 1006:            newWin = window.open(url+'?inhibitmenu=yes','preview','width=560,height=350,scrollbars=yes');
 1007:        }
 1008:        else {
 1009:            newWin = window.open(url,'preview','width=560,height=350,scrollbars=yes');
 1010:        }
 1011:        newWin.focus();
 1012:     }
 1013: 
 1014:     function checkAll() {
 1015:         var checkboxes = document.getElementsByName('check');
 1016:         for (var i = 0; i < checkboxes.length; i++) {
 1017:             if (!checkboxes[i].disabled) {
 1018:                 checkboxes[i].checked = "checked";
 1019:             }
 1020:         }
 1021:     }
 1022: 
 1023:     function uncheckAll() {
 1024:         var checkboxes = document.getElementsByName('check');
 1025:         for (var i = 0; i < checkboxes.length; i++) {
 1026:             if (!checkboxes[i].disabled) {
 1027:                 checkboxes[i].checked = "";
 1028:             }
 1029:         }
 1030:     }
 1031: 
 1032: JAVASCRIPT
 1033:    return $js;
 1034: }
 1035: 
 1036: sub JSforImport{
 1037:     my $rat = shift;
 1038: 
 1039:     my $js;
 1040:     if ($rat eq 'simple' || $rat eq '') {
 1041:         $js = &Apache::lonhtmlcommon::scripttag(<<JAVASCRIPT);
 1042:         function finish_import() {
 1043:             opener.document.forms.simpleedit.importdetail.value='';
 1044:             for (var num = 0; num < document.forms.groupsort.fnum.value; num++) {
 1045:                 try {
 1046:                     eval("document.forms.groupsort.filelink"+num+".value");
 1047:                 }
 1048:                 catch(err) {
 1049:                    continue;
 1050:                 }
 1051:                 if (eval("document.forms.groupsort.check"+num+".checked") && eval("document.forms.groupsort.filelink"+num+".value") != '') {
 1052:                     opener.document.forms.simpleedit.importdetail.value+='&'+
 1053:                     eval("document.forms.groupsort.title"+num+".value")+'='+
 1054:                     eval("document.forms.groupsort.filelink"+num+".value")+'='+
 1055:                     eval("document.forms.groupsort.id"+num+".value");
 1056:                 }
 1057:             }
 1058:             opener.document.forms.simpleedit.submit();
 1059:             self.close();
 1060:         }
 1061: JAVASCRIPT
 1062:     }
 1063:     else {
 1064:         $js = &Apache::lonhtmlcommon::scripttag(<<JAVASCRIPT);
 1065:         function finish_import() {
 1066:             var linkflag=false;
 1067:             for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
 1068:                 if (eval("document.forms.groupsort.check"+num+".checked") && eval("document.forms.groupsort.filelink"+num+".value") != '') {
 1069:                     insertRowInLastRow();
 1070:                     placeResourceInLastRow(
 1071:                         eval("document.forms.groupsort.title"+num+".value"),
 1072:                         eval("document.forms.groupsort.filelink"+num+".value"),
 1073:                         eval("document.forms.groupsort.id"+num+".value"),
 1074:                         linkflag
 1075:                         );
 1076:                     linkflag=true;
 1077:                 }
 1078:             }
 1079:             opener.editmode=0;
 1080:             opener.notclear=0;
 1081:             opener.linkmode=0;
 1082:             opener.draw();
 1083:             self.close();
 1084:         }
 1085: 
 1086:         function insertRowInLastRow() {
 1087:             opener.insertrow(opener.maxrow);
 1088:             opener.addobj(opener.maxrow,'e&2');
 1089:         }
 1090: 
 1091:         function placeResourceInLastRow (title,url,id,linkflag) {
 1092:             opener.mostrecent=opener.newresource(opener.maxrow,2,opener.unescape(title),
 1093:                               opener.unescape(url),'false','normal',id);
 1094:             opener.save();
 1095:             if (linkflag) {
 1096:                 opener.joinres(opener.linkmode,opener.mostrecent,0);
 1097:             }
 1098:             opener.linkmode=opener.mostrecent;
 1099:         }
 1100: JAVASCRIPT
 1101:     }
 1102:     return $js;
 1103: }
 1104: 
 1105: # HTML-Markup for table if in view-mode
 1106: my $wishlistHTMLview;
 1107: my $indent_view = $indentConst;
 1108: sub wishlistView {
 1109:     my $nodes = shift;
 1110: 
 1111:     foreach my $n (@$nodes) {
 1112:         my $index = $n->value()->nindex();
 1113: 
 1114:         # start row, use data_table routines to set class to LC_even or LC_odd automatically. this row contains a checkbox, the title and the note-icon.
 1115:         # only display the top level entries on load
 1116:         $wishlistHTMLview .= ($n->parent()->value() eq 'root')?&Apache::loncommon::start_data_table_row('','row'.$index)
 1117:                                                               :&Apache::loncommon::continue_data_table_row('LC_hidden','row'.$index);
 1118: 
 1119:  
 1120:         # checkboxes
 1121:         $wishlistHTMLview .= '<td><input type="checkbox" name="mark" id="check'.$index.'" value="'.$index.'" '.
 1122:                              'onclick="selectAction('."'row".$index."'".')" /></td>';
 1123: 
 1124:         # entry is a folder
 1125:         if ($n->value()->path() eq '') {
 1126:             $wishlistHTMLview .= '<td id="padd'.$index.'" style="padding-left:'.(($indent_view-$indentConst)<0?0:($indent_view-$indentConst)).'px; min-width: 220px;">'.
 1127:                                  '<a href="javascript:;" onclick="folderAction('."'row".$index."'".')" style="vertical-align:top">'.
 1128:                                  '<img src="/adm/lonIcons/arrow.closed.gif" id="img'.$index.'" alt = "" class="LC_icon"/>'.
 1129:                                  '<img src="/adm/lonIcons/navmap.folder.closed.gif" id="imgFolder'.$index.'" alt="folder"/>'.
 1130:                                  $n->value()->title().'</a></td>';
 1131:         }
 1132:         # entry is a link
 1133:         else {
 1134:             my $quotable_link = &Apache::loncommon::escape_single($n->value()->path());
 1135:             $wishlistHTMLview .= '<td id="padd'.$index.'" style="padding-left:'.(($indent_view-$indentConst)<=0?$indentConst:$indent_view).'px; min-width: 220px;">'.
 1136:                                  '<a href="javascript:preview('."'".$quotable_link."'".');">'.
 1137:                                  '<img src="/res/adm/pages/wishlist-link.png" id="img'.$index.'" alt="link" />'.
 1138:                                  $n->value()->title().'</a></td>';
 1139:         }
 1140: 
 1141:         # note-icon, different icons for an entries with note and those without
 1142:         my $noteIMG = 'anot.png';
 1143: 
 1144:         if ($n->value()->note() ne '') {
 1145:             $noteIMG = 'anot2.png';
 1146:         }
 1147: 
 1148:         $wishlistHTMLview .= '<td style="padding-left:10px;"><a href="javascript:;" onclick="setDisplayNote('."'note".$index."'".')">'.
 1149:                              '<img id="noteImg'.$index.'" src="/res/adm/pages/'.$noteIMG.'" alt="'.&mt('Note').'" title="'.&mt('Note').'" '.
 1150:                              ' class="LC_icon"/></a></td>';
 1151: 
 1152:         $wishlistHTMLview .= &Apache::loncommon::end_data_table_row();
 1153: 
 1154:         # start row containing the textarea for the note, do not display note on default
 1155:         $wishlistHTMLview .= &Apache::loncommon::continue_data_table_row('LC_hidden','note'.$index).
 1156:                              '<td></td><td>'.
 1157:                              '<textarea id="noteText'.$index.'" cols="25" rows="3" style="width:100%" '.
 1158:                              'name="newnote" >'.
 1159:                              $n->value()->note().'</textarea></td><td></td>';
 1160:         $wishlistHTMLview .= &Apache::loncommon::end_data_table_row();
 1161: 
 1162:         # if the entry is a folder, it could have other entries as content. if it has, call wishlistView for those entries 
 1163:         my @children = $n->children();
 1164:         if ($#children >=0) {
 1165:             $indent_view += 20;
 1166:             &wishlistView(\@children);
 1167:             $indent_view -= 20;
 1168:         }
 1169:     }
 1170: }
 1171: 
 1172: 
 1173: # HTML-Markup for table if in edit-mode
 1174: my $wishlistHTMLedit;
 1175: my $indent_edit = $indentConst;
 1176: sub wishlistEdit {
 1177:     my $nodes = shift;
 1178:     my $curNode = 1;
 1179: 
 1180:     foreach my $n (@$nodes) {
 1181:         my $index = $n->value()->nindex();
 1182: 
 1183:         # start row, use data_table routines to set class to LC_even or LC_odd automatically.
 1184:         # this rows contains a checkbox, a select-field for sorting entries, the title in an input-field and the note-icon.
 1185:         # only display the top level entries on load
 1186:         $wishlistHTMLedit .= ($n->parent()->value() eq 'root')?&Apache::loncommon::start_data_table_row('','row'.$index)
 1187:                                                               :&Apache::loncommon::continue_data_table_row('LC_hidden','row'.$index);
 1188: 
 1189:         # checkboxes
 1190:         $wishlistHTMLedit .= '<td><input type="checkbox" name="mark" id="check'.$index.'" value="'.$index.'" '.
 1191:                              'onclick="selectAction('."'row".$index."'".')" /></td>';
 1192: 
 1193:         # option-tags for sorting entries. we need the numbers from 1 to n with n being the number of entries on the same level as the current entry.
 1194:         # set the number for the current entry into brackets 
 1195:         my $options;
 1196:         for (my $i = 1; $i < ((scalar @{$nodes})+1); $i++) {
 1197:            if ($i == $curNode) {
 1198:                $options .= '<option selected="selected" value="">('.$i.')</option>';
 1199:            }
 1200:            else {
 1201:                $options .= '<option value="'.$i.'">'.$i.'</option>';
 1202:            }
 1203:         }
 1204:         $curNode++;
 1205: 
 1206:         # entry is a folder
 1207:         if ($n->value()->path() eq '') {
 1208:             $wishlistHTMLedit .= '<td><select class="LC_hidden" name="sel" id="sel'.$index.'" onchange="submitSelect();">'.
 1209:                                  $options.'</select></td>'.
 1210:                                  '<td id="padd'.$index.'" style="padding-left:'.(($indent_edit-$indentConst)<0?0:($indent_edit-$indentConst)).'px;">'.
 1211:                                  '<a href="javascript:;" onclick="folderAction('."'row".$index."'".')" style="vertical-align:top" >'.
 1212:                                  '<img src="/adm/lonIcons/arrow.closed.gif" id="img'.$index.'" alt = ""  class="LC_icon"/>'.
 1213:                                  '<img src="/adm/lonIcons/navmap.folder.closed.gif" id="imgFolder'.$index.'" alt="folder"/></a>'.
 1214:                                  '<input type="text" name="newtitle" value="'.$n->value()->title().'" alt = "'.$n->value()->title().'" />'.
 1215:                                  '</td><td></td>';
 1216: 
 1217:         }
 1218:         # entry is a link
 1219:         else {
 1220:             $wishlistHTMLedit .= '<td><select class="LC_hidden" name="sel" id="sel'.$index.'" onchange="submitSelect();">'.
 1221:                                  $options.'</select></td>'.
 1222:                                  '<td id="padd'.$index.'" style="padding-left:'.(($indent_edit-$indentConst)<=0?$indentConst:$indent_edit).'px;">'.
 1223:                                  '<img src="/res/adm/pages/wishlist-link.png" id="img'.$index.'" alt="link"/>'.
 1224:                                  '<input type="text" name="newtitle" value="'.$n->value()->title().'" alt = "'.$n->value()->title().'" /></td>'.
 1225:                                  '<td><input type="text" name="newpath" value="'.$n->value()->path().'" alt = "'.$n->value()->path().'" /></td>';
 1226:         }
 1227:         
 1228:         # note-icon, different icons for an entries with note and those without
 1229:         my $noteIMG = 'anot.png';
 1230: 
 1231:         if ($n->value()->note() ne '') {
 1232:             $noteIMG = 'anot2.png';
 1233:         }
 1234: 
 1235:         $wishlistHTMLedit .= '<td style="padding-left:10px;"><a href="javascript:;" onclick="setDisplayNote('."'note".$index."'".')">'.
 1236:                              '<img id="noteImg'.$index.'" src="/res/adm/pages/'.$noteIMG.'" alt="'.&mt('Note').'" title="'.&mt('Note').'" '.
 1237:                              ' class="LC_icon"/></a></td>';
 1238: 
 1239:         $wishlistHTMLedit .= &Apache::loncommon::end_data_table_row();
 1240: 
 1241:         # start row containing the textarea for the note
 1242:         $wishlistHTMLedit .= &Apache::loncommon::continue_data_table_row('LC_hidden','note'.$index).
 1243:                              '<td></td><td></td><td colspan="2">'.
 1244:                              '<textarea id="noteText'.$index.'" cols="25" rows="3" style="width:100%" '.
 1245:                              'name="newnote">'.
 1246:                              $n->value()->note().'</textarea></td><td></td>';
 1247:         $wishlistHTMLedit .= &Apache::loncommon::end_data_table_row();
 1248: 
 1249:         # if the entry is a folder, it could have other entries as content. if it has, call wishlistEdit for those entries 
 1250:         my @children = $n->children();
 1251:         if ($#children >=0) {
 1252:             $indent_edit += 20;
 1253:             &wishlistEdit(\@children);
 1254:             $indent_edit -= 20;
 1255:         }
 1256:     }
 1257: }
 1258: 
 1259: 
 1260: 
 1261: # HTML-Markup for table if in move-mode
 1262: my $wishlistHTMLmove ='<tr id="root" class="LC_odd_row"><td><input type="radio" name="mark" id="radioRoot" value="root" /></td>'.
 1263:                       '<td>'.&mt('Top level').'</td><td></td></tr>';
 1264: my $indent_move = $indentConst;
 1265: sub wishlistMove {
 1266:     my $nodes = shift;
 1267:     my $marked = shift;
 1268: 
 1269:     foreach my $n (@$nodes) {
 1270:         my $index = $n->value()->nindex();
 1271: 
 1272:         #find out wether the current entry was marked to be moved.
 1273:         my $isIn = 0;
 1274:         foreach my $m (@$marked) {
 1275:             if ($index == $m) {
 1276:                $isIn = 1;
 1277:             }
 1278:         }
 1279:         # start row and set class for even or odd row. this rows contains the title and the note-icon and can contain a radio-button
 1280:         $wishlistHTMLmove .= &Apache::loncommon::start_data_table_row('','row'.$index);
 1281: 
 1282: 
 1283:         # entry is a folder
 1284:         if ($n->value()->path() eq '') {
 1285:             # display a radio-button, if the folder was not selected to be moved
 1286:             if (!$isIn) {
 1287:                 $wishlistHTMLmove .= '<td><input type="radio" name="mark" id="radio'.$index.'" value="'.$index.'" /></td>'.
 1288:                                      '<td id="padd'.$index.'" style="padding-left:'.(($indent_move-$indentConst)<0?0:($indent_move-$indentConst)).'px; min-width: 220px;">';
 1289:             }
 1290:             # highlight the title, if the folder was selected to be moved
 1291:             else {
 1292:                 $wishlistHTMLmove .= '<td></td>'.
 1293:                                      '<td id="padd'.$index.'" style="padding-left:'.(($indent_move-$indentConst)<0?0:($indent_move-$indentConst)).'px; min-width: 220px;'.
 1294:                                      'color:red;">';
 1295:             }
 1296:             #arrow- and folder-image, all folders are open, and title
 1297:             $wishlistHTMLmove .= '<img src="/adm/lonIcons/arrow.open.gif" id="img'.$index.'" alt = "" />'.
 1298:                                  '<img src="/adm/lonIcons/navmap.folder.open.gif" id="imgFolder'.$index.'" alt="folder"/>'.
 1299:                                  $n->value()->title().'</td>';
 1300:         }
 1301:         # entry is a link
 1302:         else {
 1303:             # higlight the title, if the link was selected to be moved
 1304:             my $highlight = '';
 1305:             if ($isIn) {
 1306:                $highlight = 'style="color:red;"';
 1307:             }
 1308:             # link-image and title
 1309:             my $quotable_link = &Apache::loncommon::escape_single($n->value()->path());
 1310:             $wishlistHTMLmove .= '<td></td>'.
 1311:                                  '<td id="padd'.$index.'" style="padding-left:'.(($indent_move-$indentConst)<=0?$indentConst:$indent_move).'px; min-width: 220px;">'.
 1312:                                  '<a href="javascript:preview('."'".$quotable_link."'".');" '.$highlight.'>'.
 1313:                                  '<img src="/res/adm/pages/wishlist-link.png" id="img'.$index.'" alt="link"/>'.
 1314:                                  $n->value()->title().'</a></td>';
 1315:         }
 1316: 
 1317:         # note-icon, different icons for an entries with note and those without
 1318:         my $noteIMG = 'anot.png';
 1319: 
 1320:         if ($n->value()->note() ne '') {
 1321:             $noteIMG = 'anot2.png';
 1322:         }
 1323: 
 1324:         $wishlistHTMLmove .= '<td style="padding-left:10px;"><a href="javascript:;" onclick="setDisplayNote('."'note".$index."'".')">'.
 1325:                              '<img id="noteImg'.$index.'" src="/res/adm/pages/'.$noteIMG.'" alt="'.&mt('Note').'" title="'.&mt('Note').'" '.
 1326:                              ' class="LC_icon"/></a></td>';
 1327: 
 1328:         $wishlistHTMLmove .= &Apache::loncommon::end_data_table_row();
 1329: 
 1330:         # start row containing the textarea for the note, readonly in move-mode
 1331:         $wishlistHTMLmove .= &Apache::loncommon::continue_data_table_row('LC_hidden','note'.$index).
 1332:                              '<td></td><td>'.
 1333:                              '<textarea id="noteText'.$index.'" cols="25" rows="3" style="width:100%" '.
 1334:                              'name="newnote" readonly="readonly">'.
 1335:                              $n->value()->note().'</textarea></td><td></td>'.
 1336:                              &Apache::loncommon::end_data_table_row();
 1337: 
 1338:         # if the entry is a folder, it could have other entries as content. if it has, call wishlistMove for those entries 
 1339:         my @children = $n->children();
 1340:         if ($#children >=0) {
 1341:             $indent_move += 20;
 1342:             &wishlistMove(\@children, $marked);
 1343:             $indent_move -= 20;
 1344:         }
 1345:     }
 1346: }
 1347: 
 1348: 
 1349: 
 1350: # HTML-Markup for table if in import-mode
 1351: my $wishlistHTMLimport;
 1352: my $indent_imp = $indentConst;
 1353: my $form = 1;
 1354: sub wishlistImport {
 1355:     my ($nodes,$numskipped) = @_;
 1356: 
 1357:     my ($is_community,%nopick);
 1358:     if ($env{'request.course.id'}) {
 1359:         if (&Apache::loncommon::course_type() eq 'Community') {
 1360:             $is_community = 1;
 1361:         }
 1362:     }
 1363: 
 1364:     foreach my $n (@$nodes) {
 1365:         my $index = $n->value()->nindex();
 1366: 
 1367:         #
 1368:         # Determine which resources in stored links may be imported into a course/community.
 1369:         # (a) Import of directories in /res space is not supported.
 1370:         # (b) Import of a resource into a community requires user has 'bro' privilege for resource
 1371:         #     (i.e., user has author or co-author role for corresponcding Authoring Space).
 1372:         # (c) Import of a resource into a course requires user has 'be' privilege for resource.
 1373:         #
 1374: 
 1375:         if ($n->value()->path() =~ m{^(/res/$match_domain/$match_username/)}) {
 1376:             if ($n->value()->path() =~ m{/$}) {
 1377:                 $nopick{$n->value()->path()} = $n->value()->title();
 1378:                 $$numskipped ++;
 1379:             } else {
 1380:                 if ($is_community) {
 1381:                     unless (&Apache::lonnet::allowed('bro',$n->value()->path())) {
 1382:                         $nopick{$n->value()->path()} = $n->value()->title();
 1383:                         $$numskipped ++;
 1384:                     }
 1385:                 } else {
 1386:                     unless (&Apache::lonnet::allowed('bre',$n->value()->path())) {
 1387:                         $nopick{$n->value()->path()} = $n->value()->title();
 1388:                         $$numskipped ++;
 1389:                     }
 1390:                 }
 1391:             }
 1392:         }
 1393: 
 1394:         # start row, use data_table routines to set class to LC_even or LC_odd automatically. this row contains a checkbox, the title and the note-icon.
 1395:         # only display the top level entries on load
 1396:         $wishlistHTMLimport .= ($n->parent()->value() eq 'root')?&Apache::loncommon::start_data_table_row('','row'.$index)
 1397:                                                                 :&Apache::loncommon::continue_data_table_row('LC_hidden','row'.$index);
 1398: 
 1399:  
 1400:         # checkboxes
 1401:         $wishlistHTMLimport .= '<td>';
 1402:         my ($disabled,$onclick,$image,$style);
 1403:         if ($nopick{$n->value()->path()}) {
 1404:             $disabled = ' disabled="disabled"';
 1405:             $image = 'wishlist-link-lighter.png';
 1406:             $style = 'style="color:#808080;"';
 1407:         } else {
 1408:             $onclick = ' onclick="selectAction('."'row".$index."'".')"';
 1409:             $image = 'wishlist-link.png';
 1410:         }
 1411:         $wishlistHTMLimport .= '<input type="checkbox" name="check" id="check'.$index.'" value="'.$index.'" '.
 1412:                                $disabled.$onclick.' />'.
 1413:                                '<input type="hidden" name="title'.$index.'" value="'.&escape($n->value()->title()).'" />'.
 1414:                                '<input type="hidden" name="filelink'.$index.'" value="'.&escape($n->value()->path()).'" />'.
 1415:                                '<input type="hidden" name="id'.$index.'" />';
 1416:         $wishlistHTMLimport .= '</td>';
 1417: 
 1418:         # entry is a folder
 1419:         if ($n->value()->path() eq '') {
 1420:             $wishlistHTMLimport .= '<td id="padd'.$index.'" style="padding-left:'.(($indent_imp-$indentConst)<0?0:($indent_imp-$indentConst)).'px; min-width: 220px;">'.
 1421:                                    '<a href="javascript:;" onclick="folderAction('."'row".$index."'".')" style="vertical-align:top">'.
 1422:                                    '<img src="/adm/lonIcons/arrow.closed.gif" id="img'.$index.'" alt = "" class="LC_icon"/>'.
 1423:                                    '<img src="/adm/lonIcons/navmap.folder.closed.gif" id="imgFolder'.$index.'" alt="folder"/>'.
 1424:                                    $n->value()->title().'</a></td>';
 1425:         }
 1426:         # entry is a link
 1427:         else {
 1428:             $wishlistHTMLimport .= '<td id="padd'.$index.'" style="padding-left:'.(($indent_imp-$indentConst)<=0?$indentConst:$indent_imp).'px; min-width: 220px;">';
 1429:             unless ($nopick{$n->value()->path()}) {
 1430:                 my $quotable_link = &Apache::loncommon::escape_single($n->value()->path());
 1431:                 $wishlistHTMLimport .= '<a href="javascript:preview('."'".$quotable_link."'".');">';
 1432:             }
 1433:             $wishlistHTMLimport .= '<img src="/res/adm/pages/'.$image.'" id="img'.$index.'" alt="link" />'.
 1434:                                    '<span '.$style.'>'.$n->value()->title().'</span></a></td>';
 1435:                                    $form++;
 1436:         }
 1437: 
 1438:         # note-icon, different icons for an entries with note and those without
 1439:         my $noteIMG = 'anot.png';
 1440: 
 1441:         if ($n->value()->note() ne '') {
 1442:             $noteIMG = 'anot2.png';
 1443:         }
 1444: 
 1445:         $wishlistHTMLimport .= '<td style="padding-left:10px;"><a href="javascript:;" onclick="setDisplayNote('."'note".$index."'".')">'.
 1446:                              '<img id="noteImg'.$index.'" src="/res/adm/pages/'.$noteIMG.'" alt="'.&mt('Note').'" title="'.&mt('Note').'" '.
 1447:                              ' class="LC_icon"/></a></td>';
 1448: 
 1449:         $wishlistHTMLimport .= &Apache::loncommon::end_data_table_row();
 1450: 
 1451:         # start row containing the textarea for the note, do not display note on default, readonly in import-mode
 1452:         $wishlistHTMLimport .= &Apache::loncommon::continue_data_table_row('LC_hidden','note'.$index).
 1453:                              '<td></td><td>'.
 1454:                              '<textarea id="noteText'.$index.'" cols="25" rows="3" style="width:100%" '.
 1455:                              'name="newnote" readonly="readonly">'.
 1456:                              $n->value()->note().'</textarea></td><td></td>';
 1457:         $wishlistHTMLimport .= &Apache::loncommon::end_data_table_row();
 1458: 
 1459:         # if the entry is a folder, it could have other entries as content. if it has, call wishlistImport for those entries 
 1460:         my @children = $n->children();
 1461:         if ($#children >=0) {
 1462:             $indent_imp += 20;
 1463:             &wishlistImport(\@children,$numskipped);
 1464:             $indent_imp -= 20;
 1465:         }
 1466:     }
 1467:     return;
 1468: }
 1469: 
 1470: # Returns the HTML-Markup for wishlist
 1471: sub makePage {
 1472:     my $rootgiven = shift;
 1473:     my $mode = shift;
 1474:     my $marked = shift;
 1475: 
 1476:     $root = $rootgiven;
 1477:     @childrenRt = $root->children();
 1478: 
 1479:     # breadcrumbs and start_page
 1480:     &Apache::lonhtmlcommon::clear_breadcrumbs();
 1481:     &Apache::lonhtmlcommon::add_breadcrumb(
 1482:               { href => '/adm/wishlist?mode='.$mode,
 1483:                 text => 'Stored Links'});
 1484:     my $startPage = &Apache::loncommon::start_page('Stored Links',undef,
 1485:                                                      {'add_entries' => {
 1486:                                                         'onload' => 'javascript:onLoadAction('."'".$mode."'".');',
 1487:                                                         'onunload' => 'javascript:window.name = '."'loncapaclient'"}});
 1488: 
 1489:     my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs(&mt('Stored Links'),'Wishlist');
 1490: 
 1491:     # get javascript-code for wishlist-interactions
 1492:     my $js = &JSforWishlist();
 1493: 
 1494:     # texthash for items in funtionlist
 1495:     my %lt = &Apache::lonlocal::texthash(
 1496:                  'ed' => 'Edit',
 1497:                  'vw' => 'View',
 1498:                  'al' => 'Add Link',
 1499:                  'af' => 'Add Folder',
 1500:                  'mv' => 'Move Selected',
 1501:                  'dl' => 'Delete Selected',
 1502:                  'sv' => 'Save');
 1503: 
 1504:     # start functionlist
 1505:     my $functions = &Apache::lonhtmlcommon::start_funclist();
 1506: 
 1507:     # icon for edit-mode, display when in view-mode
 1508:     if ($mode eq 'view') {
 1509:         $functions .= &Apache::lonhtmlcommon::add_item_funclist('<a href="javascript:;" '.
 1510:                           'onclick="setFormAction('."'save','edit'".');" class="LC_menubuttons_link">'.
 1511:                           '<img src="/res/adm/pages/edit-mode-22x22.png" alt="'.$lt{'ed'}.'" '.
 1512:                           'title="'.$lt{'ed'}.'" class="LC_icon"/> '.
 1513:                           '<span class="LC_menubuttons_inline_text">'.$lt{'ed'}.'</span></a>');
 1514:     }
 1515:     # icon for view-mode, display when in edit-mode
 1516:     else {
 1517:         $functions .= &Apache::lonhtmlcommon::add_item_funclist('<a href="javascript:;" '.
 1518:                           'onclick="setFormAction('."'save','view'".');" class="LC_menubuttons_link">'.
 1519:                           '<img src="/res/adm/pages/view-mode-22x22.png" alt="'.$lt{'vw'}.'" '.
 1520:                           'title="'.$lt{'vw'}.'" class="LC_icon"/> '.
 1521:                           '<span class="LC_menubuttons_inline_text">'.$lt{'vw'}.'</span></a>');
 1522:     }
 1523:     
 1524:     # icon for adding a new link
 1525:     $functions .= &Apache::lonhtmlcommon::add_item_funclist('<a href="javascript:;" '.
 1526:                       'onclick="newLink();" class="LC_menubuttons_link">'.
 1527:                       '<img src="/res/adm/pages/link-new-22x22.png" alt="'.$lt{'al'}.'" '.
 1528:                       'title="'.$lt{'al'}.'" class="LC_icon"/>'.
 1529:                       '<span class="LC_menubuttons_inline_text">'.$lt{'al'}.'</span></a>');
 1530: 
 1531:     # icon for adding a new folder
 1532:     $functions .= &Apache::lonhtmlcommon::add_item_funclist('<a href="javascript:;" '.
 1533:                       'onclick="newFolder();" class="LC_menubuttons_link">'.
 1534:                       '<img src="/res/adm/pages/folder-new-22x22.png" alt="'.$lt{'af'}.'" '.
 1535:                       'title="'.$lt{'af'}.'" class="LC_icon"/>'.
 1536:                       '<span class="LC_menubuttons_inline_text">'.$lt{'af'}.'</span></a>');
 1537: 
 1538:     # icon for moving entries
 1539:     $functions .= &Apache::lonhtmlcommon::add_item_funclist('<a href="javascript:;" '.
 1540:                       'onclick="setFormAction('."'move','move'".'); " class="LC_menubuttons_link">'.
 1541:                       '<img src="/res/adm/pages/move-22x22.png" alt="'.$lt{'mv'}.'" '.
 1542:                       'title="'.$lt{'mv'}.'" class="LC_icon" />'.
 1543:                       '<span class="LC_menubuttons_inline_text">'.$lt{'mv'}.'</span></a>');
 1544: 
 1545:     # icon for deleting entries
 1546:     $functions .= &Apache::lonhtmlcommon::add_item_funclist('<a href="javascript:;" '.
 1547:                       'onclick="setFormAction('."'delete','".$mode."'".'); " class="LC_menubuttons_link">'.
 1548:                       '<img src="/res/adm/pages/del.png" alt="'.$lt{'dl'}.'" '.
 1549:                       'title="'.$lt{'dl'}.'" class="LC_icon" />'.
 1550:                       '<span class="LC_menubuttons_inline_text">'.$lt{'dl'}.'</span></a>');
 1551: 
 1552:     # icon for saving changes
 1553:     $functions .= &Apache::lonhtmlcommon::add_item_funclist('<a href="javascript:;" '.
 1554:                       'onclick="setFormAction('."'saveOK','".$mode."'".'); " class="LC_menubuttons_link">'.
 1555:                       '<img src="/res/adm/pages/save-22x22.png" alt="'.$lt{'sv'}.'" '.
 1556:                       'title="'.$lt{'sv'}.'" class="LC_icon" />'.
 1557:                       '<span class="LC_menubuttons_inline_text">'.$lt{'sv'}.'</span></a>');
 1558: 
 1559:     # end funtionlist and generate subbox 
 1560:     $functions.= &Apache::lonhtmlcommon::end_funclist();
 1561:     my $subbox = &Apache::loncommon::head_subbox($functions);
 1562: 
 1563:     # start form 
 1564:     my $inner .= '<form name="list" action ="/adm/wishlist" method="post">'.
 1565:                  '<input type="hidden" id="action" name="action" value="" />';
 1566:  
 1567:     # only display subbox in view- or edit-mode
 1568:     if ($mode eq 'view' || $mode eq 'edit') {
 1569:         $inner .= $subbox;
 1570:     }
 1571: 
 1572:     # generate table-content depending on mode
 1573:     if ($mode eq 'edit') {
 1574:         &wishlistEdit(\@childrenRt);
 1575:         if ($wishlistHTMLedit ne '') {
 1576:             $inner .= &Apache::loncommon::start_data_table("LC_tableOfContent");
 1577:             $inner .= $wishlistHTMLedit;
 1578:             $inner .= &Apache::loncommon::end_data_table();
 1579:         }
 1580:         else {
 1581:             $inner .= '<span class="LC_info">'.&mt("Your Stored Links list is currently empty.").'</span>';
 1582:         }
 1583:         $wishlistHTMLedit = '';
 1584:     }
 1585:     elsif ($mode eq 'view') {
 1586:         &wishlistView(\@childrenRt);
 1587:         if ($wishlistHTMLview ne '') {
 1588:             $inner .= '<table class="LC_data_table LC_tableOfContent">'.$wishlistHTMLview.'</table>';
 1589:         }
 1590:         else {
 1591:             $inner .= '<span class="LC_info">'.&mt("Your Stored Links list is currently empty.").'</span>';
 1592:         }
 1593:         $wishlistHTMLview = '';
 1594:     }
 1595:     else {
 1596:         my $markStr = '';
 1597:         foreach my $m (@$marked) {
 1598:             $markStr .= $m.',';
 1599:         }
 1600:         if ($markStr) {
 1601:             $markStr = substr($markStr, 0, length($markStr)-1);
 1602:             $inner .= '<input type="hidden" value="'.$markStr.'" name="markedToMove" />';
 1603:             $inner .= '<p><span class="LC_info">'.&mt('You have selected the red marked entries to be moved to another folder. '.
 1604:                                                    'Now choose the new destination folder.').'</span></p>';
 1605:             &wishlistMove(\@childrenRt, $marked);
 1606:             $inner .= '<table class="LC_data_table LC_tableOfContent">'.$wishlistHTMLmove.'</table><br/><br/>';
 1607:             $inner .= '<input type="button" value="'.&mt('Move').'" onclick="setFormAction('."'move','view'".');" />'.
 1608:                       '<input type="button" value="'.&mt('Cancel').'" onclick="go('."'/adm/wishlist'".')" />';
 1609: 
 1610:             $wishlistHTMLmove ='<tr id="root" class="LC_odd_row"><td><input type="radio" name="mark" id="radioRoot" value="root" /></td>'.
 1611:                                '<td>'.&mt('Top level').'</td><td></td></tr>';
 1612:         }
 1613:         else {
 1614:             $inner .= '<p><span class="LC_info">'.&mt("You haven't marked any entry to move.").'</span></p>'.
 1615:                       '<input type="button" value="'.&mt('Back').'" onclick="go('."'/adm/wishlist'".')" />';
 1616:         }
 1617:     }
 1618:     
 1619:     # end form 
 1620:     $inner .= '</form>';
 1621: 
 1622:     # end_page 
 1623:     my $endPage =  &Apache::loncommon::end_page();
 1624: 
 1625:     # put all page-elements together
 1626:     my $page = $startPage.$breadcrumbs.$js.$inner.$endPage;
 1627: 
 1628:     return $page;
 1629: }
 1630: 
 1631: 
 1632: # Returns the HTML-Markup for the PopUp, shown when a new link should set, when NOT
 1633: # beeing in the wishlist-interface (method is called in lonmenu and lonsearchcat)
 1634: sub makePopUpNewLink {
 1635:     my ($title, $path) = @_;
 1636: 
 1637:     # Get all existing folders to offer posibility to set a new link
 1638:     # into a folder
 1639:     my %TreeHashLink = &Apache::lonwishlist::getWishlist();
 1640:     my $rootLink = &Apache::Tree::HashToTree(\%TreeHashLink);
 1641:     my @childrenRtLink = $rootLink->children();
 1642: 
 1643:     $foldersOption = '';
 1644:     @allFolders = ();
 1645:     &getFoldersToArray(\@childrenRtLink);
 1646:     &getFoldersForOption(\@childrenRtLink);
 1647: 
 1648:     my $options = '<option value="" selected="selected">('.&mt('Top level').')</option>'.$foldersOption;
 1649:     $foldersOption = '';
 1650:     @allFolders = ();
 1651: 
 1652:     # HTML-Markup for the Pop-Up-window 'Set a link for this resource to wishlist'
 1653:     my $startPageWishlistlink = 
 1654:         &Apache::loncommon::start_page('Save to Stored Links',undef,
 1655:                                       {'only_body' => 1,
 1656:                                        'bgcolor'   => '#FFFFFF',});
 1657: 
 1658:     my $warningLink = &mt('You must insert a title!');
 1659:     my $warningLinkNotAllowed1 =
 1660:         &mt('You can only insert links to LON-CAPA resources from the resource-pool'.
 1661:             ' or to external websites.'.
 1662:             ' Paths to LON-CAPA resources must be of the form /res/domain/user/...'.
 1663:             ' Paths to external websites must contain the network protocol, e.g. http://...');
 1664: 
 1665:     my $inPageWishlistlink1 = '<h1>'.&mt('Save to Stored Links').'</h1>';
 1666:     # If no title is delivered, 'New Link' is called up from the wishlist-interface, so after
 1667:     # submitting the window should close instead of offering a link to wishlist (like it should do
 1668:     # if we call 'Set New Link' from within a browsed ressource)
 1669:     if (!$title) {
 1670:         $inPageWishlistlink1 .= '<form method="post" name="newlink" action="/adm/wishlist" target="wishlist"'.
 1671:                                 'onsubmit="return newlinksubmit();" >';
 1672:     }
 1673:     else {
 1674:         $inPageWishlistlink1 .= '<form method="post" name="newlink" action="/adm/wishlist?mode=set" '.
 1675:                                 'onsubmit="return newlinksubmit();" >';
 1676:     }
 1677:     $inPageWishlistlink1 .= &Apache::lonhtmlcommon::start_pick_box().
 1678:                             &Apache::lonhtmlcommon::row_title(&mt('Link Title'));
 1679: 
 1680:     my $inPageWishlistlink2 = &Apache::lonhtmlcommon::row_closure().
 1681:                               &Apache::lonhtmlcommon::row_title(&mt('Path'));
 1682: 
 1683:     my $inPageWishlistlink3 = &Apache::lonhtmlcommon::row_closure().
 1684:                               &Apache::lonhtmlcommon::row_title(&mt('Note')).
 1685:                               '<textarea name="note" rows="3" cols="35" style="width:100%"></textarea>'.
 1686:                               &Apache::lonhtmlcommon::row_closure(1).
 1687:                               &Apache::lonhtmlcommon::end_pick_box().
 1688:                               '<br/><br/>'.
 1689:                               '<input type="submit" value="'.&mt('Save in').'" />'.
 1690:                               '<select name="folders">'.
 1691:                               $options.
 1692:                               '</select>'.
 1693:                               '<input type="button" value="'.&mt('Cancel').'" onclick="javascript:window.close();" />'.
 1694:                               '</form>';
 1695:     $options = '';
 1696: 
 1697:     my $endPageWishlistlink = &Apache::loncommon::end_page();
 1698: 
 1699:     my $popUp = $startPageWishlistlink.
 1700:     $inPageWishlistlink1.
 1701:     '<input type="text" name="title" size="45" value="" />'.
 1702:     $inPageWishlistlink2.
 1703:     '<input type="text" name="path" size="45" value="" />'.
 1704:     $inPageWishlistlink3;
 1705: 
 1706:     # JavaScript-function to set title and path of ressource automatically
 1707:     # and show warning, if no title was set or path is invalid
 1708:     $popUp .= <<SCRIPT;
 1709:     <script type="text\/javascript">
 1710:     document.getElementsByName("title")[0].value = '$title';
 1711:     document.getElementsByName("path")[0].value = '$path';
 1712:     var fromwishlist = false;
 1713:     var titleget = '$title';
 1714:     if (!titleget) {
 1715:         fromwishlist = true;
 1716:     } 
 1717:     function newlinksubmit(){
 1718:     var title = document.getElementsByName("title")[0].value;
 1719:     var path = document.getElementsByName("path")[0].value;
 1720:     if (!title) {
 1721:         alert("$warningLink");
 1722:         return false;}
 1723:     var linkOK = (path.match(/\^http:(\\\/\\\/)/) || path.match(/\^https:(\\\/\\\/)/))
 1724:     && !(path.match(/\\.problem/) || path.match(/\\.exam/)
 1725:     || path.match(/\\.quiz/) || path.match(/\\.assess/)
 1726:     || path.match(/\\.survey/) || path.match(/\\.form/)
 1727:     || path.match(/\\.library/) || path.match(/\\.page/)
 1728:     || path.match(/\\.sequence/));
 1729:     if (!path.match(/\^(\\\/res\\\/)/) && !linkOK) {
 1730:         alert("$warningLinkNotAllowed1");
 1731:         return false;}
 1732:     if (fromwishlist) {
 1733:         window.close();
 1734:     }
 1735:     return true;}
 1736:     <\/script>
 1737: SCRIPT
 1738: 
 1739:     $popUp .= $endPageWishlistlink;
 1740: 
 1741:     return $popUp;
 1742: }
 1743: 
 1744: sub makePopUpNewFolder {
 1745:     # Get all existing folders to offer posibility to create a new folder
 1746:     # into an existing folder
 1747:     my %TreeHashLink = &Apache::lonwishlist::getWishlist();
 1748:     my $rootLink = &Apache::Tree::HashToTree(\%TreeHashLink);
 1749:     my @childrenRtLink = $rootLink->children();
 1750: 
 1751:     $foldersOption = '';
 1752:     @allFolders = ();
 1753:     &getFoldersToArray(\@childrenRtLink);
 1754:     &getFoldersForOption(\@childrenRtLink);
 1755: 
 1756:     my $options = '<option value="" selected="selected">('.&mt('Top level').')</option>'.$foldersOption;
 1757:     $foldersOption = '';
 1758:     @allFolders = ();
 1759: 
 1760:     # HTML-Markup for the Pop-Up-window 'New Folder'
 1761:     my $startPageWishlistfolder = 
 1762:         &Apache::loncommon::start_page('New Folder',undef,
 1763:                                       {'only_body' => 1,
 1764:                                        'bgcolor'   => '#FFFFFF',});
 1765: 
 1766:     my $warningFolder = &mt('You must insert a title!');
 1767: 
 1768: 
 1769:     my $inPageNewFolder = '<h1>'.&mt('New Folder').'</h1>'.
 1770:                           '<form method="post" name="newfolder" action="/adm/wishlist" target="wishlist" '.
 1771:                           'onsubmit="return newfoldersubmit();" >'.
 1772:                           &Apache::lonhtmlcommon::start_pick_box().
 1773:                           &Apache::lonhtmlcommon::row_title(&mt('Folder title')).
 1774:                           '<input type="text" name="title" size="45" value="" /><br />'.
 1775:                           &Apache::lonhtmlcommon::row_closure().
 1776:                           &Apache::lonhtmlcommon::row_title(&mt('Note')).
 1777:                           '<textarea name="note" rows="3" cols="35" style="width:100%"></textarea><br />'.
 1778:                           &Apache::lonhtmlcommon::row_closure(1).
 1779:                           &Apache::lonhtmlcommon::end_pick_box().
 1780:                           '<br/><br/>'.
 1781:                           '<input type="submit" value="'.&mt('Save in').'" />'.
 1782:                           '<select name="folders">'.
 1783:                           $options.
 1784:                           '</select>'.
 1785:                           '<input type="button" value="'.&mt('Cancel').'" onclick="javascript:window.close();" />'.
 1786:                           '</form>';
 1787:     my $endPageWishlistfolder = &Apache::loncommon::end_page();
 1788: 
 1789:     my $popUp = $startPageWishlistfolder.
 1790:     $inPageNewFolder;
 1791: 
 1792:     $popUp .= <<SCRIPT;
 1793:     <script type="text\/javascript">
 1794:         function newfoldersubmit(){
 1795:             var title = document.getElementsByName("title")[0].value;
 1796:             if (!title) {
 1797:             alert("$warningFolder");
 1798:             return false;}
 1799:             else {
 1800:             window.close();
 1801:             return true;}}
 1802:     <\/script>
 1803: SCRIPT
 1804: 
 1805:     $popUp .= $endPageWishlistfolder;
 1806: 
 1807:     return $popUp;
 1808: }
 1809: 
 1810: # Returns the HTML-Markup for the page, shown when a link was set
 1811: sub makePageSet {
 1812:     my $title = 'Stored Links';
 1813: 
 1814:     # start_page
 1815:     my $output =
 1816:         &Apache::loncommon::start_page($title,undef,
 1817:                                        {'only_body' => 1})
 1818:        .'<h1>'.&mt($title).'</h1>';
 1819:     
 1820:     # confirm success and offer link to wishlist
 1821:     $output .=
 1822:         &Apache::loncommon::confirmwrapper(
 1823:             &Apache::lonhtmlcommon::confirm_success(
 1824:                 &mt('Link successfully saved!')))
 1825:        .&Apache::lonhtmlcommon::actionbox(
 1826:             ['<a href="javascript:;" onclick="opener.open('."'/adm/wishlist'".');window.close();">'.&mt('Go to Stored Links').'</a>',
 1827:              '<a href="javascript:;" onclick="window.close();">'.&mt('Close this window').'</a>'
 1828:             ]);
 1829: 
 1830:     # end_page 
 1831:     $output .= &Apache::loncommon::end_page();
 1832: 
 1833:     return $output;
 1834: }
 1835: 
 1836: 
 1837: # Returns the HTML-Markup for the page, shown when links should be imported into a course
 1838: sub makePageImport {
 1839:     my $rootgiven = shift;
 1840:     my $rat = shift;
 1841: 
 1842:     $root = $rootgiven;
 1843:     @childrenRt = $root->children();
 1844:     # start_page 
 1845:     my $startPage = &Apache::loncommon::start_page('Stored Links',undef,
 1846:                                                    {'only_body' => 1});
 1847:     
 1848:     # get javascript-code for wishlist-interactions
 1849:     my $js = &JSforWishlist();
 1850:     $js .= &JSforImport($rat);
 1851: 
 1852:     my $inner = '<h1>'.&mt('Import Resources from Stored Links').'</h1>';
 1853:     if (!$rat) {
 1854:         $inner .=
 1855:             '<ul>'.
 1856:             '<li class="LC_info">'.&mt('Use the checkboxes corresponding to a folder to '.
 1857:                 'easily check all links within the folder.').'</li>'.
 1858:             '<li class="LC_info">'.&mt('The folder structure itself cannot be imported.').'</li>'.
 1859:             '<li class="LC_info">'.&mt('All checked links will be imported into the current folder of your course.').'</li>'.
 1860:             '</ul>';
 1861:     }
 1862:     else {
 1863:         $inner .=
 1864:             '<ul>'.
 1865:             '<li class="LC_info">'.&mt('Use the checkboxes corresponding to a folder to '.
 1866:                 'easily check all links within this folder.').'</li>'.
 1867:             '<li class="LC_info">'.&mt('The folder structure itself cannot be imported.').'</li>'.
 1868:             '</ul>';
 1869:     }
 1870:     my %wishlist = &getWishlist();
 1871: 
 1872:     #FIXME Saved string containing all folders in wishlist.db-file (key 'folders') in first version of lonwishlist
 1873:     #After splitting lonwishlist into two modules, this is not necessary anymore. So, dependent from when the wishlist
 1874:     #was first called (i.e. when wishlist.db was created), there might be an entry 'folders' or not. Number of links in
 1875:     #wishlist.db depends on whether this entry exists or not...JW  
 1876:     my $fnum;
 1877:     if (defined $wishlist{'folders'}) {
 1878:         $fnum = (keys(%wishlist))-2;
 1879:     }
 1880:     else {
 1881:         $fnum = (keys(%wishlist))-1;
 1882:     }
 1883: 
 1884:     $inner .= '<form method="post" name="groupsort" action="">'.
 1885:               '<input type="hidden" value="'.$fnum.'" name="fnum" />'.
 1886:               '<input type="button" onclick="javascript:checkAll()" id="checkallbutton" value="'.&mt('Check All').'" />'.
 1887:               '<input type="button" onclick="javascript:uncheckAll()" id="uncheckallbutton" value="'.&mt('Uncheck All').'" />'.
 1888:               '<input type="button" value="'.&mt('Import Checked').'" onclick="finish_import();" />'.    
 1889:               '<input type="button" value="'.&mt('Cancel').'" onclick="window.close();" /><br/><br/>'; 
 1890: 
 1891:     
 1892:     # wishlist-table
 1893:     my $numskipped = 0;
 1894:     &wishlistImport(\@childrenRt,\$numskipped);
 1895:     if ($wishlistHTMLimport ne '') {
 1896:         $inner .= '<table class="LC_data_table LC_tableOfContent">'.$wishlistHTMLimport.'</table>';
 1897:     }
 1898:     else {
 1899:         $inner .= '<span class="LC_info">'.&mt("Your Stored Links list is currently empty.").'</span>';
 1900:     }
 1901:     if ($numskipped > 0) {
 1902:         $inner .= '<p class="LC_info">'.&mt('Note: where a Stored Link is unavailable for import in the current context it is grayed out.').'</p>';
 1903:     }
 1904:     $wishlistHTMLimport = '';
 1905: 
 1906:     $inner .= '</form>';
 1907: 
 1908:     # end_page 
 1909:     my $endPage =  &Apache::loncommon::end_page();
 1910: 
 1911:     # put all page-elements together
 1912:     my $page = $startPage.$js.$inner.$endPage;
 1913: 
 1914:     return $page;
 1915: }
 1916: 
 1917: 
 1918: # Returns the HTML-Markup for error-page
 1919: sub makeErrorPage {
 1920:     # breadcrumbs and start_page 
 1921:     &Apache::lonhtmlcommon::add_breadcrumb(
 1922:               { href => '/adm/wishlist',
 1923:                 text => 'Stored Links'});
 1924:     my $startPage = &Apache::loncommon::start_page('Stored Links');
 1925:     
 1926:     my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs(&mt('Stored Links'),'Wishlist');
 1927:     &Apache::lonhtmlcommon::clear_breadcrumbs();
 1928: 
 1929:     # error-message
 1930:     my $inner .= '<p class="LC_error">'.&mt('An error occurred! Please try again later.').'</p>';
 1931: 
 1932:     # end_page 
 1933:     my $endPage =  &Apache::loncommon::end_page();
 1934: 
 1935:     # put all page-elements together
 1936:     my $page = $startPage.$breadcrumbs.$inner.$endPage;
 1937: 
 1938:     return $page;
 1939: }
 1940: 
 1941: 
 1942: # ----------------------------------------------------- package Tree
 1943: # Extend CPAN-Module Tree by function like 'moveNode' or 'deleteNode'
 1944: package Apache::Tree;
 1945: 
 1946: =pod
 1947: 
 1948: =head2 Routines from package Tree
 1949: 
 1950: =over 4
 1951: 
 1952: =item * &getNodeByIndex(index, nodes)
 1953: 
 1954:      Searches for a node, specified by the index, in nodes (reference to array) and returns it. 
 1955:  
 1956: 
 1957: =item * &moveNode(node, at, newParent)
 1958: 
 1959:      Moves a given node to a new parent (if new parents is defined) or change the position from a node within its siblings (means sorting, at must be defined).
 1960: 
 1961: 
 1962: =item * &removeNode(node)
 1963: 
 1964:      Removes a node given by node from the tree.
 1965: 
 1966: 
 1967: =item * &TreeIndex(children)
 1968: 
 1969:      Sets an index for every node in the tree, beginning with 0.
 1970:      Recursive call starting with all children of the root of the tree (parameter children is reference to an array containing the nodes of the current level).     
 1971: 
 1972: 
 1973: =item * &setCountZero()
 1974: 
 1975:      Resets index counter.
 1976: 
 1977: 
 1978: =item * &RootToHash(childrenRt)
 1979: 
 1980:      Converts the root-node to a hash-entry: the key is root and values are just the indices of root's children.
 1981:    
 1982: 
 1983: =item * &TreeToHash(childrenRt)
 1984: 
 1985:      Converts all other nodes in the tree to hash. Each node is one hash-entry where the keys are the index of a node and the values are all other attributes (containing tile, path, note, date and indices for all direct children).
 1986:      Recursive call starting with all children of the root of the tree (parameter childrenRT is reference to an array containing the nodes of the current level).     
 1987: 
 1988: 
 1989: =item * &HashToTree()
 1990: 
 1991:      Converts the hash to a tree. Builds a tree-object for each entry in the hash. Afterwards call &buildTree(node, childrenIn, TreeNodes, TreeHash) to connect the tree-objects.
 1992: 
 1993: 
 1994: =item * &buildTree(node, childrenIn, TreeNodes, TreeHash)
 1995: 
 1996:      Joins the nodes to a tree.
 1997:      Recursive call starting with root and all children of root (parameter childrenIn is reference to an array containing the nodes indices of the current level).
 1998:    
 1999: 
 2000: =back
 2001: 
 2002: =cut
 2003: 
 2004: 
 2005: # returns the node with a given index from a list of nodes
 2006: sub getNodeByIndex {
 2007:     my $index = shift;
 2008:     my $nodes = shift;
 2009:     my $found;
 2010:     
 2011:     foreach my $n (@$nodes) {
 2012:         my $curIndex = $n->value()->nindex();
 2013:         if ($curIndex == $index) {
 2014:             $found = $n;
 2015:         }
 2016:     }
 2017:     return $found;
 2018: }
 2019: 
 2020: # moves a given node to a new parent or change the position from a node
 2021: # within its siblings (sorting)
 2022: sub moveNode {
 2023:     my $node = shift;
 2024:     my $at = shift;
 2025:     my $newParent = shift;
 2026: 
 2027: 
 2028:     if (!$newParent) {
 2029:         $newParent = $node->parent();
 2030:     }
 2031: 
 2032:     $node->parent()->remove_child($node);
 2033: 
 2034:     if (defined $at) {
 2035:         $newParent->add_child({at => $at},$node);
 2036:     }
 2037:     else {
 2038:         $newParent->add_child($node);
 2039:     }
 2040:     
 2041:     # updating root's children
 2042:     @childrenRt = $root->children();
 2043: }
 2044: 
 2045: # removes a given node
 2046: sub removeNode() {
 2047:     my $node = shift;
 2048:     my @children = $node->children();
 2049: 
 2050:     if ($#children >= 0) {
 2051:         foreach my $c (@children) {
 2052:             &removeNode($c);
 2053:         }
 2054:     }
 2055:     $node->parent()->remove_child($node);
 2056: 
 2057:     # updating root's children
 2058:     @childrenRt = $root->children();
 2059: }
 2060: 
 2061: 
 2062: # set an index for every node in the tree, beginning with 0
 2063: my $count = 0;
 2064: sub TreeIndex {
 2065:     my $children = shift;
 2066: 
 2067:     foreach my $n (@$children) {
 2068:         my @children = $n->children();
 2069:         $n->value()->nindex($count);$count++;
 2070: 
 2071:         if ($#children>=0) {
 2072:             &TreeIndex(\@children);
 2073:         }
 2074:     }
 2075: }
 2076: 
 2077: # reset index counter
 2078: sub setCountZero {
 2079:     $count = 0;
 2080: }
 2081: 
 2082: 
 2083: # convert the tree to a hash
 2084: # each node is one hash-entry
 2085: # keys are the indices, values are all other attributes
 2086: # (containing tile, path, note, date and indices for all direct children)
 2087: # except for root: the key is root and values are
 2088: # just the indices of root's children
 2089: sub RootToHash {
 2090:     my $childrenRt = shift;
 2091:     my @indexarr = ();
 2092: 
 2093:     foreach my $c (@$childrenRt) {
 2094:        push (@indexarr, $c->value()->nindex());
 2095:     }
 2096:     $TreeToHash{'root'} = [@indexarr];
 2097: }
 2098: 
 2099: sub TreeToHash {
 2100:     my $childrenRt = shift;
 2101: 
 2102:     foreach my $n (@$childrenRt) {
 2103:         my @arrtmp = ();
 2104:         $arrtmp[0] = $n->value()->title();
 2105:         $arrtmp[1] = $n->value()->path();
 2106:         $arrtmp[2] = $n->value()->note();
 2107:         $arrtmp[3] = $n->value()->date();
 2108:         my @childrenRt = $n->children();
 2109:         my $co = 4;
 2110:         foreach my $c (@childrenRt) {
 2111:             my $i = $c->value()->nindex();
 2112:             $arrtmp[$co] = $i;
 2113:             $co++;
 2114:         }
 2115:         $TreeToHash{$n->value()->nindex} = [ @arrtmp]; 
 2116:         if ($#childrenRt>=0) {
 2117:             &TreeToHash(\@childrenRt);
 2118:         }
 2119:     }
 2120: }
 2121: 
 2122: 
 2123: # convert the hash to a tree
 2124: # build a tree-object for each entry in the hash
 2125: # afterwards call &buildTree to connect the tree-objects
 2126: sub HashToTree {
 2127:     my $TreeHash = shift;
 2128:     my @TreeNodes = ();
 2129:     my $root;
 2130: 
 2131:     foreach my $key (keys(%$TreeHash)) {
 2132:         if ($key eq 'root') {
 2133:             $root = Tree->new("root");
 2134:         }
 2135:         elsif ($key ne 'folders') {
 2136:         my @attributes = @{ $$TreeHash{$key} };
 2137:         my $tmpNode;
 2138:             $tmpNode = Tree->new(Entry->new(title=>$attributes[0],
 2139:                                             path=>$attributes[1],
 2140:                                             note=>$attributes[2],
 2141:                                             date=>$attributes[3],
 2142:                                             nindex=>$key));
 2143:         push(@TreeNodes, $tmpNode);
 2144:         # shift all attributes except for
 2145:         # the indices representing the children of a node
 2146:         shift(@attributes);
 2147:         shift(@attributes);
 2148:         shift(@attributes);
 2149:         shift(@attributes);
 2150:         $$TreeHash{$key} = [ @attributes ];
 2151:         }
 2152:     }
 2153:     # if there are nodes, build up the tree-structure
 2154:     if (defined $$TreeHash{'root'} && $$TreeHash{'root'} ne '') {
 2155:         my @childrenRtIn = @{ $$TreeHash{'root'} };
 2156:         &buildTree(\$root, \@childrenRtIn,\@TreeNodes,$TreeHash);
 2157:     }
 2158:     return $root; 
 2159: }
 2160: 
 2161: 
 2162: # join the nodes to a tree
 2163: sub buildTree {
 2164:     my ($node, $childrenIn, $TreeNodes, $TreeHash) = @_;
 2165:     bless($node, 'Tree');
 2166:     foreach my $c (@$childrenIn) {
 2167:         my $tmpNode =  &getNodeByIndex($c,$TreeNodes);
 2168:         $$node->add_child($tmpNode);
 2169:         my @childrenIn = @{ $$TreeHash{$tmpNode->value()->nindex()} };
 2170:         &buildTree(\$tmpNode,\@childrenIn,$TreeNodes,$TreeHash);
 2171:     }
 2172: 
 2173: }
 2174: 
 2175: 
 2176: # ----------------------------------------------------- package Entry
 2177: # package that defines the entrys a wishlist could have
 2178: # i.e. folders and links
 2179: package Entry;
 2180: 
 2181: # constructor
 2182: sub new {
 2183:     my $invocant = shift;
 2184:     my $class = ref($invocant) || $invocant;
 2185:     my $self = { @_ }; #set attributes
 2186:     bless($self, $class);
 2187:     return $self;    
 2188: }
 2189: 
 2190: # getter and setter
 2191: sub title {
 2192:     my $self = shift;
 2193:     if ( @_ ) { $self->{title} = shift}
 2194:     return $self->{title};
 2195: }
 2196: 
 2197: sub date {
 2198:     my $self = shift;
 2199:     if ( @_ ) { $self->{date} = shift}
 2200:     return $self->{date};
 2201: }
 2202: 
 2203: sub note {
 2204:     my $self = shift;
 2205:     if ( @_ ) { $self->{note} = shift}
 2206:     return $self->{note};
 2207: }
 2208: 
 2209: sub path {
 2210:     my $self = shift;
 2211:     if ( @_ ) { $self->{path} = shift}
 2212:     return $self->{path};
 2213: }
 2214: 
 2215: sub nindex {
 2216:     my $self = shift;
 2217:     if ( @_ ) { $self->{nindex} = shift}
 2218:     return $self->{nindex};
 2219: }
 2220: 
 2221: 
 2222: 1;
 2223: __END__

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