Skip to content

Commit

Permalink
Merge branch 'MDL-54997-master' of git://github.com/damyon/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Jun 27, 2016
2 parents 74eb5bb + e824cd5 commit f8ca380
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 6 deletions.
9 changes: 3 additions & 6 deletions competency/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -4152,7 +4152,7 @@ public static function list_evidence($userid = 0, $competencyid = 0, $planid = 0
* @param int $limit Number of records to return.
* @return \core_competency\evidence[]
*/
public static function list_evidence_in_course($userid = 0, $courseid = 0, $competencyid = 0, $sort = 'timecreated',
public static function list_evidence_in_course($userid = 0, $courseid = 0, $competencyid = 0, $sort = 'timecreated, id',
$order = 'DESC', $skip = 0, $limit = 0) {
static::require_enabled();

Expand All @@ -4166,11 +4166,8 @@ public static function list_evidence_in_course($userid = 0, $courseid = 0, $comp
return array();
}

$params = array(
'usercompetencyid' => $usercompetency->get_id(),
'contextid' => context_course::instance($courseid)->id
);
return evidence::get_records($params, $sort, $order, $skip, $limit);
$context = context_course::instance($courseid);
return evidence::get_records_for_usercompetency($usercompetency->get_id(), $context, $sort, $order, $skip, $limit);
}

/**
Expand Down
48 changes: 48 additions & 0 deletions competency/classes/evidence.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,52 @@ public static function can_delete_user($userid) {
return has_capability('moodle/competency:evidencedelete', context_user::instance($userid));
}

/**
* Load a list of records in a context for a user competency.
*
* @param int $usercompetencyid The id of the user competency.
* @param context $context Context to filter the evidence list.
* @param string $sort The field from the evidence table to sort on.
* @param string $order The sort direction
* @param int $skip Limitstart.
* @param int $limit Number of rows to return.
*
* @return \core_competency\persistent[]
*/
public static function get_records_for_usercompetency($usercompetencyid,
\context $context,
$sort = '',
$order = 'ASC',
$skip = 0,
$limit = 0) {
global $DB;

$params = array(
'usercompid' => $usercompetencyid,
'path' => $context->path . '/%',
'contextid' => $context->id
);

if (!empty($sort)) {
$sortcolumns = explode(',', $sort);
$sortcolumns = array_map('trim', $sortcolumns);
$sort = ' ORDER BY e.' . implode(', e.', $sortcolumns) . ' ' . $order;
}

$sql = 'SELECT e.*
FROM {' . static::TABLE . '} e
JOIN {context} c ON c.id = e.contextid
WHERE (c.path LIKE :path OR c.id = :contextid)
AND e.usercompetencyid = :usercompid
' . $sort;
$records = $DB->get_records_sql($sql, $params, $skip, $limit);
$instances = array();

foreach ($records as $record) {
$newrecord = new static(0, $record);
array_push($instances, $newrecord);
}
return $instances;
}

}
41 changes: 41 additions & 0 deletions competency/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2579,6 +2579,47 @@ public function test_observe_course_completed() {
$this->assertEquals(null, $ev4->get_actionuserid());
}

public function test_list_evidence_in_course() {
global $SITE;

$this->resetAfterTest(true);
$dg = $this->getDataGenerator();
$lpg = $dg->get_plugin_generator('core_competency');
$u1 = $dg->create_user();
$course = $dg->create_course();
$coursecontext = context_course::instance($course->id);

$this->setAdminUser();
$f = $lpg->create_framework();
$c = $lpg->create_competency(array('competencyframeworkid' => $f->get_id()));
$c2 = $lpg->create_competency(array('competencyframeworkid' => $f->get_id()));
$cc = api::add_competency_to_course($course->id, $c->get_id());
$cc2 = api::add_competency_to_course($course->id, $c2->get_id());

$pagegenerator = $this->getDataGenerator()->get_plugin_generator('mod_page');
$page = $pagegenerator->create_instance(array('course' => $course->id));

$cm = get_coursemodule_from_instance('page', $page->id);
$cmcontext = context_module::instance($cm->id);
// Add the competency to the course module.
$ccm = api::add_competency_to_course_module($cm, $c->get_id());

// Now add the evidence to the course.
$evidence1 = api::add_evidence($u1->id, $c->get_id(), $coursecontext->id, \core_competency\evidence::ACTION_LOG,
'invaliddata', 'error');

$result = api::list_evidence_in_course($u1->id, $course->id, $c->get_id());
$this->assertEquals($result[0]->get_id(), $evidence1->get_id());

// Now add the evidence to the course module.
$evidence2 = api::add_evidence($u1->id, $c->get_id(), $cmcontext->id, \core_competency\evidence::ACTION_LOG,
'invaliddata', 'error');

$result = api::list_evidence_in_course($u1->id, $course->id, $c->get_id());
$this->assertEquals($result[0]->get_id(), $evidence2->get_id());
$this->assertEquals($result[1]->get_id(), $evidence1->get_id());
}

public function test_list_course_modules_using_competency() {
global $SITE;

Expand Down

0 comments on commit f8ca380

Please sign in to comment.