.
defined('MOODLE_INTERNAL') || die();
/**
* Renderers to align Moodle's HTML with that expected by Bootstrap
*
* @package theme_bootstrap
* @copyright 2012
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class theme_bootstrap_core_renderer extends core_renderer {
public function notification($message, $classes = 'notifyproblem') {
$message = clean_text($message);
if ($classes == 'notifyproblem') {
return html_writer::div($message, 'alert alert-danger');
}
if ($classes == 'notifywarning') {
return html_writer::div($message, 'alert alert-warning');
}
if ($classes == 'notifysuccess') {
return html_writer::div($message, 'alert alert-success');
}
if ($classes == 'notifymessage') {
return html_writer::div($message, 'alert alert-info');
}
if ($classes == 'redirectmessage') {
return html_writer::div($message, 'alert alert-block alert-info');
}
if ($classes == 'notifytiny') {
// Not an appropriate semantic alert class!
return $this->debug_listing($message);
}
return html_writer::div($message, $classes);
}
private function debug_listing($message) {
$message = str_replace('
'alert alert-info'));
}
public function navbar() {
$breadcrumbs = '';
foreach ($this->page->navbar->get_items() as $item) {
$item->hideicon = true;
$breadcrumbs .= '- '.$this->render($item).'
';
}
return "$breadcrumbs
";
}
public function custom_menu($custommenuitems = '') {
// The custom menu is always shown, even if no menu items
// are configured in the global theme settings page.
global $CFG;
if (!empty($CFG->custommenuitems)) {
$custommenuitems .= $CFG->custommenuitems;
}
$custommenu = new custom_menu($custommenuitems, current_language());
return $this->render_custom_menu($custommenu);
}
protected function render_custom_menu(custom_menu $menu) {
global $CFG, $USER;
// TODO: eliminate this duplicated logic, it belongs in core, not
// here. See MDL-39565.
$content = '';
foreach ($menu->get_children() as $item) {
$content .= $this->render_custom_menu_item($item, 1);
}
return $content.'
';
}
public function user_menu() {
global $CFG;
$usermenu = new custom_menu('', current_language());
return $this->render_user_menu($usermenu);
}
protected function render_user_menu(custom_menu $menu) {
global $CFG, $USER, $DB;
$addusermenu = true;
$addlangmenu = true;
$addmessagemenu = true;
if (!isloggedin() || isguestuser()) {
$addmessagemenu = false;
}
if ($addmessagemenu) {
$messages = $this->get_user_messages();
$messagecount = count($messages);
$messagemenu = $menu->add(
$messagecount . ' ' . get_string('messages', 'message'),
new moodle_url('/message/index.php', array('viewing' => 'recentconversations')),
get_string('messages', 'message'),
9999
);
foreach ($messages as $message) {
if (!$message->from) { // Workaround for issue #103.
continue;
}
$senderpicture = new user_picture($message->from);
$senderpicture->link = false;
$senderpicture = $this->render($senderpicture);
$messagecontent = $senderpicture;
$messagecontent .= html_writer::start_span('msg-body');
$messagecontent .= html_writer::start_span('msg-title');
$messagecontent .= html_writer::span($message->from->firstname . ': ', 'msg-sender');
$messagecontent .= $message->text;
$messagecontent .= html_writer::end_span();
$messagecontent .= html_writer::start_span('msg-time');
$messagecontent .= html_writer::tag('i', '', array('class' => 'icon-time'));
$messagecontent .= html_writer::span($message->date);
$messagecontent .= html_writer::end_span();
$messageurl = new moodle_url('/message/index.php', array('user1' => $USER->id, 'user2' => $message->from->id));
$messagemenu->add($messagecontent, $messageurl, $message->state);
}
}
$langs = get_string_manager()->get_list_of_translations();
if (count($langs) < 2
or empty($CFG->langmenu)
or ($this->page->course != SITEID and !empty($this->page->course->lang))) {
$addlangmenu = false;
}
if ($addlangmenu) {
$language = $menu->add(get_string('language'), new moodle_url('#'), get_string('language'), 10000);
foreach ($langs as $langtype => $langname) {
$language->add($langname, new moodle_url($this->page->url, array('lang' => $langtype)), $langname);
}
}
if ($addusermenu) {
if (isloggedin()) {
$usermenu = $menu->add(fullname($USER), new moodle_url('#'), fullname($USER), 10001);
$usermenu->add(
'' . get_string('logout'),
new moodle_url('/login/logout.php', array('sesskey' => sesskey(), 'alt' => 'logout')),
get_string('logout')
);
$usermenu->add(
'' . get_string('viewprofile'),
new moodle_url('/user/profile.php', array('id' => $USER->id)),
get_string('viewprofile')
);
$usermenu->add(
'' . get_string('editmyprofile'),
new moodle_url('/user/edit.php', array('id' => $USER->id)),
get_string('editmyprofile')
);
} else {
$usermenu = $menu->add(get_string('login'), new moodle_url('/login/index.php'), get_string('login'), 10001);
}
}
$content = '';
foreach ($menu->get_children() as $item) {
$content .= $this->render_custom_menu_item($item, 1);
}
return $content.'
';
}
protected function process_user_messages() {
$messagelist = array();
foreach ($usermessages as $message) {
$cleanmsg = new stdClass();
$cleanmsg->from = fullname($message);
$cleanmsg->msguserid = $message->id;
$userpicture = new user_picture($message);
$userpicture->link = false;
$picture = $this->render($userpicture);
$cleanmsg->text = $picture . ' ' . $cleanmsg->text;
$messagelist[] = $cleanmsg;
}
return $messagelist;
}
protected function get_user_messages() {
global $USER, $DB;
$messagelist = array();
$maxmessages = 5;
$readmessagesql = "SELECT id, smallmessage, useridfrom, useridto, timecreated, fullmessageformat, notification
FROM {message_read}
WHERE useridto = :userid
ORDER BY timecreated DESC
LIMIT $maxmessages";
$newmessagesql = "SELECT id, smallmessage, useridfrom, useridto, timecreated, fullmessageformat, notification
FROM {message}
WHERE useridto = :userid";
$readmessages = $DB->get_records_sql($readmessagesql, array('userid' => $USER->id));
$newmessages = $DB->get_records_sql($newmessagesql, array('userid' => $USER->id));
foreach ($newmessages as $message) {
$messagelist[] = $this->bootstrap_process_message($message, 'new');
}
foreach ($readmessages as $message) {
$messagelist[] = $this->bootstrap_process_message($message, 'old');
}
return $messagelist;
}
protected function bootstrap_process_message($message, $state) {
global $DB;
$messagecontent = new stdClass();
if ($message->notification) {
$messagecontent->text = get_string('unreadnewnotification', 'message');
} else {
if ($message->fullmessageformat == FORMAT_HTML) {
$message->smallmessage = html_to_text($message->smallmessage);
}
if (core_text::strlen($message->smallmessage) > 15) {
$messagecontent->text = core_text::substr($message->smallmessage, 0, 15).'...';
} else {
$messagecontent->text = $message->smallmessage;
}
}
if ((time() - $message->timecreated ) <= (3600 * 3)) {
$messagecontent->date = format_time(time() - $message->timecreated);
} else {
$messagecontent->date = userdate($message->timecreated, get_string('strftimetime', 'langconfig'));
}
$messagecontent->from = $DB->get_record('user', array('id' => $message->useridfrom));
$messagecontent->state = $state;
return $messagecontent;
}
protected function render_custom_menu_item(custom_menu_item $menunode, $level = 0 ) {
static $submenucount = 0;
if ($menunode->has_children()) {
if ($level == 1) {
$dropdowntype = 'dropdown';
} else {
$dropdowntype = 'dropdown-submenu';
}
$content = html_writer::start_tag('li', array('class' => $dropdowntype));
// If the child has menus render it as a sub menu.
$submenucount++;
if ($menunode->get_url() !== null) {
$url = $menunode->get_url();
} else {
$url = '#cm_submenu_'.$submenucount;
}
$linkattributes = array(
'href' => $url,
'class' => 'dropdown-toggle',
'data-toggle' => 'dropdown',
'title' => $menunode->get_title(),
);
$content .= html_writer::start_tag('a', $linkattributes);
$content .= $menunode->get_text();
if ($level == 1) {
$content .= '';
}
$content .= '';
$content .= '';
} else {
$content = '- ';
// The node doesn't have children so produce a final menuitem.
if ($menunode->get_url() !== null) {
$url = $menunode->get_url();
} else {
$url = '#';
}
$content .= html_writer::link($url, $menunode->get_text(), array('title' => $menunode->get_title()));
}
return $content;
}
protected function render_tabtree(tabtree $tabtree) {
if (empty($tabtree->subtree)) {
return '';
}
$firstrow = $secondrow = '';
foreach ($tabtree->subtree as $tab) {
$firstrow .= $this->render($tab);
if (($tab->selected || $tab->activated) && !empty($tab->subtree) && $tab->subtree !== array()) {
$secondrow = $this->tabtree($tab->subtree);
}
}
return html_writer::tag('ul', $firstrow, array('class' => 'nav nav-tabs nav-justified')) . $secondrow;
}
protected function render_tabobject(tabobject $tab) {
if ($tab->selected or $tab->activated) {
return html_writer::tag('li', html_writer::tag('a', $tab->text), array('class' => 'active'));
} else if ($tab->inactive) {
return html_writer::tag('li', html_writer::tag('a', $tab->text), array('class' => 'disabled'));
} else {
if (!($tab->link instanceof moodle_url)) {
// Backward compatibility when link was passed as quoted string.
$link = "link\" title=\"$tab->title\">$tab->text";
} else {
$link = html_writer::link($tab->link, $tab->text, array('title' => $tab->title));
}
return html_writer::tag('li', $link);
}
}
protected function render_pix_icon(pix_icon $icon) {
if ($this->page->theme->settings->fonticons === '1'
&& $icon->attributes["alt"] === '') {
$iconout = $this->replace_moodle_icon($icon->pix);
if ($iconout !== false) {
return $iconout;
}
}
return parent::render_pix_icon($icon);
}
protected function replace_moodle_icon($name) {
$icons = array(
'add' => 'plus',
'book' => 'book',
'chapter' => 'file',
'docs' => 'question-sign',
'generate' => 'gift',
'i/backup' => 'download',
't/backup' => 'download',
'i/checkpermissions' => 'user',
'i/edit' => 'pencil',
'i/filter' => 'filter',
'i/grades' => 'grades',
'i/group' => 'user',
'i/hide' => 'eye-open',
'i/import' => 'upload',
'i/info' => 'info',
'i/move_2d' => 'move',
'i/navigationitem' => 'chevron-right',
'i/publish' => 'globe',
'i/reload' => 'refresh',
'i/report' => 'list-alt',
'i/restore' => 'upload',
't/restore' => 'upload',
'i/return' => 'repeat',
'i/roles' => 'user',
'i/settings' => 'cog',
'i/show' => 'eye-close',
'i/switchrole' => 'user',
'i/user' => 'user',
'i/users' => 'user',
'spacer' => 'spacer',
't/add' => 'plus',
't/assignroles' => 'user',
't/copy' => 'plus-sign',
't/delete' => 'remove',
't/down' => 'arrow-down',
't/edit' => 'edit',
't/editstring' => 'tag',
't/hide' => 'eye-open',
't/left' => 'arrow-left',
't/move' => 'resize-vertical',
't/right' => 'arrow-right',
't/show' => 'eye-close',
't/switch_minus' => 'minus-sign',
't/switch_plus' => 'plus-sign',
't/up' => 'arrow-up',
);
if (isset($icons[$name])) {
return ' ';
} else {
return false;
}
}
public function box($contents, $classes = 'generalbox', $id = null, $attributes = array()) {
if (isset($attributes['data-rel']) && $attributes['data-rel'] === 'fatalerror') {
return html_writer::div($contents, 'alert alert-danger', $attributes);
}
return parent::box($contents, $classes, $id, $attributes);
}
}