Merge branch 'wip-MDL-37615-m25' of git://github.com/samhemelryk/moodle

This commit is contained in:
Dan Poltawski 2013-01-29 11:47:17 +08:00
commit 5774879060
8 changed files with 350 additions and 212 deletions

View File

@ -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,51 +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');
}
}
/**
* 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{
// TODO: Move these config settings to proper ones using component name
$config = get_config('blocks/section_links');
$config = get_config('block_section_links');
}
if ($this->content !== NULL) {
if ($this->content !== null) {
return $this->content;
}
@ -72,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) {
@ -103,58 +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 = '<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 before_delete() {
global $DB;
// TODO: Move these config settings to proper ones using component name
$DB->delete_records('config_plugins', array('plugin' => 'blocks/section_links'));
// Have to manually purge the cache as well
cache_helper::invalidate_by_definition('core', 'config', array(), 'blocks/section_links');
}
function has_config() {
/**
* Returns true if this block has global config.
*
* @return bool
*/
public function has_config() {
return true;
}
}

View File

@ -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('blocks/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 File

@ -0,0 +1,82 @@
<?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/>.
/**
* This file keeps track of upgrades to the section links block
*
* Sometimes, changes between versions involve alterations to database structures
* and other major things that may break installations.
*
* The upgrade function in this file will attempt to perform all the necessary
* actions to upgrade your older installation to the current version.
*
* If there's something it cannot do itself, it will tell you what you need to do.
*
* The commands in here will all be database-neutral, using the methods of
* database_manager class
*
* Please do not forget to use upgrade_set_timeout()
* before any action that may take longer time to finish.
*
* @since 2.5
* @package block_section_links
* @copyright 2013 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Upgrade code for the section links block.
*
* @global moodle_database $DB
* @param int $oldversion
* @param object $block
*/
function xmldb_block_section_links_upgrade($oldversion, $block) {
global $DB;
// Moodle v2.3.0 release upgrade line
// Put any upgrade step following this
// Moodle v2.4.0 release upgrade line
// Put any upgrade step following this
// Moodle v2.5.0 release upgrade line
// Put any upgrade step following this
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';
// Use the proper API here... thats what we should be doing as it ensures any caches etc are cleared
// along the way!
// It may be quicker to just write an SQL statement but that would be reckless.
$config = get_config($oldplugin);
if (!empty($config)) {
foreach ($config as $name => $value) {
set_config($name, $value, $newplugin);
unset_config($name, $oldplugin);
}
}
// Main savepoint reached.
upgrade_block_savepoint(true, 2013012200.00, 'section_links');
}
return true;
}

View File

@ -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 File

@ -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 File

@ -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 File

@ -14,19 +14,17 @@
// 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
*/
defined('MOODLE_INTERNAL') || die;
if ($ADMIN->fulltree) {
$configs = array();
$numberofsections = array();
for ($i = 1; $i < 53; $i++){
@ -42,17 +40,12 @@ if ($ADMIN->fulltree) {
2 => array(40,5));
for($i = 1; $i < 3; $i++){
$configs[] = new admin_setting_configselect('numsections'.$i, get_string('numsections'.$i, 'block_section_links'),
get_string('numsectionsdesc'.$i, 'block_section_links'),
$selected[$i][0], $numberofsections);
$settings->add(new admin_setting_configselect('block_section_links/numsections'.$i, get_string('numsections'.$i, 'block_section_links'),
get_string('numsections'.$i.'_help', 'block_section_links'),
$selected[$i][0], $numberofsections));
$configs[] = new admin_setting_configselect('incby'.$i, get_string('incby'.$i, 'block_section_links'),
get_string('incbydesc'.$i, 'block_section_links'),
$selected[$i][1], $increments);
}
foreach ($configs as $config) {
$config->plugin = 'blocks/section_links';
$settings->add($config);
$settings->add(new admin_setting_configselect('block_section_links/incby'.$i, get_string('incby'.$i, 'block_section_links'),
get_string('incby'.$i.'_help', 'block_section_links'),
$selected[$i][1], $increments));
}
}

View File

@ -17,14 +17,13 @@
/**
* 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
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2012112900; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2013012200; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2012112900; // Requires this Moodle version
$plugin->component = 'block_section_links'; // Full name of the plugin (used for diagnostics)