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 310 and 311] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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  /**
  18   * This file contains the class for restore of this gradebookservices plugin
  19   *
  20   * @package    ltiservice_gradebookservices
  21   * @copyright  2017 Cengage Learning http://www.cengage.com
  22   * @author     Dirk Singels, Diego del Blanco, Claude Vervoort
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot.'/mod/lti/locallib.php');
  30  
  31  /**
  32   * Restore subplugin class.
  33   *
  34   * Provides the necessary information
  35   * needed to restore the lineitems related with the lti activity (coupled),
  36   * and all the uncoupled ones from the course.
  37   *
  38   * @package    ltiservice_gradebookservices
  39   * @copyright  2017 Cengage Learning http://www.cengage.com
  40   * @author     Dirk Singels, Diego del Blanco, Claude Vervoort
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class restore_ltiservice_gradebookservices_subplugin extends restore_subplugin {
  44  
  45      /**
  46       * Returns the subplugin structure to attach to the XML element.
  47       *
  48       * @return restore_path_element[] array of elements to be processed on restore.
  49       */
  50      protected function define_lti_subplugin_structure() {
  51  
  52          $paths = array();
  53          $elename = $this->get_namefor('lineitem');
  54          $elepath = $this->get_pathfor('/lineitems/lineitem');
  55          $paths[] = new restore_path_element($elename, $elepath);
  56          return $paths;
  57      }
  58  
  59      /**
  60       * Processes one lineitem
  61       *
  62       * @param mixed $data
  63       * @return void
  64       */
  65      public function process_ltiservice_gradebookservices_lineitem($data) {
  66          global $DB;
  67          $data = (object)$data;
  68          // The coupled lineitems are restored as any other grade item
  69          // so we will only create the entry in the ltiservice_gradebookservices table.
  70          // We will try to find a valid toolproxy in the system.
  71          // If it has been found before... we use it.
  72          /* cache parent property to account for missing PHPDoc type specification */
  73          /** @var backup_activity_task $activitytask */
  74          $activitytask = $this->task;
  75          $courseid = $activitytask->get_courseid();
  76          if ($data->typeid != null) {
  77              if ($ltitypeid = $this->get_mappingid('ltitype', $data->typeid)) {
  78                  $newtypeid = $ltitypeid;
  79              } else { // If not, then we will call our own function to find it.
  80                  $newtypeid = $this->find_typeid($data, $courseid);
  81              }
  82          } else {
  83              $newtypeid = null;
  84          }
  85          if ($data->toolproxyid != null) {
  86              $ltitoolproxy = $this->get_mappingid('ltitoolproxy', $data->toolproxyid);
  87              if ($ltitoolproxy && $ltitoolproxy != 0) {
  88                  $newtoolproxyid = $ltitoolproxy;
  89              } else { // If not, then we will call our own function to find it.
  90                  $newtoolproxyid = $this->find_proxy_id($data);
  91              }
  92          } else {
  93              $newtoolproxyid = null;
  94          }
  95          if ($data->ltilinkid != null) {
  96              if ($data->ltilinkid != $this->get_old_parentid('lti')) {
  97                  // This is a linked item, but not for the current lti link, so skip it.
  98                  return;
  99              }
 100              $ltilinkid = $this->get_new_parentid('lti');
 101          } else {
 102              $ltilinkid = null;
 103          }
 104          $resourceid = null;
 105          if (property_exists( $data, 'resourceid' )) {
 106              $resourceid = $data->resourceid;
 107          }
 108          // If this has not been restored before.
 109          if ($this->get_mappingid('gbsgradeitemrestored',  $data->id, 0) == 0) {
 110              $newgbsid = $DB->insert_record('ltiservice_gradebookservices', (object) array(
 111                      'gradeitemid' => 0,
 112                      'courseid' => $courseid,
 113                      'toolproxyid' => $newtoolproxyid,
 114                      'ltilinkid' => $ltilinkid,
 115                      'typeid' => $newtypeid,
 116                      'baseurl' => $data->baseurl,
 117                      'resourceid' => $resourceid,
 118                      'tag' => $data->tag
 119              ));
 120              $this->set_mapping('gbsgradeitemoldid', $newgbsid, $data->gradeitemid);
 121              $this->set_mapping('gbsgradeitemrestored', $data->id, $data->id);
 122          }
 123      }
 124  
 125      /**
 126       * If the toolproxy is not in the mapping (or it is 0)
 127       * we try to find the toolproxyid.
 128       * If none is found, then we set it to 0.
 129       *
 130       * @param mixed $data
 131       * @return integer $newtoolproxyid
 132       */
 133      private function find_proxy_id($data) {
 134          global $DB;
 135          $newtoolproxyid = 0;
 136          $oldtoolproxyguid = $data->guid;
 137          $oldtoolproxyvendor = $data->vendorcode;
 138  
 139          $dbtoolproxyjsonparams = array('guid' => $oldtoolproxyguid, 'vendorcode' => $oldtoolproxyvendor);
 140          $dbtoolproxy = $DB->get_field('lti_tool_proxies', 'id', $dbtoolproxyjsonparams, IGNORE_MISSING);
 141          if ($dbtoolproxy) {
 142              $newtoolproxyid = $dbtoolproxy;
 143          }
 144          return $newtoolproxyid;
 145      }
 146  
 147      /**
 148       * If the typeid is not in the mapping or it is 0, (it should be most of the times)
 149       * we will try to find the better typeid that matches with the lineitem.
 150       * If none is found, then we set it to 0.
 151       *
 152       * @param stdClass $data
 153       * @param int $courseid
 154       * @return int The item type id
 155       */
 156      private function find_typeid($data, $courseid) {
 157          global $DB;
 158          $newtypeid = 0;
 159          $oldtypeid = $data->typeid;
 160  
 161          // 1. Find a type with the same id in the same course.
 162          $dbtypeidparameter = array('id' => $oldtypeid, 'course' => $courseid, 'baseurl' => $data->baseurl);
 163          $dbtype = $DB->get_field_select('lti_types', 'id', "id=:id
 164                  AND course=:course AND ".$DB->sql_compare_text('baseurl')."=:baseurl",
 165                  $dbtypeidparameter);
 166          if ($dbtype) {
 167              $newtypeid = $dbtype;
 168          } else {
 169              // 2. Find a site type for all the courses (course == 1), but with the same id.
 170              $dbtypeidparameter = array('id' => $oldtypeid, 'baseurl' => $data->baseurl);
 171              $dbtype = $DB->get_field_select('lti_types', 'id', "id=:id
 172                      AND course=1 AND ".$DB->sql_compare_text('baseurl')."=:baseurl",
 173                      $dbtypeidparameter);
 174              if ($dbtype) {
 175                  $newtypeid = $dbtype;
 176              } else {
 177                  // 3. Find a type with the same baseurl in the actual site.
 178                  $dbtypeidparameter = array('course' => $courseid, 'baseurl' => $data->baseurl);
 179                  $dbtype = $DB->get_field_select('lti_types', 'id', "course=:course
 180                          AND ".$DB->sql_compare_text('baseurl')."=:baseurl",
 181                          $dbtypeidparameter);
 182                  if ($dbtype) {
 183                      $newtypeid = $dbtype;
 184                  } else {
 185                      // 4. Find a site type for all the courses (course == 1) with the same baseurl.
 186                      $dbtypeidparameter = array('course' => 1, 'baseurl' => $data->baseurl);
 187                      $dbtype = $DB->get_field_select('lti_types', 'id', "course=1
 188                              AND ".$DB->sql_compare_text('baseurl')."=:baseurl",
 189                              $dbtypeidparameter);
 190                      if ($dbtype) {
 191                          $newtypeid = $dbtype;
 192                      }
 193                  }
 194              }
 195          }
 196          return $newtypeid;
 197      }
 198  
 199      /**
 200       * We call the after_restore_lti to update the grade_items id's that we didn't know in the moment of creating
 201       * the gradebookservices rows.
 202       */
 203      protected function after_restore_lti() {
 204          global $DB;
 205          $activitytask = $this->task;
 206          $courseid = $activitytask->get_courseid();
 207          $gbstoupdate = $DB->get_records('ltiservice_gradebookservices', array('gradeitemid' => 0, 'courseid' => $courseid));
 208          foreach ($gbstoupdate as $gbs) {
 209              $oldgradeitemid = $this->get_mappingid('gbsgradeitemoldid', $gbs->id, 0);
 210              $newgradeitemid = $this->get_mappingid('grade_item', $oldgradeitemid, 0);
 211              if ($newgradeitemid > 0) {
 212                  $gbs->gradeitemid = $newgradeitemid;
 213                  if (!isset($gbs->resourceid)) {
 214                      // Before 3.9 resourceid was stored in grade_item->idnumber.
 215                      $gbs->resourceid = $DB->get_field_select('grade_items', 'idnumber', "id=:id", ['id' => $newgradeitemid]);
 216                  }
 217                  $DB->update_record('ltiservice_gradebookservices', $gbs);
 218              }
 219          }
 220          // Pre 3.9 backups did not include a gradebookservices record. Adding one here if missing for the restored instance.
 221          $gi = $DB->get_record('grade_items', array('itemtype' => 'mod', 'itemmodule' => 'lti', 'courseid' => $courseid,
 222              'iteminstance' => $this->task->get_activityid()));
 223          if ($gi) {
 224              $gbs = $DB->get_records('ltiservice_gradebookservices', ['gradeitemid' => $gi->id]);
 225              if (empty($gbs)) {
 226                  // The currently restored LTI link has a grade item but no gbs, so let's create a gbs entry.
 227                  if ($instance = $DB->get_record('lti', array('id' => $gi->iteminstance))) {
 228                      if ($tool = lti_get_instance_type($instance)) {
 229                          $DB->insert_record('ltiservice_gradebookservices', (object) array(
 230                              'gradeitemid' => $gi->id,
 231                              'courseid' => $courseid,
 232                              'toolproxyid' => $tool->toolproxyid,
 233                              'ltilinkid' => $gi->iteminstance,
 234                              'typeid' => $tool->id,
 235                              'baseurl' => $tool->baseurl,
 236                              'resourceid' => $gi->idnumber
 237                          ));
 238                      }
 239                  }
 240              }
 241          }
 242      }
 243  
 244  }