Course::class, 'foreign_key' => 'course_id' ]; $config['belongs_to']['tool'] = [ 'class_name' => LtiTool::class, 'foreign_key' => 'tool_id' ]; $config['has_many']['grades'] = [ 'class_name' => LtiGrade::class, 'assoc_foreign_key' => 'link_id', 'on_delete' => 'delete' ]; $config['registered_callbacks']['before_create'] = ['cbCalculatePosition']; parent::configure($config); } /** * Calculates the position of the new deployment in the course. */ public function cbCalculatePosition() : void { $this->position = self::countBySql( 'JOIN `lti_tools` ON `tool_id` = `lti_tools`.`id` WHERE `lti_tools`.`range_id` = :range_id', ['range_id' => $this->tool->range_id] ) + 1; } /** * Find a single entry by course_id and position. * * @return static|null */ public static function findByCourseAndPosition($course_id, $position) { return self::findOneBySQL('course_id = ? AND position = ?', [$course_id, $position]); } /** * Delete this entity. */ public function delete() { $db = DBManager::get(); $course_id = $this->course_id; $position = $this->position; if ($result = parent::delete()) { $db->execute('UPDATE `lti_deployments` SET `position` = position - 1 WHERE `course_id` = ? AND `position` > ?', [$course_id, $position]); } return $result; } public function getToolLtiVersion() : string { return $this->tool->lti_version ?? ''; } /** * Get the launch_url of this entry. */ public function getLaunchURL() { if (empty($this->tool->allow_custom_url) && empty($this->tool->deep_linking) || empty($this->launch_url)) { return $this->tool->launch_url ?? ''; } return $this->launch_url; } /** * Get the consumer_key of this entry. */ public function getConsumerKey() { return $this->tool->consumer_key ?? ''; } /** * Get the consumer_secret of this entry. */ public function getConsumerSecret() { return $this->tool->consumer_secret ?? ''; } /** * Get the oauth_signature_method of this entry. */ public function getOauthSignatureMethod() { return $this->tool->oauth_signature_method ?? 'sha1'; } /** * Get the custom_parameters of this entry. */ public function getCustomParameters() { $parameters = ''; if (!empty($this->tool->custom_parameters)) { $parameters .= $this->tool->custom_parameters . "\n"; } $parameters .= $this->options['custom_parameters'] ?? ''; return $parameters; } public function getCustomLtiParameterArray() : array { $parameter_str = $this->getCustomParameters(); if (empty($parameter_str)) { return []; } $parameters = explode("\n", $parameter_str); $array = []; foreach ($parameters as $parameter) { $key_value_parts = explode('=', $parameter, 2); if (count($key_value_parts) === 2) { $array[trim($key_value_parts[0])] = trim($key_value_parts[1]); } } return ['https://purl.imsglobal.org/spec/lti/claim/custom' => $array]; } /** * Get the send_lis_person attribute of this entry. */ public function getSendLisPerson() { return $this->tool->send_lis_person; } /** * Whether the LtiData instance uses its own (private) tool * or one of the globally defined LTI tools. * * @return bool True, if the LtiData instance uses its own tool, false otherwise. */ public function hasOwnTool() : bool { return $this->tool && !$this->tool->is_global; } }