mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 05:58:34 +01:00
MDL-44070 Conditional availability enhancements (5): deprecate old lib
The old conditionlib.php, which is replaced by the new availability API, has been deprecated. * Code which uses the key functions (is_available, get_full_information) should still work. These now show deprecated warnings and then pass through to equivalents in the new API. * I have created new unit tests for these functions. * The old language file has been removed (reused strings already moved with AMOS). * Most other functions throw exceptions because it was impossible (due to fundamental API differences) or difficult to reimplement. * I don't really expect that non-core code (outside unit tests) will have used any of the other functions.
This commit is contained in:
parent
38674ef91a
commit
6a601097a0
@ -1,103 +0,0 @@
|
||||
<?php
|
||||
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Strings for component 'condition', language 'en', branch 'MOODLE_20_STABLE'
|
||||
*
|
||||
* @package condition
|
||||
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
$string['addcompletions'] = 'Add {no} activity conditions to form';
|
||||
$string['addgrades'] = 'Add {no} grade conditions to form';
|
||||
$string['adduserfields'] = 'Add {no} user field conditions to form';
|
||||
$string['availabilityconditions'] = 'Restrict access';
|
||||
$string['availablefrom'] = 'Allow access from';
|
||||
$string['availablefrom_help'] = 'Access from/to dates determine when students can access the activity via a link on the course page.
|
||||
|
||||
The difference between access from/to dates and availability settings for the activity is that outside the set dates, access from/to prevents access completely, while availability allows students to view the activity description.';
|
||||
$string['availableuntil'] = 'Allow access until';
|
||||
$string['badavailabledates'] = 'Invalid dates. If you set both dates, the \'Allow access from\' date should be before the \'until\' date.';
|
||||
$string['badgradelimits'] = 'If you set both an upper and lower grade limit, the upper limit must be higher than the lower limit.';
|
||||
$string['completion_complete'] = 'must be marked complete';
|
||||
$string['completioncondition'] = 'Activity completion condition';
|
||||
$string['completioncondition_help'] = 'This setting determines any activity completion conditions which must be met in order to access the activity. Note that completion tracking must first be set before an activity completion condition can be set.
|
||||
|
||||
Multiple activity completion conditions may be set if desired. If so, access to the activity will only be permitted when ALL activity completion conditions are met.';
|
||||
$string['completionconditionsection'] = 'Activity completion condition';
|
||||
$string['completionconditionsection_help'] = 'This setting determines any activity completion conditions which must be met in order to access the section. Note that completion tracking must first be set before an activity completion condition can be set.
|
||||
|
||||
Multiple activity completion conditions may be set if desired. If so, access to the section will only be permitted when ALL activity completion conditions are met.';
|
||||
$string['completion_fail'] = 'must be complete with fail grade';
|
||||
$string['completion_incomplete'] = 'must not be marked complete';
|
||||
$string['completion_pass'] = 'must be complete with pass grade';
|
||||
$string['configenableavailability'] = 'When enabled, this lets you set conditions (based on date, grade, or completion) that control whether an activity or resource can be accessed.';
|
||||
$string['contains'] = 'contains';
|
||||
$string['doesnotcontain'] = 'doesn\'t contain';
|
||||
$string['enableavailability'] = 'Enable conditional access';
|
||||
$string['endswith'] = 'ends with';
|
||||
$string['fielddeclaredmultipletimes'] = 'You can not declare the same field more than once per activity.';
|
||||
$string['grade_atleast'] = 'must be at least';
|
||||
$string['gradecondition'] = 'Grade condition';
|
||||
$string['gradecondition_help'] = 'This setting determines any grade conditions which must be met in order to access the activity.
|
||||
|
||||
Multiple grade conditions may be set if desired. If so, the activity will only allow access when ALL grade conditions are met.';
|
||||
$string['gradeconditionsection'] = 'Grade condition';
|
||||
$string['gradeconditionsection_help'] = 'This setting determines any grade conditions which must be met in order to access the section.
|
||||
|
||||
Multiple grade conditions may be set if desired. If so, the section will only allow access when ALL grade conditions are met.';
|
||||
$string['grade_upto'] = 'and less than';
|
||||
$string['gradeitembutnolimits'] = 'You must enter an upper or lower limit, or both.';
|
||||
$string['gradelimitsbutnoitem'] = 'You must choose a grade item.';
|
||||
$string['gradesmustbenumeric'] = 'The minimum and maximum grades must be numeric (or blank).';
|
||||
$string['isempty'] = 'is empty';
|
||||
$string['isequalto'] = 'is equal to';
|
||||
$string['isnotempty'] = 'is not empty';
|
||||
$string['none'] = '(none)';
|
||||
$string['notavailableyet'] = 'Not available yet';
|
||||
$string['requires_completion_0'] = 'Not available unless the activity <strong>{$a}</strong> is incomplete.';
|
||||
$string['requires_completion_1'] = 'Not available until the activity <strong>{$a}</strong> is marked complete.';
|
||||
$string['requires_completion_2'] = 'Not available until the activity <strong>{$a}</strong> is complete and passed.';
|
||||
$string['requires_completion_3'] = 'Not available unless the activity <strong>{$a}</strong> is complete and failed.';
|
||||
$string['requires_date'] = 'Available from {$a}.';
|
||||
$string['requires_date_before'] = 'Available until {$a}.';
|
||||
$string['requires_date_both'] = 'Available from {$a->from} to {$a->until}.';
|
||||
$string['requires_date_both_single_day'] = 'Available on {$a}.';
|
||||
$string['requires_grade_any'] = 'Not available until you have a grade in <strong>{$a}</strong>.';
|
||||
$string['requires_grade_max'] = 'Not available unless you get an appropriate score in <strong>{$a}</strong>.';
|
||||
$string['requires_grade_min'] = 'Not available until you achieve a required score in <strong>{$a}</strong>.';
|
||||
$string['requires_grade_range'] = 'Not available unless you get a particular score in <strong>{$a}</strong>.';
|
||||
$string['requires_user_field_contains'] = 'Not available unless your <strong>{$a->field}</strong> contains <strong>{$a->value}</strong>.';
|
||||
$string['requires_user_field_doesnotcontain'] = 'Not available if your <strong>{$a->field}</strong> contains <strong>{$a->value}</strong>.';
|
||||
$string['requires_user_field_endswith'] = 'Not available unless your <strong>{$a->field}</strong> ends with <strong>{$a->value}</strong>.';
|
||||
$string['requires_user_field_isempty'] = 'Not available unless your <strong>{$a->field}</strong> is empty.';
|
||||
$string['requires_user_field_isequalto'] = 'Not available unless your <strong>{$a->field}</strong> is equal to <strong>{$a->value}</strong>.';
|
||||
$string['requires_user_field_isnotempty'] = 'Not available if your <strong>{$a->field}</strong> is empty.';
|
||||
$string['requires_user_field_startswith'] = 'Not available unless your <strong>{$a->field}</strong> starts withs <strong>{$a->value}</strong>.';
|
||||
$string['showavailability'] = 'While access is prevented';
|
||||
$string['showavailabilitysection'] = 'Before section can be accessed';
|
||||
$string['showavailability_hide'] = 'Hide activity entirely in the course and gradebook';
|
||||
$string['showavailability_show'] = 'Show activity greyed-out, with restriction information';
|
||||
$string['showavailabilitysection_hide'] = 'Hide section entirely';
|
||||
$string['showavailabilitysection_show'] = 'Show section greyed-out, with restriction information';
|
||||
$string['startswith'] = 'starts with';
|
||||
$string['userfield'] = 'User field';
|
||||
$string['userfield_help'] = 'You can restrict access based on any field from the users profile.';
|
||||
$string['userrestriction_hidden'] = 'Restricted (completely hidden, no message): {$a}';
|
||||
$string['userrestriction_visible'] = 'Restricted: {$a}';
|
||||
$string['groupingnoaccess'] = 'You do not currently belong to a group which has access to this section. ';
|
@ -321,7 +321,6 @@ $cache = '.var_export($cache, true).';
|
||||
'cache' => $CFG->dirroot.'/cache',
|
||||
'calendar' => $CFG->dirroot.'/calendar',
|
||||
'cohort' => $CFG->dirroot.'/cohort',
|
||||
'condition' => null,
|
||||
'completion' => null,
|
||||
'countries' => null,
|
||||
'course' => $CFG->dirroot.'/course',
|
||||
|
1363
lib/conditionlib.php
1363
lib/conditionlib.php
File diff suppressed because it is too large
Load Diff
@ -15,13 +15,11 @@
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Tests for conditional activities.
|
||||
* Tests for deprecated conditional activities classes.
|
||||
*
|
||||
* @package core
|
||||
* @category phpunit
|
||||
* @copyright © 2008 The Open University
|
||||
* @author Sam Marshall
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
||||
* @package core_availability
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
@ -30,6 +28,13 @@ global $CFG;
|
||||
require_once($CFG->dirroot . '/lib/conditionlib.php');
|
||||
|
||||
|
||||
/**
|
||||
* Tests for deprecated conditional activities classes.
|
||||
*
|
||||
* @package core_availability
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
||||
*/
|
||||
class core_conditionlib_testcase extends advanced_testcase {
|
||||
|
||||
protected function setUp() {
|
||||
@ -44,761 +49,166 @@ class core_conditionlib_testcase extends advanced_testcase {
|
||||
$this->setUser($user);
|
||||
}
|
||||
|
||||
protected function wipe_condition_cache() {
|
||||
cache::make('core', 'gradecondition')->purge();
|
||||
}
|
||||
|
||||
public function test_constructor() {
|
||||
global $DB;
|
||||
$cm = new stdClass;
|
||||
|
||||
// Test records.
|
||||
$id = $DB->insert_record('course_modules', (object)array(
|
||||
'showavailability'=>1, 'availablefrom'=>17, 'availableuntil'=>398, 'course'=>64));
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course();
|
||||
$page = $generator->get_plugin_generator('mod_page')->create_instance(
|
||||
array('course' => $course));
|
||||
$modinfo = get_fast_modinfo($course);
|
||||
|
||||
// No ID.
|
||||
try {
|
||||
$test = new condition_info($cm);
|
||||
$test = new condition_info((object)array());
|
||||
$this->fail();
|
||||
} catch (coding_exception $e) {
|
||||
// Do nothing.
|
||||
$this->assertDebuggingCalled();
|
||||
}
|
||||
|
||||
// Get actual cm_info for comparison.
|
||||
$realcm = $modinfo->get_cm($page->cmid);
|
||||
|
||||
// No other data.
|
||||
$cm->id = $id;
|
||||
$test = new condition_info($cm, CONDITION_MISSING_EVERYTHING);
|
||||
$this->assertEquals(
|
||||
(object)array('id'=>$id, 'showavailability'=>1,
|
||||
'availablefrom'=>17, 'availableuntil'=>398, 'course'=>64,
|
||||
'conditionsgrade'=>array(), 'conditionscompletion'=>array(),
|
||||
'visible' => 1, 'conditionsfield' => array()),
|
||||
$test->get_full_course_module());
|
||||
$test = new condition_info((object)array('id' => $page->cmid));
|
||||
$this->assertDebuggingCalled();
|
||||
$this->assertEquals($realcm, $test->get_full_course_module());
|
||||
$this->assertDebuggingCalled();
|
||||
|
||||
// Just the course_modules stuff, check it doesn't request that from db.
|
||||
$cm->showavailability = 0;
|
||||
$cm->availablefrom = 2;
|
||||
$cm->availableuntil = 74;
|
||||
$cm->course = 38;
|
||||
$cm->visible = 1;
|
||||
$test = new condition_info($cm, CONDITION_MISSING_EXTRATABLE);
|
||||
$this->assertEquals(
|
||||
(object)array('id'=>$id, 'showavailability'=>0,
|
||||
'availablefrom'=>2, 'availableuntil'=>74, 'course'=>38,
|
||||
'conditionsgrade' => array(), 'conditionscompletion' => array(),
|
||||
'visible' => 1, 'conditionsfield' => array()),
|
||||
$test->get_full_course_module());
|
||||
// Course id.
|
||||
$test = new condition_info((object)array('id' => $page->cmid, 'course' => $course->id));
|
||||
$this->assertDebuggingCalled();
|
||||
$this->assertEquals($realcm, $test->get_full_course_module());
|
||||
$this->assertDebuggingCalled();
|
||||
|
||||
// Now let's add some actual grade/completion conditions.
|
||||
$DB->insert_record('course_modules_availability', (object)array(
|
||||
'coursemoduleid'=>$id,
|
||||
'sourcecmid'=>42,
|
||||
'requiredcompletion'=>2
|
||||
));
|
||||
$DB->insert_record('course_modules_availability', (object)array(
|
||||
'coursemoduleid'=>$id,
|
||||
'sourcecmid'=>666,
|
||||
'requiredcompletion'=>1
|
||||
));
|
||||
$DB->insert_record('course_modules_availability', (object)array(
|
||||
'coursemoduleid'=>$id,
|
||||
'gradeitemid'=>37,
|
||||
'grademin'=>5.5
|
||||
));
|
||||
|
||||
$cm = (object)array('id'=>$id);
|
||||
$test = new condition_info($cm, CONDITION_MISSING_EVERYTHING);
|
||||
$fullcm = $test->get_full_course_module();
|
||||
$this->assertEquals(array(42=>2, 666=>1), $fullcm->conditionscompletion);
|
||||
$this->assertEquals(array(37=>(object)array('min'=>5.5, 'max'=>null, 'name'=>'!missing')),
|
||||
$fullcm->conditionsgrade);
|
||||
// Full cm.
|
||||
$test = new condition_info($realcm);
|
||||
$this->assertDebuggingCalled();
|
||||
$this->assertEquals($realcm, $test->get_full_course_module());
|
||||
$this->assertDebuggingCalled();
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as above test but for course_sections instead of course_modules.
|
||||
*/
|
||||
public function test_section_constructor() {
|
||||
global $DB, $CFG;
|
||||
|
||||
// Test records.
|
||||
$id = $DB->insert_record('course_sections', (object)array(
|
||||
'showavailability' => 1, 'availablefrom' => 17,
|
||||
'availableuntil' => 398, 'course' => 64, 'groupingid' => 13));
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course(
|
||||
array('numsections' => 1), array('createsections' => true));
|
||||
$modinfo = get_fast_modinfo($course);
|
||||
|
||||
// No ID.
|
||||
$section = new stdClass;
|
||||
try {
|
||||
$test = new condition_info_section($section);
|
||||
$test = new condition_info_section(((object)array()));
|
||||
$this->fail();
|
||||
} catch (coding_exception $e) {
|
||||
// Do nothing.
|
||||
$this->assertDebuggingCalled();
|
||||
}
|
||||
|
||||
// Get actual cm_info for comparison.
|
||||
$realsection = $modinfo->get_section_info(1);
|
||||
|
||||
// No other data.
|
||||
$section->id = $id;
|
||||
$test = new condition_info_section($section, CONDITION_MISSING_EVERYTHING);
|
||||
$this->assertEquals(
|
||||
(object)array('id' => $id, 'showavailability' => 1, 'groupingid' => 13,
|
||||
'availablefrom' => 17, 'availableuntil' => 398, 'course' => 64,
|
||||
'conditionsgrade' => array(), 'conditionscompletion' => array(),
|
||||
'visible' => 1, 'conditionsfield' => array()),
|
||||
$test->get_full_section());
|
||||
|
||||
// Just the course_sections stuff; check it doesn't request that from db
|
||||
// (by using fake values and ensuring it still has those).
|
||||
$section->showavailability = 0;
|
||||
$section->availablefrom = 2;
|
||||
$section->availableuntil = 74;
|
||||
$section->course = 38;
|
||||
$section->groupingid = 99;
|
||||
$section->visible = 1;
|
||||
$test = new condition_info_section($section, CONDITION_MISSING_EXTRATABLE);
|
||||
$this->assertEquals(
|
||||
(object)array('id' => $id, 'showavailability' => 0, 'groupingid' => 99,
|
||||
'availablefrom' => 2, 'availableuntil' => 74, 'course' => 38,
|
||||
'conditionsgrade' => array(), 'conditionscompletion' => array(),
|
||||
'visible' => 1, 'conditionsfield' => array()),
|
||||
$test->get_full_section());
|
||||
|
||||
// Now let's add some actual grade/completion conditions.
|
||||
$DB->insert_record('course_sections_availability', (object)array(
|
||||
'coursesectionid' => $id,
|
||||
'sourcecmid' => 42,
|
||||
'requiredcompletion' => 2
|
||||
));
|
||||
$DB->insert_record('course_sections_availability', (object)array(
|
||||
'coursesectionid' => $id,
|
||||
'sourcecmid' => 666,
|
||||
'requiredcompletion' => 1
|
||||
));
|
||||
$DB->insert_record('course_sections_availability', (object)array(
|
||||
'coursesectionid' => $id,
|
||||
'gradeitemid' => 37,
|
||||
'grademin' => 5.5
|
||||
));
|
||||
|
||||
$section = (object)array('id' => $id);
|
||||
$test = new condition_info_section($section, CONDITION_MISSING_EVERYTHING);
|
||||
$fullsection = $test->get_full_section();
|
||||
$this->assertEquals(array(42 => 2, 666 => 1), $fullsection->conditionscompletion);
|
||||
$this->assertEquals(array(37 => (object)array('min' => 5.5, 'max' => null, 'name' => '!missing')),
|
||||
$fullsection->conditionsgrade);
|
||||
}
|
||||
|
||||
private function make_course() {
|
||||
$category = $this->getDataGenerator()->create_category(array('name' => 'conditionlibtest'));
|
||||
$course = $this->getDataGenerator()->create_course(
|
||||
array('fullname' => 'Condition test',
|
||||
'shortname' => 'CT1',
|
||||
'category' => $category->id,
|
||||
'enablecompletion' => 1));
|
||||
context_course::instance($course->id);
|
||||
return $course->id;
|
||||
}
|
||||
|
||||
private function make_course_module($courseid, $params = array()) {
|
||||
global $DB;
|
||||
|
||||
$moduleid = $DB->get_field('modules', 'id', array('name'=>'resource'));
|
||||
|
||||
$rid = $DB->insert_record('resource', (object)array('course'=>$courseid,
|
||||
'name'=>'xxx', 'alltext'=>'', 'popup'=>''));
|
||||
$settings = (object)array(
|
||||
'course'=>$courseid, 'module'=>$moduleid, 'instance'=>$rid);
|
||||
foreach ($params as $name => $value) {
|
||||
$settings->{$name} = $value;
|
||||
}
|
||||
$cmid = $DB->insert_record('course_modules', $settings);
|
||||
rebuild_course_cache($courseid, true);
|
||||
return $cmid;
|
||||
}
|
||||
|
||||
private function make_section($courseid, $cmids, $sectionnum = 0, $params = array()) {
|
||||
global $DB;
|
||||
$record = (object)array(
|
||||
'course' => $courseid,
|
||||
'sequence' => implode(',', $cmids),
|
||||
'section' => $sectionnum);
|
||||
foreach ($params as $name => $value) {
|
||||
$record->{$name} = $value;
|
||||
}
|
||||
$sectionid = $DB->insert_record('course_sections', $record);
|
||||
foreach ($cmids as $cmid) {
|
||||
$DB->update_record('course_modules', array('section' => $sectionid, 'id' => $cmid));
|
||||
}
|
||||
rebuild_course_cache($courseid, true);
|
||||
return $sectionid;
|
||||
}
|
||||
|
||||
private function make_grouping($courseid, $name) {
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/group/lib.php');
|
||||
return groups_create_grouping((object)array('courseid' => $courseid,
|
||||
'name' => $name));
|
||||
}
|
||||
|
||||
private function make_group($courseid, $name, $groupingid = 0) {
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/group/lib.php');
|
||||
$groupid = groups_create_group((object)array('courseid' => $courseid,
|
||||
'name' => $name));
|
||||
if ($groupingid) {
|
||||
groups_assign_grouping($groupingid, $groupid);
|
||||
}
|
||||
return $groupid;
|
||||
}
|
||||
|
||||
public function test_modinfo() {
|
||||
global $DB;
|
||||
|
||||
// Let's make a course.
|
||||
$courseid = $this->make_course();
|
||||
|
||||
// Now let's make a couple modules on that course.
|
||||
$cmid1 = $this->make_course_module($courseid, array(
|
||||
'showavailability'=>1, 'availablefrom'=>17, 'availableuntil'=>398,
|
||||
'completion'=>COMPLETION_TRACKING_MANUAL));
|
||||
$cmid2 = $this->make_course_module($courseid, array(
|
||||
'showavailability'=>0, 'availablefrom'=>0, 'availableuntil'=>0));
|
||||
$this->make_section($courseid, array($cmid1, $cmid2), 1);
|
||||
|
||||
// Add a fake grade item.
|
||||
$gradeitemid = $DB->insert_record('grade_items', (object)array(
|
||||
'courseid'=>$courseid, 'itemname'=>'frog'));
|
||||
|
||||
// One of the modules has grade and completion conditions, other doesn't.
|
||||
$DB->insert_record('course_modules_availability', (object)array(
|
||||
'coursemoduleid'=>$cmid2,
|
||||
'sourcecmid'=>$cmid1,
|
||||
'requiredcompletion'=>1
|
||||
));
|
||||
$DB->insert_record('course_modules_availability', (object)array(
|
||||
'coursemoduleid'=>$cmid2,
|
||||
'gradeitemid'=>$gradeitemid,
|
||||
'grademin'=>5.5
|
||||
));
|
||||
|
||||
// Okay sweet, now get modinfo.
|
||||
$modinfo = get_fast_modinfo($courseid);
|
||||
|
||||
// Test basic data.
|
||||
$this->assertEquals(1, $modinfo->cms[$cmid1]->showavailability);
|
||||
$this->assertEquals(17, $modinfo->cms[$cmid1]->availablefrom);
|
||||
$this->assertEquals(398, $modinfo->cms[$cmid1]->availableuntil);
|
||||
$this->assertEquals(0, $modinfo->cms[$cmid2]->showavailability);
|
||||
$this->assertEquals(0, $modinfo->cms[$cmid2]->availablefrom);
|
||||
$this->assertEquals(0, $modinfo->cms[$cmid2]->availableuntil);
|
||||
|
||||
// Test condition arrays.
|
||||
$this->assertEquals(array(), $modinfo->cms[$cmid1]->conditionscompletion);
|
||||
$this->assertEquals(array(), $modinfo->cms[$cmid1]->conditionsgrade);
|
||||
$this->assertEquals(array($cmid1=>1),
|
||||
$modinfo->cms[$cmid2]->conditionscompletion);
|
||||
$this->assertEquals(array($gradeitemid=>(object)array('min'=>5.5, 'max'=>null, 'name'=>'frog')),
|
||||
$modinfo->cms[$cmid2]->conditionsgrade);
|
||||
}
|
||||
|
||||
public function test_section_modinfo() {
|
||||
global $DB;
|
||||
|
||||
// Let's make a course.
|
||||
$courseid = $this->make_course();
|
||||
|
||||
// Now let's make a couple sections on that course, one of which has a cm.
|
||||
$cmid = $this->make_course_module($courseid);
|
||||
$sectionid1 = $this->make_section($courseid, array($cmid), 1, array(
|
||||
'showavailability' => 1, 'availablefrom' => 17,
|
||||
'availableuntil' => 398, 'groupingid' => 13));
|
||||
$sectionid2 = $this->make_section($courseid, array(), 2);
|
||||
|
||||
// Add a fake grade item.
|
||||
$gradeitemid = $DB->insert_record('grade_items', (object)array(
|
||||
'courseid' => $courseid, 'itemname' => 'frog'));
|
||||
|
||||
// One of the sections has grade and completion conditions, other doesn't.
|
||||
$DB->insert_record('course_sections_availability', (object)array(
|
||||
'coursesectionid' => $sectionid2,
|
||||
'sourcecmid' => $cmid,
|
||||
'requiredcompletion'=>1
|
||||
));
|
||||
$DB->insert_record('course_sections_availability', (object)array(
|
||||
'coursesectionid' => $sectionid2,
|
||||
'gradeitemid' => $gradeitemid,
|
||||
'grademin' => 5.5
|
||||
));
|
||||
|
||||
rebuild_course_cache($courseid, true);
|
||||
// Okay sweet, now get modinfo.
|
||||
$modinfo = get_fast_modinfo($courseid);
|
||||
|
||||
// Test basic data.
|
||||
$section1 = $modinfo->get_section_info(1);
|
||||
$this->assertEquals(1, $section1->showavailability);
|
||||
$this->assertEquals(17, $section1->availablefrom);
|
||||
$this->assertEquals(398, $section1->availableuntil);
|
||||
$this->assertEquals(13, $section1->groupingid);
|
||||
$section2 = $modinfo->get_section_info(2);
|
||||
$this->assertEquals(0, $section2->showavailability);
|
||||
$this->assertEquals(0, $section2->availablefrom);
|
||||
$this->assertEquals(0, $section2->availableuntil);
|
||||
$this->assertEquals(0, $section2->groupingid);
|
||||
|
||||
// Test condition arrays.
|
||||
$this->assertEquals(array(), $section1->conditionscompletion);
|
||||
$this->assertEquals(array(), $section1->conditionsgrade);
|
||||
$this->assertEquals(array($cmid => 1),
|
||||
$section2->conditionscompletion);
|
||||
$this->assertEquals(array($gradeitemid => (object)array('min' => 5.5, 'max' => null, 'name' => 'frog')),
|
||||
$section2->conditionsgrade);
|
||||
}
|
||||
|
||||
public function test_add_and_remove() {
|
||||
global $DB;
|
||||
// Make course and module.
|
||||
$courseid = $this->make_course();
|
||||
$cmid = $this->make_course_module($courseid, array(
|
||||
'showavailability'=>0, 'availablefrom'=>0, 'availableuntil'=>0));
|
||||
$this->make_section($courseid, array($cmid), 1);
|
||||
|
||||
// Check it has no conditions.
|
||||
$test1 = new condition_info((object)array('id'=>$cmid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$cm = $test1->get_full_course_module();
|
||||
$this->assertEquals(array(), $cm->conditionscompletion);
|
||||
$this->assertEquals(array(), $cm->conditionsgrade);
|
||||
|
||||
// Add conditions of each type.
|
||||
$test1->add_completion_condition(13, 3);
|
||||
$this->assertEquals(array(13=>3), $cm->conditionscompletion);
|
||||
$test1->add_grade_condition(666, 0.4, null, true);
|
||||
$this->assertEquals(array(666=>(object)array('min'=>0.4, 'max'=>null, 'name'=>'!missing')),
|
||||
$cm->conditionsgrade);
|
||||
|
||||
// Check they were really added in db.
|
||||
$test2 = new condition_info((object)array('id'=>$cmid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$cm = $test2->get_full_course_module();
|
||||
$this->assertEquals(array(13=>3), $cm->conditionscompletion);
|
||||
$this->assertEquals(array(666=>(object)array('min'=>0.4, 'max'=>null, 'name'=>'!missing')),
|
||||
$cm->conditionsgrade);
|
||||
|
||||
// Wipe conditions.
|
||||
$test2->wipe_conditions();
|
||||
$this->assertEquals(array(), $cm->conditionscompletion);
|
||||
$this->assertEquals(array(), $cm->conditionsgrade);
|
||||
|
||||
// Check they were really wiped.
|
||||
$test3 = new condition_info((object)array('id'=>$cmid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$cm = $test3->get_full_course_module();
|
||||
$this->assertEquals(array(), $cm->conditionscompletion);
|
||||
$this->assertEquals(array(), $cm->conditionsgrade);
|
||||
}
|
||||
|
||||
public function test_section_add_and_remove() {
|
||||
global $DB;
|
||||
|
||||
// Make course and module.
|
||||
$courseid = $this->make_course();
|
||||
$cmid = $this->make_course_module($courseid);
|
||||
$sectionid = $this->make_section($courseid, array($cmid), 1);
|
||||
|
||||
// Check it has no conditions.
|
||||
$test1 = new condition_info_section((object)array('id'=>$sectionid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$section = $test1->get_full_section();
|
||||
$this->assertEquals(array(), $section->conditionscompletion);
|
||||
$this->assertEquals(array(), $section->conditionsgrade);
|
||||
|
||||
// Add conditions of each type.
|
||||
$test1->add_completion_condition(13, 3);
|
||||
$this->assertEquals(array(13 => 3), $section->conditionscompletion);
|
||||
$test1->add_grade_condition(666, 0.4, null, true);
|
||||
$this->assertEquals(array(666 => (object)array('min' => 0.4, 'max' => null, 'name' => '!missing')),
|
||||
$section->conditionsgrade);
|
||||
|
||||
// Check they were really added in db.
|
||||
$test2 = new condition_info_section((object)array('id' => $sectionid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$section = $test2->get_full_section();
|
||||
$this->assertEquals(array(13 => 3), $section->conditionscompletion);
|
||||
$this->assertEquals(array(666 => (object)array('min' => 0.4, 'max' => null, 'name' => '!missing')),
|
||||
$section->conditionsgrade);
|
||||
|
||||
// Wipe conditions.
|
||||
$test2->wipe_conditions();
|
||||
$this->assertEquals(array(), $section->conditionscompletion);
|
||||
$this->assertEquals(array(), $section->conditionsgrade);
|
||||
|
||||
// Check they were really wiped.
|
||||
$test3 = new condition_info_section((object)array('id' => $cmid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$section = $test3->get_full_section();
|
||||
$this->assertEquals(array(), $section->conditionscompletion);
|
||||
$this->assertEquals(array(), $section->conditionsgrade);
|
||||
}
|
||||
|
||||
public function test_is_available() {
|
||||
global $DB, $USER;
|
||||
$courseid = $this->make_course();
|
||||
|
||||
// No conditions.
|
||||
$cmid = $this->make_course_module($courseid);
|
||||
$ci = new condition_info((object)array('id'=>$cmid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$this->assertTrue($ci->is_available($text, false, 0));
|
||||
$this->assertEquals('', $text);
|
||||
|
||||
// Time (from).
|
||||
$time = time()+100;
|
||||
$cmid = $this->make_course_module($courseid, array('availablefrom'=>$time));
|
||||
$ci = new condition_info((object)array('id'=>$cmid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
$this->assertRegExp('/'.preg_quote(userdate($time, get_string('strftimedate', 'langconfig'))).'/', $text);
|
||||
|
||||
$time = time()-100;
|
||||
$cmid = $this->make_course_module($courseid, array('availablefrom'=>$time));
|
||||
$ci = new condition_info((object)array('id'=>$cmid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$this->assertTrue($ci->is_available($text));
|
||||
$this->assertEquals('', $text);
|
||||
$this->assertRegExp('/'.preg_quote(userdate($time, get_string('strftimedate', 'langconfig'))).'/', $ci->get_full_information());
|
||||
|
||||
// Time (until).
|
||||
$cmid = $this->make_course_module($courseid, array('availableuntil'=>time()-100));
|
||||
$ci = new condition_info((object)array('id'=>$cmid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
$this->assertEquals('', $text);
|
||||
|
||||
// Completion.
|
||||
$oldid = $cmid;
|
||||
$cmid = $this->make_course_module($courseid);
|
||||
$this->make_section($courseid, array($oldid, $cmid), 1);
|
||||
$oldcm = $DB->get_record('course_modules', array('id'=>$oldid));
|
||||
$oldcm->completion = COMPLETION_TRACKING_MANUAL;
|
||||
$DB->update_record('course_modules', $oldcm);
|
||||
|
||||
// Need to reset modinfo after changing the options.
|
||||
rebuild_course_cache($courseid);
|
||||
|
||||
$ci = new condition_info((object)array('id'=>$cmid), CONDITION_MISSING_EVERYTHING);
|
||||
$ci->add_completion_condition($oldid, COMPLETION_COMPLETE);
|
||||
$this->wipe_condition_cache();
|
||||
|
||||
$this->assertFalse($ci->is_available($text, false));
|
||||
$this->assertEquals(get_string('requires_completion_1', 'condition', 'xxx'), $text);
|
||||
completion_info::wipe_session_cache();
|
||||
$completion = new completion_info($DB->get_record('course', array('id'=>$courseid)));
|
||||
$completion->update_state($oldcm, COMPLETION_COMPLETE);
|
||||
completion_info::wipe_session_cache();
|
||||
$this->wipe_condition_cache();
|
||||
|
||||
$this->assertTrue($ci->is_available($text));
|
||||
$this->assertFalse($ci->is_available($text, false, $USER->id+1));
|
||||
completion_info::wipe_session_cache();
|
||||
$this->wipe_condition_cache();
|
||||
$completion = new completion_info($DB->get_record('course', array('id'=>$courseid)));
|
||||
$completion->update_state($oldcm, COMPLETION_INCOMPLETE);
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
|
||||
$ci->wipe_conditions();
|
||||
$ci->add_completion_condition($oldid, COMPLETION_INCOMPLETE);
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertTrue($ci->is_available($text));
|
||||
$this->assertTrue($ci->is_available($text, false, $USER->id+1));
|
||||
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertTrue($ci->is_available($text, true));
|
||||
|
||||
// Grade.
|
||||
$ci->wipe_conditions();
|
||||
// Add a fake grade item.
|
||||
$gradeitemid = $DB->insert_record('grade_items', (object)array(
|
||||
'courseid'=>$courseid, 'itemname'=>'frog'));
|
||||
// Add a condition on a value existing...
|
||||
$ci->add_grade_condition($gradeitemid, null, null, true);
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
$this->assertEquals(get_string('requires_grade_any', 'condition', 'frog'), $text);
|
||||
|
||||
// Fake it existing.
|
||||
$DB->insert_record('grade_grades', (object)array(
|
||||
'itemid'=>$gradeitemid, 'userid'=>$USER->id, 'finalgrade'=>3.78));
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertTrue($ci->is_available($text));
|
||||
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertTrue($ci->is_available($text, true));
|
||||
|
||||
// Now require that user gets more than 3.78001.
|
||||
$ci->wipe_conditions();
|
||||
$ci->add_grade_condition($gradeitemid, 3.78001, null, true);
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
$this->assertEquals(get_string('requires_grade_min', 'condition', 'frog'), $text);
|
||||
|
||||
// ...just on 3.78...
|
||||
$ci->wipe_conditions();
|
||||
$ci->add_grade_condition($gradeitemid, 3.78, null, true);
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertTrue($ci->is_available($text));
|
||||
|
||||
// ...less than 3.78.
|
||||
$ci->wipe_conditions();
|
||||
$ci->add_grade_condition($gradeitemid, null, 3.78, true);
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
$this->assertEquals(get_string('requires_grade_max', 'condition', 'frog'), $text);
|
||||
|
||||
// ...less than 3.78001.
|
||||
$ci->wipe_conditions();
|
||||
$ci->add_grade_condition($gradeitemid, null, 3.78001, true);
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertTrue($ci->is_available($text));
|
||||
|
||||
// ...in a range that includes it.
|
||||
$ci->wipe_conditions();
|
||||
$ci->add_grade_condition($gradeitemid, 3, 4, true);
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertTrue($ci->is_available($text));
|
||||
|
||||
// ...in a range that doesn't include it.
|
||||
$ci->wipe_conditions();
|
||||
$ci->add_grade_condition($gradeitemid, 4, 5, true);
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
$this->assertEquals(get_string('requires_grade_range', 'condition', 'frog'), $text);
|
||||
}
|
||||
|
||||
public function test_section_is_available() {
|
||||
global $DB, $USER;
|
||||
$courseid = $this->make_course();
|
||||
|
||||
// Enrol user (needed for groups).
|
||||
$enrolplugin = enrol_get_plugin('manual');
|
||||
$course = $DB->get_record('course', array('id' => $courseid));
|
||||
$enrolplugin->add_instance($course);
|
||||
$enrolinstances = enrol_get_instances($courseid, false);
|
||||
foreach ($enrolinstances as $enrolinstance) {
|
||||
if ($enrolinstance->enrol === 'manual') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
$enrolplugin->enrol_user($enrolinstance, $USER->id);
|
||||
|
||||
// Module for conditions later.
|
||||
$cmid = $this->make_course_module($courseid);
|
||||
|
||||
// No conditions.
|
||||
$sectionid = $this->make_section($courseid, array($cmid), 1);
|
||||
$ci = new condition_info_section((object)array('id' => $sectionid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$this->assertTrue($ci->is_available($text, false, 0));
|
||||
$this->assertEquals('', $text);
|
||||
|
||||
// Time (from).
|
||||
$time = time() + 100;
|
||||
$sectionid = $this->make_section($courseid, array(), 2, array('availablefrom' => $time));
|
||||
$ci = new condition_info_section((object)array('id' => $sectionid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
$timetext = userdate($time, get_string('strftimedate', 'langconfig'));
|
||||
$this->assertRegExp('~' . preg_quote($timetext) . '~', $text);
|
||||
|
||||
$time = time()-100;
|
||||
$sectionid = $this->make_section($courseid, array(), 3, array('availablefrom' => $time));
|
||||
$ci = new condition_info_section((object)array('id' => $sectionid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$this->assertTrue($ci->is_available($text));
|
||||
$this->assertEquals('', $text);
|
||||
$timetext = userdate($time, get_string('strftimedate', 'langconfig'));
|
||||
$this->assertRegExp('~' . preg_quote($timetext) . '~', $ci->get_full_information());
|
||||
|
||||
// Time (until).
|
||||
$sectionid = $this->make_section($courseid, array(), 4, array('availableuntil' => time() - 100));
|
||||
$ci = new condition_info_section((object)array('id' => $sectionid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
$this->assertEquals('', $text);
|
||||
|
||||
// Completion: first set up cm.
|
||||
$sectionid = $this->make_section($courseid, array(), 5);
|
||||
$cm = $DB->get_record('course_modules', array('id' => $cmid));
|
||||
$cm->completion = COMPLETION_TRACKING_MANUAL;
|
||||
$DB->update_record('course_modules', $cm);
|
||||
|
||||
// Completion: Reset modinfo after changing the options.
|
||||
rebuild_course_cache($courseid);
|
||||
|
||||
// Completion: Add condition.
|
||||
$ci = new condition_info_section((object)array('id' => $sectionid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$ci->add_completion_condition($cmid, COMPLETION_COMPLETE);
|
||||
$this->wipe_condition_cache();
|
||||
|
||||
// Completion: Check.
|
||||
$this->assertFalse($ci->is_available($text, false));
|
||||
$this->assertEquals(get_string('requires_completion_1', 'condition', 'xxx'), $text);
|
||||
completion_info::wipe_session_cache();
|
||||
$completion = new completion_info($DB->get_record('course', array('id' => $courseid)));
|
||||
$completion->update_state($cm, COMPLETION_COMPLETE);
|
||||
completion_info::wipe_session_cache();
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertTrue($ci->is_available($text));
|
||||
$this->assertFalse($ci->is_available($text, false, $USER->id + 1));
|
||||
|
||||
// Completion: Uncheck.
|
||||
completion_info::wipe_session_cache();
|
||||
$this->wipe_condition_cache();
|
||||
$completion = new completion_info($DB->get_record('course', array('id' => $courseid)));
|
||||
$completion->update_state($cm, COMPLETION_INCOMPLETE);
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
|
||||
// Completion: Incomplete condition.
|
||||
$ci->wipe_conditions();
|
||||
$ci->add_completion_condition($cmid, COMPLETION_INCOMPLETE);
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertTrue($ci->is_available($text));
|
||||
$this->assertTrue($ci->is_available($text, false, $USER->id + 1));
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertTrue($ci->is_available($text, true));
|
||||
|
||||
// Grade: Add a fake grade item.
|
||||
$gradeitemid = $DB->insert_record('grade_items', (object)array(
|
||||
'courseid' => $courseid, 'itemname' => 'frog'));
|
||||
|
||||
// Grade: Add a condition on a value existing.
|
||||
$ci->wipe_conditions();
|
||||
$ci->add_grade_condition($gradeitemid, null, null, true);
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
$this->assertEquals(get_string('requires_grade_any', 'condition', 'frog'), $text);
|
||||
|
||||
// Grade: Fake it existing.
|
||||
$DB->insert_record('grade_grades', (object)array(
|
||||
'itemid' => $gradeitemid, 'userid' => $USER->id, 'finalgrade' => 3.78));
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertTrue($ci->is_available($text));
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertTrue($ci->is_available($text, true));
|
||||
|
||||
// Grade: Now require that user gets more than 3.78001.
|
||||
$ci->wipe_conditions();
|
||||
$ci->add_grade_condition($gradeitemid, 3.78001, null, true);
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
$this->assertEquals(get_string('requires_grade_min', 'condition', 'frog'), $text);
|
||||
|
||||
// Grade: ...just on 3.78...
|
||||
$ci->wipe_conditions();
|
||||
$ci->add_grade_condition($gradeitemid, 3.78, null, true);
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertTrue($ci->is_available($text));
|
||||
|
||||
// Grade: ...less than 3.78.
|
||||
$ci->wipe_conditions();
|
||||
$ci->add_grade_condition($gradeitemid, null, 3.78, true);
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
$this->assertEquals(get_string('requires_grade_max', 'condition', 'frog'), $text);
|
||||
|
||||
// Grade: ...less than 3.78001.
|
||||
$ci->wipe_conditions();
|
||||
$ci->add_grade_condition($gradeitemid, null, 3.78001, true);
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertTrue($ci->is_available($text));
|
||||
|
||||
// Grade: ...in a range that includes it.
|
||||
$ci->wipe_conditions();
|
||||
$ci->add_grade_condition($gradeitemid, 3, 4, true);
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertTrue($ci->is_available($text));
|
||||
|
||||
// Grade: ...in a range that doesn't include it.
|
||||
$ci->wipe_conditions();
|
||||
$ci->add_grade_condition($gradeitemid, 4, 5, true);
|
||||
$this->wipe_condition_cache();
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
$this->assertEquals(get_string('requires_grade_range', 'condition', 'frog'), $text);
|
||||
|
||||
// Grouping: Not member.
|
||||
$groupingid = $this->make_grouping($courseid, 'Grouping');
|
||||
$groupid = $this->make_group($courseid, 'Group', $groupingid);
|
||||
$sectionid = $this->make_section($courseid, array(), 6, array('groupingid' => $groupingid));
|
||||
$ci = new condition_info_section((object)array('id' => $sectionid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
$this->assertEquals(trim(get_string('groupingnoaccess', 'condition')), $text);
|
||||
|
||||
// Grouping: Member.
|
||||
$this->assertTrue(groups_add_member($groupid, $USER->id));
|
||||
|
||||
condition_info_section::init_global_cache();
|
||||
$this->assertTrue($ci->is_available($text));
|
||||
$this->assertEquals('', $text);
|
||||
$this->assertTrue($ci->is_available($text, true));
|
||||
|
||||
// Grouping: Somebody else.
|
||||
$this->assertFalse($ci->is_available($text, false, $USER->id + 1));
|
||||
$this->assertFalse($ci->is_available($text, true, $USER->id + 1));
|
||||
$test = new condition_info_section((object)array('id' => $realsection->id));
|
||||
$this->assertDebuggingCalled();
|
||||
$this->assertEquals($realsection, $test->get_full_section());
|
||||
$this->assertDebuggingCalled();
|
||||
|
||||
// Course id.
|
||||
$test = new condition_info_section((object)array('id' => $realsection->id,
|
||||
'course' => $course->id));
|
||||
$this->assertDebuggingCalled();
|
||||
$this->assertEquals($realsection, $test->get_full_section());
|
||||
$this->assertDebuggingCalled();
|
||||
|
||||
// Full object.
|
||||
$test = new condition_info_section($realsection);
|
||||
$this->assertDebuggingCalled();
|
||||
$this->assertEquals($realsection, $test->get_full_section());
|
||||
$this->assertDebuggingCalled();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests user fields to ensure that the list of provided fields includes only
|
||||
* fields which the equivalent function can be used to obtain the value of.
|
||||
* Tests the is_available function for modules. This does not test all the
|
||||
* conditions and stuff, because it only needs to check that the system
|
||||
* connects through to the real availability API. Also tests
|
||||
* get_full_information function.
|
||||
*/
|
||||
public function test_condition_user_fields() {
|
||||
global $CFG, $DB, $USER;
|
||||
public function test_is_available() {
|
||||
// Create course.
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course();
|
||||
|
||||
// Set up basic data.
|
||||
$courseid = $this->make_course();
|
||||
$cmid = $this->make_course_module($courseid);
|
||||
$ci = new condition_info_testwrapper(
|
||||
(object)array('id' => $cmid), CONDITION_MISSING_EVERYTHING);
|
||||
// Create activity with no restrictions and one with date restriction.
|
||||
$page1 = $generator->get_plugin_generator('mod_page')->create_instance(
|
||||
array('course' => $course));
|
||||
$time = time() + 100;
|
||||
$avail = '{"op":"|","show":true,"c":[{"type":"date","d":">=","t":' . $time . '}]}';
|
||||
$page2 = $generator->get_plugin_generator('mod_page')->create_instance(
|
||||
array('course' => $course, 'availability' => $avail));
|
||||
|
||||
// Add a custom user profile field. Unfortunately there is no back-end
|
||||
// API for adding profile fields without having an actual form and doing
|
||||
// redirects and stuff! These are the default text field parameters.
|
||||
require_once($CFG->dirroot . '/user/profile/lib.php');
|
||||
$field = (object)array(
|
||||
'shortname' => 'myfield', 'name' => 'My field', 'required' => 0,
|
||||
'locked' => 0, 'forceunique' => 0, 'signup' => 0,
|
||||
'visible' => PROFILE_VISIBLE_ALL,
|
||||
'datatype' => 'text', 'description' => 'A field of mine',
|
||||
'descriptionformat' => FORMAT_HTML, 'defaultdata' => '',
|
||||
'defaultdataformat' => FORMAT_HTML, 'param1' => 30, 'param2' => 2048,
|
||||
'param3' => 0, 'param4' => '', 'param5' => '');
|
||||
$customfieldid = $DB->insert_record('user_info_field', $field);
|
||||
// No conditions.
|
||||
$ci = new condition_info((object)array('id' => $page1->cmid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$this->assertDebuggingCalled();
|
||||
$this->assertTrue($ci->is_available($text, false, 0));
|
||||
$this->assertDebuggingCalled();
|
||||
$this->assertEquals('', $text);
|
||||
|
||||
// Get list of condition user fields.
|
||||
$fields = condition_info::get_condition_user_fields();
|
||||
// Date condition.
|
||||
$ci = new condition_info((object)array('id' => $page2->cmid),
|
||||
CONDITION_MISSING_EVERYTHING);
|
||||
$this->assertDebuggingCalled();
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
$this->assertDebuggingCalled();
|
||||
$expectedtime = userdate($time, get_string('strftimedate', 'langconfig'));
|
||||
$this->assertContains($expectedtime, $text);
|
||||
|
||||
// Check custom field is included.
|
||||
$this->assertEquals('My field', $fields[$customfieldid]);
|
||||
// Full information display.
|
||||
$text = $ci->get_full_information();
|
||||
$this->assertDebuggingCalled();
|
||||
$expectedtime = userdate($time, get_string('strftimedate', 'langconfig'));
|
||||
$this->assertContains($expectedtime, $text);
|
||||
}
|
||||
|
||||
// For all other fields, check it actually works to get data from them.
|
||||
foreach ($fields as $fieldid => $name) {
|
||||
// Not checking the result, just that it's possible to get it
|
||||
// without error.
|
||||
$ci->inspect_get_cached_user_profile_field($USER->id, $fieldid);
|
||||
}
|
||||
/**
|
||||
* Tests the is_available function for sections.
|
||||
*/
|
||||
public function test_section_is_available() {
|
||||
global $DB;
|
||||
|
||||
// Change to not logged in user.
|
||||
$this->setUser(null);
|
||||
// Create course.
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course(
|
||||
array('numsections' => 2), array('createsections' => true));
|
||||
|
||||
foreach ($fields as $fieldid => $name) {
|
||||
// Should get false always when not logged in.
|
||||
$this->assertEquals(false, $ci->inspect_get_cached_user_profile_field($USER->id, $fieldid));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test wrapper used only to make protected functions public so they can be
|
||||
* tested.
|
||||
*/
|
||||
class condition_info_testwrapper extends condition_info {
|
||||
public function inspect_get_cached_user_profile_field($userid, $fieldid) {
|
||||
return parent::get_cached_user_profile_field($userid, $fieldid);
|
||||
// Set one of the sections unavailable.
|
||||
$time = time() + 100;
|
||||
$avail = '{"op":"|","show":true,"c":[{"type":"date","d":">=","t":' . $time . '}]}';
|
||||
$DB->set_field('course_sections', 'availability', $avail, array(
|
||||
'course' => $course->id, 'section' => 2));
|
||||
|
||||
$modinfo = get_fast_modinfo($course);
|
||||
|
||||
// No conditions.
|
||||
$ci = new condition_info_section($modinfo->get_section_info(1));
|
||||
$this->assertDebuggingCalled();
|
||||
$this->assertTrue($ci->is_available($text, false, 0));
|
||||
$this->assertDebuggingCalled();
|
||||
$this->assertEquals('', $text);
|
||||
|
||||
// Date condition.
|
||||
$ci = new condition_info_section($modinfo->get_section_info(2));
|
||||
$this->assertDebuggingCalled();
|
||||
$this->assertFalse($ci->is_available($text));
|
||||
$this->assertDebuggingCalled();
|
||||
$expectedtime = userdate($time, get_string('strftimedate', 'langconfig'));
|
||||
$this->assertContains($expectedtime, $text);
|
||||
|
||||
// Full information display.
|
||||
$text = $ci->get_full_information();
|
||||
$this->assertDebuggingCalled();
|
||||
$expectedtime = userdate($time, get_string('strftimedate', 'langconfig'));
|
||||
$this->assertContains($expectedtime, $text);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user