Now the db/service.php array can contain these extra keys to provide information
on how a webservice may be called:
'ajax' => true (Default is false)
Replaces the xx_is_allowed_from_ajax callback.
'loginrequired' => false (Default is true)
Means that this webservice can be called through lib/ajax/service-nosession.php
which sets NO_MOODLE_COOKIES to true (faster). This is only safe for webservices returning
static public data (e.g. get_string).
component: component,
template: name
}
- }]);
+ }], true, false);
// When returns a new promise that is resolved when all the passed in promises are resolved.
// The arguments to the done become the values of each resolved promise.
args: { component: componentStr, search: searchStr },
done: reloadListTemplate,
fail: notification.exception }
- ]);
+ ], true, false);
};
var throttle = null;
return new external_function_parameters($params);
}
- /**
- * Expose to AJAX
- * @return boolean
- */
- public static function list_templates_is_allowed_from_ajax() {
- return true;
- }
-
/**
* Loads the list of templates.
* @param string $component Limit the search to a component.
);
}
- /**
- * Can this function be called directly from ajax?
- *
- * @return boolean
- * @since Moodle 2.9
- */
- public static function load_canonical_template_is_allowed_from_ajax() {
- return true;
- }
-
/**
* Return a mustache template.
* Note - this function differs from the function core_output_load_template
'description' => 'List/search templates by component.',
'type' => 'read',
'capabilities'=> '',
+ 'ajax' => true,
+ 'loginrequired' => false,
),
'tool_templatelibrary_load_canonical_template' => array(
'classname' => 'tool_templatelibrary\external',
'methodname' => 'load_canonical_template',
'description' => 'Load a canonical template by name (not the theme overidden one).',
- 'type' => 'read'
+ 'type' => 'read',
+ 'ajax' => true,
+ 'loginrequired' => false,
),
);
--- /dev/null
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle 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.
+//
+// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * This file is used to call any registered externallib function in Moodle.
+ *
+ * It will process more than one request and return more than one response if required.
+ * It is recommended to add webservice functions and re-use this script instead of
+ * writing any new custom ajax scripts.
+ *
+ * @since Moodle 2.9
+ * @package core
+ * @copyright 2015 Damyon Wiese
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+define('NO_MOODLE_COOKIES', true);
+require_once('service.php');
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->libdir . '/externallib.php');
-require_sesskey();
-
$rawjson = file_get_contents('php://input');
$requests = json_decode($rawjson, true);
$externalfunctioninfo = external_function_info($methodname);
if (!$externalfunctioninfo->allowed_from_ajax) {
+ error_log('This external function is not available to ajax. Failed to call "' . $methodname . '"');
throw new moodle_exception('servicenotavailable', 'webservice');
}
if (!isloggedin()) {
error_log('This external function is not available to public users. Failed to call "' . $methodname . '"');
throw new moodle_exception('servicenotavailable', 'webservice');
+ } else {
+ require_sesskey();
}
}
* can be attached to the promises returned by this function.
* @param {Boolean} async Optional, defaults to true.
* If false - this function will not return until the promises are resolved.
+ * @param {Boolean} loginrequired Optional, defaults to true.
+ * If false - this function will call the faster nologin ajax script - but
+ * will fail unless all functions have been marked as 'loginrequired' => false
+ * in services.php
* @return {Promise[]} Array of promises that will be resolved when the ajax call returns.
*/
- call: function(requests, async) {
+ call: function(requests, async, loginrequired) {
var ajaxRequestData = [],
i,
promises = [];
async: async
};
+ var script = config.wwwroot + '/lib/ajax/service.php?sesskey=' + config.sesskey;
+ if (!loginrequired) {
+ script = config.wwwroot + '/lib/ajax/service-nologin.php?sesskey=' + config.sesskey;
+ }
+
// Jquery deprecated done and fail with async=false so we need to do this 2 ways.
if (async) {
- $.ajax(config.wwwroot + '/lib/ajax/service.php?sesskey=' + config.sesskey, settings)
+ $.ajax(script, settings)
.done(requestSuccess)
.fail(requestFail);
} else {
settings.success = requestSuccess;
settings.error = requestFail;
- $.ajax(config.wwwroot + '/lib/ajax/service.php?sesskey=' + config.sesskey, settings);
+ $.ajax(script, settings);
}
return promises;
});
}
- var deferreds = ajax.call(ajaxrequests);
+ var deferreds = ajax.call(ajaxrequests, true, false);
$.when.apply(null, deferreds).done(
function() {
// Turn the list of arguments (unknown length) into a real array.
template: name,
themename: currentThemeName
}
- }], async);
+ }], async, false);
promises[0].done(
function (templateSource) {
);
}
- /**
- * Can this function be called directly from ajax?
- *
- * @return boolean
- * @since Moodle 2.9
- */
- public static function load_template_is_allowed_from_ajax() {
- return true;
- }
-
/**
* Return a mustache template, and all the strings it requires.
*
'classpath' => 'lib/external/externallib.php',
'description' => 'Return a translated string - similar to core get_string() call',
'type' => 'read',
- 'loginrequired' => false
+ 'loginrequired' => false,
+ 'ajax' => true,
),
'core_get_strings' => array(
'classpath' => 'lib/external/externallib.php',
'description' => 'Return some translated strings - like several core get_string() calls',
'type' => 'read',
- 'loginrequired' => false
+ 'loginrequired' => false,
+ 'ajax' => true,
),
'core_get_component_strings' => array(
'description' => 'Return all raw strings (with {$a->xxx}) for a specific component
- similar to core get_component_strings() call',
'type' => 'read',
- 'loginrequired' => false
+ 'loginrequired' => false,
+ 'ajax' => true,
),
'methodname' => 'load_template',
'description' => 'Load a template for a renderable',
'type' => 'read',
- 'loginrequired' => false
+ 'loginrequired' => false,
+ 'ajax' => true,
),
// Completion related functions.
'methodname' => 'update_tags',
'description' => 'Updates tags',
'type' => 'write',
+ 'ajax' => true
),
'core_tag_get_tags' => array(
'methodname' => 'get_tags',
'description' => 'Gets tags by their ids',
'type' => 'read',
+ 'ajax' => true
),
);
return $strparams;
}
- /**
- * Can this function be called directly from ajax?
- *
- * @return boolean
- * @since Moodle 2.9
- */
- public static function get_string_is_allowed_from_ajax() {
- return true;
- }
-
/**
* Returns description of get_string parameters
*
);
}
- /**
- * Can this function be called directly from ajax?
- *
- * @return boolean
- * @since Moodle 2.9
- */
- public static function get_strings_is_allowed_from_ajax() {
- return true;
- }
-
-
/**
* Return multiple call to core get_string()
*
);
}
- /**
- * Can this function be called directly from ajax?
- *
- * @return boolean
- * @since Moodle 2.9
- */
- public static function get_component_strings_is_allowed_from_ajax() {
- return true;
- }
-
/**
* Return all lang strings of a component - call to core get_component_strings().
*
if (isset($functions[$function->name]['type'])) {
$function->type = $functions[$function->name]['type'];
}
+ if (isset($functions[$function->name]['ajax'])) {
+ $function->allowed_from_ajax = $functions[$function->name]['ajax'];
+ }
if (isset($functions[$function->name]['loginrequired'])) {
$function->loginrequired = $functions[$function->name]['loginrequired'];
} else {
);
}
- /**
- * Expose to AJAX
- *
- * @return boolean
- */
- public static function update_tags_is_allowed_from_ajax() {
- return true;
- }
-
/**
* Update tags
*
);
}
- /**
- * Expose to AJAX
- *
- * @return boolean
- */
- public static function get_tags_is_allowed_from_ajax() {
- return true;
- }
-
/**
* Get tags by their ids
*
)
);
}
-}
\ No newline at end of file
+}