General developer forum

Automatically resetting a course

Picture of BAT BAT
Automatically resetting a course

So I want to automatically reset a course.  I know .. moosh... (I think that is what is called can do it).  However, I did not want to rely on a 3rd party program because I was worried I would not be able to install it on a live server.  After searching through the different sources I was able to come up with what I think reset_course_userdata(...) is expecting as a parameter.   However, when I plug it in I get: "Can not find data record in database table course."

I have verified my course ID is 2 and I have manually found it through mysql so I know it is in the table "mdl_course".   

1st) any ideas what I may be doing wrong.  (This is sandbox code so please excuse the mess).

2nd) Is there an easier way of resetting a course without involving human interaction?


        $START_D_A = explode('/',$START_TIME);

        $END_D_A = explode('/',$END_TIME);

        echo "The course id is ".$COURSEID;

        $data = array(

                'id' => $COURSEID,



                'reset_events' => '1',

                'reset_notes' => '1',

                'reset_comments' =>'1',


                'unenrol_users'=> array('5'=>'Student'),

                'reset_gradebook_items' => '1',

                'reset_forum_all'=> '1',

                'reset_quiz_attempts' => '1',




        reset_course_userdata($data);  //error thrown on this line

Average of ratings: -
Picture of Andreas Grabs
Re: Automatically resetting a course
Core developersParticularly helpful MoodlersPlugin developersTranslators


I don't know the rest of your code but $data must be an instance of stdClass() and you have an array.

Average of ratings: Useful (1)
Picture of BAT BAT
Re: Automatically resetting a course

Thank you very much. 

I had to do a var dump to see how the object was formed but that did the trick.  Thanks!


Average of ratings: -
Picture of Alexander Bias
Re: Automatically resetting a course
Core developersPlugin developers


are you aware of which you could use out of the box or where you could extract some code?


Average of ratings: -