Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   1  <?php
   2  
   3  namespace IMSGlobal\LTI\ToolProvider\DataConnector;
   4  
   5  use IMSGlobal\LTI\ToolProvider;
   6  use IMSGlobal\LTI\ToolProvider\Context;
   7  use IMSGlobal\LTI\ToolProvider\ToolConsumer;
   8  use PDO;
   9  
  10  /**
  11   * Class to represent an LTI Data Connector for PDO variations for SQLite connections
  12   *
  13   * @author  Stephen P Vickers <svickers@imsglobal.org>
  14   * @copyright  IMS Global Learning Consortium Inc
  15   * @date  2016
  16   * @version 3.0.0
  17   * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  18   */
  19  
  20  
  21  #[\AllowDynamicProperties]
  22  class DataConnector_pdo_sqlite extends DataConnector_pdo
  23  {
  24  
  25  ###
  26  ###  ToolConsumer methods
  27  ###
  28  
  29  /**
  30   * Delete tool consumer object.
  31   *
  32   * @param ToolConsumer $consumer Consumer object
  33   *
  34   * @return boolean True if the tool consumer object was successfully deleted
  35   */
  36      public function deleteToolConsumer($consumer)
  37      {
  38  
  39          $id = $consumer->getRecordId();
  40  
  41  // Delete any nonce values for this consumer
  42          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::NONCE_TABLE_NAME . ' WHERE consumer_pk = :id';
  43          $query = $this->db->prepare($sql);
  44          $query->bindValue('id', $id, PDO::PARAM_INT);
  45          $query->execute();
  46  
  47  // Delete any outstanding share keys for resource links for this consumer
  48          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
  49                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
  50                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . '.resource_link_pk = rl.resource_link_pk) AND (rl.consumer_pk = :id))';
  51          $query = $this->db->prepare($sql);
  52          $query->bindValue('id', $id, PDO::PARAM_INT);
  53          $query->execute();
  54  
  55  // Delete any outstanding share keys for resource links for contexts in this consumer
  56          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
  57                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
  58                 "INNER JOIN {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME . ' c ON rl.context_pk = c.context_pk ' .
  59                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . '.resource_link_pk = rl.resource_link_pk) AND (c.consumer_pk = :id))';
  60          $query = $this->db->prepare($sql);
  61          $query->bindValue('id', $id, PDO::PARAM_INT);
  62          $query->execute();
  63  
  64  // Delete any users in resource links for this consumer
  65          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . ' ' .
  66                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
  67                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . '.resource_link_pk = rl.resource_link_pk) AND (rl.consumer_pk = :id))';
  68          $query = $this->db->prepare($sql);
  69          $query->bindValue('id', $id, PDO::PARAM_INT);
  70          $query->execute();
  71  
  72  // Delete any users in resource links for contexts in this consumer
  73          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . ' ' .
  74                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
  75                 "INNER JOIN {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME . ' c ON rl.context_pk = c.context_pk ' .
  76                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . '.resource_link_pk = rl.resource_link_pk) AND (c.consumer_pk = :id))';
  77          $query = $this->db->prepare($sql);
  78          $query->bindValue('id', $id, PDO::PARAM_INT);
  79          $query->execute();
  80  
  81  // Update any resource links for which this consumer is acting as a primary resource link
  82          $sql = "UPDATE {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' .
  83                 'SET primary_resource_link_pk = NULL, share_approved = NULL ' .
  84                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
  85                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . '.primary_resource_link_pk = rl.resource_link_pk) AND (rl.consumer_pk = :id))';
  86          $query = $this->db->prepare($sql);
  87          $query->bindValue('id', $id, PDO::PARAM_INT);
  88          $query->execute();
  89  
  90  // Update any resource links for contexts in which this consumer is acting as a primary resource link
  91          $sql = "UPDATE {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' .
  92                 'SET primary_resource_link_pk = NULL, share_approved = NULL ' .
  93                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
  94                 "INNER JOIN {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME . ' c ON rl.context_pk = c.context_pk ' .
  95                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . '.primary_resource_link_pk = rl.resource_link_pk) AND (c.consumer_pk = :id))';
  96          $query = $this->db->prepare($sql);
  97          $query->bindValue('id', $id, PDO::PARAM_INT);
  98          $query->execute();
  99  
 100  // Delete any resource links for this consumer
 101          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' .
 102                 'WHERE consumer_pk = :id';
 103          $query = $this->db->prepare($sql);
 104          $query->bindValue('id', $id, PDO::PARAM_INT);
 105          $query->execute();
 106  
 107  // Delete any resource links for contexts in this consumer
 108          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' .
 109                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME . ' c ' .
 110                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . '.context_pk = c.context_pk) AND (c.consumer_pk = :id))';
 111          $query = $this->db->prepare($sql);
 112          $query->bindValue('id', $id, PDO::PARAM_INT);
 113          $query->execute();
 114  
 115  // Delete any contexts for this consumer
 116          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME . ' ' .
 117                 'WHERE consumer_pk = :id';
 118          $query = $this->db->prepare($sql);
 119          $query->bindValue('id', $id, PDO::PARAM_INT);
 120          $query->execute();
 121  
 122  // Delete consumer
 123          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::CONSUMER_TABLE_NAME . ' ' .
 124                 'WHERE consumer_pk = :id';
 125          $query = $this->db->prepare($sql);
 126          $query->bindValue('id', $id, PDO::PARAM_INT);
 127          $ok = $query->execute();
 128  
 129          if ($ok) {
 130              $consumer->initialize();
 131          }
 132  
 133          return $ok;
 134  
 135      }
 136  
 137  ###
 138  ###  Context methods
 139  ###
 140  
 141  /**
 142   * Delete context object.
 143   *
 144   * @param Context $context Context object
 145   *
 146   * @return boolean True if the Context object was successfully deleted
 147   */
 148      public function deleteContext($context)
 149      {
 150  
 151          $id = $context->getRecordId();
 152  
 153  // Delete any outstanding share keys for resource links for this context
 154          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
 155                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
 156                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . '.resource_link_pk = rl.resource_link_pk) AND (rl.context_pk = :id))';
 157          $query = $this->db->prepare($sql);
 158          $query->bindValue('id', $id, PDO::PARAM_INT);
 159          $query->execute();
 160  
 161  // Delete any users in resource links for this context
 162          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . ' ' .
 163                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
 164                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . '.resource_link_pk = rl.resource_link_pk) AND (rl.context_pk = :id))';
 165          $query = $this->db->prepare($sql);
 166          $query->bindValue('id', $id, PDO::PARAM_INT);
 167          $query->execute();
 168  
 169  // Update any resource links for which this consumer is acting as a primary resource link
 170          $sql = "UPDATE {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' .
 171                 'SET primary_resource_link_pk = null, share_approved = null ' .
 172                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
 173                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . '.primary_resource_link_pk = rl.resource_link_pk) AND (rl.context_pk = :id))';
 174          $query = $this->db->prepare($sql);
 175          $query->bindValue('id', $id, PDO::PARAM_INT);
 176          $query->execute();
 177  
 178  // Delete any resource links for this consumer
 179          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' .
 180                 'WHERE context_pk = :id';
 181          $query = $this->db->prepare($sql);
 182          $query->bindValue('id', $id, PDO::PARAM_INT);
 183          $query->execute();
 184  
 185  // Delete context
 186          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME . ' ' .
 187                 'WHERE context_pk = :id';
 188          $query = $this->db->prepare($sql);
 189          $query->bindValue('id', $id, PDO::PARAM_INT);
 190          $ok = $query->execute();
 191  
 192          if ($ok) {
 193              $context->initialize();
 194          }
 195  
 196          return $ok;
 197  
 198      }
 199  
 200  }