}
// If a course has been supplied in the URL, change the filters to show that one
- if($urlcourse > 0 && record_exists('course', 'id', $urlcourse)) {
+ if($urlcourse > 0 && $DB->record_exists('course', array('id'=>$urlcourse))) {
require_login($urlcourse, false);
if($urlcourse == SITEID) {
switch($action) {
case 'delete':
$title = get_string('deleteevent', 'calendar');
- $event = get_record('event', 'id', $eventid);
+ $event = $DB->get_record('event', array('id'=>$eventid));
if($event === false) {
print_error('invalidevent');
}
case 'edit':
$title = get_string('editevent', 'calendar');
- $event = get_record('event', 'id', $eventid);
+ $event = $DB->get_record('event', array('id'=>$eventid));
$repeats = optional_param('repeats', 0, PARAM_INT);
- if($event === false) {
+ if ($event === false) {
print_error('invalidevent');
}
- if(!calendar_edit_event_allowed($event)) {
+ if (!calendar_edit_event_allowed($event)) {
print_error('nopermissions');
}
- if($form = data_submitted()) {
+ if($form = data_submitted(false)) {
$form->name = clean_param(strip_tags($form->name,'<lang><span>'), PARAM_CLEAN);
$timestartoffset = 'timestart - '.($event->timestart - $form->timestart);
}
- execute_sql('UPDATE '.$CFG->prefix.'event SET '.
- 'name = '.addslashes($form->name).','.
- 'description = '.addslashes($form->description).','.
- 'timestart = '.$timestartoffset.','.
- 'timeduration = '.$form->timeduration.','.
- 'timemodified = '.time().' WHERE repeatid = '.$event->repeatid);
+ $sql = "UPDATE {event}
+ SET name = ?,
+ description = ?,
+ timestart = ?,
+ timeduration = ?,
+ timemodified = ?
+ WHERE repeatid = ?";
+ $params = array($form->name, $form->description, $timestartoffset, $form->timeduration, time(), $event->repeatid);
+
+ $DB->execute_sql($sql, $params);
/// Log the event update.
- $form->name = stripslashes($form->name); //To avoid double-slashes
add_to_log($form->courseid, 'calendar', 'edit all', 'event.php?action=edit&id='.$form->id, $form->name);
}
else {
// Update this
$form->timemodified = time();
- update_record('event', $form);
+ $DB->update_record('event', $form);
/// Log the event update.
- $form->name = stripslashes($form->name); //To avoid double-slashes
add_to_log($form->courseid, 'calendar', 'edit', 'event.php?action=edit&id='.$form->id, $form->name);
}
case 'new':
$title = get_string('newevent', 'calendar');
- $form = data_submitted();
+ $form = data_submitted(false);
if(!empty($form) && !empty($form->name)) {
$form->name = clean_text(strip_tags($form->name, '<lang><span>'));
$form->timemodified = time();
/// Get the event id for the log record.
- $eventid = insert_record('event', $form, true);
+ $eventid = $DB->insert_record('event', $form);
/// Use the event id as the repeatid to link repeat entries together
if ($form->repeat) {
$form->repeatid = $form->id = $eventid;
- update_record('event', $form); // update the row, to set its repeatid
+ $DB->update_record('event', $form); // update the row, to set its repeatid
}
/// Log the event entry.
- add_to_log($form->courseid, 'calendar', 'add', 'event.php?action=edit&id='.$eventid, stripslashes($form->name));
+ add_to_log($form->courseid, 'calendar', 'add', 'event.php?action=edit&id='.$eventid, $form->name);
if ($form->repeat) {
for($i = 1; $i < $form->repeats; $i++) {
$form->timestart += $dst_offset_prev - dst_offset_on($form->timestart);
/// Get the event id for the log record.
- $eventid = insert_record('event', $form, true);
+ $eventid = $DB->insert_record('event', $form);
/// Log the event entry.
- add_to_log($form->courseid, 'calendar', 'add', 'event.php?action=edit&id='.$eventid, stripslashes($form->name));
+ add_to_log($form->courseid, 'calendar', 'add', 'event.php?action=edit&id='.$eventid, $form->name);
}
}
// OK, now redirect to day view
if (!empty($SESSION->cal_course_referer)) {
// TODO: This is part of the Great $course Hack in Moodle. Replace it at some point.
- $course = get_record('course', 'id', $SESSION->cal_course_referer);
+ $course = $DB->get_record('course', array('id'=>$SESSION->cal_course_referer));
} else {
$course = $site;
}
echo '<table id="calendar">';
echo '<tr><td class="maincalendar">';
- switch($action) {
+ switch ($action) {
case 'delete':
$confirm = optional_param('confirm', 0, PARAM_INT);
$repeats = optional_param('repeats', 0, PARAM_INT);
- if($confirm) {
+ if ($confirm) {
// Kill it and redirect to day view
- if(($event = get_record('event', 'id', $eventid)) !== false) {
+ if(($event = $DB->get_record('event', array('id'=>$eventid))) !== false) {
- if($event->repeatid && $repeats) {
- delete_records('event', 'repeatid', $event->repeatid);
+ if ($event->repeatid && $repeats) {
+ $DB->delete_records('event', array('repeatid'=>$event->repeatid));
add_to_log($event->courseid, 'calendar', 'delete all', '', $event->name);
- }
- else {
- delete_records('event', 'id', $eventid);
+ } else {
+ $DB->delete_records('event', array('id'=>$eventid));
add_to_log($event->courseid, 'calendar', 'delete', '', $event->name);
}
}
$d = $eventtime['mday'];
$y = $eventtime['year'];
- if($event->repeatid) {
- $fetch = get_record_sql('SELECT 1, COUNT(id) AS repeatcount FROM '.$CFG->prefix.'event WHERE repeatid = '.$event->repeatid);
- $repeatcount = $fetch->repeatcount;
- }
- else {
+ if ($event->repeatid) {
+ $repeatcount = $DB->count_records('event', array('repeatid'=>$event->repeatid));
+ } else {
$repeatcount = 0;
}
if (!empty($form->courseid)) {
// TODO: This is part of the Great $course Hack in Moodle. Replace it at some point.
- $course = get_record('course', 'id', $form->courseid);
+ $course = $DB->get_record('course', array('id'=>$form->courseid));
} else {
$course = $site;
}
- if($event->repeatid) {
- $fetch = get_record_sql('SELECT 1, COUNT(id) AS repeatcount FROM '.$CFG->prefix.'event WHERE repeatid = '.$event->repeatid);
+ if ($event->repeatid) {
+ $repeatcount = $DB->count_records('event', array('repeatid'=>$event->repeatid));
$repeatcount = $fetch->repeatcount;
- }
- else {
+ } else {
$repeatcount = 0;
}
break;
case 'course':
$courseid = optional_param('courseid', 0, PARAM_INT);
- if(!record_exists('course', 'id', $courseid)) {
+ if (!$DB->record_exists('course', array('id'=>$courseid))) {
calendar_get_allowed_types($allowed);
$eventtype = 'select';
}
if ($courseid == 0) { // workaround by Dan for bug #6130
$courseid = SITEID;
}
- if (!$course = get_record('course', 'id', $courseid)) {
+ if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
print_error('invalidcourse');
}
function validate_form(&$form, &$err) {
+ global $DB;
$form->name = trim($form->name);
$form->description = trim($form->description);
}
if(!empty($form->courseid)) {
// Timestamps must be >= course startdate
- $course = get_record('course', 'id', $form->courseid);
+ $course = $DB->get_record('course', array('id'=>$form->courseid));
if($course === false) {
print_error('invalidcourse');
}
}
function calendar_add_event_allowed($event) {
- global $USER;
+ global $USER, $DB;
// can not be using guest account
if (empty($USER->id) or $USER->username == 'guest') {
// Allow users to add/edit group events if:
// 1) They have manageentries (= entries for whole course)
// 2) They have managegroupentries AND are in the group
- $group = get_record('groups', 'id', $event->groupid);
+ $group = $DB->get_record('groups', array('id'=>$event->groupid));
return $group && (
has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE, $group->courseid)) ||
(has_capability('moodle/calendar:managegroupentries', get_context_instance(CONTEXT_COURSE, $group->courseid))
<?php
// The following is a hack to fix bug 1488
- $course = get_record('course', 'id', ($form->courseid) ? $form->courseid : $site->id);
+ $course = $DB->get_record('course', array('id'=>($form->courseid ? $form->courseid : $site->id)));
?>
<form method="post" action="event.php" id="eventform">
<table cellpadding="5">
$mon = optional_param('cal_m', 0, PARAM_INT);
$yr = optional_param('cal_y', 0, PARAM_INT);
if ($courseid = optional_param('course', 0, PARAM_INT)) {
- $course = get_record('course', 'id', $courseid);
+ $course = $DB->get_record('course', array('id'=>$courseid));
}
require_login();
'type' => 'misc');
}
$navlinks[] = array('name' => get_string('calendar', 'calendar'),
- 'link' =>calendar_get_link_href(CALENDAR_URL.'view.php?view=upcoming&course='.$course.'&',
+ 'link' =>calendar_get_link_href(CALENDAR_URL.'view.php?view=upcoming&course='.$courseid.'&',
$now['mday'], $now['mon'], $now['year']),
'type' => 'misc');
$navlinks[] = array('name' => $pagetitle, 'link' => null, 'type' => 'misc');
$getvars = 'cal_d='.$day.'&cal_m='.$mon.'&cal_y='.$yr; // For filtering
echo '<div class="minicalendarblock">';
-echo calendar_top_controls('display', array('id' => $course, 'm' => $prevmon, 'y' => $prevyr));
+echo calendar_top_controls('display', array('id' => $courseid, 'm' => $prevmon, 'y' => $prevyr));
echo calendar_get_mini($courses, $groups, $users, $prevmon, $prevyr);
echo '</div><div class="minicalendarblock">';
-echo calendar_top_controls('display', array('id' => $course, 'm' => $mon, 'y' => $yr));
+echo calendar_top_controls('display', array('id' => $courseid, 'm' => $mon, 'y' => $yr));
echo calendar_get_mini($courses, $groups, $users, $mon, $yr);
echo '</div><div class="minicalendarblock">';
-echo calendar_top_controls('display', array('id' => $course, 'm' => $nextmon, 'y' => $nextyr));
+echo calendar_top_controls('display', array('id' => $courseid, 'm' => $nextmon, 'y' => $nextyr));
echo calendar_get_mini($courses, $groups, $users, $nextmon, $nextyr);
echo '</div>';
// Default value = 65 = 64 + 1 = 2^6 + 2^0 = Saturday & Sunday
define('CALENDAR_DEFAULT_WEEKEND', 65);
+//TODO: fix this ehm "not nice code at all"
+
// Fetch the correct values from admin settings/lang pack
// If no such settings found, use the above defaults
$firstday = isset($CFG->calendar_startwday) ? $CFG->calendar_startwday : get_string('firstdayofweek');
} else {
$popupcaption .= get_string('eventsfor', 'calendar', userdate($event_timestart, get_string('strftimedayshort')));
}
- $popupcontent = str_replace("'", "\'", htmlspecialchars($popupcontent));
- $popupcaption = str_replace("'", "\'", htmlspecialchars($popupcaption));
+ $popupcontent = addslashes_js($popupcontent);
+ $popupcaption = addslashes_js($popupcaption);
$popup = 'onmouseover="return overlib(\''.$popupcontent.'\', CAPTION, \''.$popupcaption.'\');" onmouseout="return nd();"';
return $popup;
}
function calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxevents, $fromtime=0) {
- global $CFG, $COURSE;
+ global $CFG, $COURSE, $DB;
$display = new stdClass;
$display->range = $daysinfuture; // How many days in the future we 'll look
if ($event->modulename == 'assignment'){
// TODO: rewrite this hack somehow
if (!calendar_edit_event_allowed($event)){ // cannot manage entries, eg. student
- if(!$assignment = get_record('assignment','id',$event->instance)){
+ if (!$assignment = $DB->get_record('assignment', array('id'=>$event->instance))) {
// print_error("invalidid", 'assignment');
continue;
}
* @return array of selected events or an empty array if there aren't any (or there was an error)
*/
function calendar_get_events($tstart, $tend, $users, $groups, $courses, $withduration=true, $ignorehidden=true) {
+ global $DB;
+
$whereclause = '';
// Quick test
if(is_bool($users) && is_bool($groups) && is_bool($courses)) {
$whereclause .= ' AND visible = 1';
}
- $events = get_records_select('event', $whereclause, 'timestart');
+ $events = $DB->get_records_select('event', $whereclause, null, 'timestart');
if ($events === false) {
$events = array();
}
}
function calendar_get_course_cached(&$coursecache, $courseid) {
- global $COURSE;
+ global $COURSE, $DB;
+
if (!isset($coursecache[$courseid])) {
if ($courseid == $COURSE->id) {
$coursecache[$courseid] = $COURSE;
} else {
- $coursecache[$courseid] = get_record('course', 'id', $courseid);
+ $coursecache[$courseid] = $DB->get_record('course', array('id'=>$courseid));
}
}
return $coursecache[$courseid];
}
function calendar_set_filters(&$courses, &$group, &$user, $courseeventsfrom = NULL, $groupeventsfrom = NULL, $ignorefilters = false) {
- global $SESSION, $USER, $CFG;
+ global $SESSION, $USER, $CFG, $DB;
// Insidious bug-wannabe: setting $SESSION->cal_courses_shown to $course->id would cause
// the code to function incorrectly UNLESS we convert it to an integer. One case where
// we probably should do some clean up and make sure that session is set to use the proper form
if (is_int($courseeventsfrom)) { // case of an int, e.g. calendar view page
$c = array();
- $c[$courseeventsfrom] = get_record('course', 'id', $courseeventsfrom);
+ $c[$courseeventsfrom] = $DB->get_record('course', array('id'=>$courseeventsfrom));
$courseeventsfrom = $c;
} else if (is_array($courseeventsfrom)) { // case of an array of ints, e.g. course home page
foreach ($courseeventsfrom as $i=>$courseid) { // TODO: this seems wrong, the array is often constructed as [courseid] => 1 ???
if (is_int($courseid)) {
- $courseeventsfrom[$i] = get_record('course', 'id', $courseid);
+ $courseeventsfrom[$i] = $DB->get_record('course', array('id'=>$courseid));
}
}
}
if (is_object($courseeventsfrom[$courseid])) { // SHOULD be set MDL-11221
$courserecord = $courseeventsfrom[$courseid];
} else {
- $courserecord = get_record('course', 'id', $courseid);
+ $courserecord = $DB->get_record('course', array('id'=>$courseid));
}
- $courserecord = get_record('course', 'id', $courseid);
+ $courserecord = $DB->get_record('course', array('id'=>$courseid));
if ($courserecord->groupmode != NOGROUPS || !$courserecord->groupmodeforce) {
$groupids[] = $courseid;
}
if (!empty($groupids)) {
$sql = "SELECT *
- FROM {$CFG->prefix}groups
+ FROM {groups}
WHERE courseid IN (".implode(',', $groupids).')';
- if ($grouprecords= get_records_sql($sql)) {
+ if ($grouprecords = $DB->get_records_sql($sql, null)) {
foreach ($grouprecords as $grouprecord) {
$grouparray[] = $grouprecord->id;
}
}
function calendar_edit_event_allowed($event) {
-
- global $USER;
+ global $USER, $DB;
// Must be logged in
if (!isloggedin()) {
// Allow users to add/edit group events if:
// 1) They have manageentries (= entries for whole course)
// 2) They have managegroupentries AND are in the group
- $group = get_record('groups', 'id', $event->groupid);
+ $group = $DB->get_record('groups', array('id'=>$event->groupid));
return $group && (
has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE, $group->courseid)) ||
(has_capability('moodle/calendar:managegroupentries', get_context_instance(CONTEXT_COURSE, $group->courseid))
}
function calendar_get_default_courses($ignoreref = false) {
- global $USER, $CFG, $SESSION;
+ global $USER, $CFG, $SESSION, $DB;
if(!empty($SESSION->cal_course_referer) && !$ignoreref) {
return array($SESSION->cal_course_referer => 1);
$courses = array();
if (has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM))) {
if (!empty($CFG->calendar_adminseesall)) {
- $courses = get_records_sql('SELECT id, 1 FROM '.$CFG->prefix.'course');
+ $courses = $DB->get_records_sql('SELECT id, 1 FROM {course}');
return $courses;
}
}
}
function calendar_get_allowed_types(&$allowed) {
- global $USER, $CFG, $SESSION;
+ global $USER, $CFG, $SESSION, $DB;
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
$allowed->user = has_capability('moodle/calendar:manageownentries', $sitecontext);
$allowed->groups = false; // This may change just below
$allowed->site = has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE, SITEID));
if(!empty($SESSION->cal_course_referer) && $SESSION->cal_course_referer != SITEID) {
- $course = get_record('course', 'id', $SESSION->cal_course_referer);
+ $course = $DB->get_record('course', array('id'=>$SESSION->cal_course_referer));
$coursecontext = get_context_instance(CONTEXT_COURSE, $SESSION->cal_course_referer);
if(has_capability('moodle/calendar:manageentries', $coursecontext)) {
require_once($CFG->dirroot.'/calendar/lib.php');
if (isset($SESSION->cal_course_referer)) {
- if (! $course = get_record('course', 'id', $SESSION->cal_course_referer)) {
+ if (! $course = $DB->get_record('course', array('id'=>$SESSION->cal_course_referer))) {
$course = get_site();
}
}
/// If data submitted, then process and store.
- if ($form = data_submitted()) {
+ if ($form = data_submitted(false)) {
foreach ($form as $preference => $value) {
switch ($preference) {
case 'timeformat':
calendar_set_referring_course(0);
}
else {
- if(get_record('course', 'id', $id) === false) {
+ if($DB->get_record('course', array('id'=>$id)) === false) {
// There is no such course
$SESSION->cal_courses_shown = array();
calendar_set_referring_course(0);
// If a course has been supplied in the URL, change the filters to show that one
if (!empty($courseid)) {
- if ($course = get_record('course', 'id', $courseid)) {
+ if ($course = $DB->get_record('course', array('id'=>$courseid))) {
if ($course->id == SITEID) {
// If coming from the home page, show all courses
$SESSION->cal_courses_shown = calendar_get_default_courses(true);
// Let's see if we are supposed to provide a referring course link
// but NOT for the "main page" course
if ($SESSION->cal_course_referer != SITEID &&
- ($shortname = get_field('course', 'shortname', 'id', $SESSION->cal_course_referer)) !== false) {
+ ($shortname = $DB->get_field('course', 'shortname', array('id'=>$SESSION->cal_course_referer))) !== false) {
require_login();
if (empty($course)) {
- $course = get_record('course', 'id', $SESSION->cal_course_referer); // Useful to have around
+ $course = $DB->get_record('course', array('id'=>$SESSION->cal_course_referer)); // Useful to have around
}
}
$getvars = 'from=month&cal_d='.$day.'&cal_m='.$mon.'&cal_y='.$yr; // For filtering
- $display = &New stdClass;
+ $display = new stdClass;
$display->minwday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
$display->maxwday = $display->minwday + 6;