Ejemplo n.º 1
0
 /**
  * Recusively converts a child node and its children to XML for output
  *
  * @param navigation_node $child The child to convert
  * @param int $depth Pointlessly used to track the depth of the XML structure
  * @return string JSON
  */
 protected function convert_child($child, $depth = 1)
 {
     if (!$child->display) {
         return '';
     }
     $attributes = array();
     $attributes['id'] = $child->id;
     $attributes['name'] = $child->text;
     $attributes['type'] = $child->type;
     $attributes['key'] = $child->key;
     $attributes['class'] = $child->get_css_type();
     if ($child->icon instanceof pix_icon) {
         $attributes['icon'] = array('component' => $child->icon->component, 'pix' => $child->icon->pix);
         foreach ($child->icon->attributes as $key => $value) {
             if ($key == 'class') {
                 $attributes['icon']['classes'] = explode(' ', $value);
             } else {
                 if (!array_key_exists($key, $attributes['icon'])) {
                     $attributes['icon'][$key] = $value;
                 }
             }
         }
     } else {
         if (!empty($child->icon)) {
             $attributes['icon'] = (string) $child->icon;
         }
     }
     if ($child->forcetitle || $child->title !== $child->text) {
         $attributes['title'] = htmlentities($child->title, ENT_QUOTES, 'UTF-8');
     }
     if (array_key_exists($child->key . ':' . $child->type, $this->expandable)) {
         $attributes['expandable'] = $child->key;
         $child->add_class($this->expandable[$child->key . ':' . $child->type]['id']);
     }
     if (count($child->classes) > 0) {
         $attributes['class'] .= ' ' . join(' ', $child->classes);
     }
     if (is_string($child->action)) {
         $attributes['link'] = $child->action;
     } else {
         if ($child->action instanceof moodle_url) {
             $attributes['link'] = $child->action->out();
         } else {
             if ($child->action instanceof action_link) {
                 $attributes['link'] = $child->action->url->out();
             }
         }
     }
     $attributes['hidden'] = $child->hidden;
     $attributes['haschildren'] = $child->children->count() > 0 || $child->type == navigation_node::TYPE_CATEGORY;
     if ($child->children->count() > 0) {
         $attributes['children'] = array();
         foreach ($child->children as $subchild) {
             $attributes['children'][] = $this->convert_child($subchild, $depth + 1);
         }
     }
     if ($depth > 1) {
         return $attributes;
     } else {
         return json_encode($attributes);
     }
 }