3 require_once('../config.php');
4 require_once('change_password_form.php');
6 $id = optional_param('id', SITEID
, PARAM_INT
); // current course
8 //HTTPS is potentially required in this page
11 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
13 if (!$course = get_record('course', 'id', $id)) {
14 error('No such course!');
17 // require proper login; guest user can not change password
18 if (empty($USER->id
) or isguestuser()) {
19 if (empty($SESSION->wantsurl
)) {
20 $SESSION->wantsurl
= $CFG->httpswwwroot
.'/login/change_password.php';
22 redirect($CFG->httpswwwroot
.'/login/index.php');
25 // do not require change own password cap if change forced
26 if (!get_user_preferences('auth_forcepasswordchange', false)) {
27 require_capability('moodle/user:changeownpassword', $systemcontext);
30 // do not allow "Logged in as" users to change any passwords
31 if (!empty($USER->realuser
)) {
32 error('Can not use this script when "Logged in as"!');
35 if (is_mnet_remote_user($USER)) {
36 $message = get_string('usercannotchangepassword', 'mnet');
37 if ($idprovider = get_record('mnet_host', 'id', $USER->mnethostid
)) {
38 $message .= get_string('userchangepasswordlink', 'mnet', $idprovider);
43 // load the appropriate auth plugin
44 $userauth = get_auth_plugin($USER->auth
);
46 if (!$userauth->can_change_password()) {
47 error(get_string('nopasswordchange', 'auth'));
50 if ($changeurl = $userauth->change_password_url()) {
51 // this internal scrip not used
55 $mform = new login_change_password_form();
56 $mform->set_data(array('id'=>$course->id
));
58 if ($mform->is_cancelled()) {
59 redirect($CFG->wwwroot
.'/user/view.php?id='.$USER->id
.'&course='.$course->id
);
60 } else if ($data = $mform->get_data()) {
62 if (!$userauth->user_update_password(addslashes_recursive($USER), $data->newpassword1
)) {
63 error(get_string('errorpasswordupdate', 'auth'));
66 // register success changing password
67 unset_user_preference('auth_forcepasswordchange', $USER->id
);
69 $strpasswordchanged = get_string('passwordchanged');
72 $eventdata = new object();
73 $eventdata -> user
= $USER;
74 $eventdata -> newpassword
= $data -> newpassword1
;
75 events_trigger('password_changed', $eventdata);
77 add_to_log($course->id
, 'user', 'change password', "view.php?id=$USER->id&course=$course->id", "$USER->id");
79 $fullname = fullname($USER, true);
81 if ($course->id
!= SITEID
) {
82 $navstr = "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ";
86 $navstr .= "<a href=\"$CFG->wwwroot/user/index.php?id=$course->id\">".get_string("participants")."</a> -> <a href=\"$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id\">$fullname</a> -> $strpasswordchanged";
88 print_header($strpasswordchanged, $strpasswordchanged, $navstr);
90 if (empty($SESSION->wantsurl
) or $SESSION->wantsurl
== $CFG->httpswwwroot
.'/login/change_password.php') {
91 $returnto = "$CFG->wwwroot/user/view.php?id=$USER->id&course=$id";
93 $returnto = $SESSION->wantsurl
;
96 notice($strpasswordchanged, $returnto);
103 $strchangepassword = get_string('changepassword');
105 $fullname = fullname($USER, true);
107 if ($course->id
!= SITEID
) {
108 $navstr = "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ";
112 $navstr .= "<a href=\"$CFG->wwwroot/user/index.php?id=$course->id\">".get_string('participants')."</a> -> <a href=\"$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id\">$fullname</a> -> $strchangepassword";
115 print_header($strchangepassword, $strchangepassword, $navstr);
116 if (get_user_preferences('auth_forcepasswordchange')) {
117 notify(get_string('forcepasswordchangenotice'));