DROP PROCEDURE IF EXISTS GenerateNotifications;
DELIMITER $$
CREATE PROCEDURE GenerateNotifications(IN NumRows INT)
    BEGIN
          DECLARE minUserId, maxUserId, i, createdStart, readStart, timeCreated, timeRead INT;
          SELECT MIN(id) INTO minUserId FROM mdl_user;
          SELECT MAX(id) INTO maxUserId FROM mdl_user;
          SET i = 1;
          SET createdStart = (UNIX_TIMESTAMP() - NumRows);
 		  SET readStart = (UNIX_TIMESTAMP() + 86400 - NumRows);
          WHILE i < NumRows DO
                IF i % 4 = 0 THEN
                   SET timeRead = readStart + i;
                ELSE
                   SET timeRead = NULL;
                END IF;
               SET timeCreated = createdStart + ((i DIV 10) * 10);
          		INSERT INTO mdl_notifications (useridfrom, useridto, subject, fullmessage, fullmessagehtml, smallmessage, component, eventtype, contexturl, contexturlname, timeread, timecreated, customdata)
                    VALUES (
                        FLOOR(minUserId + RAND() *  (maxUserId - minUserId)),
                        FLOOR(minUserId + RAND() *  (maxUserId - minUserId)),
                        CONCAT('Message subject ', i),
                        CONCAT('Full message ', i),
                        CONCAT('<p>Full message</p> ', i),
                        CONCAT('small message', i),
                        'core',
                       'notification',
                      'http://example.com',
                      'example',
                  	  timeRead,
                  	  timeCreated,
                  	  '{"foo":"bar"}'
                  );
                 SET i = i + 1;
          END WHILE;
    END$$
DELIMITER ;

CALL GenerateNotifications(4000000);
