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 competencies.
  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  $competencyframeworkid = optional_param('competencyframeworkid', 0, PARAM_INT);
  30  $pagecontextid = required_param('pagecontextid', PARAM_INT);  // Reference to the context we came from.
  31  $parentid = optional_param('parentid', 0, PARAM_INT);
  32  
  33  require_login(null, false);
  34  \core_competency\api::require_enabled();
  35  
  36  if (empty($competencyframeworkid) && empty($id)) {
  37      throw new coding_exception('Competencyframeworkid param is required');
  38  }
  39  
  40  // Get competency framework.
  41  $competencyframework = null;
  42  if (!empty($competencyframeworkid)) {
  43      $competencyframework = \core_competency\api::read_framework($competencyframeworkid);
  44  }
  45  
  46  // Get competency.
  47  $competency = null;
  48  if (!empty($id)) {
  49      $competency = \core_competency\api::read_competency($id);
  50      if (empty($competencyframework)) {
  51          $competencyframework = $competency->get_framework();
  52      }
  53  }
  54  
  55  // Get parent competency, if any.
  56  $parent = null;
  57  if ($competency) {
  58      $parent = $competency->get_parent();
  59  } else if ($parentid) {
  60      $parent = \core_competency\api::read_competency($parentid);
  61  }
  62  
  63  // Get page URL.
  64  $urloptions = [
  65      'id' => $id,
  66      'competencyframeworkid' => $competencyframework->get('id'),
  67      'parentid' => $parentid,
  68      'pagecontextid' => $pagecontextid
  69  ];
  70  $url = new moodle_url("/admin/tool/lp/editcompetency.php", $urloptions);
  71  
  72  // Set up the page.
  73  list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_competency($pagecontextid, $url, $competencyframework,
  74      $competency, $parent);
  75  
  76  // Set up the form.
  77  $formoptions = [
  78      'competencyframework' => $competencyframework,
  79      'parent' => $parent,
  80      'persistent' => $competency,
  81      'pagecontextid' => $pagecontextid
  82  ];
  83  $form = new \tool_lp\form\competency($url->out(false), $formoptions);
  84  
  85  // Form cancelled.
  86  if ($form->is_cancelled()) {
  87      redirect($returnurl);
  88  }
  89  
  90  // Get form data.
  91  $data = $form->get_data();
  92  if ($data) {
  93      if (empty($competency)) {
  94          \core_competency\api::create_competency($data);
  95          $returnmsg = get_string('competencycreated', 'tool_lp');
  96      } else {
  97          \core_competency\api::update_competency($data);
  98          $returnmsg = get_string('competencyupdated', 'tool_lp');
  99      }
 100      redirect($returnurl, $returnmsg, null, \core\output\notification::NOTIFY_SUCCESS);
 101  }
 102  
 103  // Render the page.
 104  $output = $PAGE->get_renderer('tool_lp');
 105  echo $output->header();
 106  echo $output->heading($title);
 107  echo $output->heading($subtitle, 3);
 108  
 109  $form->display();
 110  
 111  echo $output->footer();