Last active
February 26, 2018 13:32
-
-
Save xyulex/f468387c67525939276e3fa802c4090a to your computer and use it in GitHub Desktop.
MOODLE: WP SSO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package local_wpsso | |
* @copyright 2018 Raúl Martínez <raul@tresipunt.com> | |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
*/ | |
require('../../config.php'); | |
require_once("$CFG->dirroot/group/lib.php"); | |
global $CFG, $USER, $DB, $PAGE, $OUTPUT; | |
$PAGE->set_url($CFG->wwwroot.'/local/wpsso/automatricula.php'); | |
// Get POST data. | |
if ($_POST) { | |
print_object($_POST); | |
if (!$_POST['nombre'] || !$_POST['apellido'] | |
|| !$_POST['email'] || !$_POST['shortname'] || !$_POST['method'] ) { | |
echo 'No se están enviando todos los datos por POST.'; | |
return 'No se están enviando todos los datos por POST.'; | |
} else { | |
$firstname = $_POST['nombre']; | |
$lastname = $_POST['apellido']; | |
$email = $_POST['email']; | |
$shortname = $_POST['shortname']; | |
$method = $_POST['method']; | |
$username = explode('@', $email); | |
$password = 'tobechanged'; | |
} | |
} else { | |
echo 'El objeto POST no existe.'; | |
return 'El objeto POST no existe.'; | |
} | |
// USER PART. | |
// New user, create it. | |
if (!$exists = $DB->get_record('user', array('email' => $email))) { | |
$user = create_user_record($username[0], $password, $auth = 'manual'); | |
$user->confirmed = 1; | |
$user->email = $email; | |
$user->firstname = $firstname; | |
$user->lastname = $lastname; | |
$DB->update_record('user', $user); | |
$userid = $user->id; | |
$usernew = $DB->get_record('user', array('id' => $userid)); | |
$lang = empty($user->lang) ? $CFG->lang : $user->lang; | |
$site = get_site(); | |
$supportuser = core_user::get_support_user(); | |
$newpassword = generate_password(); | |
update_internal_user_password($user, $newpassword, false); | |
$a = new stdClass(); | |
$a->firstname = fullname($usernew, true); | |
$a->sitename = format_string($site->fullname); | |
$a->username = $usernew->username; | |
$a->newpassword = $newpassword; | |
$a->link = $CFG->wwwroot .'/login/'; | |
$a->signoff = generate_email_signoff(); | |
$a->mailformat = 1; | |
$message.= '<table><tr><td>'.(string)new lang_string('newusernewpasswordtext', '', $a, $lang).'</td></tr></table>'; | |
$subject = format_string($site->fullname) .': '. (string)new lang_string('newusernewpasswordsubj', '', $a, $lang); | |
unset_user_preference('create_password', $usernew); | |
set_user_preference('auth_forcepasswordchange', 1, $usernew); | |
email_to_user($usernew, $supportuser, $subject, null, $message); | |
echo "<br />Usuario nuevo"; | |
} else { | |
// Existing user. Get id. | |
$userid = $exists->id; | |
echo "<br />Usuario actualizado"; | |
} | |
// Check if course exists. | |
$courseexists = $DB->get_record('course', array('shortname' => $shortname)); | |
if ($courseexists) { | |
$courseid = $courseexists->id; | |
} else { | |
echo 'El curso con shortname ' . $shortname. ' no existe'; | |
return 'El curso con shortname ' . $shortname. ' no existe'; | |
} | |
// Enrol. | |
$plugin_instance = $DB->get_record("enrol", array('courseid' => $courseid, 'enrol'=>'manual')); | |
$plugin = enrol_get_plugin('manual'); | |
$roleid = $DB->get_field('role', 'id', array('shortname' => 'student')); | |
$plugin->enrol_user($plugin_instance, $userid, $roleid); | |
echo '<br />Usuario enrolado en ' . $courseid; | |
// Create group with 1 person if course is online. | |
if ( $method == 'online' ) { | |
if ( !$existing = $DB->get_record('groups', ['name' => $email]) ) { | |
$group = new stdClass(); | |
$group->courseid = $courseid; | |
$group->timecreated = time(); | |
$group->timemodified = $group->timecreated; | |
$group->name = $email; | |
$group->id = $DB->insert_record('groups', $group); | |
$groupid = $group->id; | |
$context = context_course::instance($courseid); | |
cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($courseid)); | |
// Trigger group event. | |
$params = array( | |
'context' => $context, | |
'objectid' => $group->id | |
); | |
$event = \core\event\group_created::create($params); | |
$event->add_record_snapshot('groups', $group); | |
$event->trigger(); | |
echo 'Grupo creado: ' . $group->name; | |
} else { | |
$groupid = $existing->id; | |
$groupname = $existing->name; | |
echo 'Grupo ' . $groupname . 'ya existente'; | |
} | |
// Add user to group | |
groups_add_member($groupid, $userid); | |
echo "<br />Usuario incluido en grupo: " . $email; | |
} | |
return true; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1 - Se crea usuario en plataforma
2 - Se matricula al usuario en ese curso
3 - Se crea un grupo y se añade al usuario que ha comprado el curso dependiendo del tipo de curso (i.e. online)