Commit 830dd6e9 authored by tjhunt's avatar tjhunt
Browse files

moodle_page: MDL-12212 reimplement user_is_editing, deprecate isediting

parent d7f688d4
......@@ -31,12 +31,6 @@ class page_admin extends page_base {
return has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_SYSTEM));
}
// has to be fixed. i know there's a "proper" way to do this
function user_is_editing() {
global $USER;
return $USER->adminediting;
}
/**
* Use this to pass extra HTML that is added after the turn blocks editing on/off button.
*
......
......@@ -58,16 +58,8 @@ $PAGE = page_create_object(PAGE_ADMIN, 0); // there must be any constant id numb
$PAGE->set_pagetype('admin-setting-' . $section);
$PAGE->init_extra($section);
if (!isset($USER->adminediting)) {
$USER->adminediting = false;
}
if ($PAGE->user_allowed_editing()) {
if ($adminediting == 1) {
$USER->adminediting = true;
} elseif ($adminediting == 0) {
$USER->adminediting = false;
}
if ($PAGE->user_allowed_editing() && $adminediting != -1) {
$USER->editing = $adminediting;
}
......
......@@ -8,7 +8,7 @@ class block_admin extends block_list {
function get_content() {
global $CFG, $USER, $SITE, $COURSE, $DB;
global $CFG, $USER, $SITE, $COURSE, $DB, $PAGE;
if ($this->content !== NULL) {
return $this->content;
......@@ -57,7 +57,7 @@ class block_admin extends block_list {
if ($course->id !== SITEID and has_capability('moodle/course:update', $context)) {
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/edit.gif" class="icon" alt="" />';
if (isediting($this->instance->pageid)) {
if ($PAGE->user_is_editing()) {
$this->content->items[]='<a href="view.php?id='.$this->instance->pageid.'&amp;edit=off&amp;sesskey='.sesskey().'">'.get_string('turneditingoff').'</a>';
} else {
$this->content->items[]='<a href="view.php?id='.$this->instance->pageid.'&amp;edit=on&amp;sesskey='.sesskey().'">'.get_string('turneditingon').'</a>';
......
......@@ -14,7 +14,7 @@ class block_course_summary extends block_base {
}
function get_content() {
global $CFG, $COURSE;
global $CFG, $COURSE, $PAGE;
if($this->content !== NULL) {
return $this->content;
......@@ -28,7 +28,7 @@ class block_course_summary extends block_base {
$options = new object();
$options->noclean = true; // Don't clean Javascripts etc
$this->content->text = format_text($COURSE->summary, FORMAT_HTML, $options);
if (isediting($COURSE->id)) { // ?? courseid param not there??
if ($PAGE->user_is_editing()) {
if($COURSE->id == SITEID) {
$editpage = $CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=frontpagesettings';
} else {
......
......@@ -11,7 +11,7 @@ class block_site_main_menu extends block_list {
}
function get_content() {
global $USER, $CFG, $COURSE, $DB;
global $USER, $CFG, $COURSE, $DB, $PAGE;
if ($this->content !== NULL) {
return $this->content;
......@@ -35,8 +35,8 @@ class block_site_main_menu extends block_list {
require_once($CFG->dirroot.'/course/lib.php');
$context = get_context_instance(CONTEXT_COURSE, $course->id);
$isediting = isediting($this->instance->pageid) && has_capability('moodle/course:manageactivities', $context);
$modinfo =& get_fast_modinfo($course);
$isediting = $PAGE->user_is_editing() && has_capability('moodle/course:manageactivities', $context);
$modinfo = get_fast_modinfo($course);
/// extra fast view mode
if (!$isediting) {
......
......@@ -11,7 +11,7 @@ class block_social_activities extends block_list {
}
function get_content() {
global $USER, $CFG, $COURSE, $DB;
global $USER, $CFG, $COURSE, $DB, $PAGE;
if ($this->content !== NULL) {
return $this->content;
......@@ -35,7 +35,7 @@ class block_social_activities extends block_list {
require_once($CFG->dirroot.'/course/lib.php');
$context = get_context_instance(CONTEXT_COURSE, $course->id);
$isediting = isediting($this->instance->pageid) && has_capability('moodle/course:manageactivities', $context);
$isediting = $PAGE->user_is_editing() && has_capability('moodle/course:manageactivities', $context);
$modinfo = get_fast_modinfo($course);
/// extra fast view mode
......
......@@ -56,18 +56,6 @@ class page_blog extends page_base {
return false;
}
// Also, admins are considered to have "always on" editing (I wanted to avoid duplicating
// the code that turns editing on/off here; you can roll your own or copy course/view.php).
function user_is_editing() {
global $SESSION;
if (isloggedin() && !isguest()) {
$this->editing = !empty($SESSION->blog_editing_enabled);
return $this->editing;
}
return false;
}
//over-ride parent method's print_header because blog already passes more than just the title along
function print_header($pageTitle='', $pageHeading='', $pageNavigation='', $pageFocus='', $pageMeta='') {
global $USER;
......@@ -82,18 +70,18 @@ class page_blog extends page_base {
/////////// Blog page specific functions
function get_extra_header_string() {
global $SESSION, $CFG, $USER;
global $CFG, $USER;
$editformstring = '';
if ($this->user_allowed_editing()) {
if (!empty($SESSION->blog_editing_enabled)) {
if ($this->user_is_editing()) {
$editingString = get_string('turneditingoff');
} else {
$editingString = get_string('turneditingon');
}
$params = $this->url->params();
$params['edit'] = empty($SESSION->blog_editing_enabled) ? 1 : 0;
$params['edit'] = $this->user_is_editing() ? 0 : 1;
$paramstring = '';
foreach ($params as $key=>$val) {
$paramstring .= '<input type="hidden" name="'.$key.'" value="'.s($val).'" />';
......
......@@ -29,10 +29,9 @@ if (empty($CFG->bloglevel)) {
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
// change block edit staus if not guest and logged in
if (isloggedin() and !isguest() and $edit != -1) {
$SESSION->blog_editing_enabled = $edit;
$USER->editing = $edit;
}
if (empty($filtertype)) {
......
......@@ -53,17 +53,6 @@
}
/**
* Adaptation of isediting in moodlelib.php for blog module
* @return bool
*/
function blog_isediting() {
global $SESSION;
return !empty($SESSION->blog_editing_enabled);
}
/**
* This function is in lib and not in BlogInfo because entries being searched
* might be found in any number of blogs rather than just one.
......
......@@ -39,9 +39,9 @@
if (update_category_button($category->id)) {
if ($categoryedit !== -1) {
$USER->categoryediting = $categoryedit;
$USER->editing = $categoryedit;
}
$editingon = !empty($USER->categoryediting);
$editingon = $PAGE->user_is_editing();
$navbaritem = update_category_button($category->id); // Must call this again after updating the state.
} else {
$navbaritem = print_course_search('', true, 'navbar');
......
......@@ -125,7 +125,7 @@
$section = 0;
$thissection = $sections[$section];
if ($thissection->summary or $thissection->sequence or isediting($course->id)) {
if ($thissection->summary or $thissection->sequence or $PAGE->user_is_editing()) {
// Note, no need for a 'left side' cell or DIV.
// Note, 'right side' is BEFORE content.
......@@ -141,7 +141,7 @@
$summaryformatoptions->noclean = true;
echo format_text($summarytext, FORMAT_HTML, $summaryformatoptions);
if (isediting($course->id) && has_capability('moodle/course:update', $coursecontext)) {
if ($PAGE->user_is_editing() && has_capability('moodle/course:update', $coursecontext)) {
echo '<a title="'.$streditsummary.'" '.
' href="editsection.php?id='.$thissection->id.'"><img src="'.$CFG->pixpath.'/t/edit.gif" '.
' class="icon edit" alt="'.$streditsummary.'" /></a>';
......@@ -150,7 +150,7 @@
print_section($course, $thissection, $mods, $modnamesused);
if (isediting($course->id)) {
if ($PAGE->user_is_editing()) {
print_section_add_menus($course, $section, $modnames);
}
......@@ -227,7 +227,7 @@
'<img src="'.$CFG->pixpath.'/i/one.gif" class="icon" alt="'.$strshowonlytopic.'" /></a><br />';
}
if (isediting($course->id) && has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id))) {
if ($PAGE->user_is_editing() && has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id))) {
if ($course->marker == $section) { // Show the "light globe" on/off
echo '<a href="view.php?id='.$course->id.'&amp;marker=0&amp;sesskey='.sesskey().'#section-'.$section.'" title="'.$strmarkedthistopic.'">'.'<img src="'.$CFG->pixpath.'/i/marked.gif" alt="'.$strmarkedthistopic.'" /></a><br />';
......@@ -266,7 +266,7 @@
echo '&nbsp;';
}
if (isediting($course->id) && has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id))) {
if ($PAGE->user_is_editing() && has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id))) {
echo ' <a title="'.$streditsummary.'" href="editsection.php?id='.$thissection->id.'">'.
'<img src="'.$CFG->pixpath.'/t/edit.gif" class="icon edit" alt="'.$streditsummary.'" /></a><br /><br />';
}
......@@ -274,7 +274,7 @@
print_section($course, $thissection, $mods, $modnamesused);
if (isediting($course->id)) {
if ($PAGE->user_is_editing()) {
print_section_add_menus($course, $section, $modnames);
}
}
......
......@@ -115,7 +115,7 @@
$section = 0;
$thissection = $sections[$section];
if ($thissection->summary or $thissection->sequence or isediting($course->id)) {
if ($thissection->summary or $thissection->sequence or $PAGE->user_is_editing()) {
// Note, 'right side' is BEFORE content.
echo '<li id="section-0" class="section main" >';
......@@ -131,7 +131,7 @@
$summaryformatoptions->noclean = true;
echo format_text($summarytext, FORMAT_HTML, $summaryformatoptions);
if (isediting($course->id) && has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id))) {
if ($PAGE->user_is_editing() && has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id))) {
echo '<p><a title="'.$streditsummary.'" '.
' href="editsection.php?id='.$thissection->id.'"><img src="'.$CFG->pixpath.'/t/edit.gif" '.
' class="icon edit" alt="'.$streditsummary.'" /></a></p>';
......@@ -140,7 +140,7 @@
print_section($course, $thissection, $mods, $modnamesused);
if (isediting($course->id)) {
if ($PAGE->user_is_editing()) {
print_section_add_menus($course, $section, $modnames);
}
......@@ -223,7 +223,7 @@
'<img src="'.$CFG->pixpath.'/i/one.gif" class="icon wkone" alt="'.$strshowonlyweek.'" /></a><br />';
}
if (isediting($course->id) && has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id))) {
if ($PAGE->user_is_editing() && has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id))) {
if ($thissection->visible) { // Show the hide/show eye
echo '<a href="view.php?id='.$course->id.'&amp;hide='.$section.'&amp;sesskey='.sesskey().'#section-'.$section.'" title="'.$strweekhide.'">'.
'<img src="'.$CFG->pixpath.'/i/hide.gif" class="icon hide" alt="'.$strweekhide.'" /></a><br />';
......@@ -256,7 +256,7 @@
$summaryformatoptions->noclean = true;
echo format_text($thissection->summary, FORMAT_HTML, $summaryformatoptions);
if (isediting($course->id) && has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id))) {
if ($PAGE->user_is_editing() && has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id))) {
echo ' <a title="'.$streditsummary.'" href="editsection.php?id='.$thissection->id.'">'.
'<img src="'.$CFG->pixpath.'/t/edit.gif" class="icon edit" alt="'.$streditsummary.'" /></a><br /><br />';
}
......@@ -264,7 +264,7 @@
print_section($course, $thissection, $mods, $modnamesused);
if (isediting($course->id)) {
if ($PAGE->user_is_editing()) {
print_section_add_menus($course, $section, $modnames);
}
}
......
......@@ -26,9 +26,9 @@
if (update_category_button()) {
if ($categoryedit !== -1) {
$USER->categoryediting = $categoryedit;
$USER->editing = $categoryedit;
}
$adminediting = !empty($USER->categoryediting);
$adminediting = $PAGE->user_is_editing();
} else {
$adminediting = false;
}
......
......@@ -1367,7 +1367,7 @@ function set_section_visible($courseid, $sectionnumber, $visibility) {
* Prints a section full of activity modules
*/
function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%", $hidecompletion=false) {
global $CFG, $USER, $DB;
global $CFG, $USER, $DB, $PAGE;
static $initialised;
......@@ -1385,7 +1385,7 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false,
if (!isset($initialised)) {
$groupbuttons = ($course->groupmode or (!$course->groupmodeforce));
$groupbuttonslink = (!$course->groupmodeforce);
$isediting = isediting($course->id);
$isediting = $PAGE->user_is_editing();
$ismoving = $isediting && ismoving($course->id);
if ($ismoving) {
$strmovehere = get_string("movehere");
......@@ -2181,7 +2181,7 @@ function print_course_request_buttons($systemcontext) {
* to see it.
*/
function update_category_button($categoryid = 0) {
global $CFG, $USER;
global $CFG, $PAGE;
// Check permissions.
$context = get_category_or_system_context($categoryid);
......@@ -2190,7 +2190,7 @@ function update_category_button($categoryid = 0) {
}
// Work out the appropriate action.
if (!empty($USER->categoryediting)) {
if ($PAGE->user_is_editing()) {
$label = get_string('turneditingoff');
$edit = 'off';
} else {
......
......@@ -35,9 +35,9 @@
if (update_category_button()) {
if ($edit !== -1) {
$USER->categoryediting = $edit;
$USER->editing = $edit;
}
$adminediting = !empty($USER->categoryediting);
$adminediting = $PAGE->user_is_editing();
} else {
$adminediting = false;
}
......
......@@ -3931,17 +3931,8 @@ function admin_externalpage_setup($section, $extrabutton = '',
}
$adminediting = optional_param('adminedit', -1, PARAM_BOOL);
if (!isset($USER->adminediting)) {
$USER->adminediting = false;
}
if ($PAGE->user_allowed_editing()) {
if ($adminediting == 1) {
$USER->adminediting = true;
} elseif ($adminediting == 0) {
$USER->adminediting = false;
}
if ($PAGE->user_allowed_editing() && $adminediting != -1) {
$USER->editing = $adminediting;
}
}
......
......@@ -2628,6 +2628,7 @@ function isguestuser($user=NULL) {
}
/**
* @deprecated since Moodle 2.0 - use $PAGE->user_is_editing() instead.
* Determines if the currently logged in user is in editing mode.
* Note: originally this function had $userid parameter - it was not usable anyway
*
......@@ -2635,14 +2636,9 @@ function isguestuser($user=NULL) {
* @return bool
*/
function isediting() {
global $USER, $PAGE;
if (empty($USER->editing)) {
return false;
} elseif (is_object($PAGE) && method_exists($PAGE,'user_allowed_editing')) {
return $PAGE->user_allowed_editing();
}
return true;//false;
global $PAGE;
debugging('call to deprecated function isediting(). Please use $PAGE->user_is_editing() instead', DEBUG_DEVELOPER);
return $PAGE->user_is_editing();
}
/**
......
......@@ -211,6 +211,25 @@ class moodle_page {
}
}
/// Other information getting methods ==========================================
/**
* @return boolean should the current user see this page in editing mode.
* That is, are they allowed to edit this page, and are they currently in
* editing mode.
*/
public function user_is_editing() {
global $USER;
return !empty($USER->editing) && $this->user_allowed_editing();
}
/**
* @return boolean does the user have permission to see this page in editing mode.
*/
public function user_allowed_editing() {
return true; // TODO
}
/// Setter methods =============================================================
/**
......@@ -738,20 +757,6 @@ class page_base extends moodle_page {
/// Class Functions
// USER-RELATED THINGS
// By default, no user is editing anything and none CAN edit anything. Developers
// will have to override these settings to let Moodle know when it should grant
// editing rights to the user viewing the page.
function user_allowed_editing() {
trigger_error('Page class does not implement method <strong>user_allowed_editing()</strong>', E_USER_WARNING);
return false;
}
function user_is_editing() {
trigger_error('Page class does not implement method <strong>user_is_editing()</strong>', E_USER_WARNING);
return false;
}
// HTML OUTPUT SECTION
// We have absolutely no idea what derived pages are all about
......@@ -860,15 +865,6 @@ class page_course extends page_base {
}
}
// Is the user actually editing this course page or "sticky page" right now?
function user_is_editing() {
if (has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_SYSTEM)) && defined('ADMIN_STICKYBLOCKS')) {
//always in edit mode on sticky page
return true;
}
return isediting($this->id);
}
// HTML OUTPUT SECTION
// This function prints out the common part of the page's header.
......@@ -994,11 +990,6 @@ class page_generic_activity extends page_base {
return has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_MODULE, $this->modulerecord->id));
}
function user_is_editing() {
$this->init_full();
return isediting($this->modulerecord->course);
}
function print_header($title, $morenavlinks = NULL, $bodytags = '', $meta = '') {
global $USER, $CFG;
......
......@@ -453,4 +453,39 @@ class moodle_page_categories_test extends UnitTestCaseUsingDatabase {
}
}
/**
* Test functions that affect filter_active table with contextid = $syscontextid.
*/
class moodle_page_editing_test extends UnitTestCase {
protected $testpage;
protected $originaluserediting;
public function setUp() {
global $USER;
$this->originaluserediting = !empty($USER->editing);
$this->testpage = new testable_moodle_page();
}
public function tearDown() {
global $USER;
$this->testpage = NULL;
$USER->editing = $this->originaluserediting;
}
public function test_user_is_editing_on() {
// Setup fixture
global $USER;
$USER->editing = true;
// Validate
$this->assertTrue($this->testpage->user_is_editing());
}
public function test_user_is_editing_off() {
// Setup fixture
global $USER;
$USER->editing = false;
// Validate
$this->assertFalse($this->testpage->user_is_editing());
}
}
?>
......@@ -5091,11 +5091,11 @@ function update_module_button($cmid, $ignored, $string) {
*/
function update_categories_search_button($search,$page,$perpage) {
global $CFG, $USER;
global $CFG, $PAGE;
// not sure if this capability is the best here
if (has_capability('moodle/category:manage', get_context_instance(CONTEXT_SYSTEM))) {
if (!empty($USER->categoryediting)) {
if ($PAGE->user_is_editing()) {
$string = get_string("turneditingoff");
$edit = "off";
$perpage = 30;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment