Skip to content

Commit

Permalink
Update helper.php
Browse files Browse the repository at this point in the history
Fixes #2 - Fixes #11 by using core get_default() function for category
  • Loading branch information
lushonline committed Aug 13, 2020
1 parent 56396a7 commit 9b21296
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,37 +85,36 @@ public static function resolve_category_by_idnumber($idnumber) {
* @return int category ID.
*/
public static function resolve_category_by_id_or_idnumber($id) {
global $DB;
global $CFG, $DB;

// Handle null id by selecting the first non zero category id.
if (is_null($id)) {
try {
$gettop1categories = $DB->get_records('course_categories', null, 'id asc', '*', 0, 1);
if (count($gettop1categories) != 0) {
$firstcategory = array_pop($gettop1categories);
$id = $firstcategory->id;
}
if (method_exists('\core_course_category', 'create')) {
$id = core_course_category::get_default()->id;
return $id;
} else {
require_once($CFG->libdir . '/coursecatlib.php');
$id = coursecat::get_default()->id;
return $id;
} catch (Exception $e) {
return null;
}
return null;
}

// Handle numeric id by confirming it exists.
$params = array('id' => $id);
if (is_numeric($id)) {
if ($DB->record_exists('course_categories', $params)) {
return $id;
} else {
return null;
}
} else {
$params = array('idnumber' => $id);
try {
$id = $DB->get_field_select('course_categories', 'id', 'idnumber = :idnumber', $params, MUST_EXIST);
return $id;
} catch (Exception $e) {
return null;
}
return null;
}

// Handle any other id format by treating as a string idnumber value.
$params = array('idnumber' => $id);
if ($id = $DB->get_field_select('course_categories', 'id', 'idnumber = :idnumber', $params, MUST_EXIST)) {
return $id;
}
return null;
}

/**
Expand Down

0 comments on commit 9b21296

Please sign in to comment.