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   * Step definition to generate database fixtures for learning plan system.
  19   *
  20   * @package    tool_lp
  21   * @category   test
  22   * @copyright  2016 Issam Taboubi <issam.taboubi@umontreal.ca>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once (__DIR__ . '/../../../../../lib/behat/behat_base.php');
  27  
  28  use Behat\Gherkin\Node\TableNode as TableNode;
  29  use Behat\Behat\Tester\Exception\PendingException as PendingException;
  30  use core_competency\competency;
  31  use core_competency\competency_framework;
  32  use core_competency\plan;
  33  use core_competency\user_evidence;
  34  
  35  /**
  36   * Step definition to generate database fixtures for learning plan system.
  37   *
  38   * @package    tool_lp
  39   * @category   test
  40   * @copyright  2016 Issam Taboubi <issam.taboubi@umontreal.ca>
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class behat_tool_lp_data_generators extends behat_base {
  44  
  45      /**
  46       * @var tool_lp data generator
  47       */
  48      protected $datageneratorlp;
  49  
  50      /**
  51       * Each element specifies:
  52       * - The data generator sufix used.
  53       * - The required fields.
  54       * - The mapping between other elements references and database field names.
  55       * @var array
  56       */
  57      protected static $elements = array(
  58          'frameworks' => array(
  59              'datagenerator' => 'framework',
  60              'required' => array()
  61          ),
  62          'templates' => array(
  63              'datagenerator' => 'template',
  64              'required' => array()
  65          ),
  66          'plans' => array(
  67              'datagenerator' => 'plan',
  68              'required' => array('user')
  69          ),
  70          'competencies' => array(
  71              'datagenerator' => 'competency',
  72              'required' => array('framework')
  73          ),
  74          'userevidence' => array(
  75              'datagenerator' => 'user_evidence',
  76              'required' => array('user')
  77          ),
  78          'plancompetencies' => array(
  79              'datagenerator' => 'plan_competency',
  80              'required' => array('plan', 'competency')
  81          ),
  82          'userevidencecompetencies' => array(
  83              'datagenerator' => 'user_evidence_competency',
  84              'required' => array('userevidence', 'competency')
  85          ),
  86          'usercompetencies' => array(
  87              'datagenerator' => 'user_competency',
  88              'required' => array('user', 'competency')
  89          ),
  90          'usercompetencyplans' => array(
  91              'datagenerator' => 'user_competency_plan',
  92              'required' => array('user', 'competency', 'plan')
  93          )
  94      );
  95  
  96      /**
  97       * Creates the specified element. More info about available elements in https://moodledev.io/general/development/tools/behat.
  98       *
  99       * @Given /^the following lp "(?P<element_string>(?:[^"]|\\")*)" exist:$/
 100       *
 101       * @throws Exception
 102       * @throws PendingException
 103       * @param string    $elementname The name of the entity to add
 104       * @param TableNode $data
 105       */
 106      public function the_following_lp_exist($elementname, TableNode $data) {
 107  
 108          // Now that we need them require the data generators.
 109          require_once (__DIR__.'/../../../../../lib/phpunit/classes/util.php');
 110  
 111          if (empty(self::$elements[$elementname])) {
 112              throw new PendingException($elementname . ' data generator is not implemented');
 113          }
 114  
 115          $datagenerator = testing_util::get_data_generator();
 116          $this->datageneratorlp = $datagenerator->get_plugin_generator('core_competency');
 117  
 118          $elementdatagenerator = self::$elements[$elementname]['datagenerator'];
 119          $requiredfields = self::$elements[$elementname]['required'];
 120          if (!empty(self::$elements[$elementname]['switchids'])) {
 121              $switchids = self::$elements[$elementname]['switchids'];
 122          }
 123  
 124          foreach ($data->getHash() as $elementdata) {
 125  
 126              // Check if all the required fields are there.
 127              foreach ($requiredfields as $requiredfield) {
 128                  if (!isset($elementdata[$requiredfield])) {
 129                      throw new Exception($elementname . ' requires the field ' . $requiredfield . ' to be specified');
 130                  }
 131              }
 132  
 133              // Switch from human-friendly references to ids.
 134              if (isset($switchids)) {
 135                  foreach ($switchids as $element => $field) {
 136                      $methodname = 'get_' . $element . '_id';
 137  
 138                      // Not all the switch fields are required, default vars will be assigned by data generators.
 139                      if (isset($elementdata[$element])) {
 140                          // Temp $id var to avoid problems when $element == $field.
 141                          $id = $this->{$methodname}($elementdata[$element]);
 142                          unset($elementdata[$element]);
 143                          $elementdata[$field] = $id;
 144                      }
 145                  }
 146              }
 147  
 148              // Preprocess the entities that requires a special treatment.
 149              if (method_exists($this, 'preprocess_' . $elementdatagenerator)) {
 150                  $elementdata = $this->{'preprocess_' . $elementdatagenerator}($elementdata);
 151              }
 152  
 153              // Creates element.
 154              $methodname = 'create_' . $elementdatagenerator;
 155              if (method_exists($this->datageneratorlp, $methodname)) {
 156                  // Using data generators directly.
 157                  $this->datageneratorlp->{$methodname}($elementdata);
 158  
 159              } else if (method_exists($this, 'process_' . $elementdatagenerator)) {
 160                  // Using an alternative to the direct data generator call.
 161                  $this->{'process_' . $elementdatagenerator}($elementdata);
 162              } else {
 163                  throw new PendingException($elementname . ' data generator is not implemented');
 164              }
 165          }
 166      }
 167  
 168      /**
 169       * Adapt creating competency from framework idnumber or frameworkid.
 170       *
 171       * @param array $data
 172       * @return array
 173       */
 174      protected function preprocess_competency($data) {
 175          if (isset($data['framework'])) {
 176              $framework = competency_framework::get_record(array('idnumber' => $data['framework']));
 177              if ($framework) {
 178                  $data['competencyframeworkid'] = $framework->get('id');
 179              } else {
 180                  $framework = competency_framework::get_record(array('id' => $data['framework']));
 181                  if ($framework) {
 182                      $data['competencyframeworkid'] = $framework->get('id');
 183                  } else {
 184                      throw new Exception('Could not resolve framework with idnumber or id : "' . $data['category'] . '"');
 185                  }
 186              }
 187          }
 188          unset($data['framework']);
 189          return $data;
 190      }
 191  
 192      /**
 193       * Adapt creating plan from user username.
 194       *
 195       * @param array $data
 196       * @return array
 197       */
 198      protected function preprocess_plan($data) {
 199          global $DB;
 200  
 201          if (isset($data['user'])) {
 202              $user = $DB->get_record('user', array('username' => $data['user']), '*', MUST_EXIST);
 203              $data['userid'] = $user->id;
 204          }
 205          unset($data['user']);
 206  
 207          if (isset($data['reviewer'])) {
 208              if (is_number($data['reviewer'])) {
 209                  $data['reviewerid'] = $data['reviewer'];
 210              } else {
 211                  $user = $DB->get_record('user', array('username' => $data['reviewer']), '*', MUST_EXIST);
 212                  $data['reviewerid'] = $user->id;
 213              }
 214              unset($data['reviewer']);
 215          }
 216  
 217          if (isset($data['status'])) {
 218              switch ($data['status']) {
 219                  case 'draft':
 220                      $status = plan::STATUS_DRAFT;
 221                      break;
 222                  case 'in review':
 223                      $status = plan::STATUS_IN_REVIEW;
 224                      break;
 225                  case 'waiting for review':
 226                      $status = plan::STATUS_WAITING_FOR_REVIEW;
 227                      break;
 228                  case 'active':
 229                      $status = plan::STATUS_ACTIVE;
 230                      break;
 231                  case 'complete':
 232                      $status = plan::STATUS_COMPLETE;
 233                      break;
 234                  default:
 235                      throw new Exception('Could not resolve plan status with: "' . $data['status'] . '"');
 236                      break;
 237              }
 238  
 239              $data['status'] = $status;
 240          }
 241  
 242          return $data;
 243      }
 244  
 245      /**
 246       * Adapt creating user_evidence from user username.
 247       *
 248       * @param array $data
 249       * @return array
 250       */
 251      protected function preprocess_user_evidence($data) {
 252          global $DB;
 253  
 254          if (isset($data['user'])) {
 255              $user = $DB->get_record('user', array('username' => $data['user']), '*', MUST_EXIST);
 256              $data['userid'] = $user->id;
 257          }
 258          unset($data['user']);
 259          return $data;
 260      }
 261  
 262      /**
 263       * Adapt creating plan_competency from plan name and competency shortname.
 264       *
 265       * @param array $data
 266       * @return array
 267       */
 268      protected function preprocess_plan_competency($data) {
 269          global $DB;
 270  
 271          if (isset($data['plan'])) {
 272              $plan = $DB->get_record(plan::TABLE, array('name' => $data['plan']), '*', MUST_EXIST);
 273              $data['planid'] = $plan->id;
 274          }
 275          unset($data['plan']);
 276  
 277          if (isset($data['competency'])) {
 278              $competency = $DB->get_record(competency::TABLE, array('shortname' => $data['competency']), '*', MUST_EXIST);
 279              $data['competencyid'] = $competency->id;
 280          }
 281          unset($data['competency']);
 282          return $data;
 283      }
 284  
 285      /**
 286       * Adapt creating plan_competency from user evidence name and competency shortname.
 287       *
 288       * @param array $data
 289       * @return array
 290       */
 291      protected function preprocess_user_evidence_competency($data) {
 292          global $DB;
 293  
 294          if (isset($data['userevidence'])) {
 295              $userevidence = $DB->get_record(user_evidence::TABLE, array('name' => $data['userevidence']), '*', MUST_EXIST);
 296              $data['userevidenceid'] = $userevidence->id;
 297          }
 298          unset($data['userevidence']);
 299  
 300          if (isset($data['competency'])) {
 301              $competency = $DB->get_record(competency::TABLE, array('shortname' => $data['competency']), '*', MUST_EXIST);
 302              $data['competencyid'] = $competency->id;
 303          }
 304          unset($data['competency']);
 305          return $data;
 306      }
 307  
 308      /**
 309       * Adapt creating user_competency from user name and competency shortname.
 310       *
 311       * @param array $data
 312       * @return array
 313       */
 314      protected function preprocess_user_competency($data) {
 315          global $DB;
 316  
 317          if (isset($data['user'])) {
 318              $user = $DB->get_record('user', array('username' => $data['user']), '*', MUST_EXIST);
 319              $data['userid'] = $user->id;
 320          }
 321          unset($data['user']);
 322  
 323          if (isset($data['competency'])) {
 324              $competency = $DB->get_record(competency::TABLE, array('shortname' => $data['competency']), '*', MUST_EXIST);
 325              $data['competencyid'] = $competency->id;
 326          }
 327          unset($data['competency']);
 328  
 329          return $data;
 330      }
 331  
 332      /**
 333       * Adapt creating user_competency_plan from user name, competency shortname and plan name.
 334       *
 335       * @param array $data
 336       * @return array
 337       */
 338      protected function preprocess_user_competency_plan($data) {
 339          global $DB;
 340  
 341          if (isset($data['user'])) {
 342              $user = $DB->get_record('user', array('username' => $data['user']), '*', MUST_EXIST);
 343              $data['userid'] = $user->id;
 344          }
 345          unset($data['user']);
 346  
 347          if (isset($data['competency'])) {
 348              $competency = $DB->get_record(competency::TABLE, array('shortname' => $data['competency']), '*', MUST_EXIST);
 349              $data['competencyid'] = $competency->id;
 350          }
 351          unset($data['competency']);
 352  
 353          if (isset($data['plan'])) {
 354              $plan = $DB->get_record(plan::TABLE, array('name' => $data['plan']), '*', MUST_EXIST);
 355              $data['planid'] = $plan->id;
 356          }
 357          unset($data['plan']);
 358  
 359          return $data;
 360      }
 361  
 362  }