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.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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  
  83              // Render a static element to tell the user what type of event will
  84              // be created.
  85              $mform->addElement('static', 'staticeventtype', get_string('eventkind', 'calendar'), $options['user']);
  86              return;
  87          } else {
  88              $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $options);
  89          }
  90  
  91          if (!empty($eventtypes['category'])) {
  92              $categoryoptions = [];
  93              foreach (\core_course_category::make_categories_list('moodle/category:manage') as $id => $category) {
  94                  $categoryoptions[$id] = $category;
  95              }
  96  
  97              $mform->addElement('autocomplete', 'categoryid', get_string('category'), $categoryoptions);
  98              $mform->hideIf('categoryid', 'eventtype', 'noteq', 'category');
  99          }
 100  
 101          $showall = is_siteadmin() && !empty($CFG->calendar_adminseesall);
 102          if (!empty($eventtypes['course'])) {
 103              $mform->addElement('course', 'courseid', get_string('course'), ['limittoenrolled' => !$showall]);
 104              $mform->hideIf('courseid', 'eventtype', 'noteq', 'course');
 105          }
 106  
 107          if (!empty($eventtypes['group'])) {
 108              $groups = !(empty($this->_customdata['groups'])) ? $this->_customdata['groups'] : null;
 109              // Get the list of courses without groups to filter on the course selector.
 110              $sql = "SELECT c.id
 111                        FROM {course} c
 112                       WHERE c.id NOT IN (
 113                              SELECT DISTINCT courseid FROM {groups}
 114                             )";
 115              $coursesnogroup = $DB->get_records_sql($sql);
 116              $mform->addElement('course', 'groupcourseid', get_string('course'),  ['limittoenrolled' => !$showall,
 117                      'exclude' => array_keys($coursesnogroup)]);
 118              $mform->hideIf('groupcourseid', 'eventtype', 'noteq', 'group');
 119  
 120              $mform->addElement('select', 'groupid', get_string('group'), $groups);
 121              $mform->hideIf('groupid', 'eventtype', 'noteq', 'group');
 122              // We handle the group select hide/show actions on the event_form module.
 123          }
 124      }
 125  }