Moodle
  1. Moodle
  2. MDL-28132

long question categories names causes errors on upgrade

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 2.0.3, 2.1
    • Fix Version/s: 2.0.4
    • Component/s: Questions
    • Labels:
      None
    • Database:
      Any
    • Testing Instructions:
      Hide

      1. Create a question category with a very long name.
      2. Create a question in that category (e.g. truefalse)
      3. Try to add one random question from that category to a quiz.

      Show
      1. Create a question category with a very long name. 2. Create a question in that category (e.g. truefalse) 3. Try to add one random question from that category to a quiz.
    • Affected Branches:
      MOODLE_20_STABLE, MOODLE_21_STABLE
    • Fixed Branches:
      MOODLE_20_STABLE
    • Pull from Repository:
    • Pull Master Branch:

      Description

      I have categories with very long names.
      On upgrade from moodle 1.9 this causes error:

      !!! Error writing to database !!!
      !! Data too long for column 'name' at row 27
      UPDATE mdl_question SET name = ? WHERE qtype = 'random' AND category = ? AND questiontext = ?
      [array (
      0 => 'Random ( VERY LONG STRING HERE IN RUSSIAN (two bytes per char in utf8) and sub-categories)',
      1 => '7926'
      2 => '1',
      )] !!
      !! Stack trace: * line 394 of /lib/dml/moodle_database.php: dml_write_exception thrown

      • line 1056 of /lib/dml/mysqli_native_moodle_database.php: call to moodle_database->query_end()
      • line 50 of /question/type/multichoice/db/upgrade.php: call to mysqli_native_moodle_database->set_field_select()
      • line 373 of /lib/upgradelib.php: call to xmldb_qtype_multichoice_upgrade()
      • line 1421 of /lib/upgradelib.php: call to upgrade_plugins()
      • line 146 of /admin/cli/upgrade.php: call to upgrade_noncore()

      problem is in /question/type/multichoice/db/upgrade.php:
      where str length of $randomqname is not limited.

      $randomqname = $QTYPES[RANDOM]->question_name($cat, false);
      $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '0'));

      better is to limit it into
      /question/type/random/questiontype.php :

      function question_name($category, $includesubcategories) should be something like :

      function question_name($category, $includesubcategories) {

      +$max = 50;

      +$str = $category->name;

      +if (mb_strlen($str,'utf-8') > $max) {$str = mb_substr($str,0,$max-3,'utf-8').'...';}

      if ($includesubcategories)

      { $string = 'randomqplusname'; }

      else

      { $string = 'randomqname'; }

      return get_string($string, 'qtype_random', $category>name);
      +return get_string($string, 'qtype_random', $str);
      }

        Gliffy Diagrams

          Activity

          Hide
          Vadim Dvorovenko added a comment -

          Limiting question name should also make quizes, containing many random questions, to look better.

          p.s. Sorry have no time to register on github and create normall pull request

          Show
          Vadim Dvorovenko added a comment - Limiting question name should also make quizes, containing many random questions, to look better. p.s. Sorry have no time to register on github and create normall pull request
          Hide
          Tim Hunt added a comment -

          Note that it is better to use Moodle's build-in shorten_text function.

          Show
          Tim Hunt added a comment - Note that it is better to use Moodle's build-in shorten_text function.
          Hide
          Eloy Lafuente (stronk7) added a comment -

          Integrated, thanks!

          Show
          Eloy Lafuente (stronk7) added a comment - Integrated, thanks!
          Hide
          Rajesh Taneja added a comment -

          Works Great Tim
          Thanks for providing the fix.

          One question:
          Can we consider shortening category text, while selecting a category in Question bank (Drop down)?

          Show
          Rajesh Taneja added a comment - Works Great Tim Thanks for providing the fix. One question: Can we consider shortening category text, while selecting a category in Question bank (Drop down)?
          Hide
          Tim Hunt added a comment -

          Well, presumably people are creating really long category names for a reason (not just testing). So shortening where not necessary needs careful consideration. I won't even thing about it unless a user asks.

          Show
          Tim Hunt added a comment - Well, presumably people are creating really long category names for a reason (not just testing). So shortening where not necessary needs careful consideration. I won't even thing about it unless a user asks.
          Hide
          Eloy Lafuente (stronk7) added a comment -

          This is now part of Moodle, many thanks!

          Show
          Eloy Lafuente (stronk7) added a comment - This is now part of Moodle, many thanks!

            People

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

              Dates

              • Created:
                Updated:
                Resolved: