Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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   * The trait for adding eventtype fields to a form.
  19   *
  20   * @package     core_calendar
  21   * @copyright   2017 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_calendar\local\event\forms;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * The trait for adding eventtype fields to a form.
  30   *
  31   * @copyright   2017 Andrew Nicols <andrew@nicols.co.uk>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  trait eventtype {
  35  
  36      /**
  37       * Add the appropriate elements for the available event types.
  38       *
  39       * If the only event type available is 'user' then we add a hidden
  40       * element because there is nothing for the user to choose.
  41       *
  42       * If more than one type is available then we add the elements as
  43       * follows:
  44       *      - Always add the event type selector
  45       *      - Elements per type:
  46       *          - course: add an additional select element with each
  47       *                    course as an option.
  48       *          - group: add a select element for the course (different
  49       *                   from the above course select) and a select
  50       *                   element for the group.
  51       *
  52       * @param MoodleQuickForm $mform
  53       * @param array $eventtypes The available event types for the user
  54       */
  55      protected function add_event_type_elements($mform, $eventtypes) {
  56          global $CFG, $DB;
  57          $options = [];
  58  
  59          if (!empty($eventtypes['user'])) {
  60              $options['user'] = get_string('user', 'calendar');
  61          }
  62          if (!empty($eventtypes['group'])) {
  63              $options['group'] = get_string('group', 'calendar');
  64          }
  65          if (!empty($eventtypes['course'])) {
  66              $options['course'] = get_string('course', 'calendar');
  67          }
  68          if (!empty($eventtypes['category'])) {
  69              $options['category'] = get_string('category', 'calendar');
  70          }
  71          if (!empty($eventtypes['site'])) {
  72              $options['site'] = get_string('site', 'calendar');
  73          }
  74  
  75          // If we only have one event type and it's 'user' event then don't bother
  76          // rendering the select boxes because there is no choice for the user to
  77          // make.
  78          if (!empty($eventtypes['user']) && count($options) == 1) {
  79              $mform->addElement('hidden', 'eventtype');
  80              $mform->setType('eventtype', PARAM_TEXT);
  81              $mform->setDefault('eventtype', 'user');
  82              return;
  83          } else {
  84              $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $options);
  85          }
  86  
  87          if (!empty($eventtypes['category'])) {
  88              $categoryoptions = [];
  89              foreach (\core_course_category::make_categories_list('moodle/category:manage') as $id => $category) {
  90                  $categoryoptions[$id] = $category;
  91              }
  92  
  93              $mform->addElement('autocomplete', 'categoryid', get_string('category'), $categoryoptions);
  94              $mform->hideIf('categoryid', 'eventtype', 'noteq', 'category');
  95          }
  96  
  97          $showall = is_siteadmin() && !empty($CFG->calendar_adminseesall);
  98          if (!empty($eventtypes['course'])) {
  99              $mform->addElement('course', 'courseid', get_string('course'), ['limittoenrolled' => !$showall]);
 100              $mform->hideIf('courseid', 'eventtype', 'noteq', 'course');
 101          }
 102  
 103          if (!empty($eventtypes['group'])) {
 104              $groups = !(empty($this->_customdata['groups'])) ? $this->_customdata['groups'] : null;
 105              // Get the list of courses without groups to filter on the course selector.
 106              $sql = "SELECT c.id
 107                        FROM {course} c
 108                       WHERE c.id NOT IN (
 109                              SELECT DISTINCT courseid FROM {groups}
 110                             )";
 111              $coursesnogroup = $DB->get_records_sql($sql);
 112              $mform->addElement('course', 'groupcourseid', get_string('course'),  ['limittoenrolled' => !$showall,
 113                      'exclude' => array_keys($coursesnogroup)]);
 114              $mform->hideIf('groupcourseid', 'eventtype', 'noteq', 'group');
 115  
 116              $mform->addElement('select', 'groupid', get_string('group'), $groups);
 117              $mform->hideIf('groupid', 'eventtype', 'noteq', 'group');
 118              // We handle the group select hide/show actions on the event_form module.
 119          }
 120      }
 121  }