17
17
/**
18
18
* autogroup local plugin
19
19
*
20
- * A course object relates to a Moodle course and acts as a container
21
- * for multiple groups. Initialising a course object will automatically
22
- * load each autogroup group for that course into memory.
20
+ * A user object relates to a real Moodle user; it acts as a container
21
+ * for multiple courses which in turn contain multiple groups.
22
+ * Initialising a course object will automatically load each autogroup
23
+ * group which could be relevant for a user into memory.
24
+ *
25
+ * A user is also a group member; a membership register is also maintained
26
+ * by this class.
23
27
*
24
28
* @package local
25
29
* @subpackage autogroup
26
30
* @author Mark Ward ([email protected] )
27
- * @date April 2015
31
+ * @date December 2014
28
32
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
33
*/
30
34
31
- /**
32
- * This file allows users with the correct capability to manage
33
- * grouping logic for autogroups within a course.
34
- */
35
-
36
- namespace local_autogroup ;
37
-
38
- require_once (dirname (__FILE__ ) . '/pageinit.php ' );
39
-
40
- use \local_autogroup \domain ;
41
- use \local_autogroup \form ;
42
- use \local_autogroup \usecase ;
43
- use \local_autogroup_renderer ;
44
- use \moodle_url ;
45
- use \context_course ;
46
- use \stdClass ;
47
-
48
- $ courseid = required_param ('courseid ' , PARAM_INT );
49
- $ context = context_course::instance ($ courseid );
35
+ namespace local_autogroup \domain ;
50
36
51
- require_capability ('local/autogroup:managecourse ' , $ context );
37
+ use local_autogroup \domain ;
38
+ use local_autogroup \exception ;
52
39
53
- global $ PAGE , $ DB , $ SITE ;
54
-
55
- if ($ courseid == $ SITE ->id || !plugin_is_enabled ()){
56
- //do not allow editing for front page.
57
- die ();
58
- }
59
-
60
- $ course = $ DB ->get_record ('course ' , array ('id ' => $ courseid ));
61
- $ groupsets = $ DB ->get_records ('local_autogroup_set ' , array ('courseid ' =>$ courseid ));
62
-
63
- foreach ($ groupsets as $ k => $ groupset ) {
64
- $ groupsets [$ k ] = new domain \autogroup_set ($ DB , $ groupset );
40
+ /**
41
+ * Class user
42
+ *
43
+ * Wraps a standard moodle user with additional helper functions, linking
44
+ * to the users courses and on to their autogroups.
45
+ *
46
+ * TODO: Some of the functionality here belongs in a repository class
47
+ *
48
+ * @package local_autogroup\domain
49
+ */
50
+ class user extends domain
51
+ {
52
+ /**
53
+ * @param object|int $user
54
+ * @param \moodle_database $db
55
+ * @param int $onlyload a courseid to restrict loading to
56
+ * @throws exception\invalid_user_argument
57
+ */
58
+ public function __construct ($ user , \moodle_database $ db , $ onlyload = 0 )
59
+ {
60
+ //get the data for this user
61
+ $ this ->parse_user_data ($ user , $ db );
62
+
63
+ //register which autogroup groups this user is a member of currently
64
+ $ this ->get_group_membership ($ db );
65
+
66
+ //if applicable, load courses this user is on and their autogroup groups
67
+ $ this ->get_courses ($ db , $ onlyload );
68
+
69
+ return true ;
70
+ }
71
+
72
+ /**
73
+ * @param \moodle_database $db
74
+ * @return bool
75
+ */
76
+ public function verify_user_group_membership (\moodle_database $ db )
77
+ {
78
+ $ result = true ;
79
+ foreach ($ this ->courses as $ course ){
80
+ $ result &= $ course ->verify_user_group_membership ($ this ->object , $ db );
81
+ }
82
+
83
+ $ this ->get_group_membership ($ db );
84
+
85
+ return $ result ;
86
+ }
87
+
88
+ /**
89
+ * Get courses for this user where an autogroup set has been added
90
+ *
91
+ * @param \moodle_database $db
92
+ */
93
+ private function get_courses (\moodle_database $ db , $ onlyload = 0 )
94
+ {
95
+ if ($ onlyload < 1 ) {
96
+ $ sql = "SELECT e.courseid " . PHP_EOL
97
+ . "FROM {enrol} e " . PHP_EOL
98
+ . "LEFT JOIN {user_enrolments} ue " . PHP_EOL
99
+ . "ON ue.enrolid = e.id " . PHP_EOL
100
+ . "LEFT JOIN {local_autogroup_set} gs " . PHP_EOL
101
+ . "ON gs.courseid = e.courseid " . PHP_EOL
102
+ . "WHERE ue.userid = :userid " . PHP_EOL
103
+ . "AND gs.id IS NOT NULL " ;
104
+ $ param = array ('userid ' => $ this ->id );
105
+
106
+ $ this ->courses = $ db ->get_fieldset_sql ($ sql , $ param );
107
+ }
108
+
109
+ else {
110
+ $ this ->courses [] = $ onlyload ;
111
+ }
112
+
113
+ foreach ($ this ->courses as $ k => $ courseid ){
114
+ try {
115
+ $ courseid = (int ) $ courseid ;
116
+ $ this ->courses [$ k ] = new course ($ courseid , $ db );
117
+ } catch (exception \invalid_course_argument $ e ){
118
+ unset($ this ->courses [$ k ]);
119
+ }
120
+ }
121
+ }
122
+
123
+ /**
124
+ * @param \moodle_database $db
125
+ */
126
+ private function get_group_membership (\moodle_database $ db )
127
+ {
128
+ $ sql = "SELECT g.id, g.courseid " .PHP_EOL
129
+ ."FROM {groups} g " .PHP_EOL
130
+ ."LEFT JOIN {groups_members} gm " .PHP_EOL
131
+ ."ON gm.groupid = g.id " .PHP_EOL
132
+ ."WHERE gm.userid = :userid " .PHP_EOL
133
+ ."AND " .$ db ->sql_like ('g.idnumber ' , ':autogrouptag ' );
134
+ $ param = array (
135
+ 'userid ' => $ this ->id ,
136
+ 'autogrouptag ' => 'autogroup|% '
137
+ );
138
+
139
+ $ this ->membership = $ db ->get_records_sql_menu ($ sql ,$ param );
140
+ }
141
+
142
+ /**
143
+ * @param object|int $user
144
+ * @return bool
145
+ * @throws exception\invalid_user_argument
146
+ */
147
+ private function parse_user_data ($ user , \moodle_database $ db )
148
+ {
149
+ //TODO: restructure to allow usage of custom profile fields
150
+
151
+ if (is_int ($ user ) && $ user > 0 ){
152
+ $ this ->id = $ user ;
153
+ $ this ->object = $ db ->get_record ('user ' ,array ('id ' =>$ user ));
154
+ return true ;
155
+ }
156
+
157
+ if (is_object ($ user ) && isset ($ user ->id ) && $ user ->id > 0 ){
158
+ $ this ->id = $ user ->id ;
159
+ $ this ->object = $ user ;
160
+ return true ;
161
+ }
162
+
163
+ throw new exception \invalid_user_argument ($ user );
164
+ }
165
+
166
+ /**
167
+ * @var array
168
+ */
169
+ private $ membership = array ();
170
+
171
+ /**
172
+ * @var array
173
+ */
174
+ private $ courses = array ();
175
+
176
+ /**
177
+ * @var /stdclass
178
+ */
179
+ private $ object ;
65
180
}
66
-
67
- $ heading = \get_string ('coursesettingstitle ' , 'local_autogroup ' , $ course ->shortname );
68
-
69
- global $ PAGE ;
70
-
71
- $ PAGE ->set_context ($ context );
72
- $ PAGE ->set_url (local_autogroup_renderer::URL_COURSE_MANAGE , array ('courseid ' =>$ courseid ));
73
- $ PAGE ->set_title ($ heading );
74
- $ PAGE ->set_heading ($ heading );
75
- $ PAGE ->set_pagelayout ('incourse ' );
76
- $ PAGE ->set_course ($ course );
77
-
78
- $ output = $ PAGE ->get_renderer ('local_autogroup ' );
79
-
80
-
81
- echo $ output ->header ();
82
-
83
- echo $ output ->intro_text (count ($ groupsets ));
84
-
85
- echo $ output ->groupsets_table ($ groupsets );
86
-
87
- echo $ output ->add_new_groupset ($ courseid );
88
-
89
- echo $ output ->footer ();
0 commit comments