Skip to content

Instantly share code, notes, and snippets.

@raztam
Created April 29, 2025 10:16
Show Gist options
  • Save raztam/3e4cf1712f27051669ae62f429ba9b60 to your computer and use it in GitHub Desktop.
Save raztam/3e4cf1712f27051669ae62f429ba9b60 to your computer and use it in GitHub Desktop.
/**
* Get activity id from moodl's module id
* @param int - activity id from moodle
* @return string - activity name from course sophia
*/
public function get_activity_id($moduleid): string {
global $DB;
$util = new utility();
// Get the modulename by its id.
$modulename = $DB->get_field('modules', 'name', ['id' => $moduleid]);
// Get the activity id in our table by its modulename (first one).
// This will get the default activity id for the module.
$activityid = $DB->get_field_sql(
"SELECT csid FROM {coursesophia_activity} WHERE modulename = ? ORDER BY csid ASC LIMIT 1",
[$modulename]
);
// If the activity doesn't exists in our table, add it to the table.
if (!$activityid) {
// Get the csid of the last record in the table.
$lastrecord = $DB->get_record_sql('SELECT * FROM {coursesophia_activity} ORDER BY csid DESC LIMIT 1');
$lastcsid = $lastrecord ? $lastrecord->csid + 1 : 0;
// Add the new activity to the table.
$newactivity = new stdClass();
$newactivity->id = $lastcsid;
$newactivity->name = $modulename;
$newactivity->modulename = $modulename;
$newactivity->type = 'NotSupported';
$newactivity->uniqueicon = 0;
$newactivity->csid = $lastcsid;
$util->copy_activity_logo($modulename);
return $DB->insert_record('coursesophia_activity', $newactivity, false);
}
return $activityid ?? 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment