block_section_links.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. * This file contains the main class for the section links block.
  18. *
  19. * @package block_section_links
  20. * @copyright Jason Hardin
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. /**
  24. * Section links block class.
  25. *
  26. * @package block_section_links
  27. * @copyright Jason Hardin
  28. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29. */
  30. class block_section_links extends block_base {
  31. /**
  32. * Initialises the block instance.
  33. */
  34. public function init() {
  35. $this->title = get_string('pluginname', 'block_section_links');
  36. }
  37. /**
  38. * Returns an array of formats for which this block can be used.
  39. *
  40. * @return array
  41. */
  42. public function applicable_formats() {
  43. return array(
  44. 'course-view-weeks' => true,
  45. 'course-view-topics' => true
  46. );
  47. }
  48. /**
  49. * Generates the content of the block and returns it.
  50. *
  51. * If the content has already been generated then the previously generated content is returned.
  52. *
  53. * @return stdClass
  54. */
  55. public function get_content() {
  56. // The config should be loaded by now.
  57. // If its empty then we will use the global config for the section links block.
  58. if (isset($this->config)){
  59. $config = $this->config;
  60. } else{
  61. $config = get_config('block_section_links');
  62. }
  63. if ($this->content !== null) {
  64. return $this->content;
  65. }
  66. $this->content = new stdClass;
  67. $this->content->footer = '';
  68. $this->content->text = '';
  69. if (empty($this->instance)) {
  70. return $this->content;
  71. }
  72. $course = $this->page->course;
  73. $courseformat = course_get_format($course);
  74. $courseformatoptions = $courseformat->get_format_options();
  75. $context = context_course::instance($course->id);
  76. // Course format options 'numsections' is required to display the block.
  77. if (empty($courseformatoptions['numsections'])) {
  78. return $this->content;
  79. }
  80. // Prepare the highlight value.
  81. if ($course->format == 'weeks') {
  82. $highlight = ceil((time() - $course->startdate) / 604800);
  83. } else if ($course->format == 'topics') {
  84. $highlight = $course->marker;
  85. } else {
  86. $highlight = 0;
  87. }
  88. // Prepare the increment value.
  89. if (!empty($config->numsections1) and ($courseformatoptions['numsections'] > $config->numsections1)) {
  90. $inc = $config->incby1;
  91. } else if ($courseformatoptions['numsections'] > 22) {
  92. $inc = 2;
  93. } else {
  94. $inc = 1;
  95. }
  96. if (!empty($config->numsections2) and ($courseformatoptions['numsections'] > $config->numsections2)) {
  97. $inc = $config->incby2;
  98. } else {
  99. if ($courseformatoptions['numsections'] > 40) {
  100. $inc = 5;
  101. }
  102. }
  103. // Prepare an array of sections to create links for.
  104. $sections = array();
  105. $canviewhidden = has_capability('moodle/course:update', $context);
  106. $coursesections = $courseformat->get_sections();
  107. $coursesectionscount = count($coursesections);
  108. for ($i = $inc; $i <= $coursesectionscount; $i += $inc) {
  109. if ($i > $courseformatoptions['numsections'] || !isset($coursesections[$i])) {
  110. continue;
  111. }
  112. $section = $coursesections[$i];
  113. if ($section->section && ($section->visible || $canviewhidden)) {
  114. $sections[$i] = (object)array(
  115. 'section' => $section->section,
  116. 'visible' => $section->visible,
  117. 'highlight' => ($section->section == $highlight)
  118. );
  119. }
  120. }
  121. if (!empty($sections)) {
  122. $sectiontojumpto = false;
  123. if ($highlight && isset($sections[$highlight]) && ($sections[$highlight]->visible || $canviewhidden)) {
  124. $sectiontojumpto = $highlight;
  125. }
  126. // Render the sections.
  127. $renderer = $this->page->get_renderer('block_section_links');
  128. $this->content->text = $renderer->render_section_links($this->page->course, $sections, $sectiontojumpto);
  129. }
  130. return $this->content;
  131. }
  132. /**
  133. * Returns true if this block has instance config.
  134. *
  135. * @return bool
  136. **/
  137. public function instance_allow_config() {
  138. return true;
  139. }
  140. /**
  141. * Returns true if this block has global config.
  142. *
  143. * @return bool
  144. */
  145. public function has_config() {
  146. return true;
  147. }
  148. }