roles MDL-22126 Fixed up usage of admin/roles/tags.php
authorSam Hemelryk <sam@moodle.com>
Wed, 21 Apr 2010 02:43:02 +0000 (02:43 +0000)
committerSam Hemelryk <sam@moodle.com>
Wed, 21 Apr 2010 02:43:02 +0000 (02:43 +0000)
admin/roles/assign.php
admin/roles/check.php
admin/roles/override.php
admin/roles/permissions.php
admin/roles/tabs.php
course/modedit.php
filter/manage.php
lib/navigationlib.php

index 0b330cc..e001c9c 100755 (executable)
@@ -31,41 +31,35 @@ define("MAX_USERS_TO_LIST_PER_ROLE", 10);
 
 $contextid      = required_param('contextid',PARAM_INT);
 $roleid         = optional_param('roleid', 0, PARAM_INT);
-$courseid       = optional_param('courseid', 0, PARAM_INT); // needed for user tabs
 $extendperiod   = optional_param('extendperiod', 0, PARAM_INT);
 $extendbase     = optional_param('extendbase', 3, PARAM_INT);
 
 list($context, $course, $cm) = get_context_info_array($contextid);
 
-$PAGE->set_url('/admin/roles/assign.php', array('contextid' => $contextid));
-$PAGE->set_context($context);
-
-$userid  = 0;
-$tabfile = null;
-
-if ($course) {
-    $isfrontpage = ($context->contextlevel == CONTEXT_COURSE and $context->instanceid == SITEID);
+$url = new moodle_url('/admin/roles/assign.php', array('contextid' => $contextid));
 
-} else {
-    $isfrontpage = false;
+if (!$course) {
     if ($context->contextlevel == CONTEXT_USER) {
-        $courseid = optional_param('courseid', SITEID, PARAM_INT); // needed for user/tabs.php
-        $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
-        $PAGE->url->param('courseid', $courseid);
-        $userid = $context->instanceid;
+        $course = $DB->get_record('course', array('id'=>optional_param('courseid', SITEID, PARAM_INT)), '*', MUST_EXIST);
+        $user = $DB->get_record('user', array('id'=>$context->instanceid), '*', MUST_EXIST);
+        $url->param('courseid', $course->id);
+        $url->param('userid', $user->id);
     } else {
         $course = $SITE;
     }
 }
 
+
 // security
 require_login($course, false, $cm);
 require_capability('moodle/role:assign', $context);
+$PAGE->set_url($url);
 $PAGE->set_context($context);
 
 $contextname = print_context_name($context);
 $courseid = $course->id;
 $inmeta = $course->metacourse;
+$isfrontpage = ($course->id == SITEID);
 
 // These are needed early because of tabs.php
 list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true);
@@ -79,9 +73,6 @@ if ($roleid && !isset($assignableroles[$roleid])) {
     print_error('cannotassignrolehere', '', get_context_url($context), $a);
 }
 
-// Get some language strings
-$straction = get_string('assignroles', 'role'); // Used by tabs.php
-
 // Work out an appropriate page title.
 if ($roleid) {
     $a = new stdClass;
@@ -231,50 +222,49 @@ if ($roleid) {
     }
 }
 
-// Print the header and tabs
-if ($context->contextlevel == CONTEXT_USER) {
-    $user = $DB->get_record('user', array('id'=>$userid));
-    $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
-
-    /// course header
-    $PAGE->set_title($title);
-    if ($courseid != SITEID) {
-        if (has_capability('moodle/course:viewparticipants', get_context_instance(CONTEXT_COURSE, $courseid))) {
-            $PAGE->navbar->add(get_string('participants'), new moodle_url('/user/index.php', array('id'=>$courseid)));
+$PAGE->set_pagelayout('admin');
+$PAGE->set_title($title);
+$tabfile = $CFG->dirroot.'/'.$CFG->admin.'/roles/tabs.php';
+
+switch ($context->contextlevel) {
+    case CONTEXT_SYSTEM:
+        admin_externalpage_setup('assignroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
+        break;
+    case CONTEXT_USER:
+        $tabfile = $CFG->dirroot.'/user/tabs.php';
+        if ($isfrontpage) {
+            $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
+            $PAGE->set_heading($fullname);
+        } else {
+            $PAGE->set_heading($course->fullname);
         }
-        $PAGE->set_heading($fullname);
-    } else {
-        $PAGE->set_heading($course->fullname);
-    }
-    $PAGE->navbar->add($fullname, new moodle_url("$CFG->wwwroot/user/view.php", array('id'=>$userid,'course'=>$courseid)));
-    $PAGE->navbar->add($straction);
-    echo $OUTPUT->header();
-
-    $showroles = 1;
-    $currenttab = 'assign';
-    echo $OUTPUT->header();
-    include($CFG->dirroot.'/user/tabs.php');
-
-} else if ($context->contextlevel == CONTEXT_SYSTEM) {
-    admin_externalpage_setup('assignroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
-    echo $OUTPUT->header();
-
-} else if ($isfrontpage) {
-    admin_externalpage_setup('frontpageroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
-    echo $OUTPUT->header();
-    $currenttab = 'assign';
-    include('tabs.php');
-
-} else {
-    $PAGE->set_title($title);
-    $PAGE->set_heading(print_context_name($context, false));
-    echo $OUTPUT->header();
-    $currenttab = 'assign';
-    include('tabs.php');
+        $showroles = 1;
+        break;
+    case CONTEXT_COURSECAT:
+        $PAGE->set_heading("$SITE->fullname: ".get_string("categories"));
+        break;
+    case CONTEXT_COURSE:
+        if ($isfrontpage) {
+            admin_externalpage_setup('frontpageroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
+        } else {
+            $PAGE->set_heading($course->fullname);
+        }
+        break;
+    case CONTEXT_MODULE:
+        $PAGE->set_heading(print_context_name($context, false));
+        $PAGE->set_cacheable(false);
+        break;
+    case CONTEXT_BLOCK:
+        $PAGE->set_heading($PAGE->course->fullname);
+        break;
 }
 
+$currenttab = 'assign';
+echo $OUTPUT->header();
+include($tabfile);
+
 // Print heading.
-echo $OUTPUT->heading_with_help($title, 'assignroles');
+echo $OUTPUT->heading_with_help($title, 'assignroles', 'role');
 
 if ($roleid) {
     // Show UI for assigning a particular role to users.
@@ -417,7 +407,9 @@ $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
     echo html_writer::table($table);
 
     if ($context->contextlevel > CONTEXT_USER) {
-        echo '<div class="backlink"><a href="' . get_context_url($context) . '">' . get_string('backto', '', $contextname) . '</a></div>';
+        echo html_writer::start_tag('div', array('class'=>'backlink'));
+        echo html_writer::tag('a', get_string('backto', '', $contextname), array('href'=>get_context_url($context)));
+        echo html_writer::end_tag('div');
     }
 }
 
index e12c4cd..435e9cb 100755 (executable)
@@ -30,19 +30,15 @@ require_once($CFG->dirroot . '/' . $CFG->admin . '/roles/lib.php');
 $contextid = required_param('contextid',PARAM_INT);
 
 list($context, $course, $cm) = get_context_info_array($contextid);
-$PAGE->set_url('/admin/roles/check.php', array('contextid' => $contextid));
-$PAGE->set_context($context);
 
-if ($course) {
-    $isfrontpage = ($context->contextlevel == CONTEXT_COURSE and $context->instanceid == SITEID);
+$url = new moodle_url('/admin/roles/check.php', array('contextid' => $contextid));
 
-} else {
-    $isfrontpage = false;
+if (!$course) {
     if ($context->contextlevel == CONTEXT_USER) {
-        $courseid = optional_param('courseid', SITEID, PARAM_INT); // needed for user/tabs.php
-        $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
-        $PAGE->url->param('courseid', $courseid);
-        $userid = $context->instanceid;
+        $course = $DB->get_record('course', array('id'=>optional_param('courseid', SITEID, PARAM_INT)), '*', MUST_EXIST);
+        $user = $DB->get_record('user', array('id'=>$context->instanceid), '*', MUST_EXIST);
+        $url->param('courseid', $course->id);
+        $url->param('userid', $user->id);
     } else {
         $course = $SITE;
     }
@@ -50,18 +46,15 @@ if ($course) {
 
 // security first
 require_login($course, false, $cm);
-$canview = has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride',
-        'moodle/role:override', 'moodle/role:manage'), $context);
-if (!$canview) {
+if (!has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride', 'moodle/role:override', 'moodle/role:manage'), $context)) {
     print_error('nopermissions', 'error', '', get_string('checkpermissions', 'role'));
 }
+$PAGE->set_url($url);
+$PAGE->set_context($context);
 
 $courseid = $course->id;
 $contextname = print_context_name($context);
-
-// These are needed early because of tabs.php
-$assignableroles = get_assignable_roles($context, ROLENAME_BOTH);
-$overridableroles = get_overridable_roles($context, ROLENAME_BOTH);
+$isfrontpage = ($course->id == SITEID);
 
 // Get the user_selector we will need.
 // Teachers within a course just get to see the same list of people they can
@@ -77,48 +70,53 @@ $userselector->set_rows(10);
 
 // Work out an appropriate page title.
 $title = get_string('checkpermissionsin', 'role', $contextname);
-$straction = get_string('checkpermissions', 'role'); // Used by tabs.php
-
-// Print the header and tabs
-if ($context->contextlevel == CONTEXT_USER) {
-    $user = $DB->get_record('user', array('id' => $userid));
-    $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
 
-    $PAGE->set_title($title);
-    if ($courseid != SITEID) {
-        if (has_capability('moodle/course:viewparticipants', get_context_instance(CONTEXT_COURSE, $courseid))) {
-            $PAGE->navbar->add(get_string('participants'), new moodle_url('/user/index.php', array('id'=>$courseid)));
+$PAGE->set_pagelayout('admin');
+$PAGE->set_title($title);
+$tabfile = $CFG->dirroot.'/'.$CFG->admin.'/roles/tabs.php';
+
+switch ($context->contextlevel) {
+    case CONTEXT_SYSTEM:
+        admin_externalpage_setup('assignroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
+        break;
+    case CONTEXT_USER:
+        $tabfile = $CFG->dirroot.'/user/tabs.php';
+        if ($isfrontpage) {
+            $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
+            $PAGE->set_heading($fullname);
+        } else {
+            $PAGE->set_heading($course->fullname);
         }
-        $PAGE->set_heading($fullname);
-    } else {
-        $PAGE->set_heading($course->fullname);
-    }
-    $PAGE->navbar->add($fullname, new moodle_url("$CFG->wwwroot/user/view.php", array('id'=>$userid,'course'=>$courseid)));
-    $PAGE->navbar->add($straction);
-    echo $OUTPUT->header();
-
-    $showroles = 1;
-    $currenttab = 'check';
-    include($CFG->dirroot.'/user/tabs.php');
-
-} else if ($context->contextlevel == CONTEXT_SYSTEM) {
-    admin_externalpage_setup('checkpermissions', '', array('contextid' => $contextid));
-    echo $OUTPUT->header();
-
-} else if ($context->contextlevel == CONTEXT_COURSE and $context->instanceid == SITEID) {
-    admin_externalpage_setup('frontpageroles', '', array('contextid' => $contextid), $CFG->wwwroot . '/' . $CFG->admin . '/roles/check.php');
-    echo $OUTPUT->header();
-    $currenttab = 'check';
-    include('tabs.php');
-
-} else {
-    echo $OUTPUT->header();
-    $currenttab = 'check';
-    include('tabs.php');
+        $showroles = 1;
+        break;
+    case CONTEXT_COURSECAT:
+        $PAGE->set_heading("$SITE->fullname: ".get_string("categories"));
+        break;
+    case CONTEXT_COURSE:
+        if ($isfrontpage) {
+            admin_externalpage_setup('frontpageroles', '', array('contextid' => $contextid), $CFG->wwwroot . '/' . $CFG->admin . '/roles/check.php');
+        } else {
+            $PAGE->set_heading($course->fullname);
+        }
+        break;
+    case CONTEXT_MODULE:
+        $PAGE->set_heading(print_context_name($context, false));
+        $PAGE->set_cacheable(false);
+        break;
+    case CONTEXT_BLOCK:
+        $PAGE->set_heading($PAGE->course->fullname);
+        break;
 }
 
+$currenttab = 'check';
+echo $OUTPUT->header();
+// These are needed early because of tabs.php
+$assignableroles = get_assignable_roles($context, ROLENAME_BOTH);
+$overridableroles = get_overridable_roles($context, ROLENAME_BOTH);
+include($tabfile);
+
 // Print heading.
-echo $OUTPUT->heading_with_help($title, 'checkpermissions');
+echo $OUTPUT->heading_with_help($title, 'checkpermissions', 'role');
 
 // If a user has been chosen, show all the permissions for this user.
 $reportuser = $userselector->get_selected_user();
@@ -141,10 +139,10 @@ echo '<form method="get" action="' . $CFG->wwwroot . '/' . $CFG->admin . '/roles
 
 // Hidden fields.
 echo '<input type="hidden" name="contextid" value="' . $context->id . '" />';
-if (!empty($userid)) {
-    echo '<input type="hidden" name="userid" value="' . $userid . '" />';
+if (!empty($user->id)) {
+    echo '<input type="hidden" name="userid" value="' . $user->id . '" />';
 }
-if ($courseid && $courseid != SITEID) {
+if ($isfrontpage) {
     echo '<input type="hidden" name="courseid" value="' . $courseid . '" />';
 }
 
@@ -159,7 +157,9 @@ echo $OUTPUT->box_end();
 
 // Appropriate back link.
 if ($context->contextlevel > CONTEXT_USER) {
-    echo '<div class="backlink"><a href="' . get_context_url($context) . '">' . get_string('backto', '', $contextname) . '</a></div>';
+    echo html_writer::start_tag('div', array('class'=>'backlink'));
+    echo html_writer::tag('a', get_string('backto', '', $contextname), array('href'=>get_context_url($context)));
+    echo html_writer::end_tag('div');
 }
 
 echo $OUTPUT->footer();
index 3a58eda..da6e181 100755 (executable)
@@ -32,22 +32,14 @@ $roleid    = required_param('roleid', PARAM_INT);   // requested role id
 
 list($context, $course, $cm) = get_context_info_array($contextid);
 
-$PAGE->set_url('/admin/roles/override.php', array('contextid' => $contextid, 'roleid' => $roleid));
-$PAGE->set_context($context);
-
-$userid  = 0;
-$tabfile = null;
+$url = new moodle_url('/admin/roles/override.php', array('contextid' => $contextid, 'roleid' => $roleid));
 
-if ($course) {
-    $isfrontpage = ($context->contextlevel == CONTEXT_COURSE and $context->instanceid == SITEID);
-
-} else {
-    $isfrontpage = false;
+if (!$course) {
     if ($context->contextlevel == CONTEXT_USER) {
-        $courseid = optional_param('courseid', SITEID, PARAM_INT); // needed for user/tabs.php
-        $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
-        $PAGE->url->param('courseid', $courseid);
-        $userid = $context->instanceid;
+        $course = $DB->get_record('course', array('id'=>optional_param('courseid', SITEID, PARAM_INT)), '*', MUST_EXIST);
+        $user = $DB->get_record('user', array('id'=>$context->instanceid), '*', MUST_EXIST);
+        $url->param('courseid', $course->id);
+        $url->param('userid', $user->id);
     } else {
         $course = $SITE;
     }
@@ -55,16 +47,22 @@ if ($course) {
 
 // security first
 require_login($course, false, $cm);
-$safeoverridesonly = !has_capability('moodle/role:override', $context);
-if ($safeoverridesonly) {
+if (!has_capability('moodle/role:override', $context)) {
     require_capability('moodle/role:safeoverride', $context);
 }
+$PAGE->set_url($url);
+$PAGE->set_context($context);
 
 $courseid = $course->id;
+$isfrontpage = ($course->id == SITEID);
 
-$baseurl = $PAGE->url->out();
 $returnurl = new moodle_url('/admin/roles/permissions.php', array('contextid' => $context->id));
 
+// Handle the cancel button.
+if (optional_param('cancel', false, PARAM_BOOL)) {
+    redirect($returnurl);
+}
+
 $role = $DB->get_record('role', array('id'=>$roleid), '*', MUST_EXIST);
 
 // These are needed early because of tabs.php
@@ -77,46 +75,43 @@ $straction = get_string('overrideroles', 'role'); // Used by tabs.php
 $a = (object)array('context' => $contextname, 'role' => $overridableroles[$roleid]);
 $title = get_string('overridepermissionsforrole', 'role', $a);
 
-// Print the header and tabs
-if ($context->contextlevel == CONTEXT_SYSTEM) {
-    print_error('cannotoverridebaserole', 'error');
-
-} else if ($context->contextlevel == CONTEXT_USER) {
-    // NOTE: this is not linked from UI for now
-    $userid = $context->instanceid;
-    $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST);
-    $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
-
-    // course header
-    if ($isfrontpage) {
-        $PAGE->set_heading($course->fullname);
-    } else {
-        if (has_capability('moodle/course:viewparticipants', get_context_instance(CONTEXT_COURSE, $courseid))) {
-            $PAGE->navbar->add(get_string('participants'), new moodle_url('/user/index.php', array('id'=>$courseid)));
+$tabfile = $CFG->dirroot.'/'.$CFG->admin.'/roles/tabs.php';
+$currenttab = 'permissions';
+
+$PAGE->set_pagelayout('admin');
+$PAGE->set_title($title);
+$PAGE->navbar->add($straction);
+switch ($context->contextlevel) {
+    case CONTEXT_SYSTEM:
+        print_error('cannotoverridebaserole', 'error');
+        break;
+    case CONTEXT_USER:
+        $tabfile = $CFG->dirroot.'/user/tabs.php';
+        if ($isfrontpage) {
+            $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
+            $PAGE->set_heading($fullname);
+        } else {
+            $PAGE->set_heading($course->fullname);
         }
-        $PAGE->set_heading($fullname);
-    }
-    $PAGE->navbar->add($fullname, new moodle_url("$CFG->wwwroot/user/view.php", array('id'=>$userid,'course'=>$courseid)));
-    $PAGE->navbar->add($straction);
-
-    $showroles = 1;
-    $currenttab = 'permissions';
-    $tabfile = $CFG->dirroot.'/user/tabs.php';
-
-} else if ($isfrontpage) {
-    admin_externalpage_setup('frontpageroles', '', array(), $PAGE->url);
-    $currenttab = 'permissions';
-    $tabfile = 'tabs.php';
-
-} else {
-    $currenttab = 'permissions';
-    $tabfile = 'tabs.php';
-}
-
-
-// Handle the cancel button.
-if (optional_param('cancel', false, PARAM_BOOL)) {
-    redirect($returnurl);
+        $showroles = 1;
+        break;
+    case CONTEXT_COURSECAT:
+        $PAGE->set_heading("$SITE->fullname: ".get_string("categories"));
+        break;
+    case CONTEXT_COURSE:
+        if ($isfrontpage) {
+            admin_externalpage_setup('frontpageroles', '', array(), $PAGE->url);
+        } else {
+            $PAGE->set_heading($course->fullname);
+        }
+        break;
+    case CONTEXT_MODULE:
+        $PAGE->set_heading(print_context_name($context, false));
+        $PAGE->set_cacheable(false);
+        break;
+    case CONTEXT_BLOCK:
+        $PAGE->set_heading($PAGE->course->fullname);
+        break;
 }
 
 // Make sure this user can override that role
@@ -128,7 +123,7 @@ if (empty($overridableroles[$roleid])) {
 }
 
 // If we are actually overriding a role, create the table object, and save changes if appropriate.
-$overridestable = new override_permissions_table_advanced($context, $roleid, $safeoverridesonly);
+$overridestable = new override_permissions_table_advanced($context, $roleid, false);
 $overridestable->read_submitted_permissions();
 
 if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
@@ -140,10 +135,8 @@ if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
 
 // Finally start page output
 echo $OUTPUT->header();
-if ($tabfile) {
-    include($tabfile);
-}
-echo $OUTPUT->heading_with_help($title, 'overrides');
+include($tabfile);
+echo $OUTPUT->heading_with_help($title, 'overrides', 'role');
 
 // Show UI for overriding roles.
 if (!empty($capabilities)) {
@@ -152,37 +145,32 @@ if (!empty($capabilities)) {
 } else {
     // Print the capabilities overrideable in this context
     echo $OUTPUT->box_start('generalbox capbox');
+    echo html_writer::start_tag('form', array('id'=>'overrideform', 'action'=>$PAGE->url->out(), 'method'=>'post'));
+    echo html_writer::start_tag('div');
+    echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
+    echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'roleid', 'value'=>$roleid));
+    echo html_writer::tag('p', get_string('highlightedcellsshowinherit', 'role'), array('class'=>'overridenotice'));
+    
+    $overridestable->display();
+    if ($overridestable->has_locked_capabiltites()) {
+        echo '<p class="overridenotice">' . get_string('safeoverridenotice', 'role') . "</p>\n";
+    }
 
-    ?>
-<form id="overrideform" action="<?php echo $baseurl ?>" method="post"><div>
-    <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
-    <input type="hidden" name="roleid" value="<?php p($roleid); ?>" />
-            <?php
-
-            echo '<p class="overridenotice">' . get_string('highlightedcellsshowinherit', 'role') . ' </p>';
-            $overridestable->display();
-
-            if ($overridestable->has_locked_capabiltites()) {
-                echo '<p class="overridenotice">' . get_string('safeoverridenotice', 'role') . "</p>\n";
-            }
-
-            ?>
-    <div class="submit buttons">
-        <input type="submit" name="savechanges" value="<?php print_string('savechanges') ?>" />
-        <input type="submit" name="cancel" value="<?php print_string('cancel') ?>" />
-    </div>
-</div></form>
-    <?php
+    echo html_writer::start_tag('div', array('class'=>'submit_buttons'));
+    echo html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'savechanges', 'value'=>get_string('savechanges')));
+    echo html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'cancel', 'value'=>get_string('cancel')));
+    echo html_writer::end_tag('div');
+    echo html_writer::end_tag('div');
+    echo html_writer::end_tag('form');
     echo $OUTPUT->box_end();
-
 }
 
 // Print a form to swap roles, and a link back to the all roles list.
-echo '<div class="backlink">';
-$select = new single_select(new moodle_url($baseurl), 'roleid', $nameswithcounts, $roleid, null);
+echo html_writer::start_tag('div', array('class'=>'backlink'));
+$select = new single_select($PAGE->url, 'roleid', $nameswithcounts, $roleid, null);
 $select->label = get_string('overrideanotherrole', 'role');
 echo $OUTPUT->render($select);
-echo '<p><a href="' . $returnurl . '">' . get_string('backtoallroles', 'role') . '</a></p>';
-echo '</div>';
+echo html_writer::tag('p', html_writer::tag('a', get_string('backtoallroles', 'role'), array('href'=>$returnurl)));
+echo html_writer::end_tag('div');
 
 echo $OUTPUT->footer();
index 062a164..4d131b1 100644 (file)
@@ -40,22 +40,14 @@ $prohibit   = optional_param('prohibit', 0, PARAM_BOOL);
 
 list($context, $course, $cm) = get_context_info_array($contextid);
 
-$PAGE->set_url('/admin/roles/permissions.php', array('contextid' => $contextid));
-$PAGE->set_context($context);
-
-$userid  = 0;
-$tabfile = null;
-
-if ($course) {
-    $isfrontpage = ($context->contextlevel == CONTEXT_COURSE and $context->instanceid == SITEID);
+$url = new moodle_url('/admin/roles/permissions.php', array('contextid' => $contextid));
 
-} else {
-    $isfrontpage = false;
+if (!$course) {
     if ($context->contextlevel == CONTEXT_USER) {
-        $courseid = optional_param('courseid', SITEID, PARAM_INT); // needed for user/tabs.php
-        $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
-        $PAGE->url->param('courseid', $courseid);
-        $userid = $context->instanceid;
+        $course = $DB->get_record('course', array('id'=>optional_param('courseid', SITEID, PARAM_INT)), '*', MUST_EXIST);
+        $user = $DB->get_record('user', array('id'=>$context->instanceid), '*', MUST_EXIST);
+        $url->param('courseid', $course->id);
+        $url->param('userid', $user->id);
     } else {
         $course = $SITE;
     }
@@ -64,14 +56,15 @@ if ($course) {
 // security first
 require_login($course, false, $cm);
 require_capability('moodle/role:review', $context);
-
+$PAGE->set_url($url);
+$PAGE->set_context($context);
 $courseid = $course->id;
+$isfrontpage = ($course->id == SITEID);
 
 
 // These are needed early because of tabs.php
 $assignableroles = get_assignable_roles($context, ROLENAME_BOTH);
 list($overridableroles, $overridecounts, $nameswithcounts) = get_overridable_roles($context, ROLENAME_BOTH, true);
-
 if ($capability) {
     $capability = $DB->get_record('capabilities', array('name'=>$capability), '*', MUST_EXIST);
 }
@@ -82,154 +75,124 @@ $allowsafeoverrides = has_capability('moodle/role:safeoverride', $context);
 $contextname = print_context_name($context);
 $title = get_string('permissionsincontext', 'role', $contextname);
 $straction = get_string('permissions', 'role'); // Used by tabs.php
-$PAGE->set_title($title);
-
-// Print the header and tabs
-if ($context->contextlevel == CONTEXT_SYSTEM) {
-    print_error('cannotoverridebaserole', 'error');
-
-} else if ($context->contextlevel == CONTEXT_USER) {
-    // NOTE: this is not linked from UI for now
-    $userid = $context->instanceid;
-    $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST);
-    $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
-
-    // course header
-    if ($isfrontpage) {
-        $PAGE->set_heading($course->fullname);
-    } else {
-        if (has_capability('moodle/course:viewparticipants', get_context_instance(CONTEXT_COURSE, $courseid))) {
-            $PAGE->navbar->add(get_string('participants'), new moodle_url('/user/index.php', array('id'=>$courseid)));
-        }
-        $PAGE->set_heading($fullname);
-    }
-    $PAGE->navbar->add($fullname, new moodle_url("$CFG->wwwroot/user/view.php", array('id'=>$userid,'course'=>$courseid)));
-    $PAGE->navbar->add($straction);
-
-    $showroles = 1;
-    $currenttab = 'permissions';
-    $tabfile = $CFG->dirroot.'/user/tabs.php';
-
-} else if ($isfrontpage) {
-    admin_externalpage_setup('frontpageroles', '', array(), $PAGE->url);
-    $currenttab = 'permissions';
-    $tabfile = 'tabs.php';
-
-} else {
-    $currenttab = 'permissions';
-    $tabfile = 'tabs.php';
-}
-
-// handle confirmations and actions
-if ($prevent and isset($overridableroles[$roleid]) and $capability) {
-    if ($allowoverrides or ($allowsafeoverrides and is_safe_capability($capability))) {
-        if ($confirm and data_submitted() and confirm_sesskey()) {
-            role_change_permission($roleid, $context, $capability->name, CAP_PREVENT);
-            redirect($PAGE->url);
+$tabfile = $CFG->dirroot.'/'.$CFG->admin.'/roles/tabs.php';
+$currenttab = 'permissions';
 
+$PAGE->set_pagelayout('admin');
+$PAGE->set_title($title);
+switch ($context->contextlevel) {
+    case CONTEXT_SYSTEM:
+        print_error('cannotoverridebaserole', 'error');
+        break;
+    case CONTEXT_USER:
+        $tabfile = $CFG->dirroot.'/user/tabs.php';
+        if ($isfrontpage) {
+            $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
+            $PAGE->set_heading($fullname);
         } else {
-            $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'role'=>$overridableroles[$roleid], 'context'=>$contextname);
-            $message = get_string('confirmroleprevent', 'role', $a);
-            $continueurl = new moodle_url($PAGE->url, array('contextid'=>$context->id, 'roleid'=>$roleid, 'capability'=>$capability->name, 'prevent'=>1, 'sesskey'=>sesskey(), 'confirm'=>1));
-
-            echo $OUTPUT->header();
-            if ($tabfile) {
-                include($tabfile);
-            }
-            echo $OUTPUT->heading($title);
-            echo $OUTPUT->confirm($message, $continueurl, $PAGE->url);
-            echo $OUTPUT->footer();
-            die;
+            $PAGE->set_heading($course->fullname);
         }
-    }
-}
-
-if ($unprohibit and isset($overridableroles[$roleid]) and $capability) {
-    if ($allowoverrides or ($allowsafeoverrides and is_safe_capability($capability))) {
-        if ($confirm and data_submitted() and confirm_sesskey()) {
-            role_change_permission($roleid, $context, $capability->name, CAP_INHERIT);
-            redirect($PAGE->url);
-
+        $showroles = 1;
+        break;
+    case CONTEXT_COURSECAT:
+        $PAGE->set_heading("$SITE->fullname: ".get_string("categories"));
+        break;
+    case CONTEXT_COURSE:
+        if ($isfrontpage) {
+            admin_externalpage_setup('frontpageroles', '', array(), $PAGE->url);
         } else {
-            $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'role'=>$overridableroles[$roleid], 'context'=>$contextname);
-            $message = get_string('confirmroleunprohibit', 'role', $a);
-            $continueurl = new moodle_url($PAGE->url, array('contextid'=>$context->id, 'roleid'=>$roleid, 'capability'=>$capability->name, 'unprohibit'=>1, 'sesskey'=>sesskey(), 'confirm'=>1));
-
-            echo $OUTPUT->header();
-            if ($tabfile) {
-                include($tabfile);
-            }
-            echo $OUTPUT->heading($title);
-            echo $OUTPUT->confirm($message, $continueurl, $PAGE->url);
-            echo $OUTPUT->footer();
-            die;
+            $PAGE->set_heading($course->fullname);
         }
-    }
+        break;
+    case CONTEXT_MODULE:
+        $PAGE->set_heading(print_context_name($context, false));
+        $PAGE->set_cacheable(false);
+        break;
+    case CONTEXT_BLOCK:
+        $PAGE->set_heading($PAGE->course->fullname);
+        break;
 }
 
-if ($allow and $capability) {
-    if ($allowoverrides or ($allowsafeoverrides and is_safe_capability($capability))) {
-        $mform = new role_allow_form(null, array($context, $capability, $overridableroles));
-        if ($mform->is_cancelled()) {
-            redirect($PAGE->url);
-
-        } else if ($data = $mform->get_data() and !empty($data->roleid)) {
-            $roleid = $data->roleid;
-            if (isset($overridableroles[$roleid])) {
-                role_change_permission($roleid, $context, $capability->name, CAP_ALLOW);
+// handle confirmations and actions
+// We have a capability and overrides are allowed or safe overrides are allowed and this is safe
+if ($capability && ($allowoverrides || ($allowsafeoverrides && is_safe_capability($capability)))) {
+    // If we already know the the role ID, it is overrideable, and we are setting prevent or unprohibit
+    if (isset($overridableroles[$roleid]) && ($prevent || $unprohibit)) {
+        // We are preventing
+        if ($prevent) {
+            if ($confirm && data_submitted() && confirm_sesskey()) {
+                role_change_permission($roleid, $context, $capability->name, CAP_PREVENT);
+                redirect($PAGE->url);
+
+            } else {
+                $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'role'=>$overridableroles[$roleid], 'context'=>$contextname);
+                $message = get_string('confirmroleprevent', 'role', $a);
+                $continueurl = new moodle_url($PAGE->url, array('contextid'=>$context->id, 'roleid'=>$roleid, 'capability'=>$capability->name, 'prevent'=>1, 'sesskey'=>sesskey(), 'confirm'=>1));
             }
-            redirect($PAGE->url);
-
-        } else {
-            $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'context'=>$contextname);
-            $message = get_string('roleallowinfo', 'role', $a);
-
-            echo $OUTPUT->header();
-            if ($tabfile) {
-                include($tabfile);
+        }
+        // We are unprohibiting
+        if ($unprohibit) {
+            if ($confirm && data_submitted() && confirm_sesskey()) {
+                role_change_permission($roleid, $context, $capability->name, CAP_INHERIT);
+                redirect($PAGE->url);
+            } else {
+                $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'role'=>$overridableroles[$roleid], 'context'=>$contextname);
+                $message = get_string('confirmroleunprohibit', 'role', $a);
+                $continueurl = new moodle_url($PAGE->url, array('contextid'=>$context->id, 'roleid'=>$roleid, 'capability'=>$capability->name, 'unprohibit'=>1, 'sesskey'=>sesskey(), 'confirm'=>1));
             }
-            echo $OUTPUT->heading($title);
-            echo $OUTPUT->box($message);
-            $mform->display();
-            echo $OUTPUT->footer();
-            die;
         }
+        // Display and print
+        echo $OUTPUT->header();
+        include($tabfile);
+        echo $OUTPUT->heading($title);
+        echo $OUTPUT->confirm($message, $continueurl, $PAGE->url);
+        echo $OUTPUT->footer();
+        die;
     }
-}
-
-if ($prohibit and $capability) {
-    if ($allowoverrides or ($allowsafeoverrides and is_safe_capability($capability))) {
-        $mform = new role_prohibit_form(null, array($context, $capability, $overridableroles));
-        if ($mform->is_cancelled()) {
-            redirect($PAGE->url);
 
-        } else if ($data = $mform->get_data() and !empty($data->roleid)) {
-            $roleid = $data->roleid;
-            if (isset($overridableroles[$roleid])) {
-                role_change_permission($roleid, $context, $capability->name, CAP_PROHIBIT);
+    if ($allow || $prohibit) {
+        if ($allow) {
+            $mform = new role_allow_form(null, array($context, $capability, $overridableroles));
+            if ($mform->is_cancelled()) {
+                redirect($PAGE->url);
+            } else if ($data = $mform->get_data() and !empty($data->roleid)) {
+                $roleid = $data->roleid;
+                if (isset($overridableroles[$roleid])) {
+                    role_change_permission($roleid, $context, $capability->name, CAP_ALLOW);
+                }
+                redirect($PAGE->url);
+            } else {
+                $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'context'=>$contextname);
+                $message = get_string('roleallowinfo', 'role', $a);
             }
-            redirect($PAGE->url);
-
-        } else {
-            $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'context'=>$contextname);
-            $message = get_string('roleprohibitinfo', 'role', $a);
-
-            echo $OUTPUT->header();
-            if ($tabfile) {
-                include($tabfile);
+        }
+        if ($prohibit) {
+            $mform = new role_prohibit_form(null, array($context, $capability, $overridableroles));
+            if ($mform->is_cancelled()) {
+                redirect($PAGE->url);
+            } else if ($data = $mform->get_data() and !empty($data->roleid)) {
+                $roleid = $data->roleid;
+                if (isset($overridableroles[$roleid])) {
+                    role_change_permission($roleid, $context, $capability->name, CAP_PROHIBIT);
+                }
+                redirect($PAGE->url);
+            } else {
+                $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'context'=>$contextname);
+                $message = get_string('roleprohibitinfo', 'role', $a);
             }
-            echo $OUTPUT->box($message);
-            $mform->display();
-            echo $OUTPUT->footer();
-            die;
         }
+        echo $OUTPUT->header();
+        include($tabfile);
+        echo $OUTPUT->heading($title);
+        echo $OUTPUT->box($message);
+        $mform->display();
+        echo $OUTPUT->footer();
+        die;
     }
 }
 
 echo $OUTPUT->header();
-if ($tabfile) {
-    include($tabfile);
-}
+include($tabfile);
 echo $OUTPUT->heading($title);
 
 $table = new permissions_table($context, $contextname, $allowoverrides, $allowsafeoverrides, $overridableroles);
@@ -239,14 +202,16 @@ if ($overridableroles) {
     $overrideurl = new moodle_url('/admin/roles/override.php', array('contextid' => $context->id));
     $select = new single_select($overrideurl, 'roleid', $nameswithcounts);
     $select->label = get_string('advancedoverride', 'role');
-    echo '<div class="advancedoverride">'.$OUTPUT->render($select).'</div>';
+    echo html_writer::tag('div', $OUTPUT->render($select), array('class'=>'advancedoverride'));
 }
 $table->display();
 echo $OUTPUT->box_end();
 
 
 if ($context->contextlevel > CONTEXT_USER) {
-    echo '<div class="backlink"><a href="' . get_context_url($context) . '">' . get_string('backto', '', $contextname) . '</a></div>';
+    echo html_writer::start_tag('div', array('class'=>'backlink'));
+    echo html_writer::tag('a', get_string('backto', '', $contextname), array('href'=>get_context_url($context)));
+    echo html_writer::end_tag('div');
 }
 
 echo $OUTPUT->footer($course);
index 6dac7bd..6c69a06 100755 (executable)
@@ -38,90 +38,6 @@ if (!isset($availablefilters)) {
     }
 }
 
-if ($currenttab != 'update') {
-    switch ($context->contextlevel) {
-
-        case CONTEXT_SYSTEM:
-            $stradministration = get_string('administration');
-            if (empty($title)) {
-                $title = $SITE->fullname;
-            }
-            $PAGE->navbar->add($stradministration, new moodle_url('/admin/'), navigation_node::TYPE_SETTING);
-            $PAGE->navbar->add($straction);
-            $PAGE->set_title($title);
-            $PAGE->set_heading($SITE->fullname);
-            break;
-
-        case CONTEXT_USER:
-            break;
-
-        case CONTEXT_COURSECAT:
-            $category = $DB->get_record('course_categories', array('id'=>$context->instanceid));
-            $strcategories = get_string("categories");
-            $strcategory = get_string("category");
-            $strcourses = get_string("courses");
-
-            if (empty($title)) {
-                $title = "$SITE->shortname: $category->name";
-            }
-
-            $PAGE->navbar->add($strcategories, new moodle_url('/course/index.php'), navigation_node::TYPE_SETTING);
-            $PAGE->navbar->add($category->name, new moodle_url('/course/category.php', array('id'=>$category->id)), navigation_node::TYPE_SETTING);
-            $PAGE->navbar->add(get_string("roles"));
-            $PAGE->set_title($title);
-            $PAGE->set_heading("$SITE->fullname: $strcourses");
-            break;
-
-        case CONTEXT_COURSE:
-            if ($context->instanceid != SITEID) {
-                $course = $DB->get_record('course', array('id'=>$context->instanceid));
-
-                if (empty($title)) {
-                    $title = get_string("editcoursesettings");
-                }
-                $roleslink = new moodle_url("$CFG->wwwroot/$CFG->admin/roles/assign.php", array('contextid'=>$context->id));
-                $PAGE->navbar->add(get_string('roles'), $roleslink, navigation_node::TYPE_SETTING);
-                $PAGE->set_title($title);
-                $PAGE->set_heading($course->fullname);
-            }
-            break;
-
-        case CONTEXT_MODULE:
-            if (!$cm = get_coursemodule_from_id('', $context->instanceid)) {
-                print_error('invalidcoursemodule', 'error');
-            }
-            if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
-                print_error('invalidcourse');
-            }
-
-            $PAGE->navigation->add(get_string('roles'));
-
-            if (empty($title)) {
-                $title = get_string("editinga", "moodle", $fullmodulename);
-            }
-            $PAGE->set_title($title);
-            $PAGE->set_cacheable(false);
-            break;
-
-        case CONTEXT_BLOCK:
-            if ($blockinstance = $DB->get_record('block_instances', array('id' => $context->instanceid))) {
-                $blockname = print_context_name($context);
-
-                $PAGE->navbar->add($blockname);
-                $PAGE->navbar->add($straction);
-                $PAGE->set_title("$straction: $blockname");
-                $PAGE->set_heading($PAGE->course->fullname);
-            }
-            break;
-
-        default:
-            print_error('unknowncontext');
-            return false;
-
-    }
-}
-
-
 $toprow = array();
 $inactive = array();
 $activetwo = array();
@@ -132,27 +48,23 @@ $permissionsrow = array();
 if ($context->contextlevel != CONTEXT_SYSTEM) {    // Print tabs for anything except SYSTEM context
 
     if ($context->contextlevel == CONTEXT_MODULE) {  // Only show update button if module
-        $toprow[] = new tabobject('update', $CFG->wwwroot.'/course/mod.php?update='.
-                        $context->instanceid.'&amp;return=true&amp;sesskey='.sesskey(), get_string('settings'));
+        $url = new moodle_url('/course/mod.php', array('update'=>$context->instanceid, 'return'=>'true', 'sesskey'=>sesskey()));
+        $toprow[] = new tabobject('update', $url, get_string('settings'));
     }
 
     if (!empty($assignableroles) || $currenttab=='assign') {
-        $toprow[] = new tabobject('assign',
-                new moodle_url('/admin/roles/assign.php', array('contextid'=>$context->id)),
-                get_string('localroles', 'role'), '', true);
+        $url = new moodle_url('/admin/roles/assign.php', array('contextid'=>$context->id));
+        $toprow[] = new tabobject('assign', $url, get_string('localroles', 'role'), '', true);
     }
 
     if (has_capability('moodle/role:review', $context) or !empty($overridableroles)) {
-        $permissionsrow['permissions'] = new tabobject('permissions',
-                new moodle_url('/admin/roles/permissions.php', array('contextid'=>$context->id)),
-                get_string('permissions', 'role'), '', true);
+        $url = new moodle_url('/admin/roles/permissions.php', array('contextid'=>$context->id));
+        $permissionsrow['permissions'] = new tabobject('permissions', $url, get_string('permissions', 'role'), '', true);
     }
 
-    if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride',
-            'moodle/role:override', 'moodle/role:assign'), $context)) {
-        $permissionsrow['check'] = new tabobject('check',
-                new moodle_url('/admin/roles/check.php', array('contextid'=>$context->id)),
-                get_string('checkpermissions', 'role'));
+    if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride', 'moodle/role:override', 'moodle/role:assign'), $context)) {
+        $url = new moodle_url('/admin/roles/check.php', array('contextid'=>$context->id));
+        $permissionsrow['check'] = new tabobject('check', $url, get_string('checkpermissions', 'role'));
     }
 
     if ($permissionsrow) {
@@ -166,9 +78,8 @@ if ($context->contextlevel != CONTEXT_SYSTEM) {    // Print tabs for anything ex
     }
 
     if (!empty($availablefilters)) {
-        $toprow[] = new tabobject('filters',
-                new moodle_url('/filter/manage.php', array('contextid'=>$context->id)),
-                get_string('filters', 'admin'));
+        $url = new moodle_url('/filter/manage.php', array('contextid'=>$context->id));
+        $toprow[] = new tabobject('filters', $url, get_string('filters', 'admin'));
     }
 }
 unset($permissionsrow);
index 2d8b976..cf6109c 100644 (file)
 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 
 /**
- * Adds or updates modules in a course using new formslib
- *
- * @package    moodlecore
- * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
- * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-    require_once("../config.php");
-    require_once("lib.php");
-    require_once($CFG->libdir.'/filelib.php');
-    require_once($CFG->libdir.'/gradelib.php');
-    require_once($CFG->libdir.'/completionlib.php');
-    require_once($CFG->libdir.'/conditionlib.php');
-
-    $add    = optional_param('add', '', PARAM_ALPHA);     // module name
-    $update = optional_param('update', 0, PARAM_INT);
-    $return = optional_param('return', 0, PARAM_BOOL);    //return to course/view.php if false or mod/modname/view.php if true
-    $type   = optional_param('type', '', PARAM_ALPHANUM); //TODO: hopefully will be removed in 2.0
-
-    $url = new moodle_url('/course/modedit.php');
-    if (!empty($add)) {
-        $url->param('add', $add);
-    } else if (!empty($update)) {
-        $url->param('update', $update);
-    } else {
-        debugging('Neither add nor update arguments were provided, PAGE->set_url needs to be checked', DEBUG_DEVELOPER);
-    }
-    if (!empty($return)) {
-        $url->param('return', $return);
-    }
-    if (!empty($type)) {
-        $url->param('type', $type);
-    }
+* Adds or updates modules in a course using new formslib
+*
+* @package    moodlecore
+* @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
+* @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+*/
+
+require_once("../config.php");
+require_once("lib.php");
+require_once($CFG->libdir.'/filelib.php');
+require_once($CFG->libdir.'/gradelib.php');
+require_once($CFG->libdir.'/completionlib.php');
+require_once($CFG->libdir.'/conditionlib.php');
+
+$add    = optional_param('add', '', PARAM_ALPHA);     // module name
+$update = optional_param('update', 0, PARAM_INT);
+$return = optional_param('return', 0, PARAM_BOOL);    //return to course/view.php if false or mod/modname/view.php if true
+$type   = optional_param('type', '', PARAM_ALPHANUM); //TODO: hopefully will be removed in 2.0
+
+$url = new moodle_url('/course/modedit.php');
+if (!empty($return)) {
+    $url->param('return', $return);
+}
+
+if (!empty($add)) {
+    $url->param('add', $add);
     $PAGE->set_url($url);
+    
+    $section = required_param('section', PARAM_INT);
+    $course  = required_param('course', PARAM_INT);
 
-    if (!empty($add)) {
-        $section = required_param('section', PARAM_INT);
-        $course  = required_param('course', PARAM_INT);
-
-        if (!$course = $DB->get_record('course', array('id'=>$course))) {
-            print_error('invalidcourseid');
-        }
-
-        require_login($course);
-        $context = get_context_instance(CONTEXT_COURSE, $course->id);
-        require_capability('moodle/course:manageactivities', $context);
-
-        if (!$module = $DB->get_record('modules', array('name'=>$add))) {
-            print_error('moduledoesnotexist');
-        }
-
-        $cw = get_course_section($section, $course->id);
+    $course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST);
+    $module = $DB->get_record('modules', array('name'=>$add), '*', MUST_EXIST);
 
-        if (!course_allowed_module($course, $module->id)) {
-            print_error('moduledisable');
-        }
-
-        $cm = null;
-
-        $data = new object();
-        $data->section          = $section;  // The section number itself - relative!!! (section column in course_sections)
-        $data->visible          = $cw->visible;
-        $data->course           = $course->id;
-        $data->module           = $module->id;
-        $data->modulename       = $module->name;
-        $data->groupmode        = $course->groupmode;
-        $data->groupingid       = $course->defaultgroupingid;
-        $data->groupmembersonly = 0;
-        $data->id               = '';
-        $data->instance         = '';
-        $data->coursemodule     = '';
-        $data->add              = $add;
-        $data->return           = 0; //must be false if this is an add, go back to course view on cancel
-
-        if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
-            $draftid_editor = file_get_submitted_draft_itemid('introeditor');
-            file_prepare_draft_area($draftid_editor, null, null, null);
-            $data->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default
-        }
-
-        if (!empty($type)) { //TODO: hopefully will be removed in 2.0
-            $data->type = $type;
-        }
+    require_login($course);
+    $context = get_context_instance(CONTEXT_COURSE, $course->id);
+    require_capability('moodle/course:manageactivities', $context);
 
-        $sectionname = get_section_name($course->format);
-        $fullmodulename = get_string('modulename', $module->name);
+    $cw = get_course_section($section, $course->id);
 
-        if ($data->section && $course->format != 'site') {
-            $heading = new object();
-            $heading->what = $fullmodulename;
-            $heading->to   = "$sectionname $data->section";
-            $pageheading = get_string('addinganewto', 'moodle', $heading);
-        } else {
-            $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
-        }
-
-        $pagepath = 'mod-' . $module->name . '-';
-        if (!empty($type)) { //TODO: hopefully will be removed in 2.0
-            $pagepath .= $type;
-        } else {
-            $pagepath .= 'mod';
-        }
-        $PAGE->set_pagetype($pagepath);
+    if (!course_allowed_module($course, $module->id)) {
+        print_error('moduledisable');
+    }
 
-        $navlinksinstancename = '';
+    $cm = null;
+
+    $data = new object();
+    $data->section          = $section;  // The section number itself - relative!!! (section column in course_sections)
+    $data->visible          = $cw->visible;
+    $data->course           = $course->id;
+    $data->module           = $module->id;
+    $data->modulename       = $module->name;
+    $data->groupmode        = $course->groupmode;
+    $data->groupingid       = $course->defaultgroupingid;
+    $data->groupmembersonly = 0;
+    $data->id               = '';
+    $data->instance         = '';
+    $data->coursemodule     = '';
+    $data->add              = $add;
+    $data->return           = 0; //must be false if this is an add, go back to course view on cancel
+
+    if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
+        $draftid_editor = file_get_submitted_draft_itemid('introeditor');
+        file_prepare_draft_area($draftid_editor, null, null, null);
+        $data->introeditor = array('text'=>'', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor); // TODO: add better default
+    }
 
-    } else if (!empty($update)) {
-        if (!$cm = get_coursemodule_from_id('', $update, 0)) {
-            print_error('invalidcoursemodule');
-        }
+    if (!empty($type)) { //TODO: hopefully will be removed in 2.0
+        $data->type = $type;
+    }
 
-        if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
-            print_error('invalidcourseid');
-        }
+    $sectionname = get_section_name($course->format);
+    $fullmodulename = get_string('modulename', $module->name);
 
-        require_login($course, false, $cm); // needed to setup proper $COURSE
-        $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-        require_capability('moodle/course:manageactivities', $context);
-
-        if (!$module = $DB->get_record('modules', array('id'=>$cm->module))) {
-            print_error('moduledoesnotexist');
-        }
+    if ($data->section && $course->format != 'site') {
+        $heading = new object();
+        $heading->what = $fullmodulename;
+        $heading->to   = "$sectionname $data->section";
+        $pageheading = get_string('addinganewto', 'moodle', $heading);
+    } else {
+        $pageheading = get_string('addinganew', 'moodle', $fullmodulename);
+    }
 
-        if (!$data = $DB->get_record($module->name, array('id'=>$cm->instance))) {
-            print_error('moduleinstancedoesnotexist');
-        }
+} else if (!empty($update)) {
 
-        if (!$cw = $DB->get_record('course_sections', array('id'=>$cm->section))) {
-            print_error('sectionnotexist');
-        }
+    $url->param('update', $update);
+    $PAGE->set_url($url);
 
-        $data->coursemodule       = $cm->id;
-        $data->section            = $cw->section;  // The section number itself - relative!!! (section column in course_sections)
-        $data->visible            = $cm->visible; //??  $cw->visible ? $cm->visible : 0; // section hiding overrides
-        $data->cmidnumber         = $cm->idnumber;          // The cm IDnumber
-        $data->groupmode          = groups_get_activity_groupmode($cm); // locked later if forced
-        $data->groupingid         = $cm->groupingid;
-        $data->groupmembersonly   = $cm->groupmembersonly;
-        $data->course             = $course->id;
-        $data->module             = $module->id;
-        $data->modulename         = $module->name;
-        $data->instance           = $cm->instance;
-        $data->return             = $return;
-        $data->update             = $update;
-        $data->completion         = $cm->completion;
-        $data->completionview     = $cm->completionview;
-        $data->completionexpected = $cm->completionexpected;
-        $data->completionusegrade = is_null($cm->completiongradeitemnumber) ? 0 : 1;
-        if (!empty($CFG->enableavailability)) {
-            $data->availablefrom      = $cm->availablefrom;
-            $data->availableuntil     = $cm->availableuntil;
-            $data->showavailability   = $cm->showavailability;
-        }
+    $cm = get_coursemodule_from_id('', $update, 0, false, MUST_EXIST);
+    $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
+
+    require_login($course, false, $cm); // needed to setup proper $COURSE
+    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+    require_capability('moodle/course:manageactivities', $context);
+
+    $module = $DB->get_record('modules', array('id'=>$cm->module), '*', MUST_EXIST);
+    $data = $data = $DB->get_record($module->name, array('id'=>$cm->instance), '*', MUST_EXIST);
+    $cw = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST);
+
+    $data->coursemodule       = $cm->id;
+    $data->section            = $cw->section;  // The section number itself - relative!!! (section column in course_sections)
+    $data->visible            = $cm->visible; //??  $cw->visible ? $cm->visible : 0; // section hiding overrides
+    $data->cmidnumber         = $cm->idnumber;          // The cm IDnumber
+    $data->groupmode          = groups_get_activity_groupmode($cm); // locked later if forced
+    $data->groupingid         = $cm->groupingid;
+    $data->groupmembersonly   = $cm->groupmembersonly;
+    $data->course             = $course->id;
+    $data->module             = $module->id;
+    $data->modulename         = $module->name;
+    $data->instance           = $cm->instance;
+    $data->return             = $return;
+    $data->update             = $update;
+    $data->completion         = $cm->completion;
+    $data->completionview     = $cm->completionview;
+    $data->completionexpected = $cm->completionexpected;
+    $data->completionusegrade = is_null($cm->completiongradeitemnumber) ? 0 : 1;
+    if (!empty($CFG->enableavailability)) {
+        $data->availablefrom      = $cm->availablefrom;
+        $data->availableuntil     = $cm->availableuntil;
+        $data->showavailability   = $cm->showavailability;
+    }
 
-        if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
-            $draftid_editor = file_get_submitted_draft_itemid('introeditor');
-            $currentintro = file_prepare_draft_area($draftid_editor, $context->id, $data->modulename.'_intro', 0, array('subdirs'=>true), $data->intro);
-            $data->introeditor = array('text'=>$currentintro, 'format'=>$data->introformat, 'itemid'=>$draftid_editor);
-        }
+    if (plugin_supports('mod', $data->modulename, FEATURE_MOD_INTRO, true)) {
+        $draftid_editor = file_get_submitted_draft_itemid('introeditor');
+        $currentintro = file_prepare_draft_area($draftid_editor, $context->id, $data->modulename.'_intro', 0, array('subdirs'=>true), $data->intro);
+        $data->introeditor = array('text'=>$currentintro, 'format'=>$data->introformat, 'itemid'=>$draftid_editor);
+    }
 
-        if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$data->modulename,
-                                                 'iteminstance'=>$data->instance, 'courseid'=>$course->id))) {
-            // add existing outcomes
-            foreach ($items as $item) {
-                if (!empty($item->outcomeid)) {
-                    $data->{'outcome_'.$item->outcomeid} = 1;
-                }
+    if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$data->modulename,
+                                             'iteminstance'=>$data->instance, 'courseid'=>$course->id))) {
+        // add existing outcomes
+        foreach ($items as $item) {
+            if (!empty($item->outcomeid)) {
+                $data->{'outcome_'.$item->outcomeid} = 1;
             }
+        }
 
-            // set category if present
-            $gradecat = false;
-            foreach ($items as $item) {
-                if ($gradecat === false) {
-                    $gradecat = $item->categoryid;
-                    continue;
-                }
-                if ($gradecat != $item->categoryid) {
-                    //mixed categories
-                    $gradecat = false;
-                    break;
-                }
+        // set category if present
+        $gradecat = false;
+        foreach ($items as $item) {
+            if ($gradecat === false) {
+                $gradecat = $item->categoryid;
+                continue;
             }
-            if ($gradecat !== false) {
-                // do not set if mixed categories present
-                $data->gradecat = $gradecat;
+            if ($gradecat != $item->categoryid) {
+                //mixed categories
+                $gradecat = false;
+                break;
             }
         }
-
-        $sectionname = get_section_name($course->format);
-        $fullmodulename = get_string('modulename', $module->name);
-
-        if ($data->section && $course->format != 'site') {
-            $heading = new object();
-            $heading->what = $fullmodulename;
-            $heading->in   = "$sectionname $cw->section";
-            $pageheading = get_string('updatingain', 'moodle', $heading);
-        } else {
-            $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
+        if ($gradecat !== false) {
+            // do not set if mixed categories present
+            $data->gradecat = $gradecat;
         }
+    }
 
-        $navlinksinstancename = array('name' => format_string($data->name, true), 'link' => "$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id", 'type' => 'activityinstance');
+    $sectionname = get_section_name($course->format);
+    $fullmodulename = get_string('modulename', $module->name);
 
-        $pagetype = 'mod-' . $module->name . '-';
-        if (!empty($type)) {
-            $pagetype .= $type;
-        } else {
-            $pagetype .= 'mod';
-        }
-        $PAGE->set_pagetype($pagetype);
+    if ($data->section && $course->format != 'site') {
+        $heading = new object();
+        $heading->what = $fullmodulename;
+        $heading->in   = "$sectionname $cw->section";
+        $pageheading = get_string('updatingain', 'moodle', $heading);
     } else {
-        require_login();
-        print_error('invalidaction');
+        $pageheading = get_string('updatinga', 'moodle', $fullmodulename);
     }
 
-    $modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
-    if (file_exists($modmoodleform)) {
-        require_once($modmoodleform);
-
+} else {
+    require_login();
+    print_error('invalidaction');
+}
+
+$pagepath = 'mod-' . $module->name . '-';
+if (!empty($type)) { //TODO: hopefully will be removed in 2.0
+    $pagepath .= $type;
+} else {
+    $pagepath .= 'mod';
+}
+$PAGE->set_pagetype($pagepath);
+
+$modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
+if (file_exists($modmoodleform)) {
+    require_once($modmoodleform);
+} else {
+    print_error('noformdesc');
+}
+
+$modlib = "$CFG->dirroot/mod/$module->name/lib.php";
+if (file_exists($modlib)) {
+    include_once($modlib);
+} else {
+    print_error('modulemissingcode', '', '', $modlib);
+}
+
+$mformclassname = 'mod_'.$module->name.'_mod_form';
+$mform = new $mformclassname($data, $cw->section, $cm, $course);
+$mform->set_data($data);
+
+if ($mform->is_cancelled()) {
+    if ($return && !empty($cm->id)) {
+        redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
     } else {
-        print_error('noformdesc');
+        redirect("$CFG->wwwroot/course/view.php?id=$course->id#section-".$cw->section);
+    }
+} else if ($fromform = $mform->get_data()) {
+    if (empty($fromform->coursemodule)) {
+        // Add
+        $cm = null;
+        $course = $DB->get_record('course', array('id'=>$fromform->course), '*', MUST_EXIST);
+        $fromform->instance     = '';
+        $fromform->coursemodule = '';
+    } else { 
+        // Update
+        $cm = get_coursemodule_from_id('', $fromform->coursemodule, 0, false, MUST_EXIST);
+        $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
+        $fromform->instance     = $cm->instance;
+        $fromform->coursemodule = $cm->id;
     }
 
-    $modlib = "$CFG->dirroot/mod/$module->name/lib.php";
-    if (file_exists($modlib)) {
-        include_once($modlib);
+    if (!empty($fromform->coursemodule)) {
+        $context = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
     } else {
-        print_error('modulemissingcode', '', '', $modlib);
+        $context = get_context_instance(CONTEXT_COURSE, $course->id);
     }
 
-    $mformclassname = 'mod_'.$module->name.'_mod_form';
-    $mform = new $mformclassname($data, $cw->section, $cm, $course);
-    $mform->set_data($data);
-
-    if ($mform->is_cancelled()) {
-        if ($return && !empty($cm->id)) {
-            redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$cm->id");
-        } else {
-            redirect("$CFG->wwwroot/course/view.php?id=$course->id#section-".$cw->section);
-        }
+    $fromform->course = $course->id;
+    $fromform->modulename = clean_param($fromform->modulename, PARAM_SAFEDIR);  // For safety
 
-    } else if ($fromform = $mform->get_data()) {
-        if (empty($fromform->coursemodule)) { //add
-            $cm = null;
-            if (!$course = $DB->get_record('course', array('id'=>$fromform->course))) {
-                print_error('invalidcourseid');
-            }
-            $fromform->instance     = '';
-            $fromform->coursemodule = '';
-        } else { //update
-            if (!$cm = get_coursemodule_from_id('', $fromform->coursemodule, 0)) {
-                print_error('invalidcoursemodule');
-            }
+    $addinstancefunction    = $fromform->modulename."_add_instance";
+    $updateinstancefunction = $fromform->modulename."_update_instance";
 
-            if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
-                print_error('invalidcourseid');
-            }
-            $fromform->instance     = $cm->instance;
-            $fromform->coursemodule = $cm->id;
-        }
-
-
-        if (!empty($fromform->coursemodule)) {
-            $context = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
-        } else {
-            $context = get_context_instance(CONTEXT_COURSE, $course->id);
-        }
+    if (!isset($fromform->groupingid)) {
+        $fromform->groupingid = 0;
+    }
 
-        $fromform->course = $course->id;
-        $fromform->modulename = clean_param($fromform->modulename, PARAM_SAFEDIR);  // For safety
+    if (!isset($fromform->groupmembersonly)) {
+        $fromform->groupmembersonly = 0;
+    }
 
-        $addinstancefunction    = $fromform->modulename."_add_instance";
-        $updateinstancefunction = $fromform->modulename."_update_instance";
+    if (!isset($fromform->name)) { //label
+        $fromform->name = $fromform->modulename;
+    }
 
-        if (!isset($fromform->groupingid)) {
-            $fromform->groupingid = 0;
-        }
+    if (!isset($fromform->completion)) {
+        $fromform->completion = COMPLETION_DISABLED;
+    }
+    if (!isset($fromform->completionview)) {
+        $fromform->completionview = COMPLETION_VIEW_NOT_REQUIRED;
+    }
 
-        if (!isset($fromform->groupmembersonly)) {
-            $fromform->groupmembersonly = 0;
-        }
+    // Convert the 'use grade' checkbox into a grade-item number: 0 if
+    // checked, null if not
+    if (isset($fromform->completionusegrade) && $fromform->completionusegrade) {
+        $fromform->completiongradeitemnumber = 0;
+    } else {
+        $fromform->completiongradeitemnumber = null;
+    }
 
-        if (!isset($fromform->name)) { //label
-            $fromform->name = $fromform->modulename;
-        }
+    if (!empty($fromform->update)) {
 
-        if (!isset($fromform->completion)) {
-            $fromform->completion = COMPLETION_DISABLED;
-        }
-        if (!isset($fromform->completionview)) {
-            $fromform->completionview = COMPLETION_VIEW_NOT_REQUIRED;
+        if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
+            $fromform->groupmode = $cm->groupmode; // keep original
         }
 
-        // Convert the 'use grade' checkbox into a grade-item number: 0 if
-        // checked, null if not
-        $fromform->completiongradeitemnumber =
-            isset($fromform->completionusegrade) && $fromform->completionusegrade
-            ? 0 : null;
-
-        if (!empty($fromform->update)) {
-
-            if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
-                $fromform->groupmode = $cm->groupmode; // keep original
-            }
+        // update course module first
+        $cm->groupmode        = $fromform->groupmode;
+        $cm->groupingid       = $fromform->groupingid;
+        $cm->groupmembersonly = $fromform->groupmembersonly;
 
-            // update course module first
-            $cm->groupmode        = $fromform->groupmode;
-            $cm->groupingid       = $fromform->groupingid;
-            $cm->groupmembersonly = $fromform->groupmembersonly;
-
-            $completion = new completion_info($course);
-            if ($completion->is_enabled()) {
-                // Handle completion settings. If necessary, wipe existing completion
-                // data first.
-                if (!empty($fromform->completionunlocked)) {
-                    $completion = new completion_info($course);
-                    $completion->reset_all_state($cm);
-                }
-
-                $cm->completion                = $fromform->completion;
-                $cm->completiongradeitemnumber = $fromform->completiongradeitemnumber;
-                $cm->completionview            = $fromform->completionview;
-                $cm->completionexpected        = $fromform->completionexpected;
-            }
-            if (!empty($CFG->enableavailability)) {
-                $cm->availablefrom             = $fromform->availablefrom;
-                $cm->availableuntil            = $fromform->availableuntil;
-                // The form time is midnight, but because we want it to be
-                // inclusive, set it to 23:59:59 on that day.
-                if ($cm->availableuntil) {
-                    $cm->availableuntil = strtotime('23:59:59',
-                        $cm->availableuntil);
-                }
-                $cm->showavailability          = $fromform->showavailability;
-                condition_info::update_cm_from_form($cm,$fromform,true);
+        $completion = new completion_info($course);
+        if ($completion->is_enabled()) {
+            // Handle completion settings. If necessary, wipe existing completion
+            // data first.
+            if (!empty($fromform->completionunlocked)) {
+                $completion = new completion_info($course);
+                $completion->reset_all_state($cm);
             }
 
-            $DB->update_record('course_modules', $cm);
-
-            $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
-
-            // update embedded links and save files
-            if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
-                $fromform->intro = file_save_draft_area_files($fromform->introeditor['itemid'], $modcontext->id,
-                                                              $fromform->modulename.'_intro', 0,
-                                                              array('subdirs'=>true), $fromform->introeditor['text']);
-                $fromform->introformat = $fromform->introeditor['format'];
-                unset($fromform->introeditor);
+            $cm->completion                = $fromform->completion;
+            $cm->completiongradeitemnumber = $fromform->completiongradeitemnumber;
+            $cm->completionview            = $fromform->completionview;
+            $cm->completionexpected        = $fromform->completionexpected;
+        }
+        if (!empty($CFG->enableavailability)) {
+            $cm->availablefrom             = $fromform->availablefrom;
+            $cm->availableuntil            = $fromform->availableuntil;
+            // The form time is midnight, but because we want it to be
+            // inclusive, set it to 23:59:59 on that day.
+            if ($cm->availableuntil) {
+                $cm->availableuntil = strtotime('23:59:59',
+                    $cm->availableuntil);
             }
-
-            if (!$updateinstancefunction($fromform, $mform)) {
-                print_error('cannotupdatemod', '', 'view.php?id=$course->id', $fromform->modulename);
+            $cm->showavailability          = $fromform->showavailability;
+            condition_info::update_cm_from_form($cm,$fromform,true);
+        }
+
+        $DB->update_record('course_modules', $cm);
+
+        $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
+
+        // update embedded links and save files
+        if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
+            $fromform->intro = file_save_draft_area_files($fromform->introeditor['itemid'], $modcontext->id,
+                                                          $fromform->modulename.'_intro', 0,
+                                                          array('subdirs'=>true), $fromform->introeditor['text']);
+            $fromform->introformat = $fromform->introeditor['format'];
+            unset($fromform->introeditor);
+        }
+
+        if (!$updateinstancefunction($fromform, $mform)) {
+            print_error('cannotupdatemod', '', 'view.php?id=$course->id', $fromform->modulename);
+        }
+
+        // make sure visibility is set correctly (in particular in calendar)
+        set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
+
+        if (isset($fromform->cmidnumber)) { //label
+            // set cm idnumber - uniqueness is already verified by form validation
+            set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
+        }
+        //trigger event with information about this module.
+        $eventdata = new object();
+        $eventdata->component = 'mod/'.$fromform->modulename;
+        $eventdata->course    = $course->id;
+        $eventdata->cm        = $fromform->coursemodule;
+        events_trigger($fromform->modulename.'_mod_updated', $eventdata);
+
+        add_to_log($course->id, "course", "update mod",
+                   "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
+                   "$fromform->modulename $fromform->instance");
+        add_to_log($course->id, $fromform->modulename, "update",
+                   "view.php?id=$fromform->coursemodule",
+                   "$fromform->instance", $fromform->coursemodule);
+
+    } else if (!empty($fromform->add)) {
+
+        if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
+            $fromform->groupmode = 0; // do not set groupmode
+        }
+
+        if (!course_allowed_module($course, $fromform->modulename)) {
+            print_error('moduledisable', '', '', $fromform->modulename);
+        }
+
+        // first add course_module record because we need the context
+        $newcm = new object();
+        $newcm->course           = $course->id;
+        $newcm->module           = $fromform->module;
+        $newcm->instance         = 0; // not known yet, will be updated later (this is similar to restore code)
+        $newcm->visible          = $fromform->visible;
+        $newcm->groupmode        = $fromform->groupmode;
+        $newcm->groupingid       = $fromform->groupingid;
+        $newcm->groupmembersonly = $fromform->groupmembersonly;
+        $completion = new completion_info($course);
+        if ($completion->is_enabled()) {
+            $newcm->completion                = $fromform->completion;
+            $newcm->completiongradeitemnumber = $fromform->completiongradeitemnumber;
+            $newcm->completionview            = $fromform->completionview;
+            $newcm->completionexpected        = $fromform->completionexpected;
+        }
+        if(!empty($CFG->enableavailability)) {
+            $newcm->availablefrom             = $fromform->availablefrom;
+            $newcm->availableuntil            = $fromform->availableuntil;
+            // The form time is midnight, but because we want it to be
+            // inclusive, set it to 23:59:59 on that day.
+            if ($newcm->availableuntil) {
+                $newcm->availableuntil = strtotime('23:59:59',
+                    $newcm->availableuntil);
             }
+            $newcm->showavailability          = $fromform->showavailability;
+        }
 
-            // make sure visibility is set correctly (in particular in calendar)
-            set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
-
-            if (isset($fromform->cmidnumber)) { //label
-                // set cm idnumber - uniqueness is already verified by form validation
-                set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
-            }
-            //trigger event with information about this module.
-            $eventdata = new object();
-            $eventdata->component = 'mod/'.$fromform->modulename;
-            $eventdata->course    = $course->id;
-            $eventdata->cm        = $fromform->coursemodule;
-            events_trigger($fromform->modulename.'_mod_updated', $eventdata);
-
-            add_to_log($course->id, "course", "update mod",
-                       "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
-                       "$fromform->modulename $fromform->instance");
-            add_to_log($course->id, $fromform->modulename, "update",
-                       "view.php?id=$fromform->coursemodule",
-                       "$fromform->instance", $fromform->coursemodule);
-
-        } else if (!empty($fromform->add)) {
-
-            if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) {
-                $fromform->groupmode = 0; // do not set groupmode
-            }
+        if (!$fromform->coursemodule = add_course_module($newcm)) {
+            print_error('cannotaddcoursemodule');
+        }
 
-            if (!course_allowed_module($course, $fromform->modulename)) {
-                print_error('moduledisable', '', '', $fromform->modulename);
-            }
+        if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, false)) {
+            $introeditor = $fromform->introeditor;
+            unset($fromform->introeditor);
+            $fromform->intro       = $introeditor['text'];
+            $fromform->introformat = $introeditor['format'];
+        }
 
-            // first add course_module record because we need the context
-            $newcm = new object();
-            $newcm->course           = $course->id;
-            $newcm->module           = $fromform->module;
-            $newcm->instance         = 0; // not known yet, will be updated later (this is similar to restore code)
-            $newcm->visible          = $fromform->visible;
-            $newcm->groupmode        = $fromform->groupmode;
-            $newcm->groupingid       = $fromform->groupingid;
-            $newcm->groupmembersonly = $fromform->groupmembersonly;
-            $completion = new completion_info($course);
-            if ($completion->is_enabled()) {
-                $newcm->completion                = $fromform->completion;
-                $newcm->completiongradeitemnumber = $fromform->completiongradeitemnumber;
-                $newcm->completionview            = $fromform->completionview;
-                $newcm->completionexpected        = $fromform->completionexpected;
-            }
-            if(!empty($CFG->enableavailability)) {
-                $newcm->availablefrom             = $fromform->availablefrom;
-                $newcm->availableuntil            = $fromform->availableuntil;
-                // The form time is midnight, but because we want it to be
-                // inclusive, set it to 23:59:59 on that day.
-                if ($newcm->availableuntil) {
-                    $newcm->availableuntil = strtotime('23:59:59',
-                        $newcm->availableuntil);
-                }
-                $newcm->showavailability          = $fromform->showavailability;
-            }
+        $returnfromfunc = $addinstancefunction($fromform, $mform);
 
-            if (!$fromform->coursemodule = add_course_module($newcm)) {
-                print_error('cannotaddcoursemodule');
-            }
+        if (!$returnfromfunc or !is_number($returnfromfunc)) {
+            // undo everything we can
+            $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
+            delete_context(CONTEXT_MODULE, $fromform->coursemodule);
+            $DB->delete_records('course_modules', array('id'=>$fromform->coursemodule));
 
-            if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, false)) {
-                $introeditor = $fromform->introeditor;
-                unset($fromform->introeditor);
-                $fromform->intro       = $introeditor['text'];
-                $fromform->introformat = $introeditor['format'];
+            if (!is_number($returnfromfunc)) {
+                print_error('invalidfunction', '', 'view.php?id=$course->id');
+            } else {
+                print_error('cannotaddnewmodule', '', "view.php?id=$course->id", $fromform->modulename);
             }
+        }
 
-            $returnfromfunc = $addinstancefunction($fromform, $mform);
+        $fromform->instance = $returnfromfunc;
 
-            if (!$returnfromfunc or !is_number($returnfromfunc)) {
-                // undo everything we can
-                $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
-                delete_context(CONTEXT_MODULE, $fromform->coursemodule);
-                $DB->delete_records('course_modules', array('id'=>$fromform->coursemodule));
+        $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$fromform->coursemodule));
 
-                if (!is_number($returnfromfunc)) {
-                    print_error('invalidfunction', '', 'view.php?id=$course->id');
-                } else {
-                    print_error('cannotaddnewmodule', '', "view.php?id=$course->id", $fromform->modulename);
-                }
-            }
+        // update embedded links and save files
+        $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
+        if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
+            $fromform->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id,
+                                                          $fromform->modulename.'_intro', 0,
+                                                          array('subdirs'=>true), $introeditor['text']);
+            $DB->set_field($fromform->modulename, 'intro', $fromform->intro, array('id'=>$fromform->instance));
+        }
 
-            $fromform->instance = $returnfromfunc;
+        // course_modules and course_sections each contain a reference
+        // to each other, so we have to update one of them twice.
+        $sectionid = add_mod_to_section($fromform);
 
-            $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$fromform->coursemodule));
+        $DB->set_field('course_modules', 'section', $sectionid, array('id'=>$fromform->coursemodule));
 
-            // update embedded links and save files
-            $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
-            if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
-                $fromform->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id,
-                                                              $fromform->modulename.'_intro', 0,
-                                                              array('subdirs'=>true), $introeditor['text']);
-                $DB->set_field($fromform->modulename, 'intro', $fromform->intro, array('id'=>$fromform->instance));
-            }
+        // make sure visibility is set correctly (in particular in calendar)
+        set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
 
-            // course_modules and course_sections each contain a reference
-            // to each other, so we have to update one of them twice.
-            $sectionid = add_mod_to_section($fromform);
+        if (isset($fromform->cmidnumber)) { //label
+            // set cm idnumber - uniqueness is already verified by form validation
+            set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
+        }
 
-            $DB->set_field('course_modules', 'section', $sectionid, array('id'=>$fromform->coursemodule));
+        // Set up conditions
+        if ($CFG->enableavailability) {
+            condition_info::update_cm_from_form((object)array('id'=>$fromform->coursemodule), $fromform, false);
+        }
+        //trigger event with information about this module.
+        $eventdata = new object();
+        $eventdata->component = 'mod/'.$fromform->modulename;
+        $eventdata->course    = $course->id;
+        $eventdata->cm        = $fromform->coursemodule;
+        events_trigger($fromform->modulename.'_mod_created', $eventdata);
 
-            // make sure visibility is set correctly (in particular in calendar)
-            set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
+        add_to_log($course->id, "course", "add mod",
+                   "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
+                   "$fromform->modulename $fromform->instance");
+        add_to_log($course->id, $fromform->modulename, "add",
+                   "view.php?id=$fromform->coursemodule",
+                   "$fromform->instance", $fromform->coursemodule);
+    } else {
+        print_error('invaliddata');
+    }
 
-            if (isset($fromform->cmidnumber)) { //label
-                // set cm idnumber - uniqueness is already verified by form validation
-                set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
-            }
+    // sync idnumber with grade_item
+    if ($grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
+                 'iteminstance'=>$fromform->instance, 'itemnumber'=>0, 'courseid'=>$course->id))) {
+        if ($grade_item->idnumber != $fromform->cmidnumber) {
+            $grade_item->idnumber = $fromform->cmidnumber;
+            $grade_item->update();
+        }
+    }
 
-            // Set up conditions
-            if ($CFG->enableavailability) {
-                condition_info::update_cm_from_form((object)array('id'=>$fromform->coursemodule), $fromform, false);
+    $items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
+                                         'iteminstance'=>$fromform->instance, 'courseid'=>$course->id));
+
+    // create parent category if requested and move to correct parent category
+    if ($items and isset($fromform->gradecat)) {
+        if ($fromform->gradecat == -1) {
+            $grade_category = new grade_category();
+            $grade_category->courseid = $course->id;
+            $grade_category->fullname = $fromform->name;
+            $grade_category->insert();
+            if ($grade_item) {
+                $parent = $grade_item->get_parent_category();
+                $grade_category->set_parent($parent->id);
             }
-            //trigger event with information about this module.
-            $eventdata = new object();
-            $eventdata->component = 'mod/'.$fromform->modulename;
-            $eventdata->course    = $course->id;
-            $eventdata->cm        = $fromform->coursemodule;
-            events_trigger($fromform->modulename.'_mod_created', $eventdata);
-
-            add_to_log($course->id, "course", "add mod",
-                       "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
-                       "$fromform->modulename $fromform->instance");
-            add_to_log($course->id, $fromform->modulename, "add",
-                       "view.php?id=$fromform->coursemodule",
-                       "$fromform->instance", $fromform->coursemodule);
-        } else {
-            print_error('invaliddata');
+            $fromform->gradecat = $grade_category->id;
         }
-
-        // sync idnumber with grade_item
-        if ($grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
-                     'iteminstance'=>$fromform->instance, 'itemnumber'=>0, 'courseid'=>$course->id))) {
-            if ($grade_item->idnumber != $fromform->cmidnumber) {
-                $grade_item->idnumber = $fromform->cmidnumber;
-                $grade_item->update();
+        foreach ($items as $itemid=>$unused) {
+            $items[$itemid]->set_parent($fromform->gradecat);
+            if ($itemid == $grade_item->id) {
+                // use updated grade_item
+                $grade_item = $items[$itemid];
             }
         }
+    }
 
-        $items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
-                                             'iteminstance'=>$fromform->instance, 'courseid'=>$course->id));
+    // add outcomes if requested
+    if ($outcomes = grade_outcome::fetch_all_available($course->id)) {
+        $grade_items = array();
 
-        // create parent category if requested and move to correct parent category
-        if ($items and isset($fromform->gradecat)) {
-            if ($fromform->gradecat == -1) {
-                $grade_category = new grade_category();
-                $grade_category->courseid = $course->id;
-                $grade_category->fullname = $fromform->name;
-                $grade_category->insert();
-                if ($grade_item) {
-                    $parent = $grade_item->get_parent_category();
-                    $grade_category->set_parent($parent->id);
-                }
-                $fromform->gradecat = $grade_category->id;
-            }
-            foreach ($items as $itemid=>$unused) {
-                $items[$itemid]->set_parent($fromform->gradecat);
-                if ($itemid == $grade_item->id) {
-                    // use updated grade_item
-                    $grade_item = $items[$itemid];
+        // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes
+        $max_itemnumber = 999;
+        if ($items) {
+            foreach($items as $item) {
+                if ($item->itemnumber > $max_itemnumber) {
+                    $max_itemnumber = $item->itemnumber;
                 }
             }
         }
 
-        // add outcomes if requested
-        if ($outcomes = grade_outcome::fetch_all_available($course->id)) {
-            $grade_items = array();
+        foreach($outcomes as $outcome) {
+            $elname = 'outcome_'.$outcome->id;
 
-            // Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes
-            $max_itemnumber = 999;
-            if ($items) {
-                foreach($items as $item) {
-                    if ($item->itemnumber > $max_itemnumber) {
-                        $max_itemnumber = $item->itemnumber;
+            if (array_key_exists($elname, $fromform) and $fromform->$elname) {
+                // so we have a request for new outcome grade item?
+                if ($items) {
+                    foreach($items as $item) {
+                        if ($item->outcomeid == $outcome->id) {
+                            //outcome aready exists
+                            continue 2;
+                        }
                     }
                 }
-            }
 
-            foreach($outcomes as $outcome) {
-                $elname = 'outcome_'.$outcome->id;
-
-                if (array_key_exists($elname, $fromform) and $fromform->$elname) {
-                    // so we have a request for new outcome grade item?
-                    if ($items) {
-                        foreach($items as $item) {
-                            if ($item->outcomeid == $outcome->id) {
-                                //outcome aready exists
-                                continue 2;
-                            }
-                        }
-                    }
+                $max_itemnumber++;
+
+                $outcome_item = new grade_item();
+                $outcome_item->courseid     = $course->id;
+                $outcome_item->itemtype     = 'mod';
+                $outcome_item->itemmodule   = $fromform->modulename;
+                $outcome_item->iteminstance = $fromform->instance;
+                $outcome_item->itemnumber   = $max_itemnumber;
+                $outcome_item->itemname     = $outcome->fullname;
+                $outcome_item->outcomeid    = $outcome->id;
+                $outcome_item->gradetype    = GRADE_TYPE_SCALE;
+                $outcome_item->scaleid      = $outcome->scaleid;
+                $outcome_item->insert();
+
+                // move the new outcome into correct category and fix sortorder if needed
+                if ($grade_item) {
+                    $outcome_item->set_parent($grade_item->categoryid);
+                    $outcome_item->move_after_sortorder($grade_item->sortorder);
 
-                    $max_itemnumber++;
-
-                    $outcome_item = new grade_item();
-                    $outcome_item->courseid     = $course->id;
-                    $outcome_item->itemtype     = 'mod';
-                    $outcome_item->itemmodule   = $fromform->modulename;
-                    $outcome_item->iteminstance = $fromform->instance;
-                    $outcome_item->itemnumber   = $max_itemnumber;
-                    $outcome_item->itemname     = $outcome->fullname;
-                    $outcome_item->outcomeid    = $outcome->id;
-                    $outcome_item->gradetype    = GRADE_TYPE_SCALE;
-                    $outcome_item->scaleid      = $outcome->scaleid;
-                    $outcome_item->insert();
-
-                    // move the new outcome into correct category and fix sortorder if needed
-                    if ($grade_item) {
-                        $outcome_item->set_parent($grade_item->categoryid);
-                        $outcome_item->move_after_sortorder($grade_item->sortorder);
-
-                    } else if (isset($fromform->gradecat)) {
-                        $outcome_item->set_parent($fromform->gradecat);
-                    }
+                } else if (isset($fromform->gradecat)) {
+                    $outcome_item->set_parent($fromform->gradecat);
                 }
             }
         }
+    }
 
-        rebuild_course_cache($course->id);
-        grade_regrade_final_grades($course->id);
-
-        if (isset($fromform->submitbutton)) {
-            redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
-        } else {
-            redirect("$CFG->wwwroot/course/view.php?id=$course->id");
-        }
-        exit;
+    rebuild_course_cache($course->id);
+    grade_regrade_final_grades($course->id);
 
+    if (isset($fromform->submitbutton)) {
+        redirect("$CFG->wwwroot/mod/$module->name/view.php?id=$fromform->coursemodule");
     } else {
-        if (!empty($cm->id)) {
-            $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-        } else {
-            $context = get_context_instance(CONTEXT_COURSE, $course->id);
-        }
+        redirect("$CFG->wwwroot/course/view.php?id=$course->id");
+    }
+    exit;
 
-        $streditinga = get_string('editinga', 'moodle', $fullmodulename);
-        $strmodulenameplural = get_string('modulenameplural', $module->name);
-
-        $PAGE->navbar->add($streditinga);
-        $PAGE->set_heading($course->fullname);
-        $PAGE->set_title($streditinga);
-        $PAGE->set_focuscontrol($mform->focus());
-        $PAGE->set_cacheable(false);
-        $PAGE->set_pagelayout('admin');
-        echo $OUTPUT->header();
-
-        if (!empty($cm->id)) {
-            $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-            $overridableroles = get_overridable_roles($context);
-            $assignableroles  = get_assignable_roles($context);
-            $currenttab = 'update';
-            require($CFG->dirroot.'/'.$CFG->admin.'/roles/tabs.php');
-        }
+} else {
 
-        if (get_string_manager()->string_exists('modulename_hlp', $module->name)) {
-            echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name, 'icon');
-        } else {
-            echo $OUTPUT->heading_with_help($pageheading, '', '', 'icon');
-        }
+    $streditinga = get_string('editinga', 'moodle', $fullmodulename);
+    $strmodulenameplural = get_string('modulenameplural', $module->name);
 
-        $mform->display();
+    if (!empty($cm->id)) {
+        $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+    } else {
+        $context = get_context_instance(CONTEXT_COURSE, $course->id);
+    }
+
+    $PAGE->set_heading($course->fullname);
+    $PAGE->set_title($streditinga);
+    $PAGE->set_focuscontrol($mform->focus());
+    $PAGE->set_cacheable(false);
+    $PAGE->set_pagelayout('admin');
+    echo $OUTPUT->header();
+
+    if (!empty($cm->id)) {
+        $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+        $overridableroles = get_overridable_roles($context);
+        $assignableroles  = get_assignable_roles($context);
+        $currenttab = 'update';
+        require($CFG->dirroot.'/'.$CFG->admin.'/roles/tabs.php');
+    }
 
-        echo $OUTPUT->footer();
+    if (get_string_manager()->string_exists('modulename_hlp', $module->name)) {
+        echo $OUTPUT->heading_with_help($pageheading, 'modulename', $module->name, 'icon');
+    } else {
+        echo $OUTPUT->heading_with_help($pageheading, '', '', 'icon');
     }
+
+    $mform->display();
+
+    echo $OUTPUT->footer();
+}
\ No newline at end of file
index 0699e66..bf9ec23 100644 (file)
@@ -36,9 +36,7 @@ require_once($CFG->libdir . '/adminlib.php');
 $contextid = required_param('contextid',PARAM_INT);
 $forfilter = optional_param('filter', '', PARAM_SAFEPATH);
 
-if (!$context = get_context_instance_by_id($contextid)) {
-    print_error('wrongcontextid', 'error');
-}
+$context = get_context_instance_by_id($contextid, MUST_EXIST);
 
 $args = array('contextid'=>$contextid);
 $baseurl = new moodle_url('/filter/manage.php', $args);
@@ -52,26 +50,34 @@ if (!in_array($context->contextlevel, array(CONTEXT_COURSECAT, CONTEXT_COURSE, C
     print_error('cannotcustomisefiltersblockuser', 'error');
 }
 
-$isfrontpage = $context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID;
+$isfrontpage = ($context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID);
 $contextname = print_context_name($context);
 
+$cm = null;
 if ($context->contextlevel == CONTEXT_COURSECAT) {
     $course = clone($SITE);
+    $heading = "$SITE->fullname: ".get_string("categories");
+    $PAGE->set_heading();
 } else if ($context->contextlevel == CONTEXT_COURSE) {
-    $course = $DB->get_record('course', array('id' => $context->instanceid));
+    $course = $DB->get_record('course', array('id' => $context->instanceid), '*', MUST_EXIST);
+    $heading = $course->fullname;
 } else {
     // Must be module context.
-    $course = $DB->get_record_sql('SELECT c.* FROM {course} c JOIN {context} ctx ON c.id = ctx.instanceid WHERE ctx.id = ?',
-            array(get_parent_contextid($context)));
-}
-if (!$course) {
-    print_error('invalidcourse', 'error');
+    $cm = get_coursemodule_from_id(null, $context->instanceid, false, 0, MUST_EXIST);
+    $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
 }
 
 /// Check login and permissions.
-require_login($course);
+require_login($course, false, $cm);
 require_capability('moodle/filter:manage', $context);
 
+if ($cm) {
+    $heading = $PAGE->activityrecord->name;
+}
+
+$PAGE->set_context($context);
+$PAGE->set_heading($heading);
+
 /// Get the list of available filters.
 $availablefilters = filter_get_available_in_context($context);
 if (!$isfrontpage && empty($availablefilters)) {
@@ -106,10 +112,6 @@ if ($forfilter == '' && optional_param('savechanges', false, PARAM_BOOL) && conf
     redirect($CFG->wwwroot . '/filter/manage.php?contextid=' . $context->id, get_string('changessaved'), 1);
 }
 
-/// These are needed early because of tabs.php
-$assignableroles = get_assignable_roles($context, ROLENAME_BOTH);
-$overridableroles = get_overridable_roles($context, ROLENAME_BOTH);
-
 /// Work out an appropriate page title.
 if ($forfilter) {
     $a = new stdClass;
@@ -122,16 +124,24 @@ if ($forfilter) {
 $straction = get_string('filters', 'admin'); // Used by tabs.php
 
 /// Print the header and tabs
-if ($context->contextlevel == CONTEXT_COURSE and $context->instanceid == SITEID) {
+if ($isfrontpage) {
     admin_externalpage_setup('frontpagefilters');
     echo $OUTPUT->header();
 } else {
+    $PAGE->set_cacheable(false);
+    $PAGE->set_title($title);
+    $PAGE->set_pagelayout('admin');
+    echo $OUTPUT->header();
+    
+    // These are because of /roles/tabs.php
     $currenttab = 'filters';
-    include_once($CFG->dirroot . '/' . $CFG->admin . '/roles/tabs.php');
+    $assignableroles = get_assignable_roles($context, ROLENAME_BOTH);
+    $overridableroles = get_overridable_roles($context, ROLENAME_BOTH);
+    include($CFG->dirroot . '/' . $CFG->admin . '/roles/tabs.php');
 }
 
 /// Print heading.
-echo $OUTPUT->heading_with_help($title, 'localfiltersettings');
+echo $OUTPUT->heading_with_help($title, 'localfiltersettings', 'filters');
 
 if (empty($availablefilters)) {
     echo '<p class="centerpara">' . get_string('nofiltersenabled', 'filters') . "</p>\n";
@@ -158,9 +168,9 @@ if (empty($availablefilters)) {
         TEXTFILTER_ON => $stron,
     );
 
-    echo '<form action="'.$baseurl->out().'" method="post">';
-    echo "\n<div>\n";
-    echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
+    echo html_writer::start_tag('form', array('action'=>$baseurl->out(), 'method'=>'post'));
+    echo html_writer::start_tag('div');
+    echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
 
     $table = new html_table();
     $table->head  = array(get_string('filter'), get_string('isactive', 'filters'));
@@ -200,18 +210,18 @@ if (empty($availablefilters)) {
     }
 
     echo html_writer::table($table);
-    echo '<div class="buttons">' . "\n";
-    echo '<input type="submit" name="savechanges" value="' . get_string('savechanges') . '" />';
-    echo "\n</div>\n";
-    echo "</div>\n";
-    echo "</form>\n";
-
+    echo html_writer::start_tag('div', array('class'=>'buttons'));
+    echo html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'savechanges', 'value'=>get_string('savechanges')));
+    echo html_writer::end_tag('div');
+    echo html_writer::end_tag('div');
+    echo html_writer::end_tag('form');
 }
 
 /// Appropriate back link.
-if (!$isfrontpage && ($url = get_context_url($context))) {
-    echo '<div class="backlink"><a href="' . $url . '">' .
-        get_string('backto', '', $contextname) . '</a></div>';
+if (!$isfrontpage) {
+    echo html_writer::start_tag('div', array('class'=>'backlink'));
+    echo html_writer::tag('a', get_string('backto', '', $contextname), array('href'=>get_context_url($context)));
+    echo html_writer::end_tag('div');
 }
 
 echo $OUTPUT->footer();
index 1858e59..6de4a2b 100644 (file)
@@ -2733,30 +2733,35 @@ class settings_navigation extends navigation_node {
     protected function load_category_settings() {
         global $CFG;
 
-        $blocknode = $this->add(print_context_name($this->context));
-        $blocknode->force_open();
+        $categorynode = $this->add(print_context_name($this->context));
+        $categorynode->force_open();
 
         if ($this->page->user_is_editing() && has_capability('moodle/category:manage', $this->context)) {
-            $blocknode->add(get_string('editcategorythis'), new moodle_url('/course/editcategory.php', array('id' => $this->context->instanceid)));
-            $blocknode->add(get_string('addsubcategory'), new moodle_url('/course/editcategory.php', array('parent' => $this->context->instanceid)));
+            $categorynode->add(get_string('editcategorythis'), new moodle_url('/course/editcategory.php', array('id' => $this->context->instanceid)));
+            $categorynode->add(get_string('addsubcategory'), new moodle_url('/course/editcategory.php', array('parent' => $this->context->instanceid)));
         }
 
         // Assign local roles
         $assignurl = new moodle_url('/'.$CFG->admin.'/roles/assign.php', array('contextid'=>$this->context->id));
-        $blocknode->add(get_string('assignroles', 'role'), $assignurl, self::TYPE_SETTING);
+        $categorynode->add(get_string('assignroles', 'role'), $assignurl, self::TYPE_SETTING);
 
         // Override roles
         if (has_capability('moodle/role:review', $this->context) or count(get_overridable_roles($this->context))>0) {
             $url = new moodle_url('/'.$CFG->admin.'/roles/permissions.php', array('contextid'=>$this->context->id));
-            $blocknode->add(get_string('permissions', 'role'), $url, self::TYPE_SETTING);
+            $categorynode->add(get_string('permissions', 'role'), $url, self::TYPE_SETTING);
         }
         // Check role permissions
         if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride','moodle/role:override', 'moodle/role:assign'), $this->context)) {
             $url = new moodle_url('/'.$CFG->admin.'/roles/check.php', array('contextid'=>$this->context->id));
-            $blocknode->add(get_string('checkpermissions', 'role'), $url, self::TYPE_SETTING);
+            $categorynode->add(get_string('checkpermissions', 'role'), $url, self::TYPE_SETTING);
+        }
+        // Manage filters
+        if (has_capability('moodle/filter:manage', $this->context) && count(filter_get_available_in_context($this->context))>0) {
+            $url = new moodle_url('/filter/manage.php', array('contextid'=>$this->context->id));
+            $categorynode->add(get_string('filters', 'admin'), $url, self::TYPE_SETTING);
         }
 
-        return $blocknode;
+        return $categorynode;
     }
 
     /**