Skip to content

Commit

Permalink
MDL-22755, create filesystem plugin needs moodle/site:config capabili…
Browse files Browse the repository at this point in the history
…ty, and a few coding style fixing
  • Loading branch information
Dongsheng Cai committed Jun 22, 2010
1 parent 54632b0 commit 49d20de
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 122 deletions.
52 changes: 26 additions & 26 deletions lib/flickrlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1096,43 +1096,43 @@ function urls_lookupUser($url)
* @return boolean
*/

function upload ($photo, $title = null, $description = null, $tags = null, $is_public = null, $is_friend = null, $is_family = null) {
function upload ($photo, $title = null, $description = null, $tags = null, $is_public = null, $is_friend = null, $is_family = null) {
global $SESSION;
$args = array("async" => 1, "api_key" => $this->api_key, "title" => $title, "description" => $description, "tags" => $tags, "is_public" => $is_public, "is_friend" => $is_friend, "is_family" => $is_family);
if (!empty($this->email)) {
$args = array_merge($args, array("email" => $this->email));
}
if (!empty($this->password)) {
$args = array_merge($args, array("password" => $this->password));
}
$args = array("async" => 1, "api_key" => $this->api_key, "title" => $title, "description" => $description, "tags" => $tags, "is_public" => $is_public, "is_friend" => $is_friend, "is_family" => $is_family);
if (!empty($this->email)) {
$args = array_merge($args, array("email" => $this->email));
}
if (!empty($this->password)) {
$args = array_merge($args, array("password" => $this->password));
}
// TODO:
// should we request a token if it is not valid?
if (!empty($this->token)) {
$args = array_merge($args, array("auth_token" => $this->token));
}

ksort($args);
$auth_sig = "";
foreach ($args as $key => $data) {
if ($data !== null) {
$auth_sig .= $key . $data;
if (!empty($this->token)) {
$args = array_merge($args, array("auth_token" => $this->token));
}

ksort($args);
$auth_sig = "";
foreach ($args as $key => $data) {
if ($data !== null) {
$auth_sig .= $key . $data;
} else {
unset($args[$key]);
}
}
if (!empty($this->secret)) {
$api_sig = md5($this->secret . $auth_sig);
}
}
if (!empty($this->secret)) {
$api_sig = md5($this->secret . $auth_sig);
$args['api_sig'] = $api_sig;
}
}

$photo = realpath($photo);
$photo = realpath($photo);
$args['photo'] = '@'.$photo;

if ($response = $this->curl->post($this->Upload, $args)) {
if ($response = $this->curl->post($this->Upload, $args)) {
return true;
} else {
} else {
return false;
}
}
}
}
?>
52 changes: 33 additions & 19 deletions repository/filesystem/repository.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,30 +150,44 @@ public function set_option($options = array()) {
return $ret;
}
public function instance_config_form($mform) {
global $CFG;
$path = $CFG->dataroot . '/repository/';
if (!is_dir($path)) {
mkdir($path);
}
if ($handle = opendir($path)) {
$fieldname = get_string('path', 'repository_filesystem');
$choices = array();
while (false !== ($file = readdir($handle))) {
if (is_dir($path.$file) && $file != '.' && $file!= '..') {
$choices[$file] = $file;
$fieldname = '';
}
global $CFG, $PAGE;
if (has_capability('moodle/site:config', $PAGE->context)) {
$path = $CFG->dataroot . '/repository/';
if (!is_dir($path)) {
mkdir($path);
}
if (empty($choices)) {
$mform->addElement('static', '', '', get_string('nosubdir', 'repository_filesystem', $path));
} else {
$mform->addElement('select', 'fs_path', $fieldname, $choices);
$mform->addElement('static', null, '', get_string('information','repository_filesystem', $path));
if ($handle = opendir($path)) {
$fieldname = get_string('path', 'repository_filesystem');
$choices = array();
while (false !== ($file = readdir($handle))) {
if (is_dir($path.$file) && $file != '.' && $file!= '..') {
$choices[$file] = $file;
$fieldname = '';
}
}
if (empty($choices)) {
$mform->addElement('static', '', '', get_string('nosubdir', 'repository_filesystem', $path));
} else {
$mform->addElement('select', 'fs_path', $fieldname, $choices);
$mform->addElement('static', null, '', get_string('information','repository_filesystem', $path));
}
closedir($handle);
}
closedir($handle);
} else {
$mform->addElement('static', null, '', get_string('nopermissions', 'error', get_string('configplugin', 'repository_filesystem')));
return false;
}
}
public function supported_returntypes() {
return FILE_INTERNAL;
}
public static function create($type, $userid, $context, $params, $readonly=0) {
global $PAGE;
if (has_capability('moodle/site:config', $PAGE->context)) {
return parent::create($type, $userid, $context, $params, $readonly);
} else {
require_capability('moodle/site:config', $PAGE->context);
return false;
}
}
}
80 changes: 40 additions & 40 deletions repository/flickr_public/repository.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
/**
* repository_flickr_public class
* This one is used to create public repository
* You can set up a public account in admin page, so everyone can
* access photos in this public account
* You can set up a public account in admin page, so everyone can access
* flickr photos from this plugin
*
* @since 2.0
* @package moodlecore
Expand All @@ -35,6 +35,42 @@ class repository_flickr_public extends repository {
private $flickr;
public $photos;

/**
* constructor method
*
* @global object $CFG
* @global object $SESSION
* @param int $repositoryid
* @param int $context
* @param array $options
* @param boolean $readonly
*/
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array(), $readonly=0) {
global $CFG, $SESSION;
parent::__construct($repositoryid, $context, $options,$readonly);
$this->api_key = $this->get_option('api_key');
$this->flickr = new phpFlickr($this->api_key);
$this->flickr_account = $this->get_option('email_address');

$account = optional_param('flickr_account', '', PARAM_RAW);
$fulltext = optional_param('flickr_fulltext', '', PARAM_RAW);
if (empty($fulltext)) {
$fulltext = optional_param('s', '', PARAM_RAW);
}
$tag = optional_param('flickr_tag', '', PARAM_RAW);
$license = optional_param('flickr_license', '', PARAM_RAW);

$this->sess_account = 'flickr_public_'.$this->id.'_account';
$this->sess_tag = 'flickr_public_'.$this->id.'_tag';
$this->sess_text = 'flickr_public_'.$this->id.'_text';

if (!empty($account) or !empty($fulltext) or !empty($tag) or !empty($license)) {
$SESSION->{$this->sess_tag} = $tag;
$SESSION->{$this->sess_text} = $fulltext;
$SESSION->{$this->sess_account} = $account;
}
}

/**
* save api_key in config table
* @param array $options
Expand Down Expand Up @@ -77,43 +113,7 @@ public function global_search() {
}

/**
* constructor method
*
* @global object $CFG
* @global object $SESSION
* @param int $repositoryid
* @param int $context
* @param array $options
* @param boolean $readonly
*/
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array(), $readonly=0) {
global $CFG, $SESSION;
parent::__construct($repositoryid, $context, $options,$readonly);
$this->api_key = $this->get_option('api_key');
$this->flickr = new phpFlickr($this->api_key);
$this->flickr_account = $this->get_option('email_address');

$account = optional_param('flickr_account', '', PARAM_RAW);
$fulltext = optional_param('flickr_fulltext', '', PARAM_RAW);
if (empty($fulltext)) {
$fulltext = optional_param('s', '', PARAM_RAW);
}
$tag = optional_param('flickr_tag', '', PARAM_RAW);
$license = optional_param('flickr_license', '', PARAM_RAW);

$this->sess_account = 'flickr_public_'.$this->id.'_account';
$this->sess_tag = 'flickr_public_'.$this->id.'_tag';
$this->sess_text = 'flickr_public_'.$this->id.'_text';

if (!empty($account) or !empty($fulltext) or !empty($tag) or !empty($license)) {
$SESSION->{$this->sess_tag} = $tag;
$SESSION->{$this->sess_text} = $fulltext;
$SESSION->{$this->sess_account} = $account;
}
}

/**
* check flickr account
* check if flickr account
* @return boolean
*/
public function check_login() {
Expand Down Expand Up @@ -469,7 +469,7 @@ public function type_config_form($mform) {

/**
* Names of the plugin settings
* @return array
* @return array
*/
public static function get_type_option_names() {
return array('api_key');
Expand Down
43 changes: 27 additions & 16 deletions repository/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ final public function get_meta() {
* @param integer $readonly whether to create it readonly or not (defaults to not)
* @return mixed
*/
final public static function create($type, $userid, $context, $params, $readonly=0) {
public static function create($type, $userid, $context, $params, $readonly=0) {
global $CFG, $DB;
$params = (array)$params;
require_once($CFG->dirroot . '/repository/'. $type . '/repository.class.php');
Expand Down Expand Up @@ -1613,7 +1613,6 @@ public function get_short_filename($str, $maxlength) {
return $str;
}
}

}

/**
Expand Down Expand Up @@ -1642,17 +1641,7 @@ class repository_exception extends moodle_exception {
final class repository_instance_form extends moodleform {
protected $instance;
protected $plugin;

public function definition() {
global $CFG;
// type of plugin, string
$this->plugin = $this->_customdata['plugin'];
$this->typeid = $this->_customdata['typeid'];
$this->contextid = $this->_customdata['contextid'];
$this->instance = (isset($this->_customdata['instance'])
&& is_subclass_of($this->_customdata['instance'], 'repository'))
? $this->_customdata['instance'] : null;

protected function add_defaults() {
$mform =& $this->_form;
$strrequired = get_string('required');

Expand All @@ -1669,17 +1658,35 @@ public function definition() {

$mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="30"');
$mform->addRule('name', $strrequired, 'required', null, 'client');
}

public function definition() {
global $CFG;
// type of plugin, string
$this->plugin = $this->_customdata['plugin'];
$this->typeid = $this->_customdata['typeid'];
$this->contextid = $this->_customdata['contextid'];
$this->instance = (isset($this->_customdata['instance'])
&& is_subclass_of($this->_customdata['instance'], 'repository'))
? $this->_customdata['instance'] : null;

$mform =& $this->_form;

$this->add_defaults();
//add fields
if (!$this->instance) {
$result = repository::static_function($this->plugin, 'instance_config_form', $mform);
}
else {
if ($result === false) {
$mform->removeElement('name');
}
} else {
$data = array();
$data['name'] = $this->instance->name;
if (!$this->instance->readonly) {
$result = $this->instance->instance_config_form($mform);
if ($result === false) {
$mform->removeElement('name');
}
// and set the data if we have some.
foreach ($this->instance->get_instance_option_names() as $config) {
if (!empty($this->instance->options[$config])) {
Expand All @@ -1692,7 +1699,11 @@ public function definition() {
$this->set_data($data);
}

$this->add_action_buttons(true, get_string('save','repository'));
if ($result === false) {
$mform->addElement('cancel');
} else {
$this->add_action_buttons(true, get_string('save','repository'));
}
}

public function validation($data) {
Expand Down
Loading

0 comments on commit 49d20de

Please sign in to comment.