Skip to content

Commit

Permalink
MDL-39961 Events: Replace Legacy events - User
Browse files Browse the repository at this point in the history
This change includes:
* Added user_updated event, replacing old event
* Added user_created event, replacing old event
* Added user_deleted event, replacing old event
* Added user_loggedout event, replacing old event
* Added user_enrolment_created event, replacing old event
* Added user_enrolment_deleted event, replacing old event
* Added user_enrolment_updated event, replacing old event
  • Loading branch information
Rajesh Taneja authored and Damyon Wiese committed Sep 2, 2013
1 parent 80032fe commit bb78e24
Show file tree
Hide file tree
Showing 38 changed files with 1,517 additions and 337 deletions.
13 changes: 4 additions & 9 deletions admin/tool/uploaduser/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/csvlib.class.php');
require_once($CFG->dirroot.'/user/profile/lib.php');
require_once($CFG->dirroot.'/user/lib.php');
require_once($CFG->dirroot.'/group/lib.php');
require_once($CFG->dirroot.'/cohort/lib.php');
require_once('locallib.php');
Expand Down Expand Up @@ -654,9 +655,8 @@
}

if ($doupdate or $existinguser->password !== $oldpw) {
// we want only users that were really updated

$DB->update_record('user', $existinguser);
// We want only users that were really updated.
user_update_user($existinguser, false);

$upt->track('status', $struserupdated);
$usersupdated++;
Expand All @@ -668,8 +668,6 @@
profile_save_data($existinguser);
}

events_trigger('user_updated', $existinguser);

if ($bulk == UU_BULK_UPDATED or $bulk == UU_BULK_ALL) {
if (!in_array($user->id, $SESSION->bulk_users)) {
$SESSION->bulk_users[] = $user->id;
Expand Down Expand Up @@ -789,8 +787,7 @@
$upt->track('password', '-', 'normal', false);
}

// create user - insert_record ignores any extra properties
$user->id = $DB->insert_record('user', $user);
$user->id = user_create_user($user, false);
$upt->track('username', html_writer::link(new moodle_url('/user/profile.php', array('id'=>$user->id)), s($user->username)), 'normal', false);

// pre-process custom profile menu fields data from csv file
Expand All @@ -812,8 +809,6 @@
// make sure user context exists
context_user::instance($user->id);

events_trigger('user_created', $user);

if ($bulk == UU_BULK_NEW or $bulk == UU_BULK_ALL) {
if (!in_array($user->id, $SESSION->bulk_users)) {
$SESSION->bulk_users[] = $user->id;
Expand Down
13 changes: 4 additions & 9 deletions admin/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/authlib.php');
require_once($CFG->dirroot.'/user/filters/lib.php');
require_once($CFG->dirroot.'/user/lib.php');

$delete = optional_param('delete', 0, PARAM_INT);
$confirm = optional_param('confirm', '', PARAM_ALPHANUM); //md5 confirmation hash
Expand Down Expand Up @@ -123,12 +124,9 @@
if ($user = $DB->get_record('user', array('id'=>$suspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
if (!is_siteadmin($user) and $USER->id != $user->id and $user->suspended != 1) {
$user->suspended = 1;
$user->timemodified = time();
$DB->set_field('user', 'suspended', $user->suspended, array('id'=>$user->id));
$DB->set_field('user', 'timemodified', $user->timemodified, array('id'=>$user->id));
// force logout
// Force logout.
session_kill_user($user->id);
events_trigger('user_updated', $user);
user_update_user($user, false);
}
}
redirect($returnurl);
Expand All @@ -139,10 +137,7 @@
if ($user = $DB->get_record('user', array('id'=>$unsuspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
if ($user->suspended != 0) {
$user->suspended = 0;
$user->timemodified = time();
$DB->set_field('user', 'suspended', $user->suspended, array('id'=>$user->id));
$DB->set_field('user', 'timemodified', $user->timemodified, array('id'=>$user->id));
events_trigger('user_updated', $user);
user_update_user($user, false);
}
}
redirect($returnurl);
Expand Down
4 changes: 2 additions & 2 deletions auth/db/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ function sync_users(progress_trace $trace, $do_updates=false) {
$remove_users = $DB->get_records_sql($sql, $params);

if (!empty($remove_users)) {
require_once($CFG->dirroot.'/user/lib.php');
$trace->output(get_string('auth_dbuserstoremove','auth_db', count($remove_users)));

foreach ($remove_users as $user) {
Expand All @@ -294,8 +295,7 @@ function sync_users(progress_trace $trace, $do_updates=false) {
$updateuser = new stdClass();
$updateuser->id = $user->id;
$updateuser->suspended = 1;
$updateuser->timemodified = time();
$DB->update_record('user', $updateuser);
user_update_user($updateuser, false);
$trace->output(get_string('auth_dbsuspenduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)), 1);
}
}
Expand Down
8 changes: 3 additions & 5 deletions auth/email/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,15 @@ function can_signup() {
function user_signup($user, $notify=true) {
global $CFG, $DB;
require_once($CFG->dirroot.'/user/profile/lib.php');
require_once($CFG->dirroot.'/user/lib.php');

$user->password = hash_internal_user_password($user->password);

$user->id = $DB->insert_record('user', $user);
$user->id = user_create_user($user, false);

/// Save any custom profile field information
// Save any custom profile field information.
profile_save_data($user);

$user = $DB->get_record('user', array('id'=>$user->id));
events_trigger('user_created', $user);

if (! send_confirmation_email($user)) {
print_error('auth_emailnoemail','auth_email');
}
Expand Down
53 changes: 26 additions & 27 deletions auth/ldap/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@

require_once($CFG->libdir.'/authlib.php');
require_once($CFG->libdir.'/ldaplib.php');
require_once($CFG->dirroot.'/user/lib.php');

/**
* LDAP authentication plugin.
Expand Down Expand Up @@ -550,7 +551,7 @@ function user_signup($user, $notify=true) {
print_error('auth_ldap_create_error', 'auth_ldap');
}

$user->id = $DB->insert_record('user', $user);
$user->id = user_create_user($user, false);

// Save any custom profile field information
profile_save_data($user);
Expand All @@ -562,7 +563,6 @@ function user_signup($user, $notify=true) {
update_internal_user_password($user, $plainslashedpassword);

$user = $DB->get_record('user', array('id'=>$user->id));
events_trigger('user_created', $user);

if (! send_confirmation_email($user)) {
print_error('noemail', 'auth_ldap');
Expand Down Expand Up @@ -612,12 +612,12 @@ function user_confirm($username, $confirmsecret) {
if (!$this->user_activate($username)) {
return AUTH_CONFIRM_FAIL;
}
$DB->set_field('user', 'confirmed', 1, array('id'=>$user->id));
$user->confirmed = 1;
if ($user->firstaccess == 0) {
$DB->set_field('user', 'firstaccess', time(), array('id'=>$user->id));
$user->firstaccess = time();
}
$euser = $DB->get_record('user', array('id' => $user->id));
events_trigger('user_updated', $euser);
require_once($CFG->dirroot.'/user/lib.php');
user_update_user($user, false);
return AUTH_CONFIRM_OK;
}
} else {
Expand Down Expand Up @@ -806,10 +806,8 @@ function sync_users($do_updates=true) {
$updateuser = new stdClass();
$updateuser->id = $user->id;
$updateuser->suspended = 1;
$DB->update_record('user', $updateuser);
user_update_user($updateuser, false);
echo "\t"; print_string('auth_dbsuspenduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
$euser = $DB->get_record('user', array('id' => $user->id));
events_trigger('user_updated', $euser);
session_kill_user($user->id);
}
} else {
Expand All @@ -835,10 +833,8 @@ function sync_users($do_updates=true) {
$updateuser->id = $user->id;
$updateuser->auth = $this->authtype;
$updateuser->suspended = 0;
$DB->update_record('user', $updateuser);
user_update_user($updateuser, false);
echo "\t"; print_string('auth_dbreviveduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
$euser = $DB->get_record('user', array('id' => $user->id));
events_trigger('user_updated', $euser);
}
} else {
print_string('nouserentriestorevive', 'auth_ldap');
Expand Down Expand Up @@ -950,10 +946,10 @@ function sync_users($do_updates=true) {
$user->lang = $CFG->lang;
}

$id = $DB->insert_record('user', $user);
$id = user_create_user($user, false);
echo "\t"; print_string('auth_dbinsertuser', 'auth_db', array('name'=>$user->username, 'id'=>$id)); echo "\n";
$euser = $DB->get_record('user', array('id' => $id));
events_trigger('user_created', $euser);

if (!empty($this->config->forcechangepassword)) {
set_user_preference('auth_forcepasswordchange', 1, $id);
}
Expand Down Expand Up @@ -1011,22 +1007,25 @@ function update_user_record($username, $updatekeys = false) {
$updatekeys = array_keys($newinfo);
}

foreach ($updatekeys as $key) {
if (isset($newinfo[$key])) {
$value = $newinfo[$key];
} else {
$value = '';
}
if (!empty($updatekeys)) {
$newuser = new stdClass();
$newuser->id = $userid;

foreach ($updatekeys as $key) {
if (isset($newinfo[$key])) {
$value = $newinfo[$key];
} else {
$value = '';
}

if (!empty($this->config->{'field_updatelocal_' . $key})) {
if ($user->{$key} != $value) { // only update if it's changed
$DB->set_field('user', $key, $value, array('id'=>$userid));
if (!empty($this->config->{'field_updatelocal_' . $key})) {
// Only update if it's changed.
if ($user->{$key} != $value) {
$newuser->$key = $value;
}
}
}
}
if (!empty($updatekeys)) {
$euser = $DB->get_record('user', array('id' => $userid));
events_trigger('user_updated', $euser);
user_update_user($newuser, false);
}
} else {
return false;
Expand Down
4 changes: 2 additions & 2 deletions auth/mnet/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ function confirm_mnet_session($token, $remotepeer) {
global $CFG, $DB;
require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';
require_once $CFG->libdir . '/gdlib.php';
require_once($CFG->dirroot.'/user/lib.php');

// verify the remote host is configured locally before attempting RPC call
if (! $remotehost = $DB->get_record('mnet_host', array('wwwroot' => $remotepeer->wwwroot, 'deleted' => 0))) {
Expand Down Expand Up @@ -361,8 +362,7 @@ function confirm_mnet_session($token, $remotepeer) {
if (empty($localuser->firstaccess)) { // Now firstaccess, grab it here
$localuser->firstaccess = time();
}

$DB->update_record('user', $localuser);
user_update_user($localuser, false);

if (!$firsttime) {
// repeat customer! let the IDP know about enrolments
Expand Down
34 changes: 33 additions & 1 deletion badges/classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static function course_module_criteria_review(\core\event\course_module_c
/**
* Triggered when 'course_completed' event is triggered.
*
* @param \core\event\course_completed $event
* @param \core\event\course_completed $event
*/
public static function course_criteria_review(\core\event\course_completed $event) {
global $DB, $CFG;
Expand Down Expand Up @@ -105,4 +105,36 @@ public static function course_criteria_review(\core\event\course_completed $even
}
}
}

/**
* Triggered when 'user_updated' event happens.
*
* @param \core\event\user_updated $event event generated when user profile is updated.
*/
public static function profile_criteria_review(\core\event\user_updated $event) {
global $DB, $CFG;

if (!empty($CFG->enablebadges)) {
require_once($CFG->dirroot.'/lib/badgeslib.php');
$userid = $event->objectid;

if ($rs = $DB->get_records('badge_criteria', array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE))) {
foreach ($rs as $r) {
$badge = new badge($r->badgeid);
if (!$badge->is_active() || $badge->is_issued($userid)) {
continue;
}

if ($badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->review($userid)) {
$badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->mark_complete($userid);

if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
$badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
$badge->issue($userid);
}
}
}
}
}
}
}
19 changes: 19 additions & 0 deletions badges/tests/badgeslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,23 @@ public function test_badges_observer_course_criteria_review() {
$this->assertDebuggingCalled('Error baking badge image!');
$this->assertTrue($badge->is_issued($this->user->id));
}

/**
* Test badges observer when user_updated event is fired.
*/
public function test_badges_observer_profile_criteria_review() {
$badge = new badge($this->coursebadge);
$this->assertFalse($badge->is_issued($this->user->id));

$criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
$criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
$criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
$criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address'));

$this->user->address = 'Test address';
user_update_user($this->user, false);
// Check if badge is awarded.
$this->assertDebuggingCalled('Error baking badge image!');
$this->assertTrue($badge->is_issued($this->user->id));
}
}
12 changes: 11 additions & 1 deletion enrol/manual/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,17 @@ public function process(course_enrolment_manager $manager, array $users, stdClas
foreach ($user->enrolments as $enrolment) {
$enrolment->courseid = $enrolment->enrolmentinstance->courseid;
$enrolment->enrol = 'manual';
events_trigger('user_enrol_modified', $enrolment);
// Trigger event.
$event = \core\event\user_enrolment_updated::create(
array(
'objectid' => $enrolment->id,
'courseid' => $enrolment->courseid,
'context' => context_course::instance($enrolment->courseid),
'relateduserid' => $user->id,
'other' => array('enrol' => 'manual')
)
);
$event->trigger();
}
}
return true;
Expand Down
Loading

0 comments on commit bb78e24

Please sign in to comment.