Beispiel #1
0
 function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     $mform =& $this->_form;
     // check the calculation formula
     if ($data['calculation'] != '') {
         $grade_item = grade_item::fetch(array('id' => $data['id'], 'courseid' => $data['courseid']));
         $calculation = calc_formula::unlocalize(stripslashes($data['calculation']));
         $result = $grade_item->validate_formula($calculation);
         if ($result !== true) {
             $errors['calculation'] = $result;
         }
     }
     return $errors;
 }
 function validation($data)
 {
     $errors = array();
     $mform =& $this->_form;
     // check the calculation formula
     if ($data['calculation'] != '') {
         $grade_item = grade_item::fetch(array('id' => $data['id'], 'courseid' => $data['courseid']));
         $calculation = calc_formula::unlocalize(stripslashes($data['calculation']));
         $result = $grade_item->validate_formula($calculation);
         if ($result !== true) {
             $errors['calculation'] = $result;
         }
     }
     if (0 == count($errors)) {
         return true;
     } else {
         return $errors;
     }
 }
    error('Incorect item id');
}
// activity items and items without grade can not have calculation
if ($grade_item->is_external_item() or $grade_item->gradetype != GRADE_TYPE_VALUE and $grade_item->gradetype != GRADE_TYPE_SCALE) {
    redirect($returnurl, get_string('errornocalculationallowed', 'grades'));
}
$mform = new edit_calculation_form(null, array('gpr' => $gpr, 'itemid' => $grade_item->id));
if ($mform->is_cancelled()) {
    redirect($returnurl);
}
$calculation = calc_formula::localize($grade_item->calculation);
$calculation = grade_item::denormalize_formula($calculation, $grade_item->courseid);
$mform->set_data(array('courseid' => $grade_item->courseid, 'calculation' => $calculation, 'id' => $grade_item->id, 'itemname' => $grade_item->itemname));
$errors = array();
if ($data = $mform->get_data(false)) {
    $calculation = calc_formula::unlocalize($data->calculation);
    $grade_item->set_calculation($calculation);
    redirect($returnurl);
} elseif (!empty($section) and $section = 'idnumbers' and !empty($idnumbers)) {
    // Handle idnumbers separately (non-mform)
    //first validate and store the new idnumbers
    foreach ($idnumbers as $giid => $value) {
        if ($gi = grade_item::fetch(array('id' => $giid))) {
            if ($gi->itemtype == 'mod') {
                $cm = get_coursemodule_from_instance($gi->itemmodule, $gi->iteminstance, $gi->courseid);
            } else {
                $cm = null;
            }
            if (!grade_verify_idnumber($value, $COURSE->id, $gi, $cm)) {
                $errors[$giid] = get_string('idnumbertaken');
                continue;
 /**
  * Tests that the event is fired in the correct locations in core.
  */
 public function test_event_is_triggered()
 {
     global $DB;
     // Create the items we need to test with.
     $course = $this->getDataGenerator()->create_course();
     $user = $this->getDataGenerator()->create_user();
     $this->getDataGenerator()->enrol_user($user->id, $course->id);
     $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
     $quizitemparams = array('itemtype' => 'mod', 'itemmodule' => 'quiz', 'iteminstance' => $quiz->id, 'courseid' => $course->id);
     $gradeitem = grade_item::fetch($quizitemparams);
     $courseitem = grade_item::fetch_course_item($course->id);
     // Now mark the quiz using grade_update as this is the function that modules use.
     $grade = array();
     $grade['userid'] = $user->id;
     $grade['rawgrade'] = 60;
     $sink = $this->redirectEvents();
     grade_update('mod/quiz', $course->id, 'mod', 'quiz', $quiz->id, 0, $grade);
     $events = $sink->get_events();
     $sink->close();
     // Ensure we have two user_graded events, one for the item, one for the course.
     $this->assertEquals(2, count($events));
     $this->assertInstanceOf('\\core\\event\\user_graded', $events[0]);
     $this->assertEquals($gradeitem->id, $events[0]->other['itemid']);
     $this->assertInstanceOf('\\core\\event\\user_graded', $events[1]);
     $this->assertEquals($courseitem->id, $events[1]->other['itemid']);
     // Remove the grades, force the regrading and re-fetch the item. This is needed because the item
     // will be set as needing an update when the grades are deleted.
     $gradeitem->delete_all_grades();
     grade_regrade_final_grades($course->id);
     $gradeitem = grade_item::fetch($quizitemparams);
     // Now, create a grade using grade_item::update_final_grade().
     $sink = $this->redirectEvents();
     $gradeitem->update_raw_grade($user->id, 10);
     $events = $sink->get_events();
     $sink->close();
     // Ensure we have two user_graded events, one for the item, one for the course.
     $this->assertEquals(2, count($events));
     $this->assertInstanceOf('\\core\\event\\user_graded', $events[0]);
     $this->assertEquals($gradeitem->id, $events[0]->other['itemid']);
     $this->assertInstanceOf('\\core\\event\\user_graded', $events[1]);
     $this->assertEquals($courseitem->id, $events[1]->other['itemid']);
     // Now, update this grade using grade_item::update_raw_grade().
     $sink = $this->redirectEvents();
     $gradeitem->update_raw_grade($user->id, 20);
     $events = $sink->get_events();
     $sink->close();
     // Ensure we have two user_graded events, one for the item, one for the course.
     $this->assertEquals(2, count($events));
     $this->assertInstanceOf('\\core\\event\\user_graded', $events[0]);
     $this->assertEquals($gradeitem->id, $events[0]->other['itemid']);
     $this->assertInstanceOf('\\core\\event\\user_graded', $events[1]);
     $this->assertEquals($courseitem->id, $events[1]->other['itemid']);
     // Remove the grades, force the regrading and re-fetch the item. This is needed because the item
     // will be set as needing an update when the grades are deleted.
     $gradeitem->delete_all_grades();
     grade_regrade_final_grades($course->id);
     $gradeitem = grade_item::fetch($quizitemparams);
     // Now, create a grade using grade_item::update_final_grade().
     $sink = $this->redirectEvents();
     $gradeitem->update_final_grade($user->id, 30);
     $events = $sink->get_events();
     $sink->close();
     // Ensure we have two user_graded events, one for the item, one for the course.
     $this->assertEquals(2, count($events));
     $this->assertInstanceOf('\\core\\event\\user_graded', $events[0]);
     $this->assertEquals($gradeitem->id, $events[0]->other['itemid']);
     $this->assertInstanceOf('\\core\\event\\user_graded', $events[1]);
     $this->assertEquals($courseitem->id, $events[1]->other['itemid']);
     // Now, update this grade using grade_item::update_final_grade().
     $sink = $this->redirectEvents();
     $gradeitem->update_final_grade($user->id, 40);
     $events = $sink->get_events();
     $sink->close();
     // Ensure we have two user_graded events, one for the item, one for the course.
     $this->assertEquals(2, count($events));
     $this->assertInstanceOf('\\core\\event\\user_graded', $events[0]);
     $this->assertEquals($gradeitem->id, $events[0]->other['itemid']);
     $this->assertInstanceOf('\\core\\event\\user_graded', $events[1]);
     $this->assertEquals($courseitem->id, $events[1]->other['itemid']);
     // Remove the overridden flag from the grade, this was set by grade_item::update_final_grade().
     $gradegrade = grade_grade::fetch(array('itemid' => $gradeitem->id, 'userid' => $user->id));
     $gradegrade->set_overridden(false, false);
     // Let's change the calculation to anything that won't cause an error.
     $calculation = calc_formula::unlocalize("=3");
     $gradeitem->set_calculation($calculation);
     // Now force the computation of the grade.
     $sink = $this->redirectEvents();
     grade_regrade_final_grades($course->id);
     $events = $sink->get_events();
     $sink->close();
     // Ensure we have two user_graded events, one for the item, one for the course.
     $this->assertEquals(2, count($events));
     $this->assertInstanceOf('\\core\\event\\user_graded', $events[0]);
     $this->assertEquals($gradeitem->id, $events[0]->other['itemid']);
     $this->assertInstanceOf('\\core\\event\\user_graded', $events[1]);
     $this->assertEquals($courseitem->id, $events[1]->other['itemid']);
     // Now, let's trick the gradebook, we manually update a grade, and flag the grade item as
     // needing a regrading, so we can trigger the event in grade_item::regrade_final_grades().
     $gradeitem = grade_item::fetch($quizitemparams);
     $gradeitem->set_calculation('');
     $gradegrade = grade_grade::fetch(array('itemid' => $gradeitem->id, 'userid' => $user->id));
     $gradegrade->rawgrade = 50;
     $gradegrade->update();
     $sink = $this->redirectEvents();
     grade_regrade_final_grades($course->id);
     $events = $sink->get_events();
     $sink->close();
     // Ensure we have two user_graded events, one for the item, one for the course.
     $this->assertEquals(2, count($events));
     $this->assertInstanceOf('\\core\\event\\user_graded', $events[0]);
     $this->assertEquals($gradeitem->id, $events[0]->other['itemid']);
     $this->assertInstanceOf('\\core\\event\\user_graded', $events[1]);
     $this->assertEquals($courseitem->id, $events[1]->other['itemid']);
 }
    /**
     * Tests that the event is fired in the correct locations in core.
     */
    public function test_event_is_triggered() {
        global $DB;

        // Create the items we need to test with.
        $course = $this->getDataGenerator()->create_course();
        $user = $this->getDataGenerator()->create_user();
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
        $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));

        // Now mark the quiz using grade_update as this is the function that modules use.
        $grade = array();
        $grade['userid'] = $user->id;
        $grade['rawgrade'] = 50;

        $sink = $this->redirectEvents();
        grade_update('mod/quiz', $course->id, 'mod', 'quiz', $quiz->id, 0, $grade);
        $events = $sink->get_events();
        $event = reset($events);
        $sink->close();

        // Ensure we have a user_graded event.
        $this->assertEquals(1, count($events));
        $this->assertInstanceOf('\core\event\user_graded', $event);

        // Get the grade item.
        $gradeitem = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'quiz', 'iteminstance' => $quiz->id,
            'courseid' => $course->id));

        // Let's alter the grade in the DB so when we call regrade_final_grades() it is changed and an event is called.
        $sql = "UPDATE {grade_grades}
                   SET finalgrade = '2'
                 WHERE itemid = :itemid
                   AND userid = :userid";
        $DB->execute($sql, array('itemid' => $gradeitem->id, 'userid' => $user->id));

        // Now check when we regrade this that there is a user graded event.
        $sink = $this->redirectEvents();
        $gradeitem->regrade_final_grades();
        $events = $sink->get_events();
        $event = reset($events);
        $sink->close();

        // Ensure we have a user_graded event.
        $this->assertEquals(1, count($events));
        $this->assertInstanceOf('\core\event\user_graded', $event);

        // Remove the grades.
        $gradeitem->delete_all_grades();

        // Now, create a grade using update_raw_grade().
        $sink = $this->redirectEvents();
        $gradeitem->update_raw_grade($user->id, 50);
        $events = $sink->get_events();
        $event = reset($events);
        $sink->close();

        // Ensure we have a user_graded event.
        $this->assertEquals(1, count($events));
        $this->assertInstanceOf('\core\event\user_graded', $event);

        // Now, update this grade using update_raw_grade().
        $sink = $this->redirectEvents();
        $gradeitem->update_raw_grade($user->id, 100);
        $events = $sink->get_events();
        $event = reset($events);
        $sink->close();

        $this->assertEquals(1, count($events));
        $this->assertInstanceOf('\core\event\user_graded', $event);

        // Remove the grades.
        $gradeitem->delete_all_grades();

        // Now, create a grade using update_final_grade().
        $sink = $this->redirectEvents();
        $gradeitem->update_final_grade($user->id, 50);
        $events = $sink->get_events();
        $event = reset($events);
        $sink->close();

        // Ensure we have a user_graded event.
        $this->assertEquals(1, count($events));
        $this->assertInstanceOf('\core\event\user_graded', $event);

        // Now, update this grade using update_final_grade().
        $sink = $this->redirectEvents();
        $gradeitem->update_final_grade($user->id, 100);
        $events = $sink->get_events();
        $event = reset($events);
        $sink->close();

        $this->assertEquals(1, count($events));
        $this->assertInstanceOf('\core\event\user_graded', $event);

        // Let's change the calculation to anything that won't cause an error.
        $calculation = calc_formula::unlocalize("=3");
        $gradeitem->set_calculation($calculation);

        // Let's alter the grade in the DB so when we call compute() it is changed and an event is called.
        $sql = "UPDATE {grade_grades}
                   SET finalgrade = 2, overridden = 0
                 WHERE itemid = :itemid
                   AND userid = :userid";
        $DB->execute($sql, array('itemid' => $gradeitem->id, 'userid' => $user->id));

        // Now check when we compute that there is a user graded event.
        $sink = $this->redirectEvents();
        $gradeitem->compute();
        $events = $sink->get_events();
        $event = reset($events);
        $sink->close();

        $this->assertEquals(1, count($events));
        $this->assertInstanceOf('\core\event\user_graded', $event);
    }