Skip to content

Commit a5a9625

Browse files
Update AutoGroup to Totara 12 capability
1 parent 257c57d commit a5a9625

File tree

7 files changed

+179
-80
lines changed

7 files changed

+179
-80
lines changed

classes/domain/autogroup_set.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -658,4 +658,4 @@ private function validate_object($autogroupset)
658658
*/
659659
private $roles = array();
660660

661-
}
661+
}

classes/domain/group.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,4 @@ private function validate_object($group)
303303
*/
304304
private $members;
305305

306-
}
306+
}

classes/domain/user.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,4 @@ private function parse_user_data ($user, \moodle_database $db)
177177
* @var /stdclass
178178
*/
179179
private $object;
180-
}
180+
}

classes/sort_module/primary_position.php renamed to classes/sort_module/position.php

+22-14
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
*
4141
* @package local_autogroup\domain
4242
*/
43-
class primary_position extends sort_module
44-
{
43+
class position extends sort_module {
4544
/**
4645
* @param stdClass $config
4746
* @param int $courseid
@@ -80,23 +79,32 @@ public function eligible_groups_for_user(stdClass $user)
8079
{
8180
global $CFG;
8281
require_once("{$CFG->dirroot}/totara/hierarchy/prefix/position/lib.php");
82+
require_once("{$CFG->dirroot}/totara/hierarchy/lib.php");
8383

8484
$field = $this->field . 'id';
8585

86-
// Attempt to load the assignment
87-
$primarypos = new \position_assignment(
88-
array(
89-
'userid' => $user->id,
90-
'type' => POSITION_TYPE_PRIMARY
91-
)
92-
);
86+
// Attempt to load the assignment -- Totara 9 only
87+
$pos = new \stdClass();
88+
if (class_exists("position_assignment")) {
89+
$pos = new \position_assignment(
90+
array(
91+
'userid' => $user->id
92+
)
93+
);
94+
} else {
95+
$pos = new \position(
96+
array(
97+
'userid' => $user->id
98+
)
99+
);
100+
}
93101

94-
if (isset($primarypos->$field) && !empty($primarypos->$field)) {
102+
if (isset($pos->$field) && !empty($pos->$field)) {
95103
$method = 'parse_name_' . $this->field; // like parse_name_manager();
96104

97105
$group = new stdClass();
98-
$group->idnumber = $this->field . '_' . $primarypos->$field;
99-
$group->friendlyname = $this->$method($primarypos->$field);
106+
$group->idnumber = $this->field . '_' . $pos->$field;
107+
$group->friendlyname = $this->$method($pos->$field);
100108

101109
return array($group);
102110
} else {
@@ -115,7 +123,7 @@ public function get_config_options()
115123
$options = array(
116124
'organisation' => get_string('organisation', 'totara_hierarchy'),
117125
'position' => get_string('position', 'totara_hierarchy'),
118-
'manager' => get_string('manager', 'totara_hierarchy'),
126+
'manager' => get_string('manager', 'tool_totara_sync'),
119127
);
120128
return $options;
121129
}
@@ -170,4 +178,4 @@ private function parse_name_position($id)
170178

171179
}
172180

173-
}
181+
}

edit.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,4 @@
190190

191191
$form->display();
192192

193-
echo $output->footer();
193+
echo $output->footer();

manage.php

+151-60
Original file line numberDiff line numberDiff line change
@@ -17,73 +17,164 @@
1717
/**
1818
* autogroup local plugin
1919
*
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.
2327
*
2428
* @package local
2529
* @subpackage autogroup
2630
* @author Mark Ward ([email protected])
27-
* @date April 2015
31+
* @date December 2014
2832
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2933
*/
3034

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;
5036

51-
require_capability('local/autogroup:managecourse', $context);
37+
use local_autogroup\domain;
38+
use local_autogroup\exception;
5239

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;
65180
}
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();

version.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030

3131
defined('MOODLE_INTERNAL') || die();
3232

33-
$plugin->version = 2018102400;
33+
$plugin->version = 2020061800;
3434
$plugin->requires = 2013111800.00; // Requires this Moodle version (2.7).
35-
$plugin->release = '2.4.1'; // Plugin release.
35+
$plugin->release = '2.4.2'; // Plugin release.
3636
$plugin->component = 'local_autogroup'; // Full name of the plugin (used for diagnostics).
3737
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)