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 39 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  namespace core_competency;
  18  
  19  /**
  20   * This test ensures that the course competency settings are applied and work correctly.
  21   *
  22   * @package    core_competency
  23   * @copyright  2016 Frédéric Massart - FMCorz.net
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  class course_competency_settings_test extends \advanced_testcase {
  27  
  28      public function test_who_can_change_settings() {
  29          global $CFG, $DB;
  30  
  31          $this->resetAfterTest(true);
  32  
  33          $syscontext = \context_system::instance();
  34          $dg = $this->getDataGenerator();
  35          $lpg = $dg->get_plugin_generator('core_competency');
  36          $role = create_role('Settings changer role', 'settingschanger', 'Someone who can change course competency settings');
  37          assign_capability('moodle/competency:coursecompetencyconfigure', CAP_ALLOW, $role, $syscontext->id);
  38          assign_capability('moodle/competency:competencygrade', CAP_ALLOW, $role, $syscontext->id);
  39          assign_capability('moodle/competency:coursecompetencyview', CAP_ALLOW, $role, $syscontext->id);
  40          assign_capability('moodle/competency:planview', CAP_ALLOW, $role, $syscontext->id);
  41          $gradedrole = create_role('Graded role', 'graded', 'Someone who can be graded');
  42          assign_capability('moodle/competency:coursecompetencygradable', CAP_ALLOW, $gradedrole, $syscontext->id);
  43  
  44          $c1 = $dg->create_course();
  45          $u1 = $dg->create_user();
  46          $u2 = $dg->create_user();
  47          $u3 = $dg->create_user();
  48  
  49          $framework = $lpg->create_framework();
  50          $comp1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
  51          $comp2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
  52          $lpg->create_course_competency(array('competencyid' => $comp1->get('id'), 'courseid' => $c1->id));
  53          $lpg->create_course_competency(array('competencyid' => $comp2->get('id'), 'courseid' => $c1->id));
  54  
  55          // Enrol the user.
  56          $dg->enrol_user($u1->id, $c1->id);
  57          role_assign($gradedrole, $u1->id, $syscontext->id);
  58  
  59          // Assign roles.
  60          role_assign($role, $u2->id, $syscontext->id);
  61  
  62          $this->setUser($u2);
  63  
  64          set_config('pushcourseratingstouserplans', true, 'core_competency');
  65  
  66          $coursesettings = course_competency_settings::get_by_courseid($c1->id);
  67          $this->assertTrue((boolean)$coursesettings->get('pushratingstouserplans'));
  68  
  69          set_config('pushcourseratingstouserplans', false, 'core_competency');
  70  
  71          $coursesettings = course_competency_settings::get_by_courseid($c1->id);
  72          $this->assertFalse((boolean)$coursesettings->get('pushratingstouserplans'));
  73  
  74          api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => true));
  75          $coursesettings = course_competency_settings::get_by_courseid($c1->id);
  76          $this->assertTrue((boolean)$coursesettings->get('pushratingstouserplans'));
  77  
  78          set_config('pushcourseratingstouserplans', true, 'core_competency');
  79          api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => false));
  80          $coursesettings = course_competency_settings::get_by_courseid($c1->id);
  81          $this->assertFalse((boolean)$coursesettings->get('pushratingstouserplans'));
  82  
  83          // Right now the setting is false.
  84          api::grade_competency_in_course($c1->id, $u1->id, $comp1->get('id'), 1, 'Note');
  85          $filterparams = array(
  86              'userid' => $u1->id,
  87              'competencyid' => $comp1->get('id'),
  88          );
  89          $usercompcourse = \core_competency\user_competency_course::get_record($filterparams);
  90          $usercomp = \core_competency\user_competency::get_record($filterparams);
  91  
  92          // No grade in plan - only a grade in the course.
  93          $this->assertEmpty($usercomp->get('grade'));
  94          $this->assertEquals(1, $usercompcourse->get('grade'));
  95  
  96          api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => true));
  97          api::grade_competency_in_course($c1->id, $u1->id, $comp1->get('id'), 2, 'Note 2');
  98          $filterparams = array(
  99              'userid' => $u1->id,
 100              'competencyid' => $comp1->get('id'),
 101          );
 102          $usercompcourse = \core_competency\user_competency_course::get_record($filterparams);
 103          $usercomp = \core_competency\user_competency::get_record($filterparams);
 104  
 105          // Updated grade in plan - updated grade in the course.
 106          $this->assertEquals(2, $usercomp->get('grade'));
 107          $this->assertEquals(2, $usercompcourse->get('grade'));
 108  
 109          $this->setUser($u3);
 110          $this->expectException('required_capability_exception');
 111          api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => false));
 112      }
 113  
 114  }