Merged branch 'MLD-27551' of git://github.com/mouneyrac/moodle.git with changes

This commit is contained in:
Sam Hemelryk 2011-06-08 17:34:17 +08:00
commit 96e0194c05
11 changed files with 239 additions and 3 deletions

View file

@ -6487,6 +6487,162 @@ class admin_setting_managerepository extends admin_setting {
}
}
/**
* Special checkbox for enable mobile web service
* If enable then we store the service id of the mobile service into config table
* If disable then we unstore the service id from the config table
*/
class admin_setting_enablemobileservice extends admin_setting_configcheckbox {
private $xmlrpcuse; //boolean: true => capability 'webservice/xmlrpc:use' is set for authenticated user role
/**
* Return true if Authenticated user role has the capability 'webservice/xmlrpc:use', otherwise false
* @return boolean
*/
private function is_xmlrpc_cap_allowed() {
global $DB, $CFG;
//if the $this->xmlrpcuse variable is not set, it needs to be set
if (empty($this->xmlrpcuse) and $this->xmlrpcuse!==false) {
$params = array();
$params['permission'] = CAP_ALLOW;
$params['roleid'] = $CFG->defaultuserroleid;
$params['capability'] = 'webservice/xmlrpc:use';
$this->xmlrpcuse = $DB->record_exists('role_capabilities', $params);
}
return $this->xmlrpcuse;
}
/**
* Set the 'webservice/xmlrpc:use' to the Authenticated user role (allow or not)
* @param type $status true to allow, false to not set
*/
private function set_xmlrpc_cap($status) {
global $CFG;
if ($status and !$this->is_xmlrpc_cap_allowed()) {
//need to allow the cap
$permission = CAP_ALLOW;
$assign = true;
} else if (!$status and $this->is_xmlrpc_cap_allowed()){
//need to disallow the cap
$permission = CAP_INHERIT;
$assign = true;
}
if (!empty($assign)) {
$systemcontext = get_system_context();
assign_capability('webservice/xmlrpc:use', $permission, $CFG->defaultuserroleid, $systemcontext->id, true);
}
}
/**
* Builds XHTML to display the control.
* The main purpose of this overloading is to display a warning when https
* is not supported by the server
* @param string $data Unused
* @param string $query
* @return string XHTML
*/
public function output_html($data, $query='') {
global $CFG, $OUTPUT;
$html = parent::output_html($data, $query);
if ((string)$data === $this->yes) {
require_once($CFG->dirroot . "/lib/filelib.php");
$curl = new curl();
$httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot); //force https url
$curl->head($httpswwwroot . "/login/index.php");
$info = $curl->get_info();
if (empty($info['http_code']) or ($info['http_code'] >= 400)) {
$html .= $OUTPUT->notification(get_string('nohttpsformobilewarning', 'admin'));
}
}
return $html;
}
/**
* Retrieves the current setting using the objects name
*
* @return string
*/
public function get_setting() {
global $CFG;
$webservicesystem = $CFG->enablewebservices;
require_once($CFG->dirroot . '/webservice/lib.php');
$webservicemanager = new webservice();
$mobileservice = $webservicemanager->get_external_service_by_shortname(MOODLE_OFFICIAL_MOBILE_SERVICE);
if ($mobileservice->enabled and !empty($webservicesystem) and $this->is_xmlrpc_cap_allowed()) {
return $this->config_read($this->name); //same as returning 1
} else {
return 0;
}
}
/**
* Save the selected setting
*
* @param string $data The selected site
* @return string empty string or error message
*/
public function write_setting($data) {
global $DB, $CFG;
$servicename = MOODLE_OFFICIAL_MOBILE_SERVICE;
require_once($CFG->dirroot . '/webservice/lib.php');
$webservicemanager = new webservice();
if ((string)$data === $this->yes) {
//code run when enable mobile web service
//enable web service systeme if necessary
set_config('enablewebservices', true);
//enable mobile service
$mobileservice = $webservicemanager->get_external_service_by_shortname(MOODLE_OFFICIAL_MOBILE_SERVICE);
$mobileservice->enabled = 1;
$webservicemanager->update_external_service($mobileservice);
//enable xml-rpc server
$activeprotocols = empty($CFG->webserviceprotocols) ? array() : explode(',', $CFG->webserviceprotocols);
if (!in_array('xmlrpc', $activeprotocols)) {
$activeprotocols[] = 'xmlrpc';
set_config('webserviceprotocols', implode(',', $activeprotocols));
}
//allow xml-rpc:use capability for authenticated user
$this->set_xmlrpc_cap(true);
} else {
//disable web service system if no other services are enabled
$otherenabledservices = $DB->get_records_select('external_services',
'enabled = :enabled AND (shortname != :shortname OR shortname IS NULL)', array('enabled' => 1,
'shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE));
if (empty($otherenabledservices)) {
set_config('enablewebservices', false);
//also disable xml-rpc server
$activeprotocols = empty($CFG->webserviceprotocols) ? array() : explode(',', $CFG->webserviceprotocols);
$protocolkey = array_search('xmlrpc', $activeprotocols);
if ($protocolkey !== false) {
unset($activeprotocols[$protocolkey]);
set_config('webserviceprotocols', implode(',', $activeprotocols));
}
//disallow xml-rpc:use capability for authenticated user
$this->set_xmlrpc_cap(false);
}
//disable the mobile service
$mobileservice = $webservicemanager->get_external_service_by_shortname(MOODLE_OFFICIAL_MOBILE_SERVICE);
$mobileservice->enabled = 0;
$webservicemanager->update_external_service($mobileservice);
}
return (parent::write_setting($data));
}
}
/**
* Special class for management of external services

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20110525" COMMENT="XMLDB file for core Moodle tables"
<XMLDB PATH="lib/db" VERSION="20110526" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
@ -2562,7 +2562,8 @@
<FIELD NAME="restrictedusers" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="requiredcapability" NEXT="component"/>
<FIELD NAME="component" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" PREVIOUS="restrictedusers" NEXT="timecreated"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="component" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" PREVIOUS="timecreated"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" PREVIOUS="timecreated" NEXT="shortname"/>
<FIELD NAME="shortname" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="a unique shortname" PREVIOUS="timemodified"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>

View file

@ -229,3 +229,15 @@ $functions = array(
),
);
$services = array(
'Moodle mobile web service' => array(
'functions' => array (
'moodle_enrol_get_users_courses',
'moodle_enrol_get_enrolled_users',
'moodle_user_get_users_by_id'),
'enabled' => 0,
'restrictedusers' => 0,
'shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE
),
);

View file

@ -6531,6 +6531,20 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
upgrade_main_savepoint(true, 2011060800);
}
if ($oldversion < 2011060800.01) { //TODO: put the right latest version
// Define field shortname to be added to external_services
$table = new xmldb_table('external_services');
$field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'timemodified');
// Conditionally launch add field shortname
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached
upgrade_main_savepoint(true, 2011060800.01);
}
return true;
}

View file

@ -424,6 +424,10 @@ define('HUB_HUBDIRECTORYURL', "http://hubdirectory.moodle.org");
*/
define('HUB_MOODLEORGHUBURL', "http://hub.moodle.org");
/**
* Moodle mobile app service name
*/
define('MOODLE_OFFICIAL_MOBILE_SERVICE', 'moodle_mobile_app');
/// PARAMETER HANDLING ////////////////////////////////////////////////////

View file

@ -904,6 +904,7 @@ function external_update_descriptions($component) {
$service['enabled'] = empty($service['enabled']) ? 0 : $service['enabled'];
$service['requiredcapability'] = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
$service['restrictedusers'] = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
$service['shortname'] = !isset($service['shortname']) ? null : $service['shortname'];
$update = false;
if ($dbservice->enabled != $service['enabled']) {
@ -918,6 +919,23 @@ function external_update_descriptions($component) {
$dbservice->restrictedusers = $service['restrictedusers'];
$update = true;
}
//if shortname is not a PARAM_ALPHANUMEXT, fail (tested here for service update and creation)
if (isset($service['shortname']) and
(clean_param($service['shortname'], PARAM_ALPHANUMEXT) != $service['shortname'])) {
throw new moodle_exception('installserviceshortnameerror', 'webservice', '', $service['shortname']);
}
if ($dbservice->shortname != $service['shortname']) {
//check that shortname is unique
if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
$existingservice = $DB->get_record('external_services',
array('shortname' => $service['shortname']));
if (!empty($existingservice)) {
throw new moodle_exception('installexistingserviceshortnameerror', 'webservice', '', $service['shortname']);
}
}
$dbservice->shortname = $service['shortname'];
$update = true;
}
if ($update) {
$DB->update_record('external_services', $dbservice);
}
@ -940,11 +958,21 @@ function external_update_descriptions($component) {
unset($functions);
}
foreach ($services as $name => $service) {
//check that shortname is unique
if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
$existingservice = $DB->get_record('external_services',
array('shortname' => $service['shortname']));
if (!empty($existingservice)) {
throw new moodle_exception('installserviceshortnameerror', 'webservice');
}
}
$dbservice = new stdClass();
$dbservice->name = $name;
$dbservice->enabled = empty($service['enabled']) ? 0 : $service['enabled'];
$dbservice->requiredcapability = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
$dbservice->restrictedusers = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
$dbservice->shortname = !isset($service['shortname']) ? null : $service['shortname'];
$dbservice->component = $component;
$dbservice->timecreated = time();
$dbservice->id = $DB->insert_record('external_services', $dbservice);