PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/calendar/classes/local/event/forms/eventtype.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 131 lines | 62 code | 14 blank | 55 comment | 15 complexity | 1fb9b161534fe5ebdb14c5b1821207db MD5 | raw file
  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. * The trait for adding eventtype fields to a form.
  18. *
  19. * @package core_calendar
  20. * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. namespace core_calendar\local\event\forms;
  24. defined('MOODLE_INTERNAL') || die();
  25. /**
  26. * The trait for adding eventtype fields to a form.
  27. *
  28. * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. */
  31. trait eventtype {
  32. /**
  33. * Add the appropriate elements for the available event types.
  34. *
  35. * If the only event type available is 'user' then we add a hidden
  36. * element because there is nothing for the user to choose.
  37. *
  38. * If more than one type is available then we add the elements as
  39. * follows:
  40. * - Always add the event type selector
  41. * - Elements per type:
  42. * - course: add an additional select element with each
  43. * course as an option.
  44. * - group: add a select element for the course (different
  45. * from the above course select) and a select
  46. * element for the group.
  47. *
  48. * @param MoodleQuickForm $mform
  49. * @param array $eventtypes The available event types for the user
  50. */
  51. protected function add_event_type_elements($mform, $eventtypes) {
  52. $options = [];
  53. if (isset($eventtypes['user'])) {
  54. $options['user'] = get_string('user');
  55. }
  56. if (isset($eventtypes['group'])) {
  57. $options['group'] = get_string('group');
  58. }
  59. if (isset($eventtypes['course'])) {
  60. $options['course'] = get_string('course');
  61. }
  62. if (isset($eventtypes['category'])) {
  63. $options['category'] = get_string('category');
  64. }
  65. if (isset($eventtypes['site'])) {
  66. $options['site'] = get_string('site');
  67. }
  68. // If we only have one event type and it's 'user' event then don't bother
  69. // rendering the select boxes because there is no choice for the user to
  70. // make.
  71. if (count(array_keys($eventtypes)) == 1 && isset($eventtypes['user'])) {
  72. $mform->addElement('hidden', 'eventtype');
  73. $mform->setType('eventtype', PARAM_TEXT);
  74. $mform->setDefault('eventtype', 'user');
  75. // Render a static element to tell the user what type of event will
  76. // be created.
  77. $mform->addElement('static', 'staticeventtype', get_string('eventkind', 'calendar'), $options['user']);
  78. return;
  79. } else {
  80. $mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $options);
  81. }
  82. if (isset($eventtypes['category'])) {
  83. $categoryoptions = [];
  84. foreach ($eventtypes['category'] as $id => $category) {
  85. $categoryoptions[$id] = $category;
  86. }
  87. $mform->addElement('select', 'categoryid', get_string('category'), $categoryoptions);
  88. $mform->hideIf('categoryid', 'eventtype', 'noteq', 'category');
  89. }
  90. if (isset($eventtypes['course'])) {
  91. $limit = !has_capability('moodle/calendar:manageentries', \context_system::instance());
  92. $mform->addElement('course', 'courseid', get_string('course'), ['limittoenrolled' => $limit]);
  93. $mform->hideIf('courseid', 'eventtype', 'noteq', 'course');
  94. }
  95. if (isset($eventtypes['group'])) {
  96. $options = ['limittoenrolled' => true];
  97. // Exclude courses without group.
  98. if (isset($eventtypes['course']) && isset($eventtypes['groupcourses'])) {
  99. $options['exclude'] = array_diff(array_keys($eventtypes['course']),
  100. array_keys($eventtypes['groupcourses']));
  101. }
  102. $mform->addElement('course', 'groupcourseid', get_string('course'), $options);
  103. $mform->hideIf('groupcourseid', 'eventtype', 'noteq', 'group');
  104. $groupoptions = [];
  105. foreach ($eventtypes['group'] as $group) {
  106. // We are formatting it this way in order to provide the javascript both
  107. // the course and group ids so that it can enhance the form for the user.
  108. $index = "{$group->courseid}-{$group->id}";
  109. $groupoptions[$index] = format_string($group->name, true,
  110. ['context' => \context_course::instance($group->courseid)]);
  111. }
  112. $mform->addElement('select', 'groupid', get_string('group'), $groupoptions);
  113. $mform->hideIf('groupid', 'eventtype', 'noteq', 'group');
  114. // We handle the group select hide/show actions on the event_form module.
  115. }
  116. }
  117. }