Moodle
  1. Moodle
  2. MDL-20665

error on add new user profile category

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.9.6
    • Fix Version/s: 1.9.7
    • Component/s: Other
    • Labels:
      None
    • Database:
      PostgreSQL
    • Difficulty:
      Easy
    • Affected Branches:
      MOODLE_19_STABLE
    • Fixed Branches:
      MOODLE_19_STABLE
    • Rank:
      31899

      Description

      when logged in as admin using the "user profile fields" administration screen /user/profile/index.php when I use the "create new profile category" button, enter a text string and save I get an sql error:

      ERROR: invalid input syntax for integer: ""

      SELECT * FROM dcfs_user_info_category WHERE id = ''

      • line 796 of lib/dmllib.php: call to debugging()
      • line 591 of lib/dmllib.php: call to get_recordset_sql()
      • line 531 of lib/dmllib.php: call to get_record_sql()
      • line 34 of user/profile/index_category_form.php: call to get_record()
      • line 333 of lib/formslib.php: call to category_form->validation()
      • line 378 of lib/formslib.php: call to moodleform->is_validated()
      • line 415 of user/profile/definelib.php: call to moodleform->get_data()
      • line 88 of user/profile/index.php: call to profile_edit_category()

      This is caused by the form validation which aims to avoid you creating profile categories with duplicate names. MySQL doesn't complain about this SQL statement and so does the validation succesfully. Postgres does not like the SQL, as shown above, and ultimately allows you to enter duplicates.

      I discussed this with Eloy in the HQ chat http://moodle.org/mod/cvsadmin/view.php?conversationid=3510#c154302 where we agreed the code needs a minor tweak to only check the current name of the category if you're editing an existing category rather than adding a new one.

        Activity

        Hide
        Colin Chambers added a comment -

        Guys,

        we've decided on this for our code. We've gone for ease of understanding over shortest number of lines. I've checked against all test cases I think are valid.

        • New category duplicate name fails, new name ok
        • Update category same name ok, new unique name ok, new duplicate name fails

        function validation($data, $files) {
        global $CFG;
        $errors = parent::validation($data, $files);
        $data = (object)$data;

        $duplicate = record_exists('user_info_category', 'name', $data->name);

        /// Check the name is unique
        if (!empty($data->id))

        { // we are editing an existing record $olddata = get_record('user_info_category', 'id', $data->id); // name has changed, new name in use, new name in use by another record $dupfound = (($olddata->name !== $data->name) && $duplicate && ($data->id != $duplicate->id)); }

        else

        { // new profile category $dupfound = $duplicate; }

        if ($dupfound )

        { $errors['name'] = get_string('profilecategorynamenotunique', 'admin'); }

        return $errors;
        }

        Show
        Colin Chambers added a comment - Guys, we've decided on this for our code. We've gone for ease of understanding over shortest number of lines. I've checked against all test cases I think are valid. New category duplicate name fails, new name ok Update category same name ok, new unique name ok, new duplicate name fails function validation($data, $files) { global $CFG; $errors = parent::validation($data, $files); $data = (object)$data; $duplicate = record_exists('user_info_category', 'name', $data->name); /// Check the name is unique if (!empty($data->id)) { // we are editing an existing record $olddata = get_record('user_info_category', 'id', $data->id); // name has changed, new name in use, new name in use by another record $dupfound = (($olddata->name !== $data->name) && $duplicate && ($data->id != $duplicate->id)); } else { // new profile category $dupfound = $duplicate; } if ($dupfound ) { $errors['name'] = get_string('profilecategorynamenotunique', 'admin'); } return $errors; }
        Hide
        Jenny Gray added a comment -

        user/profile/index_category_form.php fixed in head and 1.9 branch, tagged as merged

        Show
        Jenny Gray added a comment - user/profile/index_category_form.php fixed in head and 1.9 branch, tagged as merged

          People

          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: