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.
   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 for tool_policy
  19   *
  20   * @package    tool_policy
  21   * @category   test
  22   * @copyright  2018 Marina Glancy
  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  
  30  /**
  31   * Step definition for tool_policy
  32   *
  33   * @package    tool_policy
  34   * @category   test
  35   * @copyright  2018 Marina Glancy
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class behat_tool_policy extends behat_base {
  39  
  40      /**
  41       * Click on an entry in the edit menu.
  42       *
  43       * @Given /^the following policies exist:$/
  44       *
  45       * Supported table fields:
  46       *
  47       * - Name: Policy name (required).
  48       * - Revision: Revision name (policy version).
  49       * - Status: Policy version status - 'draft', 'active' or 'archived'. Defaults to 'active'.
  50       * - Audience: Target users - 'guest', 'all' or 'loggedin'. Default to 'all'.
  51       * - Type: 0 - site policy, 1 - privacy policy, 2 - third party policy, 99 - other.
  52       * - Summary: Policy summary text.
  53       * - Content: Policy full text.
  54       * - Agreement style (agreementstyle): 0 - On the consent page, 1 - On its own page
  55       * - Agreement optional (optional): 0 - Compulsory policy, 1 - Optional policy
  56       *
  57       * @param TableNode $data
  58       */
  59      public function the_following_policies_exist(TableNode $data) {
  60          global $CFG;
  61          if (empty($CFG->sitepolicyhandler) || $CFG->sitepolicyhandler !== 'tool_policy') {
  62              throw new Exception('Site policy handler is not set to "tool_policy"');
  63          }
  64  
  65          $fields = [
  66              'name',
  67              'revision',
  68              'policy',
  69              'status',
  70              'audience',
  71              'type',
  72              'content',
  73              'summary',
  74              'agreementstyle',
  75              'optional',
  76          ];
  77  
  78          // Associative array "policy identifier" => id in the database .
  79          $policies = [];
  80  
  81          foreach ($data->getHash() as $elementdata) {
  82              $data = (object)[
  83                  'audience' => \tool_policy\policy_version::AUDIENCE_ALL,
  84                  'archived' => 0,
  85                  'type' => 0
  86              ];
  87              $elementdata = array_change_key_case($elementdata, CASE_LOWER);
  88              foreach ($elementdata as $key => $value) {
  89                  if ($key === 'policy') {
  90                      if (array_key_exists($value, $policies)) {
  91                          $data->policyid = $policies[$value];
  92                      }
  93                  } else if ($key === 'status') {
  94                      $data->archived = ($value === 'archived');
  95                  } else if ($key === 'audience') {
  96                      if ($value === 'guest') {
  97                          $data->audience = \tool_policy\policy_version::AUDIENCE_GUESTS;
  98                      } else if ($value === 'loggedin') {
  99                          $data->audience = \tool_policy\policy_version::AUDIENCE_LOGGEDIN;
 100                      }
 101                  } else if (($key === 'summary' || $key === 'content') && !empty($value)) {
 102                      $data->{$key.'_editor'} = ['text' => $value, 'format' => FORMAT_MOODLE];
 103                  } else if (in_array($key, $fields) && $value !== '') {
 104                      $data->$key = $value;
 105                  }
 106              }
 107              if (empty($data->name) || empty($data->content_editor) || empty($data->summary_editor)) {
 108                  throw new Exception('Policy is missing at least one of the required fields: name, content, summary');
 109              }
 110  
 111              if (!empty($data->policyid)) {
 112                  $version = tool_policy\api::form_policydoc_update_new($data);
 113              } else {
 114                  $version = \tool_policy\api::form_policydoc_add($data);
 115              }
 116  
 117              if (!empty($elementdata['policy'])) {
 118                  $policies[$elementdata['policy']] = $version->get('policyid');
 119              }
 120              if (empty($elementdata['status']) || $elementdata['status'] === 'active') {
 121                  \tool_policy\api::make_current($version->get('id'));
 122              }
 123          }
 124      }
 125  }