Skip to content
Permalink
Browse files

mod_aspirelist: Enable per list knowledge group config via custom table

  • Loading branch information...
tonyjbutler committed Oct 25, 2016
1 parent 5d16783 commit 16a70a27d5bc7a9ee0393e336c6d3f9c50b109ec
Showing with 78 additions and 23 deletions.
  1. +4 −2 lang/en/aspirelist.php
  2. +72 −21 locallib.php
  3. +2 −0 settings.php
@@ -48,7 +48,8 @@
$string['configcoursecolumn'] = 'Optional. The name of the column in the custom table containing the course attribute that is mapped against your Talis Aspire list codes.';
$string['configdefaultdisplay'] = 'By default, should new resource lists be displayed on a separate page via a link, or inline on the course page?';
$string['configincludechildcodes'] = 'Check for course meta links and include the Aspire list codes associated with any child courses found.';
$string['configknowledgegroup'] = 'Select the target knowledge group for your Talis Aspire lists.';
$string['configkgcolumn'] = 'Optional. The name of the column in the custom table containing the knowledge group (if left blank the default knowledge group configured in the Talis Aspire site settings will be used for all lists).';
$string['configknowledgegroup'] = 'Select the default target knowledge group for your Talis Aspire lists. This can be overridden per list if using a custom database table for your Aspire code source (configured below).';
$string['configrequiremodintro'] = 'Enable this option if you want to force users to enter a description for each resource list.';
$string['configyearregex'] = 'Optional. This is a regular expression (including delimiters) matching the year code part of your course ID numbers or short names (both will be checked), and can be used irrespective of the source of the Aspire list codes. If no regex is provided here it will be assumed that there is no year code, and the latest version of each list will be used.';
$string['contentheader'] = 'Resource list selection';
@@ -74,7 +75,8 @@
$string['institution'] = 'Institution';
$string['itemcount'] = '{$a} item';
$string['itemcountplural'] = '{$a} items';
$string['knowledgegroup'] = 'Aspire knowledge group';
$string['kgcolumn'] = 'Knowledge group column';
$string['knowledgegroup'] = 'Default knowledge group';
$string['level'] = 'Level';
$string['modulename'] = 'Aspire resource list';
$string['modulename_help'] = '<p>The Aspire resource list module enables a teacher to include a selection of resources from associated Talis Aspire resource lists directly within the content of their course.</p><p>The resource list can be displayed either in a separate, linked page, or embedded in the course page itself (hidden initially, with a link to toggle visibility).</p>';
@@ -423,29 +423,22 @@ private function get_codes($course, $child = false) {
$coursecolumn = $adminconfig->coursecolumn;
$courseattribute = $course->{$adminconfig->courseattribute};
if (!$codes = $DB->get_records($codetable, array($coursecolumn => $courseattribute), null, 'id, ' . $codecolumn)) {
$codes = array();
$codes = array();
if ($records = $DB->get_records($codetable, array($coursecolumn => $courseattribute))) {
foreach ($records as $index => $record) {
$codes[$index]['code'] = $record->$codecolumn;
if ($kgcolumn = $adminconfig->kgcolumn and !empty($record->$kgcolumn)) {
$codes[$index]['kg'] = $record->$kgcolumn;
}
}
}
$codes = array_map(create_function('$code', 'return $code->' . $codecolumn . ';'), $codes);
} else if ($adminconfig->codesource == 'shortname') {
$codes = $this->extract_codes($course->shortname);
}
// Try ID number as fallback if no code found in code table, regardless of code source specified in admin config.
// Try ID number as fallback if no code found so far, regardless of code source specified in admin config.
if ($adminconfig->codesource == 'idnumber' || empty($codes)) {
if ($coderegex = $adminconfig->coderegex) {
preg_match_all($coderegex, $course->idnumber, $codes, PREG_PATTERN_ORDER);
$codes = (!empty($codes[1])) ? $codes[1] : $codes[0];
} else {
$codes = array($course->idnumber);
}
}
if ($adminconfig->codesource == 'shortname') {
if ($coderegex = $adminconfig->coderegex) {
preg_match_all($coderegex, $course->shortname, $codes, PREG_PATTERN_ORDER);
$codes = (!empty($codes[1])) ? $codes[1] : $codes[0];
} else {
$codes = array($course->shortname);
}
$codes = $this->extract_codes($course->idnumber);
}
// Check for additional codes in meta child courses (if enabled in site config).
@@ -457,7 +450,27 @@ private function get_codes($course, $child = false) {
}
}
return array_unique(array_filter($codes));
return array_filter($codes);
}
/**
* Extract one or more Talis Aspire list codes from a given source string.
*
* @param string $source A string containing one or more codes
* @return array An array of Aspire list codes
*/
private function extract_codes($source) {
$adminconfig = $this->get_admin_config();
if ($coderegex = $adminconfig->coderegex) {
preg_match_all($coderegex, $source, $codes, PREG_PATTERN_ORDER);
$codes = (!empty($codes[1])) ? $codes[1] : $codes[0];
} else {
$codes = array($source);
}
$codes = array_unique($codes);
return array_map(create_function('$code', 'return array(\'code\' => $code);'), $codes);
}
/**
@@ -482,6 +495,42 @@ private function get_child_courses($courseid) {
return $childcourses;
}
/**
* Return the pluralised version of a given knowledge group name (as used in Aspire URLs),
* or that of the admin configured default knowledge group if not valid.
*
* @param string $name Official name of an Aspire knowledge group
* @return string Pluralised name as used in Aspire URLs
*/
private function get_url_knowledge_group($name) {
$adminconfig = $this->get_admin_config();
$name = strtolower($name);
$knowledgegroups = array(
'center' => 'centers',
'college' => 'colleges',
'course' => 'courses',
'department' => 'departments',
'division' => 'divisions',
'faculty' => 'faculties',
'field' => 'fields',
'institute' => 'institutes',
'institution' => 'institutions',
'level' => 'levels',
'module' => 'modules',
'pathway' => 'pathways',
'programme' => 'programmes',
'school' => 'schools',
'subject' => 'subjects',
'unit' => 'units'
);
if (!in_array($name, array_keys($knowledgegroups))) {
return $adminconfig->knowledgegroup;
}
return $knowledgegroups[$name];
}
/**
* Fetch all Talis Aspire lists associated with the current course (and year if applicable).
*
@@ -503,8 +552,10 @@ public function get_lists($course) {
$lists = array();
foreach ($codes as $code) {
$knowledgegroup = isset($code['kg']) ? $this->get_url_knowledge_group($code['kg']) : $adminconfig->knowledgegroup;
// Build the URL for the JSON request.
$codedata = $adminconfig->aspireurl . '/' . $adminconfig->knowledgegroup . '/' . strtolower($code);
$codedata = $adminconfig->aspireurl . '/' . $knowledgegroup . '/' . strtolower($code['code']);
if ($year) {
$url = $codedata . '/lists/' . $year . '.json';
} else {
@@ -99,4 +99,6 @@
get_string('configcoursecolumn', 'aspirelist'), '', PARAM_TEXT));
$settings->add(new aspirelist_courseattribute_setting('aspirelist/courseattribute',
get_string('courseattribute', 'aspirelist'), get_string('configcourseattribute', 'aspirelist'), '', PARAM_TEXT));
$settings->add(new admin_setting_configtext('aspirelist/kgcolumn', get_string('kgcolumn', 'aspirelist'),
get_string('configkgcolumn', 'aspirelist'), '', PARAM_TEXT));
}

0 comments on commit 16a70a2

Please sign in to comment.
You can’t perform that action at this time.