Skip to content

Commit

Permalink
Adding can_hide_show_instance and can_delete_instance
Browse files Browse the repository at this point in the history
  • Loading branch information
pitbulk committed Nov 18, 2016
1 parent 33be162 commit 9f6c30c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 56 deletions.
123 changes: 73 additions & 50 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,56 +169,79 @@ public function sync_user_enrolments($user) {

if($pluginconfig->supportcourses != 'nosupport' ) {

if(!isset($pluginconfig->moodlecoursefieldid)) {
$pluginconfig->moodlecoursefieldid = 'shortname';
}
try {
$plugin = enrol_get_plugin('saml');
foreach($SAML_COURSE_INFO->mapped_roles as $role) {
$moodle_role = $DB->get_record("role", array("shortname" =>$role));
if($moodle_role) {
$new_course_ids_with_role = array();
$delete_course_ids_with_role = array();
if (isset($SAML_COURSE_INFO->mapped_courses[$role])) {
if(isset($SAML_COURSE_INFO->mapped_courses[$role]['active'])) {
$new_course_ids_with_role = array_keys($SAML_COURSE_INFO->mapped_courses[$role]['active']);
}
if(isset($SAML_COURSE_INFO->mapped_courses[$role]['inactive'])) {
$delete_course_ids_with_role = array_keys($SAML_COURSE_INFO->mapped_courses[$role]['inactive']);
}
}
if(!$pluginconfig->ignoreinactivecourses) {
foreach($delete_course_ids_with_role as $course_identify) {
if($course = $DB->get_record("course", array($pluginconfig->moodlecoursefieldid => $course_identify))) {
$instance = $plugin->get_or_create_instance($course);
if(!empty($instance)) {
$plugin->unenrol_user($instance, $user->id);
} }
}
}
foreach($new_course_ids_with_role as $course_identify) {
if($course = $DB->get_record("course", array($pluginconfig->moodlecoursefieldid => $course_identify))) {
$instance = $plugin->get_or_create_instance($course);
if(empty($instance)) {
$err['enrollment'][] = get_string("error_instance_creation", "role_saml", $role, $course->id);
}
else {
$plugin->enrol_user($instance, $user->id, $moodle_role->id, 0, 0, 0); // last parameter (status) 0->active 1->suspended
}
}
}
}
else {
$err['enrollment'][] = get_string("auth_saml_error_role_not_found", "auth_saml", $role);
}
}
}
catch (Exception $e) {
$err['enrollment'][] = $e->getMessage();
}
unset($SAML_COURSE_INFO->mapped_courses);
unset($SAML_COURSE_INFO->mapped_roles);
}
if(!isset($pluginconfig->moodlecoursefieldid)) {
$pluginconfig->moodlecoursefieldid = 'shortname';
}
try {
$plugin = enrol_get_plugin('saml');
foreach($SAML_COURSE_INFO->mapped_roles as $role) {
$moodle_role = $DB->get_record("role", array("shortname" =>$role));
if($moodle_role) {
$new_course_ids_with_role = array();
$delete_course_ids_with_role = array();
if (isset($SAML_COURSE_INFO->mapped_courses[$role])) {
if(isset($SAML_COURSE_INFO->mapped_courses[$role]['active'])) {
$new_course_ids_with_role = array_keys($SAML_COURSE_INFO->mapped_courses[$role]['active']);
}
if(isset($SAML_COURSE_INFO->mapped_courses[$role]['inactive'])) {
$delete_course_ids_with_role = array_keys($SAML_COURSE_INFO->mapped_courses[$role]['inactive']);
}
}
if(!$pluginconfig->ignoreinactivecourses) {
foreach($delete_course_ids_with_role as $course_identify) {
if($course = $DB->get_record("course", array($pluginconfig->moodlecoursefieldid => $course_identify))) {
$instance = $plugin->get_or_create_instance($course);
if(!empty($instance)) {
$plugin->unenrol_user($instance, $user->id);
}
}
}
}
foreach($new_course_ids_with_role as $course_identify) {
if($course = $DB->get_record("course", array($pluginconfig->moodlecoursefieldid => $course_identify))) {
$instance = $plugin->get_or_create_instance($course);
if(empty($instance)) {
$err['enrollment'][] = get_string("error_instance_creation", "role_saml", $role, $course->id);
}
else {
$plugin->enrol_user($instance, $user->id, $moodle_role->id, 0, 0, 0); // last parameter (status) 0->active 1->suspended
}
}
}
}
else {
$err['enrollment'][] = get_string("auth_saml_error_role_not_found", "auth_saml", $role);
}
}
}
catch (Exception $e) {
$err['enrollment'][] = $e->getMessage();
}
unset($SAML_COURSE_INFO->mapped_courses);
unset($SAML_COURSE_INFO->mapped_roles);
}
}

/**
* Is it possible to hide/show enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_hide_show_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/apply:config', $context);
}

/**
* Is it possible to delete enrol instance via standard UI?
*
* @param stdClass $instance
* @return bool
*/
public function can_delete_instance($instance) {
$context = context_course::instance($instance->courseid);
return has_capability('enrol/apply:config', $context);
}
}

Expand Down
6 changes: 2 additions & 4 deletions moodle_enrol_saml.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
SAML Enrollments for Moodle
-------------------------------------------------------------------------------

SAML for Moodle is an open-source implementation for federated identity-based
authentication infrastructure based on Security Assertion Markup Language
(SAML) that allows the Moodle users log in through simpleSAMLphp.
SAML for Moodle is an open-source implementation for federated identity-based authentication infrastructure based on Security Assertion Markup Language (SAML) that allows the Moodle users log in through simpleSAMLphp.


Install Instruction
Expand All @@ -12,7 +10,7 @@ Install Instruction
1. Unpack this saml directory into the /enrol/saml directory as you would for any Moodle enrol module
(http://docs.moodle.org/en/Installing_contributed_modules_or_plugins).

2. Login to Moodle as an administrator, and activate the module by navigating
2. Login to Moodle as an administrator, and activate the module by navigating
Site administration->Plugins->Enrolments->Manage enrol plugins->SAML enrolments


Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

$plugin->version = 2017111701;
$plugin->version = 2017111801;
$plugin->requires = 2012062500;
$plugin->release = 'enrol/saml version of 17-11-2016';
$plugin->release = 'enrol/saml version of 18-11-2016';
$plugin->maturity = MATURITY_BETA;
$plugin->component = 'enrol_saml';

0 comments on commit 9f6c30c

Please sign in to comment.