Skip to content

Commit

Permalink
MDL-27809 navigation: Fixed up a couple of minor issues after peer-re…
Browse files Browse the repository at this point in the history
…view
  • Loading branch information
Sam Hemelryk committed Jun 27, 2011
1 parent e26507b commit 3992a46
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 43 deletions.
33 changes: 14 additions & 19 deletions lib/navigationlib.php
Expand Up @@ -1354,13 +1354,6 @@ protected function load_all_courses($categoryids=null) {
$categoryselect = ''; $categoryselect = '';
} }


if (count($this->addedcategories) > 0) {
list($courseselect, $courseparams) = $DB->get_in_or_equal(array_keys($this->addedcourses), SQL_PARAMS_NAMED, 'course', false);
$params += $courseparams;
} else {
$courseselect = '';
}

list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
list($courseids, $courseparams) = $DB->get_in_or_equal(array_keys($this->addedcourses) + array(SITEID), SQL_PARAMS_NAMED, 'lcourse', false); list($courseids, $courseparams) = $DB->get_in_or_equal(array_keys($this->addedcourses) + array(SITEID), SQL_PARAMS_NAMED, 'lcourse', false);
$sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category, cat.path AS categorypath $ccselect $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category, cat.path AS categorypath $ccselect
Expand Down Expand Up @@ -1430,7 +1423,7 @@ protected function load_all_categories($categoryid = null, $showbasecategories =
} else { } else {
// This category hasn't been loaded yet so we need to fetch it, work out its category path // This category hasn't been loaded yet so we need to fetch it, work out its category path
// and load this category plus all its parents and subcategories // and load this category plus all its parents and subcategories
$category = $DB->get_record('course_categories', array('id'=>$categoryid), 'path', MUST_EXIST); $category = $DB->get_record('course_categories', array('id' => $categoryid), 'path', MUST_EXIST);
$coursestoload = explode('/', trim($category->path, '/')); $coursestoload = explode('/', trim($category->path, '/'));
list($select, $params) = $DB->get_in_or_equal($coursestoload); list($select, $params) = $DB->get_in_or_equal($coursestoload);
$select = 'id '.$select.' OR parent '.$select; $select = 'id '.$select.' OR parent '.$select;
Expand Down Expand Up @@ -1963,9 +1956,7 @@ protected function load_for_user($user=null, $forceforcontext=false) {


// Check the number of nodes in the report node... if there are none remove // Check the number of nodes in the report node... if there are none remove
// the node // the node
if (count($reporttab->children)===0) { $reporttab->trim_if_empty();
$usernode->remove_child($reporttab);
}
} }


// If the user is the current user add the repositories for the current user // If the user is the current user add the repositories for the current user
Expand Down Expand Up @@ -2128,13 +2119,17 @@ public function add_course(stdClass $course, $forcegeneric = false, $ismycourse
$url = new moodle_url('/course/view.php', array('id'=>$course->id)); $url = new moodle_url('/course/view.php', array('id'=>$course->id));
} }


if (!$ismycourse && !$issite && !empty($CFG->navshowcategories) && !empty($course->category)) { if (!$ismycourse && !$issite && !empty($course->category)) {
// We need to load the category structure for this course if (!empty($CFG->navshowcategories)) {
$this->load_all_categories($course->category); // We need to load the category structure for this course
$parent = $this->addedcategories[$course->category]; $this->load_all_categories($course->category);
// This could lead to the course being created so we should check whether it is the case again }
if (!$forcegeneric && array_key_exists($course->id, $this->addedcourses)) { if (array_key_exists($course->category, $this->addedcategories)) {
return $this->addedcourses[$course->id]; $parent = $this->addedcategories[$course->category];
// This could lead to the course being created so we should check whether it is the case again
if (!$forcegeneric && array_key_exists($course->id, $this->addedcourses)) {
return $this->addedcourses[$course->id];
}
} }
} }


Expand Down Expand Up @@ -3990,7 +3985,7 @@ protected function convert_child($child, $depth=1) {
$attributes['hidden'] = ($child->hidden); $attributes['hidden'] = ($child->hidden);
$attributes['haschildren'] = ($child->children->count()>0 || $child->type == navigation_node::TYPE_CATEGORY); $attributes['haschildren'] = ($child->children->count()>0 || $child->type == navigation_node::TYPE_CATEGORY);


if (count($child->children)>0) { if ($child->children->count() > 0) {
$attributes['children'] = array(); $attributes['children'] = array();
foreach ($child->children as $subchild) { foreach ($child->children as $subchild) {
$attributes['children'][] = $this->convert_child($subchild, $depth+1); $attributes['children'][] = $this->convert_child($subchild, $depth+1);
Expand Down
28 changes: 4 additions & 24 deletions lib/simpletest/testnavigationlib.php
Expand Up @@ -316,14 +316,10 @@ class global_navigation_test extends UnitTestCase {
* @var global_navigation * @var global_navigation
*/ */
public $node; public $node;
protected $cache;
protected $modinfo5 = 'O:6:"object":6:{s:8:"courseid";s:1:"5";s:6:"userid";s:1:"2";s:8:"sections";a:1:{i:0;a:1:{i:0;s:3:"288";}}s:3:"cms";a:1:{i:288;O:6:"object":17:{s:2:"id";s:3:"288";s:8:"instance";s:2:"19";s:6:"course";s:1:"5";s:7:"modname";s:5:"forum";s:4:"name";s:10:"News forum";s:7:"visible";s:1:"1";s:10:"sectionnum";s:1:"0";s:9:"groupmode";s:1:"0";s:10:"groupingid";s:1:"0";s:16:"groupmembersonly";s:1:"0";s:6:"indent";s:1:"0";s:10:"completion";s:1:"0";s:5:"extra";s:0:"";s:4:"icon";s:0:"";s:11:"uservisible";b:1;s:9:"modplural";s:6:"Forums";s:9:"available";b:1;}}s:9:"instances";a:1:{s:5:"forum";a:1:{i:19;R:8;}}s:6:"groups";N;}';
protected $coursesections5 = 'a:5:{i:0;O:8:"stdClass":6:{s:7:"section";s:1:"0";s:2:"id";s:2:"14";s:6:"course";s:1:"5";s:7:"summary";N;s:8:"sequence";s:3:"288";s:7:"visible";s:1:"1";}i:1;O:8:"stdClass":6:{s:7:"section";s:1:"1";s:2:"id";s:2:"97";s:6:"course";s:1:"5";s:7:"summary";s:0:"";s:8:"sequence";N;s:7:"visible";s:1:"1";}i:2;O:8:"stdClass":6:{s:7:"section";s:1:"2";s:2:"id";s:2:"98";s:6:"course";s:1:"5";s:7:"summary";s:0:"";s:8:"sequence";N;s:7:"visible";s:1:"1";}i:3;O:8:"stdClass":6:{s:7:"section";s:1:"3";s:2:"id";s:2:"99";s:6:"course";s:1:"5";s:7:"summary";s:0:"";s:8:"sequence";N;s:7:"visible";s:1:"1";}i:4;O:8:"stdClass":6:{s:7:"section";s:1:"4";s:2:"id";s:3:"100";s:6:"course";s:1:"5";s:7:"summary";s:0:"";s:8:"sequence";N;s:7:"visible";s:1:"1";}}';
public static $includecoverage = array('./lib/navigationlib.php'); public static $includecoverage = array('./lib/navigationlib.php');
public static $excludecoverage = array(); public static $excludecoverage = array();


public function setUp() { public function setUp() {
$this->cache = new navigation_cache('simpletest_nav');
$this->node = new exposed_global_navigation(); $this->node = new exposed_global_navigation();
// Create an initial tree structure to work with // Create an initial tree structure to work with
$cat1 = $this->node->add('category 1', null, navigation_node::TYPE_CATEGORY, null, 'cat1'); $cat1 = $this->node->add('category 1', null, navigation_node::TYPE_CATEGORY, null, 'cat1');
Expand All @@ -335,33 +331,17 @@ public function setUp() {
$course1 = $sub2->add('course 1', null, navigation_node::TYPE_COURSE, null, 'course1'); $course1 = $sub2->add('course 1', null, navigation_node::TYPE_COURSE, null, 'course1');
$course2 = $sub2->add('course 2', null, navigation_node::TYPE_COURSE, null, 'course2'); $course2 = $sub2->add('course 2', null, navigation_node::TYPE_COURSE, null, 'course2');
$course3 = $sub2->add('course 3', null, navigation_node::TYPE_COURSE, null, 'course3'); $course3 = $sub2->add('course 3', null, navigation_node::TYPE_COURSE, null, 'course3');
$section1 = $course2->add('section 1', null, navigation_node::TYPE_COURSE, null, 'sec1'); $section1 = $course2->add('section 1', null, navigation_node::TYPE_SECTION, null, 'sec1');
$section2 = $course2->add('section 2', null, navigation_node::TYPE_COURSE, null, 'sec2'); $section2 = $course2->add('section 2', null, navigation_node::TYPE_SECTION, null, 'sec2');
$section3 = $course2->add('section 3', null, navigation_node::TYPE_COURSE, null, 'sec3'); $section3 = $course2->add('section 3', null, navigation_node::TYPE_SECTION, null, 'sec3');
$act1 = $section2->add('activity 1', null, navigation_node::TYPE_ACTIVITY, null, 'act1'); $act1 = $section2->add('activity 1', null, navigation_node::TYPE_ACTIVITY, null, 'act1');
$act2 = $section2->add('activity 2', null, navigation_node::TYPE_ACTIVITY, null, 'act2'); $act2 = $section2->add('activity 2', null, navigation_node::TYPE_ACTIVITY, null, 'act2');
$act3 = $section2->add('activity 3', null, navigation_node::TYPE_ACTIVITY, null, 'act3'); $act3 = $section2->add('activity 3', null, navigation_node::TYPE_ACTIVITY, null, 'act3');
$res1 = $section2->add('resource 1', null, navigation_node::TYPE_RESOURCE, null, 'res1'); $res1 = $section2->add('resource 1', null, navigation_node::TYPE_RESOURCE, null, 'res1');
$res2 = $section2->add('resource 2', null, navigation_node::TYPE_RESOURCE, null, 'res2'); $res2 = $section2->add('resource 2', null, navigation_node::TYPE_RESOURCE, null, 'res2');
$res3 = $section2->add('resource 3', null, navigation_node::TYPE_RESOURCE, null, 'res3'); $res3 = $section2->add('resource 3', null, navigation_node::TYPE_RESOURCE, null, 'res3');

$this->cache->clear();
$this->cache->modinfo5 = unserialize($this->modinfo5);
$this->cache->coursesections5 = unserialize($this->coursesections5);
$this->cache->canviewhiddenactivities = true;
$this->cache->canviewhiddensections = true;
$this->cache->canviewhiddencourses = true;
$sub2->add('Test Course 5', new moodle_url('http://moodle.org'),navigation_node::TYPE_COURSE,null,'5');
}
public function test_load_generic_course_sections() {
$coursenode = $this->node->find('5', navigation_node::TYPE_COURSE);
$course = new stdClass;
$course->id = '5';
$course->numsections = 10;
$course->modinfo = $this->modinfo5;
$this->node->load_generic_course_sections($course, $coursenode, 'topic', 'topic');
$this->assertEqual($coursenode->children->count(),1);
} }

public function test_format_display_course_content() { public function test_format_display_course_content() {
$this->assertTrue($this->node->exposed_format_display_course_content('topic')); $this->assertTrue($this->node->exposed_format_display_course_content('topic'));
$this->assertFalse($this->node->exposed_format_display_course_content('scorm')); $this->assertFalse($this->node->exposed_format_display_course_content('scorm'));
Expand Down

0 comments on commit 3992a46

Please sign in to comment.