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

Invalid argument supplied for foreach() in /var/www/html/moodle/admin/cron.php

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 1.9.10
    • Fix Version/s: 1.9.14
    • Component/s: Administration
    • Labels:
    • Database:
      MySQL
    • Testing Instructions:
      Hide

      First you will need to upload one or more new users. There is a csv file attached to this issue that you can use. The 2 users in the file have no password so it will be generated by cron. Upload the users at Site administration > Users > Accounts > Upload users. During the upload set "new user password" to "create password if needed"

      Now run /admin/cron.php. Unfortunately the code involved is only run 20% of the time and that is random. You can either run it repeatedly or insert "$random100 = 10;" at line 236 to get it to run every time.

      In cron's output you should see the following 2 lines.

      checking for create_password
      creating passwords for new users

      If there is no error then all is well.

      Show
      First you will need to upload one or more new users. There is a csv file attached to this issue that you can use. The 2 users in the file have no password so it will be generated by cron. Upload the users at Site administration > Users > Accounts > Upload users. During the upload set "new user password" to "create password if needed" Now run /admin/cron.php. Unfortunately the code involved is only run 20% of the time and that is random. You can either run it repeatedly or insert "$random100 = 10;" at line 236 to get it to run every time. In cron's output you should see the following 2 lines. checking for create_password creating passwords for new users If there is no error then all is well.
    • Affected Branches:
      MOODLE_19_STABLE
    • Fixed Branches:
      MOODLE_19_STABLE

      Description

      In Moodle 1.9.10+ (Build: 20110119), when running the admin cron job from command line or as a cron, the error "Invalid argument supplied for foreach() in /var/www/html/moodle/admin/cron.php" is entered in the log for this line:

      foreach ($newusers as $newuserid => $newuser)

      in this section of code:

      //
      // generate new password emails for users 
      //
      mtrace('checking for create_password');
      if (count_records('user_preferences', 'name', 'create_password', 'value', '1')) {
          mtrace('creating passwords for new users');
          $newusers = get_records_sql("SELECT  u.id as id, u.email, u.firstname, 
                                              u.lastname, u.username,
                                              p.id as prefid 
                                      FROM {$CFG->prefix}user u 
                                           JOIN {$CFG->prefix}user_preferences p ON u.id=p.userid
                                      WHERE p.name='create_password' AND p.value='1' AND u.email !='' ");
       
          foreach ($newusers as $newuserid => $newuser) {  // ERROR ON THIS LINE
              $newuser->emailstop = 0; // send email regardless
              // email user                               
              if (setnew_password_and_mail($newuser)) {
                  // remove user pref
                  delete_records('user_preferences', 'id', $newuser->prefid);
              } else {
                  trigger_error("Could not create and mail new user password!");
              }
          }
      }

        Gliffy Diagrams

          Attachments

            Activity

            Hide
            salvetore Michael de Raadt added a comment -

            Thanks for reporting this.

            I've put it on our backlog and we'll try to get to it as soon as we can.

            In the meantime adding more information replication instructions, fix test instructions, a workaround or even a code solution, will help us and other users.

            Show
            salvetore Michael de Raadt added a comment - Thanks for reporting this. I've put it on our backlog and we'll try to get to it as soon as we can. In the meantime adding more information replication instructions, fix test instructions, a workaround or even a code solution, will help us and other users.
            Hide
            andyjdavis Andrew Davis added a comment -

            Heres a 1.9 fix. This bug doesn't exist in 2.0 or later due to changes in the way record sets work in Moodle.

            Show
            andyjdavis Andrew Davis added a comment - Heres a 1.9 fix. This bug doesn't exist in 2.0 or later due to changes in the way record sets work in Moodle.
            Hide
            samhemelryk Sam Hemelryk added a comment -

            Looks good to me thanks Andrew

            Show
            samhemelryk Sam Hemelryk added a comment - Looks good to me thanks Andrew
            Hide
            andyjdavis Andrew Davis added a comment -

            attaching a csv to help with testing

            Show
            andyjdavis Andrew Davis added a comment - attaching a csv to help with testing
            Hide
            andyjdavis Andrew Davis added a comment -

            Added testing instructions. Sam, you missed my missing testing instructions

            Show
            andyjdavis Andrew Davis added a comment - Added testing instructions. Sam, you missed my missing testing instructions
            Hide
            stronk7 Eloy Lafuente (stronk7) added a comment -

            The main moodle.git repository has just been updated with latest weekly modifications. You may wish to rebase your PULL branches to simplify history and avoid any possible merge conflicts. This would also make integrator's life easier next week.

            TIA and ciao

            Show
            stronk7 Eloy Lafuente (stronk7) added a comment - The main moodle.git repository has just been updated with latest weekly modifications. You may wish to rebase your PULL branches to simplify history and avoid any possible merge conflicts. This would also make integrator's life easier next week. TIA and ciao
            Hide
            stronk7 Eloy Lafuente (stronk7) added a comment -

            Integrated, thanks!

            Show
            stronk7 Eloy Lafuente (stronk7) added a comment - Integrated, thanks!
            Hide
            samhemelryk Sam Hemelryk added a comment -

            Confirmed working, thanks guys

            Show
            samhemelryk Sam Hemelryk added a comment - Confirmed working, thanks guys
            Hide
            stronk7 Eloy Lafuente (stronk7) added a comment -

            git & cvs repositories have been populated with this solution. Many thanks for your collaboration, yay!

            Closing, ciao

            Show
            stronk7 Eloy Lafuente (stronk7) added a comment - git & cvs repositories have been populated with this solution. Many thanks for your collaboration, yay! Closing, ciao

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Fix Release Date:
                  10/Oct/11