-
Notifications
You must be signed in to change notification settings - Fork 2
Add user profile field functionality. #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
troywilliams
wants to merge
3
commits into
learningworks:master
Choose a base branch
from
troywilliams:learner_profile_fields
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
<?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/>. | ||
|
||
namespace report_lp\local\fields; | ||
|
||
use MoodleQuickForm; | ||
use report_lp\local\contracts\extra_configuration; | ||
use report_lp\local\learner_field; | ||
use report_lp\local\user_list; | ||
use report_lp\output\cell; | ||
use stdClass; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
class learner_profile_field extends learner_field implements extra_configuration { | ||
|
||
/** @var string $fieldinputname Used as reference to field stored on User object. Example $user->profile_field_nsn. */ | ||
public $fieldinputname; | ||
|
||
/** | ||
* Build field class based off extra configuration data. | ||
* | ||
* @return \profile_field_base $field | ||
* @throws \coding_exception | ||
* @throws \dml_exception | ||
*/ | ||
public function get_field() : \profile_field_base { | ||
global $DB, $CFG; | ||
try { | ||
$fieldid = $this->get_configuration()->get('extraconfigurationdata')->fieldid ?? null; | ||
if (is_null($fieldid)) { | ||
throw new \coding_exception('nofieldiddefinedinextraconfig'); | ||
} | ||
$fieldrecord = $DB->get_record('user_info_field', ['id' => $fieldid], '*', MUST_EXIST); | ||
require_once($CFG->dirroot . '/user/profile/field/' . $fieldrecord->datatype . '/field.class.php'); | ||
$classname= 'profile_field_' . $fieldrecord->datatype; | ||
/** @var profile_field_base $fieldobject */ | ||
$field = new $classname($fieldrecord->id); | ||
$this->fieldinputname = $field->inputname; | ||
return $field; | ||
} catch (\coding_exception $exception) { | ||
throw $exception; | ||
} | ||
} | ||
|
||
/** | ||
* Build field class based off extra configuration and load user data into class. | ||
* | ||
* @param stdClass $user | ||
* @return mixed|\profile_field_base $field | ||
* @throws \coding_exception | ||
* @throws \dml_exception | ||
*/ | ||
public function get_field_with_user_data(stdClass $user) : \profile_field_base { | ||
global $DB, $CFG; | ||
try { | ||
$fieldid = $this->get_configuration()->get('extraconfigurationdata')->fieldid ?? null; | ||
if (is_null($fieldid)) { | ||
throw new \coding_exception('nofieldiddefinedinextraconfig'); | ||
} | ||
$sql = "SELECT uif.*, uic.name AS categoryname, uind.id AS hasuserdata, uind.data, uind.dataformat | ||
FROM {user_info_field} uif | ||
LEFT JOIN {user_info_category} uic ON uic.id = uif.categoryid | ||
LEFT JOIN {user_info_data} uind ON uind.fieldid = uif.id | ||
WHERE uif.id = :fieldid AND uind.userid = :userid"; | ||
$fieldrecord = $DB->get_record_sql($sql, ['fieldid' => $fieldid, 'userid' => $user->id]); | ||
if (!$fieldrecord) { | ||
return $this->get_field(); | ||
} | ||
require_once($CFG->dirroot . '/user/profile/field/' . $fieldrecord->datatype . '/field.class.php'); | ||
$classname = 'profile_field_' . $fieldrecord->datatype; | ||
/** @var profile_field_base $fieldobject */ | ||
$field = new $classname($fieldrecord->id, $user->id, $fieldrecord); | ||
$this->fieldinputname = $field->inputname; | ||
$field->set_category_name($fieldrecord->categoryname); | ||
unset($fieldrecord->categoryname); | ||
return $field; | ||
} catch (\coding_exception $exception) { | ||
throw $exception; | ||
} | ||
} | ||
|
||
public function build_data_cell($user) { | ||
$cell = new cell(); | ||
$cell->class = "cell"; | ||
if (isset($user->{$this->fieldinputname})) { | ||
$cell->plaintextcontent = $user->{$this->fieldinputname}->display_data(); | ||
} else { | ||
$cell->plaintextcontent = ''; | ||
} | ||
return $cell; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function get_data_for_user(stdClass $user): stdClass | ||
{ | ||
$field = $this->get_field_with_user_data($user); | ||
if ($field) { | ||
$user->{$field->inputname} = $field; | ||
} | ||
return $user; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function get_data_for_users(user_list $userlist): array | ||
{ | ||
$data = []; | ||
foreach ($userlist as $user) { | ||
$data[$user->id] = $this->get_data_for_user($user); | ||
} | ||
return $data; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function moodlequickform_extend(MoodleQuickForm &$mform) | ||
{ | ||
global $DB; | ||
$learnerprofilefields = []; | ||
$categories = $DB->get_records('user_info_category', null, 'sortorder ASC'); | ||
foreach ($categories as $category) { | ||
$fields = $DB->get_records('user_info_field',['categoryid' => $category->id], 'sortorder ASC'); | ||
foreach ($fields as $field) { | ||
$learnerprofilefields[$field->id] = format_text($category->name . '/' . $field->name); | ||
} | ||
} | ||
if (empty($learnerprofilefields)) { | ||
$mform->addElement( | ||
'warning', | ||
'nolearnerprofilefieldswarning', | ||
null, | ||
get_string('nolearnerprofilefields', 'report_lp') | ||
); | ||
$mform->addElement('hidden', 'nolearnerprofilefields'); | ||
$mform->setType('nolearnerprofilefields', PARAM_INT); | ||
$mform->setDefault('nolearnerprofilefields', 1); | ||
$mform->disabledIf('submitbutton', 'nolearnerprofilefields', 'eq', 1); | ||
$mform->removeElement('specific'); | ||
} else { | ||
$options = [0 => get_string('choose')] + $learnerprofilefields; | ||
$mform->addElement('select', 'field', | ||
get_string('fieldname', 'report_lp'), $options); | ||
} | ||
|
||
} | ||
|
||
public function moodlequickform_validation($data, $files): array | ||
{ | ||
$errors = []; | ||
if ($data['field'] == 0) { | ||
$errors['field'] = get_string('pleasechoose', 'report_lp'); | ||
} | ||
return $errors; | ||
} | ||
|
||
|
||
public function moodlequickform_get_extra_configuration_data($data): stdClass | ||
{ | ||
if (empty($data['field'])) { | ||
throw new coding_exception('Something went horribly wrong'); | ||
} | ||
|
||
$object = new stdClass(); | ||
$object->fieldid = $data['field']; | ||
return $object; | ||
} | ||
|
||
|
||
public function moodlequickform_get_extra_configuration_defaults(): array | ||
{ | ||
$configuration = $this->get_configuration(); | ||
$extraconfigurationdata = $configuration->get('extraconfigurationdata'); | ||
$defaults = []; | ||
if (empty($extraconfigurationdata)) { | ||
$defaults['field'] = 0; | ||
} else { | ||
$defaults['field'] = $extraconfigurationdata->fieldid; | ||
} | ||
return $defaults; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function get_name(): string | ||
{ | ||
return get_string('profilefield:learnerfield:name', 'report_lp'); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function get_default_label(): string | ||
{ | ||
global $DB; | ||
if (!empty($this->get_id())) { | ||
$extraconfigurationdata = $this->get_extraconfigurationdata(); | ||
if ($extraconfigurationdata instanceof stdClass) { | ||
$field = $DB->get_record('user_info_field', ['id' => $extraconfigurationdata->fieldid]); | ||
return format_text($field->name, FORMAT_PLAIN); | ||
} | ||
} | ||
return get_string('profilefield:learnerfield:label', 'report_lp'); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function get_description(): string | ||
{ | ||
return get_string('profilefield:learnerfield:description', 'report_lp'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is get_data_for_users() used? Does it need to be added before release?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so, but I wil sort.