~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/navigationlib.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 * The name that will be used to separate the navigation cache within SESSION
30
30
 */
31
31
define('NAVIGATION_CACHE_NAME', 'navigation');
 
32
define('NAVIGATION_SITE_ADMIN_CACHE_NAME', 'navigationsiteadmin');
32
33
 
33
34
/**
34
35
 * This class is used to represent a node in a navigation tree
71
72
    const TYPE_CUSTOM =     60;
72
73
    /** @var int Setting node type, used only within settings nav 70 */
73
74
    const TYPE_SETTING =    70;
 
75
    /** @var int site admin branch node type, used only within settings nav 71 */
 
76
    const TYPE_SITE_ADMIN = 71;
74
77
    /** @var int Setting node type, used only within settings nav 80 */
75
78
    const TYPE_USER =       80;
76
79
    /** @var int Setting node type, used for containers of no importance 90 */
129
132
    /** @var bool Set to true if we KNOW that this node can be expanded.  */
130
133
    public $isexpandable = false;
131
134
    /** @var array */
132
 
    protected $namedtypes = array(0=>'system',10=>'category',20=>'course',30=>'structure',40=>'activity',50=>'resource',60=>'custom',70=>'setting', 80=>'user');
 
135
    protected $namedtypes = array(0=>'system',10=>'category',20=>'course',30=>'structure',40=>'activity',50=>'resource',60=>'custom',70=>'setting',71=>'siteadmin', 80=>'user');
133
136
    /** @var moodle_url */
134
137
    protected static $fullmeurl = null;
135
138
    /** @var bool toogles auto matching of active node */
136
139
    public static $autofindactive = true;
 
140
    /** @var bool should we load full admin tree or rely on AJAX for performance reasons */
 
141
    protected static $loadadmintree = false;
137
142
    /** @var mixed If set to an int, that section will be included even if it has no activities */
138
143
    public $includesectionnum = false;
139
144
 
237
242
     * is either $PAGE->url or if that hasn't been set $FULLME.
238
243
     *
239
244
     * @param moodle_url $url The url to use for the fullmeurl.
 
245
     * @param bool $loadadmintree use true if the URL point to administration tree
240
246
     */
241
 
    public static function override_active_url(moodle_url $url) {
 
247
    public static function override_active_url(moodle_url $url, $loadadmintree = false) {
242
248
        // Clone the URL, in case the calling script changes their URL later.
243
249
        self::$fullmeurl = new moodle_url($url);
 
250
        // True means we do not want AJAX loaded admin tree, required for all admin pages.
 
251
        if ($loadadmintree) {
 
252
            // Do not change back to false if already set.
 
253
            self::$loadadmintree = true;
 
254
        }
 
255
    }
 
256
 
 
257
    /**
 
258
     * Use when page is linked from the admin tree,
 
259
     * if not used navigation could not find the page using current URL
 
260
     * because the tree is not fully loaded.
 
261
     */
 
262
    public static function require_admin_tree() {
 
263
        self::$loadadmintree = true;
244
264
    }
245
265
 
246
266
    /**
324
344
        // If added node is a category node or the user is logged in and it's a course
325
345
        // then mark added node as a branch (makes it expandable by AJAX)
326
346
        $type = $childnode->type;
327
 
        if (($type == self::TYPE_CATEGORY) || (isloggedin() && ($type == self::TYPE_COURSE)) || ($type == self::TYPE_MY_CATEGORY)) {
 
347
        if (($type == self::TYPE_CATEGORY) || (isloggedin() && ($type == self::TYPE_COURSE)) || ($type == self::TYPE_MY_CATEGORY) ||
 
348
                ($type === self::TYPE_SITE_ADMIN)) {
328
349
            $node->nodetype = self::NODETYPE_BRANCH;
329
350
        }
330
351
        // If this node is hidden mark it's children as hidden also
1252
1273
        }
1253
1274
 
1254
1275
        // Give the local plugins a chance to include some navigation if they want.
1255
 
        foreach (get_plugin_list_with_file('local', 'lib.php', true) as $plugin => $file) {
 
1276
        foreach (core_component::get_plugin_list_with_file('local', 'lib.php', true) as $plugin => $file) {
1256
1277
            $function = "local_{$plugin}_extends_navigation";
1257
1278
            $oldfunction = "{$plugin}_extends_navigation";
1258
1279
            if (function_exists($function)) {
1426
1447
            if (count($fullfetch)) {
1427
1448
                // First up fetch all of the courses in categories where we know that we are going to
1428
1449
                // need the majority of courses.
1429
 
                list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
1430
1450
                list($categoryids, $categoryparams) = $DB->get_in_or_equal($fullfetch, SQL_PARAMS_NAMED, 'lcategory');
 
1451
                $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
 
1452
                $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
 
1453
                $categoryparams['contextlevel'] = CONTEXT_COURSE;
1431
1454
                $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect
1432
1455
                            FROM {course} c
1433
1456
                                $ccjoin
1448
1471
                    if (!$this->can_add_more_courses_to_category($course->category)) {
1449
1472
                        continue;
1450
1473
                    }
1451
 
                    context_instance_preload($course);
 
1474
                    context_helper::preload_from_record($course);
1452
1475
                    if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
1453
1476
                        continue;
1454
1477
                    }
1461
1484
                // Next we will work our way through the categories where we will likely only need a small
1462
1485
                // proportion of the courses.
1463
1486
                foreach ($partfetch as $categoryid) {
1464
 
                    list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
 
1487
                    $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
 
1488
                    $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
1465
1489
                    $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect
1466
1490
                                FROM {course} c
1467
1491
                                    $ccjoin
1468
1492
                                WHERE c.category = :categoryid
1469
1493
                            ORDER BY c.sortorder ASC";
1470
 
                    $courseparams = array('categoryid' => $categoryid);
 
1494
                    $courseparams = array('categoryid' => $categoryid, 'contextlevel' => CONTEXT_COURSE);
1471
1495
                    $coursesrs = $DB->get_recordset_sql($sql, $courseparams, 0, $limit * 5);
1472
1496
                    foreach ($coursesrs as $course) {
1473
1497
                        if ($course->id == $SITE->id) {
1484
1508
                        if (!$this->can_add_more_courses_to_category($course->category)) {
1485
1509
                            break;
1486
1510
                        }
1487
 
                        context_instance_preload($course);
 
1511
                        context_helper::preload_from_record($course);
1488
1512
                        if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
1489
1513
                            continue;
1490
1514
                        }
1495
1519
            }
1496
1520
        } else {
1497
1521
            // Prepare the SQL to load the courses and their contexts
1498
 
            list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
1499
1522
            list($courseids, $courseparams) = $DB->get_in_or_equal(array_keys($this->addedcourses), SQL_PARAMS_NAMED, 'lc', false);
 
1523
            $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
 
1524
            $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
 
1525
            $courseparams['contextlevel'] = CONTEXT_COURSE;
1500
1526
            $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect
1501
1527
                        FROM {course} c
1502
1528
                            $ccjoin
1512
1538
                    // Don't include the currentcourse in this nodelist - it's displayed in the Current course node
1513
1539
                    continue;
1514
1540
                }
1515
 
                context_instance_preload($course);
 
1541
                context_helper::preload_from_record($course);
1516
1542
                if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
1517
1543
                    continue;
1518
1544
                }
1728
1754
        $categoryname = format_string($category->name, true, array('context' => $context));
1729
1755
        $categorynode = $parent->add($categoryname, $url, $nodetype, $categoryname, $category->id);
1730
1756
        if (empty($category->visible)) {
1731
 
            if (has_capability('moodle/category:viewhiddencategories', get_system_context())) {
 
1757
            if (has_capability('moodle/category:viewhiddencategories', context_system::instance())) {
1732
1758
                $categorynode->hidden = true;
1733
1759
            } else {
1734
1760
                $categorynode->display = false;
2177
2203
        }
2178
2204
 
2179
2205
        if (!empty($CFG->messaging)) {
2180
 
            $messageargs = null;
 
2206
            $messageargs = array('user1' => $USER->id);
2181
2207
            if ($USER->id != $user->id) {
2182
 
                $messageargs = array('user1' => $user->id);
 
2208
                $messageargs['user2'] = $user->id;
 
2209
            }
 
2210
            if ($course->id != $SITE->id) {
 
2211
                $messageargs['viewing'] = MESSAGE_VIEW_COURSE. $course->id;
2183
2212
            }
2184
2213
            $url = new moodle_url('/message/index.php',$messageargs);
2185
2214
            $usernode->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages');
2223
2252
        } else if ($course->id == $SITE->id && has_capability('moodle/user:viewdetails', $usercontext) && (!in_array('mycourses', $hiddenfields) || has_capability('moodle/user:viewhiddendetails', $coursecontext))) {
2224
2253
 
2225
2254
            // Add view grade report is permitted
2226
 
            $reports = get_plugin_list('gradereport');
 
2255
            $reports = core_component::get_plugin_list('gradereport');
2227
2256
            arsort($reports); // user is last, we want to test it first
2228
2257
 
2229
2258
            $userscourses = enrol_get_users_courses($user->id);
2482
2511
        $coursenode->add('frontpageloaded', null, self::TYPE_CUSTOM, null, 'frontpageloaded')->display = false;
2483
2512
 
2484
2513
        //Participants
2485
 
        if (has_capability('moodle/course:viewparticipants',  get_system_context())) {
 
2514
        if (has_capability('moodle/course:viewparticipants',  context_system::instance())) {
2486
2515
            $coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id='.$course->id), self::TYPE_CUSTOM, get_string('participants'), 'participants');
2487
2516
        }
2488
2517
 
2719
2748
        $this->rootnodes['site']    = $this->add_course($SITE);
2720
2749
        $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), new moodle_url('/my'), self::TYPE_ROOTNODE, null, 'mycourses');
2721
2750
        $this->rootnodes['courses'] = $this->add(get_string('courses'), null, self::TYPE_ROOTNODE, null, 'courses');
 
2751
        // The courses branch is always displayed, and is always expandable (although may be empty).
 
2752
        // This mimicks what is done during {@link global_navigation::initialise()}.
 
2753
        $this->rootnodes['courses']->isexpandable = true;
2722
2754
 
2723
2755
        // Branchtype will be one of navigation_node::TYPE_*
2724
2756
        switch ($this->branchtype) {
2936
2968
    public $children = array();
2937
2969
    /** @var bool A switch for whether we want to include the root node in the navbar */
2938
2970
    public $includesettingsbase = false;
 
2971
    /** @var navigation_node[] $prependchildren */
 
2972
    protected $prependchildren = array();
2939
2973
    /**
2940
2974
     * The almighty constructor
2941
2975
     *
2971
3005
        $activenodefound = ($this->page->navigation->contains_active_node() ||
2972
3006
                            $this->page->settingsnav->contains_active_node());
2973
3007
 
2974
 
        $outcome = (count($this->children)>0 || (!$this->ignoreactive && $activenodefound));
 
3008
        $outcome = (count($this->children) > 0 || count($this->prependchildren) || (!$this->ignoreactive && $activenodefound));
2975
3009
        $this->hasitems = $outcome;
2976
3010
        return $outcome;
2977
3011
    }
2998
3032
                return $child;
2999
3033
            }
3000
3034
        }
 
3035
        foreach ($this->prependchildren as &$child) {
 
3036
            if ($child->key === $key && ($type == null || $type == $child->type)) {
 
3037
                return $child;
 
3038
            }
 
3039
        }
3001
3040
        return false;
3002
3041
    }
3003
3042
    /**
3017
3056
        }
3018
3057
 
3019
3058
        if (count($this->children) > 0) {
3020
 
            // Add the custom children
 
3059
            // Add the custom children.
3021
3060
            $items = array_reverse($this->children);
3022
3061
        }
3023
3062
 
3081
3120
            'action'=>$this->page->navigation->action
3082
3121
        ));
3083
3122
 
 
3123
        if (count($this->prependchildren) > 0) {
 
3124
            // Add the custom children
 
3125
            $items = array_merge($items, array_reverse($this->prependchildren));
 
3126
        }
 
3127
 
3084
3128
        $this->items = array_reverse($items);
3085
3129
        return $this->items;
3086
3130
    }
3178
3222
        $this->children[] = new navigation_node($itemarray);
3179
3223
        return $this;
3180
3224
    }
 
3225
 
 
3226
    /**
 
3227
     * Prepends a new navigation_node to the start of the navbar
 
3228
     *
 
3229
     * @param string $text
 
3230
     * @param string|moodle_url|action_link $action An action to associate with this node.
 
3231
     * @param int $type One of navigation_node::TYPE_*
 
3232
     * @param string $shorttext
 
3233
     * @param string|int $key A key to identify this node with. Key + type is unique to a parent.
 
3234
     * @param pix_icon $icon An optional icon to use for this node.
 
3235
     * @return navigation_node
 
3236
     */
 
3237
    public function prepend($text, $action=null, $type=self::TYPE_CUSTOM, $shorttext=null, $key=null, pix_icon $icon=null) {
 
3238
        if ($this->content !== null) {
 
3239
            debugging('Nav bar items must be printed before $OUTPUT->header() has been called', DEBUG_DEVELOPER);
 
3240
        }
 
3241
        // Properties array used when creating the new navigation node.
 
3242
        $itemarray = array(
 
3243
            'text' => $text,
 
3244
            'type' => $type
 
3245
        );
 
3246
        // Set the action if one was provided.
 
3247
        if ($action!==null) {
 
3248
            $itemarray['action'] = $action;
 
3249
        }
 
3250
        // Set the shorttext if one was provided.
 
3251
        if ($shorttext!==null) {
 
3252
            $itemarray['shorttext'] = $shorttext;
 
3253
        }
 
3254
        // Set the icon if one was provided.
 
3255
        if ($icon!==null) {
 
3256
            $itemarray['icon'] = $icon;
 
3257
        }
 
3258
        // Default the key to the number of children if not provided.
 
3259
        if ($key === null) {
 
3260
            $key = count($this->children);
 
3261
        }
 
3262
        // Set the key.
 
3263
        $itemarray['key'] = $key;
 
3264
        // Set the parent to this node.
 
3265
        $itemarray['parent'] = $this;
 
3266
        // Add the child node to the prepend list.
 
3267
        $this->prependchildren[] = new navigation_node($itemarray);
 
3268
        return $this;
 
3269
    }
3181
3270
}
3182
3271
 
3183
3272
/**
3245
3334
            $this->load_block_settings();
3246
3335
            $context = $context->get_parent_context();
3247
3336
        }
3248
 
 
3249
3337
        switch ($context->contextlevel) {
3250
3338
            case CONTEXT_SYSTEM:
3251
3339
                if ($this->page->url->compare(new moodle_url('/admin/settings.php', array('section'=>'frontpagesettings')))) {
3273
3361
                break;
3274
3362
        }
3275
3363
 
3276
 
        $settings = $this->load_user_settings($this->page->course->id);
3277
 
 
3278
 
        if (isloggedin() && !isguestuser() && (!property_exists($SESSION, 'load_navigation_admin') || $SESSION->load_navigation_admin)) {
3279
 
            $admin = $this->load_administration_settings();
3280
 
            $SESSION->load_navigation_admin = ($admin->has_children());
3281
 
        } else {
3282
 
            $admin = false;
 
3364
        $usersettings = $this->load_user_settings($this->page->course->id);
 
3365
 
 
3366
        $adminsettings = false;
 
3367
        if (isloggedin() && !isguestuser() && (!isset($SESSION->load_navigation_admin) || $SESSION->load_navigation_admin)) {
 
3368
            $isadminpage = $this->is_admin_tree_needed();
 
3369
 
 
3370
            if (has_capability('moodle/site:config', context_system::instance())) {
 
3371
                // Make sure this works even if config capability changes on the fly
 
3372
                // and also make it fast for admin right after login.
 
3373
                $SESSION->load_navigation_admin = 1;
 
3374
                if ($isadminpage) {
 
3375
                    $adminsettings = $this->load_administration_settings();
 
3376
                }
 
3377
 
 
3378
            } else if (!isset($SESSION->load_navigation_admin)) {
 
3379
                $adminsettings = $this->load_administration_settings();
 
3380
                $SESSION->load_navigation_admin = (int)($adminsettings->children->count() > 0);
 
3381
 
 
3382
            } else if ($SESSION->load_navigation_admin) {
 
3383
                if ($isadminpage) {
 
3384
                    $adminsettings = $this->load_administration_settings();
 
3385
                }
 
3386
            }
 
3387
 
 
3388
            // Print empty navigation node, if needed.
 
3389
            if ($SESSION->load_navigation_admin && !$isadminpage) {
 
3390
                if ($adminsettings) {
 
3391
                    // Do not print settings tree on pages that do not need it, this helps with performance.
 
3392
                    $adminsettings->remove();
 
3393
                    $adminsettings = false;
 
3394
                }
 
3395
                $siteadminnode = $this->add(get_string('administrationsite'), new moodle_url('/admin'), self::TYPE_SITE_ADMIN, null, 'siteadministration');
 
3396
                $siteadminnode->id = 'expandable_branch_'.$siteadminnode->type.'_'.clean_param($siteadminnode->key, PARAM_ALPHANUMEXT);
 
3397
                $this->page->requires->data_for_js('siteadminexpansion', $siteadminnode);
 
3398
            }
3283
3399
        }
3284
3400
 
3285
 
        if ($context->contextlevel == CONTEXT_SYSTEM && $admin) {
3286
 
            $admin->force_open();
3287
 
        } else if ($context->contextlevel == CONTEXT_USER && $settings) {
3288
 
            $settings->force_open();
 
3401
        if ($context->contextlevel == CONTEXT_SYSTEM && $adminsettings) {
 
3402
            $adminsettings->force_open();
 
3403
        } else if ($context->contextlevel == CONTEXT_USER && $usersettings) {
 
3404
            $usersettings->force_open();
3289
3405
        }
3290
3406
 
3291
3407
        // Check if the user is currently logged in as another user
3292
 
        if (session_is_loggedinas()) {
 
3408
        if (\core\session\manager::is_loggedinas()) {
3293
3409
            // Get the actual user, we need this so we can display an informative return link
3294
 
            $realuser = session_get_realuser();
 
3410
            $realuser = \core\session\manager::get_realuser();
3295
3411
            // Add the informative return to original user link
3296
3412
            $url = new moodle_url('/course/loginas.php',array('id'=>$this->page->course->id, 'return'=>1,'sesskey'=>sesskey()));
3297
3413
            $this->add(get_string('returntooriginaluser', 'moodle', fullname($realuser, true)), $url, self::TYPE_SETTING, null, null, new pix_icon('t/left', ''));
3302
3418
 
3303
3419
        foreach ($this->children as $key=>$node) {
3304
3420
            if ($node->nodetype != self::NODETYPE_BRANCH || $node->children->count()===0) {
 
3421
                // Site administration is shown as link.
 
3422
                if (!empty($SESSION->load_navigation_admin) && ($node->type === self::TYPE_SITE_ADMIN)) {
 
3423
                    continue;
 
3424
                }
3305
3425
                $node->remove();
3306
3426
            }
3307
3427
        }
3350
3470
        }
3351
3471
        return $node;
3352
3472
    }
 
3473
 
 
3474
    /**
 
3475
     * Does this page require loading of full admin tree or is
 
3476
     * it enough rely on AJAX?
 
3477
     *
 
3478
     * @return bool
 
3479
     */
 
3480
    protected function is_admin_tree_needed() {
 
3481
        if (self::$loadadmintree) {
 
3482
            // Usually external admin page or settings page.
 
3483
            return true;
 
3484
        }
 
3485
 
 
3486
        if ($this->page->pagelayout === 'admin' or strpos($this->page->pagetype, 'admin-') === 0) {
 
3487
            // Admin settings tree is intended for system level settings and management only, use navigation for the rest!
 
3488
            if ($this->page->context->contextlevel != CONTEXT_SYSTEM) {
 
3489
                return false;
 
3490
            }
 
3491
            return true;
 
3492
        }
 
3493
 
 
3494
        return false;
 
3495
    }
 
3496
 
3353
3497
    /**
3354
3498
     * Load the site administration tree
3355
3499
     *
3377
3521
 
3378
3522
            // Disable the navigation from automatically finding the active node
3379
3523
            navigation_node::$autofindactive = false;
3380
 
            $referencebranch = $this->add(get_string('administrationsite'), null, self::TYPE_SETTING, null, 'root');
 
3524
            $referencebranch = $this->add(get_string('administrationsite'), null, self::TYPE_SITE_ADMIN, null, 'root');
3381
3525
            foreach ($adminroot->children as $adminbranch) {
3382
3526
                $this->load_administration_settings($referencebranch, $adminbranch);
3383
3527
            }
3465
3609
    }
3466
3610
 
3467
3611
    /**
3468
 
     * Generate the list of modules for the given course.
3469
 
     *
3470
 
     * @param stdClass $course The course to get modules for
3471
 
     */
3472
 
    protected function get_course_modules($course) {
3473
 
        global $CFG;
3474
 
        // This function is included when we include course/lib.php at the top
3475
 
        // of this file
3476
 
        $modnames = get_module_types_names();
3477
 
        $resources = array();
3478
 
        $activities = array();
3479
 
        foreach($modnames as $modname=>$modnamestr) {
3480
 
            if (!course_allowed_module($course, $modname)) {
3481
 
                continue;
3482
 
            }
3483
 
 
3484
 
            $libfile = "$CFG->dirroot/mod/$modname/lib.php";
3485
 
            if (!file_exists($libfile)) {
3486
 
                continue;
3487
 
            }
3488
 
            include_once($libfile);
3489
 
            $gettypesfunc =  $modname.'_get_types';
3490
 
            if (function_exists($gettypesfunc)) {
3491
 
                $types = $gettypesfunc();
3492
 
                foreach($types as $type) {
3493
 
                    if (!isset($type->modclass) || !isset($type->typestr)) {
3494
 
                        debugging('Incorrect activity type in '.$modname);
3495
 
                        continue;
3496
 
                    }
3497
 
                    if ($type->modclass == MOD_CLASS_RESOURCE) {
3498
 
                        $resources[html_entity_decode($type->type, ENT_QUOTES, 'UTF-8')] = $type->typestr;
3499
 
                    } else {
3500
 
                        $activities[html_entity_decode($type->type, ENT_QUOTES, 'UTF-8')] = $type->typestr;
3501
 
                    }
3502
 
                }
3503
 
            } else {
3504
 
                $archetype = plugin_supports('mod', $modname, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
3505
 
                if ($archetype == MOD_ARCHETYPE_RESOURCE) {
3506
 
                    $resources[$modname] = $modnamestr;
3507
 
                } else {
3508
 
                    // all other archetypes are considered activity
3509
 
                    $activities[$modname] = $modnamestr;
3510
 
                }
3511
 
            }
3512
 
        }
3513
 
        return array($resources, $activities);
3514
 
    }
3515
 
 
3516
 
    /**
3517
3612
     * This function loads the course settings that are available for the user
3518
3613
     *
3519
3614
     * @param bool $forceopen If set to true the course node will be forced open
3580
3675
        if (has_capability('moodle/site:viewreports', $coursecontext)) { // Basic capability for listing of reports.
3581
3676
            $reportnav = $coursenode->add(get_string('reports'), null, self::TYPE_CONTAINER, null, null,
3582
3677
                    new pix_icon('i/stats', ''));
3583
 
            $coursereports = get_plugin_list('coursereport');
 
3678
            $coursereports = core_component::get_plugin_list('coursereport');
3584
3679
            foreach ($coursereports as $report => $dir) {
3585
3680
                $libfile = $CFG->dirroot.'/course/report/'.$report.'/lib.php';
3586
3681
                if (file_exists($libfile)) {
3603
3698
        if (has_capability('moodle/grade:viewall', $coursecontext)) {
3604
3699
            $reportavailable = true;
3605
3700
        } else if (!empty($course->showgrades)) {
3606
 
            $reports = get_plugin_list('gradereport');
 
3701
            $reports = core_component::get_plugin_list('gradereport');
3607
3702
            if (is_array($reports) && count($reports)>0) {     // Get all installed reports
3608
3703
                arsort($reports); // user is last, we want to test it first
3609
3704
                foreach ($reports as $plugin => $plugindir) {
3749
3844
 
3750
3845
        // Settings for the module
3751
3846
        if (has_capability('moodle/course:manageactivities', $this->page->cm->context)) {
3752
 
            $url = new moodle_url('/course/modedit.php', array('update' => $this->page->cm->id, 'return' => true, 'sesskey' => sesskey()));
 
3847
            $url = new moodle_url('/course/modedit.php', array('update' => $this->page->cm->id, 'return' => 1));
3753
3848
            $modulenode->add(get_string('editsettings'), $url, navigation_node::TYPE_SETTING, null, 'modedit');
3754
3849
        }
3755
3850
        // Assign local roles
3921
4016
        }
3922
4017
 
3923
4018
        $coursecontext = context_course::instance($course->id);   // Course context
3924
 
        $systemcontext   = get_system_context();
 
4019
        $systemcontext   = context_system::instance();
3925
4020
        $currentuser = ($USER->id == $userid);
3926
4021
 
3927
4022
        if ($currentuser) {
4017
4112
        }
4018
4113
 
4019
4114
        // Change password link
4020
 
        if ($userauthplugin && $currentuser && !session_is_loggedinas() && !isguestuser() && has_capability('moodle/user:changeownpassword', $systemcontext) && $userauthplugin->can_change_password()) {
 
4115
        if ($userauthplugin && $currentuser && !\core\session\manager::is_loggedinas() && !isguestuser() && has_capability('moodle/user:changeownpassword', $systemcontext) && $userauthplugin->can_change_password()) {
4021
4116
            $passwordchangeurl = $userauthplugin->change_password_url();
4022
4117
            if (empty($passwordchangeurl)) {
4023
4118
                $passwordchangeurl = new moodle_url('/login/change_password.php', array('id'=>$course->id));
4051
4146
        // Portfolio
4052
4147
        if ($currentuser && !empty($CFG->enableportfolios) && has_capability('moodle/portfolio:export', $systemcontext)) {
4053
4148
            require_once($CFG->libdir . '/portfoliolib.php');
4054
 
            if (portfolio_instances(true, false)) {
 
4149
            if (portfolio_has_visible_instances()) {
4055
4150
                $portfolio = $usersetting->add(get_string('portfolios', 'portfolio'), null, self::TYPE_SETTING);
4056
4151
 
4057
4152
                $url = new moodle_url('/user/portfolio.php', array('courseid'=>$course->id));
4067
4162
            $enablemanagetokens = true;
4068
4163
        } else if (!is_siteadmin($USER->id)
4069
4164
             && !empty($CFG->enablewebservices)
4070
 
             && has_capability('moodle/webservice:createtoken', get_system_context()) ) {
 
4165
             && has_capability('moodle/webservice:createtoken', context_system::instance()) ) {
4071
4166
            $enablemanagetokens = true;
4072
4167
        }
4073
4168
        // Security keys
4135
4230
        $reporttab->trim_if_empty();
4136
4231
 
4137
4232
        // Login as ...
4138
 
        if (!$user->deleted and !$currentuser && !session_is_loggedinas() && has_capability('moodle/user:loginas', $coursecontext) && !is_siteadmin($user->id)) {
 
4233
        if (!$user->deleted and !$currentuser && !\core\session\manager::is_loggedinas() && has_capability('moodle/user:loginas', $coursecontext) && !is_siteadmin($user->id)) {
4139
4234
            $url = new moodle_url('/course/loginas.php', array('id'=>$course->id, 'user'=>$user->id, 'sesskey'=>sesskey()));
4140
4235
            $usersetting->add(get_string('loginas'), $url, self::TYPE_SETTING);
4141
4236
        }
4151
4246
    protected function load_block_settings() {
4152
4247
        global $CFG;
4153
4248
 
4154
 
        $blocknode = $this->add(print_context_name($this->context), null, self::TYPE_SETTING, null, 'blocksettings');
 
4249
        $blocknode = $this->add($this->context->get_context_name(), null, self::TYPE_SETTING, null, 'blocksettings');
4155
4250
        $blocknode->force_open();
4156
4251
 
4157
4252
        // Assign local roles
4180
4275
    protected function load_category_settings() {
4181
4276
        global $CFG;
4182
4277
 
4183
 
        $categorynode = $this->add(print_context_name($this->context), null, null, null, 'categorysettings');
 
4278
        $categorynode = $this->add($this->context->get_context_name(), null, null, null, 'categorysettings');
4184
4279
        $categorynode->force_open();
4185
 
        $onmanagepage = $this->page->url->compare(new moodle_url('/course/manage.php'), URL_MATCH_BASE);
4186
4280
 
4187
 
        if (can_edit_in_category($this->context->instanceid) && !$onmanagepage) {
4188
 
            $url = new moodle_url('/course/manage.php', array('categoryid' => $this->context->instanceid));
 
4281
        if (can_edit_in_category($this->context->instanceid)) {
 
4282
            $url = new moodle_url('/course/management.php', array('categoryid' => $this->context->instanceid));
4189
4283
            $editstring = get_string('managecategorythis');
4190
4284
            $categorynode->add($editstring, $url, self::TYPE_SETTING, null, null, new pix_icon('i/edit', ''));
4191
4285
        }
4216
4310
        }
4217
4311
 
4218
4312
        // Cohorts
4219
 
        if (has_capability('moodle/cohort:manage', $this->context) or has_capability('moodle/cohort:view', $this->context)) {
 
4313
        if (has_any_capability(array('moodle/cohort:view', 'moodle/cohort:manage'), $this->context)) {
4220
4314
            $categorynode->add(get_string('cohorts', 'cohort'), new moodle_url('/cohort/index.php', array('contextid' => $this->context->id)), self::TYPE_SETTING, null, 'cohort', new pix_icon('i/cohort', ''));
4221
4315
        }
4222
4316
 
4305
4399
        if (has_capability('moodle/site:viewreports', $coursecontext)) { // Basic capability for listing of reports.
4306
4400
            $frontpagenav = $frontpage->add(get_string('reports'), null, self::TYPE_CONTAINER, null, null,
4307
4401
                    new pix_icon('i/stats', ''));
4308
 
            $coursereports = get_plugin_list('coursereport');
 
4402
            $coursereports = core_component::get_plugin_list('coursereport');
4309
4403
            foreach ($coursereports as $report=>$dir) {
4310
4404
                $libfile = $CFG->dirroot.'/course/report/'.$report.'/lib.php';
4311
4405
                if (file_exists($libfile)) {
4342
4436
        // Manage files
4343
4437
        if ($course->legacyfiles == 2 and has_capability('moodle/course:managefiles', $this->context)) {
4344
4438
            //hiden in new installs
4345
 
            $url = new moodle_url('/files/index.php', array('contextid'=>$coursecontext->id, 'itemid'=>0, 'component' => 'course', 'filearea'=>'legacy'));
 
4439
            $url = new moodle_url('/files/index.php', array('contextid'=>$coursecontext->id));
4346
4440
            $frontpage->add(get_string('sitelegacyfiles'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/folder', ''));
4347
4441
        }
4348
4442
        return $frontpage;
4368
4462
}
4369
4463
 
4370
4464
/**
 
4465
 * Class used to populate site admin navigation for ajax.
 
4466
 *
 
4467
 * @package   core
 
4468
 * @category  navigation
 
4469
 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
 
4470
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 
4471
 */
 
4472
class settings_navigation_ajax extends settings_navigation {
 
4473
    /**
 
4474
     * Constructs the navigation for use in an AJAX request
 
4475
     *
 
4476
     * @param moodle_page $page
 
4477
     */
 
4478
    public function __construct(moodle_page &$page) {
 
4479
        $this->page = $page;
 
4480
        $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME);
 
4481
        $this->children = new navigation_node_collection();
 
4482
        $this->initialise();
 
4483
    }
 
4484
 
 
4485
    /**
 
4486
     * Initialise the site admin navigation.
 
4487
     *
 
4488
     * @return array An array of the expandable nodes
 
4489
     */
 
4490
    public function initialise() {
 
4491
        if ($this->initialised || during_initial_install()) {
 
4492
            return false;
 
4493
        }
 
4494
        $this->load_administration_settings();
 
4495
 
 
4496
        // Check if local plugins is adding node to site admin.
 
4497
        $this->load_local_plugin_settings();
 
4498
 
 
4499
        $this->initialised = true;
 
4500
    }
 
4501
}
 
4502
 
 
4503
/**
4371
4504
 * Simple class used to output a navigation branch in XML
4372
4505
 *
4373
4506
 * @package   core
4413
4546
        }
4414
4547
        $attributes = array();
4415
4548
        $attributes['id'] = $child->id;
4416
 
        $attributes['name'] = $child->text;
 
4549
        $attributes['name'] = (string)$child->text; // This can be lang_string object so typecast it.
4417
4550
        $attributes['type'] = $child->type;
4418
4551
        $attributes['key'] = $child->key;
4419
4552
        $attributes['class'] = $child->get_css_type();
4659
4792
    public function volatile($setting = true) {
4660
4793
        if (self::$volatilecaches===null) {
4661
4794
            self::$volatilecaches = array();
4662
 
            register_shutdown_function(array('navigation_cache','destroy_volatile_caches'));
 
4795
            core_shutdown_manager::register_function(array('navigation_cache','destroy_volatile_caches'));
4663
4796
        }
4664
4797
 
4665
4798
        if ($setting) {