Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   * Class registration_form
  19   *
  20   * @package    tool_brickfield
  21   * @copyright  2021 Brickfield Education Labs https://www.brickfield.ie
  22   * @author  2020 JM Tomas <jmtomas@tresipunt.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
  24   */
  25  
  26  namespace tool_brickfield\form;
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use coding_exception;
  30  use dml_exception;
  31  use html_writer;
  32  use moodle_exception;
  33  use moodleform;
  34  use stdClass;
  35  use tool_brickfield\manager;
  36  use tool_brickfield\registration;
  37  
  38  global $CFG;
  39  require_once($CFG->libdir . '/formslib.php');
  40  
  41  /**
  42   * Class registration_form
  43   *
  44   * @package    tool_brickfield
  45   * @author  2020 JM Tomas <jmtomas@tresipunt.com>
  46   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
  47   */
  48  class registration_form  extends moodleform {
  49  
  50      /**
  51       * Form definition.
  52       * @throws moodle_exception
  53       */
  54      public function definition() {
  55          $mform = & $this->_form;
  56          $required = get_string('required');
  57          $info = $this->get_defaultinfo();
  58          $registration = new registration();
  59          $key = $registration->get_api_key();
  60          $hash = $registration->get_secret_key();
  61  
  62          $mform->addElement('header', 'activationheader', get_string('activationheader', manager::PLUGINNAME));
  63          $mform->addElement('text', 'key', get_string('secretkey', manager::PLUGINNAME));
  64          $mform->setType('key', PARAM_TEXT);
  65          $mform->setDefault('key', !empty($key) ? $key : '');
  66          $mform->addHelpButton('key', 'secretkey', manager::PLUGINNAME);
  67  
  68          $mform->addElement('text', 'hash', get_string('sitehash', manager::PLUGINNAME));
  69          $mform->setType('hash', PARAM_TEXT);
  70          $mform->setDefault('hash', !empty($hash) ? $hash : '');
  71          $mform->addHelpButton('hash', 'sitehash', manager::PLUGINNAME);
  72  
  73          $mform->addElement('header', 'moreinfo', get_string('moreinfo', manager::PLUGINNAME));
  74          $mform->addElement('static', 'siteinfosummary', '',
  75              get_string('sendfollowinginfo', manager::PLUGINNAME, $info->moreinfostring));
  76  
  77          $mform->addElement('hidden', 'lang', $info->languagecode);
  78          $mform->setType('lang', PARAM_TEXT);
  79          $mform->addElement('hidden', 'countrycode', $info->country);
  80          $mform->setType('countrycode', PARAM_TEXT);
  81          $mform->addElement('hidden', 'url', $info->url);
  82          $mform->setType('url', PARAM_URL);
  83  
  84          $this->add_action_buttons(false, get_string('activate', manager::PLUGINNAME, '#'));
  85      }
  86  
  87      /**
  88       * Get default data for registration form
  89       *
  90       * @throws moodle_exception
  91       * @return stdClass
  92       */
  93      protected function get_defaultinfo(): stdClass {
  94          global $CFG;
  95          $admin = get_admin();
  96          $site = get_site();
  97          $data = new stdClass();
  98          $data->name = $site->fullname;
  99          $data->url = $CFG->wwwroot;
 100          $data->language = get_string('thislanguage', 'langconfig');
 101          $data->languagecode = $admin->lang ?: $CFG->lang;
 102          $data->country = $admin->country ?: $CFG->country;
 103          $data->email = $admin->email;
 104          $data->moreinfo = self::get_moreinfo();
 105          $data->moreinfostring = self::get_moreinfostring($data->moreinfo);
 106          return $data;
 107      }
 108  
 109      /**
 110       * Get more information.
 111       * @return array
 112       * @throws dml_exception
 113       */
 114      private static function get_moreinfo(): array {
 115          global $CFG, $DB;
 116          $moreinfo = array();
 117          $moodlerelease = $CFG->release;
 118          if (preg_match('/^(\d+\.\d.*?)[. ]/', $moodlerelease, $matches)) {
 119              $moodlerelease = $matches[1];
 120          }
 121          $moreinfo['release'] = $moodlerelease;
 122          $moreinfo['numcourses'] = $DB->count_records('course') - 1;
 123          $moreinfo['numusers'] = $DB->count_records('user', array('deleted' => 0));
 124          $moreinfo['numfiles'] = $DB->count_records('files');
 125          $moreinfo['numfactivities'] = $DB->count_records('course_modules');
 126          $moreinfo['mobileservice'] = empty($CFG->enablemobilewebservice) ? 0 : $CFG->enablemobilewebservice;
 127          $moreinfo['usersmobileregistered'] = $DB->count_records('user_devices');
 128          $moreinfo['contentyperesults'] = '';
 129          $moreinfo['contenttypeerrors'] = '';
 130          $moreinfo['percheckerrors'] = '';
 131          return $moreinfo;
 132      }
 133  
 134      /**
 135       * Get HTML list for more information.
 136       *
 137       * @param array $moreinfo
 138       * @return string
 139       * @throws coding_exception
 140       */
 141      private static function get_moreinfostring(array $moreinfo): string {
 142          $html = html_writer::start_tag('ul');
 143          foreach ($moreinfo as $key => $value) {
 144              $html .= html_writer::tag('li', get_string($key, manager::PLUGINNAME, $value));
 145          }
 146          $html .= html_writer::end_tag('ul');
 147          return $html;
 148      }
 149  }