3 require_once('../config.php');
4 require_once('change_password_form.php');
6 $id = optional_param('id', SITEID
, PARAM_INT
);
8 //HTTPS is potentially required in this page
11 $sitecontext = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
13 if (!$course = get_record('course', 'id', $id)) {
14 error('No such course!');
17 if (is_mnet_remote_user($USER)) {
18 $message = get_string('usercannotchangepassword', 'mnet');
19 if ($idprovider = get_record('mnet_host', 'id', $USER->mnethostid
)) {
20 $message .= get_string('userchangepasswordlink', 'mnet', $idprovider);
25 // require proper login; guest can not change password
26 // TODO: add change password capability so that we can prevent participants to change password
27 if (empty($USER->id
) or isguestuser() or has_capability('moodle/legacy:guest', $sitecontext, $USER->id
, false)) {
28 if (empty($SESSION->wantsurl
)) {
29 $SESSION->wantsurl
= $CFG->httpswwwroot
.'/login/change_password.php';
31 redirect($CFG->httpswwwroot
.'/login/index.php');
34 // do not allow "Logged in as" users to change any passwords
35 if (!empty($USER->realuser
)) {
36 error('Can not use this script when "Logged in as"!');
39 // load the appropriate auth plugin
40 $userauth = get_auth_plugin($USER->auth
);
42 if (!$userauth->can_change_password()) {
43 error(get_string('nopasswordchange', 'auth'));
46 if (method_exists($userauth, 'change_password_url') and $userauth->change_password_url()) {
47 // this internal scrip not used
48 redirect($userauth->change_password_url());
51 $mform = new login_change_password_form();
52 $mform->set_data(array('id'=>$course->id
, 'username'=>$USER->username
));
54 if ($mform->is_cancelled()) {
55 redirect($CFG->wwwroot
.'/user/view.php?id='.$USER->id
.'&course='.$course->id
);
56 } else if ($data = $mform->get_data()) {
58 if (!has_capability('moodle/user:update', $sitecontext)) {
59 //ignore submitted username - the same is done in form validation
60 $data->username
= $USER->username
;
63 if ($data->username
== $USER->username
) {
66 $user = get_complete_user_data('username', $data->username
);
69 // register success changing password
70 unset_user_preference('auth_forcepasswordchange', $user->id
);
72 $strpasswordchanged = get_string('passwordchanged');
74 add_to_log($course->id
, 'user', 'change password', "view.php?id=$user->id&course=$course->id", "$user->id");
76 $fullname = fullname($USER, true);
78 if ($course->id
!= SITEID
) {
79 $navstr = "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ";
83 $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";
85 print_header($strpasswordchanged, $strpasswordchanged, $navstr);
87 if (empty($SESSION->wantsurl
) or $SESSION->wantsurl
== $CFG->httpswwwroot
.'/login/change_password.php') {
88 $returnto = "$CFG->wwwroot/user/view.php?id=$USER->id&course=$id";
90 $returnto = $SESSION->wantsurl
;
93 notice($strpasswordchanged, $returnto);
100 $strchangepassword = get_string('changepassword');
102 $fullname = fullname($USER, true);
104 if ($course->id
!= SITEID
) {
105 $navstr = "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ";
109 $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";
112 print_header($strchangepassword, $strchangepassword, $navstr);
113 if (!empty($USER->preference
['auth_forcepasswordchange'])) {
114 notify(get_string('forcepasswordchangenotice'));