Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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                  'subreviewurl',
  69                  'subreviewparams'
  70                  )
  71          );
  72  
  73          // Build the tree.
  74          $subplugin->add_child($subpluginwrapper);
  75          $subpluginwrapper->add_child($lineitems);
  76          $lineitems->add_child($lineitem);
  77  
  78          // We need to know the actual activity tool or toolproxy.
  79          // If and activity is assigned to a type that doesn't exists we don't want to backup any related lineitems.``
  80          // Default to invalid condition.
  81          $typeid = 0;
  82          $toolproxyid = '0';
  83  
  84          /* cache parent property to account for missing PHPDoc type specification */
  85          /** @var backup_activity_task $activitytask */
  86          $activitytask = $this->task;
  87          $activityid = $activitytask->get_activityid();
  88          $activitycourseid = $activitytask->get_courseid();
  89          $lti = $DB->get_record('lti', ['id' => $activityid], 'typeid, toolurl, securetoolurl');
  90          $ltitype = $DB->get_record('lti_types', ['id' => $lti->typeid], 'toolproxyid, baseurl');
  91          if ($ltitype) {
  92              $typeid = $lti->typeid;
  93              $toolproxyid = $ltitype->toolproxyid;
  94          } else if ($lti->typeid == self::NONVALIDTYPEID) { // This activity comes from an old backup.
  95              // 1. Let's check if the activity is coupled. If so, find the values in the GBS element.
  96              $gbsrecord = $DB->get_record('ltiservice_gradebookservices',
  97                      ['ltilinkid' => $activityid], 'typeid,toolproxyid,baseurl');
  98              if ($gbsrecord) {
  99                  $typeid = $gbsrecord->typeid;
 100                  $toolproxyid = $gbsrecord->toolproxyid;
 101              } else { // 2. If it is uncoupled... we will need to guess the right activity typeid
 102                  // Guess the typeid for the activity.
 103                  $tool = lti_get_tool_by_url_match($lti->toolurl, $activitycourseid);
 104                  if (!$tool) {
 105                      $tool = lti_get_tool_by_url_match($lti->securetoolurl, $activitycourseid);
 106                  }
 107                  if ($tool) {
 108                      $alttypeid = $tool->id;
 109                      // If we have a valid typeid then get types again.
 110                      if ($alttypeid != self::NONVALIDTYPEID) {
 111                          $ltitype = $DB->get_record('lti_types', ['id' => $alttypeid], 'toolproxyid, baseurl');
 112                          $toolproxyid = $ltitype->toolproxyid;
 113                      }
 114                  }
 115              }
 116          }
 117  
 118          // Define sources.
 119          if ($toolproxyid != null) {
 120              $lineitemssql = "SELECT l.*, t.vendorcode as vendorcode, t.guid as guid
 121                                 FROM {ltiservice_gradebookservices} l
 122                           INNER JOIN {lti_tool_proxies} t ON (t.id = l.toolproxyid)
 123                                WHERE l.courseid = ?
 124                                  AND l.toolproxyid = ?
 125                                  AND l.typeid is null";
 126              $lineitemsparams = ['courseid' => backup::VAR_COURSEID, backup_helper::is_sqlparam($toolproxyid)];
 127          } else {
 128              $lineitemssql = "SELECT l.*, null as vendorcode, null as guid
 129                                 FROM {ltiservice_gradebookservices} l
 130                                WHERE l.courseid = ?
 131                                  AND l.typeid = ?
 132                                  AND l.toolproxyid is null";
 133              $lineitemsparams = ['courseid' => backup::VAR_COURSEID, backup_helper::is_sqlparam($typeid)];
 134          }
 135  
 136          $lineitem->set_source_sql($lineitemssql, $lineitemsparams);
 137  
 138          return $subplugin;
 139      }
 140  }