Skip to content

Commit

Permalink
started Moodle 2.0 integration work.
Browse files Browse the repository at this point in the history
--HG--
extra : convert_revision : svn%3Afc1ba2b4-ac28-11dd-a772-7fcde58d38e6/trunk%40107
  • Loading branch information
mihai.sucan committed Aug 10, 2009
1 parent 089b1c0 commit 2956dd4
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 109 deletions.
17 changes: 17 additions & 0 deletions CHANGES
@@ -1,3 +1,20 @@
2009-08-10:
===========

- Started work on Moodle 2.0 integration. Moodle 1.9 integration has been
postponed for a number of reasons. Further details in an upcoming blog post at
robodesign.ro/mihai.

- New script for localization integration specific to Moodle 2.0 - see
update_tinymcelang.pnp.

- Improved/fixed gen_moodlelang.php.

- Moved the TODO file to the Google Code wiki:
http://code.google.com/p/paintweb/wiki/TODO

The wiki page includes new items and some fixes.

2009-08-03: 2009-08-03:
=========== ===========


Expand Down
96 changes: 0 additions & 96 deletions TODO

This file was deleted.

19 changes: 13 additions & 6 deletions ext/moodle/gen_moodlelang.php
Expand Up @@ -18,7 +18,7 @@
* along with PaintWeb. If not, see <http://www.gnu.org/licenses/>. * along with PaintWeb. If not, see <http://www.gnu.org/licenses/>.
* *
* $URL: http://code.google.com/p/paintweb $ * $URL: http://code.google.com/p/paintweb $
* $Date: 2009-08-03 19:46:27 +0300 $ * $Date: 2009-08-10 20:41:45 +0300 $
*/ */


// This script allows you to convert PaintWeb JSON language files into Moodle // This script allows you to convert PaintWeb JSON language files into Moodle
Expand All @@ -36,6 +36,8 @@
// Warning: running this script will overwrite "paintweb.php" in your Moodle // Warning: running this script will overwrite "paintweb.php" in your Moodle
// lang/*/ folders. // lang/*/ folders.


// This script works with Moodle 1.9 and Moodle 2.0.

$paintweblangdir = '../../build/lang'; $paintweblangdir = '../../build/lang';
$moodlelangdir = '../../../../lang'; $moodlelangdir = '../../../../lang';
$moodlelangfile = 'paintweb.php'; $moodlelangfile = 'paintweb.php';
Expand Down Expand Up @@ -72,17 +74,19 @@
* Convert a PaintWeb JSON language file to a Moodle PHP language file. * Convert a PaintWeb JSON language file to a Moodle PHP language file.
* *
* @param string $file The file you want to convert. * @param string $file The file you want to convert.
* @return boolean True if the operation executed successfully, or false
* otherwise.
*/ */
function paintweb_convert_json_file($file) { function paintweb_convert_json_file($file) {
global $moodlelangdir, $moodlelangfile, $paintweblangdir; global $moodlelangdir, $moodlelangfile, $paintweblangdir;


$lang = str_replace('.json', '', $file); $lang = basename($file, '.json');


$outputfolder = $moodlelangdir . '/' . ($lang === 'en' ? 'en_utf8' : $lang); $outputfolder = $moodlelangdir . '/' . ($lang === 'en' ? 'en_utf8' : $lang);


if (!is_dir($outputfolder)) { if (!is_dir($outputfolder)) {
echo "Skipping $file because $outputfolder was not found.\n"; echo "Skipping $file because $outputfolder was not found.\n";
continue; return false;
} }


$langparsed = file_get_contents($paintweblangdir . '/' . $file); $langparsed = file_get_contents($paintweblangdir . '/' . $file);
Expand All @@ -91,7 +95,7 @@ function paintweb_convert_json_file($file) {
$langparsed = json_decode($langparsed, true); $langparsed = json_decode($langparsed, true);
if (!$langparsed) { if (!$langparsed) {
echo "Parsing $file failed. \n"; echo "Parsing $file failed. \n";
continue; return false;
} }


$output = "<?php\n". paintweb_json2php($langparsed); $output = "<?php\n". paintweb_json2php($langparsed);
Expand All @@ -101,6 +105,8 @@ function paintweb_convert_json_file($file) {
} else { } else {
echo "Failed to write $outputfolder/$moodlelangfile\n"; echo "Failed to write $outputfolder/$moodlelangfile\n";
} }

return true;
} }


/** /**
Expand All @@ -118,8 +124,9 @@ function paintweb_json2php($obj, $prefix='') {
if (is_array($val)) { if (is_array($val)) {
$result .= paintweb_json2php($val, $prefix . $key . ':'); $result .= paintweb_json2php($val, $prefix . $key . ':');
} else { } else {
$val = str_replace("'", "\\'", $val); $key = var_export($prefix . $key, true);
$result .= "\$string['$prefix$key'] = '" . $val . "';\n"; $val = var_export($val, true);
$result .= "\$string[$key] = $val;\n";
} }
} }


Expand Down
4 changes: 3 additions & 1 deletion ext/moodle/gen_paintweblang.php
Expand Up @@ -18,7 +18,7 @@
* along with PaintWeb. If not, see <http://www.gnu.org/licenses/>. * along with PaintWeb. If not, see <http://www.gnu.org/licenses/>.
* *
* $URL: http://code.google.com/p/paintweb $ * $URL: http://code.google.com/p/paintweb $
* $Date: 2009-08-03 19:35:04 +0300 $ * $Date: 2009-08-10 15:58:48 +0300 $
*/ */


// This script allows you to convert a Moodle PHP language file into a PaintWeb // This script allows you to convert a Moodle PHP language file into a PaintWeb
Expand All @@ -32,6 +32,8 @@
// Warning: running this script will overwrite the JSON language file from the // Warning: running this script will overwrite the JSON language file from the
// PaintWeb language folder. // PaintWeb language folder.


// This script works with Moodle 1.9 and Moodle 2.0.

$paintweblangdir = '../../build/lang'; $paintweblangdir = '../../build/lang';
$moodlelangdir = '../../../../lang'; $moodlelangdir = '../../../../lang';
$moodlelangfile = 'paintweb.php'; $moodlelangfile = 'paintweb.php';
Expand Down
5 changes: 4 additions & 1 deletion ext/moodle/lang.json.php
Expand Up @@ -18,12 +18,15 @@
* along with PaintWeb. If not, see <http://www.gnu.org/licenses/>. * along with PaintWeb. If not, see <http://www.gnu.org/licenses/>.
* *
* $URL: http://code.google.com/p/paintweb $ * $URL: http://code.google.com/p/paintweb $
* $Date: 2009-08-03 21:17:30 +0300 $ * $Date: 2009-08-10 21:55:57 +0300 $
*/ */


// This script generates the PaintWeb JSON language file dynamically using the // This script generates the PaintWeb JSON language file dynamically using the
// Moodle language files. The language picked is the one configured in Moodle. // Moodle language files. The language picked is the one configured in Moodle.



// This script works with Moodle 1.9 and Moodle 2.0.

require_once('../../../../config.php'); require_once('../../../../config.php');


$moodlelangdir = '../../../../lang'; $moodlelangdir = '../../../../lang';
Expand Down
175 changes: 175 additions & 0 deletions ext/moodle/update_tinymcelang.php
@@ -0,0 +1,175 @@
<?php
/*
* Copyright (C) 2009 Mihai Şucan
*
* This file is part of PaintWeb.
*
* PaintWeb 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.
*
* PaintWeb 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 PaintWeb. If not, see <http://www.gnu.org/licenses/>.
*
* $URL: http://code.google.com/p/paintweb $
* $Date: 2009-08-10 21:12:30 +0300 $
*/

// This script allows you to update the TinyMCE language files from Moodle to
// hold the new strings of the TinyMCE plugin provided by PaintWeb.

// In a typical setup, Moodle holds PaintWeb in lib/paintweb.

// If you execute this script without any arguments, all the language files
// found in the TinyMCE plugin folder will be converted to the Moodle PHP
// language file format. The new strings will be appended to the
// editor_tinymce.php file from the Moodle languages folder.
//
// Optionally, you can give this script one argument, to tell it which language
// you want to update. This allows you to update only one language file, instead
// of all at once.

// This script works only with Moodle 2.0.

$pluginlangdir = '../tinymce-plugin/paintweb/langs';
$moodlelangdir = '../../../../lang';
$tinymcelangfile = 'editor_tinymce.php';

if (!is_dir($pluginlangdir)) {
echo "The TinyMCE plugin folder could not be found: $paintweblangdir\n";
return 1;
}

if (!is_dir($moodlelangdir)) {
echo "The Moodle folder could not be found: $moodlelangdir\n";
return 1;
}

if (isset($_SERVER['argv'][1])) {
$file = $_SERVER['argv'][1] . '.js';

if (!file_exists($pluginlangdir . '/' . $file)) {
echo "The TinyMCE plugin language file was not found: $pluginlangdir/$file\n";
return 1;
}

if (!paintweb_update_tinymcelang($file)) {
return 0;
} else {
return 1;
}
}

$files = glob($pluginlangdir . '/*.js');
foreach ($files as $file) {
echo "Reading $file.\n";
paintweb_update_tinymcelang($file);
}

/**
* Update a TinyMCE language file from the Moodle language folder
* (editor_tinymce.php) to include the language strings from the TinyMCE plugin
* provided by PaintWeb (see ext/tinymce-plugin/paintweb/langs).
*
* @param string $file The TinyMCE plugin language file (for example en.js) you
* want to read in order to update the associated editor_tinymce.php file from
* the Moodle language folder.
*
* @return boolean True if the operation was successful or false otherwise.
*/
function paintweb_update_tinymcelang($file) {
global $moodlelangdir, $tinymcelangfile, $pluginlangdir;

if (empty($file)) {
return false;
}

$file = basename($file);
$lang = substr($file, 0, 2);

$content = file_get_contents($pluginlangdir . '/' . $file);
if (empty($content)) {
echo "Failed to read $pluginlangdir/$file\n";
return false;
}

if ($lang === 'en') {
$filephp = "$moodlelangdir/en_utf8/$tinymcelangfile";
} else {
$filephp = "$moodlelangdir/$lang/$tinymcelangfile";
}

if (!file_exists($filephp)) {
echo "Skipping $lang. File not found: $filephp\n";
return false;
}

$output = file_get_contents($filephp);
if (empty($output)) {
echo "Failed to read $filephp\n";
return false;
}
include($filephp);

$group = '';
$content = explode("\n", $content);
$json = '';
foreach ($content as $line) {
$matches = array();

if (empty($line)) {
continue;
}

if (preg_match('/tinymce\.addi18n\s*\([\'"][a-z]+\.([^\']+)[\'"]\s*,\s*\{/i', $line, $matches)) {
$group = $matches[1];

} else if (!empty($group) && preg_match('/}\s*\)\s*;*/', $line)) {
$group = '';

} else if (!empty($group) && preg_match('/^\s*([\'"]*)(.+?)\1\s*:\s*([\'"])(.+?)\3\s*,?/', $line, $matches)) {
$json .= ',"' . $group . ':' . $matches[2] .'": "' . $matches[4] . "\"";
}
}

if (empty($json)) {
echo "No string was found in the language file $pluginlangdir/$file\n";
return false;
}

$json = json_decode('{' . substr($json, 1) . '}');

if (empty($json)) {
echo "Failed to parse the language file $pluginlangdir/$file\n";
return false;
}

foreach ($json as $key => $val) {
$key2 = var_export($key, true);
$val = var_export($val, true);
$line = "\$string[$key2] = $val;\n";

if (isset($string[$key])) {
$output = preg_replace('/^\$string[' . $key2 . '].+$/', $line, $output);
} else {
$output .= $line;
}
}

if (file_put_contents($filephp, $output)) {
echo "Updated $filephp.\n";
return true;
} else {
echo "Failed to update $filephp.\n";
return false;
}
}

// vim:set spell spl=en fo=tanqrowcb tw=80 ts=4 sw=4 sts=4 sta et noai nocin fenc=utf-8 ff=unix:

0 comments on commit 2956dd4

Please sign in to comment.