File:  [LON-CAPA] / loncom / interface / lonwishlist.pm
Revision 1.5: download - view: text, annotated - select for diffs
Tue Aug 17 12:32:58 2010 UTC (13 years, 10 months ago) by wenzelju
Branches: MAIN
CVS tags: HEAD
lonwishlist.pm:
- Only submit "move" if a destination-folder was selected.
Wishlist.tex
- Corrected some little typos in user-help.

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

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