MDL-50439 tool_lp: APIs to fetch plans and competencies to review
This commit is contained in:
@@ -1003,6 +1003,89 @@ class api {
|
||||
return $orderedusercompetencies;
|
||||
}
|
||||
|
||||
/**
|
||||
* List the user competencies to review.
|
||||
*
|
||||
* The method returns values in this format:
|
||||
*
|
||||
* array(
|
||||
* 'competencies' => array(
|
||||
* (stdClass)(
|
||||
* 'usercompetency' => (user_competency),
|
||||
* 'competency' => (competency),
|
||||
* 'user' => (user)
|
||||
* )
|
||||
* ),
|
||||
* 'count' => (int)
|
||||
* )
|
||||
*
|
||||
* @param int $skip The number of records to skip.
|
||||
* @param int $limit The number of results to return.
|
||||
* @param int $userid The user we're getting the competencies to review for.
|
||||
* @return array Containing the keys 'count', and 'competencies'. The 'competencies' key contains an object
|
||||
* which contains 'competency', 'usercompetency' and 'user'.
|
||||
*/
|
||||
public static function list_user_competencies_to_review($skip = 0, $limit = 50, $userid = null) {
|
||||
global $DB, $USER;
|
||||
if ($userid === null) {
|
||||
$userid = $USER->id;
|
||||
}
|
||||
|
||||
$capability = 'tool/lp:usercompetencyreview';
|
||||
$ucfields = user_competency::get_sql_fields('uc');
|
||||
$compfields = competency::get_sql_fields('c');
|
||||
$usercols = array('id') + get_user_fieldnames();
|
||||
$userfields = implode(',', array_map(create_function('$a', 'return "u.".$a." AS usr_".$a;'), $usercols));
|
||||
|
||||
$select = "SELECT $ucfields, $compfields, $userfields";
|
||||
$countselect = "SELECT COUNT('x')";
|
||||
$sql = " FROM {" . user_competency::TABLE . "} uc
|
||||
JOIN {" . competency::TABLE . "} c
|
||||
ON c.id = uc.competencyid
|
||||
JOIN {user} u
|
||||
ON u.id = uc.userid
|
||||
WHERE (uc.status = :waitingforreview
|
||||
OR (uc.status = :inreview AND uc.reviewerid = :reviewerid))";
|
||||
$ordersql = " ORDER BY c.shortname ASC";
|
||||
$params = array(
|
||||
'inreview' => user_competency::STATUS_IN_REVIEW,
|
||||
'reviewerid' => $userid,
|
||||
'waitingforreview' => user_competency::STATUS_WAITING_FOR_REVIEW,
|
||||
);
|
||||
$countsql = $countselect . $sql;
|
||||
|
||||
// Primary check to avoid the hard work of getting the users in which the user has permission.
|
||||
$count = $DB->count_records_sql($countselect . $sql, $params);
|
||||
if ($count < 1) {
|
||||
return array('count' => 0, 'competencies' => array());
|
||||
}
|
||||
|
||||
// TODO MDL-52243 Use core function.
|
||||
list($insql, $inparams) = external::filter_users_with_capability_on_user_context_sql(
|
||||
$capability, $userid, SQL_PARAMS_NAMED);
|
||||
$params += $inparams;
|
||||
$countsql = $countselect . $sql . " AND uc.userid $insql";
|
||||
$getsql = $select . $sql . " AND uc.userid $insql " . $ordersql;
|
||||
|
||||
// Extracting the results.
|
||||
$competencies = array();
|
||||
$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)),
|
||||
'user' => persistent::extract_record($record, 'usr_'),
|
||||
);
|
||||
$competencies[] = $objects;
|
||||
}
|
||||
$records->close();
|
||||
|
||||
return array(
|
||||
'count' => $DB->count_records_sql($countsql, $params),
|
||||
'competencies' => $competencies
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a competency to this course.
|
||||
*
|
||||
@@ -1733,6 +1816,94 @@ class api {
|
||||
return plan::get_records_select($select, $params, 'name ASC');
|
||||
}
|
||||
|
||||
/**
|
||||
* List the plans to review.
|
||||
*
|
||||
* The method returns values in this format:
|
||||
*
|
||||
* array(
|
||||
* 'plans' => array(
|
||||
* (stdClass)(
|
||||
* 'plan' => (plan),
|
||||
* 'template' => (template),
|
||||
* 'owner' => (stdClass)
|
||||
* )
|
||||
* ),
|
||||
* 'count' => (int)
|
||||
* )
|
||||
*
|
||||
* @param int $skip The number of records to skip.
|
||||
* @param int $limit The number of results to return.
|
||||
* @param int $userid The user we're getting the plans to review for.
|
||||
* @return array Containing the keys 'count', and 'plans'. The 'plans' key contains an object
|
||||
* which contains 'plan', 'template' and 'owner'.
|
||||
*/
|
||||
public static function list_plans_to_review($skip = 0, $limit = 100, $userid = null) {
|
||||
global $DB, $USER;
|
||||
|
||||
if ($userid === null) {
|
||||
$userid = $USER->id;
|
||||
}
|
||||
|
||||
$planfields = plan::get_sql_fields('p');
|
||||
$tplfields = template::get_sql_fields('t');
|
||||
$usercols = array('id') + get_user_fieldnames();
|
||||
$userfields = implode(',', array_map(create_function('$a', 'return "u.".$a." AS usr_".$a;'), $usercols));
|
||||
|
||||
$select = "SELECT $planfields, $tplfields, $userfields";
|
||||
$countselect = "SELECT COUNT('x')";
|
||||
|
||||
$sql = " FROM {" . plan::TABLE . "} p
|
||||
JOIN {user} u
|
||||
ON u.id = p.userid
|
||||
LEFT JOIN {" . template::TABLE . "} t
|
||||
ON t.id = p.templateid
|
||||
WHERE (p.status = :waitingforreview
|
||||
OR (p.status = :inreview AND p.reviewerid = :reviewerid))
|
||||
AND p.userid != :userid";
|
||||
|
||||
$params = array(
|
||||
'waitingforreview' => plan::STATUS_WAITING_FOR_REVIEW,
|
||||
'inreview' => plan::STATUS_IN_REVIEW,
|
||||
'reviewerid' => $userid,
|
||||
'userid' => $userid
|
||||
);
|
||||
|
||||
// Primary check to avoid the hard work of getting the users in which the user has permission.
|
||||
$count = $DB->count_records_sql($countselect . $sql, $params);
|
||||
if ($count < 1) {
|
||||
return array('count' => 0, 'plans' => array());
|
||||
}
|
||||
|
||||
// TODO MDL-52243 Use core function.
|
||||
list($insql, $inparams) = external::filter_users_with_capability_on_user_context_sql('tool/lp:planreview', $userid, SQL_PARAMS_NAMED);
|
||||
$sql .= " AND p.userid $insql";
|
||||
$params += $inparams;
|
||||
|
||||
$plans = array();
|
||||
$records = $DB->get_recordset_sql($select . $sql, $params, $skip, $limit);
|
||||
foreach ($records as $record) {
|
||||
$plan = new plan(0, plan::extract_record($record));
|
||||
$template = null;
|
||||
|
||||
if ($plan->is_based_on_template()) {
|
||||
$template = new template(0, template::extract_record($record));
|
||||
}
|
||||
|
||||
$plans[] = (object) array(
|
||||
'plan' => $plan,
|
||||
'template' => $template,
|
||||
'owner' => persistent::extract_record($record, 'usr_'),
|
||||
);
|
||||
}
|
||||
$records->close();
|
||||
|
||||
return array(
|
||||
'count' => $DB->count_records_sql($countselect . $sql, $params),
|
||||
'plans' => $plans
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a learning plan based on the provided data.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user