3 require_once('../config.php');
4 require_once('change_password_form.php');
6 $id = optional_param('id', SITEID
, PARAM_INT
); // current course
8 $strparticipants = get_string('participants');
10 //HTTPS is potentially required in this page
13 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
15 if (!$course = get_record('course', 'id', $id)) {
16 error('No such course!');
19 // require proper login; guest user can not change password
20 if (empty($USER->id
) or isguestuser()) {
21 if (empty($SESSION->wantsurl
)) {
22 $SESSION->wantsurl
= $CFG->httpswwwroot
.'/login/change_password.php';
24 redirect($CFG->httpswwwroot
.'/login/index.php');
27 // do not require change own password cap if change forced
28 if (!get_user_preferences('auth_forcepasswordchange', false)) {
29 require_capability('moodle/user:changeownpassword', $systemcontext);
32 // do not allow "Logged in as" users to change any passwords
33 if (!empty($USER->realuser
)) {
34 error('Can not use this script when "Logged in as"!');
37 if (is_mnet_remote_user($USER)) {
38 $message = get_string('usercannotchangepassword', 'mnet');
39 if ($idprovider = get_record('mnet_host', 'id', $USER->mnethostid
)) {
40 $message .= get_string('userchangepasswordlink', 'mnet', $idprovider);
45 // load the appropriate auth plugin
46 $userauth = get_auth_plugin($USER->auth
);
48 if (!$userauth->can_change_password()) {
49 error(get_string('nopasswordchange', 'auth'));
52 if ($changeurl = $userauth->change_password_url()) {
53 // this internal scrip not used
57 $mform = new login_change_password_form();
58 $mform->set_data(array('id'=>$course->id
));
61 $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$course->id", 'type' => 'misc');
63 if ($mform->is_cancelled()) {
64 redirect($CFG->wwwroot
.'/user/view.php?id='.$USER->id
.'&course='.$course->id
);
65 } else if ($data = $mform->get_data()) {
67 if (!$userauth->user_update_password(addslashes_recursive($USER), $data->newpassword1
)) {
68 error(get_string('errorpasswordupdate', 'auth'));
71 // register success changing password
72 unset_user_preference('auth_forcepasswordchange', $USER->id
);
74 $strpasswordchanged = get_string('passwordchanged');
77 $eventdata = new object();
78 $eventdata -> user
= $USER;
79 $eventdata -> newpassword
= $data -> newpassword1
;
80 events_trigger('password_changed', $eventdata);
82 add_to_log($course->id
, 'user', 'change password', "view.php?id=$USER->id&course=$course->id", "$USER->id");
84 $fullname = fullname($USER, true);
86 $navlinks[] = array('name' => $fullname,
87 'link' => "$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id",
89 $navlinks[] = array('name' => $strpasswordchanged, 'link' => null, 'type' => 'misc');
90 $navigation = build_navigation($navlinks);
92 print_header($strpasswordchanged, $strpasswordchanged, $navigation);
94 if (empty($SESSION->wantsurl
) or $SESSION->wantsurl
== $CFG->httpswwwroot
.'/login/change_password.php') {
95 $returnto = "$CFG->wwwroot/user/view.php?id=$USER->id&course=$id";
97 $returnto = $SESSION->wantsurl
;
100 notice($strpasswordchanged, $returnto);
107 $strchangepassword = get_string('changepassword');
109 $fullname = fullname($USER, true);
111 $navlinks[] = array('name' => $fullname, 'link' => "$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id", 'type' => 'misc');
112 $navlinks[] = array('name' => $strchangepassword, 'link' => null, 'type' => 'misc');
113 $navigation = build_navigation($navlinks);
115 print_header($strchangepassword, $strchangepassword, $navigation);
116 if (get_user_preferences('auth_forcepasswordchange')) {
117 notify(get_string('forcepasswordchangenotice'));