public function test_link_to_popup() { $link = new html_link(); $link->text = 'Click here'; $link->url = 'http://test.com'; $link->title = 'Popup window'; $popupaction = new popup_action('click', 'http://test.com', 'my_popup'); $link->add_action($popupaction); $html = $this->renderer->link_to_popup($link); $expectedattributes = array('title' => 'Popup window', 'href' => 'http://test.com'); $this->assert(new ContainsTagWithAttributes('a', $expectedattributes), $html); $this->assert(new ContainsTagWithContents('a', 'Click here'), $html); // Try a different url for the link than for the popup $link->url = 'http://otheraddress.com'; $html = $this->renderer->link_to_popup($link); $this->assert(new ContainsTagWithAttribute('a', 'title', 'Popup window'), $html); $this->assert(new ContainsTagWithAttribute('a', 'href', 'http://otheraddress.com'), $html); $this->assert(new ContainsTagWithContents('a', 'Click here'), $html); // Give it a moodle_url object instead of a string $link->url = new moodle_url('http://otheraddress.com'); $html = $this->renderer->link_to_popup($link); $this->assert(new ContainsTagWithAttribute('a', 'title', 'Popup window'), $html); $this->assert(new ContainsTagWithAttribute('a', 'href', 'http://otheraddress.com'), $html); $this->assert(new ContainsTagWithContents('a', 'Click here'), $html); }
/** * Print the specified user's avatar. * * This method can be used in two ways: * <pre> * // Option 1: * $userpic = new moodle_user_picture(); * // Set properties of $userpic * $OUTPUT->user_picture($userpic); * * // Option 2: (shortcut for simple cases) * // $user has come from the DB and has fields id, picture, imagealt, firstname and lastname * $OUTPUT->user_picture($user, $COURSE->id); * </pre> * * @param object $userpic Object with at least fields id, picture, imagealt, firstname, lastname * If any of these are missing, or if a userid is passed, the database is queried. Avoid this * if at all possible, particularly for reports. It is very bad for performance. * A moodle_user_picture object is a better parameter. * @param int $courseid courseid Used when constructing the link to the user's profile. Required if $userpic * is not a moodle_user_picture object * @return string HTML fragment */ public function user_picture($userpic, $courseid = null) { // Instantiate a moodle_user_picture object if $user is not already one if (!$userpic instanceof moodle_user_picture) { if (empty($courseid)) { throw new coding_exception('Called $OUTPUT->user_picture with a $user object but no $courseid.'); } $user = $userpic; $userpic = new moodle_user_picture(); $userpic->user = $user; $userpic->courseid = $courseid; } else { $userpic = clone $userpic; } $userpic->prepare(); $output = $this->image($userpic->image); if (!empty($userpic->url)) { $actions = $userpic->get_actions(); if ($userpic->popup && !empty($actions)) { $link = new html_link(); $link->url = $userpic->url; $link->text = fullname($userpic->user); $link->title = fullname($userpic->user); foreach ($actions as $action) { $link->add_action($action); } $output = $this->link_to_popup($link, $userpic->image); } else { $output = $this->link(prepare_url($userpic->url), $output); } } return $output; }
/** * @see lib/moodle_html_component#prepare() * @return void */ public function prepare() { $this->image->add_class('action-icon'); if (!empty($this->actions)) { foreach ($this->actions as $action) { $this->link->add_action($action); } unset($this->actions); } parent::prepare(); if (empty($this->image->src)) { throw new coding_exception('moodle_action_icon->image->src must not be empty'); } if (empty($this->image->alt) && !empty($this->linktext)) { $this->image->alt = $this->linktext; } else { if (empty($this->image->alt)) { debugging('moodle_action_icon->image->alt should not be empty.', DEBUG_DEVELOPER); } } }