Wrote the custom external functions, just in case anyone needs...
/* GENERATE LTI CODE FOR THE COURSE */
public static function add_lti_course_code_parameters() {
return new external_function_parameters(
array(
'schoolname' => new external_value(PARAM_TEXT, 'The School Name', VALUE_REQUIRED),
'courseid' => new external_value(PARAM_INT, 'The ID of the Course', VALUE_REQUIRED),
'startdate' => new external_value(PARAM_INT, 'The startdate (timestamp) of the new Course', VALUE_REQUIRED),
'enddate' => new external_value(PARAM_INT, 'The enddate (timestamp) of the new Course', VALUE_REQUIRED)
)
);
}
public static function add_lti_course_code($schoolname='',$courseid=0,$startdate=0,$enddate=0) {
global $CFG, $USER, $DB;
require_once($CFG->dirroot . '/enrol/lti/lib.php');
$params = self::validate_parameters(self::add_lti_course_code_parameters(),
array(
'schoolname' => $schoolname,
'courseid'=> $courseid,
'startdate'=> $startdate,
'enddate' => $enddate
)
);
//validate course exists ...
if (!($course = $DB->get_record('course', array('id'=>$params['courseid'])))) {
throw new moodle_exception('invalidcourseid', 'error');
}
$context = context_course::instance($courseid);
$plugin = enrol_get_plugin('lti');
$data = new stdClass();
$toolname = $course->fullname . ' :: ' . $params['schoolname'];
$data->name = $toolname; //$course->fullname . ' :: ' . $schoolname;
$data->contextid = $context->id;
$data->enrolperiod = 0;
$data->enrolstartdate = $params['startdate'];
$data->enrolenddate = $params['enddate'];
$data->maxenrolled = 0;
$data->roleinstructor = 3;
$data->rolelearner = 5;
$data->secret = random_string(32);
$data->gradesync = 1;
$data->gradesynccompletion = 0;
$data->membersync = 1;
$data->membersyncmode = 1;
$data->maildisplay = 2;
$data->city = '';
$data->country = '';
$data->timezone = 99;
$data->lang = 'en';
$data->institution = '';
$data->id = 0;
$data->courseid = $params['courseid'];
$data->type = 'lti';
$fields = (array) $data;
$result = $plugin->add_instance($course, $fields);
if(!isset($result) || $result <= 0){
throw new moodle_exception('The creation of ' . $toolname . ' failed', 'error');
}
try {
$thetool = $DB->get_record('enrol_lti_tools', array('enrolid'=>$result));
} catch (\Throwable $th) {
throw $th;
}
return array('id' => $thetool->id,
'secret' => $thetool->secret,
'toolname' => $toolname );
}
public static function add_lti_course_code_returns() {
return new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'new course id'),
'secret' => new external_value(PARAM_TEXT, 'LTI secret'),
'toolname' => new external_value(PARAM_TEXT, 'New ToolName')
)
);
}
/* GENERATE LTI CODE FOR THE COURSE */
public static function add_lti_course_code_parameters() {
return new external_function_parameters(
array(
'schoolname' => new external_value(PARAM_TEXT, 'The School Name', VALUE_REQUIRED),
'courseid' => new external_value(PARAM_INT, 'The ID of the Course', VALUE_REQUIRED),
'startdate' => new external_value(PARAM_INT, 'The startdate (timestamp) of the new Course', VALUE_REQUIRED),
'enddate' => new external_value(PARAM_INT, 'The enddate (timestamp) of the new Course', VALUE_REQUIRED)
)
);
}
public static function add_lti_course_code($schoolname='',$courseid=0,$startdate=0,$enddate=0) {
global $CFG, $USER, $DB;
require_once($CFG->dirroot . '/enrol/lti/lib.php');
$params = self::validate_parameters(self::add_lti_course_code_parameters(),
array(
'schoolname' => $schoolname,
'courseid'=> $courseid,
'startdate'=> $startdate,
'enddate' => $enddate
)
);
//validate course exists ...
if (!($course = $DB->get_record('course', array('id'=>$params['courseid'])))) {
throw new moodle_exception('invalidcourseid', 'error');
}
$context = context_course::instance($courseid);
$plugin = enrol_get_plugin('lti');
$data = new stdClass();
$toolname = $course->fullname . ' :: ' . $params['schoolname'];
$data->name = $toolname; //$course->fullname . ' :: ' . $schoolname;
$data->contextid = $context->id;
$data->enrolperiod = 0;
$data->enrolstartdate = $params['startdate'];
$data->enrolenddate = $params['enddate'];
$data->maxenrolled = 0;
$data->roleinstructor = 3;
$data->rolelearner = 5;
$data->secret = random_string(32);
$data->gradesync = 1;
$data->gradesynccompletion = 0;
$data->membersync = 1;
$data->membersyncmode = 1;
$data->maildisplay = 2;
$data->city = '';
$data->country = '';
$data->timezone = 99;
$data->lang = 'en';
$data->institution = '';
$data->id = 0;
$data->courseid = $params['courseid'];
$data->type = 'lti';
$fields = (array) $data;
$result = $plugin->add_instance($course, $fields);
if(!isset($result) || $result <= 0){
throw new moodle_exception('The creation of ' . $toolname . ' failed', 'error');
}
try {
$thetool = $DB->get_record('enrol_lti_tools', array('enrolid'=>$result));
} catch (\Throwable $th) {
throw $th;
}
return array('id' => $thetool->id,
'secret' => $thetool->secret,
'toolname' => $toolname );
}
public static function add_lti_course_code_returns() {
return new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'new course id'),
'secret' => new external_value(PARAM_TEXT, 'LTI secret'),
'toolname' => new external_value(PARAM_TEXT, 'New ToolName')
)
);
}