Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 311 and 403]

   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  class DataConnector_pdo_sqlite extends DataConnector_pdo
  22  {
  23  
  24  ###
  25  ###  ToolConsumer methods
  26  ###
  27  
  28  /**
  29   * Delete tool consumer object.
  30   *
  31   * @param ToolConsumer $consumer Consumer object
  32   *
  33   * @return boolean True if the tool consumer object was successfully deleted
  34   */
  35      public function deleteToolConsumer($consumer)
  36      {
  37  
  38          $id = $consumer->getRecordId();
  39  
  40  // Delete any nonce values for this consumer
  41          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::NONCE_TABLE_NAME . ' WHERE consumer_pk = :id';
  42          $query = $this->db->prepare($sql);
  43          $query->bindValue('id', $id, PDO::PARAM_INT);
  44          $query->execute();
  45  
  46  // Delete any outstanding share keys for resource links for this consumer
  47          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
  48                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
  49                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . '.resource_link_pk = rl.resource_link_pk) AND (rl.consumer_pk = :id))';
  50          $query = $this->db->prepare($sql);
  51          $query->bindValue('id', $id, PDO::PARAM_INT);
  52          $query->execute();
  53  
  54  // Delete any outstanding share keys for resource links for contexts in this consumer
  55          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
  56                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
  57                 "INNER JOIN {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME . ' c ON rl.context_pk = c.context_pk ' .
  58                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . '.resource_link_pk = rl.resource_link_pk) AND (c.consumer_pk = :id))';
  59          $query = $this->db->prepare($sql);
  60          $query->bindValue('id', $id, PDO::PARAM_INT);
  61          $query->execute();
  62  
  63  // Delete any users in resource links for this consumer
  64          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . ' ' .
  65                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
  66                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . '.resource_link_pk = rl.resource_link_pk) AND (rl.consumer_pk = :id))';
  67          $query = $this->db->prepare($sql);
  68          $query->bindValue('id', $id, PDO::PARAM_INT);
  69          $query->execute();
  70  
  71  // Delete any users in resource links for contexts in this consumer
  72          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . ' ' .
  73                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
  74                 "INNER JOIN {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME . ' c ON rl.context_pk = c.context_pk ' .
  75                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . '.resource_link_pk = rl.resource_link_pk) AND (c.consumer_pk = :id))';
  76          $query = $this->db->prepare($sql);
  77          $query->bindValue('id', $id, PDO::PARAM_INT);
  78          $query->execute();
  79  
  80  // Update any resource links for which this consumer is acting as a primary resource link
  81          $sql = "UPDATE {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' .
  82                 'SET primary_resource_link_pk = NULL, share_approved = NULL ' .
  83                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
  84                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . '.primary_resource_link_pk = rl.resource_link_pk) AND (rl.consumer_pk = :id))';
  85          $query = $this->db->prepare($sql);
  86          $query->bindValue('id', $id, PDO::PARAM_INT);
  87          $query->execute();
  88  
  89  // Update any resource links for contexts in which this consumer is acting as a primary resource link
  90          $sql = "UPDATE {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' .
  91                 'SET primary_resource_link_pk = NULL, share_approved = NULL ' .
  92                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
  93                 "INNER JOIN {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME . ' c ON rl.context_pk = c.context_pk ' .
  94                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . '.primary_resource_link_pk = rl.resource_link_pk) AND (c.consumer_pk = :id))';
  95          $query = $this->db->prepare($sql);
  96          $query->bindValue('id', $id, PDO::PARAM_INT);
  97          $query->execute();
  98  
  99  // Delete any resource links for this consumer
 100          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' .
 101                 'WHERE consumer_pk = :id';
 102          $query = $this->db->prepare($sql);
 103          $query->bindValue('id', $id, PDO::PARAM_INT);
 104          $query->execute();
 105  
 106  // Delete any resource links for contexts in this consumer
 107          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' .
 108                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME . ' c ' .
 109                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . '.context_pk = c.context_pk) AND (c.consumer_pk = :id))';
 110          $query = $this->db->prepare($sql);
 111          $query->bindValue('id', $id, PDO::PARAM_INT);
 112          $query->execute();
 113  
 114  // Delete any contexts for this consumer
 115          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME . ' ' .
 116                 'WHERE consumer_pk = :id';
 117          $query = $this->db->prepare($sql);
 118          $query->bindValue('id', $id, PDO::PARAM_INT);
 119          $query->execute();
 120  
 121  // Delete consumer
 122          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::CONSUMER_TABLE_NAME . ' ' .
 123                 'WHERE consumer_pk = :id';
 124          $query = $this->db->prepare($sql);
 125          $query->bindValue('id', $id, PDO::PARAM_INT);
 126          $ok = $query->execute();
 127  
 128          if ($ok) {
 129              $consumer->initialize();
 130          }
 131  
 132          return $ok;
 133  
 134      }
 135  
 136  ###
 137  ###  Context methods
 138  ###
 139  
 140  /**
 141   * Delete context object.
 142   *
 143   * @param Context $context Context object
 144   *
 145   * @return boolean True if the Context object was successfully deleted
 146   */
 147      public function deleteContext($context)
 148      {
 149  
 150          $id = $context->getRecordId();
 151  
 152  // Delete any outstanding share keys for resource links for this context
 153          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
 154                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
 155                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . '.resource_link_pk = rl.resource_link_pk) AND (rl.context_pk = :id))';
 156          $query = $this->db->prepare($sql);
 157          $query->bindValue('id', $id, PDO::PARAM_INT);
 158          $query->execute();
 159  
 160  // Delete any users in resource links for this context
 161          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . ' ' .
 162                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
 163                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . '.resource_link_pk = rl.resource_link_pk) AND (rl.context_pk = :id))';
 164          $query = $this->db->prepare($sql);
 165          $query->bindValue('id', $id, PDO::PARAM_INT);
 166          $query->execute();
 167  
 168  // Update any resource links for which this consumer is acting as a primary resource link
 169          $sql = "UPDATE {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' .
 170                 'SET primary_resource_link_pk = null, share_approved = null ' .
 171                 "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
 172                 "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . '.primary_resource_link_pk = rl.resource_link_pk) AND (rl.context_pk = :id))';
 173          $query = $this->db->prepare($sql);
 174          $query->bindValue('id', $id, PDO::PARAM_INT);
 175          $query->execute();
 176  
 177  // Delete any resource links for this consumer
 178          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' .
 179                 'WHERE context_pk = :id';
 180          $query = $this->db->prepare($sql);
 181          $query->bindValue('id', $id, PDO::PARAM_INT);
 182          $query->execute();
 183  
 184  // Delete context
 185          $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME . ' ' .
 186                 'WHERE context_pk = :id';
 187          $query = $this->db->prepare($sql);
 188          $query->bindValue('id', $id, PDO::PARAM_INT);
 189          $ok = $query->execute();
 190  
 191          if ($ok) {
 192              $context->initialize();
 193          }
 194  
 195          return $ok;
 196  
 197      }
 198  
 199  }