Skip to content

Commit

Permalink
StudentQuiz: Implement Fast filter UI redesign
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuvu authored and AnupamaSarjoshi committed Sep 17, 2024
1 parent b262876 commit 1a6b714
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 188 deletions.
95 changes: 52 additions & 43 deletions classes/condition/studentquiz_condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,56 +123,59 @@ protected function init() {
$this->tests = array();
$this->customparams = array();

foreach ($this->filterform->get_fields() as $field) {

// Validate input.
$data = $field->check_data($adddata);

// If input is valid, at least one filter was activated.
if ($data === false) {
continue;
} else {
$this->isfilteractive = true;
}
foreach ($this->filterform->get_fields() as $fieldgroup) {
$conditionsgroup = [];
foreach ($fieldgroup as $field) {
// Validate input.
$data = $field->check_data($adddata);

// If input is valid, at least one filter was activated.
if ($data === false) {
continue;
} else {
$this->isfilteractive = true;
}

$sqldata = $field->get_sql_filter($data);
$sqldata = $field->get_sql_filter($data);

// Disable filtering by firstname if anonymized.
if ($field->_name == 'firstname' && !(mod_studentquiz_check_created_permission($this->cm->id) ||
!$this->report->is_anonymized())) {
continue;
}
// Disable filtering by firstname if anonymized.
if ($field->_name == 'firstname' && !(mod_studentquiz_check_created_permission($this->cm->id) ||
!$this->report->is_anonymized())) {
continue;
}

// Disable filtering by firstname if anonymized.
if ($field->_name == 'lastname' && !(mod_studentquiz_check_created_permission($this->cm->id) ||
!$this->report->is_anonymized())) {
continue;
}
// Disable filtering by firstname if anonymized.
if ($field->_name == 'lastname' && !(mod_studentquiz_check_created_permission($this->cm->id) ||
!$this->report->is_anonymized())) {
continue;
}

// Respect leading and ending ',' for the tagarray as provided by tag_column.php.
if ($field->_name == 'tagarray') {
foreach ($sqldata[1] as $key => $value) {
if (!empty($value)) {
$sqldata[1][$key] = "%,$value,%";
} else {
$sqldata[0] = "$field->_name IS NULL";
// Respect leading and ending ',' for the tagarray as provided by tag_column.php.
if ($field->_name == 'tagarray') {
foreach ($sqldata[1] as $key => $value) {
if (!empty($value)) {
$sqldata[1][$key] = "%,$value,%";
} else {
$sqldata[0] = "$field->_name IS NULL";
}
}
}
}

// TODO: cleanup that buggy filter function to remove this!
// The user_filter_checkbox class has a buggy get_sql_filter function.
if ($field->_name == 'createdby') {
$sqldata = array($field->_name . ' = ' . intval($data['value']), array());
}
// TODO: cleanup that buggy filter function to remove this!
// The user_filter_checkbox class has a buggy get_sql_filter function.
if ($field->_name == 'createdby') {
$sqldata = [$field->_name . ' = ' . intval($data['value']), []];
}

if (is_array($sqldata)) {
$sqldata[0] = str_replace($field->_name, $this->get_sql_field($field->_name)
, $sqldata[0]);
$sqldata[0] = $this->get_special_sql($sqldata[0], $field->_name);
$this->tests[] = '((' . $sqldata[0] . '))';
$this->customparams = array_merge($this->customparams, $sqldata[1]);
if (is_array($sqldata)) {
$sqldata[0] = str_replace($field->_name, $this->get_sql_field($field->_name)
, $sqldata[0]);
$sqldata[0] = $this->get_special_sql($sqldata[0], $field->_name);
$conditionsgroup[] = $sqldata[0];
$this->customparams = array_merge($this->customparams, $sqldata[1]);
}
}
$this->tests[] = $conditionsgroup;
}
}
}
Expand All @@ -191,7 +194,7 @@ private function get_special_sql($sqldata, $name) {
ELSE 0
END)', $sqldata);
}
if ($name == "onlynew") {
if ($name == 'onlynew' || $name == 'onlyanswered') {
return str_replace('myattempts', 'sp.attempts', $sqldata);
}
return $sqldata;
Expand Down Expand Up @@ -256,7 +259,13 @@ private function get_sql_table_prefix($name) {
* @return string SQL fragment. Must use named parameters.
*/
public function where() {
return implode(' AND ', $this->tests);
$conditions = [];
foreach ($this->tests as $groupconditions) {
if (!empty($groupconditions)) {
$conditions[] = '(' . implode(' OR ', $groupconditions) . ')';
}
}
return implode(' AND ', $conditions);
}

/**
Expand Down
31 changes: 22 additions & 9 deletions classes/question/bank/question_bank_filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,27 @@ public function definition() {

$mform->addElement('header', 'filtertab', get_string('filter', 'studentquiz'));
$mform->setExpanded('filtertab', true);
$fastfilters = array();
foreach ($this->fields as $field) {
if ($field instanceof toggle_filter_checkbox) {
$field->setup_form_in_group($mform, $fastfilters);
$grouplabel = [
get_string('state_column_name', 'studentquiz'),
get_string('filter_label_owner', 'studentquiz'),
get_string('filter_label_difficulty_level', 'studentquiz'),
get_string('filter_label_rates', 'studentquiz')
];
$i = 0;
foreach ($this->fields as $fieldgroup) {
$fasterfiltergroup = [];
foreach ($fieldgroup as $f) {
if ($f instanceof toggle_filter_checkbox) {
$f->setup_form_in_group($mform, $fasterfiltergroup);
} else {
$f->setupForm($mform);
}
}
}
$mform->addGroup($fastfilters, 'fastfilters', get_string('filter_label_fast_filters', 'studentquiz'), ' ', false);
foreach ($this->fields as $field) {
if (!$field instanceof toggle_filter_checkbox) {
$field->setupForm($mform);
if ($fasterfiltergroup) {
$mform->addGroup($fasterfiltergroup, 'fastfiltersgroup' . $i,
$grouplabel[$i], '', false);
}
$i++;
}
$group = array();
$group[] = $mform->createElement('submit', 'submitbutton', get_string('filter'));
Expand Down Expand Up @@ -377,6 +387,9 @@ public function get_sql_filter($data) {
case 2:
$res = "$this->field = $this->value";
break;
case 3:
$res = "$this->field <> $this->value";
break;
default:
$res = '';
}
Expand Down
113 changes: 66 additions & 47 deletions classes/question/bank/studentquiz_bank_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,99 +418,118 @@ private function set_filter_form_fields($anonymize = true) {
$this->fields = array();

// Fast filters.
$this->fields[] = new \toggle_filter_checkbox('onlynew',
$stategroup = [];
$stategroup[] = new \toggle_filter_checkbox('only_new_state',
get_string('state_newplural', 'studentquiz'), false, 'sqq.state',
['approved'], 2, studentquiz_helper::STATE_NEW);
$stategroup[] = new \toggle_filter_checkbox('only_changed_state',
get_string('state_changedplural', 'studentquiz'), false, 'sqq.state', // Fast filters.

['approved'], 2, studentquiz_helper::STATE_CHANGED);
$stategroup[] = new \toggle_filter_checkbox('only_reviewable_state',
get_string('state_reviewableplural', 'studentquiz'), false, 'sqq.state',
['approved'], 2, studentquiz_helper::STATE_REVIEWABLE);
$stategroup[] = new \toggle_filter_checkbox('only_approved_state',
get_string('state_approvedplural', 'studentquiz'), false, 'sqq.state',
['approved'], 2, studentquiz_helper::STATE_APPROVED);
$stategroup[] = new \toggle_filter_checkbox('only_disapproved_state',
get_string('state_disapprovedplural', 'studentquiz'), false, 'sqq.state',
['approved'], 2, studentquiz_helper::STATE_DISAPPROVED);
$stategroup[] = new \toggle_filter_checkbox('onlynew',
get_string('filter_label_onlynew', 'studentquiz'),
false, 'myattempts', array('myattempts', 'myattempts_op'), 0, 0,
false, 'myattempts', ['myattempts', 'myattempts_op'], 0, 0,
get_string('filter_label_onlynew_help', 'studentquiz'));
$stategroup[] = new \toggle_filter_checkbox('onlyanswered',
get_string('filter_label_answered', 'studentquiz'),
false, 'myattempts', ['myattempts', 'myattempts_op'], 1, 1,
get_string('filter_label_onlynew_help', 'studentquiz'));
$this->fields[] = $stategroup;

$this->fields[] = new \toggle_filter_checkbox('only_new_state',
get_string('state_newplural', 'studentquiz'), false, 'sqq.state',
['approved'], 2, studentquiz_helper::STATE_NEW);
$this->fields[] = new \toggle_filter_checkbox('only_approved_state',
get_string('state_approvedplural', 'studentquiz'), false, 'sqq.state',
['approved'], 2, studentquiz_helper::STATE_APPROVED);
$this->fields[] = new \toggle_filter_checkbox('only_disapproved_state',
get_string('state_disapprovedplural', 'studentquiz'), false, 'sqq.state',
['approved'], 2, studentquiz_helper::STATE_DISAPPROVED);
$this->fields[] = new \toggle_filter_checkbox('only_changed_state',
get_string('state_changedplural', 'studentquiz'), false, 'sqq.state',
['approved'], 2, studentquiz_helper::STATE_CHANGED);
$this->fields[] = new \toggle_filter_checkbox('only_reviewable_state',
get_string('state_reviewableplural', 'studentquiz'), false, 'sqq.state',
['approved'], 2, studentquiz_helper::STATE_REVIEWABLE);

$this->fields[] = new \toggle_filter_checkbox('onlygood',
get_string('filter_label_onlygood', 'studentquiz'),
false, 'vo.rate', array('rate', 'rate_op'), 1, 4,
get_string('filter_label_onlygood_help', 'studentquiz', '4'));

$this->fields[] = new \toggle_filter_checkbox('onlymine',
$ownergroup = [];
$ownergroup[] = new \toggle_filter_checkbox('onlymine',
get_string('filter_label_onlymine', 'studentquiz'),
false, 'q.createdby', array('createdby'), 2, $this->userid,
false, 'q.createdby', ['createdby'], 2, $this->userid,
get_string('filter_label_onlymine_help', 'studentquiz'));

$this->fields[] = new \toggle_filter_checkbox('onlydifficultforme',
$ownergroup[] = new \toggle_filter_checkbox('notmine',
get_string('filter_label_notmine', 'studentquiz'),
false, 'q.createdby', ['createdby'], 3, $this->userid,
get_string('filter_label_notmine_help', 'studentquiz'));
$this->fields[]= $ownergroup;

$difficultygroup = [];
$difficultygroup[] = new \toggle_filter_checkbox('onlydifficultforme',
get_string('filter_label_onlydifficultforme', 'studentquiz'),
false, 'mydifficulty', array('mydifficulty', 'mydifficulty_op'), 1, 0.60,
false, 'mydifficulty', ['mydifficulty', 'mydifficulty_op'], 1, 0.60,
get_string('filter_label_onlydifficultforme_help', 'studentquiz', '60'));

$this->fields[] = new \toggle_filter_checkbox('onlydifficult',
$difficultygroup[] = new \toggle_filter_checkbox('onlydifficult',
get_string('filter_label_onlydifficult', 'studentquiz'),
false, 'dl.difficultylevel', array('difficultylevel', 'difficultylevel_op'), 1, 0.60,
false, 'dl.difficultylevel', ['difficultylevel', 'difficultylevel_op'], 1, 0.60,
get_string('filter_label_onlydifficult_help', 'studentquiz', '60'));

$this->fields[] = $difficultygroup;
$ratinggroup = [];
$ratinggroup[] = new \toggle_filter_checkbox('onlygood',
get_string('filter_label_onlygood', 'studentquiz'),
false, 'vo.rate', ['rate', 'rate_op'], 1, 4,
get_string('filter_label_onlygood_help', 'studentquiz', '4'));
$this->fields[] = $ratinggroup;

// Advanced filters.
$this->fields[] = new \studentquiz_user_filter_text('tagarray', get_string('filter_label_tags', 'studentquiz'),
$advancedgroups = [];
$advancedgroups[] = new \studentquiz_user_filter_text('tagarray', get_string('filter_label_tags', 'studentquiz'),
true, 'tagarray');

$states = array();
$states = [];
foreach (studentquiz_helper::$statename as $num => $name) {
if ($num == studentquiz_helper::STATE_DELETE || $num == studentquiz_helper::STATE_HIDE) {
continue;
}
$states[$num] = get_string('state_'.$name, 'studentquiz');
}
$this->fields[] = new \user_filter_simpleselect('state', get_string('state_column_name', 'studentquiz'),
$advancedgroups[] = new \user_filter_simpleselect('state', get_string('state_column_name', 'studentquiz'),
true, 'state', $states);

$this->fields[] = new \user_filter_number('rate', get_string('filter_label_rates', 'studentquiz'),
$advancedgroups[] = new \user_filter_number('rate', get_string('filter_label_rates', 'studentquiz'),
true, 'rate');
$this->fields[] = new \user_filter_percent('difficultylevel', get_string('filter_label_difficulty_level', 'studentquiz'),
$advancedgroups[] = new \user_filter_percent('difficultylevel', get_string('filter_label_difficulty_level', 'studentquiz'),
true, 'difficultylevel');

$this->fields[] = new \user_filter_number('publiccomment', get_string('filter_label_comment', 'studentquiz'),
$advancedgroups[] = new \user_filter_number('publiccomment', get_string('filter_label_comment', 'studentquiz'),
true, 'publiccomment');
$this->fields[] = new \studentquiz_user_filter_text('name', get_string('filter_label_question', 'studentquiz'),
$advancedgroups[] = new \studentquiz_user_filter_text('name', get_string('filter_label_question', 'studentquiz'),
true, 'name');
$this->fields[] = new \studentquiz_user_filter_text('questiontext', get_string('filter_label_questiontext', 'studentquiz'),
$advancedgroups[] = new \studentquiz_user_filter_text('questiontext', get_string('filter_label_questiontext', 'studentquiz'),
true, 'questiontext');

if ($anonymize) {
$this->fields[] = new \user_filter_checkbox('createdby', get_string('filter_label_show_mine', 'studentquiz'),
$advancedgroups[] = new \user_filter_checkbox('createdby', get_string('filter_label_show_mine', 'studentquiz'),
true, 'createdby');
} else {
$this->fields[] = new \studentquiz_user_filter_text('firstname', get_string('firstname'), true, 'firstname');
$this->fields[] = new \studentquiz_user_filter_text('lastname', get_string('lastname'), true, 'lastname');
$advancedgroups[] = new \studentquiz_user_filter_text('firstname', get_string('firstname'), true, 'firstname');
$advancedgroups[] = new \studentquiz_user_filter_text('lastname', get_string('lastname'), true, 'lastname');
}

$this->fields[] = new \studentquiz_user_filter_date('timecreated', get_string('filter_label_createdate', 'studentquiz'),
$advancedgroups[] = new \studentquiz_user_filter_date('timecreated', get_string('filter_label_createdate', 'studentquiz'),
true, 'timecreated');

$this->fields[] = new \user_filter_simpleselect('lastanswercorrect',
$advancedgroups[] = new \user_filter_simpleselect('lastanswercorrect',
get_string('filter_label_mylastattempt', 'studentquiz'),
true, 'lastanswercorrect', array(
true, 'lastanswercorrect', [
'1' => get_string('lastattempt_right', 'studentquiz'),
'0' => get_string('lastattempt_wrong', 'studentquiz')
));
]);

$this->fields[] = new \user_filter_number('myattempts', get_string('filter_label_myattempts', 'studentquiz'),
$advancedgroups[] = new \user_filter_number('myattempts', get_string('filter_label_myattempts', 'studentquiz'),
true, 'myattempts');

$this->fields[] = new \user_filter_number('mydifficulty', get_string('filter_label_mydifficulty', 'studentquiz'),
$advancedgroups[] = new \user_filter_number('mydifficulty', get_string('filter_label_mydifficulty', 'studentquiz'),
true, 'mydifficulty');

$this->fields[] = new \user_filter_number('myrate', get_string('filter_label_myrate', 'studentquiz'),
$advancedgroups[] = new \user_filter_number('myrate', get_string('filter_label_myrate', 'studentquiz'),
true, 'myrate');
$this->fields[] = $advancedgroups;
}

/**
Expand Down
Loading

0 comments on commit 1a6b714

Please sign in to comment.