{{!
    This file is part of Moodle - https://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/>.
}}
{{!
    @template file/mod/backup/moodle2/backup_activity_task_class

    Activity backup/moodle2/backup_<modname>_activity_task.class.php file.

    Context variables required for this template:
    * component
    * copyright

    Example context (json):
    {
        "component": "local_test",
        "copyright": "2021 David Mudrák <david@moodle.com>"
    }
}}
{{< common/backup_php }}
{{$ description}}The task that provides all the steps to perform a complete backup is defined here.{{/ description }}
{{$ package }}{{ component }}{{/ package }}
{{$ copyright }}{{ copyright }}{{/ copyright }}
{{/ common/backup_php }}

require_once($CFG->dirroot.'/{{ component_root }}/{{ component_name }}/backup/moodle2/backup_{{ component_name }}_stepslib.php');
{{# self.has_settingslib }}
require_once($CFG->dirroot.'/{{ component_root }}/{{ component_name }}/backup/moodle2/backup_{{ component_name }}_settingslib.php');
{{/ self.has_settingslib }}

/**
 * Provides all the settings and steps to perform a complete backup of {{ component }}.
 */
class backup_{{ component_name }}_activity_task extends backup_activity_task {

    /**
     * Defines particular settings for the plugin.
     */
    protected function define_my_settings() {
        return;
    }

    /**
     * Defines particular steps for the backup process.
     */
    protected function define_my_steps() {
        $this->add_step(new backup_{{ component_name }}_activity_structure_step('{{ component_name }}_structure', '{{ component_name }}.xml'));
    }

    /**
     * Codes the transformations to perform in the activity in order to get transportable (encoded) links.
     *
     * @param string $content
     * @return string
     */
    public static function encode_content_links($content) {
        global $CFG;

        $base = preg_quote($CFG->wwwroot, '/');

        // Link to the list of choices.
        $search = "/(".$base."\/{{ component_root }}\/{{ component_name }}\/index.php\?id\=)([0-9]+)/";
        $content = preg_replace($search, '$@{{ self.component_name_all_caps }}INDEX*$2@$', $content);

        // Link to choice view by moduleid.
        $search = "/(".$base."\/{{ component_root }}\/{{ component_name }}\/view.php\?id\=)([0-9]+)/";
        $content = preg_replace($search, '$@{{ self.component_name_all_caps }}VIEWBYID*$2@$', $content);

        return $content;
    }
}
