MDL-67613 availability_completion: replacing old arrays

This commit is contained in:
Ferran Recio 2020-04-17 18:03:12 +02:00
parent c54b183c8d
commit 1db6424ceb
3 changed files with 165 additions and 126 deletions

View file

@ -56,13 +56,13 @@ class condition extends \core_availability\condition {
protected $expectedcompletion; protected $expectedcompletion;
/** @var array Array of previous cmids used to calculate relative completions */ /** @var array Array of previous cmids used to calculate relative completions */
protected $modfastprevious = array(); protected $modfastprevious = [];
/** @var array Array of cmids previous to each course section */ /** @var array Array of cmids previous to each course section */
protected $sectionfastprevious = array(); protected $sectionfastprevious = [];
/** @var array Array of modules used in these conditions for course */ /** @var array Array of modules used in these conditions for course */
protected static $modsusedincondition = array(); protected static $modsusedincondition = [];
/** /**
* Constructor. * Constructor.
@ -79,8 +79,8 @@ class condition extends \core_availability\condition {
} }
// Get expected completion. // Get expected completion.
if (isset($structure->e) && in_array($structure->e, if (isset($structure->e) && in_array($structure->e,
array(COMPLETION_COMPLETE, COMPLETION_INCOMPLETE, [COMPLETION_COMPLETE, COMPLETION_INCOMPLETE,
COMPLETION_COMPLETE_PASS, COMPLETION_COMPLETE_FAIL))) { COMPLETION_COMPLETE_PASS, COMPLETION_COMPLETE_FAIL])) {
$this->expectedcompletion = $structure->e; $this->expectedcompletion = $structure->e;
} else { } else {
throw new \coding_exception('Missing or invalid ->e for completion condition'); throw new \coding_exception('Missing or invalid ->e for completion condition');
@ -93,8 +93,11 @@ class condition extends \core_availability\condition {
* @return stdClass Structure object (ready to be made into JSON format) * @return stdClass Structure object (ready to be made into JSON format)
*/ */
public function save(): stdClass { public function save(): stdClass {
return (object)array('type' => 'completion', return (object) [
'cm' => $this->cmid, 'e' => $this->expectedcompletion); 'type' => 'completion',
'cm' => $this->cmid,
'e' => $this->expectedcompletion,
];
} }
/** /**
@ -108,8 +111,11 @@ class condition extends \core_availability\condition {
* @return stdClass Object representing condition * @return stdClass Object representing condition
*/ */
public static function get_json(int $cmid, int $expectedcompletion): stdClass { public static function get_json(int $cmid, int $expectedcompletion): stdClass {
return (object)array('type' => 'completion', 'cm' => (int)$cmid, return (object) [
'e' => (int)$expectedcompletion); 'type' => 'completion',
'cm' => (int)$cmid,
'e' => (int)$expectedcompletion,
];
} }
/** /**
@ -138,7 +144,7 @@ class condition extends \core_availability\condition {
$allow = false; $allow = false;
} else { } else {
// The completion system caches its own data so no caching needed here. // The completion system caches its own data so no caching needed here.
$completiondata = $completion->get_data((object)array('id' => $cmid), $completiondata = $completion->get_data((object)['id' => $cmid],
$grabthelot, $userid, $modinfo); $grabthelot, $userid, $modinfo);
$allow = true; $allow = true;
@ -180,19 +186,19 @@ class condition extends \core_availability\condition {
if ($info instanceof info_module) { if ($info instanceof info_module) {
$cminfo = $info->get_course_module(); $cminfo = $info->get_course_module();
if (!empty($cminfo->id)) { if (!empty($cminfo->id)) {
$this->selfids = array($cminfo->id, null); $this->selfids = [$cminfo->id, null];
return $this->selfids; return $this->selfids;
} }
} }
if ($info instanceof info_section) { if ($info instanceof info_section) {
$section = $info->get_section(); $section = $info->get_section();
if (!empty($section->id)) { if (!empty($section->id)) {
$this->selfids = array(null, $section->id); $this->selfids = [null, $section->id];
return $this->selfids; return $this->selfids;
} }
} }
return array(null, null); return [null, null];
} }
/** /**
@ -258,8 +264,8 @@ class condition extends \core_availability\condition {
} }
if (empty($this->modfastprevious)) { if (empty($this->modfastprevious)) {
$this->modfastprevious = array(); $this->modfastprevious = [];
$sectionprevious = array(); $sectionprevious = [];
$modinfo = get_fast_modinfo($course); $modinfo = get_fast_modinfo($course);
$lastcmid = 0; $lastcmid = 0;
@ -427,7 +433,7 @@ class condition extends \core_availability\condition {
// If we are on the same course (e.g. duplicate) then we can just // If we are on the same course (e.g. duplicate) then we can just
// use the existing one. // use the existing one.
if ($DB->record_exists('course_modules', if ($DB->record_exists('course_modules',
array('id' => $this->cmid, 'course' => $courseid))) { ['id' => $this->cmid, 'course' => $courseid])) {
return $res; return $res;
} }
// Otherwise it's a warning. // Otherwise it's a warning.
@ -455,7 +461,7 @@ class condition extends \core_availability\condition {
if (!array_key_exists($course->id, self::$modsusedincondition)) { if (!array_key_exists($course->id, self::$modsusedincondition)) {
// We don't have data for this course, build it. // We don't have data for this course, build it.
$modinfo = get_fast_modinfo($course); $modinfo = get_fast_modinfo($course);
self::$modsusedincondition[$course->id] = array(); self::$modsusedincondition[$course->id] = [];
// Activities. // Activities.
foreach ($modinfo->cms as $othercm) { foreach ($modinfo->cms as $othercm) {
@ -494,7 +500,7 @@ class condition extends \core_availability\condition {
* Wipes the static cache of modules used in a condition (for unit testing). * Wipes the static cache of modules used in a condition (for unit testing).
*/ */
public static function wipe_static_cache() { public static function wipe_static_cache() {
self::$modsusedincondition = array(); self::$modsusedincondition = [];
} }
public function update_dependency_id($table, $oldid, $newid) { public function update_dependency_id($table, $oldid, $newid) {

View file

@ -37,7 +37,7 @@ class frontend extends \core_availability\frontend {
/** /**
* @var array Cached init parameters * @var array Cached init parameters
*/ */
protected $cacheparams = array(); protected $cacheparams = [];
/** /**
* @var string IDs of course, cm, and section for cache (if any) * @var string IDs of course, cm, and section for cache (if any)
@ -45,8 +45,8 @@ class frontend extends \core_availability\frontend {
protected $cachekey = ''; protected $cachekey = '';
protected function get_javascript_strings() { protected function get_javascript_strings() {
return array('option_complete', 'option_fail', 'option_incomplete', 'option_pass', return ['option_complete', 'option_fail', 'option_incomplete', 'option_pass',
'label_cm', 'label_completion'); 'label_cm', 'label_completion'];
} }
protected function get_javascript_init_params($course, \cm_info $cm = null, protected function get_javascript_init_params($course, \cm_info $cm = null,
@ -59,29 +59,29 @@ class frontend extends \core_availability\frontend {
// Get list of activities on course which have completion values, // Get list of activities on course which have completion values,
// to fill the dropdown. // to fill the dropdown.
$context = \context_course::instance($course->id); $context = \context_course::instance($course->id);
$cms = array(); $cms = [];
$modinfo = get_fast_modinfo($course); $modinfo = get_fast_modinfo($course);
$previouscm = false; $previouscm = false;
foreach ($modinfo->cms as $id => $othercm) { foreach ($modinfo->cms as $id => $othercm) {
// Add each course-module if it has completion turned on and is not // Add each course-module if it has completion turned on and is not
// the one currently being edited. // the one currently being edited.
if ($othercm->completion && (empty($cm) || $cm->id != $id) && !$othercm->deletioninprogress) { if ($othercm->completion && (empty($cm) || $cm->id != $id) && !$othercm->deletioninprogress) {
$cms[] = (object)array('id' => $id, $cms[] = (object)['id' => $id,
'name' => format_string($othercm->name, true, array('context' => $context)), 'name' => format_string($othercm->name, true, ['context' => $context]),
'completiongradeitemnumber' => $othercm->completiongradeitemnumber); 'completiongradeitemnumber' => $othercm->completiongradeitemnumber];
} }
if (count($cms) && (empty($cm) || $cm->id == $id)) { if (count($cms) && (empty($cm) || $cm->id == $id)) {
$previouscm = true; $previouscm = true;
} }
} }
if ($previouscm) { if ($previouscm) {
$previous = (object)array('id' => \availability_completion\condition::OPTION_PREVIOUS, $previous = (object)['id' => \availability_completion\condition::OPTION_PREVIOUS,
'name' => get_string('option_previous', 'availability_completion'), 'name' => get_string('option_previous', 'availability_completion'),
'completiongradeitemnumber' => \availability_completion\condition::OPTION_PREVIOUS); 'completiongradeitemnumber' => \availability_completion\condition::OPTION_PREVIOUS];
array_unshift($cms, $previous); array_unshift($cms, $previous);
} }
$this->cachekey = $cachekey; $this->cachekey = $cachekey;
$this->cacheinitparams = array($cms); $this->cacheinitparams = [$cms];
} }
return $this->cacheinitparams; return $this->cacheinitparams;
} }

View file

@ -69,19 +69,27 @@ class availability_completion_condition_testcase extends advanced_testcase {
$CFG->enablecompletion = true; $CFG->enablecompletion = true;
$CFG->enableavailability = true; $CFG->enableavailability = true;
$generator = $this->getDataGenerator(); $generator = $this->getDataGenerator();
$course = $generator->create_course(array('enablecompletion' => 1)); $course = $generator->create_course(['enablecompletion' => 1]);
$page = $generator->get_plugin_generator('mod_page')->create_instance( $page = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL)); ['course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL]);
$selfpage = $generator->get_plugin_generator('mod_page')->create_instance( $selfpage = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL)); ['course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL]);
$modinfo = get_fast_modinfo($course); $modinfo = get_fast_modinfo($course);
$cm = $modinfo->get_cm($page->cmid); $cm = $modinfo->get_cm($page->cmid);
$info = new \core_availability\mock_info($course, $USER->id); $info = new \core_availability\mock_info($course, $USER->id);
$structure = (object)array('op' => '|', 'show' => true, 'c' => array( $structure = (object)[
(object)array('type' => 'completion', 'cm' => (int)$cm->id, 'op' => '|',
'e' => COMPLETION_COMPLETE))); 'show' => true,
'c' => [
(object)[
'type' => 'completion',
'cm' => (int)$cm->id,
'e' => COMPLETION_COMPLETE
]
]
];
$tree = new \core_availability\tree($structure); $tree = new \core_availability\tree($structure);
// Initial check (user has not completed activity). // Initial check (user has not completed activity).
@ -166,7 +174,7 @@ class availability_completion_condition_testcase extends advanced_testcase {
* Tests the save() function. * Tests the save() function.
*/ */
public function test_save() { public function test_save() {
$structure = (object)array('cm' => 42, 'e' => COMPLETION_COMPLETE); $structure = (object)['cm' => 42, 'e' => COMPLETION_COMPLETE];
$cond = new condition($structure); $cond = new condition($structure);
$structure->type = 'completion'; $structure->type = 'completion';
$this->assertEquals($structure, $cond->save()); $this->assertEquals($structure, $cond->save());
@ -184,24 +192,24 @@ class availability_completion_condition_testcase extends advanced_testcase {
$CFG->enablecompletion = true; $CFG->enablecompletion = true;
$CFG->enableavailability = true; $CFG->enableavailability = true;
$generator = $this->getDataGenerator(); $generator = $this->getDataGenerator();
$course = $generator->create_course(array('enablecompletion' => 1)); $course = $generator->create_course(['enablecompletion' => 1]);
$user = $generator->create_user(); $user = $generator->create_user();
$generator->enrol_user($user->id, $course->id); $generator->enrol_user($user->id, $course->id);
$this->setUser($user); $this->setUser($user);
// Create a Page with manual completion for basic checks. // Create a Page with manual completion for basic checks.
$page = $generator->get_plugin_generator('mod_page')->create_instance( $page = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course->id, 'name' => 'Page!', ['course' => $course->id, 'name' => 'Page!',
'completion' => COMPLETION_TRACKING_MANUAL)); 'completion' => COMPLETION_TRACKING_MANUAL]);
// Create an assignment - we need to have something that can be graded // Create an assignment - we need to have something that can be graded
// so as to test the PASS/FAIL states. Set it up to be completed based // so as to test the PASS/FAIL states. Set it up to be completed based
// on its grade item. // on its grade item.
$assignrow = $this->getDataGenerator()->create_module('assign', array( $assignrow = $this->getDataGenerator()->create_module('assign', [
'course' => $course->id, 'name' => 'Assign!', 'course' => $course->id, 'name' => 'Assign!',
'completion' => COMPLETION_TRACKING_AUTOMATIC)); 'completion' => COMPLETION_TRACKING_AUTOMATIC]);
$DB->set_field('course_modules', 'completiongradeitemnumber', 0, $DB->set_field('course_modules', 'completiongradeitemnumber', 0,
array('id' => $assignrow->cmid)); ['id' => $assignrow->cmid]);
$assign = new assign(context_module::instance($assignrow->cmid), false, false); $assign = new assign(context_module::instance($assignrow->cmid), false, false);
// Get basic details. // Get basic details.
@ -211,8 +219,9 @@ class availability_completion_condition_testcase extends advanced_testcase {
$info = new \core_availability\mock_info($course, $user->id); $info = new \core_availability\mock_info($course, $user->id);
// COMPLETE state (false), positive and NOT. // COMPLETE state (false), positive and NOT.
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$pagecm->id, 'e' => COMPLETION_COMPLETE)); 'cm' => (int)$pagecm->id, 'e' => COMPLETION_COMPLETE
]);
$this->assertFalse($cond->is_available(false, $info, true, $user->id)); $this->assertFalse($cond->is_available(false, $info, true, $user->id));
$information = $cond->get_description(false, false, $info); $information = $cond->get_description(false, false, $info);
$information = \core_availability\info::format_info($information, $course); $information = \core_availability\info::format_info($information, $course);
@ -220,8 +229,9 @@ class availability_completion_condition_testcase extends advanced_testcase {
$this->assertTrue($cond->is_available(true, $info, true, $user->id)); $this->assertTrue($cond->is_available(true, $info, true, $user->id));
// INCOMPLETE state (true). // INCOMPLETE state (true).
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$pagecm->id, 'e' => COMPLETION_INCOMPLETE)); 'cm' => (int)$pagecm->id, 'e' => COMPLETION_INCOMPLETE
]);
$this->assertTrue($cond->is_available(false, $info, true, $user->id)); $this->assertTrue($cond->is_available(false, $info, true, $user->id));
$this->assertFalse($cond->is_available(true, $info, true, $user->id)); $this->assertFalse($cond->is_available(true, $info, true, $user->id));
$information = $cond->get_description(false, true, $info); $information = $cond->get_description(false, true, $info);
@ -233,8 +243,9 @@ class availability_completion_condition_testcase extends advanced_testcase {
$completion->update_state($pagecm, COMPLETION_COMPLETE); $completion->update_state($pagecm, COMPLETION_COMPLETE);
// COMPLETE state (true). // COMPLETE state (true).
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$pagecm->id, 'e' => COMPLETION_COMPLETE)); 'cm' => (int)$pagecm->id, 'e' => COMPLETION_COMPLETE
]);
$this->assertTrue($cond->is_available(false, $info, true, $user->id)); $this->assertTrue($cond->is_available(false, $info, true, $user->id));
$this->assertFalse($cond->is_available(true, $info, true, $user->id)); $this->assertFalse($cond->is_available(true, $info, true, $user->id));
$information = $cond->get_description(false, true, $info); $information = $cond->get_description(false, true, $info);
@ -242,8 +253,9 @@ class availability_completion_condition_testcase extends advanced_testcase {
$this->assertRegExp('~Page!.*is incomplete~', $information); $this->assertRegExp('~Page!.*is incomplete~', $information);
// INCOMPLETE state (false). // INCOMPLETE state (false).
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$pagecm->id, 'e' => COMPLETION_INCOMPLETE)); 'cm' => (int)$pagecm->id, 'e' => COMPLETION_INCOMPLETE
]);
$this->assertFalse($cond->is_available(false, $info, true, $user->id)); $this->assertFalse($cond->is_available(false, $info, true, $user->id));
$information = $cond->get_description(false, false, $info); $information = $cond->get_description(false, false, $info);
$information = \core_availability\info::format_info($information, $course); $information = \core_availability\info::format_info($information, $course);
@ -253,32 +265,36 @@ class availability_completion_condition_testcase extends advanced_testcase {
// We are going to need the grade item so that we can get pass/fails. // We are going to need the grade item so that we can get pass/fails.
$gradeitem = $assign->get_grade_item(); $gradeitem = $assign->get_grade_item();
grade_object::set_properties($gradeitem, array('gradepass' => 50.0)); grade_object::set_properties($gradeitem, ['gradepass' => 50.0]);
$gradeitem->update(); $gradeitem->update();
// With no grade, it should return true for INCOMPLETE and false for // With no grade, it should return true for INCOMPLETE and false for
// the other three. // the other three.
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$assigncm->id, 'e' => COMPLETION_INCOMPLETE)); 'cm' => (int)$assigncm->id, 'e' => COMPLETION_INCOMPLETE
]);
$this->assertTrue($cond->is_available(false, $info, true, $user->id)); $this->assertTrue($cond->is_available(false, $info, true, $user->id));
$this->assertFalse($cond->is_available(true, $info, true, $user->id)); $this->assertFalse($cond->is_available(true, $info, true, $user->id));
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE)); 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE
]);
$this->assertFalse($cond->is_available(false, $info, true, $user->id)); $this->assertFalse($cond->is_available(false, $info, true, $user->id));
$this->assertTrue($cond->is_available(true, $info, true, $user->id)); $this->assertTrue($cond->is_available(true, $info, true, $user->id));
// Check $information for COMPLETE_PASS and _FAIL as we haven't yet. // Check $information for COMPLETE_PASS and _FAIL as we haven't yet.
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_PASS)); 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_PASS
]);
$this->assertFalse($cond->is_available(false, $info, true, $user->id)); $this->assertFalse($cond->is_available(false, $info, true, $user->id));
$information = $cond->get_description(false, false, $info); $information = $cond->get_description(false, false, $info);
$information = \core_availability\info::format_info($information, $course); $information = \core_availability\info::format_info($information, $course);
$this->assertRegExp('~Assign!.*is complete and passed~', $information); $this->assertRegExp('~Assign!.*is complete and passed~', $information);
$this->assertTrue($cond->is_available(true, $info, true, $user->id)); $this->assertTrue($cond->is_available(true, $info, true, $user->id));
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_FAIL)); 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_FAIL
]);
$this->assertFalse($cond->is_available(false, $info, true, $user->id)); $this->assertFalse($cond->is_available(false, $info, true, $user->id));
$information = $cond->get_description(false, false, $info); $information = $cond->get_description(false, false, $info);
$information = \core_availability\info::format_info($information, $course); $information = \core_availability\info::format_info($information, $course);
@ -288,26 +304,30 @@ class availability_completion_condition_testcase extends advanced_testcase {
// Change the grade to be complete and failed. // Change the grade to be complete and failed.
self::set_grade($assignrow, $user->id, 40); self::set_grade($assignrow, $user->id, 40);
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$assigncm->id, 'e' => COMPLETION_INCOMPLETE)); 'cm' => (int)$assigncm->id, 'e' => COMPLETION_INCOMPLETE
]);
$this->assertFalse($cond->is_available(false, $info, true, $user->id)); $this->assertFalse($cond->is_available(false, $info, true, $user->id));
$this->assertTrue($cond->is_available(true, $info, true, $user->id)); $this->assertTrue($cond->is_available(true, $info, true, $user->id));
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE)); 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE
]);
$this->assertTrue($cond->is_available(false, $info, true, $user->id)); $this->assertTrue($cond->is_available(false, $info, true, $user->id));
$this->assertFalse($cond->is_available(true, $info, true, $user->id)); $this->assertFalse($cond->is_available(true, $info, true, $user->id));
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_PASS)); 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_PASS
]);
$this->assertFalse($cond->is_available(false, $info, true, $user->id)); $this->assertFalse($cond->is_available(false, $info, true, $user->id));
$information = $cond->get_description(false, false, $info); $information = $cond->get_description(false, false, $info);
$information = \core_availability\info::format_info($information, $course); $information = \core_availability\info::format_info($information, $course);
$this->assertRegExp('~Assign!.*is complete and passed~', $information); $this->assertRegExp('~Assign!.*is complete and passed~', $information);
$this->assertTrue($cond->is_available(true, $info, true, $user->id)); $this->assertTrue($cond->is_available(true, $info, true, $user->id));
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_FAIL)); 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_FAIL
]);
$this->assertTrue($cond->is_available(false, $info, true, $user->id)); $this->assertTrue($cond->is_available(false, $info, true, $user->id));
$this->assertFalse($cond->is_available(true, $info, true, $user->id)); $this->assertFalse($cond->is_available(true, $info, true, $user->id));
$information = $cond->get_description(false, true, $info); $information = $cond->get_description(false, true, $info);
@ -317,26 +337,30 @@ class availability_completion_condition_testcase extends advanced_testcase {
// Now change it to pass. // Now change it to pass.
self::set_grade($assignrow, $user->id, 60); self::set_grade($assignrow, $user->id, 60);
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$assigncm->id, 'e' => COMPLETION_INCOMPLETE)); 'cm' => (int)$assigncm->id, 'e' => COMPLETION_INCOMPLETE
]);
$this->assertFalse($cond->is_available(false, $info, true, $user->id)); $this->assertFalse($cond->is_available(false, $info, true, $user->id));
$this->assertTrue($cond->is_available(true, $info, true, $user->id)); $this->assertTrue($cond->is_available(true, $info, true, $user->id));
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE)); 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE
]);
$this->assertTrue($cond->is_available(false, $info, true, $user->id)); $this->assertTrue($cond->is_available(false, $info, true, $user->id));
$this->assertFalse($cond->is_available(true, $info, true, $user->id)); $this->assertFalse($cond->is_available(true, $info, true, $user->id));
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_PASS)); 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_PASS
]);
$this->assertTrue($cond->is_available(false, $info, true, $user->id)); $this->assertTrue($cond->is_available(false, $info, true, $user->id));
$this->assertFalse($cond->is_available(true, $info, true, $user->id)); $this->assertFalse($cond->is_available(true, $info, true, $user->id));
$information = $cond->get_description(false, true, $info); $information = $cond->get_description(false, true, $info);
$information = \core_availability\info::format_info($information, $course); $information = \core_availability\info::format_info($information, $course);
$this->assertRegExp('~Assign!.*is not complete and passed~', $information); $this->assertRegExp('~Assign!.*is not complete and passed~', $information);
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_FAIL)); 'cm' => (int)$assigncm->id, 'e' => COMPLETION_COMPLETE_FAIL
]);
$this->assertFalse($cond->is_available(false, $info, true, $user->id)); $this->assertFalse($cond->is_available(false, $info, true, $user->id));
$information = $cond->get_description(false, false, $info); $information = $cond->get_description(false, false, $info);
$information = \core_availability\info::format_info($information, $course); $information = \core_availability\info::format_info($information, $course);
@ -345,15 +369,17 @@ class availability_completion_condition_testcase extends advanced_testcase {
// Simulate deletion of an activity by using an invalid cmid. These // Simulate deletion of an activity by using an invalid cmid. These
// conditions always fail, regardless of NOT flag or INCOMPLETE. // conditions always fail, regardless of NOT flag or INCOMPLETE.
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => ($assigncm->id + 100), 'e' => COMPLETION_COMPLETE)); 'cm' => ($assigncm->id + 100), 'e' => COMPLETION_COMPLETE
]);
$this->assertFalse($cond->is_available(false, $info, true, $user->id)); $this->assertFalse($cond->is_available(false, $info, true, $user->id));
$information = $cond->get_description(false, false, $info); $information = $cond->get_description(false, false, $info);
$information = \core_availability\info::format_info($information, $course); $information = \core_availability\info::format_info($information, $course);
$this->assertRegExp('~(Missing activity).*is marked complete~', $information); $this->assertRegExp('~(Missing activity).*is marked complete~', $information);
$this->assertFalse($cond->is_available(true, $info, true, $user->id)); $this->assertFalse($cond->is_available(true, $info, true, $user->id));
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => ($assigncm->id + 100), 'e' => COMPLETION_INCOMPLETE)); 'cm' => ($assigncm->id + 100), 'e' => COMPLETION_INCOMPLETE
]);
$this->assertFalse($cond->is_available(false, $info, true, $user->id)); $this->assertFalse($cond->is_available(false, $info, true, $user->id));
} }
@ -379,39 +405,40 @@ class availability_completion_condition_testcase extends advanced_testcase {
$CFG->enablecompletion = true; $CFG->enablecompletion = true;
$CFG->enableavailability = true; $CFG->enableavailability = true;
$generator = $this->getDataGenerator(); $generator = $this->getDataGenerator();
$course = $generator->create_course(array('enablecompletion' => 1)); $course = $generator->create_course(['enablecompletion' => 1]);
$user = $generator->create_user(); $user = $generator->create_user();
$generator->enrol_user($user->id, $course->id); $generator->enrol_user($user->id, $course->id);
$this->setUser($user); $this->setUser($user);
// Page 1 (manual completion). // Page 1 (manual completion).
$page1 = $generator->get_plugin_generator('mod_page')->create_instance( $page1 = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course->id, 'name' => 'Page1!', ['course' => $course->id, 'name' => 'Page1!',
'completion' => COMPLETION_TRACKING_MANUAL)); 'completion' => COMPLETION_TRACKING_MANUAL]);
// Page 2 (manual completion). // Page 2 (manual completion).
$page2 = $generator->get_plugin_generator('mod_page')->create_instance( $page2 = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course->id, 'name' => 'Page2!', ['course' => $course->id, 'name' => 'Page2!',
'completion' => COMPLETION_TRACKING_MANUAL)); 'completion' => COMPLETION_TRACKING_MANUAL]);
// Page ignored (no completion). // Page ignored (no completion).
$pagenocompletion = $generator->get_plugin_generator('mod_page')->create_instance( $pagenocompletion = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course->id, 'name' => 'Page ignored!')); ['course' => $course->id, 'name' => 'Page ignored!']);
// Create an assignment - we need to have something that can be graded // Create an assignment - we need to have something that can be graded
// so as to test the PASS/FAIL states. Set it up to be completed based // so as to test the PASS/FAIL states. Set it up to be completed based
// on its grade item. // on its grade item.
$assignrow = $this->getDataGenerator()->create_module('assign', array( $assignrow = $this->getDataGenerator()->create_module('assign', [
'course' => $course->id, 'name' => 'Assign!', 'course' => $course->id, 'name' => 'Assign!',
'completion' => COMPLETION_TRACKING_AUTOMATIC)); 'completion' => COMPLETION_TRACKING_AUTOMATIC
]);
$DB->set_field('course_modules', 'completiongradeitemnumber', 0, $DB->set_field('course_modules', 'completiongradeitemnumber', 0,
array('id' => $assignrow->cmid)); ['id' => $assignrow->cmid]);
$assign = new assign(context_module::instance($assignrow->cmid), false, false); $assign = new assign(context_module::instance($assignrow->cmid), false, false);
// Page 3 (manual completion). // Page 3 (manual completion).
$page3 = $generator->get_plugin_generator('mod_page')->create_instance( $page3 = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course->id, 'name' => 'Page3!', ['course' => $course->id, 'name' => 'Page3!',
'completion' => COMPLETION_TRACKING_MANUAL)); 'completion' => COMPLETION_TRACKING_MANUAL]);
// Get basic details. // Get basic details.
$activities = []; $activities = [];
@ -425,7 +452,7 @@ class availability_completion_condition_testcase extends advanced_testcase {
// Setup gradings and completion. // Setup gradings and completion.
if ($grade) { if ($grade) {
$gradeitem = $assign->get_grade_item(); $gradeitem = $assign->get_grade_item();
grade_object::set_properties($gradeitem, array('gradepass' => 50.0)); grade_object::set_properties($gradeitem, ['gradepass' => 50.0]);
$gradeitem->update(); $gradeitem->update();
self::set_grade($assignrow, $user->id, $grade); self::set_grade($assignrow, $user->id, $grade);
} }
@ -436,8 +463,9 @@ class availability_completion_condition_testcase extends advanced_testcase {
// Set opprevious WITH non existent previous activity. // Set opprevious WITH non existent previous activity.
$info = new \core_availability\mock_info_module($user->id, $activities[$activity]); $info = new \core_availability\mock_info_module($user->id, $activities[$activity]);
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$prevvalue, 'e' => $condition)); 'cm' => (int)$prevvalue, 'e' => $condition
]);
// Do the checks. // Do the checks.
$this->assertEquals($result, $cond->is_available(false, $info, true, $user->id)); $this->assertEquals($result, $cond->is_available(false, $info, true, $user->id));
@ -558,29 +586,29 @@ class availability_completion_condition_testcase extends advanced_testcase {
$CFG->enableavailability = true; $CFG->enableavailability = true;
$generator = $this->getDataGenerator(); $generator = $this->getDataGenerator();
$course = $generator->create_course( $course = $generator->create_course(
array('numsections' => 4, 'enablecompletion' => 1), ['numsections' => 4, 'enablecompletion' => 1],
array('createsections' => true)); ['createsections' => true]);
$user = $generator->create_user(); $user = $generator->create_user();
$generator->enrol_user($user->id, $course->id); $generator->enrol_user($user->id, $course->id);
$this->setUser($user); $this->setUser($user);
// Section 1 - page1 (manual completion). // Section 1 - page1 (manual completion).
$page1 = $generator->get_plugin_generator('mod_page')->create_instance( $page1 = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course->id, 'name' => 'Page1!', 'section' => 1, ['course' => $course->id, 'name' => 'Page1!', 'section' => 1,
'completion' => COMPLETION_TRACKING_MANUAL)); 'completion' => COMPLETION_TRACKING_MANUAL]);
// Section 1 - page ignored 1 (no completion). // Section 1 - page ignored 1 (no completion).
$pagenocompletion1 = $generator->get_plugin_generator('mod_page')->create_instance( $pagenocompletion1 = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course, 'name' => 'Page ignored!', 'section' => 1)); ['course' => $course, 'name' => 'Page ignored!', 'section' => 1]);
// Section 2 - page ignored 2 (no completion). // Section 2 - page ignored 2 (no completion).
$pagenocompletion2 = $generator->get_plugin_generator('mod_page')->create_instance( $pagenocompletion2 = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course, 'name' => 'Page ignored!', 'section' => 2)); ['course' => $course, 'name' => 'Page ignored!', 'section' => 2]);
// Section 3 - page2 (manual completion). // Section 3 - page2 (manual completion).
$page2 = $generator->get_plugin_generator('mod_page')->create_instance( $page2 = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course->id, 'name' => 'Page2!', 'section' => 3, ['course' => $course->id, 'name' => 'Page2!', 'section' => 3,
'completion' => COMPLETION_TRACKING_MANUAL)); 'completion' => COMPLETION_TRACKING_MANUAL]);
// Section 4 is empty. // Section 4 is empty.
@ -601,8 +629,9 @@ class availability_completion_condition_testcase extends advanced_testcase {
} }
$info = new \core_availability\mock_info_section($user->id, $sections[$section]); $info = new \core_availability\mock_info_section($user->id, $sections[$section]);
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => (int)$prevvalue, 'e' => $condition)); 'cm' => (int)$prevvalue, 'e' => $condition
]);
$this->assertEquals($result, $cond->is_available(false, $info, true, $user->id)); $this->assertEquals($result, $cond->is_available(false, $info, true, $user->id));
$this->assertEquals($resultnot, $cond->is_available(true, $info, true, $user->id)); $this->assertEquals($resultnot, $cond->is_available(true, $info, true, $user->id));
$information = $cond->get_description(false, false, $info); $information = $cond->get_description(false, false, $info);
@ -675,41 +704,41 @@ class availability_completion_condition_testcase extends advanced_testcase {
$CFG->enableavailability = true; $CFG->enableavailability = true;
$generator = $this->getDataGenerator(); $generator = $this->getDataGenerator();
$course = $generator->create_course( $course = $generator->create_course(
array('numsections' => 1, 'enablecompletion' => 1), ['numsections' => 1, 'enablecompletion' => 1],
array('createsections' => true)); ['createsections' => true]);
// Create six pages with manual completion. // Create six pages with manual completion.
$page1 = $generator->get_plugin_generator('mod_page')->create_instance( $page1 = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL)); ['course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL]);
$page2 = $generator->get_plugin_generator('mod_page')->create_instance( $page2 = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL)); ['course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL]);
$page3 = $generator->get_plugin_generator('mod_page')->create_instance( $page3 = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL)); ['course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL]);
$page4 = $generator->get_plugin_generator('mod_page')->create_instance( $page4 = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL)); ['course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL]);
$page5 = $generator->get_plugin_generator('mod_page')->create_instance( $page5 = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL)); ['course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL]);
$page6 = $generator->get_plugin_generator('mod_page')->create_instance( $page6 = $generator->get_plugin_generator('mod_page')->create_instance(
array('course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL)); ['course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL]);
// Set up page3 to depend on page1, and section1 to depend on page2. // Set up page3 to depend on page1, and section1 to depend on page2.
$DB->set_field('course_modules', 'availability', $DB->set_field('course_modules', 'availability',
'{"op":"|","show":true,"c":[' . '{"op":"|","show":true,"c":[' .
'{"type":"completion","e":1,"cm":' . $page1->cmid . '}]}', '{"type":"completion","e":1,"cm":' . $page1->cmid . '}]}',
array('id' => $page3->cmid)); ['id' => $page3->cmid]);
$DB->set_field('course_sections', 'availability', $DB->set_field('course_sections', 'availability',
'{"op":"|","show":true,"c":[' . '{"op":"|","show":true,"c":[' .
'{"type":"completion","e":1,"cm":' . $page2->cmid . '}]}', '{"type":"completion","e":1,"cm":' . $page2->cmid . '}]}',
array('course' => $course->id, 'section' => 1)); ['course' => $course->id, 'section' => 1]);
// Set up page5 and page6 to depend on previous activity. // Set up page5 and page6 to depend on previous activity.
$DB->set_field('course_modules', 'availability', $DB->set_field('course_modules', 'availability',
'{"op":"|","show":true,"c":[' . '{"op":"|","show":true,"c":[' .
'{"type":"completion","e":1,"cm":' . $prevvalue . '}]}', '{"type":"completion","e":1,"cm":' . $prevvalue . '}]}',
array('id' => $page5->cmid)); ['id' => $page5->cmid]);
$DB->set_field('course_modules', 'availability', $DB->set_field('course_modules', 'availability',
'{"op":"|","show":true,"c":[' . '{"op":"|","show":true,"c":[' .
'{"type":"completion","e":1,"cm":' . $prevvalue . '}]}', '{"type":"completion","e":1,"cm":' . $prevvalue . '}]}',
array('id' => $page6->cmid)); ['id' => $page6->cmid]);
// Check 1: nothing depends on page3 and page6 but something does on the others. // Check 1: nothing depends on page3 and page6 but something does on the others.
$this->assertTrue(availability_completion\condition::completion_value_used( $this->assertTrue(availability_completion\condition::completion_value_used(
@ -734,9 +763,10 @@ class availability_completion_condition_testcase extends advanced_testcase {
* @param float $grade Grade * @param float $grade Grade
*/ */
protected static function set_grade($assignrow, $userid, $grade) { protected static function set_grade($assignrow, $userid, $grade) {
$grades = array(); $grades = [];
$grades[$userid] = (object)array( $grades[$userid] = (object)[
'rawgrade' => $grade, 'userid' => $userid); 'rawgrade' => $grade, 'userid' => $userid
];
$assignrow->cmidnumber = null; $assignrow->cmidnumber = null;
assign_grade_item_update($assignrow, $grades); assign_grade_item_update($assignrow, $grades);
} }
@ -745,8 +775,9 @@ class availability_completion_condition_testcase extends advanced_testcase {
* Tests the update_dependency_id() function. * Tests the update_dependency_id() function.
*/ */
public function test_update_dependency_id() { public function test_update_dependency_id() {
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => 42, 'e' => COMPLETION_COMPLETE, 'selfid' => 43)); 'cm' => 42, 'e' => COMPLETION_COMPLETE, 'selfid' => 43
]);
$this->assertFalse($cond->update_dependency_id('frogs', 42, 540)); $this->assertFalse($cond->update_dependency_id('frogs', 42, 540));
$this->assertFalse($cond->update_dependency_id('course_modules', 12, 34)); $this->assertFalse($cond->update_dependency_id('course_modules', 12, 34));
$this->assertTrue($cond->update_dependency_id('course_modules', 42, 456)); $this->assertTrue($cond->update_dependency_id('course_modules', 42, 456));
@ -754,17 +785,19 @@ class availability_completion_condition_testcase extends advanced_testcase {
$this->assertEquals(456, $after->cm); $this->assertEquals(456, $after->cm);
// Test selfid updating. // Test selfid updating.
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => 42, 'e' => COMPLETION_COMPLETE)); 'cm' => 42, 'e' => COMPLETION_COMPLETE
]);
$this->assertFalse($cond->update_dependency_id('frogs', 43, 540)); $this->assertFalse($cond->update_dependency_id('frogs', 43, 540));
$this->assertFalse($cond->update_dependency_id('course_modules', 12, 34)); $this->assertFalse($cond->update_dependency_id('course_modules', 12, 34));
$after = $cond->save(); $after = $cond->save();
$this->assertEquals(42, $after->cm); $this->assertEquals(42, $after->cm);
// Test on previous activity. // Test on previous activity.
$cond = new condition((object)array( $cond = new condition((object)[
'cm' => condition::OPTION_PREVIOUS, 'cm' => condition::OPTION_PREVIOUS,
'e' => COMPLETION_COMPLETE)); 'e' => COMPLETION_COMPLETE
]);
$this->assertFalse($cond->update_dependency_id('frogs', 43, 80)); $this->assertFalse($cond->update_dependency_id('frogs', 43, 80));
$this->assertFalse($cond->update_dependency_id('course_modules', 12, 34)); $this->assertFalse($cond->update_dependency_id('course_modules', 12, 34));
$after = $cond->save(); $after = $cond->save();