File:  [LON-CAPA] / loncom / interface / lonwishlist.pm
Revision 1.8: download - view: text, annotated - select for diffs
Wed Aug 25 12:38:45 2010 UTC (13 years, 10 months ago) by wenzelju
Branches: MAIN
CVS tags: HEAD
Save string for folder-dropdown-list (when setting a new link viewing a resource) in user-data. So a call from lonwishlist-routine in lonmenu/lonsearchcat is not necessary (i.e. no issue with modules not supporting Apache::Constants).

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

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