Differences Between: [Versions 310 and 311] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]
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 * Fee enrolment plugin. 19 * 20 * This plugin allows you to set up paid courses. 21 * 22 * @package enrol_fee 23 * @copyright 2019 Shamim Rezaie <shamim@moodle.com> 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 27 /** 28 * Fee enrolment plugin implementation. 29 * 30 * @copyright 2019 Shamim Rezaie <shamim@moodle.com> 31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 32 */ 33 class enrol_fee_plugin extends enrol_plugin { 34 35 /** 36 * Returns the list of currencies that the payment subsystem supports and therefore we can work with. 37 * 38 * @return array[currencycode => currencyname] 39 */ 40 public function get_possible_currencies(): array { 41 $codes = \core_payment\helper::get_supported_currencies(); 42 43 $currencies = []; 44 foreach ($codes as $c) { 45 $currencies[$c] = new lang_string($c, 'core_currencies'); 46 } 47 48 uasort($currencies, function($a, $b) { 49 return strcmp($a, $b); 50 }); 51 52 return $currencies; 53 } 54 55 /** 56 * Returns optional enrolment information icons. 57 * 58 * This is used in course list for quick overview of enrolment options. 59 * 60 * We are not using single instance parameter because sometimes 61 * we might want to prevent icon repetition when multiple instances 62 * of one type exist. One instance may also produce several icons. 63 * 64 * @param array $instances all enrol instances of this type in one course 65 * @return array of pix_icon 66 */ 67 public function get_info_icons(array $instances) { 68 $found = false; 69 foreach ($instances as $instance) { 70 if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) { 71 continue; 72 } 73 if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) { 74 continue; 75 } 76 $found = true; 77 break; 78 } 79 if ($found) { 80 return array(new pix_icon('icon', get_string('pluginname', 'enrol_fee'), 'enrol_fee')); 81 } 82 return array(); 83 } 84 85 public function roles_protected() { 86 // Users with role assign cap may tweak the roles later. 87 return false; 88 } 89 90 public function allow_unenrol(stdClass $instance) { 91 // Users with unenrol cap may unenrol other users manually - requires enrol/fee:unenrol. 92 return true; 93 } 94 95 public function allow_manage(stdClass $instance) { 96 // Users with manage cap may tweak period and status - requires enrol/fee:manage. 97 return true; 98 } 99 100 public function show_enrolme_link(stdClass $instance) { 101 return ($instance->status == ENROL_INSTANCE_ENABLED); 102 } 103 104 /** 105 * Returns true if the user can add a new instance in this course. 106 * @param int $courseid 107 * @return boolean 108 */ 109 public function can_add_instance($courseid) { 110 $context = context_course::instance($courseid, MUST_EXIST); 111 112 if (empty(\core_payment\helper::get_supported_currencies())) { 113 return false; 114 } 115 116 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/fee:config', $context)) { 117 return false; 118 } 119 120 // Multiple instances supported - different cost for different roles. 121 return true; 122 } 123 124 /** 125 * We are a good plugin and don't invent our own UI/validation code path. 126 * 127 * @return boolean 128 */ 129 public function use_standard_editing_ui() { 130 return true; 131 } 132 133 /** 134 * Add new instance of enrol plugin. 135 * @param object $course 136 * @param array $fields instance fields 137 * @return int id of new instance, null if can not be created 138 */ 139 public function add_instance($course, array $fields = null) { 140 if ($fields && !empty($fields['cost'])) { 141 $fields['cost'] = unformat_float($fields['cost']); 142 } 143 return parent::add_instance($course, $fields); 144 } 145 146 /** 147 * Update instance of enrol plugin. 148 * @param stdClass $instance 149 * @param stdClass $data modified instance fields 150 * @return boolean 151 */ 152 public function update_instance($instance, $data) { 153 if ($data) { 154 $data->cost = unformat_float($data->cost); 155 } 156 return parent::update_instance($instance, $data); 157 } 158 159 /** 160 * Creates course enrol form, checks if form submitted 161 * and enrols user if necessary. It can also redirect. 162 * 163 * @param stdClass $instance 164 * @return string html text, usually a form in a text box 165 */ 166 public function enrol_page_hook(stdClass $instance) { 167 global $CFG, $USER, $OUTPUT, $PAGE, $DB; 168 169 ob_start(); 170 171 if ($DB->record_exists('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) { 172 return ob_get_clean(); 173 } 174 175 if ($instance->enrolstartdate != 0 && $instance->enrolstartdate > time()) { 176 return ob_get_clean(); 177 } 178 179 if ($instance->enrolenddate != 0 && $instance->enrolenddate < time()) { 180 return ob_get_clean(); 181 } 182 183 $course = $DB->get_record('course', array('id' => $instance->courseid)); 184 $context = context_course::instance($course->id); 185 186 $shortname = format_string($course->shortname, true, array('context' => $context)); 187 $strloginto = get_string("loginto", "", $shortname); 188 $strcourses = get_string("courses"); 189 190 // Pass $view=true to filter hidden caps if the user cannot see them. 191 if ($users = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC', 192 '', '', '', '', false, true)) { 193 $users = sort_by_roleassignment_authority($users, $context); 194 $teacher = array_shift($users); 195 } else { 196 $teacher = false; 197 } 198 199 if ( (float) $instance->cost <= 0 ) { 200 $cost = (float) $this->get_config('cost'); 201 } else { 202 $cost = (float) $instance->cost; 203 } 204 205 if (abs($cost) < 0.01) { // No cost, other enrolment methods (instances) should be used. 206 echo '<p>'.get_string('nocost', 'enrol_fee').'</p>'; 207 } else { 208 209 $data = [ 210 'isguestuser' => isguestuser(), 211 'cost' => \core_payment\helper::get_cost_as_string($cost, $instance->currency), 212 'instanceid' => $instance->id, 213 'description' => get_string('purchasedescription', 'enrol_fee', 214 format_string($course->fullname, true, ['context' => $context])), 215 'successurl' => \enrol_fee\payment\service_provider::get_success_url('fee', $instance->id)->out(false), 216 ]; 217 echo $OUTPUT->render_from_template('enrol_fee/payment_region', $data); 218 } 219 220 return $OUTPUT->box(ob_get_clean()); 221 } 222 223 /** 224 * Restore instance and map settings. 225 * 226 * @param restore_enrolments_structure_step $step 227 * @param stdClass $data 228 * @param stdClass $course 229 * @param int $oldid 230 */ 231 public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) { 232 global $DB; 233 if ($step->get_task()->get_target() == backup::TARGET_NEW_COURSE) { 234 $merge = false; 235 } else { 236 $merge = array( 237 'courseid' => $data->courseid, 238 'enrol' => $this->get_name(), 239 'roleid' => $data->roleid, 240 'cost' => $data->cost, 241 'currency' => $data->currency, 242 ); 243 } 244 if ($merge and $instances = $DB->get_records('enrol', $merge, 'id')) { 245 $instance = reset($instances); 246 $instanceid = $instance->id; 247 } else { 248 $instanceid = $this->add_instance($course, (array) $data); 249 } 250 $step->set_mapping('enrol', $oldid, $instanceid); 251 } 252 253 /** 254 * Restore user enrolment. 255 * 256 * @param restore_enrolments_structure_step $step 257 * @param stdClass $data 258 * @param stdClass $instance 259 * @param int $oldinstancestatus 260 * @param int $userid 261 */ 262 public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) { 263 $this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, $data->status); 264 } 265 266 /** 267 * Return an array of valid options for the status. 268 * 269 * @return array 270 */ 271 protected function get_status_options() { 272 $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'), 273 ENROL_INSTANCE_DISABLED => get_string('no')); 274 return $options; 275 } 276 277 /** 278 * Return an array of valid options for the roleid. 279 * 280 * @param stdClass $instance 281 * @param context $context 282 * @return array 283 */ 284 protected function get_roleid_options($instance, $context) { 285 if ($instance->id) { 286 $roles = get_default_enrol_roles($context, $instance->roleid); 287 } else { 288 $roles = get_default_enrol_roles($context, $this->get_config('roleid')); 289 } 290 return $roles; 291 } 292 293 294 /** 295 * Add elements to the edit instance form. 296 * 297 * @param stdClass $instance 298 * @param MoodleQuickForm $mform 299 * @param context $context 300 * @return bool 301 */ 302 public function edit_instance_form($instance, MoodleQuickForm $mform, $context) { 303 304 $mform->addElement('text', 'name', get_string('custominstancename', 'enrol')); 305 $mform->setType('name', PARAM_TEXT); 306 307 $options = $this->get_status_options(); 308 $mform->addElement('select', 'status', get_string('status', 'enrol_fee'), $options); 309 $mform->setDefault('status', $this->get_config('status')); 310 311 $accounts = \core_payment\helper::get_payment_accounts_menu($context); 312 if ($accounts) { 313 $accounts = ((count($accounts) > 1) ? ['' => ''] : []) + $accounts; 314 $mform->addElement('select', 'customint1', get_string('paymentaccount', 'payment'), $accounts); 315 } else { 316 $mform->addElement('static', 'customint1_text', get_string('paymentaccount', 'payment'), 317 html_writer::span(get_string('noaccountsavilable', 'payment'), 'alert alert-danger')); 318 $mform->addElement('hidden', 'customint1'); 319 $mform->setType('customint1', PARAM_INT); 320 } 321 $mform->addHelpButton('customint1', 'paymentaccount', 'enrol_fee'); 322 323 $mform->addElement('text', 'cost', get_string('cost', 'enrol_fee'), array('size' => 4)); 324 $mform->setType('cost', PARAM_RAW); 325 $mform->setDefault('cost', format_float($this->get_config('cost'), 2, true)); 326 327 $supportedcurrencies = $this->get_possible_currencies(); 328 $mform->addElement('select', 'currency', get_string('currency', 'enrol_fee'), $supportedcurrencies); 329 $mform->setDefault('currency', $this->get_config('currency')); 330 331 $roles = $this->get_roleid_options($instance, $context); 332 $mform->addElement('select', 'roleid', get_string('assignrole', 'enrol_fee'), $roles); 333 $mform->setDefault('roleid', $this->get_config('roleid')); 334 335 $options = array('optional' => true, 'defaultunit' => 86400); 336 $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_fee'), $options); 337 $mform->setDefault('enrolperiod', $this->get_config('enrolperiod')); 338 $mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_fee'); 339 340 $options = array('optional' => true); 341 $mform->addElement('date_time_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_fee'), $options); 342 $mform->setDefault('enrolstartdate', 0); 343 $mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_fee'); 344 345 $options = array('optional' => true); 346 $mform->addElement('date_time_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_fee'), $options); 347 $mform->setDefault('enrolenddate', 0); 348 $mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_fee'); 349 350 if (enrol_accessing_via_instance($instance)) { 351 $warningtext = get_string('instanceeditselfwarningtext', 'core_enrol'); 352 $mform->addElement('static', 'selfwarn', get_string('instanceeditselfwarning', 'core_enrol'), $warningtext); 353 } 354 } 355 356 /** 357 * Perform custom validation of the data used to edit the instance. 358 * 359 * @param array $data array of ("fieldname"=>value) of submitted data 360 * @param array $files array of uploaded files "element_name"=>tmp_file_path 361 * @param object $instance The instance loaded from the DB 362 * @param context $context The context of the instance we are editing 363 * @return array of "element_name"=>"error_description" if there are errors, 364 * or an empty array if everything is OK. 365 * @return void 366 */ 367 public function edit_instance_validation($data, $files, $instance, $context) { 368 $errors = array(); 369 370 if (!empty($data['enrolenddate']) and $data['enrolenddate'] < $data['enrolstartdate']) { 371 $errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_fee'); 372 } 373 374 $cost = str_replace(get_string('decsep', 'langconfig'), '.', $data['cost']); 375 if (!is_numeric($cost)) { 376 $errors['cost'] = get_string('costerror', 'enrol_fee'); 377 } 378 379 $validstatus = array_keys($this->get_status_options()); 380 $validcurrency = array_keys($this->get_possible_currencies()); 381 $validroles = array_keys($this->get_roleid_options($instance, $context)); 382 $tovalidate = array( 383 'name' => PARAM_TEXT, 384 'status' => $validstatus, 385 'currency' => $validcurrency, 386 'roleid' => $validroles, 387 'enrolperiod' => PARAM_INT, 388 'enrolstartdate' => PARAM_INT, 389 'enrolenddate' => PARAM_INT 390 ); 391 392 $typeerrors = $this->validate_param_types($data, $tovalidate); 393 $errors = array_merge($errors, $typeerrors); 394 395 if ($data['status'] == ENROL_INSTANCE_ENABLED && 396 (!$data['customint1'] 397 || !array_key_exists($data['customint1'], \core_payment\helper::get_payment_accounts_menu($context)))) { 398 $errors['status'] = 'Enrolments can not be enabled without specifying the payment account'; 399 } 400 401 return $errors; 402 } 403 404 /** 405 * Execute synchronisation. 406 * @param progress_trace $trace 407 * @return int exit code, 0 means ok 408 */ 409 public function sync(progress_trace $trace) { 410 $this->process_expirations($trace); 411 return 0; 412 } 413 414 /** 415 * Is it possible to delete enrol instance via standard UI? 416 * 417 * @param stdClass $instance 418 * @return bool 419 */ 420 public function can_delete_instance($instance) { 421 $context = context_course::instance($instance->courseid); 422 return has_capability('enrol/fee:config', $context); 423 } 424 425 /** 426 * Is it possible to hide/show enrol instance via standard UI? 427 * 428 * @param stdClass $instance 429 * @return bool 430 */ 431 public function can_hide_show_instance($instance) { 432 $context = context_course::instance($instance->courseid); 433 return has_capability('enrol/fee:config', $context); 434 } 435 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body