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.

Differences Between: [Versions 400 and 402]

   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   * Contact site support.
  19   *
  20   * @copyright 2022 Simey Lameze <simey@moodle.com>
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   * @package core_user
  23   */
  24  require_once('../config.php');
  25  require_once($CFG->dirroot . '/user/lib.php');
  26  
  27  $user = isloggedin() && !isguestuser() ? $USER : null;
  28  
  29  // If not allowed to view this page, redirect to the homepage. This would be where the site has
  30  // disabled support, or limited it to authenticated users and the current user is a guest or not logged in.
  31  if (!isset($CFG->supportavailability) ||
  32          $CFG->supportavailability == CONTACT_SUPPORT_DISABLED ||
  33          ($CFG->supportavailability == CONTACT_SUPPORT_AUTHENTICATED && is_null($user))) {
  34      redirect($CFG->wwwroot);
  35  }
  36  
  37  if (!empty($CFG->supportpage)) {
  38      redirect($CFG->supportpage);
  39  }
  40  
  41  $PAGE->set_context(context_system::instance());
  42  $PAGE->set_url('/user/contactsitesupport.php');
  43  $PAGE->set_title(get_string('contactsitesupport', 'admin'));
  44  $PAGE->set_heading(get_string('contactsitesupport', 'admin'));
  45  $PAGE->set_pagelayout('standard');
  46  
  47  $renderer = $PAGE->get_renderer('user');
  48  
  49  $form = new \core_user\form\contactsitesupport_form(null, $user);
  50  if ($form->is_cancelled()) {
  51      redirect($CFG->wwwroot);
  52  } else if ($form->is_submitted() && $form->is_validated() && confirm_sesskey()) {
  53      $data = $form->get_data();
  54  
  55      $from = $user ?? core_user::get_noreply_user();
  56      $subject = get_string('supportemailsubject', 'admin', format_string($SITE->fullname));
  57      $data->notloggedinuser = (!$user);
  58      $message = $renderer->render_from_template('user/contact_site_support_email_body', $data);
  59  
  60      if (!email_to_user(core_user::get_support_user(), $from, $subject, $message)) {
  61          $supportemail = $CFG->supportemail;
  62          $form->set_data($data);
  63          $templatectx = [
  64              'supportemail' => $user ? html_writer::link("mailto:{$supportemail}", $supportemail) : false,
  65              'supportform' => $form->render(),
  66          ];
  67  
  68          $output = $renderer->render_from_template('user/contact_site_support_not_available', $templatectx);
  69      } else {
  70          $level = \core\output\notification::NOTIFY_SUCCESS;
  71          redirect($CFG->wwwroot, get_string('supportmessagesent', 'user'), 3, $level);
  72      }
  73  } else {
  74      $output = $form->render();
  75  }
  76  
  77  echo $OUTPUT->header();
  78  
  79  echo $output;
  80  
  81  echo $OUTPUT->footer();