From 5cd8e5c2998e071d2d92eb94b455df6a1ffb4e78 Mon Sep 17 00:00:00 2001
From: zishanj <zishanj@programmer.net>
Date: Wed, 4 Jan 2023 22:36:45 +0500
Subject: [PATCH] Created separate visibility option for Parents

---
 lang/en/admin.php          |  1 +
 user/classes/fields.php    |  1 +
 user/profile/definelib.php |  1 +
 user/profile/lib.php       | 17 +++++++++++++++--
 user/tests/fields_test.php | 10 ++++++----
 5 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/lang/en/admin.php b/lang/en/admin.php
index 17be3b3dfd..90351849ce 100644
--- a/lang/en/admin.php
+++ b/lang/en/admin.php
@@ -1053,6 +1053,7 @@ $string['profilevisibleall'] = 'Visible to everyone';
 $string['profilevisiblenone'] = 'Not visible';
 $string['profilevisibleprivate'] = 'Visible to user';
 $string['profilevisibleteachers'] = 'Visible to user, teachers and admins';
+$string['profilevisibleparents'] = 'Visible to user, parents and admins';
 $string['profiling'] = 'Profiling';
 $string['profilingallowall'] = 'Continuous profiling';
 $string['profilingallowall_help'] = 'If you enable this setting, then, at any moment, you can use the PROFILEALL parameter anywhere (PGC) to enable profiling for all the executed scripts along the Moodle session life. Analogously, you can use the PROFILEALLSTOP parameter to stop it.';
diff --git a/user/classes/fields.php b/user/classes/fields.php
index c8495d2387..f066c40936 100644
--- a/user/classes/fields.php
+++ b/user/classes/fields.php
@@ -381,6 +381,7 @@ class fields {
                     switch ($fieldinfo->visible ?? -1) {
                         case PROFILE_VISIBLE_NONE:
                         case PROFILE_VISIBLE_PRIVATE:
+                        case PROFILE_VISIBLE_PARENTS:
                             $allowed = !$context || has_capability('moodle/user:viewalldetails', $context);
                             break;
                         case PROFILE_VISIBLE_TEACHERS:
diff --git a/user/profile/definelib.php b/user/profile/definelib.php
index efd03c4d4d..aeff320fef 100644
--- a/user/profile/definelib.php
+++ b/user/profile/definelib.php
@@ -76,6 +76,7 @@ class profile_define_base {
         $choices[PROFILE_VISIBLE_NONE]    = get_string('profilevisiblenone', 'admin');
         $choices[PROFILE_VISIBLE_PRIVATE] = get_string('profilevisibleprivate', 'admin');
         $choices[PROFILE_VISIBLE_TEACHERS] = get_string('profilevisibleteachers', 'admin');
+        $choices[PROFILE_VISIBLE_PARENTS] = get_string('profilevisibleparents', 'admin');
         $choices[PROFILE_VISIBLE_ALL]     = get_string('profilevisibleall', 'admin');
 
         $form->addElement('select', 'visible', get_string('profilevisible', 'admin'), $choices);
diff --git a/user/profile/lib.php b/user/profile/lib.php
index ea04d63109..4931af5392 100644
--- a/user/profile/lib.php
+++ b/user/profile/lib.php
@@ -22,6 +22,13 @@
  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
+/**
+ * Visible to anyone who has the moodle/user:viewalldetails permission.
+ * Editable by the profile owner if they have the moodle/user:editownprofile capability
+ * or any user with the moodle/user:update capability.
+ */
+define('PROFILE_VISIBLE_PARENTS', '4');
+
 /**
  * Visible to anyone who has the moodle/site:viewuseridentity permission.
  * Editable by the profile owner if they have the moodle/user:editownprofile capability
@@ -448,13 +455,19 @@ class profile_field_base {
         $context = ($this->userid > 0) ? context_user::instance($this->userid) : context_system::instance();
 
         switch ($this->field->visible) {
-            case PROFILE_VISIBLE_TEACHERS:
+            case PROFILE_VISIBLE_PARENTS:
                 if ($this->is_signup_field() && (empty($this->userid) || isguestuser($this->userid))) {
                     return true;
                 } else if ($this->userid == $USER->id) {
                     return true;
-                } else if ($this->userid > 0) {
+                } else {
                     return has_capability('moodle/user:viewalldetails', $context);
+                }
+            case PROFILE_VISIBLE_TEACHERS:
+                if ($this->is_signup_field() && (empty($this->userid) || isguestuser($this->userid))) {
+                    return true;
+                } else if ($this->userid == $USER->id) {
+                    return true;
                 } else {
                     $coursecontext = context_course::instance($COURSE->id);
                     return has_capability('moodle/site:viewuseridentity', $coursecontext);
diff --git a/user/tests/fields_test.php b/user/tests/fields_test.php
index bd4be5649d..c0474d9046 100644
--- a/user/tests/fields_test.php
+++ b/user/tests/fields_test.php
@@ -67,10 +67,12 @@ class fields_test extends \advanced_testcase {
                 'visible' => PROFILE_VISIBLE_NONE]);
         $generator->create_custom_profile_field(['datatype' => 'text', 'shortname' => 'd', 'name' => 'D',
                 'visible' => PROFILE_VISIBLE_TEACHERS]);
+        $generator->create_custom_profile_field(['datatype' => 'text', 'shortname' => 'e', 'name' => 'E',
+                'visible' => PROFILE_VISIBLE_PARENTS]);
 
         // Set the extra user fields to include email, department, and all custom profile fields.
         set_config('showuseridentity', 'email,department,profile_field_a,profile_field_b,' .
-                'profile_field_c,profile_field_d');
+                'profile_field_c,profile_field_d,profile_field_e');
         set_config('hiddenuserfields', 'email');
 
         // Create a test course and a student in the course.
@@ -83,7 +85,7 @@ class fields_test extends \advanced_testcase {
 
         // When no context is provided, it does no access checks and should return all specified.
         $this->assertEquals(['email', 'department', 'profile_field_a', 'profile_field_b',
-                'profile_field_c', 'profile_field_d'],
+                'profile_field_c', 'profile_field_d', 'profile_field_e'],
                 fields::get_identity_fields(null));
 
         // If you turn off custom profile fields, you don't get those.
@@ -92,7 +94,7 @@ class fields_test extends \advanced_testcase {
         // Request in context as an administator.
         $this->setAdminUser();
         $this->assertEquals(['email', 'department', 'profile_field_a', 'profile_field_b',
-                'profile_field_c', 'profile_field_d'],
+                'profile_field_c', 'profile_field_d', 'profile_field_e'],
                 fields::get_identity_fields($coursecontext));
         $this->assertEquals(['email', 'department'],
                 fields::get_identity_fields($coursecontext, false));
@@ -147,7 +149,7 @@ class fields_test extends \advanced_testcase {
         // Also give them permission to view all profile fields.
         role_change_permission($roleid, $usercontext, 'moodle/user:viewalldetails', CAP_ALLOW);
         $this->assertEquals(['email', 'department', 'profile_field_a', 'profile_field_b',
-                'profile_field_c', 'profile_field_d'],
+                'profile_field_c', 'profile_field_d', 'profile_field_e'],
                 fields::get_identity_fields($usercontext));
         $this->assertEquals(['email', 'department'],
                 fields::get_identity_fields($usercontext, false));
-- 
2.38.1

