Skip to content

Commit

Permalink
Merge branch 'MDL-69582-master' of git://github.com/ferranrecio/moodl…
Browse files Browse the repository at this point in the history
…e into master
  • Loading branch information
stronk7 committed Oct 8, 2020
2 parents 2be470a + 40fecef commit 56be458
Show file tree
Hide file tree
Showing 13 changed files with 389 additions and 9 deletions.
68 changes: 68 additions & 0 deletions admin/tool/customlang/classes/form/export.php
@@ -0,0 +1,68 @@
<?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/>.

/**
* Creates Formular for customlang file export
*
* @package tool_customlang
* @copyright 2020 Thomas Wedekind <Thomas.Wedekind@univie.ac.at>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_customlang\form;

use tool_customlang_utils;

/**
* Formular for customlang file export
*
* @copyright 2020 Thomas Wedekind <Thomas.Wedekind@univie.ac.at>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class export extends \moodleform {

/**
* Add elements to form
*/
public function definition() {
$lng = $this->_customdata['lng'];
$mform = $this->_form;

$langdir = tool_customlang_utils::get_localpack_location($lng);

// The export button only appears if a local lang is present.
if (!check_dir_exists($langdir) || !count(glob("$langdir/*"))) {
print_error('nolocallang', 'tool_customlang');
}

$langfiles = scandir($langdir);
$fileoptions = [];
foreach ($langfiles as $file) {
if (substr($file, 0, 1) != '.') {
$fileoptions[$file] = $file;
}
}

$mform->addElement('hidden', 'lng', $lng);
$mform->setType('lng', PARAM_LANG);

$select = $mform->addElement('select', 'files', get_string('exportfilter', 'tool_customlang'), $fileoptions);
$select->setMultiple(true);
$mform->addRule('files', get_string('required'), 'required', null, 'client');
$mform->setDefault('files', $fileoptions);

$this->add_action_buttons(true, get_string('export', 'tool_customlang'));
}
}
5 changes: 3 additions & 2 deletions admin/tool/customlang/classes/output/renderer.php
Expand Up @@ -57,8 +57,9 @@ protected function render_tool_customlang_translator(\tool_customlang_translator
protected function render_tool_customlang_menu(\tool_customlang_menu $menu) {
$output = '';
foreach ($menu->get_items() as $item) {
$output .= $this->single_button($item->url, $item->title, $item->method);
$button = $this->single_button($item->url, $item->title, $item->method);
$output .= $this->box($button, 'menu');
}
return $this->box($output, 'menu');
return $output;
}
}
135 changes: 135 additions & 0 deletions admin/tool/customlang/cli/export.php
@@ -0,0 +1,135 @@
<?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/>.

/**
* Export custom language strings to zip files.
*
* @package tool_customlang
* @subpackage customlang
* @copyright 2020 Thomas Wedekind <thomas.wedekind@univie.ac.at>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define('CLI_SCRIPT', true);

require(__DIR__ . '/../../../../config.php');
require_once("$CFG->libdir/clilib.php");
require_once("$CFG->dirroot/$CFG->admin/tool/customlang/locallib.php");

$usage = <<<EOF
"Export custom language files to a target folder.
Useful for uploading custom langstrings to AMOS or importing or syncing them to other moodle instances.
Options:
-l, --lang Comma seperated language ids to export, default: all
-c, --components Comma seperated components to export, default: all
-t, --target Target to copy the zip files to, default: $CFG->tempdir/customlang
-o, --overwrite Overwrite existing files in the target folder.
Note: If the target is not set, the files are always overwritten!
-h, --help Print out this help
Examples:
Export all custom language files to the default folder:
\$ sudo -u www-data /usr/bin/php admin/tool/customlang/cli/export.php
Export just the english files of moodle core and the activity 'quiz' in a subfolder in my home folder:
\$ sudo -u www-data /usr/bin/php admin/tool/customlang/cli/export.php --lang='en' --components='moodle,quiz' --target='~/customdir'
EOF;

$dafaulttarget = "$CFG->tempdir/customlang/";

// Now get cli options.
list($options, $unrecognized) = cli_get_params(
[
'lang' => '',
'components' => '',
'target' => $dafaulttarget,
'overwrite' => false,
'help' => false,
],
['h' => 'help', 'c' => 'components', 't' => 'target', 'o' => 'overwrite']
);

if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
}

if ($options['help']) {
echo $usage;
die;
}
if (!file_exists($options['target'])) {
mkdir($options['target'], 0777, true);
}

cli_writeln(get_string('cliexportheading', 'tool_customlang'));
$langs = [];
if ($options['lang']) {
$langs = explode(',', $options['lang']);
} else {
// No language set. We export all installed languages.
$langs = array_keys(get_string_manager()->get_list_of_translations(true));
}

foreach ($langs as $lang) {
$filename = $options['target'] . get_string('exportzipfilename', 'tool_customlang', ['lang' => $lang]);
// If the file exists and we are not using the temp folder it requires an ovewrite.
if ($options['target'] != $dafaulttarget && file_exists($filename) && !$options['overwrite']) {
cli_problem(get_string('cliexportfileexists', 'tool_customlang', $lang));
continue;
}
cli_heading(get_string('cliexportstartexport', 'tool_customlang', $lang));
$langdir = tool_customlang_utils::get_localpack_location($lang);
if (!file_exists($langdir)) {
// No custom files set for this language set.
cli_writeln(get_string('cliexportnofilefoundforlang', 'tool_customlang', ['lang' => $lang]));
continue;
}
$zipper = get_file_packer();
$tempzip = tempnam($CFG->tempdir . '/', 'tool_customlang_export');
$filelist = [];
if ($options['components']) {
$components = explode(',', $options['components']);
foreach ($components as $component) {
$filepath = "$langdir/$component.php";
if (file_exists($filepath)) {
$filelist["$component.php"] = $filepath;
} else {
cli_problem(
get_string('cliexportfilenotfoundforcomponent', 'tool_customlang', ['lang' => $lang, 'file' => $filepath])
);
}
}
} else {
$langfiles = scandir($langdir);
foreach ($langfiles as $file) {
if (substr($file, 0, 1) != '.') {
$filelist[$file] = "$langdir/$file";
}
}
}
if (empty($filelist)) {
cli_problem(get_string('cliexportnofilefoundforlang', 'tool_customlang', ['lang' => $lang]));
continue;
}
if ($zipper->archive_to_pathname($filelist, $filename)) {
cli_writeln(get_string('cliexportzipdone', 'tool_customlang', $filename));
} else {
cli_problem(get_string('cliexportzipfail', 'tool_customlang', $filename));
}
}
9 changes: 9 additions & 0 deletions admin/tool/customlang/db/access.php
Expand Up @@ -46,5 +46,14 @@
'manager' => CAP_ALLOW
),
),
/* allows the user to export the current language customization */
'tool/customlang:export' => array(
'riskbitmask' => RISK_CONFIG,
'captype' => 'read',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'manager' => CAP_ALLOW
),
),

);
68 changes: 68 additions & 0 deletions admin/tool/customlang/export.php
@@ -0,0 +1,68 @@
<?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/>.

/**
* Performs the custom lang export.
*
* @package tool_customlang
* @subpackage customlang
* @copyright 2020 Thomas Wedekind <thomas.wedekind@univie.ac.at>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require(__DIR__ . '/../../../config.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/customlang/locallib.php');
require_once($CFG->libdir.'/adminlib.php');

global $PAGE, $CFG;

require_login(SITEID, false);
require_capability('tool/customlang:export', context_system::instance());

$lng = required_param('lng', PARAM_LANG);

admin_externalpage_setup('toolcustomlang', '', null,
new moodle_url('/admin/tool/customlang/import.php', ['lng' => $lng]));

$form = new \tool_customlang\form\export(null, ['lng' => $lng]);

if ($form->is_cancelled()) {
redirect('index.php');
die();
} else if ($formdata = $form->get_data()) {
$tempzip = tempnam($CFG->tempdir . '/', 'tool_customlang_export');
$filelist = [];
foreach ($formdata->files as $file) {
$filepath = tool_customlang_utils::get_localpack_location($lng). '/' . $file;
if (file_exists($filepath)) {
$filelist[$file] = $filepath;
}
}
$zipper = new zip_packer();

if (!empty($filelist) && $zipper->archive_to_pathname($filelist, $tempzip)) {
// Filename include the lang name so the file can be imported with automatic language detection.
send_temp_file($tempzip, "customlang_$lng.zip");
die();
}
}

$output = $PAGE->get_renderer('tool_customlang');

echo $output->header();
echo $output->heading(get_string('pluginname', 'tool_customlang'));
$form->display();
echo $OUTPUT->footer();
1 change: 0 additions & 1 deletion admin/tool/customlang/filter_form.php
Expand Up @@ -32,7 +32,6 @@ class tool_customlang_filter_form extends moodleform {

function definition() {
$mform = $this->_form;
$current = $this->_customdata['current'];

$mform->addElement('header', 'filtersettings', get_string('filter', 'tool_customlang'));

Expand Down
14 changes: 12 additions & 2 deletions admin/tool/customlang/index.php
Expand Up @@ -35,6 +35,7 @@
$action = optional_param('action', '', PARAM_ALPHA);
$confirm = optional_param('confirm', false, PARAM_BOOL);
$lng = optional_param('lng', '', PARAM_LANG);
$next = optional_param('next', 'edit', PARAM_ALPHA);

admin_externalpage_setup('toolcustomlang');
$langs = get_string_manager()->get_list_of_translations();
Expand All @@ -59,11 +60,10 @@
raise_memory_limit(MEMORY_EXTRA);
tool_customlang_utils::checkout($lng, $progressbar);

echo $output->continue_button(new moodle_url('/admin/tool/customlang/edit.php', array('lng' => $lng)), 'get');
echo $output->continue_button(new moodle_url("/admin/tool/customlang/{$next}.php", array('lng' => $lng)), 'get');
echo $output->footer();
exit;
}

if ($action === 'checkin') {
require_sesskey();
require_capability('tool/customlang:edit', context_system::instance());
Expand Down Expand Up @@ -133,6 +133,16 @@
);
}
}
if (has_capability('tool/customlang:export', context_system::instance())) {
$langdir = tool_customlang_utils::get_localpack_location($lng);
if (check_dir_exists(dirname($langdir)) && count(glob("$langdir/*"))) {
$menu['export'] = [
'title' => get_string('export', 'tool_customlang'),
'url' => new moodle_url("/admin/tool/customlang/export.php", ['lng' => $lng]),
'method' => 'post',
];
}
}
echo $output->render(new tool_customlang_menu($menu));

echo $output->footer();
11 changes: 11 additions & 0 deletions admin/tool/customlang/lang/en/tool_customlang.php
Expand Up @@ -30,9 +30,19 @@
$string['checkout'] = 'Open language pack for editing';
$string['checkoutdone'] = 'Language pack loaded';
$string['checkoutinprogress'] = 'Loading language pack';
$string['cliexportfileexists'] = 'File for {$a->lang} already exists, skipping. If you want to overwrite add the --override=true option.';
$string['cliexportheading'] = 'Starting to export lang files.';
$string['cliexportnofilefoundforlang'] = 'No file found to export. Skipping export for this language.';
$string['cliexportfilenotfoundforcomponent'] = 'File {$a->filepath} not found for language {$a->lang}.Skipping this file.';
$string['cliexportstartexport'] = 'Exporting language "{$a}"';
$string['cliexportzipdone'] = 'Zip created: {$a}';
$string['cliexportzipfail'] = 'Cannot create zip {$a}';
$string['confirmcheckin'] = 'You are about to save modifications to your local language pack. This will export the customised strings from the translator into your site data directory and your site will start using the modified strings. Press \'Continue\' to proceed with saving.';
$string['customlang:edit'] = 'Edit local translation';
$string['customlang:export'] = 'Export local translation';
$string['customlang:view'] = 'View local translation';
$string['export'] = 'Export custom strings';
$string['exportfilter'] = 'Select component(s) to export';
$string['filter'] = 'Filter strings';
$string['filtercomponent'] = 'Show strings of these components';
$string['filtercustomized'] = 'Customised only';
Expand All @@ -50,6 +60,7 @@
$string['markuptodate'] = 'mark as up-to-date';
$string['modifiedno'] = 'There are no modified strings to save.';
$string['modifiednum'] = 'There are {$a} modified strings. Do you wish to save these changes to your local language pack?';
$string['nolocallang'] = 'No local strings found.';
$string['nostringsfound'] = 'No strings found, please modify the filter settings';
$string['placeholder'] = 'Placeholders';
$string['placeholder_help'] = 'Placeholders are special statements like `{$a}` or `{$a->something}` within the string. They are replaced with a value when the string is actually printed.
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/customlang/locallib.php
Expand Up @@ -243,7 +243,7 @@ public static function checkin($lang) {
* @param string $lang language code
* @return string full path
*/
protected static function get_localpack_location($lang) {
public static function get_localpack_location($lang) {
global $CFG;

return $CFG->langlocalroot.'/'.$lang.'_local';
Expand Down
4 changes: 2 additions & 2 deletions admin/tool/customlang/templates/translator.mustache
Expand Up @@ -129,8 +129,8 @@
<strong>{{#str}}headinglocal, tool_customlang{{/str}}</strong>
</div>
<div class="py-2 py-md-0 px-md-3">
<textarea class="form-control w-100 border-box" name="cust[{{id}}]" cols="40" rows="3">{{{ local }}}</textarea>

<label for="{{id}}" class="sr-only sr-only-focusable">{{{ component }}}/{{{ stringid }}}</label>
<textarea class="form-control w-100 border-box" id="{{id}}" name="cust[{{id}}]" cols="40" rows="3">{{{ local }}}</textarea>
{{#checkupdated}}
<div class="uptodatewrapper">
<div class="form-check">
Expand Down

0 comments on commit 56be458

Please sign in to comment.