Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-29538 condition: Tidied up user field condition code post merge

  • Loading branch information...
commit 141d3c86d3c3688dd3a3523f852b803a8132c13c 1 parent 871c06e
@samhemelryk samhemelryk authored
View
12 backup/moodle2/backup_stepslib.php
@@ -322,7 +322,7 @@ 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(
+ $availabilityfield = new backup_nested_element('availability_field', array('id'), array(
'userfield', 'customfieldid', 'operator', 'value'));
// attach format plugin structure to $module element, only one allowed
@@ -335,7 +335,7 @@ protected function define_structure() {
// Define the tree
$module->add_child($availinfo);
$availinfo->add_child($availability);
- $availinfo->add_child($availability_field);
+ $availinfo->add_child($availabilityfield);
// Set the sources
$module->set_source_sql('
@@ -346,7 +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_avail_fields', array('coursemoduleid' => backup::VAR_MODID));
+ $availabilityfield->set_source_table('course_modules_avail_fields', array('coursemoduleid' => backup::VAR_MODID));
// Define annotations
$module->annotate_ids('grouping', 'groupingid');
@@ -376,15 +376,15 @@ protected function define_structure() {
// Add nested elements for _availability table
$avail = new backup_nested_element('availability', array('id'), array(
'sourcecmid', 'requiredcompletion', 'gradeitemid', 'grademin', 'grademax'));
- $avail_field = new backup_nested_element('availability_field', array('id'), array(
+ $availfield = new backup_nested_element('availability_field', array('id'), array(
'userfield', 'customfieldid', 'operator', 'value'));
$section->add_child($avail);
- $section->add_child($avail_field);
+ $section->add_child($availfield);
// Define sources
$section->set_source_table('course_sections', array('id' => backup::VAR_SECTIONID));
$avail->set_source_table('course_sections_availability', array('coursesectionid' => backup::VAR_SECTIONID));
- $avail_field->set_source_table('course_sections_avail_fields', array('coursesectionid' => backup::VAR_SECTIONID));
+ $availfield->set_source_table('course_sections_avail_fields', array('coursesectionid' => backup::VAR_SECTIONID));
// Aliases
$section->set_source_alias('section', 'number');
View
29 backup/moodle2/restore_stepslib.php
@@ -546,7 +546,6 @@ protected function define_execution() {
$DB->insert_record('course_modules_availability', $availability);
}
}
-
$rs->close();
}
}
@@ -1148,13 +1147,13 @@ public function process_availability_field($data) {
}
if ($passed) {
// Create the object to insert into the database
- $avail_field = new stdClass();
- $avail_field->coursesectionid = $this->task->get_sectionid();
- $avail_field->userfield = $data->userfield;
- $avail_field->customfieldid = $data->customfieldid;
- $avail_field->operator = $data->operator;
- $avail_field->value = $data->value;
- $DB->insert_record('course_sections_avail_fields', $avail_field);
+ $availfield = new stdClass();
+ $availfield->coursesectionid = $this->task->get_sectionid();
+ $availfield->userfield = $data->userfield;
+ $availfield->customfieldid = $data->customfieldid;
+ $availfield->operator = $data->operator;
+ $availfield->value = $data->value;
+ $DB->insert_record('course_sections_avail_fields', $availfield);
}
}
@@ -2680,14 +2679,14 @@ protected function process_availability_field($data) {
}
if ($passed) {
// Create the object to insert into the database
- $avail_field = new stdClass();
- $avail_field->coursemoduleid = $this->task->get_moduleid(); // Lets add the availability cmid
- $avail_field->userfield = $data->userfield;
- $avail_field->customfieldid = $data->customfieldid;
- $avail_field->operator = $data->operator;
- $avail_field->value = $data->value;
+ $availfield = new stdClass();
+ $availfield->coursemoduleid = $this->task->get_moduleid(); // Lets add the availability cmid
+ $availfield->userfield = $data->userfield;
+ $availfield->customfieldid = $data->customfieldid;
+ $availfield->operator = $data->operator;
+ $availfield->value = $data->value;
// Insert into the database
- $DB->insert_record('course_modules_avail_fields', $avail_field);
+ $DB->insert_record('course_modules_avail_fields', $availfield);
}
}
}
View
2  lang/en/condition.php
@@ -84,7 +84,7 @@
$string['showavailabilitysection_hide'] = 'Hide section entirely';
$string['showavailabilitysection_show'] = 'Show section greyed-out, with restriction information';
$string['userfield'] = 'User field';
-$string['userfield_help'] = 'You can restrict access based on any field from the user’s profile.';
+$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. ';
View
76 lib/conditionlib.php
@@ -363,8 +363,17 @@ public static function update_section_from_form($section, $fromform, $wipefirst=
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class condition_info_base {
- /** @var object, bool, string, string, array */
- protected $item, $gotdata, $availtable, $availfieldtable, $idfieldname, $usergroupings;
+ protected $item;
+ /** @var bool */
+ protected $gotdata;
+ /** @var string */
+ protected $availtable;
+ /** @var string */
+ protected $availfieldtable;
+ /** @var string */
+ protected $idfieldname;
+ /** @var array */
+ protected $usergroupings;
/**
* Constructs with item details.
@@ -441,8 +450,7 @@ public function __construct($item, $tableprefix, $idfield, $expectingmissing, $l
$this->gotdata = true;
// Missing extra data
- if (!isset($item->conditionsgrade) || !isset($item->conditionscompletion)
- || !isset($item->conditionsfield)) {
+ if (!isset($item->conditionsgrade) || !isset($item->conditionscompletion) || !isset($item->conditionsfield)) {
if ($expectingmissing<CONDITION_MISSING_EXTRATABLE) {
debugging('Performance warning: condition_info constructor is ' .
'faster if you pass in a $item from get_fast_modinfo or ' .
@@ -480,8 +488,7 @@ protected static function fill_availability_conditions_inner($item, $tableprefix
}
// Does nothing if the variables are already present
- if (!isset($item->conditionsgrade) || !isset($item->conditionscompletion)
- || !isset($item->conditionsfield)) {
+ if (!isset($item->conditionsgrade) || !isset($item->conditionscompletion) || !isset($item->conditionsfield)) {
$item->conditionsgrade = array();
$item->conditionscompletion = array();
$item->conditionsfield = array();
@@ -510,33 +517,30 @@ protected static function fill_availability_conditions_inner($item, $tableprefix
// For user fields
$sql = "SELECT a.id as cmaid, a.*, uf.*
FROM {" . $tableprefix . "_avail_fields} a
- LEFT JOIN {user_info_field} uf
- ON a.customfieldid = uf.id
+ LEFT JOIN {user_info_field} uf ON a.customfieldid = uf.id
WHERE " . $idfield . " = :itemid";
- if ($conditions = $DB->get_records_sql($sql, array('itemid' => $item->id))) {
- foreach ($conditions as $condition) {
- // If the custom field is not empty, then
- // we have a custom profile field
- if (!empty($condition->customfieldid)) {
- $field = $condition->customfieldid;
- // Check if the profile field name is not empty, if it is
- // then the custom profile field no longer exists, so
- // display !missing instead.
- if (!empty($condition->name)) {
- $fieldname = $condition->name;
- } else {
- $fieldname = '!missing';
- }
+ $conditions = $DB->get_records_sql($sql, array('itemid' => $item->id));
+ foreach ($conditions as $condition) {
+ // If the custom field is not empty, then we have a custom profile field
+ if (!empty($condition->customfieldid)) {
+ $field = $condition->customfieldid;
+ // Check if the profile field name is not empty, if it is
+ // then the custom profile field no longer exists, so
+ // display !missing instead.
+ if (!empty($condition->name)) {
+ $fieldname = $condition->name;
} else {
- $field = $condition->userfield;
- $fieldname = $condition->userfield;
+ $fieldname = '!missing';
}
- $details = new stdClass;
- $details->fieldname = $fieldname;
- $details->operator = $condition->operator;
- $details->value = $condition->value;
- $item->conditionsfield[$field] = $details;
+ } else {
+ $field = $condition->userfield;
+ $fieldname = $condition->userfield;
}
+ $details = new stdClass;
+ $details->fieldname = $fieldname;
+ $details->operator = $condition->operator;
+ $details->value = $condition->value;
+ $item->conditionsfield[$field] = $details;
}
}
}
@@ -591,7 +595,7 @@ public static function get_condition_user_field_operators() {
/**
* The user fields we can compare
*
- * @global $DB
+ * @global moodle_database $DB
* @return array Associative array from user field constants to display name
*/
public static function get_condition_user_fields() {
@@ -1175,9 +1179,9 @@ private function get_cached_grade_score($gradeitemid, $grabthelot=false, $userid
/**
* Returns true if a field meets the required conditions, false otherwise.
*
- * @param char $operator the requirement/condition
- * @param char $uservalue the user's value
- * @param char $value the value required
+ * @param string $operator the requirement/condition
+ * @param string $uservalue the user's value
+ * @param string $value the value required
* @return boolean
*/
private function is_field_condition_met($operator, $uservalue, $value) {
@@ -1227,11 +1231,9 @@ private function is_field_condition_met($operator, $uservalue, $value) {
/**
* Return the value for a user's profile field
*
- * @param int $userid set if requesting grade for a different user (does
- * not use cache)
+ * @param int $userid set if requesting grade for a different user (does not use cache)
* @param int $fieldid the user profile field id
- * @param bool $grabthelot If true, grabs all the user profile fields for
- * current user on this course, so that later ones come from cache
+ * @param bool $grabthelot If true, grabs all the user profile fields for current user on this course, so that later ones come from cache
* @return string the user value, or false if user does not have a user field value yet
*/
private function get_cached_user_profile_field($userid, $fieldid, $grabthelot) {
View
8 lib/db/install.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="lib/db" VERSION="20120620" COMMENT="XMLDB file for core Moodle tables"
+<XMLDB PATH="lib/db" VERSION="20120709" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
@@ -347,7 +347,7 @@
<KEY NAME="gradeitemid" TYPE="foreign" FIELDS="gradeitemid" REFTABLE="grade_items" REFFIELDS="id" PREVIOUS="sourcecmid"/>
</KEYS>
</TABLE>
- <TABLE NAME="course_modules_avail_fields" COMMENT="Stores user field conditions that affect whether an activity is currently available to students." PREVIOUS="course_modules_availability" NEXT="course_modules_completion">
+ <TABLE NAME="course_modules_avail_fields" COMMENT="Stores user field conditions that affect whether an activity is currently available." PREVIOUS="course_modules_availability" NEXT="course_modules_completion">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="coursemoduleid"/>
<FIELD NAME="coursemoduleid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="ID of the module whose availability is being restricted by this condition." PREVIOUS="id" NEXT="userfield"/>
@@ -417,7 +417,7 @@
<KEY NAME="gradeitemid" TYPE="foreign" FIELDS="gradeitemid" REFTABLE="grade_items" REFFIELDS="id" PREVIOUS="sourcecmid"/>
</KEYS>
</TABLE>
- <TABLE NAME="course_sections_avail_fields" COMMENT="Stores user field conditions that affect whether an activity is currently available to students." PREVIOUS="course_sections_availability" NEXT="course_request">
+ <TABLE NAME="course_sections_avail_fields" COMMENT="Stores user field conditions that affect whether an activity is currently available." PREVIOUS="course_sections_availability" NEXT="course_request">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="coursesectionid"/>
<FIELD NAME="coursesectionid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="ID of the section whose availability is being restricted by this condition." PREVIOUS="id" NEXT="userfield"/>
@@ -2875,4 +2875,4 @@
</KEYS>
</TABLE>
</TABLES>
-</XMLDB>
+</XMLDB>
View
94 lib/db/upgrade.php
@@ -594,51 +594,6 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2012051100.03);
}
- if ($oldversion < 2012051700.02) {
- // Define table course_modules_avail_fields to be created
- $table = new xmldb_table('course_modules_avail_fields');
-
- // Adding fields to table course_modules_avail_fields
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
- $table->add_field('userfield', XMLDB_TYPE_CHAR, '50', null, null, null, null);
- $table->add_field('customfieldid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
- $table->add_field('operator', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
- $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
-
- // Adding keys to table course_modules_avail_fields
- $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
- $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
-
- // Conditionally launch create table for course_modules_avail_fields
- if (!$dbman->table_exists($table)) {
- $dbman->create_table($table);
- }
-
- // Define table section_modules_avail_fields to be created
- $table = new xmldb_table('course_sections_avail_fields');
-
- // Adding fields to table course_modules_section_fields
- $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('coursesectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
- $table->add_field('userfield', XMLDB_TYPE_CHAR, '50', null, null, null, null);
- $table->add_field('customfieldid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
- $table->add_field('operator', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
- $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
-
- // Adding keys to table course_modules_avail_fields
- $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
- $table->add_key('coursesectionid', XMLDB_KEY_FOREIGN, array('coursesectionid'), 'course_sections', array('id'));
-
- // Conditionally launch create table for section_modules_avail_fields
- if (!$dbman->table_exists($table)) {
- $dbman->create_table($table);
- }
-
- // Main savepoint reached
- upgrade_main_savepoint(true, 2012051700.02);
- }
-
if ($oldversion < 2012052100.00) {
// Define field referencefileid to be added to files.
@@ -944,6 +899,55 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2012062500.02);
}
+ if ($oldversion < 2012062500.04) {
+ // Define table course_modules_avail_fields to be created
+ $table = new xmldb_table('course_modules_avail_fields');
+
+ // Adding fields to table course_modules_avail_fields
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('userfield', XMLDB_TYPE_CHAR, '50', null, null, null, null);
+ $table->add_field('customfieldid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
+ $table->add_field('operator', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
+
+ // Adding keys to table course_modules_avail_fields
+ $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
+ $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
+
+ // Conditionally launch create table for course_modules_avail_fields
+ if (!$dbman->table_exists($table)) {
+ $dbman->create_table($table);
+ }
+
+ // Main savepoint reached
+ upgrade_main_savepoint(true, 2012062500.04);
+ }
+
+ if ($oldversion < 2012062500.05) {
+ // Define table course_sections_avail_fields to be created
+ $table = new xmldb_table('course_sections_avail_fields');
+
+ // Adding fields to table course_sections_avail_fields
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('coursesectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('userfield', XMLDB_TYPE_CHAR, '50', null, null, null, null);
+ $table->add_field('customfieldid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
+ $table->add_field('operator', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
+
+ // Adding keys to table course_sections_avail_fields
+ $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
+ $table->add_key('coursesectionid', XMLDB_KEY_FOREIGN, array('coursesectionid'), 'course_sections', array('id'));
+
+ // Conditionally launch create table for course_sections_avail_fields
+ if (!$dbman->table_exists($table)) {
+ $dbman->create_table($table);
+ }
+
+ // Main savepoint reached
+ upgrade_main_savepoint(true, 2012062500.05);
+ }
return true;
}
View
10 lib/moodlelib.php
@@ -4515,15 +4515,6 @@ function remove_course_contents($courseid, $showfeedback = true, array $options
echo $OUTPUT->notification($strdeleted.get_string('completion', 'completion'), 'notifysuccess');
}
- // Remove all data from availability and completion tables that is associated
- // with course-modules belonging to this course. Note this is done even if the
- // features are not enabled now, in case they were enabled previously
- $subquery = 'coursemoduleid IN (SELECT id from {course_modules} WHERE course = ?)';
- $subqueryparam = array($courseid);
- $DB->delete_records_select('course_modules_completion', $subquery, $subqueryparam);
- $DB->delete_records_select('course_modules_availability', $subquery, $subqueryparam);
- $DB->delete_records_select('course_modules_avail_fields', $subquery, $subqueryparam);
-
// Remove all data from gradebook - this needs to be done before course modules
// because while deleting this information, the system may need to reference
// the course modules that own the grades.
@@ -4616,6 +4607,7 @@ function remove_course_contents($courseid, $showfeedback = true, array $options
context_helper::delete_instance(CONTEXT_MODULE, $cm->id);
$DB->delete_records('course_modules', array('id'=>$cm->id));
}
+
if ($showfeedback) {
echo $OUTPUT->notification($strdeleted.get_string('type_mod_plural', 'plugin'), 'notifysuccess');
}
View
2  version.php
@@ -30,7 +30,7 @@
defined('MOODLE_INTERNAL') || die();
-$version = 2012062500.03; // YYYYMMDD = weekly release date of this DEV branch
+$version = 2012062500.05; // YYYYMMDD = weekly release date of this DEV branch
// RR = release increments - 00 in DEV branches
// .XX = incremental changes
Please sign in to comment.
Something went wrong with that request. Please try again.