From 554b67545bcb05970b36439761752c21d90fc93b Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Tue, 22 Jan 2013 11:40:32 +1300 Subject: [PATCH] MDL-37615 block_section_links: brought up to current standards. --- blocks/section_links/block_section_links.php | 166 +++++++++--------- blocks/section_links/config_instance.html | 97 ---------- blocks/section_links/db/upgrade.php | 6 +- blocks/section_links/edit_form.php | 86 +++++++++ .../lang/en/block_section_links.php | 15 +- blocks/section_links/renderer.php | 76 ++++++++ blocks/section_links/settings.php | 8 +- blocks/section_links/version.php | 5 +- 8 files changed, 266 insertions(+), 193 deletions(-) delete mode 100644 blocks/section_links/config_instance.html create mode 100644 blocks/section_links/edit_form.php create mode 100644 blocks/section_links/renderer.php diff --git a/blocks/section_links/block_section_links.php b/blocks/section_links/block_section_links.php index a5489bd84d0..6e4bd1e1d35 100644 --- a/blocks/section_links/block_section_links.php +++ b/blocks/section_links/block_section_links.php @@ -1,5 +1,4 @@ . +/** + * 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'); - } - } + /** + * 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 applicable_formats() { - return (array('course-view-weeks' => true, 'course-view-topics' => true)); - } + /** + * 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() { - function get_content() { - global $CFG, $USER, $DB; - - $highlight = 0; - if(isset($this->config)){ + // 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,30 +80,28 @@ class block_section_links extends block_base { } $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'; - } - $inc = 1; - - if(!empty($config->numsections1) and ($courseformatoptions['numsections'] > $config->numsections1)) { - $inc = $config->incby1; } else { - if ($courseformatoptions['numsections'] > 22) { - $inc = 2; - } + $highlight = 0; } - if(!empty($config->numsections2) and ($courseformatoptions['numsections'] > $config->numsections2)) { + // Prepare the increment value. + if (!empty($config->numsections1) and ($courseformatoptions['numsections'] > $config->numsections1)) { + $inc = $config->incby1; + } else if ($courseformatoptions['numsections'] > 22) { + $inc = 2; + } else { + $inc = 1; + } + if (!empty($config->numsections2) and ($courseformatoptions['numsections'] > $config->numsections2)) { $inc = $config->incby2; } else { if ($courseformatoptions['numsections'] > 40) { @@ -102,51 +109,52 @@ class block_section_links extends block_base { } } - $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 = '
    '; - 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 .= '
  1. $i
  2. \n"; - } else { - $text .= '
  3. $i
  4. \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 .= '
'; - 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$linktext"; - } + $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; } } diff --git a/blocks/section_links/config_instance.html b/blocks/section_links/config_instance.html deleted file mode 100644 index 87bedc5c27a..00000000000 --- a/blocks/section_links/config_instance.html +++ /dev/null @@ -1,97 +0,0 @@ -. - - -/** - * 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); -} - -?> - - - - - - - - - - - - - - - - -
- - - - - -
- - - - - -
- - -
diff --git a/blocks/section_links/db/upgrade.php b/blocks/section_links/db/upgrade.php index 06657d0159f..dbe36b17c19 100644 --- a/blocks/section_links/db/upgrade.php +++ b/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; -} \ No newline at end of file +} diff --git a/blocks/section_links/edit_form.php b/blocks/section_links/edit_form.php new file mode 100644 index 00000000000..63a3593aa61 --- /dev/null +++ b/blocks/section_links/edit_form.php @@ -0,0 +1,86 @@ +. + +/** + * 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'); + } + + } +} \ No newline at end of file diff --git a/blocks/section_links/lang/en/block_section_links.php b/blocks/section_links/lang/en/block_section_links.php index 37502602dcb..77cf69fb5d0 100644 --- a/blocks/section_links/lang/en/block_section_links.php +++ b/blocks/section_links/lang/en/block_section_links.php @@ -1,5 +1,4 @@ . + +/** + * 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; + } +} \ No newline at end of file diff --git a/blocks/section_links/settings.php b/blocks/section_links/settings.php index 5b0d4f548bf..2fcb4a4a729 100644 --- a/blocks/section_links/settings.php +++ b/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 . - /** * 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 @@ if ($ADMIN->fulltree) { 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)); } } \ No newline at end of file diff --git a/blocks/section_links/version.php b/blocks/section_links/version.php index b62989ecd71..1bccc9042ff 100644 --- a/blocks/section_links/version.php +++ b/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 */