Skip to content

Commit

Permalink
Merge branch 'MDL-51408-master' of git://github.com/marinaglancy/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Oct 5, 2015
2 parents b0fca92 + 4f2424c commit fe73503
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
55 changes: 25 additions & 30 deletions badges/criteria/award_criteria_profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,33 +170,31 @@ public function review($userid, $filtered = false) {
}

$join = '';
$where = '';
$whereparts = array();
$sqlparams = array();
$rule = ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) ? ' OR ' : ' AND ';

foreach ($this->params as $param) {
if (is_numeric($param['field'])) {
$infodata[] = " uid.fieldid = :fieldid{$param['field']} ";
$sqlparams["fieldid{$param['field']}"] = $param['field'];
// This is a custom field.
$idx = count($whereparts) + 1;
$join .= " LEFT JOIN {user_info_data} uid{$idx} ON uid{$idx}.userid = u.id AND uid{$idx}.fieldid = :fieldid{$idx} ";
$sqlparams["fieldid{$idx}"] = $param['field'];
$whereparts[] = "uid{$idx}.id IS NOT NULL";
} else {
$userdata[] = $DB->sql_isnotempty('u', "u.{$param['field']}", false, true);
// This is a field from {user} table.
$whereparts[] = $DB->sql_isnotempty('u', "u.{$param['field']}", false, true);
}
}

// Add user custom field parameters if there are any.
if (!empty($infodata)) {
$extraon = implode($rule, $infodata);
$join = " LEFT JOIN {user_info_data} uid ON uid.userid = u.id AND ({$extraon})";
}
$sqlparams['userid'] = $userid;

// Add user table field parameters if there are any.
if (!empty($userdata)) {
$extraon = implode($rule, $userdata);
$where = " AND ({$extraon})";
if ($whereparts) {
$where = " AND (" . implode($rule, $whereparts) . ")";
} else {
$where = '';
}

$sqlparams['userid'] = $userid;
$sql = "SELECT u.* FROM {user} u " . $join . " WHERE u.id = :userid " . $where;
$sql = "SELECT 1 FROM {user} u " . $join . " WHERE u.id = :userid $where";
$overall = $DB->record_exists_sql($sql, $sqlparams);

return $overall;
Expand All @@ -212,29 +210,26 @@ public function get_completed_criteria_sql() {
global $DB;

$join = '';
$where = '';
$whereparts = array();
$params = array();
$rule = ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) ? ' OR ' : ' AND ';

foreach ($this->params as $param) {
if (is_numeric($param['field'])) {
$infodata[] = " uid.fieldid = :fieldid{$param['field']} ";
$params["fieldid{$param['field']}"] = $param['field'];
// This is a custom field.
$idx = count($whereparts);
$join .= " LEFT JOIN {user_info_data} uid{$idx} ON uid{$idx}.userid = u.id AND uid{$idx}.fieldid = :fieldid{$idx} ";
$params["fieldid{$idx}"] = $param['field'];
$whereparts[] = "uid{$idx}.id IS NOT NULL";
} else {
$userdata[] = $DB->sql_isnotempty('u', "u.{$param['field']}", false, true);
$whereparts[] = $DB->sql_isnotempty('u', "u.{$param['field']}", false, true);
}
}

// Add user custom fields if there are any.
if (!empty($infodata)) {
$extraon = implode($rule, $infodata);
$join = " LEFT JOIN {user_info_data} uid ON uid.userid = u.id AND ({$extraon})";
}

// Add user table fields if there are any.
if (!empty($userdata)) {
$extraon = implode($rule, $userdata);
$where = " AND ({$extraon})";
if ($whereparts) {
$where = " AND (" . implode($rule, $whereparts) . ")";
} else {
$where = '';
}
return array($join, $where, $params);
}
Expand Down
29 changes: 27 additions & 2 deletions badges/tests/badgeslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ public function test_badges_observer_course_module_criteria_review() {
$criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_ACTIVITY, 'badgeid' => $badge->id));
$criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'module_'.$this->module->cmid => $this->module->cmid));

// Assert the badge will not be issued to the user as is.
$badge = new badge($this->coursebadge);
$badge->review_all_criteria();
$this->assertFalse($badge->is_issued($this->user->id));

// Set completion for forum activity.
$c = new completion_info($this->course);
$activities = $c->get_activities();
Expand Down Expand Up @@ -379,6 +384,11 @@ public function test_badges_observer_course_criteria_review() {

$ccompletion = new completion_completion(array('course' => $this->course->id, 'userid' => $this->user->id));

// Assert the badge will not be issued to the user as is.
$badge = new badge($this->coursebadge);
$badge->review_all_criteria();
$this->assertFalse($badge->is_issued($this->user->id));

// Mark course as complete.
$sink = $this->redirectEmails();
$ccompletion->mark_complete();
Expand All @@ -394,18 +404,33 @@ public function test_badges_observer_course_criteria_review() {
* Test badges observer when user_updated event is fired.
*/
public function test_badges_observer_profile_criteria_review() {
global $CFG, $DB;
require_once($CFG->dirroot.'/user/profile/lib.php');

// Add a custom field of textarea type.
$customprofileid = $DB->insert_record('user_info_field', array(
'shortname' => 'newfield', 'name' => 'Description of new field', 'categoryid' => 1,
'datatype' => 'textarea'));

$this->preventResetByRollback(); // Messaging is not compatible with transactions.
$badge = new badge($this->coursebadge);
$this->assertFalse($badge->is_issued($this->user->id));

$criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
$criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
$criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
$criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'field_aim' => 'aim'));
$criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'field_aim' => 'aim',
'field_' . $customprofileid => $customprofileid));

// Assert the badge will not be issued to the user as is.
$badge = new badge($this->coursebadge);
$badge->review_all_criteria();
$this->assertFalse($badge->is_issued($this->user->id));

// Set the required fields and make sure the badge got issued.
$this->user->address = 'Test address';
$this->user->aim = '999999999';
$sink = $this->redirectEmails();
profile_save_data((object)array('id' => $this->user->id, 'profile_field_newfield' => 'X'));
user_update_user($this->user, false);
$this->assertCount(1, $sink->get_messages());
$sink->close();
Expand Down

0 comments on commit fe73503

Please sign in to comment.