Skip to content

Commit

Permalink
Extend behaviour to allow single user visibility. Wunderbyte-GmbH#162
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelpollak committed Jul 23, 2021
1 parent 70e64cd commit c352fba
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
16 changes: 16 additions & 0 deletions behavior/behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ private function user_is_admin($user) {
public function is_visible_to_user($user = null, $isentryauthor = false, $ismentor = false) {
global $USER;
$user = $user ? $user : $USER;

// If special visibletouser is set overrule other visibility options.
if (isset($this->visibleto['user']) AND $this->visibleto['user'] == $user->id) {
return true;
}

$permissions = $this->datalynx->get_user_datalynx_permissions($user->id, 'view');
return $this->user_is_admin($user) || (array_intersect($permissions, $this->visibleto)) ||
($isentryauthor && in_array(mod_datalynx\datalynx::PERMISSION_AUTHOR, $this->visibleto)) ||
Expand All @@ -135,6 +141,11 @@ public static function db_to_form($record) {
$formdata->name = $record->name;
$formdata->description = $record->description;
$formdata->visibleto = unserialize($record->visibleto);

if(isset($formdata->visibleto['user'])) {
$formdata->visibletouser = $formdata->visibleto['user'];
}

$formdata->editableby = unserialize($record->editableby);
$formdata->required = $record->required;

Expand All @@ -147,6 +158,11 @@ public static function form_to_db($formdata) {
$record->dataid = $formdata->d;
$record->name = $formdata->name;
$record->description = $formdata->description;

if($formdata->visibletouser) {
$formdata->visibleto['user'] = $formdata->visibletouser;
}

$record->visibleto = serialize(isset($formdata->visibleto) ? $formdata->visibleto : []);
$record->editableby = serialize(isset($formdata->editableby) ? $formdata->editableby : []);
$record->required = $formdata->required;
Expand Down
27 changes: 26 additions & 1 deletion behavior/behavior_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ protected function definition() {
array(mod_datalynx\datalynx::PERMISSION_MANAGER, mod_datalynx\datalynx::PERMISSION_TEACHER,
mod_datalynx\datalynx::PERMISSION_STUDENT));
}

// Interface for single user, this overrules other visibility options.
$allusers = $this->get_allusers();
$mform->addElement('autocomplete', 'visibletouser', get_string('otheruser', 'datalynx'), $allusers);
$mform->setType('visibletouser', PARAM_INT);

// EDITING OPTIONS.

$mform->addElement('header', 'editing', get_string('editing', 'datalynx'));
$mform->setExpanded('editing');

Expand Down Expand Up @@ -118,6 +122,27 @@ protected function definition() {
$this->add_action_buttons();
}

/**
* Get all users in moodle instance for autocomplete list.
*
* @return array with userid -> firstname lastname.
* @throws coding_exception
*/
public function get_allusers() {
global $DB;
$tempusers = $DB->get_records('user', array(), '', $fields='id, firstname, lastname');

$allusers[0] = get_string('noselection', 'datalynx');
foreach($tempusers as $userdata) {
// Remove empties to make list more usable.
if($userdata->lastname == '') {
continue;
}
$allusers[$userdata->id] = "$userdata->firstname $userdata->lastname";
}
return $allusers;
}

/**
* @return object
*/
Expand Down

0 comments on commit c352fba

Please sign in to comment.