Skip to content

Commit

Permalink
Merge branch 'MDL-54542-master' of git://github.com/FMCorz/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed May 18, 2016
2 parents ccf323d + 4e968dc commit 60e75de
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 9 deletions.
16 changes: 8 additions & 8 deletions competency/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1332,8 +1332,8 @@ public static function list_user_competencies_to_review($skip = 0, $limit = 50,
}

$capability = 'moodle/competency:usercompetencyreview';
$ucfields = user_competency::get_sql_fields('uc');
$compfields = competency::get_sql_fields('c');
$ucfields = user_competency::get_sql_fields('uc', 'uc_');
$compfields = competency::get_sql_fields('c', 'c_');
$usercols = array('id') + get_user_fieldnames();
$userfields = array();
foreach ($usercols as $field) {
Expand Down Expand Up @@ -1376,8 +1376,8 @@ public static function list_user_competencies_to_review($skip = 0, $limit = 50,
$records = $DB->get_recordset_sql($getsql, $params, $skip, $limit);
foreach ($records as $record) {
$objects = (object) array(
'usercompetency' => new user_competency(0, user_competency::extract_record($record)),
'competency' => new competency(0, competency::extract_record($record)),
'usercompetency' => new user_competency(0, user_competency::extract_record($record, 'uc_')),
'competency' => new competency(0, competency::extract_record($record, 'c_')),
'user' => persistent::extract_record($record, 'usr_'),
);
$competencies[] = $objects;
Expand Down Expand Up @@ -2369,8 +2369,8 @@ public static function list_plans_to_review($skip = 0, $limit = 100, $userid = n
$userid = $USER->id;
}

$planfields = plan::get_sql_fields('p');
$tplfields = template::get_sql_fields('t');
$planfields = plan::get_sql_fields('p', 'plan_');
$tplfields = template::get_sql_fields('t', 'tpl_');
$usercols = array('id') + get_user_fieldnames();
$userfields = array();
foreach ($usercols as $field) {
Expand Down Expand Up @@ -2412,11 +2412,11 @@ public static function list_plans_to_review($skip = 0, $limit = 100, $userid = n
$plans = array();
$records = $DB->get_recordset_sql($select . $sql, $params, $skip, $limit);
foreach ($records as $record) {
$plan = new plan(0, plan::extract_record($record));
$plan = new plan(0, plan::extract_record($record, 'plan_'));
$template = null;

if ($plan->is_based_on_template()) {
$template = new template(0, template::extract_record($record));
$template = new template(0, template::extract_record($record, 'tpl_'));
}

$plans[] = (object) array(
Expand Down
10 changes: 9 additions & 1 deletion competency/classes/persistent.php
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ public static function get_records_select($select, $params = null, $sort = '', $
* @return string The SQL fragment.
*/
public static function get_sql_fields($alias, $prefix = null) {
global $CFG;
$fields = array();

if ($prefix === null) {
Expand All @@ -798,7 +799,14 @@ public static function get_sql_fields($alias, $prefix = null) {
$properties = array('id' => $id) + $properties;

foreach ($properties as $property => $definition) {
$fields[] = $alias . '.' . $property . ' AS ' . $prefix . $property;
$as = $prefix . $property;
$fields[] = $alias . '.' . $property . ' AS ' . $as;

// Warn developers that the query will not always work.
if ($CFG->debugdeveloper && strlen($as) > 30) {
throw new coding_exception("The alias '$as' for column '$alias.$property' exceeds 30 characters" .
" and will therefore not work across all supported databases.");
}
}

return implode(', ', $fields);
Expand Down
96 changes: 96 additions & 0 deletions competency/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4405,4 +4405,100 @@ public function test_delete_evidence_without_permissions() {

api::delete_evidence($ev1);
}

public function test_list_plans_to_review() {
$dg = $this->getDataGenerator();
$this->resetAfterTest();
$ccg = $dg->get_plugin_generator('core_competency');
$sysctx = context_system::instance();
$this->setAdminUser();

$reviewer = $dg->create_user();
$roleallow = $dg->create_role();
$roleprohibit = $dg->create_role();
assign_capability('moodle/competency:planreview', CAP_ALLOW, $roleallow, $sysctx->id);
assign_capability('moodle/competency:planreview', CAP_PROHIBIT, $roleprohibit, $sysctx->id);
role_assign($roleallow, $reviewer->id, $sysctx->id);
accesslib_clear_all_caches_for_unit_testing();

$u1 = $dg->create_user();
$u2 = $dg->create_user();
$f1 = $ccg->create_framework();
$comp1 = $ccg->create_competency(['competencyframeworkid' => $f1->get_id()]);
$p1a = $ccg->create_plan(['userid' => $u1->id, 'status' => plan::STATUS_WAITING_FOR_REVIEW]);
$p1b = $ccg->create_plan(['userid' => $u1->id, 'status' => plan::STATUS_IN_REVIEW, 'reviewerid' => $reviewer->id]);
$p1c = $ccg->create_plan(['userid' => $u1->id, 'status' => plan::STATUS_DRAFT]);
$p2a = $ccg->create_plan(['userid' => $u2->id, 'status' => plan::STATUS_WAITING_FOR_REVIEW]);
$p2b = $ccg->create_plan(['userid' => $u2->id, 'status' => plan::STATUS_IN_REVIEW]);
$p2c = $ccg->create_plan(['userid' => $u2->id, 'status' => plan::STATUS_ACTIVE]);
$p2d = $ccg->create_plan(['userid' => $u2->id, 'status' => plan::STATUS_ACTIVE]);
api::complete_plan($p2d);

// The reviewer can review all plans waiting for review, or in review where they are the reviewer.
$this->setUser($reviewer);
$result = api::list_plans_to_review();
$this->assertEquals(3, $result['count']);
$this->assertEquals($p1a->get_id(), $result['plans'][0]->plan->get_id());
$this->assertEquals($p1b->get_id(), $result['plans'][1]->plan->get_id());
$this->assertEquals($p2a->get_id(), $result['plans'][2]->plan->get_id());

// The reviewer cannot view the plans when they do not have the permission in the user's context.
role_assign($roleprohibit, $reviewer->id, context_user::instance($u2->id)->id);
accesslib_clear_all_caches_for_unit_testing();
$result = api::list_plans_to_review();
$this->assertEquals(2, $result['count']);
$this->assertEquals($p1a->get_id(), $result['plans'][0]->plan->get_id());
$this->assertEquals($p1b->get_id(), $result['plans'][1]->plan->get_id());
}

public function test_list_user_competencies_to_review() {
$dg = $this->getDataGenerator();
$this->resetAfterTest();
$ccg = $dg->get_plugin_generator('core_competency');
$sysctx = context_system::instance();
$this->setAdminUser();

$reviewer = $dg->create_user();
$roleallow = $dg->create_role();
$roleprohibit = $dg->create_role();
assign_capability('moodle/competency:usercompetencyreview', CAP_ALLOW, $roleallow, $sysctx->id);
assign_capability('moodle/competency:usercompetencyreview', CAP_PROHIBIT, $roleprohibit, $sysctx->id);
role_assign($roleallow, $reviewer->id, $sysctx->id);
accesslib_clear_all_caches_for_unit_testing();

$u1 = $dg->create_user();
$u2 = $dg->create_user();
$f1 = $ccg->create_framework();
$c1 = $ccg->create_competency(['competencyframeworkid' => $f1->get_id()]);
$c2 = $ccg->create_competency(['competencyframeworkid' => $f1->get_id()]);
$c3 = $ccg->create_competency(['competencyframeworkid' => $f1->get_id()]);
$uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $c1->get_id(),
'status' => user_competency::STATUS_IDLE]);
$uc1b = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $c2->get_id(),
'status' => user_competency::STATUS_WAITING_FOR_REVIEW]);
$uc1c = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $c3->get_id(),
'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $reviewer->id]);
$uc2a = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $c1->get_id(),
'status' => user_competency::STATUS_WAITING_FOR_REVIEW]);
$uc2b = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $c2->get_id(),
'status' => user_competency::STATUS_IDLE]);
$uc2c = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $c3->get_id(),
'status' => user_competency::STATUS_IN_REVIEW]);

// The reviewer can review all plans waiting for review, or in review where they are the reviewer.
$this->setUser($reviewer);
$result = api::list_user_competencies_to_review();
$this->assertEquals(3, $result['count']);
$this->assertEquals($uc2a->get_id(), $result['competencies'][0]->usercompetency->get_id());
$this->assertEquals($uc1b->get_id(), $result['competencies'][1]->usercompetency->get_id());
$this->assertEquals($uc1c->get_id(), $result['competencies'][2]->usercompetency->get_id());

// The reviewer cannot view the plans when they do not have the permission in the user's context.
role_assign($roleprohibit, $reviewer->id, context_user::instance($u2->id)->id);
accesslib_clear_all_caches_for_unit_testing();
$result = api::list_user_competencies_to_review();
$this->assertEquals(2, $result['count']);
$this->assertEquals($uc1b->get_id(), $result['competencies'][0]->usercompetency->get_id());
$this->assertEquals($uc1c->get_id(), $result['competencies'][1]->usercompetency->get_id());
}
}
29 changes: 29 additions & 0 deletions competency/tests/persistent_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,35 @@ public function test_record_exists() {
$this->assertFalse(core_competency_testable_persistent::record_exists($id));
}

public function test_get_sql_fields() {
$expected = '' .
'c.id AS comp_id, ' .
'c.shortname AS comp_shortname, ' .
'c.idnumber AS comp_idnumber, ' .
'c.description AS comp_description, ' .
'c.descriptionformat AS comp_descriptionformat, ' .
'c.parentid AS comp_parentid, ' .
'c.path AS comp_path, ' .
'c.sortorder AS comp_sortorder, ' .
'c.competencyframeworkid AS comp_competencyframeworkid, ' .
'c.ruletype AS comp_ruletype, ' .
'c.ruleconfig AS comp_ruleconfig, ' .
'c.ruleoutcome AS comp_ruleoutcome, ' .
'c.scaleid AS comp_scaleid, ' .
'c.scaleconfiguration AS comp_scaleconfiguration, ' .
'c.timecreated AS comp_timecreated, ' .
'c.timemodified AS comp_timemodified, ' .
'c.usermodified AS comp_usermodified';
$this->assertEquals($expected, core_competency_testable_persistent::get_sql_fields('c', 'comp_'));
}

/**
* @expectedException coding_exception
* @expectedExceptionMessageRegExp /The alias .+ exceeds 30 characters/
*/
public function test_get_sql_fields_too_long() {
core_competency_testable_persistent::get_sql_fields('c');
}
}

/**
Expand Down

0 comments on commit 60e75de

Please sign in to comment.