-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpclib.php
203 lines (171 loc) · 6.68 KB
/
rpclib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?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/>.
/**
* @package format_page
* @category format
* @author valery fremaux (valery.fremaux@gmail.com)
* @copyright 2008 Valery Fremaux (Edunao.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* Library of functions for rpc remote calls at tracker. All complex
* variables transport are performed using JSON format.
*/
defined('MOODLE_INTERNAL') || die();
/**
* Constants
*
*/
if (!defined('RPC_SUCCESS')) {
define('RPC_TEST', 100);
define('RPC_SUCCESS', 200);
define('RPC_FAILURE', 500);
define('RPC_FAILURE_USER', 501);
define('RPC_FAILURE_CONFIG', 502);
define('RPC_FAILURE_DATA', 503);
define('RPC_FAILURE_CAPABILITY', 510);
}
/**
* checks an user has local identity and comes from a known host
* @param string $username the user's login
* @param string $remotehostroot the host he comes from
* @return a failure report if unchecked, null elsewhere.
*/
function format_page_rpc_check($username, $remotehostroot, &$localuser) {
global $DB;
// Get local identity for user.
if (!$remotehost = $DB->get_record('mnet_host', array('wwwroot' => $remotehostroot))) {
$response->status = RPC_FAILURE;
$response->error = "Calling host is not registered. Check MNET configuration";
return json_encode($response);
}
$select = '
username = ? AND
mnethostid = ? AND
deleted = 0
';
if (!$localuser = $DB->get_record_select('user', $select, array($username, $remotehost->id))) {
$response->status = RPC_FAILURE_USER;
$response->error = "Calling user has no local account. Register remote user first";
return json_encode($response);
}
return null;
}
/**
* creates a page in course
* @param int $courseid
* @param object $pagerec a page record
* @param boolean $nojson when true, avoids serializing through JSON syntax
* @return string a JSON encoded information structure.
*/
function format_page_rpc_create_page($username, $remotehostroot, $courseid, $pagerec = null, $nojson = true) {
global $DB;
if ($failedcheck = format_page_rpc_check($username, $remotehostroot, $localuser)) {
return $failedcheck;
}
if ($nojson) {
return true;
}
$format = $DB->get_field('course', 'format', array('id' => $courseid));
if ($format != 'page') {
$response->status = RPC_FAILURE_DATA;
$response->error = 'Not a page formatted course.';
}
$page = new StdClass();
$page->courseid = $courseid;
if ($pagerec) {
foreach($pagerec as $field => $value) {
$page->$field = $value;
}
}
if (empty($page->nameone)) {
$page->nameone = get_string('newpage', 'format_page');
}
if (empty($page->nametwo)) {
$page->nametwo = get_string('newpage', 'format_page');
}
$pageobj = new course_page($page);
$pageobj->insert_in_sections();
return json_encode(true);
}
/**
* returns an internal pagid, knowing a page idnumber.
* If idnumber is not unique, will return the list of all page ids
* holding this idnumber.
* @param int $pageidnumber
* @return string a JSON encoded information structure.
*/
function format_page_rpc_get_page_id_from_idnumber($username, $remotehostroot, $pageidnumber) {
global $DB;
if ($failedcheck = format_page_rpc_check($username, $remotehostroot, $localuser)) {
return $failedcheck;
}
$pages = $DB->get_records('format_page', array('idnumber' => $pageidnumber));
return json_encode(implode(',', array_keys($pages)));
}
/**
* creates a page in course from an identified page template by full copying its content.
* @param int $courseid
* @param object $pagerec a page record overrdides that will be applied to format_pge record after being copied
* @param int $templateid the pageID of the orginal template to copy.
* @return string a JSON encoded information structure.
*/
function format_page_rpc_create_page_from_template($username, $remotehostroot, $courseid, $templateid, $pagerec = null) {
if ($failedcheck = format_page_rpc_check($username, $remotehostroot, $localuser)) {
return $failedcheck;
}
return true;
}
/**
* Adds a page_item to a page building every internal links needed.
* @param string $username the user's login
* @param string $remotehostroot the host he comes from
* @param string $pageid the page id where to add the item.
* @param string $blockarea the block region, as 'left', 'main', 'right'
* @param string $pos the position in region. Special values : -1 first, 9999 end. If numeric but higher
* max numer of items, will take the max available pos (eq. end). If pos has an item, will push the stack down from this location.
* @param string $itemtype, 'block' or 'module'. If is a module, will create the page_module block instance
* @param array $configdata, an array of properties that will be pushed into block instance configuration if building a block, or
* into the course module instance record if a module.
*/
function format_page_rpc_add_page_item($username, $remotehostroot, $pageid, $blockregion, $pos, $itemtype, $itemclass, $configdata) {
if ($failedcheck = format_page_rpc_check($username, $remotehostroot, $localuser)) {
return $failedcheck;
}
return;
}
/**
* Deletes a page_item.
* @param string $username the user's login
* @param string $remotehostroot the host he comes from
* @param string $pageitemid the pageitem id
*/
function format_page_rpc_delete_page_item($username, $remotehostroot, $pageitemid) {
global $DB;
if ($failedcheck = format_page_rpc_check($username, $remotehostroot, $localuser)) {
return $failedcheck;
}
if (!$pageitem = $DB->get_record('format_page_item', array('id' => $pageitemid))) {
$response->status = RPC_FAILURE_USER;
$response->error = "Page item does not exist";
return json_encode($response);
}
$page = course_page::get($pageitem->pageid);
if ($page) {
$page->block_delete($pageitem);
}
$response->status = RPC_SUCCESS;
return json_encode($response);
}