Permalink
Browse files

Continued Moodle 2.0 integration work. PaintWeb is now able to work f…

…ine inside Moodle: image save works properly.

--HG--
extra : convert_revision : svn%3Afc1ba2b4-ac28-11dd-a772-7fcde58d38e6/trunk%40108
  • Loading branch information...
1 parent 2956dd4 commit 6b83d5a977506af260ddde86b9449117f2d8b4da mihai.sucan committed Aug 12, 2009
View
@@ -1,3 +1,26 @@
+2009-08-12:
+===========
+
+- Modified the Makefile such that "make moodle" creates an archive which only
+holds the important parts of PaintWeb - without the docs, tests and demos.
+(Suggested by Martin Langhoff, my mentor.)
+
+- Changed the styling of the "plugin bar" of the TinyMCE plugin - the one
+displayed at the top of PaintWeb, when it runs inside TinyMCE. The background
+color is now yellow, and the two Save/Cancel actions use inputs of type=button.
+Thanks to Olli Savolainen.
+
+- The TinyMCE plugin now sets up a reference to the current TinyMCE editor
+instance in the current PaintWeb.config instance. This allows extensions from
+inside PaintWeb to access the TinyMCE editor - using
+PaintWeb.config.tinymceEditor. This is used by the updated Moodle extension.
+
+- Updated the Moodle extension to support image saving inside Moodle 2.0.
+
+- Created a new imagesave20.php script which implements the image save operation
+for Moodle 2.0. This script saves images inside the user_draft file area.
+
+
2009-08-10:
===========
View
@@ -18,7 +18,7 @@
# along with PaintWeb. If not, see <http://www.gnu.org/licenses/>.
#
# $URL: http://code.google.com/p/paintweb $
-# $Date: 2009-07-16 18:43:06 +0300 $
+# $Date: 2009-08-12 12:52:24 +0300 $
#### Config:start #####################################################
@@ -193,6 +193,11 @@ tags:
# Generate a custom Moodle build.
moodle: EXTENSIONS=colormixer moodle
moodle: all
+ tar --exclude=".*" --exclude="*~" --exclude="*bak" --exclude="*bz2" --exclude="tags" \
+ --exclude="paintweb/docs" --exclude="paintweb/demos" --exclude="paintweb/tests" --exclude="paintweb/scripts" \
+ -C $(FOLDER_PARENT) -cjvf /tmp/paintweb.tar.bz2 $(FOLDER_SELF)
+ mv /tmp/paintweb.tar.bz2 ./paintweb-$(BUILD_VERSION)-$(BUILD_DATE)-moodle.tar.bz2
+
# vim:set spell spl=en fo=wan1croql tw=80 ts=2 sw=2 sts=0 sta noet ai cin fenc=utf-8 ff=unix:
@@ -18,13 +18,15 @@
* along with PaintWeb. If not, see <http://www.gnu.org/licenses/>.
*
* $URL: http://code.google.com/p/paintweb $
- * $Date: 2009-08-03 22:03:08 +0300 $
+ * $Date: 2009-08-11 14:29:15 +0300 $
*/
// This script performs asynchronous image save in PaintWeb. This is used by the
// Moodle extension of PaintWeb, to save image edits. You should not include
// this script yourself.
+// This script only works with Moodle 1.9.
+
require_once('../../../../config.php');
/**
@@ -0,0 +1,186 @@
+<?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-12 17:24:43 +0300 $
+ */
+
+// This script performs asynchronous image save in PaintWeb. This is used by the
+// Moodle extension of PaintWeb, to save image edits. You should not include
+// this script yourself.
+
+// This script only works with Moodle 2.0.
+
+
+// The Moodle extension (see paintweb/src/extensions/moodle.js) calls this
+// script with several parameters:
+// - url: the URL of the image being edited. '-' is used for images with data
+// URLs.
+//
+// - dataURL: the dataURL generated by the browser. This holds the
+// base64-encoded content of the image.
+//
+// - contextid and draftitemid: both are used when saving the new image file
+// inside the user_draft file area of Moodle 2.0. If the draftitemid/contextid
+// values are missing, then new values will be determined.
+
+require_once('../../../../config.php');
+require_once('../../../../lib/filelib.php');
+require_once('../../../../repository/lib.php');
+
+/**
+ * Send the JSON object result to PaintWeb.
+ *
+ * @param string $url The image URL we are saving/updating.
+ * @param string $urlnew The new image URL generated for the saved image.
+ * @param boolean $successful Tells if the save operation was successful or not.
+ * @param string $errormessage Holds an error message if the save operation
+ * failed.
+ */
+function paintweb_send_result($url, $urlnew, $successful, $errormessage=null) {
+ $output = array(
+ 'successful' => $successful,
+ 'url' => $url,
+ 'urlNew' => $urlnew,
+ 'errorMessage' => $errormessage
+ );
+
+ echo json_encode($output);
+ exit;
+}
+
+// Files saved by PaintWeb are stored in the draft area.
+$filearea = 'user_draft';
+$filepath = '/'; // ... in the root folder
+
+// The list of allowed image MIME types associated to file extensions.
+$imgallowedtypes = array(
+ 'image/png' => 'png',
+ 'image/jpeg' => 'jpg'
+);
+
+$imgurl = optional_param('url', '', PARAM_URL);
+if (empty($imgurl)) {
+ $imgurl = '-';
+}
+
+$imgdataurl = required_param('dataURL', PARAM_RAW);
+
+$draftitemid = optional_param('draftitemid', '', PARAM_INT);
+if (empty($draftitemid)) {
+ $draftitemid = (int)substr(hexdec(uniqid()), 0, 9) + rand(1, 100);
+}
+
+$context = get_context_instance(CONTEXT_USER, $USER->id);
+$contextid = optional_param('contextid', SITEID, PARAM_INT);
+$imgurlnew = null;
+
+if (empty($contextid)) {
+ $contextid = $context->id;
+}
+
+if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isloggedin() ||
+ !repository::check_context($contextid)) {
+ paintweb_send_result($imgurl, $imgurlnew, false,
+ get_string('moodleServer:permissionDenied', 'paintweb'));
+}
+
+if (empty($imgdataurl)) {
+ paintweb_send_result($imgurl, $imgurlnew, false,
+ get_string('moodleServer:saveEmptyDataUrl', 'paintweb'));
+}
+
+// A data URL starts like this:
+// data:[<MIME-type>][;charset="<encoding>"][;base64],<data>
+
+// Here we find the comma delimiter.
+$comma = strpos($imgdataurl, ',');
+if (!$comma) {
+ paintweb_send_result($imgurl, $imgurlnew, false,
+ get_string('moodleServer:malformedDataUrl', 'paintweb'));
+}
+
+$imginfo = substr($imgdataurl, 0, $comma);
+if (empty($imginfo) || !isset($imgdataurl{($comma+2)})) {
+ paintweb_send_result($imgurl, $imgurlnew, false,
+ get_string('moodleServer:malformedDataUrl', 'paintweb'));
+}
+
+// Split by ':' to find the 'data' prefix and the rest of the info.
+$imginfo = explode(':', $imginfo);
+
+// The array must have exactly two elements and the second element must not be
+// empty.
+if (count($imginfo) !== 2 || $imginfo[0] !== 'data' || empty($imginfo[1])) {
+ paintweb_send_result($imgurl, $imgurlnew, false,
+ get_string('moodleServer:malformedDataUrl', 'paintweb'));
+}
+
+// The MIME type must be given and it must be base64-encoded.
+$imginfo = explode(';', $imginfo[1]);
+
+if (count($imginfo) < 2 || !array_key_exists($imginfo[0], $imgallowedtypes) ||
+ ($imginfo[1] !== 'base64' && $imginfo[2] !== 'base64')) {
+ paintweb_send_result($imgurl, $imgurlnew, false,
+ get_string('moodleServer:malformedDataUrl', 'paintweb'));
+}
+
+$imgdata = base64_decode(substr($imgdataurl, $comma + 1));
+$imgdataurl = null;
+
+$filename = 'paintweb_' . sha1($imgdata) . '.' . $imgallowedtypes[$imginfo[0]];
+
+// Save the file using the new File API.
+
+$fs = get_file_storage();
+$fbrowser = get_file_browser();
+
+$file_record = new object();
+$file_record->contextid = $context->id;
+$file_record->filearea = $filearea;
+$file_record->itemid = $draftitemid;
+$file_record->filepath = $filepath;
+$file_record->filename = $filename;
+$file_record->userid = $USER->id;
+
+try {
+ $file = $fs->create_file_from_string($file_record, $imgdata);
+} catch (Exception $err) {
+ paintweb_send_result($imgurl, $imgurlnew, false, $err->getMessage());
+}
+
+$binfo = $fbrowser->get_file_info($context, $file->get_filearea(),
+ $file->get_itemid(), $file->get_filepath(), $file->get_filename());
+
+if (empty($binfo)) {
+ paintweb_send_result($imgurl, $imgurlnew, false,
+ get_string('moodleServer:saveFailed', 'paintweb'));
+}
+
+$imgurlnew = $binfo->get_url();
+
+if (empty($imgurlnew)) {
+ paintweb_send_result($imgurl, $imgurlnew, false,
+ get_string('moodleServer:saveFailed', 'paintweb'));
+}
+
+paintweb_send_result($imgurl, $imgurlnew, true);
+
+// vim:set spell spl=en fo=tanqrowcb tw=80 ts=4 sw=4 sts=4 sta et noai nocin fenc=utf-8 ff=unix:
+
@@ -18,11 +18,13 @@
* along with PaintWeb. If not, see <http://www.gnu.org/licenses/>.
*
* $URL: http://code.google.com/p/paintweb $
- * $Date: 2009-08-03 19:35:31 +0300 $
+ * $Date: 2009-08-11 14:29:31 +0300 $
*/
// This script serves images saved by PaintWeb to the browser.
+// This script only works with Moodle 1.9.
+
require_once('../../../../config.php');
require_once('../../../../lib/filelib.php');
@@ -17,7 +17,7 @@
* along with PaintWeb. If not, see <http://www.gnu.org/licenses/>.
*
* $URL: http://code.google.com/p/paintweb $
- * $Date: 2009-07-30 21:47:45 +0300 $
+ * $Date: 2009-08-12 18:42:12 +0300 $
*/
/**
@@ -171,6 +171,9 @@ function paintwebLoaded () {
paintwebConfig[prop] = config[prop];
}
+ // Give PaintWeb access to the TinyMCE editor instance.
+ paintwebConfig.tinymceEditor = targetEditor;
+
paintwebInstance.init(paintwebInitialized);
};
@@ -208,23 +211,17 @@ function paintwebInitialized (ev) {
/**
* The <code>click</code> event handler for the Save button displayed on the
* plugin bar.
- *
- * @param {Event} ev The DOM Event object.
*/
-function pluginSaveButton (ev) {
- ev.preventDefault();
+function pluginSaveButton () {
pwSaveReturn = true;
paintwebInstance.imageSave();
};
/**
* The <code>click</code> event handler for the Cancel button displayed on the
* plugin bar.
- *
- * @param {Event} ev The DOM Event object.
*/
-function pluginCancelButton (ev) {
- ev.preventDefault();
+function pluginCancelButton () {
paintwebHide();
};
@@ -336,6 +333,8 @@ function paintwebEditStart () {
}
if (paintwebInstance) {
+ // Give PaintWeb access to the TinyMCE editor instance.
+ paintwebConfig.tinymceEditor = targetEditor;
paintwebInstance.imageLoad(targetImage);
paintwebShow();
} else {
@@ -737,25 +736,22 @@ tinymce.create('tinymce.plugins.paintweb', {
if (config.tinymce.pluginBar) {
pluginBar = document.createElement('div');
- var saveBtn = document.createElement('a'),
- cancelBtn = document.createElement('a'),
+ var saveBtn = document.createElement('input'),
+ cancelBtn = document.createElement('input'),
textSpan = document.createElement('span');
+ saveBtn.type = 'button';
saveBtn.className = 'paintweb_tinymce_save';
- saveBtn.href = '#';
saveBtn.title = ed.getLang('paintweb.imageSaveButtonTitle',
'Save the image and return to TinyMCE.');
-
- saveBtn.appendChild(document.createTextNode(ed.getLang('paintweb.imageSaveButton',
- 'Save')));
+ saveBtn.value = ed.getLang('paintweb.imageSaveButton', 'Save');
saveBtn.addEventListener('click', pluginSaveButton, false);
+ cancelBtn.type = 'button';
cancelBtn.className = 'paintweb_tinymce_cancel';
- cancelBtn.href = '#';
cancelBtn.title = ed.getLang('paintweb.cancelEditButtonTitle',
'Cancel image edits and return to TinyMCE.');
- cancelBtn.appendChild(document.createTextNode(ed.getLang('paintweb.cancelEditButton',
- 'Cancel')));
+ cancelBtn.value = ed.getLang('paintweb.cancelEditButton', 'Cancel');
cancelBtn.addEventListener('click', pluginCancelButton, false);
pluginBar.className = 'paintweb_tinymce_status';
Oops, something went wrong. Retry.

0 comments on commit 6b83d5a

Please sign in to comment.