Skip to content

Commit

Permalink
Merge branch 'wip-MDL-34914-master' of git://github.com/marinaglancy/…
Browse files Browse the repository at this point in the history
…moodle
  • Loading branch information
danpoltawski committed May 19, 2014
2 parents 9febfdc + 88de944 commit 457bc31
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions user/filters/cohort.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function getOperators() {
1 => get_string('doesnotcontain', 'filters'),
2 => get_string('isequalto', 'filters'),
3 => get_string('startswith', 'filters'),
4 => get_string('endswith', 'filters'));
4 => get_string('endswith', 'filters'),
5 => get_string('isempty', 'filters'));
}

/**
Expand Down Expand Up @@ -80,10 +81,16 @@ public function check_data($formdata) {
$operator = $field.'_op';

if (array_key_exists($operator, $formdata)) {
if ($formdata->$field == '') {
if ($formdata->$operator != 5 and $formdata->$field == '') {
// No data - no change except for empty filter.
return false;
}
return array('operator' => (int)$formdata->$operator, 'value' => $formdata->$field);
// If field value is set then use it, else it's null.
$fieldvalue = null;
if (isset($formdata->$field)) {
$fieldvalue = $formdata->$field;
}
return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue);
}

return false;
Expand All @@ -108,13 +115,15 @@ public function get_sql_filter($data) {
return '';
}

$not = '';
switch($operator) {
case 0: // Contains.
$res = $DB->sql_like('idnumber', ":$name", false, false);
$params[$name] = "%$value%";
break;
case 1: // Does not contain.
$res = $DB->sql_like('idnumber', ":$name", false, false, true);
$not = 'NOT';
$res = $DB->sql_like('idnumber', ":$name", false, false);
$params[$name] = "%$value%";
break;
case 2: // Equal to.
Expand All @@ -129,11 +138,16 @@ public function get_sql_filter($data) {
$res = $DB->sql_like('idnumber', ":$name", false, false);
$params[$name] = "%$value";
break;
case 5: // Empty.
$not = 'NOT';
$res = '(idnumber IS NOT NULL AND idnumber <> :'.$name.')';
$params[$name] = '';
break;
default:
return '';
}

$sql = "id IN (SELECT userid
$sql = "id $not IN (SELECT userid
FROM {cohort_members}
JOIN {cohort} ON {cohort_members}.cohortid = {cohort}.id
WHERE $res)";
Expand Down Expand Up @@ -163,6 +177,8 @@ public function get_label($data) {
case 3: // Starts with.
case 4: // Ends with.
return get_string('textlabel', 'filters', $a);
case 5: // Empty.
return get_string('textlabelnovalue', 'filters', $a);
}

return '';
Expand Down

0 comments on commit 457bc31

Please sign in to comment.