MDL-66301 forumreport_summary: Minor code improvements

This commit is contained in:
Michael Hawkins 2020-04-02 15:17:57 +08:00
parent bff097f216
commit 99f928a893
3 changed files with 12 additions and 10 deletions

View File

@ -106,14 +106,15 @@ class filters implements renderable, templatable {
/**
* Builds renderable filter data.
*
* @param stdClass $course The course object.
* @param array $cms Array of course module objects.
* @param moodle_url $actionurl The form action URL.
* @param array $filterdata (optional) Associative array of data that has been set on available filters, if any,
* in the format filtertype => [values]
*/
public function __construct(array $cms, moodle_url $actionurl, array $filterdata = []) {
public function __construct(stdClass $course, array $cms, moodle_url $actionurl, array $filterdata = []) {
$this->cms = $cms;
$this->courseid = $cms[0]->course;
$this->courseid = $course->id;
$this->actionurl = $actionurl;
// Prepare groups filter data.

View File

@ -68,11 +68,11 @@ if ($forumid) {
$filters['forums'] = [$forumid];
$title = $forumsvisibletouser[$forumid]->name;
$forumcm = $forumsvisibletouser[$forumid]->get_course_module_record();
$forumcm = $forumsvisibletouser[$forumid];
$cms[] = $forumcm;
require_login($courseid, false, $forumcm);
$context = \context_module::instance($forumcm->id);
$context = $forumcm->context;
$canexport = !$download && has_capability('mod/forum:exportforum', $context);
$redirecturl = new moodle_url('/mod/forum/view.php', ['id' => $forumid]);
$numforums = 1;
@ -86,7 +86,7 @@ if ($forumid) {
// Fetch the forum CMs for the course.
foreach ($forumsvisibletouser as $visibleforum) {
$cms[] = $visibleforum->get_course_module_record();
$cms[] = $visibleforum;
}
$context = \context_course::instance($courseid);
@ -117,7 +117,7 @@ $viewallcount = 0;
$canview = false;
foreach ($cms as $cm) {
$forumcontext = \context_module::instance($cm->id);
$forumcontext = $cm->context;
// This capability is required in at least one of the given contexts to view any version of the report.
if (has_capability('forumreport/summary:view', $forumcontext)) {
@ -175,7 +175,7 @@ if ($download) {
// Allow switching to course report (or other forum user has access to).
$reporturl = new moodle_url('/mod/forum/report/summary/index.php', ['courseid' => $courseid]);
$forumselect = new single_select($reporturl, 'forumid', $forumselectoptions, $forumid);
$forumselect = new single_select($reporturl, 'forumid', $forumselectoptions, $forumid, '');
$forumselect->set_label(get_string('forumselectlabel', 'forumreport_summary'));
echo $OUTPUT->render($forumselect);
@ -183,7 +183,7 @@ if ($download) {
$renderer = $PAGE->get_renderer('forumreport_summary');
unset($filters['forums']);
echo $renderer->render_filters_form($cms, $pageurl, $filters);
echo $renderer->render_filters_form($course, $cms, $pageurl, $filters);
$table->show_download_buttons_at(array(TABLE_P_BOTTOM));
echo $renderer->render_summary_table($table);
echo $OUTPUT->footer();

View File

@ -37,13 +37,14 @@ class forumreport_summary_renderer extends plugin_renderer_base {
/**
* Render the filters available for the forum summary report.
*
* @param stdClass $course The course object.
* @param array $cms Array of course module objects.
* @param moodle_url $actionurl The form action URL.
* @param array $filters Optional array of currently applied filter values.
* @return string The filter form HTML.
*/
public function render_filters_form(array $cms, moodle_url $actionurl, array $filters = []): string {
$renderable = new \forumreport_summary\output\filters($cms, $actionurl, $filters);
public function render_filters_form(stdClass $course, array $cms, moodle_url $actionurl, array $filters = []): string {
$renderable = new \forumreport_summary\output\filters($course, $cms, $actionurl, $filters);
$templatecontext = $renderable->export_for_template($this);
return $this->render_from_template('forumreport_summary/filters', $templatecontext);