[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/assignmentupgrade/ -> locallib.php (source)

   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   * Assignment upgrade tool library functions
  19   *
  20   * @package    tool_assignmentupgrade
  21   * @copyright  2012 NetSpot
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Get the URL of a script within this plugin.
  29   * @param string $script the script name, without .php. E.g. 'index'
  30   * @param array $params URL parameters (optional)
  31   * @return moodle_url
  32   */
  33  function tool_assignmentupgrade_url($script, $params = array()) {
  34      return new moodle_url('/admin/tool/assignmentupgrade/' . $script . '.php', $params);
  35  }
  36  
  37  /**
  38   * Class to encapsulate the continue / cancel for batch operations
  39   *
  40   * @package    tool_assignmentupgrade
  41   * @copyright  2012 NetSpot
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class tool_assignmentupgrade_batchoperationconfirm implements renderable {
  45      /** @var string $continuemessage The message to show above the continue cancel buttons */
  46      public $continuemessage = '';
  47      /** @var string $continueurl The url to load if the user clicks continue */
  48      public $continueurl;
  49  
  50      /**
  51       * Constructor for this class
  52       * @param stdClass $data - The data from the previous batch form
  53       */
  54      public function __construct($data) {
  55          if (isset($data->upgradeselected)) {
  56              $this->continuemessage = get_string('upgradeselectedcount',
  57                                                  'tool_assignmentupgrade',
  58                                                  count(explode(',', $data->selectedassignments)));
  59              $urlparams = array('upgradeselected'=>'1',
  60                                 'confirm'=>'1',
  61                                 'sesskey'=>sesskey(),
  62                                 'selected'=>$data->selectedassignments);
  63              $this->continueurl = new moodle_url('/admin/tool/assignmentupgrade/batchupgrade.php', $urlparams);
  64          } else if (isset($data->upgradeall)) {
  65              if (!tool_assignmentupgrade_any_upgradable_assignments()) {
  66                  $this->continuemessage = get_string('noassignmentstoupgrade', 'tool_assignmentupgrade');
  67                  $this->continueurl = '';
  68              } else {
  69                  $this->continuemessage = get_string('upgradeallconfirm', 'tool_assignmentupgrade');
  70                  $urlparams = array('upgradeall'=>'1', 'confirm'=>'1', 'sesskey'=>sesskey());
  71                  $this->continueurl = new moodle_url('/admin/tool/assignmentupgrade/batchupgrade.php', $urlparams);
  72              }
  73          }
  74      }
  75  }
  76  
  77  
  78  /**
  79   * Class to encapsulate one of the functionalities that this plugin offers.
  80   *
  81   * @package    tool_assignmentupgrade
  82   * @copyright  2012 NetSpot
  83   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  84   */
  85  class tool_assignmentupgrade_action {
  86      /** @var string the name of this action. */
  87      public $name;
  88      /** @var moodle_url the URL to launch this action. */
  89      public $url;
  90      /** @var string a description of this aciton. */
  91      public $description;
  92  
  93      /**
  94       * Constructor to set the fields.
  95       *
  96       * In order to create a new tool_assignmentupgrade_action instance you must use
  97       * the tool_assignmentupgrade_action::make
  98       * method.
  99       *
 100       * @param string $name the name of this action.
 101       * @param moodle_url $url the URL to launch this action.
 102       * @param string $description a description of this aciton.
 103       */
 104      protected function __construct($name, moodle_url $url, $description) {
 105          $this->name = $name;
 106          $this->url = $url;
 107          $this->description = $description;
 108      }
 109  
 110      /**
 111       * Make an action with standard values.
 112       * @param string $shortname internal name of the action. Used to get strings and build a URL.
 113       * @param array $params any URL params required.
 114       * @return tool_assignmentupgrade_action
 115       */
 116      public static function make($shortname, $params = array()) {
 117          return new self(
 118                  get_string($shortname, 'tool_assignmentupgrade'),
 119                  tool_assignmentupgrade_url($shortname, $params),
 120                  get_string($shortname . '_desc', 'tool_assignmentupgrade'));
 121      }
 122  }
 123  
 124  /**
 125   * Determine if there are any assignments that can be upgraded
 126   * @return boolean - Are there any assignments that can be upgraded
 127   */
 128  function tool_assignmentupgrade_any_upgradable_assignments() {
 129      global $DB, $CFG;
 130      require_once($CFG->dirroot . '/mod/assign/locallib.php');
 131      // First find all the unique assignment types.
 132      $types = $DB->get_records_sql('SELECT plugin AS assignmenttype,
 133                                            value AS version
 134                                     FROM {config_plugins}
 135                                     WHERE
 136                                         name = ? AND
 137                                         plugin LIKE ?', array('version', 'assignment_%'));
 138  
 139      $upgradabletypes = array();
 140  
 141      foreach ($types as $assignment) {
 142          $shorttype = substr($assignment->assignmenttype, strlen('assignment_'));
 143          if (assign::can_upgrade_assignment($shorttype, $assignment->version)) {
 144              $upgradabletypes[] = $shorttype;
 145          }
 146      }
 147      list($sql, $params) = $DB->get_in_or_equal($upgradabletypes);
 148  
 149      $count = $DB->count_records_sql('SELECT COUNT(id) FROM {assignment} WHERE assignmenttype ' . $sql, $params);
 150  
 151      return $count > 0;
 152  }
 153  
 154  /**
 155   * Load a list of all the assignmentids that can be upgraded
 156   * @return array of assignment ids
 157   */
 158  function tool_assignmentupgrade_load_all_upgradable_assignmentids() {
 159      global $DB, $CFG;
 160      require_once($CFG->dirroot . '/mod/assign/locallib.php');
 161      // First find all the unique assignment types.
 162      $types = $DB->get_records_sql('SELECT
 163                                         plugin AS assignmenttype,
 164                                         value AS version
 165                                     FROM {config_plugins}
 166                                     WHERE
 167                                         name = ? AND
 168                                         plugin LIKE ?', array('version', 'assignment_%'));
 169  
 170      $upgradabletypes = array();
 171  
 172      foreach ($types as $assignment) {
 173          $shorttype = substr($assignment->assignmenttype, strlen('assignment_'));
 174          if (assign::can_upgrade_assignment($shorttype, $assignment->version)) {
 175              $upgradabletypes[] = $shorttype;
 176          }
 177      }
 178  
 179      list($sql, $params) = $DB->get_in_or_equal($upgradabletypes);
 180  
 181      $records = $DB->get_records_sql('SELECT id from {assignment} where assignmenttype ' . $sql, $params);
 182      $ids = array();
 183      foreach ($records as $record) {
 184          $ids[] = $record->id;
 185      }
 186  
 187      return $ids;
 188  }
 189  
 190  
 191  /**
 192   * Upgrade a single assignment. This is used by both upgrade single and upgrade batch
 193   *
 194   * @param int $assignmentid - The assignment id to upgrade
 195   * @return array(string, boolean, string) -
 196   *                  The array contains
 197   *                      - the assignment summary (returned by tool_assignmentupgrade_get_assignment)
 198   *                      - success
 199   *                      - the upgrade log
 200   */
 201  function tool_assignmentupgrade_upgrade_assignment($assignmentid) {
 202      global $CFG;
 203      require_once($CFG->dirroot . '/mod/assign/upgradelib.php');
 204  
 205      $assignment_upgrader = new assign_upgrade_manager();
 206      $info = tool_assignmentupgrade_get_assignment($assignmentid);
 207      if ($info) {
 208          $log = '';
 209          $success = $assignment_upgrader->upgrade_assignment($assignmentid, $log);
 210      } else {
 211          $success = false;
 212          $log = get_string('assignmentnotfound', 'tool_assignmentupgrade', $assignmentid);
 213          $info = new stdClass();
 214          $info->name = get_string('unknown', 'tool_assignmentupgrade');
 215          $info->shortname = get_string('unknown', 'tool_assignmentupgrade');
 216      }
 217  
 218      return array($info, $success, $log);
 219  }
 220  
 221  /**
 222   * Get the information about a assignment to be upgraded.
 223   * @param int $assignmentid the assignment id.
 224   * @return stdClass the information about that assignment.
 225   */
 226  function tool_assignmentupgrade_get_assignment($assignmentid) {
 227      global $DB;
 228      return $DB->get_record_sql("
 229              SELECT a.id, a.name, c.shortname, c.id AS courseid
 230              FROM {assignment} a
 231              JOIN {course} c ON c.id = a.course
 232              WHERE a.id = ?", array($assignmentid));
 233  }
 234  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1