user1015214
user1015214

Reputation: 3091

Programically complete course and set grades in Moodle

I am new to Moodle and I am working on an old application, here are the specs:

$version  = 2016120502.05;             
$release  = '3.2.2+ (Build: 20170407)'; 
$branch   = '32';  

My ultimate goal is to be able to create a generator class to create dummy data for some PHPUnit unit tests. (https://docs.moodle.org/dev/Writing_PHPUnit_tests) I found that there is a prebuilt one for creating courses and users. But I need to be able to also mark them complete in the course with their grades.

I was looking at this https://docs.moodle.org/dev/Gradebook_API which might be at least part of what I need. However, there are lots of tables in the system, and I'm not confident that this is what I need.

Here is my code up until this last point:

// Create user;
    $this->user = $this->getDataGenerator()->create_user();
    // Create courses.
    $courseCount = 0;
    $courses = [];
    while ($courseCount < 5) {
        $courses[] = $this->getDataGenerator()->create_course();
        $courseCount++;
    }
    /** @var \myGlobal_generator $generator */
    $generator = $this->getDataGenerator()->get_plugin_generator('myGlobal_generator');
    // Create curriculum.
    $this->curriculum = $generator->createCurriculum($courses);
    // Now we need to set a user to have completed each one 
    // of the courses and set their grades for each as well.

Upvotes: 0

Views: 1069

Answers (1)

Mitxel
Mitxel

Reputation: 453

Notice that grading and completion tracking are not necessarily related things. For example, you can mark activities and courses as completed without any grading involved, like so:

$cmassign = get_coursemodule_from_id('assign', $cmid);
$completion = new completion_info($course);
$completion->update_state($cmassign, COMPLETION_COMPLETE, $user->id);
$ccompletion = new completion_completion(['course' => $course->id, 'userid' => $user->id]);
$ccompletion->mark_complete();

If you really need to test/generate data with grading and grade-based completion you may need to code it along this lines:

  1. create the course with completion tracking enabled.
  2. create the activity (add it to the course) with grade-based completion tracking (completion tracking automatic, completion use grade to true).
  3. create the new grading item (related to the activity and a user) with the global helper grade_update.
  4. Calculate the internal completion state of the activity (for example with the public method $completion->internal_get_state )
  5. Test the state of the activity (completed pass or completed fail).

Upvotes: 1

Related Questions