1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace enrol_lti\local\ltiadvantage\repository; 18 use enrol_lti\local\ltiadvantage\entity\deployment; 19 use enrol_lti\local\ltiadvantage\entity\resource_link; 20 21 /** 22 * Class resource_link_repository. 23 * 24 * This class encapsulates persistence logic for \enrol_lti\local\entity\resource_link type objects. 25 * 26 * @package enrol_lti 27 * @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com> 28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 29 */ 30 class resource_link_repository { 31 32 /** @var string the name of the table to which the entity will be persisted */ 33 private $table = 'enrol_lti_resource_link'; 34 35 /** @var string the name of the table to which user-entity mappings will have been persisted. */ 36 private $userresourcelinkmaptable = 'enrol_lti_user_resource_link'; 37 38 /** 39 * Convert a record into an object and return it. 40 * 41 * @param \stdClass $record the record from the store. 42 * @return resource_link a resource_link object. 43 */ 44 private function resource_link_from_record(\stdClass $record): resource_link { 45 $resourcelink = resource_link::create( 46 $record->resourcelinkid, 47 $record->ltideploymentid, 48 $record->resourceid, 49 $record->lticontextid, 50 $record->id 51 ); 52 53 if ($record->lineitemsservice || $record->lineitemservice) { 54 $scopes = []; 55 if ($record->lineitemscope) { 56 $lineitemscopes = json_decode($record->lineitemscope); 57 foreach ($lineitemscopes as $lineitemscope) { 58 $scopes[] = $lineitemscope; 59 } 60 } 61 if ($record->resultscope) { 62 $scopes[] = $record->resultscope; 63 } 64 if ($record->scorescope) { 65 $scopes[] = $record->scorescope; 66 } 67 $resourcelink->add_grade_service( 68 $record->lineitemsservice ? new \moodle_url($record->lineitemsservice) : null, 69 $record->lineitemservice ? new \moodle_url($record->lineitemservice) : null, 70 $scopes 71 ); 72 } 73 74 if ($record->contextmembershipsurl) { 75 $resourcelink->add_names_and_roles_service( 76 new \moodle_url($record->contextmembershipsurl), 77 json_decode($record->nrpsserviceversions) 78 ); 79 } 80 81 return $resourcelink; 82 } 83 84 /** 85 * Get a list of resource_link objects from a list of records. 86 * 87 * @param array $records the list of records to transform. 88 * @return array the array of resource_link instances. 89 */ 90 private function resource_links_from_records(array $records): array { 91 $resourcelinks = []; 92 foreach ($records as $record) { 93 $resourcelinks[] = $this->resource_link_from_record($record); 94 } 95 return $resourcelinks; 96 } 97 98 /** 99 * Get a stdClass object ready for persisting, based on the supplied resource_link object. 100 * 101 * @param resource_link $resourcelink the resource link instance. 102 * @return \stdClass the record. 103 */ 104 private function record_from_resource_link(resource_link $resourcelink): \stdClass { 105 106 $gradeservice = $resourcelink->get_grade_service(); 107 $nrpservice = $resourcelink->get_names_and_roles_service(); 108 109 $record = [ 110 'id' => $resourcelink->get_id(), 111 'resourcelinkid' => $resourcelink->get_resourcelinkid(), 112 'ltideploymentid' => $resourcelink->get_deploymentid(), 113 'resourceid' => $resourcelink->get_resourceid(), 114 'lticontextid' => $resourcelink->get_contextid(), 115 'lineitemsservice' => null, 116 'lineitemservice' => null, 117 'lineitemscope' => null, 118 'resultscope' => $gradeservice ? $gradeservice->get_resultscope() : null, 119 'scorescope' => $gradeservice ? $gradeservice->get_scorescope() : null, 120 'contextmembershipsurl' => $nrpservice ? $nrpservice->get_context_memberships_url()->out(false) : null, 121 'nrpsserviceversions' => $nrpservice ? json_encode($nrpservice->get_service_versions()) : null 122 ]; 123 124 if ($gradeservice && ($lineitemsurl = $gradeservice->get_lineitemsurl())) { 125 $record['lineitemsservice'] = $lineitemsurl->out(false); 126 } 127 if ($gradeservice && ($lineitemurl = $gradeservice->get_lineitemurl())) { 128 $record['lineitemservice'] = $lineitemurl->out(false); 129 } 130 if ($gradeservice && ($lineitemscopes = $gradeservice->get_lineitemscope())) { 131 $record['lineitemscope'] = json_encode($lineitemscopes); 132 } 133 134 return (object) $record; 135 } 136 137 /** 138 * Save a resource link instance in the store. 139 * 140 * @param resource_link $resourcelink the object to save. 141 * @return resource_link the saved object. 142 */ 143 public function save(resource_link $resourcelink): resource_link { 144 global $DB; 145 $id = $resourcelink->get_id(); 146 $exists = $id ? $this->exists($id) : false; 147 if ($id && !$exists) { 148 throw new \coding_exception("Cannot save resource_link with id '{$id}'. The record does not exist."); 149 } 150 151 $record = $this->record_from_resource_link($resourcelink); 152 $timenow = time(); 153 if ($exists) { 154 $record->timemodified = $timenow; 155 $DB->update_record($this->table, $record); 156 } else { 157 $record->timecreated = $record->timemodified = $timenow; 158 $id = $DB->insert_record($this->table, $record); 159 $record->id = $id; 160 } 161 162 return $this->resource_link_from_record($record); 163 } 164 165 /** 166 * Find and return a resource_link by id. 167 * 168 * @param int $id the id of the resource_link object. 169 * @return resource_link|null the resource_link object, or null if the object cannot be found. 170 */ 171 public function find(int $id): ?resource_link { 172 global $DB; 173 try { 174 $record = $DB->get_record($this->table, ['id' => $id], '*', MUST_EXIST); 175 return $this->resource_link_from_record($record); 176 } catch (\dml_missing_record_exception $ex) { 177 return null; 178 } 179 } 180 181 /** 182 * Get a resource by id, within a given tool deployment. 183 * 184 * @param deployment $deployment the deployment instance. 185 * @param string $resourcelinkid the resourcelinkid from the platform. 186 * @return resource_link|null the resource link instance, or null if not found. 187 */ 188 public function find_by_deployment(deployment $deployment, string $resourcelinkid): ?resource_link { 189 global $DB; 190 try { 191 $record = $DB->get_record($this->table, ['ltideploymentid' => $deployment->get_id(), 192 'resourcelinkid' => $resourcelinkid], '*', MUST_EXIST); 193 return $this->resource_link_from_record($record); 194 } catch (\dml_missing_record_exception $ex) { 195 return null; 196 } 197 } 198 199 /** 200 * Find resource_link objects based on the resource and a given launching user. 201 * 202 * @param int $resourceid the local id of the resource (enrol_lti_tools id) 203 * @param int $userid the local id of the enrol_lti\local\ltiadvantage\user object 204 * @return array an array of resource_links 205 */ 206 public function find_by_resource_and_user(int $resourceid, int $userid): array { 207 global $DB; 208 $sql = "SELECT r.id, r.resourcelinkid, r.resourceid, r.ltideploymentid, r.lticontextid, r.lineitemsservice, 209 r.lineitemservice, r.lineitemscope, r.resultscope, r.scorescope, r.contextmembershipsurl, 210 r.nrpsserviceversions, r.timecreated, r.timemodified 211 FROM {enrol_lti_resource_link} r 212 JOIN {enrol_lti_user_resource_link} ur 213 ON (r.id = ur.resourcelinkid) 214 WHERE ur.ltiuserid = :ltiuserid 215 AND r.resourceid = :resourceid"; 216 $records = $DB->get_records_sql($sql, ['ltiuserid' => $userid, 'resourceid' => $resourceid]); 217 return $this->resource_links_from_records($records); 218 } 219 220 /** 221 * Gets all mapped resource links for a given resource. 222 * 223 * @param int $resourceid the local id of the shared resource. 224 * @return array the array of resource_link instances. 225 */ 226 public function find_by_resource(int $resourceid): array { 227 global $DB; 228 $records = $DB->get_records($this->table, ['resourceid' => $resourceid]); 229 return $this->resource_links_from_records($records); 230 } 231 232 /** 233 * Check whether or not the given resource_link object exists. 234 * 235 * @param int $id the unique id the resource_link. 236 * @return bool true if found, false otherwise. 237 */ 238 public function exists(int $id): bool { 239 global $DB; 240 return $DB->record_exists($this->table, ['id' => $id]); 241 } 242 243 /** 244 * Delete a resource_link based on id. 245 * 246 * @param int $id the id of the resource_link to remove. 247 */ 248 public function delete(int $id) { 249 global $DB; 250 // First remove all enrol_lti_user_resource_link mappings. 251 $DB->delete_records($this->userresourcelinkmaptable, ['resourcelinkid' => $id]); 252 253 // And the resource_link itself. 254 $DB->delete_records($this->table, ['id' => $id]); 255 } 256 257 /** 258 * Delete all resource links for a given deployment, as well as any mappings between users and the respective links. 259 * 260 * @param int $deploymentid the id of the deployment instance. 261 */ 262 public function delete_by_deployment(int $deploymentid): void { 263 global $DB; 264 265 // First remove all enrol_lti_user_resource_link mappings. 266 $DB->delete_records_select( 267 $this->userresourcelinkmaptable, 268 "resourcelinkid IN (SELECT id FROM {{$this->table}} WHERE ltideploymentid = :ltideploymentid)", 269 ['ltideploymentid' => $deploymentid] 270 ); 271 272 // And remove the resource_link entries themselves. 273 $DB->delete_records($this->table, ['ltideploymentid' => $deploymentid]); 274 } 275 276 /** 277 * Delete all resource_link instances referring to the resource identified by $resourceid. 278 * 279 * @param int $resourceid the id of the published resource. 280 */ 281 public function delete_by_resource(int $resourceid) { 282 global $DB; 283 284 // First remove all enrol_lti_user_resource_link mappings. 285 $DB->delete_records_select( 286 $this->userresourcelinkmaptable, 287 "resourcelinkid IN (SELECT id FROM {{$this->table}} WHERE resourceid = :resourceid)", 288 ['resourceid' => $resourceid] 289 ); 290 291 // And remove the resource_link entries themselves. 292 $DB->delete_records($this->table, ['resourceid' => $resourceid]); 293 } 294 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body