registration.php 8.15 KB
Newer Older
1
2
<?php
/**
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    mahara
 * @subpackage core
 * @author     Catalyst IT Ltd
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 * @file Register a mahara site
 */
/**
 * @defgroup Registration Registration
 * Send site information to mahara.org
 * 
 */
 
defined('INTERNAL') || die();

/**
 * @return string that is the registation form
 * @ingroup Registration 
 */
function register_site()  {
Nigel McNie's avatar
Nigel McNie committed
40
41
    $strfield = get_string('Field', 'admin');
    $strvalue = get_string('Value', 'admin');
42
    $info = <<<EOF
43
<tr><td>
44
45
<table>
    <tr>
Nigel McNie's avatar
Nigel McNie committed
46
47
        <th>$strfield</th>
        <th>$strvalue</th>
48
49
50
51
52
53
    </tr>
EOF;
    $data = registration_data();
    foreach($data as $key => $val) {
        $info .= '<tr><td>'. hsc($key) . '</td><td>' . hsc($val) . "</td></tr>\n";
    }
54
    $info .= '</table></td></tr>';
55

56
57
58
59
60
61
    $form = array(
        'name' => 'register',
        'autofocus' => false,
        'elements' => array(
            'whatsent' => array(
                'type' => 'fieldset',
Nigel McNie's avatar
Nigel McNie committed
62
                'legend' => get_string('datathatwillbesent', 'admin'),
63
64
65
66
67
                'collapsible' => true,
                'collapsed' => true,
                'elements' => array(
                    'info' => array(
                        'type' => 'markup',
Nigel McNie's avatar
Nigel McNie committed
68
                        'value'=> $info,
69
70
71
72
73
                    ),
                )
            ),
            'sendweeklyupdates' => array(
                'type' => 'checkbox',
Nigel McNie's avatar
Nigel McNie committed
74
75
                'title' => get_string('sendweeklyupdates', 'admin'),
                'defaultvalue' => true,
76
77
78
            ),
            'register' => array(
                'type' => 'submit',
Nigel McNie's avatar
Nigel McNie committed
79
                'value' => get_string('Register', 'admin'),
80
81
82
            ),
        )
     );
83
84
85
86
87
88
     
     return pieform($form);
}
/**
 * Runs when registration form is submitted
 */
89
90
91
function register_submit(Pieform $form, $values) {
    global $SESSION;

92
    $result = registration_send_data();
93

94
    if ($result->data != '1') {
Nigel McNie's avatar
Nigel McNie committed
95
96
        log_info($result);
        $SESSION->add_error_msg(get_string('registrationfailedtrylater', 'admin', $result->info['http_code']));
97
98
    }
    else {
99
        set_config('registration_lastsent', time());
100
        set_config('registration_sendweeklyupdates', $values['sendweeklyupdates']);
101
        $SESSION->add_ok_msg(get_string('registrationsuccessfulthanksforregistering', 'admin'));
102
103
    }
    redirect('/admin/');
104
105
}

106
107
108
109
110

/**
 * Worker - performs sending of registration data to mahara.org
 */
function registration_send_data() {
111
    $registrationurl = 'http://mahara.org/api/registration.php';
112
113
114
115
116
117
    $data = registration_data();
    $request = array(
        CURLOPT_URL        => $registrationurl,
        CURLOPT_POST       => 1,
        CURLOPT_POSTFIELDS => $data,
    );
118
    return mahara_http_request($request);
119
120
}

121

122
123
124
/**
 * Builds the data that will be sent by the "register your site" feature
 */
125
function registration_data() {
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
    foreach (array(
        'wwwroot',
        'installation_key',
        'sitename',
        'dbtype',
        'lang',
        'theme',
        'enablenetworking',
        'allowpublicviews',
        'allowpublicprofiles',
        'version',
        'release') as $key) {
        $data_to_send[$key] = get_config($key);
    }

    foreach (array(
        'usr_friend',
        'usr_institution',
        'group_member',
        'block_instance',
        'institution',
        'blocktype_wall_post',
        'institution') as $key) {
        $data_to_send['count_' . $key] = count_records($key);
    }
151

152
153
154
155
156
157
158
    foreach (array(
        'usr',
        'group',
        'host') as $key) {
        $data_to_send['count_' . $key] = count_records_select($key, 'deleted = 0');
        }

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    // Don't include the root user
    $data_to_send['count_usr']--;

    // Slightly more drilled down information
    if ($data = get_records_sql_array('SELECT artefacttype, COUNT(*) AS count
        FROM {artefact}
        GROUP BY artefacttype', array())) {
        foreach ($data as $artefacttypeinfo) {
            $data_to_send['artefact_type_' . $artefacttypeinfo->artefacttype] = $artefacttypeinfo->count;
        }
    }

    if ($data = get_records_sql_array('SELECT type, COUNT(*) AS count
        FROM {view}
        GROUP BY type', array())) {
        foreach ($data as $viewtypeinfo) {
            $data_to_send['view_type_' . $viewtypeinfo->type] = $viewtypeinfo->count;
        }
    }
178

179
    return $data_to_send;
180
}
181

182
function site_data_current() {
183
184
185
186
187
188
189
    return array(
        'users' => count_records_select('usr', 'id > 0 AND deleted = 0'),
        'groups' => count_records('group', 'deleted', 0),
        'views' => count_records_select('view', 'owner <> 0'),
    );
}

190
function site_statistics($full=false) {
191
192
    $data = array();

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    if ($full) {
        $data = site_data_current();
        $lastyear = db_format_timestamp(time() - 60*60*12*365);
        $values = array($lastyear, 'view-count', 'user-count', 'group-count');
        $weekly = get_records_sql_array('
            SELECT ctime, type, value, ' . db_format_tsfield('ctime', 'ts') . '
            FROM {site_data}
            WHERE ctime >= ? AND type IN (?,?,?)
            ORDER BY ctime, type', $values);
        if ($weekly) {
            $data['weekly'] = array('user-count' => array(), 'group-count' => array(), 'view-count' => array());
            $keys = array('user-count' => 0, 'group-count' => 0, 'view-count' => 0);
            foreach ($weekly as &$r) {
                $data['weekly'][$r->type][$keys[$r->type]++] = array($keys[$r->type], $r->value);
            }
208
        }
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

        if (is_postgres()) {
            $weekago = "CURRENT_DATE - INTERVAL '1 week'";
            $thisweeksql = "(lastaccess > $weekago)::int";
            $todaysql = '(lastaccess > CURRENT_DATE)::int';
            $eversql = "(NOT lastaccess IS NULL)::int";
        }
        else {
            $weekago = 'CURRENT_DATE - INTERVAL 1 WEEK';
            $thisweeksql = "lastaccess > $weekago";
            $todaysql = 'lastaccess > CURRENT_DATE';
            $eversql = "NOT lastaccess IS NULL";
        }
        $sql = "SELECT SUM($todaysql) AS today, SUM($thisweeksql) AS thisweek, $weekago AS weekago, SUM($eversql) AS ever FROM {usr}";
        $active = get_record_sql($sql);
        $data['usersloggedin'] = get_string('loggedinsince', 'admin', $active->today, $active->thisweek, format_date(strtotime($active->weekago), 'strftimedateshort'), $active->ever);

        $memberships = count_records_sql("
            SELECT COUNT(*)
            FROM {group_member} m JOIN {group} g ON g.id = m.group
            WHERE g.deleted = 0
        ");
        $data['groupmemberaverage'] = get_string('groupmemberaverage', 'admin', $memberships/$data['users']);
        $data['viewsperuser'] = get_string('viewsperuser', 'admin', get_field_sql("
            SELECT (0.0 + COUNT(id)) / COUNT(DISTINCT owner)
            FROM {view}
            WHERE NOT owner IS NULL AND owner > 0
        "));
237
    }
238

239
    $data['name'] = get_config('sitename');
240
241
242
243
244
    $data['release']     = get_config('release');
    $data['version']     = get_config('version');
    $data['installdate'] = format_date(strtotime(get_config('installation_time')), 'strftimedate');
    $data['dbsize']      = db_total_size();
    $data['diskusage']   = get_field('site_data', 'value', 'type', 'disk-usage');
Richard Mansfield's avatar
Richard Mansfield committed
245
246
    $data['cronrunning'] = !record_exists_select('cron', 'nextrun < CURRENT_DATE');

247
    return($data);
248
249
}

250
?>