original_string: public static function convert_text_to_menu_nodes($text, $language = null) {
          $root = new custom_menu();
          $lastitem = $root;
          $lastdepth = 0;
          $hiddenitems = array();
          $lines = explode("\n", $text);
          foreach ($lines as $linenumber => $line) {
              $line = trim($line);
              if (strlen($line) == 0) {
                  continue;
              }
              // Parse item settings.
              $itemtext = null;
              $itemurl = null;
              $itemtitle = null;
              $itemvisible = true;
              $settings = explode('|', $line);
              foreach ($settings as $i => $setting) {
                  $setting = trim($setting);
                  if (!empty($setting)) {
                      switch ($i) {
                          case 0:
                              $itemtext = ltrim($setting, '-');
                              $itemtitle = $itemtext;
                              break;
                          case 1:
                              try {
                                  $itemurl = new moodle_url($setting);
                              } catch (moodle_exception $exception) {
                                  // We're not actually worried about this, we don't want to mess up the display
                                  // just for a wrongly entered URL.
                                  $itemurl = null;
                              }
                              break;
                          case 2:
                              $itemtitle = $setting;
                              break;
                          case 3:
                              if (!empty($language)) {
                                  $itemlanguages = array_map('trim', explode(',', $setting));
                                  $itemvisible &= in_array($language, $itemlanguages);
                              }
                              break;
                      }
                  }
              }
              // Get depth of new item.
              preg_match('/^(\-*)/', $line, $match);
              $itemdepth = strlen($match[1]) + 1;
              // Find parent item for new item.
              while (($lastdepth - $itemdepth) >= 0) {
                  $lastitem = $lastitem->get_parent();
                  $lastdepth--;
              }
              $lastitem = $lastitem->add($itemtext, $itemurl, $itemtitle, $linenumber + 1);
              $lastdepth++;
              if (!$itemvisible) {
                  $hiddenitems[] = $lastitem;
              }
          }
          foreach ($hiddenitems as $item) {
              $item->parent->remove_child($item);
          }
          return $root->get_children();
      }
code: public static function convert_text_to_menu_nodes($text, $language = null) {
          $root = new custom_menu();
          $lastitem = $root;
          $lastdepth = 0;
          $hiddenitems = array();
          $lines = explode("\n", $text);
          foreach ($lines as $linenumber => $line) {
              $line = trim($line);
              if (strlen($line) == 0) {
                  continue;
              }
              // Parse item settings.
              $itemtext = null;
              $itemurl = null;
              $itemtitle = null;
              $itemvisible = true;
              $settings = explode('|', $line);
              foreach ($settings as $i => $setting) {
                  $setting = trim($setting);
                  if (!empty($setting)) {
                      switch ($i) {
                          case 0:
                              $itemtext = ltrim($setting, '-');
                              $itemtitle = $itemtext;
                              break;
                          case 1:
                              try {
                                  $itemurl = new moodle_url($setting);
                              } catch (moodle_exception $exception) {
                                  // We're not actually worried about this, we don't want to mess up the display
                                  // just for a wrongly entered URL.
                                  $itemurl = null;
                              }
                              break;
                          case 2:
                              $itemtitle = $setting;
                              break;
                          case 3:
                              if (!empty($language)) {
                                  $itemlanguages = array_map('trim', explode(',', $setting));
                                  $itemvisible &= in_array($language, $itemlanguages);
                              }
                              break;
                      }
                  }
              }
              // Get depth of new item.
              preg_match('/^(\-*)/', $line, $match);
              $itemdepth = strlen($match[1]) + 1;
              // Find parent item for new item.
              while (($lastdepth - $itemdepth) >= 0) {
                  $lastitem = $lastitem->get_parent();
                  $lastdepth--;
              }
              $lastitem = $lastitem->add($itemtext, $itemurl, $itemtitle, $linenumber + 1);
              $lastdepth++;
              if (!$itemvisible) {
                  $hiddenitems[] = $lastitem;
              }
          }
          foreach ($hiddenitems as $item) {
              $item->parent->remove_child($item);
          }
          return $root->get_children();
      }
code_tokens: public static function convert_text_to_menu_nodes ( $ text , $ language = null ) { $ root = new custom_menu ( ) ; $ lastitem = $ root ; $ lastdepth = 0 ; $ hiddenitems = array ( ) ; $ lines = explode ( "\n" , $ text ) ; foreach ( $ lines as $ linenumber => $ line ) { $ line = trim ( $ line ) ; if ( strlen ( $ line ) == 0 ) { continue ; } // Parse item settings. $ itemtext = null ; $ itemurl = null ; $ itemtitle = null ; $ itemvisible = true ; $ settings = explode ( '|' , $ line ) ; foreach ( $ settings as $ i => $ setting ) { $ setting = trim ( $ setting ) ; if ( ! empty ( $ setting ) ) { switch ( $ i ) { case 0 : $ itemtext = ltrim ( $ setting , '-' ) ; $ itemtitle = $ itemtext ; break ; case 1 : try { $ itemurl = new moodle_url ( $ setting ) ; } catch ( moodle_exception $ exception ) { // We're not actually worried about this, we don't want to mess up the display // just for a wrongly entered URL. $ itemurl = null ; } break ; case 2 : $ itemtitle = $ setting ; break ; case 3 : if ( ! empty ( $ language ) ) { $ itemlanguages = array_map ( 'trim' , explode ( ',' , $ setting ) ) ; $ itemvisible &= in_array ( $ language , $ itemlanguages ) ; } break ; } } } // Get depth of new item. preg_match ( '/^(\-*)/' , $ line , $ match ) ; $ itemdepth = strlen ( $ match [ 1 ] ) + 1 ; // Find parent item for new item. while ( ( $ lastdepth - $ itemdepth ) >= 0 ) { $ lastitem = $ lastitem -> get_parent ( ) ; $ lastdepth -- ; } $ lastitem = $ lastitem -> add ( $ itemtext , $ itemurl , $ itemtitle , $ linenumber + 1 ) ; $ lastdepth ++ ; if ( ! $ itemvisible ) { $ hiddenitems [ ] = $ lastitem ; } } foreach ( $ hiddenitems as $ item ) { $ item -> parent -> remove_child ( $ item ) ; } return $ root -> get_children ( ) ; }
docstring: Converts a string into a structured array of custom_menu_items which can
  then be added to a custom menu.
  
  Structure:
  text|url|title|langs
  The number of hyphens at the start determines the depth of the item. The
  languages are optional, comma separated list of languages the line is for.
  
  Example structure:
  First level first item|http://www.moodle.com/
  -Second level first item|http://www.moodle.com/partners/
  -Second level second item|http://www.moodle.com/hq/
  --Third level first item|http://www.moodle.com/jobs/
  -Second level third item|http://www.moodle.com/development/
  First level second item|http://www.moodle.com/feedback/
  First level third item
  English only|http://moodle.com|English only item|en
  German only|http://moodle.de|Deutsch|de,de_du,de_kids
  
  
  @static
  @param string $text the menu items definition
  @param string $language the language code, null disables multilang support
  @return array
docstring_tokens: Converts a string into a structured array of custom_menu_items which can then be added to a custom menu .