Permalink
Browse files

MDL-29538 core_condition: initial commit to add support for making th…

…ings conditional on user profile fields
  • Loading branch information...
markn86 committed Sep 26, 2011
1 parent 6be7840 commit 76af15bb4c9f524497693e7fd8231e9d495c191b
@@ -322,6 +322,8 @@ protected function define_structure() {
$availinfo = new backup_nested_element('availability_info');
$availability = new backup_nested_element('availability', array('id'), array(
'sourcecmid', 'requiredcompletion', 'gradeitemid', 'grademin', 'grademax'));
+ $availability_field = new backup_nested_element('availability_field', array('id'), array(
+ 'field', 'operator', 'value'));
// attach format plugin structure to $module element, only one allowed
$this->add_plugin_structure('format', $module, false);
@@ -333,9 +335,9 @@ protected function define_structure() {
// Define the tree
$module->add_child($availinfo);
$availinfo->add_child($availability);
+ $availinfo->add_child($availability_field);
// Set the sources
-
$module->set_source_sql('
SELECT cm.*, m.version, m.name AS modulename, s.id AS sectionid, s.section AS sectionnumber
FROM {course_modules} cm
@@ -344,6 +346,7 @@ protected function define_structure() {
WHERE cm.id = ?', array(backup::VAR_MODID));
$availability->set_source_table('course_modules_availability', array('coursemoduleid' => backup::VAR_MODID));
+ $availability_field->set_source_table('course_modules_availability_field', array('coursemoduleid' => backup::VAR_MODID));
// Define annotations
$module->annotate_ids('grouping', 'groupingid');
@@ -546,6 +546,14 @@ protected function define_execution() {
$DB->insert_record('course_modules_availability', $availability);
}
}
+
+ // Now we need to do it for conditional field availability
+ $params = array('backupid' => $this->get_restoreid(), 'itemname' => 'module_availability_field');
+ $rs = $DB->get_recordset('backup_ids_temp', $params, '', 'itemid');
+ foreach($rs as $availrec) {
+ $availability = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'module_availability_field', $availrec->itemid)->info;
+ $DB->insert_record('course_modules_availability_field', $availability);
+ }
$rs->close();
}
}
@@ -2539,6 +2547,7 @@ protected function define_structure() {
$paths[] = $module;
if ($CFG->enableavailability) {
$paths[] = new restore_path_element('availability', '/module/availability_info/availability');
+ $paths[] = new restore_path_element('availability_fields', '/module/availability_info/availability_field');
}
// Apply for 'format' plugins optional paths at module level
@@ -2632,15 +2641,23 @@ protected function process_module($data) {
$DB->set_field('course_sections', 'sequence', $sequence, array('id' => $data->section));
}
-
protected function process_availability($data) {
$data = (object)$data;
// Simply going to store the whole availability record now, we'll process
- // all them later in the final task (once all actvivities have been restored)
+ // all them later in the final task (once all activities have been restored)
// Let's call the low level one to be able to store the whole object
$data->coursemoduleid = $this->task->get_moduleid(); // Let add the availability cmid
restore_dbops::set_backup_ids_record($this->get_restoreid(), 'module_availability', $data->id, 0, null, $data);
}
+
+ protected function process_availability_fields($data) {
+ $data = (object)$data;
+ // Simply going to store the whole availability record now, we'll process
+ // all them later in the final task (once all activities have been restored)
+ // Let's call the low level one to be able to store the whole object
+ $data->coursemoduleid = $this->task->get_moduleid(); // Let add the availability cmid
+ restore_dbops::set_backup_ids_record($this->get_restoreid(), 'module_availability_field', $data->id, 0, null, $data);
+ }
}
/**
View
@@ -1111,6 +1111,7 @@ function get_array_of_activities($courseid) {
condition_info::fill_availability_conditions($rawmods[$seq]);
$mod[$seq]->conditionscompletion = $rawmods[$seq]->conditionscompletion;
$mod[$seq]->conditionsgrade = $rawmods[$seq]->conditionsgrade;
+ $mod[$seq]->conditionsfield = $rawmods[$seq]->conditionsfield;
}
$modname = $mod[$seq]->mod;
View
@@ -259,6 +259,15 @@ function definition_after_data() {
$num++;
}
+ $num=0;
+ foreach($fullcm->conditionsfield as $field=>$details) {
+ $groupelements=$mform->getElement('conditionfieldgroup['.$num.']')->getElements();
+ $groupelements[0]->setValue($field);
+ $groupelements[1]->setValue(is_null($details->operator)?'':$details->operator);
+ $groupelements[2]->setValue(is_null($details->value)?'':($details->value));
+ $num++;
+ }
+
if ($completion->is_enabled()) {
$num=0;
foreach($fullcm->conditionscompletion as $othercmid=>$state) {
@@ -471,6 +480,8 @@ function standard_coursemodule_elements(){
}
if (!empty($CFG->enableavailability)) {
+ // String used by conditions
+ $strnone = get_string('none','condition');
// Conditional availability
// Available from/to defaults to midnight because then the display
@@ -504,7 +515,7 @@ function standard_coursemodule_elements(){
$gradeoptions[$id] = $item->get_name();
}
asort($gradeoptions);
- $gradeoptions = array(0=>get_string('none','condition'))+$gradeoptions;
+ $gradeoptions = array(0=>$strnone)+$gradeoptions;
$grouparray = array();
$grouparray[] =& $mform->createElement('select','conditiongradeitemid','',$gradeoptions);
@@ -522,14 +533,34 @@ function standard_coursemodule_elements(){
$ci = new condition_info($this->_cm, CONDITION_MISSING_EXTRATABLE);
$this->_cm = $ci->get_full_course_module();
$count = count($this->_cm->conditionsgrade)+1;
+ $fieldcount = count($this->_cm->conditionsfield)+1;
} else {
$count = 1;
+ $fieldcount = 1;
}
$this->repeat_elements(array($group), $count, array(), 'conditiongraderepeats', 'conditiongradeadds', 2,
get_string('addgrades', 'condition'), true);
$mform->addHelpButton('conditiongradegroup[0]', 'gradecondition', 'condition');
+ // Conditions based on user fields
+ $operators = condition_info::get_condition_user_field_operators();
+ $useroptions = condition_info::get_condition_user_fields();
+ asort($useroptions);
+
+ $useroptions = array(0=>$strnone)+$useroptions;
+ $grouparray = array();
+ $grouparray[] =& $mform->createElement('select','conditionfield','',$useroptions);
+ $grouparray[] =& $mform->createElement('select', 'conditionfieldoperator','',$operators);
+ $grouparray[] =& $mform->createElement('text', 'conditionfieldvalue');
+ $mform->setType('conditionfieldvalue',PARAM_RAW);
+ $group = $mform->createElement('group','conditionfieldgroup',
+ get_string('userfield', 'condition'),$grouparray);
+
+ $this->repeat_elements(array($group), $fieldcount, array(), 'conditionfieldrepeats', 'conditionfieldadds', 2,
+ get_string('adduserfields', 'condition'), true);
+ $mform->addHelpButton('conditionfieldgroup[0]', 'userfield', 'condition');
+
// Conditions based on completion
$completion = new completion_info($COURSE);
if ($completion->is_enabled()) {
@@ -544,7 +575,7 @@ function standard_coursemodule_elements(){
}
}
asort($completionoptions);
- $completionoptions = array(0=>get_string('none','condition'))+$completionoptions;
+ $completionoptions = array(0=>$strnone)+$completionoptions;
$completionvalues=array(
COMPLETION_COMPLETE=>get_string('completion_complete','condition'),
View
@@ -25,6 +25,7 @@
$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.
@@ -74,12 +75,15 @@
$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_restriction'] = 'Only available to users who\'s user field \'{$a->field}\' {$a->operator} \'{$a->value}\'.';
$string['showavailability'] = 'Before activity can be accessed';
$string['showavailabilitysection'] = 'Before section can be accessed';
$string['showavailability_hide'] = 'Hide activity entirely';
$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['userfield'] = 'User field';
+$string['userfield_help'] = 'This is an attribute associated with each individual user';
$string['userrestriction_hidden'] = 'Restricted (completely hidden, no message): &lsquo;{$a}&rsquo;';
$string['userrestriction_visible'] = 'Restricted: &lsquo;{$a}&rsquo;';
$string['groupingnoaccess'] = 'You do not currently belong to a group which has access to this section. ';
Oops, something went wrong.

0 comments on commit 76af15b

Please sign in to comment.