Browse Source

Moodle user profile field for brazilian CPF

master
Sesostris Vieira 11 years ago
parent
commit
ac94d95381
  1. 29
      README.md
  2. 14
      define.class.php
  3. 66
      field.class.php
  4. 28
      lang/en/profilefield_cpf.php
  5. 28
      version.php

29
README.md

@ -2,3 +2,32 @@ moodle_profilefield_cpf
=======================
Moodle user profile field for brazilian CPF
-------------------------------------------
A Moodle plugin that validates a Brazilian CPF
Instalation instructions:
'''''''''''''''''''''''''
1. Clone the repository to a folder named cpf;
2. Copy the folder cpf to user/profile/field directory on your moodle installation;
3. Open custom field editor (Site administration->Users->Accounts->User profile fields);
4. Choose Brazilian CPF in "Create new profile field" dropdown;
5. Configure and save the field.
In portuguese:
=============
Campo de perfil do Moodle para CPF brasileiro
---------------------------------------------
Um plugin Moodle que valida um CPF brasileiro
Instruções de instalação:
'''''''''''''''''''''''''
1. Clone o repositório para uma pasta denominada cpf;
2. Copie a pasta cpf para o diretório user/profile/field na sua instalação Moodle;
3. Abra o editor de campos de perfil (Administração do site -> Usuários -> Contas -> Campos de perfil do usuário);
4. Escolha Brazilian CPF na caixa de seleção "Criar um novo campo de perfil";
5. Configure e salve o campo.

14
define.class.php

@ -0,0 +1,14 @@
<?php
class profile_define_cpf extends profile_define_base {
function define_form_specific($form) {
/// Default data
$form->addElement('text', 'defaultdata', get_string('profiledefaultdata', 'admin'), 'size="50"');
$form->setType('defaultdata', PARAM_TEXT);
}
}

66
field.class.php

@ -0,0 +1,66 @@
<?php
class profile_field_cpf extends profile_field_base {
/**
* Overwrite the base class to display the data for this field
*/
function display_data() {
/// Default formatting
$data = parent::display_data();
if ($data !== '') {
$data = substr($data,0,3).'.'.substr($data,3,3).'.'.substr($data,6,3).'-'.substr($data,9,2);
}
/*
/// Are we creating a link?
if (!empty($this->field->param4) and !empty($data)) {
/// Define the target
if (! empty($this->field->param5)) {
$target = 'target="'.$this->field->param5.'"';
} else {
$target = '';
}
/// Create the link
$data = '<a href="'.str_replace('$$', urlencode($data), $this->field->param4).'" '.$target.'>'.htmlspecialchars($data).'</a>';
}
*/
return $data;
}
function edit_field_add($mform) {
/// Create the form field
$mform->addElement('text', $this->inputname, format_string($this->field->name), 'maxlength="11" size="11"');
$mform->setType($this->inputname, PARAM_INT);
}
function edit_validate_field($usernew) {
// Based on script by Moacir, in http://codigofonte.uol.com.br/codigos/validacao-de-cpf-com-php
$errors = array();
if (isset($usernew->{$this->inputname})) {
$cpf = $usernew->{$this->inputname};
if (strlen($cpf) != 11) {
return array($this->inputname => get_string('cpf_size', 'profilefield_cpf'));
}
if (in_array($cpf, array('00000000000', '11111111111', '22222222222', '33333333333', '44444444444', '55555555555', '66666666666', '77777777777', '88888888888', '99999999999'))) {
return array($this->inputname => get_string('cpf_invalid', 'profilefield_cpf'));
}
for ($t = 9; $t < 11; $t++) {
for ($d = 0, $c = 0; $c < $t; $c++) {
$v = substr($cpf,$c,1);
$d += $v * (($t + 1) - $c);
}
$v = substr($cpf,$c,1);
$d = ((10 * $d) % 11) % 10;
if ($v != $d) {
return array($this->inputname => get_string('cpf_invalid', 'profilefield_cpf'));
}
}
}
return $errors;
}
}

28
lang/en/profilefield_cpf.php

@ -0,0 +1,28 @@
<?php
// 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/>.
/**
* Strings for component 'profilefield_text', language 'en', branch 'MOODLE_20_STABLE'
*
* @package profilefield_text
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['pluginname'] = 'Brazilian CPF';
$string['cpf_size'] = 'CPF must have 11 digits';
$string['cpf_invalid'] = 'Invalid CPF';

28
version.php

@ -0,0 +1,28 @@
<?php
// 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/>.
/**
* @package profilefield
* @subpackage text
* @copyright 2007 onwards Shane Elliot {@link http://pukunui.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2013050100; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2013050100; // Requires this Moodle version
$plugin->component = 'profilefield_cpf'; // Full name of the plugin (used for diagnostics)
Loading…
Cancel
Save