Commit dd97c328 authored by skodak's avatar skodak
Browse files

MDL-12945 a slightly bigger patch for performance in recent activity and...

MDL-12945 a slightly bigger patch for performance in recent activity and various group related issues + some other fixes - see list of subtasks in tracker; merged from MOODLE_19_STABLE
parent d20737e8
......@@ -7,21 +7,7 @@ class block_activity_modules extends block_list {
}
function get_content() {
global $USER, $CFG;
// TODO: FIX: HACK: (any other tags I should add? :P)
// Hacker's improvised caching scheme: avoid fetching the mod
// data from db if the course format has already fetched them
if(!isset($GLOBALS['modnamesplural']) || !isset($GLOBALS['modnamesused'])) {
require_once($CFG->dirroot.'/course/lib.php');
if (!empty($this->instance)) {
get_all_mods($this->instance->pageid, $mods, $modnames, $modnamesplural, $modnamesused);
}
}
else {
$modnamesplural = $GLOBALS['modnamesplural'];
$modnamesused = $GLOBALS['modnamesused'];
}
global $CFG, $COURSE;
if($this->content !== NULL) {
return $this->content;
......@@ -32,12 +18,30 @@ class block_activity_modules extends block_list {
$this->content->icons = array();
$this->content->footer = '';
if (isset($modnamesused) && $modnamesused) {
foreach ($modnamesused as $modname => $modfullname) {
if ($modname != 'label') {
$this->content->items[] = '<a href="'.$CFG->wwwroot.'/mod/'.$modname.'/index.php?id='.$this->instance->pageid.'">'.$modnamesplural[$modname].'</a>';
$this->content->icons[] = '<img src="'.$CFG->modpixpath.'/'.$modname.'/icon.gif" class="icon" alt="" />';
}
if ($COURSE->id == $this->instance->pageid) {
$course = $COURSE;
} else {
$course = get_record('course', 'id', $this->instance->pageid);
}
require_once($CFG->dirroot.'/course/lib.php');
$modinfo = get_fast_modinfo($course);
$modfullanmes = array();
foreach($modinfo->cms as $cm) {
if (!$cm->uservisible) {
continue;
}
$modfullnames[$cm->modname] = $cm->modplural;
}
asort($modfullnames, SORT_LOCALE_STRING);
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->icons[] = '<img src="'.$CFG->modpixpath.'/'.$modname.'/icon.gif" class="icon" alt="" />';
}
}
......
......@@ -49,7 +49,7 @@ class block_admin extends block_list {
/// Course editing on/off
if (has_capability('moodle/course:update', $context) && ($course->id!==SITEID)) {
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)) {
$this->content->items[]='<a href="view.php?id='.$this->instance->pageid.'&amp;edit=off&amp;sesskey='.sesskey().'">'.get_string('turneditingoff').'</a>';
......@@ -63,7 +63,7 @@ class block_admin extends block_list {
/// Assign roles to the course
if (has_capability('moodle/role:assign', $context) && ($course->id!==SITEID)) {
if ($course->id !== SITEID and 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>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/roles.gif" class="icon" alt="" />';
......@@ -71,16 +71,20 @@ class block_admin extends block_list {
/// View course grades (or just your own grades, same link)
/// find all accessible reports
if ($course->id!==SITEID) {
if ($reports = get_list_of_plugins('grade/report', 'CVS')) { // Get all installed reports
foreach ($reports as $key => $plugin) { // Remove ones we can't see
if (!has_capability('gradereport/'.$plugin.':view', $context)) {
unset($reports[$key]);
if ($course->id !== SITEID) {
$reportavailable = false;
if ($reports = get_list_of_plugins('grade/report')) { // Get all installed reports
arsort($reports); // user is last, we want to test it first
foreach ($reports as $plugin) {
if (has_capability('gradereport/'.$plugin.':view', $context)) {
//stop when fisrt visible found
$reportavailable = true;
break;
}
}
}
if (!empty($reports)) {
if ($reportavailable) {
$this->content->items[]='<a href="'.$CFG->wwwroot.'/grade/report/index.php?id='.$this->instance->pageid.'">'.get_string('grades').'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/grades.gif" class="icon" alt="" />';
}
......@@ -88,7 +92,7 @@ class block_admin extends block_list {
/// Course outcomes (to help give it more prominence because it's important)
if (!empty($CFG->enableoutcomes)) {
if (has_capability('moodle/course:update', $context) && ($course->id!==SITEID)) {
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->icons[]='<img src="'.$CFG->pixpath.'/i/outcomes.gif" class="icon" alt="" />';
}
......@@ -110,7 +114,7 @@ class block_admin extends block_list {
/// Manage groups in this course
if (($course->groupmode || !$course->groupmodeforce) && has_capability('moodle/course:managegroups', $context) && ($course->id!==SITEID)) {
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->icons[]='<img src="'.$CFG->pixpath.'/i/group.gif" class="icon" alt="" />';
......@@ -118,37 +122,37 @@ class block_admin extends block_list {
/// Backup this course
if (has_capability('moodle/site:backup', $context)&& ($course->id!==SITEID)) {
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->icons[]='<img src="'.$CFG->pixpath.'/i/backup.gif" class="icon" alt="" />';
}
/// Restore to this course
if (has_capability('moodle/site:restore', $context) && ($course->id!==SITEID)) {
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->icons[]='<img src="'.$CFG->pixpath.'/i/restore.gif" class="icon" alt="" />';
}
/// Import data from other courses
if (has_capability('moodle/site:import', $context) && ($course->id!==SITEID)) {
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->icons[]='<img src="'.$CFG->pixpath.'/i/restore.gif" class="icon" alt="" />';
}
/// Reset this course
if (has_capability('moodle/course:reset', $context) && ($course->id!==SITEID)) {
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->icons[]='<img src="'.$CFG->pixpath.'/i/return.gif" class="icon" alt="" />';
}
/// View course reports
if (has_capability('moodle/site:viewreports', $context) && ($course->id!==SITEID)) {
if ($course->id !== SITEID and has_capability('moodle/site:viewreports', $context)) {
$this->content->items[]='<a href="'.$CFG->wwwroot.'/course/report.php?id='.$this->instance->pageid.'">'.get_string('reports').'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/stats.gif" class="icon" alt="" />';
}
/// Manage questions
if ($course->id!==SITEID){
if ($course->id !== SITEID){
$questionlink = '';
$questioncaps = array(
'moodle/question:add',
......@@ -176,7 +180,7 @@ class block_admin extends block_list {
/// Manage files
if (has_capability('moodle/course:managefiles', $context) && ($course->id!==SITEID)) {
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->icons[]='<img src="'.$CFG->pixpath.'/i/files.gif" class="icon" alt="" />';
}
......
......@@ -20,8 +20,8 @@ class block_calendar_month extends block_base {
if ($this->content !== NULL) {
return $this->content;
}
// Initialize the session variables
calendar_session_vars();
// Reset the session variables
calendar_session_vars($COURSE);
$this->content = new stdClass;
$this->content->text = '';
......@@ -35,11 +35,12 @@ class block_calendar_month extends block_base {
if($courseshown == SITEID) {
// Being displayed at site level. This will cause the filter to fall back to auto-detecting
// the list of courses it will be grabbing events from.
$filtercourse = NULL;
}
else {
$filtercourse = NULL;
$groupeventsfrom = NULL;
} else {
// Forcibly filter events to include only those from the particular course we are in.
$filtercourse = array($courseshown => 1);
$filtercourse = array($courseshown => $COURSE);
$groupeventsfrom = array($courseshown => 1);
}
// We 'll need this later
......@@ -53,7 +54,7 @@ class block_calendar_month extends block_base {
// Be VERY careful with the format for default courses arguments!
// Correct formatting is [courseid] => 1 to be concise with moodlelib.php functions.
calendar_set_filters($courses, $group, $user, $filtercourse, $filtercourse);
calendar_set_filters($courses, $group, $user, $filtercourse, $groupeventsfrom, false);
if ($courseshown == SITEID) {
// For the front page
$this->content->text .= calendar_overlib_html();
......
......@@ -16,8 +16,8 @@ class block_calendar_upcoming extends block_base {
if ($this->content !== NULL) {
return $this->content;
}
// Initialize the session variables
calendar_session_vars();
// Reset the session variables
calendar_session_vars($COURSE);
$this->content = new stdClass;
$this->content->text = '';
......@@ -42,10 +42,12 @@ class block_calendar_upcoming extends block_base {
if ($courseshown == SITEID) {
// Being displayed at site level. This will cause the filter to fall back to auto-detecting
// the list of courses it will be grabbing events from.
$filtercourse = NULL;
$filtercourse = NULL;
$groupeventsfrom = NULL;
} else {
// Forcibly filter events to include only those from the particular course we are in.
$filtercourse = array($courseshown => 1);
$filtercourse = array($courseshown => $COURSE);
$groupeventsfrom = array($courseshown => 1);
}
}
......@@ -55,7 +57,7 @@ class block_calendar_upcoming extends block_base {
// Be VERY careful with the format for default courses arguments!
// Correct formatting is [courseid] => 1 to be concise with moodlelib.php functions.
calendar_set_filters($courses, $group, $user, $filtercourse, $filtercourse, false);
calendar_set_filters($courses, $group, $user, $filtercourse, $groupeventsfrom, false);
$events = calendar_get_upcoming($courses, $group, $user,
get_user_preferences('calendar_lookahead', CALENDAR_UPCOMING_DAYS),
get_user_preferences('calendar_maxevents', CALENDAR_UPCOMING_MAXEVENTS));
......
......@@ -110,6 +110,12 @@ class block_course_list extends block_list {
function get_remote_courses() {
global $THEME, $CFG, $USER;
if (!is_enabled_auth('mnet')) {
// no need to query anything remote related
return;
}
$icon = '<img src="'.$CFG->pixpath.'/i/mnethost.gif" class="icon" alt="'.get_string('course').'" />';
// only for logged in users!
......
......@@ -24,10 +24,11 @@ class block_course_summary extends block_base {
return '';
}
$this->content = New stdClass;
$this->content = new object();
$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)) {
if (isediting($COURSE->id)) { // ?? courseid param not there??
if($COURSE->id == SITEID) {
$editpage = $CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=frontpagesettings';
} else {
......
......@@ -189,13 +189,10 @@ class block_glossary_random extends block_base {
// place link to glossary in the footer if the glossary is visible
//Create a temp valid module structure (course,id)
$tempmod->course = $this->course->id;
$tempmod->id = $glossaryid;
$cm = get_coursemodule_from_instance('glossary',$glossaryid, $this->course->id) ;
//Obtain the visible property from the instance
if (instance_is_visible('glossary', $tempmod)) {
$cm = get_coursemodule_from_instance('glossary',$glossaryid, $this->course->id) ;
if (coursemodule_visible_for_user($cm)) {
if (has_capability('mod/glossary:write', get_context_instance(CONTEXT_MODULE, $cm->id))) {
$this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?id='.$cm->id
.'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
......
......@@ -26,6 +26,11 @@ class block_mnet_hosts extends block_list {
return false;
}
if (!is_enabled_auth('mnet')) {
// no need to query anything remote related
return '';
}
// check for outgoing roaming permission first
if (!has_capability('moodle/site:mnetlogintoremote', get_context_instance(CONTEXT_SYSTEM), NULL, false)) {
return '';
......
......@@ -32,9 +32,11 @@ class block_news_items extends block_base {
return '';
}
if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $COURSE->id)) {
$modinfo = get_fast_modinfo($COURSE);
if (empty($modinfo->instances['forum'][$forum->id])) {
return '';
}
$cm = $modinfo->instances['forum'][$forum->id];
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
......
......@@ -83,23 +83,26 @@ class block_online_users extends block_base {
$pcontext = get_related_contexts_string($context);
if ($pusers = get_records_sql($SQL, 0, 50)) { // We'll just take the most recent 50 maximum
foreach ($pusers as $puser) {
$hidden = false;
if (!has_capability('moodle/role:viewhiddenassigns', $context)) {
// if current user can't view hidden role assignment in this context and
// user has a hidden role assigned at this context or any parent contexts,
// ignore this user
$SQL = "SELECT id FROM {$CFG->prefix}role_assignments
WHERE userid = $puser->id
AND contextid $pcontext
AND hidden = 1";
if (!has_capability('moodle/role:viewhiddenassigns', $context) && record_exists_sql($SQL)) {
// can't see this user as the current user has no capability
// and this user has a hidden assignment at this context or higher
continue;
$userids = array_keys($pusers);
$userids = implode(',', $userids);
$sql = "SELECT userid
FROM {$CFG->prefix}role_assignments
WHERE userid IN ($userids) AND contextid $pcontext AND hidden = 1
GROUP BY userid";
$hidden = get_records_sql($sql);
}
foreach ($pusers as $puser) {
if ($hidden and isset($hidden[$puser->id])) {
continue;
}
$puser->fullname = fullname($puser);
$users[$puser->id] = $puser;
}
......
......@@ -75,25 +75,37 @@ class block_section_links extends block_base {
} else {
$link = '#section-';
}
$text = '<ol class="inline-list">';
for ($i = $inc; $i <= $course->numsections; $i += $inc) {
$isvisible = get_field('course_sections', 'visible', 'course', $this->instance->pageid, 'section', $i);
if (!$isvisible and !has_capability('moodle/course:update', $context)) {
continue;
}
$style = ($isvisible) ? '' : ' class="dimmed"';
if ($i == $highlight) {
$text .= "<li><a href=\"$link$i\"$style><strong>$i</strong></a></li>\n";
} else {
$text .= "<li><a href=\"$link$i\"$style>$i</a></li>\n";
}
}
$text .= '</ol>';
if ($highlight) {
$isvisible = get_field('course_sections', 'visible', 'course', $this->instance->pageid, 'section', $highlight);
if ($isvisible or has_capability('moodle/course:update', $context)) {
$sql = "SELECT section, visible
FROM {$CFG->prefix}course_sections
WHERE course = $course->id AND
section < ".($course->numsections+1)."
ORDER BY section";
if ($sections = get_records_sql($sql)) {
$text = '<ol class="inline-list">';
for ($i = $inc; $i <= $course->numsections; $i += $inc) {
if (!isset($sections[$i])) {
continue;
}
$isvisible = $sections[$i]->visible;
if (!$isvisible and !has_capability('moodle/course:update', $context)) {
continue;
}
$style = ($isvisible) ? '' : ' class="dimmed"';
$text .= "\n<a href=\"$link$highlight\"$style>$linktext</a>";
if ($i == $highlight) {
$text .= "<li><a href=\"$link$i\"$style><strong>$i</strong></a></li>\n";
} else {
$text .= "<li><a href=\"$link$i\"$style>$i</a></li>\n";
}
}
$text .= '</ol>';
if ($highlight and isset($sections[$highlight])) {
$isvisible = $sections[$highlight]->visible;
if ($isvisible or has_capability('moodle/course:update', $context)) {
$style = ($isvisible) ? '' : ' class="dimmed"';
$text .= "\n<a href=\"$link$highlight\"$style>$linktext</a>";
}
}
}
......
......@@ -11,13 +11,13 @@ class block_site_main_menu extends block_list {
}
function get_content() {
global $USER, $CFG;
global $USER, $CFG, $COURSE;
if ($this->content !== NULL) {
return $this->content;
}
$this->content = new stdClass;
$this->content = new object();
$this->content->items = array();
$this->content->icons = array();
$this->content->footer = '';
......@@ -26,19 +26,57 @@ class block_site_main_menu extends block_list {
return $this->content;
}
$course = get_record('course', 'id', $this->instance->pageid);
$context = get_context_instance(CONTEXT_COURSE, $course->id);
if ($COURSE->id == $this->instance->pageid) {
$course = $COURSE;
} else {
$course = get_record('course', 'id', $this->instance->pageid);
}
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);
$ismoving = ismoving($this->instance->pageid);
$modinfo =& get_fast_modinfo($course);
/// extra fast view mode
if (!$isediting) {
if (!empty($modinfo->sections[0])) {
foreach($modinfo->sections[0] as $cmid) {
$cm = $modinfo->cms[$cmid];
if (!$cm->uservisible) {
continue;
}
if ($cm->modname == 'label') {
$this->content->items[] = format_text($cm->extra, FORMAT_HTML);
$this->content->icons[] = '';
} else {
$linkcss = $cm->visible ? '' : ' class="dimmed" ';
$instancename = format_string($cm->name, true, $course->id);
$this->content->items[] = '<a title="'.$cm->modplural.'" '.$linkcss.' '.$cm->extra.
' href="'.$CFG->wwwroot.'/mod/'.$cm->modname.'/view.php?id='.$cm->id.'">'.$instancename.'</a>';
//Accessibility: incidental image - should be empty Alt text
if (!empty($cm->icon)) {
$icon = $CFG->pixpath.'/'.$cm->icon;
} else {
$icon = $CFG->modpixpath.'/'.$cm->modname.'/icon.gif';
}
$this->content->icons[] = '<img src="'.$icon.'" class="icon" alt="" />';
}
}
}
return $this->content;
}
/// slow & hacky editing mode
$ismoving = ismoving($this->instance->pageid);
$sections = get_all_sections($this->instance->pageid);
if(!empty($sections) && isset($sections[0])) {
$section = $sections[0];
}
if (!empty($section) || $isediting) {
if (!empty($section)) {
get_all_mods($this->instance->pageid, $mods, $modnames, $modnamesplural, $modnamesused);
}
......@@ -52,8 +90,6 @@ class block_site_main_menu extends block_list {
$stractivityclipboard = $USER->activitycopyname;
}
/// Casting $course->modinfo to string prevents one notice when the field is null
$modinfo = unserialize((string)$course->modinfo);
$editbuttons = '';
if ($ismoving) {
......@@ -68,7 +104,7 @@ class block_site_main_menu extends block_list {
continue;
}
$mod = $mods[$modnumber];
if ($isediting && !$ismoving) {
if (!$ismoving) {
if ($groupbuttons) {
if (! $mod->groupmodelink = $groupbuttonslink) {
$mod->groupmode = $course->groupmode;
......@@ -89,17 +125,17 @@ class block_site_main_menu extends block_list {
$this->content->items[] = '<a title="'.$strmovefull.'" href="'.$CFG->wwwroot.'/course/mod.php?moveto='.$mod->id.'&amp;sesskey='.$USER->sesskey.'">'.
'<img style="height:16px; width:80px; border:0px" src="'.$CFG->pixpath.'/movehere.gif" alt="'.$strmovehere.'" /></a>';
$this->content->icons[] = '';
}
$instancename = urldecode($modinfo[$modnumber]->name);
}
$instancename = $modinfo->cms[$modnumber]->name;
$instancename = format_string($instancename, true, $this->instance->pageid);
$linkcss = $mod->visible ? '' : ' class="dimmed" ';
if (!empty($modinfo[$modnumber]->extra)) {
$extra = urldecode($modinfo[$modnumber]->extra);
if (!empty($modinfo->cms[$modnumber]->extra)) {
$extra = $modinfo->cms[$modnumber]->extra;
} else {
$extra = '';
}
if (!empty($modinfo[$modnumber]->icon)) {
$icon = $CFG->pixpath.'/'.urldecode($modinfo[$modnumber]->icon);
if (!empty($modinfo->cms[$modnumber]->icon)) {
$icon = $CFG->pixpath.'/'.$modinfo->cms[$modnumber]->icon;
} else {
$icon = $CFG->modpixpath.'/'.$mod->modname.'/icon.gif';
}
......@@ -110,7 +146,7 @@ class block_site_main_menu extends block_list {
} else {
$this->content->items[] = '<a title="'.$mod->modfullname.'" '.$linkcss.' '.$extra.
' href="'.$CFG->wwwroot.'/mod/'.$mod->modname.'/view.php?id='.$mod->id.'">'.$instancename.'</a>'.$editbuttons;
//Accessibility: incidental image - should be empty Alt text
//Accessibility: incidental image - should be empty Alt text
$this->content->icons[] = '<img src="'.$icon.'" class="icon" alt="" />';
}
}
......@@ -123,7 +159,7 @@ class block_site_main_menu extends block_list {
$this->content->icons[] = '';
}
if ($isediting && $modnames) {
if ($modnames) {
$this->content->footer = print_section_add_menus($course, 0, $modnames, true, true);
} else {
$this->content->footer = '';
......
......@@ -2,7 +2,7 @@
class block_social_activities extends block_list {
function init(){
$this->title = get_string('blockname','block_social_activities');
$this->title = get_string('blockname', 'block_social_activities');
$this->version = 2004041800;
}
......@@ -11,13 +11,13 @@ class block_social_activities extends block_list {
}
function get_content() {
global $USER, $CFG;
global $USER, $CFG, $COURSE;
if ($this->content !== NULL) {
return $this->content;
}
$this->content = new stdClass;
$this->content = new object();