protected $_blocks = null;
+ protected $_blockseditingcap = 'moodle/site:manageblocks';
+
+ protected $_othereditingcaps = array();
+
/// Getter methods =============================================================
/// Due to the __get magic below, you normally do not call these as $PAGE->get_x
/// methods, but instead use the $PAGE->x syntax.
* @return boolean does the user have permission to see this page in editing mode.
*/
public function user_allowed_editing() {
- return true; // TODO
+ return has_any_capability($this->all_editing_caps(), $this->_context);
}
/// Setter methods =============================================================
}
}
+ /**
+ * Set the capability that allows users to edit blocks on this page. Normally
+ * the default of 'moodle/site:manageblocks' is used, but a few pages like
+ * the My Moodle page need to use a different capability like 'moodle/my:manageblocks'.
+ * @param string $capability a capability.
+ */
+ public function set_blocks_editing_capability($capability) {
+ $this->_blockseditingcap = $capability;
+ }
+
+ /**
+ * Some pages let you turn editing on for reasons other than editing blocks.
+ * If that is the case, you can pass other capabilitise that let the user
+ * edit this page here.
+ * @param string|array $capability either a capability, or an array of capabilities.
+ */
+ public function set_other_editing_capability($capability) {
+ if (is_array($capability)) {
+ $this->_othereditingcaps = array_unique($this->_othereditingcaps + $capability);
+ } else {
+ $this->_othereditingcaps[] = $capability;
+ }
+ }
+
/// Initialisation methods =====================================================
/// These set various things up in a default way.
return $class;
}
+ protected function all_editing_caps() {
+ $caps = $this->_othereditingcaps;
+ $caps[] = $this->_blockseditingcap;
+ return $caps;
+ }
+
/// Deprecated fields and methods for backwards compatibility ==================
/**
public function url_to_class_name($url) {
return parent::url_to_class_name($url);
}
+ public function all_editing_caps() {
+ return parent::all_editing_caps();
+ }
}
/**
global $USER;
$this->originaluserediting = !empty($USER->editing);
$this->testpage = new testable_moodle_page();
+ $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM));
}
public function tearDown() {
$USER->editing = $this->originaluserediting;
}
+ // We are relying on the fact that unit tests are alwyas run by admin, to
+ // ensure the user_allows_editing call returns true.
public function test_user_is_editing_on() {
// Setup fixture
global $USER;
$this->assertTrue($this->testpage->user_is_editing());
}
+ // We are relying on the fact that unit tests are alwyas run by admin, to
+ // ensure the user_allows_editing call returns true.
public function test_user_is_editing_off() {
// Setup fixture
global $USER;
// Validate
$this->assertFalse($this->testpage->user_is_editing());
}
+
+ public function test_default_editing_capabilities() {
+ // Validate
+ $this->assertEqual(array('moodle/site:manageblocks'), $this->testpage->all_editing_caps());
+ }
+
+ public function test_other_block_editing_cap() {
+ // Exercise SUT
+ $this->testpage->set_blocks_editing_capability('moodle/my:manageblocks');
+ // Validate
+ $this->assertEqual(array('moodle/my:manageblocks'), $this->testpage->all_editing_caps());
+ }
+
+ public function test_other_editing_cap() {
+ // Exercise SUT
+ $this->testpage->set_other_editing_capability('moodle/course:manageactivities');
+ // Validate
+ $actualcaps = $this->testpage->all_editing_caps();
+ $expectedcaps = array('moodle/course:manageactivities', 'moodle/site:manageblocks');
+ $this->assert(new ArraysHaveSameValuesExpectation($expectedcaps), $actualcaps);
+ }
+
+ public function test_other_editing_caps() {
+ // Exercise SUT
+ $this->testpage->set_other_editing_capability(array('moodle/course:manageactivities', 'moodle/site:other'));
+ // Validate
+ $actualcaps = $this->testpage->all_editing_caps();
+ $expectedcaps = array('moodle/course:manageactivities', 'moodle/site:other', 'moodle/site:manageblocks');
+ $this->assert(new ArraysHaveSameValuesExpectation($expectedcaps), $actualcaps);
+ }
}
?>