Permalink
Browse files

MDL-29538 core_condition: code cleanup

  • Loading branch information...
markn86 committed May 10, 2012
1 parent af2ce78 commit a8f84c287ccc3d067d36dc64e2865c0abaddc09c
Showing with 55 additions and 55 deletions.
  1. +1 −1 course/lib.php
  2. +11 −11 course/moodleform_mod.php
  3. +42 −42 lib/conditionlib.php
  4. +1 −1 user/profile/definelib.php
View
@@ -2929,7 +2929,7 @@ function delete_course_module($id) {
// very quick on an empty table)
$DB->delete_records('course_modules_completion', array('coursemoduleid' => $cm->id));
$DB->delete_records('course_modules_availability', array('coursemoduleid'=> $cm->id));
- $DB->delete_records('course_modules_avail_fields', array('coursemoduleid'=> $cm->id));
+ $DB->delete_records('course_modules_avail_fields', array('coursemoduleid' => $cm->id));
$DB->delete_records('course_completion_criteria', array('moduleinstance' => $cm->id,
'criteriatype' => COMPLETION_CRITERIA_TYPE_ACTIVITY));
View
@@ -259,9 +259,9 @@ function definition_after_data() {
$num++;
}
- $num=0;
- foreach($fullcm->conditionsfield as $field=>$details) {
- $groupelements=$mform->getElement('conditionfieldgroup['.$num.']')->getElements();
+ $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);
@@ -515,7 +515,7 @@ function standard_coursemodule_elements(){
$gradeoptions[$id] = $item->get_name();
}
asort($gradeoptions);
- $gradeoptions = array(0=>$strnone)+$gradeoptions;
+ $gradeoptions = array(0 => $strnone) + $gradeoptions;
$grouparray = array();
$grouparray[] =& $mform->createElement('select','conditiongradeitemid','',$gradeoptions);
@@ -548,14 +548,14 @@ function standard_coursemodule_elements(){
$useroptions = condition_info::get_condition_user_fields();
asort($useroptions);
- $useroptions = array(0=>$strnone)+$useroptions;
+ $useroptions = array(0 => $strnone) + $useroptions;
$grouparray = array();
- $grouparray[] =& $mform->createElement('select','conditionfield','',$useroptions);
- $grouparray[] =& $mform->createElement('select', 'conditionfieldoperator','',$operators);
+ $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);
+ $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);
@@ -575,7 +575,7 @@ function standard_coursemodule_elements(){
}
}
asort($completionoptions);
- $completionoptions = array(0=>$strnone) + $completionoptions;
+ $completionoptions = array(0 => $strnone) + $completionoptions;
$completionvalues=array(
COMPLETION_COMPLETE=>get_string('completion_complete','condition'),
View
@@ -505,16 +505,19 @@ protected static function fill_availability_conditions_inner($item, $tableprefix
}
// For user fields
$sql = "SELECT cma.id as cmaid, cma.*, uf.*
- FROM {course_modules_avail_fields} cma
- LEFT JOIN {user_info_field} uf
- ON cma.customfieldid = uf.id
- WHERE coursemoduleid= :cmid";
- if ($conditions = $DB->get_records_sql($sql, array('cmid'=>$cm->id))) {
+ FROM {course_modules_avail_fields} cma
+ LEFT JOIN {user_info_field} uf
+ ON cma.customfieldid = uf.id
+ WHERE coursemoduleid = :cmid";
+ if ($conditions = $DB->get_records_sql($sql, array('cmid' => $cm->id))) {
foreach ($conditions as $condition) {
- // If the condition field is numeric, check
- // the user profile field is available
+ // 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 {
@@ -715,7 +718,7 @@ public function wipe_conditions() {
$DB->delete_records($this->availtable, array($this->idfieldname => $this->item->id));
$DB->delete_records('course_modules_avail_fields',
- array('coursemoduleid'=>$this->cm->id));
+ array('coursemoduleid' => $this->cm->id));
// And from memory
$this->item->conditionsgrade = array();
@@ -782,14 +785,14 @@ public function get_full_information($modinfo=null) {
}
// User field conditions
- if (count($this->cm->conditionsfield)>0) {
+ if (count($this->cm->conditionsfield) > 0) {
// Need the array of operators
- foreach ($this->cm->conditionsfield as $field=>$details) {
+ foreach ($this->cm->conditionsfield as $field => $details) {
$a = new stdclass;
$a->field = $details->fieldname;
$a->operator = get_string($details->operator, 'filters');
$a->value = $details->value;
- $information .= get_string('requires_user_field_restriction', 'condition', $a);
+ $information .= get_string('requires_user_field_restriction', 'condition', $a) . ' ';
}
}
@@ -986,20 +989,17 @@ public function is_available(&$information, $grabthelot=false, $userid=0, $modin
}
// Check if user field condition
- if (count($this->cm->conditionsfield)>0) {
- foreach ($this->cm->conditionsfield as $field=>$details) {
- // Need the array of operators
- $operator = $details->operator;
- $value = $details->value;
+ if (count($this->cm->conditionsfield) > 0) {
+ foreach ($this->cm->conditionsfield as $field => $details) {
$uservalue = $this->get_cached_user_profile_field($userid, $field, $grabthelot);
- if (!$fieldconditionmet = $this->field_condition_met($operator, $uservalue, $value)) {
+ if (!$fieldconditionmet = $this->is_field_condition_met($details->operator, $uservalue, $details->value)) {
// Set available to false
$available = false;
$a = new stdClass();
$a->field = $details->fieldname;
- $a->operator = get_string($operator, 'filters');
- $a->value = $value;
- $information .= get_string('requires_user_field_restriction', 'condition', $a).' ';
+ $a->operator = get_string($details->operator, 'filters');
+ $a->value = $details->value;
+ $information .= get_string('requires_user_field_restriction', 'condition', $a) . ' ';
}
}
}
@@ -1173,7 +1173,7 @@ private function get_cached_grade_score($gradeitemid, $grabthelot=false, $userid
* @param char $value the value required
* @return boolean
*/
- private function field_condition_met($operator, $uservalue, $value) {
+ private function is_field_condition_met($operator, $uservalue, $value) {
$fieldconditionmet = true;
switch($operator) {
case OP_CONTAINS: // contains
@@ -1195,15 +1195,15 @@ private function field_condition_met($operator, $uservalue, $value) {
$fieldconditionmet = false;
}
break;
- case OP_STARTS_WITH: // starts with
+ case OP_STARTS_WITH: // starts with
$length = strlen($value);
if ((substr($uservalue, 0, $length) !== $value)) {
$fieldconditionmet = false;
}
break;
case OP_ENDS_WITH: // ends with
$length = strlen($value);
- $start = $length * -1; //negative
+ $start = $length * -1; // negative
if (substr($uservalue, $start) !== $value) {
$fieldconditionmet = false;
}
@@ -1231,7 +1231,7 @@ private function get_cached_user_profile_field($userid, $fieldid, $grabthelot) {
global $USER, $DB, $SESSION;
// Check if the field is a custom profile field
$iscustomprofilefield = is_numeric($fieldid) ? true : false;
- if ($userid==0 || $userid==$USER->id) {
+ if ($userid == 0 || $userid == $USER->id) {
if ($iscustomprofilefield) {
// For current user, go via cache in session
if (empty($SESSION->userfieldcache) || $SESSION->userfieldcacheuserid!=$USER->id) {
@@ -1242,24 +1242,24 @@ private function get_cached_user_profile_field($userid, $fieldid, $grabthelot) {
if ($grabthelot) {
// Get all custom profile field values for user
$sql = "SELECT uf.id, ud.data
- FROM {user_info_field} uf
- LEFT JOIN {user_info_data} ud
- ON uf.id = ud.fieldid
- WHERE ud.userid = :userid";
- if ($records = $DB->get_records_sql($sql, array('userid'=>$USER->id))) {
+ FROM {user_info_field} uf
+ LEFT JOIN {user_info_data} ud
+ ON uf.id = ud.fieldid
+ WHERE ud.userid = :userid";
+ if ($records = $DB->get_records_sql($sql, array('userid' => $USER->id))) {
foreach ($records as $r) {
$SESSION->userfieldcache[$r->id] = $r->data;
}
}
} else {
// Just get specified user field
$sql = "SELECT ud.data
- FROM {user_info_data} ud
- INNER JOIN {user_info_field} uf
- ON ud.fieldid = uf.id
- WHERE uf.id = :fieldid
- AND ud.userid = :userid";
- if ($record = $DB->get_record_sql($sql, array('fieldid'=>$fieldid, 'userid'=>$USER->id))) {
+ FROM {user_info_data} ud
+ INNER JOIN {user_info_field} uf
+ ON ud.fieldid = uf.id
+ WHERE uf.id = :fieldid
+ AND ud.userid = :userid";
+ if ($record = $DB->get_record_sql($sql, array('fieldid' => $fieldid, 'userid' => $USER->id))) {
$field = $record->data;
} else {
$field = false;
@@ -1278,16 +1278,16 @@ private function get_cached_user_profile_field($userid, $fieldid, $grabthelot) {
} else {
if ($iscustomprofilefield) {
$sql = "SELECT ud.data
- FROM {user_info_data} ud
- INNER JOIN {user_info_field} uf
- ON ud.fieldid = uf.id
- WHERE uf.id = :fieldid
- AND ud.userid = :userid";
- if ($record = $DB->get_record_sql($sql, array('fieldid'=>$fieldid, 'userid'=>$userid))) {
+ FROM {user_info_data} ud
+ INNER JOIN {user_info_field} uf
+ ON ud.fieldid = uf.id
+ WHERE uf.id = :fieldid
+ AND ud.userid = :userid";
+ if ($record = $DB->get_record_sql($sql, array('fieldid' => $fieldid, 'userid' => $userid))) {
return $record->data;
}
} else {
- return $DB->get_field('user', $fieldid, array('id'=>$userid), MUST_EXIST);
+ return $DB->get_field('user', $fieldid, array('id' => $userid), MUST_EXIST);
}
// If it reaches here, then no matches found
return false;
@@ -290,7 +290,7 @@ function profile_delete_field($id) {
}
// Delete any module dependencies for this field
- $DB->delete_records('course_modules_avail_fields', array('field'=>$id));
+ $DB->delete_records('course_modules_avail_fields', array('field' => $id));
// Need to rebuild course cache to update the info
rebuild_course_cache();
/// Try to remove the record from the database

0 comments on commit a8f84c2

Please sign in to comment.