lib.php 10.5 KB
Newer Older
1
<?php
2

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 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/>.




//// SITE PRIVACY /////

/**
 * Site privacy: private
 */
26
define('HUB_SITENOTPUBLISHED', 'notdisplayed');
27
28
29
30

/**
 * Site privacy: public
 */
31
define('HUB_SITENAMEPUBLISHED', 'named');
32
33
34
35

/**
 * Site privacy: public and global
 */
36
define('HUB_SITELINKPUBLISHED', 'linked');
37
38

/**
39
40
41
42
43
44
45
46
 *
 * Site registration library
 *
 * @package   course
 * @copyright 2010 Moodle Pty Ltd (http://moodle.com)
 * @author    Jerome Mouneyrac
 * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47
 */
48
class registration_manager {
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

    /**
     * Automatically update the registration on all hubs
     */
    public function cron() {
        global $CFG;
        if (extension_loaded('xmlrpc')) {
            //check if the last registration cron update was less than a week ago
            $lastcron = get_config('registration', 'crontime');
            if ($lastcron === false or $lastcron < strtotime("-7 day")) { //set to a week, see MDL-23704
                $function = 'hub_update_site_info';
                require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");

                //update all hub where the site is registered on
                $hubs = $this->get_registered_on_hubs();
                foreach ($hubs as $hub) {
                    //update the registration
                    $siteinfo = $this->get_site_info($hub->huburl);
67
                    $params = array('siteinfo' => $siteinfo);
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
                    $serverurl = $hub->huburl . "/local/hub/webservice/webservices.php";
                    $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $hub->token);
                    try {
                        $result = $xmlrpcclient->call($function, $params);
                        mtrace(get_string('siteupdatedcron', 'hub', $hub->hubname));
                    } catch (Exception $e) {
                        $errorparam = new stdClass();
                        $errorparam->errormessage = $e->getMessage();
                        $errorparam->hubname = $hub->hubname;
                        mtrace(get_string('errorcron', 'hub', $errorparam));
                    }
                }
                set_config('crontime', time(), 'registration');
            }
        } else {
            mtrace(get_string('errorcronnoxmlrpc', 'hub'));
        }
    }

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
    /**
     * Return the site secret for a given hub
     * site identifier is assigned to Mooch
     * each hub has a unique and personal site secret.
     * @param string $huburl
     * @return string site secret
     */
    public function get_site_secret_for_hub($huburl) {
        global $DB;

        $existingregistration = $DB->get_record('registration_hubs',
                    array('huburl' => $huburl));

        if (!empty($existingregistration)) {
            return $existingregistration->secret;
        }

        if ($huburl == HUB_MOODLEORGHUBURL) {
            $siteidentifier =  get_site_identifier();
        } else {
            $siteidentifier = random_string(32) . $_SERVER['HTTP_HOST'];
        }

        return $siteidentifier;

    }

114
115
116
117
118
    /**
     * When the site register on a hub, he must call this function
     * @param object $hub where the site is registered on
     * @return integer id of the record
     */
119
120
121
122
123
124
    public function add_registeredhub($hub) {
        global $DB;
        $id = $DB->insert_record('registration_hubs', $hub);
        return $id;
    }

125
126
127
128
    /**
     * When a site unregister from a hub, he must call this function
     * @param string $huburl the huburl to delete
     */
129
    public function delete_registeredhub($huburl) {
130
131
132
133
        global $DB;
        $DB->delete_records('registration_hubs', array('huburl' => $huburl));
    }

134
135
136
137
138
139
140
    /**
     * Get a hub on which the site is registered for a given url or token
     * Mostly use to check if the site is registered on a specific hub
     * @param string $huburl
     * @param string $token
     * @return object the  hub
     */
141
142
143
144
145
146
147
148
149
150
    public function get_registeredhub($huburl = null, $token = null) {
        global $DB;

        $params = array();
        if (!empty($huburl)) {
            $params['huburl'] = $huburl;
        }
        if (!empty($token)) {
            $params['token'] = $token;
        }
151
        $params['confirmed'] = 1;
152
        $token = $DB->get_record('registration_hubs', $params);
153
154
155
        return $token;
    }

156
    /**
157
158
     * Get the hub which has not confirmed that the site is registered on,
     * but for which a request has been sent
159
160
161
     * @param string $huburl
     * @return object the  hub
     */
162
163
164
165
166
167
    public function get_unconfirmedhub($huburl) {
        global $DB;

        $params = array();
        $params['huburl'] = $huburl;
        $params['confirmed'] = 0;
168
        $token = $DB->get_record('registration_hubs', $params);
169
170
171
        return $token;
    }

172
173
174
175
    /**
     * Update a registered hub (mostly use to update the confirmation status)
     * @param object $communication the hub
     */
176
177
178
179
180
    public function update_registeredhub($communication) {
        global $DB;
        $DB->update_record('registration_hubs', $communication);
    }

181
182
183
184
    /**
     * Return all hubs where the site is registered on
     */
    public function get_registered_on_hubs() {
185
        global $DB;
186
        $hubs = $DB->get_records('registration_hubs', array('confirmed' => 1));
187
        return $hubs;
188
    }
189

190
191
192
193
194
195
196
197
198
199
    /**
     * Return site information for a specific hub
     * @param string $huburl
     * @return array site info
     */
    public function get_site_info($huburl) {
        global $CFG, $DB;

        $siteinfo = array();
        $cleanhuburl = clean_param($huburl, PARAM_ALPHANUMEXT);
200
201
202
203
204
205
206
207
208
209
210
211
212
213
        $siteinfo['name'] = get_config('hub', 'site_name_' . $cleanhuburl);
        $siteinfo['description'] = get_config('hub', 'site_description_' . $cleanhuburl);
        $siteinfo['contactname'] = get_config('hub', 'site_contactname_' . $cleanhuburl);
        $siteinfo['contactemail'] = get_config('hub', 'site_contactemail_' . $cleanhuburl);
        $siteinfo['contactphone'] = get_config('hub', 'site_contactphone_' . $cleanhuburl);
        $siteinfo['imageurl'] = get_config('hub', 'site_imageurl_' . $cleanhuburl);
        $siteinfo['privacy'] = get_config('hub', 'site_privacy_' . $cleanhuburl);
        $siteinfo['street'] = get_config('hub', 'site_address_' . $cleanhuburl);
        $siteinfo['regioncode'] = get_config('hub', 'site_region_' . $cleanhuburl);
        $siteinfo['countrycode'] = get_config('hub', 'site_country_' . $cleanhuburl);
        $siteinfo['geolocation'] = get_config('hub', 'site_geolocation_' . $cleanhuburl);
        $siteinfo['contactable'] = get_config('hub', 'site_contactable_' . $cleanhuburl);
        $siteinfo['emailalert'] = get_config('hub', 'site_emailalert_' . $cleanhuburl);
        if (get_config('hub', 'site_coursesnumber_' . $cleanhuburl) == -1) {
214
215
            $coursecount = -1;
        } else {
216
            $coursecount = $DB->count_records('course') - 1;
217
218
        }
        $siteinfo['courses'] = $coursecount;
219
        if (get_config('hub', 'site_usersnumber_' . $cleanhuburl) == -1) {
220
221
            $usercount = -1;
        } else {
222
            $usercount = $DB->count_records('user', array('deleted' => 0));
223
224
225
        }
        $siteinfo['users'] = $usercount;

226
        if (get_config('hub', 'site_roleassignmentsnumber_' . $cleanhuburl) == -1) {
227
228
229
230
231
            $roleassigncount = -1;
        } else {
            $roleassigncount = $DB->count_records('role_assignments');
        }
        $siteinfo['enrolments'] = $roleassigncount;
232
        if (get_config('hub', 'site_postsnumber_' . $cleanhuburl) == -1) {
233
234
235
236
237
            $postcount = -1;
        } else {
            $postcount = $DB->count_records('forum_posts');
        }
        $siteinfo['posts'] = $postcount;
238
        if (get_config('hub', 'site_questionsnumber_' . $cleanhuburl) == -1) {
239
240
241
242
243
            $questioncount = -1;
        } else {
            $questioncount = $DB->count_records('question');
        }
        $siteinfo['questions'] = $questioncount;
244
        if (get_config('hub', 'site_resourcesnumber_' . $cleanhuburl) == -1) {
245
246
247
248
249
250
            $resourcecount = -1;
        } else {
            $resourcecount = $DB->count_records('resource');
        }
        $siteinfo['resources'] = $resourcecount;
        //TODO
251
252
        require_once($CFG->dirroot . "/course/lib.php");
        if (get_config('hub', 'site_participantnumberaverage_' . $cleanhuburl) == -1) {
253
254
255
256
257
            $participantnumberaverage = -1;
        } else {
            $participantnumberaverage = average_number_of_participants();
        }
        $siteinfo['participantnumberaverage'] = $participantnumberaverage;
258
        if (get_config('hub', 'site_modulenumberaverage_' . $cleanhuburl) == -1) {
259
260
261
262
263
            $modulenumberaverage = -1;
        } else {
            $modulenumberaverage = average_number_of_courses_modules();
        }
        $siteinfo['modulenumberaverage'] = $modulenumberaverage;
264
        $siteinfo['language'] = get_config('hub', 'site_language_' . $cleanhuburl);
265
266
267
268
269
270
271
272
273
274
275
276
277
278
        $siteinfo['moodleversion'] = $CFG->version;
        $siteinfo['moodlerelease'] = $CFG->release;
        $siteinfo['url'] = $CFG->wwwroot;

        return $siteinfo;
    }

    /**
     * Retrieve the site privacy string matching the define value
     * @param string $privacy must match the define into moodlelib.php
     * @return string
     */
    public function get_site_privacy_string($privacy) {
        switch ($privacy) {
279
280
            case HUB_SITENOTPUBLISHED:
                $privacystring = get_string('siteprivacynotpublished', 'hub');
281
                break;
282
283
            case HUB_SITENAMEPUBLISHED:
                $privacystring = get_string('siteprivacypublished', 'hub');
284
                break;
285
286
            case HUB_SITELINKPUBLISHED:
                $privacystring = get_string('siteprivacylinked', 'hub');
287
288
289
290
291
292
293
                break;
        }
        if (empty($privacystring)) {
            throw new moodle_exception('unknownprivacy');
        }
        return $privacystring;
    }
294

295
}
296
?>