function sermon_alphabetical_get_one_node($letterstartnumber) { global $CFG, $COURSE, $alphabet; if (empty($letterstartnumber)) { return ''; } $extrawhere = array(); for ($x = 0; $x < NUMBER_OF_PARTION_LETTERS; $x++) { $extrawhere[] = "name LIKE '{$alphabet[$letterstartnumber + $x]}%' "; } //get the relavant sermons for this sermon series $sql = "SELECT r.*, rs.datedelivered, rs.seriesname, rs.book, rs.beginchapter, cm.id as `cmid`,\n rs.guestspeaker, rs.guestspeakername, rs.hitcounter, rs.lastaccess \n FROM {$CFG->prefix}resource r \n JOIN {$CFG->prefix}resource_sermon rs ON rs.resourceid = r.id\n JOIN {$CFG->prefix}course_modules cm ON cm.instance = r.id \n WHERE r.type = 'sermon' AND r.course = {$COURSE->id} AND name != '' "; $sql .= !empty($extrawhere) ? ' AND (' . implode(' OR ', $extrawhere) . ')' : ''; $sql .= " ORDER BY r.name ASC"; $sermons = get_recordset_sql($sql); //loop through and make them into an array of objecst compatible for a json_encode $letters = array(); while (($sermon = rs_fetch_next_record($sermons)) !== false) { if (empty($sermon->seriesname)) { $sermon->seriesname = 'no name'; } $sermon->datedelivered = date('m-d-Y', $sermon->datedelivered); $sermonnode = new stdClass(); $sermonnode->attributes = new stdClass(); $sermonnode->attributes->id = $sermon->cmid . '~' . sermon_block_make_name_safe_for_id($sermon->seriesname); $sermonnode->attributes->class = 'leaf sermon'; if (!empty($sermon->reference)) { $sermonnode->attributes->class .= ' mp3 '; } else { if (!empty($sermon->referencesermontext)) { $sermonnode->attributes->class .= ' text '; } else { if (!empty($sermon->referencelesson)) { $sermonnode->attributes->class .= ' lesson '; } } } $sermonnode->attributes->class .= rs_fetch_record($sermons) === false ? ' last ' : ''; $sermonnode->data = get_string('sermonleaf', 'resource', $sermon); $letters[] = $sermonnode; } $letters = array_values($letters); if (empty($letters)) { return ''; } return $letters; }
function sermon_series_get_one_node($seriesname) { global $CFG, $COURSE; if (empty($seriesname)) { return ''; } //get the relavant sermons for this sermon series $sql = "SELECT r.*, rs.datedelivered, rs.seriesname, rs.book, rs.beginchapter, cm.id as `cmid`,\n rs.guestspeaker, rs.guestspeakername, rs.hitcounter, rs.lastaccess \n FROM {$CFG->prefix}resource r \n JOIN {$CFG->prefix}resource_sermon rs ON rs.resourceid = r.id\n JOIN {$CFG->prefix}course_modules cm ON cm.instance = r.id \n WHERE r.type = 'sermon' AND r.course = {$COURSE->id} AND name != '' AND rs.seriesname = '{$seriesname}'\n ORDER BY rs.seriesname ASC, rs.datedelivered DESC"; $sermons = get_recordset_sql($sql); //loop through and make them into an array of objecst compatible for a json_encode $series = array(); while (($sermon = rs_fetch_next_record($sermons)) !== false) { if (empty($sermon->seriesname)) { $sermon->seriesname = 'no name'; } //clean up some variable // $sermon->seriesname = stripslashes($sermon->seriesname); // $sermon->name = stripslashes($sermon->name); $sermon->datedelivered = date('m-d-Y', $sermon->datedelivered); $sermonnode = new stdClass(); $sermonnode->attributes = new stdClass(); $sermonnode->attributes->id = $sermon->cmid . '~' . sermon_block_make_name_safe_for_id($sermon->seriesname); $sermonnode->attributes->class = 'leaf sermon'; if (!empty($sermon->reference)) { $sermonnode->attributes->class .= ' mp3 '; } else { if (!empty($sermon->referencesermontext)) { $sermonnode->attributes->class .= ' text '; } else { if (!empty($sermon->referencelesson)) { $sermonnode->attributes->class .= ' lesson '; } } } $sermonnode->attributes->class .= rs_fetch_record($sermons) === false ? ' last ' : ''; $sermonnode->data = get_string('sermonleaf', 'resource', $sermon); $series[] = $sermonnode; } $series = array_values($series); if (empty($series)) { return ''; } return $series; }
/** * Reads any other information for a user from external database, * then returns it in an array * * @param string $username (with system magic quotes) * * @return array without magic quotes */ function get_userinfo($username) { global $CFG; $textlib = textlib_get_instance(); $extusername = $textlib->convert(stripslashes($username), 'utf-8', $this->config->extencoding); $authdb = $this->db_init(); //Array to map local fieldnames we want, to external fieldnames $selectfields = $this->db_attributes(); $result = array(); //If at least one field is mapped from external db, get that mapped data: if ($selectfields) { $select = ''; foreach ($selectfields as $localname => $externalname) { $select .= ", {$externalname} AS {$localname}"; } $select = 'SELECT ' . substr($select, 1); $sql = $select . " FROM {$this->config->table}" . " WHERE {$this->config->fielduser} = '" . $this->ext_addslashes($extusername) . "'"; if ($rs = $authdb->Execute($sql)) { if ($rs->RecordCount() == 1) { $fields_obj = rs_fetch_record($rs); foreach ($selectfields as $localname => $externalname) { $result[$localname] = $textlib->convert($fields_obj->{$localname}, $this->config->extencoding, 'utf-8'); } } rs_close($rs); } } $authdb->Close(); return $result; }
/** * Gets the default category in the most specific context. * If no categories exist yet then default ones are created in all contexts. * * @param array $contexts The context objects for this context and all parent contexts. * @return object The default category - the category in the course context */ function question_make_default_categories($contexts) { $toreturn = null; // If it already exists, just return it. foreach ($contexts as $key => $context) { if (!($categoryrs = get_recordset_select("question_categories", "contextid = '{$context->id}'", 'sortorder, name', '*', '', 1))) { error('error getting category record'); } else { if (!($category = rs_fetch_record($categoryrs))) { // Otherwise, we need to make one $category = new stdClass(); $contextname = print_context_name($context, false, true); $category->name = addslashes(get_string('defaultfor', 'question', $contextname)); $category->info = addslashes(get_string('defaultinfofor', 'question', $contextname)); $category->contextid = $context->id; $category->parent = 0; $category->sortorder = 999; // By default, all categories get this number, and are sorted alphabetically. $category->stamp = make_unique_id_code(); if (!($category->id = insert_record('question_categories', $category))) { error('Error creating a default category for context ' . print_context_name($context)); } } } if ($context->contextlevel == CONTEXT_COURSE) { $toreturn = clone $category; } } return $toreturn; }
function chat_get_latest_message($chatid, $groupid = 0) { /// Efficient way to extract just the latest message /// Uses ADOdb directly instead of get_record_sql() /// because the LIMIT command causes problems with /// the developer debugging in there. global $db, $CFG; if ($groupid) { $groupselect = " AND (groupid='{$groupid}' OR groupid='0')"; } else { $groupselect = ""; } if (!($rs = $db->SelectLimit("SELECT *\n FROM {$CFG->prefix}chat_messages\n WHERE chatid = '{$chatid}' {$groupselect}\n ORDER BY timestamp DESC", 1))) { return false; } if ($rs->RecordCount() == 1) { return rs_fetch_record($rs); } else { return false; // Found no records } }
/** * sync enrolments with database, create courses if required. * * @param object The role to sync for. If no role is specified, defaults are * used. */ function sync_enrolments($role = null) { global $CFG; global $db; error_reporting(E_ALL); // Connect to the external database $enroldb = $this->enrol_connect(); if (!$enroldb) { notify("enrol/database cannot connect to server"); return false; } if (isset($role)) { echo '=== Syncing enrolments for role: ' . $role->shortname . " ===\n"; } else { echo "=== Syncing enrolments for default role ===\n"; } // first, pack the sortorder... fix_course_sortorder(); list($have_role, $remote_role_name, $remote_role_value) = $this->role_fields($enroldb, $role); if (!$have_role) { if (!empty($CFG->enrol_db_defaultcourseroleid) and $role = get_record('role', 'id', $CFG->enrol_db_defaultcourseroleid)) { echo "=== Using enrol_db_defaultcourseroleid: {$role->id} ({$role->shortname}) ===\n"; } elseif (isset($role)) { echo "!!! WARNING: Role specified by caller, but no (or invalid) role configuration !!!\n"; } } // get enrolments per-course $sql = "SELECT DISTINCT {$CFG->enrol_remotecoursefield} " . " FROM {$CFG->enrol_dbtable} " . " WHERE {$CFG->enrol_remoteuserfield} IS NOT NULL" . (isset($remote_role_name, $remote_role_value) ? ' AND ' . $remote_role_name . ' = ' . $remote_role_value : ''); $rs = $enroldb->Execute($sql); if (!$rs) { trigger_error($enroldb->ErrorMsg() . ' STATEMENT: ' . $sql); return false; } if ($rs->EOF) { // no courses! outta here... return true; } begin_sql(); $extcourses = array(); while ($extcourse_obj = rs_fetch_next_record($rs)) { // there are more course records $extcourse_obj = (object) array_change_key_case((array) $extcourse_obj, CASE_LOWER); $extcourse = $extcourse_obj->{strtolower($CFG->enrol_remotecoursefield)}; array_push($extcourses, $extcourse); // does the course exist in moodle already? $course = false; $course = get_record('course', $CFG->enrol_localcoursefield, $extcourse); if (!is_object($course)) { if (empty($CFG->enrol_db_autocreate)) { // autocreation not allowed if (debugging('', DEBUG_ALL)) { error_log("Course {$extcourse} does not exist, skipping"); } continue; // next foreach course } // ok, now then let's create it! // prepare any course properties we actually have $course = new StdClass(); $course->{$CFG->enrol_localcoursefield} = $extcourse; $course->fullname = $extcourse; $course->shortname = $extcourse; if (!($newcourseid = $this->create_course($course, true) and $course = get_record('course', 'id', $newcourseid))) { error_log("Creating course {$extcourse} failed"); continue; // nothing left to do... } } $context = get_context_instance(CONTEXT_COURSE, $course->id); // If we don't have a proper role setup, then we default to the default // role for the current course. if (!$have_role) { $role = get_default_course_role($course); } // get a list of the student ids the are enrolled // in the external db -- hopefully it'll fit in memory... $extenrolments = array(); $sql = "SELECT {$CFG->enrol_remoteuserfield} " . " FROM {$CFG->enrol_dbtable} " . " WHERE {$CFG->enrol_remotecoursefield} = " . $enroldb->quote($extcourse) . ($have_role ? ' AND ' . $remote_role_name . ' = ' . $remote_role_value : ''); $crs = $enroldb->Execute($sql); if (!$crs) { trigger_error($enroldb->ErrorMsg() . ' STATEMENT: ' . $sql); return false; } if ($crs->EOF) { // shouldn't happen, but cover all bases continue; } // slurp results into an array while ($crs_obj = rs_fetch_next_record($crs)) { $crs_obj = (object) array_change_key_case((array) $crs_obj, CASE_LOWER); array_push($extenrolments, $crs_obj->{strtolower($CFG->enrol_remoteuserfield)}); } rs_close($crs); // release the handle // // prune enrolments to users that are no longer in ext auth // hopefully they'll fit in the max buffer size for the RDBMS // // TODO: This doesn't work perfectly. If we are operating without // roles in the external DB, then this doesn't handle changes of role // within a course (because the user is still enrolled in the course, // so NOT IN misses the course). // // When the user logs in though, their role list will be updated // correctly. // if (!$CFG->enrol_db_disableunenrol) { $to_prune = get_records_sql("\n SELECT ra.*\n FROM {$CFG->prefix}role_assignments ra\n JOIN {$CFG->prefix}user u ON ra.userid = u.id\n WHERE ra.enrol = 'database'\n AND ra.contextid = {$context->id}\n AND ra.roleid = " . $role->id . ($extenrolments ? " AND u.{$CFG->enrol_localuserfield} NOT IN (" . join(", ", array_map(array(&$db, 'quote'), $extenrolments)) . ")" : '')); if ($to_prune) { foreach ($to_prune as $role_assignment) { if (role_unassign($role->id, $role_assignment->userid, 0, $role_assignment->contextid)) { error_log("Unassigned {$role->shortname} assignment #{$role_assignment->id} for course {$course->id} (" . format_string($course->shortname) . "); user {$role_assignment->userid}"); } else { error_log("Failed to unassign {$role->shortname} assignment #{$role_assignment->id} for course {$course->id} (" . format_string($course->shortname) . "); user {$role_assignment->userid}"); } } } } // // insert current enrolments // bad we can't do INSERT IGNORE with postgres... // foreach ($extenrolments as $member) { // Get the user id and whether is enrolled in one fell swoop $sql = "\n SELECT u.id AS userid, ra.id AS enrolmentid\n FROM {$CFG->prefix}user u\n LEFT OUTER JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid\n AND ra.roleid = {$role->id}\n AND ra.contextid = {$context->id}\n WHERE u.{$CFG->enrol_localuserfield} = " . $db->quote($member) . " AND (u.deleted IS NULL OR u.deleted=0) "; $ers = $db->Execute($sql); if (!$ers) { trigger_error($db->ErrorMsg() . ' STATEMENT: ' . $sql); return false; } if ($ers->EOF) { // if this returns empty, it means we don't have the student record. // should not happen -- but skip it anyway trigger_error('weird! no user record entry?'); continue; } $user_obj = rs_fetch_record($ers); $userid = $user_obj->userid; $enrolmentid = $user_obj->enrolmentid; rs_close($ers); // release the handle if ($enrolmentid) { // already enrolled - skip continue; } if (role_assign($role->id, $userid, 0, $context->id, 0, 0, 0, 'database')) { error_log("Assigned role {$role->shortname} to user {$userid} in course {$course->id} (" . format_string($course->shortname) . ")"); } else { error_log("Failed to assign role {$role->shortname} to user {$userid} in course {$course->id} (" . format_string($course->shortname) . ")"); } } // end foreach member } // end while course records rs_close($rs); //Close the main course recordset // // prune enrolments to courses that are no longer in ext auth // // TODO: This doesn't work perfectly. If we are operating without // roles in the external DB, then this doesn't handle changes of role // within a course (because the user is still enrolled in the course, // so NOT IN misses the course). // // When the user logs in though, their role list will be updated // correctly. // if (!$CFG->enrol_db_disableunenrol) { $sql = "\n SELECT ra.roleid, ra.userid, ra.contextid\n FROM {$CFG->prefix}role_assignments ra\n JOIN {$CFG->prefix}context cn ON cn.id = ra.contextid\n JOIN {$CFG->prefix}course c ON c.id = cn.instanceid\n WHERE ra.enrol = 'database'\n AND cn.contextlevel = " . CONTEXT_COURSE . " " . ($have_role ? ' AND ra.roleid = ' . $role->id : '') . ($extcourses ? " AND c.{$CFG->enrol_localcoursefield} NOT IN (" . join(",", array_map(array(&$db, 'quote'), $extcourses)) . ")" : ''); $ers = $db->Execute($sql); if (!$ers) { trigger_error($db->ErrorMsg() . ' STATEMENT: ' . $sql); return false; } if (!$ers->EOF) { while ($user_obj = rs_fetch_next_record($ers)) { $user_obj = (object) array_change_key_case((array) $user_obj, CASE_LOWER); $roleid = $user_obj->roleid; $user = $user_obj->userid; $contextid = $user_obj->contextid; if (role_unassign($roleid, $user, 0, $contextid)) { error_log("Unassigned role {$roleid} from user {$user} in context {$contextid}"); } else { error_log("Failed unassign role {$roleid} from user {$user} in context {$contextid}"); } } rs_close($ers); // release the handle } } commit_sql(); // we are done now, a bit of housekeeping fix_course_sortorder(); $this->enrol_disconnect($enroldb); return true; }
/** * Gets the default category in the most specific context. * If no categories exist yet then default ones are created in all contexts. * * @param array $contexts The context objects for this context and all parent contexts. * @return object The default category - the category in the course context */ function question_make_default_categories($contexts) { static $preferredlevels = array(CONTEXT_COURSE => 4, CONTEXT_MODULE => 3, CONTEXT_COURSECAT => 2, CONTEXT_SYSTEM => 1); $toreturn = null; $preferredness = 0; // If it already exists, just return it. foreach ($contexts as $key => $context) { if (!($categoryrs = get_recordset_select("question_categories", "contextid = '{$context->id}'", 'sortorder, name', '*', '', 1))) { error('error getting category record'); } else { if (!($category = rs_fetch_record($categoryrs))) { // Otherwise, we need to make one $category = new stdClass(); $contextname = print_context_name($context, false, true); $category->name = addslashes(get_string('defaultfor', 'question', $contextname)); $category->info = addslashes(get_string('defaultinfofor', 'question', $contextname)); $category->contextid = $context->id; $category->parent = 0; $category->sortorder = 999; // By default, all categories get this number, and are sorted alphabetically. $category->stamp = make_unique_id_code(); if (!($category->id = insert_record('question_categories', $category))) { error('Error creating a default category for context ' . print_context_name($context)); } } } if ($preferredlevels[$context->contextlevel] > $preferredness && has_any_capability(array('moodle/question:usemine', 'moodle/question:useall'), $context)) { $toreturn = $category; $preferredness = $preferredlevels[$context->contextlevel]; } } if (!is_null($toreturn)) { $toreturn = clone $toreturn; } return $toreturn; }
public function current() { return empty($this->rs) ? null : (rs_EOF($this->rs) ? false : rs_fetch_record($this->rs)); }
function sermon_date_get_one_node($datecategory) { global $CFG, $COURSE, $alphabet; if (empty($datecategory)) { return ''; } //get the relevant sermons for these dates $sql = "SELECT r.*, rs.datedelivered, rs.seriesname, rs.book, rs.beginchapter, cm.id as `cmid`,\n rs.guestspeaker, rs.guestspeakername, rs.hitcounter, rs.lastaccess \n FROM {$CFG->prefix}resource r \n JOIN {$CFG->prefix}resource_sermon rs ON rs.resourceid = r.id\n JOIN {$CFG->prefix}course_modules cm ON cm.instance = r.id \n WHERE r.type = 'sermon' AND r.course = {$COURSE->id} AND name != '' "; $startdate = 0; $enddate = 0; switch ($datecategory) { case CURRENT_YEAR: $startdate = mktime(0, 0, 0, 1, 1, date('Y')); $enddate = time(); break; case PREVIOUS_YEAR: $startdate = mktime(0, 0, 0, 1, 1, date('Y') - 1); $enddate = mktime(0, 0, 0, 1, 1, date('Y')); break; case PREVIOUS_FIVE_YEARS: $startdate = mktime(0, 0, 0, 1, 1, date('Y') - 6); $enddate = mktime(0, 0, 0, 1, 1, date('Y') - 1); break; case OLDER: $startdate = 0; $enddate = mktime(0, 0, 0, 1, 1, date('Y') - 6); break; } $sql .= " AND rs.datedelivered >= '{$startdate}' AND rs.datedelivered < '{$enddate}' "; $sql .= " ORDER BY rs.datedelivered DESC"; $sermons = get_recordset_sql($sql); //loop through and make them into an array of objects compatible for a json_encode $datesermons = array(); while (($sermon = rs_fetch_next_record($sermons)) !== false) { if (empty($sermon->seriesname)) { $sermon->seriesname = 'no name'; } // $sermon->seriesname = stripslashes($sermon->seriesname); // $sermon->name = stripslashes($sermon->name); $sermon->datedelivered = date('m-d-Y', $sermon->datedelivered); $sermonnode = new stdClass(); $sermonnode->attributes = new stdClass(); $sermonnode->attributes->id = $sermon->cmid . '~' . sermon_block_make_name_safe_for_id($sermon->seriesname); $sermonnode->attributes->class = 'leaf sermon'; if (!empty($sermon->reference)) { $sermonnode->attributes->class .= ' mp3 '; } else { if (!empty($sermon->referencesermontext)) { $sermonnode->attributes->class .= ' text '; } else { if (!empty($sermon->referencelesson)) { $sermonnode->attributes->class .= ' lesson '; } } } $sermonnode->attributes->class .= rs_fetch_record($sermons) === false ? ' last ' : ''; $sermonnode->data = get_string('sermonleaf', 'resource', $sermon); $datesermons[] = $sermonnode; } $datesermons = array_values($datesermons); if (empty($datesermons)) { return ''; } return $datesermons; }