From: skodak Date: Thu, 24 Jan 2008 20:33:50 +0000 (+0000) Subject: MDL-12945 a slightly bigger patch for performance in recent activity and various... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=dd97c3286859dff8e9f14f23941d8cb891bba18b;p=moodle.git 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 --- diff --git a/blocks/activity_modules/block_activity_modules.php b/blocks/activity_modules/block_activity_modules.php index 5d45a328ba..7e2f0a5494 100644 --- a/blocks/activity_modules/block_activity_modules.php +++ b/blocks/activity_modules/block_activity_modules.php @@ -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[] = ''.$modnamesplural[$modname].''; - $this->content->icons[] = ''; - } + 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[] = ''.$modfullname.''; + $this->content->icons[] = ''; } } diff --git a/blocks/admin/block_admin.php b/blocks/admin/block_admin.php index 19b03475d3..e50951f809 100644 --- a/blocks/admin/block_admin.php +++ b/blocks/admin/block_admin.php @@ -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[]=''; if (isediting($this->instance->pageid)) { $this->content->items[]=''.get_string('turneditingoff').''; @@ -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[]=''.get_string('assignroles', 'role').''; $this->content->icons[]=''; @@ -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[]=''.get_string('grades').''; $this->content->icons[]=''; } @@ -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[]=''.get_string('outcomes', 'grades').''; $this->content->icons[]=''; } @@ -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[]=''.$strgroups.''; $this->content->icons[]=''; @@ -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[]=''.get_string('backup').''; $this->content->icons[]=''; } /// 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[]=''.get_string('restore').''; $this->content->icons[]=''; } /// 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[]=''.get_string('import').''; $this->content->icons[]=''; } /// 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[]=''.get_string('reset').''; $this->content->icons[]=''; } /// 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[]=''.get_string('reports').''; $this->content->icons[]=''; } /// 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[]=''.get_string('files').''; $this->content->icons[]=''; } diff --git a/blocks/calendar_month/block_calendar_month.php b/blocks/calendar_month/block_calendar_month.php index 748d5f8e84..246170f9bd 100644 --- a/blocks/calendar_month/block_calendar_month.php +++ b/blocks/calendar_month/block_calendar_month.php @@ -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(); diff --git a/blocks/calendar_upcoming/block_calendar_upcoming.php b/blocks/calendar_upcoming/block_calendar_upcoming.php index f7d4770896..f531ad2827 100644 --- a/blocks/calendar_upcoming/block_calendar_upcoming.php +++ b/blocks/calendar_upcoming/block_calendar_upcoming.php @@ -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)); diff --git a/blocks/course_list/block_course_list.php b/blocks/course_list/block_course_list.php index 34fed95979..6fa0713ec0 100644 --- a/blocks/course_list/block_course_list.php +++ b/blocks/course_list/block_course_list.php @@ -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 = ''.get_string('course').''; // only for logged in users! diff --git a/blocks/course_summary/block_course_summary.php b/blocks/course_summary/block_course_summary.php index 02cc6800c2..d0c0cf3e0d 100644 --- a/blocks/course_summary/block_course_summary.php +++ b/blocks/course_summary/block_course_summary.php @@ -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 { diff --git a/blocks/glossary_random/block_glossary_random.php b/blocks/glossary_random/block_glossary_random.php index 2b61d43e52..58e8642010 100644 --- a/blocks/glossary_random/block_glossary_random.php +++ b/blocks/glossary_random/block_glossary_random.php @@ -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 = ''.$this->config->addentry.'
'; diff --git a/blocks/mnet_hosts/block_mnet_hosts.php b/blocks/mnet_hosts/block_mnet_hosts.php index 5968e2aed2..9f20d09e21 100644 --- a/blocks/mnet_hosts/block_mnet_hosts.php +++ b/blocks/mnet_hosts/block_mnet_hosts.php @@ -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 ''; diff --git a/blocks/news_items/block_news_items.php b/blocks/news_items/block_news_items.php index b93b350bb0..24b0dbc0dc 100644 --- a/blocks/news_items/block_news_items.php +++ b/blocks/news_items/block_news_items.php @@ -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); diff --git a/blocks/online_users/block_online_users.php b/blocks/online_users/block_online_users.php index 13c14ad7ae..38b03c7b54 100644 --- a/blocks/online_users/block_online_users.php +++ b/blocks/online_users/block_online_users.php @@ -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; } diff --git a/blocks/section_links/block_section_links.php b/blocks/section_links/block_section_links.php index 494619be88..d0acec31c2 100644 --- a/blocks/section_links/block_section_links.php +++ b/blocks/section_links/block_section_links.php @@ -75,25 +75,37 @@ class block_section_links extends block_base { } else { $link = '#section-'; } - $text = '
    '; - 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 .= "
  1. $i
  2. \n"; - } else { - $text .= "
  3. $i
  4. \n"; - } - } - $text .= '
'; - 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 = '
    '; + 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$linktext"; + if ($i == $highlight) { + $text .= "
  1. $i
  2. \n"; + } else { + $text .= "
  3. $i
  4. \n"; + } + } + $text .= '
'; + 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$linktext"; + } } } diff --git a/blocks/site_main_menu/block_site_main_menu.php b/blocks/site_main_menu/block_site_main_menu.php index 9e8b66d382..50bce7ca8f 100644 --- a/blocks/site_main_menu/block_site_main_menu.php +++ b/blocks/site_main_menu/block_site_main_menu.php @@ -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[] = 'extra. + ' href="'.$CFG->wwwroot.'/mod/'.$cm->modname.'/view.php?id='.$cm->id.'">'.$instancename.''; + //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[] = ''; + } + } + } + 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[] = ''. ''.$strmovehere.''; $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[] = ''.$instancename.''.$editbuttons; - //Accessibility: incidental image - should be empty Alt text + //Accessibility: incidental image - should be empty Alt text $this->content->icons[] = ''; } } @@ -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 = ''; diff --git a/blocks/social_activities/block_social_activities.php b/blocks/social_activities/block_social_activities.php index 2a67f4d868..3fa3cca5f0 100644 --- a/blocks/social_activities/block_social_activities.php +++ b/blocks/social_activities/block_social_activities.php @@ -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(); $this->content->items = array(); $this->content->icons = array(); $this->content->footer = ''; @@ -26,20 +26,63 @@ class block_social_activities extends block_list { return $this->content; } - $course = get_record('course', 'id', $this->instance->pageid); + 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); + $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[] = 'extra. + ' href="'.$CFG->wwwroot.'/mod/'.$cm->modname.'/view.php?id='.$cm->id.'">'.$instancename.''; + //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[] = ''; + } + } + } + return $this->content; + } + + +/// slow & hacky editing mode + $ismoving = ismoving($this->instance->pageid); + $sections = get_all_sections($this->instance->pageid); - // To make our day, we start with an ugly hack - global $sections, $mods, $modnames; + if(!empty($sections) && isset($sections[0])) { + $section = $sections[0]; + } - $section = $sections[0]; - // That wasn't so bad, was it? + if (!empty($section)) { + get_all_mods($this->instance->pageid, $mods, $modnames, $modnamesplural, $modnamesused); + } $groupbuttons = $course->groupmode; $groupbuttonslink = (!$course->groupmodeforce); - $viewhiddenactivities = has_capability('moodle/course:viewhiddenactivities', $context); - $isediting = isediting($this->instance->pageid); - $ismoving = ismoving($this->instance->pageid); + if ($ismoving) { $strmovehere = get_string('movehere'); $strmovefull = strip_tags(get_string('movefull', '', "'$USER->activitycopyname'")); @@ -47,7 +90,6 @@ class block_social_activities 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) { @@ -55,14 +97,14 @@ class block_social_activities extends block_list { $this->content->items[] = $USER->activitycopyname.' ('.$strcancel.')'; } - if (!empty($section->sequence)) { + if (!empty($section) && !empty($section->sequence)) { $sectionmods = explode(',', $section->sequence); foreach ($sectionmods as $modnumber) { if (empty($mods[$modnumber])) { continue; } $mod = $mods[$modnumber]; - if ($isediting && !$ismoving) { + if (!$ismoving) { if ($groupbuttons) { if (! $mod->groupmodelink = $groupbuttonslink) { $mod->groupmode = $course->groupmode; @@ -75,25 +117,25 @@ class block_social_activities extends block_list { } else { $editbuttons = ''; } - if ($mod->visible || $viewhiddenactivities) { + if ($mod->visible || has_capability('moodle/course:viewhiddenactivities', $context)) { if ($ismoving) { if ($mod->id == $USER->activitycopy) { continue; } $this->content->items[] = ''. - ''.$strmovehere.''; + ''.$strmovehere.''; $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'; } @@ -104,7 +146,8 @@ class block_social_activities extends block_list { } else { $this->content->items[] = ''.$instancename.''.$editbuttons; - $this->content->icons[] = ''.$mod->modfullname.''; + //Accessibility: incidental image - should be empty Alt text + $this->content->icons[] = ''; } } } @@ -112,11 +155,11 @@ class block_social_activities extends block_list { if ($ismoving) { $this->content->items[] = ''. - ''.$strmovehere.''; + ''.$strmovehere.''; $this->content->icons[] = ''; } - if ($isediting && $modnames) { + if ($modnames) { $this->content->footer = print_section_add_menus($course, 0, $modnames, true, true); } else { $this->content->footer = ''; diff --git a/calendar/lib.php b/calendar/lib.php index d4d9ac4d3e..dbeb4a6a95 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -69,7 +69,7 @@ $CALENDARDAYS = array('sunday','monday','tuesday','wednesday','thursday','friday function calendar_get_mini($courses, $groups, $users, $cal_month = false, $cal_year = false) { global $CFG, $USER; - $display = &New stdClass; + $display = new stdClass; $display->minwday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY); $display->maxwday = $display->minwday + 6; @@ -363,9 +363,9 @@ function calendar_get_popup($is_today, $event_timestart, $popupcontent='') { } function calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxevents, $fromtime=0) { - global $CFG; + global $CFG, $COURSE; - $display = &new stdClass; + $display = new stdClass; $display->range = $daysinfuture; // How many days in the future we 'll look $display->maxevents = $maxevents; @@ -404,42 +404,55 @@ function calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxeve } } - if($events !== false) { + if ($events !== false) { + + $modinfo =& get_fast_modinfo($COURSE); foreach($events as $event) { - if(!empty($event->modulename)) { - $mod = get_coursemodule_from_instance($event->modulename, $event->instance); - if (!groups_course_module_visible($mod)) { - continue; - } - } - - - if ($event->modulename == 'assignment'){ - if(!calendar_edit_event_allowed($event)){ // cannot manage entries, eg. student - if(!$assignment = get_record('assignment','id',$event->instance)){ - // error("assignment ID was incorrect"); + + if (!empty($event->modulename)) { + if ($event->courseid == $COURSE->id) { + if (isset($modinfo->instances[$event->modulename][$event->instance])) { + $cm = $modinfo->instances[$event->modulename][$event->instance]; + if (!$cm->uservisible) { + continue; + } + } + } else { + if (!$cm = get_coursemodule_from_instance($event->modulename, $event->instance)) { continue; } - // assign assignment to assignment object to use hidden_is_hidden method - require_once($CFG->dirroot.'/mod/assignment/lib.php'); - - if (!file_exists($CFG->dirroot.'/mod/assignment/type/'.$assignment->assignmenttype.'/assignment.class.php')) { + if (!coursemodule_visible_for_user($cm)) { continue; } - require_once ($CFG->dirroot.'/mod/assignment/type/'.$assignment->assignmenttype.'/assignment.class.php'); - - $assignmentclass = 'assignment_'.$assignment->assignmenttype; - $assignmentinstance = new $assignmentclass($mod->id,$assignment); - - if ($assignmentinstance->description_is_hidden()){//force not to show description before availability - $event->description = get_string('notavailableyet', 'assignment'); + } + if ($event->modulename == 'assignment'){ + // TODO: rewrite this hack somehow + if (!calendar_edit_event_allowed($event)){ // cannot manage entries, eg. student + if(!$assignment = get_record('assignment','id',$event->instance)){ + // error("assignment ID was incorrect"); + continue; + } + // assign assignment to assignment object to use hidden_is_hidden method + require_once($CFG->dirroot.'/mod/assignment/lib.php'); + + if (!file_exists($CFG->dirroot.'/mod/assignment/type/'.$assignment->assignmenttype.'/assignment.class.php')) { + continue; + } + require_once ($CFG->dirroot.'/mod/assignment/type/'.$assignment->assignmenttype.'/assignment.class.php'); + + $assignmentclass = 'assignment_'.$assignment->assignmenttype; + $assignmentinstance = new $assignmentclass($cm->id, $assignment, $cm); + + if ($assignmentinstance->description_is_hidden()){//force not to show description before availability + $event->description = get_string('notavailableyet', 'assignment'); + } } } } - if($processed >= $display->maxevents) { + if ($processed >= $display->maxevents) { break; } @@ -1121,7 +1134,7 @@ function calendar_get_course_cached(&$coursecache, $courseid) { return $coursecache[$courseid]; } -function calendar_session_vars() { +function calendar_session_vars($course=null) { global $SESSION, $USER; if(!empty($USER->id) && isset($USER->realuser) && !isset($SESSION->cal_loggedinas)) { @@ -1158,9 +1171,12 @@ function calendar_session_vars() { if(!isset($SESSION->cal_show_user)) { $SESSION->cal_show_user = true; } - // if(empty($SESSION->cal_courses_shown)) { + if (isset($course)) { + // speedup hack for calendar related blocks + $SESSION->cal_courses_shown = array($course->id => $course); + } else { $SESSION->cal_courses_shown = calendar_get_default_courses(true); - //} + } if(empty($SESSION->cal_users_shown)) { // The empty() instead of !isset() here makes a whole world of difference, // as it will automatically change to the user's id when the user first logs @@ -1204,7 +1220,7 @@ function calendar_set_filters(&$courses, &$group, &$user, $courseeventsfrom = NU $c[$courseeventsfrom] = get_record('course', 'id', $courseeventsfrom); $courseeventsfrom = $c; } else if (is_array($courseeventsfrom)) { // case of an array of ints, e.g. course home page - foreach ($courseeventsfrom as $i=>$courseid) { + foreach ($courseeventsfrom as $i=>$courseid) { // TODO: this seems wrong, the array is often constructed as [courseid] => 1 ??? if (is_int($courseid)) { $courseeventsfrom[$i] = get_record('course', 'id', $courseid); } diff --git a/course/lib.php b/course/lib.php index 18cdab7b3a..4a8003041e 100644 --- a/course/lib.php +++ b/course/lib.php @@ -2,10 +2,6 @@ // Library of useful functions -if (defined('COURSE_MAX_LOG_DISPLAY')) { // Being included again - should never happen!! - return; -} - define('COURSE_MAX_LOG_DISPLAY', 150); // days define('COURSE_MAX_LOGS_PER_PAGE', 1000); // records define('COURSE_LIVELOG_REFRESH', 60); // Seconds @@ -26,221 +22,6 @@ define('MOD_CLASS_ACTIVITY', 0); define('MOD_CLASS_RESOURCE', 1); -function print_recent_selector_form($course, $advancedfilter=0, $selecteduser=0, $selecteddate="lastlogin", - $mod="", $modid="activity/All", $modaction="", $selectedgroup="", $selectedsort="default") { - - global $USER, $CFG; - - if ($advancedfilter) { - - // Get all the possible users - $users = array(); - - if ($courseusers = get_course_users($course->id, '', '', 'u.id, u.firstname, u.lastname')) { - foreach ($courseusers as $courseuser) { - $users[$courseuser->id] = fullname($courseuser, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id))); - } - } - if ($guest = get_guest()) { - $users[$guest->id] = fullname($guest); - } - - if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) { - if ($ccc = get_records("course", "", "", "fullname")) { - foreach ($ccc as $cc) { - if ($cc->category) { - $courses["$cc->id"] = "$cc->fullname"; - } else { - $courses["$cc->id"] = " $cc->fullname (Site)"; - } - } - } - asort($courses); - } - - $activities = array(); - - $selectedactivity = $modid; - - /// Casting $course->modinfo to string prevents one notice when the field is null - if ($modinfo = unserialize((string)$course->modinfo)) { - $section = 0; - if ($course->format == 'weeks') { // Body - $strsection = get_string("week"); - } else { - $strsection = get_string("topic"); - } - - $activities["activity/All"] = "All activities"; - $activities["activity/Assignments"] = "All assignments"; - $activities["activity/Chats"] = "All chats"; - $activities["activity/Forums"] = "All forums"; - $activities["activity/Quizzes"] = "All quizzes"; - $activities["activity/Workshops"] = "All workshops"; - - $activities["section/individual"] = "------------- Individual Activities --------------"; - - foreach ($modinfo as $mod) { - if ($mod->mod == "label") { - continue; - } - if (!$mod->visible and !has_capability('moodle/course:viewhiddenactivities',get_context_instance(CONTEXT_MODULE, $mod->cm))) { - continue; - } - $mod->id = $mod->cm; - if (!groups_course_module_visible($mod)) { - continue; - } - - if ($mod->section > 0 and $section <> $mod->section) { - $activities["section/$mod->section"] = "-------------- $strsection $mod->section --------------"; - } - $section = $mod->section; - $mod->name = strip_tags(format_string(urldecode($mod->name),true)); - if (strlen($mod->name) > 55) { - $mod->name = substr($mod->name, 0, 50)."..."; - } - if (!$mod->visible) { - $mod->name = "(".$mod->name.")"; - } - $activities["$mod->cm"] = $mod->name; - - if ($mod->cm == $modid) { - $selectedactivity = "$mod->cm"; - } - } - } - - $strftimedate = get_string("strftimedate"); - $strftimedaydate = get_string("strftimedaydate"); - - asort($users); - - // Get all the possible dates - // Note that we are keeping track of real (GMT) time and user time - // User time is only used in displays - all calcs and passing is GMT - - $timenow = time(); // GMT - - // What day is it now for the user, and when is midnight that day (in GMT). - $timemidnight = $today = usergetmidnight($timenow); - - $dates = array(); - $dates["$USER->lastlogin"] = get_string("lastlogin").", ".userdate($USER->lastlogin, $strftimedate); - $dates["$timemidnight"] = get_string("today").", ".userdate($timenow, $strftimedate); - - if (!$course->startdate or ($course->startdate > $timenow)) { - $course->startdate = $course->timecreated; - } - - $numdates = 1; - while ($timemidnight > $course->startdate and $numdates < 365) { - $timemidnight = $timemidnight - 86400; - $timenow = $timenow - 86400; - $dates["$timemidnight"] = userdate($timenow, $strftimedaydate); - $numdates++; - } - - if ($selecteddate === "lastlogin") { - $selecteddate = $USER->lastlogin; - } - - echo '
'; - echo ''; - echo "
"; - echo ""; - - if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) { - echo ""; - } else { - echo ''; - } - - $sortfields = array("default" => get_string("bycourseorder"),"dateasc" => get_string("datemostrecentlast"), "datedesc" => get_string("datemostrecentfirst")); - - echo ""; - - echo '"; - - echo ""; - - echo '"; - - echo ''; - - $groupmode = groups_get_course_groupmode($course); - - if ($groupmode == VISIBLEGROUPS or ($groupmode and has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id)))) { - if ($groups = groups_get_all_groups($course->id)) { - $group_names = array(); - foreach($groups as $group) { - $group_names[$group->id] = format_string($group->name); - } - echo ''; - } - } - - - echo '"; - - echo "
" . get_string("courses") . ""; - choose_from_menu ($courses, "id", $course->id, ""); - echo "
" . get_string("participants") . ""; - choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") ); - echo "' . get_string("since") . ''; - choose_from_menu ($dates, "date", $selecteddate, get_string("alldays")); - echo "
" . get_string("activities") . ""; - choose_from_menu ($activities, "modid", $selectedactivity, ""); - echo "' . get_string("sortby") . ""; - choose_from_menu ($sortfields, "sortby", $selectedsort, ""); - echo "
'; - if ($groupmode == VISIBLEGROUPS) { - print_string('groupsvisible'); - } else { - print_string('groupsseparate'); - } - echo ':'; - choose_from_menu($group_names, "selectedgroup", $selectedgroup, get_string("allgroups"), "", ""); - echo ''; - echo ''; - echo "
"; - - $advancedlink = "wwwroot/course/recent.php?id=$course->id&advancedfilter=0\">" . get_string("normalfilter") . ""; - print_heading($advancedlink); - echo "
"; - echo "
"; - - } else { - - $day_list = array("1","7","14","21","30"); - $strsince = get_string("since"); - $strlastlogin = get_string("lastlogin"); - $strday = get_string("day"); - $strdays = get_string("days"); - - $heading = ""; - foreach ($day_list as $count) { - if ($count == "1") { - $day = $strday; - } else { - $day = $strdays; - } - $tmpdate = time() - ($count * 3600 * 24); - $heading = $heading . - "wwwroot/course/recent.php?id=$course->id&date=$tmpdate\"> $count $day | "; - } - - $heading = $strsince . ": wwwroot/course/recent.php?id=$course->id\">$strlastlogin" . " | " . $heading; - print_heading($heading); - - $advancedlink = "wwwroot/course/recent.php?id=$course->id&advancedfilter=1\">" . get_string("advancedfilter") . ""; - print_heading($advancedlink); - - } - -} - - function make_log_url($module, $url) { switch ($module) { case 'user': @@ -1018,7 +799,9 @@ function print_recent_activity($course) { $context = get_context_instance(CONTEXT_COURSE, $course->id); - $timestart = time() - COURSE_MAX_RECENT_PERIOD; + $viewfullnames = has_capability('moodle/site:viewfullnames', $context); + + $timestart = round(time() - COURSE_MAX_RECENT_PERIOD, -2); // better db caching for guests - 100 seconds if (!has_capability('moodle/legacy:guest', $context, NULL, false)) { if (!empty($USER->lastcourseaccess[$course->id])) { @@ -1037,123 +820,119 @@ function print_recent_activity($course) { echo "\n"; - - // Firstly, have there been any new enrolments? - - $heading = false; $content = false; +/// Firstly, have there been any new enrolments? + $users = get_recent_enrolments($course->id, $timestart); //Accessibility: new users now appear in an
    list. if ($users) { echo '
    '; - if (! $heading) { - print_headline(get_string("newusers").':', 3); - $heading = true; - $content = true; - } + print_headline(get_string("newusers").':', 3); + $content = true; echo "
      \n"; foreach ($users as $user) { - - $fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id))); - echo '
    1. id&course=$course->id\">$fullname
    2. \n"; + $fullname = fullname($user, $viewfullnames); + echo '
    3. wwwroot/user/view.php?id=$user->id&course=$course->id\">$fullname
    4. \n"; } echo "
    \n
    \n"; } - // Next, have there been any modifications to the course structure? +/// Next, have there been any modifications to the course structure? + + $modinfo =& get_fast_modinfo($course); + + $changelist = array(); - $logs = get_records_select('log', "time > '$timestart' AND course = '$course->id' AND - module = 'course' AND action LIKE '% mod'", "time ASC"); + $logs = get_records_select('log', "time > $timestart AND course = $course->id AND + module = 'course' AND + (action = 'add mod' OR action = 'update mod' OR action = 'delete mod')", + "id ASC"); if ($logs) { + $actions = array('add mod', 'update mod', 'delete mod'); + $newgones = array(); // added and later deleted items foreach ($logs as $key => $log) { + if (!in_array($log->action, $actions)) { + continue; + } $info = split(' ', $log->info); - if ($info[0] == 'label') { // Labels are special activities + if ($info[0] == 'label') { // Labels are ignored in recent activity continue; } - $modname = get_field($info[0], 'name', 'id', $info[1]); - //Create a temp valid module structure (course,id) - $tempmod->course = $log->course; - $tempmod->id = $info[1]; - //Obtain the visible property from the instance - $modvisible = instance_is_visible($info[0],$tempmod); - - //Only if the mod is visible - if ($modvisible) { - switch ($log->action) { - case 'add mod': - $stradded = get_string('added', 'moodle', get_string('modulename', $info[0])); - $changelist[$log->info] = array ('operation' => 'add', 'text' => "$stradded:
    wwwroot/course/$log->url\">".format_string($modname,true).""); - break; - case 'update mod': - $strupdated = get_string('updated', 'moodle', get_string('modulename', $info[0])); - if (empty($changelist[$log->info])) { - $changelist[$log->info] = array ('operation' => 'update', 'text' => "$strupdated:
    wwwroot/course/$log->url\">".format_string($modname,true).""); - } - break; - case 'delete mod': - if (!empty($changelist[$log->info]['operation']) and - $changelist[$log->info]['operation'] == 'add') { - $changelist[$log->info] = NULL; - } else { - $strdeleted = get_string('deletedactivity', 'moodle', get_string('modulename', $info[0])); - $changelist[$log->info] = array ('operation' => 'delete', 'text' => $strdeleted); - } - break; + if (count($info) != 2) { + debugging("Incorrect log entry info: id = ".$log->id, DEBUG_DEVELOPER); + continue; + } + + $modname = $info[0]; + $instanceid = $info[1]; + + if ($log->action == 'delete mod') { + // unfortunately we do not know if the mod was visible + if (!array_key_exists($log->info, $newgones)) { + $strdeleted = get_string('deletedactivity', 'moodle', get_string('modulename', $modname)); + $changelist[$log->info] = array ('operation' => 'delete', 'text' => $strdeleted); + } + } else { + if (!isset($modinfo->instances[$modname][$instanceid])) { + if ($log->action == 'add mod') { + // do not display added and later deleted activities + $newgones[$log->info] = true; + } + continue; + } + $cm = $modinfo->instances[$modname][$instanceid]; + if (!$cm->uservisible) { + //continue; + } + + if ($log->action == 'add mod') { + $stradded = get_string('added', 'moodle', get_string('modulename', $modname)); + $changelist[$log->info] = array('operation' => 'add', 'text' => "$stradded:
    wwwroot/mod/$cm->modname/view.php?id={$cm->id}\">".format_string($cm->name, true).""); + + } else if ($log->action == 'update mod' and empty($changelist[$log->info])) { + $strupdated = get_string('updated', 'moodle', get_string('modulename', $modname)); + $changelist[$log->info] = array('operation' => 'update', 'text' => "$strupdated:
    wwwroot/mod/$cm->modname/view.php?id={$cm->id}\">".format_string($cm->name, true).""); } } } } if (!empty($changelist)) { + print_headline(get_string('courseupdates').':', 3); + $content = true; foreach ($changelist as $changeinfo => $change) { - if ($change) { - $changes[$changeinfo] = $change; - } - } - if (isset($changes)){ - if (count($changes) > 0) { - print_headline(get_string('courseupdates').':', 3); - $content = true; - foreach ($changes as $changeinfo => $change) { - echo '

    '.$change['text'].'

    '; - } - } + echo '

    '.$change['text'].'

    '; } } - // Now display new things from each module - - $mods = get_records('modules', 'visible', '1', 'name', 'id, name'); +/// Now display new things from each module - $viewfullnames = has_capability('moodle/site:viewfullnames', $context); + $usedmodules = array(); + foreach($modinfo->cms as $cm) { + if (isset($usedmodules[$cm->modname])) { + continue; + } + if (!$cm->uservisible) { + continue; + } + $usedmodules[$cm->modname] = $cm->modname; + } - foreach ($mods as $mod) { // Each module gets it's own logs and prints them - if (file_exists($CFG->dirroot.'/mod/'.$mod->name.'/lib.php')) { - include_once($CFG->dirroot.'/mod/'.$mod->name.'/lib.php'); - $print_recent_activity = $mod->name.'_print_recent_activity'; + foreach ($usedmodules as $modname) { // Each module gets it's own logs and prints them + if (file_exists($CFG->dirroot.'/mod/'.$modname.'/lib.php')) { + include_once($CFG->dirroot.'/mod/'.$modname.'/lib.php'); + $print_recent_activity = $modname.'_print_recent_activity'; if (function_exists($print_recent_activity)) { - // - // NOTE: - // $isteacher (second parameter below) is to be deprecated! - // - // TODO: - // 1) Make sure that all _print_recent_activity functions are - // not using the $isteacher value. - // 2) Eventually, remove the $isteacher parameter from the - // function calls. - // - $modcontent = $print_recent_activity($course, $viewfullnames, $timestart); - if ($modcontent) { - $content = true; - } + // NOTE: original $isteacher (second parameter below) was replaced with $viewfullnames! + $content = $print_recent_activity($course, $viewfullnames, $timestart) || $content; } } else { - /// Really need to indicate an error here to admins. Is there a way to do this? + debugging("Missing lib.php in lib/{$mod->name} - please reinstall files or uninstall the module"); } } @@ -1180,7 +959,7 @@ function get_array_of_activities($courseid) { $mod = array(); if (!$rawmods = get_course_mods($courseid)) { - return NULL; + return $mod; // always return array } if ($sections = get_records("course_sections", "course", $courseid, "section ASC")) { @@ -1191,14 +970,16 @@ function get_array_of_activities($courseid) { if (empty($rawmods[$seq])) { continue; } - $mod[$seq]->cm = $rawmods[$seq]->id; - $mod[$seq]->mod = $rawmods[$seq]->modname; - $mod[$seq]->section = $section->section; - $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance)); - $mod[$seq]->visible = $rawmods[$seq]->visible; - $mod[$seq]->groupingid = $rawmods[$seq]->groupingid; + $mod[$seq]->id = $rawmods[$seq]->instance; + $mod[$seq]->cm = $rawmods[$seq]->id; + $mod[$seq]->mod = $rawmods[$seq]->modname; + $mod[$seq]->section = $section->section; + $mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance)); + $mod[$seq]->visible = $rawmods[$seq]->visible; + $mod[$seq]->groupmode = $rawmods[$seq]->groupmode; + $mod[$seq]->groupingid = $rawmods[$seq]->groupingid; $mod[$seq]->groupmembersonly = $rawmods[$seq]->groupmembersonly; - $mod[$seq]->extra = ""; + $mod[$seq]->extra = ""; $modname = $mod[$seq]->mod; $functionname = $modname."_get_coursemodule_info"; @@ -1223,15 +1004,141 @@ function get_array_of_activities($courseid) { } +/** + * Returns reference to full info about modules in course (including visibility). + * Cached and as fast as possible (0 or 1 db query). + * @param $course object or 'reset' string to reset caches + * @return mixed courseinfo object or nothing if resetting + */ +function &get_fast_modinfo($course, $userid=0) { + global $CFG, $USER; + + static $cache = array(); + + if ($course === 'reset') { + $cache = array(); + $nothing = null; + return $nothing; // we must return some reference + } + + if (empty($userid)) { + $userid = $USER->id; + } + + if (array_key_exists($course->id, $cache) and $cache[$course->id]->userid == $userid) { + return $cache[$course->id]; + } + + if (empty($course->modinfo)) { + // no modinfo yet - load it + rebuild_course_cache($course->id); + if (!$course = get_record('course', 'id', $course->id)) { + error("Can not find course id: $course->id"); + } + } + + $modinfo = new object(); + $modinfo->courseid = $course->id; + $modinfo->userid = $userid; + $modinfo->sections = array(); + $modinfo->cms = array(); + $modinfo->instances = array(); + $modinfo->groups = null; // loaded only when really needed - the only one db query + + $info = unserialize($course->modinfo); + if (!is_array($info)) { + // hmm, something is wrong - lets try to fix it + rebuild_course_cache($course->id); + if (!$course = get_record('course', 'id', $course->id)) { + error("Can not find course id: $course->id"); + } + $info = unserialize($course->modinfo); + if (!is_array($info)) { + return $modinfo; + } + } + + if ($info) { + // detect if upgrade required + $first = reset($info); + if (!isset($first->id)) { + rebuild_course_cache($course->id); + if (!$course = get_record('course', 'id', $course->id)) { + error("Can not find course id: $course->id"); + } + $info = unserialize($course->modinfo); + if (!is_array($info)) { + return $modinfo; + } + } + } + + $modlurals = array(); + + foreach ($info as $mod) { + // reconstruct minimalistic $cm + $cm = new object(); + $cm->id = $mod->cm; + $cm->instance = $mod->id; + $cm->course = $course->id; + $cm->modname = $mod->mod; + $cm->name = urldecode($mod->name); + $cm->visible = $mod->visible; + $cm->section = $mod->section; + $cm->groupmode = $mod->groupmode; + $cm->groupingid = $mod->groupingid; + $cm->groupmembersonly = $mod->groupmembersonly; + $cm->extra = isset($mod->extra) ? urldecode($mod->extra) : ''; + $cm->icon = isset($mod->icon) ? $mod->icon : ''; + $cm->uservisible = true; + + // preload long names plurals - used very often + if (!isset($modlurals[$cm->modname])) { + $modlurals[$cm->modname] = get_string('modulenameplural', $cm->modname); + } + $cm->modplural = $modlurals[$cm->modname]; + + if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_MODULE, $cm->id), $userid)) { + $cm->uservisible = false; + + } else if (!empty($CFG->enablegroupings) and !empty($cm->groupmembersonly) + and !has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id), $userid)) { + if (is_null($modinfo->groups)) { + $modinfo->groups = groups_get_user_groups($course->id, $userid); + } + if (empty($modinfo->groups[$cm->groupingid])) { + $cm->uservisible = false; + } + } + + if (!isset($modinfo->instances[$cm->modname])) { + $modinfo->instances[$cm->modname] = array(); + } + $modinfo->instances[$cm->modname][$cm->instance] =& $cm; + $modinfo->cms[$cm->id] =& $cm; + + // reconstruct sections + if (!isset($modinfo->sections[$cm->section])) { + $modinfo->sections[$cm->section] = array(); + } + $modinfo->sections[$cm->section][] = $cm->id; + + unset($cm); + } + + $cache[$course->id] = $modinfo; + + return $cache[$course->id]; +} function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) { // Returns a number of useful structures for course displays - $mods = NULL; // course modules indexed by id - $modnames = NULL; // all course module names (except resource!) - $modnamesplural= NULL; // all course module names (plural form) - $modnamesused = NULL; // course module names used + $mods = array(); // course modules indexed by id + $modnames = array(); // all course module names (except resource!) + $modnamesplural= array(); // all course module names (plural form) + $modnamesused = array(); // course module names used if ($allmods = get_records("modules")) { foreach ($allmods as $mod) { @@ -1250,15 +1157,16 @@ function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modname if (empty($modnames[$mod->modname])) { continue; } + $mods[$mod->id] = $mod; + $mods[$mod->id]->modfullname = $modnames[$mod->modname]; + if (!$mod->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE, $courseid))) { + continue; + } // Check groupings if (!groups_course_module_visible($mod)) { continue; } - $mods[$mod->id] = $mod; - $mods[$mod->id]->modfullname = $modnames[$mod->modname]; - if ($mod->visible or has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE, $courseid))) { - $modnamesused[$mod->modname] = $modnames[$mod->modname]; - } + $modnamesused[$mod->modname] = $modnames[$mod->modname]; } if ($modnamesused) { asort($modnamesused, SORT_LOCALE_STRING); @@ -1285,6 +1193,7 @@ function course_set_display($courseid, $display=0) { } else if (record_exists("course_display", "userid", $USER->id, "course", $courseid)) { set_field("course_display", "display", $display, "userid", $USER->id, "course", $courseid); } else { + $record = new object(); $record->userid = $USER->id; $record->course = $courseid; $record->display = $display; @@ -1317,41 +1226,43 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false, /// Prints a section full of activity modules global $CFG, $USER; + static $initialised; + static $groupbuttons; static $groupbuttonslink; - static $isteacher; static $isediting; static $ismoving; static $strmovehere; static $strmovefull; static $strunreadpostsone; - static $untracked; static $usetracking; + static $groupings; - $labelformatoptions = New stdClass; - if (!isset($isteacher)) { + if (!isset($initialised)) { $groupbuttons = ($course->groupmode or (!$course->groupmodeforce)); $groupbuttonslink = (!$course->groupmodeforce); - $isediting = isediting($course->id); - $ismoving = $isediting && ismoving($course->id); + $isediting = isediting($course->id); + $ismoving = $isediting && ismoving($course->id); if ($ismoving) { - $strmovehere = get_string("movehere"); - $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'")); + $strmovehere = get_string("movehere"); + $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'")); } include_once($CFG->dirroot.'/mod/forum/lib.php'); if ($usetracking = forum_tp_can_track_forums()) { - $strunreadpostsone = get_string('unreadpostsone', 'forum'); - $untracked = forum_tp_get_untracked_forums($USER->id, $course->id); + $strunreadpostsone = get_string('unreadpostsone', 'forum'); + $untracked = forum_tp_get_untracked_forums($USER->id, $course->id); } + $initialised = true; } + + $labelformatoptions = new object(); $labelformatoptions->noclean = true; /// Casting $course->modinfo to string prevents one notice when the field is null - $modinfo = unserialize((string)$course->modinfo); + $modinfo = get_fast_modinfo($course); - $groupings = groups_get_all_groupings($course->id); //Acccessibility: replace table with list