Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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   * User evidence (evidence of prior learning).
  19   *
  20   * @package    tool_lp
  21   * @copyright  2015 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../../../config.php');
  26  
  27  require_login(null, false);
  28  if (isguestuser()) {
  29      throw new require_login_exception('Guests are not allowed here.');
  30  }
  31  \core_competency\api::require_enabled();
  32  
  33  $userid = optional_param('userid', $USER->id, PARAM_INT);
  34  $id = optional_param('id', null, PARAM_INT);
  35  $returntype = optional_param('return', null, PARAM_ALPHA);
  36  
  37  $url = new moodle_url('/admin/tool/lp/user_evidence_edit.php', array('id' => $id, 'userid' => $userid, 'return' => $returntype));
  38  
  39  $userevidence = null;
  40  if (empty($id)) {
  41      $pagetitle = get_string('addnewuserevidence', 'tool_lp');
  42      list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_user_evidence($userid, $url, null,
  43          $pagetitle, $returntype);
  44  
  45  } else {
  46      $userevidence = \core_competency\api::read_user_evidence($id);
  47  
  48      // The userid parameter must be the same as the owner of the evidence.
  49      if ($userid != $userevidence->get('userid')) {
  50          throw new coding_exception('Inconsistency between the userid parameter and the userid of the plan.');
  51      }
  52  
  53      $pagetitle = get_string('edituserevidence', 'tool_lp');
  54      list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_user_evidence($userid, $url, $userevidence,
  55          $pagetitle, $returntype);
  56  }
  57  
  58  // The context has been set to the user context in the page_helper.
  59  $context = $PAGE->context;
  60  
  61  $fileareaoptions = array('subdirs' => false);
  62  $customdata = array(
  63      'fileareaoptions' => $fileareaoptions,
  64      'persistent' => $userevidence,
  65      'userid' => $userid,
  66  );
  67  
  68  // Check if user has permissions to manage user evidence.
  69  if ($userevidence != null) {
  70      if (!$userevidence->can_manage()) {
  71          throw new required_capability_exception($context, 'moodle/competency:userevidencemanage', 'nopermissions', '');
  72      }
  73      $customdata['evidence'] = $userevidence;
  74  
  75  } else if (!\core_competency\user_evidence::can_manage_user($userid)) {
  76      throw new required_capability_exception($context, 'moodle/competency:userevidencemanage', 'nopermissions', '');
  77  }
  78  
  79  $form = new \tool_lp\form\user_evidence($url->out(false), $customdata);
  80  if ($form->is_cancelled()) {
  81      redirect($returnurl);
  82  }
  83  
  84  // Load existing user evidence.
  85  $itemid = null;
  86  if ($userevidence) {
  87      $itemid = $userevidence->get('id');
  88  }
  89  
  90  // Massaging the file API.
  91  $draftitemid = file_get_submitted_draft_itemid('files');
  92  file_prepare_draft_area($draftitemid, $context->id, 'core_competency', 'userevidence', $itemid, $fileareaoptions);
  93  $form->set_data((object) array('files' => $draftitemid));
  94  
  95  // Hurray, the user has submitted the form! Everyone loves forms :)!
  96  if ($data = $form->get_data()) {
  97      require_sesskey();
  98      $draftitemid = $data->files;
  99      unset($data->files);
 100  
 101      if (empty($userevidence)) {
 102          $userevidence = \core_competency\api::create_user_evidence($data, $draftitemid);
 103          $returnurl = new moodle_url('/admin/tool/lp/user_evidence.php', ['id' => $userevidence->get('id')]);
 104          $returnmsg = get_string('userevidencecreated', 'tool_lp');
 105      } else {
 106          \core_competency\api::update_user_evidence($data, $draftitemid);
 107          $returnmsg = get_string('userevidenceupdated', 'tool_lp');
 108      }
 109      redirect($returnurl, $returnmsg, null, \core\output\notification::NOTIFY_SUCCESS);
 110  }
 111  
 112  // We're getting there...
 113  $output = $PAGE->get_renderer('tool_lp');
 114  echo $output->header();
 115  echo $output->heading($title);
 116  if (!empty($subtitle)) {
 117      echo $output->heading($subtitle, 3);
 118  }
 119  
 120  $form->display();
 121  
 122  echo $output->footer();