|
| 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 | +} |
0 commit comments