Skip to content

Commit

Permalink
MDL-13450 and MDL-13303 - sorry for changing forum API so late, but i…
Browse files Browse the repository at this point in the history
…t was needed; major perf improvement for forum; fixed printing of user pictures; merged from MOODLE_19_STABLE
  • Loading branch information
skodak committed Feb 13, 2008
1 parent d19f477 commit 65bcf17
Show file tree
Hide file tree
Showing 33 changed files with 672 additions and 420 deletions.
4 changes: 2 additions & 2 deletions blocks/messages/block_messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function get_content() {
$this->content->footer = '<a href="'.$CFG->wwwroot.'/message/index.php" onclick="this.target=\'message\'; return openpopup(\'/message/index.php\', \'message\', \'menubar=0,location=0,scrollbars,status,resizable,width=400,height=500\', 0);">'.get_string('messages', 'message').'</a>...';

$users = get_records_sql("SELECT m.useridfrom as id, COUNT(m.useridfrom) as count,
u.firstname, u.lastname, u.picture, u.lastaccess
u.firstname, u.lastname, u.picture, u.imagealt, u.lastaccess
FROM {$CFG->prefix}user u,
{$CFG->prefix}message m
WHERE m.useridto = '$USER->id'
Expand All @@ -43,7 +43,7 @@ function get_content() {
foreach ($users as $user) {
$timeago = format_time(time() - $user->lastaccess);
$this->content->text .= '<li class="listentry"><div class="user"><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$this->instance->pageid.'" title="'.$timeago.'">';
$this->content->text .= print_user_picture($user->id, $this->instance->pageid, $user->picture, 0, true, false, '', false);
$this->content->text .= print_user_picture($user, $this->instance->pageid, $user->picture, 0, true, false, '', false);
$this->content->text .= fullname($user).'</a></div>';
$this->content->text .= '<div class="message"><a href="'.$CFG->wwwroot.'/message/discussion.php?id='.$user->id.'" onclick="this.target=\'message_'.$user->id.'\'; return openpopup(\'/message/discussion.php?id='.$user->id.'\', \'message_'.$user->id.'\', \'menubar=0,location=0,scrollbars,status,resizable,width=400,height=500\', 0);"><img class="iconsmall" src="'.$CFG->pixpath.'/t/message.gif" alt="" />&nbsp;'.$user->count.'</a>';
$this->content->text .= '</div></li>';
Expand Down
2 changes: 1 addition & 1 deletion blog/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function blog_print_entry($blogEntry, $viewtype='full', $filtertype='', $filters
echo '<table cellspacing="0" class="forumpost blogpost blog'.$template['publishstate'].'" width="100%">';

echo '<tr class="header"><td class="picture left">';
print_user_picture($template['userid'], SITEID, $user->picture);
print_user_picture($user, SITEID, $user->picture);
echo '</td>';

echo '<td class="topic starter"><div class="subject">'.$template['title'].'</div><div class="author">';
Expand Down
18 changes: 16 additions & 2 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,16 @@ function &get_fast_modinfo(&$course, $userid=0) {

$modlurals = array();

$cmids = array();
$contexts = null;
foreach ($info as $mod) {
$cmids[$mod->cm] = $mod->cm;
}
if ($cmids) {
// preload all module contexts with one query
$contexts = get_context_instance(CONTEXT_MODULE, $cmids);
}

foreach ($info as $mod) {
// reconstruct minimalistic $cm
$cm = new object();
Expand All @@ -1097,11 +1107,11 @@ function &get_fast_modinfo(&$course, $userid=0) {
}
$cm->modplural = $modlurals[$cm->modname];

if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_MODULE, $cm->id), $userid)) {
if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $contexts[$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)) {
and !has_capability('moodle/site:accessallgroups', $contexts[$cm->id], $userid)) {
if (is_null($modinfo->groups)) {
$modinfo->groups = groups_get_user_groups($course->id, $userid);
}
Expand Down Expand Up @@ -1440,6 +1450,10 @@ function print_section_add_menus($course, $section, $modnames, $vertical=false,
if (function_exists($gettypesfunc)) {
$types = $gettypesfunc();
foreach($types as $type) {
if (!isset($type->modclass) or !isset($type->typestr)) {
debugging('Incorrect ativity type in '.$modname);
continue;
}
if ($type->modclass == MOD_CLASS_RESOURCE) {
$resources[$type->type] = $type->typestr;
} else {
Expand Down
77 changes: 63 additions & 14 deletions lib/accesslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2538,7 +2538,7 @@ function cleanup_contexts() {
*/
function get_context_instance($contextlevel, $instance=0) {

global $context_cache, $context_cache_id;
global $context_cache, $context_cache_id, $CFG;
static $allowed_contexts = array(CONTEXT_SYSTEM, CONTEXT_USER, CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_GROUP, CONTEXT_MODULE, CONTEXT_BLOCK);

if ($contextlevel === 'clearcache') {
Expand All @@ -2561,30 +2561,79 @@ function get_context_instance($contextlevel, $instance=0) {
error('Error: get_context_instance() called with incorrect context level "'.s($contextlevel).'"');
}

/// Check the cache
if (isset($context_cache[$contextlevel][$instance])) { // Already cached
return $context_cache[$contextlevel][$instance];
if (!is_array($instance)) {
/// Check the cache
if (isset($context_cache[$contextlevel][$instance])) { // Already cached
return $context_cache[$contextlevel][$instance];
}

/// Get it from the database, or create it
if (!$context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instance)) {
$context = create_context($contextlevel, $instance);
}

/// Only add to cache if context isn't empty.
if (!empty($context)) {
$context_cache[$contextlevel][$instance] = $context; // Cache it for later
$context_cache_id[$context->id] = $context; // Cache it for later
}

return $context;
}

/// Get it from the database, or create it
if (!$context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instance)) {
$context = create_context($contextlevel, $instance);

/// ok, somebody wants to load several contexts to save some db queries ;-)
$instances = $instance;
$result = array();

foreach ($instances as $key=>$instance) {
/// Check the cache first
if (isset($context_cache[$contextlevel][$instance])) { // Already cached
$result[$instance] = $context_cache[$contextlevel][$instance];
unset($instances[$key]);
continue;
}
}

/// Only add to cache if context isn't empty.
if (!empty($context)) {
$context_cache[$contextlevel][$instance] = $context; // Cache it for later
$context_cache_id[$context->id] = $context; // Cache it for later
if ($instances) {
if (count($instances) > 1) {
$instanceids = implode(',', $instances);
$instanceids = "instanceid IN ($instanceids)";
} else {
$instance = reset($instances);
$instanceids = "instanceid = $instance";
}

if (!$contexts = get_records_sql("SELECT instanceid, id, contextlevel, path, depth
FROM {$CFG->prefix}context
WHERE contextlevel=$contextlevel AND $instanceids")) {
$contexts = array();
}

foreach ($instances as $instance) {
if (isset($contexts[$instance])) {
$context = $contexts[$instance];
} else {
$context = create_context($contextlevel, $instance);
}

if (!empty($context)) {
$context_cache[$contextlevel][$instance] = $context; // Cache it for later
$context_cache_id[$context->id] = $context; // Cache it for later
}

$result[$instance] = $context;
}
}

return $context;
return $result;
}


/**
* Get a context instance as an object, from a given context id.
* @param $id a context id.
* @return object The context object.
* @param mixed $id a context id or array of ids.
* @return mixed object or array of the context object.
*/
function get_context_instance_by_id($id) {

Expand Down
22 changes: 14 additions & 8 deletions lib/grouplib.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,31 @@ function groups_get_grouping($groupingid) {
/**
* Gets array of all groups in a specified course.
* @param int $courseid The id of the course.
* @param int $userid optional user id, returns only groups of the user.
* @param mixed $userid optional user id or array of ids, returns only groups of the user.
* @param int $groupingid optional returns only groups in the specified grouping.
* @return array | false Returns an array of the group objects or false if no records
* or an error occurred.
* or an error occurred. (userid field returned if array in $userid)
*/
function groups_get_all_groups($courseid, $userid=0, $groupingid=0) {
function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.*') {
global $CFG;

// groupings are ignored when not enabled
if (empty($CFG->enablegroupings)) {
$groupingid = 0;
}

if (!empty($userid)) {
$userfrom = ", {$CFG->prefix}groups_members gm";
$userwhere = "AND g.id = gm.groupid AND gm.userid = '$userid'";
} else {
if (empty($userid)) {
$userfrom = "";
$userwhere = "";

} else if (is_array($userid)) {
$userids = implode(',', $userid);
$userfrom = ", {$CFG->prefix}groups_members gm";
$userwhere = "AND g.id = gm.groupid AND gm.userid IN ($userids)";

} else {
$userfrom = ", {$CFG->prefix}groups_members gm";
$userwhere = "AND g.id = gm.groupid AND gm.userid = '$userid'";
}

if (!empty($groupingid)) {
Expand All @@ -122,7 +128,7 @@ function groups_get_all_groups($courseid, $userid=0, $groupingid=0) {
$groupingwhere = "";
}

return get_records_sql("SELECT g.*
return get_records_sql("SELECT $fields
FROM {$CFG->prefix}groups g $userfrom $groupingfrom
WHERE g.courseid = $courseid $userwhere $groupingwhere
ORDER BY name ASC");
Expand Down
2 changes: 1 addition & 1 deletion message/discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
print_header(get_string('discussion', 'message').': '.fullname($user), '', '', 'edit-message');
echo '<div class="message-discussion-noframes">';
echo '<div id="userinfo">';
echo print_user_picture($user->id, SITEID, $user->picture, 48, true, true, 'userwindow');
echo print_user_picture($user, SITEID, $user->picture, 48, true, true, 'userwindow');
echo '<div class="name"><h1>'.$userfullname.'</h1></div>';
echo '<div class="commands"><ul>';
if ($contact = get_record('message_contacts', 'userid', $USER->id, 'contactid', $user->id)) {
Expand Down
2 changes: 1 addition & 1 deletion message/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
print_header('','','','','',false,'','',false,'');
echo '<table width="100%" cellpadding="0" cellspacing="0"><tr>';
echo '<td width="100">';
echo print_user_picture($user->id, SITEID, $user->picture, true, true, true, 'userwindow').'</td>';
echo print_user_picture($user, SITEID, $user->picture, true, true, true, 'userwindow').'</td>';
echo '<td valign="middle" align="center">';

echo '<div class="name">'.fullname($user).'</div>';
Expand Down
2 changes: 1 addition & 1 deletion mod/chat/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ function chat_get_users($chatid, $groupid=0, $groupingid=0) {
$groupingjoin = '';
}

return get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping, c.firstping
return get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping, c.firstping, u.imagealt
FROM {$CFG->prefix}chat_users c
INNER JOIN {$CFG->prefix}user u ON u.id = c.userid
$groupingjoin
Expand Down
2 changes: 1 addition & 1 deletion mod/chat/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
arsort($sessionusers);
foreach ($sessionusers as $sessionuser => $usermessagecount) {
if ($user = get_record('user', 'id', $sessionuser)) {
print_user_picture($user->id, $course->id, $user->picture);
print_user_picture($user, $course->id, $user->picture);
echo '&nbsp;'.fullname($user, true); // XXX TODO use capability instead of true
echo "&nbsp;($usermessagecount)<br />";
}
Expand Down
2 changes: 1 addition & 1 deletion mod/chat/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
$lastping = $timenow - $chatuser->lastmessageping;
echo '<tr><td class="chatuserimage">';
echo "<a href=\"$CFG->wwwroot/user/view.php?id=$chatuser->id&amp;course=$chat->course\">";
print_user_picture($chatuser->id, 0, $chatuser->picture, false, false, false);
print_user_picture($chatuser, 0, $chatuser->picture, false, false, false);
echo '</a></td><td class="chatuserdetails">';
echo '<p>';
echo fullname($chatuser).'<br />';
Expand Down
2 changes: 1 addition & 1 deletion mod/data/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,7 @@ function data_print_comment($data, $comment, $page=0) {
echo '<table cellspacing="0" align="center" width="50%" class="datacomment forumpost">';

echo '<tr class="header"><td class="picture left">';
print_user_picture($comment->userid, $data->course, $user->picture);
print_user_picture($user, $data->course, $user->picture);
echo '</td>';

echo '<td class="topic starter" align="left"><div class="author">';
Expand Down
Loading

0 comments on commit 65bcf17

Please sign in to comment.