Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-18964

Modified custom profiles are not saved in session data

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical
    • Resolution: Fixed
    • Affects Version/s: 1.9.3
    • Fix Version/s: 2.0
    • Component/s: Authentication
    • Labels:
      None
    • Environment:
      moodle 1.9.4 ubuntu
        $release = '1.9.3+ (Build: 20081112)
    • Affected Branches:
      MOODLE_19_STABLE
    • Fixed Branches:
      MOODLE_20_STABLE

      Description

      When writing custom code using mforms we were pleased to find that the $USER global contains all relevant fields for the current user, so no need to fetch them again from the DB. Great.

      But we noticed that when an user edits his profil and modify some of his custom fields, these fields are indeed saved into the database but the global $USER is not updated for the new values. So he has to logout/login again ( and close its browser if using some CAS authentication plugin) to have these new values in the global $USER...
      Quite a pain for us since we ask them in our custom forms to make sure some profile's custom fields are correct before processing (and to swap to the 'edit profile page' if some of the custom fields are missing or if he wants them to be changed) .

      we found the culprit code in the validation part of user/edit.php or user/editadvanced.php :

      after saving the custom fields from usernew to the table mdl_user_info_data
      the same variable $usernew is immediately after reloaded from table mdl_user, thus loosing all custom fields names and values...
      and later on the code that update $USER just ignore all custom fields ; see below :

      // save custom profile fields data
      profile_save_data($usernew); <-- custom fields new values are still there OK and saved into

      // reload from db
      $usernew = get_record('user', 'id', $usernew->id); <--- this is done to early so it destroys edited custom user profile

      // trigger events
      if ($usercreated)

      { events_trigger('user_created', $usernew); }

      else

      { events_trigger('user_updated', $usernew); }

      if ($user->id == $USER->id) {
      // Override old $USER session variable
      print_r($usernew); <--- testing code : do not contains anymore custom user profile fields
      foreach ((array)$usernew as $variable => $value)

      { $USER->$variable = $value; <-- so the global USER is not updated and keeps old values }

      print_r($USER); die(); <-- Testing code still has the old values got when user logged in

      if (!empty($USER->newadminuser))

      { unset($USER->newadminuser); // apply defaults again - some of them might depend on admin user info, backup, roles, etc. admin_apply_default_settings(NULL , false); // redirect to admin/ to continue with installation redirect("$CFG->wwwroot/$CFG->admin/"); }

      else

      { redirect("$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id"); }

      } else

      { redirect("$CFG->wwwroot/$CFG->admin/user.php"); }

      our current solution is to duplicate code just before reading again the BD

      if ($user->id == $USER->id) {
      foreach ((array)$usernew as $variable => $value)

      { $USER->$variable = $value; }

      // reload from db
      $usernew = get_record('user', 'id', $usernew->id);

      It works here but I just don't know if this will create some unwanted side effects elsewhere

      Cheers.

        Attachments

          Activity

            People

            Assignee:
            skodak Petr Skoda
            Reporter:
            ppollet Patrick Pollet
            Participants:
            Component watchers:
            Adrian Greeve, Jake Dallimore, Mathew May, Mihail Geshoski, Peter Dias
            Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Fix Release Date:
              24/Nov/10