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 can not change password
18 // TODO: add change password capability so that we can prevent participants from changing password
19 if (empty($USER->id
) or isguestuser() or has_capability('moodle/legacy:guest', $systemcontext, $USER->id
, false)) {
20 if (empty($SESSION->wantsurl
)) {
21 $SESSION->wantsurl
= $CFG->httpswwwroot
.'/login/change_password.php';
23 redirect($CFG->httpswwwroot
.'/login/index.php');
26 // do not allow "Logged in as" users to change any passwords
27 if (!empty($USER->realuser
)) {
28 error('Can not use this script when "Logged in as"!');
31 if (is_mnet_remote_user($USER)) {
32 $message = get_string('usercannotchangepassword', 'mnet');
33 if ($idprovider = get_record('mnet_host', 'id', $USER->mnethostid
)) {
34 $message .= get_string('userchangepasswordlink', 'mnet', $idprovider);
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 ($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
));
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 (!$userauth->user_update_password(addslashes_recursive($USER), $data->newpassword1
)) {
59 error(get_string('errorpasswordupdate', 'auth'));
62 // register success changing password
63 unset_user_preference('auth_forcepasswordchange', $USER->id
);
65 $strpasswordchanged = get_string('passwordchanged');
67 add_to_log($course->id
, 'user', 'change password', "view.php?id=$USER->id&course=$course->id", "$USER->id");
69 $fullname = fullname($USER, true);
71 if ($course->id
!= SITEID
) {
72 $navstr = "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ";
76 $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";
78 print_header($strpasswordchanged, $strpasswordchanged, $navstr);
80 if (empty($SESSION->wantsurl
) or $SESSION->wantsurl
== $CFG->httpswwwroot
.'/login/change_password.php') {
81 $returnto = "$CFG->wwwroot/user/view.php?id=$USER->id&course=$id";
83 $returnto = $SESSION->wantsurl
;
86 notice($strpasswordchanged, $returnto);
93 $strchangepassword = get_string('changepassword');
95 $fullname = fullname($USER, true);
97 if ($course->id
!= SITEID
) {
98 $navstr = "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ";
102 $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";
105 print_header($strchangepassword, $strchangepassword, $navstr);
106 if (get_user_preferences('auth_forcepasswordchange')) {
107 notify(get_string('forcepasswordchangenotice'));