Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This page lets users to manage site wide learning plan templates.
  19   *
  20   * @package    tool_lp
  21   * @copyright  2015 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../../../config.php');
  26  require_once($CFG->libdir.'/adminlib.php');
  27  
  28  $id = optional_param('id', 0, PARAM_INT);
  29  $returntype = optional_param('return', null, PARAM_ALPHA);
  30  $pagecontextid = required_param('pagecontextid', PARAM_INT);  // Reference to where we can from.
  31  
  32  $template = null;
  33  if (!empty($id)) {
  34      // Always use the context from the framework when it exists.
  35      $template = new \core_competency\template($id);
  36      $context = $template->get_context();
  37  } else {
  38      $context = context::instance_by_id($pagecontextid);
  39  }
  40  
  41  // We check that we have the permission to edit this framework, in its own context.
  42  require_login(0, false);
  43  \core_competency\api::require_enabled();
  44  require_capability('moodle/competency:templatemanage', $context);
  45  
  46  // We keep the original context in the URLs, so that we remain in the same context.
  47  $url = new moodle_url("/admin/tool/lp/edittemplate.php", [
  48      'id' => $id,
  49      'pagecontextid' => $pagecontextid,
  50      'return' => $returntype
  51  ]);
  52  
  53  if (empty($id)) {
  54      $pagetitle = get_string('addnewtemplate', 'tool_lp');
  55      list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_template($pagecontextid, $url, null, $pagetitle,
  56          $returntype);
  57  } else {
  58      $template = \core_competency\api::read_template($id);
  59      $pagetitle = get_string('edittemplate', 'tool_lp');
  60      list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_template($pagecontextid, $url, $template,
  61          $pagetitle, $returntype);
  62  }
  63  
  64  $form = new \tool_lp\form\template($url->out(false), array('persistent' => $template, 'context' => $context));
  65  if ($form->is_cancelled()) {
  66      redirect($returnurl);
  67  }
  68  
  69  $data = $form->get_data();
  70  if ($data) {
  71      if (empty($data->id)) {
  72          $template = \core_competency\api::create_template($data);
  73          $returnurl = new moodle_url('/admin/tool/lp/templatecompetencies.php', [
  74              'templateid' => $template->get('id'),
  75              'pagecontextid' => $pagecontextid
  76          ]);
  77          $returnmsg = get_string('templatecreated', 'tool_lp');
  78      } else {
  79          \core_competency\api::update_template($data);
  80          $returnmsg = get_string('templateupdated', 'tool_lp');
  81      }
  82      redirect($returnurl, $returnmsg, null, \core\output\notification::NOTIFY_SUCCESS);
  83  }
  84  
  85  $output = $PAGE->get_renderer('tool_lp');
  86  echo $output->header();
  87  echo $output->heading($title);
  88  if (!empty($subtitle)) {
  89      echo $output->heading($subtitle, 3);
  90  }
  91  
  92  $form->display();
  93  
  94  echo $output->footer();