@@ -60,4 +60,59 @@ public function search_users_with_groups(string $search = '', bool $searchanywhe
60
60
$ params = array_merge ($ params , $ inparams );
61
61
return $ this ->execute_search_queries ($ search , $ fields , $ countfields , $ sql , $ params , $ page , $ perpage , 0 , false );
62
62
}
63
+
64
+ /**
65
+ * Searches through the enrolled users in this course based on search_users but with mod_dialogue permission.
66
+ *
67
+ * @param string $search The search term.
68
+ * @param bool $searchanywhere Can the search term be anywhere, or must it be at the start.
69
+ * @param int $page Starting at 0.
70
+ * @param int $perpage Number of users returned per page.
71
+ * @param bool $returnexactcount Return the exact total users using count_record or not.
72
+ * @param ?int $contextid Context ID we are in - we might use search on activity level and its group mode can be different from course group mode.
73
+ * @return array with two or three elements:
74
+ * int totalusers Number users matching the search. (This element only exist if $returnexactcount was set to true)
75
+ * array users List of user objects returned by the query.
76
+ * boolean moreusers True if there are still more users, otherwise is False.
77
+ */
78
+ public function search_users (string $ search = '' , bool $ searchanywhere = false , int $ page = 0 , int $ perpage = 25 ,
79
+ bool $ returnexactcount = false , ?int $ contextid = null ) {
80
+ global $ USER ;
81
+
82
+ [$ ufields , $ joins , $ params , $ wherecondition ] = $ this ->get_basic_search_conditions ($ search , $ searchanywhere );
83
+
84
+ if (isset ($ contextid )) {
85
+ // If contextid is set, we need to determine the group mode that should be used (module or course).
86
+ [$ context , $ course , $ cm ] = get_context_info_array ($ contextid );
87
+ // If cm instance is returned, then use the group mode from the module, otherwise get the course group mode.
88
+ $ groupmode = $ cm ? groups_get_activity_groupmode ($ cm , $ course ) : groups_get_course_groupmode ($ this ->course );
89
+ } else {
90
+ // Otherwise, default to the group mode of the course.
91
+ $ context = $ this ->context ;
92
+ $ groupmode = groups_get_course_groupmode ($ this ->course );
93
+ }
94
+
95
+ if ($ groupmode == SEPARATEGROUPS && !has_capability ('moodle/site:accessallgroups ' , $ context )) {
96
+ $ groups = groups_get_all_groups ($ this ->course ->id , $ USER ->id , 0 , 'g.id ' );
97
+ $ groupids = array_column ($ groups , 'id ' );
98
+ if (!$ groupids ) {
99
+ return ['totalusers ' => 0 , 'users ' => [], 'moreusers ' => false ];
100
+ }
101
+ } else {
102
+ $ groupids = [];
103
+ }
104
+
105
+ [$ enrolledsql , $ enrolledparams ] = get_enrolled_sql ($ context , 'mod/dialogue:receive ' , $ groupids );
106
+
107
+ $ fields = 'SELECT ' . $ ufields ;
108
+ $ countfields = 'SELECT COUNT(u.id) ' ;
109
+ $ sql = " FROM {user} u
110
+ $ joins
111
+ JOIN ( $ enrolledsql) je ON je.id = u.id
112
+ WHERE $ wherecondition " ;
113
+
114
+ $ params = array_merge ($ params , $ enrolledparams );
115
+
116
+ return $ this ->execute_search_queries ($ search , $ fields , $ countfields , $ sql , $ params , $ page , $ perpage , 0 , $ returnexactcount );
117
+ }
63
118
}
0 commit comments