diff --git a/db/access.php b/db/access.php new file mode 100644 index 0000000..a4f62b8 --- /dev/null +++ b/db/access.php @@ -0,0 +1,43 @@ +. + +/** + * Capabilities + * + * @package report_progress + * @copyright 2008 Sam Marshall + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$capabilities = array( + + 'report/ilbsplit:view' => array( + 'riskbitmask' => RISK_PERSONAL, + 'captype' => 'read', + 'contextlevel' => CONTEXT_COURSE, + 'archetypes' => array( + 'teacher' => CAP_ALLOW, + 'editingteacher' => CAP_ALLOW, + 'manager' => CAP_ALLOW + ), + + 'clonepermissionsfrom' => 'coursereport/progress:view', + ) +); + + diff --git a/db/install.php b/db/install.php new file mode 100644 index 0000000..8101862 --- /dev/null +++ b/db/install.php @@ -0,0 +1,32 @@ +. + +/** + * Post installation and migration code. + * + * @package report + * @subpackage progress + * @copyright 2011 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +function xmldb_report_ilbsplit_install() { + global $DB; + +} + diff --git a/form.php b/form.php new file mode 100644 index 0000000..85cfc72 --- /dev/null +++ b/form.php @@ -0,0 +1,85 @@ +. + +/** + * This file defines the user filter form + * + * @package report-ilbsplit + * @copyrigth 2014 Interlegis (http://www.interlegis.leg.br) + * + * @author Sesostris Vieira + * + * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.0.html + */ + +if (!defined('MOODLE_INTERNAL')) { + die('Direct access to this script is forbidden.'); +} + +require_once("$CFG->libdir/formslib.php"); + +class split_form extends moodleform { + protected $_courseid; + protected $_options; + + function split_form($courseid, $options, $action=null, $customdata=null, $method='get', $target='', $attributes=null, $editable=true) { + $this->_courseid = $courseid; + $this->_options = $options; + parent::moodleform($action, $customdata, $method, $target, $attributes, $editable); + } + + public function definition() { + global $CFG; + global $DB; + + $mform = $this->_form; // Don't forget the underscore! + $courseid = $this->_courseid; + $options = $this->_options; + + $course = get_course($courseid); + $courses = $DB->get_records('course', array('category'=>$course->category)); + + // Source options + foreach ($options as $key=>$option) { + $mform->addElement('header', "role_header_{$key}", $option[0]); + foreach ($option[1] as $roleid=>$role) { + $description = get_string('role_option','report_ilbsplit', $role); + $mform->addElement('radio', 'role', '', $description, "{$key}:{$roleid}"); + } + } + $mform->addRule('role', null, 'required'); + + // Courses + $coursesarray = array(); + foreach ($courses as $c) { + if ($c->id !== $courseid) { + $coursesarray[$c->id] = $c->fullname; + } + } + + $mform->addElement('select', 'target', get_string('target_course','report_ilbsplit'), $coursesarray); + $mform->addRule('target', null, 'required'); + $mform->addElement('text', 'qty', get_string('splitqty', 'report_ilbsplit')); + $mform->setType('qty', PARAM_INT); + $mform->addRule('qty', null, 'required'); + $mform->closeHeaderBefore('target'); + + $mform->addElement('submit', 'filterbutton', get_string('split', 'report_ilbsplit')); + $mform->addElement('hidden', 'course', $courseid); +// $mform->closeHeaderBefore('filterbutton'); + } +} diff --git a/index.php b/index.php new file mode 100644 index 0000000..d2cd04f --- /dev/null +++ b/index.php @@ -0,0 +1,161 @@ +. + +/** + * ILB Enrol Confirmation reports + * + * @package report + * @subpackage ilbsplit + * @copyright 2008 Sam Marshall + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require('../../config.php'); +require("{$CFG->dirroot}/enrol/locallib.php"); +require_once("$CFG->dirroot/user/profile/lib.php"); +require_once('form.php'); + +// Get course +$id = required_param('course',PARAM_INT); +$course = $DB->get_record('course',array('id'=>$id)); + +if (!$course) { + print_error('invalidcourseid'); +} + +$context = context_course::instance($course->id); + +$url = new moodle_url('/report/ilbsplit/index.php', array('course'=>$id)); +$PAGE->set_url($url); +$PAGE->set_pagelayout('report'); + +require_login($course); + +// Check basic permission +require_capability('report/ilbsplit:view',$context); + +// Get group mode +$group = groups_get_course_group($course,true); // Supposed to verify group +if ($group===0 && $course->groupmode==SEPARATEGROUPS) { + require_capability('moodle/site:accessallgroups',$context); +} + +// Get data for user filtering +$manager = new course_enrolment_manager($PAGE, $course); + +$options = array(); +$has_options = false; + +foreach ($manager->get_enrolment_instances() as $e) { + $options[$e->id] = array("{$e->name} ({$e->enrol})", array()); + $m = new course_enrolment_manager($PAGE, $course, $e->id); + foreach ($m->get_all_roles() as $role) { + $tm = new course_enrolment_manager($PAGE, $course, $e->id, $role->id); + $tu = $tm->get_total_users(); + if ($tu > 0) { + $options[$e->id][1][$role->id] = array('localname' => $role->localname, 'totalusers' => $tu); + $has_options = true; + } + } +} +$pagetitle = get_string('title','report_ilbsplit'); +$PAGE->set_title($pagetitle); +$PAGE->set_heading($course->fullname); +echo $OUTPUT->header(); +echo $OUTPUT->heading($pagetitle); + +if (!$has_options) { + echo $OUTPUT->box(get_string('nouserstosplit', 'report_ilbsplit'), 'center'); + echo $OUTPUT->footer(); + die; +} + +$mform = new split_form($course->id, $options); + +if ($formdata = $mform->get_data()) { + $role = explode(':', $formdata->role); + $enrolid = $role[0]; + $roleid = $role[1]; + $targetid = $formdata->target; + $qty = $formdata->qty; + + $course_target = $DB->get_record('course',array('id'=>$targetid)); + + if (!$course_target) { + print_error('invalidcourseid'); + } + + $context_target = context_course::instance($course_target->id); + $manager_target = new course_enrolment_manager($PAGE, $course_target); + + if (!array_key_exists($enrolid, $manager->get_enrolment_instances())) { + echo $OUTPUT->box(get_string('invalidenrolid', 'report_ilbsplit'), 'center'); + echo $OUTPUT->footer(); + die; + } + + $enrol = $manager->get_enrolment_instances()[$enrolid]; + $enrol_target = null; + + foreach ($manager_target->get_enrolment_instances() as $et) { + if ($et->enrol == $enrol->enrol) { + $enrol_target = $et; + break; + } + } + + if (is_null($enrol_target)) { + echo $OUTPUT->box(get_string('noenroltarget', 'report_ilbsplit'), 'center'); + echo $OUTPUT->footer(); + die; + } + + // Mount parameters and sql-updates + + $params = array('course_source' => $course->id, + 'course_target' => $course_target->id, + 'enrol_source' => $enrol->id, + 'enrol_target' => $enrol_target->id, + 'context_source' => $context->id, + 'context_target' => $context_target->id, + 'roleid' => $roleid, + 'qty' => $qty); + + $sql_transfer_enrols = 'update {user_enrolments} set enrolid = :enrol_target where enrolid = :enrol_source and userid in (select userid from {role_assignments} where roleid = :roleid and contextid = :context_source limit :qty)'; + $sql_transfer_roles = 'update {role_assignments} set contextid = :context_target where roleid = :roleid and contextid = :context_source and userid in (select userid from {user_enrolments} where enrolid = :enrol_target)'; + + // Execute updates + + $transaction = $DB->start_delegated_transaction(); + + try { + $DB->execute($sql_transfer_enrols, $params); + $DB->execute($sql_transfer_roles, $params); + $transaction->allow_commit(); + } catch(Exception $e) { + $transaction->rollback($e); + echo $OUTPUT->box(get_string('nouserstransfered', 'report_ilbsplit'), 'center'); + echo $OUTPUT->footer(); + die; + } + + echo $OUTPUT->box(get_string('userstransfered', 'report_ilbsplit'), 'center'); +} else { + echo $OUTPUT->box(get_string('alert', 'report_ilbsplit'), 'center'); + $mform->display(); +} + +echo $OUTPUT->footer(); diff --git a/lang/en/report_ilbsplit.php b/lang/en/report_ilbsplit.php new file mode 100644 index 0000000..d92523a --- /dev/null +++ b/lang/en/report_ilbsplit.php @@ -0,0 +1,40 @@ +. + +/** + * Lang strings + * + * @package report + * @subpackage progress + * @copyright 2008 Sam Marshall + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +$string['pluginname'] = 'ILB Split Course'; +$string['title'] = 'ILB Split Course Report'; +$string['alert'] = 'ALERT +

Users transferred by this plugin may be confused with users who are already in the destination course, preventing any subsequent separation

Be careful'; +$string['nouserstosplit'] = 'No users to split'; +$string['role_option'] = '{$a->totalusers} enrolled users as {$a->localname}'; +$string['target_course'] = 'Target course'; +$string['split'] = 'Split'; +$string['splitqty'] = 'Split quantity'; +$string['invalidenrolid'] = 'Invalid enrol ID'; +$string['noenroltarget'] = 'No enrol method in target course that can receipt these users'; +$string['nouserstransfered'] = 'No user has been transferred'; +$string['userstransfered'] = 'Users has been transferred'; +$string['page-report-ilbsplit-x'] = 'Page to split users into different courses'; +$string['page-report-ilbsplit-index'] = 'Page index to split users into different courses'; diff --git a/lang/pt_br/report_ilbsplit.php b/lang/pt_br/report_ilbsplit.php new file mode 100644 index 0000000..4810336 --- /dev/null +++ b/lang/pt_br/report_ilbsplit.php @@ -0,0 +1,40 @@ +. + +/** + * Lang strings + * + * @package report + * @subpackage progress + * @copyright 2008 Sam Marshall + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +$string['pluginname'] = 'Divisor de cursos do ILB'; +$string['title'] = 'Rotina de divisão de turmas'; +$string['alert'] = 'ALERTA +

Usuários transferidos por este plugin poderão se misturar com os usuários que já se encontram no curso de destino, impossibilitando uma separação posterior

Seja cuidadoso'; +$string['nouserstosplit'] = 'Não há usuários para dividir'; +$string['role_option'] = '{$a->totalusers} usuários matriculados como {$a->localname}'; +$string['target_course'] = 'Curso de destino'; +$string['split'] = 'Dividir'; +$string['splitqty'] = 'Quantidade a transferir'; +$string['invalidenrolid'] = 'Identificação de enrol inválido'; +$string['noenroltarget'] = 'Nenhum método de inscrição no curso de destino que possa receber esses usuários'; +$string['nouserstransfered'] = 'Nenhum usuário pode ser transferido'; +$string['userstransfered'] = 'Os usuários foram transferidos com sucesso!'; +$string['page-report-ilbsplit-x'] = 'Página para dividir usuários entre diferentes cursos'; +$string['page-report-ilbsplit-index'] = 'Página índice para dividir usuários entre diferentes cursos'; diff --git a/lib.php b/lib.php new file mode 100644 index 0000000..e48fcad --- /dev/null +++ b/lib.php @@ -0,0 +1,65 @@ +. + +/** + * This file contains functions used by the progress report + * + * @package report + * @subpackage progress + * @copyright 2009 Sam Hemelryk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +/** + * This function extends the navigation with the report items + * + * @param navigation_node $navigation The navigation node to extend + * @param stdClass $course The course to object for the report + * @param stdClass $context The context of the course + */ +function report_ilbsplit_extend_navigation_course($navigation, $course, $context) { + global $CFG, $OUTPUT; + + $showonnavigation = has_capability('report/ilbsplit:view', $context); + $group = groups_get_course_group($course,true); // Supposed to verify group + if($group===0 && $course->groupmode==SEPARATEGROUPS) { + $showonnavigation = ($showonnavigation && has_capability('moodle/site:accessallgroups', $context)); + } + + if ($showonnavigation) { + $url = new moodle_url('/report/ilbsplit/index.php', array('course'=>$course->id)); + $navigation->add(get_string('pluginname','report_ilbsplit'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/report', '')); + } +} + +/** + * Return a list of page types + * @param string $pagetype current page type + * @param stdClass $parentcontext Block's parent context + * @param stdClass $currentcontext Current context of block + * @return array + */ +function report_ilbsplit_page_type_list($pagetype, $parentcontext, $currentcontext) { + $array = array( + '*' => get_string('page-x', 'pagetype'), + 'report-*' => get_string('page-report-x', 'pagetype'), + 'report-ilbsplit-*' => get_string('page-report-ilbsplit-x', 'report_ilbsplit'), + 'report-ilbsplit-index' => get_string('page-report-ilbsplit-index', 'report_ilbsplit'), + ); + return $array; +} diff --git a/settings.php b/settings.php new file mode 100644 index 0000000..9a1ea79 --- /dev/null +++ b/settings.php @@ -0,0 +1,3 @@ +. + +/** + * Version details + * + * @package report + * @subpackage progress + * @copyright 2008 Sam Marshall + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +$plugin->version = 2013050100; // The current plugin version (Date: YYYYMMDDXX) +$plugin->requires = 2013050100; // Requires this Moodle version +$plugin->component = 'report_ilbsplit'; // Full name of the plugin (used for diagnostics)