| 1 | 
           efrain | 
           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 | 
           declare(strict_types=1);
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           namespace core_cohort\reportbuilder\audience;
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
           use context;
  | 
        
        
            | 
            | 
           22 | 
           use context_system;
  | 
        
        
            | 
            | 
           23 | 
           use core_course_category;
  | 
        
        
            | 
            | 
           24 | 
           use stdClass;
  | 
        
        
            | 
            | 
           25 | 
           use core_reportbuilder\local\audiences\base;
  | 
        
        
            | 
            | 
           26 | 
           use core_reportbuilder\local\helpers\database;
  | 
        
        
            | 
            | 
           27 | 
           use MoodleQuickForm;
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           /**
  | 
        
        
            | 
            | 
           30 | 
            * The backend class for Cohort member audience type
  | 
        
        
            | 
            | 
           31 | 
            *
  | 
        
        
            | 
            | 
           32 | 
            * @package     core_reportbuilder
  | 
        
        
            | 
            | 
           33 | 
            * @copyright   2021 David Matamoros <davidmc@moodle.com>
  | 
        
        
            | 
            | 
           34 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           35 | 
            */
  | 
        
        
            | 
            | 
           36 | 
           class cohortmember extends base {
  | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
               /**
  | 
        
        
            | 
            | 
           39 | 
                * Adds audience's elements to the given mform
  | 
        
        
            | 
            | 
           40 | 
                *
  | 
        
        
            | 
            | 
           41 | 
                * @param MoodleQuickForm $mform The form to add elements to
  | 
        
        
            | 
            | 
           42 | 
                */
  | 
        
        
            | 
            | 
           43 | 
               public function get_config_form(MoodleQuickForm $mform): void {
  | 
        
        
            | 
            | 
           44 | 
                   $cohorts = self::get_cohorts();
  | 
        
        
            | 
            | 
           45 | 
                   $mform->addElement('autocomplete', 'cohorts', get_string('selectfromcohort', 'cohort'),
  | 
        
        
            | 
            | 
           46 | 
                       $cohorts, ['multiple' => true]);
  | 
        
        
            | 
            | 
           47 | 
                   $mform->addRule('cohorts', null, 'required', null, 'client');
  | 
        
        
            | 
            | 
           48 | 
               }
  | 
        
        
            | 
            | 
           49 | 
              | 
        
        
            | 
            | 
           50 | 
               /**
  | 
        
        
            | 
            | 
           51 | 
                * Helps to build SQL to retrieve users that matches the current report audience
  | 
        
        
            | 
            | 
           52 | 
                *
  | 
        
        
            | 
            | 
           53 | 
                * @param string $usertablealias
  | 
        
        
            | 
            | 
           54 | 
                * @return array array of three elements [$join, $where, $params]
  | 
        
        
            | 
            | 
           55 | 
                */
  | 
        
        
            | 
            | 
           56 | 
               public function get_sql(string $usertablealias): array {
  | 
        
        
            | 
            | 
           57 | 
                   global $DB;
  | 
        
        
            | 
            | 
           58 | 
              | 
        
        
            | 
            | 
           59 | 
                   $cohorts = $this->get_configdata()['cohorts'];
  | 
        
        
            | 
            | 
           60 | 
                   [$insql, $inparams] = $DB->get_in_or_equal($cohorts, SQL_PARAMS_NAMED, database::generate_param_name('_'));
  | 
        
        
            | 
            | 
           61 | 
              | 
        
        
            | 
            | 
           62 | 
                   $cm = database::generate_alias();
  | 
        
        
            | 
            | 
           63 | 
                   $join = "JOIN {cohort_members} {$cm}
  | 
        
        
            | 
            | 
           64 | 
                              ON ({$cm}.userid = {$usertablealias}.id)";
  | 
        
        
            | 
            | 
           65 | 
              | 
        
        
            | 
            | 
           66 | 
                   return [$join, "{$cm}.cohortid " . $insql, $inparams];
  | 
        
        
            | 
            | 
           67 | 
               }
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
               /**
  | 
        
        
            | 
            | 
           70 | 
                * Return user friendly name of this audience type
  | 
        
        
            | 
            | 
           71 | 
                *
  | 
        
        
            | 
            | 
           72 | 
                * @return string
  | 
        
        
            | 
            | 
           73 | 
                */
  | 
        
        
            | 
            | 
           74 | 
               public function get_name(): string {
  | 
        
        
            | 
            | 
           75 | 
                   return get_string('memberofcohort', 'cohort');
  | 
        
        
            | 
            | 
           76 | 
               }
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
            | 
            | 
           78 | 
               /**
  | 
        
        
            | 
            | 
           79 | 
                * Return the description for the audience.
  | 
        
        
            | 
            | 
           80 | 
                *
  | 
        
        
            | 
            | 
           81 | 
                * @return string
  | 
        
        
            | 
            | 
           82 | 
                */
  | 
        
        
            | 
            | 
           83 | 
               public function get_description(): string {
  | 
        
        
            | 
            | 
           84 | 
                   global $DB;
  | 
        
        
            | 
            | 
           85 | 
              | 
        
        
            | 
            | 
           86 | 
                   $cohortlist = [];
  | 
        
        
            | 
            | 
           87 | 
              | 
        
        
            | 
            | 
           88 | 
                   $cohortids = $this->get_configdata()['cohorts'];
  | 
        
        
            | 
            | 
           89 | 
                   $cohorts = $DB->get_records_list('cohort', 'id', $cohortids, 'name');
  | 
        
        
            | 
            | 
           90 | 
                   foreach ($cohorts as $cohort) {
  | 
        
        
            | 
            | 
           91 | 
                       $cohortlist[] = format_string($cohort->name, true, ['context' => $cohort->contextid, 'escape' => false]);
  | 
        
        
            | 
            | 
           92 | 
                   }
  | 
        
        
            | 
            | 
           93 | 
              | 
        
        
            | 
            | 
           94 | 
                   return $this->format_description_for_multiselect($cohortlist);
  | 
        
        
            | 
            | 
           95 | 
               }
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
               /**
  | 
        
        
            | 
            | 
           98 | 
                * If the current user is able to add this audience.
  | 
        
        
            | 
            | 
           99 | 
                *
  | 
        
        
            | 
            | 
           100 | 
                * @return bool
  | 
        
        
            | 
            | 
           101 | 
                */
  | 
        
        
            | 
            | 
           102 | 
               public function user_can_add(): bool {
  | 
        
        
            | 
            | 
           103 | 
                   // Check system context first.
  | 
        
        
            | 
            | 
           104 | 
                   if (has_capability('moodle/cohort:view', context_system::instance())) {
  | 
        
        
            | 
            | 
           105 | 
                       return true;
  | 
        
        
            | 
            | 
           106 | 
                   }
  | 
        
        
            | 
            | 
           107 | 
                   // If there is at least one category with given permissions, user can add.
  | 
        
        
            | 
            | 
           108 | 
                   return !empty(core_course_category::make_categories_list('moodle/cohort:view'));
  | 
        
        
            | 
            | 
           109 | 
               }
  | 
        
        
            | 
            | 
           110 | 
              | 
        
        
            | 
            | 
           111 | 
               /**
  | 
        
        
            | 
            | 
           112 | 
                * Returns if this audience type is available for the user
  | 
        
        
            | 
            | 
           113 | 
                *
  | 
        
        
            | 
            | 
           114 | 
                * Check if there are available cohorts in the system for this user to use.
  | 
        
        
            | 
            | 
           115 | 
                *
  | 
        
        
            | 
            | 
           116 | 
                * @return bool
  | 
        
        
            | 
            | 
           117 | 
                */
  | 
        
        
            | 
            | 
           118 | 
               public function is_available(): bool {
  | 
        
        
            | 
            | 
           119 | 
                   return !empty(self::get_cohorts());
  | 
        
        
            | 
            | 
           120 | 
               }
  | 
        
        
            | 
            | 
           121 | 
              | 
        
        
            | 
            | 
           122 | 
               /**
  | 
        
        
            | 
            | 
           123 | 
                * If the current user is able to edit this audience.
  | 
        
        
            | 
            | 
           124 | 
                *
  | 
        
        
            | 
            | 
           125 | 
                * @return bool
  | 
        
        
            | 
            | 
           126 | 
                */
  | 
        
        
            | 
            | 
           127 | 
               public function user_can_edit(): bool {
  | 
        
        
            | 
            | 
           128 | 
                   global $DB;
  | 
        
        
            | 
            | 
           129 | 
              | 
        
        
            | 
            | 
           130 | 
                   $canedit = true;
  | 
        
        
            | 
            | 
           131 | 
                   $cohortids = $this->get_configdata()['cohorts'];
  | 
        
        
            | 
            | 
           132 | 
                   $cohorts = $DB->get_records_list('cohort', 'id', $cohortids);
  | 
        
        
            | 
            | 
           133 | 
                   foreach ($cohorts as $cohort) {
  | 
        
        
            | 
            | 
           134 | 
                       $context = context::instance_by_id($cohort->contextid, MUST_EXIST);
  | 
        
        
            | 
            | 
           135 | 
                       $canedit = $canedit && has_capability('moodle/cohort:view', $context);
  | 
        
        
            | 
            | 
           136 | 
                       if ($canedit === false) {
  | 
        
        
            | 
            | 
           137 | 
                           break;
  | 
        
        
            | 
            | 
           138 | 
                       }
  | 
        
        
            | 
            | 
           139 | 
                   }
  | 
        
        
            | 
            | 
           140 | 
              | 
        
        
            | 
            | 
           141 | 
                   return $canedit;
  | 
        
        
            | 
            | 
           142 | 
               }
  | 
        
        
            | 
            | 
           143 | 
              | 
        
        
            | 
            | 
           144 | 
               /**
  | 
        
        
            | 
            | 
           145 | 
                * Cohorts selector.
  | 
        
        
            | 
            | 
           146 | 
                *
  | 
        
        
            | 
            | 
           147 | 
                * @return array
  | 
        
        
            | 
            | 
           148 | 
                */
  | 
        
        
            | 
            | 
           149 | 
               private static function get_cohorts(): array {
  | 
        
        
            | 
            | 
           150 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           151 | 
              | 
        
        
            | 
            | 
           152 | 
                   require_once($CFG->dirroot.'/cohort/lib.php');
  | 
        
        
            | 
            | 
           153 | 
              | 
        
        
            | 
            | 
           154 | 
                   $cohortslist = [];
  | 
        
        
            | 
            | 
           155 | 
              | 
        
        
            | 
            | 
           156 | 
                   // Search cohorts user can view.
  | 
        
        
            | 
            | 
           157 | 
                   $usercohorts = cohort_get_all_cohorts(0, 0);
  | 
        
        
            | 
            | 
           158 | 
              | 
        
        
            | 
            | 
           159 | 
                   // The previous method doesn't check cohorts on system context.
  | 
        
        
            | 
            | 
           160 | 
                   $syscontext = context_system::instance();
  | 
        
        
            | 
            | 
           161 | 
                   $cohorts = array_filter($usercohorts['cohorts'], static function(stdClass $cohort) use ($syscontext): bool {
  | 
        
        
            | 
            | 
           162 | 
                       return ($cohort->contextid != $syscontext->id) || has_capability('moodle/cohort:view', $syscontext);
  | 
        
        
            | 
            | 
           163 | 
                   });
  | 
        
        
            | 
            | 
           164 | 
              | 
        
        
            | 
            | 
           165 | 
                   foreach ($cohorts as $cohort) {
  | 
        
        
            | 
            | 
           166 | 
                       $cohortslist[$cohort->id] = format_string($cohort->name, true, [
  | 
        
        
            | 
            | 
           167 | 
                           'context' => $cohort->contextid,
  | 
        
        
            | 
            | 
           168 | 
                           'escape' => false,
  | 
        
        
            | 
            | 
           169 | 
                       ]);
  | 
        
        
            | 
            | 
           170 | 
                   }
  | 
        
        
            | 
            | 
           171 | 
              | 
        
        
            | 
            | 
           172 | 
                   return $cohortslist;
  | 
        
        
            | 
            | 
           173 | 
               }
  | 
        
        
            | 
            | 
           174 | 
           }
  |