MDL-19756 MDL-19973 Fixed support of url by user_picture, and improved API for adding a popup action (set $userpic->popup = true)

This commit is contained in:
nicolasconnault 2009-07-31 02:39:38 +00:00
parent e82eb34ad0
commit 2c78f462ed
2 changed files with 37 additions and 6 deletions

View File

@ -2379,7 +2379,7 @@ class moodle_core_renderer extends moodle_renderer_base {
*/ */
public function blocks_for_region($region) { public function blocks_for_region($region) {
$blockcontents = $this->page->blocks->get_content_for_region($region, $this); $blockcontents = $this->page->blocks->get_content_for_region($region, $this);
$output = ''; $output = '';
foreach ($blockcontents as $bc) { foreach ($blockcontents as $bc) {
if ($bc instanceof block_contents) { if ($bc instanceof block_contents) {
@ -2727,14 +2727,18 @@ class moodle_core_renderer extends moodle_renderer_base {
$output = $this->image($userpic->image); $output = $this->image($userpic->image);
if (!empty($userpic->link) && !empty($userpic->url)) { if (!empty($userpic->url)) {
$actions = $userpic->get_actions(); $actions = $userpic->get_actions();
if (!empty($actions)) { if ($userpic->popup && !empty($actions)) {
$link = new html_link(); $link = new html_link();
$link->url = $userpic->url; $link->url = $userpic->url;
$link->text = fullname($userpic->user); $link->text = fullname($userpic->user);
$link->add_action($actions[0]); $link->title = fullname($userpic->user);
$output = $this->link_to_popup($link);
foreach ($actions as $action) {
$link->add_action($action);
}
$output = $this->link_to_popup($link, $userpic->image);
} else { } else {
$output = $this->link(prepare_url($userpic->url), $output); $output = $this->link(prepare_url($userpic->url), $output);
} }
@ -4550,6 +4554,10 @@ class user_picture extends moodle_html_component {
* @var boolean $alttext add non-blank alt-text to the image. (Default true, set to false for purely * @var boolean $alttext add non-blank alt-text to the image. (Default true, set to false for purely
*/ */
public $alttext = true; public $alttext = true;
/**
* @var boolean $popup Whether or not to open the link in a popup window
*/
public $popup = false;
/** /**
* Constructor: sets up the other components in case they are needed * Constructor: sets up the other components in case they are needed
@ -4610,10 +4618,14 @@ class user_picture extends moodle_html_component {
$this->user = $DB->get_record('user', array('id' => $this->user), 'id,firstname,lastname,imagealt'); $this->user = $DB->get_record('user', array('id' => $this->user), 'id,firstname,lastname,imagealt');
} }
if (!empty($this->link) && empty($this->url)) { if ($this->url === true) {
$this->url = new moodle_url('/user/view.php', array('id' => $this->user->id, 'course' => $this->courseid)); $this->url = new moodle_url('/user/view.php', array('id' => $this->user->id, 'course' => $this->courseid));
} }
if (!empty($this->url) && $this->popup) {
$this->add_action(new popup_action('click', $this->url));
}
if (empty($this->size)) { if (empty($this->size)) {
$file = 'f2'; $file = 'f2';
$this->size = 35; $this->size = 35;

View File

@ -1187,4 +1187,23 @@ class moodle_core_renderer_test extends UnitTestCase {
$this->assert(new ContainsTagWithContents('option', 'value3'), $html); $this->assert(new ContainsTagWithContents('option', 'value3'), $html);
$this->assert(new ContainsTagWithContents('option', 'value4'), $html); $this->assert(new ContainsTagWithContents('option', 'value4'), $html);
} }
public function test_userpicture() {
global $CFG;
// Set up the user with the required fields
$user = new stdClass();
$user->firstname = 'Test';
$user->lastname = 'User';
$user->picture = false;
$user->imagealt = false;
$user->id = 1;
$userpic = new user_picture();
$userpic->user = $user;
$userpic->courseid = 1;
$userpic->url = true;
// Setting popup to true adds JS for the link to open in a popup
$userpic->popup = true;
$html = $this->renderer->user_picture($userpic);
$this->assert(new ContainsTagWithAttributes('a', array('title' => 'Test User', 'href' => $CFG->wwwroot.'/user/view.php?id=1&course=1')), $html);
}
} }