/** * Generate role export xml file. * * @param $roleid * @return string */ public static function get_export_xml($roleid) { global $DB; $role = $DB->get_record('role', array('id' => $roleid), '*', MUST_EXIST); $dom = new DOMDocument('1.0', 'UTF-8'); $top = $dom->createElement('role'); $dom->appendChild($top); $top->appendChild($dom->createElement('shortname', $role->shortname)); $top->appendChild($dom->createElement('name', htmlspecialchars($role->name, ENT_COMPAT | ENT_HTML401, 'UTF-8'))); $top->appendChild($dom->createElement('description', htmlspecialchars($role->description, ENT_COMPAT | ENT_HTML401, 'UTF-8'))); $top->appendChild($dom->createElement('archetype', $role->archetype)); $contextlevels = $dom->createElement('contextlevels'); $top->appendChild($contextlevels); foreach (get_role_contextlevels($roleid) as $level) { $name = context_helper::get_class_for_level($level); $name = preg_replace('/^context_/', '', $name); $contextlevels->appendChild($dom->createElement('level', $name)); } foreach (array('assign', 'override', 'switch') as $type) { $allows = $dom->createElement('allow' . $type); $top->appendChild($allows); $records = $DB->get_records('role_allow_' . $type, array('roleid' => $roleid), "allow{$type} ASC"); foreach ($records as $record) { if (!($ar = $DB->get_record('role', array('id' => $record->{'allow' . $type})))) { continue; } $allows->appendChild($dom->createElement('shortname', $ar->shortname)); } } $permissions = $dom->createElement('permissions'); $top->appendChild($permissions); $capabilities = $DB->get_records_sql_menu("SELECT capability, permission\n FROM {role_capabilities}\n WHERE contextid = :syscontext AND roleid = :roleid\n ORDER BY capability ASC", array('syscontext' => context_system::instance()->id, 'roleid' => $roleid)); $allcapabilities = $DB->get_records('capabilities', array(), 'name ASC'); foreach ($allcapabilities as $cap) { if (!isset($capabilities[$cap->name])) { $permissions->appendChild($dom->createElement('inherit', $cap->name)); } } foreach ($capabilities as $capability => $permission) { if ($permission == CAP_ALLOW) { $permissions->appendChild($dom->createElement('allow', $capability)); } } foreach ($capabilities as $capability => $permission) { if ($permission == CAP_PREVENT) { $permissions->appendChild($dom->createElement('prevent', $capability)); } } foreach ($capabilities as $capability => $permission) { if ($permission == CAP_PROHIBIT) { $permissions->appendChild($dom->createElement('prohibit', $capability)); } } return $dom->saveXML(); }
/** * Returns the name of specified context level * * @static * @param int $contextlevel * @return string name of the context level */ public static function get_level_name($contextlevel) { $classname = context_helper::get_class_for_level($contextlevel); return $classname::get_level_name(); }
/** * Process the course data in the xml * * @param $groupnode * @param $xpath * @throws Exception * @throws coding_exception * @throws dml_missing_record_exception */ private function process_course_group_node($groupnode, $xpath) { global $DB, $CFG; $course = new stdClass(); if ($groupnode->getAttribute("recstatus") != 3) { $shortdesc = $xpath->evaluate("description/short", $groupnode)->item(0); if ($shortdesc) { $course->shortname = $shortdesc->nodeValue; } $longdesc = $xpath->evaluate("description/long", $groupnode)->item(0); if ($longdesc) { $course->fullname = $longdesc->nodeValue; } $idnumber = $xpath->evaluate("sourcedid/id", $groupnode)->item(0); if ($idnumber) { $course->idnumber = htmlspecialchars_decode($idnumber->nodeValue); } $course->format = 'topics'; $course->visible = 0; $course->timecreated = time(); $course->startdate = time(); $course->sortorder = 0; $parentgroup = $xpath->evaluate("relationship/sourcedid/id", $groupnode)->item(0); if ($parentgroup) { $parentid = $DB->get_field_select('course_categories', 'id', 'description=\'' . htmlspecialchars_decode($parentgroup->nodeValue) . '\''); if ($parentid) { $course->category = $parentid; } else { $course->category = 4; } } else { $course->category = 4; } $settings = $xpath->evaluate("extension/settings", $groupnode); foreach ($settings as $setting) { $key = $xpath->evaluate("setting", $setting)->item(0)->nodeValue; $val = $xpath->evaluate("value", $setting)->item(0)->nodeValue; $course->{$key} = $val; } if (!$DB->get_field('course', 'id', array('idnumber' => $course->idnumber))) { // New Course. if (!isset($course->numsections)) { $course->numsections = 10; } $courseid = $DB->insert_record('course', $course); // Setup the blocks. $course = $DB->get_record('course', array('id' => $courseid)); blocks_add_default_course_blocks($course); $section = new stdClass(); $section->course = $course->id; // Create a default section. $section->section = 0; $section->summaryformat = FORMAT_HTML; $section->id = $DB->insert_record("course_sections", $section); $enrol = enrol_get_plugin('manual'); if ($audroleid = $DB->get_field('role', 'id', array('shortname' => 'auditor'))) { $enrol->add_instance($course, array('roleid' => $audroleid)); } else { $enrol->add_instance($course); } $coursemanagement = new stdClass(); // Get the start and end date for the course. $begin = $xpath->evaluate("timeframe/begin", $groupnode)->item(0); if ($begin) { $coursemanagement->startdate = $begin->nodeValue; } $end = $xpath->evaluate("timeframe/end", $groupnode)->item(0); if ($end) { $coursemanagement->enddate = $end->nodeValue; } if (isset($coursemanagement->startdate) && $coursemanagement->startdate > 0 && isset($coursemanagement->enddate) && $coursemanagement->enddate > 0) { // The course validly has both start and end dates. $coursemanagement->courseid = $course->id; $coursemanagement->timemodified = time(); $DB->insert_record("eclass_course_management", $coursemanagement); } else { if (isset($coursemanagement->startdate) || isset($coursemanagement->enddate)) { // Something isn't right with the start or end date. throw new Exception('UAIMS: Course Creation without valid start or end date'); } } // No else needed. No actions required if the course is validly lacking both start and end dates. } else { // Update existing Course, if corresponding eclass_course_management exist. Otherwise, create a // corresponding eclass_course_management entry. $ecourse = $DB->get_record('course', array('idnumber' => $course->idnumber), '*', MUST_EXIST); $enableqrtoggle = $this->get_config('enableqrvisibilitytoggle'); // Disable or enable QR toggling. if (!isset($enableqrtoggle) || !$enableqrtoggle) { unset($course->visible); } else { // Otherwise update the startend dates in the eclass_course_management table from uaims documents. $coursemanagement = $DB->get_record('eclass_course_management', array('courseid' => $ecourse->id), $fields = 'id', $strictness = IGNORE_MISSING); // Get the start and end date for the course. $begin = $xpath->evaluate("timeframe/begin", $groupnode)->item(0); $end = $xpath->evaluate("timeframe/end", $groupnode)->item(0); // To avoid errors when course is created outside of uaims (doesn't have eclass_course_management entry), // create and set appropriate attributes of $coursemanagement that matches eclass_course_management // database schema (or atleast NOT NULL fields). $coursemanagementexist = $coursemanagement != false; if (!$coursemanagementexist) { $enrol = enrol_get_plugin('manual'); if ($audroleid = $DB->get_field('role', 'id', array('shortname' => 'auditor'))) { $enrol->add_instance($ecourse, array('roleid' => $audroleid)); } else { $enrol->add_instance($ecourse); } $coursemanagement = new stdClass(); // The test process_imsdoc_test::test_process_imsdoc_should_update_course_with_minimal_imsdoc // implies that one of the requirement of this module is to ignore updates/insert in the case where // course exist, yet no start/end date. (Note this differ from the case where the course don't exist // in which case the requirements of this module requires it to throw an exception. if ($begin and $end) { // Set appropriate eclass_course_management row attributes and finally insert it to db. $coursemanagement->courseid = $ecourse->id; $coursemanagement->startdate = $begin->nodeValue; $coursemanagement->enddate = $end->nodeValue; $coursemanagement->timemodified = time(); $DB->insert_record("eclass_course_management", $coursemanagement); } } else { // Since corresponding eclass_course_management entry exist, only we don't need all parameters // to be present. Either/both $begin/$end dates is/are sufficient. if ($begin) { $coursemanagement->startdate = $begin->nodeValue; } if ($end) { $coursemanagement->enddate = $end->nodeValue; } if (isset($coursemanagement->startdate) || isset($coursemanagement->enddate)) { // If one of them is there then do the update. if ($coursemanagement->startdate || $coursemanagement->enddate) { $coursemanagement->timemodified = time(); $DB->update_record("eclass_course_management", $coursemanagement); } } } } // The $course var should never have an 'id' attribute, but lets make sure. if (isset($course->id)) { unset($course->id); } $ecourse = $this->extend($ecourse, $course); $DB->update_record('course', $ecourse); $classname = context_helper::get_class_for_level(CONTEXT_COURSE); $context = $classname::instance($ecourse->id, IGNORE_MISSING); $classname = context_helper::get_class_for_level(CONTEXT_COURSECAT); $newparent = $classname::instance($ecourse->category, IGNORE_MISSING); $context->update_moved($newparent); if (!($instanceid = $DB->get_field('enrol', 'id', array('enrol' => 'manual', 'courseid' => $ecourse->id)))) { $enrol = enrol_get_plugin('manual'); if ($audroleid = $DB->get_field('role', 'id', array('shortname' => 'auditor'))) { $enrol->add_instance($ecourse, array('roleid' => $audroleid)); } else { $enrol->add_instance(${$ecourse}); } } } } else { $idnumber = $xpath->evaluate("sourcedid/id", $groupnode)->item(0); if ($idnumber) { $idnumber = htmlspecialchars_decode($idnumber->nodeValue); $course = $DB->get_record('course', array('idnumber' => $idnumber)); delete_course($course, false); } } }
/** * Remove a context record and any dependent entries, * removes context from static context cache too * * @deprecated since Moodle 2.2 * @see context_helper::delete_instance() or context::delete_content() * @param int $contextlevel * @param int $instanceid * @param bool $deleterecord false means keep record for now * @return bool returns true or throws an exception */ function delete_context($contextlevel, $instanceid, $deleterecord = true) { if ($deleterecord) { debugging('delete_context() is deprecated, please use context_helper::delete_instance() instead.', DEBUG_DEVELOPER); context_helper::delete_instance($contextlevel, $instanceid); } else { debugging('delete_context() is deprecated, please use $context->delete_content() instead.', DEBUG_DEVELOPER); $classname = context_helper::get_class_for_level($contextlevel); if ($context = $classname::instance($instanceid, IGNORE_MISSING)) { $context->delete_content(); } } return true; }
/** * Get the context instance as an object. This function will create the * context instance if it does not exist yet. * * @deprecated since 2.2, use context_course::instance() or other relevant class instead * @todo This will be deleted in Moodle 2.8, refer MDL-34472 * @param integer $contextlevel The context level, for example CONTEXT_COURSE, or CONTEXT_MODULE. * @param integer $instance The instance id. For $level = CONTEXT_COURSE, this would be $course->id, * for $level = CONTEXT_MODULE, this would be $cm->id. And so on. Defaults to 0 * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found; * MUST_EXIST means throw exception if no record or multiple records found * @return context The context object. */ function get_context_instance($contextlevel, $instance = 0, $strictness = IGNORE_MISSING) { debugging('get_context_instance() is deprecated, please use context_xxxx::instance() instead.', DEBUG_DEVELOPER); $instances = (array) $instance; $contexts = array(); $classname = context_helper::get_class_for_level($contextlevel); // we do not load multiple contexts any more, PAGE should be responsible for any preloading foreach ($instances as $inst) { $contexts[$inst] = $classname::instance($inst, $strictness); } if (is_array($instance)) { return $contexts; } else { return $contexts[$instance]; } }
/** * Tests for the enrol_uaims process_cohort_group_node function. * * Test to make sure that the cohort is created in the correct * category context. * * @package enrol_uaims * @copyright 2014 Anthony Radziszewski radzisze@ualberta.ca * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ public function test_process_cohort_group_node() { global $DB, $CFG; $this->resetAfterTest(true); $xmldoc = <<<DOC <?xml version="1.0" encoding="utf-8"?> <enterprise xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <group recstatus="1"> <sourcedid> <id>test_cohort_id</id> </sourcedid> <grouptype> <typevalue>Cohort</typevalue> </grouptype> <description> <short>Test Cohort</short> <long>Test Cohort</long> </description> <org /> <timeframe> <begin /> <end /> </timeframe> <enrollcontrol /> <relationship> <sourcedid> <id>UOFAB-AU</id> </sourcedid> </relationship> </group></enterprise> DOC; $enrol = new enrol_uaims_plugin(); $category = new stdClass(); $category->name = "test"; $category->description = "UOFAB-AU"; $category->idnumber = "UOFAB-AU"; $category->timemodified = time(); $category->id = $DB->insert_record('course_categories', $category); $classname = context_helper::get_class_for_level(CONTEXT_COURSECAT); $category->context = $classname::instance($category->id, IGNORE_MISSING); $category->context->mark_dirty(); $DB->update_record('course_categories', $category); fix_course_sortorder(); $enrol->process_imsdoc($xmldoc); // Make sure the cohort exists. $getcohort = $DB->get_record('cohort', array('idnumber' => "test_cohort_id"), $fields = '*'); $this->assertEquals($getcohort->idnumber, "test_cohort_id"); $this->assertEquals($getcohort->visible, 0); // Make sure the cohort contextid matches the id from the context table for the specified category. $getcategory = $DB->get_record('course_categories', array('idnumber' => "UOFAB-AU"), $fields = '*'); $this->assertEquals($getcategory->description, "UOFAB-AU"); $getcontext = $DB->get_record('context', array('contextlevel' => 40, 'instanceid' => $getcategory->id), $fields = '*'); $this->assertEquals($getcontext->instanceid, $getcategory->id); $this->assertEquals($getcontext->id, $getcohort->contextid); }
/** * Executes the search * * @global moodle_database $DB * @return int The number of results */ public final function search() { global $DB; if (!is_null($this->results)) { return $this->results; } $this->results = array(); $this->totalcount = 0; $contextlevel = $this->get_itemcontextlevel(); list($sql, $params) = $this->get_searchsql(); $blocksz = 5000; $offs = 0; // Get total number, to avoid some incorrect iterations $countsql = preg_replace('/ORDER BY.*/', '', $sql); $totalcourses = $DB->count_records_sql("SELECT COUNT(*) FROM ({$countsql}) sel", $params); // User to be checked is always the same (usually null, get it form first element) $firstcap = reset($this->requiredcapabilities); $userid = isset($firstcap['user']) ? $firstcap['user'] : null; // Extract caps to check, this saves us a bunch of iterations $requiredcaps = array(); foreach ($this->requiredcapabilities as $cap) { $requiredcaps[] = $cap['capability']; } // Iterate while we have records and haven't reached MAXRESULTS while ($totalcourses > $offs and $this->totalcount < self::$MAXRESULTS) { $resultset = $DB->get_records_sql($sql, $params, $offs, $blocksz); foreach ($resultset as $result) { context_instance_preload($result); $classname = context_helper::get_class_for_level($contextlevel); $context = $classname::instance($result->id); if (count($requiredcaps) > 0) { if (!has_all_capabilities($requiredcaps, $context, $userid)) { continue; } } $this->results[$result->id] = $result; $this->totalcount++; if ($this->totalcount >= self::$MAXRESULTS) { break; } } $offs += $blocksz; } return $this->totalcount; }
/** * Executes the search * * @global moodle_database $DB * @return int The number of results */ public final function search() { global $DB; if (!is_null($this->results)) { return $this->results; } $this->results = array(); $this->totalcount = 0; $contextlevel = $this->get_itemcontextlevel(); list($sql, $params) = $this->get_searchsql(); // Get total number, to avoid some incorrect iterations. $countsql = preg_replace('/ORDER BY.*/', '', $sql); $totalcourses = $DB->count_records_sql("SELECT COUNT(*) FROM ({$countsql}) sel", $params); if ($totalcourses > 0) { // User to be checked is always the same (usually null, get it from first element). $firstcap = reset($this->requiredcapabilities); $userid = isset($firstcap['user']) ? $firstcap['user'] : null; // Extract caps to check, this saves us a bunch of iterations. $requiredcaps = array(); foreach ($this->requiredcapabilities as $cap) { $requiredcaps[] = $cap['capability']; } // Iterate while we have records and haven't reached $this->maxresults. $resultset = $DB->get_recordset_sql($sql, $params); foreach ($resultset as $result) { context_helper::preload_from_record($result); $classname = context_helper::get_class_for_level($contextlevel); $context = $classname::instance($result->id); if (count($requiredcaps) > 0) { if (!has_all_capabilities($requiredcaps, $context, $userid)) { continue; } } // Check if we are over the limit. if ($this->totalcount + 1 > $this->maxresults) { $this->hasmoreresults = true; break; } // If not, then continue. $this->totalcount++; $this->results[$result->id] = $result; } $resultset->close(); } return $this->totalcount; }
/** * context * * a wrapper method to offer consistent API to get contexts * in Moodle 2.0 and 2.1, we use get_context_instance() function * in Moodle >= 2.2, we use static context_xxx::instance() method * * @param integer $contextlevel * @param integer $instanceid (optional, default=0) * @param int $strictness (optional, default=0 i.e. IGNORE_MISSING) * @return required context * @todo Finish documenting this function */ function hotpot_get_context($contextlevel, $instanceid = 0, $strictness = 0) { if (class_exists('context_helper')) { // use call_user_func() to prevent syntax error in PHP 5.2.x // return $classname::instance($instanceid, $strictness); $class = context_helper::get_class_for_level($contextlevel); return call_user_func(array($class, 'instance'), $instanceid, $strictness); } else { return get_context_instance($contextlevel, $instanceid); } }
// 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/>. /** * IMS Enterprise enrolments plugin settings and presets. * * @package enrol * @subpackage imsenterprise * @copyright 2010 Eugene Venter * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die; global $CFG; if ($ADMIN->fulltree) { require_once $CFG->dirroot . '/enrol/uaims/locallib.php'; $settings->add(new admin_setting_configcheckbox('enrol_uaims/enableautocourseopenclose', new lang_string('enableautocourseopenclose', 'enrol_uaims'), new lang_string('enableautocourseopenclosedesc', 'enrol_uaims'), 0)); $settings->add(new admin_setting_configcheckbox('enrol_uaims/enableqrvisibilitytoggle', new lang_string('enableqrvisibilitytoggle', 'enrol_uaims'), new lang_string('enableqrvisibilitytoggledesc', 'enrol_uaims'), 1)); if (!during_initial_install()) { $classname = context_helper::get_class_for_level(CONTEXT_COURSE); $coursecontext = $classname::instance(SITEID, IGNORE_MISSING); $assignableroles = get_assignable_roles($coursecontext); $assignableroles = array('0' => get_string('ignore', 'enrol_uaims')) + $assignableroles; $imsroles = new uaims_roles(); foreach ($imsroles->get_imsroles() as $imsrolenum => $imsrolename) { $settings->add(new admin_setting_configselect('enrol_uaims/imsrolemap' . $imsrolenum, format_string('"' . $imsrolename . '" (' . $imsrolenum . ')'), '', (int) $imsroles->determine_default_rolemapping($imsrolenum), $assignableroles)); } } }