Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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
  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  
  30  require_once($CFG->dirroot.'/mod/lti/locallib.php');
  31  
  32  /**
  33   * Provides the information to backup gradebookservices lineitems
  34   *
  35   * @package    ltiservice_gradebookservices
  36   * @copyright  2017 Cengage Learning http://www.cengage.com
  37   * @author     Dirk Singels, Diego del Blanco, Claude Vervoort
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class backup_ltiservice_gradebookservices_subplugin extends backup_subplugin {
  41  
  42      /** TypeId contained in DB but is invalid */
  43      const NONVALIDTYPEID = 0;
  44  
  45      /**
  46       * Returns the subplugin information to attach to submission element
  47       * @return backup_subplugin_element
  48       */
  49      protected function define_lti_subplugin_structure() {
  50          global $DB;
  51  
  52          // Create XML elements.
  53          $subplugin = $this->get_subplugin_element();
  54          $subpluginwrapper = new backup_nested_element($this->get_recommended_name());
  55          // The gbs entries related with this element.
  56          $lineitems = new backup_nested_element('lineitems');
  57          $lineitem = new backup_nested_element('lineitem', array('id'), array(
  58                  'gradeitemid',
  59                  'courseid',
  60                  'toolproxyid',
  61                  'typeid',
  62                  'baseurl',
  63                  'ltilinkid',
  64                  'resourceid',
  65                  'tag',
  66                  'vendorcode',
  67                  'guid'
  68                  )
  69          );
  70  
  71          // Build the tree.
  72          $subplugin->add_child($subpluginwrapper);
  73          $subpluginwrapper->add_child($lineitems);
  74          $lineitems->add_child($lineitem);
  75  
  76          // We need to know the actual activity tool or toolproxy.
  77          // If and activity is assigned to a type that doesn't exists we don't want to backup any related lineitems.``
  78          // Default to invalid condition.
  79          $typeid = 0;
  80          $toolproxyid = '0';
  81  
  82          /* cache parent property to account for missing PHPDoc type specification */
  83          /** @var backup_activity_task $activitytask */
  84          $activitytask = $this->task;
  85          $activityid = $activitytask->get_activityid();
  86          $activitycourseid = $activitytask->get_courseid();
  87          $lti = $DB->get_record('lti', ['id' => $activityid], 'typeid, toolurl, securetoolurl');
  88          $ltitype = $DB->get_record('lti_types', ['id' => $lti->typeid], 'toolproxyid, baseurl');
  89          if ($ltitype) {
  90              $typeid = $lti->typeid;
  91              $toolproxyid = $ltitype->toolproxyid;
  92          } else if ($lti->typeid == self::NONVALIDTYPEID) { // This activity comes from an old backup.
  93              // 1. Let's check if the activity is coupled. If so, find the values in the GBS element.
  94              $gbsrecord = $DB->get_record('ltiservice_gradebookservices',
  95                      ['ltilinkid' => $activityid], 'typeid,toolproxyid,baseurl');
  96              if ($gbsrecord) {
  97                  $typeid = $gbsrecord->typeid;
  98                  $toolproxyid = $gbsrecord->toolproxyid;
  99              } else { // 2. If it is uncoupled... we will need to guess the right activity typeid
 100                  // Guess the typeid for the activity.
 101                  $tool = lti_get_tool_by_url_match($lti->toolurl, $activitycourseid);
 102                  if (!$tool) {
 103                      $tool = lti_get_tool_by_url_match($lti->securetoolurl, $activitycourseid);
 104                  }
 105                  if ($tool) {
 106                      $alttypeid = $tool->id;
 107                      // If we have a valid typeid then get types again.
 108                      if ($alttypeid != self::NONVALIDTYPEID) {
 109                          $ltitype = $DB->get_record('lti_types', ['id' => $alttypeid], 'toolproxyid, baseurl');
 110                          $toolproxyid = $ltitype->toolproxyid;
 111                      }
 112                  }
 113              }
 114          }
 115  
 116          // Define sources.
 117          if ($toolproxyid != null) {
 118              $lineitemssql = "SELECT l.*, t.vendorcode as vendorcode, t.guid as guid
 119                                 FROM {ltiservice_gradebookservices} l
 120                           INNER JOIN {lti_tool_proxies} t ON (t.id = l.toolproxyid)
 121                                WHERE l.courseid = ?
 122                                  AND l.toolproxyid = ?
 123                                  AND l.typeid is null";
 124              $lineitemsparams = ['courseid' => backup::VAR_COURSEID, backup_helper::is_sqlparam($toolproxyid)];
 125          } else {
 126              $lineitemssql = "SELECT l.*, null as vendorcode, null as guid
 127                                 FROM {ltiservice_gradebookservices} l
 128                                WHERE l.courseid = ?
 129                                  AND l.typeid = ?
 130                                  AND l.toolproxyid is null";
 131              $lineitemsparams = ['courseid' => backup::VAR_COURSEID, backup_helper::is_sqlparam($typeid)];
 132          }
 133  
 134          $lineitem->set_source_sql($lineitemssql, $lineitemsparams);
 135  
 136          return $subplugin;
 137      }
 138  }