Merge branch 'MDL-49821-master-master' of git://github.com/jleyva/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Wed, 30 Sep 2015 23:26:58 +0000 (01:26 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Wed, 30 Sep 2015 23:26:58 +0000 (01:26 +0200)
135 files changed:
admin/cli/install.php
admin/index.php
admin/renderer.php
admin/settings/courses.php
availability/tests/info_test.php
backup/upgrade.txt
backup/util/helper/backup_cron_helper.class.php
backup/util/helper/tests/cronhelper_test.php
badges/backpack_form.php
badges/backpackconnect.php
cohort/lib.php
config-dist.php
course/format/renderer.php
course/format/topics/format.js
course/format/topics/lang/en/format_topics.php
course/format/topics/renderer.php
course/format/topics/styles.css
course/format/topics/tests/behat/edit_delete_sections.feature
course/format/upgrade.txt
course/format/weeks/format.js
course/format/weeks/lang/en/format_weeks.php
course/format/weeks/styles.css
course/format/weeks/tests/behat/edit_delete_sections.feature
course/lib.php
course/tests/behat/behat_course.php
course/tests/courselib_test.php
course/yui/build/moodle-course-dragdrop/moodle-course-dragdrop-debug.js
course/yui/build/moodle-course-dragdrop/moodle-course-dragdrop-min.js
course/yui/build/moodle-course-dragdrop/moodle-course-dragdrop.js
course/yui/build/moodle-course-toolboxes/moodle-course-toolboxes-debug.js
course/yui/build/moodle-course-toolboxes/moodle-course-toolboxes-min.js
course/yui/build/moodle-course-toolboxes/moodle-course-toolboxes.js
course/yui/src/dragdrop/js/section.js
course/yui/src/toolboxes/js/section.js
enrol/cohort/edit.php
enrol/guest/lib.php
enrol/manual/edit.php
enrol/meta/classes/observer.php
enrol/meta/db/events.php
enrol/meta/tests/plugin_test.php
enrol/meta/version.php
enrol/paypal/edit.php
enrol/self/edit.php
enrol/tests/enrollib_test.php
enrol/upgrade.txt
grade/import/direct/index.php
grade/lib.php
group/externallib.php
install/stringnames.txt
lang/en/admin.php
lang/en/backup.php
lang/en/enrol.php
lang/en/message.php
lang/en/moodle.php
lang/en/plugin.php
lib/badgeslib.php
lib/classes/event/enrol_instance_created.php [new file with mode: 0644]
lib/classes/event/enrol_instance_deleted.php [new file with mode: 0644]
lib/classes/event/enrol_instance_updated.php [new file with mode: 0644]
lib/classes/event/message_deleted.php [new file with mode: 0644]
lib/db/upgrade.php
lib/dml/mssql_native_moodle_database.php
lib/dml/sqlsrv_native_moodle_database.php
lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-debug.js
lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-min.js
lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor.js
lib/editor/atto/yui/src/editor/js/commands.js
lib/editor/atto/yui/src/editor/js/styling.js
lib/enrollib.php
lib/grade/grade_category.php
lib/grade/grade_item.php
lib/installlib.php
lib/outputrenderers.php
lib/phpunit/classes/advanced_testcase.php
lib/phpunit/classes/database_driver_testcase.php
lib/phpunit/classes/util.php
lib/phpunit/tests/advanced_test.php
lib/tests/messagelib_test.php
lib/upgradelib.php
lib/yui/build/moodle-core-formchangechecker/moodle-core-formchangechecker-debug.js
lib/yui/build/moodle-core-formchangechecker/moodle-core-formchangechecker-min.js
lib/yui/build/moodle-core-formchangechecker/moodle-core-formchangechecker.js
lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-debug.js
lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-min.js
lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue.js
lib/yui/src/formchangechecker/js/formchangechecker.js
lib/yui/src/notification/js/dialogue.js
message/lib.php
message/tests/events_test.php
message/yui/build/moodle-core_message-messenger/moodle-core_message-messenger-debug.js
message/yui/build/moodle-core_message-messenger/moodle-core_message-messenger-min.js
message/yui/build/moodle-core_message-messenger/moodle-core_message-messenger.js
message/yui/src/messenger/js/sendmessage.js
mod/assign/lib.php
mod/assign/tests/lib_test.php
mod/forum/discuss.php
mod/forum/lang/en/forum.php
mod/forum/lib.php
mod/forum/renderer.php
mod/forum/styles.css
mod/forum/tests/behat/posts_ordering_blog.feature [new file with mode: 0644]
mod/forum/tests/behat/posts_ordering_general.feature [new file with mode: 0644]
mod/forum/tests/behat/timed_discussions.feature [new file with mode: 0644]
mod/forum/tests/lib_test.php
mod/forum/tests/subscriptions_test.php
mod/forum/view.php
mod/lti/launch.php
mod/lti/locallib.php
mod/lti/service/memberships/classes/local/resource/contextmemberships.php [new file with mode: 0644]
mod/lti/service/memberships/classes/local/resource/linkmemberships.php [new file with mode: 0644]
mod/lti/service/memberships/classes/local/service/memberships.php [new file with mode: 0644]
mod/lti/service/memberships/lang/en/ltiservice_memberships.php [new file with mode: 0644]
mod/lti/service/memberships/version.php [new file with mode: 0644]
mod/lti/tests/locallib_test.php
mod/lti/view.php
mod/quiz/locallib.php
notes/externallib.php
pix/i/delete.png [new file with mode: 0644]
pix/i/delete.svg [new file with mode: 0644]
question/behaviour/manualgraded/tests/walkthrough_test.php
question/engine/lib.php
question/engine/tests/questionengine_test.php
question/type/ddimageortext/tests/behat/preview.feature
question/type/ddmarker/tests/behat/behat_qtype_ddmarker.php
question/type/ddmarker/tests/behat/preview.feature
question/type/ddwtos/tests/behat/preview.feature
question/type/gapselect/tests/behat/basic_test.feature
theme/base/style/core.css
theme/base/style/course.css
theme/bootstrapbase/less/moodle/core.less
theme/bootstrapbase/less/moodle/course.less
theme/bootstrapbase/style/moodle.css
user/externallib.php
user/lib.php
version.php

index 51364b9..523f9e8 100644 (file)
@@ -74,6 +74,7 @@ Options:
 --adminpass=PASSWORD  Password for the moodle admin account,
                       required in non-interactive mode.
 --adminemail=STRING   Email address for the moodle admin account.
+--upgradekey=STRING   The upgrade key to be set in the config.php, leave empty to not set it.
 --non-interactive     No interactive questions, installation fails if any
                       problem encountered.
 --agree-license       Indicates agreement with software license,
@@ -258,6 +259,7 @@ list($options, $unrecognized) = cli_get_params(
         'adminuser'         => 'admin',
         'adminpass'         => '',
         'adminemail'        => '',
+        'upgradekey'        => '',
         'non-interactive'   => false,
         'agree-license'     => false,
         'allow-unstable'    => false,
@@ -722,6 +724,24 @@ if (!empty($options['adminemail']) && !validate_email($options['adminemail'])) {
     cli_error(get_string('cliincorrectvalueerror', 'admin', $a));
 }
 
+// Ask for the upgrade key.
+if ($interactive) {
+    cli_separator();
+    cli_heading(get_string('upgradekeyset', 'admin'));
+    if ($options['upgradekey'] !== '') {
+        $prompt = get_string('clitypevaluedefault', 'admin', $options['upgradekey']);
+        $options['upgradekey'] = cli_input($prompt, $options['upgradekey']);
+    } else {
+        $prompt = get_string('clitypevalue', 'admin');
+        $options['upgradekey'] = cli_input($prompt);
+    }
+}
+
+// Set the upgrade key if it was provided.
+if ($options['upgradekey'] !== '') {
+    $CFG->upgradekey = $options['upgradekey'];
+}
+
 if ($interactive) {
     if (!$options['agree-license']) {
         cli_separator();
index 1cea8f8..3e73aa4 100644 (file)
@@ -54,6 +54,16 @@ if (!function_exists('json_encode') || !function_exists('json_decode')) {
 
 define('NO_OUTPUT_BUFFERING', true);
 
+if (isset($_POST['upgradekey'])) {
+    // Before you start reporting issues about the collision attacks against
+    // SHA-1, you should understand that we are not actually attempting to do
+    // any cryptography here. This is hashed purely so that the key is not
+    // that apparent in the address bar itself. Anyone who catches the HTTP
+    // traffic can immediately use it as a valid admin key.
+    header('Location: index.php?cache=0&upgradekeyhash='.sha1($_POST['upgradekey']));
+    die();
+}
+
 if ((isset($_GET['cache']) and $_GET['cache'] === '0')
         or (isset($_POST['cache']) and $_POST['cache'] === '0')
         or (!isset($_POST['cache']) and !isset($_GET['cache']) and empty($_GET['sesskey']) and empty($_POST['sesskey']))) {
@@ -95,10 +105,14 @@ $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL);
 $agreelicense   = optional_param('agreelicense', 0, PARAM_BOOL);
 $fetchupdates   = optional_param('fetchupdates', 0, PARAM_BOOL);
 $newaddonreq    = optional_param('installaddonrequest', null, PARAM_RAW);
+$upgradekeyhash = optional_param('upgradekeyhash', null, PARAM_ALPHANUM);
 
 // Set up PAGE.
 $url = new moodle_url('/admin/index.php');
 $url->param('cache', $cache);
+if (isset($upgradekeyhash)) {
+    $url->param('upgradekeyhash', $upgradekeyhash);
+}
 $PAGE->set_url($url);
 unset($url);
 
@@ -203,7 +217,7 @@ if (!core_tables_exist()) {
         $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
 
         $output = $PAGE->get_renderer('core', 'admin');
-        $url = new moodle_url('/admin/index.php', array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
+        $url = new moodle_url($PAGE->url, array('agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang));
         echo $output->unsatisfied_dependencies_page($version, $failed, $url);
         die();
     }
@@ -253,11 +267,13 @@ if (empty($CFG->version)) {
 // Detect config cache inconsistency, this happens when you switch branches on dev servers.
 if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
     purge_all_caches();
-    redirect(new moodle_url('/admin/index.php'), 'Config cache inconsistency detected, resetting caches...');
+    redirect(new moodle_url($PAGE->url), 'Config cache inconsistency detected, resetting caches...');
 }
 
 if (!$cache and $version > $CFG->version) {  // upgrade
 
+    check_upgrade_key($upgradekeyhash);
+
     // Warning about upgrading a test site.
     $testsite = false;
     if (defined('BEHAT_SITE_RUNNING')) {
@@ -318,7 +334,7 @@ if (!$cache and $version > $CFG->version) {  // upgrade
         $PAGE->set_heading($strplugincheck);
         $PAGE->set_cacheable(false);
 
-        $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1, 'cache' => 0));
+        $reloadurl = new moodle_url($PAGE->url, array('confirmupgrade' => 1, 'confirmrelease' => 1, 'cache' => 0));
 
         if ($fetchupdates) {
             // No sesskey support guaranteed here, because sessions might not work yet.
@@ -342,15 +358,15 @@ if (!$cache and $version > $CFG->version) {  // upgrade
         }
 
         echo $output->upgrade_plugin_check_page(core_plugin_manager::instance(), \core\update\checker::instance(),
-                $version, $showallplugins, $reloadurl,
-                new moodle_url('/admin/index.php', array('confirmupgrade'=>1, 'confirmrelease'=>1, 'confirmplugincheck'=>1, 'cache'=>0)));
+                $version, $showallplugins, $reloadurl, new moodle_url($PAGE->url, array(
+                'confirmupgrade' => 1, 'confirmrelease' => 1, 'confirmplugincheck' => 1, 'cache' => 0)));
         die();
 
     } else {
         // Always verify plugin dependencies!
         $failed = array();
         if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
-            $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1, 'cache' => 0));
+            $reloadurl = new moodle_url($PAGE->url, array('confirmupgrade' => 1, 'confirmrelease' => 1, 'cache' => 0));
             echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
             die();
         }
@@ -374,6 +390,9 @@ if (!$cache and $branch <> $CFG->branch) {  // Update the branch
 }
 
 if (!$cache and moodle_needs_upgrading()) {
+
+    check_upgrade_key($upgradekeyhash);
+
     if (!$PAGE->headerprinted) {
         // means core upgrade or installation was not already done
 
@@ -413,7 +432,7 @@ if (!$cache and moodle_needs_upgrading()) {
             echo $output->upgrade_plugin_check_page(core_plugin_manager::instance(), \core\update\checker::instance(),
                     $version, $showallplugins,
                     new moodle_url($PAGE->url),
-                    new moodle_url('/admin/index.php', array('confirmplugincheck'=>1, 'cache'=>0)));
+                    new moodle_url($PAGE->url, array('confirmplugincheck' => 1, 'cache' => 0)));
             die();
         }
 
@@ -422,7 +441,7 @@ if (!$cache and moodle_needs_upgrading()) {
         if (!core_plugin_manager::instance()->all_plugins_ok($version, $failed)) {
             /** @var core_admin_renderer $output */
             $output = $PAGE->get_renderer('core', 'admin');
-            $reloadurl = new moodle_url('/admin/index.php', array('cache' => 0));
+            $reloadurl = new moodle_url($PAGE->url, array('cache' => 0));
             echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
             die();
         }
index 5d75e70..40036da 100644 (file)
@@ -43,7 +43,8 @@ class core_admin_renderer extends plugin_renderer_base {
         $copyrightnotice = text_to_html(get_string('gpl3'));
         $copyrightnotice = str_replace('target="_blank"', 'onclick="this.target=\'_blank\'"', $copyrightnotice); // extremely ugly validation hack
 
-        $continue = new single_button(new moodle_url('/admin/index.php', array('lang'=>$CFG->lang, 'agreelicense'=>1)), get_string('continue'), 'get');
+        $continue = new single_button(new moodle_url($this->page->url, array(
+            'lang' => $CFG->lang, 'agreelicense' => 1)), get_string('continue'), 'get');
 
         $output .= $this->header();
         $output .= $this->heading('<a href="http://moodle.org">Moodle</a> - Modular Object-Oriented Dynamic Learning Environment');
@@ -96,10 +97,11 @@ class core_admin_renderer extends plugin_renderer_base {
         $output .= $this->environment_check_table($envstatus, $environment_results);
 
         if (!$envstatus) {
-            $output .= $this->upgrade_reload(new moodle_url('/admin/index.php', array('agreelicense' => 1, 'lang' => $CFG->lang)));
+            $output .= $this->upgrade_reload(new moodle_url($this->page->url, array('agreelicense' => 1, 'lang' => $CFG->lang)));
         } else {
             $output .= $this->notification(get_string('environmentok', 'admin'), 'notifysuccess');
-            $output .= $this->continue_button(new moodle_url('/admin/index.php', array('agreelicense'=>1, 'confirmrelease'=>1, 'lang'=>$CFG->lang)));
+            $output .= $this->continue_button(new moodle_url($this->page->url, array(
+                'agreelicense' => 1, 'confirmrelease' => 1, 'lang' => $CFG->lang)));
         }
 
         $output .= $this->footer();
@@ -140,7 +142,7 @@ class core_admin_renderer extends plugin_renderer_base {
     public function upgrade_confirm_page($strnewversion, $maturity, $testsite) {
         $output = '';
 
-        $continueurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'cache' => 0));
+        $continueurl = new moodle_url($this->page->url, array('confirmupgrade' => 1, 'cache' => 0));
         $continue = new single_button($continueurl, get_string('continue'), 'get');
         $cancelurl = new moodle_url('/admin/index.php');
 
@@ -170,7 +172,7 @@ class core_admin_renderer extends plugin_renderer_base {
         $output .= $this->environment_check_table($envstatus, $environment_results);
 
         if (!$envstatus) {
-            $output .= $this->upgrade_reload(new moodle_url('/admin/index.php'), array('confirmupgrade' => 1, 'cache' => 0));
+            $output .= $this->upgrade_reload(new moodle_url($this->page->url, array('confirmupgrade' => 1, 'cache' => 0)));
 
         } else {
             $output .= $this->notification(get_string('environmentok', 'admin'), 'notifysuccess');
@@ -179,7 +181,8 @@ class core_admin_renderer extends plugin_renderer_base {
                 $output .= $this->box(get_string('langpackwillbeupdated', 'admin'), 'generalbox', 'notice');
             }
 
-            $output .= $this->continue_button(new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1, 'cache' => 0)));
+            $output .= $this->continue_button(new moodle_url($this->page->url, array(
+                'confirmupgrade' => 1, 'confirmrelease' => 1, 'cache' => 0)));
         }
 
         $output .= $this->footer();
@@ -991,7 +994,7 @@ class core_admin_renderer extends plugin_renderer_base {
             $out  = $this->output->container_start('nonehighlighted', 'plugins-check-info');
             $out .= $this->output->heading(get_string('nonehighlighted', 'core_plugin'));
             if (empty($options['full'])) {
-                $out .= html_writer::link(new moodle_url('/admin/index.php',
+                $out .= html_writer::link(new moodle_url($this->page->url,
                     array('confirmupgrade' => 1, 'confirmrelease' => 1, 'showallplugins' => 1, 'cache' => 0)),
                     get_string('nonehighlightedinfo', 'core_plugin'));
             }
@@ -999,13 +1002,14 @@ class core_admin_renderer extends plugin_renderer_base {
 
         } else {
             $out  = $this->output->container_start('somehighlighted', 'plugins-check-info');
-            $out .= $this->output->heading(get_string('somehighlighted', 'core_plugin', $sumofhighlighted));
             if (empty($options['full'])) {
-                $out .= html_writer::link(new moodle_url('/admin/index.php',
+                $out .= $this->output->heading(get_string('somehighlighted', 'core_plugin', $sumofhighlighted));
+                $out .= html_writer::link(new moodle_url($this->page->url,
                     array('confirmupgrade' => 1, 'confirmrelease' => 1, 'showallplugins' => 1, 'cache' => 0)),
                     get_string('somehighlightedinfo', 'core_plugin'));
             } else {
-                $out .= html_writer::link(new moodle_url('/admin/index.php',
+                $out .= $this->output->heading(get_string('somehighlightedall', 'core_plugin', $sumofhighlighted));
+                $out .= html_writer::link(new moodle_url($this->page->url,
                     array('confirmupgrade' => 1, 'confirmrelease' => 1, 'showallplugins' => 0, 'cache' => 0)),
                     get_string('somehighlightedonly', 'core_plugin'));
             }
@@ -1571,4 +1575,26 @@ class core_admin_renderer extends plugin_renderer_base {
 
         return $output;
     }
+
+    /**
+     * Render a simple page for providing the upgrade key.
+     *
+     * @param moodle_url|string $url
+     * @return string
+     */
+    public function upgradekey_form_page($url) {
+
+        $output = '';
+        $output .= $this->header();
+        $output .= $this->container_start('upgradekeyreq');
+        $output .= $this->heading(get_string('upgradekeyreq', 'core_admin'));
+        $output .= html_writer::start_tag('form', array('method' => 'POST', 'action' => $url));
+        $output .= html_writer::empty_tag('input', array('name' => 'upgradekey', 'type' => 'password'));
+        $output .= html_writer::empty_tag('input', array('value' => get_string('submit'), 'type' => 'submit'));
+        $output .= html_writer::end_tag('form');
+        $output .= $this->container_end();
+        $output .= $this->footer();
+
+        return $output;
+    }
 }
index 1442ab8..bd88630 100644 (file)
@@ -226,7 +226,8 @@ if ($hassiteconfig or has_any_capability($capabilities, $systemcontext)) {
     );
     $temp->add(new admin_setting_configselect('backup/backup_auto_storage', new lang_string('automatedstorage', 'backup'), new lang_string('automatedstoragehelp', 'backup'), 0, $storageoptions));
     $temp->add(new admin_setting_special_backup_auto_destination());
-    $keepoptoins = array(
+
+    $maxkeptoptions = array(
         0 => new lang_string('all'), 1 => '1',
         2 => '2',
         5 => '5',
@@ -240,7 +241,44 @@ if ($hassiteconfig or has_any_capability($capabilities, $systemcontext)) {
         300 => '300',
         400 => '400',
         500 => '500');
-    $temp->add(new admin_setting_configselect('backup/backup_auto_keep', new lang_string('keep'), new lang_string('backupkeephelp'), 1, $keepoptoins));
+    $temp->add(new admin_setting_configselect('backup/backup_auto_max_kept', new lang_string('automatedmaxkept', 'backup'),
+            new lang_string('automatedmaxkepthelp', 'backup'), 1, $maxkeptoptions));
+
+    $automateddeletedaysoptions = array(
+        0 => new lang_string('never'),
+        1000 => new lang_string('numdays', '', 1000),
+        365  => new lang_string('numdays', '', 365),
+        180  => new lang_string('numdays', '', 180),
+        150  => new lang_string('numdays', '', 150),
+        120  => new lang_string('numdays', '', 120),
+        90   => new lang_string('numdays', '', 90),
+        60   => new lang_string('numdays', '', 60),
+        35   => new lang_string('numdays', '', 35),
+        10   => new lang_string('numdays', '', 10),
+        5    => new lang_string('numdays', '', 5),
+        2    => new lang_string('numdays', '', 2)
+    );
+    $temp->add(new admin_setting_configselect('backup/backup_auto_delete_days', new lang_string('automateddeletedays', 'backup'),
+            '', 0, $automateddeletedaysoptions));
+
+    $minkeptoptions = array(
+        0 => new lang_string('none'),
+        1 => '1',
+        2 => '2',
+        5 => '5',
+        10 => '10',
+        20 => '20',
+        30 => '30',
+        40 => '40',
+        50 => '50',
+        100 => '100',
+        200 => '200',
+        300 => '300',
+        400 => '400'
+    );
+    $temp->add(new admin_setting_configselect('backup/backup_auto_min_kept', new lang_string('automatedminkept', 'backup'),
+            new lang_string('automatedminkepthelp', 'backup'), 0, $minkeptoptions));
+
     $temp->add(new admin_setting_configcheckbox('backup/backup_shortname', new lang_string('backup_shortname', 'admin'), new lang_string('backup_shortnamehelp', 'admin'), 0));
     $temp->add(new admin_setting_configcheckbox('backup/backup_auto_skip_hidden', new lang_string('skiphidden', 'backup'), new lang_string('skiphiddenhelp', 'backup'), 1));
     $temp->add(new admin_setting_configselect('backup/backup_auto_skip_modif_days', new lang_string('skipmodifdays', 'backup'), new lang_string('skipmodifdayshelp', 'backup'), 30, array(
index 13091ec..1477978 100644 (file)
@@ -35,7 +35,7 @@ use core_availability\info_section;
  * @copyright 2014 The Open University
  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
-class info_testcase extends \advanced_testcase {
+class info_testcase extends advanced_testcase {
     public function setUp() {
         // Load the mock condition so that it can be used.
         require_once(__DIR__ . '/fixtures/mock_condition.php');
@@ -87,8 +87,8 @@ class info_testcase extends \advanced_testcase {
         // Check invalid one.
         $info = new info_module($cm3);
         $this->assertFalse($info->is_available($information));
-        $debugging = phpunit_util::get_debugging_messages();
-        phpunit_util::reset_debugging();
+        $debugging = $this->getDebuggingMessages();
+        $this->resetDebugging();
         $this->assertEquals(1, count($debugging));
         $this->assertContains('Invalid availability', $debugging[0]->message);
 
@@ -141,8 +141,8 @@ class info_testcase extends \advanced_testcase {
         // Check invalid one.
         $info = new info_section($sections[3]);
         $this->assertFalse($info->is_available($information));
-        $debugging = phpunit_util::get_debugging_messages();
-        phpunit_util::reset_debugging();
+        $debugging = $this->getDebuggingMessages();
+        $this->resetDebugging();
         $this->assertEquals(1, count($debugging));
         $this->assertContains('Invalid availability', $debugging[0]->message);
 
index 26e2e00..31e7f03 100644 (file)
@@ -1,6 +1,11 @@
 This files describes API changes in /backup/*,
 information provided here is intended especially for developers.
 
+=== 3.0 ===
+
+* The backup_auto_keep setting, in automated backups configuration, is now
+  renamed to backup_auto_max_kept as part of a rationalise of naming (see MDL-50602)
+
 === 2.6 ===
 
 * The backup_controller_dbops::create_temptable_from_real_table()
index 1188bf7..8b6eeb8 100644 (file)
@@ -61,6 +61,13 @@ abstract class backup_cron_automated_helper {
     const AUTO_BACKUP_ENABLED = 1;
     const AUTO_BACKUP_MANUAL = 2;
 
+    /** Automated backup storage in course backup filearea */
+    const STORAGE_COURSE = 0;
+    /** Automated backup storage in specified directory */
+    const STORAGE_DIRECTORY = 1;
+    /** Automated backup storage in course backup filearea and specified directory */
+    const STORAGE_COURSE_AND_DIRECTORY = 2;
+
     /**
      * Runs the automated backups if required
      *
@@ -174,42 +181,42 @@ abstract class backup_cron_automated_helper {
                     $backupcourse->nextstarttime = $nextstarttime;
                     $DB->update_record('backup_courses', $backupcourse);
                     mtrace('Skipping ' . $course->fullname . ' (Not scheduled for backup until ' . $showtime . ')');
-                } else if ($skipped) { // Must have been skipped for a reason.
-                    $backupcourse->laststatus = self::BACKUP_STATUS_SKIPPED;
-                    $backupcourse->nextstarttime = $nextstarttime;
-                    $DB->update_record('backup_courses', $backupcourse);
-                    mtrace('Skipping ' . $course->fullname . ' (' . $skippedmessage . ')');
-                    mtrace('Backup of \'' . $course->fullname . '\' is scheduled on ' . $showtime);
                 } else {
-                    // Backup every non-skipped courses.
-                    mtrace('Backing up '.$course->fullname.'...');
+                    if ($skipped) { // Must have been skipped for a reason.
+                        $backupcourse->laststatus = self::BACKUP_STATUS_SKIPPED;
+                        $backupcourse->nextstarttime = $nextstarttime;
+                        $DB->update_record('backup_courses', $backupcourse);
+                        mtrace('Skipping ' . $course->fullname . ' (' . $skippedmessage . ')');
+                        mtrace('Backup of \'' . $course->fullname . '\' is scheduled on ' . $showtime);
+                    } else {
+                        // Backup every non-skipped courses.
+                        mtrace('Backing up '.$course->fullname.'...');
 
-                    // We have to send an email because we have included at least one backup.
-                    $emailpending = true;
+                        // We have to send an email because we have included at least one backup.
+                        $emailpending = true;
 
-                    // Only make the backup if laststatus isn't 2-UNFINISHED (uncontrolled error).
-                    if ($backupcourse->laststatus != self::BACKUP_STATUS_UNFINISHED) {
-                        // Set laststarttime.
-                        $starttime = time();
+                        // Only make the backup if laststatus isn't 2-UNFINISHED (uncontrolled error).
+                        if ($backupcourse->laststatus != self::BACKUP_STATUS_UNFINISHED) {
+                            // Set laststarttime.
+                            $starttime = time();
 
-                        $backupcourse->laststarttime = time();
-                        $backupcourse->laststatus = self::BACKUP_STATUS_UNFINISHED;
-                        $DB->update_record('backup_courses', $backupcourse);
+                            $backupcourse->laststarttime = time();
+                            $backupcourse->laststatus = self::BACKUP_STATUS_UNFINISHED;
+                            $DB->update_record('backup_courses', $backupcourse);
 
-                        $backupcourse->laststatus = backup_cron_automated_helper::launch_automated_backup($course, $backupcourse->laststarttime, $admin->id);
-                        $backupcourse->lastendtime = time();
-                        $backupcourse->nextstarttime = $nextstarttime;
+                            $backupcourse->laststatus = self::launch_automated_backup($course, $backupcourse->laststarttime,
+                                    $admin->id);
+                            $backupcourse->lastendtime = time();
+                            $backupcourse->nextstarttime = $nextstarttime;
 
-                        $DB->update_record('backup_courses', $backupcourse);
+                            $DB->update_record('backup_courses', $backupcourse);
 
-                        if ($backupcourse->laststatus === self::BACKUP_STATUS_OK) {
-                            // Clean up any excess course backups now that we have
-                            // taken a successful backup.
-                            $removedcount = backup_cron_automated_helper::remove_excess_backups($course);
+                            mtrace("complete - next execution: $showtime");
                         }
                     }
 
-                    mtrace("complete - next execution: $showtime");
+                    // Remove excess backups.
+                    $removedcount = self::remove_excess_backups($course, $now);
                 }
             }
             $rs->close();
@@ -537,98 +544,177 @@ abstract class backup_cron_automated_helper {
     }
 
     /**
-     * Removes excess backups from the external system and the local file system.
+     * Removes excess backups from a specified course.
      *
-     * The number of backups keep comes from $config->backup_auto_keep.
-     *
-     * @param stdClass $course object
-     * @return bool
+     * @param stdClass $course Course object
+     * @param int $now Starting time of the process
+     * @return bool Whether or not backups is being removed
      */
-    public static function remove_excess_backups($course) {
+    public static function remove_excess_backups($course, $now = null) {
         $config = get_config('backup');
-        $keep =     (int)$config->backup_auto_keep;
-        $storage =  $config->backup_auto_storage;
-        $dir =      $config->backup_auto_destination;
+        $maxkept = (int)$config->backup_auto_max_kept;
+        $storage = $config->backup_auto_storage;
+        $deletedays = (int)$config->backup_auto_delete_days;
 
-        if ($keep == 0) {
-            // Means keep all backup files.
+        if ($maxkept == 0 && $deletedays == 0) {
+            // Means keep all backup files and never delete backup after x days.
             return true;
         }
 
-        if (!file_exists($dir) || !is_dir($dir) || !is_writable($dir)) {
-            $dir = null;
+        if (!isset($now)) {
+            $now = time();
         }
 
         // Clean up excess backups in the course backup filearea.
-        if ($storage == 0 || $storage == 2) {
-            $fs = get_file_storage();
-            $context = context_course::instance($course->id);
-            $component = 'backup';
-            $filearea = 'automated';
-            $itemid = 0;
-            $files = array();
-            // Store all the matching files into timemodified => stored_file array.
-            foreach ($fs->get_area_files($context->id, $component, $filearea, $itemid) as $file) {
-                $files[$file->get_timemodified()] = $file;
+        $deletedcoursebackups = false;
+        if ($storage == self::STORAGE_COURSE || $storage == self::STORAGE_COURSE_AND_DIRECTORY) {
+            $deletedcoursebackups = self::remove_excess_backups_from_course($course, $now);
+        }
+
+        // Clean up excess backups in the specified external directory.
+        $deleteddirectorybackups = false;
+        if ($storage == self::STORAGE_DIRECTORY || $storage == self::STORAGE_COURSE_AND_DIRECTORY) {
+            $deleteddirectorybackups = self::remove_excess_backups_from_directory($course, $now);
+        }
+
+        if ($deletedcoursebackups || $deleteddirectorybackups) {
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    /**
+     * Removes excess backups in the course backup filearea from a specified course.
+     *
+     * @param stdClass $course Course object
+     * @param int $now Starting time of the process
+     * @return bool Whether or not backups are being removed
+     */
+    protected static function remove_excess_backups_from_course($course, $now) {
+        $fs = get_file_storage();
+        $context = context_course::instance($course->id);
+        $component = 'backup';
+        $filearea = 'automated';
+        $itemid = 0;
+        $backupfiles = array();
+        $backupfilesarea = $fs->get_area_files($context->id, $component, $filearea, $itemid, 'timemodified DESC', false);
+        // Store all the matching files into timemodified => stored_file array.
+        foreach ($backupfilesarea as $backupfile) {
+            $backupfiles[$backupfile->get_timemodified()] = $backupfile;
+        }
+
+        $backupstodelete = self::get_backups_to_delete($backupfiles, $now);
+        if ($backupstodelete) {
+            foreach ($backupstodelete as $backuptodelete) {
+                $backuptodelete->delete();
             }
-            if (count($files) <= $keep) {
-                // There are less matching files than the desired number to keep there is nothing to clean up.
-                return 0;
+            mtrace('Deleted ' . count($backupstodelete) . ' old backup file(s) from the automated filearea');
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    /**
+     * Removes excess backups in the specified external directory from a specified course.
+     *
+     * @param stdClass $course Course object
+     * @param int $now Starting time of the process
+     * @return bool Whether or not backups are being removed
+     */
+    protected static function remove_excess_backups_from_directory($course, $now) {
+        $config = get_config('backup');
+        $dir = $config->backup_auto_destination;
+
+        $isnotvaliddir = !file_exists($dir) || !is_dir($dir) || !is_writable($dir);
+        if ($isnotvaliddir) {
+            mtrace('Error: ' . $dir . ' does not appear to be a valid directory');
+            return false;
+        }
+
+        // Calculate backup filename regex, ignoring the date/time/info parts that can be
+        // variable, depending of languages, formats and automated backup settings.
+        $filename = backup::FORMAT_MOODLE . '-' . backup::TYPE_1COURSE . '-' . $course->id . '-';
+        $regex = '#' . preg_quote($filename, '#') . '.*\.mbz$#';
+
+        // Store all the matching files into filename => timemodified array.
+        $backupfiles = array();
+        foreach (scandir($dir) as $backupfile) {
+            // Skip files not matching the naming convention.
+            if (!preg_match($regex, $backupfile)) {
+                continue;
             }
-            // Sort by keys descending (newer to older filemodified).
-            krsort($files);
-            $remove = array_splice($files, $keep);
-            foreach ($remove as $file) {
-                $file->delete();
+
+            // Read the information contained in the backup itself.
+            try {
+                $bcinfo = backup_general_helper::get_backup_information_from_mbz($dir . '/' . $backupfile);
+            } catch (backup_helper_exception $e) {
+                mtrace('Error: ' . $backupfile . ' does not appear to be a valid backup (' . $e->errorcode . ')');
+                continue;
             }
-            //mtrace('Removed '.count($remove).' old backup file(s) from the automated filearea');
-        }
 
-        // Clean up excess backups in the specified external directory.
-        if (!empty($dir) && ($storage == 1 || $storage == 2)) {
-            // Calculate backup filename regex, ignoring the date/time/info parts that can be
-            // variable, depending of languages, formats and automated backup settings.
-            $filename = backup::FORMAT_MOODLE . '-' . backup::TYPE_1COURSE . '-' . $course->id . '-';
-            $regex = '#' . preg_quote($filename, '#') . '.*\.mbz$#';
-
-            // Store all the matching files into filename => timemodified array.
-            $files = array();
-            foreach (scandir($dir) as $file) {
-                // Skip files not matching the naming convention.
-                if (!preg_match($regex, $file, $matches)) {
-                    continue;
-                }
+            // Make sure this backup concerns the course and site we are looking for.
+            if ($bcinfo->format === backup::FORMAT_MOODLE &&
+                    $bcinfo->type === backup::TYPE_1COURSE &&
+                    $bcinfo->original_course_id == $course->id &&
+                    backup_general_helper::backup_is_samesite($bcinfo)) {
+                $backupfiles[$bcinfo->backup_date] = $backupfile;
+            }
+        }
 
-                // Read the information contained in the backup itself.
-                try {
-                    $bcinfo = backup_general_helper::get_backup_information_from_mbz($dir . '/' . $file);
-                } catch (backup_helper_exception $e) {
-                    mtrace('Error: ' . $file . ' does not appear to be a valid backup (' . $e->errorcode . ')');
-                    continue;
-                }
+        $backupstodelete = self::get_backups_to_delete($backupfiles, $now);
+        if ($backupstodelete) {
+            foreach ($backupstodelete as $backuptodelete) {
+                unlink($dir . '/' . $backuptodelete);
+            }
+            mtrace('Deleted ' . count($backupstodelete) . ' old backup file(s) from external directory');
+            return true;
+        } else {
+            return false;
+        }
+    }
 
-                // Make sure this backup concerns the course and site we are looking for.
-                if ($bcinfo->format === backup::FORMAT_MOODLE &&
-                        $bcinfo->type === backup::TYPE_1COURSE &&
-                        $bcinfo->original_course_id == $course->id &&
-                        backup_general_helper::backup_is_samesite($bcinfo)) {
-                    $files[$file] = $bcinfo->backup_date;
+    /**
+     * Get the list of backup files to delete depending on the automated backup settings.
+     *
+     * @param array $backupfiles Existing backup files
+     * @param int $now Starting time of the process
+     * @return array Backup files to delete
+     */
+    protected static function get_backups_to_delete($backupfiles, $now) {
+        $config = get_config('backup');
+        $maxkept = (int)$config->backup_auto_max_kept;
+        $deletedays = (int)$config->backup_auto_delete_days;
+        $minkept = (int)$config->backup_auto_min_kept;
+
+        // Sort by keys descending (newer to older filemodified).
+        krsort($backupfiles);
+        $tokeep = $maxkept;
+        if ($deletedays > 0) {
+            $deletedayssecs = $deletedays * DAYSECS;
+            $tokeep = 0;
+            $backupfileskeys = array_keys($backupfiles);
+            foreach ($backupfileskeys as $timemodified) {
+                $mustdeletebackup = $timemodified < ($now - $deletedayssecs);
+                if ($mustdeletebackup || $tokeep >= $maxkept) {
+                    break;
                 }
+                $tokeep++;
             }
-            if (count($files) <= $keep) {
-                // There are less matching files than the desired number to keep there is nothing to clean up.
-                return 0;
-            }
-            // Sort by values descending (newer to older filemodified).
-            arsort($files);
-            $remove = array_splice($files, $keep);
-            foreach (array_keys($remove) as $file) {
-                unlink($dir . '/' . $file);
+
+            if ($tokeep < $minkept) {
+                $tokeep = $minkept;
             }
-            //mtrace('Removed '.count($remove).' old backup file(s) from external directory');
         }
 
-        return true;
+        if (count($backupfiles) <= $tokeep) {
+            // There are less or equal matching files than the desired number to keep, there is nothing to clean up.
+            return false;
+        } else {
+            $backupstodelete = array_splice($backupfiles, $tokeep);
+            return $backupstodelete;
+        }
     }
 
     /**
index cb88d53..320daac 100644 (file)
@@ -244,4 +244,100 @@ class backup_cron_helper_testcase extends advanced_testcase {
         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
         $this->assertEquals(date('w-20:00'), date('w-H:i', $next));
     }
+
+    /**
+     * Test {@link backup_cron_automated_helper::get_backups_to_delete}.
+     */
+    public function test_get_backups_to_delete() {
+        $this->resetAfterTest();
+        // Active only backup_auto_max_kept config to 2 days.
+        set_config('backup_auto_max_kept', '2', 'backup');
+        set_config('backup_auto_delete_days', '0', 'backup');
+        set_config('backup_auto_min_kept', '0', 'backup');
+
+        // No backups to delete.
+        $backupfiles = array(
+            '1000000000' => 'file1.mbz',
+            '1000432000' => 'file3.mbz'
+        );
+        $deletedbackups = testable_backup_cron_automated_helper::testable_get_backups_to_delete($backupfiles, 1000432000);
+        $this->assertFalse($deletedbackups);
+
+        // Older backup to delete.
+        $backupfiles['1000172800'] = 'file2.mbz';
+        $deletedbackups = testable_backup_cron_automated_helper::testable_get_backups_to_delete($backupfiles, 1000432000);
+        $this->assertEquals(1, count($deletedbackups));
+        $this->assertArrayHasKey('1000000000', $backupfiles);
+        $this->assertEquals('file1.mbz', $backupfiles['1000000000']);
+
+        // Activate backup_auto_max_kept to 5 days and backup_auto_delete_days to 10 days.
+        set_config('backup_auto_max_kept', '5', 'backup');
+        set_config('backup_auto_delete_days', '10', 'backup');
+        set_config('backup_auto_min_kept', '0', 'backup');
+
+        // No backups to delete. Timestamp is 1000000000 + 10 days.
+        $backupfiles['1000432001'] = 'file4.mbz';
+        $backupfiles['1000864000'] = 'file5.mbz';
+        $deletedbackups = testable_backup_cron_automated_helper::testable_get_backups_to_delete($backupfiles, 1000864000);
+        $this->assertFalse($deletedbackups);
+
+        // One old backup to delete. Timestamp is 1000000000 + 10 days + 1 second.
+        $deletedbackups = testable_backup_cron_automated_helper::testable_get_backups_to_delete($backupfiles, 1000864001);
+        $this->assertEquals(1, count($deletedbackups));
+        $this->assertArrayHasKey('1000000000', $backupfiles);
+        $this->assertEquals('file1.mbz', $backupfiles['1000000000']);
+
+        // Two old backups to delete. Timestamp is 1000000000 + 12 days + 1 second.
+        $deletedbackups = testable_backup_cron_automated_helper::testable_get_backups_to_delete($backupfiles, 1001036801);
+        $this->assertEquals(2, count($deletedbackups));
+        $this->assertArrayHasKey('1000000000', $backupfiles);
+        $this->assertEquals('file1.mbz', $backupfiles['1000000000']);
+        $this->assertArrayHasKey('1000172800', $backupfiles);
+        $this->assertEquals('file2.mbz', $backupfiles['1000172800']);
+
+        // Activate backup_auto_max_kept to 5 days, backup_auto_delete_days to 10 days and backup_auto_min_kept to 2.
+        set_config('backup_auto_max_kept', '5', 'backup');
+        set_config('backup_auto_delete_days', '10', 'backup');
+        set_config('backup_auto_min_kept', '2', 'backup');
+
+        // Three instead of four old backups are deleted. Timestamp is 1000000000 + 16 days.
+        $deletedbackups = testable_backup_cron_automated_helper::testable_get_backups_to_delete($backupfiles, 1001382400);
+        $this->assertEquals(3, count($deletedbackups));
+        $this->assertArrayHasKey('1000000000', $backupfiles);
+        $this->assertEquals('file1.mbz', $backupfiles['1000000000']);
+        $this->assertArrayHasKey('1000172800', $backupfiles);
+        $this->assertEquals('file2.mbz', $backupfiles['1000172800']);
+        $this->assertArrayHasKey('1000432000', $backupfiles);
+        $this->assertEquals('file3.mbz', $backupfiles['1000432000']);
+
+        // Three instead of all five backups are deleted. Timestamp is 1000000000 + 60 days.
+        $deletedbackups = testable_backup_cron_automated_helper::testable_get_backups_to_delete($backupfiles, 1005184000);
+        $this->assertEquals(3, count($deletedbackups));
+        $this->assertArrayHasKey('1000000000', $backupfiles);
+        $this->assertEquals('file1.mbz', $backupfiles['1000000000']);
+        $this->assertArrayHasKey('1000172800', $backupfiles);
+        $this->assertEquals('file2.mbz', $backupfiles['1000172800']);
+        $this->assertArrayHasKey('1000432000', $backupfiles);
+        $this->assertEquals('file3.mbz', $backupfiles['1000432000']);
+    }
+}
+
+/**
+ * Provides access to protected methods we want to explicitly test
+ *
+ * @copyright 2015 Jean-Philippe Gaudreau <jp.gaudreau@umontreal.ca>
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class testable_backup_cron_automated_helper extends backup_cron_automated_helper {
+
+    /**
+     * Provides access to protected method get_backups_to_remove.
+     *
+     * @param array $backupfiles Existing backup files
+     * @param int $now Starting time of the process
+     * @return array Backup files to remove
+     */
+    public static function testable_get_backups_to_delete($backupfiles, $now) {
+        return parent::get_backups_to_delete($backupfiles, $now);
+    }
 }
index 1941738..ad9d427 100644 (file)
@@ -45,7 +45,7 @@ class edit_backpack_form extends moodleform {
         $mform->addElement('html', html_writer::tag('span', '', array('class' => 'notconnected', 'id' => 'connection-error')));
         $mform->addElement('header', 'backpackheader', get_string('backpackconnection', 'badges'));
         $mform->addHelpButton('backpackheader', 'backpackconnection', 'badges');
-        $mform->addElement('static', 'url', get_string('url'), 'http://' . BADGE_BACKPACKURL);
+        $mform->addElement('static', 'url', get_string('url'), BADGE_BACKPACKURL);
         $status = html_writer::tag('span', get_string('notconnected', 'badges'),
             array('class' => 'notconnected', 'id' => 'connection-status'));
         $mform->addElement('static', 'status', get_string('status'), $status);
@@ -67,7 +67,7 @@ class edit_backpack_form extends moodleform {
         $mform->addElement('hidden', 'userid', $USER->id);
         $mform->setType('userid', PARAM_INT);
 
-        $mform->addElement('hidden', 'backpackurl', 'http://' . BADGE_BACKPACKURL);
+        $mform->addElement('hidden', 'backpackurl', BADGE_BACKPACKURL);
         $mform->setType('backpackurl', PARAM_URL);
 
     }
@@ -118,7 +118,7 @@ class edit_collections_form extends moodleform {
 
         $mform->addElement('header', 'backpackheader', get_string('backpackconnection', 'badges'));
         $mform->addHelpButton('backpackheader', 'backpackconnection', 'badges');
-        $mform->addElement('static', 'url', get_string('url'), 'http://' . BADGE_BACKPACKURL);
+        $mform->addElement('static', 'url', get_string('url'), BADGE_BACKPACKURL);
 
         $status = html_writer::tag('span', get_string('connected', 'badges'), array('class' => 'connected'));
         $mform->addElement('static', 'status', get_string('status'), $status);
index 382749a..9365afe 100644 (file)
@@ -87,7 +87,7 @@ if (!isset($data->status) || $data->status != 'okay') {
 
 // Make sure email matches a backpack.
 $check = new stdClass();
-$check->backpackurl = 'http://' . BADGE_BACKPACKURL;
+$check->backpackurl = BADGE_BACKPACKURL;
 $check->email = $data->email;
 
 $bp = new OpenBadgesBackpackHandler($check);
@@ -106,7 +106,7 @@ if (isset($request->status) && $request->status == 'missing') {
 $obj = new stdClass();
 $obj->userid = $USER->id;
 $obj->email = $data->email;
-$obj->backpackurl = 'http://' . BADGE_BACKPACKURL;
+$obj->backpackurl = BADGE_BACKPACKURL;
 $obj->backpackuid = $backpackuid;
 $obj->autosync = 0;
 $obj->password = '';
index e859cac..5c51578 100644 (file)
@@ -256,15 +256,17 @@ function cohort_get_available_cohorts($currentcontext, $withmembers = 0, $offset
     $groupbysql = '';
     $havingsql = '';
     if ($withmembers) {
-        $groupbysql = " GROUP BY $fieldssql";
+        $fieldssql .= ', s.memberscnt';
+        $subfields = "c.id, COUNT(DISTINCT cm.userid) AS memberscnt";
+        $groupbysql = " GROUP BY c.id";
         $fromsql = " LEFT JOIN {cohort_members} cm ON cm.cohortid = c.id ";
-        $fieldssql .= ', COUNT(DISTINCT cm.userid) AS memberscnt';
         if (in_array($withmembers,
                 array(COHORT_COUNT_ENROLLED_MEMBERS, COHORT_WITH_ENROLLED_MEMBERS_ONLY, COHORT_WITH_NOTENROLLED_MEMBERS_ONLY))) {
             list($esql, $params2) = get_enrolled_sql($currentcontext);
             $fromsql .= " LEFT JOIN ($esql) u ON u.id = cm.userid ";
             $params = array_merge($params2, $params);
-            $fieldssql .= ', COUNT(DISTINCT u.id) AS enrolledcnt';
+            $fieldssql .= ', s.enrolledcnt';
+            $subfields .= ', COUNT(DISTINCT u.id) AS enrolledcnt';
         }
         if ($withmembers == COHORT_WITH_MEMBERS_ONLY) {
             $havingsql = " HAVING COUNT(DISTINCT cm.userid) > 0";
@@ -280,13 +282,20 @@ function cohort_get_available_cohorts($currentcontext, $withmembers = 0, $offset
         $params = array_merge($params, $searchparams);
     }
 
-    $sql = "SELECT $fieldssql
-              FROM {cohort} c
-              $fromsql
-             WHERE $wheresql
-             $groupbysql
-             $havingsql
-          ORDER BY c.name, c.idnumber";
+    if ($withmembers) {
+        $sql = "SELECT " . str_replace('c.', 'cohort.', $fieldssql) . "
+                  FROM {cohort} cohort
+                  JOIN (SELECT $subfields
+                          FROM {cohort} c $fromsql
+                         WHERE $wheresql $groupbysql $havingsql
+                        ) s ON cohort.id = s.id
+              ORDER BY cohort.name, cohort.idnumber";
+    } else {
+        $sql = "SELECT $fieldssql
+                  FROM {cohort} c $fromsql
+                 WHERE $wheresql
+              ORDER BY c.name, c.idnumber";
+    }
 
     return $DB->get_records_sql($sql, $params, $offset, $limit);
 }
index df033d9..a9b59fc 100644 (file)
@@ -530,6 +530,19 @@ $CFG->admin = 'admin';
 // any icon inside the pix/f folder. You can also set the customdescription field
 // (shown above) and (for advanced use) the groups, string, and defaulticon fields.
 //
+// Upgrade key
+//
+// If the upgrade key is defined here, then the value must be provided every time
+// the site is being upgraded though the web interface, regardless of whether the
+// administrator is logged in or not. This prevents anonymous access to the upgrade
+// screens where the real authentication and authorization mechanisms can not be
+// relied on.
+//
+// It is strongly recommended to use a value different from your real account
+// password.
+//
+//      $CFG->upgradekey = 'put_some_password-like_value_here';
+//
 //=========================================================================
 // 7. SETTINGS FOR DEVELOPMENT SERVERS - not intended for production use!!!
 //=========================================================================
index af8533c..cb4c081 100644 (file)
@@ -86,6 +86,46 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
         return $title;
     }
 
+    /**
+     * Generate the edit control action menu
+     *
+     * @param array $controls The edit control items from section_edit_control_items
+     * @param stdClass $course The course entry from DB
+     * @param stdClass $section The course_section entry from DB
+     * @return string HTML to output.
+     */
+    protected function section_edit_control_menu($controls, $course, $section) {
+        $o = "";
+        if (!empty($controls)) {
+            $menu = new action_menu();
+            if ($section->section && get_string_manager()->string_exists('sectionmenu', 'format_'.$course->format)) {
+                $menu->set_menu_trigger(get_string('sectionmenu', 'format_'.$course->format));
+            } else {
+                $menu->set_menu_trigger(get_string('sectionmenu'));
+            }
+            $menu->attributes['class'] .= ' section-actions';
+            foreach ($controls as $value) {
+                $url = empty($value['url']) ? '' : $value['url'];
+                $icon = empty($value['icon']) ? '' : $value['icon'];
+                $name = empty($value['name']) ? '' : $value['name'];
+                $attr = empty($value['attr']) ? '' : $value['attr'];
+                $class = empty($item['pixattr']['class']) ? '' : $item['pixattr']['class'];
+                $alt = empty($item['pixattr']['alt']) ? '' : $item['pixattr']['alt'];
+                $al = new action_menu_link_secondary(
+                    new moodle_url($url),
+                    new pix_icon($icon, $name, null, array('class' => "smallicon " . $class, 'alt' => $alt)),
+                    $name,
+                    $attr
+                );
+                $menu->add($al);
+            }
+
+            $o .= html_writer::div($this->render($menu), 'section_action_menu');
+        }
+
+        return $o;
+    }
+
     /**
      * Generate the content to displayed on the right part of a section
      * before course modules are included
@@ -98,12 +138,8 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
     protected function section_right_content($section, $course, $onsectionpage) {
         $o = $this->output->spacer();
 
-        if ($section->section != 0) {
-            $controls = $this->section_edit_controls($course, $section, $onsectionpage);
-            if (!empty($controls)) {
-                $o = implode('<br />', $controls);
-            }
-        }
+        $controls = $this->section_edit_control_items($course, $section, $onsectionpage);
+        $o .= $this->section_edit_control_menu($controls, $course, $section);
 
         return $o;
     }
@@ -160,6 +196,9 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
             'class' => 'section main clearfix'.$sectionstyle, 'role'=>'region',
             'aria-label'=> get_section_name($course, $section)));
 
+        // Create a span that contains the section title to be used to create the keyboard section move menu.
+        $o .= html_writer::tag('span', $this->section_title($section, $course), array('class' => 'hidden sectionname'));
+
         $leftcontent = $this->section_left_content($section, $course, $onsectionpage);
         $o.= html_writer::tag('div', $leftcontent, array('class' => 'left side'));
 
@@ -181,17 +220,9 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
 
         $o.= html_writer::start_tag('div', array('class' => 'summary'));
         $o.= $this->format_summary_text($section);
-
-        $context = context_course::instance($course->id);
-        if ($PAGE->user_is_editing() && has_capability('moodle/course:update', $context)) {
-            $url = new moodle_url('/course/editsection.php', array('id'=>$section->id, 'sr'=>$sectionreturn));
-            $o.= html_writer::link($url,
-                html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/settings'),
-                    'class' => 'iconsmall edit', 'alt' => get_string('edit'))),
-                array('title' => get_string('editsummary')));
-        }
         $o.= html_writer::end_tag('div');
 
+        $context = context_course::instance($course->id);
         $o .= $this->section_availability_message($section,
                 has_capability('moodle/course:viewhiddensections', $context));
 
@@ -217,6 +248,8 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
      * @param stdClass $section The course_section entry from DB
      * @param bool $onsectionpage true if being printed on a section page
      * @return array of links with edit controls
+     * @deprecated since Moodle 3.0 MDL-48947 - please do not use this function any more.
+     * @see format_section_renderer_base::section_edit_control_items()
      */
     protected function section_edit_controls($course, $section, $onsectionpage = false) {
         global $PAGE;
@@ -225,6 +258,45 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
             return array();
         }
 
+        $controls = array();
+        $items = $this->section_edit_control_items($course, $section, $onsectionpage);
+
+        foreach ($items as $key => $item) {
+                $url = empty($item['url']) ? '' : $item['url'];
+                $icon = empty($item['icon']) ? '' : $item['icon'];
+                $name = empty($item['name']) ? '' : $item['name'];
+                $attr = empty($item['attr']) ? '' : $item['attr'];
+                $class = empty($item['pixattr']['class']) ? '' : $item['pixattr']['class'];
+                $alt = empty($item['pixattr']['alt']) ? '' : $item['pixattr']['alt'];
+                $controls[$key] = html_writer::link(
+                    new moodle_url($url),
+                    html_writer::empty_tag('img', array(
+                        'src' => $this->output->pix_url($icon),
+                        'class' => "icon " . $class,
+                        'alt' => $alt
+                    )),
+                    $attr);
+        }
+
+        debugging('section_edit_controls() is deprecated, please use section_edit_control_items() instead.', DEBUG_DEVELOPER);
+        return $controls;
+    }
+
+    /**
+     * Generate the edit control items of a section
+     *
+     * @param stdClass $course The course entry from DB
+     * @param stdClass $section The course_section entry from DB
+     * @param bool $onsectionpage true if being printed on a section page
+     * @return array of edit control items
+     */
+    protected function section_edit_control_items($course, $section, $onsectionpage = false) {
+        global $PAGE;
+
+        if (!$PAGE->user_is_editing()) {
+            return array();
+        }
+
         $coursecontext = context_course::instance($course->id);
         $isstealth = isset($course->numsections) && ($section->section > $course->numsections);
 
@@ -237,62 +309,94 @@ abstract class format_section_renderer_base extends plugin_renderer_base {
 
         $controls = array();
 
-        $url = clone($baseurl);
-        if (!$isstealth && has_capability('moodle/course:sectionvisibility', $coursecontext)) {
-            if ($section->visible) { // Show the hide/show eye.
-                $strhidefromothers = get_string('hidefromothers', 'format_'.$course->format);
-                $url->param('hide', $section->section);
-                $controls[] = html_writer::link($url,
-                    html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/hide'),
-                    'class' => 'icon hide', 'alt' => $strhidefromothers)),
-                    array('title' => $strhidefromothers, 'class' => 'editing_showhide'));
+        if (!$isstealth && has_capability('moodle/course:update', $coursecontext)) {
+            if ($section->section > 0
+                && get_string_manager()->string_exists('editsection', 'format_'.$course->format)) {
+                $streditsection = get_string('editsection', 'format_'.$course->format);
             } else {
-                $strshowfromothers = get_string('showfromothers', 'format_'.$course->format);
-                $url->param('show',  $section->section);
-                $controls[] = html_writer::link($url,
-                    html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/show'),
-                    'class' => 'icon hide', 'alt' => $strshowfromothers)),
-                    array('title' => $strshowfromothers, 'class' => 'editing_showhide'));
+                $streditsection = get_string('editsection');
             }
-        }
 
-        if (course_can_delete_section($course, $section)) {
-            if (get_string_manager()->string_exists('deletesection', 'format_'.$course->format)) {
-                $strdelete = get_string('deletesection', 'format_'.$course->format);
-            } else {
-                $strdelete = get_string('deletesection');
-            }
-            $url = new moodle_url('/course/editsection.php', array('id' => $section->id,
-                'sr' => $onsectionpage ? $section->section : 0, 'delete' => 1));
-            $controls[] = html_writer::link($url,
-                html_writer::empty_tag('img', array('src' => $this->output->pix_url('t/delete'),
-                    'class' => 'icon delete', 'alt' => $strdelete)),
-                array('title' => $strdelete));
+            $controls['edit'] = array(
+                'url'   => new moodle_url('/course/editsection.php', array('id' => $section->id, 'sr' => $onsectionpage)),
+                'icon' => 'i/settings',
+                'name' => $streditsection,
+                'pixattr' => array('class' => '', 'alt' => $streditsection),
+                'attr' => array('class' => 'icon edit', 'title' => $streditsection));
         }
 
-        if (!$isstealth && !$onsectionpage && has_capability('moodle/course:movesections', $coursecontext)) {
+        if ($section->section) {
             $url = clone($baseurl);
-            if ($section->section > 1) { // Add a arrow to move section up.
-                $url->param('section', $section->section);
-                $url->param('move', -1);
-                $strmoveup = get_string('moveup');
-
-                $controls[] = html_writer::link($url,
-                    html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/up'),
-                    'class' => 'icon up', 'alt' => $strmoveup)),
-                    array('title' => $strmoveup, 'class' => 'moveup'));
+            if (!$isstealth) {
+                if (has_capability('moodle/course:sectionvisibility', $coursecontext)) {
+                    if ($section->visible) { // Show the hide/show eye.
+                        $strhidefromothers = get_string('hidefromothers', 'format_'.$course->format);
+                        $url->param('hide', $section->section);
+                        $controls['visiblity'] = array(
+                            'url' => $url,
+                            'icon' => 'i/hide',
+                            'name' => $strhidefromothers,
+                            'pixattr' => array('class' => '', 'alt' => $strhidefromothers),
+                            'attr' => array('class' => 'icon editing_showhide', 'title' => $strhidefromothers));
+                    } else {
+                        $strshowfromothers = get_string('showfromothers', 'format_'.$course->format);
+                        $url->param('show',  $section->section);
+                        $controls['visiblity'] = array(
+                            'url' => $url,
+                            'icon' => 'i/show',
+                            'name' => $strshowfromothers,
+                            'pixattr' => array('class' => '', 'alt' => $strshowfromothers),
+                            'attr' => array('class' => 'icon editing_showhide', 'title' => $strshowfromothers));
+                    }
+                }
+
+                if (!$onsectionpage) {
+                    if (has_capability('moodle/course:movesections', $coursecontext)) {
+                        $url = clone($baseurl);
+                        if ($section->section > 1) { // Add a arrow to move section up.
+                            $url->param('section', $section->section);
+                            $url->param('move', -1);
+                            $strmoveup = get_string('moveup');
+                            $controls['moveup'] = array(
+                                'url' => $url,
+                                'icon' => 'i/up',
+                                'name' => $strmoveup,
+                                'pixattr' => array('class' => '', 'alt' => $strmoveup),
+                                'attr' => array('class' => 'icon moveup', 'title' => $strmoveup));
+                        }
+
+                        $url = clone($baseurl);
+                        if ($section->section < $course->numsections) { // Add a arrow to move section down.
+                            $url->param('section', $section->section);
+                            $url->param('move', 1);
+                            $strmovedown = get_string('movedown');
+                            $controls['movedown'] = array(
+                                'url' => $url,
+                                'icon' => 'i/down',
+                                'name' => $strmovedown,
+                                'pixattr' => array('class' => '', 'alt' => $strmovedown),
+                                'attr' => array('class' => 'icon movedown', 'title' => $strmovedown));
+                        }
+                    }
+                }
             }
 
-            $url = clone($baseurl);
-            if ($section->section < $course->numsections) { // Add a arrow to move section down.
-                $url->param('section', $section->section);
-                $url->param('move', 1);
-                $strmovedown =  get_string('movedown');
-
-                $controls[] = html_writer::link($url,
-                    html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/down'),
-                    'class' => 'icon down', 'alt' => $strmovedown)),
-                    array('title' => $strmovedown, 'class' => 'movedown'));
+            if (course_can_delete_section($course, $section)) {
+                if (get_string_manager()->string_exists('deletesection', 'format_'.$course->format)) {
+                    $strdelete = get_string('deletesection', 'format_'.$course->format);
+                } else {
+                    $strdelete = get_string('deletesection');
+                }
+                $url = new moodle_url('/course/editsection.php', array(
+                    'id' => $section->id,
+                    'sr' => $onsectionpage ? $section->section : 0,
+                    'delete' => 1));
+                $controls['delete'] = array(
+                    'url' => $url,
+                    'icon' => 'i/delete',
+                    'name' => $strdelete,
+                    'pixattr' => array('class' => '', 'alt' => $strdelete),
+                    'attr' => array('class' => 'icon delete', 'title' => $strdelete));
             }
         }
 
index 94bc371..4e98edb 100644 (file)
@@ -74,7 +74,7 @@ M.course.format.process_sections = function(Y, sectionlist, response, sectionfro
 
         for (var i = sectionfrom; i <= sectionto; i++) {
             // Update section title.
-            sectionlist.item(i).one('.'+CSS.SECTIONNAME).setContent(response.sectiontitles[i]);
+            sectionlist.item(i).all('.'+CSS.SECTIONNAME).setContent(response.sectiontitles[i]);
             // Update move icon.
             ele = sectionlist.item(i).one(SELECTORS.SECTIONLEFTSIDE);
             str = ele.getAttribute('alt');
index 37e2f11..c24e68a 100644 (file)
  */
 
 $string['currentsection'] = 'This topic';
+$string['editsection'] = 'Edit topic';
 $string['deletesection'] = 'Delete topic';
 $string['sectionname'] = 'Topic';
 $string['pluginname'] = 'Topics format';
+$string['sectionmenu'] = 'Topic menu';
 $string['section0name'] = 'General';
 $string['page-course-view-topics'] = 'Any course main page in topics format';
 $string['page-course-view-topics-x'] = 'Any course page in topics format';
index 6e2b43b..45d0684 100644 (file)
@@ -74,14 +74,14 @@ class format_topics_renderer extends format_section_renderer_base {
     }
 
     /**
-     * Generate the edit controls of a section
+     * Generate the edit control items of a section
      *
      * @param stdClass $course The course entry from DB
      * @param stdClass $section The course_section entry from DB
      * @param bool $onsectionpage true if being printed on a section page
-     * @return array of links with edit controls
+     * @return array of edit control items
      */
-    protected function section_edit_controls($course, $section, $onsectionpage = false) {
+    protected function section_edit_control_items($course, $section, $onsectionpage = false) {
         global $PAGE;
 
         if (!$PAGE->user_is_editing()) {
@@ -99,22 +99,26 @@ class format_topics_renderer extends format_section_renderer_base {
 
         $isstealth = $section->section > $course->numsections;
         $controls = array();
-        if (!$isstealth && has_capability('moodle/course:setcurrentsection', $coursecontext)) {
+        if (!$isstealth && $section->section && has_capability('moodle/course:setcurrentsection', $coursecontext)) {
             if ($course->marker == $section->section) {  // Show the "light globe" on/off.
                 $url->param('marker', 0);
-                $controls[] = html_writer::link($url,
-                                    html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/marked'),
-                                        'class' => 'icon ', 'alt' => get_string('markedthistopic'))),
-                                    array('title' => get_string('markedthistopic'), 'class' => 'editing_highlight'));
+                $markedthistopic = get_string('markedthistopic');
+                $highlightoff = get_string('highlightoff');
+                $controls[] = array("url" => $url, "icon" => 'i/marked',
+                                    "name" => $highlightoff,
+                                    'pixattr' => array('class' => '', 'alt' => $markedthistopic),
+                                    "attr" => array('class' => 'editing_highlight', 'title' => $markedthistopic));
             } else {
                 $url->param('marker', $section->section);
-                $controls[] = html_writer::link($url,
-                                html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/marker'),
-                                    'class' => 'icon', 'alt' => get_string('markthistopic'))),
-                                array('title' => get_string('markthistopic'), 'class' => 'editing_highlight'));
+                $markthistopic = get_string('markthistopic');
+                $highlight = get_string('highlight');
+                $controls[] = array("url" => $url, "icon" => 'i/marker',
+                                    "name" => $highlight,
+                                    'pixattr' => array('class' => '', 'alt' => $markthistopic),
+                                    "attr" => array('class' => 'editing_highlight', 'title' => $markthistopic));
             }
         }
 
-        return array_merge($controls, parent::section_edit_controls($course, $section, $onsectionpage));
+        return array_merge($controls, parent::section_edit_control_items($course, $section, $onsectionpage));
     }
 }
index 55f52a5..068dac4 100644 (file)
@@ -1,9 +1,12 @@
 .course-content ul.topics {margin:0;}
 .course-content ul.topics li.section {list-style: none;margin:0 0 5px 0;padding:0;}
 .course-content ul.topics li.section .content {margin:0 40px;}
-.course-content ul.topics li.section .left {float:left;}
-.course-content ul.topics li.section .right {float:right;}
 .course-content ul.topics li.section .left,
-.course-content ul.topics li.section .right {width:40px;text-align:center;padding: 6px 0;}
+.course-content ul.topics li.section .right {width:40px;padding: 0 6px;}
 .course-content ul.topics li.section .right img.icon { padding: 0 0 4px 0;}
+.course-content ul.topics li.section .left {padding-top:22px;text-align: right;}
+.jsenabled .course-content ul.topics li.section .left,
+.jsenabled .course-content ul.topics li.section .right {width:auto;}
 .course-content ul.topics li.section .left .section-handle img.icon { padding:0; vertical-align: baseline; }
+.course-content ul.topics li.section .section_action_menu .textmenu,
+.course-content ul.topics li.section .section_action_menu .menu-action-text { white-space: nowrap; }
\ No newline at end of file
index 4b036d8..6973dea 100644 (file)
@@ -25,14 +25,14 @@ Feature: Sections can be edited and deleted in topics format
     And I turn editing mode on
 
   Scenario: Edit section summary in topics format
-    When I click on "Edit summary" "link" in the "li#section-2" "css_element"
+    When I edit the section "2"
     And I set the following fields to these values:
       | Summary | Welcome to section 2 |
     And I press "Save changes"
     Then I should see "Welcome to section 2" in the "li#section-2" "css_element"
 
   Scenario: Edit section default name in topics format
-    When I click on "Edit summary" "link" in the "li#section-2" "css_element"
+    When I edit the section "2"
     And I set the following fields to these values:
       | Use default section name | 0                        |
       | name                     | This is the second topic |
@@ -41,7 +41,7 @@ Feature: Sections can be edited and deleted in topics format
     And I should not see "Topic 2" in the "li#section-2" "css_element"
 
   Scenario: Deleting the last section in topics format
-    When I click on "Delete topic" "link" in the "li#section-5" "css_element"
+    When I delete section "5"
     Then I should see "Are you absolutely sure you want to completely delete \"Topic 5\" and all the activities it contains?"
     And I press "Delete"
     And I should not see "Topic 5"
@@ -50,7 +50,7 @@ Feature: Sections can be edited and deleted in topics format
     And the field "Number of sections" matches value "4"
 
   Scenario: Deleting the middle section in topics format
-    When I click on "Delete topic" "link" in the "li#section-4" "css_element"
+    When I delete section "4"
     And I press "Delete"
     Then I should not see "Topic 5"
     And I should not see "Test chat name"
@@ -62,7 +62,7 @@ Feature: Sections can be edited and deleted in topics format
   Scenario: Deleting the orphaned section in topics format
     When I follow "Reduce the number of sections"
     Then I should see "Orphaned activities (section 5)" in the "li#section-5" "css_element"
-    And I click on "Delete topic" "link" in the "li#section-5" "css_element"
+    And I delete section "5"
     And I press "Delete"
     And I should not see "Topic 5"
     And I should not see "Orphaned activities"
@@ -76,7 +76,7 @@ Feature: Sections can be edited and deleted in topics format
     Then I should see "Orphaned activities (section 5)" in the "li#section-5" "css_element"
     And "li#section-5.orphaned" "css_element" should exist
     And "li#section-4.orphaned" "css_element" should not exist
-    And I click on "Delete topic" "link" in the "li#section-1" "css_element"
+    And I delete section "1"
     And I press "Delete"
     And I should not see "Test book name"
     And I should see "Orphaned activities (section 4)" in the "li#section-4" "css_element"
index 6919320..4878b9b 100644 (file)
@@ -2,6 +2,10 @@ This files describes API changes for course formats
 
 Overview of this plugin type at http://docs.moodle.org/dev/Course_formats
 
+=== 3.0 ===
+* Course formats should now use section_edit_control_items and use the returned array of controls items and their attributes to create a
+  renderable menu or array of links.  Plugin calls to section_edit_controls will now include the section edit control in the returned array.
+
 === 2.9 ===
 * Course formats may support deleting sections, see MDL-10405 for more details.
   format_section_renderer_base::section_edit_controls() is now also called for
index eb5e106..546a24f 100644 (file)
@@ -74,7 +74,7 @@ M.course.format.process_sections = function(Y, sectionlist, response, sectionfro
 
         for (var i = sectionfrom; i <= sectionto; i++) {
             // Update section title.
-            sectionlist.item(i).one('.'+CSS.SECTIONNAME).setContent(response.sectiontitles[i]);
+            sectionlist.item(i).all('.'+CSS.SECTIONNAME).setContent(response.sectiontitles[i]);
 
             // Update move icon.
             ele = sectionlist.item(i).one(SELECTORS.SECTIONLEFTSIDE);
index 86e24b7..2294574 100644 (file)
  */
 
 $string['currentsection'] = 'This week';
+$string['editsection'] = 'Edit week';
 $string['deletesection'] = 'Delete week';
 $string['sectionname'] = 'Week';
 $string['pluginname'] = 'Weekly format';
+$string['sectionmenu'] = 'Week menu';
 $string['section0name'] = 'General';
 $string['page-course-view-weeks'] = 'Any course main page in weeks format';
 $string['page-course-view-weeks-x'] = 'Any course page in weeks format';
index 11fd482..8f1a5b3 100644 (file)
@@ -1,9 +1,12 @@
 .course-content ul.weeks {margin:0;}
 .course-content ul.weeks li.section {list-style: none;margin:0 0 5px 0;padding:0;}
 .course-content ul.weeks li.section .content {margin:0 40px;}
-.course-content ul.weeks li.section .left {float:left;}
-.course-content ul.weeks li.section .right {float:right;}
 .course-content ul.weeks li.section .left,
-.course-content ul.weeks li.section .right {width:40px;text-align:center;padding: 6px 0;}
+.course-content ul.weeks li.section .right {width:40px;padding: 0 6px;}
 .course-content ul.weeks li.section .right img.icon { padding: 0 0 4px 0;}
+.course-content ul.weeks li.section .left {padding-top:22px;text-align: right;}
+.jsenabled .course-content ul.weeks li.section .left,
+.jsenabled .course-content ul.weeks li.section .right {width:auto;}
 .course-content ul.weeks li.section .left .section-handle img.icon { padding:0; vertical-align: baseline; }
+.course-content ul.weeks li.section .section_action_menu .textmenu,
+.course-content ul.weeks li.section .section_action_menu .menu-action-text { white-space: nowrap; }
\ No newline at end of file
index c8b1022..255fa83 100644 (file)
@@ -25,7 +25,7 @@ Feature: Sections can be edited and deleted in weeks format
     And I turn editing mode on
 
   Scenario: Edit section summary in weeks format
-    When I click on "Edit summary" "link" in the "li#section-2" "css_element"
+    When I click on "Edit week" "link" in the "li#section-2" "css_element"
     And I set the following fields to these values:
       | Summary | Welcome to section 2 |
     And I press "Save changes"
@@ -33,7 +33,7 @@ Feature: Sections can be edited and deleted in weeks format
 
   Scenario: Edit section default name in weeks format
     Given I should see "8 May - 14 May" in the "li#section-2" "css_element"
-    When I click on "Edit summary" "link" in the "li#section-2" "css_element"
+    When I click on "Edit week" "link" in the "li#section-2" "css_element"
     And I set the following fields to these values:
       | Use default section name | 0                       |
       | name                     | This is the second week |
@@ -43,7 +43,7 @@ Feature: Sections can be edited and deleted in weeks format
 
   Scenario: Deleting the last section in weeks format
     Given I should see "29 May - 4 June" in the "li#section-5" "css_element"
-    When I click on "Delete week" "link" in the "li#section-5" "css_element"
+    When I delete section "5"
     Then I should see "Are you absolutely sure you want to completely delete \"29 May - 4 June\" and all the activities it contains?"
     And I press "Delete"
     And I should not see "29 May - 4 June"
@@ -53,7 +53,7 @@ Feature: Sections can be edited and deleted in weeks format
 
   Scenario: Deleting the middle section in weeks format
     Given I should see "29 May - 4 June" in the "li#section-5" "css_element"
-    When I click on "Delete week" "link" in the "li#section-4" "css_element"
+    When I delete section "4"
     And I press "Delete"
     Then I should not see "29 May - 4 June"
     And I should not see "Test chat name"
@@ -65,7 +65,7 @@ Feature: Sections can be edited and deleted in weeks format
   Scenario: Deleting the orphaned section in weeks format
     When I follow "Reduce the number of sections"
     Then I should see "Orphaned activities (section 5)" in the "li#section-5" "css_element"
-    And I click on "Delete week" "link" in the "li#section-5" "css_element"
+    And I delete section "5"
     And I press "Delete"
     And I should not see "29 May - 4 June"
     And I should not see "Orphaned activities"
@@ -79,7 +79,7 @@ Feature: Sections can be edited and deleted in weeks format
     Then I should see "Orphaned activities (section 5)" in the "li#section-5" "css_element"
     And "li#section-5.orphaned" "css_element" should exist
     And "li#section-4.orphaned" "css_element" should not exist
-    And I click on "Delete week" "link" in the "li#section-1" "css_element"
+    And I delete section "1"
     And I press "Delete"
     And I should not see "Test book name"
     And I should see "Orphaned activities (section 4)" in the "li#section-4" "css_element"
index 5cbc191..38e19b6 100644 (file)
@@ -3322,6 +3322,8 @@ function include_course_ajax($course, $usedmodules = array(), $enabledmodules =
             'edittitleinstructions',
             'show',
             'hide',
+            'highlight',
+            'highlightoff',
             'groupsnone',
             'groupsvisible',
             'groupsseparate',
index 9996533..ffe7470 100644 (file)
@@ -202,6 +202,56 @@ class behat_course extends behat_base {
 
     }
 
+
+    /**
+     * Opens a section edit menu if it is not already opened.
+     *
+     * @Given /^I open section "(?P<section_number>\d+)" edit menu$/
+     * @throws DriverException The step is not available when Javascript is disabled
+     * @param string $sectionnumber
+     */
+    public function i_open_section_edit_menu($sectionnumber) {
+        if (!$this->running_javascript()) {
+            throw new DriverException('Section edit menu not available when Javascript is disabled');
+        }
+
+        // If it is already opened we do nothing.
+        $xpath = $this->section_exists($sectionnumber);
+        $xpath .= "/descendant::div[contains(@class, 'section-actions')]/descendant::a[contains(@class, 'textmenu')]";
+
+        $exception = new ExpectationException('Section "' . $sectionnumber . '" was not found', $this->getSession());
+        $menu = $this->find('xpath', $xpath, $exception);
+        $menu->click();
+        $this->i_wait_until_section_is_available($sectionnumber);
+    }
+
+    /**
+     * Deletes course section.
+     *
+     * @Given /^I delete section "(?P<section_number>\d+)"$/
+     * @param int $sectionnumber The section number
+     * @return Given[]
+     */
+    public function i_delete_section($sectionnumber) {
+        // Ensures the section exists.
+        $xpath = $this->section_exists($sectionnumber);
+
+        // We need to know the course format as the text strings depends on them.
+        $courseformat = $this->get_course_format();
+        if (get_string_manager()->string_exists('deletesection', $courseformat)) {
+            $strdelete = get_string('deletesection', $courseformat);
+        } else {
+            $strdelete = get_string('deletesection');
+        }
+
+        // If javascript is on, link is inside a menu.
+        if ($this->running_javascript()) {
+            $this->i_open_section_edit_menu($sectionnumber);
+        }
+
+        return new Given('I click on "' . $strdelete . '" "link" in the "' . $this->escape($xpath) . '" "xpath_element"');
+    }
+
     /**
      * Turns course section highlighting on.
      *
@@ -214,6 +264,11 @@ class behat_course extends behat_base {
         // Ensures the section exists.
         $xpath = $this->section_exists($sectionnumber);
 
+        // If javascript is on, link is inside a menu.
+        if ($this->running_javascript()) {
+            $this->i_open_section_edit_menu($sectionnumber);
+        }
+
         return new Given('I click on "' . get_string('markthistopic') . '" "link" in the "' . $this->escape($xpath) . '" "xpath_element"');
     }
 
@@ -229,6 +284,11 @@ class behat_course extends behat_base {
         // Ensures the section exists.
         $xpath = $this->section_exists($sectionnumber);
 
+        // If javascript is on, link is inside a menu.
+        if ($this->running_javascript()) {
+            $this->i_open_section_edit_menu($sectionnumber);
+        }
+
         return new Given('I click on "' . get_string('markedthistopic') . '" "link" in the "' . $this->escape($xpath) . '" "xpath_element"');
     }
 
@@ -271,7 +331,20 @@ class behat_course extends behat_base {
      * @param int $sectionnumber
      */
     public function i_edit_the_section($sectionnumber) {
-        return new Given('I click on "' . get_string('editsummary') . '" "link" in the "#section-' . $sectionnumber . '" "css_element"');
+        // If javascript is on, link is inside a menu.
+        if ($this->running_javascript()) {
+            $this->i_open_section_edit_menu($sectionnumber);
+        }
+
+        // We need to know the course format as the text strings depends on them.
+        $courseformat = $this->get_course_format();
+        if (get_string_manager()->string_exists('editsection', $courseformat)) {
+            $stredit = get_string('editsection', $courseformat);
+        } else {
+            $stredit = get_string('editsection');
+        }
+
+        return new Given('I click on "' . $stredit . '" "link" in the "#section-' . $sectionnumber . '" "css_element"');
     }
 
     /**
@@ -304,7 +377,7 @@ class behat_course extends behat_base {
         $xpath = $this->section_exists($sectionnumber);
 
         // The important checking, we can not check the img.
-        $xpath = $xpath . "/descendant::img[@alt='" . get_string('markedthistopic') . "'][contains(@src, 'marked')]";
+        $xpath = $xpath . "/descendant::img[contains(@src, 'marked')]";
         $exception = new ExpectationException('The "' . $sectionnumber . '" section is not highlighted', $this->getSession());
         $this->find('xpath', $xpath, $exception);
     }
@@ -409,9 +482,14 @@ class behat_course extends behat_base {
             throw new ExpectationException('The section is hidden', $this->getSession());
         }
 
-        // Hide section button should be visible.
+        // Edit menu should be visible.
         if ($this->is_course_editor()) {
-            $this->hide_section_icon_exists($sectionnumber);
+            $xpath = $sectionxpath .
+                     "/descendant::div[contains(@class, 'section-actions')]" .
+                     "/descendant::a[contains(@class, 'textmenu')]";
+            if (!$this->getSession()->getPage()->find('xpath', $xpath)) {
+                throw new ExpectationException('The section edit menu is not available', $this->getSession());
+            }
         }
     }
 
@@ -431,6 +509,11 @@ class behat_course extends behat_base {
         // Ensures the section exists.
         $sectionxpath = $this->section_exists($sectionnumber);
 
+        // If javascript is on, link is inside a menu.
+        if ($this->running_javascript()) {
+            $this->i_open_section_edit_menu($sectionnumber);
+        }
+
         // Follows the link
         $moveuplink = $this->get_node_in_container('link', get_string('moveup'), 'xpath_element', $sectionxpath);
         $moveuplink->click();
@@ -452,6 +535,11 @@ class behat_course extends behat_base {
         // Ensures the section exists.
         $sectionxpath = $this->section_exists($sectionnumber);
 
+        // If javascript is on, link is inside a menu.
+        if ($this->running_javascript()) {
+            $this->i_open_section_edit_menu($sectionnumber);
+        }
+
         // Follows the link
         $movedownlink = $this->get_node_in_container('link', get_string('movedown'), 'xpath_element', $sectionxpath);
         $movedownlink->click();
@@ -876,10 +964,15 @@ class behat_course extends behat_base {
         // We need to know the course format as the text strings depends on them.
         $courseformat = $this->get_course_format();
 
+        // If javascript is on, link is inside a menu.
+        if ($this->running_javascript()) {
+            $this->i_open_section_edit_menu($sectionnumber);
+        }
+
         // Checking the show button alt text and show icon.
         $showtext = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('showfromothers', $courseformat));
         $linkxpath = $xpath . "/descendant::a[@title=$showtext]";
-        $imgxpath = $linkxpath . "/descendant::img[@alt=$showtext][contains(@src, 'show')]";
+        $imgxpath = $linkxpath . "/descendant::img[contains(@src, 'show')]";
 
         $exception = new ElementNotFoundException($this->getSession(), 'Show section icon ');
         $this->find('xpath', $imgxpath, $exception);
@@ -903,10 +996,15 @@ class behat_course extends behat_base {
         // We need to know the course format as the text strings depends on them.
         $courseformat = $this->get_course_format();
 
+        // If javascript is on, link is inside a menu.
+        if ($this->running_javascript()) {
+            $this->i_open_section_edit_menu($sectionnumber);
+        }
+
         // Checking the hide button alt text and hide icon.
         $hidetext = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('hidefromothers', $courseformat));
         $linkxpath = $xpath . "/descendant::a[@title=$hidetext]";
-        $imgxpath = $linkxpath . "/descendant::img[@alt=$hidetext][contains(@src, 'hide')]";
+        $imgxpath = $linkxpath . "/descendant::img[contains(@src, 'hide')]";
 
         $exception = new ElementNotFoundException($this->getSession(), 'Hide section icon ');
         $this->find('xpath', $imgxpath, $exception);
index 27b2603..e34a793 100644 (file)
@@ -1603,7 +1603,7 @@ class core_course_courselib_testcase extends advanced_testcase {
         $sink->close();
 
         // Validate the event.
-        $event = $events[0];
+        $event = array_pop($events);
         $this->assertInstanceOf('\core\event\course_created', $event);
         $this->assertEquals('course', $event->objecttable);
         $this->assertEquals($course->id, $event->objectid);
@@ -1633,7 +1633,7 @@ class core_course_courselib_testcase extends advanced_testcase {
         $imstestcase->imsplugin->cron();
         $events = $sink->get_events();
         $sink->close();
-        $event = $events[0];
+        $event = array_pop($events);
 
         // Validate the event triggered is \core\event\course_created. There is no need to validate the other values
         // as they have already been validated in the previous steps. Here we only want to make sure that when the
@@ -1750,7 +1750,7 @@ class core_course_courselib_testcase extends advanced_testcase {
         $sink->close();
 
         // Validate the event.
-        $event = $events[1];
+        $event = array_pop($events);
         $this->assertInstanceOf('\core\event\course_deleted', $event);
         $this->assertEquals('course', $event->objecttable);
         $this->assertEquals($course->id, $event->objectid);
@@ -1802,7 +1802,7 @@ class core_course_courselib_testcase extends advanced_testcase {
         $sink->close();
 
         // Validate the event.
-        $event = $events[0];
+        $event = array_pop($events);
         $this->assertInstanceOf('\core\event\course_content_deleted', $event);
         $this->assertEquals('course', $event->objecttable);
         $this->assertEquals($course->id, $event->objectid);
@@ -1927,7 +1927,7 @@ class core_course_courselib_testcase extends advanced_testcase {
         $sink->close();
 
         // Validate the event.
-        $event = $events[0];
+        $event = array_pop($events);
         $this->assertInstanceOf('\core\event\course_restored', $event);
         $this->assertEquals('course', $event->objecttable);
         $this->assertEquals($rc->get_courseid(), $event->objectid);
index 08ce8ea..fe94ff0 100644 (file)
Binary files a/course/yui/build/moodle-course-dragdrop/moodle-course-dragdrop-debug.js and b/course/yui/build/moodle-course-dragdrop/moodle-course-dragdrop-debug.js differ
index 0af8c1b..ba2426d 100644 (file)
Binary files a/course/yui/build/moodle-course-dragdrop/moodle-course-dragdrop-min.js and b/course/yui/build/moodle-course-dragdrop/moodle-course-dragdrop-min.js differ
index 9da1d7d..7befde3 100644 (file)
Binary files a/course/yui/build/moodle-course-dragdrop/moodle-course-dragdrop.js and b/course/yui/build/moodle-course-dragdrop/moodle-course-dragdrop.js differ
index 6579845..be0b6a0 100644 (file)
Binary files a/course/yui/build/moodle-course-toolboxes/moodle-course-toolboxes-debug.js and b/course/yui/build/moodle-course-toolboxes/moodle-course-toolboxes-debug.js differ
index 3da12bd..f0323e8 100644 (file)
Binary files a/course/yui/build/moodle-course-toolboxes/moodle-course-toolboxes-min.js and b/course/yui/build/moodle-course-toolboxes/moodle-course-toolboxes-min.js differ
index 6579845..be0b6a0 100644 (file)
Binary files a/course/yui/build/moodle-course-toolboxes/moodle-course-toolboxes.js and b/course/yui/build/moodle-course-toolboxes/moodle-course-toolboxes.js differ
index 3799bb5..b585004 100644 (file)
@@ -75,10 +75,30 @@ Y.extend(DRAGSECTION, M.core.dragdrop, {
                     cssleft.appendChild(this.get_drag_handle(title, CSS.SECTIONHANDLE, 'icon', true));
 
                     if (moveup) {
-                        moveup.remove();
+                        if (moveup.previous('br')) {
+                            moveup.previous('br').remove();
+                        } else if (moveup.next('br')) {
+                            moveup.next('br').remove();
+                        }
+
+                        if (moveup.ancestor('.section_action_menu')) {
+                            moveup.ancestor('li').remove();
+                        } else {
+                            moveup.remove();
+                        }
                     }
                     if (movedown) {
-                        movedown.remove();
+                        if (movedown.previous('br')) {
+                            movedown.previous('br').remove();
+                        } else if (movedown.next('br')) {
+                            movedown.next('br').remove();
+                        }
+
+                        if (movedown.ancestor('.section_action_menu')) {
+                            movedown.ancestor('li').remove();
+                        } else {
+                            movedown.remove();
+                        }
                     }
 
                     // This section can be moved - add the class to indicate this to Y.DD.
index 1298a24..fe9b84e 100644 (file)
@@ -49,6 +49,7 @@ Y.extend(SECTIONTOOLBOX, TOOLBOX, {
         var section = e.target.ancestor(M.course.format.get_section_selector(Y)),
             button = e.target.ancestor('a', true),
             hideicon = button.one('img'),
+            buttontext = button.one('span'),
 
         // The value to submit
             value,
@@ -75,8 +76,11 @@ Y.extend(SECTIONTOOLBOX, TOOLBOX, {
             'src'   : M.util.image_url('i/' + nextaction)
         });
         button.set('title', newstring);
+        if (buttontext) {
+            buttontext.set('text', newstring);
+        }
 
-        // Change the highlight status
+        // Change the show/hide status
         var data = {
             'class' : 'section',
             'field' : 'visible',
@@ -121,6 +125,7 @@ Y.extend(SECTIONTOOLBOX, TOOLBOX, {
         var section = e.target.ancestor(M.course.format.get_section_selector(Y));
         var button = e.target.ancestor('a', true);
         var buttonicon = button.one('img');
+        var buttontext = button.one('span');
 
         // Determine whether the marker is currently set.
         var togglestatus = section.hasClass('current');
@@ -128,16 +133,21 @@ Y.extend(SECTIONTOOLBOX, TOOLBOX, {
 
         // Set the current highlighted item text.
         var old_string = M.util.get_string('markthistopic', 'moodle');
-        Y.one(SELECTOR.PAGECONTENT)
+
+        var selectedpage = Y.one(SELECTOR.PAGECONTENT);
+        selectedpage
             .all(M.course.format.get_section_selector(Y) + '.current ' + SELECTOR.HIGHLIGHT)
             .set('title', old_string);
-        Y.one(SELECTOR.PAGECONTENT)
+        selectedpage
+            .all(M.course.format.get_section_selector(Y) + '.current ' + SELECTOR.HIGHLIGHT + ' span')
+            .set('text', M.util.get_string('highlight', 'moodle'));
+        selectedpage
             .all(M.course.format.get_section_selector(Y) + '.current ' + SELECTOR.HIGHLIGHT + ' img')
             .set('alt', old_string)
             .set('src', M.util.image_url('i/marker'));
 
         // Remove the highlighting from all sections.
-        Y.one(SELECTOR.PAGECONTENT).all(M.course.format.get_section_selector(Y))
+        selectedpage.all(M.course.format.get_section_selector(Y))
             .removeClass('current');
 
         // Then add it if required to the selected section.
@@ -150,6 +160,10 @@ Y.extend(SECTIONTOOLBOX, TOOLBOX, {
             buttonicon
                 .set('alt', new_string)
                 .set('src', M.util.image_url('i/marked'));
+            if (buttontext) {
+                buttontext
+                    .set('text', M.util.get_string('highlightoff', 'moodle'));
+            }
         }
 
         // Change the highlight status.
index b921f15..3affaa9 100644 (file)
@@ -96,6 +96,7 @@ if ($mform->is_cancelled()) {
             $instance->customint2 = $groupid;
         }
         $DB->update_record('enrol', $instance);
+        \core\event\enrol_instance_updated::create_from_record($instance)->trigger();
     }  else {
         // Create a new group for the cohort if requested.
         if ($data->customint2 == COHORT_CREATE_GROUP) {
index 9336e1c..f224f0a 100644 (file)
@@ -332,6 +332,7 @@ class enrol_guest_plugin extends enrol_plugin {
                         $instance->password = $data->{'enrol_guest_password_'.$i};
                     }
                     $DB->update_record('enrol', $instance);
+                    \core\event\enrol_instance_updated::create_from_record($instance)->trigger();
 
                     if ($reset) {
                         $context = context_course::instance($course->id);
index d6d01e2..27e1de7 100644 (file)
@@ -92,13 +92,13 @@ if ($mform->is_cancelled()) {
         $instance->notifyall       = $data->notifyall;
         $instance->expirythreshold = $data->expirythreshold;
         $instance->timemodified    = time();
+        $markdirty = ($instance->status != $data->status);
+        $instance->status = $data->status;
 
         $DB->update_record('enrol', $instance);
+        \core\event\enrol_instance_updated::create_from_record($instance)->trigger();
 
-        // Use standard API to update instance status.
-        if ($instance->status != $data->status) {
-            $instance = $DB->get_record('enrol', array('id'=>$instance->id));
-            $plugin->update_status($instance, $data->status);
+        if ($markdirty) {
             $context->mark_dirty();
         }
 
index dd62786..446fca5 100644 (file)
@@ -204,4 +204,30 @@ class enrol_meta_observer extends enrol_meta_handler {
 
         return true;
     }
+
+    /**
+     * Triggered via enrol_instance_updated event.
+     *
+     * @param \core\event\enrol_instance_updated $event
+     * @return boolean
+     */
+    public static function enrol_instance_updated(\core\event\enrol_instance_updated $event) {
+        global $DB;
+
+        if (!enrol_is_enabled('meta')) {
+            // This is slow, let enrol_meta_sync() deal with disabled plugin.
+            return true;
+        }
+
+        // Does anything want to sync with this parent?
+        $affectedcourses = $DB->get_fieldset_sql('SELECT DISTINCT courseid FROM {enrol} '.
+                'WHERE customint1 = ? AND enrol = ?',
+                array($event->courseid, 'meta'));
+
+        foreach ($affectedcourses as $courseid) {
+            enrol_meta_sync($courseid);
+        }
+
+        return true;
+    }
 }
index 8d2a590..560aefc 100644 (file)
@@ -52,4 +52,8 @@ $observers = array(
         'eventname'   => '\core\event\course_deleted',
         'callback'    => 'enrol_meta_observer::course_deleted',
     ),
+    array(
+        'eventname'   => '\core\event\enrol_instance_updated',
+        'callback'    => 'enrol_meta_observer::enrol_instance_updated',
+    ),
 );
index 4d3b549..9f0fb8a 100644 (file)
@@ -847,6 +847,16 @@ class enrol_meta_plugin_testcase extends advanced_testcase {
         // Disable manual enrolment in course1 and make sure all user enrolments in course2 are suspended.
         $manplugin->update_status($manual1, ENROL_INSTANCE_DISABLED);
         $allsuspendedenrolemnts = array_combine(array_keys($expectedenrolments), array_fill(0, 5, ENROL_USER_SUSPENDED));
+        $enrolmentstatuses = $DB->get_records_menu('user_enrolments', array('enrolid' => $meta2id), '', 'userid, status');
+        $this->assertEquals($allsuspendedenrolemnts, $enrolmentstatuses);
+
+        $manplugin->update_status($manual1, ENROL_INSTANCE_ENABLED);
+        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta2id), '', 'userid, timestart, timeend, status');
+        $this->assertEquals($expectedenrolments, $enrolments);
+
+        // Disable events and repeat the same for course3 (testing sync):
+        $sink = $this->redirectEvents();
+        $manplugin->update_status($manual1, ENROL_INSTANCE_DISABLED);
         enrol_meta_sync($course3->id);
         $enrolmentstatuses = $DB->get_records_menu('user_enrolments', array('enrolid' => $meta3id), '', 'userid, status');
         $this->assertEquals($allsuspendedenrolemnts, $enrolmentstatuses);
@@ -855,5 +865,6 @@ class enrol_meta_plugin_testcase extends advanced_testcase {
         enrol_meta_sync($course3->id);
         $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta3id), '', 'userid, timestart, timeend, status');
         $this->assertEquals($expectedenrolments, $enrolments);
+        $sink->close();
     }
 }
index 3df94e1..83de31c 100644 (file)
@@ -24,7 +24,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2015051100;        // The current plugin version (Date: YYYYMMDDXX)
+$plugin->version   = 2015082400;        // The current plugin version (Date: YYYYMMDDXX)
 $plugin->requires  = 2015050500;        // Requires this Moodle version
 $plugin->component = 'enrol_meta';      // Full name of the plugin (used for diagnostics)
 $plugin->cron      = 60*60;             // run cron every hour by default, it is not out-of-sync often
index 6175b3b..9c0541a 100644 (file)
@@ -76,6 +76,7 @@ if ($mform->is_cancelled()) {
         $instance->enrolenddate   = $data->enrolenddate;
         $instance->timemodified   = time();
         $DB->update_record('enrol', $instance);
+        \core\event\enrol_instance_updated::create_from_record($instance)->trigger();
 
         if ($reset) {
             $context->mark_dirty();
index a339b8e..f429c39 100644 (file)
@@ -109,6 +109,7 @@ if ($mform->is_cancelled()) {
         $instance->enrolenddate   = $data->enrolenddate;
         $instance->timemodified   = time();
         $DB->update_record('enrol', $instance);
+        \core\event\enrol_instance_updated::create_from_record($instance)->trigger();
 
         if ($reset) {
             $context->mark_dirty();
index 61a4392..1a88c50 100644 (file)
@@ -362,4 +362,64 @@ class core_enrollib_testcase extends advanced_testcase {
         $this->assertEventLegacyLogData($expected, $event);
         $this->assertEventContextNotUsed($event);
     }
+
+    /**
+     * Test enrol_instance_created, enrol_instance_updated and enrol_instance_deleted events.
+     */
+    public function test_instance_events() {
+        global $DB;
+
+        $this->resetAfterTest(true);
+
+        $selfplugin = enrol_get_plugin('self');
+        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
+
+        $course = $this->getDataGenerator()->create_course();
+
+        // Creating enrol instance.
+        $sink = $this->redirectEvents();
+        $instanceid = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
+                                                                'name' => 'Test instance 1',
+                                                                'customint6' => 1,
+                                                                'roleid' => $studentrole->id));
+        $events = $sink->get_events();
+        $sink->close();
+
+        $this->assertCount(1, $events);
+        $event = array_pop($events);
+        $this->assertInstanceOf('\core\event\enrol_instance_created', $event);
+        $this->assertEquals(context_course::instance($course->id), $event->get_context());
+        $this->assertEquals('self', $event->other['enrol']);
+        $this->assertEventContextNotUsed($event);
+
+        // Updating enrol instance.
+        $instance = $DB->get_record('enrol', array('id' => $instanceid));
+        $sink = $this->redirectEvents();
+        $selfplugin->update_status($instance, ENROL_INSTANCE_DISABLED);
+
+        $events = $sink->get_events();
+        $sink->close();
+
+        $this->assertCount(1, $events);
+        $event = array_pop($events);
+        $this->assertInstanceOf('\core\event\enrol_instance_updated', $event);
+        $this->assertEquals(context_course::instance($course->id), $event->get_context());
+        $this->assertEquals('self', $event->other['enrol']);
+        $this->assertEventContextNotUsed($event);
+
+        // Deleting enrol instance.
+        $instance = $DB->get_record('enrol', array('id' => $instanceid));
+        $sink = $this->redirectEvents();
+        $selfplugin->delete_instance($instance);
+
+        $events = $sink->get_events();
+        $sink->close();
+
+        $this->assertCount(1, $events);
+        $event = array_pop($events);
+        $this->assertInstanceOf('\core\event\enrol_instance_deleted', $event);
+        $this->assertEquals(context_course::instance($course->id), $event->get_context());
+        $this->assertEquals('self', $event->other['enrol']);
+        $this->assertEventContextNotUsed($event);
+    }
 }
index 40f9682..d409025 100644 (file)
@@ -1,6 +1,12 @@
 This files describes API changes in /enrol/* - plugins,
 information provided here is intended especially for developers.
 
+=== 3.0 ===
+
+* Added new events enrol_instance_created, enrol_instance_updated and
+  enrol_instance_deleted . Always trigger them when changing records in the
+  DB table 'enrol'.
+
 === 2.9 ===
 
 * External function core_enrol_external::get_users_courses now returns additional optional fields:
index b014e43..44aabad 100644 (file)
@@ -14,7 +14,7 @@
 // You should have received a copy of the GNU General Public License
 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 
-require_once(__DIR__ . "../../../../config.php");
+require_once(__DIR__ . "/../../../config.php");
 require_once($CFG->libdir.'/gradelib.php');
 require_once($CFG->dirroot.'/grade/lib.php');
 require_once($CFG->dirroot.'/grade/import/lib.php');
index 2216d57..1c88196 100644 (file)
@@ -308,6 +308,7 @@ class graded_users_iterator {
                     $grades[$grade_item->id] =
                         new grade_grade(array('userid'=>$user->id, 'itemid'=>$grade_item->id), false);
                 }
+                $grades[$grade_item->id]->grade_item = $grade_item;
             }
         }
 
index 9d54e7c..d7a5a07 100644 (file)
@@ -1359,13 +1359,14 @@ class core_group_external extends external_api {
             }
 
             // Validate if the user is enrolled in the course.
-            if (!is_enrolled($coursecontext, $user->id)) {
+            $course = get_course($cm->course);
+            if (!can_access_course($course, $user, '', true)) {
                 // We return a warning because the function does not fail for not enrolled users.
                 $warning = array();
                 $warning['item'] = 'course';
                 $warning['itemid'] = $cm->course;
                 $warning['warningcode'] = '1';
-                $warning['message'] = "User $user->id is not enrolled in course $cm->course";
+                $warning['message'] = "User $user->id cannot access course $cm->course";
                 $warnings[] = $warning;
             }
         }
index 0e94de5..1e89ba4 100644 (file)
@@ -64,6 +64,7 @@ reload,moodle
 remotedownloaderror,error
 thisdirection,langconfig
 thislanguage,langconfig
+upgradekeyset,admin
 welcomep10,install
 welcomep20,install
 welcomep30,install
index 99715f5..4ac1e2b 100644 (file)
@@ -1105,6 +1105,8 @@ $string['upgradepluginsinfo_link'] = 'admin/upgradepluginsinfo';
 $string['upgradeerror'] = 'Unknown error upgrading {$a->plugin} to version {$a->version}, can not continue.';
 $string['upgradeforumread'] = 'A new feature has been added in Moodle 1.5 to track read/unread forum posts.<br />To use this functionality you need to <a href="{$a}">update your tables</a>.';
 $string['upgradeforumreadinfo'] = 'A new feature has been added in Moodle 1.5 to track read/unread forum posts.  To use this functionality you need to update your tables with all the tracking information for existing posts.  Depending on the size of your site this can take a long time (hours) and can be quite taxing on the database, so it\'s best to do it during a quiet period.  However, your site will continue functioning during this upgrade and users won\'t be affected.  Once you start this process you should let it finish (keep your browser window open).  However, if you stop the process by closing the window: don\'t worry, you can start over.<br /><br />Do you want to start the upgrading process now?';
+$string['upgradekeyreq'] = 'Upgrade key required';
+$string['upgradekeyset'] = 'Upgrade key (leave empty to not set it)';
 $string['upgradelogs'] = 'For full functionality, your old logs need to be upgraded.  <a href="{$a}">More information</a>';
 $string['upgradelogsinfo'] = 'Some changes have recently been made in the way logs are stored.  To be able to view all of your old logs on a per-activity basis, your old logs need to be upgraded.  Depending on your site this can take a long time (eg several hours) and can be quite taxing on the database for large sites.  Once you start this process you should let it finish (by keeping the browser window open).  Don\'t worry - your site will work fine for other people while the logs are being upgraded.<br /><br />Do you want to upgrade your logs now?';
 $string['upgradesettings'] = 'New settings';
index d8fd647..c0c0078 100644 (file)
@@ -30,6 +30,11 @@ $string['automatedbackupschedule'] = 'Schedule';
 $string['automatedbackupschedulehelp'] = 'Choose which days of the week to perform automated backups.';
 $string['automatedbackupsinactive'] = 'Automated backups haven\'t been enabled by the site admin';
 $string['automatedbackupstatus'] = 'Automated backup status';
+$string['automateddeletedays'] = 'Delete backups older than';
+$string['automatedmaxkept'] = 'Maximum number of backups kept';
+$string['automatedmaxkepthelp'] = 'This specifies the maximum number of recent automated backups to be kept for each course. Older backups will be deleted automatically.';
+$string['automatedminkept'] = 'Minimum number of backups kept';
+$string['automatedminkepthelp'] = 'If backups older than a specified number of days are deleted, it can happen that an inactive course ends up with no backup. To prevent this, a minimum number of backups kept should be specified.';
 $string['automatedsetup'] = 'Automated backup setup';
 $string['automatedsettings'] = 'Automated backup settings';
 $string['automatedstorage'] = 'Automated backup storage';
index 1ca0a6e..ec115e2 100644 (file)
@@ -48,6 +48,9 @@ $string['enrolcandidates'] = 'Not enrolled users';
 $string['enrolcandidatesmatching'] = 'Matching not enrolled users';
 $string['enrolcohort'] = 'Enrol cohort';
 $string['enrolcohortusers'] = 'Enrol users';
+$string['eventenrolinstancecreated'] = 'Enrolment instance created';
+$string['eventenrolinstancedeleted'] = 'Enrolment instance deleted';
+$string['eventenrolinstanceupdated'] = 'Enrolment instance updated';
 $string['enrollednewusers'] = 'Successfully enrolled {$a} new users';
 $string['enrolledusers'] = 'Enrolled users';
 $string['enrolledusersmatching'] = 'Matching enrolled users';
index 2acbcfa..a618353 100644 (file)
@@ -60,6 +60,7 @@ $string['eventmessagecontactadded'] = 'Message contact added';
 $string['eventmessagecontactblocked'] = 'Message contact blocked';
 $string['eventmessagecontactremoved'] = 'Message contact removed';
 $string['eventmessagecontactunblocked'] = 'Message contact unblocked';
+$string['eventmessagedeleted'] = 'Message deleted';
 $string['eventmessageviewed'] = 'Message viewed';
 $string['eventmessagesent'] = 'Message sent';
 $string['forced'] = 'Forced';
index 4a6e3fc..47cd797 100644 (file)
@@ -186,7 +186,6 @@ $string['backupfromthissite'] = 'Backup was made on this site?';
 $string['backupgradebookhistoryhelp'] = 'If enabled then gradebook history will be included in automated backups. Note that grade history must not be disabled in server settings (disablegradehistory) in order for this to work';
 $string['backupincludemoduleshelp'] = 'Choose whether you want to include course modules, with or without user data, in automated backups';
 $string['backupincludemoduleuserdatahelp'] = 'Choose whether you want to include module user data in automated backups.';
-$string['backupkeephelp'] = 'How many recent backups for each course do you want to keep? (older ones will be deleted automatically)';
 $string['backuplogdetailed'] = 'Detailed execution log';
 $string['backuploglaststatus'] = 'Last execution log';
 $string['backupmissinguserinfoperms'] = 'Note: This backup contains no user data. Exercise and Workshop activities will not be included in the backup, since these modules are not compatible with this type of backup.';
@@ -557,7 +556,7 @@ $string['editorpreferences'] = 'Editor preferences';
 $string['editorresettodefaults'] = 'Reset to default values';
 $string['editorsettings'] = 'Editor settings';
 $string['editorshortcutkeys'] = 'Editor shortcut keys';
-$string['editsettings'] = 'Edit settings';
+$string['editsection'] = 'Edit section';
 $string['editsummary'] = 'Edit summary';
 $string['edittitle'] = 'Edit title';
 $string['edittitleinstructions'] = 'Escape to cancel, Enter when finished';
@@ -924,6 +923,8 @@ $string['hidepicture'] = 'Hide picture';
 $string['hidesection'] = 'Hide section {$a}';
 $string['hidesettings'] = 'Hide settings';
 $string['hideshowblocks'] = 'Hide or show blocks';
+$string['highlight'] = 'Highlight';
+$string['highlightoff'] = 'Remove highlight';
 $string['hits'] = 'Hits';
 $string['hitsoncourse'] = 'Hits on {$a->coursename} by {$a->username}';
 $string['hitsoncoursetoday'] = 'Today\'s hits on {$a->coursename} by {$a->username}';
@@ -1632,6 +1633,7 @@ $string['secondstotime86400'] = '1 day';
 $string['secretalreadyused'] = 'Change password confirmation link was already used, password was not changed.';
 $string['secs'] = 'secs';
 $string['section'] = 'Section';
+$string['sectionmenu'] = 'Section menu';
 $string['sectionname'] = 'Section name';
 $string['sections'] = 'Sections';
 $string['sectionusedefaultname'] = 'Use default section name';
index 6a65b98..95db37f 100644 (file)
@@ -72,6 +72,7 @@ $string['requires'] = 'Requires';
 $string['rootdir'] = 'Directory';
 $string['settings'] = 'Settings';
 $string['somehighlighted'] = 'Number of plugins requiring your attention: {$a}';
+$string['somehighlightedall'] = 'Number of installed plugins: {$a}';
 $string['somehighlightedinfo'] = 'Display the full list of installed plugins';
 $string['somehighlightedonly'] = 'Display only plugins requiring your attention';
 $string['source'] = 'Source';
index 8e3d7e5..d88de86 100644 (file)
@@ -96,7 +96,7 @@ define('BADGE_MESSAGE_MONTHLY', 4);
 /*
  * URL of backpack. Currently only the Open Badges backpack is supported.
  */
-define('BADGE_BACKPACKURL', 'backpack.openbadges.org');
+define('BADGE_BACKPACKURL', 'https://backpack.openbadges.org');
 
 /**
  * Class that represents badge.
@@ -1160,7 +1160,7 @@ function badges_check_backpack_accessibility() {
         'HEADER' => 0,
         'CONNECTTIMEOUT' => 2,
     );
-    $location = 'http://' . BADGE_BACKPACKURL . '/baker';
+    $location = BADGE_BACKPACKURL . '/baker';
     $out = $curl->get($location, array('assertion' => $fakeassertion->out(false)), $options);
 
     $data = json_decode($out);
@@ -1228,8 +1228,7 @@ function badges_setup_backpack_js() {
     global $CFG, $PAGE;
     if (!empty($CFG->badges_allowexternalbackpack)) {
         $PAGE->requires->string_for_js('error:backpackproblem', 'badges');
-        $protocol = (is_https()) ? 'https://' : 'http://';
-        $PAGE->requires->js(new moodle_url($protocol . BADGE_BACKPACKURL . '/issuer.js'), true);
+        $PAGE->requires->js(new moodle_url(BADGE_BACKPACKURL . '/issuer.js'), true);
         $PAGE->requires->js('/badges/backpack.js', true);
     }
 }
diff --git a/lib/classes/event/enrol_instance_created.php b/lib/classes/event/enrol_instance_created.php
new file mode 100644 (file)
index 0000000..ba03ea4
--- /dev/null
@@ -0,0 +1,109 @@
+<?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/>.
+
+/**
+ * Enrol instance created event.
+ *
+ * @package    core
+ * @copyright  2015 Marina Glancy
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Enrol instance created event class.
+ *
+ * @property-read array $other {
+ *      Extra information about event.
+ *
+ *      - string enrol: name of enrol method
+ * }
+ *
+ * @package    core
+ * @since      Moodle 2.9
+ * @copyright  2015 Marina Glancy
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class enrol_instance_created extends base {
+
+    /**
+     * Api to Create new event from enrol object.
+     *
+     * @param \stdClass $enrol record from DB table 'enrol'
+     * @return \core\event\base returns instance of new event
+     */
+    public static final function create_from_record($enrol) {
+        $event = static::create(array(
+            'context'  => \context_course::instance($enrol->courseid),
+            'objectid' => $enrol->id,
+            'other'    => array('enrol' => $enrol->enrol)
+        ));
+        return $event;
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' created the instance of enrolment method '" .
+                $this->other['enrol'] . "' with id '$this->objectid'.";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventenrolinstancecreated', 'enrol');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/enrol/instances.php', array('id' => $this->courseid));
+    }
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'c';
+        $this->data['edulevel'] = self::LEVEL_OTHER;
+        $this->data['objecttable'] = 'enrol';
+    }
+
+    /**
+     * custom validations
+     *
+     * Throw \coding_exception notice in case of any problems.
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->other['enrol'])) {
+            throw new \coding_exception('The \'enrol\' value must be set in other.');
+        }
+    }
+}
diff --git a/lib/classes/event/enrol_instance_deleted.php b/lib/classes/event/enrol_instance_deleted.php
new file mode 100644 (file)
index 0000000..9c1bf95
--- /dev/null
@@ -0,0 +1,110 @@
+<?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/>.
+
+/**
+ * Enrol instance deleted event.
+ *
+ * @package    core
+ * @copyright  2015 Marina Glancy
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Enrol instance deleted event class.
+ *
+ * @property-read array $other {
+ *      Extra information about event.
+ *
+ *      - string enrol: name of enrol method
+ * }
+ *
+ * @package    core
+ * @since      Moodle 2.9
+ * @copyright  2015 Marina Glancy
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class enrol_instance_deleted extends base {
+
+    /**
+     * Api to Create new event from enrol object.
+     *
+     * @param \stdClass $enrol record from DB table 'enrol'
+     * @return \core\event\base returns instance of new event
+     */
+    public static final function create_from_record($enrol) {
+        $event = static::create(array(
+            'context'  => \context_course::instance($enrol->courseid),
+            'objectid' => $enrol->id,
+            'other'    => array('enrol' => $enrol->enrol)
+        ));
+        $event->add_record_snapshot('enrol', $enrol);
+        return $event;
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' deleted the instance of enrolment method '" .
+                $this->other['enrol'] . "' with id '$this->objectid'.";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventgroupingdeleted', 'group');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/enrol/instances.php', array('id' => $this->courseid));
+    }
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'd';
+        $this->data['edulevel'] = self::LEVEL_OTHER;
+        $this->data['objecttable'] = 'enrol';
+    }
+
+    /**
+     * custom validations
+     *
+     * Throw \coding_exception notice in case of any problems.
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->other['enrol'])) {
+            throw new \coding_exception('The \'enrol\' value must be set in other.');
+        }
+    }
+}
diff --git a/lib/classes/event/enrol_instance_updated.php b/lib/classes/event/enrol_instance_updated.php
new file mode 100644 (file)
index 0000000..98732d7
--- /dev/null
@@ -0,0 +1,110 @@
+<?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/>.
+
+/**
+ * Enrol instance updated event.
+ *
+ * @package    core
+ * @copyright  2015 Marina Glancy
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Enrol instance updated event class.
+ *
+ * @property-read array $other {
+ *      Extra information about event.
+ *
+ *      - string enrol: name of enrol method
+ * }
+ *
+ * @package    core
+ * @since      Moodle 2.9
+ * @copyright  2015 Marina Glancy
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class enrol_instance_updated extends base {
+
+    /**
+     * Api to Create new event from enrol object.
+     *
+     * @param \stdClass $enrol record from DB table 'enrol'
+     * @return \core\event\base returns instance of new event
+     */
+    public static final function create_from_record($enrol) {
+        $event = static::create(array(
+            'context'  => \context_course::instance($enrol->courseid),
+            'objectid' => $enrol->id,
+            'other'    => array('enrol' => $enrol->enrol)
+        ));
+        $event->add_record_snapshot('enrol', $enrol);
+        return $event;
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' updated the instance of enrolment method '" .
+                $this->other['enrol'] . "' with id '$this->objectid'.";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventenrolinstanceupdated', 'enrol');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/enrol/instances.php', array('id' => $this->courseid));
+    }
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'u';
+        $this->data['edulevel'] = self::LEVEL_OTHER;
+        $this->data['objecttable'] = 'enrol';
+    }
+
+    /**
+     * custom validations
+     *
+     * Throw \coding_exception notice in case of any problems.
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->other['enrol'])) {
+            throw new \coding_exception('The \'enrol\' value must be set in other.');
+        }
+    }
+}
diff --git a/lib/classes/event/message_deleted.php b/lib/classes/event/message_deleted.php
new file mode 100644 (file)
index 0000000..5d3f71b
--- /dev/null
@@ -0,0 +1,145 @@
+<?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/>.
+
+/**
+ * Message deleted event.
+ *
+ * @package    core
+ * @copyright  2015 Mark Nelson <markn@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Message deleted event class.
+ *
+ * @property-read array $other {
+ *      Extra information about event.
+ *
+ *      - string $messagetable: the table we marked the message as deleted from (message/message_read).
+ *      - int messageid: the id of the message.
+ *      - int useridfrom: the id of the user who received the message.
+ *      - int useridto: the id of the user who sent the message.
+ * }
+ *
+ * @package    core
+ * @since      Moodle 3.0
+ * @copyright  2015 Mark Nelson <markn@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class message_deleted extends base {
+
+    /**
+     * Create event using ids.
+     *
+     * @param int $userfromid the user who the message was from.
+     * @param int $usertoid the user who the message was sent to.
+     * @param int $userdeleted the user who deleted it.
+     * @param string $messagetable the table we are marking the message as deleted in.
+     * @param int $messageid the id of the message that was deleted.
+     * @return message_deleted
+     */
+    public static function create_from_ids($userfromid, $usertoid, $userdeleted, $messagetable, $messageid) {
+        // Check who was deleting the message.
+        if ($userdeleted == $userfromid) {
+            $relateduserid = $usertoid;
+        } else {
+            $relateduserid = $userfromid;
+        }
+
+        // We set the userid to the user who deleted the message, nothing to do
+        // with whether or not they sent or received the message.
+        $event = self::create(array(
+            'userid' => $userdeleted,
+            'context' => \context_system::instance(),
+            'relateduserid' => $relateduserid,
+            'other' => array(
+                'messagetable' => $messagetable,
+                'messageid' => $messageid,
+                'useridfrom' => $userfromid,
+                'useridto' => $usertoid
+            )
+        ));
+
+        return $event;
+    }
+
+    /**
+     * Init method.
+     */
+    protected function init() {
+        $this->data['crud'] = 'u';
+        $this->data['edulevel'] = self::LEVEL_OTHER;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventmessagedeleted', 'message');
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        // Check if the person who deleted the message received or sent it.
+        if ($this->userid == $this->other['useridto']) {
+            $str = 'from';
+        } else {
+            $str = 'to';
+        }
+
+        return "The user with id '$this->userid' deleted a message sent $str the user with id '$this->relateduserid'.";
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+
+        if (!isset($this->other['messagetable'])) {
+            throw new \coding_exception('The \'messagetable\' value must be set in other.');
+        }
+
+        if (!isset($this->other['messageid'])) {
+            throw new \coding_exception('The \'messageid\' value must be set in other.');
+        }
+
+        if (!isset($this->other['useridfrom'])) {
+            throw new \coding_exception('The \'useridfrom\' value must be set in other.');
+        }
+
+        if (!isset($this->other['useridto'])) {
+            throw new \coding_exception('The \'useridto\' value must be set in other.');
+        }
+    }
+}
index fd98221..56e153f 100644 (file)
@@ -4572,5 +4572,17 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2015092200.00);
     }
 
+    if ($oldversion < 2015092900.00) {
+        // Rename backup_auto_keep setting to backup_auto_max_kept.
+        $keep = get_config('backup', 'backup_auto_keep');
+        if ($keep !== false) {
+            set_config('backup_auto_max_kept', $keep, 'backup');
+            unset_config('backup_auto_keep', 'backup');
+        }
+
+        // Main savepoint reached.
+        upgrade_main_savepoint(true, 2015092900.00);
+    }
+
     return true;
 }
index 96df899..ba6dcf6 100644 (file)
@@ -40,6 +40,11 @@ class mssql_native_moodle_database extends moodle_database {
     protected $mssql     = null;
     protected $last_error_reporting; // To handle mssql driver default verbosity
     protected $collation;  // current DB collation cache
+    /**
+     * Does the used db version support ANSI way of limiting (2012 and higher)
+     * @var bool
+     */
+    protected $supportsoffsetfetch;
 
     /**
      * Detects if all needed PHP stuff installed.
@@ -229,6 +234,10 @@ class mssql_native_moodle_database extends moodle_database {
 
         $this->free_result($result);
 
+        $serverinfo = $this->get_server_info();
+        // Fetch/offset is supported staring from SQL Server 2012.
+        $this->supportsoffsetfetch = $serverinfo['version'] > '11';
+
         // Connection stabilised and configured, going to instantiate the temptables controller
         $this->temptables = new mssql_native_moodle_temptables($this);
 
@@ -737,13 +746,28 @@ class mssql_native_moodle_database extends moodle_database {
         list($limitfrom, $limitnum) = $this->normalise_limit_from_num($limitfrom, $limitnum);
 
         if ($limitfrom or $limitnum) {
-            if ($limitnum >= 1) { // Only apply TOP clause if we have any limitnum (limitfrom offset is handled later)
-                $fetch = $limitfrom + $limitnum;
-                if (PHP_INT_MAX - $limitnum < $limitfrom) { // Check PHP_INT_MAX overflow
-                    $fetch = PHP_INT_MAX;
+            if (!$this->supportsoffsetfetch) {
+                if ($limitnum >= 1) { // Only apply TOP clause if we have any limitnum (limitfrom offset is handled later).
+                    $fetch = $limitfrom + $limitnum;
+                    if (PHP_INT_MAX - $limitnum < $limitfrom) { // Check PHP_INT_MAX overflow.
+                        $fetch = PHP_INT_MAX;
+                    }
+                    $sql = preg_replace('/^([\s(])*SELECT([\s]+(DISTINCT|ALL))?(?!\s*TOP\s*\()/i',
+                                        "\\1SELECT\\2 TOP $fetch", $sql);
+                }
+            } else {
+                $sql = (substr($sql, -1) === ';') ? substr($sql, 0, -1) : $sql;
+                // We need order by to use FETCH/OFFSET.
+                // Ordering by first column shouldn't break anything if there was no order in the first place.
+                if (!strpos(strtoupper($sql), "ORDER BY")) {
+                    $sql .= " ORDER BY 1";
+                }
+
+                $sql .= " OFFSET ".$limitfrom." ROWS ";
+
+                if ($limitnum > 0) {
+                    $sql .= " FETCH NEXT ".$limitnum." ROWS ONLY";
                 }
-                $sql = preg_replace('/^([\s(])*SELECT([\s]+(DISTINCT|ALL))?(?!\s*TOP\s*\()/i',
-                                    "\\1SELECT\\2 TOP $fetch", $sql);
             }
         }
 
@@ -754,7 +778,7 @@ class mssql_native_moodle_database extends moodle_database {
         $result = mssql_query($rawsql, $this->mssql);
         $this->query_end($result);
 
-        if ($limitfrom) { // Skip $limitfrom records
+        if ($limitfrom && !$this->supportsoffsetfetch) { // Skip $limitfrom records.
             if (!@mssql_data_seek($result, $limitfrom)) {
                 // Nothing, most probably seek past the end.
                 mssql_free_result($result);
index 07d110b..2543336 100644 (file)
@@ -41,6 +41,12 @@ class sqlsrv_native_moodle_database extends moodle_database {
     protected $last_error_reporting; // To handle SQL*Server-Native driver default verbosity
     protected $temptables; // Control existing temptables (sqlsrv_moodle_temptables object)
     protected $collation;  // current DB collation cache
+    /**
+     * Does the used db version support ANSI way of limiting (2012 and higher)
+     * @var bool
+     */
+    protected $supportsoffsetfetch;
+
     /** @var array list of open recordsets */
     protected $recordsets = array();
 
@@ -240,6 +246,10 @@ class sqlsrv_native_moodle_database extends moodle_database {
 
         $this->free_result($result);
 
+        $serverinfo = $this->get_server_info();
+        // Fetch/offset is supported staring from SQL Server 2012.
+        $this->supportsoffsetfetch = $serverinfo['version'] > '11';
+
         // Connection established and configured, going to instantiate the temptables controller
         $this->temptables = new sqlsrv_native_moodle_temptables($this);
 
@@ -809,20 +819,37 @@ class sqlsrv_native_moodle_database extends moodle_database {
     public function get_recordset_sql($sql, array $params = null, $limitfrom = 0, $limitnum = 0) {
 
         list($limitfrom, $limitnum) = $this->normalise_limit_from_num($limitfrom, $limitnum);
+        $needscrollable = (bool)$limitfrom; // To determine if we'll need to perform scroll to $limitfrom.
 
         if ($limitfrom or $limitnum) {
-            if ($limitnum >= 1) { // Only apply TOP clause if we have any limitnum (limitfrom offset is handled later)
-                $fetch = $limitfrom + $limitnum;
-                if (PHP_INT_MAX - $limitnum < $limitfrom) { // Check PHP_INT_MAX overflow
-                    $fetch = PHP_INT_MAX;
+            if (!$this->supportsoffsetfetch) {
+                if ($limitnum >= 1) { // Only apply TOP clause if we have any limitnum (limitfrom offset is handled later).
+                    $fetch = $limitfrom + $limitnum;
+                    if (PHP_INT_MAX - $limitnum < $limitfrom) { // Check PHP_INT_MAX overflow.
+                        $fetch = PHP_INT_MAX;
+                    }
+                    $sql = preg_replace('/^([\s(])*SELECT([\s]+(DISTINCT|ALL))?(?!\s*TOP\s*\()/i',
+                                        "\\1SELECT\\2 TOP $fetch", $sql);
+                }
+            } else {
+                $needscrollable = false; // Using supported fetch/offset, no need to scroll anymore.
+                $sql = (substr($sql, -1) === ';') ? substr($sql, 0, -1) : $sql;
+                // We need order by to use FETCH/OFFSET.
+                // Ordering by first column shouldn't break anything if there was no order in the first place.
+                if (!strpos(strtoupper($sql), "ORDER BY")) {
+                    $sql .= " ORDER BY 1";
+                }
+
+                $sql .= " OFFSET ".$limitfrom." ROWS ";
+
+                if ($limitnum > 0) {
+                    $sql .= " FETCH NEXT ".$limitnum." ROWS ONLY";
                 }
-                $sql = preg_replace('/^([\s(])*SELECT([\s]+(DISTINCT|ALL))?(?!\s*TOP\s*\()/i',
-                                    "\\1SELECT\\2 TOP $fetch", $sql);
             }
         }
-        $result = $this->do_query($sql, $params, SQL_QUERY_SELECT, false, (bool)$limitfrom);
+        $result = $this->do_query($sql, $params, SQL_QUERY_SELECT, false, $needscrollable);
 
-        if ($limitfrom) { // Skip $limitfrom records
+        if ($needscrollable) { // Skip $limitfrom records.
             sqlsrv_fetch($result, SQLSRV_SCROLL_ABSOLUTE, $limitfrom - 1);
         }
         return $this->create_recordset($result);
index 8ef6171..f5d2fea 100644 (file)
Binary files a/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-debug.js and b/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-debug.js differ
index bdcd0dc..e89ccd1 100644 (file)
Binary files a/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-min.js and b/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-min.js differ
index 239dbd9..a51598d 100644 (file)
Binary files a/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor.js and b/lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor.js differ
index 98e25d4..187c493 100644 (file)
@@ -57,6 +57,10 @@ EditorCommand.prototype = {
 
                 // Collapse selection so cursor is at end of inserted material.
                 selection.collapseToEnd();
+
+                // Save save selection and editor contents.
+                this.saveSelection();
+                this.updateOriginal();
             }, this, e, callback, context, args, anchorNode, anchorOffset));
         }
 
@@ -80,6 +84,9 @@ EditorCommand.prototype = {
         // The range is not collapsed; so apply callback method immediately.
         callback.apply(context, [e, args]);
 
+        // Save save selection and editor contents.
+        this.saveSelection();
+        this.updateOriginal();
     },
 
     /**
index dfb58d1..4795cd8 100644 (file)
@@ -87,12 +87,9 @@ EditorStyling.prototype = {
      */
     toggleInlineSelectionClass: function(toggleclasses) {
         var classname = toggleclasses.join(" ");
-        var originalSelection = this.getSelection();
         var cssApplier = rangy.createCssClassApplier(classname, {normalize: true});
 
         cssApplier.toggleSelection();
-
-        this.setSelection(originalSelection);
     },
 
     /**
@@ -100,12 +97,11 @@ EditorStyling.prototype = {
      *
      * This will set inline styles on the current selection.
      *
-     * @method toggleInlineSelectionClass
+     * @method formatSelectionInlineStyle
      * @param {Array} styles - Style attributes to set on the nodes.
      */
     formatSelectionInlineStyle: function(styles) {
         var classname = this.PLACEHOLDER_CLASS;
-        var originalSelection = this.getSelection();
         var cssApplier = rangy.createCssClassApplier(classname, {normalize: true});
 
         cssApplier.applyToSelection();
@@ -114,7 +110,6 @@ EditorStyling.prototype = {
             node.removeClass(classname).setStyles(styles);
         }, this);
 
-        this.setSelection(originalSelection);
     },
 
     /**
index 67a1cfd..593cafa 100644 (file)
@@ -1692,7 +1692,11 @@ abstract class enrol_plugin {
             $instance->$field = $value;
         }
 
-        return $DB->insert_record('enrol', $instance);
+        $instance->id = $DB->insert_record('enrol', $instance);
+
+        \core\event\enrol_instance_created::create_from_record($instance)->trigger();
+
+        return $instance->id;
     }
 
     /**
@@ -1723,8 +1727,10 @@ abstract class enrol_plugin {
         $instance->status = $newstatus;
         $DB->update_record('enrol', $instance);
 
-        // invalidate all enrol caches
         $context = context_course::instance($instance->courseid);
+        \core\event\enrol_instance_updated::create_from_record($instance)->trigger();
+
+        // Invalidate all enrol caches.
         $context->mark_dirty();
     }
 
@@ -1756,8 +1762,10 @@ abstract class enrol_plugin {
         // finally drop the enrol row
         $DB->delete_records('enrol', array('id'=>$instance->id));
 
-        // invalidate all enrol caches
         $context = context_course::instance($instance->courseid);
+        \core\event\enrol_instance_deleted::create_from_record($instance)->trigger();
+
+        // Invalidate all enrol caches.
         $context->mark_dirty();
     }
 
index dfc2005..8c77cdc 100644 (file)
@@ -466,7 +466,7 @@ class grade_category extends grade_object {
                      WHERE id $usql";
             $items = $DB->get_records_sql($sql, $params);
             foreach ($items as $id => $item) {
-                $items[$id] = new grade_item($item);
+                $items[$id] = new grade_item($item, false);
             }
         }
 
@@ -501,10 +501,13 @@ class grade_category extends grade_object {
             $grademinoverrides = array();
 
             foreach ($rs as $used) {
-                $grade = new grade_grade($used);
+                $grade = new grade_grade($used, false);
                 if (isset($items[$grade->itemid])) {
                     // Prevent grade item to be fetched from DB.
                     $grade->grade_item =& $items[$grade->itemid];
+                } else if ($grade->itemid == $this->grade_item->id) {
+                    // This grade's grade item is not in $items.
+                    $grade->grade_item =& $this->grade_item;
                 }
                 if ($grade->userid != $prevuser) {
                     $this->aggregate_grades($prevuser,
@@ -882,7 +885,7 @@ class grade_category extends grade_object {
                                                        & $weights = null,
                                                        $grademinoverrides = array(),
                                                        $grademaxoverrides = array()) {
-        $category_item = $this->get_grade_item();
+        $category_item = $this->load_grade_item();
         $grademin = $category_item->grademin;
         $grademax = $category_item->grademax;
 
index d470795..6bb860e 100644 (file)
@@ -694,7 +694,7 @@ class grade_item extends grade_object {
         // aggregate the category grade
         } else if ($this->is_category_item() or $this->is_course_item()) {
             // aggregate category grade item
-            $category = $this->get_item_category();
+            $category = $this->load_item_category();
             $category->grade_item =& $this;
             if ($category->generate_grades($userid)) {
                 return true;
index 233c34d..c85b826 100644 (file)
@@ -256,6 +256,10 @@ function install_generate_configphp($database, $cfg) {
     }
     $configphp .= '$CFG->directorypermissions = ' . $chmod . ';' . PHP_EOL . PHP_EOL;
 
+    if (isset($cfg->upgradekey) and $cfg->upgradekey !== '') {
+        $configphp .= '$CFG->upgradekey = ' . var_export($cfg->upgradekey, true) . ';' . PHP_EOL . PHP_EOL;
+    }
+
     $configphp .= 'require_once(dirname(__FILE__) . \'/lib/setup.php\');' . PHP_EOL . PHP_EOL;
     $configphp .= '// There is no php closing tag in this file,' . PHP_EOL;
     $configphp .= '// it is intentional because it prevents trailing whitespace problems!' . PHP_EOL;
index 435dec2..97abc70 100644 (file)
@@ -3987,6 +3987,7 @@ EOD;
                         'page' => $this->page
                     )
                 );
+                $this->page->requires->string_for_js('changesmadereallygoaway', 'moodle');
             }
         }
 
index 1acdba7..8995f76 100644 (file)
@@ -83,14 +83,14 @@ abstract class advanced_testcase extends base_testcase {
 
             // Deal with any debugging messages.
             $debugerror = phpunit_util::display_debugging_messages();
-            phpunit_util::reset_debugging();
+            $this->resetDebugging();
             if ($debugerror) {
                 trigger_error('Unenxpected debugging() call detected.', E_USER_NOTICE);
             }
 
         } catch (Exception $e) {
             // cleanup after failed expectation
-            phpunit_util::reset_all_data();
+            self::resetAllData();
             throw $e;
         }
 
@@ -104,7 +104,7 @@ abstract class advanced_testcase extends base_testcase {
                 phpunit_util::reset_all_database_sequences();
                 phpunit_util::$lastdbwrites = $DB->perf_get_writes(); // no db reset necessary
             }
-            phpunit_util::reset_all_data(null);
+            self::resetAllData(null);
 
         } else if ($this->resetAfterTest === false) {
             if ($this->testdbtransaction) {
@@ -118,16 +118,16 @@ abstract class advanced_testcase extends base_testcase {
                 try {
                     $this->testdbtransaction->allow_commit();
                 } catch (dml_transaction_exception $e) {
-                    phpunit_util::reset_all_data();
+                    self::resetAllData();
                     throw new coding_exception('Invalid transaction state detected in test '.$this->getName());
                 }
             }
-            phpunit_util::reset_all_data(true);
+            self::resetAllData(true);
         }
 
         // make sure test did not forget to close transaction
         if ($DB->is_transaction_started()) {
-            phpunit_util::reset_all_data();
+            self::resetAllData();
             if ($this->getStatus() == PHPUnit_Runner_BaseTestRunner::STATUS_PASSED
                 or $this->getStatus() == PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED
                 or $this->getStatus() == PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE) {
@@ -272,7 +272,7 @@ abstract class advanced_testcase extends base_testcase {
      * @param string $message
      */
     public function assertDebuggingCalled($debugmessage = null, $debuglevel = null, $message = '') {
-        $debugging = phpunit_util::get_debugging_messages();
+        $debugging = $this->getDebuggingMessages();
         $count = count($debugging);
 
         if ($count == 0) {
@@ -297,7 +297,7 @@ abstract class advanced_testcase extends base_testcase {
             $this->assertSame($debuglevel, $debug->level, $message);
         }
 
-        phpunit_util::reset_debugging();
+        $this->resetDebugging();
     }
 
     /**
@@ -305,7 +305,7 @@ abstract class advanced_testcase extends base_testcase {
      * @param string $message
      */
     public function assertDebuggingNotCalled($message = '') {
-        $debugging = phpunit_util::get_debugging_messages();
+        $debugging = $this->getDebuggingMessages();
         $count = count($debugging);
 
         if ($message === '') {
@@ -446,16 +446,21 @@ abstract class advanced_testcase extends base_testcase {
      * @return void
      */
     public static function tearDownAfterClass() {
-        phpunit_util::reset_all_data();
+        self::resetAllData();
     }
 
+
     /**
      * Reset all database tables, restore global state and clear caches and optionally purge dataroot dir.
-     * @static
+     *
+     * @param bool $detectchanges
+     *      true  - changes in global state and database are reported as errors
+     *      false - no errors reported
+     *      null  - only critical problems are reported as errors
      * @return void
      */
-    public static function resetAllData() {
-        phpunit_util::reset_all_data();
+    public static function resetAllData($detectchanges = false) {
+        phpunit_util::reset_all_data($detectchanges);
     }
 
     /**
index f186abc..eaacaa2 100644 (file)
@@ -45,7 +45,7 @@
  * @copyright  2012 Petr Skoda {@link http://skodak.org}
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
-abstract class database_driver_testcase extends PHPUnit_Framework_TestCase {
+abstract class database_driver_testcase extends base_testcase {
     /** @var moodle_database connection to extra database */
     private static $extradb = null;
 
@@ -176,7 +176,7 @@ abstract class database_driver_testcase extends PHPUnit_Framework_TestCase {
      * @param string $message
      */
     public function assertDebuggingCalled($debugmessage = null, $debuglevel = null, $message = '') {
-        $debugging = phpunit_util::get_debugging_messages();
+        $debugging = $this->getDebuggingMessages();
         $count = count($debugging);
 
         if ($count == 0) {
@@ -201,7 +201,7 @@ abstract class database_driver_testcase extends PHPUnit_Framework_TestCase {
             $this->assertSame($debuglevel, $debug->level, $message);
         }
 
-        phpunit_util::reset_debugging();
+        $this->resetDebugging();
     }
 
     /**
@@ -209,7 +209,7 @@ abstract class database_driver_testcase extends PHPUnit_Framework_TestCase {
      * @param string $message
      */
     public function assertDebuggingNotCalled($message = '') {
-        $debugging = phpunit_util::get_debugging_messages();
+        $debugging = $this->getDebuggingMessages();
         $count = count($debugging);
 
         if ($message === '') {
index 8114893..cc1c64a 100644 (file)
@@ -104,16 +104,16 @@ class phpunit_util extends testing_util {
         global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION;
 
         // Stop any message redirection.
-        phpunit_util::stop_message_redirection();
+        self::stop_message_redirection();
 
         // Stop any message redirection.
-        phpunit_util::stop_event_redirection();
+        self::stop_event_redirection();
 
         // Start a new email redirection.
         // This will clear any existing phpmailer redirection.
         // We redirect all phpmailer output to this message sink which is
         // called instead of phpmailer actually sending the message.
-        phpunit_util::start_phpmailer_redirection();
+        self::start_phpmailer_redirection();
 
         // We used to call gc_collect_cycles here to ensure desctructors were called between tests.
         // This accounted for 25% of the total time running phpunit - so we removed it.
@@ -320,7 +320,7 @@ class phpunit_util extends testing_util {
         self::$globals['DB'] = $DB;
 
         // refresh data in all tables, clear caches, etc.
-        phpunit_util::reset_all_data();
+        self::reset_all_data();
     }
 
     /**
@@ -427,7 +427,7 @@ class phpunit_util extends testing_util {
         }
 
         if ($DB->get_tables()) {
-            list($errorcode, $message) = phpunit_util::testing_ready_problem();
+            list($errorcode, $message) = self::testing_ready_problem();
             if ($errorcode) {
                 phpunit_bootstrap_error(PHPUNIT_EXITCODE_REINSTALL, 'Database tables already present, Moodle PHPUnit test environment can not be initialised');
             } else {
index 1bd4d58..cc2d8e8 100644 (file)
@@ -198,13 +198,13 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
         global $DB, $CFG, $COURSE, $SITE, $USER;
 
         $this->preventResetByRollback();
-        phpunit_util::reset_all_data(true);
+        self::resetAllData(true);
 
         // Database change.
         $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2)));
         $DB->set_field('user', 'confirmed', 0, array('id'=>2));
         try {
-            phpunit_util::reset_all_data(true);
+            self::resetAllData(true);
         } catch (Exception $e) {
             $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
         }
@@ -215,7 +215,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
         unset($CFG->admin);
         $CFG->rolesactive = 0;
         try {
-            phpunit_util::reset_all_data(true);
+            self::resetAllData(true);
         } catch (Exception $e) {
             $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
             $this->assertContains('xx', $e->getMessage());
@@ -228,28 +228,28 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
 
         // _GET change.
         $_GET['__somethingthatwillnotnormallybepresent__'] = 'yy';
-        phpunit_util::reset_all_data(true);
+        self::resetAllData(true);
 
         $this->assertEquals(array(), $_GET);
 
         // _POST change.
         $_POST['__somethingthatwillnotnormallybepresent2__'] = 'yy';
-        phpunit_util::reset_all_data(true);
+        self::resetAllData(true);
         $this->assertEquals(array(), $_POST);
 
         // _FILES change.
         $_FILES['__somethingthatwillnotnormallybepresent3__'] = 'yy';
-        phpunit_util::reset_all_data(true);
+        self::resetAllData(true);
         $this->assertEquals(array(), $_FILES);
 
         // _REQUEST change.
         $_REQUEST['__somethingthatwillnotnormallybepresent4__'] = 'yy';
-        phpunit_util::reset_all_data(true);
+        self::resetAllData(true);
         $this->assertEquals(array(), $_REQUEST);
 
         // Silent changes.
         $_SERVER['xx'] = 'yy';
-        phpunit_util::reset_all_data(true);
+        self::resetAllData(true);
         $this->assertFalse(isset($_SERVER['xx']));
 
         // COURSE change.
@@ -257,7 +257,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
         $COURSE = new stdClass();
         $COURSE->id = 7;
         try {
-            phpunit_util::reset_all_data(true);
+            self::resetAllData(true);
         } catch (Exception $e) {
             $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
             $this->assertEquals(1, $SITE->id);
@@ -268,7 +268,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
         // USER change.
         $this->setUser(2);
         try {
-            phpunit_util::reset_all_data(true);
+            self::resetAllData(true);
         } catch (Exception $e) {
             $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
             $this->assertEquals(0, $USER->id);
@@ -601,7 +601,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
 
         $this->resetAfterTest();
 
-        // If this fails \phpunit_util::reset_all_data() must be updated.
+        // If this fails self::resetAllData(); must be updated.
         $this->assertSame('en_AU.UTF-8', get_string('locale', 'langconfig'));
         $this->assertSame('English_Australia.1252', get_string('localewin', 'langconfig'));
 
@@ -614,7 +614,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
         }
 
         try {
-            phpunit_util::reset_all_data(true);
+            self::resetAllData(true);
         } catch (Exception $e) {
             $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
         }
@@ -633,7 +633,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
             setlocale(LC_TIME, 'en_US.UTF-8');
         }
 
-        phpunit_util::reset_all_data(false);
+        self::resetAllData(false);
 
         if ($CFG->ostype === 'WINDOWS') {
             $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
index 9d86d0c..ecc3dc5 100644 (file)
@@ -51,7 +51,6 @@ class core_messagelib_testcase extends advanced_testcase {
 
         // Check message is not sent.
         $sink = $this->redirectEmails();
-        $this->assertTrue(phpunit_util::is_redirecting_phpmailer());
         message_send($message);
         $emails = $sink->get_messages();
         $this->assertEmpty($emails);
@@ -62,7 +61,6 @@ class core_messagelib_testcase extends advanced_testcase {
         $this->assertTrue($preferences->$disableprovidersetting == 0);
 
         $sink = $this->redirectEmails();
-        $this->assertTrue(phpunit_util::is_redirecting_phpmailer());
         message_send($message);
         $emails = $sink->get_messages();
         $email = reset($emails);
@@ -853,7 +851,6 @@ class core_messagelib_testcase extends advanced_testcase {
 
         // Make sure we are redirecting emails.
         $sink = $this->redirectEmails();
-        $this->assertTrue(phpunit_util::is_redirecting_phpmailer());
         message_send($message);
 
         // Get the email that we just sent.
index ff138b2..f26e966 100644 (file)
@@ -2342,3 +2342,32 @@ function upgrade_minmaxgrade() {
     }
     $rs->close();
 }
+
+
+/**
+ * Assert the upgrade key is provided, if it is defined.
+ *
+ * The upgrade key can be defined in the main config.php as $CFG->upgradekey. If
+ * it is defined there, then its value must be provided every time the site is
+ * being upgraded, regardless the administrator is logged in or not.
+ *
+ * This is supposed to be used at certain places in /admin/index.php only.
+ *
+ * @param string|null $upgradekeyhash the SHA-1 of the value provided by the user
+ */
+function check_upgrade_key($upgradekeyhash) {
+    global $CFG, $PAGE;
+
+    if (isset($CFG->config_php_settings['upgradekey'])) {
+        if ($upgradekeyhash === null or $upgradekeyhash !== sha1($CFG->config_php_settings['upgradekey'])) {
+            if (!$PAGE->headerprinted) {
+                $output = $PAGE->get_renderer('core', 'admin');
+                echo $output->upgradekey_form_page(new moodle_url('/admin/index.php', array('cache' => 0)));
+                die();
+            } else {
+                // This should not happen.
+                die('Upgrade locked');
+            }
+        }
+    }
+}
index 868751f..6fbee86 100644 (file)
Binary files a/lib/yui/build/moodle-core-formchangechecker/moodle-core-formchangechecker-debug.js and b/lib/yui/build/moodle-core-formchangechecker/moodle-core-formchangechecker-debug.js differ
index 505dbe8..af2f3e2 100644 (file)
Binary files a/lib/yui/build/moodle-core-formchangechecker/moodle-core-formchangechecker-min.js and b/lib/yui/build/moodle-core-formchangechecker/moodle-core-formchangechecker-min.js differ
index 868751f..6fbee86 100644 (file)
Binary files a/lib/yui/build/moodle-core-formchangechecker/moodle-core-formchangechecker.js and b/lib/yui/build/moodle-core-formchangechecker/moodle-core-formchangechecker.js differ
index f8a57da..384dfcf 100644 (file)
Binary files a/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-debug.js and b/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-debug.js differ
index 32433e4..4fa7dd8 100644 (file)
Binary files a/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-min.js and b/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-min.js differ
index 3ed1ac0..78eb3ae 100644 (file)
Binary files a/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue.js and b/lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue.js differ
index ae0d809..db1023f 100644 (file)
@@ -176,6 +176,14 @@ M.core_formchangechecker.get_form_dirty_state = function() {
     return 0;
 };
 
+/*
+ * Reset the form state
+ */
+M.core_formchangechecker.reset_form_dirty_state = function() {
+    M.core_formchangechecker.stateinformation.formsubmitted = false;
+    M.core_formchangechecker.stateinformation.formchanged = false;
+};
+
 /*
  * Return a suitable message if changes have been made to a form
  */
index 0ff4f8b..ffede8a 100644 (file)
@@ -97,6 +97,9 @@ Y.extend(DIALOGUE, Y.Panel, {
         }
 
         if (this.get('modal')) {
+            // If we're a modal then make sure our container is ARIA
+            // hidden by default. ARIA visibility is managed for modal dialogues.
+            this.get(BASE).set('aria-hidden', 'true');
             this.plug(Y.M.core.LockScroll);
         }
 
@@ -335,6 +338,13 @@ Y.extend(DIALOGUE, Y.Panel, {
             this.lockScroll.enableScrollLock(this.shouldResizeFullscreen());
         }
 
+        // Only do accessibility hiding for modals because the ARIA spec
+        // says that all ARIA dialogues should be modal.
+        if (this.get('modal')) {
+            // Make this dialogue visible to screen readers.
+            this.setAccessibilityVisible();
+        }
+
         // Try and find a node to focus on using the focusOnShowSelector attribute.
         if (focusSelector !== null) {
             focusNode = this.get('boundingBox').one(focusSelector);
@@ -362,6 +372,11 @@ Y.extend(DIALOGUE, Y.Panel, {
             }
         }
 
+        if (this.get('modal')) {
+            // Hide this dialogue from screen readers.
+            this.setAccessibilityHidden();
+        }
+
         // Unlock scroll if the plugin is present.
         if (this.lockScroll) {
             this.lockScroll.disableScrollLock();
@@ -406,6 +421,73 @@ Y.extend(DIALOGUE, Y.Panel, {
         } else if (target === firstitem && direction === 'backward') {  // Tab+shift key.
             return lastitem.focus();
         }
+    },
+
+    /**
+     * Sets the appropriate aria attributes on this dialogue and the other
+     * elements in the DOM to ensure that screen readers are able to navigate
+     * the dialogue popup correctly.
+     *
+     * @method setAccessibilityVisible
+     */
+    setAccessibilityVisible: function() {
+        // Get the element that contains this dialogue because we need it
+        // to filter out from the document.body child elements.
+        var container = this.get(BASE);
+        // Keep a record of any elements we change so that they can be reverted later.
+        this.hiddenSiblings = [];
+
+        // We need to get a list containing each sibling element and the shallowest
+        // non-ancestral nodes in the DOM. We can shortcut this a little by leveraging
+        // the fact that this dialogue is always appended to the document body therefore
+        // it's siblings are the shallowest non-ancestral nodes. If that changes then
+        // this code should also be updated.
+        Y.one(document.body).get('children').each(function(node) {
+            // Skip the element that contains us.
+            if (node !== container) {
+                var hidden = node.get('aria-hidden');
+                // If they are already hidden we can ignore them.
+                if (hidden !== 'true') {
+                    // Save their current state.
+                    node.setData('previous-aria-hidden', hidden);
+                    this.hiddenSiblings.push(node);
+
+                    // Hide this node from screen readers.
+                    node.set('aria-hidden', 'true');
+                }
+            }
+        }, this);
+
+        // Make us visible to screen readers.
+        container.set('aria-hidden', 'false');
+    },
+
+    /**
+     * Restores the aria visibility on the DOM elements changed when displaying
+     * the dialogue popup and makes the dialogue aria hidden to allow screen
+     * readers to navigate the main page correctly when the dialogue is closed.
+     *
+     * @method setAccessibilityHidden
+     */
+    setAccessibilityHidden: function() {
+        var container = this.get(BASE);
+        container.set('aria-hidden', 'true');
+
+        // Restore the sibling nodes back to their original values.
+        Y.Array.each(this.hiddenSiblings, function(node) {
+            var previousValue = node.getData('previous-aria-hidden');
+            // If the element didn't previously have an aria-hidden attribute
+            // then we can just remove the one we set.
+            if (previousValue === null) {
+                node.removeAttribute('aria-hidden');
+            } else {
+                // Otherwise set it back to the old value (which will be false).
+                node.set('aria-hidden', previousValue);
+            }
+        });
+
+        // Clear the cache. No longer need to store these.
+        this.hiddenSiblings = [];
     }
 }, {
     NAME : DIALOGUE_NAME,
index f7a7676..09c834f 100644 (file)
@@ -1214,7 +1214,15 @@ function message_delete_message($message, $userid) {
     $updatemessage = new stdClass();
     $updatemessage->id = $message->id;
     $updatemessage->$coltimedeleted = time();
-    return $DB->update_record($messagetable, $updatemessage);
+    $success = $DB->update_record($messagetable, $updatemessage);
+
+    if ($success) {
+        // Trigger event for deleting a message.
+        \core\event\message_deleted::create_from_ids($message->useridfrom, $message->useridto,
+            $userid, $messagetable, $message->id)->trigger();
+    }
+
+    return $success;
 }
 
 /**
index 0a8699e..cd92c44 100644 (file)
@@ -219,4 +219,62 @@ class core_message_events_testcase extends advanced_testcase {
         $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
         $this->assertEquals($url, $event->get_url());
     }
+
+    /**
+     * Test the message deleted event.
+     */
+    public function test_message_deleted() {
+        global $DB;
+
+        // Create a message.
+        $message = new stdClass();
+        $message->useridfrom = '1';
+        $message->useridto = '2';
+        $message->subject = 'Subject';
+        $message->message = 'Message';
+        $message->timeuserfromdeleted = 0;
+        $message->timeusertodeleted = 0;
+        $message->id = $DB->insert_record('message', $message);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        message_delete_message($message, $message->useridfrom);
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\message_deleted', $event);
+        $this->assertEquals($message->useridfrom, $event->userid); // The user who deleted it.
+        $this->assertEquals($message->useridto, $event->relateduserid);
+        $this->assertEquals('message', $event->other['messagetable']);
+        $this->assertEquals($message->id, $event->other['messageid']);
+        $this->assertEquals($message->useridfrom, $event->other['useridfrom']);
+        $this->assertEquals($message->useridto, $event->other['useridto']);
+
+        // Create a read message.
+        $message = new stdClass();
+        $message->useridfrom = '2';
+        $message->useridto = '1';
+        $message->subject = 'Subject';
+        $message->message = 'Message';
+        $message->timeuserfromdeleted = 0;
+        $message->timeusertodeleted = 0;
+        $message->timeread = time();
+        $message->id = $DB->insert_record('message_read', $message);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        message_delete_message($message, $message->useridto);
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\message_deleted', $event);
+        $this->assertEquals($message->useridto, $event->userid);
+        $this->assertEquals($message->useridfrom, $event->relateduserid);
+        $this->assertEquals('message_read', $event->other['messagetable']);
+        $this->assertEquals($message->id, $event->other['messageid']);
+        $this->assertEquals($message->useridfrom, $event->other['useridfrom']);
+        $this->assertEquals($message->useridto, $event->other['useridto']);
+    }
 }
index 1c604a8..3d73d5d 100644 (file)
Binary files a/message/yui/build/moodle-core_message-messenger/moodle-core_message-messenger-debug.js and b/message/yui/build/moodle-core_message-messenger/moodle-core_message-messenger-debug.js differ
index 82fe415..8f60a30 100644 (file)
Binary files a/message/yui/build/moodle-core_message-messenger/moodle-core_message-messenger-min.js and b/message/yui/build/moodle-core_message-messenger/moodle-core_message-messenger-min.js differ
index 1c604a8..3d73d5d 100644 (file)
Binary files a/message/yui/build/moodle-core_message-messenger/moodle-core_message-messenger.js and b/message/yui/build/moodle-core_message-messenger/moodle-core_message-messenger.js differ
index 51d81eb..8998638 100644 (file)
@@ -60,7 +60,7 @@ Y.namespace('M.core_message.messenger').sendMessage = Y.extend(SENDMSGDIALOG, M.
 
     _bb: null,
     _sendLock: false,
-
+    _hide: null,
     /**
      * Initializer.
      *
@@ -71,10 +71,11 @@ Y.namespace('M.core_message.messenger').sendMessage = Y.extend(SENDMSGDIALOG, M.
             content;
 
         this._bb = this.get('boundingBox');
+        this._hide = this.hide;
 
         // Prepare the content area.
         tpl = Y.Handlebars.compile(
-            '<form action="#">' +
+            '<form action="#" id="messageform">' +
                 '<div class="{{CSSR.INPUTAREA}}">' +
                     '<label class="{{CSSR.ACCESSHIDE}}" for="{{id}}">{{labelStr}}</label>' +
                     '<textarea class="{{CSSR.INPUT}}" id="{{id}}"></textarea>' +
@@ -131,6 +132,11 @@ Y.namespace('M.core_message.messenger').sendMessage = Y.extend(SENDMSGDIALOG, M.
 
         // Set the content as empty and lock send.
         this._bb.one(SELECTORS.SENDMSGDIALOG.INPUT).set('value', '');
+
+        // Register form with formchangechecker
+        Y.use('moodle-core-formchangechecker', function() {
+            M.core_formchangechecker.init({formid: "messageform"});
+        });
     },
 
     /**
@@ -204,6 +210,33 @@ Y.namespace('M.core_message.messenger').sendMessage = Y.extend(SENDMSGDIALOG, M.
         });
     },
 
+    /**
+     * Override the default hide function.
+     * @method hide
+     */
+    hide: function() {
+        var self = this;
+
+        if (!M.core_formchangechecker.get_form_dirty_state()) {
+            return SENDMSGDIALOG.superclass.hide.call(this, arguments);
+        }
+
+        Y.use('moodle-core-notification-confirm', function() {
+            var confirm = new M.core.confirm({
+                title : M.util.get_string('confirm', 'moodle'),
+                question : M.util.get_string('changesmadereallygoaway', 'moodle'),
+                yesLabel : M.util.get_string('confirm', 'moodle'),
+                noLabel : M.util.get_string('cancel', 'moodle')
+            });
+            confirm.on('complete-yes', function() {
+                M.core_formchangechecker.reset_form_dirty_state();
+                confirm.hide();
+                confirm.destroy();
+                return SENDMSGDIALOG.superclass.hide.call(this, arguments);
+            }, self);
+        });
+    },
+
     /**
      * Show a notice.
      *
index 61d1e98..3a1f85a 100644 (file)
@@ -87,6 +87,55 @@ function assign_reset_userdata($data) {
     return $status;
 }
 
+/**
+ * This standard function will check all instances of this module
+ * and make sure there are up-to-date events created for each of them.
+ * If courseid = 0, then every assignment event in the site is checked, else
+ * only assignment events belonging to the course specified are checked.
+ *
+ * @param int $courseid
+ * @return bool
+ */
+function assign_refresh_events($courseid = 0) {
+    global $CFG, $DB;
+    require_once($CFG->dirroot . '/mod/assign/locallib.php');
+
+    if ($courseid) {
+        // Make sure that the course id is numeric.
+        if (!is_numeric($courseid)) {
+            return false;
+        }
+        if (!$assigns = $DB->get_records('assign', array('course' => $courseid))) {
+            return false;
+        }
+        // Get course from courseid parameter.
+        if (!$course = $DB->get_record('course', array('id' => $courseid), '*')) {
+            return false;
+        }
+    } else {
+        if (!$assigns = $DB->get_records('assign')) {
+            return false;
+        }
+    }
+    foreach ($assigns as $assign) {
+        // Use assignment's course column if courseid parameter is not given.
+        if (!$courseid) {
+            $courseid = $assign->course;
+            if (!$course = $DB->get_record('course', array('id' => $courseid), '*')) {
+                continue;
+            }
+        }
+        if (!$cm = get_coursemodule_from_instance('assign', $assign->id, $courseid, false)) {
+            continue;
+        }
+        $context = context_module::instance($cm->id);
+        $assignment = new assign($context, $cm, $course);
+        $assignment->update_calendar($cm->id);
+    }
+
+    return true;
+}
+
 /**
  * Removes all grades from gradebook
  *
index 73336ae..66cabaf 100644 (file)
@@ -327,4 +327,43 @@ class mod_assign_lib_testcase extends mod_assign_base_testcase {
         $this->assertTrue($result);
     }
 
+    /**
+     * Tests for mod_assign_refresh_events.
+     */
+    public function test_assign_refresh_events() {
+        global $DB;
+        $duedate = time();
+        $this->setAdminUser();
+
+        $assign = $this->create_instance(array('duedate' => $duedate));
+
+        // Normal case, with existing course.
+        $this->assertTrue(assign_refresh_events($this->course->id));
+
+        $instance = $assign->get_instance();
+        $eventparams = array('modulename' => 'assign', 'instance' => $instance->id);
+        $event = $DB->get_record('event', $eventparams, '*', MUST_EXIST);
+        $this->assertEquals($event->timestart, $duedate);
+
+        // In case the course ID is passed as a numeric string.
+        $this->assertTrue(assign_refresh_events('' . $this->course->id));
+
+        // Course ID not provided.
+        $this->assertTrue(assign_refresh_events());
+
+        $eventparams = array('modulename' => 'assign');
+        $events = $DB->get_records('event', $eventparams);
+        foreach ($events as $event) {
+            if ($event->modulename === 'assign' && $event->instance === $instance->id) {
+                $this->assertEquals($event->timestart, $duedate);
+            }
+        }
+
+        // Non-existing course ID.
+        $this->assertFalse(assign_refresh_events(-1));
+
+        // Invalid course ID.
+        $this->assertFalse(assign_refresh_events('aaa'));
+    }
+
 }
index d5fed0b..0fd6e77 100644 (file)
@@ -266,7 +266,7 @@ if (!$canreply and $forum->type !== 'news') {
 }
 
 // Output the links to neighbour discussions.
-$neighbours = forum_get_discussion_neighbours($cm, $discussion);
+$neighbours = forum_get_discussion_neighbours($cm, $discussion, $forum);
 $neighbourlinks = $renderer->neighbouring_discussion_navigation($neighbours['prev'], $neighbours['next']);
 echo $neighbourlinks;
 
index e5f2e4c..e7e7da0 100644 (file)
@@ -483,7 +483,9 @@ $string['subscriptionauto'] = 'Auto subscription';
 $string['subscriptiondisabled'] = 'Subscription disabled';
 $string['subscriptions'] = 'Subscriptions';
 $string['thisforumisthrottled'] = 'This forum has a limit to the number of forum postings you can make in a given time period - this is currently set at {$a->blockafter} posting(s) in {$a->blockperiod}';
+$string['timedhidden'] = 'Timed status: Hidden from students';
 $string['timedposts'] = 'Timed posts';
+$string['timedvisible'] = 'Timed status: Visible to all users';
 $string['timestartenderror'] = 'Display end date cannot be earlier than the start date';
 $string['trackforum'] = 'Track unread posts';
 $string['tracking'] = 'Track';
index 42f6db3..20d459d 100644 (file)
@@ -2704,20 +2704,24 @@ function forum_get_discussions($cm, $forumsort="d.timemodified DESC", $fullpost=
  * other mean to sort the records, e.g. we cannot use IDs as a greater ID can have a lower
  * timemodified.
  *
+ * For blog-style forums, the calculation is based on the original creation time of the
+ * blog post.
+ *
  * Please note that this does not check whether or not the discussion passed is accessible
  * by the user, it simply uses it as a reference to find the neighbours. On the other hand,
  * the returned neighbours are checked and are accessible to the current user.
  *
  * @param object $cm The CM record.
  * @param object $discussion The discussion record.
+ * @param object $forum The forum instance record.
  * @return array That always contains the keys 'prev' and 'next'. When there is a result
  *               they contain the record with minimal information such as 'id' and 'name'.
  *               When the neighbour is not found the value is false.
  */
-function forum_get_discussion_neighbours($cm, $discussion) {
+function forum_get_discussion_neighbours($cm, $discussion, $forum) {
     global $CFG, $DB, $USER;
 
-    if ($cm->instance != $discussion->forum) {
+    if ($cm->instance != $discussion->forum or $discussion->forum != $forum->id or $forum->id != $cm->instance) {
         throw new coding_exception('Discussion is not part of the same forum.');
     }
 
@@ -2762,25 +2766,54 @@ function forum_get_discussion_neighbours($cm, $discussion) {
         }
     }
 
-    $params['forumid'] = $cm->instance;
-    $params['discid'] = $discussion->id;
-    $params['disctimemodified'] = $discussion->timemodified;
+    if ($forum->type === 'blog') {
+        $params['forumid'] = $cm->instance;
+        $params['discid1'] = $discussion->id;
+        $params['discid2'] = $discussion->id;
 
-    $sql = "SELECT d.id, d.name, d.timemodified, d.groupid, d.timestart, d.timeend
-              FROM {forum_discussions} d
-             WHERE d.forum = :forumid
-               AND d.id <> :discid
-                   $timelimit
-                   $groupselect";
+        $sql = "SELECT d.id, d.name, d.timemodified, d.groupid, d.timestart, d.timeend
+                  FROM {forum_discussions} d
+                  JOIN {forum_posts} p ON d.firstpost = p.id
+                 WHERE d.forum = :forumid
+                   AND d.id <> :discid1
+                       $timelimit
+                       $groupselect";
+
+        $sub = "SELECT pp.created
+                  FROM {forum_discussions} dd
+                  JOIN {forum_posts} pp ON dd.firstpost = pp.id
+                 WHERE dd.id = :discid2";
+
+        $prevsql = $sql . " AND p.created < ($sub)
+                       ORDER BY p.created DESC";
+
+        $nextsql = $sql . " AND p.created > ($sub)
+                       ORDER BY p.created ASC";
+
+        $neighbours['prev'] = $DB->get_record_sql($prevsql, $params, IGNORE_MULTIPLE);
+        $neighbours['next'] = $DB->get_record_sql($nextsql, $params, IGNORE_MULTIPLE);
+
+    } else {
+        $params['forumid'] = $cm->instance;
+        $params['discid'] = $discussion->id;
+        $params['disctimemodified'] = $discussion->timemodified;
 
-    $prevsql = $sql . " AND d.timemodified < :disctimemodified
-                   ORDER BY d.timemodified DESC";
+        $sql = "SELECT d.id, d.name, d.timemodified, d.groupid, d.timestart, d.timeend
+                  FROM {forum_discussions} d
+                 WHERE d.forum = :forumid
+                   AND d.id <> :discid
+                       $timelimit
+                       $groupselect";
 
-    $nextsql = $sql . " AND d.timemodified > :disctimemodified
-                   ORDER BY d.timemodified ASC";
+        $prevsql = $sql . " AND d.timemodified < :disctimemodified
+                       ORDER BY d.timemodified DESC";
 
-    $neighbours['prev'] = $DB->get_record_sql($prevsql, $params, IGNORE_MULTIPLE);
-    $neighbours['next'] = $DB->get_record_sql($nextsql, $params, IGNORE_MULTIPLE);
+        $nextsql = $sql . " AND d.timemodified > :disctimemodified
+                       ORDER BY d.timemodified ASC";
+
+        $neighbours['prev'] = $DB->get_record_sql($prevsql, $params, IGNORE_MULTIPLE);
+        $neighbours['next'] = $DB->get_record_sql($nextsql, $params, IGNORE_MULTIPLE);
+    }
 
     return $neighbours;
 }
@@ -3722,11 +3755,13 @@ function mod_forum_rating_can_see_item_ratings($params) {
  * @param boolean $cantrack Is tracking enabled for this forum.
  * @param boolean $forumtracked Is the user tracking this forum.
  * @param boolean $canviewparticipants True if user has the viewparticipants permission for this course
+ * @param boolean $canviewhiddentimedposts True if user has the viewhiddentimedposts permission for this forum
  */
-function forum_print_discussion_header(&$post, $forum, $group=-1, $datestring="",
-                                        $cantrack=true, $forumtracked=true, $canviewparticipants=true, $modcontext=NULL) {
+function forum_print_discussion_header(&$post, $forum, $group = -1, $datestring = "",
+                                        $cantrack = true, $forumtracked = true, $canviewparticipants = true, $modcontext = null,
+                                        $canviewhiddentimedposts = false) {
 
-    global $COURSE, $USER, $CFG, $OUTPUT;
+    global $COURSE, $USER, $CFG, $OUTPUT, $PAGE;
 
     static $rowcount;
     static $strmarkalldread;
@@ -3747,11 +3782,23 @@ function forum_print_discussion_header(&$post, $forum, $group=-1, $datestring=""
 
     $post->subject = format_string($post->subject,true);
 
+    $timeddiscussion = !empty($CFG->forum_enabletimedposts) && ($post->timestart || $post->timeend);
+    $timedoutsidewindow = '';
+    if ($timeddiscussion && ($post->timestart > time() || ($post->timeend != 0 && $post->timeend < time()))) {
+        $timedoutsidewindow = ' dimmed_text';
+    }
+
     echo "\n\n";
-    echo '<tr class="discussion r'.$rowcount.'">';
+    echo '<tr class="discussion r'.$rowcount.$timedoutsidewindow.'">';
 
     // Topic
     echo '<td class="topic starter">';
+
+    $canalwaysseetimedpost = $USER->id == $post->userid || $canviewhiddentimedposts;
+    if ($timeddiscussion && $canalwaysseetimedpost) {
+        echo $PAGE->get_renderer('mod_forum')->timed_discussion_tooltip($post, empty($timedoutsidewindow));
+    }
+
     echo '<a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'">'.$post->subject.'</a>';
     echo "</td>\n";
 
@@ -5437,6 +5484,7 @@ function forum_print_latest_discussions($course, $forum, $maxdiscussions = -1, $
     }
 
     $canviewparticipants = has_capability('moodle/course:viewparticipants',$context);
+    $canviewhiddentimedposts = has_capability('mod/forum:viewhiddentimedposts', $context);
 
     $strdatestring = get_string('strftimerecentfull');
 
@@ -5536,7 +5584,7 @@ function forum_print_latest_discussions($course, $forum, $maxdiscussions = -1, $
                     $group = -1;
                 }
                 forum_print_discussion_header($discussion, $forum, $group, $strdatestring, $cantrack, $forumtracked,
-                    $canviewparticipants, $context);
+                    $canviewparticipants, $context, $canviewhiddentimedposts);
             break;
             default:
                 $link = false;
index bcfb656..c38ebc9 100644 (file)
@@ -160,5 +160,27 @@ class mod_forum_renderer extends plugin_renderer_base {
         return $output;
     }
 
+    /**
+     * Generate the HTML for an icon to be displayed beside the subject of a timed discussion.
+     *
+     * @param object $discussion
+     * @param bool $visiblenow Indicicates that the discussion is currently
+     * visible to all users.
+     * @return string
+     */
+    public function timed_discussion_tooltip($discussion, $visiblenow) {
+        $dates = array();
+        if ($discussion->timestart) {
+            $dates[] = get_string('displaystart', 'mod_forum').': '.userdate($discussion->timestart);
+        }
+        if ($discussion->timeend) {
+            $dates[] = get_string('displayend', 'mod_forum').': '.userdate($discussion->timeend);
+        }
 
+        $str = $visiblenow ? 'timedvisible' : 'timedhidden';
+        $dates[] = get_string($str, 'mod_forum');
+
+        $tooltip = implode("\n", $dates);
+        return $this->pix_icon('i/calendar', $tooltip, 'moodle', array('class' => 'smallicon timedpost'));
+    }
 }
index a5914d7..25874a6 100644 (file)
@@ -165,3 +165,12 @@ span.unread {
 .dir-rtl .path-mod-forum .discussionsubscription {
     text-align: left;
 }
+
+#page-mod-forum-view img.timedpost {
+    margin-right: 5px;
+}
+
+.dir-rtl#page-mod-forum-view img.timedpost {
+    margin: 3px 0 0 5px;
+    float: right;
+}
diff --git a/mod/forum/tests/behat/posts_ordering_blog.feature b/mod/forum/tests/behat/posts_ordering_blog.feature
new file mode 100644 (file)
index 0000000..8d08a9d
--- /dev/null
@@ -0,0 +1,87 @@
+@mod @mod_forum
+Feature: Blog posts are always displayed in reverse chronological order
+  In order to use forum as a blog
+  As a user
+  I need to see most recent blog posts first
+
+  Background:
+    Given the following "users" exist:
+      | username  | firstname | lastname  | email                 |
+      | teacher1  | Teacher   | 1         | teacher1@example.com  |
+      | student1  | Student   | 1         | student1@example.com  |
+    And the following "courses" exist:
+      | fullname  | shortname | category  |
+      | Course 1  | C1        | 0         |
+    And the following "course enrolments" exist:
+      | user      | course    | role            |
+      | teacher1  | C1        | editingteacher  |
+      | student1  | C1        | student         |
+    And I log in as "teacher1"
+    And I follow "Course 1"
+    And I turn editing mode on
+    And I add a "Forum" to section "1" and I fill the form with:
+      | Forum name  | Course blog forum                               |
+      | Description | Single discussion forum description             |
+      | Forum type  | Standard forum displayed in a blog-like format  |
+    And I log out
+
+  #
+  # We need javascript/wait to prevent creation of the posts in the same second. The threads
+  # would then ignore each other in the prev/next navigation as the Forum is unable to compute
+  # the correct order.
+  #
+  @javascript
+  Scenario: Replying to a blog post or editing it does not affect its display order
+    Given I log in as "student1"
+    And I follow "Course 1"
+    And I follow "Course blog forum"
+    #
+    # Add three posts into the blog.
+    #
+    When I add a new topic to "Course blog forum" forum with:
+      | Subject | Blog post 1             |
+      | Message | This is the first post  |
+    And I wait "1" seconds
+    And I add a new topic to "Course blog forum" forum with:
+      | Subject | Blog post 2             |
+      | Message | This is the second post |
+    And I wait "1" seconds
+    And I add a new topic to "Course blog forum" forum with:
+      | Subject | Blog post 3             |
+      | Message | This is the third post  |
+    #
+    # Edit one of the blog posts.
+    #
+    And I click on "Edit" "link" in the "//div[@aria-label='Blog post 2 by Student 1']" "xpath_element"
+    And I set the following fields to these values:
+      | Subject | Edited blog post 2      |
+    And I press "Save changes"
+    And I wait to be redirected
+    And I log out
+    #
+    # Reply to another blog post.
+    #
+    And I log in as "teacher1"
+    And I follow "Course 1"
+    And I follow "Course blog forum"
+    And I click on "Discuss this topic" "link" in the "//div[@aria-label='Blog post 1 by Student 1']" "xpath_element"
+    And I click on "Reply" "link" in the "//div[@aria-label='Blog post 1 by Student 1']" "xpath_element"
+    And I set the following fields to these values:
+      | Message | Reply to the first post |
+    And I press "Post to forum"
+    And I wait to be redirected
+    And I am on site homepage
+    And I follow "Course 1"
+    And I follow "Course blog forum"
+    #
+    # Make sure the order of the blog posts is still reverse chronological.
+    #
+    Then I should see "This is the third post" in the "//div[contains(concat(' ', normalize-space(@class), ' '), ' forumpost ')][position()=1]" "xpath_element"
+    And I should see "This is the second post" in the "//div[contains(concat(' ', normalize-space(@class), ' '), ' forumpost ')][position()=2]" "xpath_element"
+    And I should see "This is the first post" in the "//div[contains(concat(' ', normalize-space(@class), ' '), ' forumpost ')][position()=3]" "xpath_element"
+    #
+    # Make sure the next/prev navigation uses the same order of the posts.
+    #
+    And I click on "Discuss this topic" "link" in the "//div[@aria-label='Edited blog post 2 by Student 1']" "xpath_element"
+    And "//a[@aria-label='Next discussion: Blog post 3']" "xpath_element" should exist
+    And "//a[@aria-label='Previous discussion: Blog post 1']" "xpath_element" should exist
diff --git a/mod/forum/tests/behat/posts_ordering_general.feature b/mod/forum/tests/behat/posts_ordering_general.feature
new file mode 100644 (file)
index 0000000..cf5df8a
--- /dev/null
@@ -0,0 +1,88 @@
+@mod @mod_forum
+Feature: New discussions and discussions with recently added replies are displayed first
+  In order to use forum as a discussion tool
+  As a user
+  I need to see currently active discussions first
+
+  Background:
+    Given the following "users" exist:
+      | username  | firstname | lastname  | email                 |
+      | teacher1  | Teacher   | 1         | teacher1@example.com  |
+      | student1  | Student   | 1         | student1@example.com  |
+    And the following "courses" exist:
+      | fullname  | shortname | category  |
+      | Course 1  | C1        | 0         |
+    And the following "course enrolments" exist:
+      | user      | course    | role            |
+      | teacher1  | C1        | editingteacher  |
+      | student1  | C1        | student         |
+    And I log in as "teacher1"
+    And I follow "Course 1"
+    And I turn editing mode on
+    And I add a "Forum" to section "1" and I fill the form with:
+      | Forum name  | Course general forum                |
+      | Description | Single discussion forum description |
+      | Forum type  | Standard forum for general use      |
+    And I log out
+
+  #
+  # We need javascript/wait to prevent creation of the posts in the same second. The threads
+  # would then ignore each other in the prev/next navigation as the Forum is unable to compute
+  # the correct order.
+  #
+  @javascript
+  Scenario: Replying to a forum post or editing it puts the discussion to the front
+    Given I log in as "student1"
+    And I follow "Course 1"
+    And I follow "Course general forum"
+    #
+    # Add three posts into the forum.
+    #
+    When I add a new discussion to "Course general forum" forum with:
+      | Subject | Forum post 1            |
+      | Message | This is the first post  |
+    And I wait "1" seconds
+    And I add a new discussion to "Course general forum" forum with:
+      | Subject | Forum post 2            |
+      | Message | This is the second post |
+    And I wait "1" seconds
+    And I add a new discussion to "Course general forum" forum with:
+      | Subject | Forum post 3            |
+      | Message | This is the third post  |
+    #
+    # Edit one of the forum posts.
+    #
+    And I follow "Forum post 2"
+    And I click on "Edit" "link" in the "//div[contains(concat(' ', normalize-space(@class), ' '), ' forumpost ')][contains(., 'Forum post 2')]" "xpath_element"
+    And I set the following fields to these values:
+      | Subject | Edited forum post 2     |
+    And I press "Save changes"
+    And I wait to be redirected
+    And I log out
+    #
+    # Reply to another forum post.
+    #
+    And I log in as "teacher1"
+    And I follow "Course 1"
+    And I follow "Course general forum"
+    And I follow "Forum post 1"
+    And I click on "Reply" "link" in the "//div[@aria-label='Forum post 1 by Student 1']" "xpath_element"
+    And I set the following fields to these values:
+      | Message | Reply to the first post |
+    And I press "Post to forum"
+    And I wait to be redirected
+    And I am on site homepage
+    And I follow "Course 1"
+    And I follow "Course general forum"
+    #
+    # Make sure the order of the forum posts is as expected (most recently participated first).
+    #
+    Then I should see "Forum post 3" in the "//tr[contains(concat(' ', normalize-space(@class), ' '), ' discussion ')][position()=3]" "xpath_element"
+    And I should see "Edited forum post 2" in the "//tr[contains(concat(' ', normalize-space(@class), ' '), ' discussion ')][position()=2]" "xpath_element"
+    And I should see "Forum post 1" in the "//tr[contains(concat(' ', normalize-space(@class), ' '), ' discussion ')][position()=1]" "xpath_element"
+    #
+    # Make sure the next/prev navigation uses the same order of the posts.
+    #
+    And I follow "Edited forum post 2"
+    And "//a[@aria-label='Next discussion: Forum post 1']" "xpath_element" should exist
+    And "//a[@aria-label='Previous discussion: Forum post 3']" "xpath_element" should exist
diff --git a/mod/forum/tests/behat/timed_discussions.feature b/mod/forum/tests/behat/timed_discussions.feature
new file mode 100644 (file)
index 0000000..c486c52
--- /dev/null
@@ -0,0 +1,51 @@
+@mod @mod_forum
+Feature: Users can choose to set start and end time for display of their discussions
+  In order to temporarly hide discussions to students
+  As a teacher
+  I need to set a discussion time start and time end
+
+  Scenario: Student should not see the tooltip or the discussion
+    Given the following "users" exist:
+      | username | firstname | lastname | email |
+      | student1 | Student | 1 | student1@example.com |
+    And the following "courses" exist:
+      | fullname | shortname | category |
+      | Course 1 | C1 | 0 |
+    And the following "course enrolments" exist:
+      | user | course | role |
+      | student1 | C1 | student |
+    And I log in as "admin"
+    And the following config values are set as admin:
+      | forum_enabletimedposts | 1 |
+    And I am on site homepage
+    And I follow "Course 1"
+    And I turn editing mode on
+    And I add a "Forum" to section "1" and I fill the form with:
+      | Forum name | Test forum name |
+      | Description | Test forum description |
+    And I add a new discussion to "Test forum name" forum with:
+      | Subject | Discussion 1 |
+      | Message | Discussion contents 1, first message |
+    And I add a new discussion to "Test forum name" forum with:
+      | Subject          | Discussion 2 timed not visible       |
+      | Message          | Discussion contents 2, first message |
+      | timeend[enabled] | 1 |
+      | timeend[year]    | 2014 |
+    And I add a new discussion to "Test forum name" forum with:
+      | Subject            | Discussion 3 timed visible now       |
+      | Message            | Discussion contents 3, first message |
+      | timestart[enabled] | 1 |
+    And I am on site homepage
+    And I follow "Course 1"
+    And I follow "Test forum name"
+    And I should see "Discussion 2 timed"
+    And I should see "Discussion 3 timed"
+    And ".timedpost" "css_element" should exist
+    And I log out
+    And I log in as "student1"
+    When I follow "Course 1"
+    And I follow "Test forum name"
+    Then I should see "Discussion 1"
+    And I should not see "Discussion 2 timed"
+    And ".timedpost" "css_element" should not exist
+    And I should see "Discussion 3 timed"
index bc38397..d427c85 100644 (file)
@@ -822,23 +822,23 @@ class mod_forum_lib_testcase extends advanced_testcase {
         $disc5 = $forumgen->create_discussion($record);
 
         // Getting the neighbours.
-        $neighbours = forum_get_discussion_neighbours($cm, $disc1);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc1, $forum);
         $this->assertEmpty($neighbours['prev']);
         $this->assertEquals($disc2->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc2);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc2, $forum);
         $this->assertEquals($disc1->id, $neighbours['prev']->id);
         $this->assertEquals($disc3->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc3);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc3, $forum);
         $this->assertEquals($disc2->id, $neighbours['prev']->id);
         $this->assertEquals($disc4->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc4);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc4, $forum);
         $this->assertEquals($disc3->id, $neighbours['prev']->id);
         $this->assertEquals($disc5->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc5);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc5, $forum);
         $this->assertEquals($disc4->id, $neighbours['prev']->id);
         $this->assertEmpty($neighbours['next']);
 
@@ -848,28 +848,28 @@ class mod_forum_lib_testcase extends advanced_testcase {
         $disc1->timemodified = time();
         $DB->update_record('forum_discussions', $disc1);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc5);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc5, $forum);
         $this->assertEquals($disc4->id, $neighbours['prev']->id);
         $this->assertEquals($disc1->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc2);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc2, $forum);
         $this->assertEmpty($neighbours['prev']);
         $this->assertEquals($disc3->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc1);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc1, $forum);
         $this->assertEquals($disc5->id, $neighbours['prev']->id);
         $this->assertEmpty($neighbours['next']);
 
         // After some discussions were created.
         sleep(1);
         $disc6 = $forumgen->create_discussion($record);
-        $neighbours = forum_get_discussion_neighbours($cm, $disc6);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc6, $forum);
         $this->assertEquals($disc1->id, $neighbours['prev']->id);
         $this->assertEmpty($neighbours['next']);
 
         sleep(1);
         $disc7 = $forumgen->create_discussion($record);
-        $neighbours = forum_get_discussion_neighbours($cm, $disc7);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc7, $forum);
         $this->assertEquals($disc6->id, $neighbours['prev']->id);
         $this->assertEmpty($neighbours['next']);
 
@@ -906,59 +906,59 @@ class mod_forum_lib_testcase extends advanced_testcase {
 
         // Admin user ignores the timed settings of discussions.
         $this->setAdminUser();
-        $neighbours = forum_get_discussion_neighbours($cm, $disc8);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc8, $forum);
         $this->assertEquals($disc7->id, $neighbours['prev']->id);
         $this->assertEquals($disc9->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc9);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc9, $forum);
         $this->assertEquals($disc8->id, $neighbours['prev']->id);
         $this->assertEquals($disc10->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc10);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc10, $forum);
         $this->assertEquals($disc9->id, $neighbours['prev']->id);
         $this->assertEquals($disc11->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc11);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc11, $forum);
         $this->assertEquals($disc10->id, $neighbours['prev']->id);
         $this->assertEquals($disc12->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc12);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
         $this->assertEquals($disc11->id, $neighbours['prev']->id);
         $this->assertEmpty($neighbours['next']);
 
         // Normal user can see their own timed discussions.
         $this->setUser($user);
-        $neighbours = forum_get_discussion_neighbours($cm, $disc8);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc8, $forum);
         $this->assertEquals($disc7->id, $neighbours['prev']->id);
         $this->assertEquals($disc9->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc9);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc9, $forum);
         $this->assertEquals($disc8->id, $neighbours['prev']->id);
         $this->assertEquals($disc10->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc10);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc10, $forum);
         $this->assertEquals($disc9->id, $neighbours['prev']->id);
         $this->assertEquals($disc11->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc11);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc11, $forum);
         $this->assertEquals($disc10->id, $neighbours['prev']->id);
         $this->assertEquals($disc12->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc12);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
         $this->assertEquals($disc11->id, $neighbours['prev']->id);
         $this->assertEmpty($neighbours['next']);
 
         // Normal user does not ignore timed settings.
         $this->setUser($user2);
-        $neighbours = forum_get_discussion_neighbours($cm, $disc8);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc8, $forum);
         $this->assertEquals($disc7->id, $neighbours['prev']->id);
         $this->assertEquals($disc10->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc10);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc10, $forum);
         $this->assertEquals($disc8->id, $neighbours['prev']->id);
         $this->assertEquals($disc12->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc12);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
         $this->assertEquals($disc10->id, $neighbours['prev']->id);
         $this->assertEmpty($neighbours['next']);
 
@@ -974,11 +974,197 @@ class mod_forum_lib_testcase extends advanced_testcase {
         $disc2 = $DB->get_record('forum_discussions', array('id' => $disc2->id));
         $disc3 = $DB->get_record('forum_discussions', array('id' => $disc3->id));
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc2);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc2, $forum);
         $this->assertEquals($disc12->id, $neighbours['prev']->id);
         $this->assertEmpty($neighbours['next']);
 
-        $neighbours = forum_get_discussion_neighbours($cm, $disc3);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc3, $forum);
+        $this->assertEquals($disc12->id, $neighbours['prev']->id);
+        $this->assertEmpty($neighbours['next']);
+    }
+
+    /**
+     * Test getting the neighbour threads of a blog-like forum.
+     */
+    public function test_forum_get_neighbours_blog() {
+        global $CFG, $DB;
+        $this->resetAfterTest();
+
+        // Setup test data.
+        $forumgen = $this->getDataGenerator()->get_plugin_generator('mod_forum');
+        $course = $this->getDataGenerator()->create_course();
+        $user = $this->getDataGenerator()->create_user();
+        $user2 = $this->getDataGenerator()->create_user();
+
+        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'blog'));
+        $cm = get_coursemodule_from_instance('forum', $forum->id);
+        $context = context_module::instance($cm->id);
+
+        $record = new stdClass();
+        $record->course = $course->id;
+        $record->userid = $user->id;
+        $record->forum = $forum->id;
+        $disc1 = $forumgen->create_discussion($record);
+        sleep(1);
+        $disc2 = $forumgen->create_discussion($record);
+        sleep(1);
+        $disc3 = $forumgen->create_discussion($record);
+        sleep(1);
+        $disc4 = $forumgen->create_discussion($record);
+        sleep(1);
+        $disc5 = $forumgen->create_discussion($record);
+
+        // Getting the neighbours.
+        $neighbours = forum_get_discussion_neighbours($cm, $disc1, $forum);
+        $this->assertEmpty($neighbours['prev']);
+        $this->assertEquals($disc2->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc2, $forum);
+        $this->assertEquals($disc1->id, $neighbours['prev']->id);
+        $this->assertEquals($disc3->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc3, $forum);
+        $this->assertEquals($disc2->id, $neighbours['prev']->id);
+        $this->assertEquals($disc4->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc4, $forum);
+        $this->assertEquals($disc3->id, $neighbours['prev']->id);
+        $this->assertEquals($disc5->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc5, $forum);
+        $this->assertEquals($disc4->id, $neighbours['prev']->id);
+        $this->assertEmpty($neighbours['next']);
+
+        // Make sure that the thread's timemodified does not affect the order.
+        sleep(1);
+        $disc1->timemodified = time();
+        $DB->update_record('forum_discussions', $disc1);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc1, $forum);
+        $this->assertEmpty($neighbours['prev']);
+        $this->assertEquals($disc2->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc2, $forum);
+        $this->assertEquals($disc1->id, $neighbours['prev']->id);
+        $this->assertEquals($disc3->id, $neighbours['next']->id);
+
+        // Add another blog post.
+        sleep(1);
+        $disc6 = $forumgen->create_discussion($record);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc6, $forum);
+        $this->assertEquals($disc5->id, $neighbours['prev']->id);
+        $this->assertEmpty($neighbours['next']);
+
+        sleep(1);
+        $disc7 = $forumgen->create_discussion($record);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc7, $forum);
+        $this->assertEquals($disc6->id, $neighbours['prev']->id);
+        $this->assertEmpty($neighbours['next']);
+
+        // Adding timed discussions.
+        $CFG->forum_enabletimedposts = true;
+        $now = time();
+        $past = $now - 60;
+        $future = $now + 60;
+
+        $record = new stdClass();
+        $record->course = $course->id;
+        $record->userid = $user->id;
+        $record->forum = $forum->id;
+        $record->timestart = $past;
+        $record->timeend = $future;
+        sleep(1);
+        $disc8 = $forumgen->create_discussion($record);
+        sleep(1);
+        $record->timestart = $future;
+        $record->timeend = 0;
+        $disc9 = $forumgen->create_discussion($record);
+        sleep(1);
+        $record->timestart = 0;
+        $record->timeend = 0;
+        $disc10 = $forumgen->create_discussion($record);
+        sleep(1);
+        $record->timestart = 0;
+        $record->timeend = $past;
+        $disc11 = $forumgen->create_discussion($record);
+        sleep(1);
+        $record->timestart = $past;
+        $record->timeend = $future;
+        $disc12 = $forumgen->create_discussion($record);
+
+        // Admin user ignores the timed settings of discussions.
+        $this->setAdminUser();
+        $neighbours = forum_get_discussion_neighbours($cm, $disc8, $forum);
+        $this->assertEquals($disc7->id, $neighbours['prev']->id);
+        $this->assertEquals($disc9->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc9, $forum);
+        $this->assertEquals($disc8->id, $neighbours['prev']->id);
+        $this->assertEquals($disc10->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc10, $forum);
+        $this->assertEquals($disc9->id, $neighbours['prev']->id);
+        $this->assertEquals($disc11->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc11, $forum);
+        $this->assertEquals($disc10->id, $neighbours['prev']->id);
+        $this->assertEquals($disc12->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
+        $this->assertEquals($disc11->id, $neighbours['prev']->id);
+        $this->assertEmpty($neighbours['next']);
+
+        // Normal user can see their own timed discussions.
+        $this->setUser($user);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc8, $forum);
+        $this->assertEquals($disc7->id, $neighbours['prev']->id);
+        $this->assertEquals($disc9->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc9, $forum);
+        $this->assertEquals($disc8->id, $neighbours['prev']->id);
+        $this->assertEquals($disc10->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc10, $forum);
+        $this->assertEquals($disc9->id, $neighbours['prev']->id);
+        $this->assertEquals($disc11->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc11, $forum);
+        $this->assertEquals($disc10->id, $neighbours['prev']->id);
+        $this->assertEquals($disc12->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
+        $this->assertEquals($disc11->id, $neighbours['prev']->id);
+        $this->assertEmpty($neighbours['next']);
+
+        // Normal user does not ignore timed settings.
+        $this->setUser($user2);
+        $neighbours = forum_get_discussion_neighbours($cm, $disc8, $forum);
+        $this->assertEquals($disc7->id, $neighbours['prev']->id);
+        $this->assertEquals($disc10->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc10, $forum);
+        $this->assertEquals($disc8->id, $neighbours['prev']->id);
+        $this->assertEquals($disc12->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
+        $this->assertEquals($disc10->id, $neighbours['prev']->id);
+        $this->assertEmpty($neighbours['next']);
+
+        // Reset to normal mode.
+        $CFG->forum_enabletimedposts = false;
+        $this->setAdminUser();
+
+        // Two blog posts with identical creation time ignore each other.
+        sleep(1);
+        $now = time();
+        $DB->update_record('forum_posts', (object) array('id' => $disc2->firstpost, 'created' => $now));
+        $DB->update_record('forum_posts', (object) array('id' => $disc3->firstpost, 'created' => $now));
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc2, $forum);
+        $this->assertEquals($disc12->id, $neighbours['prev']->id);
+        $this->assertEmpty($neighbours['next']);
+
+        $neighbours = forum_get_discussion_neighbours($cm, $disc3, $forum);
         $this->assertEquals($disc12->id, $neighbours['prev']->id);
         $this->assertEmpty($neighbours['next']);
     }
@@ -1051,38 +1237,232 @@ class mod_forum_lib_testcase extends advanced_testcase {
 
         // Admin user can see all groups.
         $this->setAdminUser();
-        $neighbours = forum_get_discussion_neighbours($cm1, $disc11);
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc11, $forum1);
+        $this->assertEmpty($neighbours['prev']);
+        $this->assertEquals($disc12->id, $neighbours['next']->id);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc21, $forum2);
+        $this->assertEmpty($neighbours['prev']);
+        $this->assertEquals($disc22->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc12, $forum1);
+        $this->assertEquals($disc11->id, $neighbours['prev']->id);
+        $this->assertEquals($disc13->id, $neighbours['next']->id);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc22, $forum2);
+        $this->assertEquals($disc21->id, $neighbours['prev']->id);
+        $this->assertEquals($disc23->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
+        $this->assertEquals($disc12->id, $neighbours['prev']->id);
+        $this->assertEquals($disc14->id, $neighbours['next']->id);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
+        $this->assertEquals($disc22->id, $neighbours['prev']->id);
+        $this->assertEquals($disc24->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc14, $forum1);
+        $this->assertEquals($disc13->id, $neighbours['prev']->id);
+        $this->assertEquals($disc15->id, $neighbours['next']->id);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc24, $forum2);
+        $this->assertEquals($disc23->id, $neighbours['prev']->id);
+        $this->assertEquals($disc25->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc15, $forum1);
+        $this->assertEquals($disc14->id, $neighbours['prev']->id);
+        $this->assertEmpty($neighbours['next']);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc25, $forum2);
+        $this->assertEquals($disc24->id, $neighbours['prev']->id);
+        $this->assertEmpty($neighbours['next']);
+
+        // Admin user is only viewing group 1.
+        $_POST['group'] = $group1->id;
+        $this->assertEquals($group1->id, groups_get_activity_group($cm1, true));
+        $this->assertEquals($group1->id, groups_get_activity_group($cm2, true));
+
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc11, $forum1);
+        $this->assertEmpty($neighbours['prev']);
+        $this->assertEquals($disc13->id, $neighbours['next']->id);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc21, $forum2);
+        $this->assertEmpty($neighbours['prev']);
+        $this->assertEquals($disc23->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
+        $this->assertEquals($disc11->id, $neighbours['prev']->id);
+        $this->assertEquals($disc15->id, $neighbours['next']->id);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
+        $this->assertEquals($disc21->id, $neighbours['prev']->id);
+        $this->assertEquals($disc25->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc15, $forum1);
+        $this->assertEquals($disc13->id, $neighbours['prev']->id);
+        $this->assertEmpty($neighbours['next']);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc25, $forum2);
+        $this->assertEquals($disc23->id, $neighbours['prev']->id);
+        $this->assertEmpty($neighbours['next']);
+
+        // Normal user viewing non-grouped posts (this is only possible in visible groups).
+        $this->setUser($user1);
+        $_POST['group'] = 0;
+        $this->assertEquals(0, groups_get_activity_group($cm1, true));
+
+        // They can see anything in visible groups.
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc12, $forum1);
+        $this->assertEquals($disc11->id, $neighbours['prev']->id);
+        $this->assertEquals($disc13->id, $neighbours['next']->id);
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
+        $this->assertEquals($disc12->id, $neighbours['prev']->id);
+        $this->assertEquals($disc14->id, $neighbours['next']->id);
+
+        // Normal user, orphan of groups, can only see non-grouped posts in separate groups.
+        $this->setUser($user2);
+        $_POST['group'] = 0;
+        $this->assertEquals(0, groups_get_activity_group($cm2, true));
+
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
+        $this->assertEmpty($neighbours['prev']);
+        $this->assertEmpty($neighbours['next']);
+
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc22, $forum2);
+        $this->assertEmpty($neighbours['prev']);
+        $this->assertEquals($disc23->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc24, $forum2);
+        $this->assertEquals($disc23->id, $neighbours['prev']->id);
+        $this->assertEmpty($neighbours['next']);
+
+        // Switching to viewing group 1.
+        $this->setUser($user1);
+        $_POST['group'] = $group1->id;
+        $this->assertEquals($group1->id, groups_get_activity_group($cm1, true));
+        $this->assertEquals($group1->id, groups_get_activity_group($cm2, true));
+
+        // They can see non-grouped or same group.
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc11, $forum1);
+        $this->assertEmpty($neighbours['prev']);
+        $this->assertEquals($disc13->id, $neighbours['next']->id);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc21, $forum2);
+        $this->assertEmpty($neighbours['prev']);
+        $this->assertEquals($disc23->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
+        $this->assertEquals($disc11->id, $neighbours['prev']->id);
+        $this->assertEquals($disc15->id, $neighbours['next']->id);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
+        $this->assertEquals($disc21->id, $neighbours['prev']->id);
+        $this->assertEquals($disc25->id, $neighbours['next']->id);
+
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc15, $forum1);
+        $this->assertEquals($disc13->id, $neighbours['prev']->id);
+        $this->assertEmpty($neighbours['next']);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc25, $forum2);
+        $this->assertEquals($disc23->id, $neighbours['prev']->id);
+        $this->assertEmpty($neighbours['next']);
+
+        // Querying the neighbours of a discussion passing the wrong CM.
+        $this->setExpectedException('coding_exception');
+        forum_get_discussion_neighbours($cm2, $disc11, $forum2);
+    }
+
+    /**
+     * Test getting the neighbour threads of a blog-like forum with groups involved.
+     */
+    public function test_forum_get_neighbours_with_groups_blog() {
+        $this->resetAfterTest();
+
+        // Setup test data.
+        $forumgen = $this->getDataGenerator()->get_plugin_generator('mod_forum');
+        $course = $this->getDataGenerator()->create_course();
+        $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
+        $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
+        $user1 = $this->getDataGenerator()->create_user();
+        $user2 = $this->getDataGenerator()->create_user();
+        $this->getDataGenerator()->enrol_user($user1->id, $course->id);
+        $this->getDataGenerator()->enrol_user($user2->id, $course->id);
+        $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group1->id));
+
+        $forum1 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'blog',
+                'groupmode' => VISIBLEGROUPS));
+        $forum2 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'blog',
+                'groupmode' => SEPARATEGROUPS));
+        $cm1 = get_coursemodule_from_instance('forum', $forum1->id);
+        $cm2 = get_coursemodule_from_instance('forum', $forum2->id);
+        $context1 = context_module::instance($cm1->id);
+        $context2 = context_module::instance($cm2->id);
+
+        // Creating blog posts in both forums.
+        $record = new stdClass();
+        $record->course = $course->id;
+        $record->userid = $user1->id;
+        $record->forum = $forum1->id;
+        $record->groupid = $group1->id;
+        $disc11 = $forumgen->create_discussion($record);
+        $record->forum = $forum2->id;
+        $disc21 = $forumgen->create_discussion($record);
+
+        sleep(1);
+        $record->userid = $user2->id;
+        $record->forum = $forum1->id;
+        $record->groupid = $group2->id;
+        $disc12 = $forumgen->create_discussion($record);
+        $record->forum = $forum2->id;
+        $disc22 = $forumgen->create_discussion($record);
+
+        sleep(1);
+        $record->userid = $user1->id;
+        $record->forum = $forum1->id;
+        $record->groupid = null;
+        $disc13 = $forumgen->create_discussion($record);
+        $record->forum = $forum2->id;
+        $disc23 = $forumgen->create_discussion($record);
+
+        sleep(1);
+        $record->userid = $user2->id;
+        $record->forum = $forum1->id;
+        $record->groupid = $group2->id;
+        $disc14 = $forumgen->create_discussion($record);
+        $record->forum = $forum2->id;
+        $disc24 = $forumgen->create_discussion($record);
+
+        sleep(1);
+        $record->userid = $user1->id;
+        $record->forum = $forum1->id;
+        $record->groupid = $group1->id;
+        $disc15 = $forumgen->create_discussion($record);
+        $record->forum = $forum2->id;
+        $disc25 = $forumgen->create_discussion($record);
+
+        // Admin user can see all groups.
+        $this->setAdminUser();
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc11, $forum1);
         $this->assertEmpty($neighbours['prev']);
         $this->assertEquals($disc12->id, $neighbours['next']->id);
-        $neighbours = forum_get_discussion_neighbours($cm2, $disc21);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc21, $forum2);
         $this->assertEmpty($neighbours['prev']);
         $this->assertEquals($disc22->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm1, $disc12);
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc12, $forum1);
         $this->assertEquals($disc11->id, $neighbours['prev']->id);
         $this->assertEquals($disc13->id, $neighbours['next']->id);
-        $neighbours = forum_get_discussion_neighbours($cm2, $disc22);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc22, $forum2);
         $this->assertEquals($disc21->id, $neighbours['prev']->id);
         $this->assertEquals($disc23->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm1, $disc13);
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
         $this->assertEquals($disc12->id, $neighbours['prev']->id);
         $this->assertEquals($disc14->id, $neighbours['next']->id);
-        $neighbours = forum_get_discussion_neighbours($cm2, $disc23);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
         $this->assertEquals($disc22->id, $neighbours['prev']->id);
         $this->assertEquals($disc24->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm1, $disc14);
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc14, $forum1);
         $this->assertEquals($disc13->id, $neighbours['prev']->id);
         $this->assertEquals($disc15->id, $neighbours['next']->id);
-        $neighbours = forum_get_discussion_neighbours($cm2, $disc24);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc24, $forum2);
         $this->assertEquals($disc23->id, $neighbours['prev']->id);
         $this->assertEquals($disc25->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm1, $disc15);
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc15, $forum1);
         $this->assertEquals($disc14->id, $neighbours['prev']->id);
         $this->assertEmpty($neighbours['next']);
-        $neighbours = forum_get_discussion_neighbours($cm2, $disc25);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc25, $forum2);
         $this->assertEquals($disc24->id, $neighbours['prev']->id);
         $this->assertEmpty($neighbours['next']);
 
@@ -1091,24 +1471,24 @@ class mod_forum_lib_testcase extends advanced_testcase {
         $this->assertEquals($group1->id, groups_get_activity_group($cm1, true));
         $this->assertEquals($group1->id, groups_get_activity_group($cm2, true));
 
-        $neighbours = forum_get_discussion_neighbours($cm1, $disc11);
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc11, $forum1);
         $this->assertEmpty($neighbours['prev']);
         $this->assertEquals($disc13->id, $neighbours['next']->id);
-        $neighbours = forum_get_discussion_neighbours($cm2, $disc21);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc21, $forum2);
         $this->assertEmpty($neighbours['prev']);
         $this->assertEquals($disc23->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm1, $disc13);
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
         $this->assertEquals($disc11->id, $neighbours['prev']->id);
         $this->assertEquals($disc15->id, $neighbours['next']->id);
-        $neighbours = forum_get_discussion_neighbours($cm2, $disc23);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
         $this->assertEquals($disc21->id, $neighbours['prev']->id);
         $this->assertEquals($disc25->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm1, $disc15);
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc15, $forum1);
         $this->assertEquals($disc13->id, $neighbours['prev']->id);
         $this->assertEmpty($neighbours['next']);
-        $neighbours = forum_get_discussion_neighbours($cm2, $disc25);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc25, $forum2);
         $this->assertEquals($disc23->id, $neighbours['prev']->id);
         $this->assertEmpty($neighbours['next']);
 
@@ -1118,10 +1498,10 @@ class mod_forum_lib_testcase extends advanced_testcase {
         $this->assertEquals(0, groups_get_activity_group($cm1, true));
 
         // They can see anything in visible groups.
-        $neighbours = forum_get_discussion_neighbours($cm1, $disc12);
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc12, $forum1);
         $this->assertEquals($disc11->id, $neighbours['prev']->id);
         $this->assertEquals($disc13->id, $neighbours['next']->id);
-        $neighbours = forum_get_discussion_neighbours($cm1, $disc13);
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
         $this->assertEquals($disc12->id, $neighbours['prev']->id);
         $this->assertEquals($disc14->id, $neighbours['next']->id);
 
@@ -1130,15 +1510,15 @@ class mod_forum_lib_testcase extends advanced_testcase {
         $_POST['group'] = 0;
         $this->assertEquals(0, groups_get_activity_group($cm2, true));
 
-        $neighbours = forum_get_discussion_neighbours($cm2, $disc23);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
         $this->assertEmpty($neighbours['prev']);
         $this->assertEmpty($neighbours['next']);
 
-        $neighbours = forum_get_discussion_neighbours($cm2, $disc22);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc22, $forum2);
         $this->assertEmpty($neighbours['prev']);
         $this->assertEquals($disc23->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm2, $disc24);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc24, $forum2);
         $this->assertEquals($disc23->id, $neighbours['prev']->id);
         $this->assertEmpty($neighbours['next']);
 
@@ -1149,30 +1529,30 @@ class mod_forum_lib_testcase extends advanced_testcase {
         $this->assertEquals($group1->id, groups_get_activity_group($cm2, true));
 
         // They can see non-grouped or same group.
-        $neighbours = forum_get_discussion_neighbours($cm1, $disc11);
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc11, $forum1);
         $this->assertEmpty($neighbours['prev']);
         $this->assertEquals($disc13->id, $neighbours['next']->id);
-        $neighbours = forum_get_discussion_neighbours($cm2, $disc21);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc21, $forum2);
         $this->assertEmpty($neighbours['prev']);
         $this->assertEquals($disc23->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm1, $disc13);
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
         $this->assertEquals($disc11->id, $neighbours['prev']->id);
         $this->assertEquals($disc15->id, $neighbours['next']->id);
-        $neighbours = forum_get_discussion_neighbours($cm2, $disc23);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
         $this->assertEquals($disc21->id, $neighbours['prev']->id);
         $this->assertEquals($disc25->id, $neighbours['next']->id);
 
-        $neighbours = forum_get_discussion_neighbours($cm1, $disc15);
+        $neighbours = forum_get_discussion_neighbours($cm1, $disc15, $forum1);
         $this->assertEquals($disc13->id, $neighbours['prev']->id);
         $this->assertEmpty($neighbours['next']);
-        $neighbours = forum_get_discussion_neighbours($cm2, $disc25);
+        $neighbours = forum_get_discussion_neighbours($cm2, $disc25, $forum2);
         $this->assertEquals($disc23->id, $neighbours['prev']->id);
         $this->assertEmpty($neighbours['next']);
 
         // Querying the neighbours of a discussion passing the wrong CM.
         $this->setExpectedException('coding_exception');
-        forum_get_discussion_neighbours($cm2, $disc11);
+        forum_get_discussion_neighbours($cm2, $disc11, $forum2);
     }
 
     public function test_count_discussion_replies_basic() {
index 8f57abc..8b2d40b 100644 (file)
@@ -222,8 +222,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertFalse(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // Check that the user is unsubscribed from the discussion too.
         $this->assertFalse(\mod_forum\subscriptions::is_subscribed($author->id, $forum, $discussion->id));
@@ -267,8 +267,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
         // Subscribing to the forum should create a record in the subscriptions table, but not the forum discussion
         // subscriptions table.
         forum_subscribe($author->id, $forum->id);
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
         $this->assertEquals(1, $DB->count_records('forum_subscriptions', array(
             'userid'        => $author->id,
             'forum'         => $forum->id,
@@ -281,8 +281,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
         // Unsubscribing should remove the record from the forum subscriptions table, and not modify the forum
         // discussion subscriptions table.
         forum_unsubscribe($author->id, $forum->id);
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
         $this->assertEquals(0, $DB->count_records('forum_subscriptions', array(
             'userid'        => $author->id,
             'forum'         => $forum->id,
@@ -441,8 +441,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertFalse(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // Post a discussion to the forum.
         list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
@@ -478,8 +478,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertTrue(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // Post a discussion to the forum.
         list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
@@ -508,8 +508,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertFalse(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // Post a discussion to the forum.
         list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
@@ -525,8 +525,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertFalse(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // But subscribed to the discussion.
         $this->assertTrue(\mod_forum\subscriptions::is_subscribed($author->id, $forum, $discussion->id));
@@ -555,8 +555,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertTrue(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // Post a discussion to the forum.
         list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
@@ -569,8 +569,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertTrue(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // But unsubscribed from the discussion.
         $this->assertFalse(\mod_forum\subscriptions::is_subscribed($author->id, $forum, $discussion->id));
@@ -601,8 +601,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertTrue(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // Post a discussion to the forum.
         list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
@@ -631,8 +631,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertTrue(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // An attempt to unsubscribe again should result in a falsey return to indicate that no change was made.
         $this->assertFalse(\mod_forum\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion));
@@ -670,13 +670,13 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertTrue(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // Check the deprecated function too.
         $this->assertTrue(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // And is subscribed to the discussion again.
         $this->assertTrue(\mod_forum\subscriptions::is_subscribed($author->id, $forum, $discussion->id));
@@ -701,8 +701,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertTrue(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // But unsubscribed from the discussion.
         $this->assertFalse(\mod_forum\subscriptions::is_subscribed($author->id, $forum, $discussion->id));
@@ -749,8 +749,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertTrue(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // But unsubscribed from the discussion.
         $this->assertFalse(\mod_forum\subscriptions::is_subscribed($author->id, $forum, $discussion->id));
@@ -808,8 +808,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertFalse(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // Post a discussion to the forum.
         list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
@@ -828,8 +828,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertFalse(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // But subscribed to the discussion.
         $this->assertTrue(\mod_forum\subscriptions::is_subscribed($author->id, $forum, $discussion->id));
@@ -848,8 +848,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertFalse(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // And is unsubscribed from the discussion again.
         $this->assertFalse(\mod_forum\subscriptions::is_subscribed($author->id, $forum, $discussion->id));
@@ -868,8 +868,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertFalse(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // And is subscribed to the discussion again.
         $this->assertTrue(\mod_forum\subscriptions::is_subscribed($author->id, $forum, $discussion->id));
@@ -888,8 +888,8 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check the deprecated function too.
         $this->assertFalse(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // But unsubscribed from the discussion.
         $this->assertFalse(\mod_forum\subscriptions::is_subscribed($author->id, $forum, $discussion->id));
@@ -920,25 +920,25 @@ class mod_forum_subscriptions_testcase extends advanced_testcase {
 
         // Check that the user is currently unsubscribed to the forum.
         $this->assertFalse(forum_is_subscribed($author->id, $forum->id));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // It should match the result of when it's called with the forum object.
         $this->assertFalse(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // And when the user is subscribed, we should also get the correct result.
         \mod_forum\subscriptions::subscribe_user($author->id, $forum);
 
         $this->assertTrue(forum_is_subscribed($author->id, $forum->id));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
 
         // It should match the result of when it's called with the forum object.
         $this->assertTrue(forum_is_subscribed($author->id, $forum));
-        $this->assertEquals(1, count(phpunit_util::get_debugging_messages()));
-        phpunit_util::reset_debugging();
+        $this->assertEquals(1, count($this->getDebuggingMessages()));
+        $this->resetDebugging();
     }
 
     /**
index af2c8d7..483fed7 100644 (file)
         case 'blog':
             echo '<br />';
             if (!empty($showall)) {
-                forum_print_latest_discussions($course, $forum, 0, 'plain', '', -1, -1, -1, 0, $cm);
+                forum_print_latest_discussions($course, $forum, 0, 'plain', 'p.created DESC', -1, -1, -1, 0, $cm);
             } else {
-                forum_print_latest_discussions($course, $forum, -1, 'plain', '', -1, -1, $page, $CFG->forum_manydiscussions, $cm);
+                forum_print_latest_discussions($course, $forum, -1, 'plain', 'p.created DESC', -1, -1, $page,
+                    $CFG->forum_manydiscussions, $cm);
             }
             break;
 
index f6fd502..53aaf1b 100644 (file)
@@ -61,6 +61,20 @@ $context = context_module::instance($cm->id);
 require_login($course, true, $cm);
 require_capability('mod/lti:view', $context);
 
+// Mark viewed by user (if required).
+$completion = new completion_info($course);
+$completion->set_module_viewed($cm);
+
+$params = array(
+    'context' => $context,
+    'objectid' => $lti->id
+);
+$event = \mod_lti\event\course_module_viewed::create($params);
+$event->add_record_snapshot('course_modules', $cm);
+$event->add_record_snapshot('course', $course);
+$event->add_record_snapshot('lti', $lti);
+$event->trigger();
+
 $lti->cmid = $cm->id;
 lti_view($lti);
 
index 2803555..aa718d9 100644 (file)
@@ -362,6 +362,12 @@ function lti_build_request($instance, $typeconfig, $course, $typeid = null, $isl
         'context_label' => $course->shortname,
         'context_title' => $course->fullname,
     );
+    if (!empty($instance->id)) {
+        $requestparams['resource_link_id'] = $instance->id;
+    }
+    if (!empty($instance->resource_link_id)) {
+        $requestparams['resource_link_id'] = $instance->resource_link_id;
+    }
     if ($course->format == 'site') {
         $requestparams['context_type'] = 'Group';
     } else {
@@ -370,7 +376,7 @@ function lti_build_request($instance, $typeconfig, $course, $typeid = null, $isl
     }
     $placementsecret = $instance->servicesalt;
 
-    if ( isset($placementsecret) && ($islti2 ||
+    if ( !empty($instance->id) && isset($placementsecret) && ($islti2 ||
          $typeconfig['acceptgrades'] == LTI_SETTING_ALWAYS ||
          ($typeconfig['acceptgrades'] == LTI_SETTING_DELEGATE && $instance->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS))) {
 
@@ -910,7 +916,7 @@ function lti_get_ims_role($user, $cmid, $courseid, $islti2) {
         // a real LTI instance.
         $coursecontext = context_course::instance($courseid);
 
-        if (has_capability('moodle/course:manageactivities', $coursecontext)) {
+        if (has_capability('moodle/course:manageactivities', $coursecontext, $user)) {
             array_push($roles, 'Instructor');
         } else {
             array_push($roles, 'Learner');
diff --git a/mod/lti/service/memberships/classes/local/resource/contextmemberships.php b/mod/lti/service/memberships/classes/local/resource/contextmemberships.php
new file mode 100644 (file)
index 0000000..8889658
--- /dev/null
@@ -0,0 +1,137 @@
+<?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 contains a class definition for the Context Memberships resource
+ *
+ * @package    ltiservice_memberships
+ * @copyright  2015 Vital Source Technologies http://vitalsource.com
+ * @author     Stephen Vickers
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+
+namespace ltiservice_memberships\local\resource;
+
+use \mod_lti\local\ltiservice\service_base;
+use ltiservice_memberships\local\service\memberships;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * A resource implementing Context Memberships.
+ *
+ * @package    ltiservice_memberships
+ * @since      Moodle 3.0
+ * @copyright  2015 Vital Source Technologies http://vitalsource.com
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class contextmemberships extends \mod_lti\local\ltiservice\resource_base {
+
+    /**
+     * Class constructor.
+     *
+     * @param ltiservice_memberships\local\service\memberships $service Service instance
+     */
+    public function __construct($service) {
+
+        parent::__construct($service);
+        $this->id = 'ToolProxyBindingMemberships';
+        $this->template = '/{context_type}/{context_id}/bindings/{vendor_code}/{product_code}/{tool_code}/memberships';
+        $this->variables[] = 'ToolProxyBinding.memberships.url';
+        $this->formats[] =