Skip to content
Browse files

MDL-37615 block_section_links: brought up to current standards.

  • Loading branch information...
1 parent 1631d5f commit 554b67545bcb05970b36439761752c21d90fc93b Sam Hemelryk committed Jan 22, 2013
View
164 blocks/section_links/block_section_links.php
@@ -1,5 +1,4 @@
<?php
-
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -15,50 +14,60 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+/**
+ * This file contains the main class for the section links block.
+ *
+ * @package block_section_links
+ * @copyright Jason Hardin
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
/**
- * Section links block
+ * Section links block class.
*
- * @package moodlecore
+ * @package block_section_links
+ * @copyright Jason Hardin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_section_links extends block_base {
- function init() {
+ /**
+ * Initialises the block instance.
+ */
+ public function init() {
$this->title = get_string('pluginname', 'block_section_links');
}
- function instance_config($instance) {
- global $DB;
-
- parent::instance_config($instance);
- $course = $this->page->course;
- if (isset($course->format)) {
- if ($course->format == 'topics') {
- $this->title = get_string('topics', 'block_section_links');
- } else if ($course->format == 'weeks') {
- $this->title = get_string('weeks', 'block_section_links');
- } else {
- $this->title = get_string('pluginname', 'block_section_links');
- }
- }
- }
-
- function applicable_formats() {
- return (array('course-view-weeks' => true, 'course-view-topics' => true));
+ /**
+ * Returns an array of formats for which this block can be used.
+ *
+ * @return array
+ */
+ public function applicable_formats() {
+ return array(
+ 'course-view-weeks' => true,
+ 'course-view-topics' => true
+ );
}
- function get_content() {
- global $CFG, $USER, $DB;
-
- $highlight = 0;
- if(isset($this->config)){
+ /**
+ * Generates the content of the block and returns it.
+ *
+ * If the content has already been generated then the previously generated content is returned.
+ *
+ * @return stdClass
+ */
+ public function get_content() {
+
+ // The config should be loaded by now.
+ // If its empty then we will use the global config for the section links block.
+ if (isset($this->config)){
$config = $this->config;
} else{
$config = get_config('block_section_links');
}
- if ($this->content !== NULL) {
+ if ($this->content !== null) {
return $this->content;
}
@@ -71,82 +80,81 @@ function get_content() {
}
$course = $this->page->course;
- $courseformatoptions = course_get_format($course)->get_format_options();
+ $courseformat = course_get_format($course);
+ $courseformatoptions = $courseformat->get_format_options();
$context = context_course::instance($course->id);
- if ($course->format == 'weeks' or $course->format == 'weekscss') {
- $highlight = ceil((time()-$course->startdate)/604800);
- $linktext = get_string('jumptocurrentweek', 'block_section_links');
- $sectionname = 'week';
- }
- else if ($course->format == 'topics') {
+ // Prepare the highlight value.
+ if ($course->format == 'weeks') {
+ $highlight = ceil((time() - $course->startdate) / 604800);
+ } else if ($course->format == 'topics') {
$highlight = $course->marker;
- $linktext = get_string('jumptocurrenttopic', 'block_section_links');
- $sectionname = 'topic';
+ } else {
+ $highlight = 0;
}
- $inc = 1;
- if(!empty($config->numsections1) and ($courseformatoptions['numsections'] > $config->numsections1)) {
+ // Prepare the increment value.
+ if (!empty($config->numsections1) and ($courseformatoptions['numsections'] > $config->numsections1)) {
$inc = $config->incby1;
+ } else if ($courseformatoptions['numsections'] > 22) {
+ $inc = 2;
} else {
- if ($courseformatoptions['numsections'] > 22) {
- $inc = 2;
- }
+ $inc = 1;
}
-
- if(!empty($config->numsections2) and ($courseformatoptions['numsections'] > $config->numsections2)) {
+ if (!empty($config->numsections2) and ($courseformatoptions['numsections'] > $config->numsections2)) {
$inc = $config->incby2;
} else {
if ($courseformatoptions['numsections'] > 40) {
$inc = 5;
}
}
- $sql = "SELECT section, visible
- FROM {course_sections}
- WHERE course = ? AND
- section < ".($courseformatoptions['numsections']+1)."
- ORDER BY section";
-
- if ($sections = $DB->get_records_sql($sql, array($course->id))) {
- $text = '<ol class="inline-list">';
- for ($i = $inc; $i <= $courseformatoptions['numsections']; $i += $inc) {
- if (!isset($sections[$i])) {
- continue;
- }
- $isvisible = $sections[$i]->visible;
- if (!$isvisible and !has_capability('moodle/course:update', $context)) {
- continue;
- }
- $style = ($isvisible) ? '' : ' class="dimmed"';
- if ($i == $highlight) {
- $text .= '<li><a href="'.course_get_url($course, $i)."\"$style><strong>$i</strong></a></li>\n";
- } else {
- $text .= '<li><a href="'.course_get_url($course, $i)."\"$style>$i</a></li>\n";
- }
+ // Prepare an array of sections to create links for.
+ $sections = array();
+ $canviewhidden = has_capability('moodle/course:update', $context);
+ $coursesections = $courseformat->get_sections();
+ $coursesectionscount = count($coursesections);
+ for ($i = $inc; $i <= $coursesectionscount; $i += $inc) {
+ if ($i > $courseformatoptions['numsections'] || !isset($coursesections[$i])) {
+ continue;
}
- $text .= '</ol>';
- if ($highlight and isset($sections[$highlight])) {
- $isvisible = $sections[$highlight]->visible;
- if ($isvisible or has_capability('moodle/course:update', $context)) {
- $style = ($isvisible) ? '' : ' class="dimmed"';
- $text .= "\n<a href=\"".course_get_url($course, $highlight)."\"$style>$linktext</a>";
- }
+ $section = $coursesections[$i];
+ if ($section->section && ($section->visible || $canviewhidden)) {
+ $sections[$i] = (object)array(
+ 'section' => $section->section,
+ 'visible' => $section->visible,
+ 'highlight' => ($section->section == $highlight)
+ );
}
}
- $this->content->text = $text;
+ if (!empty($sections)) {
+ $sectiontojumpto = false;
+ if ($highlight && isset($sections[$highlight]) && ($sections[$highlight]->visible || $canviewhidden)) {
+ $sectiontojumpto = $highlight;
+ }
+ // Render the sections.
+ $renderer = $this->page->get_renderer('block_section_links');
+ $this->content->text = $renderer->render_section_links($this->page->course, $sections, $sectiontojumpto);
+ }
+
return $this->content;
}
/**
- * Has instance config
- * @return boolean
+ * Returns true if this block has instance config.
+ *
+ * @return bool
**/
- function instance_allow_config() {
+ public function instance_allow_config() {
return true;
}
- function has_config() {
+ /**
+ * Returns true if this block has global config.
+ *
+ * @return bool
+ */
+ public function has_config() {
return true;
}
}
View
97 blocks/section_links/config_instance.html
@@ -1,97 +0,0 @@
-<?php
-// This file is part of Moodle - http://moodle.org/
-//
-// Moodle is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// Moodle is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
-
-
-/**
- * Section links block
- *
- * @package moodlecore
- * @Author Jason Hardin
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-$numberofsections = array();
-for ($i = 1; $i < 53; $i++){
- $numberofsections[$i] = $i;
-}
-
-$increments = array();
-
-for ($i = 1; $i < 11; $i++){
- $increments[$i] = $i;
-}
-
-if(isset($this->config)){
- $config = $this->config;
-} else{
- $config = get_config('block_section_links');
-}
-
-$selected = array();
-if (!empty($config->numsections1)) {
- if (empty($config->incby1)) {
- $config->incby1 = 2;
- }
- $selected[1] = array($config->numsections1, $config->incby1);
-} else {
- $selected[1] = array(22, 2);
-}
-
-if (!empty($config->numsections2)) {
- if (empty($config->incby1)) {
- $config->incby1 = 5;
- }
- $selected[2] = array($config->numsections2, $config->incby2);
-} else {
- $selected[2] = array(40, 5);
-}
-
-?>
-<table cellpadding="9" cellspacing="0">
-<?php
-for($i = 1; $i < 3; $i++){
-?>
- <tr valign="top">
- <td align="right">
- <label for="menunumsections<?php echo $i; ?>"><?php print_string('numsections'.$i, 'block_section_links'); ?>:</label>
- </td>
- <td>
- <?php choose_from_menu($numberofsections, 'numsections'.$i, $selected[$i][0]); ?>
- </td>
- <td>
- <?php print_string('numsectionsdesc'.$i, 'block_section_links'); ?>
- </td>
- </tr>
- <tr valign="top">
- <td align="right">
- <label for="menuincby<?php echo $i;?>"><?php print_string('incby'.$i, 'block_section_links'); ?>:</label>
- </td>
- <td>
- <?php choose_from_menu($increments, 'incby'.$i, $selected[$i][1]); ?>
- </td>
- <td>
- <?php print_string('incbydesc'.$i, 'block_section_links'); ?>
- </td>
- </tr>
-<?php }
-?>
-<tr>
- <td colspan="3" align="center">
- <input type="hidden" name="sesskey" value="<?php echo sesskey();?>">
- <input type="submit" value="<?php print_string('savechanges') ?>" />
- </td>
-</tr>
-</table>
View
6 blocks/section_links/db/upgrade.php
@@ -58,6 +58,8 @@ function xmldb_block_section_links_upgrade($oldversion, $block) {
if ($oldversion < 2013012200.00) {
+ // The section links block used to use its own crazy plugin name.
+ // Here we are converting it to the proper component name.
$oldplugin = 'blocks/section_links';
$newplugin = 'block_section_links';
@@ -73,8 +75,8 @@ function xmldb_block_section_links_upgrade($oldversion, $block) {
}
// Main savepoint reached.
- upgrade_block_savepoint(true, 2013012200.00, 'block_section_links');
+ upgrade_block_savepoint(true, 2013012200.00, 'section_links');
}
return true;
-}
+}
View
86 blocks/section_links/edit_form.php
@@ -0,0 +1,86 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Instance configuration for the section links block.
+ *
+ * @package block_section_links
+ * @copyright 2013 Sam Hemelryk
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * Instance configuration form.
+ *
+ * @package block_section_links
+ * @copyright 2013 Sam Hemelryk
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class block_section_links_edit_form extends block_edit_form {
+
+ /**
+ * The definition of the fields to use.
+ *
+ * @param MoodleQuickForm $mform
+ */
+ protected function specific_definition($mform) {
+ $mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
+
+ $numberofsections = array();
+ for ($i = 1; $i < 53; $i++){
+ $numberofsections[$i] = $i;
+ }
+
+ $increments = array();
+
+ for ($i = 1; $i < 11; $i++){
+ $increments[$i] = $i;
+ }
+
+ $config = get_config('block_section_links');
+
+ $selected = array(
+ 1 => array(22, 2),
+ 2 => array(40, 5),
+ );
+ if (!empty($config->numsections1)) {
+ if (empty($config->incby1)) {
+ $config->incby1 = $selected[1][1];
+ }
+ $selected[1] = array($config->numsections1, $config->incby1);
+ }
+
+ if (!empty($config->numsections2)) {
+ if (empty($config->incby1)) {
+ $config->incby1 = $selected[2][1];
+ }
+ $selected[2] = array($config->numsections2, $config->incby2);
+ }
+
+ for ($i = 1; $i < 3; $i++) {
+ $mform->addElement('select', 'config_numsections'.$i, get_string('numsections'.$i, 'block_section_links'), $numberofsections);
+ $mform->setDefault('config_numsections'.$i, $selected[$i][0]);
+ $mform->setType('config_numsections'.$i, PARAM_INT);
+ $mform->addHelpButton('config_numsections'.$i, 'numsections'.$i, 'block_section_links');
+
+ $mform->addElement('select', 'config_incby'.$i, get_string('incby'.$i, 'block_section_links'), $increments);
+ $mform->setDefault('config_incby'.$i, $selected[$i][1]);
+ $mform->setType('config_incby'.$i, PARAM_INT);
+ $mform->addHelpButton('config_incby'.$i, 'incby'.$i, 'block_section_links');
+ }
+
+ }
+}
View
15 blocks/section_links/lang/en/block_section_links.php
@@ -1,5 +1,4 @@
<?php
-
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -18,21 +17,21 @@
/**
* Strings for component 'block_section_links', language 'en', branch 'MOODLE_20_STABLE'
*
- * @package block_section_links
- * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package block_section_links
+ * @copyright Jason Hardin
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-$string['incbydesc1'] = 'This is the value the section is incremented each time a section link is displayed starting at 1.';
-$string['incbydesc2'] = 'This is the value the section is incremented each time a section link is displayed starting at 1.';
$string['incby1'] = 'Increase by';
+$string['incby1_help'] = 'This is the value the section is incremented each time a section link is displayed starting at 1.';
$string['incby2'] = 'Alternative increase by';
+$string['incby2_help'] = 'This is the value the section is incremented each time a section link is displayed starting at 1.';
$string['jumptocurrenttopic'] = 'Jump to current topic';
$string['jumptocurrentweek'] = 'Jump to current week';
-$string['numsectionsdesc1'] = 'Once the number of sections in the course reaches this number then the increment by value is used.';
-$string['numsectionsdesc2'] = 'Once the number of sections in the course reaches this number then the Alternative increment by value is used.';
$string['numsections1'] = 'Number of sections';
+$string['numsections1_help'] = 'Once the number of sections in the course reaches this number then the increment by value is used.';
$string['numsections2'] = 'Alternative number of sections';
+$string['numsections2_help'] = 'Once the number of sections in the course reaches this number then the Alternative increment by value is used.';
$string['pluginname'] = 'Section links';
$string['section_links:addinstance'] = 'Add a new section links block';
$string['topics'] = 'Topics';
View
76 blocks/section_links/renderer.php
@@ -0,0 +1,76 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Renderer for the section links block.
+ *
+ * @since 2.5
+ * @package block_section_links
+ * @copyright 2013 Sam Hemelryk
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * Renderer for the section links block.
+ *
+ * @package block_section_links
+ * @copyright 2013 Sam Hemelryk
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class block_section_links_renderer extends plugin_renderer_base {
+
+ /**
+ * Render a series of section links.
+ *
+ * @param stdClass $course The course we are rendering for.
+ * @param array $sections An array of section objects to render.
+ * @param bool|int The section to provide a jump to link for.
+ * @return string The HTML to display.
+ */
+ public function render_section_links(stdClass $course, array $sections, $jumptosection = false) {
+ $html = html_writer::start_tag('ol', array('class' => 'inline-list'));
+ foreach ($sections as $section) {
+ $attributes = array();
+ if (!$section->visible) {
+ $attributes['class'] = 'dimmed';
+ }
+ $html .= html_writer::start_tag('li');
+ $sectiontext = $section->section;
+ if ($section->highlight) {
+ $sectiontext = html_writer::tag('strong', $sectiontext);
+ }
+ $html .= html_writer::link(course_get_url($course, $section->section), $sectiontext, $attributes);
+ $html .= html_writer::end_tag('li').' ';
+ }
+ $html .= html_writer::end_tag('ol');
+ if ($jumptosection && isset($sections[$jumptosection])) {
+
+ if ($course->format == 'weeks') {
+ $linktext = new lang_string('jumptocurrentweek', 'block_section_links');
+ } else if ($course->format == 'topics') {
+ $linktext = new lang_string('jumptocurrenttopic', 'block_section_links');
+ }
+
+ $attributes = array();
+ if (!$sections[$jumptosection]->visible) {
+ $attributes['class'] = 'dimmed';
+ }
+ $html .= html_writer::link(course_get_url($course, $jumptosection), $linktext, $attributes);
+ }
+
+ return $html;
+ }
+}
View
8 blocks/section_links/settings.php
@@ -14,11 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
-
/**
* Section links block
*
- * @package moodlecore
+ * @package block_section_links
+ * @copyright Jason Hardin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@@ -41,11 +41,11 @@
for($i = 1; $i < 3; $i++){
$settings->add(new admin_setting_configselect('block_section_links/numsections'.$i, get_string('numsections'.$i, 'block_section_links'),
- get_string('numsectionsdesc'.$i, 'block_section_links'),
+ get_string('numsections'.$i.'_help', 'block_section_links'),
$selected[$i][0], $numberofsections));
$settings->add(new admin_setting_configselect('block_section_links/incby'.$i, get_string('incby'.$i, 'block_section_links'),
- get_string('incbydesc'.$i, 'block_section_links'),
+ get_string('incby'.$i.'_help', 'block_section_links'),
$selected[$i][1], $increments));
}
}
View
5 blocks/section_links/version.php
@@ -17,9 +17,8 @@
/**
* Version details
*
- * @package block
- * @subpackage section_links
- * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
+ * @package block_section_links
+ * @copyright Jason Hardin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

0 comments on commit 554b675

Please sign in to comment.
Something went wrong with that request. Please try again.