Skip to content

Commit 419fab1

Browse files
committed
Adição de campo CPF
1 parent fce8e1b commit 419fab1

File tree

6 files changed

+274
-0
lines changed

6 files changed

+274
-0
lines changed

user/profile/field/cpf/README.txt

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
CPF Profile Field
2+
===
3+
This is a CPF field to user profiles into moodle.
4+
5+
The Cadastro de Pessoas Físicas (CPF) – Portuguese for Natural Persons Register – is a number attributed by the Brazilian revenue agency (Receita Federal – Federal Revenue) to both Brazilians and resident aliens who pay taxes or take part, directly or indirectly, in activities that provide revenue for any of the dozens of different types of taxes existing in Brazil.
6+
7+
More about CPF: http://en.wikipedia.org/wiki/Cadastro_de_Pessoas_F%C3%ADsicas
8+
9+
For Everyone
10+
---
11+
This plugin was made to add possibility to add CPF field in the user profile form.
12+
13+
Moodle documentation about profilefields -> http://docs.moodle.org/26/en/User_profile_fields
14+
15+
I recommend to insert a mask plugin to the CPF field. I did as follows:
16+
17+
- Added the Jquery masked Input plugin -> http://digitalbush.com/projects/masked-input-plugin/
18+
- Added the follow code relative to the masked field into the selected theme file:
19+
> jQuery("#profilefield_cpf").mask("999.999.999-99");
20+
21+
Para Brasileiros
22+
---
23+
Como CPF é um padrão brasileiro, a explicação será em português. :)
24+
Este plugin foi criado para possibilitar a inclusão do campo cpf nos formulários de criação de novos usuários e também na alteração dos dados do usuário.
25+
26+
Documentação de como utilizar os profilefields -> http://docs.moodle.org/26/en/User_profile_fields
27+
28+
Recomendo inserir um plugin de máscara para o campo CPF. Eu fiz da seguinte forma:
29+
- Adicionei o plugin Jquery masked Input -> http://digitalbush.com/projects/masked-input-plugin/
30+
- Adicionei o seguinte código referente à mascara do campo no tema utilizado:
31+
> jQuery("#profilefield_cpf").mask("999.999.999-99");
32+
33+
Installation
34+
---
35+
36+
As usual:
37+
38+
* Download the source code (zip or git clone)
39+
* Uncompress to user/profile/field/cpf
40+
* Go to **Notifications**
41+
42+
License
43+
---
44+
45+
It is released as GPL v3.
46+
47+
Authors:
48+
* Willian Mano - http://willianmano.net
49+
50+
Copyright 2014 onwards Willian Mano (http://willianmano.net)
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
class profile_define_cpf extends profile_define_base {
4+
public function define_form_specific($form) {
5+
// Default data.
6+
$form->addElement('text', 'defaultdata', 'Informe o CPF');
7+
$form->setType('defaultdata', PARAM_TEXT);
8+
$form->setDefault('defaultdata', '(somente dígitos)')
9+
}
10+
11+
function define_validate_specific($data) {
12+
$errors = array();
13+
// Make sure defaultdata is not false
14+
if ($data->defaultdata == false) {
15+
$errors['cpf'] = 'Obrigatório'; //get_string('noprovided', 'profilefield_cpf');
16+
}
17+
return $errors;
18+
}
19+
}
+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
class profile_field_cpf extends profile_field_base {
4+
public function edit_field_add($mform) {
5+
$mform->addElement(
6+
'text',
7+
$this->inputname,
8+
format_string($this->field->name),
9+
'maxlength="11" size="11" id="profilefield_cpf" pattern="[0-9]{11}" data-tip="Informe o CPF (apenas números)" title="Apenas números"'
10+
);
11+
$mform->setType($this->inputname, PARAM_TEXT);
12+
// if ($this->is_required() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
13+
// $mform->addRule($this->inputname, get_string('regexerrormessage', 'pluginname'), 'regex', '\d{11}');
14+
$mform->addRule($this->inputname, get_string('onlydigits', 'profilefield_cpf'), 'numeric', null, 'client');
15+
//get_string('required'), 'nonzero', null, 'client');
16+
// }
17+
}
18+
19+
public function edit_validate_field($usernew) {
20+
$return = array();
21+
if (isset($usernew->{$this->inputname})) {
22+
if (!$this->exists($usernew->{$this->inputname}, $usernew->id)) {
23+
$return[$this->inputname] = get_string('cpfexists', 'profilefield_cpf');
24+
} else if (!$this->validatecpf($usernew->{$this->inputname})) {
25+
$return[$this->inputname] = get_string('invalidcpf', 'profilefield_cpf');
26+
}
27+
}
28+
return $return;
29+
}
30+
31+
// Define formatação para o campo de CPF
32+
public function display_data() {
33+
if( preg_match( "/^(\d{3})(\d{3})(\d{3})(\d{2})$/", $this->data, $matches ) ) {
34+
$result = $matches[1] . '.' .$matches[2] . '.' . $matches[3] . '-' . $matches[4];
35+
} else {
36+
$result = $this->data;
37+
}
38+
return $result;
39+
}
40+
41+
42+
/*
43+
Testa se já existe algum usuário com este mesmo número de CPF.
44+
Se existir, retorna falso, do contrário retorna verdadeiro.
45+
*/
46+
private function exists($cpf = null, $userid = 0) {
47+
global $DB;
48+
49+
// Por definição, se for admin, aceita 00000000000
50+
if( is_siteadmin() && $cpf == '00000000000') {
51+
return true;
52+
}
53+
54+
// Verifica se um número foi informado.
55+
if (is_null($cpf)) {
56+
return false;
57+
}
58+
59+
$sql = "SELECT uid.data FROM {user_info_data} uid
60+
INNER JOIN {user_info_field} uif ON uid.fieldid = uif.id
61+
INNER JOIN {user} u on uid.userid = u.id
62+
WHERE uif.datatype = 'cpf' AND u.deleted = 0 AND uid.data = :cpf AND uid.userid <> :userid";
63+
$params['cpf'] = $cpf;
64+
$params['userid'] = $userid;
65+
$dbcpf = current($DB->get_records_sql($sql, $params));
66+
67+
if (!empty($dbcpf)) {
68+
return false;
69+
} else {
70+
return true;
71+
}
72+
}
73+
74+
/*
75+
Verifica se um determinado valor passado corresponde a um número de CPF válido
76+
Retorna verdadeiro apenas se DV bater com esperado
77+
*/
78+
private function validatecpf($cpf = null) {
79+
// Por definição, se for admin, aceita 00000000000
80+
if( is_siteadmin() && $cpf == '00000000000') {
81+
return true;
82+
}
83+
84+
// Verifica se um numero foi informado.
85+
if (is_null($cpf)) {
86+
return false;
87+
}
88+
if (!is_numeric($cpf)) {
89+
return false;
90+
}
91+
//$cpf = str_pad($cpf, 11, '0', STR_PAD_LEFT);
92+
93+
// Verifica se o numero de digitos informados eh igual a 11.
94+
if (strlen($cpf) != 11) {
95+
return false;
96+
} else if ($cpf == '00000000000' || $cpf == '11111111111' || $cpf == '22222222222' ||
97+
$cpf == '33333333333' || $cpf == '44444444444' || $cpf == '55555555555' ||
98+
$cpf == '66666666666' || $cpf == '77777777777' || $cpf == '88888888888' ||
99+
$cpf == '99999999999') {
100+
return false;
101+
} else {
102+
// Calcula os digitos verificadores para verificar se o CPF eh valido.
103+
for ($t = 9; $t < 11; $t++) {
104+
105+
for ($d = 0, $c = 0; $c < $t; $c++) {
106+
$d += $cpf{$c} * (($t + 1) - $c);
107+
}
108+
$d = ((10 * $d) % 11) % 10;
109+
if ($cpf{$c} != $d) {
110+
return false;
111+
}
112+
}
113+
114+
return true;
115+
}
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
* Strings for component 'cpf_text', language 'en'
20+
*
21+
* @package cpf_text
22+
* @copyright 2014 onwards Willian Mano {@link http://willianmano.net}
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
26+
$string['pluginname'] = 'CPF input';
27+
$string['invalidcpf'] = 'Invalid CPF';
28+
$string['cpfexists'] = 'CPF already exists';
29+
$string['onlydigits'] = 'Only digits are accepted';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
* Strings for component 'cpf_text', language 'pt_br'
20+
*
21+
* @package cpf_text
22+
* @copyright 2014 onwards Willian Mano {@link http://willianmano.net}
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
26+
$string['pluginname'] = 'Campo de CPF';
27+
$string['invalidcpf'] = 'CPF inválido';
28+
$string['cpfexists'] = 'CPF já existe';
29+
$string['onlydigits'] = 'Informe apenas dígitos';
30+
$string['required'] = 'É obrigatório informar o CPF';

user/profile/field/cpf/version.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
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 cpf
19+
* @copyright 2014 onwards Willian Mano {@link http://willianmano.net}
20+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21+
*/
22+
23+
defined('MOODLE_INTERNAL') || die();
24+
25+
$plugin->version = 2017051702; // The current plugin version (Date: YYYYMMDDXX).
26+
$plugin->requires = 2012062500; // Requires this Moodle version.
27+
$plugin->maturity = MATURITY_STABLE;
28+
$plugin->release = 'Version for Moodle 2.2 onwards';
29+
$plugin->component = 'profilefield_cpf'; // Full name of the plugin (used for diagnostics).

0 commit comments

Comments
 (0)