Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of the Local welcome plugin
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * @package    local_welcome
19
 * @copyright  2017 Bas Brands
20
 * @author     Bas Brands, basbrands.nl
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
namespace local_welcome;
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
global $CFG;
29
require_once($CFG->dirroot . '/user/profile/lib.php');
30
require_once($CFG->dirroot . '/user/lib.php');
31
 
32
class message {
33
 
34
    public $defaultfields;
35
    public $welcomefields;
36
    public $welcomevalues;
37
    public $customfields;
38
 
39
    public function __construct() {
40
        $this->defaultfields = $this->get_default_fields();
41
        $this->welcomefields = $this->get_welcome_fields();
42
        $this->welcomevalues = $this->get_welcome_values();
43
        $this->customfields = $this->get_custom_fields();
44
    }
45
 
46
 
47
    private function get_default_fields() {
48
        $defaultfields = array('username', 'fullname', 'firstname', 'lastname', 'email',
49
            'address', 'phone1', 'phone2', 'icq', 'skype', 'yahoo', 'aim', 'msn', 'department',
50
            'institution', 'interests', 'idnumber', 'lang', 'timezone', 'description',
51
            'city', 'url', 'country'
52
        );
53
 
54
        return $defaultfields;
55
    }
56
 
57
    private function get_welcome_fields() {
58
        $welcomefields = array('sitelink', 'sitename', 'resetpasswordlink');
59
 
60
        return $welcomefields;
61
    }
62
 
63
    private function get_custom_fields() {
64
        $customfields = profile_get_custom_fields(true);
65
        $returnfields = array();
66
        foreach ($customfields as $field) {
67
            $returnfields[] = $field->shortname;
68
        }
69
        return $returnfields;
70
    }
71
 
72
    public function get_user_default_values($user) {
73
        $values = array();
74
        foreach ($this->defaultfields as $field) {
75
            if (isset($user->$field)) {
76
                $values[$field] = $user->$field;
77
            } else {
78
                $values[$field] = '';
79
            }
80
            if ($field == 'fullname') {
81
                $values[$field] = fullname($user);
82
            }
83
            if (!empty($user->$field) && $field == 'country') {
84
                $values[$field]  = get_string($user->country, 'countries');
85
            }
86
        }
87
        return $values;
88
    }
89
 
90
    public function get_user_custom_values($user) {
91
        $userinfo = profile_user_record($user->id);
92
        $values = array();
93
        foreach ($this->customfields as $field) {
94
            $fieldname = $field;
95
            if (isset($userinfo->$fieldname)) {
96
                $values[$field] = $userinfo->$fieldname;
97
            } else {
98
                $values[$field] = '';
99
            }
100
        }
101
        return $values;
102
    }
103
 
104
    public function get_welcome_values() {
105
        global $SITE;
106
 
107
        $values = array();
108
        $sitelink = \html_writer::link(new \moodle_url('/'), $SITE->fullname);
109
        $sitename = $SITE->fullname;
110
        $resetpasswordlink = \html_writer::link(
111
            new \moodle_url('/login/forgot_password.php'), get_string('resetpass', 'local_welcome'));
112
        foreach ($this->welcomefields as $field) {
113
            $values[$field] = $$field;
114
        }
115
        return $values;
116
    }
117
 
118
    public function replace_values($user, $message) {
119
        $cususervars = $this->get_user_custom_values($user);
120
        $defuservars = $this->get_user_default_values($user);
121
 
122
        foreach ($this->defaultfields as $field) {
123
            $message = str_replace('[['.$field.']]', $defuservars[$field], $message);
124
        }
125
 
126
        foreach ($this->customfields as $field) {
127
            $message = str_replace('[['.$field.']]', $cususervars[$field], $message);
128
        }
129
 
130
        foreach ($this->welcomefields as $field) {
131
            $message = str_replace('[['.$field.']]', $this->welcomevalues[$field], $message);
132
        }
133
        return $message;
134
 
135
    }
136
}