NOBUG: Just adding some "database" words here and there to help a bit when debugging...
[moodle.git] / lib / externallib.php
... / ...
CommitLineData
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Support for external API
20 *
21 * @package moodlecore
22 * @subpackage webservice
23 * @copyright 2008 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27/**
28 * Exception indicating user is not allowed to use external function in
29 * the current context.
30 */
31class restricted_context_exception extends moodle_exception {
32 /**
33 * Constructor
34 */
35 function __construct() {
36 parent::__construct('restrictedcontextexception', 'error');
37 }
38}
39
40/**
41 * Base class for external api methods.
42 */
43class external_api {
44 private static $contextrestriction;
45
46 /**
47 * Set context restriction for all folowing subsequent function calls.
48 * @param stdClass $contex
49 * @return void
50 */
51 public static function set_context_restriction($contex) {
52 self::$contextrestriction = $context;
53 }
54
55 /**
56 * Validates submitted function barameters, if anything is incorrect
57 * invalid_parameter_exception is thrown.
58 * @param ? $description description of parameters
59 * @param ? $params the actual parameters
60 * @return ? params with added defaults for optional items, invalid_parameters_exception thrown if any problem found
61 */
62 public static function validate_prameters($description, $params) {
63 //TODO: we need to define the structure of param descriptions
64
65 return $params;
66 }
67
68 /**
69 * Makes sure user may execute functions in this context.
70 * @param object $context
71 * @return void
72 */
73 protected static function validate_context($context) {
74 if (empty($context)) {
75 throw new invalid_parameter_exception('Context does not exist');
76 }
77 if (empty(self::$contextrestriction)) {
78 self::$contextrestriction = get_context_instance(CONTEXT_SYSTEM);
79 }
80 $rcontext = self::$contextrestriction;
81
82 if ($rcontext->contextlevel == $context->contextlevel) {
83 if ($rcontex->id != $context->id) {
84 throw new restricted_context_exception();
85 }
86 } else if ($rcontext->contextlevel > $context->contextlevel) {
87 throw new restricted_context_exception();
88 } else {
89 $parents = get_parent_contexts($context);
90 if (!in_array($rcontext->id, $parents)) {
91 throw new restricted_context_exception();
92 }
93 }
94
95 if ($context->contextlevel >= CONTEXT_COURSE) {
96 //TODO: temporary bloody hack, this needs to be replaced by
97 // proper enrolment and course visibility check
98 // similar to require_login() (which can not be used
99 // because it can be used only once and redirects)
100 // oh - did I tell we need to rewrite enrolments in 2.0
101 // to solve this bloody mess?
102 //
103 // missing: hidden courses and categories, groupmembersonly,
104 // conditional activities, etc.
105 require_capability('moodle/course:view', $context);
106 }
107 }
108}
109