Skip to content

Allocation by user profile attributes #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: MOODLE_401_STABLE
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions classes/local/allocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use enrol_programs\local\source\approval;
use enrol_programs\local\source\base;
use enrol_programs\local\source\cohort;
use enrol_programs\local\source\profile;
use enrol_programs\local\source\ecommerce;
use enrol_programs\local\source\manual;
use enrol_programs\local\source\selfallocation;
Expand Down Expand Up @@ -51,6 +52,7 @@ public static function get_source_classes(): array {
selfallocation::get_type() => selfallocation::class,
approval::get_type() => approval::class,
cohort::get_type() => cohort::class,
profile::get_type() => profile::class,
];

if (ecommerce::is_commerce_enabled()) {
Expand Down
17 changes: 17 additions & 0 deletions classes/local/event_observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,27 @@ public static function course_category_deleted(\core\event\course_category_delet
}
}

// When new user profile is created.
public static function user_created(\core\event\user_created $event) {
$updated = \enrol_programs\local\source\profile::fix_allocations(null, $event->relateduserid);
if ($updated) {
allocation::fix_user_enrolments(null, $event->relateduserid);
}
}

// When user profile is updated.
public static function user_updated(\core\event\user_updated $event) {
$updated = \enrol_programs\local\source\profile::fix_allocations(null, $event->relateduserid);
if ($updated) {
allocation::fix_user_enrolments(null, $event->relateduserid);
}
}

public static function user_deleted(\core\event\user_deleted $event) {
allocation::deleted_user_cleanup($event->objectid);
}


public static function cohort_member_added(\core\event\cohort_member_added $event) {
$updated = \enrol_programs\local\source\cohort::fix_allocations(null, $event->relateduserid);
if ($updated) {
Expand Down
107 changes: 107 additions & 0 deletions classes/local/form/source_profile_edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.

namespace enrol_programs\local\form;

use enrol_programs\local\program;
use enrol_programs\local\allocation;
use enrol_programs\local\source\profile;

/**
* Edit cohort allocation settings.
*
* @package enrol_programs
* @copyright 2025
* @author Johnny Tsheke
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class source_profile_edit extends \local_openlms\dialog_form {

protected function definition() {
global $DB, $CFG, $PAGE, $COURSE;
$mform = $this->_form;
$context = $this->_customdata['context'];
$source = $this->_customdata['source'];
$program = $this->_customdata['program'];

// Daily version for javascript files to load. This is to avoid cache issues
//$todayversion = 'tv='. mktime(0, 0, 0, date("m"), date("d"), date("Y")).''; //timestanp
$todayversion = 'tv='.time().'';

$mform->addElement('select', 'enable', get_string('active'), ['1' => get_string('yes'), '0' => get_string('no')]);
$mform->setDefault('enable', $source->enable);
if ($source->hasallocations) {
$mform->hardFreeze('enable');
}

// load jquery librairy for the pop up
$urlstyle= new \moodle_url($CFG->wwwroot . '/enrol/programs/style-profile.css'.'?'."$todayversion");
$mform->addElement('html', "<link href='$urlstyle' rel='stylesheet'/>");
$plugins = [];
require($CFG->libdir . '/jquery/plugins.php');
foreach ($plugins as $ptype => $files) {
foreach ($files['files'] as $file) {
if(file_exists($CFG->libdir . '/jquery/' . $file)){
$ulrfile = new \moodle_url($CFG->wwwroot . '/lib/jquery/'.$file.'?'."$todayversion");
if($ptype == 'ui-css'){
$mform->addElement('html', "<link href='$ulrfile' rel='stylesheet'/>");
}else{
$mform->addElement('html', "<script src='$ulrfile'></script>");
}

break;
}
}
}
//end style
$mform->addElement('textarea', 'datajson', get_string('attrsyntax', 'enrol_programs'));
$mform->addHelpButton('datajson', 'attrsyntax', 'enrol_programs');

$mform->addElement('html', '<div class="alert alert-warning alert-block fade in"
role="alert" data-aria-autofocus="true">' . get_string('listitem_description', 'enrol_programs') . '</div>');

$mform->addElement('hidden', 'programid');
$mform->setType('programid', PARAM_INT);
$mform->setDefault('programid', $program->id);
$mform->addElement('hidden', 'courseid');
$mform->setType('courseid', PARAM_INT);
$mform->setDefault('courseid', $COURSE->id);

$mform->addElement('hidden', 'type');
$mform->setType('type', PARAM_ALPHANUMEXT);
$mform->setDefault('type', $source->type);

$this->add_action_buttons(true, get_string('update'));
$this->set_data($source);
//----

//load javascript data for profile rules settings
$urljsparams= new \moodle_url($CFG->wwwroot . '/enrol/programs/jsparams.php'.'?'."$todayversion");
$mform->addElement('html', "<script src='$urljsparams'></script>");
$ulreditor = new \moodle_url($CFG->wwwroot . '/enrol/programs/js/jquery.booleanEditor.min.js'.'?'."$todayversion");
$mform->addElement('html', "<script src='$ulreditor'></script>");
$urljs = new \moodle_url($CFG->wwwroot . '/enrol/programs/js/javascript.min.js'.'?'."$todayversion");
$mform->addElement('html', "<script src='$urljs'></script>");
$urlstr = new \moodle_url($CFG->wwwroot . '/lib/javascript-static.js'.'?'."$todayversion");
$mform->addElement('html', "<script src='$urlstr'></script>");
}

public function validation($data, $files) {
$errors = parent::validation($data, $files);

return $errors;
}
}
Loading