MDL-67184 filter_displayh5p: Move upper than urltolink & activityname

This commit is contained in:
cescobedo 2019-11-10 21:40:25 +01:00
parent f9db5892ec
commit 64e97710a3
5 changed files with 186 additions and 4 deletions

View File

@ -31,8 +31,8 @@ defined('MOODLE_INTERNAL') || die();
function xmldb_filter_displayh5p_install() {
global $CFG;
require_once("$CFG->libdir/filterlib.php");
require_once($CFG->dirroot . '/filter/displayh5p/db/upgradelib.php');
// Display H5P filter should be enabled by default because we need this filter for H5P atto button to work.
filter_set_global_state('displayh5p', TEXTFILTER_ON, -1);
// We need to move up the displayh5p filter over urltolink and activitynames filters to works properly.
filter_displayh5p_reorder();
}

View File

@ -0,0 +1,46 @@
<?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/>.
/**
* Display H5P upgrade code
*
* @package filter_displayh5p
* @copyright 2019 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* xmldb_filter_displayh5p_upgrade
*
* @param int $oldversion the version we are upgrading from
* @return bool result
*/
function xmldb_filter_displayh5p_upgrade($oldversion) {
global $CFG;
require_once($CFG->dirroot . '/filter/displayh5p/db/upgradelib.php');
if ($oldversion < 2019110800) {
// We need to move up the displayh5p filter over urltolink and activitynames filters to works properly.
filter_displayh5p_reorder();
upgrade_plugin_savepoint(true, 2019110800, 'filter', 'displayh5p');
}
return true;
}

View File

@ -0,0 +1,55 @@
<?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/>.
/**
* Display H5P functions to install and upgrade the filter.
*
* @package filter_displayh5p
* @copyright 2019 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->libdir/filterlib.php");
/**
* Move up the displayh5p filter over urltolink and activitynames filters to works properly.
* Also, displayh5p have to be enabled in order to display H5P content.
*
* @return void
*/
function filter_displayh5p_reorder() {
// The filter enabled is mandatory to be able to display the H5P content.
filter_set_global_state('displayh5p', TEXTFILTER_ON);
$states = filter_get_global_states();
$displayh5ppos = $states['displayh5p']->sortorder;
$activitynamespos = 1;
if (!empty($states['activitynames'])) {
$activitynamespos = $states['activitynames']->sortorder;
}
$urltolinkpos = 1;
if (!empty($states['urltolink'])) {
$urltolinkpos = $states['urltolink']->sortorder;
}
$minpos = ($activitynamespos < $urltolinkpos) ? $activitynamespos : $urltolinkpos;
while ($minpos < $displayh5ppos) {
filter_set_global_state('displayh5p', TEXTFILTER_ON, -1);
$displayh5ppos--;
}
}

View File

@ -0,0 +1,81 @@
<?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/>.
/**
* Unit tests for the upgradelib of the Display H5P filter.
*
* @package filter_displayh5p
* @category test
* @copyright 2019 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/filter/displayh5p/db/upgradelib.php');
require_once("$CFG->libdir/filterlib.php");
/**
* Unit tests for the upgradelib of the Display H5P filter.
*
* @copyright 2019 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filter_displayh5p_upgradelib_testcase extends advanced_testcase {
/**
* test_filter_displayh5p_reorder
*/
public function test_filter_displayh5p_reorder() {
$this->resetAfterTest(true);
$this->setAdminUser();
// We disable the three filters involved to reorder.
// To do this, we will enable them in order and so they will be placed sorted.
filter_set_global_state('displayh5p', TEXTFILTER_DISABLED);
filter_set_global_state('activitynames', TEXTFILTER_DISABLED);
filter_set_global_state('urltolink', TEXTFILTER_DISABLED);
// First, we enabled activitynames and urltolink.
// So, displayh5p will be below them.
filter_set_global_state('activitynames', TEXTFILTER_ON);
filter_set_global_state('urltolink', TEXTFILTER_ON);
// We get the new order of the filter.
$states = filter_get_global_states();
$displayh5ppos = $states['displayh5p']->sortorder;
$activitynamespos = $states['activitynames']->sortorder;
$urltolinkpos = $states['urltolink']->sortorder;
// Make sure that activitynames and urltolink are over the displayh5p.
$this->assertLessThan($displayh5ppos, $activitynamespos);
$this->assertLessThan($displayh5ppos, $urltolinkpos);
// Call the function to reorder displayh5p.
filter_displayh5p_reorder();
// Get the new orders.
$states = filter_get_global_states();
$displayh5ppos = $states['displayh5p']->sortorder;
$activitynamespos = $states['activitynames']->sortorder;
$urltolinkpos = $states['urltolink']->sortorder;
// Make sure that displayh5p are over activitynames and urltolink.
$this->assertLessThan($activitynamespos, $displayh5ppos);
$this->assertLessThan($urltolinkpos, $displayh5ppos);
// Make sure that displayh5p is enabled.
$this->assertEquals(TEXTFILTER_ON, $states['displayh5p']->active);
}
}

View File

@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die;
$plugin->version = 2019101800;
$plugin->version = 2019110800;
$plugin->requires = 2019092000;
$plugin->component = 'filter_displayh5p';