Example #1
0
 public static function process_enrolments($event = null, $instanceid = null)
 {
     global $DB;
     $nbenrolled = 0;
     $possible_unenrolments = array();
     if ($instanceid) {
         // We're processing one particular instance, making sure it's active
         $enrol_attributes_records = $DB->get_records('enrol', array('enrol' => 'attributes', 'status' => 0, 'id' => $instanceid));
     } else {
         // We're processing all active instances,
         // because a user just logged in
         // OR we're running the cron
         $enrol_attributes_records = $DB->get_records('enrol', array('enrol' => 'attributes', 'status' => 0));
         if (!is_null($event)) {
             // Let's check if there are any potential unenroling instances
             $userid = (int) $event->userid;
             $possible_unenrolments = $DB->get_records_sql("SELECT id, enrolid FROM {user_enrolments} WHERE userid = ? AND status = 0 AND enrolid IN ( SELECT id FROM {enrol} WHERE enrol = 'attributes' AND customint1 = 1 ) ", array($userid));
         }
     }
     // are we to unenrol from anywhere?
     foreach ($possible_unenrolments as $id => $user_enrolment) {
         $unenrol_attributes_record = $DB->get_record('enrol', array('enrol' => 'attributes', 'status' => 0, 'customint1' => 1, 'id' => $user_enrolment->enrolid));
         if (!$unenrol_attributes_record) {
             continue;
         }
         $select = 'SELECT DISTINCT u.id FROM {user} u';
         $where = ' WHERE u.id=' . $userid . ' AND u.deleted=0 AND ';
         $arraysyntax = self::attrsyntax_toarray($unenrol_attributes_record->customtext1);
         $arraysql = self::arraysyntax_tosql($arraysyntax);
         $users = $DB->get_records_sql($select . $arraysql['select'] . $where . $arraysql['where'], $arraysql['params']);
         if (!array_key_exists($userid, $users)) {
             $enrol_attributes_instance = new enrol_attributes_plugin();
             $enrol_attributes_instance->unenrol_user($unenrol_attributes_record, (int) $userid);
         }
     }
     // are we to enrol anywhere?
     foreach ($enrol_attributes_records as $enrol_attributes_record) {
         $rules = json_decode($enrol_attributes_record->customtext1)->rules;
         $configured_profilefields = explode(',', get_config('enrol_attributes', 'profilefields'));
         foreach ($rules as $rule) {
             if (!isset($rule->param)) {
                 break;
             }
             if (!in_array($rule->param, $configured_profilefields)) {
                 break 2;
             }
         }
         $enrol_attributes_instance = new enrol_attributes_plugin();
         $enrol_attributes_instance->name = $enrol_attributes_record->name;
         $select = 'SELECT DISTINCT u.id FROM {user} u';
         if ($event) {
             // called by an event, i.e. user login
             $userid = (int) $event->userid;
             $where = ' WHERE u.id=' . $userid;
         } else {
             // called by cron or by construct
             $where = ' WHERE 1=1';
         }
         $where .= ' AND u.deleted=0 AND ';
         $arraysyntax = self::attrsyntax_toarray($enrol_attributes_record->customtext1);
         $arraysql = self::arraysyntax_tosql($arraysyntax);
         $users = $DB->get_records_sql($select . $arraysql['select'] . $where . $arraysql['where'], $arraysql['params']);
         foreach ($users as $user) {
             if (is_enrolled(context_course::instance($enrol_attributes_record->courseid), $user)) {
                 continue;
             }
             $enrol_attributes_instance->enrol_user($enrol_attributes_record, $user->id, $enrol_attributes_record->roleid);
             $nbenrolled++;
         }
     }
     if (!$event && !$instanceid) {
         // we only want output if runnning within the cron
         mtrace('enrol_attributes : enrolled ' . $nbenrolled . ' users.');
     }
     return $nbenrolled;
 }
// 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 <http://www.gnu.org/licenses/>.
/**
 * @package    enrol_attributes
 * @author     Nicolas Dunand <*****@*****.**>
 * @copyright  2012-2015 Université de Lausanne (@link http://www.unil.ch}
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
define('AJAX_SCRIPT', true);
require_once dirname(dirname(dirname(__FILE__))) . '/config.php';
require_once 'lib.php';
require_sesskey();
$courseid = required_param('courseid', PARAM_INT);
$instanceid = required_param('instanceid', PARAM_INT);
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$context = context_course::instance($course->id);
require_login($course);
require_capability('enrol/attributes:config', $context);
if (!$courseid || !$instanceid) {
    print_string('ajax-error', 'enrol_attributes');
    exit;
}
$nbenrolled = enrol_attributes_plugin::process_enrolments(null, $instanceid);
ob_end_clean();
if ($nbenrolled !== false) {
    echo json_encode(get_string('ajax-okforced', 'enrol_attributes', $nbenrolled));
} else {
    echo json_encode(get_string('ajax-error', 'enrol_attributes'));
}
// 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 <http://www.gnu.org/licenses/>.
/**
 * @package    enrol_attributes
 * @author     Nicolas Dunand <*****@*****.**>
 * @copyright  2012-2015 Université de Lausanne (@link http://www.unil.ch}
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
define('AJAX_SCRIPT', true);
require_once dirname(dirname(dirname(__FILE__))) . '/config.php';
require_once 'lib.php';
require_sesskey();
$courseid = required_param('courseid', PARAM_INT);
$instanceid = required_param('instanceid', PARAM_INT);
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$context = context_course::instance($course->id);
require_login($course);
require_capability('enrol/attributes:config', $context);
if (enrol_attributes_plugin::purge_instance($instanceid, $context)) {
    echo json_encode(get_string('ajax-okpurged', 'enrol_attributes'));
} else {
    echo json_encode(get_string('ajax-error', 'enrol_attributes'));
}
Example #4
0
        }
        redirect($return);
    }
}
$PAGE->set_heading($course->fullname);
$PAGE->set_title(get_string('pluginname', 'enrol_attributes'));
$PAGE->requires->jquery();
$PAGE->requires->js('/enrol/attributes/js/jquery.json-2.3.min.js');
$PAGE->requires->js('/enrol/attributes/jsparams.php');
$PAGE->requires->js('/enrol/attributes/js/jquery.booleanEditor.js');
$PAGE->requires->js('/enrol/attributes/js/javascript.js');
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('pluginname', 'enrol_attributes'));
$mform->display();
// DEBUGGING : BEGIN
if ($instanceid) {
    debugging('customtext1= ' . print_r(json_decode($instance->customtext1), true), DEBUG_DEVELOPER);
    $debug_fieldsandrules = enrol_attributes_plugin::attrsyntax_toarray($instance->customtext1);
    debugging('fieldsandrules= ' . print_r($debug_fieldsandrules, true), DEBUG_DEVELOPER);
    $debug_arraysql = enrol_attributes_plugin::arraysyntax_tosql($debug_fieldsandrules);
    debugging('arraysql= ' . print_r($debug_arraysql, true), DEBUG_DEVELOPER);
    $debug_sqlquery = 'SELECT DISTINCT u.id FROM {user} u ' . $debug_arraysql['select'] . ' WHERE ' . $debug_arraysql['where'];
    debugging('sqlquery= ' . print_r($debug_sqlquery, true), DEBUG_DEVELOPER);
    $debug_users = $DB->get_records_sql($debug_sqlquery, $debug_arraysql['params']);
    debugging('countusers= ' . print_r(count($debug_users), true), DEBUG_DEVELOPER);
    //    debugging('force.php DEBUGGING:', DEBUG_DEVELOPER);
    //    $nbenrolled = enrol_attributes_plugin::process_enrolments(null, $instanceid);
    //    debugging('nbenrolled= ' . print_r($nbenrolled, true), DEBUG_DEVELOPER);
}
// DEBUGGING : END
echo $OUTPUT->footer();