Skip to content

Commit

Permalink
MDL-21951, improve comments performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Dongsheng Cai committed Apr 9, 2010
1 parent f854cfa commit 866354a
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 96 deletions.
10 changes: 5 additions & 5 deletions blocks/comments/block_comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ function get_content() {
$this->content->text = '';
//TODO: guest and not-logged-in shoudl be able to read comments, right?
if (isloggedin() && !isguestuser()) { // Show the block
list($context, $course, $cm) = get_context_info_array($this->context->id);
$cmt = new stdclass;
$cmt->context = $this->context;
$cmt->context = $context;
$cmt->course = $course;
$cmt->area = 'block_comments';
$cmt->itemid = $this->instance->id;
$cmt->course = $this->page->course;
// this is a hack to adjust commenting UI
// in block_comments
// this is a hack to adjust commenting UI in block_comments
$cmt->env = 'block_comments';
$cmt->linktext = get_string('showcomments');
$comment = new comment($cmt);

$this->content = new stdClass;
$this->content->text = $comment->output(true);
$this->content->footer = '';

}
return $this->content;
}
Expand Down
3 changes: 2 additions & 1 deletion blog/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public function print_html($return=false) {
$user = $DB->get_record('user', array('id'=>$this->userid));
// Comments
$cmt = new stdClass();
$cmt->contextid = get_context_instance(CONTEXT_USER, $user->id)->id;
$cmt->context = get_context_instance(CONTEXT_USER, $user->id);
$cmt->courseid = $COURSE->id;
$cmt->area = 'format_blog';
$cmt->env = 'blog';
$cmt->itemid = $this->id;
Expand Down
12 changes: 7 additions & 5 deletions comment/comment_ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,22 @@

if (!empty($client_id)) {
$cmt = new stdclass;
$cmt->contextid = $contextid;
if (!empty($course)) {
$cmt->courseid = $course->id;
}
$cmt->context = $context;
$cmt->course = $course;
$cmt->cm = $cm;
$cmt->area = $area;
$cmt->itemid = $itemid;
$cmt->client_id = $client_id;
$comment = new comment($cmt);
} else {
die;
}

switch ($action) {
case 'add':
$cmt = $comment->add($content);
$cmt->count = $comment->count();
if (!empty($cmt) && is_object($cmt)) {
$cmt->count = $comment->count();
$cmt->client_id = $client_id;
echo json_encode($cmt);
}
Expand Down
147 changes: 80 additions & 67 deletions comment/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,18 @@ class comment {
*/
private $template;
private $context;
private $course;
private $courseid;
/**
* course module object
* course module object, only be used to help find pluginname automatically
* if pluginname is specified, it won't be used at all
* @var string
*/
private $cm;
private $plugintype;
/**
* When used in module, it is recommended to use it
* @var string
*/
private $pluginname;
private $viewcap;
private $postcap;
Expand All @@ -74,14 +79,15 @@ class comment {
* @var string
*/
private $linktext;

// static variable will be used by non-js comments UI
private static $nonjs = false;
// will be used by non-js comments UI
private static $comment_itemid = null;
private static $comment_context = null;
private static $comment_area = null;
private static $comment_page = null;
/**
* Construct function of comment class, initilize
* Construct function of comment class, initialise
* class members
* @param object $options
*/
Expand All @@ -95,12 +101,14 @@ public function __construct($options) {
$this->viewcap = false;
$this->postcap = false;

// setup client_id
if (!empty($options->client_id)) {
$this->cid = $options->client_id;
} else {
$this->cid = uniqid();
}


// setup context
if (!empty($options->context)) {
$this->context = $options->context;
$this->contextid = $this->context->id;
Expand All @@ -111,32 +119,71 @@ public function __construct($options) {
print_error('invalidcontext');
}

// setup course
// course will be used to generate user profile link
if (!empty($options->course)) {
$this->courseid = $options->course->id;
} else if (!empty($options->courseid)) {
$this->courseid = $options->courseid;
}

if (!empty($options->pluginname)) {
$this->pluginname = $options->pluginname;
}

// setup coursemodule
if (!empty($options->cm)) {
$this->cm = $options->cm;
} else {
$this->cm = null;
}

// setup commentarea
if (!empty($options->area)) {
$this->commentarea = $options->area;
}

// setup itemid
if (!empty($options->itemid)) {
$this->itemid = $options->itemid;
}

// setup env
if (!empty($options->env)) {
$this->env = $options->env;
} else {
$this->env = '';
}

// setup customized linktext
if (!empty($options->linktext)) {
$this->linktext = $options->linktext;
} else {
$this->linktext = get_string('comments');
}
// setting post and view permissions
$this->check_permissions();

if (!empty($options->showcount)) {
$count = $this->count();
if (empty($count)) {
$this->count = '';
} else {
$this->count = '('.$count.')';
}
} else {
$this->count = '';
}

$this->setup_plugin();

$this->options = new stdclass;
$this->options->context = $this->context;
$this->options->commentarea = $this->commentarea;
$this->options->itemid = $this->itemid;
// setup options for callback functions
$this->args = new stdclass;
$this->args->context = $this->context;
$this->args->courseid = $this->courseid;
$this->args->cm = $this->cm;
$this->args->commentarea = $this->commentarea;
$this->args->itemid = $this->itemid;

// load template
$this->template = <<<EOD
Expand All @@ -147,32 +194,7 @@ public function __construct($options) {
</div>
EOD;
if (!empty($this->plugintype)) {
$this->template = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'template', $this->options, $this->template);
}

// setting post and view permissions
$this->check_permissions();

// setup course
if (!empty($options->course)) {
$this->course = $options->course;
} else if (!empty($options->courseid)) {
$courseid = $options->courseid;
$this->setup_course($courseid);
} else {
$courseid = SITEID;
$this->setup_course($courseid);
}

if (!empty($options->showcount)) {
$count = $this->count();
if (empty($count)) {
$this->count = '';
} else {
$this->count = '('.$count.')';
}
} else {
$this->count = '';
$this->template = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'template', $this->args, $this->template);
}

unset($options);
Expand All @@ -195,22 +217,8 @@ public static function init() {
$PAGE->requires->string_for_js('comments', 'moodle');
}

private function setup_course($courseid) {
global $PAGE, $DB;
if (!empty($this->course)) {
// already set, stop
return;
}
if ($courseid == $PAGE->course->id) {
$this->course = $PAGE->course;
} else if (!$this->course = $DB->get_record('course', array('id'=>$courseid))) {
$this->course = null;
}
}

/**
* Setting module info
*
* Setup plugin type and plugin name
*/
private function setup_plugin() {
global $DB;
Expand All @@ -230,12 +238,18 @@ private function setup_plugin() {
$this->pluginname = $block->blockname;
}
}

if ($this->context->contextlevel == CONTEXT_MODULE) {
$this->plugintype = 'mod';
$this->cm = get_coursemodule_from_id('', $this->context->instanceid);
$this->setup_course($this->cm->course);
$this->modinfo = get_fast_modinfo($this->course);
$this->pluginname = $this->modinfo->cms[$this->cm->id]->modname;
// to improve performance, pluginname should be assigned before initilise comment object
// if it is empty, we will try to guess, it will rarely be used.
if (empty($this->pluginname)) {
if (empty($this->course)) {
$this->course = $DB->get_record('course', array('id'=>$this->courseid), '*', MUST_EXIST);
}
$this->modinfo = get_fast_modinfo($this->course);
$this->pluginname = $this->modinfo->cms[$this->cm->id]->modname;
}
}
}

Expand All @@ -250,7 +264,7 @@ private function check_permissions() {
$this->postcap = has_capability('moodle/comment:post', $this->context);
$this->viewcap = has_capability('moodle/comment:view', $this->context);
if (!empty($this->plugintype)) {
$permissions = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'permissions', $this->options, array('post'=>true, 'view'=>true));
$permissions = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'permissions', $this->args, array('post'=>true, 'view'=>true));
$this->postcap = $this->postcap && $permissions['post'];
$this->viewcap = $this->viewcap && $permissions['view'];
}
Expand All @@ -269,9 +283,9 @@ public function output($return = true) {
$murl = new moodle_url($this->link);
$murl->remove_params('nonjscomment');
$murl->param('nonjscomment', 'true');
$murl->param('comment_itemid', $this->options->itemid);
$murl->param('comment_context', $this->options->context->id);
$murl->param('comment_area', $this->options->commentarea);
$murl->param('comment_itemid', $this->itemid);
$murl->param('comment_context', $this->context->id);
$murl->param('comment_area', $this->commentarea);
$murl->remove_params('comment_page');
$this->link = $murl->out();

Expand All @@ -280,7 +294,7 @@ public function output($return = true) {
$options->commentarea = $this->commentarea;
$options->itemid = $this->itemid;
$options->page = 0;
$options->courseid = $this->course->id;
$options->courseid = $this->courseid;
$options->contextid = $this->contextid;
$options->env = $this->env;
if ($this->env == 'block_comments') {
Expand Down Expand Up @@ -396,7 +410,7 @@ public function get_comments($page = '') {
$candelete = has_capability('moodle/comment:delete', $this->context);
if ($records = $DB->get_records_sql($sql, $params, $start, $CFG->commentsperpage)) {
foreach ($records as &$c) {
$url = $CFG->httpswwwroot.'/user/view.php?id='.$c->userid.'&amp;course='.$this->course->id;
$url = $CFG->httpswwwroot.'/user/view.php?id='.$c->userid.'&amp;course='.$this->courseid;
$c->username = '<a href="'.$url.'">'.fullname($c).'</a>';
$c->time = userdate($c->timecreated, get_string('strftimerecent', 'langconfig'));
$user = new stdclass;
Expand All @@ -413,10 +427,9 @@ public function get_comments($page = '') {
$comments[] = $c;
}
}

if (!empty($this->plugintype)) {
// moodle module will filter comments
$comments = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'display', array($comments, $this->options));
$comments = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'display', array($comments, $this->args), $comments);
}

return $comments;
Expand Down Expand Up @@ -480,7 +493,7 @@ public function add($content, $format = FORMAT_MOODLE) {

if (!empty($this->plugintype)) {
// moodle module will check content
$ret = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'add', array(&$newcmt, $this->options), true);
$ret = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'add', array(&$newcmt, $this->args), true);
if (!$ret) {
throw new comment_exception('modulererejectcomment');
}
Expand Down Expand Up @@ -541,9 +554,9 @@ public function delete($commentid) {
public function print_comments($page = 0, $return = true, $nonjs = true) {
global $DB, $CFG, $PAGE;
$html = '';
if (!(self::$comment_itemid == $this->options->itemid &&
self::$comment_context == $this->options->context->id &&
self::$comment_area == $this->options->commentarea)) {
if (!(self::$comment_itemid == $this->itemid &&
self::$comment_context == $this->context->id &&
self::$comment_area == $this->commentarea)) {
$page = 0;
}
$comments = $this->get_comments($page);
Expand Down Expand Up @@ -575,7 +588,7 @@ public function print_comments($page = 0, $return = true, $nonjs = true) {
<input type="hidden" name="action" value="add" />
<input type="hidden" name="area" value="$this->commentarea" />
<input type="hidden" name="itemid" value="$this->itemid" />
<input type="hidden" name="courseid" value="{$this->course->id}" />
<input type="hidden" name="courseid" value="{$this->courseid}" />
<input type="hidden" name="sesskey" value="{$sesskey}" />
<input type="hidden" name="returnurl" value="{$returnurl}" />
<input type="submit" value="{$strsubmit}" />
Expand Down
16 changes: 14 additions & 2 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,6 @@ function optional_param($parname, $default=NULL, $type=PARAM_CLEAN) {
} else {
return $default;
}

return clean_param($param, $type);
}

Expand Down Expand Up @@ -520,7 +519,6 @@ function validate_param($param, $type, $allownull=NULL_NOT_ALLOWED, $debuginfo='
function clean_param($param, $type) {

global $CFG;

if (is_array($param)) { // Let's loop
$newparam = array();
foreach ($param as $key => $value) {
Expand Down Expand Up @@ -4883,6 +4881,7 @@ function send_password_change_confirmation_email($user) {
$data->lastname = $user->lastname;
$data->sitename = format_string($site->fullname);
$data->link = $CFG->httpswwwroot .'/login/forgot_password.php?p='. $user->secret .'&s='. urlencode($user->username);
debug($data);
$data->admin = generate_email_signoff();

$message = get_string('emailpasswordconfirmation', '', $data);
Expand Down Expand Up @@ -9638,3 +9637,16 @@ function mnet_get_idp_jump_url($user) {
}
return $mnetjumps[$user->mnethostid];
}

function echo_fb($var, $label='info') {
require_once('FirePHPCore/FirePHP.class.php');
$firephp = FirePHP::getInstance(true);
$firephp->log($var, $label);
}

function debug($text) {
$str = time();
$str .= var_export($text, true);
$str .= "\n==\n";
file_put_contents('/Library/WebServer/moodledata/output.log', $str, FILE_APPEND);
}
Loading

0 comments on commit 866354a

Please sign in to comment.