From 47855def51540438d1f32a1cae7bf9efd0108b38 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Thu, 19 Nov 2015 15:16:05 +0800 Subject: [PATCH] MDL-52195 tool_lp: Normalise the navigation in templates --- admin/tool/lp/classes/form/template.php | 20 ++-- admin/tool/lp/classes/output/renderer.php | 10 ++ .../lp/classes/output/template_plans_page.php | 44 ++++++++ admin/tool/lp/classes/page_helper.php | 102 ++++++++++++++++++ admin/tool/lp/edittemplate.php | 31 +++--- admin/tool/lp/learningplans.php | 18 +--- admin/tool/lp/template_plans.php | 26 +---- admin/tool/lp/templatecompetencies.php | 18 +--- 8 files changed, 193 insertions(+), 76 deletions(-) create mode 100644 admin/tool/lp/classes/output/template_plans_page.php create mode 100644 admin/tool/lp/classes/page_helper.php diff --git a/admin/tool/lp/classes/form/template.php b/admin/tool/lp/classes/form/template.php index 12785faf82f..3ce15c14455 100644 --- a/admin/tool/lp/classes/form/template.php +++ b/admin/tool/lp/classes/form/template.php @@ -45,7 +45,12 @@ class template extends moodleform { */ public function definition() { $mform = $this->_form; - $id = $this->_customdata['id']; + $template = $this->_customdata['template']; + if (empty($template)) { + $id = 0; + } else { + $id = $template->get_id(); + } $context = $this->_customdata['context']; $mform->addElement('hidden', 'id'); @@ -75,14 +80,11 @@ class template extends moodleform { $this->add_action_buttons(true, get_string('savechanges', 'tool_lp')); - if (!empty($id)) { - if (!$this->is_submitted()) { - $template = api::read_template($id); - $record = $template->to_record(); - // Massage for editor API. - $record->description = array('text' => $record->description, 'format' => $record->descriptionformat); - $this->set_data($record); - } + if (!$this->is_submitted() && !empty($template)) { + $record = $template->to_record(); + // Massage for editor API. + $record->description = array('text' => $record->description, 'format' => $record->descriptionformat); + $this->set_data($record); } } diff --git a/admin/tool/lp/classes/output/renderer.php b/admin/tool/lp/classes/output/renderer.php index a06514c632b..63bba42af05 100644 --- a/admin/tool/lp/classes/output/renderer.php +++ b/admin/tool/lp/classes/output/renderer.php @@ -131,4 +131,14 @@ class renderer extends plugin_renderer_base { return parent::render_from_template('tool_lp/related_competencies', $data); } + /** + * Render the template plans page. + * + * @param renderable $page + * @return string + */ + public function render_template_plans_page(renderable $page) { + return $page->table->out(50, true); + } + } diff --git a/admin/tool/lp/classes/output/template_plans_page.php b/admin/tool/lp/classes/output/template_plans_page.php new file mode 100644 index 00000000000..df0f0db810d --- /dev/null +++ b/admin/tool/lp/classes/output/template_plans_page.php @@ -0,0 +1,44 @@ +. + +/** + * Template plans renderable. + * + * @package tool_lp + * @copyright 2015 Frédéric Massart - FMCorz.net + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_lp\output; +defined('MOODLE_INTERNAL') || die(); + +/** + * Template plans renderable. + * + * @package tool_lp + * @copyright 2015 Frédéric Massart - FMCorz.net + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class template_plans_page implements \renderable { + + public function __construct(\tool_lp\template $template, \moodle_url $url) { + $this->template = $template; + $this->url = $url; + $this->table = new template_plans_table('tplplans', $template); + $this->table->define_baseurl($url); + } + +} diff --git a/admin/tool/lp/classes/page_helper.php b/admin/tool/lp/classes/page_helper.php new file mode 100644 index 00000000000..4a031139057 --- /dev/null +++ b/admin/tool/lp/classes/page_helper.php @@ -0,0 +1,102 @@ +. + +/** + * Page helper. + * + * @package tool_lp + * @copyright 2015 Frédéric Massart - FMCorz.net + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_lp; +defined('MOODLE_INTERNAL') || die(); + +use coding_exception; +use context; +use moodle_url; + +/** + * Page helper. + * + * @package tool_lp + * @copyright 2015 Frédéric Massart - FMCorz.net + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class page_helper { + + /** + * Set-up a template page. + * + * Example: + * list($title, $subtitle) = page_helper::setup_for_template($pagecontextid, $url, $template, $pagetitle); + * echo $OUTPUT->heading($title); + * echo $OUTPUT->heading($subtitle, 3); + * + * @param int $pagecontextid The page context ID. + * @param moodle_url $url The current page. + * @param \tool_lp\template $template The template, if any. + * @param string $subpage The title of the subpage, if any. + * @return array With the following: + * - Page title + * - Page sub title + * - Return URL (main templates page) + */ + public static function setup_for_template($pagecontextid, moodle_url $url, $template = null, $subtitle = '') { + global $PAGE, $SITE; + + $pagecontext = context::instance_by_id($pagecontextid); + $context = $pagecontext; + if (!empty($template)) { + $context = $template->get_context(); + } + + $templatesurl = new moodle_url('/admin/tool/lp/learningplans.php', array('pagecontextid' => $pagecontextid)); + + $PAGE->navigation->override_active_url($templatesurl); + $PAGE->set_context($pagecontext); + + if (!empty($template)) { + $title = format_string($template->get_shortname(), true, array('context' => $context)); + } else { + $title = get_string('templates', 'tool_lp'); + } + + if ($pagecontext->contextlevel == CONTEXT_SYSTEM) { + $heading = $SITE->fullname; + } else if ($pagecontext->contextlevel == CONTEXT_COURSECAT) { + $heading = $pagecontext->get_context_name(); + } else { + throw new coding_exception('Unexpected context!'); + } + + $PAGE->set_pagelayout('admin'); + $PAGE->set_url($url); + $PAGE->set_title($title); + $PAGE->set_heading($heading); + + if (!empty($template)) { + $PAGE->navbar->add($title); + $PAGE->navbar->add($subtitle, $url); + + } else if (!empty($subtitle)) { + // We're in a sub page without a specific template. + $PAGE->navbar->add($subtitle, $url); + } + + return array($title, $subtitle, $templatesurl); + } +} diff --git a/admin/tool/lp/edittemplate.php b/admin/tool/lp/edittemplate.php index 9dcc0fb581d..1df20cc3edf 100644 --- a/admin/tool/lp/edittemplate.php +++ b/admin/tool/lp/edittemplate.php @@ -28,6 +28,7 @@ require_once($CFG->libdir.'/adminlib.php'); $id = optional_param('id', 0, PARAM_INT); $pagecontextid = required_param('pagecontextid', PARAM_INT); // Reference to where we can from. +$template = null; if (!empty($id)) { // Always use the context from the framework when it exists. $template = new \tool_lp\template($id); @@ -37,37 +38,33 @@ if (!empty($id)) { } // We check that we have the permission to edit this framework, in its own context. -require_login(); +require_login(0, false); require_capability('tool/lp:templatemanage', $context); // We keep the original context in the URLs, so that we remain in the same context. $url = new moodle_url("/admin/tool/lp/edittemplate.php", array('id' => $id, 'pagecontextid' => $pagecontextid)); -$templatesurl = new moodle_url('/admin/tool/lp/learningplans.php', array('pagecontextid' => $pagecontextid)); $formurl = new moodle_url("/admin/tool/lp/edittemplate.php", array('pagecontextid' => $pagecontextid)); -$title = get_string('templates', 'tool_lp'); if (empty($id)) { $pagetitle = get_string('addnewtemplate', 'tool_lp'); + list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_template($pagecontextid, $url, null, $pagetitle); } else { + $template = \tool_lp\api::read_template($id); $pagetitle = get_string('edittemplate', 'tool_lp'); + list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_template($pagecontextid, $url, $template, $pagetitle); } -// Set up the page. -$PAGE->navigation->override_active_url($templatesurl); -$PAGE->set_context(context::instance_by_id($pagecontextid)); -$PAGE->set_pagelayout('admin'); -$PAGE->set_url($url); -$PAGE->set_title($title); -$PAGE->set_heading($title); -$output = $PAGE->get_renderer('tool_lp'); - -$form = new \tool_lp\form\template($formurl->out(false), array('id' => $id, 'context' => $context)); +$form = new \tool_lp\form\template($formurl->out(false), array('template' => $template, 'context' => $context)); if ($form->is_cancelled()) { - redirect($templatesurl); + redirect($returnurl); } +$output = $PAGE->get_renderer('tool_lp'); echo $output->header(); -echo $output->heading($pagetitle); +echo $output->heading($title); +if (!empty($subtitle)) { + echo $output->heading($subtitle, 3); +} $data = $form->get_data(); if ($data) { @@ -81,12 +78,12 @@ if ($data) { $data->contextid = $context->id; \tool_lp\api::create_template($data); echo $output->notification(get_string('templatecreated', 'tool_lp'), 'notifysuccess'); - echo $output->continue_button($templatesurl); + echo $output->continue_button($returnurl); } else { require_sesskey(); \tool_lp\api::update_template($data); echo $output->notification(get_string('templateupdated', 'tool_lp'), 'notifysuccess'); - echo $output->continue_button($templatesurl); + echo $output->continue_button($returnurl); } } else { $form->display(); diff --git a/admin/tool/lp/learningplans.php b/admin/tool/lp/learningplans.php index 51bb859532f..8adfac80f99 100644 --- a/admin/tool/lp/learningplans.php +++ b/admin/tool/lp/learningplans.php @@ -28,25 +28,15 @@ require_once($CFG->libdir.'/adminlib.php'); $pagecontextid = required_param('pagecontextid', PARAM_INT); $context = context::instance_by_id($pagecontextid); -$url = new moodle_url('/admin/tool/lp/learningplans.php', array('pagecontextid' => $pagecontextid)); - -require_login(); +require_login(0, false); require_capability('tool/lp:templatemanage', $context); -$title = get_string('learningplans', 'tool_lp'); -$pagetitle = get_string('templates', 'tool_lp'); +$url = new moodle_url('/admin/tool/lp/learningplans.php', array('pagecontextid' => $pagecontextid)); +list($title, $subtitle) = \tool_lp\page_helper::setup_for_template($pagecontextid, $url); -// Set up the page. -$PAGE->set_context($context); -$PAGE->set_pagelayout('admin'); -$PAGE->set_url($url); -$PAGE->set_title($title); -$PAGE->set_heading($title); $output = $PAGE->get_renderer('tool_lp'); echo $output->header(); -echo $output->heading($pagetitle); - +echo $output->heading($title); $page = new \tool_lp\output\manage_templates_page($context); echo $output->render($page); - echo $output->footer(); diff --git a/admin/tool/lp/template_plans.php b/admin/tool/lp/template_plans.php index 2633003c55c..47ef47eb1b8 100644 --- a/admin/tool/lp/template_plans.php +++ b/admin/tool/lp/template_plans.php @@ -28,11 +28,7 @@ $id = required_param('id', PARAM_INT); $pagecontextid = required_param('pagecontextid', PARAM_INT); // Reference to the context we came from. require_login(0, false); -if (isguestuser()) { - throw new require_login_exception('Guests are not allowed here.'); -} -$pagecontext = context::instance_by_id($pagecontextid); $template = \tool_lp\api::read_template($id); $context = $template->get_context(); require_capability('tool/lp:templatemanage', $context); @@ -42,27 +38,13 @@ $url = new moodle_url('/admin/tool/lp/template_plans.php', array( 'id' => $id, 'pagecontextid' => $pagecontextid )); -$templatesurl = new moodle_url('/admin/tool/lp/learningplans.php', array('pagecontextid' => $pagecontextid)); - -$PAGE->navigation->override_active_url($templatesurl); -$PAGE->set_context($pagecontext); - -$title = get_string('userplans', 'tool_lp'); -$templatename = format_string($template->get_shortname(), true, array('context' => $context)); - -$PAGE->set_pagelayout('admin'); -$PAGE->set_url($url); -$PAGE->set_title($title); -$PAGE->set_heading($templatename); -$PAGE->navbar->add($templatename, $url); +list($title, $subtitle) = \tool_lp\page_helper::setup_for_template($pagecontextid, $url, $template, get_string('userplans', 'tool_lp')); // Display the page. $output = $PAGE->get_renderer('tool_lp'); echo $output->header(); echo $output->heading($title); - -$tpl = new \tool_lp\output\template_plans_table('tplplans', $template); -$tpl->define_baseurl($url); -echo $tpl->out(50, true); - +echo $output->heading($subtitle, 3); +$page = new \tool_lp\output\template_plans_page($template, $url); +echo $output->render($page); echo $output->footer(); diff --git a/admin/tool/lp/templatecompetencies.php b/admin/tool/lp/templatecompetencies.php index 679439893d5..b8a0925cfb2 100644 --- a/admin/tool/lp/templatecompetencies.php +++ b/admin/tool/lp/templatecompetencies.php @@ -28,7 +28,7 @@ require_once($CFG->libdir.'/adminlib.php'); $templateid = required_param('templateid', PARAM_INT); $pagecontextid = required_param('pagecontextid', PARAM_INT); // Reference to the context we came from. -require_login(); +require_login(0, false); $pagecontext = context::instance_by_id($pagecontextid); $template = \tool_lp\api::read_template($templateid); @@ -38,24 +38,14 @@ require_capability('tool/lp:templatemanage', $context); // Set up the page. $url = new moodle_url('/admin/tool/lp/templatecompetencies.php', array('templateid' => $template->get_id(), 'pagecontextid' => $pagecontextid)); -$templatesurl = new moodle_url('/admin/tool/lp/learningplans.php', array('pagecontextid' => $pagecontextid)); - -$PAGE->navigation->override_active_url($templatesurl); -$PAGE->set_context($pagecontext); - -$title = get_string('templatecompetencies', 'tool_lp'); -$templatename = format_text($template->get_shortname()); - -$PAGE->set_pagelayout('admin'); -$PAGE->set_url($url); -$PAGE->set_title($title); -$PAGE->set_heading($templatename); -$PAGE->navbar->add($templatename, $url); +list($title, $subtitle) = \tool_lp\page_helper::setup_for_template($pagecontextid, $url, $template, + get_string('templatecompetencies', 'tool_lp')); // Display the page. $output = $PAGE->get_renderer('tool_lp'); echo $output->header(); echo $output->heading($title); +echo $output->heading($subtitle, 3); $page = new \tool_lp\output\template_competencies_page($template->get_id(), $pagecontext); echo $output->render($page); echo $output->footer(); -- 2.43.0