Skip to content

Commit

Permalink
MDL-47322 Availability: empty availability should be saved as null
Browse files Browse the repository at this point in the history
  • Loading branch information
sammarshallou committed Sep 26, 2014
1 parent 8c85081 commit 0e290bc
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
8 changes: 8 additions & 0 deletions availability/classes/tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,14 @@ public function save() {
return $result;
}

/**
* Checks whether this tree is empty (contains no children).
* @return boolean True if empty
*/
public function is_empty() {
return count($this->children) === 0;
}

/**
* Recursively gets all children of a particular class (you can use a base
* class to get all conditions, or a specific class).
Expand Down
16 changes: 16 additions & 0 deletions availability/tests/tree_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

use core_availability\capability_checker;
use \core_availability\tree;

defined('MOODLE_INTERNAL') || die();

Expand Down Expand Up @@ -488,6 +489,21 @@ public function test_get_full_information() {
$tree->get_full_information($info));
}

/**
* Tests the is_empty() function.
*/
public function test_is_empty() {
// Tree with nothing in should be empty.
$structure = self::tree(array());
$tree = new tree($structure);
$this->assertTrue($tree->is_empty());

// Tree with something in is not empty.
$structure = self::tree(array(self::mock(array('m' => '1'))));
$tree = new tree($structure);
$this->assertFalse($tree->is_empty());
}

/**
* Tests the get_all_children() function.
*/
Expand Down
18 changes: 18 additions & 0 deletions course/modlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ function add_moduleinfo($moduleinfo, $course, $mform = null) {
} else if (property_exists($moduleinfo, 'availability')) {
$newcm->availability = $moduleinfo->availability;
}
// If there is any availability data, verify it.
if ($newcm->availability) {
$tree = new \core_availability\tree(json_decode($newcm->availability));
// Save time and database space by setting null if the only data
// is an empty tree.
if ($tree->is_empty()) {
$newcm->availability = null;
}
}
}
if (isset($moduleinfo->showdescription)) {
$newcm->showdescription = $moduleinfo->showdescription;
Expand Down Expand Up @@ -502,6 +511,15 @@ function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) {
} else if (property_exists($moduleinfo, 'availability')) {
$cm->availability = $moduleinfo->availability;
}
// If there is any availability data, verify it.
if ($cm->availability) {
$tree = new \core_availability\tree(json_decode($cm->availability));
// Save time and database space by setting null if the only data
// is an empty tree.
if ($tree->is_empty()) {
$cm->availability = null;
}
}
}
if (isset($moduleinfo->showdescription)) {
$cm->showdescription = $moduleinfo->showdescription;
Expand Down
45 changes: 42 additions & 3 deletions course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,12 @@ private function update_specific_module_test($modulename) {

// Conditional activity.
$coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
$moduleinfo->availability = '{"op":"&","showc":[true,true],"c":[' .
$moduleinfo->availability = '{"op":"&","showc":[true,true,true,true,true],"c":[' .
'{"type":"date","d":">=","t":' . time() . '},' .
'{"type":"date","d":"<","t":' . (time() + (7 * 24 * 3600)) . '}' .
'{"type":"date","d":"<","t":' . (time() + (7 * 24 * 3600)) . '},' .
'{"type":"grade","id":' . $coursegradeitem->id . ',"min":10,"max":80},' .
'{"type":"profile","sf":"email","op":"contains","v":"@"},'.
'{"type":"completion","id":'. $assigncm->id . ',"e":' . COMPLETION_COMPLETE . '}' .
'{"type":"completion","cm":'. $assigncm->id . ',"e":' . COMPLETION_COMPLETE . '}' .
']}';

// Grading and Advanced grading.
Expand Down Expand Up @@ -2497,4 +2497,43 @@ public function test_view_resources_list() {
$this->assertEventLegacyLogData($expectedlegacydata, $event);
$this->assertEventContextNotUsed($event);
}

/**
* Tests that when creating or updating a module, if the availability settings
* are present but set to an empty tree, availability is set to null in
* database.
*/
public function test_empty_availability_settings() {
global $DB;
$this->setAdminUser();
$this->resetAfterTest();

// Enable availability.
set_config('enableavailability', 1);

// Test add.
$emptyavailability = '{"op":"&","c":[],"showc":[]}';
$course = self::getDataGenerator()->create_course();
$label = self::getDataGenerator()->create_module('label', array(
'course' => $course, 'availability' => $emptyavailability));
$this->assertNull($DB->get_field('course_modules', 'availability',
array('id' => $label->cmid)));

// Test update.
$formdata = $DB->get_record('course_modules', array('id' => $label->cmid));
unset($formdata->availability);
$formdata->availabilityconditionsjson = $emptyavailability;
$formdata->modulename = 'label';
$formdata->coursemodule = $label->cmid;
$draftid = 0;
file_prepare_draft_area($draftid, context_module::instance($label->cmid)->id,
'mod_label', 'intro', 0);
$formdata->introeditor = array(
'itemid' => $draftid,
'text' => '<p>Yo</p>',
'format' => FORMAT_HTML);
update_module($formdata);
$this->assertNull($DB->get_field('course_modules', 'availability',
array('id' => $label->cmid)));
}
}

0 comments on commit 0e290bc

Please sign in to comment.