Skip to content

Commit

Permalink
Brought up to standard for 2.6 release
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Oct 27, 2013
1 parent d6b0284 commit 3b5b1f9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
6 changes: 4 additions & 2 deletions ajax.php
Expand Up @@ -8,7 +8,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define('AJAX_SCRIPT', 1);
define('AJAX_SCRIPT', true);

require_once('../../config.php');
require_once($CFG->dirroot.'/blocks/css_theme_tool/lib.php');
Expand All @@ -31,15 +31,17 @@
$styles = required_param_array('styles', PARAM_NOTAGS);
css_theme_tool::update_via_ajax($rules, $styles);
break;

case 'purge':
// Purge the rules in the database
css_theme_tool::purge_rules();
break;

case 'exportcss':
// Export the CSS
$blockid = required_param('id', PARAM_INT);
$blockinstance = $DB->get_record('block_instances', array('id'=>$blockid), '*', MUST_EXIST);
$blockcontext = get_context_instance(CONTEXT_BLOCK, $blockinstance->id);
$blockcontext = context_block::instance($blockinstance->id);
css_theme_tool::cache_css($blockcontext);
$filepath = css_theme_tool::get_cached_file_url($blockcontext);
echo $filepath;
Expand Down
17 changes: 8 additions & 9 deletions block_css_theme_tool.php
Expand Up @@ -19,23 +19,21 @@
*/
class block_css_theme_tool extends block_base {

protected $visualversion = '0.3.0';
protected $visualversion = '0.4.0';

function init() {
public function init() {
$this->title = get_string('pluginname', 'block_css_theme_tool');
}

function applicable_formats() {
public function applicable_formats() {
return array('all' => true);
}

function instance_allow_multiple() {
public function instance_allow_multiple() {
return false;
}

function get_content() {
global $USER;

public function get_content() {
if ($this->content !== NULL) {
return $this->content;
}
Expand Down Expand Up @@ -148,9 +146,10 @@ public function initialise_javascript() {

public function send_file($context, $filearea, $itemid, $filepath, $filename) {
$fs = get_file_storage();
$file = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename);
$file = $fs->get_file($context->id, 'block_css_theme_tool', $filearea, $itemid, $filepath, $filename);
if ($file) {
session_get_instance()->write_close(); // unlock session during fileserving
$session = new \core\session\manager();
$session->write_close(); // unlock session during file serving.
send_stored_file($file, 0, 0, true); // must force download - security!
} else {
send_file_not_found();
Expand Down
3 changes: 1 addition & 2 deletions colourpicker_image.php
Expand Up @@ -34,8 +34,7 @@ function produce_colorpicker() {
$matrixcount = count($matrices);
$limit = ceil($width/$matrixcount);
$heightbreak = floor($height/2);
$heightgap = 0;


header("Content-type: image/png");
$image = imagecreatetruecolor($width, $height);
imagecolorallocate($image, 0, 0, 0);
Expand Down
51 changes: 31 additions & 20 deletions lib.php
Expand Up @@ -50,15 +50,15 @@ public function get_rules() {
}

/**
* Gets all of the rules for this css theme tool and returns them in
* an array suitable for return as JSON
* Gets all of the rules for this css theme tool and returns them in an array suitable for return as JSON.
*
* @return array
*/
public static function get_rules_for_json($currentuseronly=false) {
global $USER;
$tool = new self();
$rules = $tool->get_rules();
foreach ($rules as $key=>&$rule) {
foreach ($rules as $key => &$rule) {
if ($currentuseronly && $rule->userid != $USER->id) {
unset($rules[$key]);
continue;
Expand Down Expand Up @@ -105,13 +105,14 @@ public static function get_cached_file_url($context) {
}

/**
* Caches the CSS and returns the file so it can be used
* Caches the CSS and returns the file so it can be used.
*
* @return stored_file
*/
public static function cache_css($blockcontext) {
$tool = new self();
$rules = $tool->get_rules();
$css = "/** Created by CSS theme tool **/\n";
$css = "/** Created by the CSS theme tool block **/\n";
foreach ($rules as $rule) {
$css .= $rule->selector.' {';
foreach ($rule->styles as $style) {
Expand All @@ -121,14 +122,15 @@ public static function cache_css($blockcontext) {
}

$cssfile = array(
'contextid'=>$blockcontext->id,
'contextid' => $blockcontext->id,
'component' => 'block_css_theme_tool',
'filearea'=>'cached_css',
'itemid'=>0,
'filepath'=>'/',
'filename'=>'css_theme_tool_export.css',
'timecreated'=>time(),
'timemodified'=>time());
'filearea' => 'cached_css',
'itemid' => 0,
'filepath' => '/',
'filename' => 'css_theme_tool_export.css',
'timecreated' => time(),
'timemodified' => time()
);
$fs = get_file_storage();
if ($file = $fs->get_file($cssfile['contextid'], $cssfile['component'], $cssfile['filearea'], $cssfile['itemid'], $cssfile['filepath'], $cssfile['filename'])) {
$file->delete();
Expand All @@ -140,7 +142,7 @@ public static function cache_css($blockcontext) {
* Updates the rules stored in the database with those you have created
*
* @global moodle_database $DB
* @param array $rules
* @param css_theme_tool_rule[] $rules
*/
public function update_rules($rules) {
global $DB;
Expand Down Expand Up @@ -179,7 +181,7 @@ public function update_rules($rules) {
*/
public static function update_via_ajax($ajaxrules, $ajaxstyles) {
$rules = array();
foreach ($ajaxrules as $key=>$selector) {
foreach ($ajaxrules as $key => $selector) {
$rule = new css_theme_tool_rule();
$rule->create($selector, explode('@@@', $ajaxstyles[$key]));
$rules[] = $rule;
Expand Down Expand Up @@ -213,21 +215,25 @@ class css_theme_tool_rule {
* @var int
*/
public $id = null;

/**
* The selector as a string
* @var string
*/
public $selector;

/**
* The time this rule was last modified
* @var int
*/
public $timemodified;

/**
* The ID of the user who created this rule
* @var int
*/
public $userid;

/**
* All of the styles associated with this object {@see css_theme_tool_style}
* @var array
Expand All @@ -243,7 +249,7 @@ class css_theme_tool_rule {
* @param array $styles
* @return bool
*/
public function load($id, stdClass $rule=null, array $styles = null) {
public function load($id, stdClass $rule = null, array $styles = null) {
global $DB;
if ($rule == null) {
$rule = $DB->get_record('block_css_theme_tool', array('id'=>$id), '*', MUST_EXIST);
Expand All @@ -255,6 +261,7 @@ public function load($id, stdClass $rule=null, array $styles = null) {
$this->styles = css_theme_tool_style::load_for_rule($this, $styles);
return true;
}

/**
* Creates this object from the given selector and rules
* @param string $selector
Expand All @@ -268,6 +275,7 @@ public function create($selector, array $styles) {
$this->userid = $USER->id;
$this->styles = css_theme_tool_style::create_for_rule($this, $styles);
}

/**
* Inserts or Updates the rule in the database
* @global moodle_database $DB
Expand All @@ -281,7 +289,7 @@ public function save() {
$this->id = $DB->insert_record('block_css_theme_tool', $this);
}

$DB->delete_records('block_css_theme_tool_styles', array('ruleid'=>$this->id));
$DB->delete_records('block_css_theme_tool_styles', array('ruleid' => $this->id));
foreach ($this->styles as $style) {
$style->ruleid = $this->id;
$style->save();
Expand All @@ -302,21 +310,25 @@ class css_theme_tool_style {
* @var int
*/
public $id;

/**
* The ID of the rule this style belongs too
* @var int
*/
public $ruleid;

/**
* The rule this style belongs too
* @var css_theme_tool_rule
*/
public $rule;

/**
* The name of this style
* @var string
*/
public $name;

/**
* The value for this style
* @var string
Expand All @@ -331,7 +343,7 @@ class css_theme_tool_style {
* @param array $dbstyles
* @return array
*/
public static function load_for_rule(css_theme_tool_rule $rule, array $dbstyles=null) {
public static function load_for_rule(css_theme_tool_rule $rule, array $dbstyles = null) {
global $DB;
$styles = Array();
if ($dbstyles == null) {
Expand Down Expand Up @@ -405,8 +417,6 @@ function block_css_theme_tool_pluginfile($course, $birecord, $context, $filearea
}

$fs = get_file_storage();


$filename = array_pop($args);
$itemid = array_pop($args);
$filepath = $args ? '/'.implode('/', $args).'/' : '/';
Expand All @@ -415,6 +425,7 @@ function block_css_theme_tool_pluginfile($course, $birecord, $context, $filearea
send_file_not_found();
}

session_get_instance()->write_close();
$session = new \core\session\manager();
$session->write_close(); // unlock session during file serving.
send_stored_file($file, 60*60, 0, $forcedownload);
}
6 changes: 3 additions & 3 deletions version.php
Expand Up @@ -6,8 +6,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$plugin->version = 2013012500;
$plugin->release = '0.3.0 (beta)';
$plugin->version = 2013102800;
$plugin->release = '0.4.0 (beta)';
$plugin->component = 'block_css_theme_tool';
$plugin->requires = 2011120502; // Moodle 2.2 release
$plugin->requires = 2013102500; // Moodle 2.6 beta release
$plugin->maturity = MATURITY_BETA;

0 comments on commit 3b5b1f9

Please sign in to comment.