Skip to content

Commit

Permalink
blocklib: MDL-19010 Fix blocks that were relying on ->instance in an …
Browse files Browse the repository at this point in the history
…unfortunate way
  • Loading branch information
tjhunt committed May 6, 2009
1 parent bb46a4f commit 3179b00
Show file tree
Hide file tree
Showing 22 changed files with 119 additions and 213 deletions.
12 changes: 2 additions & 10 deletions blocks/activity_modules/block_activity_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ function get_content() {
$this->content->icons = array();
$this->content->footer = '';

if ($COURSE->id == $this->instance->pageid) {
$course = $COURSE;
} else {
$course = $DB->get_record('course', array('id'=>$this->instance->pageid));
}

if (empty($course)) {
return '';
}
$course = $this->page->course;

require_once($CFG->dirroot.'/course/lib.php');

Expand All @@ -44,7 +36,7 @@ function get_content() {

foreach ($modfullnames as $modname => $modfullname) {
if ($modname != 'label') {
$this->content->items[] = '<a href="'.$CFG->wwwroot.'/mod/'.$modname.'/index.php?id='.$this->instance->pageid.'">'.$modfullname.'</a>';
$this->content->items[] = '<a href="'.$CFG->wwwroot.'/mod/'.$modname.'/index.php?id='.$course->id.'">'.$modfullname.'</a>';
$this->content->icons[] = '<img src="'.$CFG->modpixpath.'/'.$modname.'/icon.gif" class="icon" alt="" />';
}
}
Expand Down
66 changes: 20 additions & 46 deletions blocks/admin/block_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ function init() {
}

function get_content() {

global $CFG, $USER, $SITE, $COURSE, $DB, $PAGE;
global $CFG, $USER, $DB;

if ($this->content !== NULL) {
return $this->content;
Expand All @@ -19,29 +18,8 @@ function get_content() {
$this->content->icons = array();
$this->content->footer = '';

if (empty($this->instance->pageid)) { // sticky
if (!empty($COURSE)) {
$this->instance->pageid = $COURSE->id;
}
}

if (empty($this->instance)) {
return $this->content = '';
} else if ($this->instance->pageid == SITEID) {
// return $this->content = '';
}

if (!empty($this->instance->pageid)) {
$context = get_context_instance(CONTEXT_COURSE, $this->instance->pageid);
if ($COURSE->id == $this->instance->pageid) {
$course = $COURSE;
} else {
$course = $DB->get_record('course', array('id'=>$this->instance->pageid));
}
} else {
$context = get_context_instance(CONTEXT_SYSTEM);
$course = $SITE;
}
$context = $this->page->context;
$course = $this->page->course;

if (!has_capability('moodle/course:view', $context)) { // Just return
return $this->content;
Expand All @@ -54,21 +32,19 @@ function get_content() {
}

/// Course editing on/off

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 ($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>';
if ($this->page->user_is_editing()) {
$this->content->items[]='<a href="view.php?id='.$course->id.'&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>';
$this->content->items[]='<a href="view.php?id='.$course->id.'&amp;edit=on&amp;sesskey='.sesskey().'">'.get_string('turneditingon').'</a>';
}

$this->content->items[]='<a href="'.$CFG->wwwroot.'/course/edit.php?id='.$this->instance->pageid.'">'.get_string('settings').'</a>';
$this->content->items[]='<a href="'.$CFG->wwwroot.'/course/edit.php?id='.$course->id.'">'.get_string('settings').'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/settings.gif" class="icon" alt="" />';
}

/// Assign roles to the course

if ($course->id != SITEID) {
if (has_capability('moodle/role:assign', $context)) {
$this->content->items[]='<a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$context->id.'">'.get_string('assignroles', 'role').'</a>';
Expand Down Expand Up @@ -99,15 +75,15 @@ function get_content() {
}

if ($reportavailable) {
$this->content->items[]='<a href="'.$CFG->wwwroot.'/grade/report/index.php?id='.$this->instance->pageid.'">'.get_string('grades').'</a>';
$this->content->items[]='<a href="'.$CFG->wwwroot.'/grade/report/index.php?id='.$course->id.'">'.get_string('grades').'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/grades.gif" class="icon" alt="" />';
}
}

/// Course outcomes (to help give it more prominence because it's important)
if (!empty($CFG->enableoutcomes)) {
if ($course->id!==SITEID and has_capability('moodle/course:update', $context)) {
$this->content->items[]='<a href="'.$CFG->wwwroot.'/grade/edit/outcome/course.php?id='.$this->instance->pageid.'">'.get_string('outcomes', 'grades').'</a>';
$this->content->items[]='<a href="'.$CFG->wwwroot.'/grade/edit/outcome/course.php?id='.$course->id.'">'.get_string('outcomes', 'grades').'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/outcomes.gif" class="icon" alt="" />';
}
}
Expand All @@ -116,7 +92,7 @@ function get_content() {
if ($course->metacourse) {
if (has_capability('moodle/course:managemetacourse', $context)) {
$strchildcourses = get_string('childcourses');
$this->content->items[]='<a href="importstudents.php?id='.$this->instance->pageid.'">'.$strchildcourses.'</a>';
$this->content->items[]='<a href="importstudents.php?id='.$course->id.'">'.$strchildcourses.'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/course.gif" class="icon" alt="" />';
} else if (has_capability('moodle/role:assign', $context)) {
$strchildcourses = get_string('childcourses');
Expand All @@ -127,41 +103,39 @@ function get_content() {


/// Manage groups in this course

if (($course->id!==SITEID) && ($course->groupmode || !$course->groupmodeforce) && has_capability('moodle/course:managegroups', $context)) {
$strgroups = get_string('groups');
$this->content->items[]='<a title="'.$strgroups.'" href="'.$CFG->wwwroot.'/group/index.php?id='.$this->instance->pageid.'">'.$strgroups.'</a>';
$this->content->items[]='<a title="'.$strgroups.'" href="'.$CFG->wwwroot.'/group/index.php?id='.$course->id.'">'.$strgroups.'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/group.gif" class="icon" alt="" />';
}

/// Backup this course

if ($course->id!==SITEID and has_capability('moodle/site:backup', $context)) {
$this->content->items[]='<a href="'.$CFG->wwwroot.'/backup/backup.php?id='.$this->instance->pageid.'">'.get_string('backup').'</a>';
$this->content->items[]='<a href="'.$CFG->wwwroot.'/backup/backup.php?id='.$course->id.'">'.get_string('backup').'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/backup.gif" class="icon" alt="" />';
}

/// Restore to this course
if ($course->id !== SITEID and has_capability('moodle/site:restore', $context)) {
$this->content->items[]='<a href="'.$CFG->wwwroot.'/files/index.php?id='.$this->instance->pageid.'&amp;wdir=/backupdata">'.get_string('restore').'</a>';
$this->content->items[]='<a href="'.$CFG->wwwroot.'/files/index.php?id='.$course->id.'&amp;wdir=/backupdata">'.get_string('restore').'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/restore.gif" class="icon" alt="" />';
}

/// Import data from other courses
if ($course->id !== SITEID and has_capability('moodle/site:import', $context)) {
$this->content->items[]='<a href="'.$CFG->wwwroot.'/course/import.php?id='.$this->instance->pageid.'">'.get_string('import').'</a>';
$this->content->items[]='<a href="'.$CFG->wwwroot.'/course/import.php?id='.$course->id.'">'.get_string('import').'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/restore.gif" class="icon" alt="" />';
}

/// Reset this course
if ($course->id!==SITEID and has_capability('moodle/course:reset', $context)) {
$this->content->items[]='<a href="'.$CFG->wwwroot.'/course/reset.php?id='.$this->instance->pageid.'">'.get_string('reset').'</a>';
$this->content->items[]='<a href="'.$CFG->wwwroot.'/course/reset.php?id='.$course->id.'">'.get_string('reset').'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/return.gif" class="icon" alt="" />';
}

/// View course reports
if ($course->id !== SITEID and has_capability('moodle/site:viewreports', $context)) { // basic capability for listing of reports
$this->content->items[]='<a href="'.$CFG->wwwroot.'/course/report.php?id='.$this->instance->pageid.'">'.get_string('reports').'</a>';
$this->content->items[]='<a href="'.$CFG->wwwroot.'/course/report.php?id='.$course->id.'">'.get_string('reports').'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/stats.gif" class="icon" alt="" />';
}

Expand All @@ -187,7 +161,7 @@ function get_content() {
}
if ($questionlink) {
$this->content->items[]='<a href="'.$CFG->wwwroot.'/question/'.$questionlink.
'?courseid='.$this->instance->pageid.'">'.get_string('questions', 'quiz').'</a>';
'?courseid='.$course->id.'">'.get_string('questions', 'quiz').'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/questions.gif" class="icon" alt="" />';
}
}
Expand All @@ -202,7 +176,7 @@ function get_content() {

/// Manage files
if ($course->id !== SITEID and has_capability('moodle/course:managefiles', $context)) {
$this->content->items[]='<a href="'.$CFG->wwwroot.'/files/index.php?id='.$this->instance->pageid.'">'.get_string('files').'</a>';
$this->content->items[]='<a href="'.$CFG->wwwroot.'/files/index.php?id='.$course->id.'">'.get_string('files').'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/files.gif" class="icon" alt=""/>';
}

Expand All @@ -222,10 +196,10 @@ function get_content() {
/// Unenrol link
if (empty($course->metacourse) && ($course->id!==SITEID)) {
if (has_capability('moodle/legacy:guest', $context, NULL, false)) { // Are a guest now
$this->content->items[]='<a href="'.$CFG->wwwroot.'/course/enrol.php?id='.$this->instance->pageid.'">'.get_string('enrolme', '', format_string($course->shortname)).'</a>';
$this->content->items[]='<a href="'.$CFG->wwwroot.'/course/enrol.php?id='.$course->id.'">'.get_string('enrolme', '', format_string($course->shortname)).'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/user.gif" class="icon" alt="" />';
} else if (has_capability('moodle/role:unassignself', $context, NULL, false) and get_user_roles($context, $USER->id, false)) { // Have some role
$this->content->items[]='<a href="'.$CFG->wwwroot.'/course/unenrol.php?id='.$this->instance->pageid.'">'.get_string('unenrolme', '', format_string($course->shortname)).'</a>';
$this->content->items[]='<a href="'.$CFG->wwwroot.'/course/unenrol.php?id='.$course->id.'">'.get_string('unenrolme', '', format_string($course->shortname)).'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/user.gif" class="icon" alt="" />';
}
}
Expand Down
6 changes: 0 additions & 6 deletions blocks/blog_menu/block_blog_menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ function get_content() {

$this->content = new stdClass;
$this->content->footer = '';
if (empty($this->instance) /*|| empty($CFG->blog_version)*/) {
// Either we're being asked for content without
// an associated instance of the Blog module has never been installed.
$this->content->text = $output;
return $this->content;
}

//if ( blog_isLoggedIn() && !isguest() ) {
$courseviewlink = '';
Expand Down
11 changes: 1 addition & 10 deletions blocks/blog_tags/block_blog_tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ function get_content() {
return $this->content;
}

if (empty($this->instance)) {
$this->content = '';
return $this->content;
}

$this->content = new stdClass;
$this->content->text = '';
$this->content->footer = '';
Expand Down Expand Up @@ -140,11 +135,7 @@ function get_content() {

case BLOG_COURSE_LEVEL:
$filtertype = 'course';
if (isset($COURSE->id)) {
$filterselect = $COURSE->id;
} else {
$filterselect = $this->instance->pageid;
}
$filterselect = $COURSE->id;
break;

default:
Expand Down
1 change: 0 additions & 1 deletion blocks/calendar_upcoming/block_calendar_upcoming.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ function get_content() {
$this->content->text = '';

if (empty($this->instance)) { // Overrides: use no course at all

$courseshown = false;
$filtercourse = array();
$this->content->footer = '';
Expand Down
3 changes: 1 addition & 2 deletions blocks/course_summary/block_course_summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ function init() {
}

function specialization() {
global $COURSE;
if($this->instance->pagetype == PAGE_COURSE_VIEW && $COURSE->id != SITEID) {
if($this->page->pagetype == PAGE_COURSE_VIEW && $PAGE->course->id != SITEID) {
$this->title = get_string('coursesummary', 'block_course_summary');
}
}
Expand Down
6 changes: 5 additions & 1 deletion blocks/html/block_html.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function get_content() {
return $this->content;
}

if (!empty($this->instance->pinned) or $this->instance->pagetype === 'course-view') {
if ($this->content_is_trusted()) {
// fancy html allowed only on course page and in pinned blocks for security reasons
$filteropt = new stdClass;
$filteropt->noclean = true;
Expand All @@ -41,6 +41,10 @@ function get_content() {
return $this->content;
}

function content_is_trusted() {
return in_array($this->page->context->contextlevel, array(CONTEXT_COURSE, CONTEXT_COURSECAT, CONTEXT_SYSTEM));
}

/**
* Will be called before an instance of this block is backed up, so that any links in
* any links in any HTML fields on config can be encoded.
Expand Down
2 changes: 1 addition & 1 deletion blocks/html/config_instance.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
$usehtmleditor = can_use_html_editor();

$text = isset($this->config->text) ? $this->config->text : '';
if (empty($this->instance->pinned) and $this->instance->pagetype !== 'course-view') {
if (!$this->content_is_trusted()) {
$text = clean_text($text, FORMAT_HTML);
}
?>
Expand Down
2 changes: 1 addition & 1 deletion blocks/messages/block_messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function get_content() {
$this->content = new stdClass;
$this->content->text = '';
$this->content->footer = '';

if (empty($this->instance) or empty($USER->id) or isguest() or empty($CFG->messaging)) {
return $this->content;
}
Expand Down
Loading

0 comments on commit 3179b00

Please sign in to comment.