MDL-37977 frontpage: Move html from index.php to renderer

This commit is contained in:
Marina Glancy 2017-11-28 16:44:42 +08:00
parent a400dd2d85
commit dcce35754c
2 changed files with 138 additions and 141 deletions

View File

@ -2382,6 +2382,137 @@ class core_course_renderer extends plugin_renderer_base {
return $output;
}
/**
* Output news for the frontpage (extract from site-wide news forum)
*
* @param stdClass $newsforum record from db table 'forum' that represents the site news forum
* @return string
*/
protected function frontpage_news($newsforum) {
global $CFG, $SITE, $SESSION, $USER;
require_once($CFG->dirroot .'/mod/forum/lib.php');
$output = '';
if (isloggedin()) {
$SESSION->fromdiscussion = $CFG->wwwroot;
$subtext = '';
if (\mod_forum\subscriptions::is_subscribed($USER->id, $newsforum)) {
if (!\mod_forum\subscriptions::is_forcesubscribed($newsforum)) {
$subtext = get_string('unsubscribe', 'forum');
}
} else {
$subtext = get_string('subscribe', 'forum');
}
$suburl = new moodle_url('/mod/forum/subscribe.php', array('id' => $newsforum->id, 'sesskey' => sesskey()));
$output .= html_writer::tag('div', html_writer::link($suburl, $subtext), array('class' => 'subscribelink'));
}
ob_start();
forum_print_latest_discussions($SITE, $newsforum, $SITE->newsitems, 'plain', 'p.modified DESC');
$output .= ob_get_contents();
ob_end_clean();
return $output;
}
/**
* Renders part of frontpage with a skip link (i.e. "My courses", "Site news", etc.)
*
* @param string $skipdivid
* @param string $contentsdivid
* @param string $header Header of the part
* @param string $contents Contents of the part
* @return string
*/
protected function frontpage_part($skipdivid, $contentsdivid, $header, $contents) {
$output = html_writer::link('#' . $skipdivid,
get_string('skipa', 'access', core_text::strtolower(strip_tags($header))),
array('class' => 'skip-block skip'));
// Wrap frontpage part in div container.
$output .= html_writer::start_tag('div', array('id' => $contentsdivid));
$output .= $this->heading($header);
$output .= $contents;
// End frontpage part div container.
$output .= html_writer::end_tag('div');
$output .= html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => $skipdivid));
return $output;
}
/**
* Outputs contents for frontpage as configured in $CFG->frontpage or $CFG->frontpageloggedin
*
* @return string
*/
public function frontpage() {
global $CFG, $SITE;
$output = '';
if (isloggedin() and !isguestuser() and isset($CFG->frontpageloggedin)) {
$frontpagelayout = $CFG->frontpageloggedin;
} else {
$frontpagelayout = $CFG->frontpage;
}
foreach (explode(',', $frontpagelayout) as $v) {
switch ($v) {
// Display the main part of the front page.
case FRONTPAGENEWS:
if ($SITE->newsitems) {
// Print forums only when needed.
require_once($CFG->dirroot .'/mod/forum/lib.php');
if (($newsforum = forum_get_course_forum($SITE->id, 'news')) &&
($forumcontents = $this->frontpage_news($newsforum))) {
$newsforumcm = get_fast_modinfo($SITE)->instances['forum'][$newsforum->id];
$output .= $this->frontpage_part('skipsitenews', 'site-news-forum',
$newsforumcm->get_formatted_name(), $forumcontents);
}
}
break;
case FRONTPAGEENROLLEDCOURSELIST:
$mycourseshtml = $this->frontpage_my_courses();
if (!empty($mycourseshtml)) {
$output .= $this->frontpage_part('skipmycourses', 'frontpage-course-list',
get_string('mycourses'), $mycourseshtml);
break;
}
// No "break" here. If there are no enrolled courses - continue to 'Available courses'.
case FRONTPAGEALLCOURSELIST:
$availablecourseshtml = $this->frontpage_available_courses();
if (!empty($availablecourseshtml)) {
$output .= $this->frontpage_part('skipavailablecourses', 'frontpage-available-course-list',
get_string('availablecourses'), $availablecourseshtml);
}
break;
case FRONTPAGECATEGORYNAMES:
$output .= $this->frontpage_part('skipcategories', 'frontpage-category-names',
get_string('categories'), $this->frontpage_categories_list());
break;
case FRONTPAGECATEGORYCOMBO:
$output .= $this->frontpage_part('skipcourses', 'frontpage-category-combo',
get_string('courses'), $this->frontpage_combo_list());
break;
case FRONTPAGECOURSESEARCH:
$output .= $this->box($this->course_search_form('', 'short'), 'mdl-align');
break;
}
$output .= '<br />';
}
return $output;
}
}
/**

148
index.php
View File

@ -111,15 +111,17 @@ $PAGE->set_heading($SITE->fullname);
$courserenderer = $PAGE->get_renderer('core', 'course');
echo $OUTPUT->header();
// Print Section or custom info.
$siteformatoptions = course_get_format($SITE)->get_format_options();
$modinfo = get_fast_modinfo($SITE);
$modnames = get_module_types_names();
$modnamesplural = get_module_types_names(true);
$modnamesused = $modinfo->get_used_module_names();
$mods = $modinfo->get_cms();
// Print Section or custom info.
if (!empty($CFG->customfrontpageinclude)) {
// Pre-fill some variables that custom front page might use.
$modnames = get_module_types_names();
$modnamesplural = get_module_types_names(true);
$mods = $modinfo->get_cms();
include($CFG->customfrontpageinclude);
} else if ($siteformatoptions['numsections'] > 0) {
@ -128,144 +130,8 @@ if (!empty($CFG->customfrontpageinclude)) {
// Include course AJAX.
include_course_ajax($SITE, $modnamesused);
if (isloggedin() and !isguestuser() and isset($CFG->frontpageloggedin)) {
$frontpagelayout = $CFG->frontpageloggedin;
} else {
$frontpagelayout = $CFG->frontpage;
}
echo $courserenderer->frontpage();
foreach (explode(',', $frontpagelayout) as $v) {
switch ($v) {
// Display the main part of the front page.
case FRONTPAGENEWS:
if ($SITE->newsitems) {
// Print forums only when needed.
require_once($CFG->dirroot .'/mod/forum/lib.php');
if (! $newsforum = forum_get_course_forum($SITE->id, 'news')) {
print_error('cannotfindorcreateforum', 'forum');
}
// Fetch news forum context for proper filtering to happen.
$newsforumcm = get_coursemodule_from_instance('forum', $newsforum->id, $SITE->id, false, MUST_EXIST);
$newsforumcontext = context_module::instance($newsforumcm->id, MUST_EXIST);
$forumname = format_string($newsforum->name, true, array('context' => $newsforumcontext));
echo html_writer::link('#skipsitenews',
get_string('skipa', 'access', core_text::strtolower(strip_tags($forumname))),
array('class' => 'skip-block skip'));
// Wraps site news forum in div container.
echo html_writer::start_tag('div', array('id' => 'site-news-forum'));
if (isloggedin()) {
$SESSION->fromdiscussion = $CFG->wwwroot;
$subtext = '';
if (\mod_forum\subscriptions::is_subscribed($USER->id, $newsforum)) {
if (!\mod_forum\subscriptions::is_forcesubscribed($newsforum)) {
$subtext = get_string('unsubscribe', 'forum');
}
} else {
$subtext = get_string('subscribe', 'forum');
}
echo $OUTPUT->heading($forumname);
$suburl = new moodle_url('/mod/forum/subscribe.php', array('id' => $newsforum->id, 'sesskey' => sesskey()));
echo html_writer::tag('div', html_writer::link($suburl, $subtext), array('class' => 'subscribelink'));
} else {
echo $OUTPUT->heading($forumname);
}
forum_print_latest_discussions($SITE, $newsforum, $SITE->newsitems, 'plain', 'p.modified DESC');
// End site news forum div container.
echo html_writer::end_tag('div');
echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipsitenews'));
}
break;
case FRONTPAGEENROLLEDCOURSELIST:
$mycourseshtml = $courserenderer->frontpage_my_courses();
if (!empty($mycourseshtml)) {
echo html_writer::link('#skipmycourses',
get_string('skipa', 'access', core_text::strtolower(get_string('mycourses'))),
array('class' => 'skip skip-block'));
// Wrap frontpage course list in div container.
echo html_writer::start_tag('div', array('id' => 'frontpage-course-list'));
echo $OUTPUT->heading(get_string('mycourses'));
echo $mycourseshtml;
// End frontpage course list div container.
echo html_writer::end_tag('div');
echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipmycourses'));
break;
}
// No "break" here. If there are no enrolled courses - continue to 'Available courses'.
case FRONTPAGEALLCOURSELIST:
$availablecourseshtml = $courserenderer->frontpage_available_courses();
if (!empty($availablecourseshtml)) {
echo html_writer::link('#skipavailablecourses',
get_string('skipa', 'access', core_text::strtolower(get_string('availablecourses'))),
array('class' => 'skip skip-block'));
// Wrap frontpage course list in div container.
echo html_writer::start_tag('div', array('id' => 'frontpage-course-list'));
echo $OUTPUT->heading(get_string('availablecourses'));
echo $availablecourseshtml;
// End frontpage course list div container.
echo html_writer::end_tag('div');
echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipavailablecourses'));
}
break;
case FRONTPAGECATEGORYNAMES:
echo html_writer::link('#skipcategories',
get_string('skipa', 'access', core_text::strtolower(get_string('categories'))),
array('class' => 'skip skip-block'));
// Wrap frontpage category names in div container.
echo html_writer::start_tag('div', array('id' => 'frontpage-category-names'));
echo $OUTPUT->heading(get_string('categories'));
echo $courserenderer->frontpage_categories_list();
// End frontpage category names div container.
echo html_writer::end_tag('div');
echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipcategories'));
break;
case FRONTPAGECATEGORYCOMBO:
echo html_writer::link('#skipcourses',
get_string('skipa', 'access', core_text::strtolower(get_string('courses'))),
array('class' => 'skip skip-block'));
// Wrap frontpage category combo in div container.
echo html_writer::start_tag('div', array('id' => 'frontpage-category-combo'));
echo $OUTPUT->heading(get_string('courses'));
echo $courserenderer->frontpage_combo_list();
// End frontpage category combo div container.
echo html_writer::end_tag('div');
echo html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => 'skipcourses'));
break;
case FRONTPAGECOURSESEARCH:
echo $OUTPUT->box($courserenderer->course_search_form('', 'short'), 'mdl-align');
break;
}
echo '<br />';
}
if ($editing && has_capability('moodle/course:create', context_system::instance())) {
echo $courserenderer->add_new_course_button();
}