Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja committed Jan 27, 2012
0 parents commit 1e6e7e4
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This is a custom user profile demo.

Replace "myprofilefield" with name of your plugin and copy folder under moodle/user/profile/field.
Modify functions as per your requirement and add this field to user profile http://docs.moodle.org/22/en/User_profile_fields
75 changes: 75 additions & 0 deletions myprofilefield/define.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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/>.

/**
* Contains definition of cutsom user profile field.
*
* @package profilefield_myprofilefield
* @category profilefield
* @copyright 2012 Rajesh Taneja
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

class profile_define_myprofilefield extends profile_define_base {

/**
* Prints out the form snippet for the part of creating or
* editing a profile field specific to the current data type
*
* @param moodleform $form reference to moodleform for adding elements.
*/
function define_form_specific(&$form) {
//Add elements, set defualt value and define type of data
$form->addElement('radio', 'defaultdata', get_string('mystring', 'profilefield_myprofilefield'));
$form->setDefault('defaultdata', 1); // defaults to 'yes'
$form->setType('defaultdata', PARAM_BOOL);
}

/**
* Validate the data from the add/edit profile field form
* that is specific to the current data type
*
* @param object $data from the add/edit profile field form
* @param object $files files uploaded
* @return array associative array of error messages
*/
function define_validate_specific($data, $files) {
// overwrite if necessary
}

/**
* Alter form based on submitted or existing data
*
* @param moodleform $mform reference to moodleform
*/
function define_after_data(&$mform) {
// overwrite if necessary
}

/**
* Preprocess data from the add/edit profile field form
* before it is saved.
*
* @param object $data from the add/edit profile field form
* @return object processed data object
*/
function define_save_preprocess($data) {
// overwrite if necessary
}

}


116 changes: 116 additions & 0 deletions myprofilefield/field.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?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_myprofilefield
* @category profilefield
* @copyright 2012 Rajesh Taneja
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

class profile_field_myprofilefield extends profile_field_base {

/**
* Constructor
*
* Pulls out the options for myprofilefield from the database and sets the
* the corresponding key for the data if it exists
*
* @param int $fieldid id of user profile field
* @param int $userid id of user
*/
function profile_field_myprofilefield($fieldid=0, $userid=0) {
global $DB;
//first call parent constructor
parent::__construct($fieldid, $userid);

if (!empty($this->field)) {
$datafield = $DB->get_field('user_info_data', 'data', array('userid' => $this->userid, 'fieldid' => $this->fieldid));
if ($datafield !== false) {
$this->data = $datafield;
} else {
$this->data = $this->field->defaultdata;
}
}
}

/**
* Adds the profile field to the moodle form class
*
* @param moodleform $mform instance of the moodleform class
*/
function edit_field_add(&$mform) {

}

/**
* Display the data for this field
*
* @return string data for custom profile field.
*/
function display_data() {

}

/**
* Sets the default data for the field in the form object
*
* @param moodleform $mform instance of the moodleform class
*/
function edit_field_set_default(&$mform) {
if (!empty($default)) {
$mform->setDefault($this->inputname, $this->field->defaultdata);
}
}

/**
* Validate the form field from profile page
*
* @param stdClass $usernew user input
* @return string contains error message otherwise NULL
**/
function edit_validate_field($usernew) {

}

/**
* Process the data before it gets saved in database
*
* @param stdClass $data from the add/edit profile field form
* @param stdClass $datarecord The object that will be used to save the record
* @return stdClass
*/
function edit_save_data_preprocess($data, &$datarecord) {
return $data;
}

/**
* HardFreeze the field if locked.
*
* @param moodleform $mform instance of the moodleform class
*/
function edit_field_set_locked(&$mform) {
if (!$mform->elementExists($this->inputname)) {
return;
}
if ($this->is_locked() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
$mform->hardFreeze($this->inputname);
$mform->setConstant($this->inputname, $this->data);
}
}
}


26 changes: 26 additions & 0 deletions myprofilefield/lang/en/profilefield_myprofilefield.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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/>.

/**
* Contains language strings
*
* @package profilefield_myprofilefield
* @category profilefield
* @copyright 2012 Rajesh Taneja
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
//name of the plugin should be defined.
$string['pluginname'] = 'myprofilefield';
28 changes: 28 additions & 0 deletions myprofilefield/version.php
Original file line number Diff line number Diff line change
@@ -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_myprofilefield
* @category profilefield
* @copyright 2012 Rajesh Taneja
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2011112900; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2011112900; // Requires this Moodle version
$plugin->component = 'profilefield_myprofilefield'; // Full name of the plugin (used for diagnostics)

0 comments on commit 1e6e7e4

Please sign in to comment.