original_string: protected function make_tree_object(array $tree, array $treekey, array $currentkey = []) : Array {
          $newtree = [];
          // Try to extract the context id and then add the context object.
          $addcontext = function($index, $object) {
              if (stripos($index, '_.') !== false) {
                  $namearray = explode('_.', $index);
                  $contextid = array_pop($namearray);
                  if (is_numeric($contextid)) {
                      $object[$index]->name = implode('_.', $namearray);
                      $object[$index]->context = \context::instance_by_id($contextid);
                  }
              } else {
                  $object[$index]->name = $index;
              }
          };
          // Just add the final data to the tree object.
          $addfinalfile = function($directory, $treeleaf, $file) use ($treekey) {
              $url = implode(DIRECTORY_SEPARATOR, $directory);
              $url = clean_param($url, PARAM_PATH);
              $treeleaf->name = $file;
              $treeleaf->itemtype = 'item';
              $gokey = clean_param($url . '/' . $file, PARAM_PATH);
              if (isset($treekey[$gokey]) && $treekey[$gokey] !== 'No var') {
                  $treeleaf->datavar = $treekey[$gokey];
              } else {
                  $treeleaf->url = new \moodle_url($url . '/' . $file);
              }
          };
  
          foreach ($tree as $key => $value) {
              $newtree[$key] = new \stdClass();
              if (is_array($value)) {
                  $newtree[$key]->itemtype = 'treeitem';
                  // The array merge recursive adds a numeric index, and so we only add to the current
                  // key if it is now numeric.
                  $currentkey = is_numeric($key) ? $currentkey : array_merge($currentkey, [$key]);
  
                  // Try to extract the context id and then add the context object.
                  $addcontext($key, $newtree);
                  $newtree[$key]->children = $this->make_tree_object($value, $treekey, $currentkey);
  
                  if (!is_numeric($key)) {
                      // We're heading back down the tree, so remove the last key.
                      array_pop($currentkey);
                  }
              } else {
                  // If the key is not numeric then we want to add a directory and put the file under that.
                  if (!is_numeric($key)) {
                      $newtree[$key]->itemtype = 'treeitem';
                      // Try to extract the context id and then add the context object.
                      $addcontext($key, $newtree);
                       array_push($currentkey, $key);
  
                      $newtree[$key]->children[$value] = new \stdClass();
                      $addfinalfile($currentkey, $newtree[$key]->children[$value], $value);
                      array_pop($currentkey);
                  } else {
                      // If the key is just a number then we just want to show the file instead.
                      $addfinalfile($currentkey, $newtree[$key], $value);
                  }
              }
          }
          return $newtree;
      }
code: protected function make_tree_object(array $tree, array $treekey, array $currentkey = []) : Array {
          $newtree = [];
          // Try to extract the context id and then add the context object.
          $addcontext = function($index, $object) {
              if (stripos($index, '_.') !== false) {
                  $namearray = explode('_.', $index);
                  $contextid = array_pop($namearray);
                  if (is_numeric($contextid)) {
                      $object[$index]->name = implode('_.', $namearray);
                      $object[$index]->context = \context::instance_by_id($contextid);
                  }
              } else {
                  $object[$index]->name = $index;
              }
          };
          // Just add the final data to the tree object.
          $addfinalfile = function($directory, $treeleaf, $file) use ($treekey) {
              $url = implode(DIRECTORY_SEPARATOR, $directory);
              $url = clean_param($url, PARAM_PATH);
              $treeleaf->name = $file;
              $treeleaf->itemtype = 'item';
              $gokey = clean_param($url . '/' . $file, PARAM_PATH);
              if (isset($treekey[$gokey]) && $treekey[$gokey] !== 'No var') {
                  $treeleaf->datavar = $treekey[$gokey];
              } else {
                  $treeleaf->url = new \moodle_url($url . '/' . $file);
              }
          };
  
          foreach ($tree as $key => $value) {
              $newtree[$key] = new \stdClass();
              if (is_array($value)) {
                  $newtree[$key]->itemtype = 'treeitem';
                  // The array merge recursive adds a numeric index, and so we only add to the current
                  // key if it is now numeric.
                  $currentkey = is_numeric($key) ? $currentkey : array_merge($currentkey, [$key]);
  
                  // Try to extract the context id and then add the context object.
                  $addcontext($key, $newtree);
                  $newtree[$key]->children = $this->make_tree_object($value, $treekey, $currentkey);
  
                  if (!is_numeric($key)) {
                      // We're heading back down the tree, so remove the last key.
                      array_pop($currentkey);
                  }
              } else {
                  // If the key is not numeric then we want to add a directory and put the file under that.
                  if (!is_numeric($key)) {
                      $newtree[$key]->itemtype = 'treeitem';
                      // Try to extract the context id and then add the context object.
                      $addcontext($key, $newtree);
                       array_push($currentkey, $key);
  
                      $newtree[$key]->children[$value] = new \stdClass();
                      $addfinalfile($currentkey, $newtree[$key]->children[$value], $value);
                      array_pop($currentkey);
                  } else {
                      // If the key is just a number then we just want to show the file instead.
                      $addfinalfile($currentkey, $newtree[$key], $value);
                  }
              }
          }
          return $newtree;
      }
code_tokens: protected function make_tree_object ( array $ tree , array $ treekey , array $ currentkey = [ ] ) : Array { $ newtree = [ ] ; // Try to extract the context id and then add the context object. $ addcontext = function ( $ index , $ object ) { if ( stripos ( $ index , '_.' ) !== false ) { $ namearray = explode ( '_.' , $ index ) ; $ contextid = array_pop ( $ namearray ) ; if ( is_numeric ( $ contextid ) ) { $ object [ $ index ] -> name = implode ( '_.' , $ namearray ) ; $ object [ $ index ] -> context = \ context :: instance_by_id ( $ contextid ) ; } } else { $ object [ $ index ] -> name = $ index ; } } ; // Just add the final data to the tree object. $ addfinalfile = function ( $ directory , $ treeleaf , $ file ) use ( $ treekey ) { $ url = implode ( DIRECTORY_SEPARATOR , $ directory ) ; $ url = clean_param ( $ url , PARAM_PATH ) ; $ treeleaf -> name = $ file ; $ treeleaf -> itemtype = 'item' ; $ gokey = clean_param ( $ url . '/' . $ file , PARAM_PATH ) ; if ( isset ( $ treekey [ $ gokey ] ) && $ treekey [ $ gokey ] !== 'No var' ) { $ treeleaf -> datavar = $ treekey [ $ gokey ] ; } else { $ treeleaf -> url = new \ moodle_url ( $ url . '/' . $ file ) ; } } ; foreach ( $ tree as $ key => $ value ) { $ newtree [ $ key ] = new \ stdClass ( ) ; if ( is_array ( $ value ) ) { $ newtree [ $ key ] -> itemtype = 'treeitem' ; // The array merge recursive adds a numeric index, and so we only add to the current // key if it is now numeric. $ currentkey = is_numeric ( $ key ) ? $ currentkey : array_merge ( $ currentkey , [ $ key ] ) ; // Try to extract the context id and then add the context object. $ addcontext ( $ key , $ newtree ) ; $ newtree [ $ key ] -> children = $ this -> make_tree_object ( $ value , $ treekey , $ currentkey ) ; if ( ! is_numeric ( $ key ) ) { // We're heading back down the tree, so remove the last key. array_pop ( $ currentkey ) ; } } else { // If the key is not numeric then we want to add a directory and put the file under that. if ( ! is_numeric ( $ key ) ) { $ newtree [ $ key ] -> itemtype = 'treeitem' ; // Try to extract the context id and then add the context object. $ addcontext ( $ key , $ newtree ) ; array_push ( $ currentkey , $ key ) ; $ newtree [ $ key ] -> children [ $ value ] = new \ stdClass ( ) ; $ addfinalfile ( $ currentkey , $ newtree [ $ key ] -> children [ $ value ] , $ value ) ; array_pop ( $ currentkey ) ; } else { // If the key is just a number then we just want to show the file instead. $ addfinalfile ( $ currentkey , $ newtree [ $ key ] , $ value ) ; } } } return $ newtree ; }
docstring: Add more detail to the tree to help with sorting and display in the renderer.
  
  @param  array  $tree       The file structure currently as a multidimensional array.
  @param  array  $treekey    An array of the current file paths.
  @param  array  $currentkey The current short path of the tree.
  @return array An array of objects that has additional data.
docstring_tokens: Add more detail to the tree to help with sorting and display in the renderer .