Merge branch 'wip-MDL-38055-m25' of git://github.com/samhemelryk/moodle
authorDamyon Wiese <damyon@moodle.com>
Tue, 16 Apr 2013 06:32:53 +0000 (14:32 +0800)
committerDamyon Wiese <damyon@moodle.com>
Tue, 16 Apr 2013 06:32:53 +0000 (14:32 +0800)
1  2 
lib/navigationlib.php

diff --combined lib/navigationlib.php
@@@ -677,18 -677,12 +677,18 @@@ class navigation_node implements render
       * Hides the node and any children it has.
       *
       * @since 2.5
 +     * @param array $typestohide Optional. An array of node types that should be hidden.
 +     *      If null all nodes will be hidden.
 +     *      If an array is given then nodes will only be hidden if their type mtatches an element in the array.
 +     *          e.g. array(navigation_node::TYPE_COURSE) would hide only course nodes.
       */
 -    public function hide() {
 -        $this->display = false;
 -        if ($this->has_children()) {
 -            foreach ($this->children as $child) {
 -                $child->hide();
 +    public function hide(array $typestohide = null) {
 +        if ($typestohide === null || in_array($this->type, $typestohide)) {
 +            $this->display = false;
 +            if ($this->has_children()) {
 +                foreach ($this->children as $child) {
 +                    $child->hide($typestohide);
 +                }
              }
          }
      }
@@@ -1699,7 -1693,7 +1699,7 @@@ class global_navigation extends navigat
                  $excessivecategories = $DB->get_records_sql($sql, $params);
                  foreach ($categories as &$category) {
                      if (array_key_exists($category->key, $excessivecategories) && !$this->can_add_more_courses_to_category($category)) {
 -                        $url = new moodle_url('/course/category.php', array('id'=>$category->key));
 +                        $url = new moodle_url('/course/index.php', array('categoryid' => $category->key));
                          $category->add(get_string('viewallcourses'), $url, self::TYPE_SETTING);
                      }
                  }
          if (array_key_exists($category->id, $this->addedcategories)) {
              return;
          }
 -        $url = new moodle_url('/course/category.php', array('id' => $category->id));
 +        $url = new moodle_url('/course/index.php', array('categoryid' => $category->id));
          $context = context_coursecat::instance($category->id);
          $categoryname = format_string($category->name, true, array('context' => $context));
          $categorynode = $parent->add($categoryname, $url, $nodetype, $categoryname, $category->id);
              $usernode->add(get_string('myfiles'), $url, self::TYPE_SETTING);
          }
  
 +        if (!empty($CFG->enablebadges) && $iscurrentuser &&
 +                has_capability('moodle/badges:manageownbadges', context_user::instance($USER->id))) {
 +            $url = new moodle_url('/badges/mybadges.php');
 +            $usernode->add(get_string('mybadges', 'badges'), $url, self::TYPE_SETTING);
 +        }
 +
          // Add a node to view the users notes if permitted
          if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $coursecontext)) {
              $url = new moodle_url('/notes/index.php',array('user'=>$user->id));
          $coursenode = $parent->add($coursename, $url, self::TYPE_COURSE, $shortname, $course->id);
          $coursenode->nodetype = self::NODETYPE_BRANCH;
          $coursenode->hidden = (!$course->visible);
-         $coursenode->title($fullname);
+         // We need to decode &amp;'s here as they will have been added by format_string above and attributes will be encoded again
+         // later.
+         $coursenode->title(str_replace('&amp;', '&', $fullname));
          if (!$forcegeneric) {
              $this->addedcourses[$course->id] = $coursenode;
          }
              $participants = $coursenode->add(get_string('participants'), null, self::TYPE_CONTAINER, get_string('participants'), 'participants');
          }
  
 +        // Badges.
 +        if (!empty($CFG->enablebadges) && has_capability('moodle/badges:viewbadges', $this->page->context)) {
 +            $url = new moodle_url($CFG->wwwroot . '/badges/view.php',
 +                    array('type' => 2, 'id' => $course->id));
 +
 +            $coursenode->add(get_string('coursebadges', 'badges'), null,
 +                    navigation_node::TYPE_CONTAINER, null, 'coursebadges');
 +            $coursenode->get('coursebadges')->add(get_string('badgesview', 'badges'), $url,
 +                    navigation_node::TYPE_SETTING, null, 'badgesview',
 +                    new pix_icon('i/badge', get_string('badgesview', 'badges')));
 +        }
 +
          return true;
      }
      /**
              $coursenode->add(get_string('blogssite','blog'), $blogsurls->out());
          }
  
 +        //Badges
 +        if (!empty($CFG->enablebadges) && has_capability('moodle/badges:viewbadges', $this->page->context)) {
 +            $url = new moodle_url($CFG->wwwroot . '/badges/view.php', array('type' => 1));
 +            $coursenode->add(get_string('sitebadges', 'badges'), $url, navigation_node::TYPE_CUSTOM);
 +        }
 +
          // Notes
          if (!empty($CFG->enablenotes) && (has_capability('moodle/notes:manage', $this->page->context) || has_capability('moodle/notes:view', $this->page->context))) {
              $coursenode->add(get_string('notes','notes'), new moodle_url('/notes/index.php', array('filtertype'=>'course', 'filterselect'=>$filterselect)));
      public function set_expansion_limit($type) {
          global $SITE;
          $nodes = $this->find_all_of_type($type);
 +
 +        // We only want to hide specific types of nodes.
 +        // Only nodes that represent "structure" in the navigation tree should be hidden.
 +        // If we hide all nodes then we risk hiding vital information.
 +        $typestohide = array(
 +            self::TYPE_CATEGORY,
 +            self::TYPE_COURSE,
 +            self::TYPE_SECTION,
 +            self::TYPE_ACTIVITY
 +        );
 +
          foreach ($nodes as $node) {
              // We need to generate the full site node
              if ($type == self::TYPE_COURSE && $node->key == $SITE->id) {
                  continue;
              }
              foreach ($node->children as $child) {
 -                // We still want to show course reports and participants containers
 -                // or there will be navigation missing.
 -                if ($type == self::TYPE_COURSE && $child->type === self::TYPE_CONTAINER) {
 -                    continue;
 -                }
 -                $child->hide();
 +                $child->hide($typestohide);
              }
          }
          return true;
@@@ -2971,7 -2937,6 +2973,7 @@@ class navbar extends navigation_node 
       * @return array
       */
      public function get_items() {
 +        global $CFG;
          $items = array();
          // Make sure that navigation is initialised
          if (!$this->has_items()) {
                      if (!$navigationactivenode->mainnavonly) {
                          $items[] = $navigationactivenode;
                      }
 +                    if (!empty($CFG->navshowcategories) &&
 +                            $navigationactivenode->type === self::TYPE_COURSE &&
 +                            $navigationactivenode->parent->key === 'currentcourse') {
 +                        $items = array_merge($items, $this->get_course_categories());
 +                    }
                      $navigationactivenode = $navigationactivenode->parent;
                  }
              } else if ($navigationactivenode) {
                      if (!$navigationactivenode->mainnavonly) {
                          $items[] = $navigationactivenode;
                      }
 +                    if (!empty($CFG->navshowcategories) &&
 +                            $navigationactivenode->type === self::TYPE_COURSE &&
 +                            $navigationactivenode->parent->key === 'currentcourse') {
 +                        $items = array_merge($items, $this->get_course_categories());
 +                    }
                      $navigationactivenode = $navigationactivenode->parent;
                  }
              } else if ($settingsactivenode) {
          return $this->items;
      }
  
 +    /**
 +     * Get the list of categories leading to this course.
 +     *
 +     * This function is used by {@link navbar::get_items()} to add back the "courses"
 +     * node and category chain leading to the current course.  Note that this is only ever
 +     * called for the current course, so we don't need to bother taking in any parameters.
 +     *
 +     * @return array
 +     */
 +    private function get_course_categories() {
 +        $categories = array();
 +        foreach ($this->page->categories as $category) {
 +            $url = new moodle_url('/course/index.php', array('categoryid' => $category->id));
 +            $name = format_string($category->name, true, array('context' => context_coursecat::instance($category->id)));
 +            $categories[] = navigation_node::create($name, $url, self::TYPE_CATEGORY, null, $category->id);
 +            $id = $category->parent;
 +        }
 +        if (is_enrolled(context_course::instance($this->page->course->id))) {
 +            $courses = $this->page->navigation->get('mycourses');
 +        } else {
 +            $courses = $this->page->navigation->get('courses');
 +        }
 +        if (!$courses) {
 +            // Courses node may not be present.
 +            $courses = navigation_node::create(
 +                get_string('courses'),
 +                new moodle_url('/course/index.php'),
 +                self::TYPE_CONTAINER
 +            );
 +        }
 +        $categories[] = $courses;
 +        return $categories;
 +    }
 +
      /**
       * Add a new navigation_node to the navbar, overrides parent::add
       *
@@@ -3579,10 -3500,6 +3581,10 @@@ class settings_navigation extends navig
              $coursenode->add(get_string('outcomes', 'grades'), $url, self::TYPE_SETTING, null, 'outcomes', new pix_icon('i/outcomes', ''));
          }
  
 +        //Add badges navigation
 +        require_once($CFG->libdir .'/badgeslib.php');
 +        badges_add_course_navigation($coursenode, $course);
 +
          // Backup this course
          if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
              $url = new moodle_url('/backup/backup.php', array('id'=>$course->id));
          $onmanagepage = $this->page->url->compare(new moodle_url('/course/manage.php'), URL_MATCH_BASE);
  
          if (can_edit_in_category($this->context->instanceid) && !$onmanagepage) {
 -            $url = new moodle_url('/course/manage.php', array('id' => $this->context->instanceid));
 +            $url = new moodle_url('/course/manage.php', array('categoryid' => $this->context->instanceid));
              $editstring = get_string('managecategorythis');
              $categorynode->add($editstring, $url, self::TYPE_SETTING, null, null, new pix_icon('i/edit', ''));
          }