block_calendar_upcoming.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * Handles displaying the calendar upcoming events block.
  18. *
  19. * @package block_calendar_upcoming
  20. * @copyright 2004 Eloy Lafuente (stronk7) {@link http://stronk7.com}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. class block_calendar_upcoming extends block_base {
  24. /**
  25. * Initialise the block.
  26. */
  27. public function init() {
  28. $this->title = get_string('pluginname', 'block_calendar_upcoming');
  29. }
  30. /**
  31. * Return the content of this block.
  32. *
  33. * @return stdClass the content
  34. */
  35. public function get_content() {
  36. global $CFG;
  37. require_once($CFG->dirroot.'/calendar/lib.php');
  38. if ($this->content !== null) {
  39. return $this->content;
  40. }
  41. $this->content = new stdClass;
  42. $this->content->text = '';
  43. $filtercourse = array();
  44. if (empty($this->instance)) { // Overrides: use no course at all.
  45. $courseshown = false;
  46. $this->content->footer = '';
  47. } else {
  48. $courseshown = $this->page->course->id;
  49. $this->content->footer = '<div class="gotocal"><a href="'.$CFG->wwwroot.
  50. '/calendar/view.php?view=upcoming&amp;course='.$courseshown.'">'.
  51. get_string('gotocalendar', 'calendar').'</a>...</div>';
  52. $context = context_course::instance($courseshown);
  53. if (has_any_capability(array('moodle/calendar:manageentries', 'moodle/calendar:manageownentries'), $context)) {
  54. $this->content->footer .= '<div class="newevent"><a href="'.$CFG->wwwroot.
  55. '/calendar/event.php?action=new&amp;course='.$courseshown.'">'.
  56. get_string('newevent', 'calendar').'</a>...</div>';
  57. }
  58. if ($courseshown == SITEID) {
  59. // Being displayed at site level. This will cause the filter to fall back to auto-detecting
  60. // the list of courses it will be grabbing events from.
  61. $filtercourse = calendar_get_default_courses();
  62. } else {
  63. // Forcibly filter events to include only those from the particular course we are in.
  64. $filtercourse = array($courseshown => $this->page->course);
  65. }
  66. }
  67. list($courses, $group, $user) = calendar_set_filters($filtercourse);
  68. $defaultlookahead = CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD;
  69. if (isset($CFG->calendar_lookahead)) {
  70. $defaultlookahead = intval($CFG->calendar_lookahead);
  71. }
  72. $lookahead = get_user_preferences('calendar_lookahead', $defaultlookahead);
  73. $defaultmaxevents = CALENDAR_DEFAULT_UPCOMING_MAXEVENTS;
  74. if (isset($CFG->calendar_maxevents)) {
  75. $defaultmaxevents = intval($CFG->calendar_maxevents);
  76. }
  77. $maxevents = get_user_preferences('calendar_maxevents', $defaultmaxevents);
  78. $events = calendar_get_upcoming($courses, $group, $user, $lookahead, $maxevents);
  79. if (!empty($this->instance)) {
  80. $link = 'view.php?view=day&amp;course='.$courseshown.'&amp;';
  81. $showcourselink = ($this->page->course->id == SITEID);
  82. $this->content->text = calendar_get_block_upcoming($events, $link, $showcourselink);
  83. }
  84. if (empty($this->content->text)) {
  85. $this->content->text = '<div class="post">'. get_string('noupcomingevents', 'calendar').'</div>';
  86. }
  87. return $this->content;
  88. }
  89. }